public function test_bonusCourtesy()
 {
     $obm = \Magento\Framework\App\ObjectManager::getInstance();
     /** @var  $call \Praxigento\Bonus\Hybrid\Lib\Service\ICalc */
     $call = $obm->get('Praxigento\\Bonus\\Hybrid\\Lib\\Service\\ICalc');
     $request = new Request\BonusCourtesy();
     $request->setCourtesyBonusPercent(self::COURTESY_BONUS_PERCENT);
     $response = $call->bonusCourtesy($request);
     $this->assertTrue($response->isSucceed());
 }
 public function bonusCourtesy(Request\BonusCourtesy $request)
 {
     $result = new Response\BonusCourtesy();
     $courtesyPercent = $request->getCourtesyBonusPercent();
     $datePerformed = $request->getDatePerformed();
     $dateApplied = $request->getDateApplied();
     $this->_logger->info("'Courtesy Bonus' calculation is started.");
     $reqGetPeriod = new PeriodGetForDependentCalcRequest();
     $reqGetPeriod->setBaseCalcTypeCode(Cfg::CODE_TYPE_CALC_VALUE_TV);
     $reqGetPeriod->setDependentCalcTypeCode(Cfg::CODE_TYPE_CALC_BONUS_COURTESY);
     $respGetPeriod = $this->_callPeriod->getForDependentCalc($reqGetPeriod);
     if ($respGetPeriod->isSucceed()) {
         $def = $this->_manTrans->begin();
         try {
             /* working vars */
             $thisPeriodData = $respGetPeriod->getDependentPeriodData();
             $thisPeriodId = $thisPeriodData[Period::ATTR_ID];
             $thisCalcData = $respGetPeriod->getDependentCalcData();
             $thisCalcId = $thisCalcData[Calculation::ATTR_ID];
             $basePeriodData = $respGetPeriod->getBasePeriodData();
             $baseDsBegin = $basePeriodData->getDstampBegin();
             $baseDsEnd = $basePeriodData->getDstampEnd();
             /* get PTC Compression calculation ID for this period */
             $ptcCompressCalcId = $this->_subDb->getLastCalculationIdForPeriod(Cfg::CODE_TYPE_CALC_COMPRESS_FOR_PTC, $baseDsBegin, $baseDsEnd);
             /* calculation itself */
             $this->_logger->info("Processing period #{$thisPeriodId} ({$baseDsBegin}-{$baseDsEnd})");
             /* get compressed data by calculation ID */
             $compressPtc = $this->_subDb->getCompressedPtcData($ptcCompressCalcId);
             /* get levels to calculate Personal and Team bonuses */
             $levelsPersonal = $this->_subDb->getBonusLevels(Cfg::CODE_TYPE_CALC_BONUS_PERSONAL_DEF);
             $levelsTeam = $this->_subDb->getBonusLevels(Cfg::CODE_TYPE_CALC_BONUS_TEAM_DEF);
             /* calculates bonus and save operation with transactions */
             $updates = $this->_subCalc->bonusCourtesy($compressPtc, $courtesyPercent, $levelsPersonal, $levelsTeam);
             /* prepare data for updates */
             $updatesForWallets = [];
             foreach ($updates as $custId => $items) {
                 foreach ($items as $item) {
                     $bonus = $item[Calc::A_VALUE];
                     $childId = $item[Calc::A_OTHER_ID];
                     if ($bonus > Cfg::DEF_ZERO) {
                         $updatesForWallets[] = [Calc::A_CUST_ID => $custId, Calc::A_VALUE => $bonus, Calc::A_OTHER_ID => $childId];
                     }
                 }
             }
             $respAdd = $this->_subDb->saveOperationWalletActive($updatesForWallets, Cfg::CODE_TYPE_OPER_BONUS_COURTESY, $datePerformed, $dateApplied);
             $operId = $respAdd->getOperationId();
             $transIds = $respAdd->getTransactionsIds();
             /* save orders and correspondent transactions into the log */
             $this->_subDb->saveLogCustomers($updatesForWallets, $transIds);
             /* save processed operation to calculations log and mark calculation as complete */
             $this->_subDb->saveLogOperations($operId, $thisCalcId);
             $this->_subDb->markCalcComplete($thisCalcId);
             /* finalize response as succeed */
             $this->_manTrans->commit($def);
             $result->markSucceed();
             $result->setPeriodId($thisPeriodId);
             $result->setCalcId($thisCalcId);
         } finally {
             $this->_manTrans->end($def);
         }
     }
     $this->_logMemoryUsage();
     $this->_logger->info("'Courtesy Bonus' calculation is completed.");
     return $result;
 }
 public function test_bonusCourtesy_noPeriod()
 {
     /** === Test Data === */
     $COURTESY_BONUS_PERCENT = 0.05;
     /** === 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->_callPeriod->getForDependentCalc($reqGetPeriod);
     $mRespGetPeriod = new BonusPersonalPeriodGetForDependentCalcResponse();
     $mCallBonusPersonalPeriod->expects($this->once())->method('getForDependentCalc')->willReturn($mRespGetPeriod);
     /**
      * Prepare request and perform call.
      */
     /** @var  $call Call */
     $call = new Call($mLogger, $mDba, $mToolbox, $mCallRepo, $mCallAccount, $mCallBonusPersonalPeriod, $mSubDb, $mSubCalc);
     $req = new Request\BonusCourtesy();
     $req->setCourtesyBonusPercent($COURTESY_BONUS_PERCENT);
     $resp = $call->bonusCourtesy($req);
     $this->assertFalse($resp->isSucceed());
 }