Files
kupshop/bundles/KupShop/OrderDiscountBundle/Actions/UseCouponAction.php
2025-08-02 16:30:27 +02:00

65 lines
2.1 KiB
PHP

<?php
namespace KupShop\OrderDiscountBundle\Actions;
use KupShop\OrderDiscountBundle\Entity\OrderDiscount;
use KupShop\OrderingBundle\Entity\Purchase\PurchaseState;
use Query\Operator;
class UseCouponAction extends GlobalDiscountAction
{
protected static $type = 'use_coupon';
protected static $position = 1;
protected $adminTemplate = 'actions/use_coupon.tpl';
public function applyResult(PurchaseState &$purchaseState, OrderDiscount $orderDiscount, array $data)
{
$this->messages = [];
$total_price = $purchaseState->getProductsTotalPrice();
$data['id'] = $orderDiscount->getId();
$name = $orderDiscount->getDisplayName();
if ($id_coupon = $data['trigger_data']['generated_coupon']['id'] ?? null) {
$coupon = sqlQueryBuilder()->select('price, currency')->from('discounts_coupons')
->andWhere(Operator::equals(['id' => $id_coupon]))
->execute()->fetchAssociative();
if ($price = floatval($coupon['price'] ?? null)) {
$data['discount'] = $price;
$data['unit'] = $coupon['currency'];
}
}
$note = $this->createItemNote($data, $name);
$discountPrice = $this->discountUtil->calculateDiscountPrice($total_price->getPriceWithVat(), $data, $name);
// using `value(2)` to keep price in note compatible with price applied to order item row see DiscountPurchaseItem.php:29 (getPriceWithVat)
$note['coupon_price'] = $discountPrice->value(2);
$this->addDiscountItem($discountPrice, $name, $data, $orderDiscount, $note, $purchaseState);
}
public function handleData($data)
{
$data['divideDiscountPrice'] = 'N';
return parent::handleData($data);
}
public function checkValidity(&$data)
{
return true;
}
public function createItemNote($data = [], &$name = ''): array
{
$note = parent::createItemNote($data, $name);
$note['partial'] = (($data['partial'] ?? 'N') == 'Y');
return $note;
}
public function getDelayedExecution(): ?int
{
return 1;
}
}