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