carregar_fechamento_caixa_dia_ajax()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 13
nc 1
nop 0
dl 0
loc 21
rs 9.3142
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' => (float) $totalVendas,
44
			'total_cartao' => (float) $this->carregar_total_por_tipo('cartao'),
45
			'total_dinheiro' => (float) $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
			'joins' => array(
73
			    array(
74
			        'table' => 'vendas',
75
			        'alias' => 'Venda',
76
			        'type' => 'LEFT',
77
			        'conditions' => array(
78
			            'LancamentoVenda.venda_id = Venda.id',
79
			        ),
80
			    )
81
			),
82
			'conditions' => array(
83
				'LancamentoVenda.usuario_id' => $this->instancia,
84
				'LancamentoVenda.data_pgt' => date('Y-m-d'),
85
				'Venda.orcamento <> ' => 1
86
			)
87
		);
88
89
		if ($tipo == 'cartao') {
90
			$conditions['conditions']['LancamentoVenda.forma_pagamento <> '] = 'dinheiro';
91
		} else {
92
			$conditions['conditions']['LancamentoVenda.forma_pagamento'] = 'dinheiro';
93
		}
94
95
		$LancamentoVendas = $this->LancamentoVenda->find('all', $conditions);	
96
		
97
		$total = 0;
98
99
		foreach ($LancamentoVendas as $i => $LancamentoVenda) {
100
			$total += $LancamentoVenda['LancamentoVenda']['valor_pago'];
101
		}
102
103
		return (float) $total;
104
	}
105
106
	public function carregar_total_vendas()
107
	{
108
		$this->loadModel('Venda');
109
110
		$dateInit = date('Y-m-d');
111
		$dateEnd  = date('Y-m-d');
112
113
		$vendas = $this->Venda->find('all', array(
114
				'conditions' => array(
115
					'Venda.data_venda' => $dateInit,
116
					'Venda.id_usuario' => $this->instancia,
117
					'Venda.ativo' => 1,
118
					'Venda.orcamento <> ' => 1
119
				)
120
			)
121
		);
122
123
		$total = 0;
124
		foreach ($vendas as $i => $venda) {
125
			$total += $venda['Venda']['valor'];
126
		}
127
128
		return (float) $total;
129
	}
130
131
}