Bei doppelten Kontonummern eine verständliche Fehlermeldung ausgeben.
[kivitendo-erp.git] / SL / AM.pm
1 #=====================================================================
2 # LX-Office ERP
3 # Copyright (C) 2004
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
6 #
7 #=====================================================================
8 # SQL-Ledger Accounting
9 # Copyright (C) 2001
10 #
11 #  Author: Dieter Simader
12 #   Email: dsimader@sql-ledger.org
13 #     Web: http://www.sql-ledger.org
14 #
15 #  Contributors:
16 #
17 # This program is free software; you can redistribute it and/or modify
18 # it under the terms of the GNU General Public License as published by
19 # the Free Software Foundation; either version 2 of the License, or
20 # (at your option) any later version.
21 #
22 # This program is distributed in the hope that it will be useful,
23 # but WITHOUT ANY WARRANTY; without even the implied warranty of
24 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25 # GNU General Public License for more details.
26 # You should have received a copy of the GNU General Public License
27 # along with this program; if not, write to the Free Software
28 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #======================================================================
30 #
31 # Administration module
32 #    Chart of Accounts
33 #    template routines
34 #    preferences
35 #
36 #======================================================================
37
38 package AM;
39
40 use Carp;
41 use Data::Dumper;
42 use Encode;
43 use SL::DBUtils;
44
45 use strict;
46
47 sub get_account {
48   $main::lxdebug->enter_sub();
49
50   my ($self, $myconfig, $form) = @_;
51
52   # connect to database
53   my $dbh = $form->dbconnect($myconfig);
54   my $query = qq{
55     SELECT c.accno, c.description, c.charttype, c.category,
56       c.link, c.pos_bilanz, c.pos_eur, c.new_chart_id, c.valid_from,
57       c.pos_bwa, datevautomatik,
58       tk.taxkey_id, tk.pos_ustva, tk.tax_id,
59       tk.tax_id || '--' || tk.taxkey_id AS tax, tk.startdate
60     FROM chart c
61     LEFT JOIN taxkeys tk
62     ON (c.id=tk.chart_id AND tk.id =
63       (SELECT id FROM taxkeys
64        WHERE taxkeys.chart_id = c.id AND startdate <= current_date
65        ORDER BY startdate DESC LIMIT 1))
66     WHERE c.id = ?
67     };
68
69
70   $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
71   my $sth = $dbh->prepare($query);
72   $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
73
74   my $ref = $sth->fetchrow_hashref("NAME_lc");
75
76   foreach my $key (keys %$ref) {
77     $form->{"$key"} = $ref->{"$key"};
78   }
79
80   $sth->finish;
81
82   # get default accounts
83   $query = qq|SELECT inventory_accno_id, income_accno_id, expense_accno_id
84               FROM defaults|;
85   $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
86   $sth = $dbh->prepare($query);
87   $sth->execute || $form->dberror($query);
88
89   $ref = $sth->fetchrow_hashref("NAME_lc");
90
91   map { $form->{$_} = $ref->{$_} } keys %{ $ref };
92
93   $sth->finish;
94
95
96
97   # get taxkeys and description
98   $query = qq{
99     SELECT
100       id,
101       (SELECT accno FROM chart WHERE id=tax.chart_id) AS chart_accno,
102       taxkey,
103       id||'--'||taxkey AS tax,
104       taxdescription,
105       rate
106     FROM tax ORDER BY taxkey
107   };
108   $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
109   $sth = $dbh->prepare($query);
110   $sth->execute || $form->dberror($query);
111
112   $form->{TAXKEY} = [];
113
114   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
115     push @{ $form->{TAXKEY} }, $ref;
116   }
117
118   $sth->finish;
119   if ($form->{id}) {
120     # get new accounts
121     $query = qq|SELECT id, accno,description
122                 FROM chart
123                 WHERE link = ?
124                 ORDER BY accno|;
125     $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
126     $sth = $dbh->prepare($query);
127     $sth->execute($form->{link}) || $form->dberror($query . " ($form->{link})");
128
129     $form->{NEWACCOUNT} = [];
130     while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
131       push @{ $form->{NEWACCOUNT} }, $ref;
132     }
133
134     $sth->finish;
135
136     # get the taxkeys of account
137
138     $query = qq{
139       SELECT
140         tk.id,
141         tk.chart_id,
142         c.accno,
143         tk.tax_id,
144         t.taxdescription,
145         t.rate,
146         tk.taxkey_id,
147         tk.pos_ustva,
148         tk.startdate
149       FROM taxkeys tk
150       LEFT JOIN   tax t ON (t.id = tk.tax_id)
151       LEFT JOIN chart c ON (c.id = t.chart_id)
152
153       WHERE tk.chart_id = ?
154       ORDER BY startdate DESC
155     };
156     $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
157     $sth = $dbh->prepare($query);
158
159     $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
160
161     $form->{ACCOUNT_TAXKEYS} = [];
162
163     while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
164       push @{ $form->{ACCOUNT_TAXKEYS} }, $ref;
165     }
166
167     $sth->finish;
168
169   }
170   # check if we have any transactions
171   $query = qq|SELECT a.trans_id FROM acc_trans a
172               WHERE a.chart_id = ?|;
173   $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
174   $sth = $dbh->prepare($query);
175   $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
176
177   ($form->{orphaned}) = $sth->fetchrow_array;
178   $form->{orphaned} = !$form->{orphaned};
179   $sth->finish;
180
181   # check if new account is active
182   $form->{new_chart_valid} = 0;
183   if ($form->{new_chart_id}) {
184     $query = qq|SELECT current_date-valid_from FROM chart
185                 WHERE id = ?|;
186     $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
187     my ($count) = selectrow_query($form, $dbh, $query, $form->{id});
188     if ($count >=0) {
189       $form->{new_chart_valid} = 1;
190     }
191     $sth->finish;
192   }
193
194   $dbh->disconnect;
195
196   $main::lxdebug->leave_sub();
197 }
198
199 sub save_account {
200   $main::lxdebug->enter_sub();
201
202   # TODO: it should be forbidden to change an account to a heading if there
203   # have been bookings to this account in the past
204
205   my ($self, $myconfig, $form) = @_;
206
207   # connect to database, turn off AutoCommit
208   my $dbh = $form->dbconnect_noauto($myconfig);
209
210   # sanity check, can't have AR with AR_...
211   if ($form->{AR} || $form->{AP} || $form->{IC}) {
212     map { delete $form->{$_} }
213       qw(AR_amount AR_tax AR_paid AP_amount AP_tax AP_paid IC_sale IC_cogs IC_taxpart IC_income IC_expense IC_taxservice);
214   }
215
216   if ($form->{AR_include_in_dropdown}) {
217     $form->{$form->{AR_include_in_dropdown}} = $form->{AR_include_in_dropdown};
218   }
219   if ($form->{AP_include_in_dropdown}) {
220     $form->{$form->{AP_include_in_dropdown}} = $form->{AP_include_in_dropdown};
221   }
222
223   $form->{link} = "";
224   foreach my $item ($form->{AR},            $form->{AR_amount},
225                     $form->{AR_tax},        $form->{AR_paid},
226                     $form->{AP},            $form->{AP_amount},
227                     $form->{AP_tax},        $form->{AP_paid},
228                     $form->{IC},            $form->{IC_sale},
229                     $form->{IC_cogs},       $form->{IC_taxpart},
230                     $form->{IC_income},     $form->{IC_expense},
231                     $form->{IC_taxservice}
232     ) {
233     $form->{link} .= "${item}:" if ($item);
234   }
235   chop $form->{link};
236
237   # strip blanks from accno
238   map { $form->{$_} =~ s/ //g; } qw(accno);
239
240   my ($query, $sth);
241
242   if ($form->{id} eq "NULL") {
243     $form->{id} = "";
244   }
245
246   if (!$form->{id} || $form->{id} eq "") {
247     $query = qq|SELECT nextval('id')|;
248     ($form->{"id"}) = selectrow_query($form, $dbh, $query);
249     $query = qq|INSERT INTO chart (id, accno) VALUES (?, ?)|;
250     do_query($form, $dbh, $query, $form->{"id"}, $form->{"accno"});
251   }
252
253   my @values;
254
255
256   if ($form->{id}) {
257
258     # if charttype is heading make sure certain values are empty
259     # specifically, if charttype is changed from an existing account, empty the
260     # fields unnecessary for headings, so that e.g. heading doesn't appear in
261     # drop-down menues due to still having a valid "link" entry
262
263     if ( $form->{charttype} eq 'H' ) {
264       $form->{link} = '';
265       $form->{pos_bwa} = '';
266       $form->{pos_bilanz} = '';
267       $form->{pos_eur} = '';
268       $form->{new_chart_id} = '';
269       $form->{valid_from} = '';
270     };
271
272     $query = '
273       SELECT
274         accno
275       FROM chart
276       WHERE accno = ?';
277     my ($accno) = selectrow_query($form, $dbh, $query, $form->{accno});
278
279     if ($accno) {
280       $form->error($::locale->text('Account number not unique!'));
281     }
282
283     $query = qq|UPDATE chart SET
284                   accno = ?,
285                   description = ?,
286                   charttype = ?,
287                   category = ?,
288                   link = ?,
289                   pos_bwa   = ?,
290                   pos_bilanz = ?,
291                   pos_eur = ?,
292                   new_chart_id = ?,
293                   valid_from = ?,
294                   datevautomatik = ?
295                 WHERE id = ?|;
296
297     @values = (
298                   $form->{accno},
299                   $form->{description},
300                   $form->{charttype},
301                   $form->{category},
302                   $form->{link},
303                   conv_i($form->{pos_bwa}),
304                   conv_i($form->{pos_bilanz}),
305                   conv_i($form->{pos_eur}),
306                   conv_i($form->{new_chart_id}),
307                   conv_date($form->{valid_from}),
308                   ($form->{datevautomatik} eq 'T') ? 'true':'false',
309                 $form->{id},
310     );
311
312
313   }
314
315   do_query($form, $dbh, $query, @values);
316
317   #Save Taxkeys
318
319   my @taxkeys = ();
320
321   my $MAX_TRIES = 10; # Maximum count of taxkeys in form
322   my $tk_count;
323
324   READTAXKEYS:
325   for $tk_count (0 .. $MAX_TRIES) {
326
327     # Loop control
328
329     # Check if the account already exists, else cancel
330
331     print(STDERR "Keine Taxkeys weil ID =: $form->{id}\n");
332
333     last READTAXKEYS if ( $form->{'id'} == 0);
334
335     # check if there is a startdate
336     if ( $form->{"taxkey_startdate_$tk_count"} eq '' ) {
337       $tk_count++;
338       next READTAXKEYS;
339     }
340
341     # Add valid taxkeys into the array
342     push @taxkeys ,
343       {
344         id        => ($form->{"taxkey_id_$tk_count"} eq 'NEW') ? conv_i('') : conv_i($form->{"taxkey_id_$tk_count"}),
345         tax_id    => conv_i($form->{"taxkey_tax_$tk_count"}),
346         startdate => conv_date($form->{"taxkey_startdate_$tk_count"}),
347         chart_id  => conv_i($form->{"id"}),
348         pos_ustva => conv_i($form->{"taxkey_pos_ustva_$tk_count"}),
349         delete    => ( $form->{"taxkey_del_$tk_count"} eq 'delete' ) ? '1' : '',
350       };
351
352     $tk_count++;
353   }
354
355   TAXKEY:
356   for my $j (0 .. $#taxkeys){
357     if ( defined $taxkeys[$j]{'id'} ){
358       # delete Taxkey?
359
360       if ($taxkeys[$j]{'delete'}){
361         $query = qq{
362           DELETE FROM taxkeys WHERE id = ?
363         };
364
365         @values = ($taxkeys[$j]{'id'});
366
367         do_query($form, $dbh, $query, @values);
368
369         next TAXKEY;
370       }
371
372       # UPDATE Taxkey
373
374       $query = qq{
375         UPDATE taxkeys
376         SET taxkey_id = (SELECT taxkey FROM tax WHERE tax.id = ?),
377             chart_id  = ?,
378             tax_id    = ?,
379             pos_ustva = ?,
380             startdate = ?
381         WHERE id = ?
382       };
383       @values = (
384         $taxkeys[$j]{'tax_id'},
385         $taxkeys[$j]{'chart_id'},
386         $taxkeys[$j]{'tax_id'},
387         $taxkeys[$j]{'pos_ustva'},
388         $taxkeys[$j]{'startdate'},
389         $taxkeys[$j]{'id'},
390       );
391       do_query($form, $dbh, $query, @values);
392     }
393     else {
394       # INSERT Taxkey
395
396       $query = qq{
397         INSERT INTO taxkeys (
398           taxkey_id,
399           chart_id,
400           tax_id,
401           pos_ustva,
402           startdate
403         )
404         VALUES ((SELECT taxkey FROM tax WHERE tax.id = ?), ?, ?, ?, ?)
405       };
406       @values = (
407         $taxkeys[$j]{'tax_id'},
408         $taxkeys[$j]{'chart_id'},
409         $taxkeys[$j]{'tax_id'},
410         $taxkeys[$j]{'pos_ustva'},
411         $taxkeys[$j]{'startdate'},
412       );
413
414       do_query($form, $dbh, $query, @values);
415     }
416
417   }
418
419   # Update chart.taxkey_id to the latest from taxkeys for this chart.
420   $query = <<SQL;
421     UPDATE chart
422     SET taxkey_id = (
423       SELECT taxkey_id
424       FROM taxkeys
425       WHERE taxkeys.chart_id = chart.id
426       ORDER BY startdate DESC
427       LIMIT 1
428     )
429     WHERE id = ?
430 SQL
431
432   do_query($form, $dbh, $query, $form->{id});
433
434   # commit
435   my $rc = $dbh->commit;
436   $dbh->disconnect;
437
438   $main::lxdebug->leave_sub();
439
440   return $rc;
441 }
442
443 sub delete_account {
444   $main::lxdebug->enter_sub();
445
446   my ($self, $myconfig, $form) = @_;
447
448   # connect to database, turn off AutoCommit
449   my $dbh = $form->dbconnect_noauto($myconfig);
450
451   my $query = qq|SELECT count(*) FROM acc_trans a
452                  WHERE a.chart_id = ?|;
453   my ($count) = selectrow_query($form, $dbh, $query, $form->{id});
454
455   if ($count) {
456     $dbh->disconnect;
457     $main::lxdebug->leave_sub();
458     return;
459   }
460
461   # set inventory_accno_id, income_accno_id, expense_accno_id to defaults
462   foreach my $type (qw(inventory income expense)) {
463     $query =
464       qq|UPDATE parts | .
465       qq|SET ${type}_accno_id = (SELECT ${type}_accno_id FROM defaults) | .
466       qq|WHERE ${type}_accno_id = ?|;
467     do_query($form, $dbh, $query, $form->{id});
468   }
469
470   foreach my $table (qw(partstax customertax vendortax tax)) {
471     $query = qq|DELETE FROM $table
472                 WHERE chart_id = ?|;
473     do_query($form, $dbh, $query, $form->{id});
474   }
475
476   # delete chart of account record
477   $query = qq|DELETE FROM chart
478               WHERE id = ?|;
479   do_query($form, $dbh, $query, $form->{id});
480
481   # delete account taxkeys
482   $query = qq|DELETE FROM taxkeys
483               WHERE chart_id = ?|;
484   do_query($form, $dbh, $query, $form->{id});
485
486   # commit and redirect
487   my $rc = $dbh->commit;
488   $dbh->disconnect;
489
490   $main::lxdebug->leave_sub();
491
492   return $rc;
493 }
494
495 sub lead {
496   $main::lxdebug->enter_sub();
497
498   my ($self, $myconfig, $form) = @_;
499
500   # connect to database
501   my $dbh = $form->dbconnect($myconfig);
502
503   my $query = qq|SELECT id, lead
504                  FROM leads
505                  ORDER BY 2|;
506
507   my $sth = $dbh->prepare($query);
508   $sth->execute || $form->dberror($query);
509
510   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
511     push @{ $form->{ALL} }, $ref;
512   }
513
514   $sth->finish;
515   $dbh->disconnect;
516
517   $main::lxdebug->leave_sub();
518 }
519
520 sub get_lead {
521   $main::lxdebug->enter_sub();
522
523   my ($self, $myconfig, $form) = @_;
524
525   # connect to database
526   my $dbh = $form->dbconnect($myconfig);
527
528   my $query =
529     qq|SELECT l.id, l.lead | .
530     qq|FROM leads l | .
531     qq|WHERE l.id = ?|;
532   my $sth = $dbh->prepare($query);
533   $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
534
535   my $ref = $sth->fetchrow_hashref("NAME_lc");
536
537   map { $form->{$_} = $ref->{$_} } keys %$ref;
538
539   $sth->finish;
540
541   $dbh->disconnect;
542
543   $main::lxdebug->leave_sub();
544 }
545
546 sub save_lead {
547   $main::lxdebug->enter_sub();
548
549   my ($self, $myconfig, $form) = @_;
550   my ($query);
551
552   # connect to database
553   my $dbh = $form->dbconnect($myconfig);
554
555   my @values = ($form->{description});
556   # id is the old record
557   if ($form->{id}) {
558     $query = qq|UPDATE leads SET
559                 lead = ?
560                 WHERE id = ?|;
561     push(@values, $form->{id});
562   } else {
563     $query = qq|INSERT INTO leads
564                 (lead)
565                 VALUES (?)|;
566   }
567   do_query($form, $dbh, $query, @values);
568
569   $dbh->disconnect;
570
571   $main::lxdebug->leave_sub();
572 }
573
574 sub delete_lead {
575   $main::lxdebug->enter_sub();
576
577   my ($self, $myconfig, $form) = @_;
578   my ($query);
579
580   # connect to database
581   my $dbh = $form->dbconnect($myconfig);
582
583   $query = qq|DELETE FROM leads
584               WHERE id = ?|;
585   do_query($form, $dbh, $query, $form->{id});
586
587   $dbh->disconnect;
588
589   $main::lxdebug->leave_sub();
590 }
591
592 sub language {
593   $main::lxdebug->enter_sub();
594
595   my ($self, $myconfig, $form, $return_list) = @_;
596
597   # connect to database
598   my $dbh = $form->dbconnect($myconfig);
599
600   my $query =
601     "SELECT id, description, template_code, article_code, " .
602     "  output_numberformat, output_dateformat, output_longdates " .
603     "FROM language ORDER BY description";
604
605   my $sth = $dbh->prepare($query);
606   $sth->execute || $form->dberror($query);
607
608   my $ary = [];
609
610   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
611     push(@{ $ary }, $ref);
612   }
613
614   $sth->finish;
615   $dbh->disconnect;
616
617   $main::lxdebug->leave_sub();
618
619   if ($return_list) {
620     return @{$ary};
621   } else {
622     $form->{ALL} = $ary;
623   }
624 }
625
626 sub get_language {
627   $main::lxdebug->enter_sub();
628
629   my ($self, $myconfig, $form) = @_;
630
631   # connect to database
632   my $dbh = $form->dbconnect($myconfig);
633
634   my $query =
635     "SELECT description, template_code, article_code, " .
636     "  output_numberformat, output_dateformat, output_longdates " .
637     "FROM language WHERE id = ?";
638   my $sth = $dbh->prepare($query);
639   $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
640
641   my $ref = $sth->fetchrow_hashref("NAME_lc");
642
643   map { $form->{$_} = $ref->{$_} } keys %$ref;
644
645   $sth->finish;
646
647   $dbh->disconnect;
648
649   $main::lxdebug->leave_sub();
650 }
651
652 sub get_language_details {
653   $main::lxdebug->enter_sub();
654
655   my ($self, $myconfig, $form, $id) = @_;
656
657   # connect to database
658   my $dbh = $form->dbconnect($myconfig);
659
660   my $query =
661     "SELECT template_code, " .
662     "  output_numberformat, output_dateformat, output_longdates " .
663     "FROM language WHERE id = ?";
664   my @res = selectrow_query($form, $dbh, $query, $id);
665   $dbh->disconnect;
666
667   $main::lxdebug->leave_sub();
668
669   return @res;
670 }
671
672 sub save_language {
673   $main::lxdebug->enter_sub();
674
675   my ($self, $myconfig, $form) = @_;
676
677   # connect to database
678   my $dbh = $form->dbconnect($myconfig);
679   my (@values, $query);
680
681   map({ push(@values, $form->{$_}); }
682       qw(description template_code article_code
683          output_numberformat output_dateformat output_longdates));
684
685   # id is the old record
686   if ($form->{id}) {
687     $query =
688       "UPDATE language SET " .
689       "  description = ?, template_code = ?, article_code = ?, " .
690       "  output_numberformat = ?, output_dateformat = ?, " .
691       "  output_longdates = ? " .
692       "WHERE id = ?";
693     push(@values, $form->{id});
694   } else {
695     $query =
696       "INSERT INTO language (" .
697       "  description, template_code, article_code, " .
698       "  output_numberformat, output_dateformat, output_longdates" .
699       ") VALUES (?, ?, ?, ?, ?, ?)";
700   }
701   do_query($form, $dbh, $query, @values);
702
703   $dbh->disconnect;
704
705   $main::lxdebug->leave_sub();
706 }
707
708 sub delete_language {
709   $main::lxdebug->enter_sub();
710
711   my ($self, $myconfig, $form) = @_;
712   my $query;
713
714   # connect to database
715   my $dbh = $form->dbconnect_noauto($myconfig);
716
717   foreach my $table (qw(generic_translations units_language)) {
718     $query = qq|DELETE FROM $table WHERE language_id = ?|;
719     do_query($form, $dbh, $query, $form->{"id"});
720   }
721
722   $query = "DELETE FROM language WHERE id = ?";
723   do_query($form, $dbh, $query, $form->{"id"});
724
725   $dbh->commit();
726   $dbh->disconnect;
727
728   $main::lxdebug->leave_sub();
729 }
730
731
732 sub buchungsgruppe {
733   $main::lxdebug->enter_sub();
734
735   my ($self, $myconfig, $form) = @_;
736
737   # connect to database
738   my $dbh = $form->dbconnect($myconfig);
739
740   my $query = qq|SELECT id, description,
741                  inventory_accno_id,
742                  (SELECT accno FROM chart WHERE id = inventory_accno_id) AS inventory_accno,
743                  income_accno_id_0,
744                  (SELECT accno FROM chart WHERE id = income_accno_id_0) AS income_accno_0,
745                  expense_accno_id_0,
746                  (SELECT accno FROM chart WHERE id = expense_accno_id_0) AS expense_accno_0,
747                  income_accno_id_1,
748                  (SELECT accno FROM chart WHERE id = income_accno_id_1) AS income_accno_1,
749                  expense_accno_id_1,
750                  (SELECT accno FROM chart WHERE id = expense_accno_id_1) AS expense_accno_1,
751                  income_accno_id_2,
752                  (SELECT accno FROM chart WHERE id = income_accno_id_2) AS income_accno_2,
753                  expense_accno_id_2,
754                  (select accno FROM chart WHERE id = expense_accno_id_2) AS expense_accno_2,
755                  income_accno_id_3,
756                  (SELECT accno FROM chart WHERE id = income_accno_id_3) AS income_accno_3,
757                  expense_accno_id_3,
758                  (SELECT accno FROM chart WHERE id = expense_accno_id_3) AS expense_accno_3
759                  FROM buchungsgruppen
760                  ORDER BY sortkey|;
761
762   my $sth = $dbh->prepare($query);
763   $sth->execute || $form->dberror($query);
764
765   $form->{ALL} = [];
766   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
767     push @{ $form->{ALL} }, $ref;
768   }
769
770   $sth->finish;
771   $dbh->disconnect;
772
773   $main::lxdebug->leave_sub();
774 }
775
776 sub get_buchungsgruppe {
777   $main::lxdebug->enter_sub();
778
779   my ($self, $myconfig, $form) = @_;
780   my $query;
781
782   # connect to database
783   my $dbh = $form->dbconnect($myconfig);
784
785   if ($form->{id}) {
786     $query =
787       qq|SELECT description, inventory_accno_id,
788          (SELECT accno FROM chart WHERE id = inventory_accno_id) AS inventory_accno,
789          income_accno_id_0,
790          (SELECT accno FROM chart WHERE id = income_accno_id_0) AS income_accno_0,
791          expense_accno_id_0,
792          (SELECT accno FROM chart WHERE id = expense_accno_id_0) AS expense_accno_0,
793          income_accno_id_1,
794          (SELECT accno FROM chart WHERE id = income_accno_id_1) AS income_accno_1,
795          expense_accno_id_1,
796          (SELECT accno FROM chart WHERE id = expense_accno_id_1) AS expense_accno_1,
797          income_accno_id_2,
798          (SELECT accno FROM chart WHERE id = income_accno_id_2) AS income_accno_2,
799          expense_accno_id_2,
800          (select accno FROM chart WHERE id = expense_accno_id_2) AS expense_accno_2,
801          income_accno_id_3,
802          (SELECT accno FROM chart WHERE id = income_accno_id_3) AS income_accno_3,
803          expense_accno_id_3,
804          (SELECT accno FROM chart WHERE id = expense_accno_id_3) AS expense_accno_3
805          FROM buchungsgruppen
806          WHERE id = ?|;
807     my $sth = $dbh->prepare($query);
808     $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
809
810     my $ref = $sth->fetchrow_hashref("NAME_lc");
811
812     map { $form->{$_} = $ref->{$_} } keys %$ref;
813
814     $sth->finish;
815
816     $query =
817       qq|SELECT count(id) = 0 AS orphaned
818          FROM parts
819          WHERE buchungsgruppen_id = ?|;
820     ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
821   }
822
823   $query = "SELECT inventory_accno_id, income_accno_id, expense_accno_id ".
824     "FROM defaults";
825   ($form->{"std_inventory_accno_id"}, $form->{"std_income_accno_id"},
826    $form->{"std_expense_accno_id"}) = selectrow_query($form, $dbh, $query);
827
828   my $module = "IC";
829   $query = qq|SELECT c.accno, c.description, c.link, c.id,
830               d.inventory_accno_id, d.income_accno_id, d.expense_accno_id
831               FROM chart c, defaults d
832               WHERE c.link LIKE '%$module%'
833               ORDER BY c.accno|;
834
835
836   my $sth = $dbh->prepare($query);
837   $sth->execute || $form->dberror($query);
838   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
839     foreach my $key (split(/:/, $ref->{link})) {
840       if (!$form->{"std_inventory_accno_id"} && ($key eq "IC")) {
841         $form->{"std_inventory_accno_id"} = $ref->{"id"};
842       }
843       if ($key =~ /$module/) {
844         if (   ($ref->{id} eq $ref->{inventory_accno_id})
845             || ($ref->{id} eq $ref->{income_accno_id})
846             || ($ref->{id} eq $ref->{expense_accno_id})) {
847           push @{ $form->{"${module}_links"}{$key} },
848             { accno       => $ref->{accno},
849               description => $ref->{description},
850               selected    => "selected",
851               id          => $ref->{id} };
852         } else {
853           push @{ $form->{"${module}_links"}{$key} },
854             { accno       => $ref->{accno},
855               description => $ref->{description},
856               selected    => "",
857               id          => $ref->{id} };
858         }
859       }
860     }
861   }
862   $sth->finish;
863
864
865   $dbh->disconnect;
866
867   $main::lxdebug->leave_sub();
868 }
869
870 sub save_buchungsgruppe {
871   $main::lxdebug->enter_sub();
872
873   my ($self, $myconfig, $form) = @_;
874
875   # connect to database
876   my $dbh = $form->dbconnect($myconfig);
877
878   my @values = ($form->{description}, $form->{inventory_accno_id},
879                 $form->{income_accno_id_0}, $form->{expense_accno_id_0},
880                 $form->{income_accno_id_1}, $form->{expense_accno_id_1},
881                 $form->{income_accno_id_2}, $form->{expense_accno_id_2},
882                 $form->{income_accno_id_3}, $form->{expense_accno_id_3});
883
884   my $query;
885
886   # id is the old record
887   if ($form->{id}) {
888     $query = qq|UPDATE buchungsgruppen SET
889                 description = ?, inventory_accno_id = ?,
890                 income_accno_id_0 = ?, expense_accno_id_0 = ?,
891                 income_accno_id_1 = ?, expense_accno_id_1 = ?,
892                 income_accno_id_2 = ?, expense_accno_id_2 = ?,
893                 income_accno_id_3 = ?, expense_accno_id_3 = ?
894                 WHERE id = ?|;
895     push(@values, $form->{id});
896   } else {
897     $query = qq|SELECT COALESCE(MAX(sortkey) + 1, 1) FROM buchungsgruppen|;
898     my ($sortkey) = $dbh->selectrow_array($query);
899     $form->dberror($query) if ($dbh->err);
900     push(@values, $sortkey);
901     $query = qq|INSERT INTO buchungsgruppen
902                 (description, inventory_accno_id,
903                 income_accno_id_0, expense_accno_id_0,
904                 income_accno_id_1, expense_accno_id_1,
905                 income_accno_id_2, expense_accno_id_2,
906                 income_accno_id_3, expense_accno_id_3,
907                 sortkey)
908                 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
909   }
910   do_query($form, $dbh, $query, @values);
911
912   $dbh->disconnect;
913
914   $main::lxdebug->leave_sub();
915 }
916
917 sub delete_buchungsgruppe {
918   $main::lxdebug->enter_sub();
919
920   my ($self, $myconfig, $form) = @_;
921
922   # connect to database
923   my $dbh = $form->dbconnect($myconfig);
924
925   my $query = qq|DELETE FROM buchungsgruppen WHERE id = ?|;
926   do_query($form, $dbh, $query, $form->{id});
927
928   $dbh->disconnect;
929
930   $main::lxdebug->leave_sub();
931 }
932
933 sub swap_sortkeys {
934   $main::lxdebug->enter_sub();
935
936   my ($self, $myconfig, $form, $table) = @_;
937
938   # connect to database
939   my $dbh = $form->get_standard_dbh($myconfig);
940
941   my $query =
942     qq|SELECT
943        (SELECT sortkey FROM $table WHERE id = ?) AS sortkey1,
944        (SELECT sortkey FROM $table WHERE id = ?) AS sortkey2|;
945   my @values   = ($form->{"id1"}, $form->{"id2"});
946   my @sortkeys = selectrow_query($form, $dbh, $query, @values);
947
948   $query  = qq|UPDATE $table SET sortkey = ? WHERE id = ?|;
949   my $sth = prepare_query($form, $dbh, $query);
950
951   do_statement($form, $sth, $query, $sortkeys[1], $form->{"id1"});
952   do_statement($form, $sth, $query, $sortkeys[0], $form->{"id2"});
953
954   $sth->finish();
955
956   $dbh->commit();
957
958   $main::lxdebug->leave_sub();
959 }
960
961 sub prepare_template_filename {
962   $main::lxdebug->enter_sub();
963
964   my ($self, $myconfig, $form) = @_;
965
966   my ($filename, $display_filename);
967
968   if ($form->{type} eq "stylesheet") {
969     $filename = "css/$myconfig->{stylesheet}";
970     $display_filename = $myconfig->{stylesheet};
971
972   } else {
973     $filename = $form->{formname};
974
975     if ($form->{language}) {
976       my ($id, $template_code) = split(/--/, $form->{language});
977       $filename .= "_${template_code}";
978     }
979
980     if ($form->{printer}) {
981       my ($id, $template_code) = split(/--/, $form->{printer});
982       $filename .= "_${template_code}";
983     }
984
985     $filename .= "." . ($form->{format} eq "html" ? "html" : "tex");
986     if ($form->{"formname"} =~ m|\.\.| || $form->{"formname"} =~ m|^/|) {
987       $filename =~ s|.*/||;
988     }
989     $display_filename = $filename;
990     $filename = "$myconfig->{templates}/$filename";
991   }
992
993   $main::lxdebug->leave_sub();
994
995   return ($filename, $display_filename);
996 }
997
998
999 sub load_template {
1000   $main::lxdebug->enter_sub();
1001
1002   my ($self, $filename) = @_;
1003
1004   my ($content, $lines) = ("", 0);
1005
1006   local *TEMPLATE;
1007
1008   if (open(TEMPLATE, $filename)) {
1009     while (<TEMPLATE>) {
1010       $content .= $_;
1011       $lines++;
1012     }
1013     close(TEMPLATE);
1014   }
1015
1016   $content = Encode::decode('utf-8-strict', $content) if $::locale->is_utf8;
1017
1018   $main::lxdebug->leave_sub();
1019
1020   return ($content, $lines);
1021 }
1022
1023 sub save_template {
1024   $main::lxdebug->enter_sub();
1025
1026   my ($self, $filename, $content) = @_;
1027
1028   local *TEMPLATE;
1029
1030   my $error = "";
1031
1032   if (open(TEMPLATE, ">", $filename)) {
1033     $content = Encode::encode('utf-8-strict', $content) if $::locale->is_utf8;
1034     $content =~ s/\r\n/\n/g;
1035     print(TEMPLATE $content);
1036     close(TEMPLATE);
1037   } else {
1038     $error = $!;
1039   }
1040
1041   $main::lxdebug->leave_sub();
1042
1043   return $error;
1044 }
1045
1046 sub save_defaults {
1047   $main::lxdebug->enter_sub();
1048
1049   my $self     = shift;
1050   my %params   = @_;
1051
1052   my $myconfig = \%main::myconfig;
1053   my $form     = $main::form;
1054
1055   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
1056
1057   my %accnos;
1058   map { ($accnos{$_}) = split(m/--/, $form->{$_}) } qw(inventory_accno income_accno expense_accno fxgain_accno fxloss_accno ar_paid_accno);
1059
1060   $form->{curr}  =~ s/ //g;
1061   my @currencies =  grep { $_ ne '' } split m/:/, $form->{curr};
1062   my $currency   =  join ':', @currencies;
1063
1064   # these defaults are database wide
1065
1066   my $query =
1067     qq|UPDATE defaults SET
1068         inventory_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
1069         income_accno_id    = (SELECT c.id FROM chart c WHERE c.accno = ?),
1070         expense_accno_id   = (SELECT c.id FROM chart c WHERE c.accno = ?),
1071         fxgain_accno_id    = (SELECT c.id FROM chart c WHERE c.accno = ?),
1072         fxloss_accno_id    = (SELECT c.id FROM chart c WHERE c.accno = ?),
1073         ar_paid_accno_id   = (SELECT c.id FROM chart c WHERE c.accno = ?),
1074         invnumber          = ?,
1075         cnnumber           = ?,
1076         sonumber           = ?,
1077         ponumber           = ?,
1078         sqnumber           = ?,
1079         rfqnumber          = ?,
1080         customernumber     = ?,
1081         vendornumber       = ?,
1082         articlenumber      = ?,
1083         servicenumber      = ?,
1084         sdonumber          = ?,
1085         pdonumber          = ?,
1086         curr               = ?,
1087         businessnumber     = ?,
1088         weightunit         = ?,
1089         language_id        = ?|;
1090   my @values = ($accnos{inventory_accno}, $accnos{income_accno}, $accnos{expense_accno},
1091                 $accnos{fxgain_accno},    $accnos{fxloss_accno}, $accnos{ar_paid_accno},
1092                 $form->{invnumber},       $form->{cnnumber},
1093                 $form->{sonumber},        $form->{ponumber},
1094                 $form->{sqnumber},        $form->{rfqnumber},
1095                 $form->{customernumber},  $form->{vendornumber},
1096                 $form->{articlenumber},   $form->{servicenumber},
1097                 $form->{sdonumber},       $form->{pdonumber},
1098                 $currency,
1099                 $form->{businessnumber},  $form->{weightunit},
1100                 conv_i($form->{language_id}));
1101   do_query($form, $dbh, $query, @values);
1102
1103   $dbh->commit();
1104
1105   $main::lxdebug->leave_sub();
1106 }
1107
1108
1109 sub save_preferences {
1110   $main::lxdebug->enter_sub();
1111
1112   my ($self, $myconfig, $form) = @_;
1113
1114   my $dbh = $form->get_standard_dbh($myconfig);
1115
1116   my ($currency, $businessnumber) = selectrow_query($form, $dbh, qq|SELECT curr, businessnumber FROM defaults|);
1117
1118   # update name
1119   my $query = qq|UPDATE employee SET name = ? WHERE login = ?|;
1120   do_query($form, $dbh, $query, $form->{name}, $form->{login});
1121
1122   my $rc = $dbh->commit();
1123
1124   # save first currency in myconfig
1125   $currency               =~ s/:.*//;
1126   $form->{currency}       =  $currency;
1127
1128   $form->{businessnumber} =  $businessnumber;
1129
1130   $myconfig = User->new(login => $form->{login});
1131
1132   foreach my $item (keys %$form) {
1133     $myconfig->{$item} = $form->{$item};
1134   }
1135
1136   $myconfig->save_member;
1137
1138   my $auth = $main::auth;
1139
1140   $main::lxdebug->leave_sub();
1141
1142   return $rc;
1143 }
1144
1145 sub get_defaults {
1146   $main::lxdebug->enter_sub();
1147
1148   my $self     = shift;
1149   my %params   = @_;
1150
1151   my $myconfig = \%main::myconfig;
1152   my $form     = $main::form;
1153
1154   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
1155
1156   my $defaults = selectfirst_hashref_query($form, $dbh, qq|SELECT * FROM defaults|) || {};
1157
1158   $defaults->{weightunit} ||= 'kg';
1159
1160   $main::lxdebug->leave_sub();
1161
1162   return $defaults;
1163 }
1164
1165 sub defaultaccounts {
1166   $main::lxdebug->enter_sub();
1167
1168   my ($self, $myconfig, $form) = @_;
1169
1170   # connect to database
1171   my $dbh = $form->dbconnect($myconfig);
1172
1173   # get defaults from defaults table
1174   my $query = qq|SELECT * FROM defaults|;
1175   my $sth   = $dbh->prepare($query);
1176   $sth->execute || $form->dberror($query);
1177
1178   $form->{defaults}               = $sth->fetchrow_hashref("NAME_lc");
1179   $form->{defaults}{IC}           = $form->{defaults}{inventory_accno_id};
1180   $form->{defaults}{IC_income}    = $form->{defaults}{income_accno_id};
1181   $form->{defaults}{IC_expense}   = $form->{defaults}{expense_accno_id};
1182   $form->{defaults}{FX_gain}      = $form->{defaults}{fxgain_accno_id};
1183   $form->{defaults}{FX_loss}      = $form->{defaults}{fxloss_accno_id};
1184   $form->{defaults}{AR_paid}      = $form->{defaults}{ar_paid_accno_id};
1185
1186   $form->{defaults}{weightunit} ||= 'kg';
1187
1188   $sth->finish;
1189
1190   $query = qq|SELECT c.id, c.accno, c.description, c.link
1191               FROM chart c
1192               WHERE c.link LIKE '%IC%'
1193               ORDER BY c.accno|;
1194   $sth = $dbh->prepare($query);
1195   $sth->execute || $self->dberror($query);
1196
1197   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1198     foreach my $key (split(/:/, $ref->{link})) {
1199       if ($key =~ /IC/) {
1200         my $nkey = $key;
1201         if ($key =~ /cogs/) {
1202           $nkey = "IC_expense";
1203         }
1204         if ($key =~ /sale/) {
1205           $nkey = "IC_income";
1206         }
1207         %{ $form->{IC}{$nkey}{ $ref->{accno} } } = (
1208                                              id          => $ref->{id},
1209                                              description => $ref->{description}
1210         );
1211       }
1212     }
1213   }
1214   $sth->finish;
1215
1216   $query = qq|SELECT c.id, c.accno, c.description
1217               FROM chart c
1218               WHERE c.category = 'I'
1219               AND c.charttype = 'A'
1220               ORDER BY c.accno|;
1221   $sth = $dbh->prepare($query);
1222   $sth->execute || $self->dberror($query);
1223
1224   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1225     %{ $form->{IC}{FX_gain}{ $ref->{accno} } } = (
1226                                              id          => $ref->{id},
1227                                              description => $ref->{description}
1228     );
1229   }
1230   $sth->finish;
1231
1232   $query = qq|SELECT c.id, c.accno, c.description
1233               FROM chart c
1234               WHERE c.category = 'E'
1235               AND c.charttype = 'A'
1236               ORDER BY c.accno|;
1237   $sth = $dbh->prepare($query);
1238   $sth->execute || $self->dberror($query);
1239
1240   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1241     %{ $form->{IC}{FX_loss}{ $ref->{accno} } } = (
1242                                              id          => $ref->{id},
1243                                              description => $ref->{description}
1244     );
1245   }
1246   $sth->finish;
1247
1248   # now get the tax rates and numbers
1249   $query = qq|SELECT c.id, c.accno, c.description,
1250               t.rate * 100 AS rate, t.taxnumber
1251               FROM chart c, tax t
1252               WHERE c.id = t.chart_id|;
1253
1254   $sth = $dbh->prepare($query);
1255   $sth->execute || $form->dberror($query);
1256
1257   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1258     $form->{taxrates}{ $ref->{accno} }{id}          = $ref->{id};
1259     $form->{taxrates}{ $ref->{accno} }{description} = $ref->{description};
1260     $form->{taxrates}{ $ref->{accno} }{taxnumber}   = $ref->{taxnumber}
1261       if $ref->{taxnumber};
1262     $form->{taxrates}{ $ref->{accno} }{rate} = $ref->{rate} if $ref->{rate};
1263   }
1264   # Abfrage für Standard Umlaufvermögenskonto
1265   $query =
1266     qq|SELECT id, accno, description, link | .
1267     qq|FROM chart | .
1268     qq|WHERE link LIKE ? |.
1269     qq|ORDER BY accno|;
1270   $sth = prepare_execute_query($form, $dbh, $query, '%AR%');
1271   $sth->execute || $form->dberror($query);#
1272   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1273     foreach my $item (split(/:/, $ref->{link})) {
1274       if ($item eq "AR_paid") {
1275         %{ $form->{IC}{AR_paid}{ $ref->{accno} } } = (
1276                                              id          => $ref->{id},
1277                                              description => $ref->{description}
1278           );
1279       }
1280     }
1281   }
1282
1283   $sth->finish;
1284   $dbh->disconnect;
1285
1286   $main::lxdebug->leave_sub();
1287 }
1288
1289 sub closedto {
1290   $main::lxdebug->enter_sub();
1291
1292   my ($self, $myconfig, $form) = @_;
1293
1294   my $dbh = $form->dbconnect($myconfig);
1295
1296   my $query = qq|SELECT closedto, revtrans FROM defaults|;
1297   my $sth   = $dbh->prepare($query);
1298   $sth->execute || $form->dberror($query);
1299
1300   ($form->{closedto}, $form->{revtrans}) = $sth->fetchrow_array;
1301
1302   $sth->finish;
1303
1304   $dbh->disconnect;
1305
1306   $main::lxdebug->leave_sub();
1307 }
1308
1309 sub closebooks {
1310   $main::lxdebug->enter_sub();
1311
1312   my ($self, $myconfig, $form) = @_;
1313
1314   my $dbh = $form->dbconnect($myconfig);
1315
1316   my ($query, @values);
1317
1318   if ($form->{revtrans}) {
1319     $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '1'|;
1320
1321   } elsif ($form->{closedto}) {
1322     $query = qq|UPDATE defaults SET closedto = ?, revtrans = '0'|;
1323     @values = (conv_date($form->{closedto}));
1324
1325   } else {
1326     $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '0'|;
1327   }
1328
1329   # set close in defaults
1330   do_query($form, $dbh, $query, @values);
1331
1332   $dbh->disconnect;
1333
1334   $main::lxdebug->leave_sub();
1335 }
1336
1337 sub get_base_unit {
1338   my ($self, $units, $unit_name, $factor) = @_;
1339
1340   $factor = 1 unless ($factor);
1341
1342   my $unit = $units->{$unit_name};
1343
1344   if (!defined($unit) || !$unit->{"base_unit"} ||
1345       ($unit_name eq $unit->{"base_unit"})) {
1346     return ($unit_name, $factor);
1347   }
1348
1349   return AM->get_base_unit($units, $unit->{"base_unit"}, $factor * $unit->{"factor"});
1350 }
1351
1352 sub retrieve_units {
1353   $main::lxdebug->enter_sub();
1354
1355   my ($self, $myconfig, $form, $prefix) = @_;
1356   $prefix ||= '';
1357
1358   my $dbh = $form->get_standard_dbh;
1359
1360   my $query = "SELECT *, base_unit AS original_base_unit FROM units";
1361
1362   my $sth = prepare_execute_query($form, $dbh, $query);
1363
1364   my $units = {};
1365   while (my $ref = $sth->fetchrow_hashref()) {
1366     $units->{$ref->{"name"}} = $ref;
1367   }
1368   $sth->finish();
1369
1370   my $query_lang = "SELECT id, template_code FROM language ORDER BY description";
1371   $sth = $dbh->prepare($query_lang);
1372   $sth->execute() || $form->dberror($query_lang);
1373   my @languages;
1374   while (my $ref = $sth->fetchrow_hashref()) {
1375     push(@languages, $ref);
1376   }
1377   $sth->finish();
1378
1379   $query_lang = "SELECT ul.localized, ul.localized_plural, l.id, l.template_code " .
1380     "FROM units_language ul " .
1381     "LEFT JOIN language l ON ul.language_id = l.id " .
1382     "WHERE ul.unit = ?";
1383   $sth = $dbh->prepare($query_lang);
1384
1385   foreach my $unit (values(%{$units})) {
1386     ($unit->{"${prefix}base_unit"}, $unit->{"${prefix}factor"}) = AM->get_base_unit($units, $unit->{"name"});
1387
1388     $unit->{"LANGUAGES"} = {};
1389     foreach my $lang (@languages) {
1390       $unit->{"LANGUAGES"}->{$lang->{"template_code"}} = { "template_code" => $lang->{"template_code"} };
1391     }
1392
1393     $sth->execute($unit->{"name"}) || $form->dberror($query_lang . " (" . $unit->{"name"} . ")");
1394     while (my $ref = $sth->fetchrow_hashref()) {
1395       map({ $unit->{"LANGUAGES"}->{$ref->{"template_code"}}->{$_} = $ref->{$_} } keys(%{$ref}));
1396     }
1397   }
1398   $sth->finish;
1399
1400   $main::lxdebug->leave_sub();
1401
1402   return $units;
1403 }
1404
1405 sub retrieve_all_units {
1406   $main::lxdebug->enter_sub();
1407
1408   my $self = shift;
1409
1410   if (!$::request->{cache}{all_units}) {
1411     $::request->{cache}{all_units} = $self->retrieve_units(\%main::myconfig, $main::form);
1412   }
1413
1414   $main::lxdebug->leave_sub();
1415
1416   return $::request->{cache}{all_units};
1417 }
1418
1419
1420 sub translate_units {
1421   $main::lxdebug->enter_sub();
1422
1423   my ($self, $form, $template_code, $unit, $amount) = @_;
1424
1425   my $units = $self->retrieve_units(\%main::myconfig, $form);
1426
1427   my $h = $units->{$unit}->{"LANGUAGES"}->{$template_code};
1428   my $new_unit = $unit;
1429   if ($h) {
1430     if (($amount != 1) && $h->{"localized_plural"}) {
1431       $new_unit = $h->{"localized_plural"};
1432     } elsif ($h->{"localized"}) {
1433       $new_unit = $h->{"localized"};
1434     }
1435   }
1436
1437   $main::lxdebug->leave_sub();
1438
1439   return $new_unit;
1440 }
1441
1442 sub units_in_use {
1443   $main::lxdebug->enter_sub();
1444
1445   my ($self, $myconfig, $form, $units) = @_;
1446
1447   my $dbh = $form->dbconnect($myconfig);
1448
1449   map({ $_->{"in_use"} = 0; } values(%{$units}));
1450
1451   foreach my $unit (values(%{$units})) {
1452     my $base_unit = $unit->{"original_base_unit"};
1453     while ($base_unit) {
1454       $units->{$base_unit}->{"in_use"} = 1;
1455       $units->{$base_unit}->{"DEPENDING_UNITS"} = [] unless ($units->{$base_unit}->{"DEPENDING_UNITS"});
1456       push(@{$units->{$base_unit}->{"DEPENDING_UNITS"}}, $unit->{"name"});
1457       $base_unit = $units->{$base_unit}->{"original_base_unit"};
1458     }
1459   }
1460
1461   foreach my $unit (values(%{$units})) {
1462     map({ $_ = $dbh->quote($_); } @{$unit->{"DEPENDING_UNITS"}});
1463
1464     foreach my $table (qw(parts invoice orderitems)) {
1465       my $query = "SELECT COUNT(*) FROM $table WHERE unit ";
1466
1467       if (0 == scalar(@{$unit->{"DEPENDING_UNITS"}})) {
1468         $query .= "= " . $dbh->quote($unit->{"name"});
1469       } else {
1470         $query .= "IN (" . $dbh->quote($unit->{"name"}) . "," .
1471           join(",", map({ $dbh->quote($_) } @{$unit->{"DEPENDING_UNITS"}})) . ")";
1472       }
1473
1474       my ($count) = $dbh->selectrow_array($query);
1475       $form->dberror($query) if ($dbh->err);
1476
1477       if ($count) {
1478         $unit->{"in_use"} = 1;
1479         last;
1480       }
1481     }
1482   }
1483
1484   $dbh->disconnect();
1485
1486   $main::lxdebug->leave_sub();
1487 }
1488
1489 sub convertible_units {
1490   $main::lxdebug->enter_sub();
1491
1492   my $self        = shift;
1493   my $units       = shift;
1494   my $filter_unit = shift;
1495   my $not_smaller = shift;
1496
1497   my $conv_units = [];
1498
1499   $filter_unit = $units->{$filter_unit};
1500
1501   foreach my $name (sort { lc $a cmp lc $b } keys %{ $units }) {
1502     my $unit = $units->{$name};
1503
1504     if (($unit->{base_unit} eq $filter_unit->{base_unit}) &&
1505         (!$not_smaller || ($unit->{factor} >= $filter_unit->{factor}))) {
1506       push @{$conv_units}, $unit;
1507     }
1508   }
1509
1510   my @sorted = sort { $b->{factor} <=> $a->{factor} } @{ $conv_units };
1511
1512   $main::lxdebug->leave_sub();
1513
1514   return \@sorted;
1515 }
1516
1517 # if $a is translatable to $b, return the factor between them.
1518 # else return 1
1519 sub convert_unit {
1520   $main::lxdebug->enter_sub(2);
1521   my ($this, $a, $b, $all_units) = @_;
1522
1523   if (!$all_units) {
1524     $all_units = $this->retrieve_all_units;
1525   }
1526
1527   $main::lxdebug->leave_sub(2) and return 0 unless $a && $b;
1528   $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a} && $all_units->{$b};
1529   $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a}{base_unit} eq $all_units->{$b}{base_unit};
1530   $main::lxdebug->leave_sub(2) and return $all_units->{$a}{factor} / $all_units->{$b}{factor};
1531 }
1532
1533 sub unit_select_data {
1534   $main::lxdebug->enter_sub();
1535
1536   my ($self, $units, $selected, $empty_entry, $convertible_into) = @_;
1537
1538   my $select = [];
1539
1540   if ($empty_entry) {
1541     push(@{$select}, { "name" => "", "base_unit" => "", "factor" => "", "selected" => "" });
1542   }
1543
1544   foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
1545     if (!$convertible_into ||
1546         ($units->{$convertible_into} &&
1547          ($units->{$convertible_into}->{base_unit} eq $units->{$unit}->{base_unit}))) {
1548       push @{$select}, { "name"      => $unit,
1549                          "base_unit" => $units->{$unit}->{"base_unit"},
1550                          "factor"    => $units->{$unit}->{"factor"},
1551                          "selected"  => ($unit eq $selected) ? "selected" : "" };
1552     }
1553   }
1554
1555   $main::lxdebug->leave_sub();
1556
1557   return $select;
1558 }
1559
1560 sub unit_select_html {
1561   $main::lxdebug->enter_sub();
1562
1563   my ($self, $units, $name, $selected, $convertible_into) = @_;
1564
1565   my $select = "<select name=${name}>";
1566
1567   foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
1568     if (!$convertible_into ||
1569         ($units->{$convertible_into} &&
1570          ($units->{$convertible_into}->{"base_unit"} eq $units->{$unit}->{"base_unit"}))) {
1571       $select .= "<option" . (($unit eq $selected) ? " selected" : "") . ">${unit}</option>";
1572     }
1573   }
1574   $select .= "</select>";
1575
1576   $main::lxdebug->leave_sub();
1577
1578   return $select;
1579 }
1580
1581 sub sum_with_unit {
1582   $main::lxdebug->enter_sub();
1583
1584   my $self  = shift;
1585
1586   my $units = $self->retrieve_all_units();
1587
1588   my $sum   = 0;
1589   my $base_unit;
1590
1591   while (2 <= scalar(@_)) {
1592     my $qty  = shift(@_);
1593     my $unit = $units->{shift(@_)};
1594
1595     croak "No unit defined with name $unit" if (!defined $unit);
1596
1597     if (!$base_unit) {
1598       $base_unit = $unit->{base_unit};
1599     } elsif ($base_unit ne $unit->{base_unit}) {
1600       croak "Adding values with incompatible base units $base_unit/$unit->{base_unit}";
1601     }
1602
1603     $sum += $qty * $unit->{factor};
1604   }
1605
1606   $main::lxdebug->leave_sub();
1607
1608   return wantarray ? ($sum, $base_unit) : $sum;
1609 }
1610
1611 sub add_unit {
1612   $main::lxdebug->enter_sub();
1613
1614   my ($self, $myconfig, $form, $name, $base_unit, $factor, $languages) = @_;
1615
1616   my $dbh = $form->dbconnect_noauto($myconfig);
1617
1618   my $query = qq|SELECT COALESCE(MAX(sortkey), 0) + 1 FROM units|;
1619   my ($sortkey) = selectrow_query($form, $dbh, $query);
1620
1621   $query = "INSERT INTO units (name, base_unit, factor, sortkey) " .
1622     "VALUES (?, ?, ?, ?)";
1623   do_query($form, $dbh, $query, $name, $base_unit, $factor, $sortkey);
1624
1625   if ($languages) {
1626     $query = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
1627     my $sth = $dbh->prepare($query);
1628     foreach my $lang (@{$languages}) {
1629       my @values = ($name, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
1630       $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
1631     }
1632     $sth->finish();
1633   }
1634
1635   $dbh->commit();
1636   $dbh->disconnect();
1637
1638   $main::lxdebug->leave_sub();
1639 }
1640
1641 sub save_units {
1642   $main::lxdebug->enter_sub();
1643
1644   my ($self, $myconfig, $form, $units, $delete_units) = @_;
1645
1646   my $dbh = $form->dbconnect_noauto($myconfig);
1647
1648   my ($base_unit, $unit, $sth, $query);
1649
1650   $query = "DELETE FROM units_language";
1651   $dbh->do($query) || $form->dberror($query);
1652
1653   if ($delete_units && (0 != scalar(@{$delete_units}))) {
1654     $query = "DELETE FROM units WHERE name IN (";
1655     map({ $query .= "?," } @{$delete_units});
1656     substr($query, -1, 1) = ")";
1657     $dbh->do($query, undef, @{$delete_units}) ||
1658       $form->dberror($query . " (" . join(", ", @{$delete_units}) . ")");
1659   }
1660
1661   $query = "UPDATE units SET name = ?, base_unit = ?, factor = ? WHERE name = ?";
1662   $sth = $dbh->prepare($query);
1663
1664   my $query_lang = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
1665   my $sth_lang = $dbh->prepare($query_lang);
1666
1667   foreach $unit (values(%{$units})) {
1668     $unit->{"depth"} = 0;
1669     my $base_unit = $unit;
1670     while ($base_unit->{"base_unit"}) {
1671       $unit->{"depth"}++;
1672       $base_unit = $units->{$base_unit->{"base_unit"}};
1673     }
1674   }
1675
1676   foreach $unit (sort({ $a->{"depth"} <=> $b->{"depth"} } values(%{$units}))) {
1677     if ($unit->{"LANGUAGES"}) {
1678       foreach my $lang (@{$unit->{"LANGUAGES"}}) {
1679         next unless ($lang->{"id"} && $lang->{"localized"});
1680         my @values = ($unit->{"name"}, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
1681         $sth_lang->execute(@values) || $form->dberror($query_lang . " (" . join(", ", @values) . ")");
1682       }
1683     }
1684
1685     next if ($unit->{"unchanged_unit"});
1686
1687     my @values = ($unit->{"name"}, $unit->{"base_unit"}, $unit->{"factor"}, $unit->{"old_name"});
1688     $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
1689   }
1690
1691   $sth->finish();
1692   $sth_lang->finish();
1693   $dbh->commit();
1694   $dbh->disconnect();
1695
1696   $main::lxdebug->leave_sub();
1697 }
1698
1699 sub taxes {
1700   $main::lxdebug->enter_sub();
1701
1702   my ($self, $myconfig, $form) = @_;
1703
1704   # connect to database
1705   my $dbh = $form->dbconnect($myconfig);
1706
1707   my $query = qq|SELECT
1708                    t.id,
1709                    t.taxkey,
1710                    t.taxdescription,
1711                    round(t.rate * 100, 2) AS rate,
1712                    (SELECT accno FROM chart WHERE id = chart_id) AS taxnumber,
1713                    (SELECT description FROM chart WHERE id = chart_id) AS account_description
1714                  FROM tax t
1715                  ORDER BY taxkey|;
1716
1717   my $sth = $dbh->prepare($query);
1718   $sth->execute || $form->dberror($query);
1719
1720   $form->{TAX} = [];
1721   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1722     push @{ $form->{TAX} }, $ref;
1723   }
1724
1725   $sth->finish;
1726   $dbh->disconnect;
1727
1728   $main::lxdebug->leave_sub();
1729 }
1730
1731 sub get_tax_accounts {
1732   $main::lxdebug->enter_sub();
1733
1734   my ($self, $myconfig, $form) = @_;
1735
1736   my $dbh = $form->dbconnect($myconfig);
1737
1738   # get Accounts from chart
1739   my $query = qq{ SELECT
1740                  id,
1741                  accno || ' - ' || description AS taxaccount
1742                FROM chart
1743                WHERE link LIKE '%_tax%'
1744                ORDER BY accno
1745              };
1746
1747   my $sth = $dbh->prepare($query);
1748   $sth->execute || $form->dberror($query);
1749
1750   $form->{ACCOUNTS} = [];
1751   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1752     push @{ $form->{ACCOUNTS} }, $ref;
1753   }
1754
1755   $sth->finish;
1756
1757   $dbh->disconnect;
1758
1759   $main::lxdebug->leave_sub();
1760 }
1761
1762 sub get_tax {
1763   $main::lxdebug->enter_sub();
1764
1765   my ($self, $myconfig, $form) = @_;
1766
1767   # connect to database
1768   my $dbh = $form->dbconnect($myconfig);
1769
1770   my $query = qq|SELECT
1771                    taxkey,
1772                    taxdescription,
1773                    round(rate * 100, 2) AS rate,
1774                    chart_id
1775                  FROM tax
1776                  WHERE id = ? |;
1777
1778   my $sth = $dbh->prepare($query);
1779   $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
1780
1781   my $ref = $sth->fetchrow_hashref("NAME_lc");
1782
1783   map { $form->{$_} = $ref->{$_} } keys %$ref;
1784
1785   $sth->finish;
1786
1787   # see if it is used by a taxkey
1788   $query = qq|SELECT count(*) FROM taxkeys
1789               WHERE tax_id = ? AND chart_id >0|;
1790
1791   ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
1792
1793   $form->{orphaned} = !$form->{orphaned};
1794   $sth->finish;
1795
1796   if (!$form->{orphaned} ) {
1797     $query = qq|SELECT DISTINCT c.id, c.accno
1798                 FROM taxkeys tk
1799                 JOIN   tax t ON (t.id = tk.tax_id)
1800                 JOIN chart c ON (c.id = tk.chart_id)
1801                 WHERE tk.tax_id = ?|;
1802
1803     $sth = $dbh->prepare($query);
1804     $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
1805
1806     $form->{TAXINUSE} = [];
1807     while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
1808       push @{ $form->{TAXINUSE} }, $ref;
1809     }
1810
1811     $sth->finish;
1812   }
1813
1814   $dbh->disconnect;
1815
1816   $main::lxdebug->leave_sub();
1817 }
1818
1819 sub save_tax {
1820   $main::lxdebug->enter_sub();
1821
1822   my ($self, $myconfig, $form) = @_;
1823   my $query;
1824
1825   # connect to database
1826   my $dbh = $form->get_standard_dbh($myconfig);
1827
1828   $form->{rate} = $form->{rate} / 100;
1829
1830   my @values = ($form->{taxkey}, $form->{taxdescription}, $form->{rate}, $form->{chart_id}, $form->{chart_id} );
1831   if ($form->{id} ne "") {
1832     $query = qq|UPDATE tax SET
1833                   taxkey         = ?,
1834                   taxdescription = ?,
1835                   rate           = ?,
1836                   chart_id       = ?,
1837                   taxnumber      = (SELECT accno FROM chart WHERE id= ? )
1838                 WHERE id = ?|;
1839     push(@values, $form->{id});
1840
1841   } else {
1842     #ok
1843     $query = qq|INSERT INTO tax (
1844                   taxkey,
1845                   taxdescription,
1846                   rate,
1847                   chart_id,
1848                   taxnumber
1849                 )
1850                 VALUES (?, ?, ?, ?, (SELECT accno FROM chart WHERE id = ?) )|;
1851   }
1852   do_query($form, $dbh, $query, @values);
1853
1854   $dbh->commit();
1855
1856   $main::lxdebug->leave_sub();
1857 }
1858
1859 sub delete_tax {
1860   $main::lxdebug->enter_sub();
1861
1862   my ($self, $myconfig, $form) = @_;
1863   my $query;
1864
1865   # connect to database
1866   my $dbh = $form->get_standard_dbh($myconfig);
1867
1868   $query = qq|DELETE FROM tax
1869               WHERE id = ?|;
1870   do_query($form, $dbh, $query, $form->{id});
1871
1872   $dbh->commit();
1873
1874   $main::lxdebug->leave_sub();
1875 }
1876
1877 sub save_price_factor {
1878   $main::lxdebug->enter_sub();
1879
1880   my ($self, $myconfig, $form) = @_;
1881
1882   # connect to database
1883   my $dbh = $form->get_standard_dbh($myconfig);
1884
1885   my $query;
1886   my @values = ($form->{description}, conv_i($form->{factor}));
1887
1888   if ($form->{id}) {
1889     $query = qq|UPDATE price_factors SET description = ?, factor = ? WHERE id = ?|;
1890     push @values, conv_i($form->{id});
1891
1892   } else {
1893     $query = qq|INSERT INTO price_factors (description, factor, sortkey) VALUES (?, ?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM price_factors))|;
1894   }
1895
1896   do_query($form, $dbh, $query, @values);
1897
1898   $dbh->commit();
1899
1900   $main::lxdebug->leave_sub();
1901 }
1902
1903 sub get_all_price_factors {
1904   $main::lxdebug->enter_sub();
1905
1906   my ($self, $myconfig, $form) = @_;
1907
1908   # connect to database
1909   my $dbh = $form->get_standard_dbh($myconfig);
1910
1911   $form->{PRICE_FACTORS} = selectall_hashref_query($form, $dbh, qq|SELECT * FROM price_factors ORDER BY sortkey|);
1912
1913   $main::lxdebug->leave_sub();
1914 }
1915
1916 sub get_price_factor {
1917   $main::lxdebug->enter_sub();
1918
1919   my ($self, $myconfig, $form) = @_;
1920
1921   # connect to database
1922   my $dbh = $form->get_standard_dbh($myconfig);
1923
1924   my $query = qq|SELECT description, factor,
1925                    ((SELECT COUNT(*) FROM parts      WHERE price_factor_id = ?) +
1926                     (SELECT COUNT(*) FROM invoice    WHERE price_factor_id = ?) +
1927                     (SELECT COUNT(*) FROM orderitems WHERE price_factor_id = ?)) = 0 AS orphaned
1928                  FROM price_factors WHERE id = ?|;
1929
1930   ($form->{description}, $form->{factor}, $form->{orphaned}) = selectrow_query($form, $dbh, $query, (conv_i($form->{id})) x 4);
1931
1932   $main::lxdebug->leave_sub();
1933 }
1934
1935 sub delete_price_factor {
1936   $main::lxdebug->enter_sub();
1937
1938   my ($self, $myconfig, $form) = @_;
1939
1940   # connect to database
1941   my $dbh = $form->get_standard_dbh($myconfig);
1942
1943   do_query($form, $dbh, qq|DELETE FROM price_factors WHERE id = ?|, conv_i($form->{id}));
1944   $dbh->commit();
1945
1946   $main::lxdebug->leave_sub();
1947 }
1948
1949 sub save_warehouse {
1950   $main::lxdebug->enter_sub();
1951
1952   my ($self, $myconfig, $form) = @_;
1953
1954   # connect to database
1955   my $dbh = $form->get_standard_dbh($myconfig);
1956
1957   my ($query, @values, $sth);
1958
1959   if (!$form->{id}) {
1960     $query        = qq|SELECT nextval('id')|;
1961     ($form->{id}) = selectrow_query($form, $dbh, $query);
1962
1963     $query        = qq|INSERT INTO warehouse (id, sortkey) VALUES (?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM warehouse))|;
1964     do_query($form, $dbh, $query, $form->{id});
1965   }
1966
1967   do_query($form, $dbh, qq|UPDATE warehouse SET description = ?, invalid = ? WHERE id = ?|,
1968            $form->{description}, $form->{invalid} ? 't' : 'f', conv_i($form->{id}));
1969
1970   if (0 < $form->{number_of_new_bins}) {
1971     $query = qq|INSERT INTO bin (warehouse_id, description) VALUES (?, ?)|;
1972     $sth   = prepare_query($form, $dbh, $query);
1973
1974     foreach my $i (1..$form->{number_of_new_bins}) {
1975       do_statement($form, $sth, $query, conv_i($form->{id}), "$form->{prefix}${i}");
1976     }
1977
1978     $sth->finish();
1979   }
1980
1981   $dbh->commit();
1982
1983   $main::lxdebug->leave_sub();
1984 }
1985
1986 sub save_bins {
1987   $main::lxdebug->enter_sub();
1988
1989   my ($self, $myconfig, $form) = @_;
1990
1991   # connect to database
1992   my $dbh = $form->get_standard_dbh($myconfig);
1993
1994   my ($query, @values, $commit_necessary, $sth);
1995
1996   @values = map { $form->{"id_${_}"} } grep { $form->{"delete_${_}"} } (1..$form->{rowcount});
1997
1998   if (@values) {
1999     $query = qq|DELETE FROM bin WHERE id IN (| . join(', ', ('?') x scalar(@values)) . qq|)|;
2000     do_query($form, $dbh, $query, @values);
2001
2002     $commit_necessary = 1;
2003   }
2004
2005   $query = qq|UPDATE bin SET description = ? WHERE id = ?|;
2006   $sth   = prepare_query($form, $dbh, $query);
2007
2008   foreach my $row (1..$form->{rowcount}) {
2009     next if ($form->{"delete_${row}"});
2010
2011     do_statement($form, $sth, $query, $form->{"description_${row}"}, conv_i($form->{"id_${row}"}));
2012
2013     $commit_necessary = 1;
2014   }
2015
2016   $sth->finish();
2017
2018   $dbh->commit() if ($commit_necessary);
2019
2020   $main::lxdebug->leave_sub();
2021 }
2022
2023 sub delete_warehouse {
2024   $main::lxdebug->enter_sub();
2025
2026   my ($self, $myconfig, $form) = @_;
2027
2028   # connect to database
2029   my $dbh = $form->get_standard_dbh($myconfig);
2030
2031   my $id      = conv_i($form->{id});
2032   my $query   = qq|SELECT i.bin_id FROM inventory i WHERE i.bin_id IN (SELECT b.id FROM bin b WHERE b.warehouse_id = ?) LIMIT 1|;
2033   my ($count) = selectrow_query($form, $dbh, $query, $id);
2034
2035   if ($count) {
2036     $main::lxdebug->leave_sub();
2037     return 0;
2038   }
2039
2040   do_query($form, $dbh, qq|DELETE FROM bin       WHERE warehouse_id = ?|, conv_i($form->{id}));
2041   do_query($form, $dbh, qq|DELETE FROM warehouse WHERE id           = ?|, conv_i($form->{id}));
2042
2043   $dbh->commit();
2044
2045   $main::lxdebug->leave_sub();
2046
2047   return 1;
2048 }
2049
2050 sub get_all_warehouses {
2051   $main::lxdebug->enter_sub();
2052
2053   my ($self, $myconfig, $form) = @_;
2054
2055   # connect to database
2056   my $dbh = $form->get_standard_dbh($myconfig);
2057
2058   my $query = qq|SELECT w.id, w.description, w.invalid,
2059                    (SELECT COUNT(b.description) FROM bin b WHERE b.warehouse_id = w.id) AS number_of_bins
2060                  FROM warehouse w
2061                  ORDER BY w.sortkey|;
2062
2063   $form->{WAREHOUSES} = selectall_hashref_query($form, $dbh, $query);
2064
2065   $main::lxdebug->leave_sub();
2066 }
2067
2068 sub get_warehouse {
2069   $main::lxdebug->enter_sub();
2070
2071   my ($self, $myconfig, $form) = @_;
2072
2073   # connect to database
2074   my $dbh = $form->get_standard_dbh($myconfig);
2075
2076   my $id    = conv_i($form->{id});
2077   my $query = qq|SELECT w.description, w.invalid
2078                  FROM warehouse w
2079                  WHERE w.id = ?|;
2080
2081   my $ref   = selectfirst_hashref_query($form, $dbh, $query, $id);
2082
2083   map { $form->{$_} = $ref->{$_} } keys %{ $ref };
2084
2085   $query = qq|SELECT b.*, EXISTS
2086                 (SELECT i.warehouse_id
2087                  FROM inventory i
2088                  WHERE i.bin_id = b.id
2089                  LIMIT 1)
2090                 AS in_use
2091               FROM bin b
2092               WHERE b.warehouse_id = ?|;
2093
2094   $form->{BINS} = selectall_hashref_query($form, $dbh, $query, conv_i($form->{id}));
2095
2096   $main::lxdebug->leave_sub();
2097 }
2098
2099 1;