1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
require 'AsaasController.php'; |
4
|
|
|
|
5
|
|
|
class ClienteController extends AppController{ |
6
|
|
|
// public $helpers = array('Excel'); |
7
|
|
|
|
8
|
|
|
function home() { |
9
|
|
|
$this->layout = 'wadmin'; |
10
|
|
|
} |
11
|
|
|
|
12
|
|
|
function adicionar_cliente() { |
13
|
|
|
$this->layout = 'wadmin'; |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
function s_adicionar_cliente() { |
17
|
|
|
$dados = $this->request->data('dados'); |
18
|
|
|
$dados['ativo'] = 1; |
19
|
|
|
$dados['id_usuario'] = $this->instancia; |
20
|
|
|
$dados['data_de_nascimento'] = date('y-m-d', strtotime($dados['data_de_nascimento'])); |
21
|
|
|
$dados['senha'] = md5(uniqid()); |
22
|
|
|
|
23
|
|
|
$endereco = $this->request->data('endereco'); |
24
|
|
|
$endereco['uf'] = $endereco['estado']; |
25
|
|
|
|
26
|
|
|
unset($endereco['estado']); |
27
|
|
|
|
28
|
|
|
//falta fazer o cadastro e relacionamento dos dados de endereco |
29
|
|
|
|
30
|
|
|
if ($this->Cliente->save($dados)) { |
31
|
|
|
$endereco['id_usuario'] = $this->instancia; |
32
|
|
|
$endereco['id_cliente'] = $this->Cliente->id; |
33
|
|
|
$endereco['ativo'] = 1; |
34
|
|
|
|
35
|
|
|
$this->loadModel('EnderecoClienteCadastro'); |
36
|
|
|
$this->EnderecoClienteCadastro->save($endereco); |
37
|
|
|
|
38
|
|
|
$this->Session->setFlash('Cliente salvo com sucesso!'); |
39
|
|
|
return $this->redirect('/cliente/listar_cadastros'); |
40
|
|
|
} else { |
41
|
|
|
$this->Session->setFlash('Ocorreu um erro ao salva o cliente!'); |
42
|
|
|
return $this->redirect('/cliente/listar_cadastros'); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
View Code Duplication |
function excluir_cliente() { |
47
|
|
|
$this->layout = 'ajax'; |
48
|
|
|
|
49
|
|
|
$id = $this->request->data('id'); |
50
|
|
|
|
51
|
|
|
$dados = array ('ativo' => '0'); |
52
|
|
|
$parametros = array ('id' => $id); |
53
|
|
|
|
54
|
|
|
if ($this->Cliente->updateAll($dados,$parametros)) { |
55
|
|
|
echo json_encode(true); |
56
|
|
|
} else { |
57
|
|
|
echo json_encode(false); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
View Code Duplication |
function listar_cadastros() { |
62
|
|
|
$this->layout = 'wadmin'; |
63
|
|
|
|
64
|
|
|
$this->set('clientes', $this->Cliente->find('all', |
65
|
|
|
array('conditions' => |
66
|
|
|
array('ativo' => 1, |
67
|
|
|
'id_usuario' => $this->instancia |
68
|
|
|
) |
69
|
|
|
) |
70
|
|
|
) |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
function editar_cliente() { |
75
|
|
|
$this->layout = 'wadmin'; |
76
|
|
|
$id = $this->params->pass[0]; |
77
|
|
|
|
78
|
|
|
$this->set('cliente', $this->Cliente->find('all', |
79
|
|
|
array('conditions' => |
80
|
|
|
array('ativo' => 1, |
81
|
|
|
'id' => $id |
82
|
|
|
) |
83
|
|
|
) |
84
|
|
|
) |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
function s_editar_cliente() { |
89
|
|
|
$dados = $this->request->data('dados'); |
90
|
|
|
$this->Cliente->id = $this->request->data('id'); |
91
|
|
|
|
92
|
|
|
if ($this->Cliente->save($dados)) { |
93
|
|
|
$this->Session->setFlash('Cliente editado com sucesso!'); |
94
|
|
|
return $this->redirect('/cliente/listar_cadastros'); |
95
|
|
|
} else { |
96
|
|
|
$this->Session->setFlash('Ocorreu um erro ao editar o cliente!'); |
97
|
|
|
return $this->redirect('/cliente/listar_cadastros'); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
function exportar_clientes() { |
102
|
|
|
$this->layout = 'ajax'; |
103
|
|
|
$this->set('event', $this->Cliente->find('all')); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
function listar_pedidos($id) { |
107
|
|
|
$this->layout = 'wadmin'; |
108
|
|
|
|
109
|
|
|
$this->loadModel('Venda'); |
110
|
|
|
$this->loadModel('LancamentoVenda'); |
111
|
|
|
|
112
|
|
|
$vendas = $this->Venda->find('all', array( |
113
|
|
|
'conditions' => array( |
114
|
|
|
'Venda.cliente_id' => $id |
115
|
|
|
) |
116
|
|
|
) |
117
|
|
|
); |
118
|
|
|
|
119
|
|
|
$total = 0; |
120
|
|
|
$devendo = 0; |
121
|
|
|
foreach ($vendas as $i => $venda) { |
122
|
|
|
$lancamento = $this->LancamentoVenda->find('first', array( |
123
|
|
|
'conditions' => array( |
124
|
|
|
'LancamentoVenda.venda_id' => $venda['Venda']['id'] |
125
|
|
|
) |
126
|
|
|
) |
127
|
|
|
); |
128
|
|
|
|
129
|
|
|
if (!isset($lancamento['LancamentoVenda'])) { |
130
|
|
|
unset($vendas[$i]); |
131
|
|
|
continue; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$vendas[$i]['LancamentoVenda'] = $lancamento['LancamentoVenda']; |
135
|
|
|
|
136
|
|
|
if ($lancamento['LancamentoVenda']['valor'] == $lancamento['LancamentoVenda']['valor_pago']) { |
137
|
|
|
$total += $lancamento['LancamentoVenda']['valor_pago']; |
138
|
|
|
} else { |
139
|
|
|
$devendo += $lancamento['LancamentoVenda']['valor']; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
$cliente = $this->Cliente->find('first', array( |
144
|
|
|
'conditions' => array( |
145
|
|
|
'Cliente.id' => $id |
146
|
|
|
) |
147
|
|
|
) |
148
|
|
|
); |
149
|
|
|
|
150
|
|
|
$this->set('cliente', $cliente); |
151
|
|
|
$this->set('vendas', $vendas); |
152
|
|
|
$this->set('total', $total); |
153
|
|
|
$this->set('devendo', $devendo); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
function emitir_boleto($venda_id) { |
157
|
|
|
$AsaasController = new AsaasController; |
158
|
|
|
|
159
|
|
|
$this->loadModel('Venda'); |
160
|
|
|
|
161
|
|
|
$venda = $this->Venda->find('first', array( |
162
|
|
|
'conditions' => array( |
163
|
|
|
'Venda.id' => $venda_id |
164
|
|
|
) |
165
|
|
|
) |
166
|
|
|
); |
167
|
|
|
|
168
|
|
|
$this->loadModel('Cliente'); |
169
|
|
|
|
170
|
|
|
$cliente = $this->Cliente->find('first', array( |
171
|
|
|
'conditions' => array( |
172
|
|
|
'Cliente.id' => $venda['Venda']['cliente_id'] |
173
|
|
|
) |
174
|
|
|
) |
175
|
|
|
); |
176
|
|
|
|
177
|
|
|
if (empty($cliente['Cliente']['asaas_id'])) { |
178
|
|
|
$cliente_data = array( |
179
|
|
|
"name" => $cliente['Cliente']['nome1'] . ' ' . $cliente['Cliente']['nome2'], |
180
|
|
|
"email" => $cliente['Cliente']['email'], |
181
|
|
|
"cpfCnpj" => $cliente['Cliente']['documento1'], |
182
|
|
|
"externalReference" => $cliente['Cliente']['id'] |
183
|
|
|
); |
184
|
|
|
|
185
|
|
|
$response = $AsaasController->criar_cliente($cliente_data); |
186
|
|
|
|
187
|
|
|
$asaas_id = $response->id; |
188
|
|
|
|
189
|
|
|
$this->Cliente->id = $cliente['Cliente']['id']; |
190
|
|
|
$this->Cliente->save(['asaas_id' => $asaas_id]); |
191
|
|
|
} else { |
192
|
|
|
$asaas_id = $cliente['Cliente']['asaas_id']; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
$venda = array( |
196
|
|
|
"customer" => $asaas_id, |
197
|
|
|
"billingType" => "BOLETO", |
198
|
|
|
"dueDate" => date('Y-m-d'), |
199
|
|
|
"value" => $venda['Venda']['valor'], |
200
|
|
|
"description" => "Boleto referente a venda no sistema winners REFº " . $venda['Venda']['id'], |
201
|
|
|
"externalReference" => $venda['Venda']['id'] |
202
|
|
|
); |
203
|
|
|
|
204
|
|
|
$response = $AsaasController->criar_cobranca($venda); |
205
|
|
|
|
206
|
|
|
if (isset($response->errors)) { |
207
|
|
|
foreach ($response->errors as $i => $error) { |
208
|
|
|
$this->Session->setFlash($error->description); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
return $this->redirect('/cliente/listar_pedidos/' . $cliente['Cliente']['id']); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
$this->Venda->id = $venda_id; |
215
|
|
|
|
216
|
|
|
$data = [ |
217
|
|
|
'asaas_boleto' => $response->bankSlipUrl, |
218
|
|
|
'asaas_status' => $response->status, |
219
|
|
|
'asaas_transaction_id' => $response->id |
220
|
|
|
]; |
221
|
|
|
|
222
|
|
|
if (!$this->Venda->save($data)) { |
223
|
|
|
$this->Session->setFlash('Erro ao emitir cobrança!'); |
224
|
|
|
return $this->redirect('/cliente/listar_pedidos/' . $cliente['Cliente']['id']); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
$this->Session->setFlash('Cobrança emitida com sucesso!'); |
228
|
|
|
return $this->redirect('/cliente/listar_pedidos/' . $cliente['Cliente']['id']); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
} |