Files
ltsecurityinc/app/code/Lts/Customer/Cron/CustomerNetsuiteSync.php
T

178 lines
6.1 KiB
PHP

<?php
/**
* Copyright © All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Lts\Customer\Cron;
use JR\NetSuite\Lib\Restful\NetSuiteAPI;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Customer\Model\Customer;
use Magento\Customer\Model\Data\Customer as DataCustomer;
use Magento\Framework\Api\FilterBuilder;
use Magento\Framework\Api\Search\SearchCriteriaBuilder;
use Magento\Framework\Exception\LocalizedException;
use Psr\Log\LoggerInterface;
use JR\NetSuite\Logger\Logger;
use Magento\Customer\Model\ResourceModel\Customer\CollectionFactory as CustomerCollectionFactory;
class CustomerNetsuiteSync
{
const NETSUITE_SERVICE = 'record/v1/customer';
private Logger $logger;
private NetSuiteAPI $netSuiteAPI;
private CustomerRepositoryInterface $customerRepository;
private FilterBuilder $filterBuilder;
private SearchCriteriaBuilder $searchCriteriaBuilder;
private string|null $phone = '';
private string|null $companyName = '';
private CustomerCollectionFactory $customerCollectionFactory;
/**
* Constructor
*
* @param LoggerInterface $logger
*/
public function __construct(
Logger $logger,
NetSuiteAPI $netSuiteAPI,
CustomerRepositoryInterface $customerRepository,
FilterBuilder $filterBuilder,
SearchCriteriaBuilder $searchCriteriaBuilder,
CustomerCollectionFactory $customerCollectionFactory,
) {
$this->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;
}
}