Advertisement
Guest User

Parentaccount Class

a guest
Mar 19th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 14.17 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Cminds\MultiUserAccounts\Model\Api;
  4.  
  5. use Cminds\MultiUserAccounts\Api\Data\ApiParentAccountInterface;
  6. use Cminds\MultiUserAccounts\Api\ParentaccountInterface;
  7. use Cminds\MultiUserAccounts\Api\SubaccountTransportRepositoryInterface;
  8. use Cminds\MultiUserAccounts\Model\Import;
  9. use Cminds\MultiUserAccounts\Model\Permission;
  10. use Cminds\MultiUserAccounts\Model\ResourceModel\Subaccount as SubaccountResourceModel;
  11. use Cminds\MultiUserAccounts\Model\ResourceModel\SubaccountRepository as SubaccountRepository;
  12. use Magento\Customer\Api\AddressRepositoryInterface;
  13. use Magento\Customer\Api\CustomerRepositoryInterface;
  14. use Magento\Customer\Model\CustomerRegistry;
  15. use Magento\Framework\DataObject;
  16. use Magento\Framework\DataObjectFactory;
  17. use Magento\Framework\Exception\LocalizedException;
  18. use Magento\Framework\Exception\NoSuchEntityException;
  19. use Magento\Framework\Registry;
  20. use Magento\Customer\Model\CustomerFactory;
  21.  
  22. class Parentaccount implements ParentaccountInterface
  23. {
  24.  
  25.     /**
  26.      * @var CustomerFactory
  27.      */
  28.     private $customerFactory;
  29.  
  30.     /**
  31.      * @var SubaccountTransportRepositoryInterface
  32.      */
  33.     private $subaccountTransportRepository;
  34.  
  35.     /**
  36.      * @var Permission
  37.      */
  38.     private $permission;
  39.  
  40.     /**
  41.      * @var Registry
  42.      */
  43.     private $registry;
  44.  
  45.     /**
  46.      * @var AddressRepositoryInterface
  47.      */
  48.     private $addressRepository;
  49.  
  50.     /**
  51.      * @var CustomerRepositoryInterface
  52.      */
  53.     private $customerRepository;
  54.  
  55.     /**
  56.      * @var Import
  57.      */
  58.     private $import;
  59.  
  60.     /**
  61.      * @var SubaccountResourceModel
  62.      */
  63.     private $subaccountResourceModel;
  64.  
  65.     /**
  66.      * @var DataObjectFactory
  67.      */
  68.     private $dataObjectFactory;
  69.  
  70.     /**
  71.      * @var CustomerRegistry
  72.      */
  73.     private $customerRegistry;
  74.  
  75.     /**
  76.      * @var SubaccountRepository
  77.      */
  78.     private $subaccountRepository;
  79.  
  80.     public function __construct(
  81.         AddressRepositoryInterface $addressRepository,
  82.         CustomerRepositoryInterface $customerRepository,
  83.         SubaccountResourceModel $subaccountResourceModel,
  84.         Import $import,
  85.         Registry $registry,
  86.         Permission $permission,
  87.         SubaccountTransportRepositoryInterface $subaccountTransportRepository,
  88.         DataObjectFactory $dataObjectFactory,
  89.         CustomerRegistry $customerRegistry,
  90.         SubaccountRepository $subaccountRepository,
  91.         CustomerFactory $customerFactory
  92.     ) {
  93.         $this->addressRepository = $addressRepository;
  94.         $this->customerRepository = $customerRepository;
  95.         $this->subaccountResourceModel = $subaccountResourceModel;
  96.         $this->import = $import;
  97.         $this->registry = $registry;
  98.         $this->subaccountTransportRepository = $subaccountTransportRepository;
  99.         $this->permission = $permission;
  100.         $this->dataObjectFactory = $dataObjectFactory;
  101.         $this->customerRegistry = $customerRegistry;
  102.         $this->subaccountRepository = $subaccountRepository;
  103.         $this->customerFactory = $customerFactory;
  104.     }
  105.  
  106.     /**
  107.      * Get the customer model loaded by its ID.
  108.      *
  109.      * @param int $id
  110.      *
  111.      * @return \Magento\Customer\Api\Data\CustomerInterface
  112.      * @throws \Magento\Framework\Exception\LocalizedException
  113.      * @throws \Magento\Framework\Exception\NoSuchEntityException
  114.      */
  115.     private function getCustomerById($id)
  116.     {
  117.         return $this->customerRepository->getById($id);
  118.     }
  119.  
  120.     /**
  121.      * API endpoint that fetches parent account information.
  122.      *
  123.      *
  124.      * @return DataObject
  125.      * @throws \Magento\Framework\Exception\LocalizedException
  126.      * @throws \Magento\Framework\Exception\NoSuchEntityException
  127.      */
  128.     public function getById($parentId)
  129.     {
  130.         try {
  131.             $customer = $this->getCustomerById($parentId);
  132.         } catch (NoSuchEntityException $e) {
  133.             throw new NoSuchEntityException(
  134.                 __('Customer with provided id does not exists or is not a parent account.')
  135.             );
  136.         }
  137.  
  138.         try {
  139.             $subArray = $this->subaccountResourceModel->getSubaccountIdsByParentCustomerId($customer->getId());
  140.         } catch (NoSuchEntityException $e) {
  141.             throw new NoSuchEntityException(
  142.                 __('Customer with provided id does not exist')
  143.             );
  144.         }
  145.  
  146.  
  147.         $customerData = [
  148.             'firstname' => $customer->getFirstname(),
  149.             'lastname' => $customer->getLastname(),
  150.             'email' => $customer->getEmail(),
  151.             'is_active' => $customer->getCustomAttribute('customer_is_active')->getValue(),
  152.             'can_manage_subaccounts' => $customer->getCustomAttribute('can_manage_subaccounts')->getValue(),
  153.             'id' => $customer->getId(),
  154.             'website_id' => $customer->getWebsiteId(),
  155.             'group_id' => $customer->getGroupId(),
  156.             'prefix' => $customer->getPrefix(),
  157.             'middlename' => $customer->getMiddlename(),
  158.             'suffix' => $customer->getSuffix(),
  159.             'dob' => $customer->getDob(),
  160.             'taxvat' => $customer->getTaxvat(),
  161.             'gender' => $customer->getGender(),
  162.             'company' => '',
  163.             'city' => '',
  164.             'country_id' => '',
  165.             'region' => '',
  166.             'postcode' => '',
  167.             'telephone' => '',
  168.             'fax' => '',
  169.             'vat_id' => '',
  170.             'street_1' => '',
  171.             'street_2' => '',
  172.         ];
  173.  
  174.         $addressId = $customer->getDefaultShipping();
  175.         if ($addressId !== null) {
  176.             $address = $this->addressRepository->getById($addressId);
  177.  
  178.             $streetArray = $this->getStreetArray($address);
  179.  
  180.             $customerData['city'] = $address->getCity();
  181.             $customerData['country_id'] = $address->getCountryId();
  182.             $customerData['region'] = $address->getRegionId();
  183.             $customerData['postcode'] = $address->getPostcode();
  184.             $customerData['telephone'] = $address->getTelephone();
  185.             $customerData['fax'] = '';
  186.             $customerData['vat_id'] = '';
  187.             $customerData['street_1'] = $streetArray[0];
  188.             $customerData['street_2'] = $streetArray[1];
  189.         }
  190.  
  191.         $subaccountsData = [];
  192.         $subaccountIds = $this->subaccountResourceModel
  193.             ->getSubaccountIdsByParentCustomerId($parentId);
  194.  
  195.         foreach ($subaccountIds as $subaccountId) {
  196.             $subaccountTransport = $this
  197.                 ->getSubAccountTransportDataObject($subaccountId);
  198.             $subaccountCustomer = $this->getCustomerById($subaccountTransport->getCustomerId());
  199.  
  200.             if ($subaccountCustomer->getCustomAttribute('customer_is_active')) {
  201.                 $subActive = $subaccountCustomer->getCustomAttribute('customer_is_active')->getValue();
  202.             } else {
  203.                 $subActive = 0;
  204.             }
  205.  
  206.             $subaccountsData[$subaccountId] = [
  207.                 'id' => $subaccountCustomer->getId(),
  208.                 'parent_email' => $customer->getEmail(),
  209.                 'firstname' => $subaccountCustomer->getFirstname(),
  210.                 'lastname' => $subaccountCustomer->getLastname(),
  211.                 'email' => $subaccountCustomer->getEmail(),
  212.                 'website_id' => $subaccountCustomer->getWebsiteId(),
  213.                 'group_id' => $subaccountCustomer->getGroupId(),
  214.                 'prefix' => $subaccountCustomer->getPrefix(),
  215.                 'middlename' => $subaccountCustomer->getMiddlename(),
  216.                 'suffix' => $subaccountCustomer->getSuffix(),
  217.                 'dob' => $subaccountCustomer->getDob(),
  218.                 'taxvat' => $subaccountCustomer->getTaxvat(),
  219.                 'gender' => $subaccountCustomer->getGender(),
  220.                 'is_active' => $subActive,
  221.                 'company' => '',
  222.                 'city' => '',
  223.                 'country_id' => '',
  224.                 'region_id' => '',
  225.                 'postcode' => '',
  226.                 'telephone' => '',
  227.                 'fax' => '',
  228.                 'vat_id' => '',
  229.                 'street_1' => '',
  230.                 'street_2' => '',
  231.                 'account_data_modification_permission' => (int)$subaccountTransport
  232.                     ->getAccountDataModificationPermission(),
  233.                 'account_order_history_view_permission' => (int)$subaccountTransport
  234.                     ->getAccountOrderHistoryViewPermission(),
  235.                 'checkout_order_create_permission' => (int)$subaccountTransport
  236.                     ->getCheckoutOrderCreatePermission(),
  237.                 'checkout_order_approval_permission' => (int)$subaccountTransport
  238.                     ->getCheckoutOrderApprovalPermission(),
  239.                 'checkout_cart_view_permission' => (int)$subaccountTransport
  240.                     ->getCheckoutCartViewPermission(),
  241.                 'checkout_view_permission' => (int)$subaccountTransport
  242.                     ->getCheckoutViewPermission(),
  243.                 'checkout_order_placed_notification_permission' => (int)$subaccountTransport
  244.                     ->getCheckoutOrderPlacedNotificationPermission(),
  245.                 'force_usage_parent_company_name_permission' => (int)$subaccountTransport
  246.                     ->getForceUsageParentCompanyNamePermission(),
  247.                 'force_usage_parent_vat_permission' => (int)$subaccountTransport
  248.                     ->getForceUsageParentVatPermission(),
  249.                 'force_usage_parent_addresses_permission' => (int)$subaccountTransport
  250.                     ->getForceUsageParentAddressesPermission(),
  251.             ];
  252.  
  253.             $subaccountAddress = null;
  254.             foreach ($subaccountCustomer->getAddresses() as $address) {
  255.                 if ($address->isDefaultShipping()) {
  256.                     $subaccountAddress = $address;
  257.                 }
  258.             }
  259.  
  260.             if ($subaccountAddress !== null) {
  261.                 $streetSubArray = $this->getStreetArray($subaccountAddress);
  262.  
  263.                 $subaccountsData[$subaccountId]['city'] = $subaccountAddress->getCity();
  264.                 $subaccountsData[$subaccountId]['country_id'] = $subaccountAddress->getCountryId();
  265.                 $subaccountsData[$subaccountId]['region_id'] = $subaccountAddress->getRegionId();
  266.                 $subaccountsData[$subaccountId]['postcode'] = $subaccountAddress->getPostcode();
  267.                 $subaccountsData[$subaccountId]['telephone'] = $subaccountAddress->getTelephone();
  268.                 $subaccountsData[$subaccountId]['street_1'] = $streetSubArray[0];
  269.                 $subaccountsData[$subaccountId]['street_2'] = $streetSubArray[1];
  270.             }
  271.         }
  272.  
  273.         $customerData['subaccounts'] = [];
  274.  
  275.         $result = $this->dataObjectFactory->create()
  276.             ->setData($customerData);
  277.  
  278.         return $result;
  279.     }
  280.  
  281.     public function create(array $parentCustomerData)
  282.     {
  283.         try {
  284.             $this->import
  285.                 ->initSourceProcessor(Import::SOURCE_API)
  286.                 ->setApiData($parentCustomerData)
  287.                 ->setCanManageSub();
  288.             $this->import->process();
  289.         } catch (\Exception $e) {
  290.             throw new LocalizedException(
  291.                 __('Parent account has been not created. Details: '.$e->getMessage().'.')
  292.             );
  293.         }
  294.  
  295.         $customer = $this->customerRegistry
  296.             ->retrieveByEmail($parentCustomerData['email']);
  297.  
  298.         return $this->getById($customer->getId());
  299.     }
  300.  
  301.     public function updateById($parentId, ApiParentAccountInterface $customer)
  302.     {
  303.         try {
  304.             $customerEntity = $this->getCustomerById($parentId);
  305.         } catch (NoSuchEntityException $e) {
  306.             throw new NoSuchEntityException(
  307.                 __('Customer with provided id does not exist')
  308.             );
  309.         }
  310.  
  311.         try {
  312.             $customerEntity->getCustomAttribute('can_manage_subaccounts');
  313.         } catch (NoSuchEntityException $e) {
  314.             throw new NoSuchEntityException(
  315.                     __('Customer with provided id is not a parent account.')
  316.                 );
  317.         }
  318.  
  319.         try {
  320.             $this->import->setUpdateFlag();
  321.             $this->import->setCustomerId($parentId);
  322.             $this->import
  323.                 ->initSourceProcessor(Import::SOURCE_API)
  324.                 ->setApiData($customer)
  325.                 ->setParentId($parentId);
  326.             $this->import->process();
  327.         } catch (\Exception $e) {
  328.             throw new LocalizedException(
  329.                 __('Parent account has been not updated. Details: '.$e->getMessage().'.')
  330.             );
  331.         }
  332.  
  333.         return $this->getById($parentId);
  334.     }
  335.  
  336.     public function deleteById($parentId)
  337.     {
  338.         try {
  339.             $this->registry->register('isSecureArea', true);
  340.             $customer = $this->customerRepository->getById($parentId);
  341.         } catch (NoSuchEntityException $e) {
  342.             throw new NoSuchEntityException(
  343.                 __('Customer with provided id doesn\'t exist.')
  344.             );
  345.         }
  346.         try {
  347.             $subAccount = $this->subaccountRepository->getById($parentId);
  348.             if ($subAccount) {
  349.                 throw new LocalizedException(
  350.                     __('Customer with provided id isn\'t parent account')
  351.                 );
  352.             }
  353.         } catch (NoSuchEntityException $e) {
  354.  
  355.         }
  356.         if ($customer !== null) {
  357.             $this->customerRepository->delete($customer);
  358.         }
  359.         return $result[] = [
  360.             'message' => 'Success. Customer with provided id has been deleted.'
  361.         ];
  362.     }
  363.  
  364.     private function getStreetArray($address)
  365.     {
  366.         foreach ($address->getStreet() as $street) {
  367.             $streetArray[] = $street;
  368.         }
  369.         if (count($streetArray) === 1) {
  370.             $streetArray[] = '';
  371.         }
  372.  
  373.         return $streetArray;
  374.     }
  375.  
  376.     private function getSubAccountTransportDataObject($sub)
  377.     {
  378.         $existingSubaccountTransportDataObject = $this
  379.             ->subaccountTransportRepository
  380.             ->getById($sub);
  381.         $this->permission->loadSubaccountPermissions(
  382.             $existingSubaccountTransportDataObject
  383.         );
  384.  
  385.         return $existingSubaccountTransportDataObject;
  386.     }
  387. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement