122 lines
4.3 KiB
PHP
122 lines
4.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace KupShop\BonusProgramBundle\Resources\upgrade;
|
|
|
|
class BonusProgramUpgrade extends \UpgradeNew
|
|
{
|
|
public function check_BonusPointsExchangeTable(): bool
|
|
{
|
|
return $this->checkTableExists('bonus_points_exchange');
|
|
}
|
|
|
|
/** Add table bonus_points_exchange */
|
|
public function upgrade_BonusPointsExchangeTable(): void
|
|
{
|
|
sqlQuery('CREATE TABLE IF NOT EXISTS bonus_points_exchange (
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
id_discount INT UNSIGNED NOT NULL,
|
|
date_created DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
name VARCHAR(60) NOT NULL,
|
|
points INT NOT NULL,
|
|
active TINYINT NOT NULL DEFAULT 1,
|
|
description MEDIUMTEXT DEFAULT NULL,
|
|
CONSTRAINT FK_bonus_points_exchange_discount FOREIGN KEY (id_discount) REFERENCES discounts (id) ON DELETE CASCADE ON UPDATE CASCADE
|
|
)');
|
|
|
|
$this->upgradeOK();
|
|
}
|
|
|
|
public function check_ExchangePositionColumn(): bool
|
|
{
|
|
return $this->checkColumnExists('bonus_points_exchange', 'position');
|
|
}
|
|
|
|
/** Add bonus_points_exchange.position */
|
|
public function upgrade_ExchangePositionColumn(): void
|
|
{
|
|
sqlQuery('ALTER TABLE bonus_points_exchange ADD COLUMN position INT(11) NOT NULL DEFAULT 0 AFTER id');
|
|
|
|
$this->upgradeOK();
|
|
}
|
|
|
|
public function check_ExchangeLanguagesColumn(): bool
|
|
{
|
|
return $this->checkColumnExists('bonus_points_exchange', 'languages');
|
|
}
|
|
|
|
public function upgrade_ExchangeLanguagesColumn(): void
|
|
{
|
|
sqlQuery('ALTER TABLE bonus_points_exchange ADD COLUMN languages TEXT DEFAULT NULL');
|
|
|
|
$this->upgradeOK();
|
|
}
|
|
|
|
public function check_ExchangeDataColumn(): bool
|
|
{
|
|
return $this->checkColumnExists('bonus_points_exchange', 'data');
|
|
}
|
|
|
|
/** Add bonus_points_exchange.data column */
|
|
public function upgrade_ExchangeDataColumn(): void
|
|
{
|
|
sqlQuery('ALTER TABLE bonus_points_exchange ADD COLUMN data MEDIUMTEXT DEFAULT NULL');
|
|
|
|
$this->upgradeOK();
|
|
}
|
|
|
|
public function check_trigger_BonusPointsExchange(): bool
|
|
{
|
|
return $this->checkIfTriggerExists('trigger_bonus_points_exchange');
|
|
}
|
|
|
|
/** Add trigger (incrementing position) to bonus_points_exchange */
|
|
public function upgrade_trigger_BonusPointsExchange(): void
|
|
{
|
|
sqlQuery('CREATE TRIGGER trigger_bonus_points_exchange BEFORE INSERT ON bonus_points_exchange
|
|
FOR EACH ROW
|
|
BEGIN
|
|
IF NEW.position IS NULL THEN
|
|
SET NEW.position = (SELECT COALESCE(MAX(bonus_points_exchange.position) + 1, 0) FROM bonus_points_exchange);
|
|
END IF;
|
|
END;');
|
|
$this->upgradeOK();
|
|
}
|
|
|
|
public function check_BonusPointsEarning()
|
|
{
|
|
return isset(\Settings::getDefault()->bonus_program['points_from_price_percent']);
|
|
}
|
|
|
|
/** Add 'bonus_points_earning' order discount, remove points_from_price_percent from settings */
|
|
public function upgrade_BonusPointsEarning()
|
|
{
|
|
$settings = \Settings::getDefault();
|
|
$bonus_program = $settings->bonus_program;
|
|
$points_from_price_percent = intval($bonus_program['points_from_price_percent']);
|
|
|
|
sqlQueryBuilder()->insert('order_discounts')
|
|
->directValues(['name' => 'Zisk bodů', 'active' => 'Y'])
|
|
->execute();
|
|
$id_order_discount = sqlInsertId();
|
|
$data = ['points_from_price_percent' => $points_from_price_percent, 'filter' => []];
|
|
sqlQueryBuilder()->insert('order_discounts_actions')
|
|
->directValues([
|
|
'id_order_discount' => $id_order_discount,
|
|
'type' => 'bonus_points_earning',
|
|
'data' => json_encode($data),
|
|
])->execute();
|
|
|
|
unset($bonus_program['points_from_price_percent']);
|
|
$settings->saveValue('bonus_program', $bonus_program);
|
|
|
|
sqlQuery("INSERT INTO order_discounts_orders (id_order, id_order_discount)
|
|
SELECT bp.id_order, '{$id_order_discount}' AS id_order_discount
|
|
FROM bonus_points bp JOIN orders o ON bp.id_order = o.id
|
|
WHERE bp.id_order IS NOT NULL AND bp.status != 'deleted'
|
|
GROUP BY bp.id_order");
|
|
$this->upgradeOK();
|
|
}
|
|
}
|