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