/**
  * @param Request\CompressPtc $request
  *
  * @return Response\CompressPtc
  */
 public function compressPtc(Request\CompressPtc $request)
 {
     $result = new Response\CompressPtc();
     $this->_logger->info("'PTC Compression' calculation is started.");
     $reqGetPeriod = new PeriodGetForDependentCalcRequest();
     $reqGetPeriod->setBaseCalcTypeCode(Cfg::CODE_TYPE_CALC_PV_WRITE_OFF);
     $reqGetPeriod->setDependentCalcTypeCode(Cfg::CODE_TYPE_CALC_COMPRESS_FOR_PTC);
     $respGetPeriod = $this->_callPeriod->getForDependentCalc($reqGetPeriod);
     if ($respGetPeriod->isSucceed()) {
         $def = $this->_manTrans->begin();
         try {
             /* working vars */
             $thisPeriodData = $respGetPeriod->getDependentPeriodData();
             $thisPeriodId = $thisPeriodData[Period::ATTR_ID];
             $thisDsBegin = $thisPeriodData[Period::ATTR_DSTAMP_BEGIN];
             $thisDsEnd = $thisPeriodData[Period::ATTR_DSTAMP_END];
             $thisCalcData = $respGetPeriod->getDependentCalcData();
             $thisCalcId = $thisCalcData[Calculation::ATTR_ID];
             $baseCalcData = $respGetPeriod->getBaseCalcData();
             $baseCalcIdId = $baseCalcData->getId();
             /* calculation itself */
             $this->_logger->info("Processing period #{$thisPeriodId} ({$thisDsBegin}-{$thisDsEnd})");
             $downlineSnap = $this->_subDb->getDownlineSnapshot($thisDsEnd);
             $customersData = $this->_subDb->getDownlineCustomersData();
             $transData = $this->_subDb->getDataForPvCompression($baseCalcIdId);
             $updates = $this->_subCalc->compressPtc($downlineSnap, $customersData, $transData);
             $this->_subDb->saveCompressedPtc($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("'PTC Compression' calculation is completed.");
     return $result;
 }
 public function test_compressPtc()
 {
     /** === Test Data === */
     $CUSTS = [[Customer::ATTR_CUSTOMER_ID => 1], [Customer::ATTR_CUSTOMER_ID => 2], [Customer::ATTR_CUSTOMER_ID => 3], [Customer::ATTR_CUSTOMER_ID => 4], [Customer::ATTR_CUSTOMER_ID => 5], [Customer::ATTR_CUSTOMER_ID => 6], [Customer::ATTR_CUSTOMER_ID => 7]];
     $TREE = [1 => [Snap::ATTR_CUSTOMER_ID => 1, Snap::ATTR_PARENT_ID => 1, Snap::ATTR_DEPTH => 0, Snap::ATTR_PATH => '/'], 2 => [Snap::ATTR_CUSTOMER_ID => 2, Snap::ATTR_PARENT_ID => 1, Snap::ATTR_DEPTH => 1, Snap::ATTR_PATH => '/1/'], 3 => [Snap::ATTR_CUSTOMER_ID => 3, Snap::ATTR_PARENT_ID => 2, Snap::ATTR_DEPTH => 2, Snap::ATTR_PATH => '/1/2/'], 4 => [Snap::ATTR_CUSTOMER_ID => 4, Snap::ATTR_PARENT_ID => 3, Snap::ATTR_DEPTH => 3, Snap::ATTR_PATH => '/1/2/3/'], 5 => [Snap::ATTR_CUSTOMER_ID => 5, Snap::ATTR_PARENT_ID => 4, Snap::ATTR_DEPTH => 4, Snap::ATTR_PATH => '/1/2/3/4/'], 6 => [Snap::ATTR_CUSTOMER_ID => 6, Snap::ATTR_PARENT_ID => 5, Snap::ATTR_DEPTH => 5, Snap::ATTR_PATH => '/1/2/3/4/5/'], 7 => [Snap::ATTR_CUSTOMER_ID => 7, Snap::ATTR_PARENT_ID => 3, Snap::ATTR_DEPTH => 3, Snap::ATTR_PATH => '/1/2/3/']];
     $TRANS = [[Account::ATTR_CUST_ID => 2, Transaction::ATTR_VALUE => 20], [Account::ATTR_CUST_ID => 3, Transaction::ATTR_VALUE => 20], [Account::ATTR_CUST_ID => 4, Transaction::ATTR_VALUE => 20], [Account::ATTR_CUST_ID => 5, Transaction::ATTR_VALUE => 25], [Account::ATTR_CUST_ID => 6, Transaction::ATTR_VALUE => 250], [Account::ATTR_CUST_ID => 7, Transaction::ATTR_VALUE => 20]];
     $Q_LEVELS = [Def::SCHEMA_DEFAULT => 50, Def::SCHEMA_EU => 100];
     $FORCED_IDS = [1, 2];
     /** === Mocks === */
     $mLogger = $this->_mockLogger();
     $mToolScheme = $this->_mockFor('\\Praxigento\\Bonus\\Hybrid\\Lib\\Tool\\IScheme');
     $mToolDownlineTree = new \Praxigento\Downline\Tool\Def\Tree();
     $mToolbox = $this->_mockToolbox(null, null, null, null, $mToolScheme, $mToolDownlineTree);
     $mCallDownlineSnap = $this->_mockFor('\\Praxigento\\Downline\\Service\\ISnap');
     // $qLevels = $this->_toolScheme->getQualificationLevels();
     $mToolScheme->expects($this->once())->method('getQualificationLevels')->willReturn($Q_LEVELS);
     // $forcedIds = $this->_toolScheme->getForcedQualificationCustomersIds();
     $mToolScheme->expects($this->once())->method('getForcedQualificationCustomersIds')->willReturn($FORCED_IDS);
     // $scheme = $this->_toolScheme->getSchemeByCustomer($custData);
     $mToolScheme->expects($this->any())->method('getSchemeByCustomer')->willReturn(Def::SCHEMA_DEFAULT);
     // private function _composeSnapUpdates($calculatedData)
     // $resp = $this->_callDownlineSnap->expandMinimal($req);
     $mResp = new DownlineSnapExtendMinimalResponse();
     $mResp->setSnapData([]);
     $mCallDownlineSnap->expects($this->once())->method('expandMinimal')->willReturn($mResp);
     /**
      * Prepare request and perform call.
      */
     /** @var  $sub Calc */
     $sub = new Calc($mLogger, $mToolbox, $mCallDownlineSnap);
     $data = $sub->compressPtc($TREE, $CUSTS, $TRANS);
     $this->assertTrue(is_array($data));
 }