189 lines
8.1 KiB
PHP
189 lines
8.1 KiB
PHP
<?php
|
|
|
|
namespace JR\Customer\Console\Command;
|
|
|
|
use Lts\Customer\Cron\CustomerNetsuiteSync;
|
|
use JR\NetSuite\Logger\Logger;
|
|
use Lts\Customer\Model\Netsuite\Status;
|
|
use Lts\Inventory\Api\Data\SourceCustomerInterfaceFactory;
|
|
use Lts\Inventory\Api\SourceCustomerRepositoryInterface;
|
|
use Lts\Inventory\Model\SourceCustomer;
|
|
use Lts\Sales\Cron\OrderNetsuiteSync;
|
|
use Magento\Catalog\Api\ProductRepositoryInterface;
|
|
use Magento\Customer\Api\CustomerRepositoryInterface;
|
|
use Magento\Framework\Exception\NoSuchEntityException;
|
|
use Magento\InventoryApi\Api\Data\SourceInterface;
|
|
use Magento\InventoryApi\Api\SourceRepositoryInterface;
|
|
use Magento\Inventory\Model\SourceItem;
|
|
use Magento\Framework\Exception\LocalizedException;
|
|
use Magento\Framework\File\Csv;
|
|
use Magento\InventoryApi\Api\Data\SourceItemInterface;
|
|
use Magento\InventoryApi\Api\SourceItemRepositoryInterface;
|
|
use Magento\InventoryDistanceBasedSourceSelection\Model\DistanceProvider\GetDistanceFromSourceToAddress;
|
|
use Magento\InventorySourceSelectionApi\Api\Data\AddressInterfaceFactory;
|
|
use Magento\Sales\Api\OrderRepositoryInterface;
|
|
use Magento\Sales\Model\Order;
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
use Magento\Customer\Model\ResourceModel\Customer\Collection;
|
|
use Magento\Framework\Module\Dir;
|
|
use Magento\Catalog\Model\ResourceModel\Product as ProductResource;
|
|
|
|
class TestCommand extends \Symfony\Component\Console\Command\Command
|
|
{
|
|
private \Magento\Framework\Event\ManagerInterface $_eventManager;
|
|
private CustomerRepositoryInterface $customerRepository;
|
|
private SourceCustomer $sourceCustomer;
|
|
private SourceCustomerInterfaceFactory $customerInterfaceFactory;
|
|
private SourceCustomerRepositoryInterface $sourceCustomerRepository;
|
|
private SourceCustomerInterfaceFactory $sourceCustomerFactory;
|
|
private Collection $customerCollection;
|
|
private \Magento\Eav\Api\Data\AttributeOptionInterfaceFactory $attributeOption;
|
|
private Logger $logger;
|
|
private OrderNetsuiteSync $netSuiteSync;
|
|
private Status $netSuiteStatus;
|
|
private SourceItemRepositoryInterface $sourceItemRepository;
|
|
private \Magento\Framework\Api\SearchCriteriaBuilderFactory $searchCriteriaBuilderFactory;
|
|
private GetDistanceFromSourceToAddress $getDistanceFromSourceToAddress;
|
|
private AddressInterfaceFactory $addressInterfaceFactory;
|
|
private OrderRepositoryInterface $orderRepository;
|
|
private SourceRepositoryInterface $sourceRepository;
|
|
private Csv $csv;
|
|
private Dir $dir;
|
|
private ProductRepositoryInterface $productRepository;
|
|
private $orderCollection;
|
|
private ProductResource $productResource;
|
|
|
|
public function __construct(
|
|
\Magento\Framework\Event\ManagerInterface $eventManager,
|
|
CustomerRepositoryInterface $customerRepository,
|
|
SourceCustomerInterfaceFactory $sourceCustomerFactory,
|
|
SourceCustomerRepositoryInterface $sourceCustomerRepository,
|
|
Collection $customerCollection,
|
|
\Magento\Eav\Api\Data\AttributeOptionInterfaceFactory $attributeOption,
|
|
Logger $logger,
|
|
OrderNetsuiteSync $netSuiteSync,
|
|
SourceItemRepositoryInterface $sourceItemRepository,
|
|
\Magento\Framework\Api\SearchCriteriaBuilderFactory $searchCriteriaBuilderFactory,
|
|
GetDistanceFromSourceToAddress $getDistanceFromSourceToAddress,
|
|
AddressInterfaceFactory $addressInterfaceFactory,
|
|
OrderRepositoryInterface $orderRepository,
|
|
SourceRepositoryInterface $sourceRepository,
|
|
Csv $csv,
|
|
Dir $dir,
|
|
ProductRepositoryInterface $productRepository,
|
|
Collection $orderCollection,
|
|
ProductResource $productResource,
|
|
) {
|
|
$this->_eventManager = $eventManager;
|
|
$this->sourceCustomerFactory = $sourceCustomerFactory;
|
|
$this->customerCollection = $customerCollection;
|
|
$this->sourceCustomerRepository = $sourceCustomerRepository;
|
|
$this->attributeOption = $attributeOption;
|
|
$this->logger = $logger;
|
|
$this->netSuiteSync = $netSuiteSync;
|
|
$this->sourceItemRepository = $sourceItemRepository;
|
|
$this->searchCriteriaBuilderFactory = $searchCriteriaBuilderFactory;
|
|
$this->getDistanceFromSourceToAddress = $getDistanceFromSourceToAddress;
|
|
$this->addressInterfaceFactory = $addressInterfaceFactory;
|
|
$this->orderRepository = $orderRepository;
|
|
$this->sourceRepository = $sourceRepository;
|
|
$this->csv = $csv;
|
|
$this->dir = $dir;
|
|
$this->productRepository = $productRepository;
|
|
$this->orderCollection = $orderCollection;
|
|
$this->productResource = $productResource;
|
|
parent::__construct();
|
|
}
|
|
|
|
protected function configure()
|
|
{
|
|
$this->setName('customer:code:test');
|
|
}
|
|
|
|
protected function importProductNetsuiteId()
|
|
{
|
|
$path = $this->dir->getDir('Lts_Sales').'/Data/netsuite-product.csv';
|
|
if (!file_exists($path)) {
|
|
echo sprintf('File %s does not exist', $path).PHP_EOL;
|
|
}
|
|
echo 'path: ' . $path."\r\n";
|
|
$netsuiteAttributeCode = 'netsuite_id';
|
|
try {
|
|
foreach ($this->csv->getData($path) as $index =>$item) {
|
|
|
|
if ($index === 0) {
|
|
continue;
|
|
}
|
|
echo sprintf('row %d, sku: %s, netsuite id: %s', $index, $item[1], $item[0]).PHP_EOL;
|
|
try {
|
|
$product = $this->productRepository->get($item[1]);
|
|
|
|
$product->setData($netsuiteAttributeCode, $item[0]);
|
|
|
|
// 保存产品(使用资源模型直接保存属性,避免完全保存产品)
|
|
$this->productResource->saveAttribute($product, $netsuiteAttributeCode);
|
|
echo sprintf('update product %s netsuite id %s success', $item[1], $item[0])."\r\n";
|
|
} catch (NoSuchEntityException $e) {
|
|
echo sprintf('update product %s netsuite id failed: %s', $item[1], $e->getMessage())."\r\n";
|
|
}
|
|
}
|
|
} catch (\Exception $exception){
|
|
echo sprintf('update product %s netsuite id %s success', $item[1], $item[0])."\r\n";
|
|
}
|
|
}
|
|
/**
|
|
* @param \Magento\Sales\Api\Data\OrderAddressInterface $address
|
|
* @return \Magento\InventorySourceSelectionApi\Api\Data\AddressInterface
|
|
*/
|
|
protected function addressConverter(\Magento\Sales\Api\Data\OrderAddressInterface $address) {
|
|
if ($address->getStreet()) {
|
|
$streets = implode(' ', $address->getStreet());
|
|
}
|
|
return $this->addressInterfaceFactory->create([
|
|
'country' => $address->getCountryId() ?? '',
|
|
'postcode' => $address->getPostcode() ?? '',
|
|
'street' => $streets ?? '',
|
|
'region' => $address->getRegion() ?? '',
|
|
'city' => $address->getCity() ?? ''
|
|
]);
|
|
}
|
|
|
|
protected function getInventorySourceFromProduct(string $sku)
|
|
{
|
|
$searchCriteria = $this->searchCriteriaBuilderFactory->create()
|
|
->addFilter(SourceItemInterface::SKU, $sku)
|
|
->addFilter(SourceItemInterface::SOURCE_CODE, 'default', 'neq')
|
|
->create();
|
|
|
|
return $this->sourceItemRepository->getList($searchCriteria)->getItems();
|
|
}
|
|
|
|
protected function getWaitSyncOrders()
|
|
{
|
|
$criteria = $this->searchCriteriaBuilderFactory->create()
|
|
->addFilter('state', Order::STATE_NEW)
|
|
->addFilter('is_netsuite_sync', 0)
|
|
->create();
|
|
return $this->orderRepository->getList($criteria);
|
|
}
|
|
|
|
/**
|
|
* @throws LocalizedException
|
|
*/
|
|
protected function execute(InputInterface $input, OutputInterface $output)
|
|
{
|
|
// $this->importProductNetsuiteId();
|
|
|
|
$this->netSuiteSync->execute();
|
|
|
|
// $waitSyncOrders = $this->getWaitSyncOrders();
|
|
// foreach ($waitSyncOrders->getItems() as $order) {
|
|
// /* @var $order \Magento\Sales\Model\Order */
|
|
// echo $order->getEntityId().PHP_EOL;
|
|
//// $order->setId($order->getId())->getExtensionAttributes()->setIsNetsuiteSync(1);
|
|
//// $this->orderRepository->save($order);
|
|
// }
|
|
return 1;
|
|
}
|
|
} |