✏️ 正在编辑: ConsultaHiscon.php
路径:
/srv/systems_dir/yuppiecred-lidernegocios/application/models/DbTable/ConsultaHiscon.php
提示:
您可以编辑任何文件(包括二进制文件),但请注意不当修改可能导致文件损坏。
<?php class Model_DbTable_ConsultaHiscon extends Zend_Db_Table_Abstract { protected $_primary = 'id'; protected $_name = 'tb_consultas_multibr'; protected $_referenceMap = array( 'Corretor' => array( 'columns' => array('corretor_id'), 'refTableClass' => 'Model_DbTable_Corretor', 'refColumns' => array('id') ), ); /** * * @param integer $idCorretor Se nulo, pega o id do corretor logado * @param bool $apenasEsteMes [default=true] se false, pega de todos os tempos * @param string $tipo A = Créditos avulsos, M = Créditos mensais * @return Zend_Db_Table_Rowset */ public function consultaHistorico($idCorretor = null, $apenasEsteMes = false, $tipo = null) { if (!$idCorretor && Yuppie_Auth::hasIdentity() && Yuppie_Auth::getIdentity()->grupo == 'Corretor') { $idCorretor = Yuppie_Auth::getIdentity()->id; } $where = array("corretor_id = " . $idCorretor); if ($apenasEsteMes) { $date = Zend_Date::now()->setDay(1)->setTime('00:00:00') ->get('yyyy-MM-dd HH:mm:ss'); $where[] = "data_hora_consulta = '{$date}'"; } if ($tipo) { $where[] = "tipo LIKE '{$tipo}'"; } $select = $this->getAdapter()->select()->from($this->_name, array("qtd" => "count(id)")) ->where(implode(" AND ", $where)); return $this->getAdapter()->fetchOne($select); } /** * Retorna 'M' se ainda tiver créditos mensais restantes, * 'A' se tiver créditos avulsos, ou false se não tiver mais créditos * * @param integer $idCorretor [default=null] Se nulo usa o corretor logado * @return string|boolean 'M'|'A'|false */ public function corretorTemCreditos($idCorretor = null) { if ($idCorretor != null) { $modelCorretor = new Model_Corretor(); $corretor = $modelCorretor->exibir($idCorretor); } else { $corretor = Yuppie_Auth::getIdentity(); } $creditosAvulsos = $this->calculaCreditosAvulsosRestantes($corretor->id); return $creditosAvulsos > 0 ? 'A' // tem creditos avulsos restantes : false; // não tem créditos restantes } public function calculaCreditosAvulsosRestantes($idCorretor=null) { // consultas feitas utilizando creditos avulsos $consultasFeitas = $this->consultaHistorico($idCorretor); $corretorCredito = new Model_DbTable_CorretorCredito(); $totalCreditosAvulsos = $corretorCredito->somaPorCorretor($idCorretor); return $totalCreditosAvulsos - $consultasFeitas; } public function calculaCreditosRestantes($idCorretor=null) { $modelCorretor = new Model_Corretor(); $corretor = $modelCorretor->exibir($idCorretor); $creditosAvulsos = $this->calculaCreditosAvulsosRestantes($corretor->id); return $creditosAvulsos; } public function saldosMb($corretor_id = null){ $selectCountConsultas = $this->getAdapter()->select()->from('tb_consultas_multibr', array('gastos' => 'COUNT(id)', 'corretor_id'))->group('corretor_id'); $selectCountCreditos = $this->getAdapter()->select()->from('tb_corretores_creditos', array('creditos' => 'SUM(credito)', 'corretor_id'))->where('excluido <> 1')->group('corretor_id'); $select = $this->getAdapter()->select()->from(array('corretores' => 'tb_corretores'), array('id' => 'corretores.id','corretor_nome' => 'corretores.nome')) ->joinLeft(array('consultas_multi' => $selectCountConsultas), "corretores.id = consultas_multi.corretor_id", array('gastos')) ->joinLeft(array('corretores_creditos' => $selectCountCreditos), "corretores.id = corretores_creditos.corretor_id", array('creditos')); if(!is_null($corretor_id)){ $select->where('corretores.id = ' . $corretor_id); } return $this->getAdapter()->fetchAll($select); } public function insert(array $data) { $db = Zend_Db_Table::getDefaultAdapter(); $db->getConnection(); $db->closeConnection(); if (Yuppie_Auth::getIdentity()->grupo == 'Corretor') { $data['corretor_id'] = Yuppie_Auth::getIdentity()->id; } elseif (isset(Yuppie_Auth::getIdentity()->corretor_id) && Yuppie_Auth::getIdentity()->corretor_id > 0) { $data['corretor_id'] = Yuppie_Auth::getIdentity()->corretor_id; } else { $data['empresa_id'] = Yuppie_Auth::getIdentity()->empresa_id; } $data['data_hora_consulta'] = Zend_Date::now()->get('yyyy-MM-dd HH:mm:ss'); $data['usuario_id'] = Yuppie_Auth::getIdentity()->login; if( empty($data['cpf']) || !Validators_Cpf::validate($data['cpf']) ){ return null; // throw new Core_Exception("CPF não pode estar vazio."); } $id = parent::insert($data); //grava inserção no log if( $id > 0 ){ $log = new Model_DbTable_Log(); $log->save($this, $data, 'insert', $id); } return $id; } public function getConsultasPorEmpresa($empresaId) { $where = array( "empresa_id = {$empresaId}", "DATE_FORMAT(data_hora_consulta, '%m/%Y') = '" . date('m/Y') . "'" ); $consultas = $this->fetchAll($where); return $consultas; } public function getConsultasNb(array $fields, $order = null) { $consultaContrato = $this->getAdapter()->select() ->from(array('contratos' => 'tb_contratos'), array('numero_contrato', 'data_emissao')) ->joinLeft(array('corretores' => 'tb_corretores'), 'contratos.corretor_id = corretores.id', array('corretor_contrato' => 'nome')) ->joinInner(array('clientes' => 'tb_clientes'), 'contratos.cliente_id = clientes.id', array('cpf')) ->where('contratos.tipo_registro = 1') ->where('contratos.excluido <> 1') ->group('clientes.cpf') ->order('contratos.id DESC'); $select = $this->getAdapter()->select()->from(array('consultas_multi' => 'tb_consultas_multibr'), array('corretores.nome', 'tipo', 'data_hora_consulta', 'beneficio', 'cpf_beneficio' => 'cpf', 'margem', 'margem_cartao', 'margem_cartao_rmc', 'usuario' => 'usuario_id', 'orgao_consulta', 'saldo_fgts')) ->joinLeft(array('corretores' => 'tb_corretores'), "consultas_multi.corretor_id = corretores.id", array()) ->joinLeft(array('contratos' => $consultaContrato), "consultas_multi.cpf = contratos.cpf AND contratos.data_emissao >= DATE_FORMAT(consultas_multi.data_hora_consulta, '%Y-%m-%d')", array('numero_contrato', 'cpf', 'corretor_contrato')) ->order($order); //filtros if(isset($fields['corretor_id']) && !empty($fields['corretor_id'])){ $select->where("consultas_multi.corretor_id = '" . $fields['corretor_id'] . "'"); } if( isset($fields['tipo']) && !empty($fields['tipo'])){ $select->where("consultas_multi.tipo = '" . $fields['tipo'] . "'"); } if( isset($fields['orgao_consulta']) && !empty($fields['orgao_consulta'])){ $select->where("consultas_multi.orgao_consulta = '" . $fields['orgao_consulta'] . "'"); } if( isset($fields['data_inicio']) && !empty($fields['data_inicio']) ){ if( Zend_Date::isDate($fields['data_inicio'], 'dd/MM/yyyy') ){ $dataInicio = new Zend_Date($fields['data_inicio'], 'dd/MM/yyyy'); if( !$fields['data_fim'] ){ $dataFim = Zend_Date::now(); $fields['data_fim'] = $dataFim->get('dd/MM/yyyy'); } else if( Zend_Date::isDate($fields['data_fim'], 'dd/MM/yyyy') ){ $dataFim = new Zend_Date($fields['data_fim'], 'dd/MM/yyyy'); } $select->where("DATE(data_hora_consulta) >= '" . $dataInicio->get('yyyy-MM-dd') . "'" . " AND DATE(data_hora_consulta) <= '" . $dataFim->get('yyyy-MM-dd') . "'"); } } //verifica as permissoes as quais vão fazer a consulta nb if( Yuppie_Auth::getIdentity()->corretor_id ){ $select->where("corretor_id = " . Yuppie_Auth::getIdentity()->corretor_id); } if( Yuppie_Auth::getIdentity()->grupo == 'Supervisor' ){ if( !array_key_exists('rotas', $select->getPart(Zend_Db_Select::FROM)) ){ $select->joinLeft(array( 'rotas' => 'tb_rotas' ), 'corretores.rota_id = rotas.id', array('gerente_id')); } $select->where('rotas.gerente_id = ' . Yuppie_Auth::getIdentity()->id); } elseif( Yuppie_Auth::getIdentity()->grupo == 'Gerente Regional' ){ if( !array_key_exists('rotas', $select->getPart(Zend_Db_Select::FROM)) ){ $select->joinLeft(array( 'rotas' => 'tb_rotas' ), 'corretores.rota_id = rotas.id', array('gerente_id')); } if( !array_key_exists('supervisores', $select->getPart(Zend_Db_Select::FROM)) ){ $select->joinLeft(array( 'supervisores' => 'tb_gerente_rota' ), 'supervisores.id = rotas.gerente_id', array()); } $select->where('supervisores.gerente_id = ' . Yuppie_Auth::getIdentity()->id); if( !Yuppie_Auth::getIdentity()->callcenter ){ $select->where('supervisores.gerente_id = ' . Yuppie_Auth::getIdentity()->id); } else { $fields['callcenter'] = 1; } } elseif( Yuppie_Auth::getIdentity()->grupo == 'Corretor' ){ $select->where('corretores.id = ' . Yuppie_Auth::getIdentity()->id); } elseif( Yuppie_Auth::getIdentity()->corretor_id > 0 ){ $select->where('corretores.id = ' . Yuppie_Auth::getIdentity()->corretor_id); } if( $order != null ){ $select->order($order); } else { $select->order(array( 'data_hora_consulta DESC')); } return $this->getAdapter()->fetchAll($select); } public function getConsultasNbSintetico(array $fields, $order = null) { $consultaContrato = $this->getAdapter()->select() ->from(array('contratos' => 'tb_contratos'),['data_emissao']) ->joinLeft(array('corretores' => 'tb_corretores'), 'contratos.corretor_id = corretores.id', array('corretor_contrato' => 'nome')) ->joinInner(array('clientes' => 'tb_clientes'), 'contratos.cliente_id = clientes.id',['cpf']) ->where('contratos.tipo_registro = 1') ->where('contratos.excluido <> 1'); $select = $this->getAdapter()->select()->from(array('consultas_multi' => 'tb_consultas_multibr'), array('corretores.nome')) ->joinLeft(array('corretores' => 'tb_corretores'), "consultas_multi.corretor_id = corretores.id",['COUNT(corretor_id) as total_corretor']) ->joinLeft(array('contratos' => $consultaContrato), "consultas_multi.cpf = contratos.cpf AND contratos.data_emissao >= DATE_FORMAT(consultas_multi.data_hora_consulta, '%Y-%m-%d')",[]) ->order($order) ->group('corretor_id'); //filtros if(isset($fields['corretor_id']) && !empty($fields['corretor_id'])){ $select->where("consultas_multi.corretor_id = '" . $fields['corretor_id'] . "'"); } if( isset($fields['tipo']) && !empty($fields['tipo'])){ $select->where("consultas_multi.tipo = '" . $fields['tipo'] . "'"); } if( isset($fields['orgao_consulta']) && !empty($fields['orgao_consulta'])){ $select->where("consultas_multi.orgao_consulta = '" . $fields['orgao_consulta'] . "'"); } if( isset($fields['data_inicio']) && !empty($fields['data_inicio']) ){ if( Zend_Date::isDate($fields['data_inicio'], 'dd/MM/yyyy') ){ $dataInicio = new Zend_Date($fields['data_inicio'], 'dd/MM/yyyy'); if( !$fields['data_fim'] ){ $dataFim = Zend_Date::now(); $fields['data_fim'] = $dataFim->get('dd/MM/yyyy'); } else if( Zend_Date::isDate($fields['data_fim'], 'dd/MM/yyyy') ){ $dataFim = new Zend_Date($fields['data_fim'], 'dd/MM/yyyy'); } $select->where("DATE(data_hora_consulta) >= '" . $dataInicio->get('yyyy-MM-dd') . "'" . " AND DATE(data_hora_consulta) <= '" . $dataFim->get('yyyy-MM-dd') . "'"); } } //verifica as permissoes as quais vão fazer a consulta nb if( Yuppie_Auth::getIdentity()->corretor_id ){ $select->where("corretor_id = " . Yuppie_Auth::getIdentity()->corretor_id); } if( Yuppie_Auth::getIdentity()->grupo == 'Supervisor' ){ if( !array_key_exists('rotas', $select->getPart(Zend_Db_Select::FROM)) ){ $select->joinLeft(array( 'rotas' => 'tb_rotas' ), 'corretores.rota_id = rotas.id', array('gerente_id')); } $select->where('rotas.gerente_id = ' . Yuppie_Auth::getIdentity()->id); } elseif( Yuppie_Auth::getIdentity()->grupo == 'Gerente Regional' ){ if( !array_key_exists('rotas', $select->getPart(Zend_Db_Select::FROM)) ){ $select->joinLeft(array( 'rotas' => 'tb_rotas' ), 'corretores.rota_id = rotas.id', array('gerente_id')); } if( !array_key_exists('supervisores', $select->getPart(Zend_Db_Select::FROM)) ){ $select->joinLeft(array( 'supervisores' => 'tb_gerente_rota' ), 'supervisores.id = rotas.gerente_id', array()); } $select->where('supervisores.gerente_id = ' . Yuppie_Auth::getIdentity()->id); if( !Yuppie_Auth::getIdentity()->callcenter ){ $select->where('supervisores.gerente_id = ' . Yuppie_Auth::getIdentity()->id); } else { $fields['callcenter'] = 1; } } elseif( Yuppie_Auth::getIdentity()->grupo == 'Corretor' ){ $select->where('corretores.id = ' . Yuppie_Auth::getIdentity()->id); } elseif( Yuppie_Auth::getIdentity()->corretor_id > 0 ){ $select->where('corretores.id = ' . Yuppie_Auth::getIdentity()->corretor_id); } if( $order != null ){ $select->order($order); } else { $select->order(array( 'data_hora_consulta DESC')); } return $this->getAdapter()->fetchAll($select); } }
💾 保存文件
← 返回文件管理器