/**
  * 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;
 }
 public function getLastDate(Request\GetLastDate $request)
 {
     $result = new Response\GetLastDate();
     $assetTypeId = $request->getAssetTypeId();
     $assetTypeCode = $request->getAssetTypeCode();
     if (is_null($assetTypeId)) {
         $assetTypeId = $this->_repoTypeAsset->getIdByCode($assetTypeCode);
     }
     /* get the maximal date for balance */
     $balanceMaxDate = $this->_repoBalance->getMaxDate($assetTypeId);
     if ($balanceMaxDate) {
         /* there is balance data */
         $dayBefore = $this->_toolPeriod->getPeriodPrev($balanceMaxDate, IPeriod::TYPE_DAY);
         $result->setData([Response\GetLastDate::LAST_DATE => $dayBefore]);
         $result->markSucceed();
     } else {
         /* there is no balance data yet, get transaction with minimal date */
         $transactionMinDate = $this->_repoTransaction->getMinDateApplied($assetTypeId);
         if ($transactionMinDate) {
             $period = $this->_toolPeriod->getPeriodCurrent($transactionMinDate);
             $dayBefore = $this->_toolPeriod->getPeriodPrev($period, IPeriod::TYPE_DAY);
             $result->setData([Response\GetLastDate::LAST_DATE => $dayBefore]);
             $result->markSucceed();
         }
     }
     return $result;
 }
 public function change(Request\Change $request)
 {
     $result = new Response\Reset();
     $accCustId = $request->getCustomerAccountId();
     $adminUserId = $request->getAdminUserId();
     $value = $request->getChangeValue();
     $def = $this->_manTrans->begin();
     try {
         /* get account's asset type by ID */
         $assetTypeId = $this->_repoAccount->getAssetTypeId($accCustId);
         /* get representative account id for given asset type */
         $accRepresId = $this->_repoMod->getRepresentativeAccountId($assetTypeId);
         /* get operation type by code and date performed */
         $operTypeId = $this->_repoTypeOper->getIdByCode(Cfg::CODE_TYPE_OPER_CHANGE_BALANCE);
         $dateNow = $this->_toolDate->getUtcNowForDb();
         /* create operation */
         $operation = new \Praxigento\Accounting\Data\Entity\Operation();
         $operation->setTypeId($operTypeId);
         $operation->setDatePerformed($dateNow);
         $operId = $this->_repoOperation->create($operation);
         /* create transaction */
         $trans = new \Praxigento\Accounting\Data\Entity\Transaction();
         $trans->setOperationId($operId);
         $trans->setDateApplied($dateNow);
         if ($value > 0) {
             $trans->setDebitAccId($accRepresId);
             $trans->setCreditAccId($accCustId);
         } else {
             $trans->setDebitAccId($accCustId);
             $trans->setCreditAccId($accRepresId);
         }
         $trans->setValue(abs($value));
         $this->_repoTransaction->create($trans);
         /* log details (operator name who performs the operation) */
         $log = new ELogChangeAdmin();
         $log->setOperationRef($operId);
         $log->setUserRef($adminUserId);
         $this->_repoLogChangeAdmin->create($log);
         $this->_manTrans->commit($def);
         $result->markSucceed();
     } finally {
         $this->_manTrans->end($def);
     }
     return $result;
 }