· 10 years ago · Sep 14, 2016, 02:26 PM
1CREATE TABLE IF NOT EXISTS `lancamentos` (
2
3CREATE TABLE IF NOT EXISTS `categorias` (
4
5function adicionarDespesa() {
6
7 if(!$this->permission->checkPermission($this->session->userdata('permissao'),'aLancamento')){
8 $this->session->set_flashdata('error','Você não tem permissão para adicionar lançamentos.');
9 redirect(base_url());
10 }
11
12 $this->load->library('form_validation');
13 $this->data['custom_error'] = '';
14 $urlAtual = $this->input->post('urlAtual');
15 if ($this->form_validation->run('despesa') == false) {
16 $this->data['custom_error'] = (validation_errors() ? '<div class="form_error">' . validation_errors() . '</div>' : false);
17 } else {
18
19 $vencimento = $this->input->post('vencimento');
20 $pagamento = $this->input->post('pagamento');
21 $valor = $this->input->post('valor');
22 $conta = $this->input->post('conta_id');
23
24 if($pagamento != null){
25 $pagamento = explode('/', $pagamento);
26 $pagamento = $pagamento[2].'-'.$pagamento[1].'-'.$pagamento[0];
27 }
28
29 if($vencimento == null){
30 $vencimento = date('d/m/Y');
31 }
32
33 try {
34
35 $vencimento = explode('/', $vencimento);
36 $vencimento = $vencimento[2].'-'.$vencimento[1].'-'.$vencimento[0];
37
38 } catch (Exception $e) {
39 $vencimento = date('Y/m/d');
40 }
41
42 $data = array(
43 'descricao' => set_value('descricao'),
44 //'valor' => set_value('valor'),
45 'valor' => $valor,
46 'data_vencimento' => $vencimento,
47 'categoria_id' => $this->input->post('categoria_id'),
48 'conta_id'=> $conta,
49 //'conta_id' => $this->input->post('conta_id'),
50 'baixado' => $this->input->post('pago'),
51 'data_pagamento' => $pagamento != null ? $pagamento : date('Y-m-d'),
52 'baixado' => $this->input->post('pago'),
53 'cliente_fornecedor' => set_value('fornecedor'),
54 'forma_pgto' => $this->input->post('formaPgto'),
55 'tipo' => set_value('tipo')
56 );
57
58 if ($this->financeiro_model->add('lancamentos',$data) == TRUE) {
59
60 $sql = "UPDATE contas set saldo = saldo - ? WHERE idConta = ?";
61 $this->db->query($sql, array($valor, $conta));
62 echo json_encode(array('result'=> true));
63
64 $this->session->set_flashdata('success','Despesa adicionada com sucesso!');
65 redirect($urlAtual);
66 } else {
67 $this->session->set_flashdata('error','Ocorreu um erro ao tentar adicionar despesa!');
68 echo json_encode(array('result'=> false));
69 redirect($urlAtual);
70 }
71 }
72
73 $this->session->set_flashdata('error','Ocorreu um erro ao tentar adicionar despesa.');
74 redirect($urlAtual);
75
76
77}
78
79function adicionarReceita() {
80
81 if(!$this->permission->checkPermission($this->session->userdata('permissao'),'aLancamento')){
82 $this->session->set_flashdata('error','Você não tem permissão para adicionar lançamentos.');
83 redirect(base_url());
84 }
85
86 $this->load->library('form_validation');
87 $this->data['custom_error'] = '';
88 $urlAtual = $this->input->post('urlAtual');
89
90 if ($this->form_validation->run('receita') == false) {
91 $this->data['custom_error'] = (validation_errors() ? '<div class="form_error">' . validation_errors() . '</div>' : false);
92 } else {
93
94
95 $valor = $this->input->post('valor');
96 $vencimento = $this->input->post('vencimento');
97 $recebimento = $this->input->post('recebimento');
98 $conta = $this->input->post('conta_id');
99
100 if($recebimento != null){
101 $recebimento = explode('/', $recebimento);
102 $recebimento = $recebimento[2].'-'.$recebimento[1].'-'.$recebimento[0];
103 }
104
105 if($vencimento == null){
106 $vencimento = date('d/m/Y');
107 }
108
109 try {
110
111 $vencimento = explode('/', $vencimento);
112 $vencimento = $vencimento[2].'-'.$vencimento[1].'-'.$vencimento[0];
113
114 } catch (Exception $e) {
115 $vencimento = date('Y/m/d');
116 }
117
118 $data = array(
119 'descricao' => set_value('descricao'),
120 //'valor' => set_value('valor'),
121 'valor' => $valor,
122 'data_vencimento' => $vencimento,
123 'data_pagamento' => $recebimento != null ? $recebimento : date('Y-m-d'),
124 'categoria_id' => $this->input->post('categoria_id'),
125 'conta_id'=> $conta,
126 //'conta_id' => $this->input->post('conta_id'),
127 'baixado' => $this->input->post('recebido'),
128 'cliente_fornecedor' => set_value('cliente'),
129 'forma_pgto' => $this->input->post('formaPgto'),
130 'tipo' => set_value('tipo')
131 );
132
133 if ($this->financeiro_model->add('lancamentos',$data) == TRUE) {
134
135 $sql = "UPDATE contas set saldo = saldo + ? WHERE idConta = ?";
136 $this->db->query($sql, array($valor, $conta));
137 echo json_encode(array('result'=> true));
138
139 $this->session->set_flashdata('success','Receita adicionada com sucesso!');
140 redirect($urlAtual);
141
142 } else {
143 $this->data['custom_error'] = '<div class="form_error"><p>Ocorreu um erro.</p></div>';
144
145 echo json_encode(array('result'=> false));
146 }
147 }
148
149 $this->session->set_flashdata('error','Ocorreu um erro ao tentar adicionar receita.');
150 redirect($urlAtual);
151
152}
153
154public function editar(){
155 if(!$this->permission->checkPermission($this->session->userdata('permissao'),'eLancamento')){
156 $this->session->set_flashdata('error','Você não tem permissão para editar lançamentos.');
157 redirect(base_url());
158 }
159
160 $this->load->library('form_validation');
161 $this->data['custom_error'] = '';
162 $urlAtual = $this->input->post('urlAtual');
163
164 $this->form_validation->set_rules('descricao', '', 'trim|required|xss_clean');
165 $this->form_validation->set_rules('fornecedor', '', 'trim|required|xss_clean');
166 $this->form_validation->set_rules('valor', '', 'trim|required|xss_clean');
167 $this->form_validation->set_rules('vencimento', '', 'trim|required|xss_clean');
168 $this->form_validation->set_rules('pagamento', '', 'trim|xss_clean');
169
170 if ($this->form_validation->run() == false) {
171 $this->data['custom_error'] = (validation_errors() ? '<div class="form_error">' . validation_errors() . '</div>' : false);
172 } else {
173
174 $vencimento = $this->input->post('vencimento');
175 $pagamento = $this->input->post('pagamento');
176 $valor = $this->input->post('valor');
177 $conta = $this->input->post('conta_id');
178
179 try {
180
181 $vencimento = explode('/', $vencimento);
182 $vencimento = $vencimento[2].'-'.$vencimento[1].'-'.$vencimento[0];
183
184 $pagamento = explode('/', $pagamento);
185 $pagamento = $pagamento[2].'-'.$pagamento[1].'-'.$pagamento[0];
186
187 } catch (Exception $e) {
188 $vencimento = date('Y/m/d');
189 }
190
191 $data = array(
192 'descricao' => $this->input->post('descricao'),
193 //'valor' => $this->input->post('valor'),
194 'valor' => $valor,
195 'categoria_id' => $this->input->post('categoria_id'),
196 'conta_id'=> $conta,
197 'data_vencimento' => $vencimento,
198 'data_pagamento' => $pagamento,
199 'baixado' => $this->input->post('pago'),
200 'cliente_fornecedor' => $this->input->post('fornecedor'),
201 'forma_pgto' => $this->input->post('formaPgto'),
202 'tipo' => $this->input->post('tipo')
203 );
204
205 if ($this->financeiro_model->edit('lancamentos',$data,'idLancamentos',$this->input->post('id')) == TRUE) {
206
207
208 $sql = "UPDATE contas set saldo = saldo - ? WHERE idConta = ?";
209 $this->db->query($sql, array($valor, $conta));
210 echo json_encode(array('result'=> true));
211
212 $this->session->set_flashdata('success','lançamento editado com sucesso!');
213 redirect($urlAtual);
214 } else {
215
216 $this->session->set_flashdata('error','Ocorreu um erro ao tentar editar lançamento!');
217 echo json_encode(array('result'=> false));
218 redirect($urlAtual);
219 }
220
221 $sql = "UPDATE contas set saldo = saldo + ? WHERE idConta = ?";
222 $this->db->query($sql, array($valor, $conta));
223 echo json_encode(array('result'=> true));
224 }
225
226 $this->session->set_flashdata('error','Ocorreu um erro ao tentar editar lançamento.');
227 echo json_encode(array('result'=> false));
228 redirect($urlAtual);
229
230 $data = array(
231 'descricao' => $this->input->post('descricao'),
232 //'valor' => $this->input->post('valor'),
233 'valor' => $valor,
234 'categoria_id' => $this->input->post('categoria_id'),
235 'conta_id'=> $conta,
236 'data_vencimento' => $this->input->post('vencimento'),
237 'data_pagamento' => $this->input->post('pagamento'),
238 'baixado' => $this->input->post('pago'),
239 'cliente_fornecedor' => set_value('fornecedor'),
240 'forma_pgto' => $this->input->post('formaPgto'),
241 'tipo' => $this->input->post('tipo')
242 );
243 print_r($data);
244
245}
246
247public function excluirLancamento(){
248
249 if(!$this->permission->checkPermission($this->session->userdata('permissao'),'dLancamento')){
250 $this->session->set_flashdata('error','Você não tem permissão para excluir lançamentos.');
251 redirect(base_url());
252 }
253
254 $id = $this->input->post('id');
255
256 if($id == null || ! is_numeric($id)){
257 $json = array('result'=> false);
258 echo json_encode($json);
259 }
260 else{
261
262 $result = $this->financeiro_model->delete('lancamentos','idLancamentos',$id);
263 if($result){
264 $json = array('result'=> true);
265 echo json_encode($json);
266 }
267 else{
268 $json = array('result'=> false);
269 echo json_encode($json);
270 }
271
272 }
273}
274
275function get($table,$fields,$where='',$perpage=0,$start=0,$one=false,$array='array'){
276
277 $this->db->select($fields);
278 $this->db->from($table);
279 $this->db->order_by('data_vencimento', 'asc');
280 $this->db->limit($perpage,$start);
281 if($where){
282 $this->db->where($where);
283 }
284
285 $query = $this->db->get();
286
287 $result = !$one ? $query->result() : $query->row();
288 return $result;
289}
290
291
292function getById($id){
293 $this->db->where('idClientes',$id);
294 $this->db->limit(1);
295 return $this->db->get('clientes')->row();
296}
297
298function add($table,$data){
299 $this->db->insert($table, $data);
300 if ($this->db->affected_rows() == '1')
301 {
302 return TRUE;
303 }
304
305 return FALSE;
306}
307
308function edit($table,$data,$fieldID,$ID){
309 $this->db->where($fieldID,$ID);
310 $this->db->update($table, $data);
311
312 if ($this->db->affected_rows() >= 0)
313 {
314 return TRUE;
315 }
316
317 return FALSE;
318}
319
320function delete($table,$fieldID,$ID){
321 $this->db->where($fieldID,$ID);
322 $this->db->delete($table);
323 if ($this->db->affected_rows() == '1')
324 {
325 return TRUE;
326 }
327
328 return FALSE;
329}
330
331function count($table, $where) {
332
333 $this->db->from($table);
334 if($where){
335 $this->db->where($where);
336 }
337 return $this->db->count_all_results();
338}