first commit

This commit is contained in:
2025-01-06 20:47:25 +01:00
commit 3bdbd78c2f
25591 changed files with 3586440 additions and 0 deletions

View File

@@ -0,0 +1,108 @@
<?php
namespace Empik\Marketplace\Handler;
use Context;
use EmpikMarketplace;
use Empik\Marketplace\Adapter\ToolsAdapter;
use Empik\Marketplace\Processor\OrderProcessor;
use Empik\Marketplace\Processor\ExportProductProcessor;
use Empik\Marketplace\Processor\ExportOfferProcessor;
use Empik\Marketplace\Exception\InvalidActionException;
class CronJobsHandler
{
const ACTION_EXPORT_PRODUCTS = 'exportProducts';
const ACTION_EXPORT_OFFERS = 'exportOffers';
const ACTION_IMPORT_ORDERS = 'importOrders';
const ACTIONS = [
self::ACTION_EXPORT_PRODUCTS,
self::ACTION_EXPORT_OFFERS,
self::ACTION_IMPORT_ORDERS,
];
/** @var Context */
protected $context;
/** @var EmpikMarketplace */
protected $module;
/** @var ToolsAdapter */
protected $tools;
/** @var ExportProductProcessor */
protected $exportProductProcessor;
/** @var ExportOfferProcessor */
protected $exportOfferProcessor;
/** @var OrderProcessor */
protected $orderProcessor;
public function __construct(
EmpikMarketplace $module,
ToolsAdapter $tools,
ExportProductProcessor $exportProductProcessor,
ExportOfferProcessor $exportOfferProcessor,
OrderProcessor $orderProcessor
) {
$this->module = $module;
$this->tools = $tools;
$this->exportProductProcessor = $exportProductProcessor;
$this->exportOfferProcessor = $exportOfferProcessor;
$this->orderProcessor = $orderProcessor;
$this->context = Context::getContext();
}
public function handle($action)
{
set_time_limit(0);
switch ($action) {
case self::ACTION_EXPORT_PRODUCTS:
$this->exportProductProcessor->process();
break;
case self::ACTION_EXPORT_OFFERS:
$this->exportOfferProcessor->process();
file_put_contents('logs/empik.log', date( 'Y-m-d H:i:s' ) . ' - export offers' . PHP_EOL, FILE_APPEND );
break;
case self::ACTION_IMPORT_ORDERS:
$this->orderProcessor->process();
break;
default:
throw new InvalidActionException();
}
}
public function getAvailableActionsUrls()
{
$urls = [];
foreach (self::ACTIONS as $action) {
$urls[$action] = $this->getActionUrl($action);
}
return $urls;
}
public function checkToken($token)
{
return $token === $this->getToken();
}
protected function getActionUrl($action)
{
return $this->context->link->getModuleLink($this->module->name, 'cron', [
'action' => $action,
'token' => $this->getToken(),
]);
}
protected function getToken()
{
return $this->tools->hash($this->module->name . '_cron');
}
}

View File

