Pour tout problème contactez-nous par mail : support@froggit.fr | La FAQ :grey_question: | Rejoignez-nous sur le Chat :speech_balloon:

Skip to content
Snippets Groups Projects
Commit 9132cb49 authored by Nicolas's avatar Nicolas
Browse files

Exportation des orders

parent 2b929a07
No related branches found
No related tags found
No related merge requests found
<?php
namespace NicolasBejean\Sales\Helper;
use Magento\Framework\Filesystem;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\File\Csv;
use Psr\Log\LoggerInterface;
use Magento\Framework\Exception\FileSystemException;
/**
* Class ExportCsv
*
* @category PHP
* @package NicolasBejean\Sales\Helper
* @author Nicolas Béjean <nicolas@bejean.eu>
* @license https://github.com/nicolasbejean/sales/blob/master/licence.txt BSD Licence
* @link https://www.bejean.eu
*/
abstract class ExportCsv
{
/**
* @var array
*/
protected $data;
/**
* @var string
*/
protected $dirname;
/**
* @var string
*/
protected $filename;
/**
* @var Filesystem
*/
protected $filesystem;
/**
* @var DirectoryList
*/
protected $directoryList;
/**
* @var Csv
*/
protected $csvProcessor;
/**
* @var LoggerInterface
*/
protected $logger;
/**
* ExportCsv constructor.
*
* @param Filesystem $filesystem
* @param DirectoryList $directoryList
* @param Csv $csvProcessor
* @param LoggerInterface $logger
*/
public function __construct(
Filesystem $filesystem,
DirectoryList $directoryList,
Csv $csvProcessor,
LoggerInterface $logger
) {
$this->filesystem = $filesystem;
$this->directoryList = $directoryList;
$this->csvProcessor = $csvProcessor;
$this->logger = $logger;
}
/**
* Ecrit les données dans le fichier CSV
*
* @return bool
*/
protected function writeToCsv ()
{
try {
/* Récupère le dossier où seront exportées les données */
$dirPath = $this->directoryList->getPath(DirectoryList::VAR_DIR) . '/' . $this->dirname;
} catch (FileSystemException $e) {
/* Ecrit dans le journal de log si il y a une erreur */
$this->logger->error('NicolasBejeanSales', ['class' => 'ExportCsv', 'method' => 'writeToCsv', 'error' => 'Unable to get dirPath !', 'message' => $e->getMessage()]);
return false;
}
/* Vérifie si le dossier existe */
if (!is_dir($dirPath)) {
/* Créé le dossier si il n'existe pas */
mkdir($dirPath, 0750, true);
}
/* Défini le path du fichier */
$filePath = $dirPath . '/' . $this->filename . '.csv';
try {
/* Enregistre les données dans le fichier */
$this->csvProcessor->appendData($filePath, $this->data);
} catch (FileSystemException $e) {
/* Ecrit dans le journal de log si il y a une erreur */
$this->logger->error('NicolasBejeanSales', ['class' => 'ExportCsv', 'method' => 'writeToCsv', 'error' => 'Unable to save file !', 'message' => $e->getMessage()]);
return false;
}
return true;
}
}
<?php
namespace NicolasBejean\Sales\Helper\ExportData;
use Magento\Framework\Exception\LocalizedException;
use NicolasBejean\Sales\Api\Data\OrderExporterInterface;
use NicolasBejean\Sales\Helper\ExportCsv;
use Magento\Framework\Filesystem;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\File\Csv;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\OrderRepositoryInterfaceFactory;
use Magento\Sales\Model\ResourceModel\Order\Collection;
use NicolasBejean\Sales\Api\OrderExporterRepositoryInterface;
use NicolasBejean\Sales\Model\ResourceModel\OrderExporter\Collection as OrderExporterCollection;
use NicolasBejean\Sales\Model\ResourceModel\OrderExporter\CollectionFactory as OrderExporterCollectionFactory;
use Psr\Log\LoggerInterface;
/**
* Class Order
*
* @category PHP
* @package NicolasBejean\Sales\Helper\ExportData
* @author Nicolas Béjean <nicolas@bejean.eu>
* @license https://github.com/nicolasbejean/sales/blob/master/licence.txt BSD Licence
* @link https://www.bejean.eu
*/
class Order extends ExportCsv
{
/**
* @var Collection
*/
protected $orderCollection;
/**
* Global configuration storage.
*
* @var ScopeConfigInterface
*/
protected $globalConfig;
/**
* @var OrderRepositoryInterfaceFactory
*/
protected $orderRepository;
/**
* @var OrderExporterRepositoryInterface
*/
protected $orderExporterRepository;
/**
* @var OrderExporterCollectionFactory
*/
protected $orderExporterCollection;
/**
* Order constructor.
*
* @param Filesystem $filesystem
* @param DirectoryList $directoryList
* @param Csv $csvProcessor
* @param LoggerInterface $logger
* @param Collection $orderCollection
* @param ScopeConfigInterface $globalConfig
* @param OrderRepositoryInterfaceFactory $orderRepository
* @param OrderExporterRepositoryInterface $orderExporterRepository
* @param OrderExporterCollectionFactory $orderExporterCollection
*/
public function __construct(
Filesystem $filesystem,
DirectoryList $directoryList,
Csv $csvProcessor,
LoggerInterface $logger,
Collection $orderCollection,
ScopeConfigInterface $globalConfig,
OrderRepositoryInterfaceFactory $orderRepository,
OrderExporterRepositoryInterface $orderExporterRepository,
OrderExporterCollectionFactory $orderExporterCollection
) {
parent::__construct($filesystem, $directoryList, $csvProcessor, $logger);
$this->orderCollection = $orderCollection;
$this->globalConfig = $globalConfig;
$this->orderRepository = $orderRepository;
$this->orderExporterRepository = $orderExporterRepository;
$this->orderExporterCollection = $orderExporterCollection;
$this->dirname = 'exportOrder';
$this->filename = 'order_' . date("Y-m-d-H-i");
}
public function export()
{
/* Récupère la limit définit dans la configuration */
$sendingLimit = $this->globalConfig->getValue('nicolasbejeansales/general/sending_limit');
/* Récupère la liste de toutes les commandes où l'exportation n'a pas été réalisée */
/** @var OrderExporterCollection $collection */
$collection = $this->orderExporterCollection->create();
$collection->addFieldToFilter('is_exported', '0');
/* Limite la sélection des données */
$collection->setPageSize($sendingLimit);
/** @var OrderExporterInterface $orderExporter */
foreach ($collection->getItems() as $orderExporter) {
$orderRepository = $this->orderRepository->create();
/** @var OrderInterface $order */
$order = $orderRepository->get($orderExporter->getOrderId());
/* Prépare les éléments */
$this->data[0] = $this->prepareData($order);
/* Lance l'export des commandes */
$orderExported = $this->writeToCsv();
if ($orderExported) {
$orderExporter->setIsExported(1);
try {
$this->orderExporterRepository->save($orderExporter);
} catch (LocalizedException $e) {
$this->logger->error('NicolasBejeanSales', ['class' => 'Order', 'method' => 'orderExporterRepository->save', 'error' => 'Unable to save order Exporter !', 'message' => $e->getMessage()]);
}
}
}
}
/**
* @param OrderInterface $item
* @return array
*/
protected function prepareData (OrderInterface $item): array
{
return $data = [
'order_id' => $item->getEntityId(),
'customer_email' => $item->getCustomerEmail(),
'customer_firstname' => $item->getCustomerFirstname(),
'customer_lastname' => $item->getCustomerLastname(),
'shipping_description' => $item->getShippingDescription()
];
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment