74 lines
2.7 KiB
PHP
74 lines
2.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace KupShop\OrderDiscountBundle\Admin\Actions;
|
|
|
|
use KupShop\AdminBundle\Admin\Actions\AbstractAction;
|
|
use KupShop\AdminBundle\Admin\Actions\ActionResult;
|
|
use KupShop\AdminBundle\Admin\Actions\IAction;
|
|
use KupShop\KupShopBundle\Context\CurrencyContext;
|
|
use KupShop\KupShopBundle\Util\Contexts;
|
|
use KupShop\OrderingBundle\Util\Discount\DiscountGenerator;
|
|
use Query\Operator;
|
|
use Symfony\Contracts\Service\Attribute\Required;
|
|
|
|
class GenerateOrderDiscountsCouponsAction extends AbstractAction implements IAction
|
|
{
|
|
#[Required]
|
|
public DiscountGenerator $discountGenerator;
|
|
|
|
public function getName(): string
|
|
{
|
|
return 'Vygenerovat kódy';
|
|
}
|
|
|
|
public function getTypes(): array
|
|
{
|
|
return ['OrderDiscountsCoupons'];
|
|
}
|
|
|
|
public function showInMassEdit()
|
|
{
|
|
return true;
|
|
}
|
|
|
|
public function execute(&$data, array $config, string $type): ActionResult
|
|
{
|
|
if ($ID = $this->getId()) {
|
|
if ($qty = getVal('generate_coupons_qty', $data)) {
|
|
$activated = (getVal('activated', $data, 'Y') == 'Y');
|
|
$customData = [];
|
|
if (getVal('coupon_descr', $data)) {
|
|
$customData['coupon_descr'] = $data['coupon_descr'];
|
|
}
|
|
$price = floatval(getVal('price', $data));
|
|
if ($price > 0) {
|
|
$currency = Contexts::get(CurrencyContext::class)->getOrDefault(getVal('currency', $data));
|
|
$customData['original_price'] = "{$price} {$currency->getSymbol()}";
|
|
} else {
|
|
$price = $currency = null;
|
|
}
|
|
$generated = $this->discountGenerator->generateNewCoupons(id_discount: $ID, qty: $qty,
|
|
activated: $activated, price: $price, currency: $currency);
|
|
|
|
if ($customData) {
|
|
sqlQueryBuilder()->update('discounts_coupons')
|
|
->directValues(['data' => json_encode($customData)])
|
|
->andWhere(Operator::equals(['id_discount' => $ID]))
|
|
->andWhere(Operator::inStringArray($generated['coupons'], 'code'))
|
|
->execute();
|
|
}
|
|
|
|
$result = new ActionResult(true, 'Úspěšně vygenerovaných poukazů: '.$generated['count'].'.');
|
|
$message = "<b>Úspěšně vygenerovaných poukazů: </b>{$generated['count']}.
|
|
<p><b>Seznam kódů: </b><br>".join('<br>', $generated['coupons']).'</p>';
|
|
|
|
return $result->setHTMLMessage($message);
|
|
}
|
|
}
|
|
|
|
return new ActionResult(false);
|
|
}
|
|
}
|