✏️ 正在编辑: Categoria.php
路径:
/srv/systems_dir/yuppiecred/application/models/Categoria.php
提示:
您可以编辑任何文件(包括二进制文件),但请注意不当修改可能导致文件损坏。
<?php class Model_Categoria { private $_id; private $_nome; private $_categoriaPai; private $_categoriaDre; private $_count; private $_options = array('' => ''); public function getId() { return $this->_id; } public function setId($id) { $this->_id = $id; } public function getNome() { return $this->_nome; } public function setNome($nome) { $this->_nome = $nome; } public function getCategoriaPai() { return $this->_categoriaPai; } public function setCategoriaPai($categoriaPai) { $this->_categoriaPai = $categoriaPai; } public function getCategoriaDre() { return $this->_categoriaDre; } public function setCategoriaDre($categoriaDre) { $this->_categoriaDre = $categoriaDre; } 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_Categoria(); $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_Categoria $categoria */ public function salvar(Model_Categoria $categoria) { $dbtable = new Model_DbTable_Categoria(); if( $categoria->getId() > 0 ){ $dbtable->update($categoria->_toArray(), "id = " . $categoria->getId()); return $categoria->getId(); } else { return $dbtable->insert($categoria->_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_Categoria(); 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_Categoria(); 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_Categoria(); return $dbtable->fetchRow(["id = ?" => $id]); } public function getArrayCategoriasFilhas(array $parents, $selected = 0) { $this->_count++; if (count($parents) > 0) { foreach ($parents as $key => $categoria) { $categoria['nome'] = $this->_printScore() . $categoria['nome']; if ($categoria['id'] != $selected) { $this->_options[$categoria['id']] = $categoria['nome']; } $this->getArrayCategoriasFilhas($categoria['childrens']); $this->_count--; } } return $this->_options; } protected function _printScore() { $scores = ""; for ( $i = 1; $i < $this->_count; $i++ ) { $scores .= "- "; } return $scores; } public function exibirDespesas($categoriaId, $mes, $empresaId = 0, $fornecedores = null, $competencia = null) { $params = array("(DATE_FORMAT(data_pagamento, '%m/%Y') = '{$mes}')"); if ($categoriaId > 0) { $params[] = "(categoria_id = {$categoriaId} OR categoria_pai = {$categoriaId})"; } if (!empty($competencia)) { $params[] = "competencia = '{$competencia}'"; } if ($empresaId > 0) { $params[] = "empresa_id = {$empresaId}"; } if (!empty($fornecedores)) { if (is_array($fornecedores)) { $params[] = 'fornecedor_id IN (' . implode(',', $fornecedores) . ')'; } else { $params[] = " AND fornecedor_id = {$fornecedores}"; } } $dbtable = new Model_DbTable_Lancamento(); $select = $dbtable->getAdapter()->select() ->from(array("l" => "tb_lancamentos"), array( 'categoria_id', 'valor_total' => 'SUM(valor)', 'total_pago' => 'SUM(valor_pago)' )) ->joinInner(array('c' => "tb_categorias"), "c.id = l.categoria_id and c.excluido<>1", array()) ->where("l.excluido<>1"); if( count($params) > 0 ){ $select->where(implode(" AND ", $params)); } $select->group('categoria_id'); return $dbtable->getAdapter()->fetchRow($select); } public function listarSubcategorias(array $selecionadas = null) { $dbtable = new Model_DbTable_Categoria(); $dados = $dbtable->fetchAllParents('nome ASC'); $categoria = new Model_Categoria(); $subcategorias = $categoria->getArraySubcategorias($dados); $values = $subcategorias; if ($selecionadas != null && count(array_filter($selecionadas)) > 0) { $values = array(); foreach ($selecionadas as $s) { array_key_exists($s, $subcategorias); $values[$s] = $subcategorias[$s]; } } return $values; } public function getArraySubcategorias(array $parents, $selected = 0) { if( count($parents) > 0 ){ foreach ( $parents as $key => $categoria ) { if( isset($categoria['categoria_pai']) && $categoria['categoria_pai'] > 0 ){ $categoriaPai = $this->exibir($categoria['categoria_pai']); $categoria['nome'] = $categoriaPai['nome'] . ' => ' . $categoria['nome']; } else { $categoria['nome'] = $categoria['nome']; } if( $categoria['id'] != $selected ) $this->_options[$categoria['id']] = $categoria['nome']; $this->getArraySubcategorias($categoria['childrens']); } } asort($this->_options); return array_filter($this->_options); } public function getDescendentes($categoriaId, $categorias = null, $recursion = false) { if (!$categorias) { $categorias = $this->listar('excluido = 0', 'nome'); } $listaCategorias = clone $categorias; $descendentes = array(); foreach($categorias as $categoria) { if ($categoria->categoria_pai == $categoriaId) { $descendentes[$categoria->id] = $categoria->nome; if(!$recursion){ $children = $this->getDescendentes($categoria->id, $listaCategorias, true); $descendentes += $children; } } } return $descendentes; } }
💾 保存文件
← 返回文件管理器