public function test_qualifyByUserData()
 {
     $obm = \Magento\Framework\App\ObjectManager::getInstance();
     /**
      * Prepare request data.
      */
     /** @var  $callDownlineSnap \Praxigento\Downline\Service\ISnap */
     $callDownlineSnap = $obm->get('\\Praxigento\\Downline\\Service\\ISnap');
     $reqSnap = new \Praxigento\Downline\Service\Snap\Request\GetStateOnDate();
     $reqSnap->setDatestamp('20201231');
     $respSnap = $callDownlineSnap->getStateOnDate($reqSnap);
     $flatTree = $respSnap->getData();
     /** @var  $call \Praxigento\BonusBase\Service\Compress\Call */
     $call = $obm->get('Praxigento\\BonusBase\\Service\\Compress\\Call');
     $request = new Request\QualifyByUserData();
     $request->setCalcId(6);
     $request->setFlatTree($flatTree);
     $request->setQualifier(new CustomQualifyUser());
     $response = $call->qualifyByUserData($request);
     $this->assertTrue($response->isSucceed());
 }
 /**
  * To cover $skipExpand
  * @expectedException \Exception
  */
 public function test_qualifyByUserData_skipExpand()
 {
     /** === Test Data === */
     /**
      * Test data is not real customer tree, just for code coverage.
      */
     /** === Setup Mocks === */
     $this->mLogger->shouldReceive('info');
     // $mapById = $this->_mapById($treeExpanded);
     // $resp = $this->_callDownlineMap->byId($req);
     $this->mCallDownlineMap->shouldReceive('byId')->andThrow(new \Exception());
     /** === Call and asserts  === */
     $req = new Request\QualifyByUserData();
     $req->setSkipTreeExpand(true);
     $this->call->qualifyByUserData($req);
 }
 /**
  * @param Request\QualifyByUserData $req
  *
  * @return Response\QualifyByUserData
  */
 public function qualifyByUserData(Request\QualifyByUserData $req)
 {
     $result = new Response\QualifyByUserData();
     /* parse request */
     $calcId = $req->getCalcId();
     $treeFlat = $req->getFlatTree();
     $qualifier = $req->getQualifier();
     $skipExpand = (bool) $req->getSkipTreeExpand();
     $this->_logger->info("'QualifyByUserData' operation is started.");
     $treeCompressed = [];
     if ($skipExpand) {
         $treeExpanded = $treeFlat;
     } else {
         $treeExpanded = $this->_toolDownlineTree->expandMinimal($treeFlat, ESnap::ATTR_PARENT_ID);
     }
     $mapById = $this->_mapById($treeExpanded);
     $mapDepth = $this->_mapByTreeDepthDesc($treeExpanded);
     $mapTeams = $this->_mapByTeams($treeExpanded);
     foreach ($mapDepth as $depth => $levelCustomers) {
         foreach ($levelCustomers as $custId) {
             $custData = $mapById[$custId];
             $ref = isset($custData[Customer::ATTR_HUMAN_REF]) ? $custData[Customer::ATTR_HUMAN_REF] : '';
             if ($qualifier->isQualified($custData)) {
                 $this->_logger->info("Customer #{$custId} ({$ref}) is qualified and added to compressed tree.");
                 $treeCompressed[$custId] = $custData;
             } else {
                 $this->_logger->info("Customer #{$custId} ({$ref}) is not qualified.");
                 if (isset($mapTeams[$custId])) {
                     $this->_logger->info("Customer #{$custId} ({$ref}) has own front team.");
                     /* Lookup for the closest qualified parent */
                     $path = $treeExpanded[$custId][ESnap::ATTR_PATH];
                     $parents = $this->_toolDownlineTree->getParentsFromPathReversed($path);
                     $foundParentId = null;
                     foreach ($parents as $newParentId) {
                         $parentData = $mapById[$newParentId];
                         if ($qualifier->isQualified($parentData)) {
                             $foundParentId = $newParentId;
                             break;
                         }
                     }
                     /* Change parent for all siblings of the unqualified customer. */
                     $team = $mapTeams[$custId];
                     foreach ($team as $memberId) {
                         if (isset($treeCompressed[$memberId])) {
                             /* if null set customer own id to indicate root node */
                             $treeCompressed[$memberId][ESnap::ATTR_PARENT_ID] = is_null($foundParentId) ? $memberId : $foundParentId;
                         }
                     }
                 }
             }
         }
     }
     unset($mapCustomer);
     unset($mapPv);
     unset($mapDepth);
     unset($mapTeams);
     /* save compressed tree */
     $def = $this->_manTrans->begin();
     try {
         foreach ($treeCompressed as $custId => $item) {
             $data = [ECompress::ATTR_CALC_ID => $calcId, ECompress::ATTR_CUSTOMER_ID => $custId, ECompress::ATTR_PARENT_ID => $item[ESnap::ATTR_PARENT_ID]];
             $this->_repoBonusCompress->create($data);
         }
         $this->_manTrans->commit($def);
     } finally {
         $this->_manTrans->end($def);
     }
     $result->markSucceed();
     $this->_logger->info("'QualifyByUserData' operation is completed.");
     return $result;
 }