Completed
Push — master ( 8fc659...f29135 )
by Reginaldo
26:07
created

CaixaController::finalizar_caixa()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 0
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
class CaixaController extends AppController {
4
5 View Code Duplication
	public function iniciar_caixa() {
6
		$data = $this->request->data['caixa'];
7
8
		$data['usuario_id'] = $this->instancia;
9
		$data['ativo']		= 1;
10
11
		if (!$this->Caixa->save($data)) {
12
			$this->Session->setFlash('Ocorreu um erro ao abrir o caixa, tente novamente, caso persista contate o suporte');
13
			$this->redirect('/venda/adicionar_cadastro');
14
		}
15
16
		$this->Session->setFlash('Caixa aberto com sucesso!');
17
		$this->redirect('/venda/adicionar_cadastro');
18
	}
19
20
	public function finalizar_caixa() {
21
		$data = $this->request->data['caixa'];
22
23
		if (!$this->Caixa->save($data)) {
24
			$this->Session->setFlash('Ocorreu um erro ao fechar o caixa, tente novamente, caso persista contate o suporte');
25
			$this->redirect('/venda/adicionar_cadastro');
26
		}
27
28
		$this->Session->setFlash('Caixa fechado com sucesso!');
29
		$this->redirect('/venda/adicionar_cadastro');
30
	}
31
32
	public function carregar_fechamento_caixa_dia_ajax() 
33
	{
34
		$this->layout = 'ajax';
35
36
		$caixaAtual = $this->carregar_caixa_atual();
37
38
		$totalVendas = $this->carregar_total_vendas() + $caixaAtual['Caixa']['valor_inicial'];
39
40
		$vendido = $totalVendas - $caixaAtual['Caixa']['valor_inicial'];
41
42
		$response = [
43
			'total_vendas' => $totalVendas,
44
			'total_cartao' => $this->carregar_total_por_tipo('cartao'),
45
			'total_dinheiro' => $this->carregar_total_por_tipo('dinheiro'),
46
			'caixa_atual' => $caixaAtual,
47
			'vendido' => $vendido
48
		];
49
50
		echo json_encode($response);
51
		exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method carregar_fechamento_caixa_dia_ajax() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
52
	}
53
54
	public function carregar_caixa_atual()
55
	{
56
		$caixa = $this->Caixa->find('first', array(
57
				'conditions' => array(
58
					'Caixa.usuario_id' => $this->instancia,
59
					'Caixa.data_abertura >= ' => date('Y-m-d')
60
				)
61
			)
62
		);
63
64
		return $caixa;
65
	}
66
67
	public function carregar_total_por_tipo($tipo)
68
	{
69
		$this->loadModel('LancamentoVenda');
70
71
		$conditions = array(
72
			'conditions' => array(
73
				'LancamentoVenda.usuario_id' => $this->instancia,
74
			)
75
		);
76
77
		if ($tipo == 'cartao') {
78
			$conditions['conditions']['LancamentoVenda.forma_pagamento <> '] = 'dinheiro';
79
		} else {
80
			$conditions['conditions']['LancamentoVenda.forma_pagamento'] = 'dinheiro';
81
		}
82
83
		$LancamentoVendas = $this->LancamentoVenda->find('all', $conditions);	
84
85
		$total = 0;
86
87
		foreach ($LancamentoVendas as $i => $LancamentoVenda) {
88
			$total += $LancamentoVenda['LancamentoVenda']['valor_pago'];
89
		}
90
91
		return $total;
92
	}
93
94
	public function carregar_total_vendas()
95
	{
96
		$this->loadModel('Venda');
97
98
		$dateInit = date('Y-m-d');
99
		$dateEnd  = date('Y-m-d');
100
101
		$vendas = $this->Venda->find('all', array(
102
				'conditions' => array(
103
					'Venda.data_venda >= ' => $dateInit,
104
					'Venda.data_venda <= ' => $dateEnd,
105
					'Venda.id_usuario' => $this->instancia
106
				)
107
			)
108
		);
109
110
		$total = 0;
111
		foreach ($vendas as $i => $venda) {
112
			$total += $venda['Venda']['valor'];
113
		}
114
115
		return $total;
116
	}
117
118
}