-- Falls ALTER TABLE wegen bereits vorhandener Spalten fehlschlägt,
-- kannst du die betroffene ALTER-Zeile einfach überspringen.

ALTER TABLE number_ranges ADD COLUMN year_based TINYINT(1) DEFAULT 0;
ALTER TABLE number_ranges ADD COLUMN last_year INT NULL;

INSERT INTO settings (name, value) VALUES
('number_format_offer', '{PREFIX}{YEAR}-{NUMBER}'),
('number_format_invoice', '{PREFIX}{YEAR}-{NUMBER}')
ON DUPLICATE KEY UPDATE name = name;

UPDATE number_ranges
SET prefix = 'ANG-', year_based = 1, last_year = YEAR(CURDATE())
WHERE type = 'offer';

UPDATE number_ranges
SET prefix = 'RE-', year_based = 1, last_year = YEAR(CURDATE())
WHERE type = 'invoice';
