1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
require_once(ROOT . DS . 'vendor' . DS . 'autoload.php'); |
4
|
|
|
|
5
|
|
|
use Dompdf\Dompdf; |
6
|
|
|
|
7
|
|
|
include 'ProdutoEstoqueController.php'; |
8
|
|
|
include 'VendaItensProdutoController.php'; |
9
|
|
|
include 'LancamentoVendasController.php'; |
10
|
|
|
include 'ImpressaoFiscalController.php'; |
11
|
|
|
|
12
|
|
|
class VendaController extends AppController { |
13
|
|
|
|
14
|
|
View Code Duplication |
public function pdv() { |
15
|
|
|
$this->layout = 'wadmin'; |
16
|
|
|
|
17
|
|
|
$this->loadModel('Produto'); |
18
|
|
|
|
19
|
|
|
$this->set('produtos', $this->Produto->find('all', |
20
|
|
|
array('conditions' => |
21
|
|
|
array('ativo' => 1, |
22
|
|
|
'id_usuario' => $this->instancia |
23
|
|
|
) |
24
|
|
|
) |
25
|
|
|
) |
26
|
|
|
); |
27
|
|
|
|
28
|
|
|
$this->loadModel('Cliente'); |
29
|
|
|
|
30
|
|
|
$this->set('clientes', $this->Cliente->find('all', |
31
|
|
|
array('conditions' => |
32
|
|
|
array('ativo' => 1, |
33
|
|
|
'id_usuario' => $this->instancia |
34
|
|
|
) |
35
|
|
|
) |
36
|
|
|
) |
37
|
|
|
); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function recuperar_dados_venda_ajax() { |
41
|
|
|
$this->layout = 'ajax'; |
42
|
|
|
|
43
|
|
|
$dados = $this->request->data('dados'); |
44
|
|
|
|
45
|
|
|
$this->loadModel('Produto'); |
46
|
|
|
|
47
|
|
|
$produto = $this->Produto->find('all', |
48
|
|
|
array('conditions' => |
49
|
|
|
array('ativo' => 1, |
50
|
|
|
'id_alias' => $dados['codigo_produto'] |
51
|
|
|
) |
52
|
|
|
) |
53
|
|
|
); |
54
|
|
|
|
55
|
|
|
if (empty($produto)) { |
56
|
|
|
echo json_encode(false); |
57
|
|
|
return false; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
echo json_encode($produto); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function listar_cadastros() { |
64
|
|
|
$this->layout = 'wadmin'; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function listar_cadastros_ajax() { |
|
|
|
|
68
|
|
|
$this->layout = 'ajax'; |
69
|
|
|
|
70
|
|
|
$aColumns = array( 'id', 'valor', 'forma_pagamento', 'data_venda', 'actions' ); |
71
|
|
|
|
72
|
|
|
$this->loadModel('LancamentoVenda'); |
73
|
|
|
|
74
|
|
|
$conditions = array('conditions' => |
75
|
|
|
array( |
76
|
|
|
'Venda.ativo' => 1, |
77
|
|
|
'Venda.id_usuario' => $this->instancia, |
78
|
|
|
'Venda.orcamento' => 0 |
79
|
|
|
) |
80
|
|
|
); |
81
|
|
|
|
82
|
|
|
$todasVendas = $this->Venda->find('all', |
83
|
|
|
$conditions, |
84
|
|
|
array('order' => array('Venda.id DESC')) |
85
|
|
|
); |
86
|
|
|
|
87
|
|
View Code Duplication |
if ( isset( $_GET['iDisplayStart'] ) && $_GET['iDisplayLength'] != '-1' ) |
88
|
|
|
{ |
89
|
|
|
$conditions['offset'] = $_GET['iDisplayStart']; |
90
|
|
|
$conditions['limit'] = $_GET['iDisplayLength']; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
View Code Duplication |
if ( isset( $_GET['iSortCol_0'] ) ) |
94
|
|
|
{ |
95
|
|
|
for ( $i=0 ; $i < intval( $_GET['iSortingCols'] ) ; $i++ ) |
96
|
|
|
{ |
97
|
|
|
if ( $_GET[ 'bSortable_' . intval($_GET['iSortCol_' . $i]) ] == "true" ) |
98
|
|
|
{ |
99
|
|
|
$conditions['order'] = array('Venda.' . $aColumns[intval($_GET['iSortCol_' . $i])] => $_GET['sSortDir_'.$i]); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
View Code Duplication |
if ( isset( $_GET['sSearch'] ) && !empty( $_GET['sSearch'] ) ) |
105
|
|
|
{ |
106
|
|
|
$conditions['conditions']['Venda.id LIKE '] = '%' . $_GET['sSearch'] . '%'; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$vendas = $this->Venda->find('all', $conditions); |
110
|
|
|
|
111
|
|
|
$output = array( |
112
|
|
|
"sEcho" => intval($_GET['sEcho']), |
113
|
|
|
"iTotalDisplayRecords" => count($todasVendas), |
114
|
|
|
"iTotalRecords" => count($vendas), |
115
|
|
|
"aaData" => array() |
116
|
|
|
); |
117
|
|
|
|
118
|
|
|
foreach ($vendas as $venda) { |
119
|
|
|
$row = array(); |
120
|
|
|
|
121
|
|
|
for ( $i=0 ; $i < count($aColumns) ; $i++ ) |
|
|
|
|
122
|
|
|
{ |
123
|
|
|
if ($aColumns[$i] == "forma_pagamento") { |
124
|
|
|
$lancamento = $this->LancamentoVenda->find('first', array( |
125
|
|
|
'conditions' => array( |
126
|
|
|
'LancamentoVenda.venda_id' => $venda['Venda']['id'] |
127
|
|
|
) |
128
|
|
|
) |
129
|
|
|
); |
130
|
|
|
|
131
|
|
|
$value = (isset($lancamento['LancamentoVenda']['forma_pagamento'])) ? $lancamento['LancamentoVenda']['forma_pagamento'] : array(); |
132
|
|
|
|
133
|
|
|
if (isset($value) && !empty($value)) |
134
|
|
|
$value = str_replace('_', ' ', $value); |
135
|
|
|
|
136
|
|
|
if (isset($value) && !empty($value)) |
137
|
|
|
$value = ucwords($value); |
138
|
|
|
} else if ($aColumns[$i] == "actions") { |
139
|
|
|
$value = '<a href="javascript:printNotaNaoFiscal(' . $venda['Venda']['id'] . ');" target="_blank" class="btn btn-info">'; |
140
|
|
|
$value .= '<i class="fa fa-file-text" aria-hidden="true"></i>'; |
141
|
|
|
$value .= '</a> '; |
142
|
|
|
|
143
|
|
|
$value .= ' <a onclick="remover_venda(' . $venda['Venda']['id'] . ');" id="' . $venda['Venda']['id'] . '" type="button" class="btn btn-danger"><i class="fa fa-times"></i></a>'; |
144
|
|
|
} else if ($aColumns[$i] == "valor") { |
145
|
|
|
$value = 'R$ ' . number_format($venda['Venda'][$aColumns[$i]], 2, ',', '.'); |
146
|
|
|
} else { |
147
|
|
|
$value = $venda['Venda'][$aColumns[$i]]; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$row[] = $value; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
$btEdit = '<a class="btn btn-info" href="/produto/editar_cadastro/' . $venda['Venda']['id'] . '"><i class="fa fa-pencil"></i></a>'; |
154
|
|
|
|
155
|
|
|
$row[] = $btEdit; |
156
|
|
|
|
157
|
|
|
$output['aaData'][] = $row; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
echo json_encode($output); |
161
|
|
|
exit; |
|
|
|
|
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public function adicionar_cadastro() { |
165
|
|
|
$this->layout = 'wadmin'; |
166
|
|
|
|
167
|
|
|
$this->loadModel('Cliente'); |
168
|
|
|
|
169
|
|
|
$this->set('clientes', $this->Cliente->find('all', |
170
|
|
|
array('conditions' => |
171
|
|
|
array('ativo' => 1, |
172
|
|
|
'id_usuario' => $this->instancia |
173
|
|
|
) |
174
|
|
|
) |
175
|
|
|
) |
176
|
|
|
); |
177
|
|
|
|
178
|
|
|
$this->loadModel('Produto'); |
179
|
|
|
|
180
|
|
|
$this->set('produtos', $this->Produto->find('all', |
181
|
|
|
array('conditions' => |
182
|
|
|
array('ativo' => 1, |
183
|
|
|
'id_usuario' => $this->instancia |
184
|
|
|
) |
185
|
|
|
) |
186
|
|
|
) |
187
|
|
|
); |
188
|
|
|
|
189
|
|
|
$this->set('vendaId', $this->Session->read('UltimoIdVendaSalvo')); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
public function conveter_venda($vendaId) { |
193
|
|
|
$this->layout = 'wadmin'; |
194
|
|
|
|
195
|
|
|
$this->loadModel('Cliente'); |
196
|
|
|
|
197
|
|
|
$this->set('clientes', $this->Cliente->find('all', |
198
|
|
|
array('conditions' => |
199
|
|
|
array('ativo' => 1, |
200
|
|
|
'id_usuario' => $this->instancia |
201
|
|
|
) |
202
|
|
|
) |
203
|
|
|
) |
204
|
|
|
); |
205
|
|
|
|
206
|
|
|
$this->loadModel('Produto'); |
207
|
|
|
|
208
|
|
|
$this->set('produtos', $this->Produto->find('all', |
209
|
|
|
array('conditions' => |
210
|
|
|
array('ativo' => 1, |
211
|
|
|
'id_usuario' => $this->instancia |
212
|
|
|
) |
213
|
|
|
) |
214
|
|
|
) |
215
|
|
|
); |
216
|
|
|
|
217
|
|
|
$this->set('venda', $this->Venda->find('all', |
218
|
|
|
array('conditions' => |
219
|
|
|
array( |
220
|
|
|
'Venda.ativo' => 1, |
221
|
|
|
'Venda.id' => $vendaId, |
222
|
|
|
'id_usuario' => $this->instancia |
223
|
|
|
) |
224
|
|
|
) |
225
|
|
|
) |
226
|
|
|
); |
227
|
|
|
|
228
|
|
|
$this->loadModel('VendaItensProduto'); |
229
|
|
|
|
230
|
|
|
$venda_produtos = $this->VendaItensProduto->find('all', |
231
|
|
|
array('conditions' => |
232
|
|
|
array( |
233
|
|
|
'VendaItensProduto.ativo' => 1, |
234
|
|
|
'VendaItensProduto.id' => $vendaId |
235
|
|
|
) |
236
|
|
|
) |
237
|
|
|
); |
238
|
|
|
|
239
|
|
|
$this->loadModel('Produto'); |
240
|
|
|
|
241
|
|
|
$produtos = []; |
242
|
|
|
foreach ($venda_produtos as $i => $venda_produto) |
243
|
|
|
{ |
244
|
|
|
$produto = $this->Produto->find('all', |
245
|
|
|
array('conditions' => |
246
|
|
|
array( |
247
|
|
|
'Produto.ativo' => 1, |
248
|
|
|
'Produto.id' => $venda_produto['VendaItensProduto']['produto_id'] |
249
|
|
|
) |
250
|
|
|
) |
251
|
|
|
); |
252
|
|
|
|
253
|
|
View Code Duplication |
if ($produto[0]['Produto']['estoque'] <= 0) |
254
|
|
|
{ |
255
|
|
|
$this->Session->setFlash('O produto (' . $produto[0]['Produto']['nome'] .') não tem mais estoque disponivel!'); |
256
|
|
|
continue; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
$produtos[$i] = $produto[0]['Produto']; |
260
|
|
|
$produtos[$i]['quantidade'] = $venda_produto['VendaItensProduto']['quantidade_produto']; |
261
|
|
|
|
262
|
|
|
$total = $produtos[$i]['preco'] * $venda_produto['VendaItensProduto']['quantidade_produto']; |
263
|
|
|
|
264
|
|
|
$produtos[$i]['preco'] = number_format($produtos[$i]['preco'], 2, ',', '.'); |
265
|
|
|
$produtos[$i]['total'] = number_format($total, 2, ',', '.'); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
$this->set('venda_produtos', $produtos); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
public function s_adicionar_cadastro() { |
272
|
|
|
$dados_venda = $this->request->data('venda'); |
273
|
|
|
$dados_lancamento = $this->request->data('lancamento'); |
274
|
|
|
$produtos = $this->request->data('produto'); |
275
|
|
|
|
276
|
|
|
if (!$this->validar_itens_venda($produtos) && !$dados_venda['orcamento']) { |
277
|
|
|
$this->Session->setFlash('Algum produto adicionado não possui estoque disponivel'); |
278
|
|
|
$this->redirect('/venda/adicionar_cadastro'); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
$dados_venda['valor'] = $this->calcular_valor_venda($produtos); |
282
|
|
|
$dados_venda['custo'] = $this->calcular_custo_venda($produtos); |
283
|
|
|
|
284
|
|
|
$salvar_venda = $this->salvar_venda($produtos, $dados_lancamento, $dados_venda); |
|
|
|
|
285
|
|
|
|
286
|
|
|
if (!$salvar_venda) { |
287
|
|
|
$this->Session->setFlash('Ocorreu um erro ao salvar a venda tente novamento'); |
288
|
|
|
$this->redirect('/venda/adicionar_cadastro'); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
$this->Session->write('UltimoIdVendaSalvo', $salvar_venda['id']); |
292
|
|
|
|
293
|
|
|
$this->Session->setFlash('Venda salva com sucesso'); |
294
|
|
|
$this->redirect('/venda/adicionar_cadastro'); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
View Code Duplication |
public function calcular_valor_venda($produtos) { |
298
|
|
|
$this->loadModel('Produto'); |
299
|
|
|
|
300
|
|
|
(float) $preco = 0.00; |
301
|
|
|
|
302
|
|
|
foreach ($produtos as $indice => $item) { |
303
|
|
|
$produto = $this->Produto->find('all', |
304
|
|
|
array('conditions' => |
305
|
|
|
array('Produto.id' => $item['id_produto']) |
306
|
|
|
) |
307
|
|
|
); |
308
|
|
|
|
309
|
|
|
$preco += $produto[0]['Produto']['preco'] * $item['quantidade']; |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
return $preco; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
View Code Duplication |
public function calcular_custo_venda($produtos) { |
316
|
|
|
$this->loadModel('Produto'); |
317
|
|
|
|
318
|
|
|
(float) $custo = 0.00; |
319
|
|
|
foreach ($produtos as $indice => $item) { |
320
|
|
|
$produto = $this->Produto->find('all', |
321
|
|
|
array('conditions' => |
322
|
|
|
array('Produto.id' => $item['id_produto']) |
323
|
|
|
) |
324
|
|
|
); |
325
|
|
|
|
326
|
|
|
$custo += $produto[0]['Produto']['custo'] * $item['quantidade']; |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
return $custo; |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
public function validar_itens_venda($produtos) { |
333
|
|
|
$this->loadModel('Produto'); |
334
|
|
|
|
335
|
|
|
foreach ($produtos as $indice => $item) { |
336
|
|
|
$produto = $this->Produto->find('all', |
337
|
|
|
array('conditions' => |
338
|
|
|
array('Produto.id' => $item['id_produto']) |
339
|
|
|
) |
340
|
|
|
); |
341
|
|
|
|
342
|
|
|
$objProdutoEstoqueController = new ProdutoEstoqueController(); |
343
|
|
|
|
344
|
|
|
if (!$objProdutoEstoqueController->validar_estoque($produto, $item['quantidade'])) { |
345
|
|
|
return false; |
346
|
|
|
} |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
return true; |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
public function salvar_venda($produtos, $lancamento, $informacoes, $usuario_id) { |
353
|
|
|
unset($informacoes['id_cliente']); |
354
|
|
|
|
355
|
|
|
$informacoes['data_venda'] = date('Y-m-d'); |
356
|
|
|
$informacoes['id_usuario'] = $this->instancia != 'winners' ? $this->instancia : $usuario_id; |
357
|
|
|
$informacoes['ativo'] = 1; |
358
|
|
|
$informacoes['desconto'] = (float) @$informacoes['desconto']; |
359
|
|
|
$informacoes['valor'] = $informacoes['valor'] - $informacoes['desconto']; |
360
|
|
|
$informacoes['orcamento'] = @$informacoes['orcamento']; |
361
|
|
|
|
362
|
|
|
if (!$this->Venda->save($informacoes)) { |
363
|
|
|
$this->Session->setFlash('Ocorreu algum erro ao salvar a venda'); |
364
|
|
|
return false; |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
$id_venda = $this->Venda->getLastInsertId(); |
368
|
|
|
|
369
|
|
|
$objVendaItensProdutoController = new VendaItensProdutoController(); |
370
|
|
|
|
371
|
|
|
if ($objVendaItensProdutoController->adicionar_itens_venda($id_venda, $produtos, $informacoes['orcamento']) === false) { |
372
|
|
|
return false; |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
$objLancamentoVendasController = new LancamentoVendasController(); |
376
|
|
|
|
377
|
|
|
if ($objLancamentoVendasController->salvar_lancamento($id_venda, $lancamento, $informacoes['valor'], $informacoes['id_usuario']) === false) { |
378
|
|
|
return false; |
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
return array('status' => true, 'id' => $id_venda); |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
public function relatorio_diario() { |
385
|
|
|
include(APP . 'Vendor/PHPExcel/PHPExcel.php'); |
386
|
|
|
include(APP . 'Vendor/PHPExcel/PHPExcel/IOFactory.php'); |
387
|
|
|
|
388
|
|
|
$objPHPExcel = new PHPExcel(); |
389
|
|
|
// Definimos o estilo da fonte |
390
|
|
|
$objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true); |
391
|
|
|
|
392
|
|
|
$objPHPExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(40); |
393
|
|
|
|
394
|
|
|
// Criamos as colunas |
395
|
|
|
$objPHPExcel->setActiveSheetIndex(0) |
|
|
|
|
396
|
|
|
->setCellValue('A1', "Valor Venda") |
397
|
|
|
->setCellValue('B1', "Custo Médio ") |
398
|
|
|
->setCellValue("C1", "Valor Lucro") |
399
|
|
|
->setCellValue('D1', "ID Venda" ); |
400
|
|
|
|
401
|
|
|
|
402
|
|
|
$vendas = $this->Venda->find('all', |
403
|
|
|
array('conditions' => array( |
404
|
|
|
'AND' => array( |
405
|
|
|
'Venda.ativo' => 1, |
406
|
|
|
'Venda.id_usuario' => $this->instancia, |
407
|
|
|
'Venda.data_venda' => date('Y-m-d') |
408
|
|
|
) |
409
|
|
|
) |
410
|
|
|
) |
411
|
|
|
); |
412
|
|
|
|
413
|
|
|
$i = 2; |
414
|
|
|
foreach ($vendas as $key => $venda) { |
415
|
|
|
$objPHPExcel->setActiveSheetIndex(0) |
416
|
|
|
->setCellValue('A'.$i, 'R$ ' . $venda['Venda']['valor']) |
417
|
|
|
->setCellValue('B'.$i, 'R$ ' . $venda['Venda']['custo']) |
418
|
|
|
->setCellValue('C'.$i, 'R$ ' . $venda['Venda']['valor'] - $venda['Venda']['custo']) |
419
|
|
|
->setCellValue('D'.$i, $venda['Venda']['id']); |
420
|
|
|
$i++; |
421
|
|
|
} |
422
|
|
|
|
423
|
|
|
// Podemos renomear o nome das planilha atual, lembrando que um único arquivo pode ter várias planilhas |
424
|
|
|
$objPHPExcel->getActiveSheet()->setTitle('Listagem de vendas'); |
425
|
|
|
|
426
|
|
|
// Cabeçalho do arquivo para ele baixar |
427
|
|
|
header('Content-Type: application/vnd.ms-excel'); |
428
|
|
|
header('Content-Disposition: attachment;filename="relatorio_vendas_'.date('d-m-Y').'.xls"'); |
429
|
|
|
header('Cache-Control: max-age=0'); |
430
|
|
|
// Se for o IE9, isso talvez seja necessário |
431
|
|
|
header('Cache-Control: max-age=1'); |
432
|
|
|
|
433
|
|
|
// Acessamos o 'Writer' para poder salvar o arquivo |
434
|
|
|
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); |
435
|
|
|
|
436
|
|
|
// Salva diretamente no output, poderíamos mudar arqui para um nome de arquivo em um diretório ,caso não quisessemos jogar na tela |
437
|
|
|
$objWriter->save('php://output'); |
438
|
|
|
|
439
|
|
|
exit; |
|
|
|
|
440
|
|
|
} |
441
|
|
|
|
442
|
|
|
public function recoverDataToDashboardOneWeek($id_usuario){ |
443
|
|
|
$vendas = $this->Venda->find('all', |
444
|
|
|
array('conditions' => |
445
|
|
|
array( |
446
|
|
|
'Venda.ativo' => 1, |
447
|
|
|
'Venda.id_usuario' => $id_usuario, |
448
|
|
|
), |
449
|
|
|
'limit' => 6 |
450
|
|
|
) |
451
|
|
|
); |
452
|
|
|
|
453
|
|
|
$resposta = []; |
454
|
|
|
foreach ($vendas as $i => $venda) { |
455
|
|
|
$resposta[] = (float) number_format($venda['Venda']['valor'], 2, '.', ','); |
456
|
|
|
} |
457
|
|
|
|
458
|
|
|
$resposta = [ |
459
|
|
|
'name' => 'Valor', |
460
|
|
|
'data' => $resposta |
461
|
|
|
]; |
462
|
|
|
|
463
|
|
|
return json_encode($resposta); |
464
|
|
|
} |
465
|
|
|
|
466
|
|
View Code Duplication |
public function excluir_cadastro() { |
467
|
|
|
$this->layout = 'ajax'; |
468
|
|
|
|
469
|
|
|
$id = $this->request->data('id'); |
470
|
|
|
|
471
|
|
|
$dados = array('ativo' => '0'); |
472
|
|
|
$parametros = array('id' => $id); |
473
|
|
|
|
474
|
|
|
if ($this->Venda->updateAll($dados, $parametros)) { |
475
|
|
|
echo json_encode(true); |
476
|
|
|
} else { |
477
|
|
|
echo json_encode(false); |
478
|
|
|
} |
479
|
|
|
} |
480
|
|
|
|
481
|
|
|
public function imprimir_nota_nao_fiscal($id) { |
482
|
|
|
$this->loadModel('LancamentoVenda'); |
483
|
|
|
$this->loadModel('VendaItensProduto'); |
484
|
|
|
$this->loadModel('Produto'); |
485
|
|
|
$this->loadModel('Usuario'); |
486
|
|
|
|
487
|
|
|
$ImpressaoFiscalController = new ImpressaoFiscalController; |
488
|
|
|
|
489
|
|
|
$dados_venda = $this->Venda->find('first', |
490
|
|
|
array('conditions' => |
491
|
|
|
array( |
492
|
|
|
'Venda.ativo' => 1, |
493
|
|
|
'Venda.id' => $id |
494
|
|
|
) |
495
|
|
|
) |
496
|
|
|
); |
497
|
|
|
|
498
|
|
|
$usuario = $this->Usuario->find('first', |
499
|
|
|
array('conditions' => |
500
|
|
|
array( |
501
|
|
|
'Usuario.id' => $dados_venda['Venda']['id_usuario'] |
502
|
|
|
) |
503
|
|
|
) |
504
|
|
|
); |
505
|
|
|
|
506
|
|
|
$ImpressaoFiscalController->userName = $usuario['Usuario']['nome']; |
|
|
|
|
507
|
|
|
|
508
|
|
|
$dados_lancamento = $this->LancamentoVenda->find('first', |
509
|
|
|
array('conditions' => |
510
|
|
|
array( |
511
|
|
|
'LancamentoVenda.ativo' => 1, |
512
|
|
|
'LancamentoVenda.venda_id' => $id |
513
|
|
|
) |
514
|
|
|
) |
515
|
|
|
); |
516
|
|
|
|
517
|
|
|
$produtos = $this->VendaItensProduto->find('all', |
518
|
|
|
array('conditions' => |
519
|
|
|
array( |
520
|
|
|
'VendaItensProduto.venda_id' => $id |
521
|
|
|
) |
522
|
|
|
) |
523
|
|
|
); |
524
|
|
|
|
525
|
|
|
$itens = array(); |
526
|
|
|
$totalGeral = 0.00; |
527
|
|
|
foreach ($produtos as $i => $item) { |
528
|
|
|
$produto = $this->Produto->find('first', |
529
|
|
|
array('conditions' => |
530
|
|
|
array('Produto.id' => $item['VendaItensProduto']['produto_id']) |
531
|
|
|
) |
532
|
|
|
); |
533
|
|
|
|
534
|
|
|
$total = $produto['Produto']['preco'] * $item['VendaItensProduto']['quantidade_produto']; |
535
|
|
|
|
536
|
|
|
$totalGeral += $total; |
537
|
|
|
|
538
|
|
|
$ImpressaoFiscalController->corpoTxt .= "" |
|
|
|
|
539
|
|
|
. "Produto: " . $produto['Produto']['nome'] |
540
|
|
|
. "\nQuantidade: " . $item['VendaItensProduto']['quantidade_produto'] |
541
|
|
|
. "\nPreço: R$ " . number_format($produto['Produto']['preco'], 2, ',', '.') |
542
|
|
|
. "\nTotal: R$ " . number_format($total, 2, ',', '.') |
543
|
|
|
. "\n--------------------------\n"; |
544
|
|
|
} |
545
|
|
|
|
546
|
|
|
$desconto = $totalGeral - $dados_venda['Venda']['valor']; |
547
|
|
|
|
548
|
|
|
$ImpressaoFiscalController->corpoTxt .= "Valor Total: " . number_format($totalGeral, 2, ',', '.') . "\n\n"; |
|
|
|
|
549
|
|
|
$ImpressaoFiscalController->corpoTxt .= "Valor Pago: R$ " . number_format($dados_venda['Venda']['valor'], 2, ',', '.') . "\n"; |
|
|
|
|
550
|
|
|
$ImpressaoFiscalController->corpoTxt .= "Desconto: R$ " . number_format($desconto, 2, ',', '.') . "\n\n"; |
|
|
|
|
551
|
|
|
$ImpressaoFiscalController->corpoTxt .= "Forma de Pagamento: " . $dados_lancamento['LancamentoVenda']['forma_pagamento'] . "\n\n"; |
|
|
|
|
552
|
|
|
|
553
|
|
|
$file = $ImpressaoFiscalController->gerar_arquivo(); |
554
|
|
|
|
555
|
|
|
echo json_encode(array('file' => $file)); |
556
|
|
|
|
557
|
|
|
exit; |
|
|
|
|
558
|
|
|
} |
559
|
|
|
|
560
|
|
|
public function clear_session_venda($id) |
|
|
|
|
561
|
|
|
{ |
562
|
|
|
$this->Session->write('UltimoIdVendaSalvo', null); |
563
|
|
|
exit; |
|
|
|
|
564
|
|
|
} |
565
|
|
|
|
566
|
|
|
public function relatorio() { |
|
|
|
|
567
|
|
|
$this->layout = 'wadmin'; |
568
|
|
|
|
569
|
|
|
$from = $_GET['from']; |
570
|
|
|
$to = $_GET['to']; |
571
|
|
|
|
572
|
|
|
$this->loadModel('Venda'); |
573
|
|
|
$this->loadModel('LancamentoVenda'); |
574
|
|
|
|
575
|
|
|
$conditions = array( |
576
|
|
|
'conditions' => array( |
577
|
|
|
'Venda.id_usuario' => $this->instancia, |
578
|
|
|
'Venda.data_venda >=' => $from, |
579
|
|
|
'Venda.data_venda <=' => $to, |
580
|
|
|
'Venda.orcamento <>' => 1 |
581
|
|
|
) |
582
|
|
|
); |
583
|
|
|
|
584
|
|
|
$vendas = $this->Venda->find('all', $conditions); |
585
|
|
|
|
586
|
|
|
$valorTotalVendasPeriodo = $this->calcularValorTotalVendas($vendas); |
587
|
|
|
|
588
|
|
|
$totalCustoPeriodo = $this->calcularTotalCustoProdutosPeriodo($vendas); |
589
|
|
|
|
590
|
|
|
$lancamentos = array(); |
591
|
|
|
|
592
|
|
|
foreach ($vendas as $i => $venda) { |
593
|
|
|
$lancamento = $this->LancamentoVenda->find('first', array( |
594
|
|
|
'conditions' => array( |
595
|
|
|
'LancamentoVenda.venda_id' => $venda['Venda']['id'] |
596
|
|
|
) |
597
|
|
|
) |
598
|
|
|
); |
599
|
|
|
|
600
|
|
|
if (!empty($lancamento)) |
601
|
|
|
$lancamentos[] = $lancamento; |
602
|
|
|
} |
603
|
|
|
|
604
|
|
|
$valorTotalPgt = $this->calcularTotalVendas($lancamentos); |
605
|
|
|
|
606
|
|
|
$this->set('dinheiro', $valorTotalPgt['dinheiro']); |
607
|
|
|
$this->set('cartao_credito', $valorTotalPgt['cartao_credito']); |
608
|
|
|
$this->set('cartao_debito', $valorTotalPgt['cartao_debito']); |
609
|
|
|
$this->set('valorTotalVendasPeriodo', $valorTotalVendasPeriodo); |
610
|
|
|
$this->set('totalCustoPeriodo', $totalCustoPeriodo); |
611
|
|
|
$this->set('totalLucro', $valorTotalVendasPeriodo - $totalCustoPeriodo); |
612
|
|
|
} |
613
|
|
|
|
614
|
|
|
public function calcularTotalVendas($lancamentos) |
615
|
|
|
{ |
616
|
|
|
$response = array(); |
617
|
|
|
foreach ($lancamentos as $i => $lancamento) { |
618
|
|
|
@$response[$lancamento['LancamentoVenda']['forma_pagamento']] += $lancamento['LancamentoVenda']['valor_pago']; |
|
|
|
|
619
|
|
|
} |
620
|
|
|
|
621
|
|
|
return $response; |
622
|
|
|
} |
623
|
|
|
|
624
|
|
View Code Duplication |
public function calcularTotalCustoProdutosPeriodo($vendas) |
625
|
|
|
{ |
626
|
|
|
$valor = 0.00; |
627
|
|
|
|
628
|
|
|
foreach ($vendas as $i => $venda) { |
629
|
|
|
$valor += $venda['Venda']['custo']; |
630
|
|
|
} |
631
|
|
|
|
632
|
|
|
return $valor; |
633
|
|
|
} |
634
|
|
|
|
635
|
|
View Code Duplication |
public function calcularValorTotalVendas($vendas) |
636
|
|
|
{ |
637
|
|
|
$valor = 0.00; |
638
|
|
|
|
639
|
|
|
foreach ($vendas as $i => $venda) { |
640
|
|
|
$valor += $venda['Venda']['valor']; |
641
|
|
|
} |
642
|
|
|
|
643
|
|
|
return $valor; |
644
|
|
|
} |
645
|
|
|
|
646
|
|
|
} |
647
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: