Skip to content

Commit

Permalink
Corrigindo faturar com valores em Vendas
Browse files Browse the repository at this point in the history
  • Loading branch information
cabralwms committed Jul 29, 2024
1 parent cba975e commit bde19bd
Showing 1 changed file with 33 additions and 20 deletions.
53 changes: 33 additions & 20 deletions application/controllers/Vendas.php
Original file line number Diff line number Diff line change
Expand Up @@ -508,9 +508,9 @@ public function faturar()
if ($this->form_validation->run('receita') == false) {
$this->data['custom_error'] = (validation_errors() ? '<div class="form_error">' . validation_errors() . '</div>' : false);
} else {
$venda_id = $this->input->post('vendas_id');
$vencimento = $this->input->post('vencimento');
$recebimento = $this->input->post('recebimento');
$venda_id = $this->input->post('vendas_id', true);
$vencimento = $this->input->post('vencimento', true);
$recebimento = $this->input->post('recebimento', true);

try {
$vencimento = explode('/', $vencimento);
Expand All @@ -521,42 +521,55 @@ public function faturar()
$recebimento = $recebimento[2] . '-' . $recebimento[1] . '-' . $recebimento[0];
}
} catch (Exception $e) {
$vencimento = date('Y/m/d');
$vencimento = date('Y-m-d');
}

$vendas = $this->vendas_model->getById($venda_id);
$valorTotal = $this->input->post('valor', true);
$desconto = $vendas->desconto ? $vendas->desconto : 0;
$valorDesconto = $desconto > 0 ? $valorTotal - $desconto : $valorTotal;

$data = [
'vendas_id' => $venda_id,
'descricao' => set_value('descricao'),
'valor' => $this->input->post('valor'),
'desconto' => $vendas->desconto,
'tipo_desconto' => $vendas->tipo_desconto,
'valor_desconto' => $vendas->valor_desconto,
'clientes_id' => $this->input->post('clientes_id'),
'valor' => $valorTotal,
'desconto' => $desconto,
'valor_desconto' => $valorDesconto,
'clientes_id' => $this->input->post('clientes_id', true),
'data_vencimento' => $vencimento,
'data_pagamento' => $recebimento,
'baixado' => $this->input->post('recebido') == 1 ? true : false,
'cliente_fornecedor' => set_value('cliente'),
'forma_pgto' => $this->input->post('formaPgto'),
'tipo' => $this->input->post('tipo'),
'forma_pgto' => $this->input->post('formaPgto', true),
'tipo' => $this->input->post('tipo', true),
'usuarios_id' => $this->session->userdata('id_admin'),
];

if ($this->vendas_model->add('lancamentos', $data) == true) {
$venda = $this->input->post('vendas_id');
$this->db->trans_begin();

if ($this->vendas_model->add('lancamentos', $data)) {
$this->db->set('faturado', 1);
$this->db->set('valorTotal', $this->input->post('valor'));
$this->db->set('valorTotal', $valorTotal);
$this->db->set('status', 'Faturado');
$this->db->where('idVendas', $venda_id);
$this->db->update('vendas');

log_info('Faturou uma venda.');

$this->session->set_flashdata('success', 'Venda faturada com sucesso!');
$json = ['result' => true];
echo json_encode($json);
exit();
if ($this->db->trans_status() === false) {
$this->db->trans_rollback();
$this->session->set_flashdata('error', 'Ocorreu um erro ao tentar faturar venda.');
$json = ['result' => false];
echo json_encode($json);
exit();
} else {
$this->db->trans_commit();
log_info('Faturou uma venda.');
$this->session->set_flashdata('success', 'Venda faturada com sucesso!');
$json = ['result' => true];
echo json_encode($json);
exit();
}
} else {
$this->db->trans_rollback();
$this->session->set_flashdata('error', 'Ocorreu um erro ao tentar faturar venda.');
$json = ['result' => false];
echo json_encode($json);
Expand Down

0 comments on commit bde19bd

Please sign in to comment.