public function test_bonus_success() { /** === Test Data === */ $DATE_PERFORMED = 'performed'; $DATE_APPLIED = 'applied'; $DS_BEGIN = 'begin'; $DS_END = 'end'; $CALC_TYPE_ID_COMPRESS = 4; $CALC_ID_COMPRESS = 8; $CALC_DATA_COMPRESS = [Calculation::ATTR_ID => $CALC_ID_COMPRESS]; $CONFI_PARAMS = []; $PERCENTS = 'percents'; $TREE_COMPRESS = []; $ORDERS = 'orders'; $UPDATES = ['custId' => ['rankId' => 'amount']]; $TRANS_LOG = []; /** === Setup Mocks === */ $this->mLogger->shouldReceive('info'); // $respGetPeriod = $this->_callBasePeriod->getForDependentCalc($reqGetPeriod); $mRespGetPeriod = new \Praxigento\BonusBase\Service\Period\Response\GetForDependentCalc(); $this->mCallBasePeriod->shouldReceive('getForDependentCalc')->once()->andReturn($mRespGetPeriod); // if($respGetPeriod->isSucceed()) { $mRespGetPeriod->markSucceed(); // $this->_getConn()->beginTransaction(); $this->mConn->shouldReceive('beginTransaction')->once(); // $dsBegin = $periodDataDepend[Period::ATTR_DSTAMP_BEGIN]; // $dsEnd = $periodDataDepend[Period::ATTR_DSTAMP_END]; $mRespGetPeriod->setDependentPeriodData([Period::ATTR_DSTAMP_BEGIN => $DS_BEGIN, Period::ATTR_DSTAMP_END => $DS_END]); // $calcTypeIdCompress = $this->_repoMod->getTypeCalcIdByCode(Cfg::CODE_TYPE_CALC_COMPRESSION); $this->mRepoMod->shouldReceive('getTypeCalcIdByCode')->andReturn($CALC_TYPE_ID_COMPRESS); // $calcDataCompress = $this->_repoMod->getLatestCalcForPeriod($calcTypeIdCompress, $dsBegin, $dsEnd); $this->mRepoMod->shouldReceive('getLatestCalcForPeriod')->with($CALC_TYPE_ID_COMPRESS, $DS_BEGIN, $DS_END)->andReturn($CALC_DATA_COMPRESS); // $params = $this->_repoMod->getConfigParams(); $this->mRepoMod->shouldReceive('getConfigParams')->andReturn($CONFI_PARAMS); // $percents = $this->_repoMod->getBonusPercents(); $this->mRepoMod->shouldReceive('getBonusPercents')->andReturn($PERCENTS); // $treeCompressed = $this->_repoMod->getCompressedTreeWithQualifications($calcIdCompress); $this->mRepoMod->shouldReceive('getCompressedTreeWithQualifications')->once()->with($CALC_ID_COMPRESS)->andReturn($TREE_COMPRESS); // $orders = $this->_repoMod->getSalesOrdersForPeriod($dsBegin, $dsEnd); $this->mRepoMod->shouldReceive('getSalesOrdersForPeriod')->with($DS_BEGIN, $DS_END)->andReturn($ORDERS); // $updates = $this->_subBonus->calc($treeCompressed, $pvTotal, $params); $this->mSubBonus->shouldReceive('calc')->andReturn($UPDATES); // $respAdd = $this->_createBonusOperation($updates); // $result = $this->_callWalletOperation->addToWalletActive($req); $mRespAddWallet = new \Praxigento\Wallet\Service\Operation\Response\AddToWalletActive(); $this->mCallWalletOperation->shouldReceive('addToWalletActive')->andReturn($mRespAddWallet); // $transLog = $respAdd->getTransactionsIds(); $mRespAddWallet->setTransactionsIds($TRANS_LOG); // $this->_repoMod->saveLogSaleOrders($transLog); $this->mRepoMod->shouldReceive('saveLogSaleOrders')->once(); // $this->_repoMod->updateCalcSetComplete($calcIdDepend); $this->mRepoMod->shouldReceive('updateCalcSetComplete')->once(); // $conn->commit(); $this->mConn->shouldReceive('commit')->once(); /** === Call and asserts === */ $req = new Request\Bonus(); $req->setDatePerformed($DATE_PERFORMED); $req->setDateApplied($DATE_APPLIED); $resp = $this->call->bonus($req); $this->assertTrue($resp->isSucceed()); }
/** * @param Request\Bonus $req * * @return Response\Bonus */ public function bonus(Request\Bonus $req) { $result = new Response\Bonus(); $datePerformed = $req->getDatePerformed(); $dateApplied = $req->getDateApplied(); $this->_logger->info("'Global Sales Bonus' calculation is started. Performed at: {$datePerformed}, applied at: {$dateApplied}."); $reqGetPeriod = new PeriodGetForDependentCalcRequest(); $calcTypeBase = Cfg::CODE_TYPE_CALC_QUALIFICATION; $calcType = Cfg::CODE_TYPE_CALC_BONUS; $reqGetPeriod->setBaseCalcTypeCode($calcTypeBase); $reqGetPeriod->setDependentCalcTypeCode($calcType); $respGetPeriod = $this->_callBasePeriod->getForDependentCalc($reqGetPeriod); if ($respGetPeriod->isSucceed()) { $def = $this->_manTrans->begin(); try { $periodDataDepend = $respGetPeriod->getDependentPeriodData(); $calcDataDepend = $respGetPeriod->getDependentCalcData(); $calcIdDepend = $calcDataDepend->getId(); $dsBegin = $periodDataDepend->getDstampBegin(); $dsEnd = $periodDataDepend->getDstampEnd(); /* collect data to process bonus */ $calcTypeIdCompress = $this->_repoBonusTypeCalc->getIdByCode(Cfg::CODE_TYPE_CALC_COMPRESSION); $calcDataCompress = $this->_repoBonusService->getLastCalcForPeriodByDates($calcTypeIdCompress, $dsBegin, $dsEnd); $calcIdCompress = $calcDataCompress->getId(); $params = $this->_repoMod->getConfigParams(); $treeCompressed = $this->_repoMod->getCompressedTreeWithQualifications($calcIdCompress); $pvTotal = $this->_repoMod->getSalesOrdersPvForPeriod($dsBegin, $dsEnd); /* calculate bonus */ $updates = $this->_subBonus->calc($treeCompressed, $pvTotal, $params); /* create new operation with bonus transactions and save sales log */ $respAdd = $this->_createBonusOperation($updates); $transLog = $respAdd->getTransactionsIds(); $this->_repoMod->saveLogRanks($transLog); /* mark calculation as completed and finalize bonus */ $this->_repoBonusService->markCalcComplete($calcIdDepend); $this->_manTrans->commit($def); $result->setPeriodId($periodDataDepend->getId()); $result->setCalcId($calcIdDepend); $result->markSucceed(); } finally { $this->_manTrans->end($def); } } $this->_logger->info("'Global Sales Bonus' calculation is complete."); return $result; }