✏️ 正在编辑: Despesa.php
路径:
/srv/systems_dir/yuppiecred-lidernegocios/application/models/Despesa.php
提示:
您可以编辑任何文件(包括二进制文件),但请注意不当修改可能导致文件损坏。
<?php class Model_Despesa extends Model_Lancamento { private $_rotaId; private $_folhaPagamentoId; public function getRotaId() { return $this->_rotaId; } public function setRotaId($rotaId) { $this->_rotaId = $rotaId; } public function getFolhaPagamentoId() { return $this->_folhaPagamentoId; } public function setFolhaPagamentoId($folhaPagamentoId) { $this->_folhaPagamentoId = $folhaPagamentoId; } public function _toArray() { $dbtable = new Model_DbTable_Lancamento(); $info = $dbtable->info(); $methods = get_class_methods($this); $data = []; foreach ( $info['cols'] as $key ) { $col = $key; $explode = explode("_", $key); $methodName = ''; foreach ( $explode as $v ) { $methodName .= ucfirst($v); } $key = $methodName != '' ? $methodName : $key; $method = 'get' . ucfirst($key); if( in_array($method, $methods) ){ $data[$col] = $this->$method(); } } $data['tipo'] = "DESPESA"; return $data; } /** * Insere ou altera o registro passado por parâmetro * @param Model_Despesa $despesa */ public function salvar(Model_Despesa $despesa) { $dbtable = new Model_DbTable_Lancamento(); if( $despesa->getId() > 0 ){ $dbtable->update($despesa->_toArray(), "id = " . $despesa->getId()); return $despesa->getId(); } else { if( $this->getNumeroParcelas() > 1 ){ //insere a primeira parcela $result = $dbtable->insert($despesa->_toArray()); //insere a repeticao $this->repetir($this->preparaRepeticao('DESPESA')); } else { $result = $dbtable->insert($despesa->_toArray()); } return $result; } } public function alterarItem($id, $dados) { if( $id <= 0 ){ return; } $dados = array_filter($dados); if( !$dados ){ throw new Exception("Item {$id} não tem nada para ser alterado."); } // Força a formatação de campos $this->setOptions($dados); $dados = array_filter($this->_toArray()); unset($dados['tipo']); $model = $this->exibir($id); $model->setFromArray($dados); if( !empty($dados['data_pagamento']) && empty($dados['valor_pago']) ){ $model->valor_pago = number_format($model->valor, 2, ',', '.'); } return $model->save(); } /** * Exclui o registro passado por parâmetro * @param int $id * @return int The number of rows deleted */ public function excluir($id) { if( $id > 0 ){ $dbtable = new Model_DbTable_Lancamento(); return $dbtable->delete("id = " . $id); } return null; } /** * Retorna uma lista * @param string $where * @param string $order * @param int $count * @param int $offset * @return array|Zend_Db_Table_Rowset_Abstract */ public function listar($where = null, $order = null, $count = null, $offset = null) { $where = $where != null ? "tipo = 'DESPESA' AND " . $where : "tipo = 'DESPESA'"; $dbtable = new Model_DbTable_Lancamento(); return $dbtable->fetchAll($where, $order, $count, $offset); } /** * Retorna um fetchRow do item passado por parâmetro * @param int $id * @return Zend_Db_Table_Row_Abstract | null */ public function exibir($id) { $dbtable = new Model_DbTable_Lancamento(); return $dbtable->fetchRow("id = " . $id); } public function getPaginatorAdapter($fields, $order = null) { $params = array( "tipo = 'DESPESA'" ); if( isset($fields['categoria_id']) && !empty($fields['categoria_id']) ){ $params[] = "categoria_id = " . $fields['categoria_id']; } if( isset($fields['descricao']) && !empty($fields['descricao']) ){ $params[] = "descricao LIKE '%" . $fields['descricao'] . "%'"; } if( isset($fields['data_vencimento']) && !empty($fields['data_vencimento']) ){ $params[] = "data_vencimento = STR_TO_DATE('" . $fields['data_vencimento'] . "', '%d/%m/%Y')"; } $where = null; if( count($params) > 0 ){ $where = implode(" AND ", $params); } $dbtable = new Model_DbTable_Lancamento(); $select = $dbtable->select(); if( $where != null ) $select->where($where); if( $order != null ) $select->order($order); return new Zend_Paginator_Adapter_DbTableSelect($select); } /** * Vencimento no dia OU pagos no dia * @param string $data * @return Zend_Db_Table_Rowset_Abstract | array | null */ public function getLancamentosDia($data) { $where = "(data_vencimento = '" . $data . "' OR data_pagamento = '{$data}')"; $dbtable = new Model_DbTable_Lancamento(); return $dbtable->fetchCols("id", array("data_vencimento", "descricao", "valor"), $where, "data_vencimento"); } public function getLancamentosVencidos($data) { $dbtable = new Model_DbTable_Lancamento(); $select = $dbtable->getAdapter()->select()->from("tb_lancamentos", array("descricao", "id", "valor", 'data_vencimento')); $select->where("data_vencimento <= '{$data}'") ->where("data_pagamento IN ('', '0000-00-00') OR data_pagamento IS NULL") ->where("excluido <> 1") ->order("data_vencimento ASC"); return $dbtable->getAdapter()->fetchAll($select); } public function getLancamentosMes($data) { $dbtable = new Model_DbTable_Lancamento(); $select = $dbtable->getAdapter()->select()->from(array("lancamentos" => 'tb_lancamentos'), array( 'valor_total' => "SUM(valor)", 'total_pago' => "SUM(valor_pago)", 'categoria_id' )) ->joinInner(array("categorias" => "tb_categorias"), "lancamentos.categoria_id = categorias.id", array('categoria' => 'nome')); $select->where("lancamentos.excluido<>1") ->where("lancamentos.tipo = 'DESPESA'") ->where("DATE_FORMAT(lancamentos.data_vencimento, '%Y-%m') = '" . $data . "'"); $select->group(array( 'lancamentos.categoria_id' )); return $dbtable->getAdapter()->fetchAll($select); } public function getLancamentos($fields, $order = 'data_vencimento ASC') { $params = ["l.excluido<> 1", "l.tipo = 'DESPESA'"]; $where = null; if (!empty($fields['por_corretor'])) { $params[] = "l.corretor_id IS NOT NULL AND l.corretor_id > 0"; } if (!empty($fields['por_funcionario'])) { $params[] = "l.funcionario_id IS NOT NULL AND l.funcionario_id > 0"; } if (!empty($fields['vencimento_de'])) { $params[] = "l.data_vencimento >= STR_TO_DATE('" . $fields['vencimento_de'] . "', '%d/%m/%Y')"; } if (!empty($fields['vencimento_a'])) { $params [] = "l.data_vencimento <= STR_TO_DATE('" . $fields['vencimento_a'] . "', '%d/%m/%Y')"; } if (!empty($fields['pagamento_de'])) { $params[] = "l.data_pagamento >= STR_TO_DATE('" . $fields['pagamento_de'] . "', '%d/%m/%Y')"; } if (!empty($fields['pagamento_a'])) { $params[] = "l.data_pagamento <= STR_TO_DATE('" . $fields['pagamento_a'] . "', '%d/%m/%Y')"; } if (!empty($fields['empresa_id'])) { $params[] = "l.empresa_id = {$fields['empresa_id']}"; } if (!empty($fields['fornecedor_id'])) { $params[] = "l.fornecedor_id = {$fields['fornecedor_id']}"; } if (!empty($fields['corretor_id'])) { $params[] = "l.corretor_id = " . $fields['corretor_id']; } if (!empty($fields['funcionario_id'])) { $params[] = "l.funcionario_id = " . $fields['funcionario_id']; } if (!empty($fields['rota_id'])) { $params[] = "l.rota_id = " . $fields['rota_id']; } if (!empty($fields['supervisor_id'])) { $params[] = "l.supervisor_id = " . $fields['supervisor_id']; } if (isset($fields['categoria_id']) && $fields['categoria_id'] > 0) { $categoria = new Model_Categoria(); $subCategorias = $categoria->getDescendentes($fields['categoria_id']); if ($subCategorias) { $idsCategorias = array_keys($subCategorias); $idsCategorias[] = $fields['categoria_id']; $params[] = 'l.categoria_id IN (' . implode(',', $idsCategorias) . ')'; } else { $params[] = "l.categoria_id = {$fields['categoria_id']}"; } } if (!empty($fields['numero_documento'])) { $params[] = "l.numero_documento = '" . $fields['numero_documento'] . "'"; } if (!empty($fields['setor'])) { $params[] = "setor LIKE '%" . $fields['setor'] . "%'"; } if (!empty($fields['subsetor'])) { $params[] = "subsetor LIKE '%" . $fields['subsetor'] . "%'"; } if (!empty($fields['banco_corretor'])) { $params[] = "co.banco = '" . $fields['banco_corretor'] . "'"; } if (!empty($fields['banco_funcionario'])) { $params[] = "fu.banco = '" . $fields['banco_funcionario'] . "'"; } if (!empty($fields['banco_fornecedor'])) { $params[] = "f.banco = '" . $fields['banco_fornecedor'] . "'"; } if (!empty($fields['conta_id'])) { $params[] = "contas.id = '" . $fields['conta_id'] . "'"; } if (!empty($fields['forma_pagamento'])) { $params[] = "l.forma_pagamento_id = " . $fields['forma_pagamento']; } if (!empty($fields['situacao'])) { switch ($fields['situacao']) { case "Pago": $params[] = "data_pagamento IS NOT NULL AND data_pagamento <> '0000-00-00'"; break; case "A Pagar": $params[] = "(data_pagamento IS NULL OR data_pagamento = '0000-00-00')"; break; } } if (!empty($fields['descricao'])) { $params[] = "l.descricao like '%" . $fields['descricao'] . "%'"; } if (!empty($fields['lancamento_id'])) { $params[] = "l.id = " . $fields['lancamento_id']; } $nsAcl = new Zend_Session_Namespace(); if (!$nsAcl->acl->isAllowed(Yuppie_Auth::getIdentity()->grupo, "visualizar-despesas-filiais")) { $params[] = "l.empresa_id = " . Yuppie_Auth::getIdentity()->empresa_id; } if (!empty($fields['competencia'])) { $params[] = "competencia = '{$fields['competencia']}'"; } if (count($params) > 0) { $where = implode(' AND ', $params); } $dbtable = new Model_DbTable_Lancamento(); $select = $dbtable->getAdapter()->select() ->from(['l' => 'tb_lancamentos'], ['l.*']) ->joinLeft(['e' => 'tb_empresas'], 'l.empresa_id = e.id', ['empresa' => 'nome']) ->joinLeft(['c' => 'tb_categorias'], 'l.categoria_id = c.id', ['categoria' => 'nome']) ->joinLeft(['forma_pagamentos' => 'tb_tipos'], 'forma_pagamentos.id = l.forma_pagamento_id', ['forma_pagamento' => 'forma_pagamentos.descricao']) ->joinLeft(['f' => 'tb_fornecedores'], 'l.fornecedor_id = f.id', [ 'fornecedor' => 'nome', 'fornecedor_banco' => 'banco', 'fornecedor_agencia' => 'agencia', 'fornecedor_conta' => 'conta', 'fornecedor_tipo_conta' => 'tipo_conta', 'fornecedor_cnpj' => 'cnpj' ]) ->joinLeft(['co' => 'tb_corretores'], 'l.corretor_id = co.id', [ 'corretor_nome' => 'nome', 'corretor_banco' => 'banco', 'corretor_agencia' => 'agencia', 'corretor_conta' => 'conta', 'corretor_tipo_conta' => 'tipo_conta', 'corretor_cpf' => 'cpf', 'corretor_chave_pix' => 'chave_pix' ]) ->joinLeft(['fu' => 'tb_fornecedores'], 'l.funcionario_id = fu.id', [ 'funcionario_nome' => 'nome', 'funcionario_banco' => 'banco', 'funcionario_agencia' => 'agencia', 'funcionario_conta' => 'conta', 'funcionario_tipo_conta' => 'tipo_conta', 'funcionario_cpf' => 'cpf_responsavel', 'funcionario_chave_pix' => 'chave_pix' ]) ->joinLeft(['contas' => 'tb_contas'], 'l.conta_id = contas.id', ['conta' => 'nome']) ->joinLeft(['rotas' => 'tb_rotas'], 'l.rota_id = rotas.id', ['rota' => 'nome']); $select->where($where); if ($order != null) { $select->order($order); } return $dbtable->getAdapter()->fetchAll($select); } public function getLancamentosPorCategoria($fields) { $dbtable = new Model_DbTable_Lancamento(); $select = $dbtable->select()->setIntegrityCheck(false) ->from(array( 'l' => 'tb_lancamentos' ), array( 'categoria_id', 'valor' => 'SUM(valor_pago)' )); $select->where('tipo = ?', 'DESPESA'); $select->group(array( 'c.categoria_pai', 'categ_pai', 'categoria_id', 'categoria' )); $select->joinLeft(array( 'c' => 'tb_categorias' ), 'c.id = l.categoria_id', array( 'categoria' => 'nome', 'categoria_pai' )); $select->joinLeft(array( 'p' => 'tb_categorias' ), 'p.id = c.categoria_pai', array( 'categ_pai' => 'nome' )); $select->order(array( 'p.nome', 'c.nome' )); if( isset($fields['vencimento_de']) && !empty($fields['vencimento_de']) && isset($fields['vencimento_a']) && !empty($fields['vencimento_a']) ){ $select->where("data_vencimento >= STR_TO_DATE(?, '%d/%m/%Y')", $fields['vencimento_de']); $select->where("data_vencimento <= STR_TO_DATE(?, '%d/%m/%Y')", $fields['vencimento_a']); } if( isset($fields['pagamento_de']) && !empty($fields['pagamento_de']) && isset($fields['pagamento_a']) && !empty($fields['pagamento_a']) ){ $select->where("data_pagamento >= STR_TO_DATE(?, '%d/%m/%Y')", $fields['pagamento_de']); $select->where("data_pagamento <= STR_TO_DATE(?, '%d/%m/%Y')", $fields['pagamento_a']); } if( isset($fields['empresa_id']) && $fields['empresa_id'] > 0 ){ $select->where('empresa_id = ?', $fields['empresa_id']); } if( isset($fields['fornecedor_id']) && $fields['fornecedor_id'] > 0 ){ $select->where('fornecedor_id = ?', $fields['fornecedor_id']); } if( isset($fields['corretor_id']) && $fields['corretor_id'] > 0 ){ $select->where('corretor_id = ?', $fields['corretor_id']); } if( isset($fields['funcionario_id']) && $fields['funcionario_id'] > 0 ){ $select->where('funcionario_id = ?', $fields['funcionario_id']); } if( isset($fields['rota_id']) && $fields['rota_id'] > 0 ){ $select->where('rota_id = ?', $fields['rota_id']); } if( isset($fields['numero_documento']) && $fields['numero_documento'] != '' ){ $select->where('numero_documento = ?', $fields['numero_documento']); } if( isset($fields['situacao']) && $fields['situacao'] != '' ){ if( $fields['situacao'] == 'Pago' ){ $select->where('data_pagamento IS NOT NULL AND data_pagamento <> ?', ''); } else { $select->where('data_pagamento IS NULL OR data_pagamento = ?', ''); } } if( isset($fields['setor']) && $fields['setor'] != '' ){ $select->where('setor LIKE ?', "%{$fields['setor']}%"); } if( isset($fields['subsetor']) && $fields['subsetor'] != '' ){ $select->where('subsetor LIKE ?', "%{$fields['subsetor']}%"); } if( isset($fields['supervisor_id']) && !empty($fields['supervisor_id']) ){ $select->where("l.supervisor_id = " . $fields['supervisor_id']); } return $dbtable->getAdapter()->fetchAll($select); } public function exportarDespesas($despesas) { $objPhpExcel = new PHPExcel(); $objPhpExcel->getProperties()->setDescription('Relatório de Despesas'); $objPhpExcel->getProperties()->setCreator('Yuppie Tech'); $objPhpExcel->getProperties()->setTitle('Relatório de Despesas'); $objPhpExcel->setActiveSheetIndex(0); $objPhpExcel->getActiveSheet()->setTitle('Despesas'); //cabeçalho do arquivo $objPhpExcel->getActiveSheet()->SetCellValue('A' . 1, "Empresa"); $objPhpExcel->getActiveSheet()->SetCellValue('B' . 1, "Setor"); $objPhpExcel->getActiveSheet()->SetCellValue('C' . 1, "Categoria"); $objPhpExcel->getActiveSheet()->SetCellValue('D' . 1, "Descricao"); $objPhpExcel->getActiveSheet()->SetCellValue('E' . 1, "Fornecedor"); $objPhpExcel->getActiveSheet()->SetCellValue('F' . 1, "Banco"); $objPhpExcel->getActiveSheet()->SetCellValue('G' . 1, "Agencia"); $objPhpExcel->getActiveSheet()->SetCellValue('H' . 1, "Conta"); $objPhpExcel->getActiveSheet()->SetCellValue('I' . 1, "Tipo Conta"); $objPhpExcel->getActiveSheet()->SetCellValue('J' . 1, "Vencimento"); $objPhpExcel->getActiveSheet()->SetCellValue('K' . 1, "Valor"); $objPhpExcel->getActiveSheet()->SetCellValue('L' . 1, "Pagamento"); $objPhpExcel->getActiveSheet()->SetCellValue('M' . 1, "Valor Pago"); $totalRegistrosDespesas = count($despesas); $totalValor = 0; $totalValorPago = 0; //linhas do arquivo for ( $i = 0; $i < $totalRegistrosDespesas; $i++ ) { $fornecedorNome = null; $fornecedorBanco = null; $fornecedorAgencia = null; $fornecedorConta = null; $fornecedorTipoConta = null; $totalValor += $despesas[$i]['valor']; $totalValorPago += $despesas[$i]['valor_pago']; if( $despesas[$i]['fornecedor_id'] > 0 && $despesas[$i]['funcionario_id'] > 0 ){ $fornecedorNome = $despesas[$i]['funcionario_nome']; $fornecedorBanco = $despesas[$i]['funcionario_banco']; $fornecedorAgencia = $despesas[$i]['funcionario_agencia']; $fornecedorConta = $despesas[$i]['funcionario_conta']; } elseif( $despesas[$i]['fornecedor_id'] > 0 ){ $fornecedorNome = $despesas[$i]['fornecedor']; $fornecedorBanco = $despesas[$i]['fornecedor_banco']; $fornecedorAgencia = $despesas[$i]['fornecedor_agencia']; $fornecedorConta = $despesas[$i]['fornecedor_conta']; $fornecedorTipoConta = $despesas[$i]['fornecedor_tipo_conta']; } elseif( $despesas[$i]['funcionario_id'] > 0 ){ $fornecedorNome = $despesas[$i]['funcionario_nome']; $fornecedorBanco = $despesas[$i]['funcionario_banco']; $fornecedorAgencia = $despesas[$i]['funcionario_agencia']; $fornecedorConta = $despesas[$i]['funcionario_conta']; $fornecedorTipoConta = $despesas[$i]['funcionario_tipo_conta']; } elseif( $despesas[$i]['corretor_id'] > 0 ){ $fornecedorNome = $despesas[$i]['corretor_nome']; $fornecedorBanco = $despesas[$i]['corretor_banco']; $fornecedorAgencia = $despesas[$i]['corretor_agencia']; $fornecedorConta = $despesas[$i]['corretor_conta']; $fornecedorTipoConta = $despesas[$i]['corretor_tipo_conta']; } $objPhpExcel->getActiveSheet()->setCellValueByColumnAndRow(0, $i + 2, $despesas[$i]['empresa']); $objPhpExcel->getActiveSheet()->setCellValueByColumnAndRow(1, $i + 2, $despesas[$i]['setor']); $objPhpExcel->getActiveSheet()->setCellValueByColumnAndRow(2, $i + 2, $despesas[$i]['categoria']); $objPhpExcel->getActiveSheet()->setCellValueByColumnAndRow(3, $i + 2, $despesas[$i]['descricao']); $objPhpExcel->getActiveSheet()->setCellValueByColumnAndRow(4, $i + 2, $fornecedorNome); $objPhpExcel->getActiveSheet()->setCellValueByColumnAndRow(5, $i + 2, $fornecedorBanco); $objPhpExcel->getActiveSheet()->setCellValueByColumnAndRow(6, $i + 2, $fornecedorAgencia); $objPhpExcel->getActiveSheet()->setCellValueByColumnAndRow(7, $i + 2, $fornecedorConta); $objPhpExcel->getActiveSheet()->setCellValueByColumnAndRow(8, $i + 2, $fornecedorTipoConta); $objPhpExcel->getActiveSheet()->setCellValueByColumnAndRow(9, $i + 2, date('d/m/Y', strtotime($despesas[$i]['data_vencimento']))); $objPhpExcel->getActiveSheet()->setCellValueByColumnAndRow(10, $i + 2, number_format($despesas[$i]['valor'], 2, ',', '.')); $objPhpExcel->getActiveSheet()->setCellValueByColumnAndRow(11, $i + 2, empty($despesas[$i]['data_pagamento']) ? '' : date('d/m/Y', strtotime($despesas[$i]['data_pagamento']))); $objPhpExcel->getActiveSheet()->setCellValueByColumnAndRow(12, $i + 2, number_format($despesas[$i]['valor_pago'], 2, ',', '.')); } // rodape $objPhpExcel->getActiveSheet()->setCellValueByColumnAndRow(0, $totalRegistrosDespesas + 2, 'Total'); $objPhpExcel->getActiveSheet()->setCellValueByColumnAndRow(1, $totalRegistrosDespesas + 2, ''); $objPhpExcel->getActiveSheet()->setCellValueByColumnAndRow(2, $totalRegistrosDespesas + 2, ''); $objPhpExcel->getActiveSheet()->setCellValueByColumnAndRow(3, $totalRegistrosDespesas + 2, ''); $objPhpExcel->getActiveSheet()->setCellValueByColumnAndRow(4, $totalRegistrosDespesas + 2, ''); $objPhpExcel->getActiveSheet()->setCellValueByColumnAndRow(5, $totalRegistrosDespesas + 2, ''); $objPhpExcel->getActiveSheet()->setCellValueByColumnAndRow(6, $totalRegistrosDespesas + 2, ''); $objPhpExcel->getActiveSheet()->setCellValueByColumnAndRow(7, $totalRegistrosDespesas + 2, ''); $objPhpExcel->getActiveSheet()->setCellValueByColumnAndRow(8, $totalRegistrosDespesas + 2, ''); $objPhpExcel->getActiveSheet()->setCellValueByColumnAndRow(9, $totalRegistrosDespesas + 2, ''); $objPhpExcel->getActiveSheet()->setCellValueByColumnAndRow(10, $totalRegistrosDespesas + 2, number_format($totalValor, 2, ',', '.')); $objPhpExcel->getActiveSheet()->setCellValueByColumnAndRow(11, $totalRegistrosDespesas + 2, ''); $objPhpExcel->getActiveSheet()->setCellValueByColumnAndRow(12, $totalRegistrosDespesas + 2, number_format($totalValorPago, 2, ',', '.')); // $objWriter = new PHPExcel_Writer_Excel2007($objPhpExcel); $fileName = PUBLIC_PATH . "/../public/downloads/despesas.xlsx"; $objWriter->save($fileName); return $fileName; } public function getDespesas(array $fields, $page = null, $limit = null, $order = array(), $range = null) { $params = array( "l.excluido<> 1", "l.tipo = 'DESPESA'" ); $where = null; if( isset($fields['por_corretor']) && $fields['por_corretor'] ){ $params[] = "l.corretor_id IS NOT NULL AND l.corretor_id > 0"; } if( isset($fields['por_funcionario']) && $fields['por_funcionario'] ){ $params[] = "l.funcionario_id IS NOT NULL AND l.funcionario_id > 0"; } if( isset($fields['vencimento_de']) && !empty($fields['vencimento_de']) && isset($fields['vencimento_a']) && !empty($fields['vencimento_a']) ){ $params[] = "(l.data_vencimento >= STR_TO_DATE('" . $fields['vencimento_de'] . "', '%d/%m/%Y')" . " AND l.data_vencimento <= STR_TO_DATE('" . $fields['vencimento_a'] . "', '%d/%m/%Y'))"; } if( isset($fields['pagamento_de']) && !empty($fields['pagamento_de']) && isset($fields['pagamento_a']) && !empty($fields['pagamento_a']) ){ $params[] = "(l.data_pagamento >= STR_TO_DATE('" . $fields['pagamento_de'] . "', '%d/%m/%Y')" . " AND l.data_pagamento <= STR_TO_DATE('" . $fields['pagamento_a'] . "', '%d/%m/%Y'))"; } if( isset($fields['empresa_id']) && $fields['empresa_id'] > 0 ){ $params[] = "l.empresa_id = {$fields['empresa_id']}"; } if( isset($fields['fornecedor_id']) && $fields['fornecedor_id'] > 0 ){ $params[] = "l.fornecedor_id = {$fields['fornecedor_id']}"; } if( isset($fields['corretor_id']) && $fields['corretor_id'] > 0 ){ $params[] = "l.corretor_id = " . $fields['corretor_id']; } if( isset($fields['funcionario_id']) && $fields['funcionario_id'] > 0 ){ $params[] = "l.funcionario_id = " . $fields['funcionario_id']; } if( isset($fields['rota_id']) && $fields['rota_id'] > 0 ){ $params[] = "l.rota_id = " . $fields['rota_id']; } if( isset($fields['supervisor_id']) && !empty($fields['supervisor_id']) ){ $params[] = "l.supervisor_id = " . $fields['supervisor_id']; } if( isset($fields['categoria_id']) && $fields['categoria_id'] > 0 ){ $categoria = new Model_Categoria(); $subCategorias = $categoria->getDescendentes($fields['categoria_id']); if( $subCategorias ){ $idsCategorias = array_keys($subCategorias); $idsCategorias[] = $fields['categoria_id']; $params[] = 'l.categoria_id IN (' . implode(',', $idsCategorias) . ')'; } else { $params[] = "l.categoria_id = {$fields['categoria_id']}"; } } if( isset($fields['numero_documento']) && $fields['numero_documento'] != '' ){ $params[] = "l.numero_documento = '" . $fields['numero_documento'] . "'"; } if( isset($fields['situacao']) && $fields['situacao'] != '' ){ if( $fields['situacao'] == 'Pago' ){ $params[] = "(l.data_pagamento IS NOT NULL AND l.data_pagamento <> '')"; } else { $params[] = "(l.data_pagamento IS NULL OR l.data_pagamento = '')"; } } if( isset($fields['setor']) && $fields['setor'] != '' ){ $params[] = "setor LIKE '%" . $fields['setor'] . "%'"; } if( isset($fields['subsetor']) && $fields['subsetor'] != '' ){ $params[] = "subsetor LIKE '%" . $fields['subsetor'] . "%'"; } if( count($params) > 0 ){ $where = implode(' AND ', $params); } $dbtable = new Model_DbTable_Lancamento(); $select = $dbtable->getAdapter()->select() ->from(array( 'l' => 'tb_lancamentos' ), array( 'l.*' )) ->joinLeft(array( 'e' => 'tb_empresas' ), 'l.empresa_id = e.id', array( 'empresa' => 'nome' )) ->joinLeft(array( 'c' => 'tb_categorias' ), 'l.categoria_id = c.id', array( 'categoria' => 'nome' )) ->joinLeft(array( 'f' => 'tb_fornecedores' ), 'l.fornecedor_id = f.id', array( 'fornecedor' => 'nome', 'fornecedor_banco' => 'banco', 'fornecedor_agencia' => 'agencia', 'fornecedor_conta' => 'conta', 'fornecedor_tipo_conta' => 'tipo_conta' )) ->joinLeft(array( 'co' => 'tb_corretores' ), 'l.corretor_id = co.id', array( 'corretor_nome' => 'nome', 'corretor_banco' => 'banco', 'corretor_agencia' => 'agencia', 'corretor_conta' => 'conta', 'corretor_tipo_conta' => 'tipo_conta' )) ->joinLeft(array( 'fu' => 'tb_fornecedores' ), 'l.corretor_id = fu.id', array( 'funcionario_nome' => 'nome', 'funcionario_banco' => 'banco', 'funcionario_agencia' => 'agencia', 'funcionario_conta' => 'conta', 'funcionario_tipo_conta' => 'tipo_conta' )) ->joinLeft(array( 'contas' => 'tb_contas' ), 'l.conta_id = contas.id', array( 'conta' => 'nome' )) ->joinLeft(array( 'rotas' => 'tb_rotas' ), 'l.rota_id = rotas.id', array( 'rota' => 'nome' )); $select->where($where); if( $order != null ){ $select->order($order); } if( $page ){ $paginator = Zend_Paginator::factory($select); $paginator->setCurrentPageNumber($page); if( $limit && is_numeric($limit) ){ $paginator->setItemCountPerPage($limit); } if( $range && is_numeric($range) ){ $paginator->setPageRange($range); } return $paginator; } } public function getDespesasTotalizador($fields = array()) { $params = array( "l.excluido<> 1", "l.tipo = 'DESPESA'" ); $where = null; if( isset($fields['por_corretor']) && $fields['por_corretor'] ){ $params[] = "l.corretor_id IS NOT NULL AND l.corretor_id > 0"; } if( isset($fields['por_funcionario']) && $fields['por_funcionario'] ){ $params[] = "l.funcionario_id IS NOT NULL AND l.funcionario_id > 0"; } if( isset($fields['vencimento_de']) && !empty($fields['vencimento_de']) && isset($fields['vencimento_a']) && !empty($fields['vencimento_a']) ){ $params[] = "(l.data_vencimento >= STR_TO_DATE('" . $fields['vencimento_de'] . "', '%d/%m/%Y')" . " AND l.data_vencimento <= STR_TO_DATE('" . $fields['vencimento_a'] . "', '%d/%m/%Y'))"; } if( isset($fields['pagamento_de']) && !empty($fields['pagamento_de']) && isset($fields['pagamento_a']) && !empty($fields['pagamento_a']) ){ $params[] = "(l.data_pagamento >= STR_TO_DATE('" . $fields['pagamento_de'] . "', '%d/%m/%Y')" . " AND l.data_pagamento <= STR_TO_DATE('" . $fields['pagamento_a'] . "', '%d/%m/%Y'))"; } if( isset($fields['empresa_id']) && $fields['empresa_id'] > 0 ){ $params[] = "l.empresa_id = {$fields['empresa_id']}"; } if( isset($fields['fornecedor_id']) && $fields['fornecedor_id'] > 0 ){ $params[] = "l.fornecedor_id = {$fields['fornecedor_id']}"; } if( isset($fields['corretor_id']) && $fields['corretor_id'] > 0 ){ $params[] = "l.corretor_id = " . $fields['corretor_id']; } if( isset($fields['funcionario_id']) && $fields['funcionario_id'] > 0 ){ $params[] = "l.funcionario_id = " . $fields['funcionario_id']; } if( isset($fields['rota_id']) && $fields['rota_id'] > 0 ){ $params[] = "l.rota_id = " . $fields['rota_id']; } if( isset($fields['supervisor_id']) && !empty($fields['supervisor_id']) ){ $params[] = "l.supervisor_id = " . $fields['supervisor_id']; } if( isset($fields['categoria_id']) && $fields['categoria_id'] > 0 ){ $categoria = new Model_Categoria(); $subCategorias = $categoria->getDescendentes($fields['categoria_id']); if( $subCategorias ){ $idsCategorias = array_keys($subCategorias); $idsCategorias[] = $fields['categoria_id']; $params[] = 'l.categoria_id IN (' . implode(',', $idsCategorias) . ')'; } else { $params[] = "l.categoria_id = {$fields['categoria_id']}"; } } if( isset($fields['numero_documento']) && $fields['numero_documento'] != '' ){ $params[] = "l.numero_documento = '" . $fields['numero_documento'] . "'"; } if( isset($fields['situacao']) && $fields['situacao'] != '' ){ if( $fields['situacao'] == 'Pago' ){ $params[] = "(l.data_pagamento IS NOT NULL AND l.data_pagamento <> '')"; } else { $params[] = "(l.data_pagamento IS NULL OR l.data_pagamento = '')"; } } if( isset($fields['setor']) && $fields['setor'] != '' ){ $params[] = "setor LIKE '%" . $fields['setor'] . "%'"; } if( isset($fields['subsetor']) && $fields['subsetor'] != '' ){ $params[] = "subsetor LIKE '%" . $fields['subsetor'] . "%'"; } if( count($params) > 0 ){ $where = implode(' AND ', $params); } $dbtable = new Model_DbTable_Lancamento(); $select = $dbtable->getAdapter()->select() ->from(array( 'l' => 'tb_lancamentos' ), array( 'l.*' )) ->columns(array( 'total_valor' => 'SUM(valor)', 'total_valor_pago' => 'SUM(valor_pago)' )) ->joinLeft(array( 'e' => 'tb_empresas' ), 'l.empresa_id = e.id', array( 'empresa' => 'nome' )) ->joinLeft(array( 'c' => 'tb_categorias' ), 'l.categoria_id = c.id', array( 'categoria' => 'nome' )) ->joinLeft(array( 'f' => 'tb_fornecedores' ), 'l.fornecedor_id = f.id', array( 'fornecedor' => 'nome', 'fornecedor_banco' => 'banco', 'fornecedor_agencia' => 'agencia', 'fornecedor_conta' => 'conta', 'fornecedor_tipo_conta' => 'tipo_conta' )) ->joinLeft(array( 'co' => 'tb_corretores' ), 'l.corretor_id = co.id', array( 'corretor_nome' => 'nome', 'corretor_banco' => 'banco', 'corretor_agencia' => 'agencia', 'corretor_conta' => 'conta', 'corretor_tipo_conta' => 'tipo_conta' )) ->joinLeft(array( 'fu' => 'tb_fornecedores' ), 'l.corretor_id = fu.id', array( 'funcionario_nome' => 'nome', 'funcionario_banco' => 'banco', 'funcionario_agencia' => 'agencia', 'funcionario_conta' => 'conta', 'funcionario_tipo_conta' => 'tipo_conta' )) ->joinLeft(array( 'contas' => 'tb_contas' ), 'l.conta_id = contas.id', array( 'conta' => 'nome' )) ->joinLeft(array( 'rotas' => 'tb_rotas' ), 'l.rota_id = rotas.id', array( 'rota' => 'nome' )); $select->where($where); return $dbtable->getAdapter()->fetchRow($select); } }
💾 保存文件
← 返回文件管理器