public function test_get_createNewAccountIfMissed()
 {
     /** === Test Data === */
     $assetTypeId = '12';
     $custId = '21';
     $accId = '34';
     /** === Setup Mocks === */
     // $data = $this->_repoAccount->getByCustomerId($customerId, $assetTypeId);
     $this->mRepoAccount->shouldReceive('getByCustomerId')->once()->andReturn([]);
     // $accId = $this->_repoAccount->create($data);
     $this->mRepoAccount->shouldReceive('create')->once()->andReturn($accId);
     /** === Call and asserts  === */
     $req = new Request\Get();
     $req->setCustomerId($custId);
     $req->setAssetTypeId($assetTypeId);
     $req->setCreateNewAccountIfMissed(true);
     $resp = $this->obj->get($req);
     $this->assertTrue($resp->isSucceed());
 }
 public function addToWalletActive(Request\AddToWalletActive $req)
 {
     $result = new Response\AddToWalletActive();
     $dateApplied = $req->getDateApplied();
     $datePerformed = $req->getDatePerformed();
     $operTypeCode = $req->getOperationTypeCode();
     $transData = $req->getTransData();
     $asAmount = $req->getAsAmount();
     $asCustId = $req->getAsCustomerId();
     $asRef = $req->getAsRef();
     $this->_logger->info("'Add to Wallet Active' operation is started.");
     /* prepare additional data */
     $datePerformed = is_null($datePerformed) ? $this->_toolDate->getUtcNowForDb() : $datePerformed;
     $dateApplied = is_null($dateApplied) ? $datePerformed : $dateApplied;
     /* get asset type ID */
     $assetTypeId = $this->_repoMod->getTypeAssetIdByCode(Cfg::CODE_TYPE_ASSET_WALLET_ACTIVE);
     /* get representative customer ID */
     $represAccId = $this->_getRepresentativeAccId($assetTypeId);
     /* save operation */
     $reqOperAdd = new \Praxigento\Accounting\Service\Operation\Request\Add();
     $reqOperAdd->setOperationTypeCode($operTypeCode);
     $reqOperAdd->setDatePerformed($datePerformed);
     $reqOperAdd->setAsTransRef($asRef);
     $trans = [];
     $reqGetAccount = new AccountGetRequest();
     $reqGetAccount->setCreateNewAccountIfMissed();
     $reqGetAccount->setAssetTypeId($assetTypeId);
     foreach ($transData as $item) {
         $custId = $item[$asCustId];
         $value = $item[$asAmount];
         if ($value > 0) {
             /* get WALLET_ACTIVE account ID for customer */
             $reqGetAccount->setCustomerId($custId);
             $respGetAccount = $this->_callAccount->get($reqGetAccount);
             $accId = $respGetAccount->getData(Account::ATTR_ID);
             $one = [Transaction::ATTR_DEBIT_ACC_ID => $represAccId, Transaction::ATTR_CREDIT_ACC_ID => $accId, Transaction::ATTR_DATE_APPLIED => $dateApplied, Transaction::ATTR_VALUE => $value];
             if (!is_null($asRef) && isset($item[$asRef])) {
                 $one[$asRef] = $item[$asRef];
             }
             $trans[] = $one;
             $this->_logger->debug("Transaction ({$value}) for customer #{$custId} (acc #{$accId}) is added to operation with type '{$operTypeCode}'.");
         } else {
             $this->_logger->debug("Transaction for customer #{$custId} is '{$value}'. Transaction is not included in operation with type '{$operTypeCode}'.");
         }
     }
     $reqOperAdd->setTransactions($trans);
     $respOperAdd = $this->_callOper->add($reqOperAdd);
     $operId = $respOperAdd->getOperationId();
     $this->_logger->debug("New operation (type id '{$operTypeCode}') is added with id={$operId} .");
     $result->setData($respOperAdd->getData());
     $result->markSucceed();
     $this->_logger->info("'Add to Wallet Active' operation is completed.");
     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;
 }
 /**
  * @param      $updates array [[Calc::A_CUST_ID, Calc::A_VALUE], ...]
  * @param      $operTypeCode
  * @param null $datePerformed
  * @param null $dateApplied
  *
  * @return \Praxigento\Accounting\Service\Operation\Response\Add
  */
 public function saveOperationWalletActive($updates, $operTypeCode, $datePerformed = null, $dateApplied = null)
 {
     /* prepare additional data */
     $datePerformed = is_null($datePerformed) ? $this->_toolDate->getUtcNowForDb() : $datePerformed;
     $dateApplied = is_null($dateApplied) ? $datePerformed : $dateApplied;
     /* get asset type ID */
     $assetTypeId = $this->_repoTypeAsset->getIdByCode(Cfg::CODE_TYPE_ASSET_WALLET_ACTIVE);
     /* get representative account data */
     $reqAccRepres = new AccountGetRepresentativeRequest();
     $reqAccRepres->setAssetTypeId($assetTypeId);
     $respAccRepres = $this->_callAccount->getRepresentative($reqAccRepres);
     $represAccId = $respAccRepres->getData(Account::ATTR_ID);
     /* save operation */
     $req = new OperationAddRequest();
     $req->setOperationTypeCode($operTypeCode);
     $req->setDatePerformed($datePerformed);
     $trans = [];
     $reqGetAccount = new AccountGetRequest();
     $reqGetAccount->setCreateNewAccountIfMissed();
     $reqGetAccount->setAssetTypeId($assetTypeId);
     foreach ($updates as $item) {
         $customerId = $item[Calc::A_CUST_ID];
         $value = $item[Calc::A_VALUE];
         if ($value > 0) {
             /* get WALLET_ACTIVE account ID for customer */
             $reqGetAccount->setCustomerId($customerId);
             $respGetAccount = $this->_callAccount->get($reqGetAccount);
             $accId = $respGetAccount->getData(Account::ATTR_ID);
             /* skip representative account */
             if ($accId == $represAccId) {
                 continue;
             }
             $trans[] = [Transaction::ATTR_DEBIT_ACC_ID => $represAccId, Transaction::ATTR_CREDIT_ACC_ID => $accId, Transaction::ATTR_DATE_APPLIED => $dateApplied, Transaction::ATTR_VALUE => $value];
             $this->_logger->debug("Transaction ({$value}) for customer #{$customerId} (acc #{$accId}) is added to operation '{$operTypeCode}'.");
         } else {
             $this->_logger->debug("Transaction for customer #{$customerId} is 0.00. Transaction is not included in operation '{$operTypeCode}'.");
         }
     }
     $req->setTransactions($trans);
     $result = $this->_callOper->add($req);
     $operId = $result->getOperationId();
     $this->_logger->debug("New '{$operTypeCode}' operation is added with id={$operId}.");
     return $result;
 }