/**
  *
  * Get PV related period data if no period yet exist.
  *
  * @param \Praxigento\BonusBase\Service\Period\Response\GetForPvBasedCalc $result
  * @param string $periodType
  * @param int $calcTypeId
  * @return \Praxigento\BonusBase\Service\Period\Response\GetForPvBasedCalc
  */
 public function getNewPeriodDataForPv(\Praxigento\BonusBase\Service\Period\Response\GetForPvBasedCalc $result, $periodType, $calcTypeId)
 {
     /* we should lookup for first PV transaction and calculate first period range */
     $firstDate = $this->_repoService->getFirstDateForPvTransactions();
     if ($firstDate === false) {
         $this->_logger->warning("There is no PV transactions yet. Nothing to do.");
         $result->setErrorCode($result::ERR_HAS_NO_PV_TRANSACTIONS_YET);
     } else {
         $this->_logger->info("First PV transaction was performed at '{$firstDate}'.");
         $periodMonth = $this->_toolPeriod->getPeriodCurrent($firstDate, $periodType);
         $dsBegin = $this->_toolPeriod->getPeriodFirstDate($periodMonth);
         $dsEnd = $this->_toolPeriod->getPeriodLastDate($periodMonth);
         /* create new period for given calculation type */
         $period = new EPeriod();
         $period->setCalcTypeId($calcTypeId);
         $period->setDstampBegin($dsBegin);
         $period->setDstampEnd($dsEnd);
         $periodId = $this->_repoPeriod->create($period);
         $period->setId($periodId);
         /* create related calculation */
         $calc = new ECalculation();
         $calc->setPeriodId($periodId);
         $dateStarted = $this->_toolDate->getUtcNowForDb();
         $calc->setDateStarted($dateStarted);
         $calc->setState(Cfg::CALC_STATE_STARTED);
         $calcId = $this->_repoCalc->create($calc);
         $calc->setId($calcId);
         /* place newly created objects into the response */
         $result->setPeriodData($period);
         $result->setCalcData($calc);
     }
     return $result;
 }
 /**
  * Sub-functionality to get period and calculation data for depended calculation.
  *
  * @param \Praxigento\BonusBase\Service\Period\Response\GetForDependentCalc $result
  * @param int $basePeriodId
  * @param string $baseCalcTypeCode
  * @param string $baseDsBegin
  * @param string $baseDsEnd
  * @param string $dependentCalcTypeCode
  * @param int $dependentCalcTypeId
  * @return \Praxigento\BonusBase\Service\Period\Response\GetForDependentCalc
  */
 public function getDependedCalc(\Praxigento\BonusBase\Service\Period\Response\GetForDependentCalc $result, $basePeriodId, $baseCalcTypeCode, $baseDsBegin, $baseDsEnd, $dependentCalcTypeCode, $dependentCalcTypeId)
 {
     $baseCalcData = $this->_repoService->getLastCalcForPeriodById($basePeriodId);
     $result->setBaseCalcData($baseCalcData);
     /* get depended data for complete base calculation */
     if ($baseCalcData && $baseCalcData->getState() == Cfg::CALC_STATE_COMPLETE) {
         /* there is complete base calculation, get period for depended calc */
         $this->_getForCompleteBase($result, $baseCalcTypeCode, $baseDsBegin, $baseDsEnd, $dependentCalcTypeCode, $dependentCalcTypeId);
     } else {
         /* there is no complete Base Calculation */
         $msg = "There is no complete base '{$baseCalcTypeCode}' calculation for dependent " . "'{$dependentCalcTypeCode}' calculation. New period could not be created.";
         $this->_logger->warning($msg);
     }
     return $result;
 }
 public function qualification(Request\Qualification $req)
 {
     $result = new Response\Qualification();
     $datePerformed = $req->getDatePerformed();
     $dateApplied = $req->getDateApplied();
     $gvMaxLevels = $req->getGvMaxLevels();
     $msg = "'Qualification for Global Sales' calculation is started. " . "Performed at: {$datePerformed}, applied at: {$dateApplied}.";
     $this->_logger->info($msg);
     $reqGetPeriod = new PeriodGetForDependentCalcRequest();
     $calcTypeBase = Cfg::CODE_TYPE_CALC_COMPRESSION;
     $calcType = Cfg::CODE_TYPE_CALC_QUALIFICATION;
     $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();
             $calcDataBase = $respGetPeriod->getBaseCalcData();
             $dsBegin = $periodDataDepend->getDstampBegin();
             $dsEnd = $periodDataDepend->getDstampEnd();
             $calcIdBase = $calcDataBase->getId();
             $tree = $this->_repoBonusCompress->getTreeByCalcId($calcIdBase);
             $qualData = $this->_repoMod->getQualificationData($dsBegin, $dsEnd);
             $cfgParams = $this->_repoMod->getConfigParams();
             $updates = $this->_subQualification->calcParams($tree, $qualData, $cfgParams, $gvMaxLevels);
             $this->_repoMod->saveQualificationParams($updates);
             $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("'Qualification for Global Sales' calculation is complete.");
     return $result;
 }
 public function getLatest(Request\GetLatest $request)
 {
     $result = new Response\GetLatest();
     $calcTypeId = $request->getCalcTypeId();
     $calcTypeCode = $request->getCalcTypeCode();
     $msgParams = is_null($calcTypeId) ? "type code '{$calcTypeCode}'" : "type ID #{$calcTypeId}";
     $this->_logger->info("'Get latest calculation period' operation is started with {$msgParams} in bonus base module.");
     if (is_null($calcTypeId)) {
         /* get calculation type ID by type code */
         $calcTypeId = $this->_repoTypeCalc->getIdByCode($calcTypeCode);
         $this->_logger->info("There is only calculation type code ({$calcTypeCode}) in request, calculation type id = {$calcTypeId}.");
     }
     $periodLatest = $this->_repoService->getLastPeriodByCalcType($calcTypeId);
     if ($periodLatest) {
         $result->setPeriodData($periodLatest);
         /* add period calculations to result set */
         $periodId = $periodLatest->getId();
         $calcLatest = $this->_repoService->getLastCalcForPeriodById($periodId);
         $result->setCalcData($calcLatest);
     }
     $result->markSucceed();
     $this->_logger->info("'Get latest calculation period' operation is completed in bonus base module.");
     return $result;
 }