-- BEGIN CHANGE: optional buy quantity (stackable offers, e.g. Imbuement Scrolls)
-- Run on ot_new. Safe to re-run.
-- max_quantity hard cap = 100 (protocol 8.60 / doCreateItemEx stackable limit).

SET @has_allow := (
  SELECT COUNT(*)
  FROM information_schema.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE()
    AND TABLE_NAME = 'z_shop_offer'
    AND COLUMN_NAME = 'allow_quantity'
);
SET @sql_allow := IF(
  @has_allow = 0,
  'ALTER TABLE `z_shop_offer`
     ADD COLUMN `allow_quantity` TINYINT UNSIGNED NOT NULL DEFAULT 0 AFTER `active`,
     ADD COLUMN `max_quantity` INT UNSIGNED NOT NULL DEFAULT 100 AFTER `allow_quantity`',
  'SELECT 1'
);
PREPARE stmt_allow FROM @sql_allow;
EXECUTE stmt_allow;
DEALLOCATE PREPARE stmt_allow;

-- Enable qty for Temporary Imbuement Scrolls (itemids 12742-12748)
UPDATE `z_shop_offer` o
INNER JOIN `z_shop_offer_group` g ON g.`offer_id` = o.`id`
INNER JOIN `z_shop_offer_option` op ON op.`group_id` = g.`id`
INNER JOIN `z_shop_offer_option_item` oi ON oi.`option_id` = op.`id`
SET o.`allow_quantity` = 1,
    o.`max_quantity` = 100
WHERE o.`catalog` = 'donate'
  AND o.`offer_type` = 'item'
  AND oi.`itemid` IN (12742, 12743, 12744, 12745, 12746, 12747, 12748);

-- END CHANGE
