/**
  * @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;
 }
 public function test_pvWriteOff_isPeriod_noTransactions()
 {
     /** === Test Data === */
     /** === Mocks === */
     $mLogger = $this->_mockLogger();
     $mConn = $this->_mockConnection();
     $mDba = $this->_mockDbAdapter(null, $mConn);
     $mToolbox = $this->_mockToolbox();
     $mCallRepo = $this->_mockCallRepo();
     $mCallAccount = $this->_mockFor('Praxigento\\Accounting\\Service\\IAccount');
     $mCallBonusPersonalPeriod = $this->_mockFor('Praxigento\\Bonus\\Hybrid\\Lib\\Service\\IPeriod');
     $mSubDb = $this->_mockFor('Praxigento\\Bonus\\Hybrid\\Lib\\Service\\Calc\\Sub\\Db');
     $mSubCalc = $this->_mockFor('Praxigento\\Bonus\\Hybrid\\Lib\\Service\\Calc\\Sub\\Calc');
     // $respGetPeriod = $this->_callBonusPersonalPeriod->getForWriteOff($reqGetPeriod);
     $mRespGetPeriod = new BonusPersonalPeriodGetForWriteOffResponse();
     $mRespGetPeriod->markSucceed();
     $mRespGetPeriod->setHasNoPvTransactionsYet();
     $mCallBonusPersonalPeriod->expects($this->once())->method('getForWriteOff')->willReturn($mRespGetPeriod);
     /**
      * Prepare request and perform call.
      */
     /** @var  $call Call */
     $call = new Call($mLogger, $mDba, $mToolbox, $mCallRepo, $mCallAccount, $mCallBonusPersonalPeriod, $mSubDb, $mSubCalc);
     $req = new Request\PvWriteOff();
     $resp = $call->pvWriteOff($req);
     $this->assertTrue($resp->isSucceed());
 }