Completed
Push — master ( bbeb79...483773 )
by Reginaldo
26:40
created

carregar_fechamento_caixa_dia_ajax()   A

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
			'conditions' => array(
73
				'LancamentoVenda.usuario_id' => $this->instancia,
74
				'LancamentoVenda.data_pgt' => date('Y-m-d')
75
			)
76
		);
77
78
		if ($tipo == 'cartao') {
79
			$conditions['conditions']['LancamentoVenda.forma_pagamento <> '] = 'dinheiro';
80
		} else {
81
			$conditions['conditions']['LancamentoVenda.forma_pagamento'] = 'dinheiro';
82
		}
83
84
		$LancamentoVendas = $this->LancamentoVenda->find('all', $conditions);	
85
86
		$total = 0;
87
88
		foreach ($LancamentoVendas as $i => $LancamentoVenda) {
89
			$total += $LancamentoVenda['LancamentoVenda']['valor_pago'];
90
		}
91
92
		return (float) $total;
93
	}
94
95
	public function carregar_total_vendas()
96
	{
97
		$this->loadModel('Venda');
98
99
		$dateInit = date('Y-m-d');
100
		$dateEnd  = date('Y-m-d');
101
102
		$vendas = $this->Venda->find('all', array(
103
				'conditions' => array(
104
					'Venda.data_venda' => $dateInit,
105
					'Venda.id_usuario' => $this->instancia,
106
					'Venda.ativo' => 1,
107
					'Venda.orcamento <> ' => 1
108
				)
109
			)
110
		);
111
112
		$total = 0;
113
		foreach ($vendas as $i => $venda) {
114
			$total += $venda['Venda']['valor'];
115
		}
116
117
		return (float) $total;
118
	}
119
120
}