Daniel 3 лет назад
Родитель
Сommit
01bbc6ccb3
1 измененных файлов: 46 добавлений и 32 удалений
  1. +46
    -32
      src/EventListener/PostCheckoutListener.php

+ 46
- 32
src/EventListener/PostCheckoutListener.php Просмотреть файл

@@ -19,6 +19,9 @@ class PostCheckoutListener
const WAREHOUSE_ID = 'd8a6a9b8-d0ac-4d36-8d00-bd420d0d81f5'; const WAREHOUSE_ID = 'd8a6a9b8-d0ac-4d36-8d00-bd420d0d81f5';
const PAYMENT_CONDITION = 'PP'; const PAYMENT_CONDITION = 'PP';
const VAT_FACTOR = 1.19; const VAT_FACTOR = 1.19;
const SURCHAGRGE_SHIPPING = 'shipping';
const SURCHAGRGE_RULE = 'rule';
const SHIPPING_ITEM_ID = '99068232-8a4c-4468-9228-e61e26edac43';


private Order $order; private Order $order;
private ApiExact $apiExact; private ApiExact $apiExact;
@@ -26,6 +29,8 @@ class PostCheckoutListener
private $salesOrderItems = []; private $salesOrderItems = [];
private $billingCustomer = null; private $billingCustomer = null;
private $shippingCustomer = null; private $shippingCustomer = null;
private $orderDiscount = null;
private $shippingItem = [];


/** /**
* @Hook("postCheckout") * @Hook("postCheckout")
@@ -34,21 +39,11 @@ class PostCheckoutListener
{ {
$this->order = $order; $this->order = $order;
$this->apiExact = new ApiExact(); $this->apiExact = new ApiExact();

$this->setDbProducts(); $this->setDbProducts();
$this->setSurCharges();
$this->setOrderItems(); $this->setOrderItems();
$this->setCustomers(); $this->setCustomers();

$surCharges = [];
/** @var ProductCollectionSurcharge $orderSurcharge */
foreach ($order->getSurcharges() as $orderSurcharge) {
$surCharges[] = $orderSurcharge;
}



$this->apiExact->createSalesOrder($this->createSalesOrderData()); $this->apiExact->createSalesOrder($this->createSalesOrderData());

} }


private function setDbProducts() private function setDbProducts()
@@ -63,11 +58,6 @@ class PostCheckoutListener
foreach ($dbProducts as $dbProduct) { foreach ($dbProducts as $dbProduct) {
$this->dbProductsBySku[$dbProduct['sku']] = $dbProduct; $this->dbProductsBySku[$dbProduct['sku']] = $dbProduct;
} }
// ob_start();
// var_dump($this->dbProductsBySku);
// file_put_contents('dan.txt', ob_get_contents());
// ob_end_clean();

} }


private function compareAddresses(Order $order) private function compareAddresses(Order $order)
@@ -110,12 +100,16 @@ class PostCheckoutListener
$exactId = $product->exact_id; $exactId = $product->exact_id;
$unitPrice = (float) $orderItem->tax_free_price / (int) $orderItem->quantity; $unitPrice = (float) $orderItem->tax_free_price / (int) $orderItem->quantity;
$quantity = $orderItem->quantity; $quantity = $orderItem->quantity;
$this->salesOrderItems[] = [
$item = [
'Description' => $name, 'Description' => $name,
'Item' => $exactId, 'Item' => $exactId,
'UnitPrice' => $unitPrice, 'UnitPrice' => $unitPrice,
'Quantity' => $quantity 'Quantity' => $quantity
]; ];
if ($this->orderDiscount !== null) {
$item['Discount'] = $this->orderDiscount;
}
$this->salesOrderItems[] = $item;
} else { } else {
$productSet = $this->dbProductsBySku[$product->sku]; $productSet = $this->dbProductsBySku[$product->sku];
$quantity = $orderItem->quantity; $quantity = $orderItem->quantity;
@@ -142,19 +136,24 @@ class PostCheckoutListener
]; ];
} }
} }
$setDiscountFactor = (float) $orderItem->tax_free_price / $setPriceSingleProducts;
ob_start();
var_dump($setProducts);
file_put_contents('flo.txt', ob_get_contents());
ob_end_clean();


$setDiscountFactor = 1 - $orderItem->tax_free_price / $setPriceSingleProducts;
foreach ($setProducts as $setProduct) { foreach ($setProducts as $setProduct) {
$this->salesOrderItems[] = [
$item = [
'Description' => $setProduct['product']['name'], 'Description' => $setProduct['product']['name'],
'Item' => $setProduct['product']['exact_id'], 'Item' => $setProduct['product']['exact_id'],
'UnitPrice' => round($setProduct['netPrice'] * $setDiscountFactor, 2),
'Quantity' => $quantity * $setProduct['qty']
'UnitPrice' => $setProduct['netPrice'],
'Quantity' => $quantity * $setProduct['qty'],
]; ];

if ($this->orderDiscount !== null) {
$setProductNetPrice = $setProduct['netPrice'] * (1 - $setDiscountFactor);
$discountedPrice = $setProductNetPrice * (1 - $this->orderDiscount);
$item['Discount'] = 1 - ($discountedPrice / $setProduct['netPrice']);
} else {
$item['Discount'] = $setDiscountFactor;
}
$this->salesOrderItems[] = $item;
} }
} }
} }
@@ -173,8 +172,30 @@ class PostCheckoutListener
); );
} }


private function setSurCharges()
{
/** @var ProductCollectionSurcharge $orderSurcharge */
foreach ($this->order->getSurcharges() as $orderSurcharge) {
switch ($orderSurcharge->type) {
case self::SURCHAGRGE_SHIPPING:
$this->shippingItem = [
'Description' => $orderSurcharge->label,
'Item' => self::SHIPPING_ITEM_ID,
'UnitPrice' => round($orderSurcharge->total_price / self::VAT_FACTOR, 2),
'Quantity' => 1
];
break;
case self::SURCHAGRGE_RULE:
$discountValuePercentage = round((($orderSurcharge->total_price * -1) / $this->order->subtotal), 2);
$this->orderDiscount = $this->orderDiscount === null ? $discountValuePercentage : $this->orderDiscount + $discountValuePercentage;
break;
}
}
}

private function createSalesOrderData() private function createSalesOrderData()
{ {
$this->salesOrderItems[] = $this->shippingItem;
$date = new \DateTime('now'); $date = new \DateTime('now');
$res = [ $res = [
'Description' => $this->order->id, 'Description' => $this->order->id,
@@ -187,13 +208,6 @@ class PostCheckoutListener
if ($this->shippingCustomer !== null) { if ($this->shippingCustomer !== null) {
$res['DeliverTo'] = $this->shippingCustomer->ID; $res['DeliverTo'] = $this->shippingCustomer->ID;
} }
// if ($discount !== null) {
// $res['Discount'] = $shippingCustomer->ID;
// }
ob_start();
var_dump($res);
file_put_contents('dan.txt', ob_get_contents());
ob_end_clean();
return $res; return $res;
} }
} }

Загрузка…
Отмена
Сохранить