1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class CupomController extends AppController { |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
public function listar_cadastros() |
7
|
|
|
{ |
8
|
|
|
$cupons = $this->Cupom->find('all', |
9
|
|
|
array('conditions' => |
10
|
|
|
array('ativo' => 1, |
11
|
|
|
'usuario_id' => $this->instancia |
12
|
|
|
) |
13
|
|
|
) |
14
|
|
|
); |
15
|
|
|
|
16
|
|
|
$this->set('cupons', $cupons); |
17
|
|
|
|
18
|
|
|
$this->layout = 'wadmin'; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function adicionar_cupom() |
22
|
|
|
{ |
23
|
|
|
$this->layout = 'wadmin'; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
View Code Duplication |
public function s_adicionar_cupom() |
27
|
|
|
{ |
28
|
|
|
$dados = $this->request->data('dados'); |
29
|
|
|
$dados['ativo'] = 1; |
30
|
|
|
$dados['usuario_id'] = $this->instancia; |
31
|
|
|
|
32
|
|
|
if ($this->Cupom->save($dados)) { |
33
|
|
|
$this->Session->setFlash('Cupom criado com sucesso!'); |
34
|
|
|
return $this->redirect('/cupom/listar_cadastros'); |
35
|
|
|
} else { |
36
|
|
|
$this->Session->setFlash('Ocorreu algum erro ao criar o cupom!'); |
37
|
|
|
return $this->redirect('/cupom/listar_cadastros'); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
View Code Duplication |
public function editar_cupom($id) |
42
|
|
|
{ |
43
|
|
|
$this->set('cupom', $this->Cupom->find('all', |
44
|
|
|
array('conditions' => |
45
|
|
|
array( |
46
|
|
|
'ativo' => 1, |
47
|
|
|
'usuario_id' => $this->instancia, |
48
|
|
|
'id' => $id |
49
|
|
|
) |
50
|
|
|
) |
51
|
|
|
) |
52
|
|
|
); |
53
|
|
|
|
54
|
|
|
$this->layout = 'wadmin'; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
View Code Duplication |
public function s_editar_cupom($id) |
58
|
|
|
{ |
59
|
|
|
$dados = $this->request->data('dados'); |
60
|
|
|
|
61
|
|
|
$this->Cupom->id = $id; |
62
|
|
|
|
63
|
|
|
if ($this->Cupom->save($dados)) { |
64
|
|
|
$this->Session->setFlash('Cupom editado com sucesso!'); |
65
|
|
|
return $this->redirect('/cupom/listar_cadastros'); |
66
|
|
|
} else { |
67
|
|
|
$this->Session->setFlash('Ocorreu algum erro ao editar o cupom!'); |
68
|
|
|
return $this->redirect('/cupom/listar_cadastros'); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
View Code Duplication |
public function excluir_cupom($id) |
73
|
|
|
{ |
74
|
|
|
$dados['ativo'] = 0; |
|
|
|
|
75
|
|
|
|
76
|
|
|
$this->Cupom->id = $id; |
77
|
|
|
|
78
|
|
|
if ($this->Cupom->save($dados)) { |
79
|
|
|
$this->Session->setFlash('Cupom excluido com sucesso!'); |
80
|
|
|
return $this->redirect('/cupom/listar_cadastros'); |
81
|
|
|
} else { |
82
|
|
|
$this->Session->setFlash('Ocorreu algum erro ao excluir o cupom!'); |
83
|
|
|
return $this->redirect('/cupom/listar_cadastros'); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param nome cupom |
90
|
|
|
* @param usuario id |
91
|
|
|
* @return false ou o array com os dados do cupom |
92
|
|
|
**/ |
93
|
|
|
public function procurar_cupom($nome_cupom, $usuario_id) |
94
|
|
|
{ |
95
|
|
|
if (empty($nome_cupom) && !isset($nome_cupom)) |
96
|
|
|
{ |
97
|
|
|
return false; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$cupom = $this->Cupom->find('all', |
101
|
|
|
array('conditions' => |
102
|
|
|
array( |
103
|
|
|
'ativo' => 1, |
104
|
|
|
'usuario_id' => $usuario_id, |
105
|
|
|
'nome' => $nome_cupom |
106
|
|
|
) |
107
|
|
|
) |
108
|
|
|
); |
109
|
|
|
|
110
|
|
|
if (empty($cupom)) |
111
|
|
|
{ |
112
|
|
|
return false; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $cupom[0]; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param valor original float |
120
|
|
|
* @return float com o valor final ja considerando o valor do cupom |
121
|
|
|
**/ |
122
|
|
|
public function calcular_desconto_cupom($valor_original, $valor_desconto_cupom, $tipo_cupom) |
123
|
|
|
{ |
124
|
|
|
if (empty($valor_original) && !isset($valor_original)) |
125
|
|
|
{ |
126
|
|
|
return false; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
if ($tipo_cupom == 'porcento') |
130
|
|
|
{ |
131
|
|
|
return $valor_original - (($valor_original * $valor_desconto_cupom) / 100); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
if ($tipo_cupom == 'fixo') |
135
|
|
|
{ |
136
|
|
|
return $valor_original - $valor_desconto_cupom; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return (float) $valor_original; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param $cupom String nome do cupom a ser utilizado |
144
|
|
|
* @param $valor float com o valor original da venda |
145
|
|
|
* @return $novo_valor float com o novo valor da venda |
|
|
|
|
146
|
|
|
**/ |
147
|
|
|
public function utilizar_cupom($cupom, $valor_original, $usuario_id) |
148
|
|
|
{ |
149
|
|
|
if (empty($cupom) && !isset($cupom)) |
150
|
|
|
{ |
151
|
|
|
return false; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
$cupom = $this->procurar_cupom($cupom, $usuario_id); |
155
|
|
|
|
156
|
|
|
if (!$cupom) |
157
|
|
|
{ |
158
|
|
|
return false; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
$novo_valor = $this->calcular_desconto_cupom($valor_original, $cupom['Cupom']['valor'], $cupom['Cupom']['tipo']); |
162
|
|
|
|
163
|
|
|
return $novo_valor; |
164
|
|
|
} |
165
|
|
|
} |
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.