public function dadosAction()
 {
     $modelMedicineModel = new MedicineModel();
     $getAllMedicineData = $modelMedicineModel->fetchAll($modelMedicineModel->getAllMedicineGrid());
     $this->_helper->layout->disableLayout();
     $page = $this->_request->getParam("page", 1);
     $limit = $this->_request->getParam("rows");
     $sidx = $this->_request->getParam("sidx", 1);
     $sord = $this->_request->getParam("sord");
     $medicineModel = new MedicineModel();
     $medicine = $medicineModel->fetchAll();
     $count = count($medicine);
     if ($count > 0 && $limit > 0) {
         $total_pages = ceil($count / $limit);
     } else {
         $total_pages = 0;
     }
     if ($page > $total_pages) {
         $page = $total_pages;
     }
     $getAllMedicineData = $medicineModel->fetchAll($modelMedicineModel->getAllMedicineGrid($sidx, $sord, $limit, $page));
     $response = new stdClass();
     $response->page = $page;
     $response->total = $total_pages;
     $response->records = $count;
     $i = 0;
     foreach ($getAllMedicineData as $row) {
         $responce->rows[$i]['cdmedicine'] = $row->cdmedicine;
         $response->rows[$i]['cell'] = array($row->idmedicine, $row->nmmedicine, $row->nmactive, $row->idnotation);
         $i++;
     }
     $data = '';
     $this->view->dados = $response;
 }
 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());
     }
 }