@@ -0,0 +1,189 @@
<?php
namespace Empik\Marketplace\Handler;
use Context;
use Empik\Marketplace\Adapter\ConfigurationAdapter;
use Empik\Marketplace\Manager\CsvManager;
use Empik\Marketplace\Utils\SkuExtractor;
use Product;
use EmpikMarketplace;
use Empik\Marketplace\Configuration\ExportConfiguration;
use Empik\Marketplace\Repository\CategoryRepository;
use Empik\Marketplace\Repository\FeatureRepository;
use Empik\Marketplace\Repository\ImageRepository;
use Empik\Marketplace\Repository\ProductRepository;
use Empik\Marketplace\Repository\TaxRepository;
use Empik\Marketplace\Utils\CategoryPathBuilder;
use Empik\Marketplace\Utils\IdentifierExtractor;
use Empik\Marketplace\Formatter\ProductNameFormatter;
use Empik\Marketplace\Utils\OfferPriceCalculator;
use Empik\Marketplace\Utils\OfferQuantityCalculator;
class ExportOfferHandler
{
const STATE_CODE_NEW = 1;
const STATE_CODE_USED = 11;
/** @var EmpikMarketplace */
protected $module;
/** @var Context */
protected $context;
/** @var ExportConfiguration */
protected $exportConfig;
/** @var ProductRepository */
protected $productRepository;
/** @var FeatureRepository */
protected $featureRepository;
/** @var TaxRepository */
protected $taxRepository;
/** @var CategoryRepository */
protected $categoryRepository;
/** @var ImageRepository */
protected $imageRepository;
/** @var ProductNameFormatter */
protected $productNameFormatter;
/** @var CategoryPathBuilder */
protected $categoryPathBuilder;
/** @var IdentifierExtractor */
protected $identifierExtractor;
/** @var SkuExtractor */
protected $skuExtractor;
/** @var OfferPriceCalculator */
protected $offerPriceCalculator;
/** @var OfferQuantityCalculator */
protected $offerQuantityCalculator;
/** @var CsvManager */
protected $csvManager;
public function __construct(
EmpikMarketplace $module,
ExportConfiguration $exportConfig,
ProductRepository $productRepository,
FeatureRepository $featureRepository,
TaxRepository $taxRepository,
CategoryRepository $categoryRepository,
ImageRepository $imageRepository,
ProductNameFormatter $productNameFormatter,
CategoryPathBuilder $categoryPathBuilder,
IdentifierExtractor $identifierExtractor,
SkuExtractor $skuExtractor,
OfferPriceCalculator $offerPriceCalculator,
OfferQuantityCalculator $offerQuantityCalculator
) {
$this->module = $module;
$this->exportConfig = $exportConfig;
$this->productRepository = $productRepository;
$this->featureRepository = $featureRepository;
$this->taxRepository = $taxRepository;
$this->categoryRepository = $categoryRepository;
$this->imageRepository = $imageRepository;
$this->productNameFormatter = $productNameFormatter;
$this->categoryPathBuilder = $categoryPathBuilder;
$this->identifierExtractor = $identifierExtractor;
$this->skuExtractor = $skuExtractor;
$this->offerPriceCalculator = $offerPriceCalculator;
$this->offerQuantityCalculator = $offerQuantityCalculator;
$this->csvManager = new CsvManager(
$this->exportConfig->getOfferExportPath(),
ExportConfiguration::CSV_FIELD_SEPARATOR
);
$this->context = Context::getContext();
}
public function isLastPage($page)
{
$total = $this->productRepository->getProductsTotal(ProductRepository::SRC_OFFER);
return $page * ExportConfiguration::EXPORT_PRODUCTS_PER_PAGE > $total;
}
public function handle($page = null)
{
if (!$page && file_exists($this->exportConfig->getOfferExportPath())) {
$this->csvManager->remove();
}
$localPage = $page === null ? 0 : $page;
$leadTimeToShip = \Configuration::get(ConfigurationAdapter::CONF_LEAD_TIME_TO_SHIP);
$syncLogisticClass = \Configuration::get(ConfigurationAdapter::CONF_SYNC_LOGISTIC_CLASS);
do {
$products = $this->productRepository->getProductsPaginate(
$localPage,
ExportConfiguration::EXPORT_PRODUCTS_PER_PAGE,
ProductRepository::SRC_OFFER
);
foreach ($products as $i => $product) {
$offer = [];
$priceWithReduction = Product::getPriceStatic($product['id_product'], true, $product['id_product_attribute'], 2);
$priceWithoutReduction = Product::getPriceStatic($product['id_product'], true, $product['id_product_attribute'], 2, null, false, false);
$calculatedPriceWithReduction = $this->offerPriceCalculator->calculate($priceWithReduction);
$calculatedPriceWithoutReduction = $this->offerPriceCalculator->calculate($priceWithoutReduction);
$hasReduction = $calculatedPriceWithoutReduction > $calculatedPriceWithReduction;
$quantity = \StockAvailable::getQuantityAvailableByProduct($product['id_product'], $product['id_product_attribute']);
$calculatedQuantity = $this->offerQuantityCalculator->calculate($quantity);
if ( $calculatedQuantity < 1 )
$calculatedQuantity = 1;
$offer['product-id-type'] = $this->identifierExtractor->getIdentifierType();
$offer['product-id'] = $this->identifierExtractor->extract($product);
$offer['sku'] = $this->skuExtractor->extract($product);
$offer['state'] = $product['condition'] === 'new' ? self::STATE_CODE_USED : self::STATE_CODE_NEW;
if ($product['offer_price'] > 0) {
$offer['price'] = $product['offer_price'];
} else {
$offer['price'] = $calculatedPriceWithoutReduction;
}
if ($product['offer_price_reduced'] > 0) {
$offer['discount-price'] = $product['offer_price_reduced'];
} else {
$offer['discount-price'] = $hasReduction ? $calculatedPriceWithReduction : null;
}
$offer['quantity'] = $calculatedQuantity;
if ( $offer['quantity'] < 1 )
$offer['quantity'] = 1;
if ($leadTimeToShip && $leadTimeToShip >= 0) {
$offer['leadtime-to-ship'] = $leadTimeToShip;
}
if ($syncLogisticClass) {
$offer['logistic-class'] = $product['logistic_class'];
}
// add csv header
if ($i === 0 && ($page === 0 || $page === null && $localPage === 0)) {
$this->csvManager->addRow(array_keys($offer));
}
$this->csvManager->addRow($offer);
}
$localPage++;
} while ($products && $page === null);
}
}

