public function bonusTeam(Request\BonusTeam $request) { $result = new Response\BonusTeam(); $scheme = $this->_getCalculationsScheme($request->getScheme()); $courtesyPercent = $request->getCourtesyBonusPercent(); $teamBonusPercent = $request->getTeamBonusPercent(); $datePerformed = $request->getDatePerformed(); $dateApplied = $request->getDateApplied(); $this->_logger->info("'Team Bonus' calculation is started."); $reqGetPeriod = new PeriodGetForDependentCalcRequest(); $calcTypeBase = Cfg::CODE_TYPE_CALC_VALUE_TV; if ($scheme == Def::SCHEMA_EU) { $calcType = Cfg::CODE_TYPE_CALC_BONUS_TEAM_EU; } else { $calcType = Cfg::CODE_TYPE_CALC_BONUS_TEAM_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(); /* get the last PTC compression calc id for this period */ $ptcCompressCalcId = $this->_subDb->getLastCalculationIdForPeriod(Cfg::CODE_TYPE_CALC_COMPRESS_FOR_PTC, $baseDsBegin, $baseDsEnd); /* calculation itself */ $this->_logger->info("Processing period #{$thisPeriodId} ({$baseDsBegin}-{$baseDsEnd})"); /* get compressed data by calculation ID */ $compressPtc = $this->_subDb->getCompressedPtcData($ptcCompressCalcId); /* calculate bonus values according to DEFAULT or EU schemes */ if ($scheme == Def::SCHEMA_EU) { $updates = $this->_subCalc->bonusTeamEu($compressPtc, $teamBonusPercent); /* save operation with transactions */ $respAdd = $this->_subDb->saveOperationWalletActive($updates, Cfg::CODE_TYPE_OPER_BONUS_TEAM, $datePerformed, $dateApplied); $operId = $respAdd->getOperationId(); $transIds = $respAdd->getTransactionsIds(); /* save customers and correspondent transactions into the log */ $this->_subDb->saveLogCustomers($updates, $transIds); } else { /* get levels to calculate Personal and Team bonuses */ $levelsPersonal = $this->_subDb->getBonusLevels(Cfg::CODE_TYPE_CALC_BONUS_PERSONAL_DEF); $levelsTeam = $this->_subDb->getBonusLevels(Cfg::CODE_TYPE_CALC_BONUS_TEAM_DEF); $updates = $this->_subCalc->bonusTeamDef($compressPtc, $levelsPersonal, $levelsTeam, $courtesyPercent); /* save operation with transactions */ $respAdd = $this->_subDb->saveOperationWalletActive($updates, Cfg::CODE_TYPE_OPER_BONUS_TEAM, $datePerformed, $dateApplied); $operId = $respAdd->getOperationId(); } /* 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("'Team Bonus' calculation is completed."); return $result; }
public function test_bonusTeamEu() { /** === Test Data === */ $COMPRESSED_PTC = [[PtcCompress::ATTR_CUSTOMER_ID => 2, PtcCompress::ATTR_PARENT_ID => 2, PtcCompress::ATTR_PV => 100, Customer::ATTR_HUMAN_REF => 'ref_2', Customer::ATTR_COUNTRY_CODE => 'LV'], [PtcCompress::ATTR_CUSTOMER_ID => 3, PtcCompress::ATTR_PARENT_ID => 3, PtcCompress::ATTR_PV => 100, Customer::ATTR_HUMAN_REF => 'ref_3', Customer::ATTR_COUNTRY_CODE => 'DE'], [PtcCompress::ATTR_CUSTOMER_ID => 4, PtcCompress::ATTR_PARENT_ID => 4, PtcCompress::ATTR_PV => 0, Customer::ATTR_HUMAN_REF => 'ref_4', Customer::ATTR_COUNTRY_CODE => 'LV']]; /** === Mocks === */ $mLogger = $this->_mockLogger(); $mToolFormat = new \Praxigento\Core\Tool\IFormat(); $mToolScheme = $this->_mockFor('\\Praxigento\\Bonus\\Hybrid\\Lib\\Tool\\IScheme'); $mToolbox = $this->_mockToolbox(null, null, $mToolFormat, null, $mToolScheme); $mCallDownlineSnap = $this->_mockFor('\\Praxigento\\Downline\\Service\\ISnap'); // $scheme = $this->_toolScheme->getSchemeByCustomer($one); $mToolScheme->expects($this->at(0))->method('getSchemeByCustomer')->willReturn(Def::SCHEMA_EU); $mToolScheme->expects($this->at(1))->method('getSchemeByCustomer')->willReturn(Def::SCHEMA_DEFAULT); $mToolScheme->expects($this->at(2))->method('getSchemeByCustomer')->willReturn(Def::SCHEMA_EU); /** * Prepare request and perform call. */ /** @var $sub Calc */ $sub = new Calc($mLogger, $mToolbox, $mCallDownlineSnap); $res = $sub->bonusTeamEu($COMPRESSED_PTC, $this->COURTESY_PERCENT); $this->assertTrue(is_array($res)); }