383 lines
9.2 KiB
PHP
383 lines
9.2 KiB
PHP
<?php
|
|
|
|
namespace Empik\Marketplace\OrderFulfiller;
|
|
|
|
use Empik\Marketplace\Adapter\ToolsAdapter;
|
|
use Empik\Marketplace\DataProvider\ProductDataProvider;
|
|
use Order;
|
|
use OrderDetail;
|
|
use Address;
|
|
use Customer;
|
|
use OrderCarrier;
|
|
use OrderHistory;
|
|
use Cart;
|
|
|
|
class EmpikOrder
|
|
{
|
|
/** @var ProductDataProvider */
|
|
protected $productDataProvider;
|
|
|
|
/** @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;
|
|
|
|
/** @var array */
|
|
protected $notes;
|
|
|
|
public function __construct($data)
|
|
{
|
|
$id = \EmpikOrder::getOrderIdByEmpikOrderReference($data['order_id']);
|
|
$order = new Order($id);
|
|
|
|
$this->setData($data);
|
|
$this->setOrder($order);
|
|
|
|
$this->productDataProvider = new ProductDataProvider();
|
|
}
|
|
|
|
/**
|
|
* @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()
|
|
{
|
|
$return = [];
|
|
|
|
foreach ($this->getData()['order_lines'] as $orderLine) {
|
|
$pr = $this->productDataProvider->getOneBy([
|
|
'p.reference' => $orderLine['offer_sku'] // @todo
|
|
]);
|
|
|
|
$return[] = [
|
|
'id' => (string)$orderLine['order_line_id'],
|
|
'accepted' => $pr && $pr['active'] && $pr['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->shippingAddress->id_customer = $this->customer->id;
|
|
$this->billingAddress->add();
|
|
}
|
|
|
|
$this->cart->id_customer = $this->customer->id;
|
|
// $this->cart->add();
|
|
|
|
$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;
|
|
}
|
|
|
|
// foreach ($this->notes as $note) {
|
|
// $this->order->note .= !$this->order->note ? $note : "\r\n\r\n" . $note;
|
|
// }
|
|
|
|
// create order
|
|
$this->order->add();
|
|
|
|
// add message (ps 1.6)
|
|
// if ($this->order->note && !ToolsAdapter::is17static()) {
|
|
// $msg = new \Message();
|
|
// $msg->message = $this->order->note;
|
|
// $msg->id_cart = $this->cart->id;
|
|
// $msg->id_customer = $this->customer->id;
|
|
// $msg->id_order = $this->order->id;
|
|
// $msg->private = 1;
|
|
// $msg->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();
|
|
}
|
|
|
|
foreach ($this->orderHistory as $history) {
|
|
$history->id_order = $this->order->id;
|
|
$history->add();
|
|
}
|
|
|
|
// @todo
|
|
$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;
|
|
}
|
|
|
|
/**
|
|
* @return array
|
|
*/
|
|
public function getNotes(): array
|
|
{
|
|
return $this->notes;
|
|
}
|
|
|
|
/**
|
|
* @param array $notes
|
|
*/
|
|
public function setNotes(array $notes): void
|
|
{
|
|
$this->notes = $notes;
|
|
}
|
|
} |