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(); } } } } }
public function runLaudos($where = array()) { $this->_log('Iniciado: ' . date('d/m/Y H:s:i')); Auth_Session_User::refresh('JOB_VSP'); $this->_token = $this->_doLogin(); $_pedido = new Vendas_DataView_Pedido_MapperView(); $_vistoria = new Vendas_DataView_Vistoria_MapperView(); $_where = new ZendT_Db_Where(); $_where->addFilter('pedido.id_empresa', Auth_Session_User::getInstance()->getIdEmpresa()); if (isset($where['placa'])) { $_where->addFilter('veiculo.placa', $where['placa']); } else { $_where->addFilter('pedido.status_edi', 'N'); } if (isset($where['cnpj_cliente']) && $where['cnpj_cliente']) { $_where->addFilter('cliente.codigo', $where['cnpj_cliente']); //33164021000100 } if (isset($where['dt_emis']) && $where['dt_emis']) { if (!is_array($where['dt_emis'])) { $where['dt_emis'] = array($where['dt_emis']); } $where['dt_emis'][0] = new ZendT_Type_Date($where['dt_emis'][0], 'Date'); if (!isset($where['dt_emis'][1])) { $where['dt_emis'][1] = $where['dt_emis'][0]; } else { $where['dt_emis'][1] = new ZendT_Type_Date($where['dt_emis'][1], 'Date'); } $_where->addFilter('pedido.dt_emis', $where['dt_emis'], 'BETWEEN'); } $sql = "(SELECT 1" . " FROM " . Vendas_DataView_Vistoria_MapperView::$table . " as vistoria " . " WHERE vistoria.id_pedido = pedido.id" . " AND vistoria.laudo IS NULL)"; $_where->addFilterExists($sql); $_pedido->findAll($_where, '*'); $total = $_pedido->getCountRows(); $seq = 1; while ($row = $_pedido->fetch()) { try { $this->_log("Processando " . $seq . " de " . $total . ", Placa: " . $row['placa_veiculo']); $_vistoria->newRow(); $_vistoria->setIdPedido($row['id'])->retrieve(); if ($_vistoria->getLaudo(true)->toPhp() == '' && $_vistoria->getNumero(true)->toPhp() != '') { $laudo = $this->_laudo($_vistoria->getNumero()->get()); if ($laudo) { $_laudo = new ZendT_File(str_replace(array('-', '/'), '_', $_vistoria->getNumero()->get()) . '.pdf', $laudo, 'application/pdf'); $dataLaudo = array(); $dataLaudo['file'] = $_laudo; $_vistoria->setLaudo($dataLaudo); $_vistoria->update(); } } } catch (Exception $ex) { $message = 'Mensagem: ' . $ex->getMessage() . "\n"; $message .= 'Erro: ' . $ex->getTraceAsString() . "\n"; Vendas_Model_LogPedido_Mapper::log($_pedido->getId(), $ex->getMessage()); Tools_Model_LogErro_Mapper::log('Vendas_Interface_Vsp_Tokio', $message); $this->_log('Placa: ' . $row['placa_veiculo'] . ', Erro: ' . $message); } $seq++; } $this->_log('Finalizado: ' . date('d/m/Y H:s:i')); echo "OK"; }
public function importXls($fileName = '') { $cnpj = ''; $_pessoa = new Ca_Model_Pessoa_Mapper(); $_pessoa->setCodigo($cnpj)->retrieve(); $idCliente = $_pessoa->getId(); if (!$fileName) { $fileName = APPLICATION_PATH . '/modules/vendas/contexts/Vsp/tokio.xls'; } $objReader = ZendT_Excel_IOFactory::createReader('Excel5'); $objPHPExcel = $objReader->load($fileName); $data = $objPHPExcel->getSheet()->toArray(null, true, true, true); if (count($data) > 0) { $_pedido = new Vendas_DataView_Pedido_MapperView(); $_veiculo = new Frota_DataView_Veiculo_MapperView(); unset($data[0]); foreach ($data as $row) { $_veiculo->newRow()->setPlaca($row['D']); if ($_veiculo->exists()) { $_veiculo->setDescricao($row['C'])->setChassi($row['E'])->setChassi()->update(); } else { $_veiculo->setDescricao($row['C'])->setChassi($row['E'])->setChassi()->insert(); } $_pedido->newRow()->setIdCliente($idCliente)->setDtEmis($row['A'])->setSinistro($row['B'])->setIdVeiculo($_veiculo->getId()); if (!$_pedido->exists()) { $_pedido->insert(); } } } echo '<pre>'; print_r($data); echo '</pre>'; }