Telefonnotizen: in Berichten Angebot/Auftrag filtern können.
[kivitendo-erp.git] / sql / Pg-upgrade2 / gl_storno.sql
1 -- @tag: gl_storno
2 -- @description: Spalten für Dialogbuchen zum Speichern, ob diese Buchung storniert wurde bzw. für welche andere Buchung diese eine Stornobuchung ist
3 -- @depends: release_2_4_2
4 ALTER TABLE gl ADD COLUMN storno boolean;
5 ALTER TABLE gl ALTER COLUMN storno SET DEFAULT 'f';
6
7 ALTER TABLE gl ADD COLUMN storno_id integer;
8 ALTER TABLE gl ADD FOREIGN KEY (storno_id) REFERENCES gl (id);
9
10 UPDATE gl SET storno = 'f';
11
12 UPDATE gl SET storno = 't'
13   WHERE (reference  LIKE 'Storno-%')
14    AND (description LIKE 'Storno-%')
15    AND EXISTS
16      (SELECT gl2.id
17       FROM gl gl2
18       WHERE ('Storno-' || gl2.reference   = gl.reference)
19         AND ('Storno-' || gl2.description = gl.description)
20         AND (gl2.id < gl.id));
21
22 UPDATE gl SET storno = 't'
23   WHERE (reference   NOT LIKE 'Storno-%')
24     AND (description NOT LIKE 'Storno-%')
25     AND EXISTS
26     (SELECT gl2.id
27      FROM gl gl2
28      WHERE ('Storno-' || gl.reference   = gl2.reference)
29        AND ('Storno-' || gl.description = gl2.description)
30        AND (gl2.id > gl.id));
31
32 UPDATE gl SET storno_id =
33   (SELECT id
34    FROM gl gl2
35    WHERE ('Storno-' || gl2.reference   = gl.reference)
36      AND ('Storno-' || gl2.description = gl.description)
37      AND (gl2.id < gl.id)
38    ORDER BY itime
39    LIMIT 1)
40   WHERE storno
41     AND (reference   LIKE 'Storno-%')
42     AND (description LIKE 'Storno-%');