public function bonusInfinity(Request\BonusInfinity $request) { $result = new Response\BonusInfinity(); $scheme = $this->_getCalculationsScheme($request->getScheme()); $datePerformed = $request->getDatePerformed(); $dateApplied = $request->getDateApplied(); $this->_logger->info("'Infinity Bonus' calculation is started ({$scheme} scheme)."); if ($scheme == Def::SCHEMA_EU) { $calcTypeBase = Cfg::CODE_TYPE_CALC_COMPRESS_FOR_OI_EU; $calcType = Cfg::CODE_TYPE_CALC_BONUS_INFINITY_EU; } else { $calcTypeBase = Cfg::CODE_TYPE_CALC_COMPRESS_FOR_OI_DEF; $calcType = Cfg::CODE_TYPE_CALC_BONUS_INFINITY_DEF; } $reqGetPeriod = new PeriodGetForDependentCalcRequest(); $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 */ $compressOi = $this->_subDb->getCompressedOiData($baseCalcId); /* get configuration for Override and Infinity bonuses */ $cfgParams = $this->_subDb->getCfgParams(); /* calculate bonus amounts */ $updates = $this->_subCalc->bonusInfinity($compressOi, $scheme, $cfgParams); /* prepare update data */ $updatesForWallets = []; $updatesForPvInf = []; foreach ($updates as $custId => $item) { $pv = $item[Calc::A_PV]; $bonusEntries = $item[Calc::A_ENTRIES]; $updatesForPvInf[$custId] = [OiCompress::ATTR_PV_INF => $pv]; foreach ($bonusEntries as $entry) { $bonus = $entry[Calc::A_VALUE]; $childId = $entry[Calc::A_OTHER_ID]; if ($bonus > Cfg::DEF_ZERO) { $updatesForWallets[] = [Calc::A_CUST_ID => $custId, Calc::A_VALUE => $bonus, Calc::A_OTHER_ID => $childId]; } } } unset($updates); /* update wallets */ $respAdd = $this->_subDb->saveOperationWalletActive($updatesForWallets, Cfg::CODE_TYPE_OPER_BONUS_INFINITY, $datePerformed, $dateApplied); $operId = $respAdd->getOperationId(); $transIds = $respAdd->getTransactionsIds(); /* save orders and correspondent transactions into the log */ $this->_subDb->saveLogCustomers($updatesForWallets, $transIds); unset($updatesForWallets); /* update OI Compressed data */ $this->_subDb->saveUpdatesOiCompress($updatesForPvInf, $baseCalcId); /* save processed operation to calculations log and 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 { $this->_manTrans->end($def); } } $this->_logMemoryUsage(); $this->_logger->info("'Infinity Bonus' calculation is completed."); return $result; }
public function test_bonusInfinity() { /** === Test Data === */ $COMPRESSED_OI = [[OiCompress::ATTR_CUSTOMER_ID => 1, OiCompress::ATTR_PARENT_ID => 1, OiCompress::ATTR_RANK_ID => 1, OiCompress::ATTR_PV => 100, Customer::ATTR_COUNTRY_CODE => 'LV'], [OiCompress::ATTR_CUSTOMER_ID => 2, OiCompress::ATTR_PARENT_ID => 1, OiCompress::ATTR_RANK_ID => 2, OiCompress::ATTR_PV => 100, Customer::ATTR_COUNTRY_CODE => 'LV'], [OiCompress::ATTR_CUSTOMER_ID => 3, OiCompress::ATTR_PARENT_ID => 2, OiCompress::ATTR_RANK_ID => 1, OiCompress::ATTR_PV => 100, Customer::ATTR_COUNTRY_CODE => 'LV'], [OiCompress::ATTR_CUSTOMER_ID => 4, OiCompress::ATTR_PARENT_ID => 3, OiCompress::ATTR_RANK_ID => 2, OiCompress::ATTR_PV => 100, Customer::ATTR_COUNTRY_CODE => 'LV'], [OiCompress::ATTR_CUSTOMER_ID => 5, OiCompress::ATTR_PARENT_ID => 4, OiCompress::ATTR_RANK_ID => 2, OiCompress::ATTR_PV => 100, Customer::ATTR_COUNTRY_CODE => 'LV']]; $MAP_TREE_EXP = [1 => [OiCompress::ATTR_CUSTOMER_ID => 1, OiCompress::ATTR_RANK_ID => 1, Snap::ATTR_PATH => '/'], 2 => [OiCompress::ATTR_CUSTOMER_ID => 2, OiCompress::ATTR_RANK_ID => 2, Snap::ATTR_PATH => '/1/'], 3 => [OiCompress::ATTR_CUSTOMER_ID => 3, OiCompress::ATTR_RANK_ID => 1, Snap::ATTR_PATH => '/1/2/'], 4 => [OiCompress::ATTR_CUSTOMER_ID => 4, OiCompress::ATTR_RANK_ID => 2, Snap::ATTR_PATH => '/1/2/3/'], 5 => [OiCompress::ATTR_CUSTOMER_ID => 5, OiCompress::ATTR_RANK_ID => 2, Snap::ATTR_PATH => '/1/2/3/4/']]; /** === 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'); // $mapTreeExp = $this->_getExpandedTreeSnap( // $respExt = $this->_callDownlineSnap->expandMinimal($reqExt); $mRespExt = new DataObject(); $mCallDownlineSnap->expects($this->once())->method('expandMinimal')->willReturn($mRespExt); // $result = $respExt->getSnapData(); $mRespExt->setSnapData($MAP_TREE_EXP); // $scheme = $this->_toolScheme->getSchemeByCustomer($one); $mToolScheme->expects($this->any())->method('getSchemeByCustomer')->willReturn(Def::SCHEMA_DEFAULT); /** * Prepare request and perform call. */ /** @var $sub Calc */ $sub = new Calc($mLogger, $mToolbox, $mCallDownlineSnap); $res = $sub->bonusInfinity($COMPRESSED_OI, Def::SCHEMA_DEFAULT, $this->CFG_PARAMS); $this->assertTrue(is_array($res)); }