public function test_calc()
 {
     /** === Test Data === */
     $RANK_1 = 1;
     $RANK_2 = 2;
     $CUST_1 = 10;
     $CUST_2 = 20;
     $TREE = [[Compress::ATTR_CUSTOMER_ID => $CUST_1, Qualification::ATTR_GV => 100, Qualification::ATTR_RANK_ID => $RANK_1], [Compress::ATTR_CUSTOMER_ID => $CUST_2, Qualification::ATTR_GV => 200, Qualification::ATTR_RANK_ID => $RANK_2]];
     $PV_TOTAL = 2100;
     $PARAMS = [$RANK_1 => [Param::ATTR_RANK_ID => $RANK_1, Param::ATTR_GV => 100, Param::ATTR_PERCENT => 0.01, Param::ATTR_IS_PRORATED => true], $RANK_2 => [Param::ATTR_RANK_ID => $RANK_2, Param::ATTR_GV => 200, Param::ATTR_PERCENT => 0.02, Param::ATTR_IS_PRORATED => false]];
     /** === Setup Mocks === */
     // $mapAmounts = $this->_mapPayoutsByRank($pvTotal, $params);
     // $bonus = $this->_toolFormat->roundBonus($pvTotal * $percent);
     $this->mToolFormat->shouldReceive('roundBonus')->times(4)->andReturn($PV_TOTAL * 0.01);
     $this->mToolFormat->shouldReceive('roundBonus')->once()->andReturn($PV_TOTAL * 0.02);
     /** === Call and asserts  === */
     $this->sub->calc($TREE, $PV_TOTAL, $PARAMS);
 }
 /**
  * @param Request\Bonus $req
  *
  * @return Response\Bonus
  */
 public function bonus(Request\Bonus $req)
 {
     $result = new Response\Bonus();
     $datePerformed = $req->getDatePerformed();
     $dateApplied = $req->getDateApplied();
     $this->_logger->info("'Global Sales Bonus' calculation is started. Performed at: {$datePerformed}, applied at: {$dateApplied}.");
     $reqGetPeriod = new PeriodGetForDependentCalcRequest();
     $calcTypeBase = Cfg::CODE_TYPE_CALC_QUALIFICATION;
     $calcType = Cfg::CODE_TYPE_CALC_BONUS;
     $reqGetPeriod->setBaseCalcTypeCode($calcTypeBase);
     $reqGetPeriod->setDependentCalcTypeCode($calcType);
     $respGetPeriod = $this->_callBasePeriod->getForDependentCalc($reqGetPeriod);
     if ($respGetPeriod->isSucceed()) {
         $def = $this->_manTrans->begin();
         try {
             $periodDataDepend = $respGetPeriod->getDependentPeriodData();
             $calcDataDepend = $respGetPeriod->getDependentCalcData();
             $calcIdDepend = $calcDataDepend->getId();
             $dsBegin = $periodDataDepend->getDstampBegin();
             $dsEnd = $periodDataDepend->getDstampEnd();
             /* collect data to process bonus */
             $calcTypeIdCompress = $this->_repoBonusTypeCalc->getIdByCode(Cfg::CODE_TYPE_CALC_COMPRESSION);
             $calcDataCompress = $this->_repoBonusService->getLastCalcForPeriodByDates($calcTypeIdCompress, $dsBegin, $dsEnd);
             $calcIdCompress = $calcDataCompress->getId();
             $params = $this->_repoMod->getConfigParams();
             $treeCompressed = $this->_repoMod->getCompressedTreeWithQualifications($calcIdCompress);
             $pvTotal = $this->_repoMod->getSalesOrdersPvForPeriod($dsBegin, $dsEnd);
             /* calculate bonus */
             $updates = $this->_subBonus->calc($treeCompressed, $pvTotal, $params);
             /* create new operation with bonus transactions and save sales log */
             $respAdd = $this->_createBonusOperation($updates);
             $transLog = $respAdd->getTransactionsIds();
             $this->_repoMod->saveLogRanks($transLog);
             /* mark calculation as completed and finalize bonus */
             $this->_repoBonusService->markCalcComplete($calcIdDepend);
             $this->_manTrans->commit($def);
             $result->setPeriodId($periodDataDepend->getId());
             $result->setCalcId($calcIdDepend);
             $result->markSucceed();
         } finally {
             $this->_manTrans->end($def);
         }
     }
     $this->_logger->info("'Global Sales Bonus' calculation is complete.");
     return $result;
 }