/**
  * @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_getFirstDateForPvTransactions()
 {
     /** === Test Data === */
     $TBL_ACC = 'account table';
     $TBL_TRANS = 'transaction table';
     $TBL_TYPE = 'type table';
     $RESULT_TS = 'date';
     /** === Mocks === */
     $mLogger = $this->_mockLogger();
     $mConn = $this->_mockConnection();
     $mDba = $this->_mockDbAdapter(null, $mConn);
     $mToolbox = $this->_mockToolbox();
     $mCallRepo = $this->_mockCallRepo();
     $mCallBonusBasePeriod = $this->_mockFor('\\Praxigento\\BonusBase\\Service\\IPeriod');
     $mCallTypeCalc = $this->_mockFor('\\Praxigento\\BonusBase\\Service\\ITypeCalc');
     // $tblAcc = $this->_resource->getTableName(Account::ENTITY_NAME);
     $mDba->expects($this->at(0))->method('getTableName')->with(Account::ENTITY_NAME)->willReturn($TBL_ACC);
     // $tblTrans = $this->_resource->getTableName(Transaction::ENTITY_NAME);
     $mDba->expects($this->at(1))->method('getTableName')->with(Transaction::ENTITY_NAME)->willReturn($TBL_TRANS);
     // $tblTrans = $this->_resource->getTableName(Transaction::ENTITY_NAME);
     $mDba->expects($this->at(2))->method('getTableName')->with(TypeAsset::ENTITY_NAME)->willReturn($TBL_TYPE);
     // $query = $this->_conn->select();
     $mQuery = $this->_mockDbSelect();
     $mConn->expects($this->once())->method('select')->willReturn($mQuery);
     // $result = $this->_conn->fetchOne($query);
     $mConn->expects($this->once())->method('fetchOne')->willReturn($RESULT_TS);
     /**
      * Prepare request and perform call.
      */
     /** @var  $sub Db */
     $sub = new Db($mLogger, $mDba, $mToolbox, $mCallRepo, $mCallBonusBasePeriod, $mCallTypeCalc);
     $resp = $sub->getFirstDateForPvTransactions();
     $this->assertEquals($RESULT_TS, $resp);
 }