Files
ltsecurityinc/app/code/Lts/Customer/Console/Command/AddInventorySourceCustomer.php
T

99 lines
4.0 KiB
PHP

<?php
namespace Lts\Customer\Console\Command;
use Lts\Customer\Cron\CustomerNetsuiteSync;
use Lts\Inventory\Api\SourceCustomerRepositoryInterface;
use Lts\Inventory\Api\Data\SourceCustomerInterfaceFactory;
use Magento\Customer\Model\ResourceModel\Customer\Collection;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\InventoryApi\Api\Data\SourceInterface;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Magento\Inventory\Model\ResourceModel\Source;
use Magento\InventoryApi\Api\Data\SourceInterfaceFactory;
class AddInventorySourceCustomer extends \Symfony\Component\Console\Command\Command
{
private Collection $customerCollection;
private SourceCustomerRepositoryInterface $sourceCustomerRepository;
private SourceInterfaceFactory $sourceFactory;
private Source $sourceModel;
const DEFAULT_SOURCE = 'CA-LA';
/**
* @var SourceInterface[]
*/
private array $sources;
private SourceCustomerInterfaceFactory $customerInterfaceFactory;
private CustomerNetsuiteSync $sourceNetsuiteSync;
public function __construct(
Collection $customerCollection,
SourceCustomerRepositoryInterface $sourceCustomerRepository,
SourceInterfaceFactory $sourceFactory,
Source $sourceModel,
SourceCustomerInterfaceFactory $customerInterfaceFactory,
CustomerNetsuiteSync $sourceNetsuiteSync
) {
$this->customerCollection = $customerCollection;
$this->sourceCustomerRepository = $sourceCustomerRepository;
$this->sourceFactory = $sourceFactory;
$this->sourceModel = $sourceModel;
$this->customerInterfaceFactory = $customerInterfaceFactory;
$this->sourceNetsuiteSync = $sourceNetsuiteSync;
parent::__construct();
}
protected function configure()
{
$this->setName('customer:source:sync');
$this->setDescription('Add customer init data to inventory_source_customer table');
}
/**
* @param string $sourceName
* @return SourceInterface
*/
protected function getSourceByName(string $sourceName)
{
$sourceName = trim($sourceName);
if (isset($this->sources[$sourceName]) && $this->sources[$sourceName]->getSourceCode()) {
return $this->sources[$sourceName];
}
$source = $this->sourceFactory->create();
$this->sourceModel->load($source, $sourceName, 'name');
$this->sources[$sourceName] = $source;
return $this->sources[$sourceName];
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->customerCollection->load();
foreach ($this->customerCollection as $customer) {
/* @var $customer \Magento\Customer\Model\Customer */
$attribute = $customer->getAttribute('lts_office');
$name = $attribute->getSource()->getOptionText($customer->getLtsOffice());
try {
try {
$this->sourceCustomerRepository->getFromCustomerId($customer->getId());
$output->writeln(sprintf("customer %s, source name %s already exists in customer source.", $customer->getEmail(), $name));
} catch (NoSuchEntityException $e) {
$source = $this->getSourceByName($name);
$sourceCustomer = $this->customerInterfaceFactory->create();
$sourceCustomer->setCustomerId($customer->getId())->setSourceCode($source->getSourceCode()?? self::DEFAULT_SOURCE);
$this->sourceCustomerRepository->save($sourceCustomer);
$output->writeln(sprintf("customer %s, source name %s sync success.", $customer->getEmail(), $name));
}
} catch (\Exception $e) {
$output->writeln(sprintf("customer source sync fail with customer %s, source name %s . Exception message %s", $customer->getEmail(), $name, $e->getMessage()));
}
}
return 0;
}
}