/**
  * @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.");
         }
     }
 }
 public function execute(\Magento\Framework\Event\Observer $observer)
 {
     /** @var \Magento\Customer\Model\Data\Customer $beforeSave */
     $beforeSave = $observer->getData('orig_customer_data_object');
     /** @var \Magento\Customer\Model\Data\Customer $afterSave */
     $afterSave = $observer->getData('customer_data_object');
     $idBefore = $beforeSave->getId();
     $idAfter = $afterSave->getId();
     if ($idBefore != $idAfter) {
         /* this is newly saved customer, register it into downline */
         $req = new \Praxigento\Downline\Service\Customer\Request\Add();
         $req->setCustomerId($idAfter);
         /* TODO: reference should be generated */
         $req->setReference($idAfter);
         $this->_callCustomer->add($req);
     }
     return;
 }