drink-it-cold/sql/migrations/016_deposit_accounts.sql

43 lines
2 KiB
MySQL
Raw Permalink Normal View History

-- Anzahlungskonten pflegbar machen (Verwaltung → Konten).
--
-- Bisher stand das Konto als Schlüssel in bookings.deposit_account und der
-- Kontenstamm im Code. Jetzt gibt es eine Tabelle, die der Administrator
-- pflegt; Buchungen verweisen per Fremdschlüssel darauf.
CREATE TABLE IF NOT EXISTS deposit_accounts (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
label VARCHAR(80) NOT NULL,
holder VARCHAR(120) NOT NULL,
bank VARCHAR(80) NOT NULL DEFAULT '',
iban VARCHAR(34) NOT NULL,
sort_order INT NOT NULL DEFAULT 0,
created_at DATETIME NOT NULL,
updated_at DATETIME NOT NULL,
PRIMARY KEY (id),
KEY idx_deposit_accounts_sort (sort_order, id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
INSERT INTO deposit_accounts (label, holder, bank, iban, sort_order, created_at, updated_at)
VALUES
('Postbank – Michael Schmitz', 'Michael Schmitz', 'Postbank', 'DE12 1001 0010 0464 1261 30', 1, UTC_TIMESTAMP(), UTC_TIMESTAMP()),
('Sparkasse – Michael Schmitz', 'Michael Schmitz', 'Sparkasse', 'DE83 3206 0362 3303 2140 10', 2, UTC_TIMESTAMP(), UTC_TIMESTAMP());
ALTER TABLE bookings ADD COLUMN deposit_account_id BIGINT UNSIGNED NULL AFTER deposit_account;
UPDATE bookings SET deposit_account_id = (SELECT MIN(id) FROM deposit_accounts)
WHERE deposit_account = 'postbank';
UPDATE bookings SET deposit_account_id = (SELECT MAX(id) FROM deposit_accounts)
WHERE deposit_account <> '' AND deposit_account_id IS NULL;
-- Buchungen mit „Anzahlung fällig“, aber ohne gespeichertes Konto (Altbestand
-- aus 015) bekommen das erste Konto, damit die Auftragsbestätigung weiterhin
-- Konto und Frist nennt.
UPDATE bookings SET deposit_account_id = (SELECT MIN(id) FROM deposit_accounts)
WHERE deposit_required = 1 AND deposit_account_id IS NULL;
ALTER TABLE bookings DROP COLUMN deposit_account;
ALTER TABLE bookings
ADD CONSTRAINT fk_bookings_deposit_account FOREIGN KEY (deposit_account_id)
REFERENCES deposit_accounts (id) ON DELETE SET NULL;