/** * 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; }
protected function _populateCustomerData() { $result[self::CFG_CUST_BALANCE] = 0; if ($this->sessionCustomer) { $customerId = $this->sessionCustomer->getCustomerId(); $assetTypeId = $this->repoAssetType->getIdByCode(\Praxigento\Wallet\Config::CODE_TYPE_ASSET_WALLET_ACTIVE); $account = $this->repoAccount->getByCustomerId($customerId, $assetTypeId); if ($account) { $balance = $account->getBalance(); $result[self::CFG_CUST_BALANCE] = $balance; } } return $result; }
public function create($data) { $result = parent::create($data); if ($result) { /* update balalnces for accounts */ if (is_array($data)) { $data = new Entity($data); } $value = $data->getValue(); $creditAccid = $data->getCreditAccId(); $debitAccId = $data->getDebitAccId(); $this->_repoAccount->updateBalance($creditAccid, 0 + $value); $this->_repoAccount->updateBalance($debitAccId, 0 - $value); } return $result; }
/** * @param Request\GetRepresentative $request * * @return Response\GetRepresentative */ public function getRepresentative(Request\GetRepresentative $request) { $result = new Response\GetRepresentative(); $typeId = $request->getAssetTypeId(); $typeCode = $request->getAssetTypeCode(); $this->_logger->info("'Get representative account' operation is called."); if (is_null($typeId)) { $typeId = $this->_repoTypeAsset->getIdByCode($typeCode); } if (!is_null($typeId)) { if (isset($this->_cachedRepresentAccs[$typeId])) { $result->setData($this->_cachedRepresentAccs[$typeId]); $result->markSucceed(); } else { /* there is no cached data yet */ /* get representative customer ID */ $customerId = $this->_repoAccount->getRepresentativeCustomerId(); /* get all accounts for the representative customer */ $accounts = $this->_repoAccount->getAllByCustomerId($customerId); if ($accounts) { $mapped = []; foreach ($accounts as $one) { $mapped[$one->getAssetTypeId()] = $one; } $this->_cachedRepresentAccs = $mapped; } /* check selected accounts */ if (isset($this->_cachedRepresentAccs[$typeId])) { $result->setData($this->_cachedRepresentAccs[$typeId]); $result->markSucceed(); } else { /* there is no accounts yet */ $req = new Request\Get(); $req->setCustomerId($customerId); $req->setAssetTypeId($typeId); $req->setCreateNewAccountIfMissed(); $resp = $this->get($req); $accData = $resp->getData(); $this->_cachedRepresentAccs[$accData[Account::ATTR_ASSET_TYPE_ID]] = new Account($accData); $result->setData($accData); $result->markSucceed(); } } } else { $this->_logger->error("Asset type is not defined (asset code: {$typeCode})."); } if ($result->isSucceed()) { $repAccId = $result->getId(); $this->_logger->info("Representative account #{$repAccId} is found."); } $this->_logger->info("'Get representative account' operation is completed."); return $result; }
public function getRepresentativeAccountId($assetTypeId) { /* TODO: add cache for accounts ids */ $result = null; $custId = $this->getRepresentativeCustomerId(); if ($custId) { $found = $this->_repoAccount->getByCustomerId($custId, $assetTypeId); if ($found) { $result = $found->getId(); } } 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; }