public function addCalc(Request\AddCalc $request)
 {
     $result = new Response\AddCalc();
     $calcTypeId = $request->getCalcTypeId();
     $dsBegin = $request->getDateStampBegin();
     $dsEnd = $request->getDateStampEnd();
     $def = $this->_manTrans->begin();
     try {
         /* create new period for given calculation type */
         $periodData = [Period::ATTR_CALC_TYPE_ID => $calcTypeId, Period::ATTR_DSTAMP_BEGIN => $dsBegin, Period::ATTR_DSTAMP_END => $dsEnd];
         $periodId = $this->_repoPeriod->create($periodData);
         /* create new calculation for the period */
         $dateStarted = $this->_toolDate->getUtcNowForDb();
         $calcData = [Calculation::ATTR_PERIOD_ID => $periodId, Calculation::ATTR_DATE_STARTED => $dateStarted, Calculation::ATTR_STATE => Cfg::CALC_STATE_STARTED];
         $calcId = $this->_repoCalc->create($calcData);
         $this->_manTrans->commit($def);
         /* compose response */
         $periodData[Period::ATTR_ID] = $periodId;
         $calcData[Calculation::ATTR_ID] = $calcId;
         $result->setPeriod($periodData);
         $result->setCalculation($calcData);
         $result->markSucceed();
     } finally {
         $this->_manTrans->end($def);
     }
     return $result;
 }
