public function addCalc(Request\AddCalc $request)
 {
     $result = new Response\AddCalc();
     $calcTypeId = $request->getCalcTypeId();
     $dsBegin = $request->getDateStampBegin();
     $dsEnd = $request->getDateStampEnd();
     $def = $this->_manTrans->begin();
     try {
         /* create new period for given calculation type */
         $periodData = [Period::ATTR_CALC_TYPE_ID => $calcTypeId, Period::ATTR_DSTAMP_BEGIN => $dsBegin, Period::ATTR_DSTAMP_END => $dsEnd];
         $periodId = $this->_repoPeriod->create($periodData);
         /* create new calculation for the period */
         $dateStarted = $this->_toolDate->getUtcNowForDb();
         $calcData = [Calculation::ATTR_PERIOD_ID => $periodId, Calculation::ATTR_DATE_STARTED => $dateStarted, Calculation::ATTR_STATE => Cfg::CALC_STATE_STARTED];
         $calcId = $this->_repoCalc->create($calcData);
         $this->_manTrans->commit($def);
         /* compose response */
         $periodData[Period::ATTR_ID] = $periodId;
         $calcData[Calculation::ATTR_ID] = $calcId;
         $result->setPeriod($periodData);
         $result->setCalculation($calcData);
         $result->markSucceed();
     } finally {
         $this->_manTrans->end($def);
     }
     return $result;
 }
 /**
  * This function is created for CRAP reducing and is used from this class only.
  *
  * @param \Praxigento\BonusBase\Service\Period\Response\GetForDependentCalc $result
  * @param string $baseCalcTypeCode
  * @param string $baseDsBegin
  * @param string $baseDsEnd
  * @param string $dependentCalcTypeCode
  * @param int $dependentCalcTypeId
  */
 public function _getForCompleteBase(\Praxigento\BonusBase\Service\Period\Response\GetForDependentCalc $result, $baseCalcTypeCode, $baseDsBegin, $baseDsEnd, $dependentCalcTypeCode, $dependentCalcTypeId)
 {
     $dependPeriodData = $this->_repoService->getLastPeriodByCalcType($dependentCalcTypeId);
     if (is_null($dependPeriodData)) {
         /* there is no dependent period, create new period and calc */
         $msg = "There is no period data for calculation '{$dependentCalcTypeCode}'." . " New period and related calculation will be created.";
         $this->_logger->warning($msg);
         /* create new period for given calculation type */
         $period = new EPeriod();
         $period->setCalcTypeId($dependentCalcTypeId);
         $period->setDstampBegin($baseDsBegin);
         $period->setDstampEnd($baseDsEnd);
         $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->setDependentPeriodData($period);
         $result->setDependentCalcData($calc);
     } else {
         /* there is depended period, place period data into response */
         $result->setDependentPeriodData($dependPeriodData);
         /* then analyze base/depended periods begin/end  */
         $dependentDsBegin = $dependPeriodData->getDstampBegin();
         $dependentDsEnd = $dependPeriodData->getDstampEnd();
         $this->_getDependedCalcForExistingPeriod($result, $baseCalcTypeCode, $baseDsBegin, $baseDsEnd, $dependentCalcTypeCode, $dependentCalcTypeId, $dependentDsBegin, $dependentDsEnd);
     }
 }
 /**
  *
  * 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;
 }
 public function markCalcComplete($calcId)
 {
     $tsEnded = $this->_toolDate->getUtcNowForDb();
     $bind = [ECalculation::ATTR_DATE_ENDED => $tsEnded, ECalculation::ATTR_STATE => Cfg::CALC_STATE_COMPLETE];
     $where = ECalculation::ATTR_ID . '=' . $calcId;
     $result = $this->_repoCalc->update($bind, $where);
     return $result;
 }