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_bonusPersonalDef()
 {
     /** === Test Data === */
     $COMPRESSED_PTC = [[PtcCompress::ATTR_CUSTOMER_ID => 2, PtcCompress::ATTR_PV => 100, Customer::ATTR_COUNTRY_CODE => 'LV'], [PtcCompress::ATTR_CUSTOMER_ID => 3, PtcCompress::ATTR_PV => 20, 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->any())->method('getSchemeByCustomer')->willReturn(Def::SCHEMA_DEFAULT);
     /**
      * Prepare request and perform call.
      */
     /** @var  $sub Calc */
     $sub = new Calc($mLogger, $mToolbox, $mCallDownlineSnap);
     $res = $sub->bonusPersonalDef($COMPRESSED_PTC, $this->LEVELS_PERS);
     $this->assertTrue(is_array($res));
 }