init
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
namespace Stemcell\Customer\Controller\Adminhtml\Lab;
|
||||
|
||||
use Magento\Backend\App\Action\Context;
|
||||
use Magento\Framework\App\Action\HttpGetActionInterface;
|
||||
use Magento\Framework\Controller\ResultInterface;
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Magento\Framework\Exception\NoSuchEntityException;
|
||||
use Magento\Framework\View\Result\PageFactory;
|
||||
use Stemcell\Customer\Api\LabRepositoryInterface;
|
||||
use Stemcell\Customer\Model\SalespersonRepository;
|
||||
|
||||
class Edit extends \Magento\Backend\App\Action implements HttpGetActionInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @var PageFactory
|
||||
*/
|
||||
protected PageFactory $resultPageFactory;
|
||||
protected LabRepositoryInterface $repository;
|
||||
|
||||
public function __construct(
|
||||
Context $context,
|
||||
PageFactory $resultPageFactory,
|
||||
LabRepositoryInterface $repository
|
||||
) {
|
||||
parent::__construct($context);
|
||||
$this->resultPageFactory = $resultPageFactory;
|
||||
$this->repository = $repository;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute view action
|
||||
*
|
||||
* @return ResultInterface
|
||||
* @throws LocalizedException
|
||||
*/
|
||||
public function execute()
|
||||
{
|
||||
try {
|
||||
$salesperson = $this->repository->get($this->getRequest()->getParam('id'))->getData();
|
||||
} catch (NoSuchEntityException $e) {
|
||||
$salesperson = [];
|
||||
}
|
||||
$this->_getSession()->setLab($salesperson);
|
||||
return $this->resultPageFactory->create();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace Stemcell\Customer\Controller\Adminhtml\Lab;
|
||||
|
||||
use Magento\Backend\App\Action\Context;
|
||||
use Magento\Framework\App\Action\HttpGetActionInterface;
|
||||
use Magento\Framework\App\Action\HttpPostActionInterface;
|
||||
use Magento\Framework\App\Response\Http\FileFactory;
|
||||
use Magento\Ui\Component\MassAction\Filter;
|
||||
use Magento\Ui\Model\Export\ConvertToCsv;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class Export extends \Magento\Ui\Controller\Adminhtml\Export\GridToCsv
|
||||
{
|
||||
public function __construct(
|
||||
Context $context,
|
||||
ConvertToCsv $converter,
|
||||
FileFactory $fileFactory,
|
||||
protected \Stemcell\Customer\Model\Lab\Export $exportModel,
|
||||
Filter $filter = null,
|
||||
LoggerInterface $logger = null
|
||||
){
|
||||
parent::__construct($context, $converter, $fileFactory, $filter, $logger);
|
||||
}
|
||||
|
||||
public function execute()
|
||||
{
|
||||
return $this->fileFactory->create('Labs-'.date('YmdHis').'.xlsx', $this->exportModel->getExcelFile(), 'var');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
namespace Stemcell\Customer\Controller\Adminhtml\Lab;
|
||||
|
||||
use Magento\Backend\App\Action\Context;
|
||||
use Magento\Framework\App\Action\HttpGetActionInterface;
|
||||
use Magento\Framework\Controller\ResultInterface;
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Magento\Framework\Exception\NoSuchEntityException;
|
||||
use Magento\Framework\View\Result\PageFactory;
|
||||
use Stemcell\Customer\Model\SalespersonRepository;
|
||||
|
||||
class Import extends \Magento\Backend\App\Action implements HttpGetActionInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @var PageFactory
|
||||
*/
|
||||
protected PageFactory $resultPageFactory;
|
||||
protected SalespersonRepository $repository;
|
||||
protected \Magento\Backend\Model\View\Result\ForwardFactory $resultForwardFactory;
|
||||
|
||||
public function __construct(
|
||||
Context $context,
|
||||
PageFactory $resultPageFactory,
|
||||
\Magento\Backend\Model\View\Result\ForwardFactory $resultForwardFactory,
|
||||
SalespersonRepository $repository
|
||||
) {
|
||||
parent::__construct($context);
|
||||
$this->resultPageFactory = $resultPageFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute view action
|
||||
*
|
||||
* @return ResultInterface
|
||||
* @throws LocalizedException
|
||||
*/
|
||||
public function execute()
|
||||
{
|
||||
return $this->resultPageFactory->create();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,209 @@
|
||||
<?php
|
||||
|
||||
namespace Stemcell\Customer\Controller\Adminhtml\Lab;
|
||||
|
||||
use Magento\Backend\App\Action\Context;
|
||||
use Magento\Customer\Model\Customer;
|
||||
use Magento\Customer\Model\ResourceModel\Customer\CollectionFactory as CustomerCollectionFactory;
|
||||
use \PhpOffice\PhpSpreadsheet\IOFactory;
|
||||
use Magento\Framework\App\Action\HttpPostActionInterface;
|
||||
use Magento\Framework\Controller\ResultFactory;
|
||||
use Magento\Framework\Exception\FileSystemException;
|
||||
use Magento\Framework\Filesystem\Driver\File;
|
||||
use Magento\MediaStorage\Model\File\UploaderFactory;
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Magento\Framework\Filesystem;
|
||||
use Magento\Store\Model\StoreManagerInterface;
|
||||
use Magento\Framework\App\Filesystem\DirectoryList;
|
||||
use PhpOffice\PhpSpreadsheet\Reader\Exception;
|
||||
use Stemcell\Customer\Api\Data\LabInterface;
|
||||
use Stemcell\Customer\Api\LabRepositoryInterface;
|
||||
use Stemcell\Customer\Model\LabFactory;
|
||||
use Stemcell\Customer\Model\ResourceModel\Salesperson\CollectionFactory;
|
||||
use Stemcell\Customer\Model\Salesperson;
|
||||
|
||||
/**
|
||||
* 由于生产服务器是两台,只有media目录是两台服务器共享,因此将文件上传到media下面
|
||||
*/
|
||||
class ImportPost extends \Magento\Backend\App\Action implements HttpPostActionInterface
|
||||
{
|
||||
private UploaderFactory $fileUploaderFactory;
|
||||
private Filesystem\Directory\WriteInterface $mediaDirectory;
|
||||
private StoreManagerInterface $storeManager;
|
||||
private Filesystem $fileSystem;
|
||||
private File $driverFile;
|
||||
private Customer $customer;
|
||||
private LabRepositoryInterface $labRepository;
|
||||
private LabInterface $lab;
|
||||
private LabFactory $labFactory;
|
||||
private CustomerCollectionFactory $customerCollection;
|
||||
private CollectionFactory $salespersonCollection;
|
||||
|
||||
protected array $salespersons = [];
|
||||
|
||||
protected array $customers = [];
|
||||
/**
|
||||
* @throws FileSystemException
|
||||
*/
|
||||
public function __construct(
|
||||
Context $context,
|
||||
UploaderFactory $fileUploaderFactory,
|
||||
Filesystem $filesystem,
|
||||
StoreManagerInterface $storeManager,
|
||||
Filesystem $fileSystem,
|
||||
File $driverFile,
|
||||
CustomerCollectionFactory $customerCollection,
|
||||
LabRepositoryInterface $labRepository,
|
||||
LabFactory $labFactory,
|
||||
LabInterface $lab,
|
||||
CollectionFactory $salespersonCollection
|
||||
) {
|
||||
parent::__construct($context);
|
||||
$this->fileUploaderFactory = $fileUploaderFactory;
|
||||
$this->mediaDirectory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
|
||||
$this->storeManager = $storeManager;
|
||||
$this->fileSystem = $fileSystem;
|
||||
$this->driverFile = $driverFile;
|
||||
$this->customerCollection = $customerCollection;
|
||||
$this->labRepository = $labRepository;
|
||||
$this->labFactory = $labFactory;
|
||||
$this->lab = $lab;
|
||||
$this->salespersonCollection = $salespersonCollection;
|
||||
}
|
||||
|
||||
protected function chineseEncoding(string $str)
|
||||
{
|
||||
return mb_convert_encoding($str, 'UTF-8', ['gb2312', 'gbk']);
|
||||
}
|
||||
|
||||
|
||||
protected function getSalesPersonId(string $name)
|
||||
{
|
||||
$name = trim($name);
|
||||
if (isset($this->salespersons[$name])) {
|
||||
return $this->salespersons[$name];
|
||||
}
|
||||
|
||||
try {
|
||||
$result = $this->lab->getResource()->getConnection()
|
||||
->query("select salesperson_id from salesperson where name = '$name' limit 1")
|
||||
->fetch();
|
||||
if (is_array($result)) {
|
||||
$this->salespersons[$name] = $result['salesperson_id'];
|
||||
return $this->salespersons[$name];
|
||||
}
|
||||
} catch (\Zend_Db_Statement_Exception $e) {
|
||||
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
protected function getCustomerId(string $name)
|
||||
{
|
||||
$name = trim($name);
|
||||
if (isset($this->customers[$name])) {
|
||||
return $this->customers[$name];
|
||||
}
|
||||
|
||||
try {
|
||||
$result = $this->lab->getResource()->getConnection()
|
||||
->query("select entity_id from customer_entity where `firstname` = '$name' limit 1")
|
||||
->fetch();
|
||||
if (is_array($result)) {
|
||||
$this->customers[$name] = $result['entity_id'];
|
||||
return $this->customers[$name];
|
||||
}
|
||||
} catch (\Zend_Db_Statement_Exception $e) {
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function execute()
|
||||
{
|
||||
$jsonResult = $this->resultFactory->create(ResultFactory::TYPE_JSON);
|
||||
try {
|
||||
$fileUploader = $this->fileUploaderFactory->create([
|
||||
'fileId' => 'lab[import]'
|
||||
]);
|
||||
$fileUploader->setAllowedExtensions(['xls', 'xlsx']);
|
||||
$fileUploader->setAllowRenameFiles(true);
|
||||
$fileUploader->setAllowCreateFolders(true);
|
||||
$fileUploader->setFilesDispersion(false);
|
||||
|
||||
$dir = $this->mediaDirectory->getAbsolutePath(DirectoryList::VAR_IMPORT_EXPORT.'/lab');
|
||||
!is_dir($dir) && mkdir($dir, 0755, true);
|
||||
$data = $fileUploader->save($dir);
|
||||
/* @var $logger \Psr\Log\LoggerInterface */
|
||||
$logger = \Magento\Framework\App\ObjectManager::getInstance()->get(\Psr\Log\LoggerInterface::class);
|
||||
|
||||
try {
|
||||
$spreadsheet = IOFactory::load($dir.'/'.$fileUploader->getUploadedFileName());
|
||||
$data = $spreadsheet->getActiveSheet()->toArray();
|
||||
array_shift($data);
|
||||
foreach ($data as $content) {
|
||||
array_walk($content, function (&$value){
|
||||
if (is_string($value)) {
|
||||
$value = trim($value);
|
||||
}
|
||||
});
|
||||
if (empty(array_filter($content))) {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
$content = array_slice($content, 0, 5);
|
||||
$row = array_combine(['lab_code', 'booking_account', 'pi_sap_id', 'customer_name', 'salesperson'], $content);
|
||||
|
||||
$column = 'lab_code, booking_account, pi_sap_id';
|
||||
$duplicate = 'booking_account = new.booking_account, pi_sap_id = new.pi_sap_id';
|
||||
$row['salesperson_id'] = $row['customer_id']= null;
|
||||
if ($row['customer_name']) {
|
||||
$customerId = $this->getCustomerId((string)$row['customer_name']);
|
||||
if ($customerId) {
|
||||
$column .= ',customer_id';
|
||||
$duplicate .= ', customer_id = new.customer_id';
|
||||
$row['customer_id'] .= ', '. $customerId;
|
||||
}
|
||||
|
||||
unset($row['customer_name']);
|
||||
|
||||
}
|
||||
if ($row['salesperson']) {
|
||||
/* @var $salesperson Salesperson */
|
||||
$salespersonId = $this->getSalesPersonId((string)$row['salesperson']);
|
||||
|
||||
if ($salespersonId) {
|
||||
$column .= ',salesperson_id';
|
||||
$row['salesperson_id'] .= ', '.$salespersonId;
|
||||
$duplicate .= ', salesperson_id = new.salesperson_id';
|
||||
// $this->lab->setData($row);
|
||||
}
|
||||
}
|
||||
if ($row['lab_code']) {
|
||||
$sql = "
|
||||
INSERT INTO lab ($column)
|
||||
VALUES ('{$row['lab_code']}', '{$row['booking_account']}', '{$row['pi_sap_id']}' {$row['customer_id']} {$row['salesperson_id']}) AS new
|
||||
ON DUPLICATE KEY UPDATE
|
||||
$duplicate;
|
||||
";
|
||||
$this->labFactory->create()->getResource()->getConnection()->query($sql);
|
||||
}
|
||||
|
||||
$this->lab->unsetData();
|
||||
} catch (FileSystemException | \Exception $e) {
|
||||
$logger->critical('导入lab失败,原因:'.$e->getMessage(), ['data' => $row]);
|
||||
}
|
||||
}
|
||||
// $logger->info('$this->customers', $this->customers);
|
||||
// $logger->info('$this->salespersons', $this->salespersons);
|
||||
$this->driverFile->deleteFile($dir.'/'.$fileUploader->getUploadedFileName());
|
||||
} catch (FileSystemException | Exception $exception) {
|
||||
$logger->debug($exception->getMessage());
|
||||
}
|
||||
return $jsonResult->setData($data);
|
||||
} catch (LocalizedException|Exception $error) {
|
||||
return $jsonResult->setData(['errorcode' => 0, 'error' => $error->getMessage()]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
namespace Stemcell\Customer\Controller\Adminhtml\Lab;
|
||||
|
||||
use Magento\Framework\App\Action\HttpGetActionInterface;
|
||||
use Magento\Framework\Controller\ResultInterface;
|
||||
use Magento\Framework\View\Result\PageFactory;
|
||||
|
||||
class Index implements HttpGetActionInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @var PageFactory
|
||||
*/
|
||||
protected $resultPageFactory;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param PageFactory $resultPageFactory
|
||||
*/
|
||||
public function __construct(PageFactory $resultPageFactory)
|
||||
{
|
||||
$this->resultPageFactory = $resultPageFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute view action
|
||||
*
|
||||
* @return ResultInterface
|
||||
*/
|
||||
public function execute()
|
||||
{
|
||||
return $this->resultPageFactory->create();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stemcell\Customer\Controller\Adminhtml\Lab;
|
||||
|
||||
use Magento\Backend\App\Action;
|
||||
use Magento\Backend\App\Action\Context;
|
||||
use Magento\Framework\App\Action\HttpPostActionInterface;
|
||||
use Magento\Framework\Controller\Result\Redirect;
|
||||
use Magento\Ui\Component\MassAction\Filter;
|
||||
use Stemcell\Customer\Api\LabRepositoryInterface;
|
||||
use Stemcell\Customer\Model\ResourceModel\Lab\CollectionFactory;
|
||||
|
||||
class MassDelete extends Action implements HttpPostActionInterface
|
||||
{
|
||||
public const ADMIN_RESOURCE = 'Stemcell_Customer::salesperson_index';
|
||||
|
||||
private Filter $filter;
|
||||
|
||||
private CollectionFactory $collectionFactory;
|
||||
|
||||
private LabRepositoryInterface $labRepository;
|
||||
|
||||
public function __construct(
|
||||
Context $context,
|
||||
Filter $filter,
|
||||
CollectionFactory $collectionFactory,
|
||||
LabRepositoryInterface $labRepository
|
||||
) {
|
||||
parent::__construct($context);
|
||||
$this->filter = $filter;
|
||||
$this->collectionFactory = $collectionFactory;
|
||||
$this->labRepository = $labRepository;
|
||||
}
|
||||
|
||||
public function execute(): Redirect
|
||||
{
|
||||
$collection = $this->filter->getCollection($this->collectionFactory->create());
|
||||
$deleted = 0;
|
||||
|
||||
foreach ($collection as $lab) {
|
||||
$this->labRepository->delete($lab);
|
||||
++$deleted;
|
||||
}
|
||||
|
||||
if ($deleted) {
|
||||
$this->messageManager->addSuccessMessage(
|
||||
__('A total of %1 lab record(s) have been deleted.', $deleted)
|
||||
);
|
||||
}
|
||||
|
||||
return $this->resultRedirectFactory->create()->setPath('stemcell_customer/lab/index');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
namespace Stemcell\Customer\Controller\Adminhtml\Lab;
|
||||
|
||||
use Magento\Backend\App\Action\Context;
|
||||
use Magento\Framework\App\Action\HttpGetActionInterface;
|
||||
use Magento\Framework\Controller\ResultInterface;
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Magento\Framework\Exception\NoSuchEntityException;
|
||||
use Magento\Framework\View\Result\PageFactory;
|
||||
use Stemcell\Customer\Model\SalespersonRepository;
|
||||
|
||||
class NewAction extends \Magento\Backend\App\Action implements HttpGetActionInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @var PageFactory
|
||||
*/
|
||||
protected PageFactory $resultPageFactory;
|
||||
protected SalespersonRepository $repository;
|
||||
protected \Magento\Backend\Model\View\Result\ForwardFactory $resultForwardFactory;
|
||||
|
||||
public function __construct(
|
||||
Context $context,
|
||||
PageFactory $resultPageFactory,
|
||||
\Magento\Backend\Model\View\Result\ForwardFactory $resultForwardFactory,
|
||||
SalespersonRepository $repository
|
||||
) {
|
||||
parent::__construct($context);
|
||||
$this->resultForwardFactory = $resultForwardFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute view action
|
||||
*
|
||||
* @return ResultInterface
|
||||
* @throws LocalizedException
|
||||
*/
|
||||
public function execute()
|
||||
{
|
||||
$resultForward = $this->resultForwardFactory->create();
|
||||
$resultForward->forward('edit');
|
||||
return $resultForward;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace Stemcell\Customer\Controller\Adminhtml\Lab;
|
||||
|
||||
use Magento\Framework\App\Action\HttpGetActionInterface;
|
||||
|
||||
class Sample implements HttpGetActionInterface
|
||||
{
|
||||
protected \Magento\Framework\App\Response\Http\FileFactory $fileFactory;
|
||||
private \Magento\Framework\Module\Dir $moduleDir;
|
||||
|
||||
public function __construct(
|
||||
\Magento\Framework\App\Response\Http\FileFactory $fileFactory,
|
||||
\Magento\Framework\Module\Dir $moduleDir
|
||||
) {
|
||||
$this->fileFactory = $fileFactory;
|
||||
$this->moduleDir = $moduleDir;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function execute()
|
||||
{
|
||||
$file = $this->moduleDir->getDir('Stemcell_Customer').'/data/Sample.xlsx';
|
||||
$content['type'] = 'filename';
|
||||
$content['value'] = $file;
|
||||
$content['rm'] = 0;
|
||||
return $this->fileFactory->create('Sample.xlsx', $content);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
namespace Stemcell\Customer\Controller\Adminhtml\Lab;
|
||||
|
||||
use Magento\Backend\App\Action\Context;
|
||||
use Magento\Framework\App\Action\HttpPostActionInterface;
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Stemcell\Customer\Api\Data\LabInterface;
|
||||
use Stemcell\Customer\Api\Data\SalespersonInterface;
|
||||
use Stemcell\Customer\Api\LabRepositoryInterface;
|
||||
use Stemcell\Customer\Api\SalespersonRepositoryInterface;
|
||||
|
||||
class Save extends \Magento\Backend\App\Action implements HttpPostActionInterface
|
||||
{
|
||||
protected LabInterface $salesperson;
|
||||
protected LabRepositoryInterface $salespersonRepository;
|
||||
|
||||
public function __construct(
|
||||
Context $context,
|
||||
LabInterface $salesperson,
|
||||
LabRepositoryInterface $salespersonRepository
|
||||
) {
|
||||
parent::__construct($context);
|
||||
$this->salesperson = $salesperson;
|
||||
$this->salespersonRepository = $salespersonRepository;
|
||||
}
|
||||
|
||||
public function execute()
|
||||
{
|
||||
$returnToEdit = false;
|
||||
$salesperson = $this->getRequest()->getParam('lab');
|
||||
$this->salesperson->setData($salesperson);
|
||||
try {
|
||||
$this->salespersonRepository->save($this->salesperson);
|
||||
$this->messageManager->addSuccessMessage(__('Lab Data saved.'));
|
||||
} catch (\Exception $e) {
|
||||
$this->messageManager->addExceptionMessage($e);
|
||||
$this->_getSession()->setLab($salesperson);
|
||||
$returnToEdit = true;
|
||||
}
|
||||
|
||||
$resultRedirect = $this->resultRedirectFactory->create();
|
||||
if ($returnToEdit) {
|
||||
if (isset($salesperson['salesperson_id'])) {
|
||||
$resultRedirect->setPath(
|
||||
'stemcell_customer/lab/edit',
|
||||
['id' => $salesperson['salesperson_id'], '_current' => true]
|
||||
);
|
||||
} else {
|
||||
$resultRedirect->setPath(
|
||||
'stemcell_customer/lab/new',
|
||||
['_current' => true]
|
||||
);
|
||||
}
|
||||
} else {
|
||||
$resultRedirect->setPath('stemcell_customer/lab/index');
|
||||
}
|
||||
|
||||
|
||||
return $resultRedirect;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
namespace Stemcell\Customer\Controller\Adminhtml\Salesperson;
|
||||
|
||||
use Magento\Backend\App\Action\Context;
|
||||
use Magento\Framework\App\Action\HttpGetActionInterface;
|
||||
use Magento\Framework\Controller\ResultInterface;
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Magento\Framework\Exception\NoSuchEntityException;
|
||||
use Magento\Framework\View\Result\PageFactory;
|
||||
use Stemcell\Customer\Model\SalespersonRepository;
|
||||
|
||||
class Edit extends \Magento\Backend\App\Action implements HttpGetActionInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @var PageFactory
|
||||
*/
|
||||
protected PageFactory $resultPageFactory;
|
||||
protected SalespersonRepository $repository;
|
||||
|
||||
public function __construct(
|
||||
Context $context,
|
||||
PageFactory $resultPageFactory,
|
||||
SalespersonRepository $repository
|
||||
) {
|
||||
parent::__construct($context);
|
||||
$this->resultPageFactory = $resultPageFactory;
|
||||
$this->repository = $repository;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute view action
|
||||
*
|
||||
* @return ResultInterface
|
||||
* @throws LocalizedException
|
||||
*/
|
||||
public function execute()
|
||||
{
|
||||
try {
|
||||
$salesperson = $this->repository->get($this->getRequest()->getParam('id'))->getData();
|
||||
} catch (NoSuchEntityException $e) {
|
||||
$salesperson = [];
|
||||
}
|
||||
$this->_getSession()->setSalesperson($salesperson);
|
||||
return $this->resultPageFactory->create();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
namespace Stemcell\Customer\Controller\Adminhtml\Salesperson;
|
||||
|
||||
use Magento\Backend\App\Action\Context;
|
||||
use Magento\Framework\App\Action\HttpGetActionInterface;
|
||||
use Magento\Framework\Controller\ResultInterface;
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Magento\Framework\Exception\NoSuchEntityException;
|
||||
use Magento\Framework\View\Result\PageFactory;
|
||||
use Stemcell\Customer\Model\SalespersonRepository;
|
||||
|
||||
class Import extends \Magento\Backend\App\Action implements HttpGetActionInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @var PageFactory
|
||||
*/
|
||||
protected PageFactory $resultPageFactory;
|
||||
protected SalespersonRepository $repository;
|
||||
protected \Magento\Backend\Model\View\Result\ForwardFactory $resultForwardFactory;
|
||||
|
||||
public function __construct(
|
||||
Context $context,
|
||||
PageFactory $resultPageFactory,
|
||||
\Magento\Backend\Model\View\Result\ForwardFactory $resultForwardFactory,
|
||||
SalespersonRepository $repository
|
||||
) {
|
||||
parent::__construct($context);
|
||||
$this->resultPageFactory = $resultPageFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute view action
|
||||
*
|
||||
* @return ResultInterface
|
||||
* @throws LocalizedException
|
||||
*/
|
||||
public function execute()
|
||||
{
|
||||
return $this->resultPageFactory->create();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
<?php
|
||||
|
||||
namespace Stemcell\Customer\Controller\Adminhtml\Salesperson;
|
||||
|
||||
use Magento\Backend\App\Action\Context;
|
||||
use Magento\Customer\Model\Customer;
|
||||
use Magento\Customer\Model\ResourceModel\Customer\CollectionFactory as CustomerCollectionFactory;
|
||||
use Magento\Framework\App\Action\HttpPostActionInterface;
|
||||
use Magento\Framework\Controller\ResultFactory;
|
||||
use Magento\Framework\Exception\FileSystemException;
|
||||
use Magento\Framework\Filesystem\Driver\File;
|
||||
use Magento\MediaStorage\Model\File\UploaderFactory;
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Magento\Framework\Filesystem;
|
||||
use Magento\Store\Model\StoreManagerInterface;
|
||||
use Magento\Framework\App\Filesystem\DirectoryList;
|
||||
use PhpOffice\PhpSpreadsheet\IOFactory;
|
||||
use PhpOffice\PhpSpreadsheet\Reader\Exception;
|
||||
use Stemcell\Customer\Api\Data\SalespersonInterface;
|
||||
use Stemcell\Customer\Api\SalespersonRepositoryInterface;
|
||||
|
||||
/**
|
||||
* 由于生产服务器是两台,只有media目录是两台服务器共享,因此将文件上传到media下面
|
||||
*/
|
||||
class ImportPost extends \Magento\Backend\App\Action implements HttpPostActionInterface
|
||||
{
|
||||
private UploaderFactory $fileUploaderFactory;
|
||||
private Filesystem\Directory\WriteInterface $mediaDirectory;
|
||||
private StoreManagerInterface $storeManager;
|
||||
private Filesystem $fileSystem;
|
||||
private File $driverFile;
|
||||
private CustomerCollectionFactory $customerCollection;
|
||||
private SalespersonInterface $salesperson;
|
||||
private SalespersonRepositoryInterface $salespersonRepository;
|
||||
|
||||
/**
|
||||
* @throws FileSystemException
|
||||
*/
|
||||
public function __construct(
|
||||
Context $context,
|
||||
UploaderFactory $fileUploaderFactory,
|
||||
Filesystem $filesystem,
|
||||
StoreManagerInterface $storeManager,
|
||||
Filesystem $fileSystem,
|
||||
File $driverFile,
|
||||
CustomerCollectionFactory $customerCollection,
|
||||
SalespersonRepositoryInterface $salespersonRepository,
|
||||
SalespersonInterface $salesperson
|
||||
) {
|
||||
parent::__construct($context);
|
||||
$this->fileUploaderFactory = $fileUploaderFactory;
|
||||
$this->mediaDirectory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
|
||||
$this->storeManager = $storeManager;
|
||||
$this->fileSystem = $fileSystem;
|
||||
$this->driverFile = $driverFile;
|
||||
$this->customerCollection = $customerCollection;
|
||||
$this->salespersonRepository = $salespersonRepository;
|
||||
$this->salesperson = $salesperson;
|
||||
}
|
||||
|
||||
public function execute()
|
||||
{
|
||||
$jsonResult = $this->resultFactory->create(ResultFactory::TYPE_JSON);
|
||||
try {
|
||||
$fileUploader = $this->fileUploaderFactory->create([
|
||||
'fileId' => 'salesperson[import]'
|
||||
]);
|
||||
$fileUploader->setAllowedExtensions(['xls', 'xlsx']);
|
||||
$fileUploader->setAllowRenameFiles(true);
|
||||
$fileUploader->setAllowCreateFolders(true);
|
||||
$fileUploader->setFilesDispersion(false);
|
||||
|
||||
$dir = $this->mediaDirectory->getAbsolutePath(DirectoryList::VAR_IMPORT_EXPORT.'/salesperson');
|
||||
!is_dir($dir) && mkdir($dir, 0755, true);
|
||||
$data = $fileUploader->save($dir);
|
||||
$resource = $this->driverFile->fileOpen($dir.'/'.$fileUploader->getUploadedFileName(), 'r');
|
||||
|
||||
/* @var $logger \Psr\Log\LoggerInterface */
|
||||
$logger = \Magento\Framework\App\ObjectManager::getInstance()->get(\Psr\Log\LoggerInterface::class);
|
||||
|
||||
try {
|
||||
$spreadsheet = IOFactory::load($dir.'/'.$fileUploader->getUploadedFileName());
|
||||
$data = $spreadsheet->getActiveSheet()->toArray();
|
||||
array_shift($data);
|
||||
|
||||
foreach ($data as $content) {
|
||||
|
||||
try {
|
||||
array_walk($content, function (&$value) {
|
||||
if (is_string($value)) {
|
||||
$value = trim($value);
|
||||
}
|
||||
});
|
||||
if (empty(array_filter($content))) {
|
||||
continue;
|
||||
}
|
||||
$content = array_slice($content, 0, 5);
|
||||
$row = array_combine(
|
||||
['name', 'email', 'telephone', 'rsm_email', 'sisr_email', 'csr_email'],
|
||||
$content
|
||||
);
|
||||
$this->salesperson->setData($row);
|
||||
$this->salespersonRepository->save($this->salesperson);
|
||||
$this->salesperson->unsetData();
|
||||
} catch (FileSystemException | \Exception $e) {
|
||||
$logger->critical('导入销售失败,原因:'.$e->getMessage(), ['data' => $row]);
|
||||
}
|
||||
|
||||
}
|
||||
$this->driverFile->fileClose($resource);
|
||||
$this->driverFile->deleteFile($dir.'/'.$fileUploader->getUploadedFileName());
|
||||
} catch (FileSystemException $exception) {
|
||||
$logger->debug($exception->getMessage());
|
||||
}
|
||||
return $jsonResult->setData($data);
|
||||
} catch (LocalizedException | \Exception $error) {
|
||||
return $jsonResult->setData(['errorcode' => 0, 'error' => $error->getMessage()]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
namespace Stemcell\Customer\Controller\Adminhtml\Salesperson;
|
||||
|
||||
use Magento\Framework\App\Action\HttpGetActionInterface;
|
||||
use Magento\Framework\Controller\ResultInterface;
|
||||
use Magento\Framework\View\Result\PageFactory;
|
||||
|
||||
class Index implements HttpGetActionInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @var PageFactory
|
||||
*/
|
||||
protected $resultPageFactory;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param PageFactory $resultPageFactory
|
||||
*/
|
||||
public function __construct(PageFactory $resultPageFactory)
|
||||
{
|
||||
$this->resultPageFactory = $resultPageFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute view action
|
||||
*
|
||||
* @return ResultInterface
|
||||
*/
|
||||
public function execute()
|
||||
{
|
||||
return $this->resultPageFactory->create();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stemcell\Customer\Controller\Adminhtml\Salesperson;
|
||||
|
||||
use Magento\Backend\App\Action;
|
||||
use Magento\Backend\App\Action\Context;
|
||||
use Magento\Framework\App\Action\HttpPostActionInterface;
|
||||
use Magento\Framework\Controller\Result\Redirect;
|
||||
use Magento\Ui\Component\MassAction\Filter;
|
||||
use Stemcell\Customer\Api\SalespersonRepositoryInterface;
|
||||
use Stemcell\Customer\Model\ResourceModel\Salesperson\CollectionFactory;
|
||||
|
||||
class MassDelete extends Action implements HttpPostActionInterface
|
||||
{
|
||||
public const ADMIN_RESOURCE = 'Stemcell_Customer::salesperson_index';
|
||||
|
||||
private Filter $filter;
|
||||
|
||||
private CollectionFactory $collectionFactory;
|
||||
|
||||
private SalespersonRepositoryInterface $salespersonRepository;
|
||||
|
||||
public function __construct(
|
||||
Context $context,
|
||||
Filter $filter,
|
||||
CollectionFactory $collectionFactory,
|
||||
SalespersonRepositoryInterface $salespersonRepository
|
||||
) {
|
||||
parent::__construct($context);
|
||||
$this->filter = $filter;
|
||||
$this->collectionFactory = $collectionFactory;
|
||||
$this->salespersonRepository = $salespersonRepository;
|
||||
}
|
||||
|
||||
public function execute(): Redirect
|
||||
{
|
||||
$collection = $this->filter->getCollection($this->collectionFactory->create());
|
||||
$deleted = 0;
|
||||
|
||||
foreach ($collection as $salesperson) {
|
||||
$this->salespersonRepository->delete($salesperson);
|
||||
++$deleted;
|
||||
}
|
||||
|
||||
if ($deleted) {
|
||||
$this->messageManager->addSuccessMessage(
|
||||
__('A total of %1 salesperson record(s) have been deleted.', $deleted)
|
||||
);
|
||||
}
|
||||
|
||||
return $this->resultRedirectFactory->create()->setPath('stemcell_customer/salesperson/index');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
namespace Stemcell\Customer\Controller\Adminhtml\Salesperson;
|
||||
|
||||
use Magento\Backend\App\Action\Context;
|
||||
use Magento\Framework\App\Action\HttpGetActionInterface;
|
||||
use Magento\Framework\Controller\ResultInterface;
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Magento\Framework\Exception\NoSuchEntityException;
|
||||
use Magento\Framework\View\Result\PageFactory;
|
||||
use Stemcell\Customer\Model\SalespersonRepository;
|
||||
|
||||
class NewAction extends \Magento\Backend\App\Action implements HttpGetActionInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @var PageFactory
|
||||
*/
|
||||
protected PageFactory $resultPageFactory;
|
||||
protected SalespersonRepository $repository;
|
||||
protected \Magento\Backend\Model\View\Result\ForwardFactory $resultForwardFactory;
|
||||
|
||||
public function __construct(
|
||||
Context $context,
|
||||
PageFactory $resultPageFactory,
|
||||
\Magento\Backend\Model\View\Result\ForwardFactory $resultForwardFactory,
|
||||
SalespersonRepository $repository
|
||||
) {
|
||||
parent::__construct($context);
|
||||
$this->resultForwardFactory = $resultForwardFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute view action
|
||||
*
|
||||
* @return ResultInterface
|
||||
* @throws LocalizedException
|
||||
*/
|
||||
public function execute()
|
||||
{
|
||||
$resultForward = $this->resultForwardFactory->create();
|
||||
$resultForward->forward('edit');
|
||||
return $resultForward;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace Stemcell\Customer\Controller\Adminhtml\Salesperson;
|
||||
|
||||
use Magento\Framework\App\Action\HttpGetActionInterface;
|
||||
|
||||
class Sample implements HttpGetActionInterface
|
||||
{
|
||||
protected \Magento\Framework\App\Response\Http\FileFactory $fileFactory;
|
||||
private \Magento\Framework\Module\Dir $moduleDir;
|
||||
|
||||
public function __construct(
|
||||
\Magento\Framework\App\Response\Http\FileFactory $fileFactory,
|
||||
\Magento\Framework\Module\Dir $moduleDir
|
||||
) {
|
||||
$this->fileFactory = $fileFactory;
|
||||
$this->moduleDir = $moduleDir;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function execute()
|
||||
{
|
||||
$file = $this->moduleDir->getDir('Stemcell_Customer').'/data/sample/salesperson.xlsx';
|
||||
$content['type'] = 'filename';
|
||||
$content['value'] = $file;
|
||||
$content['rm'] = 0;
|
||||
return $this->fileFactory->create('salesperson.xlsx', $content);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace Stemcell\Customer\Controller\Adminhtml\Salesperson;
|
||||
|
||||
use Magento\Backend\App\Action\Context;
|
||||
use Magento\Framework\App\Action\HttpPostActionInterface;
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Stemcell\Customer\Api\Data\SalespersonInterface;
|
||||
use Stemcell\Customer\Api\SalespersonRepositoryInterface;
|
||||
|
||||
class Save extends \Magento\Backend\App\Action implements HttpPostActionInterface
|
||||
{
|
||||
protected SalespersonInterface $salesperson;
|
||||
protected SalespersonRepositoryInterface $salespersonRepository;
|
||||
|
||||
public function __construct(
|
||||
Context $context,
|
||||
SalespersonInterface $salesperson,
|
||||
SalespersonRepositoryInterface $salespersonRepository
|
||||
) {
|
||||
parent::__construct($context);
|
||||
$this->salesperson = $salesperson;
|
||||
$this->salespersonRepository = $salespersonRepository;
|
||||
}
|
||||
|
||||
public function execute()
|
||||
{
|
||||
$returnToEdit = false;
|
||||
$salesperson = $this->getRequest()->getParam('salesperson');
|
||||
$this->salesperson->setData($salesperson);
|
||||
try {
|
||||
$this->salespersonRepository->save($this->salesperson);
|
||||
$this->messageManager->addSuccessMessage(__('Salesperson saved.'));
|
||||
} catch (\Exception $e) {
|
||||
$this->messageManager->addExceptionMessage($e);
|
||||
$this->_getSession()->setSalesperson($salesperson);
|
||||
$returnToEdit = true;
|
||||
}
|
||||
|
||||
$resultRedirect = $this->resultRedirectFactory->create();
|
||||
if ($returnToEdit) {
|
||||
if (isset($salesperson['salesperson_id'])) {
|
||||
$resultRedirect->setPath(
|
||||
'stemcell_customer/salesperson/edit',
|
||||
['id' => $salesperson['salesperson_id'], '_current' => true]
|
||||
);
|
||||
} else {
|
||||
$resultRedirect->setPath(
|
||||
'stemcell_customer/salesperson/new',
|
||||
['_current' => true]
|
||||
);
|
||||
}
|
||||
} else {
|
||||
$resultRedirect->setPath('stemcell_customer/salesperson/index');
|
||||
}
|
||||
|
||||
|
||||
return $resultRedirect;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user