[#17] CreditNote: Overhaul CreditNote
This commit is contained in:
@ -1,17 +1,16 @@
|
||||
using Microsoft.Data.Sqlite;
|
||||
using System;
|
||||
using System.Windows;
|
||||
|
||||
namespace Elwig.Helpers {
|
||||
public static class AppDbUpdater {
|
||||
|
||||
public static readonly int RequiredSchemaVersion = 10;
|
||||
public static readonly int RequiredSchemaVersion = 11;
|
||||
|
||||
private static int _versionOffset = 0;
|
||||
private static readonly Action<SqliteConnection>[] _updaters = new[] {
|
||||
UpdateDbSchema_1_To_2, UpdateDbSchema_2_To_3, UpdateDbSchema_3_To_4, UpdateDbSchema_4_To_5,
|
||||
UpdateDbSchema_5_To_6, UpdateDBSchema_6_To_7, UpdateDbSchema_7_To_8, UpdateDbSchema_8_To_9,
|
||||
UpdateDbSchema_9_To_10,
|
||||
UpdateDbSchema_9_To_10, UpdateDbSchema_10_To_11
|
||||
};
|
||||
|
||||
private static void ExecuteNonQuery(SqliteConnection cnx, string sql) {
|
||||
@ -47,8 +46,7 @@ namespace Elwig.Helpers {
|
||||
|
||||
if (App.VersionMajor > major ||
|
||||
(App.VersionMajor == major && App.VersionMinor > minor) ||
|
||||
(App.VersionMajor == major && App.VersionMinor == minor && App.VersionPatch > patch))
|
||||
{
|
||||
(App.VersionMajor == major && App.VersionMinor == minor && App.VersionPatch > patch)) {
|
||||
long vers = (App.VersionMajor << 24) | (App.VersionMinor << 16) | App.VersionPatch;
|
||||
ExecuteNonQuery(cnx, $"PRAGMA user_version = {vers}");
|
||||
}
|
||||
@ -736,5 +734,71 @@ namespace Elwig.Helpers {
|
||||
ORDER BY year, mgnr, bucket;
|
||||
""");
|
||||
}
|
||||
|
||||
private static void UpdateDbSchema_10_To_11(SqliteConnection cnx) {
|
||||
// Drop columns, if they exist...
|
||||
ExecuteNonQuery(cnx, "ALTER TABLE payment_delivery_part DROP COLUMN price_1");
|
||||
ExecuteNonQuery(cnx, "ALTER TABLE payment_delivery_part DROP COLUMN price_2");
|
||||
ExecuteNonQuery(cnx, "ALTER TABLE payment_delivery_part DROP COLUMN price_3");
|
||||
ExecuteNonQuery(cnx, "ALTER TABLE payment_delivery_part DROP COLUMN price_4");
|
||||
ExecuteNonQuery(cnx, "ALTER TABLE payment_delivery_part DROP COLUMN price_5");
|
||||
ExecuteNonQuery(cnx, "ALTER TABLE payment_delivery_part DROP COLUMN price_6");
|
||||
ExecuteNonQuery(cnx, "ALTER TABLE payment_delivery_part DROP COLUMN price_7");
|
||||
ExecuteNonQuery(cnx, "ALTER TABLE payment_delivery_part DROP COLUMN price_8");
|
||||
ExecuteNonQuery(cnx, "ALTER TABLE payment_delivery_part DROP COLUMN price_9");
|
||||
|
||||
ExecuteNonQuery(cnx, "DROP TRIGGER t_payment_delivery_part_i");
|
||||
ExecuteNonQuery(cnx, """
|
||||
CREATE TRIGGER t_payment_delivery_part_i
|
||||
AFTER INSERT ON payment_delivery_part FOR EACH ROW
|
||||
BEGIN
|
||||
INSERT INTO payment_member (year, avnr, mgnr, net_amount)
|
||||
SELECT year, NEW.avnr, mgnr, NEW.amount FROM delivery WHERE (year, did) = (NEW.year, NEW.did)
|
||||
ON CONFLICT DO UPDATE SET net_amount = net_amount + excluded.net_amount;
|
||||
END;
|
||||
""");
|
||||
|
||||
ExecuteNonQuery(cnx, "DROP TRIGGER t_payment_delivery_part_u");
|
||||
ExecuteNonQuery(cnx, """
|
||||
CREATE TRIGGER t_payment_delivery_part_u
|
||||
AFTER UPDATE ON payment_delivery_part FOR EACH ROW
|
||||
BEGIN
|
||||
UPDATE payment_member
|
||||
SET net_amount = net_amount - OLD.amount
|
||||
WHERE (year, avnr, mgnr) IN (SELECT year, OLD.avnr, mgnr FROM delivery WHERE (year, did) = (OLD.year, OLD.did));
|
||||
INSERT INTO payment_member (year, avnr, mgnr, net_amount)
|
||||
SELECT year, NEW.avnr, mgnr, NEW.amount FROM delivery WHERE (year, did) = (NEW.year, NEW.did)
|
||||
ON CONFLICT DO UPDATE SET net_amount = net_amount + excluded.net_amount;
|
||||
END;
|
||||
""");
|
||||
|
||||
ExecuteNonQuery(cnx, "DROP TRIGGER t_payment_delivery_part_d");
|
||||
ExecuteNonQuery(cnx, """
|
||||
CREATE TRIGGER t_payment_delivery_part_d
|
||||
AFTER DELETE ON payment_delivery_part FOR EACH ROW
|
||||
BEGIN
|
||||
UPDATE payment_member
|
||||
SET net_amount = net_amount - OLD.amount
|
||||
WHERE (year, avnr, mgnr) IN (SELECT year, OLD.avnr, mgnr FROM delivery WHERE (year, did) = (OLD.year, OLD.did));
|
||||
END;
|
||||
""");
|
||||
|
||||
ExecuteNonQuery(cnx, "DROP TRIGGER t_payment_delivery_part_bucket_u");
|
||||
ExecuteNonQuery(cnx, """
|
||||
CREATE TRIGGER t_payment_delivery_part_bucket_u
|
||||
AFTER UPDATE ON payment_delivery_part_bucket FOR EACH ROW
|
||||
BEGIN
|
||||
UPDATE payment_delivery_part
|
||||
SET net_amount = net_amount - OLD.amount
|
||||
WHERE (year, did, dpnr, avnr) = (OLD.year, OLD.did, OLD.dpnr, OLD.avnr);
|
||||
UPDATE payment_delivery_part
|
||||
SET net_amount = net_amount + NEW.amount
|
||||
WHERE (year, did, dpnr, avnr) = (NEW.year, NEW.did, NEW.dpnr, NEW.avnr);
|
||||
END;
|
||||
""");
|
||||
|
||||
ExecuteNonQuery(cnx, "ALTER TABLE payment_member DROP COLUMN amount");
|
||||
ExecuteNonQuery(cnx, "ALTER TABLE payment_member ADD COLUMN amount INTEGER NOT NULL GENERATED ALWAYS AS (ROUND(net_amount * (1 + mod_rel) + mod_abs)) VIRTUAL");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user