public function saveAction()
 {
     $this->_helper->layout->disableLayout();
     $array = $this->_request->getParams();
     $arrayRequest = array();
     if ($array['campo'] == 'dsobservation') {
         $arrayRequest['dsobservation'] = $array['valor'];
     } else {
         $arrayRequest['nmpassword'] = $array['valor'];
     }
     $requestModel = new RequestModel();
     $requestModel->update($arrayRequest, 'cdrequest =' . $array['cdrequest']);
 }
 public function saveAction()
 {
     $uniqueitens = array();
     $trackErrors = 0;
     $returnsaverequest = array();
     $userInfo = $_SESSION['Gslab_Session']['user'];
     $bootstrap = $this->getInvokeArg('bootstrap');
     $gslab_db = $bootstrap->getResource('db');
     $values = $this->_request->getParams();
     $billingtabdata = json_decode($values['billingtabdata'], true);
     if (isset($values['additionalitensgrid']) && $values['additionalitensgrid'] != null) {
         $additionalitens = $values['additionalitensgrid'];
     }
     if (isset($additionalitens) && $additionalitens != 'null') {
         $additionalitens = json_decode($additionalitens, true);
         foreach ($additionalitens as $unique) {
             $uniqueitens[] = $unique['cdsupply'];
         }
     }
     $billingtabdata['fgaccountentrytype'] = 1;
     $billingtabdata['fgaccountcheck'] = 1;
     //        var_dump($billingtabdata);
     //        var_dump($values);
     //        var_dump(array_unique($uniqueitens)); die;
     $requestModel = new RequestModel();
     $accentryModel = new AccountentryModel();
     if (isset($values['dtrequest']) && $values['dtrequest'] != '') {
         $dtrequest = $this->_request->getParam('dtrequest');
         $daterq = new Zend_Date($dtrequest, 'dd-MM-YYYY HH:mm:ss');
         $values['dtrequest'] = $daterq->toString('YYYY-MM-dd HH:mm:ss');
     }
     if (isset($values['dtdeadline']) && $values['dtdeadline'] != '') {
         $dtdeadline = $this->_request->getParam('dtdeadline');
         $datedl = new Zend_Date($dtdeadline, 'dd-MM-YYYY HH:mm:ss');
         $values['dtdeadline'] = $datedl->toString('YYYY-MM-dd HH:mm:ss');
     }
     if (isset($billingtabdata['dtentrydate']) && $billingtabdata['dtentrydate'] != '') {
         $dtentrydate = $billingtabdata['dtentrydate'];
         $dateed = new Zend_Date($dtentrydate, 'dd-MM-YYYY HH:mm:ss');
         $billingtabdata['dtentrydate'] = $dateed->toString('YYYY-MM-dd HH:mm:ss');
     }
     if (isset($values)) {
         $gslab_db->beginTransaction();
         try {
             $columnsRequest = $requestModel->info(Zend_Db_Table_Abstract::COLS);
             $columnsAccEntry = $accentryModel->info(Zend_Db_Table_Abstract::COLS);
             $arrayRequest = array();
             $arrayAccEntry = array();
             $arrayAccEntryReq = array();
             $arrayAccEntrySupply = array();
             $arrayAccCheck = array();
             $arrayAccCheckAccEntry = array();
             $arraySupplyFlow = array();
             foreach ($values as $id => $value) {
                 if (isset($value) && $value != '' && in_array($id, $columnsRequest)) {
                     $arrayRequest[$id] = addslashes($value);
                 }
             }
             if (isset($values['cdrequest']) && $values['cdrequest'] == '') {
                 $requestModel->insert($arrayRequest);
                 $lastCDrequest = $gslab_db->lastInsertId();
             } else {
                 // Ativa a Requisição (que já foi salva como inativa ao realizar a troca de abas)
                 $arrayRequest['fgactive'] = 1;
                 $requestModel->update($arrayRequest, "request.cdrequest = " . $values['cdrequest']);
             }
             //                $requestModel->getAdapter()->commit();
             // Verifica se existem itens a serem salvos!
             if (isset($additionalitens) && $additionalitens != 'null') {
                 // Salva Dados na AccountEntry
                 //                    $accentryModel->getAdapter()->beginTransaction();
                 try {
                     foreach ($billingtabdata as $id => $value) {
                         if (isset($value) && $value != '' && in_array($id, $columnsAccEntry)) {
                             $arrayAccEntry[$id] = addslashes($value);
                         }
                     }
                     $arrayAccEntry['cdclient'] = $values['cdclient'];
                     $arrayAccEntry['dttransactiondate'] = $values['dtrequest'];
                     $arrayAccEntry['dtdeadline'] = $values['dtdeadline'];
                     $arrayAccEntry['dtinvoicedate'] = $values['dtdeadline'];
                     $accentryModel->insert($arrayAccEntry);
                     $lastCDaccEntry = $gslab_db->lastInsertId();
                     //                         $accentryModel->getAdapter()->commit();
                 } catch (Exception $ae) {
                     //                        $accentryModel->getAdapter()->rollBack();
                     $trackErrors++;
                     $returnsaverequest['idreturn'] = 0;
                     $returnsaverequest['messagereturn'] = "Erro: Entrada de Contas";
                 }
                 // Salva Dados na AccountEntryRequest
                 $accountentryrequestModel = new AccountentryrequestModel();
                 //                    $accountentryrequestModel->getAdapter()->beginTransaction();
                 try {
                     $arrayAccEntryReq["cdaccountentry"] = $lastCDaccEntry;
                     if (!isset($values['cdrequest'])) {
                         $arrayAccEntryReq["cdrequest"] = $lastCDrequest;
                     } else {
                         $arrayAccEntryReq["cdrequest"] = $values['cdrequest'];
                     }
                     $accountentryrequestModel->insert($arrayAccEntryReq);
                     //                        $accountentryrequestModel->getAdapter()->commit();
                 } catch (Exception $aeq) {
                     //                        $accountentryrequestModel->getAdapter()->rollBack();
                     $trackErrors++;
                     $returnsaverequest['idreturn'] = 0;
                     $returnsaverequest['messagereturn'] = "Erro: Requisiçao/Cobrança ";
                 }
                 // Salva Dados na SupplyFlow
                 $supplyflowModel = new SupplyflowModel();
                 //                    $supplyflowModel->getAdapter()->beginTransaction();
                 try {
                     if (isset($additionalitens)) {
                         foreach ($additionalitens as $itens) {
                             $arraySupplyFlow["cdsupply"] = $itens['cdsupply'];
                             $arraySupplyFlow["qtquantity"] = $itens['amount'];
                             $arraySupplyFlow["cdsupplycentrefrom"] = $itens['cdsupplycentre'];
                             $arraySupplyFlow["cdsupplycentreto"] = 1;
                             $arraySupplyFlow["fgsupplyioreason"] = 1;
                             $arraySupplyFlow["dtcheckout"] = $values['dtrequest'];
                             $supplyflowModel->insert($arraySupplyFlow);
                         }
                         //                            $supplyflowModel->getAdapter()->commit();
                     }
                 } catch (Exception $sfw) {
                     //                        $supplyflowModel->getAdapter()->rollBack();
                     $trackErrors++;
                     $returnsaverequest['idreturn'] = 0;
                     $returnsaverequest['messagereturn'] = "Erro: Movimentação de Itens";
                 }
                 //|
                 //====> Desconta Itens do Estoque no SupplyStock
                 $supplystockModel = new SupplyStockModel();
                 //                            $supplystockModel->getAdapter()->beginTransaction();
                 try {
                     if (isset($additionalitens)) {
                         foreach ($additionalitens as $itens) {
                             $datadiscount = array("qtquantity" => new Zend_Db_Expr("qtquantity - " . $itens['amount']), "qtsupplybalance" => new Zend_Db_Expr("qtsupplybalance - " . $itens['amount']), "dtcheckin" => $values['dtrequest']);
                             $supplystockModel->getAdapter()->update('supplystock', $datadiscount, "cdsupply = " . $itens['cdsupply'] . " AND cdsupplycentre = " . $itens['cdsupplycentre']);
                         }
                         //                                    $supplystockModel->getAdapter()->commit();
                     }
                 } catch (Exception $sstk) {
                     //                                $supplystockModel->getAdapter()->rollBack();
                     $trackErrors++;
                     $returnsaverequest['idreturn'] = 0;
                     $returnsaverequest['messagereturn'] = "Erro: Desconto do Estoque";
                 }
                 // Salva Dados na AccountEntrySupply
                 $accountentrysupplyModel = new AccountentrysupplyModel();
                 //                    $accountentrysupplyModel->getAdapter()->beginTransaction();
                 try {
                     if (isset($uniqueitens)) {
                         foreach ($uniqueitens as $itens) {
                             $arrayAccEntrySupply["cdaccountentry"] = $lastCDaccEntry;
                             $arrayAccEntrySupply["cdsupply"] = $itens;
                             $accountentrysupplyModel->insert($arrayAccEntrySupply);
                         }
                         //                            $accountentrysupplyModel->getAdapter()->commit();
                     }
                 } catch (Exception $aes) {
                     //                        $accountentrysupplyModel->getAdapter()->rollBack();
                     $trackErrors++;
                     $returnsaverequest['idreturn'] = 0;
                     $returnsaverequest['messagereturn'] = "Erro: Entradas de contas/Itens";
                 }
                 // Salva Dados na AccountCheck
                 $accountcheckModel = new AccountcheckModel();
                 $accountcheckaccountentryModel = new accountcheckaccountentryModel();
                 //                    $accountcheckModel->getAdapter()->beginTransaction();
                 try {
                     if (isset($values['dtrequest']) && isset($billingtabdata['vlaccountcheckvalue'])) {
                         $arrayAccCheck['cdusergslab'] = $billingtabdata['cdusergslab'];
                         $arrayAccCheck['dtaccountcheckdate'] = $values['dtrequest'];
                         $arrayAccCheck['vlaccountcheckvalue'] = $billingtabdata['vlaccountcheckvalue'];
                         $accountcheckModel->insert($arrayAccCheck);
                         $arrayAccCheckAccEntry['cdaccountcheck'] = $accountcheckModel->getAdapter()->lastInsertId();
                         $arrayAccCheckAccEntry['cdaccountentry'] = $lastCDaccEntry;
                         $accountcheckaccountentryModel->insert($arrayAccCheckAccEntry);
                         //                            $accountcheckModel->getAdapter()->commit();
                     }
                 } catch (Exception $aes) {
                     //                        $accountcheckModel->getAdapter()->rollBack();
                     $trackErrors++;
                     $returnsaverequest['idreturn'] = 0;
                     $returnsaverequest['messagereturn'] = "Erro: Checagem de contas";
                 }
             }
             if ($trackErrors == 0) {
                 $gslab_db->commit();
                 $returnsaverequest['idreturn'] = 1;
                 $returnsaverequest['messagereturn'] = "Cadastro efetuado com sucesso.";
             }
             $this->view->returnSaveRequest = $returnsaverequest;
         } catch (Exception $e) {
             $gslab_db->rollBack();
             //                $requestModel->getAdapter()->rollBack();
             $returnsaverequest['idreturn'] = 0;
             $returnsaverequest['messagereturn'] = "Erro ao salvar a requisição.";
             $this->view->returnSaveRequest = $returnsaverequest;
         }
     }
 }