View File

@@ -0,0 +1,259 @@
<?php
namespace Empik\Marketplace\Handler;
use Empik\Marketplace\Manager\CsvManager;
use Tools;
use Context;
use Product;
use StockAvailable;
use EmpikMarketplace;
use Empik\Marketplace\Configuration\ExportConfiguration;
use Empik\Marketplace\Repository\CategoryRepository;
use Empik\Marketplace\Repository\FeatureRepository;
use Empik\Marketplace\Repository\AttributeRepository;
use Empik\Marketplace\Repository\ImageRepository;
use Empik\Marketplace\Repository\ProductRepository;
use Empik\Marketplace\Repository\TaxRepository;
use Empik\Marketplace\Utils\CategoryPathBuilder;
use Empik\Marketplace\Utils\IdentifierExtractor;
use Empik\Marketplace\Formatter\ProductNameFormatter;
class ExportProductHandler
{
/** @var EmpikMarketplace */
protected $module;
/** @var Context */
protected $context;
/** @var ExportConfiguration */
protected $exportConfig;
/** @var ProductRepository */
protected $productRepository;
/** @var FeatureRepository */
protected $featureRepository;
/** @var AttributeRepository */
protected $attributeRepository;
/** @var TaxRepository */
protected $taxRepository;
/** @var CategoryRepository */
protected $categoryRepository;
/** @var ImageRepository */
protected $imageRepository;
/** @var ProductNameFormatter */
protected $productNameFormatter;
/** @var CategoryPathBuilder */
protected $categoryPathBuilder;
/** @var IdentifierExtractor */
protected $identifierExtractor;
/** @var CsvManager */
protected $csvManager;
public function __construct(
EmpikMarketplace $module,
ExportConfiguration $exportConfig,
ProductRepository $productRepository,
FeatureRepository $featureRepository,
AttributeRepository $attributeRepository,
TaxRepository $taxRepository,
CategoryRepository $categoryRepository,
ImageRepository $imageRepository,
ProductNameFormatter $productNameFormatter,
CategoryPathBuilder $categoryPathBuilder,
IdentifierExtractor $identifierExtractor
) {
$this->module = $module;
$this->exportConfig = $exportConfig;
$this->productRepository = $productRepository;
$this->featureRepository = $featureRepository;
$this->attributeRepository = $attributeRepository;
$this->taxRepository = $taxRepository;
$this->categoryRepository = $categoryRepository;
$this->imageRepository = $imageRepository;
$this->productNameFormatter = $productNameFormatter;
$this->categoryPathBuilder = $categoryPathBuilder;
$this->identifierExtractor = $identifierExtractor;
$this->csvManager = new CsvManager(
$this->exportConfig->getProductExportPath(),
ExportConfiguration::CSV_FIELD_SEPARATOR
);
$this->context = Context::getContext();
}
public function isLastPage($page)
{
$total = $this->productRepository->getProductsTotal(ProductRepository::SRC_PRODUCT);
return $page * ExportConfiguration::EXPORT_PRODUCTS_PER_PAGE > $total;
}
public function handle($page = null)
{
if (!$page && file_exists($this->exportConfig->getProductExportPath())) {
$this->csvManager->remove();
}
$localPage = $page === null ? 0 : $page;
do {
$products = $this->productRepository->getProductsPaginate(
$localPage,
ExportConfiguration::EXPORT_PRODUCTS_PER_PAGE,
ProductRepository::SRC_PRODUCT
);
foreach ($products as $i => $product) {
$product = $this->addName($product);
$product = $this->addPrices($product);
$product = $this->addQuantity($product);
$product = $this->addTax($product);
$product = $this->addCategories($product);
$product = $this->addFeatures($product);
$product = $this->addAttributes($product);
$product = $this->addImages($product);
// add csv header
if ($i === 0 && ($page === 0 || $page === null && $localPage === 0)) {
$this->csvManager->addRow(array_keys($product));
}
$this->csvManager->addRow($product);
}
$localPage++;
} while ($products && $page === null);
}
/**
* @param array $product
* @return array
*/
protected function addName($product)
{
$product['name'] = $this->productNameFormatter->format($product['name']);
return $product;
}
/**
* @param array $product
* @return array
*/
protected function addFeatures($product)
{
$features = $this->featureRepository->getFeatures($product['id_product']);
foreach ($features as $feature) {
$product[sprintf('feature[%s]', $feature['feature_name'])] = $feature['value'];
}
return $product;
}
/**
* @param array $product
* @return array
*/
protected function addAttributes($product)
{
$features = $this->attributeRepository->getAttributes($product['id_product'], $product['id_product_attribute']);
foreach ($features as $feature) {
$product[sprintf('attribute[%s]', $feature['name'])] = $feature['value'];
}
return $product;
}
/**
* @param array $product
* @return array
*/
protected function addPrices($product)
{
$product['price'] = Product::getPriceStatic($product['id_product'], true, $product['id_product_attribute'], 2);
$product['price_without_reduction'] = Product::getPriceStatic($product['id_product'], true, $product['id_product_attribute'], 2, null, false, false);
$product['reduction'] = Product::getPriceStatic($product['id_product'], true, $product['id_product_attribute'], 2, null, true);
return $product;
}
/**
* @param array $product
* @return array
*/
protected function addQuantity($product)
{
$quantity = StockAvailable::getQuantityAvailableByProduct(
$product['id_product'],
$product['id_product_attribute']
);
$product['quantity'] = (int)$quantity;
return $product;
}
/**
* @param array $product
* @return array
*/
protected function addTax($product)
{
$product['vat'] = $this->taxRepository->getTaxRateByProductId($product['id_product']);
return $product;
}
/**
* @param array $product
* @return array
*/
protected function addCategories($product)
{
$categories = $this->categoryRepository->getCategoriesByProductId($product['id_product']);
$path = $this->categoryPathBuilder->buildPath($categories);
$product['category'] = $path;
return $product;
}
/**
* @param array $product
* @return array
*/
protected function addImages($product)
{
$images = $this->imageRepository->getImages($product['id_product'], $product['id_product_attribute']);
for ($i = 0; $i < ExportConfiguration::EXPORT_IMG_LIMIT; $i++) {
$imageUrl = null;
if (isset($images[$i])) {
$imageUrl = $this->context->link->getImageLink(
Tools::link_rewrite($product['link_rewrite'] ? $product['link_rewrite'] : $product['name']),
$images[$i]['id_image']
);
}
$product[sprintf('image_%d', $i + 1)] = isset($imageUrl) ? $imageUrl : null;
}
return $product;
}
}

