public function compressOi(Request\CompressOi $request)
 {
     $result = new Response\CompressOi();
     $scheme = $this->_getCalculationsScheme($request->getScheme());
     $this->_logger->info("'OI Compression' calculation is started ({$scheme} scheme).");
     if ($scheme == Def::SCHEMA_EU) {
         $calcType = Cfg::CODE_TYPE_CALC_COMPRESS_FOR_OI_EU;
     } else {
         $calcType = Cfg::CODE_TYPE_CALC_COMPRESS_FOR_OI_DEF;
     }
     $reqGetPeriod = new PeriodGetForDependentCalcRequest();
     $reqGetPeriod->setBaseCalcTypeCode(Cfg::CODE_TYPE_CALC_VALUE_OV);
     $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);
             /* ranks configuration (ranks, schemes, qualification levels, etc.)*/
             $cfgParams = $this->_subDb->getCfgParams();
             /* calculate updates */
             $updates = $this->_subCalc->compressOi($compressPtc, $cfgParams, $scheme);
             /* save updates and mark calculation complete */
             $this->_subDb->saveCompressedOi($updates, $thisCalcId);
             $this->_subDb->markCalcComplete($thisCalcId);
             $this->_manTrans->commit($def);
             $result->markSucceed();
             $result->setPeriodId($thisPeriodId);
             $result->setCalcId($thisCalcId);
         } finally {
             $this->_manTrans->end($def);
         }
     }
     $this->_logMemoryUsage();
     $this->_logger->info("'OI Compression' calculation is completed.");
     return $result;
 }
 public function test_compressOi()
 {
     /** === Test Data === */
     $COMPRESSED_PTC = [[PtcCompress::ATTR_CUSTOMER_ID => 1, PtcCompress::ATTR_PARENT_ID => 1, PtcCompress::ATTR_PATH => '/', PtcCompress::ATTR_DEPTH => 0, PtcCompress::ATTR_PV => 100, PtcCompress::ATTR_TV => 0, PtcCompress::ATTR_OV => 100000, Customer::ATTR_COUNTRY_CODE => 'LV'], [PtcCompress::ATTR_CUSTOMER_ID => 2, PtcCompress::ATTR_PARENT_ID => 1, PtcCompress::ATTR_PATH => '/1/', PtcCompress::ATTR_DEPTH => 1, PtcCompress::ATTR_PV => 100, PtcCompress::ATTR_TV => 100, PtcCompress::ATTR_OV => 100000, Customer::ATTR_COUNTRY_CODE => 'LV'], [PtcCompress::ATTR_CUSTOMER_ID => 3, PtcCompress::ATTR_PARENT_ID => 1, PtcCompress::ATTR_PATH => '/1/', PtcCompress::ATTR_DEPTH => 1, PtcCompress::ATTR_PV => 0, PtcCompress::ATTR_TV => 0, PtcCompress::ATTR_OV => 100000, Customer::ATTR_COUNTRY_CODE => 'LV'], [PtcCompress::ATTR_CUSTOMER_ID => 21, PtcCompress::ATTR_PARENT_ID => 2, PtcCompress::ATTR_PATH => '/1/2/', PtcCompress::ATTR_DEPTH => 2, PtcCompress::ATTR_PV => 100, PtcCompress::ATTR_TV => 200, PtcCompress::ATTR_OV => 100000, Customer::ATTR_COUNTRY_CODE => 'LV'], [PtcCompress::ATTR_CUSTOMER_ID => 22, PtcCompress::ATTR_PARENT_ID => 2, PtcCompress::ATTR_PATH => '/1/2/', PtcCompress::ATTR_DEPTH => 2, PtcCompress::ATTR_PV => 100, PtcCompress::ATTR_TV => 300, PtcCompress::ATTR_OV => 100000, Customer::ATTR_COUNTRY_CODE => 'LV'], [PtcCompress::ATTR_CUSTOMER_ID => 23, PtcCompress::ATTR_PARENT_ID => 2, PtcCompress::ATTR_PATH => '/1/2/', PtcCompress::ATTR_DEPTH => 2, PtcCompress::ATTR_PV => 100, PtcCompress::ATTR_TV => 300, PtcCompress::ATTR_OV => 100000, Customer::ATTR_COUNTRY_CODE => 'LV'], [PtcCompress::ATTR_CUSTOMER_ID => 21, PtcCompress::ATTR_PARENT_ID => 2, PtcCompress::ATTR_PATH => '/1/2/', PtcCompress::ATTR_DEPTH => 2, PtcCompress::ATTR_PV => 100, PtcCompress::ATTR_TV => 300, PtcCompress::ATTR_OV => 100000, Customer::ATTR_COUNTRY_CODE => 'LV'], [PtcCompress::ATTR_CUSTOMER_ID => 31, PtcCompress::ATTR_PARENT_ID => 3, PtcCompress::ATTR_PATH => '/1/3/', PtcCompress::ATTR_DEPTH => 2, PtcCompress::ATTR_PV => 100, PtcCompress::ATTR_TV => 100, PtcCompress::ATTR_OV => 100000, Customer::ATTR_COUNTRY_CODE => 'LV'], [PtcCompress::ATTR_CUSTOMER_ID => 4, PtcCompress::ATTR_PARENT_ID => 4, PtcCompress::ATTR_PATH => '/', PtcCompress::ATTR_DEPTH => 0, PtcCompress::ATTR_PV => 0, PtcCompress::ATTR_TV => 0, PtcCompress::ATTR_OV => 100000, Customer::ATTR_COUNTRY_CODE => 'LV'], [PtcCompress::ATTR_CUSTOMER_ID => 41, PtcCompress::ATTR_PARENT_ID => 4, PtcCompress::ATTR_PATH => '/4/', PtcCompress::ATTR_DEPTH => 1, PtcCompress::ATTR_PV => 1000, PtcCompress::ATTR_TV => 1000, PtcCompress::ATTR_OV => 100000, Customer::ATTR_COUNTRY_CODE => 'LV']];
     /** === Mocks === */
     $mLogger = $this->_mockLogger();
     $mToolFormat = new \Praxigento\Core\Tool\IFormat();
     $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');
     // $rankId = $this->_toolScheme->getForcedQualificationRank($custId, $scheme);
     $mToolScheme->expects($this->any())->method('getForcedQualificationRank')->willReturnCallback(function () {
         $args = func_get_args();
         $result = null;
         if ($args[0] == 1) {
             $result = 1;
         }
         return $result;
     });
     // $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->compressOi($COMPRESSED_PTC, $this->CFG_PARAMS, Def::SCHEMA_DEFAULT);
     $this->assertTrue(is_array($res));
 }