195 lines
5.7 KiB
PHP
195 lines
5.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace External\PompoBundle\Util\Order;
|
|
|
|
use External\PompoBundle\Util\Ordering\OrderingUtil;
|
|
use KupShop\KupShopBundle\Util\Price\Price;
|
|
use KupShop\OrderingBundle\Entity\Order\OrderItem;
|
|
use KupShop\OrderingBundle\Util\Order\OrderItemInfo;
|
|
use Query\Operator;
|
|
|
|
/**
|
|
* Common functions for order synchronization (DataGo, DRS).
|
|
*/
|
|
trait OrderSynchronizerTrait
|
|
{
|
|
protected function getOrderTypeSpec(?array $types): callable
|
|
{
|
|
$field = 'JSON_VALUE(COALESCE(o.note_admin, "{}"), "$.orderType")';
|
|
|
|
return $types === null ? Operator::equalsNullable([$field => $types]) : Operator::inStringArray($types, $field);
|
|
}
|
|
|
|
protected function getOrderNumber(\Order $order): string
|
|
{
|
|
if ($order->getFlags()['DSE'] ?? false) {
|
|
if ($expandoOrderId = ($order->getData('expando')['orderId'] ?? false)) {
|
|
return $expandoOrderId;
|
|
}
|
|
}
|
|
|
|
return $order->order_no;
|
|
}
|
|
|
|
protected function getOrderNote(\Order $order): string
|
|
{
|
|
$note = [(string) $order->note_user];
|
|
|
|
$discounts = $order->getData('discounts');
|
|
if (!empty($discounts['used_coupons'])) {
|
|
$note[] = 'Pouk: '.implode(', ', $discounts['used_coupons']);
|
|
}
|
|
|
|
return implode('; ', array_filter($note));
|
|
}
|
|
|
|
protected function isOrderItemTransportCharge(OrderItem $item): bool
|
|
{
|
|
if (($item->getNote()['item_type'] ?? false) === OrderItemInfo::TYPE_CHARGE) {
|
|
if (in_array($item->getNote()['id_charge'] ?? false, OrderingUtil::CHARGES_HANDLING_FEE)) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
protected function changeOrderStatus(\Order $order, int $status, ?string $emailType = null): void
|
|
{
|
|
if (!$this->isOrderStatusNext($order, $status)) {
|
|
return;
|
|
}
|
|
|
|
$forceSendMail = null;
|
|
// U Expando objednavek nechceme posilat maily
|
|
if ($order->getFlags()['DSE'] ?? false) {
|
|
$forceSendMail = false;
|
|
}
|
|
|
|
$order->changeStatus($status, null, $forceSendMail, null, $emailType);
|
|
}
|
|
|
|
/**
|
|
* Vrati true/false na zaklade toho, zda $status je vyssí stav, nez aktualni stav objednavky.
|
|
*
|
|
* Pouziva se ke kontrole, aby se neprovedo prepnuti stavu zpet (do nizsiho stavu).
|
|
*/
|
|
protected function isOrderStatusNext(\Order $order, int $status): bool
|
|
{
|
|
$statuses = array_keys(getOrderStatuses());
|
|
|
|
if (($orderStatusIndex = array_search($order->status, $statuses)) === false) {
|
|
return false;
|
|
}
|
|
|
|
if (($newStatusIndex = array_search($status, $statuses)) === false) {
|
|
return false;
|
|
}
|
|
|
|
if ($newStatusIndex > $orderStatusIndex) {
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
protected function isOrderInPerson(\Order $order): bool
|
|
{
|
|
if ($deliveryType = $order->getDeliveryType()) {
|
|
if ($delivery = $deliveryType->getDelivery()) {
|
|
if ($delivery instanceof \OdberNaProdejne || $delivery->isInPerson()) {
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
protected function setOrderPackageNumber(\Order $order, string $packageNumber): void
|
|
{
|
|
if (!empty($order->package_id)) {
|
|
return;
|
|
}
|
|
|
|
$order->package_id = $packageNumber;
|
|
|
|
sqlQueryBuilder()
|
|
->update('orders')
|
|
->directValues(
|
|
[
|
|
'package_id' => $packageNumber,
|
|
]
|
|
)
|
|
->where(Operator::equals(['id' => $order->id]))
|
|
->execute();
|
|
|
|
$order->logHistory(sprintf('[DataGo] Číslo balíku: %s', $packageNumber));
|
|
}
|
|
|
|
/** Returns split delivery and payment into single items */
|
|
protected function splitDeliveryItem(\Order $order, ?OrderItem $deliveryItem): array
|
|
{
|
|
if (!$deliveryItem || !($deliveryType = $this->getOrderDeliveryType($order))) {
|
|
return [];
|
|
}
|
|
|
|
$deliveryPrice = 0;
|
|
$paymentPrice = 0;
|
|
|
|
$deliveryItemPrice = $deliveryItem->getTotalPrice()->getPriceWithVat()->abs()->asFloat();
|
|
if ($deliveryItemPrice > 0 && $deliveryType->price_payment) {
|
|
$paymentPrice = $deliveryType->price_payment->getPriceWithVat()->asFloat();
|
|
$deliveryPrice = $deliveryItemPrice - $paymentPrice;
|
|
if ($deliveryPrice < 0) {
|
|
$deliveryPrice = 0;
|
|
}
|
|
}
|
|
|
|
$result = [];
|
|
|
|
if ($deliveryPrice > 0) {
|
|
$tmpDeliveryItem = [
|
|
'id' => 'D-'.$deliveryItem->getId(),
|
|
'price' => $deliveryPrice,
|
|
'vat' => $deliveryItem->getVat(),
|
|
'quantity' => $deliveryItem->getPieces(),
|
|
];
|
|
|
|
$result['delivery'] = $tmpDeliveryItem;
|
|
}
|
|
|
|
if ($paymentPrice > 0) {
|
|
$tmpPaymentType = [
|
|
'id' => 'P-'.$deliveryItem->getId(),
|
|
'price' => $paymentPrice,
|
|
'vat' => $deliveryItem->getVat(),
|
|
'quantity' => $deliveryItem->getPieces(),
|
|
];
|
|
|
|
$result['payment'] = $tmpPaymentType;
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
protected function getOrderDeliveryType(\Order $order): ?\DeliveryType
|
|
{
|
|
if ($deliveryType = $order->getDeliveryType()) {
|
|
$deliveryType->accept(
|
|
new Price($order->getTotalPrice()->getPriceWithVat(), $order->getTotalPrice()->getCurrency(), 0),
|
|
false,
|
|
$order->getPurchaseState()
|
|
);
|
|
|
|
$deliveryType->getPrice();
|
|
|
|
return $deliveryType;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|