View File

@@ -0,0 +1,55 @@
<?php
namespace Empik\Marketplace\Handler;
use Exception;
use Empik\Marketplace\API\EmpikClient;
use Empik\Marketplace\DataProvider\OffersDataProvider;
use Empik\Marketplace\Factory\EmpikClientFactory;
use Empik\Marketplace\Repository\ProductRepository;
use Empik\Marketplace\Traits\ErrorsTrait;
class UpdateDeleteOfferHandler
{
use ErrorsTrait;
/** @var EmpikClientFactory */
protected $empikClientFactory;
/** @var ProductRepository */
protected $productRepository;
/** @var OffersDataProvider */
protected $offersDataProvider;
/** @var EmpikClient */
protected $empikClient;
public function __construct(
EmpikClientFactory $empikClientFactory,
ProductRepository $productRepository,
OffersDataProvider $offersDataProvider
) {
$this->empikClientFactory = $empikClientFactory;
$this->productRepository = $productRepository;
$this->offersDataProvider = $offersDataProvider;
$this->empikClient = $this->empikClientFactory->createClient();
}
public function handle(array $ids)
{
try {
$offersData = $this->offersDataProvider->getOffersDataByProductIds($ids);
$this->empikClient->postOffers($offersData);
return true;
} catch (Exception $e) {
$this->addError($e->getMessage());
}
return false;
}
}

View File

@@ -0,0 +1,11 @@
<?php
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
header('Location: ../');
exit;