✏️ 正在编辑: StatusProposta.php
路径:
/srv/systems_dir/yuppiecred/application/models/StatusProposta.php
提示:
您可以编辑任何文件(包括二进制文件),但请注意不当修改可能导致文件损坏。
<?php class Model_StatusProposta { private $_id; private $_descricao; private $_codigo; private $_parentId; private $_bloqueadoAlteracao; public function getId() { return $this->_id; } public function setId($id) { $this->_id = $id; } public function getDescricao() { return $this->_descricao; } public function setDescricao($descricao) { $this->_descricao = trim($descricao); } public function getCodigo() { return $this->_codigo; } public function setCodigo($codigo) { $this->_codigo = $codigo; } public function getParentId() { return $this->_parentId; } public function setParentId($parentId) { $this->_parentId = $parentId; } public function getBloqueadoAlteracao() { return $this->_bloqueadoAlteracao; } public function setBloqueadoAlteracao($bloqueadoAlteracao) { $this->_bloqueadoAlteracao = $bloqueadoAlteracao; } 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_Tipos(); $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(); } } $data['tipo'] = 'STATUS_PROPOSTA'; return $data; } /** * Insere ou altera o registro passado por parâmetro * @param Model_StatusProposta $statusProposta */ public function salvar(Model_StatusProposta $statusProposta) { $dbtable = new Model_DbTable_Tipos(); $data = $statusProposta->_toArray(); if ($statusProposta->getId() > 0) { if ($statusProposta->getParentId() == $statusProposta->getId()) { throw new Core_Exception("Você não pode setar o status como pai dele mesmo."); } $result = $dbtable->update($data, 'id = ' . $statusProposta->getId()); if (!$result) { return null; } if ($statusProposta->getParentId() > 0) { $dbtable->update([ 'parent_id' => $statusProposta->getParentId() ], "parent_id = " . $statusProposta->getId()); (new Model_DbTable_CampanhaStatus())->update(['status_id' => $statusProposta->getParentId()], 'status_id = ' . $statusProposta->getId()); (new Model_DbTable_Contrato())->updateStatusProposta($statusProposta->getParentId(), $statusProposta->getId()); } return $statusProposta->getId(); } else { if (!empty($data['descricao'])) { $data['codigo'] = ($this->getUltimoCodigo()+1); return $dbtable->insert($data); } } return null; } /** * Exclui o registro passado por parâmetro * @param int $id * @return int The number of rows deleted */ public function excluir($id) { if(in_array($id, array(43,66,42))){ throw new Exception("Atenção! Não é possível excluir esses status."); } $contrato = (new Model_DbTable_Contrato())->fetchRow(["status_proposta = ?" => $id]); if( $contrato ){ $status = $this->exibir($id); throw new Exception("Atenção! Não é possível excluir esse status " . "({$status->descricao}), pois possui contrato vinculado."); } if( $id > 0 ){ $dbtable = new Model_DbTable_Tipos(); 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 = 'descricao ASC', $count = null, $offset = null) { $where['tipo = ?'] = 'STATUS_PROPOSTA'; $dbtable = new Model_DbTable_Tipos(); 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_Tipos(); return $dbtable->fetchRow(["id = ?" => $id]); } /** * Verifica se o registro passado por parâmetro existe * @param string $descricao * @return array|null */ public function existe($descricao) { $nome = trim($descricao); if( empty($nome) ){ return; } $dbtable = new Model_DbTable_Tipos(); return $dbtable->fetchRow([ "tipo = ?" => 'STATUS_PROPOSTA', "descricao LIKE ?" => $nome ]); } /** * Retorna o último código cadastrado * @return null */ public function getUltimoCodigo() { $dbtable = new Model_DbTable_Tipos(); $dados = $dbtable->fetchRow("tipo = 'STATUS_PROPOSTA' " . "AND codigo IS NOT NULL", 'codigo DESC'); if( $dados['codigo'] > 0 ) return $dados['codigo']; return null; } /** * Verifica se o status da proposta é cancelado * @param int $status * @return true|false */ public function isCancelado($status) { $where = ['(parent_id IS NULL OR parent_id = 0) AND descricao LIKE "%cancel%"']; $cancelados = $this->listar($where, 'descricao'); $lista = array(); foreach ( $cancelados as $cancelado ) { $lista[] = $cancelado['id']; } if(in_array($status, $lista)){ return true; } return false; } public static function getOrCreate($descricao) { $status = self::existe($descricao); if ($status) { return !empty($status->parent_id) ? self::exibir($status->parent_id) : $status; } $model = new Model_StatusProposta(['descricao' => $descricao]); $statusId = $model->salvar($model); return self::exibir($statusId); } public static function getIdsCancelados() { $where = '(parent_id IS NULL OR parent_id = 0) AND descricao LIKE "%cancel%" ' . "AND tipo ='STATUS_PROPOSTA'"; $dbtable = new Model_DbTable_Tipos(); $ids = []; foreach ($dbtable->fetchCols('id', ['id'], $where) as $cancelado) { $ids[] = $cancelado[0]; } return $ids; } public static function getIdsEstornados() { $where = '(parent_id IS NULL OR parent_id = 0) AND descricao LIKE "%estorn%" ' . "AND tipo ='STATUS_PROPOSTA'"; $dbtable = new Model_DbTable_Tipos(); $ids = []; foreach ($dbtable->fetchCols('id', ['id'], $where) as $estornado) { $ids[] = $estornado[0]; } return $ids; } /** * Verifica se permite alterar do status anterior para o atual */ public static function checkUpdateStatus(&$dados, $statusOldId) { if (empty($statusOldId)) { return true; } $dbTable = new Model_DbTable_Tipos(); $statusOld = $dbTable->fetchRow(['id = ?' => $statusOldId]); if (empty($statusOld)) { return true; } if (stristr($statusOld->descricao, 'cancel') && $statusOld->bloqueado_alteracao) { unset($dados['status_proposta']); return false; } if (empty($dados['status_proposta'])) { //se não está mudando, é pq vai permanecer. Então, trata os demais campos $dados['status_proposta'] = $statusOldId; } $statusNew = $dbTable->fetchRow(['id = ?' => $dados['status_proposta']]); if ($statusOld->bloqueado_alteracao && !stristr($statusNew->descricao, 'estorn') && !stristr($statusNew->descricao, 'cancel') && ($statusNew->id != 43 || $statusNew->id == $statusOld->id)) { unset($dados['status_proposta']); return false; } return true; } }
💾 保存文件
← 返回文件管理器