protected function _getSqlBase() { $idPedido = Zend_Controller_Front::getInstance()->getRequest()->getParam('id_pedido'); $idCliente = Zend_Controller_Front::getInstance()->getRequest()->getParam('id_cliente'); if ($this->_idClienteCon) { $idCliente = $this->_idClienteCon; } if (!$idCliente) { if ($idPedido) { $_pedido = new Vendas_DataView_Pedido_MapperView(); $_pedido->setId($idPedido)->retrieve(); $idCliente = $_pedido->getIdClienteCon(true)->toPhp(); if ($idCliente) { $idCliente = $_pedido->getIdCliente(true)->toPhp(); } } if (!$idCliente) { $idCliente = 0; } } $hoje = ZendT_Type_Date::nowDate()->getValueToDb(); $sql = parent::_getSqlBase(); $sql .= " LEFT JOIN " . Ca_DataView_Contrato_MapperView::$table . " contrato ON ('" . $hoje . "' BETWEEN contrato.dt_vig_ini AND contrato.dt_vig_fim AND contrato.status = 'A' AND contrato.id_cliente = " . $idCliente . ") "; $sql .= " LEFT JOIN " . Ca_DataView_RegraContrato_MapperView::$table . " regra_contrato ON (regra_contrato.id_produto = cv_produto.id AND regra_contrato.status = 'A' AND regra_contrato.tipo IN ('PA','PD') ) "; return $sql; }
public function _beforeSave() { if ($this->_action != 'delete') { if ($this->getIdUsuInc(true)->toPhp() == '') { $this->setIdUsuInc(Auth_Session_User::getInstance()->getId()); } $this->setIdUsuAlt(Auth_Session_User::getInstance()->getId()); if ($this->getQtdItem(true)->toPhp() == '') { $this->setQtdItem(1); } if ($this->getPerAcre(true)->toPhp() == '') { $this->setPerAcre(0); } if ($this->getPerDesc(true)->toPhp() == '') { $this->setPerDesc(0); } if ($this->getVlrItem(true)->toPhp() == '') { $_pedido = new Vendas_DataView_Pedido_MapperView(); $_pedido->setId($this->getIdPedido())->retrieve(); $_produto = new Vendas_DataView_ProdutoContrato_MapperView(); $_produto->setIdClienteCon($_pedido->getIdClienteCon())->setId($this->getIdProduto()); $row = $_produto->retrieveRow($_where); if (!$row) { throw new ZendT_Exception_Alert(_i18n('Não foi possível determinar o valor do produto!')); } else { $this->setVlrItem($row['vlr_final']); } } if ($this->getVlrFinal(true)->toPhp() == '') { $vlrFinal = $this->getVlrItem()->toPhp() * $this->getQtdItem()->toPhp(); $vlrOrig = $vlrFinal; if ($this->getPerAcre()->toPhp() > 0) { $vlrAcre = $vlrOrig * $this->getPerAcre()->toPhp() / 100; $vlrFinal += $vlrAcre; } if ($this->getPerDesc()->toPhp() > 0) { $vlrDesc = $vlrOrig * $this->getPerDesc()->toPhp() / 100; $vlrFinal += $vlrDesc; } $this->setVlrFinal($vlrFinal); } } }
public function efetivar($idPedido = false) { if (!$idPedido) { $idPedido = $this->getIdPedido(); } $_pedido = new Vendas_DataView_Pedido_MapperView(); $_pedido->setId($idPedido)->retrieve(); $_pagtoLanc = new Vendas_DataView_PagtoLanc_MapperView(); $_pagamento = new Vendas_DataView_Pagamento_MapperView(); $_lancamento = new Financeiro_DataView_Lancamento_MapperView(); $_where = new ZendT_Db_Where(); $_where->addFilter('cv_pagto_pedido.id_pedido', $idPedido); $rows = $_pagamento->recordset($_where); $pago = false; $vlrTotal = 0; while ($row = $rows->getRow()) { if ($row['pago_forma_pagto']->toPhp() == 'S') { $pago = true; } $qtd = $row['qtd_parcela']->toPhp(); if (!$qtd) { $qtd = 1; } for ($index = 0; $index < $qtd; $index++) { $dtVenc = $row['dt_venc_parc']; if ($dtVenc->toPhp() == '') { $dtVenc = $row['dt_emis_pedido']; } if ($dtVenc->toPhp() == '') { $dtVenc = ZendT_Type_Date::nowDate(); } if ($index) { $dtVenc->addMonths($index); } $vlrParc = $row['vlr_parc']; if ($vlrParc->toPhp() == '') { $vlrParc = $row['vlr_a_pagar']; } if ($vlrParc->toPhp() == '') { $vlrParc = $row['vlr_pago']; } $_lancamento->newRow()->setTipo('C')->setIdEmpresa($_pedido->getIdEmpresa())->setIdFavorecido($_pedido->getIdEmpresa())->setDescricao('VENDA DE PEDIDO')->setObservacao('PAGAMENTO ' . ($index + 1) . " de " . $qtd . '. PEDIDO: ' . $row['numero_pedido'])->setIdFormaPagto($row['id_forma_pago'])->setPago($row['pago_forma_pagto'])->setDtLanc($dtVenc)->setVlrLanc($vlrParc)->insert(); $_pagtoLanc->newRow()->setIdPagtoPedido($row['id'])->setIdLancamento($_lancamento->getId())->insert(); $vlrTotal = $vlrTotal + $vlrParc->toPhp(); } } /** * Avalia se é preciso gerar débitos para o pedido * custos com imposto ou comissões */ $dtPedido = $_pedido->getDtEmis(); if ($dtPedido->toPhp() == '') { $dtPedido = ZendT_Type_Date::nowDate(); } $dtVenc = $dtPedido; $_regra = new Ca_DataView_RegraContrato_MapperView(); $_itemLanc = new Vendas_DataView_ItemLanc_MapperView(); $_itens = new Vendas_DataView_ItemPedido_MapperView(); $_itens->setIdPedido($idPedido); $_itens->findAll(null, '*'); while ($_itens->fetch()) { $_where = new ZendT_Db_Where(); $_where->addFilter('ca_regra_contrato.id_produto', $_itens->getIdProduto()); $_where->addFilter('ca_regra_contrato.status', 'A'); $_where->addFilter('ca_regra_contrato.tipo', 'CD'); $_where->addFilter('ca_regra_contrato.favorecido', 'ca_pedido.', '?%'); $_where->addFilter('contrato.dt_vig_ini', $dtPedido, '<='); $_where->addFilter('contrato.dt_vig_fim', $dtPedido, '>='); $_where->addFilter('contrato.id_cliente', $_pedido->getIdClienteCon()); $_where->addFilter('contrato.status', 'A'); $_regra->findAll($_where, '*'); while ($_regra->fetch()) { $favorecido = $_regra->getFavorecido()->toPhp(); $favorecido = str_replace('ca_pedido.', '', $favorecido); if ($favorecido == 'especifico') { $idFavorecido = $_regra->getIdFavorecido(); } else { $idFavorecido = $_pedido->getData($favorecido); } $desc = $_regra->getDescLanc()->toPhp(); if (!$desc) { $desc = 'PEDIDO DE VENDA: ' . $_pedido->getNumero()->get(); } if (!$idFavorecido) { $idFavorecido = $_regra->getIdFavorecido(); } //$vlrTotal if ($_regra->getVlrFixo()->toPhp() > 0) { $vlrLanc = $_regra->getVlrFixo(); } else { if ($_regra->getVlrPerc()->toPhp() > 0) { $vlrLanc = $vlrTotal * $_regra->getVlrPerc()->toPhp() / 100; } else { $vlrLanc = 0; } } if ($idFavorecido) { $_lancamento->newRow()->setTipo('D')->setIdContrato($_regra->getIdContrato())->setIdFavorecido($idFavorecido)->setDescricao($desc)->setObservacao('PEDIDO DE VENDA: ' . $_pedido->getNumero()->get())->setPago('N')->setDtLanc($dtVenc)->setVlrLanc($vlrLanc)->insert(); $_itemLanc->newRow()->setIdItemPedido($_itens->getId())->setIdLancamento($_lancamento->getId())->insert(); if (!$pago) { $_lancamento->newRow()->setTipo('C')->setIdContrato($_regra->getIdContrato())->setIdFavorecido($idFavorecido)->setDescricao($desc)->setObservacao('CUSTEADO PELO CLIENTE')->setPago('S')->setDtLanc($dtVenc)->setVlrLanc($vlrLanc)->insert(); $_itemLanc->newRow()->setIdItemPedido($_itens->getId())->setIdLancamento($_lancamento->getId())->insert(); } } } } }