/** * @param Request\GetForWriteOff $request * * @return Response\GetForWriteOff */ public function getForWriteOff(Request\GetForWriteOff $request) { $result = new Response\GetForWriteOff(); $this->_logger->info("'Get latest period for Write Off calculation' operation is started."); /* get the last Write Off period data */ $calcWriteOffCode = Cfg::CODE_TYPE_CALC_PV_WRITE_OFF; $calcWriteOffId = $this->_subDb->getCalcIdByCode($calcWriteOffCode); $respWriteOffLastPeriod = $this->_subDb->getLastPeriodData($calcWriteOffId); $periodWriteOffData = $respWriteOffLastPeriod->getPeriodData(); if (is_null($periodWriteOffData)) { $this->_logger->info("There is no period for PV Write Off calculation yet."); /* calc period for PV Write Off */ $tsFirstPv = $this->_subDb->getFirstDateForPvTransactions(); if ($tsFirstPv === false) { $this->_logger->info("There is no PV transactions yet. Nothing to do."); $result->setHasNoPvTransactionsYet(); } else { $this->_logger->info("First PV transaction was performed at '{$tsFirstPv}'."); $periodMonth = $this->_toolPeriod->getPeriodCurrent($tsFirstPv, ToolPeriod::TYPE_MONTH); $dsBegin = $this->_toolPeriod->getPeriodFirstDate($periodMonth); $dsEnd = $this->_toolPeriod->getPeriodLastDate($periodMonth); $periodWriteOffData = $this->_subDb->addNewPeriodAndCalc($calcWriteOffId, $dsBegin, $dsEnd); $result->setPeriodData($periodWriteOffData->getData(Sub\Db::DATA_PERIOD)); $result->setCalcData($periodWriteOffData->getData(Sub\Db::DATA_CALC)); $result->markSucceed(); } } else { $result->setPeriodData($periodWriteOffData); $periodId = $periodWriteOffData->getId(); $this->_logger->info("There is registered period #{$periodId} for '{$calcWriteOffCode}' calculation."); $calcData = $respWriteOffLastPeriod->getCalcData(); if ($calcData === false) { $this->_logger->info("There is no calculation data for existing period. Use existing period data."); $result->markSucceed(); } else { if ($calcData && $calcData->getState() == Cfg::CALC_STATE_COMPLETE) { $this->_logger->info("There is complete calculation for existing period. Create new period."); $periodEnd = $periodWriteOffData->getDstampEnd(); /* calculate new period bounds */ $periodNext = $this->_toolPeriod->getPeriodNext($periodEnd, ToolPeriod::TYPE_MONTH); $dsNextBegin = $this->_toolPeriod->getPeriodFirstDate($periodNext); $dsNextEnd = $this->_toolPeriod->getPeriodLastDate($periodNext); $periodWriteOffData = $this->_subDb->addNewPeriodAndCalc($calcWriteOffId, $dsNextBegin, $dsNextEnd); $result->setPeriodData($periodWriteOffData->getData(Sub\Db::DATA_PERIOD)); $result->setCalcData($periodWriteOffData->getData(Sub\Db::DATA_CALC)); $result->markSucceed(); } else { $this->_logger->info("There is no complete calculation for existing period. Use existing period data."); $result->setCalcData($calcData); $result->markSucceed(); } } } $this->_logger->info("'Get latest period for Write Off calculation' operation is completed."); return $result; }
/** * * 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; }