public function balanceSupplyStock($cdsupplycentrefrom, $cdsupplycentreto, $cdsupply, $qtquantity)
 {
     $supplystock = new SupplystockModel();
     /* >>> Update centro de saída
      * Busca na tabela supplyStock o registro por centro e item/supply(getCdsupplystockFirstDtcheckin) por menor data de entrada.
      */
     $supplystockDataFrom = $supplystock->fetchRow($supplystock->getCdsupplystockFirstDtcheckin($cdsupplycentrefrom, $cdsupply));
     $cdsupplystock = $supplystockDataFrom->cdsupplystock;
     $supplystockDataFrom->qtsupplybalance = $supplystockDataFrom->qtsupplybalance - $qtquantity;
     $supplystock->update($supplystockDataFrom->toArray(), "cdsupplystock = " . $cdsupplystock);
     /* <<< */
     /* >>> Update/Insert centro de entrada */
     /* Busca na tabela supplyStock o registro por centro, item/supply e nota fiscal.
      * Condição da cláusula where(getCdsupplystockByIdinvoice): busca nota fiscal do item do centro de saída igual a nota fiscal do centro de entrada.
      * Se satisfeita a condição é feito update senão, é feito insert.
      * 
      * Agora, pega essa gambi:
      */
     $supplystockDataTo = $supplystock->fetchRow($supplystock->getCdsupplystockByIdinvoice($cdsupplycentreto, $cdsupply, $supplystockDataFrom->idinvoice));
     if (sizeof($supplystockDataTo) > 0) {
         /*             * *UPDATE** */
         $cdsupplystock = $supplystockDataTo->cdsupplystock;
         $supplystockDataTo->qtsupplybalance = $supplystockDataTo->qtsupplybalance + $qtquantity;
         $supplystock->update($supplystockDataTo->toArray(), "cdsupplystock = " . $cdsupplystock);
     } else {
         /*             * *INSERT** */
         $cdsupplier = $supplystockDataFrom->cdsupplier;
         $vlunitprice = $supplystockDataFrom->vlunitprice;
         $dtexpire = $supplystockDataFrom->dtexpire;
         $idinvoice = $supplystockDataFrom->idinvoice;
         $dtcheckin = new Zend_Date(null, 'dd-MM-YYYY HH:mm:ss');
         $dtcheckin = $dtcheckin->toString('YYYY-MM-dd HH:mm:ss');
         $supplystockDataTo = array("cdsupplycentre" => $cdsupplycentreto, "cdsupply" => $cdsupply, "cdsupplier" => $cdsupplier, "qtquantity" => "0", "qtsupplybalance" => $qtquantity, "vlunitprice" => $vlunitprice, "dtexpire" => $dtexpire, "dtcheckin" => $dtcheckin, "idinvoice" => $idinvoice);
         $supplystock->insert($supplystockDataTo);
     }
     /* >>> */
 }
 public function saverequestmedicineAction()
 {
     $values = $this->_request->getParams();
     //        var_dump($values);die;
     $array = array();
     $MedicineModel = new MedicineModel();
     $requestMedicineModel = new RequestmedicineModel();
     $supplyStockModel = new SupplystockModel();
     $supplyModel = new SupplyModel();
     $columns = $requestMedicineModel->info(Zend_Db_Table_Abstract::COLS);
     $requestMedicineModel->getAdapter()->beginTransaction();
     try {
         if ($values['dtrequestmedicine'] != null && $values['hrrequestmedicine'] != null) {
             $dtmedicinerequest = new Zend_Date($values['dtrequestmedicine'], 'dd-MM-YYYY');
             $dtmedicinerequest = $dtmedicinerequest->toString('YYYY-MM-dd');
             $array['dtmedicinerequest'] = $dtmedicinerequest . ' ' . $values['hrrequestmedicine'] . ':00';
         }
         if ($values['fgstockdiscount'] == '1') {
             $cdsupply = $MedicineModel->fetchRow("cdmedicine = " . $values['cdmedicine']);
             $qtquantitySupplyStock = $supplyStockModel->fetchRow("cdsupply = " . $cdsupply['cdsupply']);
             $qtquantitySupply = $supplyModel->fetchRow("cdsupply = " . $cdsupply['cdsupply']);
             // Valida se a quantidade em estoque é maior que a quantidade a ser descontada
             if ($qtquantitySupplyStock['qtquantity'] > $values['nrstockdiscount'] && $qtquantitySupply['qtbalance'] > $values['nrstockdiscount']) {
                 $supplyStockModel->update(array("qtquantity" => $qtquantitySupplyStock['qtquantity'] - $values['nrstockdiscount'], "qtsupplybalance" => $qtquantitySupplyStock['qtsupplybalance'] - $values['nrstockdiscount']), "cdsupply = " . $cdsupply['cdsupply']);
                 $supplyModel->update(array("qtbalance" => $qtquantitySupply['qtbalance'] - $values['nrstockdiscount']), "cdsupply = " . $cdsupply['cdsupply']);
             }
         } else {
             $values['fgstockdiscount'] = '2';
         }
         foreach ($values as $id => $value) {
             if (isset($value) && $value != '' && $value != '0' && in_array($id, $columns)) {
                 $array[$id] = addslashes($value);
             } else {
                 if (in_array($id, $columns)) {
                     $array[$id] = null;
                 }
             }
         }
         if (empty($array)) {
             throw new Exception('Dados Incorretos. Confira seus dados e Tente Novamente.');
         } else {
             $requestMedicineModel->insert($array);
         }
         $requestMedicineModel->getAdapter()->commit();
     } catch (Exception $e) {
         $this->_model->getAdapter()->rollBack();
         $this->addMessage($e->getMessage());
     }
 }