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

278 lines
11 KiB
PHP

<?php
use KupShop\AdminBundle\Util\ActivityLog;
use KupShop\KupShopBundle\Util\Compat\ServiceContainer;
use KupShop\OrderingBundle\Util\Discount\DiscountGenerator;
use Query\Operator;
$main_class = 'OrderDiscountsCoupons';
class OrderDiscountsCoupons extends Window
{
protected $nameField = 'descr';
protected $tableName = 'discounts';
protected $template = 'window/OrderDiscountsCoupons.tpl';
protected $required = ['descr'];
protected $defaults = [
'condition_type' => 'generate_coupon',
'condition_value' => '',
'date_added' => null,
];
public function get_vars()
{
$vars = parent::get_vars();
$pageVars = getVal('body', $vars);
if ($this->getID()) {
$pageVars['data']['uses_max'] = sqlQueryBuilder()->select('COUNT(*)')->from('discounts_coupons')
->where(Operator::equals(['id_discount' => $this->getID()]))->execute()->fetchOne();
$pageVars['data']['uses_count'] = sqlQueryBuilder()->select('COUNT(*)')->from('discounts_coupons')
->where(Operator::equals(['id_discount' => $this->getID(), 'used' => 'Y']))->execute()->fetchOne();
}
$this->unserializeCustomData($pageVars['data']);
$vars['body'] = $pageVars;
return $vars;
}
public function getData()
{
$data = parent::getData();
if (getVal('Submit')) {
if (empty($data['date_from'])) {
$data['date_from'] = date('Y-m-d H:i');
} else {
$data['date_from'] = $this->prepareDateTime($data['date_from']);
}
$data['date_to'] = $this->prepareDateTime($data['date_to'] ?? null);
$data['date_added'] = $this->prepareDateTime($data['date_added'] ?? null);
if (empty($data['date_added'])) {
unset($data['date_added']);
}
if (!empty($data['validity_type'])) {
if ($data['validity_type'] == 'M') {
$data['date_from'] = '';
$data['date_to'] = '';
$data['data']['valid_days'] = '';
} elseif ($data['validity_type'] == 'D') {
$data['date_from'] = '';
$data['date_to'] = '';
$data['data']['valid_months'] = '';
} elseif ($data['validity_type'] == 'R') {
$data['data']['valid_months'] = '';
$data['data']['valid_days'] = '';
}
}
if ($this->getID()) {
$data['data'] = array_replace($this->getCustomData(), $data['data'] ?? []);
}
$this->serializeCustomData($data);
}
return $data;
}
public function handleUpdate()
{
$SQL = parent::handleUpdate();
if ($SQL) {
$discount_data = $this->getCustomData();
if (empty($discount_data['bg_img']) || empty($discount_data['SVG']) || empty($discount_data['JSON'])) {
$discountGenerator = ServiceContainer::getService(DiscountGenerator::class);
$discount['ID'] = $this->getID();
$discount['data'] = $discount_data;
$discount_data = $discountGenerator->generateDefaultDesign($discount);
if ($discount_data) {
$this->updateSQL('discounts', ['data' => json_encode($discount_data)], ['id' => $this->getID()]);
}
}
}
return $SQL;
}
public function handleUpload()
{
if (!empty($_FILES['picture']['name'])) {
$discountGenerator = ServiceContainer::getService(DiscountGenerator::class);
$format = pathinfo($_FILES['picture']['name'], PATHINFO_EXTENSION);
$filename = $this->getID().'.'.$format;
$path = $discountGenerator->getCouponDataPath();
$ret = rename($_FILES['picture']['tmp_name'], $path.$filename);
if ($ret) {
$img = ['path' => $path, 'original' => $filename];
$img = $discountGenerator->generatePNG($this->getID(), $img);
$discount_data = array_replace($this->getCustomData(), $this->getData()['data'] ?? []);
$discount_data['bg_img'] = $img;
$this->updateSQL('discounts', ['data' => json_encode($discount_data)], ['id' => $this->getID()]);
addActivityLog(ActivityLog::SEVERITY_NOTICE,
ActivityLog::TYPE_CHANGE,
sprintf(translate('activityPhotoAdded', 'OrderDiscountsCoupons'), $this->getData()['descr']));
} else {
$this->addError(translate('errorBadPhoto', 'OrderDiscountsCoupons'));
}
}
$this->returnOK();
}
public function handleDeleteCoupon()
{
if ($this->getID() && ($couponID = getVal('couponID'))) {
$code = sqlFetchAssoc($this->selectSQL('discounts_coupons', ['id_discount' => $this->ID, 'id' => $couponID], ['code']));
if ($code) {
$this->deleteSQL('discounts_coupons', ['id_discount' => $this->ID, 'id' => $couponID]);
addActivityLog(ActivityLog::SEVERITY_NOTICE, ActivityLog::TYPE_CHANGE, 'Smazán kupón: '.$code['code']);
}
}
redirection('REFERER');
}
public function handleUseCoupon()
{
if ($this->getID() && ($couponID = getVal('couponID'))) {
$code = sqlFetchAssoc($this->selectSQL('discounts_coupons', ['id_discount' => $this->ID, 'id' => $couponID], ['code', 'used']));
if ($code && ($code['used'] == 'N')) {
$this->updateSQL('discounts_coupons', ['used' => 'Y'], ['id' => $couponID]);
addActivityLog(ActivityLog::SEVERITY_NOTICE, ActivityLog::TYPE_CHANGE, 'Uplatnění generovaného kódu - kód: '.$code['code']);
}
}
redirection('REFERER');
}
public function handleActivateCoupon()
{
if ($this->getID() && ($couponID = getVal('couponID'))) {
$code = sqlFetchAssoc($this->selectSQL('discounts_coupons',
['id_discount' => $this->ID, 'id' => $couponID]));
if ($code && empty($code['date_activated'])) {
if ($discount_data = $this->selectSQL('discounts', ['id' => $code['id_discount']], ['data'])->fetchOne()) {
$code['data'] = $discount_data;
}
$discountGenerator = ServiceContainer::getService(DiscountGenerator::class);
list($date_from, $date_to) = $discountGenerator->getValidDates($code);
$this->updateSQL('discounts_coupons',
['date_activated' => date('Y-m-d H:i:s'), 'date_from' => $date_from, 'date_to' => $date_to],
['id' => $couponID]);
addActivityLog(ActivityLog::SEVERITY_NOTICE,
ActivityLog::TYPE_CHANGE,
'Aktivování generovaného kódu - kód: '.$code['code']);
}
}
redirection('REFERER');
}
public function handleReactiveCoupon()
{
if ($this->getID() && ($couponID = getVal('couponID'))) {
$code = sqlFetchAssoc($this->selectSQL('discounts_coupons', ['id_discount' => $this->ID, 'id' => $couponID], ['code', 'used']));
if ($code && ($code['used'] == 'Y')) {
$this->updateSQL('discounts_coupons', ['id_order_used' => null, 'used' => 'N'], ['id' => $couponID]);
addActivityLog(ActivityLog::SEVERITY_NOTICE,
ActivityLog::TYPE_CHANGE,
'Znovu zaktivnění generovaného kódu - kód: '.$code['code']);
}
}
redirection('REFERER');
}
public function handleViewPDF()
{
if ($this->getID()) {
$discount_data = $this->getCustomData();
$discountGenerator = ServiceContainer::getService(DiscountGenerator::class);
if ($couponID = getVal('couponID')) {
$coupon = sqlQueryBuilder()->select('id, code, DATE_FORMAT(date_to, "%d.%m.%Y") AS date_to, price, currency')->from('discounts_coupons')
->andWhere(Operator::equals(['id_discount' => $this->getID()]))
->andWhere(Operator::equals(['id' => $couponID]))
->execute()->fetch();
} else {
$discount = $this->getObject();
$code = $discount['condition_value'].$discountGenerator->generateRandomCode();
$date_to = date('d.m.Y',
time() + 86400 *
(!empty($discount_data['valid_months']) ? 30.5 * $discount_data['valid_months'] :
(!empty($discount_data['valid_days']) ? $discount_data['valid_days'] : 30.5)
)
);
$coupon = ['id' => $couponID, 'code' => $code, 'date_to' => $date_to];
}
if ($coupon) {
$pdf = $discountGenerator->generatePDFCoupon($coupon, $discount_data);
if ($pdf) {
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="Coupon_'.$coupon['code'].'.pdf"');
echo $pdf;
} else {
$this->returnError(translate('PDFcouponError', 'OrderDiscountsCoupons'));
}
}
}
}
public function handleSendEmail()
{
if ($this->getID() && ($couponID = getVal('couponID'))) {
$coupon = sqlQueryBuilder()->select('id, code, DATE_FORMAT(date_to, "%d.%m.%Y") AS date_to, id_order_purchased')->from('discounts_coupons')
->andWhere(Operator::equals(['id_discount' => $this->getID()]))
->andWhere(Operator::equals(['id' => $couponID]))
->execute()->fetch();
if ($coupon && !empty($coupon['id_order_purchased'])) {
$id_order = $coupon['id_order_purchased'];
$order = new Order($id_order);
$order->createFromDB($id_order);
if (!empty($order->invoice_email)) {
$discountGenerator = ServiceContainer::getService(DiscountGenerator::class);
$sent = $discountGenerator->sendCouponsByEmail($order);
if ($sent) {
$this->returnOK("Odesláno na email {$order->invoice_email}");
} else {
$this->returnError("Nepodařilo se odeslat na email {$order->invoice_email}");
}
}
}
}
}
public function handleGetDefaultDesign()
{
if ($this->getID()) {
$discountGenerator = ServiceContainer::getService(DiscountGenerator::class);
$discount['ID'] = $this->getID();
$discount['data'] = getVal('data', $this->getData());
$discount_data = $discountGenerator->generateDefaultDesign($discount);
if ($discount_data) {
$this->updateSQL('discounts', ['data' => json_encode($discount_data)], ['id' => $this->getID()]);
}
}
$this->returnOK();
}
}
return OrderDiscountsCoupons::class;