3cad8ca8df19a4ad4ffe24f30f859d7d6daf9f75
[kivitendo-erp.git] / sql / Pg-upgrade2 / convert_drafts_to_record_templates.pl
1 # @tag: convert_drafts_to_record_templates
2 # @description: Umwandlung von existierenden Entwürfen in Buchungsvorlagen für die Finanzbuchhaltung
3 # @depends: create_record_template_tables
4 package SL::DBUpgrade2::convert_drafts_to_record_templates;
5
6 use strict;
7 use utf8;
8
9 use YAML;
10
11 use parent qw(SL::DBUpgrade2::Base);
12
13 use SL::DBUtils;
14
15 sub prepare_statements {
16   my ($self) = @_;
17
18   $self->{q_draft} = qq|
19     SELECT description, form
20     FROM drafts
21     WHERE module = ?
22 |;
23
24   $self->{q_template} = qq|
25     INSERT INTO record_templates (
26       template_name, template_type,  customer_id,    vendor_id,
27       currency_id,   department_id,  project_id,     employee_id,
28       taxincluded,   direct_debit,   ob_transaction, cb_transaction,
29       reference,     description,    ordnumber,      notes,
30       ar_ap_chart_id
31     ) VALUES (
32       ?, ? ,?, ?,
33       ?, ? ,?, ?,
34       ?, ? ,?, ?,
35       ?, ? ,?, ?,
36       ?
37     )
38     RETURNING id
39 |;
40
41   $self->{q_item} = qq|
42     INSERT INTO record_template_items (
43       record_template_id,
44       chart_id, tax_id,  project_id,
45       amount1,  amount2, source, memo
46     ) VALUES (
47       ?,
48       ?, ?, ?,
49       ?, ?, ?, ?
50     )
51 |;
52
53   $self->{h_draft}    = $self->dbh->prepare($self->{q_draft})    || die;
54   $self->{h_template} = $self->dbh->prepare($self->{q_template}) || die;
55   $self->{h_item}     = $self->dbh->prepare($self->{q_item})     || die;
56 }
57
58 sub fetch_auxilliary_data {
59   my ($self) = @_;
60
61   $self->{default_currency_id}  = selectfirst_hashref_query($::form, $self->dbh, qq|SELECT currency_id FROM defaults|)->{currency_id};
62   $self->{chart_ids_by_accno}   = { selectall_as_map($::form, $self->dbh, qq|SELECT id, accno FROM chart|,      'accno', 'id') };
63   $self->{currency_ids_by_name} = { selectall_as_map($::form, $self->dbh, qq|SELECT id, name  FROM currencies|, 'name',  'id') };
64 }
65
66 sub finish_statements {
67   my ($self) = @_;
68
69   $self->{h_item}->finish;
70   $self->{h_template}->finish;
71   $self->{h_draft}->finish;
72 }
73
74 sub migrate_ar_drafts {
75   my ($self) = @_;
76
77   $self->{h_draft}->execute('ar') || die $self->{h_draft}->errstr;
78
79   while (my $draft_record = $self->{h_draft}->fetchrow_hashref) {
80     my $draft         = YAML::Load($draft_record->{form});
81     my $currency_id   = $self->{currency_ids_by_name}->{$draft->{currency}};
82     my ($employee_id) = $draft->{employee_id} || (split m{--}, $draft->{employee})[1] || undef;
83
84     next unless $currency_id;
85
86     my @values = (
87       # template_name, template_type, customer_id, vendor_id,
88       $draft_record->{description} // $::locale->text('unnamed record template'),
89       'ar_transaction',
90       $draft->{customer_id} || undef,
91       undef,
92
93       # currency_id, department_id, project_id, employee_id,
94       $currency_id,
95       $draft->{department_id}    || undef,
96       $draft->{globalproject_id} || undef,
97       $employee_id,
98
99       # taxincluded,   direct_debit, ob_transaction, cb_transaction,
100       $draft->{taxincluded}  ? 1 : 0,
101       $draft->{direct_debit} ? 1 : 0,
102       0,
103       0,
104
105       # reference, description, ordnumber, notes,
106       undef,
107       undef,
108       $draft->{ordnumber},
109       $draft->{notes},
110
111       # ar_ap_chart_id
112       $self->{chart_ids_by_accno}->{$draft->{ARselected}},
113     );
114
115     $self->{h_template}->execute(@values) || die $self->{h_template}->errstr;
116     my ($template_id) = $self->{h_template}->fetchrow_array;
117
118     foreach my $row (1..$draft->{rowcount}) {
119       my ($chart_accno) = split m{--}, $draft->{"AR_amount_${row}"};
120       my ($tax_id)      = split m{--}, $draft->{"taxchart_${row}"};
121       my $chart_id      = $self->{chart_ids_by_accno}->{$chart_accno // ''};
122       my $amount        = $::form->parse_amount($self->{format}, $draft->{"amount_${row}"});
123
124       # $tax_id may be 0 as there's an entry in tax with id = 0.
125       # $chart_id must not be 0 as there's no entry in chart with id = 0.
126       next unless $chart_id && (($tax_id // '') ne '');
127
128       @values = (
129         # record_template_id,
130         $template_id,
131
132         # chart_id, tax_id, project_id,
133         $chart_id,
134         $tax_id,
135         $draft->{"project_id_${row}"} || undef,
136
137         # amount1, amount2, source, memo
138         $amount,
139         undef,
140         undef,
141         undef,
142       );
143
144       $self->{h_item}->execute(@values) || die $self->{h_item}->errstr;
145     }
146   }
147 }
148
149 sub migrate_ap_drafts {
150   my ($self) = @_;
151
152   $self->{h_draft}->execute('ap') || die $self->{h_draft}->errstr;
153
154   while (my $draft_record = $self->{h_draft}->fetchrow_hashref) {
155     my $draft       = YAML::Load($draft_record->{form});
156     my $currency_id = $self->{currency_ids_by_name}->{$draft->{currency}};
157
158     next unless $currency_id;
159
160     my @values = (
161       # template_name, template_type, customer_id, vendor_id,
162       $draft_record->{description} // $::locale->text('unnamed record template'),
163       'ap_transaction',
164       undef,
165       $draft->{vendor_id} || undef,
166
167       # currency_id, department_id, project_id, employee_id,
168       $currency_id,
169       $draft->{department_id}    || undef,
170       $draft->{globalproject_id} || undef,
171       undef,
172
173       # taxincluded,   direct_debit, ob_transaction, cb_transaction,
174       $draft->{taxincluded}   ? 1 : 0,
175       $draft->{direct_credit} ? 1 : 0,
176       0,
177       0,
178
179       # reference, description, ordnumber, notes,
180       undef,
181       undef,
182       $draft->{ordnumber},
183       $draft->{notes},
184
185       # ar_ap_chart_id
186       $self->{chart_ids_by_accno}->{$draft->{APselected}},
187     );
188
189     $self->{h_template}->execute(@values) || die $self->{h_template}->errstr;
190     my ($template_id) = $self->{h_template}->fetchrow_array;
191
192     foreach my $row (1..$draft->{rowcount}) {
193       my ($chart_accno) = split m{--}, $draft->{"AP_amount_${row}"};
194       my ($tax_id)      = split m{--}, $draft->{"taxchart_${row}"};
195       my $chart_id      = $self->{chart_ids_by_accno}->{$chart_accno // ''};
196       my $amount        = $::form->parse_amount($self->{format}, $draft->{"amount_${row}"});
197
198       # $tax_id may be 0 as there's an entry in tax with id = 0.
199       # $chart_id must not be 0 as there's no entry in chart with id = 0.
200       next unless $chart_id && (($tax_id // '') ne '');
201
202       @values = (
203         # record_template_id,
204         $template_id,
205
206         # chart_id, tax_id, project_id,
207         $chart_id,
208         $tax_id,
209         $draft->{"project_id_${row}"} || undef,
210
211         # amount1, amount2, source, memo
212         $amount,
213         undef,
214         undef,
215         undef,
216       );
217
218       $self->{h_item}->execute(@values) || die $self->{h_item}->errstr;
219     }
220   }
221 }
222
223 sub migrate_gl_drafts {
224   my ($self) = @_;
225
226   $self->{h_draft}->execute('gl') || die $self->{h_draft}->errstr;
227
228   while (my $draft_record = $self->{h_draft}->fetchrow_hashref) {
229     my $draft = YAML::Load($draft_record->{form});
230
231     my @values = (
232       # template_name, template_type, customer_id, vendor_id,
233       $draft_record->{description} // $::locale->text('unnamed record template'),
234       'gl_transaction',
235       undef,
236       undef,
237
238       # currency_id, department_id, project_id, employee_id,
239       $self->{default_currency_id},
240       $draft->{department_id} || undef,
241       undef,
242       undef,
243
244       # taxincluded,   direct_debit, ob_transaction, cb_transaction,
245       $draft->{taxincluded}    ? 1 : 0,
246       0,
247       $draft->{ob_transaction} ? 1 : 0,
248       $draft->{cb_transaction} ? 1 : 0,
249
250       # reference, description, ordnumber, notes,
251       $draft->{reference},
252       $draft->{description},
253       undef,
254       undef,
255
256       # ar_ap_chart_id
257       undef,
258     );
259
260     $self->{h_template}->execute(@values) || die $self->{h_template}->errstr;
261     my ($template_id) = $self->{h_template}->fetchrow_array;
262
263     foreach my $row (1..$draft->{rowcount}) {
264       my ($chart_accno) = split m{--}, $draft->{"accno_${row}"};
265       my ($tax_id)      = split m{--}, $draft->{"taxchart_${row}"};
266       my $chart_id      = $self->{chart_ids_by_accno}->{$chart_accno // ''};
267       my $debit         = $::form->parse_amount($self->{format}, $draft->{"debit_${row}"});
268       my $credit        = $::form->parse_amount($self->{format}, $draft->{"credit_${row}"});
269
270       # $tax_id may be 0 as there's an entry in tax with id = 0.
271       # $chart_id must not be 0 as there's no entry in chart with id = 0.
272       next unless $chart_id && (($tax_id // '') ne '');
273
274       @values = (
275         # record_template_id,
276         $template_id,
277
278         # chart_id, tax_id, project_id,
279         $chart_id,
280         $tax_id,
281         $draft->{"project_id_${row}"} || undef,
282
283         # amount1, amount2, source, memo
284         $debit,
285         $credit,
286         $draft->{"source_${row}"},
287         $draft->{"memo_${row}"},
288       );
289
290       $self->{h_item}->execute(@values) || die $self->{h_item}->errstr;
291     }
292   }
293 }
294
295 sub clean_drafts {
296   my ($self) = @_;
297
298   $self->db_query(qq|DELETE FROM drafts WHERE module IN ('ar', 'ap', 'gl')|);
299 }
300
301 sub run {
302   my ($self) = @_;
303
304   # A dummy for %::myconfig used for parsing numbers. The existing
305   # drafts have a fundamental flaw: they store numbers & dates in the
306   # database still formatted to the user's preferences. Determining
307   # the correct format is not possible. Therefore this script simply
308   # assumes that the installation is used by people with German
309   # preferences regarding both settings.
310   $self->{format} = {
311     numberformat => '1000,00',
312     dateformat   => 'dd.mm.yy',
313   };
314
315   $self->prepare_statements;
316   $self->fetch_auxilliary_data;
317   $self->migrate_ar_drafts;
318   $self->migrate_ap_drafts;
319   $self->migrate_gl_drafts;
320   $self->clean_drafts;
321   $self->finish_statements;
322
323   return 1;
324 }
325
326 1;