✏️ 正在编辑: GerenteRota.php
路径:
/srv/systems_dir/yuppiecred-lidernegocios/application/models/GerenteRota.php
提示:
您可以编辑任何文件(包括二进制文件),但请注意不当修改可能导致文件损坏。
<?php class Model_GerenteRota extends Model_PessoaFisica { private $_empresaId; private $_gerenteId; private $_apelido; private $_banco; private $_agencia; private $_conta; private $_tipoConta; //Corrente / Poupança private $_titular; private $_cpfTitular; private $_obs; private $_login; private $_senha; private $_valorComissao; private $_prazo; private $_exibirRanking; private $_tetoComissao; private $_receberEmailCopia; private $_atuarChamado; private $_ativo; //<editor-fold> public function getEmpresaId() { return $this->_empresaId; } public function setEmpresaId($empresaId) { $this->_empresaId = $empresaId; } public function getGerenteId() { return $this->_gerenteId; } public function setGerenteId($gerenteId) { $this->_gerenteId = $gerenteId; } public function getApelido() { return $this->_apelido; } public function setApelido($apelido) { $this->_apelido = $apelido; } public function getBanco() { return $this->_banco; } public function setBanco($banco) { $this->_banco = $banco; } public function getAgencia() { return $this->_agencia; } public function setAgencia($agencia) { $this->_agencia = $agencia; } public function getConta() { return $this->_conta; } public function setConta($conta) { $this->_conta = $conta; } public function getTipoConta() { return $this->_tipoConta; } public function setTipoConta($tipoConta) { $this->_tipoConta = $tipoConta; } public function getTitular() { return $this->_titular; } public function setTitular($titular) { $this->_titular = $titular; } public function getCpfTitular() { return $this->_cpfTitular; } public function setCpfTitular($cpfTitular) { $this->_cpfTitular = $cpfTitular; } public function getObs() { return $this->_obs; } public function setObs($obs) { $this->_obs = $obs; } public function getLogin() { return $this->_login; } public function setLogin($login) { $this->_login = $login; } public function getSenha() { return $this->_senha; } public function setSenha($senha) { $this->_senha = $senha; } public function getValorComissao() { return $this->_valorComissao; } public function setValorComissao($valorComissao) { $this->_valorComissao = str_replace(array( '.', ',' ), array( '', '.' ), $valorComissao); } public function getPrazo() { return $this->_prazo; } public function setPrazo($prazo) { $this->_prazo = $prazo; } public function getExibirRanking() { return $this->_exibirRanking; } public function setExibirRanking($exibirRanking) { $this->_exibirRanking = $exibirRanking; } public function getTetoComissao() { return $this->_tetoComissao; } public function setTetoComissao($tetoComissao) { $this->_tetoComissao = str_replace(array( '.', ',' ), array( '', '.' ), $tetoComissao); } public function getReceberEmailCopia() { return $this->_receberEmailCopia; } public function setReceberEmailCopia($receberEmailCopia) { $this->_receberEmailCopia = $receberEmailCopia; } public function getAtuarChamado() { return $this->_atuarChamado; } public function setAtuarChamado($atuarChamado) { $this->_atuarChamado = $atuarChamado; } public function getAtivo() { return $this->_ativo; } public function setAtivo($ativo) { $this->_ativo = $ativo; } //</editor-fold> public function __construct($options = null) { if( is_array($options) ){ $this->setOptions($options); } } public function setOptions(array $options) { $methods = get_class_methods($this); foreach ( $options as $key => $value ) { $explode = explode("_", $key); $methodName = ''; foreach ( $explode as $v ) { $methodName .= ucfirst($v); } $key = $methodName != '' ? $methodName : $key; $method = 'set' . ucfirst($key); if( in_array($method, $methods) ){ $this->$method($value); } } return $this; } public function _toArray() { $dbtable = new Model_DbTable_GerenteRota(); $info = $dbtable->info(); $methods = get_class_methods($this); $data = array(); 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(); } } return $data; } /** * Insere ou altera o registro passado por parâmetro * @param Model_GerenteRota $gerente */ public function salvar(Model_GerenteRota $gerente) { $dbtable = new Model_DbTable_GerenteRota(); if( $gerente->getId() > 0 ){ $dbtable->update($gerente->_toArray(), "id = " . $gerente->getId()); return $gerente->getId(); } else { return $dbtable->insert($gerente->_toArray()); } } /** * 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_GerenteRota(); return $dbtable->delete("id = " . $id); } return null; } /** * Retorna uma lista * @param string $where * @param string $order * @param int $count * @param int $offset * @return array */ public function listar($where = null, $order = null, $count = null, $offset = null) { $dbtable = new Model_DbTable_GerenteRota(); 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_GerenteRota(); return $dbtable->fetchRow("id = " . $id); } public function listagem($fields, $order = "nome ASC") { $params = array(); if( isset($fields['gerente_id']) && $fields['gerente_id'] > 0 ){ $params[] = "gerente_id = " . $fields['gerente_id']; } if( isset($fields['nome']) && $fields['nome'] != '' ){ $params[] = "nome LIKE '%" . $fields['nome'] . "%'"; } $where = null; if( count($params) > 0 ) $where = implode(" AND ", $params); $dbtable = new Model_DbTable_GerenteRota(); return $dbtable->fetchAll($where, $order); } public function autenticar($username, $password, $table, $grupo = false, $horario = false) { $autenticado = parent::autenticar($username, $password, $table, $grupo, $horario); if($autenticado){ $id = Yuppie_Auth::getIdentity()->id; $identity = Yuppie_Auth::getIdentity(); $rota = new Model_DbTable_Rota(); $rotas = $rota->fetchRow("gerente_id = {$id}"); $identity->rota_id = $rotas['id']; } return $autenticado; } //retorna a comissão do supervisor entre os percentuais public static function getPercentual($objContrato, $gerenteId, $dataEmissao) { $dbTable = new Model_DbTable_PercentualSupervisor(); $valorComissao = 0; $where = "gerente_id = " . $gerenteId . " AND banco_id = " . $objContrato['banco_id'] . " AND tabela_id = " . $objContrato['tabela_id'] . " AND convenio_id = " . $objContrato['convenio_id'] . " AND prazo = '" . $objContrato['prazo'] . "'" . " AND tipo_produto = '" . $objContrato['tipo'] . "'"; $valor = @(($objContrato['comissao_corretor'] + $objContrato['bonus_corretor']) / $objContrato['comissao_empresa'] * 100); $formatValor = number_format($valor, 6, '.', '.'); $percentual = "(percentual_de <= '{$formatValor}'" . " AND percentual_a >= '{$formatValor}')"; $selectPercentual = $dbTable->getAdapter()->select()->from(array( 'percentual_supervisores' => 'tb_percentual_supervisor' ), array( 'comissao', 'data_inicial', 'data_final' )) ->where($where) ->where($percentual) ->where('excluido <> 1'); $result = $dbTable->getAdapter()->fetchRow($selectPercentual); if( !$result && empty($result) ){ //retira as casas decimais e deixa com 4 sem arredondar $otherFormatValor = intval(strval($valor * 10000)) / 10000; $percentualFormat = "(percentual_de <= '{$otherFormatValor}'" . " AND percentual_a >= '{$otherFormatValor}')"; $selectPercentual = $dbTable->getAdapter()->select()->from(array( 'percentual_supervisores' => 'tb_percentual_supervisor' ), array( 'comissao', 'data_inicial', 'data_final' )) ->where($where) ->where($percentualFormat) ->where('excluido <> 1'); $otherPercentual = $dbTable->getAdapter()->fetchRow($selectPercentual); if( $otherPercentual ){ $valorComissao = number_format( $otherPercentual['comissao'], 6, '.', '.'); } else { $arrendondarPercentual = number_format($valor, 2, '.', '.'); $percentualArrendondado = "(percentual_de <= '{$arrendondarPercentual}'" . " AND percentual_a >= '{$arrendondarPercentual}')"; $selectPercentual = $dbTable->getAdapter()->select()->from(array( 'percentual_supervisores' => 'tb_percentual_supervisor' ), array( 'comissao', 'data_inicial', 'data_final' )) ->where($where) ->where($percentualArrendondado) ->where('excluido <> 1'); $percentualArrendondado = $dbTable->getAdapter()->fetchRow($selectPercentual); $valorComissao = number_format( $percentualArrendondado['comissao'], 6, '.', '.'); } } elseif( $result ){ $valorComissao = number_format( $result['comissao'], 6, '.', '.'); } return $valorComissao; } public function listarFaixaValores(array $fields, $page = null, $limit = null, $order = 'convenio ASC', $range = null) { $dbtable = new Model_DbTable_PercentualSupervisor(); $select = $dbtable->getAdapter()->select() ->from(array( 'percentuais' => 'tb_percentual_supervisor' )) ->joinInner(array( 'supervisores' => 'tb_gerente_rota' ), 'supervisores.id = percentuais.gerente_id', array( 'supervisor' => 'supervisores.nome' )) ->joinLeft(array( 'tabelas' => 'tb_tabelas' ), 'percentuais.tabela_id = tabelas.id', array( 'tabela' => 'nome' )) ->joinLeft(array( 'convenios' => 'tb_convenios' ), 'percentuais.convenio_id = convenios.id', array( 'convenio' => 'nome' )) ->joinLeft(array( 'bancos' => 'tb_bancos' ), 'percentuais.banco_id = bancos.id', array( 'banco' => 'nome' )) ->where('percentuais.excluido <> 1 AND tabelas.excluido<>1') ->order($order) ->group('percentuais.id'); if( isset($fields['supervisor_id']) && !empty($fields['supervisor_id']) ){ $select->where('percentuais.gerente_id = ?', $fields['supervisor_id']); } if( isset($fields['banco_id']) && !empty($fields['banco_id']) ){ $select->where('percentuais.banco_id = ?', $fields['banco_id']); } if( isset($fields['convenio_id']) && !empty($fields['convenio_id']) ){ $select->where('percentuais.convenio_id = ?', $fields['convenio_id']); } if( isset($fields['tabela_id']) && !empty($fields['tabela_id']) ){ $select->where('percentuais.tabela_id = ?', $fields['tabela_id']); } if( isset($fields['tipo_produto']) && !empty($fields['tipo_produto']) ){ $select->where('percentuais.tipo_produto = ?', $fields['tipo_produto']); } if( isset($fields['prazo']) && !empty($fields['prazo']) ){ $select->where('percentuais.prazo = ?', $fields['prazo']); } if( isset($fields['data_de']) && !empty($fields['data_de']) && isset($fields['data_a']) && !empty($fields['data_a']) ){ $select->where("(percentuais.data_inicial >= STR_TO_DATE(?, '%d/%m/%Y')", $fields['data_de']) ->where("percentuais.data_final <= STR_TO_DATE(?, '%d/%m/%Y'))", $fields['data_a']); } if( isset($fields['data_final']) && $fields['data_final'] === 'null' ){ $select->where("percentuais.data_final IS NULL OR percentuais.data_final = ?", '0000-00-00'); } if( $order != null ){ $select->order($order); } else { $select->order(array( 'supervisores.id ASC' )); } 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; } return $dbtable->getAdapter()->fetchAll($select); } public function meuDesempenhoComercial($fields = array()) { $dataNow = new Zend_Date(); $result = array(); $fields['data_emissao_de'] = $dataNow->setDay(1)->get('dd/MM/yyyy'); $fields['data_emissao_a'] = $dataNow->now()->get('dd/MM/yyyy'); // $fields['status_proposta'] = '43'; if( isset($fields['gerente_rota_id']) && !empty($fields['gerente_rota_id']) ){ $fields['gerente_id'] = $fields['gerente_rota_id']; unset($fields['gerente_rota_id']); } $contrato = new Model_Contrato(); $producaoComercial = $contrato->getDesempenhoComercial($fields); if( $producaoComercial ){ $fields['supervisor'] = true; $result = $this->_prepareProducaoComercial($producaoComercial, $fields); } return $result; } protected function _selectDesempenhoComercial(array $fields) { $dbtable = new Model_DbTable_Contrato(); $statusId = Model_Contrato::findByNameStatus(array( "Operação Finalizada", "Pagamento Enviado" )); $statusId = "'" . implode("','", $statusId) . "'"; $where = "contratos.excluido<>1 AND clientes.excluido<>1"; $columns = array( 'total_liquido' => 'SUM(base_comissao_empresa_valor)', 'banco_id' => 'contratos.banco_id', 'banco_nome' => 'bancos.nome', 'data' => "DATE_FORMAT(data_liberacao, '%d/%m/%Y')" ); $select = $dbtable->getAdapter()->select()->from(array( 'contratos' => 'tb_contratos' ), array()) ->columns($columns) ->joinInner(array( "clientes" => "tb_clientes" ), "contratos.cliente_id = clientes.id", array()) ->joinInner(array( "corretores" => "tb_corretores" ), "contratos.corretor_id = corretores.id", array()) ->joinLeft(array( "bancos" => 'tb_bancos' ), 'bancos.id = contratos.banco_id', array()) ->joinLeft(array( 'rota' => 'tb_rotas' ), 'corretores.rota_id = rota.id', array()) ->joinLeft(array( 'produtos' => 'tb_produtos' ), 'produtos.id = contratos.produto', array()) ->joinLeft(array( 'gerentes_rotas' => 'tb_gerente_rota' ), 'rota.gerente_id = gerentes_rotas.id', array( 'supervisor' => 'gerentes_rotas.nome', 'supervisor_id' => 'gerentes_rotas.id' )) ->joinLeft(array( 'gerentes' => 'tb_gerentes' ), 'gerentes_rotas.gerente_id = gerentes.id', array( 'gerente' => 'gerentes.nome', 'gerente_id' => 'gerentes.id' )) ->where("contratos.tipo_registro = 1") ->where("contratos.status_proposta IN ($statusId)") ->where("contratos.tipo not in ('PLASTICO', 'ATIVACAO')") ->where($where) ->group(array( 'contratos.banco_id', 'data', 'gerentes.id', 'gerentes_rotas.id' )); $this->_prepareFilterSelect($fields, $select); return $dbtable->getAdapter()->fetchAll($select); } protected function _selectDesempenhoComercial2(array $fields) { unset($fields['banco_id']); $dbtable = new Model_DbTable_Contrato(); $dataInicial = new Zend_Date($fields['data_liberacao_de']); $dataInicial = $dataInicial->get('YYYY-MM-dd'); $dataFinal = new Zend_Date($fields['data_liberacao_a']); $dataFinal = $dataFinal->get('YYYY-MM-dd'); $statusId = Model_Contrato::findByNameStatus(array( "Operação Finalizada", "Pagamento Enviado" )); $statusId = "'" . implode("','", $statusId) . "'"; $where = "contratos.excluido<>1 AND clientes.excluido<>1"; $columns = array( 'total_liquido' => 'SUM(valor_liquido)', 'data' => "DATE_FORMAT(data_liberacao, '%d/%m/%Y')" ); $select = $dbtable->getAdapter()->select()->from(array( 'contratos' => 'tb_contratos' ), array( new Zend_Db_Expr("'META GERAL' as banco_nome"))) ->columns($columns) ->joinInner(array( "clientes" => "tb_clientes" ), "contratos.cliente_id = clientes.id", array()) ->joinInner(array( "corretores" => "tb_corretores" ), "contratos.corretor_id = corretores.id", array()) ->joinLeft(array( 'rota' => 'tb_rotas' ), 'corretores.rota_id = rota.id', array()) ->joinLeft(array( 'produtos' => 'tb_produtos' ), 'produtos.id = contratos.produto', array()) ->joinLeft(array( 'gerentes_rotas' => 'tb_gerente_rota' ), 'rota.gerente_id = gerentes_rotas.id', array( 'supervisor' => 'gerentes_rotas.nome', 'supervisor_id' => 'gerentes_rotas.id' )) ->joinLeft(array( 'gerentes' => 'tb_gerentes' ), 'gerentes_rotas.gerente_id = gerentes.id', array( 'gerente' => 'gerentes.nome', 'gerente_id' => 'gerentes.id' )) // ->joinLeft(array( 'metas' => 'tb_gerentes_metas' ), "metas.gerente_id = gerentes_rotas.id and metas.banco_id = 0 and data_inicial <= {$dataInicial} and data_final >= {$dataFinal} ", array('meta')) ->where("contratos.tipo_registro = 1") ->where("contratos.status_proposta IN ($statusId)") ->where("contratos.tipo not in ('PLASTICO', 'ATIVACAO')") ->group('gerentes_rotas.id') ->where($where); $this->_prepareFilterSelect($fields, $select); return $dbtable->getAdapter()->fetchAll($select); } protected function _prepareFilterSelect(array $fields, Zend_Db_Select &$select) { if( isset($fields['ranking_gerente']) && !empty($fields['ranking_gerente']) ){ $select->where("gerentes.id IS NOT NULL"); unset($fields['gerente_comercial_id']); } if( isset($fields['ranking_supervisor']) && !empty($fields['ranking_supervisor']) ){ unset($fields['gerente_rota_id']); } if( isset($fields['gerente_id']) && !empty($fields['gerente_id']) ){ $select->where("gerentes.id IS NOT NULL"); if( !array_key_exists('rotas', $select->getPart(Zend_Db_Select::FROM)) ){ $select->joinLeft(array( 'rotas' => 'tb_rotas' ), 'corretores.rota_id = rotas.id', array()); } $select->where("rotas.gerente_id = " . $fields['gerente_id']); } if( isset($fields['gerente_comercial_id']) && !empty($fields['gerente_comercial_id']) ){ $select->where("gerentes.id = " . $fields['gerente_comercial_id']); } if( isset($fields['ultima_data_liberacao']) && !empty($fields['ultima_data_liberacao']) ){ $select->where("(data_liberacao = STR_TO_DATE('" . $fields['ultima_data_liberacao'] . "', '%d/%m/%Y'))"); } if( isset($fields['data_liberacao_de']) && !empty($fields['data_liberacao_de']) && isset($fields['data_liberacao_a']) && !empty($fields['data_liberacao_a']) ){ $select->where("(data_liberacao >= STR_TO_DATE('" . $fields['data_liberacao_de'] . "', '%d/%m/%Y')" . " AND data_liberacao <= STR_TO_DATE('" . $fields['data_liberacao_a'] . "', '%d/%m/%Y'))"); } if( isset($fields['gerente_rota_id']) && !empty($fields['gerente_rota_id']) ){ $select->where("gerentes.id = " . $fields['gerente_rota_id']); } if( isset($fields['rota_id']) && !empty($fields['rota_id']) ){ $select->where("corretores.rota_id = " . $fields['rota_id']); } if( isset($fields['status_proposta']) && !empty($fields['status_proposta']) ){ if( is_array($fields['status_proposta']) ){ $fields['status_proposta'] = array_filter($fields['status_proposta']); $select->where("contratos.status_proposta IN ('" . implode("','", $fields['status_proposta']) . "')"); } else { $select->where('contratos.status_proposta = ' . $fields['status_proposta']); } } } public function rankingDesempenhoComercial(array $fields, $condition = null) { $producao = array(); $dataNow = new Zend_Date(); $fields['data_liberacao_de'] = $dataNow->setDay(1)->get('dd/MM/yyyy'); $fields['data_liberacao_a'] = $dataNow->now()->get('dd/MM/yyyy'); $fields['ranking_gerente'] = true; $fields['ranking_supervisor'] = true; $producaoComercial = $this->_selectDesempenhoComercial($fields); if( $producaoComercial ){ foreach ( $producaoComercial as $producaoDados ) { switch ( $condition ) { case "Gerente Regional": $treatmentName = 'gerente'; break; case "Supervisor": $treatmentName = 'supervisor'; break; } if( !isset($producao[$producaoDados[$treatmentName]]) ){ $producao[$producaoDados[$treatmentName]] = array( 'projecao_final_mes' => 0 ); } $dt = new Zend_Date(); $qtdDiasConsulta = $dt->get(Zend_Date::DAY); $qtdDiasDoMes = $dt->get(Zend_Date::MONTH_DAYS); $mediaDiaria = $producaoDados['total_liquido'] / $qtdDiasConsulta; $projecaoFinalMes = $mediaDiaria * $qtdDiasDoMes; $fields['banco_id'] = $producaoDados['banco_id']; $fields['data_inicial'] = $producaoDados['data']; $fields['data_final'] = $producaoDados['data']; $producao[$producaoDados[$treatmentName]]['projecao_final_mes'] += $projecaoFinalMes; } arsort($producao); } return $producao; } public function _prepareProducaoComercial($producaoComercial = array(), $fields = array()) { $producaoSupervisor = array(); $modelMeta = new Model_GerenteMeta(); $metaEmpresa = Model_Empresa::getMetaEmpresa(); $modelCorretor = new Model_Corretor(); foreach ( $producaoComercial as $registro ) { if( !isset($producaoSupervisor[$registro['supervisor_id']]) ){ $producao[$registro['supervisor_id']] = array( 'producao_ultimo_dia_util' => 0, 'total_producao' => 0, 'liberado_atual_supervisor' => 0, 'producao_media_diaria' => 0, 'projecao_final_mes' => 0, 'percentual_empresa' => 0, 'nome' => null ); } $totalCorretoresCadastrados = $modelCorretor->getTotalCorretoresCadastrados($registro); $totalCorretoresProduzindo = $modelCorretor->getTotalCorretoresProduzindo($registro, $fields); $producaoUltimoDiaUtil = $this->_producaoUltimoDiaUtil($registro); $producaoAtualTotal = $this->_producaoAtual($registro); $dt = new Zend_Date(); $qtdDiasConsulta = $dt->get(Zend_Date::DAY); $qtdDiasDoMes = $dt->get(Zend_Date::MONTH_DAYS); $mediaDiaria = $registro['base_comissao_empresa_total'] / $qtdDiasConsulta; $projecaoFinalMes = $mediaDiaria * $qtdDiasDoMes; $fields['data_inicial'] = Zend_Date::now()->get('dd/MM/yyyy'); $fields['data_final'] = Zend_Date::now()->get('dd/MM/yyyy'); $fields['gerente_rota_id'] = $registro['supervisor_id']; $totalMeta = $modelMeta->getProducaoFaixaValores($fields, null, $registro); if ($producao[$registro["supervisor_id"]]['liberado_atual_supervisor']== 0){ $producao[$registro["supervisor_id"]]['liberado_atual_supervisor'] = $this->_producaoAtual($registro); } $producao[$registro["supervisor_id"]]['producao_ultimo_dia_util'] = $producaoUltimoDiaUtil; $producao[$registro["supervisor_id"]]['total_producao'] = $registro['base_comissao_empresa_total']; $producao[$registro["supervisor_id"]]['producao_media_diaria'] += $mediaDiaria; $producao[$registro["supervisor_id"]]['projecao_final_mes'] += $projecaoFinalMes; $producao[$registro['supervisor_id']]['meta'] = isset($totalMeta) ? $totalMeta['total_meta'] : 0; $producao[$registro['supervisor_id']]['percentual_empresa'] += @($projecaoFinalMes * 100 / $metaEmpresa); $producao[$registro['supervisor_id']]['nome'] = $registro['supervisor']; $producao[$registro['supervisor_id']]['total_corretores_cadastrados'] = $totalCorretoresCadastrados; $producao[$registro['supervisor_id']]['total_corretores_produzindo'] = $totalCorretoresProduzindo; } $util = new Core_Util(); $util->orderArrayMulti($producao, 'projecao_final_mes', 'desc'); return $producao; } public function _producaoUltimoDiaUtil($registro = array(), $data = null) { $dataNow = new Zend_Date(); $day = 1; $seg = 3; if($data){ $dataNow->setDate($data); $day = 0 ; $seg = 0; } $diaUtil = $dataNow->get(Zend_Date::WEEKDAY_NAME); switch ( $diaUtil ) { case 'seg': $ultimaDataUtil = $dataNow->subDay($seg); break; case 'dom': $ultimaDataUtil = $dataNow->subDay(2); break; case 'sáb': if($data){ $ultimaDataUtil = $dataNow->subDay(1); } break; default: $ultimaDataUtil = $dataNow->subDay($day); break; } $contrato = new Model_Contrato(); $fields['data_liberacao_de'] = $ultimaDataUtil->get('dd/MM/yyyy'); $fields['data_liberacao_a'] = $ultimaDataUtil->get('dd/MM/yyyy'); $fields['gerente_id'] = $registro['supervisor_id']; $fields['banco_id'] = isset($registro['banco_id'])?$registro['banco_id']:null; $statusId = Model_Contrato::findByNameStatus(array( "Operação Finalizada", "Pagamento Enviado" )); $fields['status_proposta'] = $statusId; $result = $contrato->getProducaoSupervisorTotalizador($fields); if( $result['total_liquido'] ){ return $result['total_liquido']; }else{ return 0; } } public function relatorioProducaoComercial($fields = array(), $page = null, $limit = null, $order = null, $range = null, $method = '_selectDesempenhoComercial') { $result = null; $producaoComercial = $this->$method($fields); $modelMeta = new Model_GerenteMeta(); foreach ($producaoComercial as $key => $value) { if($value['banco_nome'] == 'META GERAL'){ $meta = $modelMeta->getProducaoFaixaValores( array( 'data_inicial' => $fields['data_liberacao_de'], 'data_final' => $fields['data_liberacao_a'], 'gerente' => $value['gerente'] ), null, array('gerente_id' => $value['gerente_id'])); if(!$meta){ unset($producaoComercial[$key]); } } } if ($producaoComercial) { $result = $this->_prepareProducaoComercialGerente($producaoComercial, $fields, $page, $limit, $order, $range); } return $result; } public function _prepareProducaoComercialGerente($producaoComercial = array(), $fields = array(), $page = null, $limit = null, $order = null, $range = null) { $modelMeta = new Model_GerenteMeta(); $producao = array(); foreach ( $producaoComercial as $registro ) { if (!empty($fields['projecao_supervisor'])) { $fields['gerente_id'] = $registro['supervisor_id']; $treatName = 'supervisor'; } elseif (!empty($fields['projecao_gerente'])) { $fields['gerente_comercial_id'] = $registro['gerente_id']; $treatName = 'gerente'; } if( !isset($producao[$registro[$treatName]][$registro['banco_nome']]) ){ $producao[$registro[$treatName]][$registro['banco_nome']] = array( 'producao_ultimo_dia_util' => 0, 'total_producao' => 0, 'producao_media_diaria' => 0, 'projecao_final_mes' => 0 ); } $producaoUltimoDiaUtil = $this->_producaoUltimoDiaUtil($registro, $fields['data_liberacao_a']); $dt = new Zend_Date(); $qtdDiasConsulta = $dt->get(Zend_Date::DAY); $qtdDiasDoMes = $dt->get(Zend_Date::MONTH_DAYS); $mediaDiaria = $registro['total_liquido'] / $qtdDiasConsulta; $projecaoFinalMes = $mediaDiaria * $qtdDiasDoMes; $fields['banco_id'] = isset($registro['banco_id']) ? $registro['banco_id']: null; $fields['data_inicial'] = $fields['data_liberacao_de']; $fields['data_final'] = $fields['data_liberacao_a']; $totalMeta = $modelMeta->getProducaoFaixaValores($fields, null, $registro); $producao[$registro[$treatName]][$registro["banco_nome"]]['producao_ultimo_dia_util'] = $producaoUltimoDiaUtil; $producao[$registro[$treatName]][$registro["banco_nome"]]['total_producao'] += $registro['total_liquido']; $producao[$registro[$treatName]][$registro["banco_nome"]]['producao_media_diaria'] += $mediaDiaria; $producao[$registro[$treatName]][$registro["banco_nome"]]['projecao_final_mes'] += $projecaoFinalMes; $producao[$registro[$treatName]][$registro['banco_nome']]['meta'] = isset($totalMeta) ? $totalMeta['total_meta'] : 0; } if( $page ){ $paginator = Zend_Paginator::factory($producao); $paginator->setCurrentPageNumber($page); if( $limit && is_numeric($limit) ){ $paginator->setItemCountPerPage($limit); } if( $range && is_numeric($range) ){ $paginator->setPageRange($range); } return $paginator; } return $producao; } public function validarFaixaValores(array $data, $idSupervisor = null) { $dbtable = new Model_DbTable_GerenteRota(); $dadosSupervisor = $dbtable->fetchRow("id = {$idSupervisor}"); $tetoComissao = $dadosSupervisor->teto_comissao; $comissaoCorretor = str_replace(array( '.', ',' ), array( '', '.' ), $data['comissao']); $modelComissionamento = new Model_DbTable_ComissionamentoCorretor(); $dadosComissionamento = $modelComissionamento->fetchRow("id = {$data['comissionamento_id']}"); $comissaoEmpresa = $this->_getComissaoEmpresa($dadosComissionamento); $modelBanco = new Model_Banco(); $dadosBancos = $modelBanco->exibir($dadosComissionamento->banco_id); $teto = ($comissaoEmpresa - $dadosBancos->margem) * $comissaoCorretor; if( $teto <= $tetoComissao ){ return false; } return true; } protected function _getComissaoEmpresa($dadosComissionamento) { $model = new Model_Comissionamento(); $valorComissao = 0; $comissoes = $model->listarPercValido($dadosComissionamento->toArray()); if( $comissoes ){ $valorComissao = $comissoes[0]['perc_comissao']; } return $valorComissao; } public function getSupervisorPorCorretorId($corretorId) { $dbtable = new Model_DbTable_GerenteRota(); $select = $dbtable->getAdapter()->select()->from(array( 'corretores' => 'tb_corretores' ), array( 'corretor' => 'corretores.nome' )) ->joinLeft(array( 'rotas' => 'tb_rotas' ), 'corretores.rota_id = rotas.id', array( '' )) ->joinLeft(array( 'supervisores' => 'tb_gerente_rota' ), 'supervisores.id = rotas.gerente_id', array( 'supervisores.email', 'supervisores.nome' )) ->where('corretores.id = ' . $corretorId); return $dbtable->getAdapter()->fetchRow($select, '', Zend_Db::FETCH_OBJ); } public function _producaoAtual(array $registro) { $dataNow = new Zend_Date(); $producaoTotal = 0; $contrato = new Model_Contrato(); $fields['data_liberacao_de'] = $dataNow->setDay(1)->get('dd/MM/yyyy'); $fields['data_liberacao_a'] = $dataNow->now()->get('dd/MM/yyyy'); $fields['gerente_id'] = $registro['supervisor_id']; $statusId = Model_Contrato::findByNameStatus(array( "Operação Finalizada", "Pagamento Enviado" )); $fields['status_proposta'] = $statusId; $result = $contrato->getProducaoSupervisorTotalizador($fields); if( $result ){ $producaoTotal = $result['total_liquido']; } return $producaoTotal; } /** * Faz o reset do campo secret_gauth * @params int $id */ public function resetGauth(int $id) { if (!$id) { return; } return (new Model_DbTable_GerenteRota())->update(['secret_gauth' => null], "id = " . $id); } public function saveSecretGauth(string $secret, int $gerenteId) { if (empty($secret) || !$gerenteId) { return; } return (new Model_DbTable_GerenteRota())->update(['secret_gauth' => $secret], "id = " . $gerenteId); } }
💾 保存文件
← 返回文件管理器