X-Git-Url: http://wagnertech.de/gitweb/gitweb.cgi/mfinanz.git/blobdiff_plain/5357aec06314c90ff1143fba290734bf4041f03b..464f44accd46ed0b2e1f7e2459f53337a6720a7f:/sql/Pg-upgrade2/remove_terms_add_payment_id.sql

diff --git a/sql/Pg-upgrade2/remove_terms_add_payment_id.sql b/sql/Pg-upgrade2/remove_terms_add_payment_id.sql
new file mode 100644
index 000000000..986f433ab
--- /dev/null
+++ b/sql/Pg-upgrade2/remove_terms_add_payment_id.sql
@@ -0,0 +1,23 @@
+-- @tag: remove_terms_add_payment_id
+-- @description: In betroffenen Tabellen die veraltete Spalte Â»termsÂ« lÃ¶schen und dort, wo sie fehlt, payment_id ergÃ¤nzen
+-- @depends: release_3_2_0
+
+ALTER TABLE delivery_orders ADD COLUMN payment_id INTEGER;
+ALTER TABLE delivery_orders ADD FOREIGN KEY (payment_id) REFERENCES payment_terms (id);
+
+UPDATE delivery_orders
+SET payment_id = (
+  SELECT oe.payment_id
+  FROM record_links rl
+  LEFT JOIN oe ON rl.from_id = oe.id
+  WHERE (rl.from_table = 'oe')
+    AND (rl.to_table   = 'delivery_orders')
+    AND (rl.to_id      = delivery_orders.id)
+  ORDER BY rl.itime DESC
+  LIMIT 1
+);
+
+ALTER TABLE ar              DROP COLUMN terms;
+ALTER TABLE customer        DROP COLUMN terms;
+ALTER TABLE delivery_orders DROP COLUMN terms;
+ALTER TABLE vendor          DROP COLUMN terms;
