logger = $logger; $this->netSuiteAPI = $netSuiteAPI; $this->customerRepository = $customerRepository; $this->filterBuilder = $filterBuilder; $this->searchCriteriaBuilder = $searchCriteriaBuilder; $this->customerCollectionFactory = $customerCollectionFactory; } protected function searchCriteriaBuilder() { return $this->searchCriteriaBuilder ->addFilter($this->filterBuilder->setField('is_netsuite_sync')->setValue(0)->create()) ->addSortOrder('id', 'desc') ->create(); } /** * Execute the cron * * @return void */ public function execute(): void { try { $items = $this->customerCollectionFactory->create()->addAttributeToFilter('is_netsuite_sync', 0)->getItems(); foreach ($items as $customer) { /* @var $customer Customer */ $dataCustomer = $customer->getDataModel(); $customerData = $this->prepareCustomerData($dataCustomer); $response = $this->netSuiteAPI->postData(self::NETSUITE_SERVICE, $customerData); if ($response['error'] != true){ $dataCustomer->getExtensionAttributes()->setIsNetsuiteSync(1); $this->customerRepository->save($dataCustomer); } else { $this->logger->debug('Customer Crontab Sync to NetSuite fail: ', ['response' => $response, 'customer' => $customer->getEmail(), 'postData' => $customerData]); } } } catch (LocalizedException $e) { $this->logger->error('customer sync to Netsuite fail, '. $e->getMessage(), ['exception' => $e->getTrace()]); } } /** * build * @param DataCustomer $customer * @return array */ private function getCustomerAddressData(DataCustomer $customer): array { $defaultShippingId = $customer->getDefaultShipping(); $defaultBillingId = $customer->getDefaultBilling(); $addresses = $customer->getAddresses(); if (!$addresses) { return []; } $addressData = []; foreach ($addresses as $address) { $streets = ['', '']; if ($address->getStreet()) { $streets = $address->getStreet(); } if ($address->getId() == $defaultShippingId) { $this->phone = $address->getTelephone(); $this->companyName = $address->getCompany(); } $addressData[] = [ 'defaultBilling' => $address->getId() == $defaultBillingId, 'defaultShipping' => $address->getId() == $defaultShippingId, 'addressBookAddress' => [ 'country' => ['id'=>$address->getCountryId()], 'addressee' => $address->getFirstname() . ' ' . $address->getLastname(), 'city' => $address->getCity(), 'state' => $address->getRegion()?->getRegionCode(), 'zip' => $address->getPostcode(), 'attention' => '', 'addr1' => $streets[0], 'addr2' => $streets[1] ?? '', ] ]; } return $addressData; } private function prepareCustomerData(DataCustomer $customer) { $addressBooks = $this->getCustomerAddressData($customer); $data = array( 'email' => $customer->getEmail(), 'externalId' => 'Magento-'.$customer->getId(), 'accountNumber' => 'Magento-'.$customer->getId(), 'firstName' => $customer->getFirstName() , 'lastName' => $customer->getLastName(), 'phone' => $this->phone, 'addressBook' => ['items' => $addressBooks], "custentity_source" => 5, "leadsource" => -6, //子公司id "subsidiary" => ['id' => 3], "cseg_ncg_rep_office" => 13, "entitystatus" => 6, "isperson" => false, "custentity_credit_hold" => true ); if ($this->companyName) { $data['companyName'] = $this->companyName; } else { $data['companyName'] = 'From Magento'; } return $data; } /** * get custom attribute value * @param Customer $customer * @param string $attributeCode */ private function getCustomAttributeValue(Customer $customer, $attributeCode) { $attribute = $customer->getCustomAttribute($attributeCode); if ($attribute) { return $attribute->getValue(); } return null; } }