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,394 @@
<?php
namespace Empik\Marketplace\OrderFulfiller;
use Configuration;
use Empik\Marketplace\Adapter\ConfigurationAdapter;
use Empik\Marketplace\DataProvider\ProductDataProvider;
use Empik\Marketplace\PrestaShopContext;
use Empik\Marketplace\Repository\ProductRepository;
use Empik\Marketplace\Utils\IdentifierExtractor;
use Order;
use OrderDetail;
use Address;
use Customer;
use OrderCarrier;
use OrderHistory;
use EmpikOrder;
use Cart;
use StockAvailable;
class EmpikOrderWrapper
{
/** @var ProductDataProvider */
protected $productDataProvider;
/** @var ProductRepository */
protected $productRepository;
/** @var IdentifierExtractor */
protected $identifierExtractor;
/** @var array */
protected $data;
/** @var Order */
protected $order;
/** @var OrderDetail[] */
protected $orderDetails;
/** @var Customer */
protected $customer;
/** @var Address */
protected $shippingAddress;
/** @var Address */
protected $billingAddress;
/** @var OrderCarrier */
protected $carrier;
/** @var Cart */
protected $cart;
/** @var OrderHistory[] */
protected $orderHistory;
public function __construct()
{
$this->productDataProvider = new ProductDataProvider();
$this->productRepository = new ProductRepository(
new PrestaShopContext()
);
$this->identifierExtractor = new IdentifierExtractor();
}
public function init($data)
{
$id = EmpikOrder::getOrderIdByEmpikOrderReference($data['order_id']);
$order = new Order($id);
$this->setData($data);
$this->setOrder($order);
}
/**
* @return bool
*/
public function exist()
{
$data = $this->getData();
return (bool)\EmpikOrder::getOrderIdByEmpikOrderReference($data['order_id']);
}
/**
* @return bool
*/
public function isAcceptable()
{
foreach ($this->getAcceptanceLines() as $orderLine) {
if (!$orderLine['accepted']) {
return false;
}
}
return true;
}
/**
* @return array
*/
public function getAcceptanceLines()
{
$skuType = Configuration::get(ConfigurationAdapter::CONF_SKU_TYPE);
$return = [];
foreach ($this->getData()['order_lines'] as $orderLine) {
if ($skuType === 'ID') {
$product = $this->productRepository->getProductOrCombinationById($orderLine['offer_sku']);
} elseif ($skuType === 'EAN') {
$product = $this->productRepository->getProductOrCombinationByEan($orderLine['offer_sku']);
} elseif ($skuType === 'REFERENCE') {
$product = $this->productRepository->getProductOrCombinationBySku($orderLine['offer_sku']);
}
$return[] = [
'id' => (string)$orderLine['order_line_id'],
'accepted' => $product && $product['active'] && $product['quantity'] >= $orderLine['quantity'],
];
}
return $return;
}
public function add()
{
$context = \Context::getContext();
if ($this->customer && !$this->customer->id) {
$this->customer->id_shop = $context->shop->id;
$this->customer->id_shop_group = $context->shop->id_shop_group;
$this->customer->add();
}
if ($this->shippingAddress && !$this->shippingAddress->id) {
$this->shippingAddress->id_customer = $this->customer->id;
$this->shippingAddress->add();
}
if ($this->billingAddress && !$this->billingAddress->id) {
$this->billingAddress->id_customer = $this->customer->id;
$this->billingAddress->add();
}
$this->cart->id_customer = $this->customer->id;
$this->cart->id_address_delivery = $this->shippingAddress->id;
$this->cart->id_address_invoice = $this->billingAddress->id;
$this->cart->update();
$this->order->id_address_delivery = $this->shippingAddress->id;
$this->order->id_address_invoice = $this->billingAddress->id;
$this->order->id_cart = $this->cart->id;
$this->order->id_currency = $context->currency->id;
$this->order->id_lang = $context->language->id;
$this->order->id_shop = $context->shop->id;
$this->order->id_shop_group = $context->shop->id_shop_group;
$this->order->id_customer = $this->customer->id;
$this->order->id_carrier = $this->carrier->id_carrier;
$this->order->payment = $this->data['payment_type'];
$this->order->module = 'empikmarketplace';
$this->order->total_paid = (float)$this->data['total_price'];
$this->order->total_paid_tax_incl = (float)$this->data['total_price'];
$this->order->total_paid_tax_excl = (float)$this->data['total_price'];
$this->order->total_paid_real = (float)$this->data['total_price'];
$this->order->total_products = (float)$this->data['price'];
$this->order->total_products_wt = (float)$this->data['price'];
$this->order->total_shipping = (float)$this->data['shipping_price'];
$this->order->total_shipping_tax_incl = (float)$this->data['shipping_price'];
$this->order->total_shipping_tax_excl = (float)$this->data['shipping_price'];
$this->order->conversion_rate = 1;
$this->order->secure_key = $this->customer->secure_key;
do {
$this->order->reference = Order::generateReference();
} while (Order::getByReference($this->order->reference)->count());
if ($this->orderHistory) {
$this->order->current_state = end($this->orderHistory)->id_order_state;
}
// create order
$this->order->add();
// add order carrier
$this->carrier->id_order = $this->order->id;
$this->carrier->shipping_cost_tax_excl = $this->order->total_shipping_tax_excl;
$this->carrier->shipping_cost_tax_incl = $this->order->total_shipping_tax_incl;
$this->carrier->add();
foreach ($this->orderDetails as $orderDetail) {
$orderDetail->id_order = $this->order->id;
$orderDetail->add();
if ($this->order->current_state != Configuration::get('PS_OS_CANCELED')
&& $this->order->current_state != Configuration::get('PS_OS_ERROR')) {
if (!StockAvailable::dependsOnStock($orderDetail->product_id)) {
StockAvailable::updateQuantity(
$orderDetail->product_id,
$orderDetail->product_attribute_id,
-(int) $orderDetail->product_quantity,
$orderDetail->id_shop,
true
);
}
}
}
foreach ($this->orderHistory as $history) {
$history->id_order = $this->order->id;
$history->add();
}
$this->createEmpikOrder();
}
public function createEmpikOrder()
{
$empikOrder = new EmpikOrder();
$empikOrder->id_order = $this->order->id;
$empikOrder->empik_order_reference = $this->data['order_id'];
$empikOrder->empik_order_carrier = $this->data['shipping_type_code'];
$empikOrder->empik_payment = $this->data['payment_type'];
$empikOrder->empik_carrier = $this->data['shipping_type_label'];
foreach ($this->data['order_additional_fields'] as $additionalField) {
if ($additionalField['code'] === 'delivery-point-name') {
$empikOrder->empik_pickup_point = $additionalField['value'];
}
if ($additionalField['code'] === 'nip') {
$empikOrder->empik_vat_number = $additionalField['value'];
}
}
$empikOrder->add();
}
/**
* @return Cart
*/
public function getCart()
{
return $this->cart;
}
/**
* @param Cart $cart
*/
public function setCart($cart)
{
$this->cart = $cart;
}
/**
* @return OrderHistory[]
*/
public function getOrderHistory()
{
return $this->orderHistory;
}
/**
* @param OrderHistory[] $orderHistory
*/
public function setOrderHistory($orderHistory)
{
$this->orderHistory = $orderHistory;
}
/**
* @return OrderCarrier
*/
public function getCarrier()
{
return $this->carrier;
}
/**
* @param OrderCarrier $carrier
*/
public function setCarrier($carrier)
{
$this->carrier = $carrier;
}
/**
* @return array
*/
public function getData()
{
return $this->data;
}
/**
* @param array $data
*/
public function setData($data)
{
$this->data = $data;
}
/**
* @return Order
*/
public function getOrder()
{
return $this->order;
}
/**
* @param Order $order
*/
public function setOrder($order)
{
$this->order = $order;
}
/**
* @return OrderDetail[]
*/
public function getOrderDetails()
{
return $this->orderDetails;
}
/**
* @param OrderDetail[] $orderDetails
*/
public function setOrderDetails($orderDetails)
{
$this->orderDetails = $orderDetails;
}
/**
* @return Customer
*/
public function getCustomer()
{
return $this->customer;
}
/**
* @param Customer $customer
*/
public function setCustomer($customer)
{
$this->customer = $customer;
}
/**
* @return Address
*/
public function getShippingAddress()
{
return $this->shippingAddress;
}
/**
* @param Address $shippingAddress
*/
public function setShippingAddress($shippingAddress)
{
$this->shippingAddress = $shippingAddress;
}
/**
* @return Address
*/
public function getBillingAddress()
{
return $this->billingAddress;
}
/**
* @param Address $billingAddress
*/
public function setBillingAddress($billingAddress)
{
$this->billingAddress = $billingAddress;
}
}

