示例#1
0
 /**
  * 
  * @param array $data
  * @return int
  */
 protected function _saveTransaction($data)
 {
     $dbFEFOPTransaction = App_Model_DbTable_Factory::get('FEFOPTransaction');
     $dbFEFOPContractFund = App_Model_DbTable_Factory::get('FEFOPContractFund');
     $where = array('fk_id_fefop_contract = ?' => $data['fk_id_fefop_contract']);
     $contractFund = $dbFEFOPContractFund->fetchRow($where);
     if (empty($contractFund)) {
         $message = sprintf('Kontratu %s seidauk iha folin husi Fundu.', Fefop_Model_Mapper_Contract::buildNumById($data['fk_id_fefop_contract']));
         $this->_message->addMessage($message, App_Message::ERROR);
         throw new Exception($message);
     }
     $mapperBudgetCategory = new Fefop_Model_Mapper_Expense();
     $expense = $mapperBudgetCategory->fetchRow($data['fk_id_budget_category']);
     $data['fk_id_budget_category_type'] = $expense->fk_id_budget_category_type;
     // If the value is already a transaction
     if (!empty($data['id_fefop_transaction'])) {
         $idTransaction = $data['id_fefop_transaction'];
         $where = array('id_fefop_transaction = ?' => $idTransaction);
         $row = $dbFEFOPTransaction->fetchRow($where);
     }
     if (empty($row)) {
         $row = $dbFEFOPTransaction->createRow();
         $data['fk_id_sysuser'] = Zend_Auth::getInstance()->getIdentity()->id_sysuser;
     }
     $idContract = $data['fk_id_fefop_contract'];
     $idExpense = $data['fk_id_budget_category'];
     switch ($data['fk_id_fefop_type_transaction']) {
         case self::TYPE_CONTRACT:
             $mapperContract = new Fefop_Model_Mapper_Contract();
             $contract = $mapperContract->detail($idContract);
             $statusForbidden = array(Fefop_Model_Mapper_Status::CANCELLED, Fefop_Model_Mapper_Status::CEASED, Fefop_Model_Mapper_Status::REJECTED);
             if (in_array($contract->id_fefop_status, $statusForbidden)) {
                 $message = sprintf('Kontratu %s ho status %s. Keta halo lansamentu', Fefop_Model_Mapper_Contract::buildNumById($idContract), $contract->status_description);
                 $this->_message->addMessage($message, App_Message::ERROR);
                 throw new Exception($message);
             }
             // Se custos acrescidos
             if (Fefop_Model_Mapper_ExpenseType::ADDITIONALS == $data['fk_id_budget_category_type']) {
                 break;
             }
             $totalContract = $mapperContract->getTotalContract($idContract);
             // Get financial total without additional costs
             $totalFinancial = abs($this->getTotalContract($idContract, true));
             // Sum up current transaction
             $totalFinancial += abs($data['amount']) - (double) $row->amount;
             // Check if the total financial is over the contract amount
             if ($totalFinancial > $totalContract) {
                 $currency = new Zend_Currency();
                 $amount = $currency->setValue($totalContract)->toCurrency();
                 $message = sprintf('Total Lansamentu hotu-hotu keta liu Total Kontratu hotu-hotu. %s', $amount);
                 $this->_message->addMessage($message, App_Message::ERROR);
                 throw new Exception($message);
             }
             $totalExpenseContract = $mapperContract->getTotalExpenseContract($idContract, $idExpense);
             $totalExpenseFinancial = abs($this->getTotalExpenseContract($idContract, $idExpense));
             // Sum up current transaction
             $totalExpenseFinancial += abs($data['amount']) - (double) $row->amount;
             if ($totalExpenseFinancial > $totalExpenseContract) {
                 $currency = new Zend_Currency();
                 $amount = $currency->setValue($totalExpenseContract)->toCurrency();
                 $message = sprintf("Total Rubrika liu Total Rubrika iha Kontratu. %s", $amount);
                 $this->_message->addMessage($message, App_Message::INFO);
             }
             break;
         case self::TYPE_REIMBURSEMENT:
             $totalPaymentsExpense = abs($this->getTotalExpenseContract($idContract, $idExpense, self::TYPE_CONTRACT));
             $totalReibursementExpense = abs($this->getTotalExpenseContract($idContract, $idExpense, self::TYPE_REIMBURSEMENT));
             // Sum up current transaction
             $totalReibursementExpense += abs($data['amount']) - (double) $row->amount;
             if ($totalPaymentsExpense <= 0) {
                 $message = 'Keta halo lansamentu devolusan, seidauk iha lansamentu ba pagamentu ba Rúbrica nee';
                 $this->_message->addMessage($message, App_Message::ERROR);
                 throw new Exception($message);
             }
             if ($totalReibursementExpense > $totalPaymentsExpense) {
                 $currency = new Zend_Currency();
                 $amount = $currency->setValue($totalPaymentsExpense)->toCurrency();
                 $message = 'Keta halo lansamentu devolusan, folin hira nee liu pagamentu hotu ba Rúbrica nee: %s';
                 $this->_message->addMessage(sprintf($message, $amount), App_Message::ERROR);
                 throw new Exception($message);
             }
             break;
     }
     $row->setFromArray($data);
     $row->save();
     // Calculate Additional cost proportionality
     $this->calcAdditionalCost($data['fk_id_fefop_contract']);
     // Try to change contract status
     $this->_tryUpdateStatusContract($data['fk_id_fefop_contract']);
     return $row;
 }