3 die("This script cannot be run from the command line.") unless ($main::form);
7 die($dbup_locale->text("Database update error:") .
8 "<br>$msg<br>" . $DBI::errstr);
12 my ($query, @values) = @_;
14 $dbh->do($query, undef, @values) ||
15 mydberror($query . " (" . join(", ", @values) . ")");
19 foreach my $table (qw(customer vendor ar ap oe)) {
20 my $query = "UPDATE ${table} SET taxzone_id = 0";
21 $dbh->do($query) || mydberror($query);
30 " (SELECT DISTINCT inventory_accno_id " .
32 " WHERE (NOT inventory_accno_id ISNULL) AND (inventory_accno_id > 0))";
34 my $sth = $dbh->prepare($query);
35 $sth->execute() || mydberror($query);
37 my $query_update = "UPDATE chart SET link = ? WHERE id = ?";
38 my $sth_update = $dbh->prepare($query_update);
40 while (my $ref = $sth->fetchrow_hashref()) {
42 map({ $links{$_} = 1 } split(/:/, $ref->{"link"}));
44 my $new_link = join(":", keys(%links));
45 $sth_update->execute($new_link, $ref->{"id"}) ||
46 mydberror($query_update . " ($new_link, $ref->{id})");
50 $sth_update->finish();
53 sub force_inventory_accno_id_for_parts {
55 "UPDATE parts SET inventory_accno_id = " .
56 "(SELECT bg.inventory_accno_id " .
57 " FROM buchungsgruppen bg " .
58 " WHERE bg.description = 'Standard 16%') " .
59 "WHERE (NOT inventory_accno_id ISNULL) AND (inventory_accno_id > 0)";
61 $dbh->do($query) || mydberror($query);
64 sub retrieve_accounts {
66 "SELECT c.accno, c.description, c.link, c.id, " .
67 "d.inventory_accno_id, d.income_accno_id, d.expense_accno_id " .
68 "FROM chart c, defaults d " .
69 "WHERE c.link LIKE '%IC%' " .
72 my $sth = $dbh->prepare($query);
73 $sth->execute() || mydberror($query);
75 my ($acc_inventory, $acc_income, $acc_expense) = ({}, {}, {});
77 "IC" => $acc_inventory,
78 "IC_income" => $acc_income,
79 "IC_sale" => $acc_income,
80 "IC_expense" => $acc_expense,
81 "IC_cogs" => $acc_expense,
84 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
85 foreach my $key (split(/:/, $ref->{link})) {
86 next unless ($key_map{$key});
87 $key_map{$key}->{$ref->{"id"}} = {
88 "accno" => $ref->{"accno"},
89 "description" => $ref->{"description"},
91 "selected" => ($ref->{id} eq $ref->{inventory_accno_id})
92 || ($ref->{id} eq $ref->{income_accno_id})
93 || ($ref->{id} eq $ref->{expense_accno_id}) ?
101 [sort({ $a->{"accno"} cmp $b->{"accno"} } values(%{$acc_inventory}))];
103 [sort({ $a->{"accno"} cmp $b->{"accno"} } values(%{$acc_income}))];
105 [sort({ $a->{"accno"} cmp $b->{"accno"} } values(%{$acc_expense}))];
107 return ($acc_inventory, $acc_income, $acc_expense);
110 sub retrieve_buchungsgruppen {
115 " ci.accno AS inventory_accno, " .
116 " ci0.accno AS income_accno_0, " .
117 " ce0.accno AS expense_accno_0, " .
118 " ci1.accno AS income_accno_1, " .
119 " ce1.accno AS expense_accno_1, " .
120 " ci2.accno AS income_accno_2, " .
121 " ce2.accno AS expense_accno_2, " .
122 " ci3.accno AS income_accno_3, " .
123 " ce3.accno AS expense_accno_3 " .
124 "FROM buchungsgruppen bg " .
125 "LEFT JOIN chart ci ON bg.inventory_accno_id = ci.id " .
126 "LEFT JOIN chart ci0 ON bg.income_accno_id_0 = ci0.id " .
127 "LEFT JOIN chart ce0 ON bg.expense_accno_id_0 = ce0.id " .
128 "LEFT JOIN chart ci1 ON bg.income_accno_id_1 = ci1.id " .
129 "LEFT JOIN chart ce1 ON bg.expense_accno_id_1 = ce1.id " .
130 "LEFT JOIN chart ci2 ON bg.income_accno_id_2 = ci2.id " .
131 "LEFT JOIN chart ce2 ON bg.expense_accno_id_2 = ce2.id " .
132 "LEFT JOIN chart ci3 ON bg.income_accno_id_3 = ci3.id " .
133 "LEFT JOIN chart ce3 ON bg.expense_accno_id_3 = ce3.id";
134 my $sth = $dbh->prepare($query);
135 $sth->execute() || mydberror($query);
137 while (my $ref = $sth->fetchrow_hashref()) {
138 push(@buchungsgruppen, $ref);
142 return \@buchungsgruppen;
145 sub update_known_buchungsgruppen {
146 my ($buchungsgruppen) = @_;
151 "SELECT id, inventory_accno_id, income_accno_id, expense_accno_id " .
153 "WHERE NOT inventory_accno_id ISNULL AND (inventory_accno_id > 0)";
154 my $sth = $dbh->prepare($query);
155 $sth->execute() || mydberror($query);
157 my $query_update = "UPDATE parts SET buchungsgruppen_id = ?";
158 $query_update .= ", inventory_accno_id = ?" if ($main::eur);
159 $query_update .= " WHERE id = ?";
160 my $sth_update = $dbh->prepare($query_update);
162 while (my $ref = $sth->fetchrow_hashref()) {
163 foreach my $bg (@{$buchungsgruppen}) {
165 ($ref->{"inventory_accno_id"} == $bg->{"inventory_accno_id"})) &&
166 ($ref->{"income_accno_id"} == $bg->{"income_accno_id_0"}) &&
167 ($ref->{"expense_accno_id"} == $bg->{"expense_accno_id_0"})) {
168 my @values = ($bg->{"id"}, $ref->{"id"});
169 splice(@values, 1, 0, $bg->{"inventory_accno_id"}) if ($main::eur);
170 $sth_update->execute(@values) ||
171 mydberror($query_update . " (" . join(", ", @values) . ")");
179 "SELECT id, inventory_accno_id, income_accno_id, expense_accno_id " .
181 "WHERE inventory_accno_id ISNULL OR (inventory_accno_id = 0)";
182 my $sth = $dbh->prepare($query);
183 $sth->execute() || mydberror($query);
185 while (my $ref = $sth->fetchrow_hashref()) {
186 foreach my $bg (@{$buchungsgruppen}) {
187 if (($ref->{"income_accno_id"} == $bg->{"income_accno_id_0"}) &&
188 ($ref->{"expense_accno_id"} == $bg->{"expense_accno_id_0"})) {
189 my @values = ($bg->{"id"}, $ref->{"id"});
190 splice(@values, 1, 0, undef) if ($main::eur);
191 $sth_update->execute(@values) ||
192 mydberror($query_update . " (" . join(", ", @values) . ")");
198 $sth_update->finish();
201 sub retrieve_unknown_accno_combinations {
202 my ($buchungsgruppen) = @_;
204 my (@parts, @services, $sth, $query, $ref);
208 "p.inventory_accno_id, p.income_accno_id, p.expense_accno_id, " .
209 "c1.accno AS inventory_accno, c1.description AS inventory_description, " .
210 "c2.accno AS income_accno, c2.description AS income_description, " .
211 "c3.accno AS expense_accno, c3.description AS expense_description " .
213 "LEFT JOIN chart c1 ON p.inventory_accno_id = c1.id " .
214 "LEFT JOIN chart c2 ON p.income_accno_id = c2.id " .
215 "LEFT JOIN chart c3 ON p.expense_accno_id = c3.id " .
216 "WHERE NOT inventory_accno_id ISNULL AND (inventory_accno_id > 0)";
218 $sth = $dbh->prepare($query);
219 $sth->execute() || mydberror($query);
221 while ($ref = $sth->fetchrow_hashref()) {
224 foreach my $bg (@{$buchungsgruppen}) {
225 if (($ref->{"inventory_accno_id"} == $bg->{"inventory_accno_id"}) &&
226 ($ref->{"income_accno_id"} == $bg->{"income_accno_id_0"}) &&
227 ($ref->{"expense_accno_id"} == $bg->{"expense_accno_id_0"})) {
233 push(@parts, $ref) unless ($found);
239 "p.income_accno_id, p.expense_accno_id, " .
240 "c2.accno AS income_accno, c2.description AS income_description, " .
241 "c3.accno AS expense_accno, c3.description AS expense_description " .
243 "LEFT JOIN chart c1 ON p.inventory_accno_id = c1.id " .
244 "LEFT JOIN chart c2 ON p.income_accno_id = c2.id " .
245 "LEFT JOIN chart c3 ON p.expense_accno_id = c3.id " .
246 "WHERE inventory_accno_id ISNULL OR (inventory_accno_id = 0)";
248 $sth = $dbh->prepare($query);
249 $sth->execute() || mydberror($query);
251 while ($ref = $sth->fetchrow_hashref()) {
254 foreach my $bg (@{$buchungsgruppen}) {
255 if (($ref->{"income_accno_id"} == $bg->{"income_accno_id_0"}) &&
256 ($ref->{"expense_accno_id"} == $bg->{"expense_accno_id_0"})) {
262 push(@services, $ref) unless ($found);
266 return (\@parts, \@services);
269 sub display_create_bgs_dialog {
271 $acc_inventory, $acc_income, $acc_expense,
272 $buchungsgruppen) = @_;
274 foreach my $entry (@{$list}) {
275 $entry->{"ACC_INVENTORY"} = $acc_inventory;
276 $entry->{"ACC_INCOME"} = $acc_income;
277 $entry->{"ACC_EXPENSE"} = $acc_expense;
278 $entry->{"eur"} = $main::eur;
281 print($form->parse_html_template("dbupgrade/buchungsgruppen_${type}",
283 "BUCHUNGSGRUPPEN" => $buchungsgruppen,
287 sub create_buchungsgruppen {
288 my $form = $main::form;
290 for (my $i = 1; $i <= $form->{"rowcount"}; $i++) {
291 next unless ($form->{"description_$i"} &&
292 $form->{"inventory_accno_id_$i"} &&
293 $form->{"income_accno_id_0_$i"} &&
294 $form->{"expense_accno_id_0_$i"} &&
295 $form->{"income_accno_id_1_$i"} &&
296 $form->{"expense_accno_id_1_$i"} &&
297 $form->{"income_accno_id_2_$i"} &&
298 $form->{"expense_accno_id_2_$i"} &&
299 $form->{"income_accno_id_3_$i"} &&
300 $form->{"expense_accno_id_3_$i"});
303 "INSERT INTO buchungsgruppen (" .
304 "description, inventory_accno_id, " .
305 "income_accno_id_0, expense_accno_id_0, " .
306 "income_accno_id_1, expense_accno_id_1, " .
307 "income_accno_id_2, expense_accno_id_2, " .
308 "income_accno_id_3, expense_accno_id_3) " .
309 "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
310 my @values = ($form->{"description_$i"});
312 foreach my $acc (qw(inventory_accno_id
313 income_accno_id_0 expense_accno_id_0
314 income_accno_id_1 expense_accno_id_1
315 income_accno_id_2 expense_accno_id_2
316 income_accno_id_3 expense_accno_id_3)) {
317 push(@values, (split(/--/, $form->{"${acc}_${i}"}))[0]);
320 mydoquery($query, @values);
327 sub retrieve_std_inventory_accno_id {
330 $query = "SELECT coa FROM defaults";
331 my ($coa) = $dbh->selectrow_array($query);
335 if ($coa eq "Germany-DATEV-SKR03EU") {
336 $inventory_accno = "3980";
338 } elsif ($coa eq "Germany-DATEV-SKR04EU") {
339 $inventory_accno = "1140";
342 my $inventory_accno_id;
343 if ($inventory_accno) {
344 $query = "SELECT id FROM chart WHERE accno = $inventory_accno";
345 ($inventory_accno_id) = $dbh->selectrow_array($query);
348 if (!$inventory_accno_id) {
349 $query = "SELECT id, link FROM chart WHERE link LIKE '%IC%'";
350 my $sth = $dbh->prepare($query);
351 $sth->execute() || mydberror($query);
353 while (my $ref = $sth->fetchrow_hashref()) {
354 if (grep({ $_ eq "IC" } split(/:/, $ref->{"link"}))) {
355 $inventory_accno_id = $ref->{"id"};
362 $form->{"std_inventory_accno_id"} = $inventory_accno_id;
366 if ($main::form->{"action2"} eq "create_buchungsgruppen") {
367 create_buchungsgruppen();
370 retrieve_std_inventory_accno_id();
372 # Set all taxzone_id columns = 0.
375 # If balancing is off then force parts.inventory_accno_id to
376 # a single value for parts.
377 force_inventory_accno_id_for_parts() if ($main::eur);
379 # Force "IC" to be present in chart.link for all accounts
380 # which have been used as inventory accounts in parts.
383 # Assign buchungsgruppen_ids in parts for known combinations
384 # of inventory_accno_id, income_accno_id, expense_accno_id.
385 my $buchungsgruppen = retrieve_buchungsgruppen();
387 update_known_buchungsgruppen($buchungsgruppen);
389 # Retrieve all distinct combinations of inventory_accno_id,
390 # income_accno_id and expense_accno_id for which there's no
391 # Buchungsgruppe. Then let the user create new ones.
392 ($parts, $services) = retrieve_unknown_accno_combinations($buchungsgruppen);
394 my ($acc_inventory, $acc_income, $acc_expense) = retrieve_accounts();
396 print($form->parse_html_template("dbupgrade/buchungsgruppen_header"));
398 if (scalar(@{$parts})) {
399 display_create_bgs_dialog("parts", $parts,
400 $acc_inventory, $acc_income, $acc_expense,
405 if (scalar(@{$services})) {
406 display_create_bgs_dialog("services", $services,
407 $acc_inventory, $acc_income, $acc_expense,
412 print($form->parse_html_template("dbupgrade/buchungsgruppen_footer"));