View File

@@ -0,0 +1,120 @@
<?php
namespace Empik\Marketplace\OrderFulfiller;
use Empik\Marketplace\Provider\Order\AddressProvider;
use Empik\Marketplace\Provider\Order\CarrierProvider;
use Empik\Marketplace\Provider\Order\CustomerProvider;
use Empik\Marketplace\Provider\Order\HistoryProvider;
use Empik\Marketplace\Provider\Order\CartProvider;
use Empik\Marketplace\Provider\Order\OrderLinesProvider;
use Empik\Marketplace\Exception\OrderProcessException;
class OrderFulfiller
{
/** @var AddressProvider */
protected $addressProvider;
/** @var CustomerProvider */
protected $customerProvider;
/** @var OrderLinesProvider */
protected $orderLinesProvider;
/** @var CarrierProvider */
protected $carrierProvider;
/** @var HistoryProvider */
protected $historyProvider;
/** @var CartProvider */
protected $cartProvider;
public function __construct(
AddressProvider $addressProvider,
CustomerProvider $customerProvider,
OrderLinesProvider $orderLinesProvider,
CarrierProvider $carrierProvider,
HistoryProvider $historyProvider,
CartProvider $cartProvider
) {
$this->addressProvider = $addressProvider;
$this->customerProvider = $customerProvider;
$this->orderLinesProvider = $orderLinesProvider;
$this->carrierProvider = $carrierProvider;
$this->historyProvider = $historyProvider;
$this->cartProvider = $cartProvider;
}
/**
* @param EmpikOrderWrapper $order
* @throws OrderProcessException
* @throws \PrestaShopDatabaseException
* @throws \PrestaShopException
*/
public function fulfill(EmpikOrderWrapper $order)
{
$data = $order->getData();
$this->validate($data);
// add customer
$prestaShopCustomer = $this->customerProvider->provide($data);
$order->setCustomer($prestaShopCustomer);
// add addresses
$billingAddress = isset($data['customer']['billing_address']) ? $data['customer']['billing_address'] : null;
$shippingAddress = isset($data['customer']['shipping_address']) ? $data['customer']['shipping_address'] : null;
$prestaShopShippingAddress = $this->addressProvider->provide(
$shippingAddress,
$order->getCustomer()
);
$order->setShippingAddress($prestaShopShippingAddress);
$prestaShopBillingAddress = $this->addressProvider->provide(
$billingAddress,
$order->getCustomer()
);
$order->setBillingAddress($prestaShopBillingAddress);
// add cart
$cart = $this->cartProvider->provide($data);
$order->setCart($cart);
// add order lines
$orderLines = $this->orderLinesProvider->provide($data);
$order->setOrderDetails($orderLines);
// add carrier
$carrier = $this->carrierProvider->provide($data);
$order->setCarrier($carrier);
// add order status
$history = $this->historyProvider->provide($data);
$order->setOrderHistory($history);
$order->add();
}
/**
* @param array $data
* @throws OrderProcessException
*/
protected function validate($data)
{
if (
(
empty($data['customer']['shipping_address']) &&
empty($data['customer']['billing_address'])
) ||
empty($data['order_id']) ||
empty($data['order_lines']) ||
empty($data['order_state']) ||
empty($data['total_price'])
) {
throw new OrderProcessException(sprintf('Invalid order data for order: %s', $data['order_id']));
}
}
}

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;