1 -- @tag: file_storage_dunning_documents
2 -- @description: Dateien f. Mahnungen von gemahnter Rechnung zum Mahnlauf verschieben
3 -- @depends: file_storage_dunning_invoice
5 -- for the original invoice, assume that the dunning_id is the one from a dunning row where the trans_id is
6 -- the old files object_id (the orig. invoice) and the itime of both tables are (almost) equal
8 (SELECT dunning.dunning_id, files.id FROM files LEFT JOIN dunning ON (dunning.trans_id = files.object_id)
9 WHERE object_type ILIKE 'dunning_orig_invoice' AND file_type LIKE 'document' AND source LIKE 'created'
10 AND ABS(EXTRACT(EPOCH FROM (dunning.itime - files.itime))) < 0.1)
11 UPDATE files SET object_type = 'dunning', object_id = (SELECT dunning_id FROM table_files WHERE table_files.id = files.id)
12 WHERE EXISTS (SELECT id FROM table_files WHERE table_files.id = files.id);
14 -- the dunning_id for the following types can be found in the filename
15 UPDATE files SET object_type = 'dunning', object_id = substring(file_name FROM '(\d+).pdf')::INT
16 WHERE (object_type LIKE 'dunning1' OR object_type LIKE 'dunning2' OR object_type LIKE 'dunning3' OR object_type LIKE 'dunning_invoice')
17 AND file_type LIKE 'document' AND source LIKE 'created';