|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
class ProdutoEstoqueController extends AppController { |
|
4
|
|
|
|
|
5
|
|
|
public function validar_estoque($produto, $quantidade) { |
|
6
|
|
|
if (empty($produto)) { |
|
7
|
|
|
return false; |
|
8
|
|
|
} |
|
9
|
|
|
|
|
10
|
|
|
$user_active = $this->getUserActive($produto[0]['Produto']['id_usuario']); |
|
11
|
|
|
|
|
12
|
|
|
if ($user_active[0]['Usuario']['sale_without_stock']) |
|
13
|
|
|
return true; |
|
14
|
|
|
|
|
15
|
|
View Code Duplication |
if ($produto[0]['Produto']['estoque'] <= 0) { |
|
16
|
|
|
$this->Session->setFlash('O Produto ' . $produto[0]['Produto']['name'] . ' selecionado não possui estoque disponivel'); |
|
17
|
|
|
return false; |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
|
|
21
|
|
View Code Duplication |
if ($produto[0]['Produto']['estoque'] < $quantidade) { |
|
22
|
|
|
$this->Session->setFlash('A quantidade para o produto ' . $produto[0]['Produto']['name'] . ' escolhida é maior do que a disponivel'); |
|
23
|
|
|
return false; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
return true; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
View Code Duplication |
public function getUserActive($id) { |
|
30
|
|
|
$this->loadModel('Usuario'); |
|
31
|
|
|
|
|
32
|
|
|
$user = $this->Usuario->find('all', |
|
33
|
|
|
array('conditions' => |
|
34
|
|
|
array('Usuario.id' => $id) |
|
35
|
|
|
) |
|
36
|
|
|
); |
|
37
|
|
|
|
|
38
|
|
|
return $user; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function diminuir_estoque_produto($produto_id, $quantidade) { |
|
42
|
|
|
if (!isset($produto_id) || !isset($quantidade) || !is_numeric($quantidade)) { |
|
43
|
|
|
return false; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
try { |
|
47
|
|
|
$this->loadModel('Produto'); |
|
48
|
|
|
|
|
49
|
|
|
$produto = $this->Produto->find('first', array( |
|
50
|
|
|
'conditions' => array('Produto.id' => $produto_id), |
|
51
|
|
|
'order' => array('Produto.id' => 'desc') |
|
52
|
|
|
) |
|
53
|
|
|
); |
|
54
|
|
|
|
|
55
|
|
|
$novo_estoque['estoque'] = $produto['Produto']['estoque'] - $quantidade; |
|
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
$this->Produto->id = $produto_id; |
|
58
|
|
|
if (!$this->Produto->save($novo_estoque)) { |
|
59
|
|
|
return false; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return true; |
|
63
|
|
|
} catch (Exception $e) { |
|
64
|
|
|
print_r($e); |
|
65
|
|
|
exit(); |
|
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function diminuir_estoque_produto_variacao($produto_id, $quantidade, $variacao) { |
|
70
|
|
|
if (!isset($produto_id) || !isset($quantidade) || !isset($variacao)) { |
|
71
|
|
|
return false; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
try { |
|
75
|
|
|
$this->loadModel('Variacao'); |
|
76
|
|
|
|
|
77
|
|
|
$variacao = $this->Variacao->find('first', array( |
|
78
|
|
|
'conditions' => array( |
|
79
|
|
|
'Variacao.produto_id' => $produto_id, |
|
80
|
|
|
'Variacao.ativo' => 1, |
|
81
|
|
|
'Variacao.nome_variacao' => $variacao |
|
82
|
|
|
) |
|
83
|
|
|
) |
|
84
|
|
|
); |
|
85
|
|
|
|
|
86
|
|
|
$novo_estoque['estoque'] = $variacao['Variacao']['estoque'] - $quantidade; |
|
|
|
|
|
|
87
|
|
|
|
|
88
|
|
|
$this->Variacao->id = $variacao['Variacao']['id']; |
|
89
|
|
|
if (!$this->Variacao->save($novo_estoque)) { |
|
90
|
|
|
return false; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return true; |
|
94
|
|
|
} catch (Exception $e) { |
|
95
|
|
|
print_r($e); |
|
96
|
|
|
exit(); |
|
|
|
|
|
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
} |
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
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey 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.