79 lines
2.6 KiB
PHP
79 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace KupShop\OrderDiscountBundle\Admin;
|
|
|
|
use KupShop\KupShopBundle\Context\CurrencyContext;
|
|
use KupShop\KupShopBundle\Util\Contexts;
|
|
use KupShop\KupShopBundle\Util\Price\Price;
|
|
use KupShop\KupShopBundle\Util\Price\PriceCalculator;
|
|
use Query\Operator;
|
|
|
|
class OrderDiscountsGeneratedCoupons extends \Window
|
|
{
|
|
protected $tableName = 'discounts_coupons';
|
|
protected $nameField = 'code';
|
|
|
|
public function get_vars()
|
|
{
|
|
$vars = parent::get_vars();
|
|
|
|
$qb = sqlQueryBuilder()->select('o.order_no, dco.*')
|
|
->from('discounts_coupons_orders', 'dco')
|
|
->leftJoin('dco', 'orders', 'o', 'dco.id_order = o.id')
|
|
->andWhere(Operator::equals(['id_discount_coupon' => $this->getID()]));
|
|
if (findModule(\Modules::CURRENCIES)) {
|
|
$qb->addSelect('o.currency');
|
|
}
|
|
$head = ['Obj.', '', '', ''];
|
|
$orders = [];
|
|
foreach ($qb->execute() as $order) {
|
|
if ($order['used_amount']) {
|
|
$currency = Contexts::get(CurrencyContext::class)->getOrDefault($order['currency'] ?? null);
|
|
$used = new Price(toDecimal($order['used_amount']), $currency, 0);
|
|
$order['used_amount'] = $this->printPrice($used);
|
|
$head[2] = 'Sleva';
|
|
|
|
if ($order['remaining_amount']) {
|
|
$remaining = new Price(toDecimal($order['remaining_amount']), $currency, 0);
|
|
$order['remaining_amount'] = $this->printPrice($remaining);
|
|
$order['prev_amount'] = $this->printPrice(PriceCalculator::sub($remaining, $used));
|
|
$head[1] = 'Původní hodnota';
|
|
$head[3] = 'Zbývající částka';
|
|
}
|
|
|
|
$total = !isset($total) ? $used : PriceCalculator::add($total, $used);
|
|
}
|
|
$orders[] = $order;
|
|
}
|
|
if ($orders) {
|
|
$vars['orders'] = ['head' => $head, 'orders' => $orders, 'total' => (isset($total) ? $this->printPrice($total) : null)];
|
|
}
|
|
|
|
return $vars;
|
|
}
|
|
|
|
protected function printPrice(Price $price): string
|
|
{
|
|
return $price->getPriceWithVat(false)->printValue().' '.$price->getCurrency()->getSymbol();
|
|
}
|
|
|
|
public function handleUpdate()
|
|
{
|
|
$data = $this->getData()['data'];
|
|
$data = array_merge($this->getCustomData(), $data);
|
|
$this->setCustomData($data);
|
|
|
|
return true;
|
|
}
|
|
|
|
public function hasRights($name = null)
|
|
{
|
|
return match ($name) {
|
|
self::RIGHT_DUPLICATE => false,
|
|
default => parent::hasRights($name),
|
|
};
|
|
}
|
|
}
|
|
|
|
return OrderDiscountsGeneratedCoupons::class;
|