1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class LancamentoVendasController extends AppController { |
4
|
|
|
|
5
|
|
|
public function salvar_lancamento($id_venda, $dados, $valor_total, $id_usuario, $orcamento=true) { |
6
|
|
|
$lancamento['venda_id'] = $id_venda; |
|
|
|
|
7
|
|
|
$lancamento['valor'] = $valor_total; |
8
|
|
|
|
9
|
|
|
if ($orcamento) { |
10
|
|
|
$lancamento['valor_pago'] = 0; |
11
|
|
|
} else { |
12
|
|
|
$lancamento['valor_pago'] = $valor_total; |
13
|
|
|
} |
14
|
|
|
|
15
|
|
|
$lancamento['data_pgt'] = date('Y-m-d'); |
16
|
|
|
$lancamento['ativo'] = 1; |
17
|
|
|
$lancamento['usuario_id'] = $id_usuario; |
18
|
|
|
$lancamento['forma_pagamento'] = $dados['forma_pagamento']; |
19
|
|
|
|
20
|
|
|
$this->LancamentoVenda->save($lancamento); |
21
|
|
|
|
22
|
|
|
return true; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
View Code Duplication |
public function cancelar($id_venda, $id_cliente) { |
|
|
|
|
26
|
|
|
$response = $this->LancamentoVenda->find('first', array( |
27
|
|
|
'conditions' => array( |
28
|
|
|
'LancamentoVenda.venda_id' => $id_venda |
29
|
|
|
) |
30
|
|
|
) |
31
|
|
|
); |
32
|
|
|
|
33
|
|
|
$this->LancamentoVenda->id = $response['LancamentoVenda']['id']; |
34
|
|
|
|
35
|
|
|
$this->LancamentoVenda->save( |
36
|
|
|
['valor_pago' => 0] |
37
|
|
|
); |
38
|
|
|
|
39
|
|
|
echo json_encode( |
40
|
|
|
[ |
41
|
|
|
'valor' => $response['LancamentoVenda']['valor'] |
42
|
|
|
] |
43
|
|
|
); |
44
|
|
|
exit; |
|
|
|
|
45
|
|
|
|
46
|
|
|
} |
47
|
|
|
|
48
|
|
View Code Duplication |
public function aprovar($id_venda, $id_cliente) { |
|
|
|
|
49
|
|
|
$response = $this->LancamentoVenda->find('first', array( |
50
|
|
|
'conditions' => array( |
51
|
|
|
'LancamentoVenda.venda_id' => $id_venda |
52
|
|
|
) |
53
|
|
|
) |
54
|
|
|
); |
55
|
|
|
|
56
|
|
|
$this->LancamentoVenda->id = $response['LancamentoVenda']['id']; |
57
|
|
|
|
58
|
|
|
$this->LancamentoVenda->save( |
59
|
|
|
['valor_pago' => $response['LancamentoVenda']['valor']] |
60
|
|
|
); |
61
|
|
|
|
62
|
|
|
echo json_encode( |
63
|
|
|
[ |
64
|
|
|
'valor' => $response['LancamentoVenda']['valor'] |
65
|
|
|
] |
66
|
|
|
); |
67
|
|
|
exit; |
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
} |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.