/**
  * Get all balances for WALLET_ACTIVE asset and then reset it to zero.
  */
 private function _resetWalletBalances()
 {
     /* get WALLET_ACTIVE asset ID */
     $assetId = $this->_repoTypeAsset->getIdByCode(Cfg::CODE_TYPE_ASSET_WALLET_ACTIVE);
     /* get WALLET_ACTIVE representative account */
     $reqGetRepres = new AccGetRepresentativeRequest();
     $reqGetRepres->setAssetTypeCode(Cfg::CODE_TYPE_ASSET_WALLET_ACTIVE);
     $respGetRepres = $this->_callAccAccount->getRepresentative($reqGetRepres);
     $accountIdRepres = $respGetRepres->getData(Account::ATTR_ID);
     /* get all customer balances for WALLET_ACTIVE and create transactions */
     $whereByAsset = Account::ATTR_ASSET_TYPE_ID . '=' . $assetId;
     $whereByRepres = Account::ATTR_ID . '<>' . $accountIdRepres;
     $accounts = $this->_repoBasic->getEntities(Account::ENTITY_NAME, null, "{$whereByAsset} AND {$whereByRepres}");
     $trans = [];
     $datePerformed = $this->_toolPeriod->getTimestampTo(self::DATE_FEB_PERIOD_BEGIN);
     $dateApplied = $datePerformed;
     foreach ($accounts as $account) {
         $balance = $account[Account::ATTR_BALANCE];
         if ($balance != 0) {
             $tran = [Transaction::ATTR_DEBIT_ACC_ID => $account[Account::ATTR_ID], Transaction::ATTR_CREDIT_ACC_ID => $accountIdRepres, Transaction::ATTR_VALUE => $account[Account::ATTR_BALANCE], Transaction::ATTR_DATE_APPLIED => $dateApplied];
             $trans[] = $tran;
         }
     }
     /* create operation to write off all WALLET_ACTIVE balances */
     $reqOper = new AccOperationAddRequest();
     $reqOper->setOperationTypeCode(Cfg::CODE_TYPE_OPER_WALLET_TRANSFER);
     $reqOper->setDatePerformed($datePerformed);
     $reqOper->setTransactions($trans);
     $respOper = $this->_callAccOperation->add($reqOper);
     $this->assertTrue($respOper->isSucceed());
 }
 /**
  * Account PV on sale done.
  *
  * @param Request\AccountPv $request
  *
  * @return Response\AccountPv
  */
 public function accountPv(Request\AccountPv $request)
 {
     $result = new Response\AccountPv();
     $saleId = $request->getSaleOrderId();
     $customerId = $request->getCustomerId();
     $dateApplied = $request->getDateApplied();
     $this->_logger->info("PV accounting operation for sale order #{$saleId} is started.");
     $sale = $this->_repoSale->getById($saleId);
     $pvTotal = $sale->getTotal();
     /* get customer for sale order */
     if (is_null($customerId)) {
         $this->_logger->info("There is no customer ID in request, select customer ID from sale order data.");
         $customerId = $this->_repoMod->getSaleOrderCustomerId($saleId);
         $this->_logger->info("Order #{$saleId} is created by customer #{$customerId}.");
     }
     /* get PV account data for customer */
     $reqGetAccCust = new GetAccountRequest();
     $reqGetAccCust->setCustomerId($customerId);
     $reqGetAccCust->setAssetTypeCode(Cfg::CODE_TYPE_ASSET_PV);
     $reqGetAccCust->setCreateNewAccountIfMissed(true);
     $respGetAccCust = $this->_callAccount->get($reqGetAccCust);
     /* get PV account data for representative */
     $reqGetAccRepres = new GetAccountRepresentativeRequest();
     $reqGetAccRepres->setAssetTypeCode(Cfg::CODE_TYPE_ASSET_PV);
     $respGetAccRepres = $this->_callAccount->getRepresentative($reqGetAccRepres);
     /* create one operation with one transaction */
     $reqAddOper = new AddOperationRequest();
     $reqAddOper->setOperationTypeCode(Cfg::CODE_TYPE_OPER_PV_SALE_PAID);
     $trans = [Transaction::ATTR_DEBIT_ACC_ID => $respGetAccRepres->getId(), Transaction::ATTR_CREDIT_ACC_ID => $respGetAccCust->getId(), Transaction::ATTR_VALUE => $pvTotal, Transaction::ATTR_DATE_APPLIED => $dateApplied];
     $reqAddOper->setTransactions([$trans]);
     $respAddOper = $this->_callOperation->add($reqAddOper);
     $operId = $respAddOper->getOperationId();
     $result->setOperationId($operId);
     $result->markSucceed();
     $this->_logger->info("PV accounting operation for sale order #{$saleId} 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;
 }
 public function betweenCustomers(Request\BetweenCustomers $request)
 {
     $result = new Response\BetweenCustomers();
     /* constraints validation results */
     $isCountriesTheSame = false;
     $isTargetInDownline = false;
     /* extract input parameters */
     $custIdDebit = $request->getFromCustomerId();
     $custIdCredit = $request->getToCustomerId();
     $date = $request->getDateApplied();
     $value = $request->getValue();
     $condForceAll = $request->getConditionForceAll();
     $condForceCountry = $request->getConditionForceCountry();
     $condForceDownline = $request->getConditionForceDownline();
     $noteOper = $request->getNoteOperation();
     $noteTrans = $request->getNoteTransaction();
     if (is_null($date)) {
         $date = $this->_toolDate->getUtcNowForDb();
     }
     /* validate conditions */
     if (!$condForceAll) {
         /* validate customer countries */
         $downDebit = $this->_repoMod->getDownlineCustomerById($custIdDebit);
         $downCredit = $this->_repoMod->getDownlineCustomerById($custIdCredit);
         /* countries should be equals */
         $countryDebit = $downDebit->getCountryCode();
         $countryCredit = $downCredit->getCountryCode();
         if ($countryDebit == $countryCredit || $condForceCountry) {
             $isCountriesTheSame = true;
         }
         /* transfer is allowed to own subtree only */
         $path = $downCredit->getPath();
         $key = Cfg::DTPS . $downDebit->getCustomerId() . Cfg::DTPS;
         if (strpos($path, $key) !== false || $condForceDownline) {
             $isTargetInDownline = true;
         }
     }
     /* check validation results and perform transfer */
     if ($condForceAll || $isTargetInDownline && $isCountriesTheSame) {
         /* get PV-accounts */
         $reqAccGet = new AccountGetRequest();
         $reqAccGet->setCustomerId($custIdDebit);
         $reqAccGet->setAssetTypeCode(Cfg::CODE_TYPE_ASSET_PV);
         $reqAccGet->setCreateNewAccountIfMissed(true);
         $respAccDebit = $this->_callAccount->get($reqAccGet);
         $reqAccGet->setCustomerId($custIdCredit);
         $respAccCredit = $this->_callAccount->get($reqAccGet);
         /* add transfer operation */
         $reqAddOper = new OperationAddRequest();
         $reqAddOper->setOperationTypeCode(Cfg::CODE_TYPE_OPER_PV_TRANSFER);
         $reqAddOper->setDatePerformed($date);
         $reqAddOper->setOperationNote($noteOper);
         $reqAddOper->setTransactions([[Transaction::ATTR_DEBIT_ACC_ID => $respAccDebit->getId(), Transaction::ATTR_CREDIT_ACC_ID => $respAccCredit->getId(), Transaction::ATTR_VALUE => $value, Transaction::ATTR_NOTE => $noteTrans]]);
         $respAddOper = $this->_callOperation->add($reqAddOper);
         if ($respAddOper->isSucceed()) {
             $result->setOperationId($respAddOper->getOperationId());
             $result->setTransactionsIds($respAddOper->getTransactionsIds());
             $result->markSucceed();
         }
     }
     return $result;
 }
 private function _createOperation()
 {
     $req = new OperationAddRequest();
     $req->setOperationTypeId($this->typeOperId);
     $req->setDatePerformed(self::DATA_DATE_PERFORMED);
     $req->setTransactions([[Transaction::ATTR_DEBIT_ACC_ID => $this->acc1[Account::ATTR_ID], Transaction::ATTR_CREDIT_ACC_ID => $this->acc2[Account::ATTR_ID], Transaction::ATTR_VALUE => 5], [Transaction::ATTR_DEBIT_ACC_ID => $this->acc1[Account::ATTR_ID], Transaction::ATTR_CREDIT_ACC_ID => $this->acc2[Account::ATTR_ID], Transaction::ATTR_VALUE => 10], [Transaction::ATTR_DEBIT_ACC_ID => $this->acc1[Account::ATTR_ID], Transaction::ATTR_CREDIT_ACC_ID => $this->acc2[Account::ATTR_ID], Transaction::ATTR_VALUE => 15]]);
     $this->_amount = 5 + 10 + 15;
     /** @var  $resp OperationAddResponse */
     $resp = $this->_callOperation->add($req);
     $this->assertTrue($resp->isSucceed());
     $this->_logger->debug("New operation with 3 transactions is created.");
 }