Ejemplo n.º 1
0
 /**
  * Add new transaction and update current balances.
  *
  * @param Request\Add $request
  *
  * @return Response\Add
  */
 public function add(Request\Add $request)
 {
     $result = new Response\Add();
     $debitAccId = $request->getDebitAccId();
     $creditAccId = $request->getCreditAccId();
     $operationId = $request->getOperationId();
     $dateApplied = $request->getDateApplied();
     $value = $request->getValue();
     $def = $this->_manTrans->begin();
     try {
         /* get account type for debit account */
         $debitAcc = $this->_repoAcc->getById($debitAccId);
         $debitAssetTypeId = $debitAcc->getAssetTypeId();
         /* get account type for credit account */
         $creditAcc = $this->_repoAcc->getById($creditAccId);
         $creditAssetTypeId = $creditAcc->getAssetTypeId();
         /* asset types should be equals */
         if (!is_null($debitAssetTypeId) && $debitAssetTypeId == $creditAssetTypeId) {
             /* add transaction */
             $toAdd = [Transaction::ATTR_OPERATION_ID => $operationId, Transaction::ATTR_DEBIT_ACC_ID => $debitAccId, Transaction::ATTR_CREDIT_ACC_ID => $creditAccId, Transaction::ATTR_VALUE => $value, Transaction::ATTR_DATE_APPLIED => $dateApplied];
             $idCreated = $this->_repoTrans->create($toAdd);
             $result->setTransactionId($idCreated);
         } else {
             throw new \Exception("Asset type (#{$debitAssetTypeId}) for debit account #{$debitAccId} is not equal to " . "asset type (#{$creditAssetTypeId}) for credit account {$creditAccId}.");
         }
         $this->_manTrans->commit($def);
         $result->markSucceed();
     } finally {
         $this->_manTrans->end($def);
     }
     return $result;
 }