/**
  * @param string $dateBegin datestamp (YYYYMMDD) for the date when the first customer should be created.
  * @param bool $switchDateOnNewCustomer 'true' - create customers day by day, 'false' - create all customers
  * in one day.
  */
 protected function _createDownlineCustomers($dateBegin = self::DATE_PERIOD_BEGIN, $switchDateOnNewCustomer = true)
 {
     $dtToday = $dateBegin;
     foreach ($this->DEFAULT_DWNL_TREE as $customerRef => $parentRef) {
         $customerMageId = $this->_mapCustomerMageIdByIndex[$customerRef];
         /* get magento customer data */
         $request = new CustomerAddRequest();
         $request->setCustomerId($customerMageId);
         $request->setParentId($this->_mapCustomerMageIdByIndex[$parentRef]);
         $request->setReference($this->_mapCustomerMageIdByIndex[$customerRef]);
         $request->setCountryCode(self::DEFAULT_DOWNLINE_COUNTRY_CODE);
         $request->setDate($this->_toolPeriod->getTimestampFrom($dtToday));
         /* Create customer per day or all customers in the same day. */
         if ($switchDateOnNewCustomer) {
             $dtToday = $this->_toolPeriod->getPeriodNext($dtToday);
         }
         $response = $this->_callDownlineCustomer->add($request);
         if ($response->isSucceed()) {
             $path = $response->getData(Customer::ATTR_PATH);
             $depth = $response->getData(Customer::ATTR_DEPTH);
             $this->_logger->debug("New customer #{$customerMageId} is added to path '{$path}' on depth {$depth} at '{$dtToday}'.");
         } else {
             $this->_logger->error("Cannot add new customer #{$customerMageId} to downline tree.");
         }
     }
 }
 private function _createCustomers()
 {
     /* create Magento Customers */
     $total = count($this->_mapCsvDownline);
     $this->_createMageCustomers($total);
     /* map MLM IDs to indexes and mage IDs and create tree based on Mage IDs */
     $mapTree = $this->_mapCustomersMlmId();
     $mapByCountry = $this->_mapCustomersCountries();
     /* expand minimal tree: populate tree with depth & path */
     $reqExpand = new SnapExtendMinimalRequest();
     $reqExpand->setTree($mapTree);
     $respExpand = $this->_callDownlineSnap->expandMinimal($reqExpand);
     $mapTree = $respExpand->getSnapData();
     /* create tree map sorted by level from top to bottom */
     $mapByDepthAsc = $this->_mapTreeByDepth($mapTree, Snap::ATTR_CUSTOMER_ID, Snap::ATTR_DEPTH);
     /* create customers in downline one by one */
     $period = $this->_toolPeriod;
     $dateAdded = $period->getTimestampFrom(self::DS_CUSTOMER_ADDED);
     foreach ($mapByDepthAsc as $depth => $ids) {
         foreach ($ids as $id) {
             $data = $mapTree[$id];
             $custId = $data[Snap::ATTR_CUSTOMER_ID];
             $parentId = $data[Snap::ATTR_PARENT_ID];
             $custNdx = $this->_mapCustomerIndexByMageId[$custId];
             $mlmId = $this->_mapCustomerMlmIdByIndex[$custNdx];
             $parentNdx = $this->_mapCustomerIndexByMageId[$parentId];
             $parentMlmId = $this->_mapCustomerMlmIdByIndex[$parentNdx];
             $countryCode = $mapByCountry[$mlmId];
             $reqAdd = new CustomerAddRequest();
             $reqAdd->setCustomerId($custId);
             $reqAdd->setParentId($parentId);
             $reqAdd->setReference($mlmId);
             $reqAdd->setCountryCode($countryCode);
             $reqAdd->setDate($dateAdded);
             $respAdd = $this->_callDownlineCustomer->add($reqAdd);
             if ($respAdd->isSucceed()) {
                 $this->_logger->debug("Downline change for customer #{$custId} ({$mlmId}) with parent #{$parentId} ({$parentMlmId}) is added to downline log.");
             } else {
                 $this->_logger->error("Cannot add new customer #{$custId} to downline tree.");
             }
         }
     }
     unset($csv);
     unset($mapByDepthAsc);
     unset($mapByCountry);
     unset($mapTree);
     $this->_createCustomerDownlineTreeSnap();
 }