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;
 }
 private function _repoGetBalances()
 {
     $assetTypeId = $this->_repoTypeAsset->getIdByCode(Cfg::CODE_TYPE_ASSET_WALLET_ACTIVE);
     $where = Account::ATTR_ASSET_TYPE_ID . '=' . (int) $assetTypeId;
     $result = $this->_repoCore->getEntities(Account::ENTITY_NAME, null, $where);
     return $result;
 }
 private function _validateWalletsAfterTeam()
 {
     $EXPECT_REPRES_AMOUNT = -6.25;
     $EXPECT_BALANCE = [1 => 0.0, 2 => 0, 3 => 6.25, 4 => 0, 5 => 0, 6 => 0, 7 => 0, 8 => 0, 9 => 0, 10 => 0, 11 => 0, 12 => 0, 13 => 0];
     /* get Asset Type ID foe WALLET_ACTIVE */
     $assetTypeId = $this->_repoTypeAsset->getIdByCode(Cfg::CODE_TYPE_ASSET_WALLET_ACTIVE);
     /* get representative account for WALLET_ACTIVE */
     $reqRepresAcc = new AccGetRepresentativeRequest();
     $reqRepresAcc->setAssetTypeId($assetTypeId);
     $respRepresAcc = $this->_callAccAccount->getRepresentative($reqRepresAcc);
     $represAccId = $respRepresAcc->getData(Account::ATTR_ID);
     /* get data for WALLET_ACTIVE accounts */
     $where = Account::ATTR_ASSET_TYPE_ID . '=' . $assetTypeId;
     $balanceData = $this->_repoBasic->getEntities(Account::ENTITY_NAME, null, $where);
     /* convert balances to form that is relative to customer index (not id) */
     foreach ($balanceData as $one) {
         $accId = $one[Account::ATTR_ID];
         $custId = $one[Account::ATTR_CUST_ID];
         $balance = $one[Account::ATTR_BALANCE];
         if ($accId == $represAccId) {
             $this->assertEquals($EXPECT_REPRES_AMOUNT, $balance);
         } else {
             /* get customer index by customer id */
             $index = $this->_mapCustomerIndexByMageId[$custId];
             $expBalance = $EXPECT_BALANCE[$index];
             $this->assertEquals($expBalance, $balance);
         }
     }
 }
 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->_repoMod->getBalanceMaxDate($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->_repoMod->getTransactionMinDateApplied($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;
 }
 /**
  * @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;
 }
 /** @inheritdoc */
 public function getTypeAssetIdByCode($assetTypeCode)
 {
     $result = $this->_repoTypeAsset->getIdByCode($assetTypeCode);
     return $result;
 }