/**
  * Analyze referral code and get parent for the customer.
  *
  * @param int $customerId
  * @param int $parentId
  * @return int
  */
 public function getReferredParentId($customerId, $parentId)
 {
     /* use customer ID as parent ID if parent ID is missed */
     $result = $parentId ? $parentId : $customerId;
     $code = $this->_toolReferral->getReferralCode();
     if ($code) {
         $parentDo = $this->_repoCustomer->getByReferralCode($code);
         if ($parentDo) {
             $result = $parentDo->getCustomerId();
         }
     }
     return $result;
 }
 public function changeParent(Request\ChangeParent $request)
 {
     $result = new Response\ChangeParent();
     $customerId = $request->getCustomerId();
     $newParentId = $request->getNewParentId();
     $formatted = $request->getDate();
     $this->_logger->info("Set up new parent #{$newParentId} for customer #{$customerId}.");
     $def = $this->_manTrans->begin();
     try {
         /* get customer's downline  data */
         $data = $this->_repoCustomer->getById($customerId);
         $currParentId = $data->getParentId();
         $currDepth = $data->getDepth();
         $currPath = $data->getPath();
         if ($currParentId == $newParentId) {
             /* nothing to change */
             $result->markSucceed();
             $this->_manTrans->commit($def);
             $this->_logger->notice("Current parent is the same as new one. Nothing to do.");
         } else {
             if ($customerId == $newParentId) {
                 /* change to root node */
                 $newCustomerDepth = Cfg::INIT_DEPTH;
                 $newCustomerPath = Cfg::DTPS;
             } else {
                 /* get new parent data */
                 $newParentData = $this->_repoCustomer->getById($newParentId);
                 $newParentDepth = $newParentData->getDepth();
                 $newParentPath = $newParentData->getPath();
                 $newCustomerDepth = $newParentDepth + 1;
                 $newCustomerPath = $newParentPath . $newParentId . Cfg::DTPS;
             }
             /* update customer with new data */
             $bind = [Customer::ATTR_PARENT_ID => $newParentId, Customer::ATTR_DEPTH => $newCustomerDepth, Customer::ATTR_PATH => $newCustomerPath];
             $updateRows = $this->_repoCustomer->updateById($customerId, $bind);
             if ($updateRows == 1) {
                 /* update depths and paths in downline */
                 $deltaDepth = $newCustomerDepth - $currDepth;
                 $pathKey = $currPath . $customerId . Cfg::DTPS;
                 $pathReplace = $newCustomerPath . $customerId . Cfg::DTPS;
                 $rowsUpdated = $this->_repoCustomer->updateChildrenPath($pathKey, $pathReplace, $deltaDepth);
                 $this->_logger->info("Total '{$rowsUpdated}' customers in downline were updated.");
                 /* save new record into change log */
                 $bind = [Change::ATTR_CUSTOMER_ID => $customerId, Change::ATTR_PARENT_ID => $newParentId, Change::ATTR_DATE_CHANGED => $formatted];
                 $insertedId = $this->_repoChange->create($bind);
                 if ($insertedId) {
                     $this->_logger->info("New change log record #{$insertedId} is inserted (customer: {$customerId}, parent: {$newParentId}, date: {$formatted}).");
                     $this->_manTrans->commit($def);
                     $result->markSucceed();
                     $this->_logger->info("New parent #{$newParentId} for customer #{$customerId} is set.");
                 }
             }
         }
     } finally {
         $this->_manTrans->end($def);
     }
     return $result;
 }
 /**
  * @param \Magento\Sales\Api\Data\OrderInterface $mageOrder
  * @return \Praxigento\Odoo\Data\Odoo\SaleOrder\Customer
  */
 public function getSaleOrderCustomer(\Magento\Sales\Api\Data\OrderInterface $mageOrder)
 {
     $result = $this->_manObj->create(\Praxigento\Odoo\Data\Odoo\SaleOrder\Customer::class);
     /* collect data */
     $custMageId = (int) $mageOrder->getCustomerId();
     $dwnlCust = $this->_repoDwnlCustomer->getById($custMageId);
     $ref = $dwnlCust->getHumanRef();
     $name = $mageOrder->getCustomerName();
     $mageCust = $this->_repoMageCustomer->getById($custMageId);
     $groupCode = $this->_manBusinessCodes->getBusCodeForCustomerGroup($mageCust);
     /* init Odoo data object */
     $result->setIdMage($custMageId);
     $result->setIdMlm($ref);
     $result->setName($name);
     $result->setGroupCode($groupCode);
     return $result;
 }
Example #4
0
 public function getDownlineCustomerById($id)
 {
     $result = $this->_repoDownlineCustomer->getById($id);
     return $result;
 }