public function bonusPersonal(Request\BonusPersonal $request) { $result = new Response\BonusPersonal(); $scheme = $this->_getCalculationsScheme($request->getScheme()); $datePerformed = $request->getDatePerformed(); $dateApplied = $request->getDateApplied(); $this->_logger->info("'Personal Bonus' calculation is started. Scheme: {$scheme}, performed at: {$datePerformed}, applied at: {$dateApplied}."); $reqGetPeriod = new PeriodGetForDependentCalcRequest(); $calcTypeBase = Cfg::CODE_TYPE_CALC_COMPRESS_FOR_PTC; if ($scheme == Def::SCHEMA_EU) { $calcType = Cfg::CODE_TYPE_CALC_BONUS_PERSONAL_EU; } else { $calcType = Cfg::CODE_TYPE_CALC_BONUS_PERSONAL_DEF; } $reqGetPeriod->setBaseCalcTypeCode($calcTypeBase); $reqGetPeriod->setDependentCalcTypeCode($calcType); $respGetPeriod = $this->_callPeriod->getForDependentCalc($reqGetPeriod); if ($respGetPeriod->isSucceed()) { $def = $this->_manTrans->begin(); try { /* working vars */ $thisPeriodData = $respGetPeriod->getDependentPeriodData(); $thisPeriodId = $thisPeriodData[Period::ATTR_ID]; $thisCalcData = $respGetPeriod->getDependentCalcData(); $thisCalcId = $thisCalcData[Calculation::ATTR_ID]; $basePeriodData = $respGetPeriod->getBasePeriodData(); $baseDsBegin = $basePeriodData->getDstampBegin(); $baseDsEnd = $basePeriodData->getDstampEnd(); $baseCalcData = $respGetPeriod->getBaseCalcData(); $baseCalcId = $baseCalcData->getId(); /* calculation itself */ $this->_logger->info("Processing period #{$thisPeriodId} ({$baseDsBegin}-{$baseDsEnd})"); /* get compressed data by calculation ID */ $compressPtc = $this->_subDb->getCompressedPtcData($baseCalcId); /* calculates bonus according to the calculation scheme */ if ($scheme == Def::SCHEMA_EU) { /* use EU scheme */ $treeFlat = $this->_subDb->getDownlineSnapshot($baseDsEnd); $orders = $this->_subDb->getSaleOrdersForRebate($baseDsBegin, $baseDsEnd); $updates = $this->_subCalc->bonusPersonalEu($treeFlat, $compressPtc, $orders); /* convert */ $respAdd = $this->_subDb->saveOperationWalletActive($updates, Cfg::CODE_TYPE_OPER_BONUS_REBATE, $datePerformed, $dateApplied); $operId = $respAdd->getOperationId(); $transIds = $respAdd->getTransactionsIds(); /* save orders and correspondent transactions into the log */ $this->_subDb->saveLogSaleOrders($updates, $transIds); } else { /* use DEFAULT scheme */ /* get levels to calculate Personal bonus */ $levelsPersonal = $this->_subDb->getBonusLevels($calcType); $updates = $this->_subCalc->bonusPersonalDef($compressPtc, $levelsPersonal); /* save bonus operation with transactions */ $respAdd = $this->_subDb->saveOperationWalletActive($updates, Cfg::CODE_TYPE_OPER_BONUS_PERSONAL, $datePerformed, $dateApplied); $operId = $respAdd->getOperationId(); } /* mark calculation as complete */ $this->_subDb->saveLogOperations($operId, $thisCalcId); $this->_subDb->markCalcComplete($thisCalcId); /* finalize response as succeed */ $this->_manTrans->commit($def); $result->markSucceed(); $result->setPeriodId($thisPeriodId); $result->setCalcId($thisCalcId); } finally { // transaction will be rolled back if commit is not done (otherwise - do nothing) $this->_manTrans->end($def); } } $this->_logMemoryUsage(); $this->_logger->info("'Personal Bonus' calculation is completed."); return $result; }
public function test_bonusPersonalEu() { /** === Test Data === */ $TREE = [[Snap::ATTR_CUSTOMER_ID => 3, Snap::ATTR_PARENT_ID => 2, Snap::ATTR_PATH => '/2/']]; $COMPRESSED_PTC = [[PtcCompress::ATTR_CUSTOMER_ID => 2, PtcCompress::ATTR_PV => 100, Customer::ATTR_COUNTRY_CODE => 'LV']]; $ORDERS = [2 => [31 => 100], 3 => [32 => 200]]; /** === Mocks === */ $mLogger = $this->_mockLogger(); $mToolFormat = new \Praxigento\Core\Tool\IFormat(); $mToolScheme = $this->_mockFor('\\Praxigento\\Bonus\\Hybrid\\Lib\\Tool\\IScheme'); $mToolDownlineTree = new \Praxigento\Downline\Tool\Def\Tree(); $mToolbox = $this->_mockToolbox(null, null, $mToolFormat, null, $mToolScheme, $mToolDownlineTree); $mCallDownlineSnap = $this->_mockFor('\\Praxigento\\Downline\\Service\\ISnap'); // $scheme = $this->_toolScheme->getSchemeByCustomer($one); $mToolScheme->expects($this->any())->method('getSchemeByCustomer')->willReturn(Def::SCHEMA_EU); /** * Prepare request and perform call. */ /** @var $sub Calc */ $sub = new Calc($mLogger, $mToolbox, $mCallDownlineSnap); $res = $sub->bonusPersonalEu($TREE, $COMPRESSED_PTC, $ORDERS); $this->assertTrue(is_array($res)); }