1 -- @tag: add_tax_id_to_acc_trans
 
   2 -- @description: Neue Spalte tax_id in der acc_trans
 
   3 -- @depends: release_3_0_0 charts_without_taxkey
 
   6   --Neue Spalte tax_id in acc_trans:
 
   7   ALTER TABLE acc_trans ADD tax_id integer;
 
   9   --Spalte mit Werten füllen:
 
  10   UPDATE acc_trans ac SET tax_id=
 
  11                 (SELECT tk.tax_id FROM taxkeys tk 
 
  12                       WHERE tk.taxkey_id=ac.taxkey 
 
  13                       AND tk.startdate <= COALESCE(
 
  14                             (SELECT ar.deliverydate FROM ar WHERE ar.id=ac.trans_id), 
 
  15                             (SELECT ar.transdate FROM ar WHERE ar.id=ac.trans_id), 
 
  16                             (SELECT ap.transdate FROM ap WHERE ap.id=ac.trans_id), 
 
  17                             (SELECT gl.transdate FROM gl WHERE gl.id=ac.trans_id), 
 
  19                       ORDER BY startdate DESC LIMIT 1);
 
  21   --Spalten, die noch null sind (nur bei Einträgen möglich, wo auch taxkey null ist)
 
  22   UPDATE acc_trans SET tax_id= (SELECT id FROM tax WHERE taxkey=0 LIMIT 1) WHERE tax_id IS NULL;
 
  24   --tax_id als Pflichtfeld definieren:
 
  25   ALTER TABLE acc_trans ALTER tax_id SET NOT NULL;