1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
require_once(ROOT . DS . 'vendor' . DS . 'autoload.php'); |
4
|
|
|
|
5
|
|
|
use Dompdf\Dompdf; |
6
|
|
|
|
7
|
|
|
class OrcamentoController extends AppController |
8
|
|
|
{ |
9
|
|
|
|
10
|
|
|
public function listar_cadastros() |
11
|
|
|
{ |
12
|
|
|
$this->layout = 'wadmin'; |
13
|
|
|
|
14
|
|
|
$this->loadModel('Venda'); |
15
|
|
|
|
16
|
|
|
$this->set('vendas', $this->Venda->find('all', |
17
|
|
|
array('conditions' => |
18
|
|
|
array( |
19
|
|
|
'ativo' => 1, |
20
|
|
|
'id_usuario' => $this->instancia, |
21
|
|
|
'orcamento' => 1 |
22
|
|
|
) |
23
|
|
|
) |
24
|
|
|
) |
25
|
|
|
); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function excluir_cadastro($vendaId) |
29
|
|
|
{ |
30
|
|
|
$this->layout = 'ajax'; |
31
|
|
|
|
32
|
|
|
$this->loadModel('Venda'); |
33
|
|
|
|
34
|
|
|
$dados['ativo'] = 0; |
|
|
|
|
35
|
|
|
$dados['id_usuario'] = $this->instancia; |
36
|
|
|
$dados['id'] = $vendaId; |
37
|
|
|
|
38
|
|
|
echo json_encode($this->Venda->save($dados)); |
39
|
|
|
|
40
|
|
|
exit; |
|
|
|
|
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function salvar_orcamento() |
44
|
|
|
{ |
45
|
|
|
$this->layout = 'wadmin'; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function pdf($vendaId) |
49
|
|
|
{ |
50
|
|
|
$this->loadModel('Venda'); |
51
|
|
|
$this->loadModel('VendaItensProduto'); |
52
|
|
|
|
53
|
|
|
$dompdf = new Dompdf(); |
54
|
|
|
|
55
|
|
|
$dadosVenda = $this->Venda->find('all', |
56
|
|
|
array('conditions' => |
57
|
|
|
array( |
58
|
|
|
'Venda.id' => $vendaId |
59
|
|
|
) |
60
|
|
|
) |
61
|
|
|
); |
62
|
|
|
|
63
|
|
|
$produtosVenda = $this->VendaItensProduto->find('all', |
64
|
|
|
array('conditions' => |
65
|
|
|
array( |
66
|
|
|
'VendaItensProduto.venda_id' => $vendaId |
67
|
|
|
) |
68
|
|
|
) |
69
|
|
|
); |
70
|
|
|
|
71
|
|
|
$html = $this->pegar_venda_como_html($dadosVenda, $produtosVenda); |
72
|
|
|
|
73
|
|
|
$dompdf->loadHtml($html); |
74
|
|
|
|
75
|
|
|
$dompdf->set_paper('a4'); |
|
|
|
|
76
|
|
|
|
77
|
|
|
$dompdf->render(); |
78
|
|
|
|
79
|
|
|
$dompdf->stream(); |
80
|
|
|
|
81
|
|
|
exit; |
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function pegar_venda_como_html($dadosVenda, $produtosVenda) |
85
|
|
|
{ |
86
|
|
|
$html = ''; |
87
|
|
|
$html .= '<html>'; |
88
|
|
|
$html .= '<head>'; |
89
|
|
|
$html .= ' <title></title>'; |
90
|
|
|
$html .= '</head>'; |
91
|
|
|
$html .= '<body>'; |
92
|
|
|
$html .= ''; |
93
|
|
|
$html .= ' <table style="background-color: #cacaca;" width="100%" valign="center" align="center">'; |
94
|
|
|
$html .= ' <tr align="center">'; |
95
|
|
|
$html .= ' <td>'; |
96
|
|
|
$html .= ' <h2>Orçamento Pedido #' . $dadosVenda[0]['Venda']['id'] . '</h2>'; |
97
|
|
|
$html .= ' </td>'; |
98
|
|
|
$html .= ' </tr> '; |
99
|
|
|
$html .= ' </table>'; |
100
|
|
|
$html .= ' <br>'; |
101
|
|
|
$html .= ' <table width="100%" valign="center" align="center">'; |
102
|
|
|
$html .= ' <tr style="background-color: #ccc;">'; |
103
|
|
|
$html .= ' <td>Total: </td>'; |
104
|
|
|
$html .= ' <td>R$ ' . number_format($dadosVenda[0]['Venda']['valor'], 2, ',', '.') . '</td>'; |
105
|
|
|
$html .= ' </tr>'; |
106
|
|
|
$html .= ' <tr style="background-color: #ccc;">'; |
107
|
|
|
$html .= ' <td>Valor a Pagar: </td>'; |
108
|
|
|
$html .= ' <td>R$ ' . number_format($dadosVenda[0]['Venda']['valor'], 2, ',', '.') . '</td>'; |
109
|
|
|
$html .= ' </tr>'; |
110
|
|
|
$html .= ' </table>'; |
111
|
|
|
$html .= ' <br>'; |
112
|
|
|
$html .= ' <table width="100%" valign="center" align="center">'; |
113
|
|
|
$html .= ' <tr style="background-color: #cacaca;" align="center">'; |
114
|
|
|
$html .= ' <td>'; |
115
|
|
|
$html .= ' <h2>Produtos</h2>'; |
116
|
|
|
$html .= ' </td>'; |
117
|
|
|
$html .= ' </tr> '; |
118
|
|
|
$html .= ' </table>'; |
119
|
|
|
$html .= ' <br>'; |
120
|
|
|
$html .= ' <table width="100%" valign="center" align="center">'; |
121
|
|
|
$html .= ' <tr>'; |
122
|
|
|
$html .= ' <td>'; |
123
|
|
|
$html .= ' <table width="100%" valign="center" align="center">'; |
124
|
|
|
$html .= ' <tr style="background-color: #cacaca;">'; |
125
|
|
|
$html .= ' <td>Nome Produto</td>'; |
126
|
|
|
$html .= ' <td>Quantidade</td>'; |
127
|
|
|
$html .= ' <td>Valor</td>'; |
128
|
|
|
$html .= ' </tr>'; |
129
|
|
|
$html .= ''; |
130
|
|
|
$desconto = 0; |
131
|
|
|
foreach ($produtosVenda as $i => $produto) { |
132
|
|
|
$produtoInfos = $this->getInfoProdutos($produto['VendaItensProduto']['produto_id']); |
133
|
|
|
|
134
|
|
|
$total = $produtoInfos['Produto']['preco'] * $produto['VendaItensProduto']['quantidade_produto']; |
135
|
|
|
$total = number_format($total, 2, ',', '.'); |
136
|
|
|
|
137
|
|
|
$html .= ' <tr>'; |
138
|
|
|
$html .= ' <td>' . $produtoInfos['Produto']['nome'] . '</td>'; |
139
|
|
|
$html .= ' <td>' . $produto['VendaItensProduto']['quantidade_produto'] . '</td>'; |
140
|
|
|
$html .= ' <td>R$ ' . $total . '</td>'; |
141
|
|
|
$html .= ' </tr>'; |
142
|
|
|
} |
143
|
|
|
$html .= ' </table>'; |
144
|
|
|
$html .= ' </td>'; |
145
|
|
|
$html .= ' </tr>'; |
146
|
|
|
$html .= ' </table>'; |
147
|
|
|
$html .= ' <br>'; |
148
|
|
|
/* $html .= ' <table width="100%" valign="center" align="center">'; |
149
|
|
|
$html .= ' <tr style="background-color: #ccc;">'; |
150
|
|
|
$html .= ' <td>Total: </td>'; |
151
|
|
|
$html .= ' <td>R$ ' . number_format($desconto, 2, ',', '.') . '</td>'; |
152
|
|
|
$html .= ' </tr>'; |
153
|
|
|
$html .= ' </table>'; */ |
154
|
|
|
$html .= ''; |
155
|
|
|
$html .= '</body>'; |
156
|
|
|
$html .= '</html>'; |
157
|
|
|
|
158
|
|
|
return $html; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function getInfoProdutos($produtoId) |
162
|
|
|
{ |
163
|
|
|
$this->loadModel('Produto'); |
164
|
|
|
|
165
|
|
|
$response = $this->Produto->find('first', |
166
|
|
|
array('conditions' => array( |
167
|
|
|
'Produto.id' => $produtoId |
168
|
|
|
) |
169
|
|
|
) |
170
|
|
|
); |
171
|
|
|
|
172
|
|
|
return $response; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
} |
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.