___ajax = isset($_GET["ajax"]) ? $_GET["ajax"] : 0; $this->___client_id = isset($_POST["client_id"]) ? $_POST["client_id"] : 0; $this->___agente = isset($_POST["agente"]) ? $_POST["agente"] : 0; $this->___status = isset($_REQUEST["status"]) ? $_REQUEST["status"] : 'Todos'; $this->___dataLead = isset($_REQUEST["dataLead"]) ? $_REQUEST["dataLead"] : ''; $oldMouth = date("m", strtotime("first day of previous month")); $this->___dataIni = IsPostBack() ? $_POST['dataIni'] : date("21/{$oldMouth}/Y"); $this->___dataFim = IsPostBack() ? $_POST['dataFim'] : date('20/m/Y'); } function ___FiltrosRelatorio() { $agente = $this->getAgente($this->___agente); $status = $this->getStatus($this->___status); if ($this->___media != MEDIA_PRINT_GRID) { /* * Recupera os filtros da sessão do usuário. */ list($this->___dataIni, $this->___dataFim, $this->___tipoRelatorio) = $_SESSION["SSstorageFiltros"]; if ($this->___media == MEDIA_PRINT_HTML) { $fila = $this->GetIncDac() ? 'Fila: ' . GetDacDesc($this->GetDbCon(), $this->GetIdProg() . ' ') : ''; return sprintf("%sData Inicial: %s Data Final: %s Tipo: %s", $fila, $this->___dataIni, $this->___dataFim, ($this->___tipoRelatorio ? 'Analítico' : 'Sintético')); } $fltCsv = array(); if ($this->GetIncDac()) { $fltCsv[] = 'Fila:' . GetDacDesc($this->GetDbCon(), $this->___dac); } $fltCsv[] = "Data Inicial:" . $this->___dataIni; $fltCsv[] = "Data Final:" . $this->___dataFim; $fltCsv[] = "Data Emissao:" . date('d/m/Y H:i:s'); $fltCsv[] = "Emitido Por:" . GetLogin(); return $fltCsv; } if ($this->GetIncDac()) { $dacs = GetDac($this->GetDbCon(), $this->___dac, "", 0, 1, 0, 0, 1); } $lkPrint = ""; if ($this->___dataCount) { $prt = 'Clique aqui para imprimir!'; $prtHtml = "\"Clique"; $lkPrint .= sprintf($prtHtml, $this->GetidProg(), MEDIA_PRINT_HTML, $this->___tipoRelatorio, $this->___status, substr(LimpaString(GetDispProgSel($this->GetIdProg())), 0, 20), 'print.gif'); $lkPrint .= sprintf($prt, $this->GetidProg(), MEDIA_PRINT_EXCEL, $this->___tipoRelatorio, $this->___status, 'prt-xls.png'); $lkPrint .= sprintf($prt, $this->GetidProg(), MEDIA_PRINT_PDF, $this->___tipoRelatorio, $this->___status, 'prt-pdf.png'); //$lkPrint .= sprintf($prt, $this->GetidProg(), MEDIA_PRINT_CSV, $this->___tipoRelatorio, 'prt-csv.png'); } // $tipoRel = !$this->___tipoRelatorio ? '' : 'checked="checked"'; /* * Personalise esta função se necessitar de outras op��es de filtro. */ $filtro = ''; /* * Rotulos. */ $filtro .= ''; if ($this->GetIncDac()) $filtro .= ''; $filtro .= ''; $filtro .= ''; $filtro .= ''; $filtro .= ''; $filtro .= ''; $filtro .= ''; /* * Valores. */ $filtro .= ''; if ($this->GetIncDac()) $filtro .= sprintf('', $dacs); $filtro .= ''; $filtro .= ''; $filtro .= sprintf('', $agente); $filtro .= sprintf('', $status); $filtro .= ''; $filtro .= ''; $filtro .= ''; $filtro .= '
FilaData InicialData Final AgenteStatus 
%s%s%s%s
'; /* * Guarda os filtros passados para recuperar na emissão dos relatóios para impressão. */ $_SESSION["SSstorageFiltros"] = array($this->___dataIni, $this->___dataFim, $this->___tipoRelatorio); return sprintf($filtro, $this->___dataIni, $this->___dataFim, $lkPrint); } protected function ___GetDadosDb($add = null, $dataLead = null) { if ($dataLead && $add) { $query = sprintf("UPDATE pbx_cliente SET data_lead = '%s' WHERE client_id = '%s';", $dataLead . " " . date('H:i:s'), $add); $result = $this->___GetQuery($query); return true; } if ($add) { $query = "UPDATE pbx_cliente SET data_lead = 'now' WHERE client_out_id = $add AND client_status = '21';"; $result = $this->___GetQuery($query); return true; } $dataIni = FormatDtMssql($this->___dataIni); $dataFim = FormatDtMssql($this->___dataFim); $agente = $this->___agente; $query = "SELECT client_id, client_out_id, matricula, client_documento, client_razao_social, apelido, data_lead, client_update::date FROM pbx_cliente a INNER JOIN pbx_usuarios b ON client_user_contato = matricula WHERE client_status = '21' AND client_update >= '$dataIni' AND client_update <= '$dataFim' "; if ($agente) { $query .= " AND matricula = '$agente' "; } $query .= " ORDER BY apelido, client_update"; $result = $this->___GetQuery($query); $row = pg_fetch_all($result); if (!$agente) { $_SESSION['SSDadosAgents'] = $row; } $this->SetData($row ? $row : array()); } protected function ___PreparaCsv() { } protected function ___PreparaExcel() { $dataRel = array(); $filtro = $this->___FiltrosRelatorio(); /* * Inicia o relatório. */ /* * Cria os filtros */ $linha = array('LABEL'); foreach ($filtro as $values) { list($label, $value) = explode(":", $values); $linha[] = $label; } $dataRel[] = $linha; $linha = array('DADOS'); foreach ($filtro as $values) { list($label, $value) = explode(":", $values); $linha[] = $value; } $dataRel[] = $linha; $dadosCabecalio = array( 'Documento', 'Empresa', 'Apelido', 'Atualizado', 'Proposta' ); $dadosField = array( 'client_documento', 'client_razao_social', 'apelido', 'client_update', 'proposta' ); $linha = array('LABEL'); foreach ($dadosCabecalio as $value) { $linha[] = $value; } $dataRel[] = $linha; $count = 0; $countVendas = 0; $lastAgent = null; $proposta = $this->ConsultaProposta(); foreach ($this->GetData()[0] as $row) { if($this->___status == "LEAD" && !$proposta[$row["client_out_id"]] && !$row["data_lead"]){ continue; } else if($this->___status == "EXPORTADOS" && ($proposta[$row["client_out_id"]] || $row["data_lead"])){ continue; } $count++; $row['client_razao_social'] = RemoveAcentos($row['client_razao_social']); $row['proposta'] = ($proposta[$row["client_out_id"]] || $row["data_lead"]) ? 'SIM' : 'NAO'; if ($lastAgent == null) { $lastAgent = $row['apelido']; } if ($row['apelido'] != $lastAgent) { $dataRel[] = array('LABEL', "[$lastAgent]", 'Total Exportados: ' . $countVendas, '', ''); $countVendas = 0; $lastAgent = $row['apelido']; } $linha = array('DADOS'); foreach ($dadosField as $key) { if ($key == 'client_update') { $linha[] .= date('d/m/Y', strtotime($row["$key"])); } else { $linha[] .= $row["$key"]; } } $dataRel[] = $linha; $countVendas++; } $dataRel[] = array('LABEL', "[$lastAgent]", 'Total Exportados: ' . $countVendas); $this->___dataRel[] = $dataRel; } protected function ___PreparaGrid() { $dataRel = array(); $dadosCabecalio = array( 'Documento' => 'align="center"', 'Empresa' => 'align="center"', 'Apelido' => 'align="center"', 'Atualizado' => 'align="center"', 'Proposta' => 'align=center' ); $dadosField = array( 'client_documento' => 'align="center"', 'client_razao_social' => 'align="center"', 'apelido' => 'align="center"', 'client_update' => 'align="center"', 'proposta' => 'align="center"' ); /* * Inicia o relatório. */ $dataRel[] = ''; /* * Monta a linha de cabecalio. */ $linha = ''; $linha .= ""; $linha .= ""; $dataRel[] = $linha; /* * Verifica se retornou dados */ if (!IsPostBack() || !count($this->GetData()[0])) { $dataRel[] = sprintf("
Relatório de Comissão por Agente
%s
", count(1), (!IsPostBack() ? 'Informe os parametros e clique em consultar!' : 'Nenhum registro encontado!')); $this->___dataRel[] = $dataRel; return; } $linhaC = ""; foreach ($dadosCabecalio as $key => $value) { $linhaC .= sprintf("%s", $value, $key); } $linhaC .= ""; $dataRel[] = $linhaC; /* * Linhas de dados. */ $count = 0; $countVendas = 0; $lastAgent = null; $proposta = $this->ConsultaProposta(); foreach ($this->GetData()[0] as $row) { if($this->___status == "LEAD" && !$proposta[$row["client_out_id"]] && !$row["data_lead"]){ continue; } else if($this->___status == "EXPORTADOS" && ($proposta[$row["client_out_id"]] || $row["data_lead"])){ continue; } $count++; if ($lastAgent == null) { $lastAgent = $row['apelido']; } if ($row['apelido'] != $lastAgent) { $dataRel[] = sprintf('[%s] Total %s: %s  ', ($this->___status == "Todos" ? "GERAL" : $this->___status), $lastAgent, $countVendas); $countVendas = 0; $lastAgent = $row['apelido']; } $edit = function ($tipo, $row) { return sprintf("   " . " " . "" . "   ", $tipo, $row['client_id'], $tipo); }; $checkbox = '   %s'; $row['proposta'] = ($proposta[$row["client_out_id"]] || $row["data_lead"]) ? sprintf($checkbox, $row["client_out_id"], $row["client_out_id"], 'checked', $edit('editaUser', $row)) : sprintf($checkbox, $row["client_out_id"], $row["client_out_id"], '', $edit('editaUserOff', $row)); $linha = ""; foreach ($dadosField as $key => $value) { if ($key == 'client_update') { $linha .= sprintf("%s", $value, date('d/m/Y', strtotime($row["$key"]))); } else { $linha .= sprintf("%s", $value, $row["$key"]); } } $linha .= ""; $dataRel[] = $linha; $countVendas++; } // echo implode(',',$datas); $dataRel[] = sprintf('[%s] Total %s: %s  ', ($this->___status == "Todos" ? "GERAL" : $this->___status), $lastAgent, $countVendas); $dataRel[] = sprintf('Total: %s', $count); $this->___dataRel[] = $dataRel; } protected function ___PreparaHtml() { } protected function ___PreparaPdf() { list($this->___dataIni, $this->___dataFim, $this->___tipoRelatorio) = $_SESSION["SSstorageFiltros"]; $dadosCabecalio = array( 'Documento' => 'C', 'Empresa' => 'C', 'Apelido' => 'C', 'Atualizado' => 'C', 'Proposta' => 'C' ); $dadosField = array( 'client_documento' => 'C', 'client_razao_social' => 'C', 'apelido' => 'C', 'client_update' => 'C', 'proposta' => 'C' ); $count = 0; $countVendas = 0; $lastAgent = null; $tables = array(); $widthHeader = array(35, 73, 32, 25, 25); $dadosTable = array(); $proposta = $this->ConsultaProposta(); /* * Linhas de dados. */ foreach ($this->GetData()[0] as $key => $row) { if($this->___status == "LEAD" && !$proposta[$row["client_out_id"]] && !$row["data_lead"]){ continue; } else if($this->___status == "EXPORTADOS" && ($proposta[$row["client_out_id"]] || $row["data_lead"])){ continue; } $count++; if ($lastAgent == null) { $lastAgent = $row['apelido']; } if ($row['apelido'] != $lastAgent) { $totalizador = array('', '', "[$lastAgent]", 'Total Exportados: ' . $countVendas, ''); ${"table" . $count} = array($dadosCabecalio, $dadosField, $dadosTable, $widthHeader, $totalizador, 0); array_push($tables, ${"table" . $count}); $countVendas = 0; $dadosTable = array(); $lastAgent = $row['apelido']; } $row['proposta'] = ($proposta[$row["client_out_id"]] || $row["data_lead"]) ? 'SIM' : 'NÃO'; $row['client_update'] = date('d/m/Y', strtotime($row['client_update'])); $countVendas++; $dadosTable[] = $row; } $totalizador = array('', '', "[$lastAgent]", 'Total Exportados: ' . $countVendas, ''); ${"table" . $count} = array($dadosCabecalio, $dadosField, $dadosTable, $widthHeader, $totalizador, 0); array_push($tables, ${"table" . $count}); $totalizador = array('Total:' . $count, '' , '', ''); $total = array('', '', '', '', $totalizador, 0); array_push($tables, $total); $this->___dataRel = $tables; } function ConsultaVendaMais() { $query = "exec sip_cliente_prevenda 0"; $data = $this->getConnection($query); $clientId = array(); $cpf = array(); foreach ($data as $cli) { if ((strtotime($cli['data_cadastro']) >= strtotime(FormatDtMssql($this->___dataIni))) && (strtotime($cli['data_cadastro']) <= strtotime(FormatDtMssql($this->___dataFim)))) { $clientId[] = $cli['cliente_id']; $cpf[] = QuotedStr($cli['cgc_cpf']); } } return array($clientId, $cpf); } function ConsultaProposta() { $dados = array(); foreach ($this->GetData()[0] as $row) { $dados[] = $row['client_out_id']; } $imp = implode(',', $dados); if (!$imp) { return $imp; } $query = "SELECT a.ClienteId, RazaoSocial, VendedorId, CGC_CPF, PropostaId, Descricao, StatusId, b.AddedDate FROM Clientes a INNER JOIN Propostas b ON b.ClienteId = a.ClienteId INNER JOIN PropostasStatus c ON c.PropostaStatusId = b.StatusId WHERE a.ClienteId IN ($imp) AND b.PropostaId = (SELECT MAX(PropostaId) FROM Propostas WHERE ClienteId = a.ClienteId) GROUP BY a.ClienteId, RazaoSocial, VendedorId, CGC_CPF, PropostaId, Descricao, StatusId, b.AddedDate ORDER BY a.ClienteId, b.AddedDate"; $data = $this->getConnection($query); $clientes = array(); foreach ($data as $cli) { $clientes[$cli['ClienteId']] = $cli['CGC_CPF']; } return $clientes; } private function getConnection($query) { $connection = mssql_connect('192.168.115.28', 'integracao', 'SimpleS_root'); mssql_select_db('VendaMaisDB_simplesip'); $result = mssql_query($query, $connection); $return = array(); while ($data = mssql_fetch_array($result)) { $return[] = $data; } return $return; } private function getAgente($select = null) { $opt = ""; return $opt; } private function getStatus($select = null) { $opt = ""; return $opt; } private function vendasValores($vendas) { if (!$vendas) { return 0; } if ($vendas >= 1 && $vendas <= 10) { $meta = 10; return array('meta' => $meta, 'recebiveis' => $vendas * $meta); } if ($vendas >= 11 && $vendas <= 15) { $meta = 15; return array('meta' => $meta, 'recebiveis' => $vendas * $meta); } if ($vendas >= 16 && $vendas <= 20) { $meta = 17; return array('meta' => $meta, 'recebiveis' => $vendas * $meta); } if ($vendas >= 21) { $meta = 23; return array('meta' => $meta, 'recebiveis' => $vendas * $meta); } } public function ExecutaRelatorio() { global $jsStartup, $jsJQuery, $smarty; try { if ($this->___ajax && $this->___client_id) { $this->___GetDadosDb($this->___client_id, $this->___dataLead); echo "OK"; exit(); } /* * Use esta função para validar dados do formulário. */ $this->___ValidaForm(); /* * Recupera os dados do banco. */ $this->___ConteudoRelatorio(); /* * Imprime relatório de acordo com a media passada. */ $conteudoRelatorio = $this->___ImprimeRelatorio(); } catch (Exception $exc) { $this->SetMsg($exc->getMessage()); $jsStartup[] = sprintf("alert('%s');", $this->GetMsg()); $this->___GravaLog(); } /* * Inclua os scripts necessários aqui. */ $jsJQuery[] = "$('.clickedProposta').on('click', function(){ if(confirm('Deseja adicionar esse cliente como Lead?')){ $.ajax({ url: 'index.php?idProg=352&ajax=1', type: 'post', data: { 'client_id': this.id }, success: function (resp) { if(resp.trim() == 'OK'){ alert('Cliente adicionado com sucesso!') } } }) } else { $(this).prop('checked', false) } })"; $jsJQuery[] = "$('.editaUser').on('click', function(){ $('.date_' + this.id).on('click', function(){ if(this.id == 'close'){ $('.' + this.className).hide(); } if(this.id == 'save'){ let dataLead = $('.' + this.className).val() if(dataLead){ $.ajax({ url: 'index.php?idProg=352&ajax=1&dataLead=' + dataLead, type: 'post', data: { 'client_id': this.className.replace('date_', '') }, success: function (resp) { console.log(resp) if(resp.trim() == 'OK'){ alert('Lead alterado com sucesso!') } } }) } else { alert('Informe uma data valida para o Lead!') } } }) $('.date_' + this.id).show(); })"; $jsJQuery[] = "\$('#dataIni').keypress(function(){formataDataHora(this);}) "; $jsJQuery[] = "\$('#dataFim').keypress(function(){formataDataHora(this);}) "; $smarty->assign('filtros', $this->___FiltrosRelatorio()); $smarty->assign('conteudo', $conteudoRelatorio); $smarty->assign('msg', $this->GetMsg()); GetTemplate($smarty, 'relatoriosGrid.tpl'); } } $relRelContribuinte = new RelComissao($idProg, $dbcon, 0); $relRelContribuinte->ExecutaRelatorio(); ?>