Example #2
0
 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();
     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->setTransactions([[Transaction::ATTR_DEBIT_ACC_ID => $respAccDebit->getId(), Transaction::ATTR_CREDIT_ACC_ID => $respAccCredit->getId(), Transaction::ATTR_VALUE => $value]]);
         $respAddOper = $this->_callOperation->add($reqAddOper);
         if ($respAddOper->isSucceed()) {
             $result->markSucceed();
         }
     }
     return $result;
 }
 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;
 }
 /**
  * This function is created for CRAP reducing and is used from this class only.
  *
  * @param \Praxigento\BonusBase\Service\Period\Response\GetForDependentCalc $result
  * @param string $baseCalcTypeCode
  * @param string $baseDsBegin
  * @param string $baseDsEnd
  * @param string $dependentCalcTypeCode
  * @param int $dependentCalcTypeId
  */
 public function _getForCompleteBase(\Praxigento\BonusBase\Service\Period\Response\GetForDependentCalc $result, $baseCalcTypeCode, $baseDsBegin, $baseDsEnd, $dependentCalcTypeCode, $dependentCalcTypeId)
 {
     $dependPeriodData = $this->_repoService->getLastPeriodByCalcType($dependentCalcTypeId);
     if (is_null($dependPeriodData)) {
         /* there is no dependent period, create new period and calc */
         $msg = "There is no period data for calculation '{$dependentCalcTypeCode}'." . " New period and related calculation will be created.";
         $this->_logger->warning($msg);
         /* create new period for given calculation type */
         $period = new EPeriod();
         $period->setCalcTypeId($dependentCalcTypeId);
         $period->setDstampBegin($baseDsBegin);
         $period->setDstampEnd($baseDsEnd);
         $periodId = $this->_repoPeriod->create($period);
         $period->setId($periodId);
         /* create related calculation */
         $calc = new ECalculation();
         $calc->setPeriodId($periodId);
         $dateStarted = $this->_toolDate->getUtcNowForDb();
         $calc->setDateStarted($dateStarted);
         $calc->setState(Cfg::CALC_STATE_STARTED);
         $calcId = $this->_repoCalc->create($calc);
         $calc->setId($calcId);
         /* place newly created objects into the response */
         $result->setDependentPeriodData($period);
         $result->setDependentCalcData($calc);
     } else {
         /* there is depended period, place period data into response */
         $result->setDependentPeriodData($dependPeriodData);
         /* then analyze base/depended periods begin/end  */
         $dependentDsBegin = $dependPeriodData->getDstampBegin();
         $dependentDsEnd = $dependPeriodData->getDstampEnd();
         $this->_getDependedCalcForExistingPeriod($result, $baseCalcTypeCode, $baseDsBegin, $baseDsEnd, $dependentCalcTypeCode, $dependentCalcTypeId, $dependentDsBegin, $dependentDsEnd);
     }
 }
 /**
  *
  * Get PV related period data if no period yet exist.
  *
  * @param \Praxigento\BonusBase\Service\Period\Response\GetForPvBasedCalc $result
  * @param string $periodType
  * @param int $calcTypeId
  * @return \Praxigento\BonusBase\Service\Period\Response\GetForPvBasedCalc
  */
 public function getNewPeriodDataForPv(\Praxigento\BonusBase\Service\Period\Response\GetForPvBasedCalc $result, $periodType, $calcTypeId)
 {
     /* we should lookup for first PV transaction and calculate first period range */
     $firstDate = $this->_repoService->getFirstDateForPvTransactions();
     if ($firstDate === false) {
         $this->_logger->warning("There is no PV transactions yet. Nothing to do.");
         $result->setErrorCode($result::ERR_HAS_NO_PV_TRANSACTIONS_YET);
     } else {
         $this->_logger->info("First PV transaction was performed at '{$firstDate}'.");
         $periodMonth = $this->_toolPeriod->getPeriodCurrent($firstDate, $periodType);
         $dsBegin = $this->_toolPeriod->getPeriodFirstDate($periodMonth);
         $dsEnd = $this->_toolPeriod->getPeriodLastDate($periodMonth);
         /* create new period for given calculation type */
         $period = new EPeriod();
         $period->setCalcTypeId($calcTypeId);
         $period->setDstampBegin($dsBegin);
         $period->setDstampEnd($dsEnd);
         $periodId = $this->_repoPeriod->create($period);
         $period->setId($periodId);
         /* create related calculation */
         $calc = new ECalculation();
         $calc->setPeriodId($periodId);
         $dateStarted = $this->_toolDate->getUtcNowForDb();
         $calc->setDateStarted($dateStarted);
         $calc->setState(Cfg::CALC_STATE_STARTED);
         $calcId = $this->_repoCalc->create($calc);
         $calc->setId($calcId);
         /* place newly created objects into the response */
         $result->setPeriodData($period);
         $result->setCalcData($calc);
     }
     return $result;
 }
 public function markCalcComplete($calcId)
 {
     $tsEnded = $this->_toolDate->getUtcNowForDb();
     $bind = [ECalculation::ATTR_DATE_ENDED => $tsEnded, ECalculation::ATTR_STATE => Cfg::CALC_STATE_COMPLETE];
     $where = ECalculation::ATTR_ID . '=' . $calcId;
     $result = $this->_repoCalc->update($bind, $where);
     return $result;
 }
 /**
  * Create new period record and related calculation record.
  *
  * @param $calcTypeId
  * @param $dsBegin
  * @param $dsEnd
  *
  * @return DataObject
  */
 public function addNewPeriodAndCalc($calcTypeId, $dsBegin, $dsEnd)
 {
     $result = new DataObject();
     /* add new period */
     $periodData = [Period::ATTR_CALC_TYPE_ID => $calcTypeId, Period::ATTR_DSTAMP_BEGIN => $dsBegin, Period::ATTR_DSTAMP_END => $dsEnd];
     $periodId = $this->_repoBasic->addEntity(Period::ENTITY_NAME, $periodData);
     $this->_logger->info("New period #{$periodId} for calculation type #{$calcTypeId} is registered ({$dsBegin}-{$dsEnd}).");
     $periodData[Period::ATTR_ID] = $periodId;
     $result->setData(self::DATA_PERIOD, $periodData);
     /* add related calculation */
     $dateStarted = $this->_toolDate->getUtcNowForDb();
     $calcData = [Calculation::ATTR_PERIOD_ID => $periodId, Calculation::ATTR_DATE_STARTED => $dateStarted, Calculation::ATTR_DATE_ENDED => null, Calculation::ATTR_STATE => Cfg::CALC_STATE_STARTED];
     $calcId = $this->_repoBasic->addEntity(Calculation::ENTITY_NAME, $calcData);
     $this->_logger->info("New calculation #{$calcId} for period #{$periodId} is registered.");
     $calcData[Calculation::ATTR_ID] = $calcId;
     $result->setData(self::DATA_CALC, $calcData);
     return $result;
 }
 public function execute(\Magento\Framework\Event\Observer $observer)
 {
     /** @var \Magento\Sales\Model\Order\Invoice $invoice */
     $invoice = $observer->getData(self::DATA_INVOICE);
     $state = $invoice->getState();
     if ($state == \Magento\Sales\Model\Order\Invoice::STATE_PAID) {
         /* update date_paid in the PV registry */
         /** @var \Magento\Sales\Model\Order $order */
         $order = $invoice->getOrder();
         $orderId = $order->getEntityId();
         if ($orderId) {
             $datePaid = $this->_toolDate->getUtcNowForDb();
             $this->_logger->debug("Update paid date in PV registry on sale order (#{$orderId}) is paid.");
             $data = [ESale::ATTR_DATE_PAID => $datePaid];
             $this->_repoSale->updateById($orderId, $data);
             /* transfer PV to customer account */
             $this->_subRegister->accountPv($order);
         }
     }
 }
 /** @inheritdoc */
 public function processHttpRequest($getVar)
 {
     /* get code from cookie */
     $cookie = $this->_cookieManager->getCookie(static::COOKIE_REFERRAL_CODE);
     $voCookie = new ReferralCookie($cookie);
     /* replace cookie value if GET code is not equal to cookie value */
     if ($getVar && $getVar != $voCookie->getCode()) {
         $tsSaved = $this->_toolDate->getUtcNow();
         $saved = $tsSaved->format('Ymd');
         $voCookie->setCode($getVar);
         $voCookie->setDateSaved($saved);
         $cookie = $voCookie->generateCookieValue();
         $meta = new \Magento\Framework\Stdlib\Cookie\PublicCookieMetadata();
         $meta->setPath('/');
         $meta->setDurationOneYear();
         $this->_cookieManager->setPublicCookie(static::COOKIE_REFERRAL_CODE, $cookie, $meta);
     }
     /* save referral code into the registry */
     $code = $voCookie->getCode();
     $this->replaceCodeInRegistry($code);
 }
 public function execute(\Magento\Framework\Event\Observer $observer)
 {
     /** @var \Magento\Sales\Model\Order\Invoice $invoice */
     $invoice = $observer->getData(self::DATA_INVOICE);
     $state = $invoice->getState();
     if ($state == \Magento\Sales\Model\Order\Invoice::STATE_PAID) {
         try {
             /* update date_paid in the PV registry */
             /** @var \Magento\Sales\Model\Order $order */
             $order = $invoice->getOrder();
             $orderId = $order->getEntityId();
             $datePaid = $this->_toolDate->getUtcNowForDb();
             $this->_logger->debug("Update paid date in PV registry on sale order (#{$orderId}) is paid.");
             $data = [ESale::ATTR_DATE_PAID => $datePaid];
             $this->_repoSale->updateById($orderId, $data);
             /* transfer PV to customer account */
             $this->_subRegister->accountPv($order);
         } catch (\Throwable $e) {
             /* catch all exceptions and steal them */
             $msg = 'Some error is occurred on update of the paid date in PV register. Error: ' . $e->getMessage();
             $this->_logger->error($msg);
         }
     }
 }
 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;
 }
 /**
  * @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 test_main()
 {
     $this->_logger->debug('Story01 in Warehouse Integration tests is started.');
     $this->_conn->beginTransaction();
     try {
         /* create Magento customers, category & product */
         $this->_createMageCustomers();
         $catId = $this->_createMageCategory('All Products');
         $prodId = $this->_createMageProduct($catId, 'sku001');
         /* create 2 stocks (warehouses)*/
         $stockId01 = $this->_createWarehouse('wrhs01', 'First warehouse');
         $stockId02 = $this->_createWarehouse('wrhs02', 'Second warehouse');
         /* create stock items */
         $stockItemIdDef = $this->_createMageStockItem(1, $prodId);
         $stockItemId01 = $this->_createMageStockItem($stockId01, $prodId);
         $stockItemId02 = $this->_createMageStockItem($stockId02, $prodId);
         /* create warehouse stock items (for prices) */
         $this->_createStockItem($stockItemIdDef, '100.32');
         $this->_createStockItem($stockItemId01, '100.43');
         $this->_createStockItem($stockItemId02, '100.54');
         /* create 2 lots */
         $expDate = $this->_toolDate->getMageNowForDb();
         $lotId01 = $this->_createLot('lot01', $expDate);
         $lotId02 = $this->_createLot('lot02', $expDate);
         /* add qtys to products */
         $this->_createQty($stockItemIdDef, $lotId01, 100);
         $this->_createQty($stockItemIdDef, $lotId02, 200);
         $this->_createQty($stockItemId01, $lotId01, 10);
         $this->_createQty($stockItemId01, $lotId02, 20);
         $this->_createQty($stockItemId02, $lotId01, 30);
         $this->_createQty($stockItemId02, $lotId02, 40);
     } finally {
         //            $this->_conn->commit();
         $this->_conn->rollBack();
     }
     $this->_logger->debug('Story01 in Warehouse Integration tests is completed, all transactions are rolled back.');
 }