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_bonusTeamDef()
 {
     /** === Test Data === */
     $COMPRESSED = [[PtcCompress::ATTR_CUSTOMER_ID => 1, Customer::ATTR_HUMAN_REF => 'ref_1', Customer::ATTR_COUNTRY_CODE => 'LV', PtcCompress::ATTR_PARENT_ID => 1, PtcCompress::ATTR_PATH => '/', PtcCompress::ATTR_PV => 0, PtcCompress::ATTR_TV => 0], [PtcCompress::ATTR_CUSTOMER_ID => 22, Customer::ATTR_HUMAN_REF => 'ref_22', Customer::ATTR_COUNTRY_CODE => 'LV', PtcCompress::ATTR_PARENT_ID => 1, PtcCompress::ATTR_PATH => '/1/', PtcCompress::ATTR_PV => 75, PtcCompress::ATTR_TV => 800], [PtcCompress::ATTR_CUSTOMER_ID => 222, Customer::ATTR_HUMAN_REF => 'ref_222', Customer::ATTR_COUNTRY_CODE => 'LV', PtcCompress::ATTR_PARENT_ID => 22, PtcCompress::ATTR_PATH => '/1/22/', PtcCompress::ATTR_PV => 75, PtcCompress::ATTR_TV => 0], [PtcCompress::ATTR_CUSTOMER_ID => 21, Customer::ATTR_HUMAN_REF => 'ref_21', Customer::ATTR_COUNTRY_CODE => 'DE', PtcCompress::ATTR_PARENT_ID => 1, PtcCompress::ATTR_PATH => '/1/', PtcCompress::ATTR_PV => 0, PtcCompress::ATTR_TV => 800], [PtcCompress::ATTR_CUSTOMER_ID => 2, Customer::ATTR_HUMAN_REF => 'ref_2', Customer::ATTR_COUNTRY_CODE => 'DE', PtcCompress::ATTR_PARENT_ID => 21, PtcCompress::ATTR_PATH => '/1/21/', PtcCompress::ATTR_PV => 60, PtcCompress::ATTR_TV => 800], [PtcCompress::ATTR_CUSTOMER_ID => 3, Customer::ATTR_HUMAN_REF => 'ref_3', Customer::ATTR_COUNTRY_CODE => 'DE', PtcCompress::ATTR_PARENT_ID => 1, PtcCompress::ATTR_PATH => '/1/21/2/', PtcCompress::ATTR_PV => 120, PtcCompress::ATTR_TV => 0], [PtcCompress::ATTR_CUSTOMER_ID => 4, Customer::ATTR_HUMAN_REF => 'ref_4', Customer::ATTR_COUNTRY_CODE => 'LV', PtcCompress::ATTR_PARENT_ID => 1, PtcCompress::ATTR_PATH => '/1/21/2/3/', PtcCompress::ATTR_PV => 50, PtcCompress::ATTR_TV => 0], [PtcCompress::ATTR_CUSTOMER_ID => 5, Customer::ATTR_HUMAN_REF => 'ref_5', Customer::ATTR_COUNTRY_CODE => 'LV', PtcCompress::ATTR_PARENT_ID => 1, PtcCompress::ATTR_PATH => '/1/21/2/3/', PtcCompress::ATTR_PV => 1000, PtcCompress::ATTR_TV => 0], [PtcCompress::ATTR_CUSTOMER_ID => 6, Customer::ATTR_HUMAN_REF => 'ref_6', Customer::ATTR_COUNTRY_CODE => 'LV', PtcCompress::ATTR_PARENT_ID => 1, PtcCompress::ATTR_PATH => '/1/', PtcCompress::ATTR_PV => 0, PtcCompress::ATTR_TV => 0]];
     /** === Mocks === */
     $mLogger = $this->_mockLogger();
     $mToolFormat = new ToolFormat();
     $mToolScheme = $this->_mockFor('\\Praxigento\\Bonus\\Hybrid\\Lib\\Tool\\IScheme');
     $mToolDownlineTree = new ToolDownlineTree();
     $mToolbox = $this->_mockToolbox(null, null, $mToolFormat, null, $mToolScheme, $mToolDownlineTree);
     $mCallDownlineSnap = $this->_mockFor('\\Praxigento\\Downline\\Service\\ISnap');
     // $custScheme = $this->_toolScheme->getSchemeByCustomer($item);
     $mToolScheme->expects($this->any())->method('getSchemeByCustomer')->willReturnCallback(function () {
         $args = func_get_args();
         $item = $args[0];
         $result = Def::SCHEMA_DEFAULT;
         if (isset($item[Customer::ATTR_COUNTRY_CODE]) && $item[Customer::ATTR_COUNTRY_CODE] != 'LV') {
             $result = Def::SCHEMA_EU;
         }
         return $result;
     });
     // $pvForced = $this->_toolScheme->getForcedPv($custId, $pv);
     $mToolScheme->expects($this->any())->method('getForcedPv')->willReturnCallback(function () {
         $args = func_get_args();
         $result = $args[0] == 1 ? 200 : $args[1];
         return $result;
     });
     // $tvForced = $this->_toolScheme->getForcedTv($parentId, $scheme, $tv);
     $mToolScheme->expects($this->any())->method('getForcedTv')->willReturnCallback(function () {
         $args = func_get_args();
         $result = $args[0] == 3 ? 1000 : $args[2];
         return $result;
     });
     /**
      * Prepare request and perform call.
      */
     /** @var  $sub Calc */
     $sub = new Calc($mLogger, $mToolbox, $mCallDownlineSnap);
     $res = $sub->bonusTeamDef($COMPRESSED, $this->LEVELS_PERS, $this->LEVELS_TEAM, $this->COURTESY_PERCENT);
     $this->assertTrue(is_array($res));
 }