-- 013 - the Stripe invoice raised for an establishment order.
--
-- Checkout creates a post-payment Invoice (invoice_creation), which is the
-- buyer's facture. We keep its id and hosted URL so the confirmation page and
-- the order history can link to it without an API call every time.
--
-- Re-runnable: every ALTER is guarded on information_schema.

-- stripe_invoice_id
SET @has := (SELECT COUNT(*) FROM information_schema.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'orders' AND COLUMN_NAME = 'stripe_invoice_id');
SET @sql := IF(@has = 0,
  'ALTER TABLE `orders` ADD COLUMN `stripe_invoice_id` varchar(255) DEFAULT NULL AFTER `stripe_customer_id`',
  'DO 0');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

-- invoice_url (hosted invoice page; it carries the PDF download)
SET @has := (SELECT COUNT(*) FROM information_schema.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'orders' AND COLUMN_NAME = 'invoice_url');
SET @sql := IF(@has = 0,
  'ALTER TABLE `orders` ADD COLUMN `invoice_url` varchar(1024) DEFAULT NULL AFTER `stripe_invoice_id`',
  'DO 0');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
