✏️ 正在编辑: CallcenterMensagem.php
路径:
/srv/systems_dir/yuppiecred-lidernegocios/application/models/CallcenterMensagem.php
提示:
您可以编辑任何文件(包括二进制文件),但请注意不当修改可能导致文件损坏。
<?php class Model_CallcenterMensagem { private $_id; private $_assunto; private $_tipo; private $_mensagem; public function getId() { return $this->_id; } public function setId($id) { $this->_id = $id; } function getAssunto() { return $this->_assunto; } function getTipo() { return $this->_tipo; } function getMensagem() { return $this->_mensagem; } function setAssunto($assunto) { $this->_assunto = $assunto; } function setTipo($tipo) { $this->_tipo = $tipo; } function setMensagem($mensagem) { $this->_mensagem = $mensagem; } 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 $info => $value ) { $explode = explode('_', $info); $methodName = ''; foreach ( $explode as $v ) { $methodName .= ucfirst($v); } $info = $methodName != '' ? $methodName : $info; $method = 'set' . ucfirst($info); if( in_array($method, $methods) ){ $this->$method($value); } } return $this; } public function _toArray() { $dbtable = new Model_DbTable_CallcenterMensagem(); $info = $dbtable->info(); $methods = get_class_methods($this); $data = array(); foreach ( $info['cols'] as $info ) { $col = $info; $explode = explode('_', $info); $methodName = ''; foreach ( $explode as $v ) { $methodName .= ucfirst($v); } $info = $methodName != '' ? $methodName : $info; $method = 'get' . ucfirst($info); if( in_array($method, $methods) ){ $data[$col] = $this->$method(); } } return $data; } /** * Insere ou altera o registro passado por parâmetro * @param Model_CallcenterMensagem $mensagem */ public function salvar(Model_CallcenterMensagem $mensagem) { $dbtable = new Model_DbTable_CallcenterMensagem(); if( $mensagem->getId() > 0 ){ $dbtable->update($mensagem->_toArray(), "id = " . $mensagem->getId()); return $mensagem->getId(); } else { return $dbtable->insert($mensagem->_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_CallcenterMensagem(); return $dbtable->delete('id = ' . $id); } } /** * Retorna uma lista * @param string $where * @param string $order * @param int $count * @param int $offset * @return array */ public function listar($fields = null, $order = null) { $params = []; $where = null; if( isset($fields['assunto']) && !empty($fields['assunto']) ){ $params[] = "assunto LIKE '%" . $fields['assunto'] . "%'"; } if( isset($fields['tipo']) && !empty($fields['tipo']) ){ $params[] = "tipo = '" . $fields['tipo'] . "'"; } if( count($params) > 0 ) $where = implode(' AND ', $params); $dbtable = new Model_DbTable_CallcenterMensagem(); return $dbtable->fetchAll($where, $order); } /** * 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_CallcenterMensagem(); return $dbtable->fetchRow('id = ' . $id); } public function processarMensagem($msg, $cliente, $isHtml = false) { $clienteObj = (object) $cliente; $primeiroNome = explode(" ", $clienteObj->nome); $primeiroNome = $primeiroNome[0]; $a = ucfirst(strtolower($primeiroNome)); $strings = array( '[DATA_HOJE]' => Zend_Date::now()->get('dd/MM/yyyy'), '[CLIENTE_NOME]' => ucwords(strtolower($primeiroNome)), '[CLIENTE_NOMECOMPLETO]' => ucwords(strtolower($clienteObj->nome)), '[VALOR_DA_MARGEM]' => $clienteObj->valor_da_margem ?? '', '[VALOR_DO_EMPRESTIMO_REFIN]' => $clienteObj->valor_emprestimo_refin ?? '', ); foreach ( $strings as $k => $v ) { $msg = str_replace($k, $v, $msg); } if( !$isHtml ){ $msg = html_entity_decode(strip_tags($msg)); } return $msg; } public function getBaseCliente($fields, $groupBy = "base.id", $orderBy = null) { $dbtable = new Model_DbTable_CallcenterBase(); $select = $dbtable->getAdapter()->select()->from([ 'base' => 'tb_callcenter_base' ]) ->joinInner([ 'campanhas' => 'tb_callcenter_campanhas' ], "campanhas.id = base.campanha_id", [ 'campanha' => 'nome', 'qtd_clientes' => 'count(distinct base.id)', 'qtd_emails' => "count(distinct base.email)" ]) ->joinLeft([ 'fones' => 'tb_callcenter_telefones' ], "base.id = fones.cliente_id and fones.excluido<>1", [ 'qtd_telefones' => "count(distinct fones.id)", "fone" ]) ->where("base.ativo = 1"); if (!empty($fields['campanha_id'])) { $select->where('base.campanha_id IN (?)', $fields['campanha_id']); } if ($fields['mensagem_enviada'] == 1) { if( !array_key_exists('historico', $select->getPart(Zend_Db_Select::FROM)) ){ $select->joinLeft([ 'historico' => 'tb_callcenter_mensagens_historicos' ], "historico.base_id = base.id AND historico.campanha_id = campanhas.id", [ 'mensagem_id' ]); } $select->where('mensagem_id IS NULL'); } if ($fields['mensagem_enviada'] == 2) { if( !array_key_exists('historico', $select->getPart(Zend_Db_Select::FROM)) ){ $select->joinLeft([ 'historico' => 'tb_callcenter_mensagens_historicos' ], "historico.base_id = base.id AND historico.campanha_id = campanhas.id", [ 'mensagem_id' ]); } $select->where('mensagem_id IS NOT NULL'); } if (!empty($fields['status_atendimento'])) { $fields['status_atendimento'] = array_filter($fields['status_atendimento']); $tmp = []; //verifica se foi passado o filtro SEM STATUS. Então filtra e remove o item do array if(($key = array_search("SEM_STATUS", $fields['status_atendimento'])) !== FALSE) { $tmp[] = 'base.status_atendimento IS NULL'; unset($fields['status_atendimento'][$key]); } $fields['status_atendimento'] = array_filter($fields['status_atendimento']); if( count($fields['status_atendimento']) > 0 ){ $tmp[] = "base.status_atendimento IN ('" . implode("', '", $fields['status_atendimento']) . "')"; } if( count($tmp) > 0 ){ $select->where(implode(' OR ', $tmp)); } } if (!empty($fields['cidade'])) { $select->where('cidade = ?', $fields['cidade']); } if (!empty($fields['discador_status'])) { $select->where('discador_status = ?', $fields['discador_status']); } if (!empty($fields['uf'])) { $select->where('uf = ?', $fields['uf']); } if (isset($fields['data_final']) && $fields['data_final']) { $dtFinal = new Zend_Date($fields['data_final'], 'dd/MM/yyyy'); $select->where('DATE(data_ultimo_atendimento) < ?', $dtFinal->get('yyyy-MM-dd')); } if (!empty($fields['tipo_mensagem']) && $fields['tipo_mensagem'] == 'E-MAIL') { /* Filtra apenas clientes na base com e-mail válido */ $select->where("email REGEXP '^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$'"); } $select->group($groupBy); if ($orderBy) { $select->order($orderBy); } return $dbtable->getAdapter()->fetchAll($select); } public function gerarArquivoMalaDireta($base, $mensagem) { $objPhpExcel = new PHPExcel(); $objPhpExcel->getProperties()->setDescription('Mala Direta'); $objPhpExcel->getProperties()->setCreator('Yuppie Tech'); $objPhpExcel->getProperties()->setTitle('Mala Direta'); $objPhpExcel->setActiveSheetIndex(0); $objPhpExcel->getActiveSheet()->setTitle('Mala Direta'); //cabeçalho do arquivo $objPhpExcel->getActiveSheet()->SetCellValue('A' . 1, "Nome"); $objPhpExcel->getActiveSheet()->SetCellValue('B' . 1, "Endereco"); $objPhpExcel->getActiveSheet()->SetCellValue('C' . 1, "Numero"); $objPhpExcel->getActiveSheet()->SetCellValue('D' . 1, "Complemento"); $objPhpExcel->getActiveSheet()->SetCellValue('E' . 1, "Bairro"); $objPhpExcel->getActiveSheet()->SetCellValue('F' . 1, "Cidade"); $objPhpExcel->getActiveSheet()->SetCellValue('G' . 1, "Estado"); $objPhpExcel->getActiveSheet()->SetCellValue('H' . 1, "Cep"); $objPhpExcel->getActiveSheet()->SetCellValue('I' . 1, "Mensagem"); //linhas do arquivo if( $base->count() > 0 ){ $i = 2; foreach ( $base->toArray() as $info ) { $msg = html_entity_decode(strip_tags($mensagem->mensagem), ENT_COMPAT, 'UTF-8'); $msgAtualizada = $this->processarMensagem($msg, $info); $objPhpExcel->getActiveSheet()->setCellValueByColumnAndRow(0, $i, $info['nome']); $objPhpExcel->getActiveSheet()->setCellValueByColumnAndRow(1, $i, $info['endereco']); $objPhpExcel->getActiveSheet()->setCellValueByColumnAndRow(2, $i, $info['numero']); $objPhpExcel->getActiveSheet()->setCellValueByColumnAndRow(3, $i, $info['complemento']); $objPhpExcel->getActiveSheet()->setCellValueByColumnAndRow(4, $i, $info['bairro']); $objPhpExcel->getActiveSheet()->setCellValueByColumnAndRow(5, $i, $info['cidade']); $objPhpExcel->getActiveSheet()->setCellValueByColumnAndRow(6, $i, $info['uf']); $objPhpExcel->getActiveSheet()->setCellValueByColumnAndRow(7, $i, $info['cep']); $objPhpExcel->getActiveSheet()->setCellValueByColumnAndRow(8, $i, $msgAtualizada); $i++; } $objWriter = PHPExcel_IOFactory::createWriter($objPhpExcel, 'Excel5'); header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename="mala-direta.xls"'); header('Cache-Control: max-age=0'); $objWriter->save('php://output'); } else { return null; } } public function relatorioEnvioAnalitico($fields) { $dbtable = new Model_DbTable_CallcenterMensagemHistorico(); return $dbtable->getRelatorioAnalitico($fields); } public function enviarSms(array $fones, $dadosCliente) { if( empty($fones) ){ return; } $sucesso = 0; $enviados = []; foreach( $fones as $fone ){ /* Retornar telefone caso seja válido | 2º parâmetro true para validar apenas celular */ $tel = Core_Service_Sms::isValidPhone($fone['fone'], true); if( !$tel ){ continue; } if( in_array($tel, $enviados) ){ continue; } $msgAtualizada = $this->processarMensagem($this->getMensagem(), $dadosCliente); $data['celular'] = $tel; $data['mensagem'] = $msgAtualizada; $result = (new Core_Service_Sms())->sendMessage($tel, $msgAtualizada, $this->getId()); (new Model_DbTable_CallcenterMensagemHistorico())->insert([ 'usuario' => Yuppie_Auth::getIdentity()->login, 'base_id' => $dadosCliente['id'], 'mensagem_id' => $this->getId(), 'campanha_id' => $dadosCliente['campanha_id'], 'tipo' => "sms", 'retorno' => $result ?? "Não enviado", 'fone' => $tel, ] ); if( $result ){ $sucesso++; } $enviados[] = $tel; } return $sucesso; } }
💾 保存文件
← 返回文件管理器