1 #=====================================================================
 
   4 # Based on SQL-Ledger Version 2.1.9
 
   5 # Web http://www.lx-office.org
 
   7 #=====================================================================
 
   8 # SQL-Ledger Accounting
 
  11 #  Author: Dieter Simader
 
  12 #   Email: dsimader@sql-ledger.org
 
  13 #     Web: http://www.sql-ledger.org
 
  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.
 
  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 #======================================================================
 
  31 # Administration module
 
  36 #======================================================================
 
  47   $main::lxdebug->enter_sub();
 
  49   my ($self, $myconfig, $form) = @_;
 
  52   my $dbh = $form->dbconnect($myconfig);
 
  54     SELECT c.accno, c.description, c.charttype, c.category,
 
  55       c.link, c.pos_bilanz, c.pos_eur, c.new_chart_id, c.valid_from,
 
  56       c.pos_bwa, datevautomatik,
 
  57       tk.taxkey_id, tk.pos_ustva, tk.tax_id,
 
  58       tk.tax_id || '--' || tk.taxkey_id AS tax, tk.startdate
 
  61     ON (c.id=tk.chart_id AND tk.id =
 
  62       (SELECT id FROM taxkeys
 
  63        WHERE taxkeys.chart_id = c.id AND startdate <= current_date
 
  64        ORDER BY startdate DESC LIMIT 1))
 
  69   $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
 
  70   my $sth = $dbh->prepare($query);
 
  71   $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
  73   my $ref = $sth->fetchrow_hashref("NAME_lc");
 
  75   foreach my $key (keys %$ref) {
 
  76     $form->{"$key"} = $ref->{"$key"};
 
  81   # get default accounts
 
  82   $query = qq|SELECT inventory_accno_id, income_accno_id, expense_accno_id
 
  84   $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
 
  85   $sth = $dbh->prepare($query);
 
  86   $sth->execute || $form->dberror($query);
 
  88   $ref = $sth->fetchrow_hashref("NAME_lc");
 
  90   map { $form->{$_} = $ref->{$_} } keys %{ $ref };
 
  96   # get taxkeys and description
 
 100       (SELECT accno FROM chart WHERE id=tax.chart_id) AS chart_accno,
 
 102       id||'--'||taxkey AS tax,
 
 105     FROM tax ORDER BY taxkey
 
 107   $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
 
 108   $sth = $dbh->prepare($query);
 
 109   $sth->execute || $form->dberror($query);
 
 111   $form->{TAXKEY} = [];
 
 113   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 114     push @{ $form->{TAXKEY} }, $ref;
 
 120     $query = qq|SELECT id, accno,description
 
 124     $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
 
 125     $sth = $dbh->prepare($query);
 
 126     $sth->execute($form->{link}) || $form->dberror($query . " ($form->{link})");
 
 128     $form->{NEWACCOUNT} = [];
 
 129     while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 130       push @{ $form->{NEWACCOUNT} }, $ref;
 
 135     # get the taxkeys of account
 
 149       LEFT JOIN   tax t ON (t.id = tk.tax_id)
 
 150       LEFT JOIN chart c ON (c.id = t.chart_id)
 
 152       WHERE tk.chart_id = ?
 
 153       ORDER BY startdate DESC
 
 155     $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
 
 156     $sth = $dbh->prepare($query);
 
 158     $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
 160     $form->{ACCOUNT_TAXKEYS} = [];
 
 162     while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 163       push @{ $form->{ACCOUNT_TAXKEYS} }, $ref;
 
 169   # check if we have any transactions
 
 170   $query = qq|SELECT a.trans_id FROM acc_trans a
 
 171               WHERE a.chart_id = ?|;
 
 172   $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
 
 173   $sth = $dbh->prepare($query);
 
 174   $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
 176   ($form->{orphaned}) = $sth->fetchrow_array;
 
 177   $form->{orphaned} = !$form->{orphaned};
 
 180   # check if new account is active
 
 181   $form->{new_chart_valid} = 0;
 
 182   if ($form->{new_chart_id}) {
 
 183     $query = qq|SELECT current_date-valid_from FROM chart
 
 185     $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
 
 186     my ($count) = selectrow_query($form, $dbh, $query, $form->{id});
 
 188       $form->{new_chart_valid} = 1;
 
 195   $main::lxdebug->leave_sub();
 
 199   $main::lxdebug->enter_sub();
 
 201   my ($self, $myconfig, $form) = @_;
 
 203   # connect to database, turn off AutoCommit
 
 204   my $dbh = $form->dbconnect_noauto($myconfig);
 
 206   # sanity check, can't have AR with AR_...
 
 207   if ($form->{AR} || $form->{AP} || $form->{IC}) {
 
 208     map { delete $form->{$_} }
 
 209       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 CT_tax);
 
 213   foreach my $item ($form->{AR},            $form->{AR_amount},
 
 214                     $form->{AR_tax},        $form->{AR_paid},
 
 215                     $form->{AP},            $form->{AP_amount},
 
 216                     $form->{AP_tax},        $form->{AP_paid},
 
 217                     $form->{IC},            $form->{IC_sale},
 
 218                     $form->{IC_cogs},       $form->{IC_taxpart},
 
 219                     $form->{IC_income},     $form->{IC_expense},
 
 220                     $form->{IC_taxservice}, $form->{CT_tax}
 
 222     $form->{link} .= "${item}:" if ($item);
 
 226   # strip blanks from accno
 
 227   map { $form->{$_} =~ s/ //g; } qw(accno);
 
 231   if ($form->{id} eq "NULL") {
 
 235   if (!$form->{id} || $form->{id} eq "") {
 
 236     $query = qq|SELECT nextval('id')|;
 
 237     ($form->{"id"}) = selectrow_query($form, $dbh, $query);
 
 238     $query = qq|INSERT INTO chart (id, accno) VALUES (?, ?)|;
 
 239     do_query($form, $dbh, $query, $form->{"id"}, $form->{"accno"});
 
 245     $query = qq|UPDATE chart SET
 
 261                   $form->{description},
 
 265                   conv_i($form->{pos_bwa}),
 
 266                   conv_i($form->{pos_bilanz}),
 
 267                   conv_i($form->{pos_eur}),
 
 268                   conv_i($form->{new_chart_id}),
 
 269                   conv_date($form->{valid_from}),
 
 270                   ($form->{datevautomatik} eq 'T') ? 'true':'false',
 
 276   do_query($form, $dbh, $query, @values);
 
 282   my $MAX_TRIES = 10; # Maximum count of taxkeys in form
 
 286   for $tk_count (0 .. $MAX_TRIES) {
 
 290     # Check if the account already exists, else cancel
 
 292     print(STDERR "Keine Taxkeys weil ID =: $form->{id}\n");
 
 294     last READTAXKEYS if ( $form->{'id'} == 0);
 
 296     # check if there is a startdate
 
 297     if ( $form->{"taxkey_startdate_$tk_count"} eq '' ) {
 
 302     # Add valid taxkeys into the array
 
 305         id        => ($form->{"taxkey_id_$tk_count"} eq 'NEW') ? conv_i('') : conv_i($form->{"taxkey_id_$tk_count"}),
 
 306         tax_id    => conv_i($form->{"taxkey_tax_$tk_count"}),
 
 307         startdate => conv_date($form->{"taxkey_startdate_$tk_count"}),
 
 308         chart_id  => conv_i($form->{"id"}),
 
 309         pos_ustva => conv_i($form->{"taxkey_pos_ustva_$tk_count"}),
 
 310         delete    => ( $form->{"taxkey_del_$tk_count"} eq 'delete' ) ? '1' : '',
 
 317   for my $j (0 .. $#taxkeys){
 
 318     if ( defined $taxkeys[$j]{'id'} ){
 
 321       if ($taxkeys[$j]{'delete'}){
 
 323           DELETE FROM taxkeys WHERE id = ?
 
 326         @values = ($taxkeys[$j]{'id'});
 
 328         do_query($form, $dbh, $query, @values);
 
 337         SET taxkey_id = (SELECT taxkey FROM tax WHERE tax.id = ?),
 
 345         $taxkeys[$j]{'tax_id'},
 
 346         $taxkeys[$j]{'chart_id'},
 
 347         $taxkeys[$j]{'tax_id'},
 
 348         $taxkeys[$j]{'pos_ustva'},
 
 349         $taxkeys[$j]{'startdate'},
 
 352       do_query($form, $dbh, $query, @values);
 
 358         INSERT INTO taxkeys (
 
 365         VALUES ((SELECT taxkey FROM tax WHERE tax.id = ?), ?, ?, ?, ?)
 
 368         $taxkeys[$j]{'tax_id'},
 
 369         $taxkeys[$j]{'chart_id'},
 
 370         $taxkeys[$j]{'tax_id'},
 
 371         $taxkeys[$j]{'pos_ustva'},
 
 372         $taxkeys[$j]{'startdate'},
 
 375       do_query($form, $dbh, $query, @values);
 
 381   my $rc = $dbh->commit;
 
 384   $main::lxdebug->leave_sub();
 
 390   $main::lxdebug->enter_sub();
 
 392   my ($self, $myconfig, $form) = @_;
 
 394   # connect to database, turn off AutoCommit
 
 395   my $dbh = $form->dbconnect_noauto($myconfig);
 
 397   my $query = qq|SELECT count(*) FROM acc_trans a
 
 398                  WHERE a.chart_id = ?|;
 
 399   my ($count) = selectrow_query($form, $dbh, $query, $form->{id});
 
 403     $main::lxdebug->leave_sub();
 
 407   # set inventory_accno_id, income_accno_id, expense_accno_id to defaults
 
 408   foreach my $type (qw(inventory income expense)) {
 
 411       qq|SET ${type}_accno_id = (SELECT ${type}_accno_id FROM defaults) | .
 
 412       qq|WHERE ${type}_accno_id = ?|;
 
 413     do_query($form, $dbh, $query, $form->{id});
 
 416   foreach my $table (qw(partstax customertax vendortax tax)) {
 
 417     $query = qq|DELETE FROM $table
 
 419     do_query($form, $dbh, $query, $form->{id});
 
 422   # delete chart of account record
 
 423   $query = qq|DELETE FROM chart
 
 425   do_query($form, $dbh, $query, $form->{id});
 
 427   # delete account taxkeys
 
 428   $query = qq|DELETE FROM taxkeys
 
 430   do_query($form, $dbh, $query, $form->{id});
 
 432   # commit and redirect
 
 433   my $rc = $dbh->commit;
 
 436   $main::lxdebug->leave_sub();
 
 442   $main::lxdebug->enter_sub();
 
 444   my ($self, $myconfig, $form) = @_;
 
 446   # connect to database
 
 447   my $dbh = $form->dbconnect($myconfig);
 
 449   my $query = qq|SELECT d.id, d.description, d.role
 
 453   my $sth = $dbh->prepare($query);
 
 454   $sth->execute || $form->dberror($query);
 
 457   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 458     push @{ $form->{ALL} }, $ref;
 
 464   $main::lxdebug->leave_sub();
 
 468   $main::lxdebug->enter_sub();
 
 470   my ($self, $myconfig, $form) = @_;
 
 472   # connect to database
 
 473   my $dbh = $form->dbconnect($myconfig);
 
 475   my $query = qq|SELECT d.description, d.role
 
 478   my $sth = $dbh->prepare($query);
 
 479   $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
 481   my $ref = $sth->fetchrow_hashref("NAME_lc");
 
 483   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
 487   # see if it is in use
 
 488   $query = qq|SELECT count(*) FROM dpt_trans d
 
 489               WHERE d.department_id = ?|;
 
 490   ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
 
 492   $form->{orphaned} = !$form->{orphaned};
 
 497   $main::lxdebug->leave_sub();
 
 500 sub save_department {
 
 501   $main::lxdebug->enter_sub();
 
 503   my ($self, $myconfig, $form) = @_;
 
 506   # connect to database
 
 507   my $dbh = $form->dbconnect($myconfig);
 
 509   my @values = ($form->{description}, $form->{role});
 
 511     $query = qq|UPDATE department SET
 
 512                 description = ?, role = ?
 
 514     push(@values, $form->{id});
 
 516     $query = qq|INSERT INTO department
 
 520   do_query($form, $dbh, $query, @values);
 
 524   $main::lxdebug->leave_sub();
 
 527 sub delete_department {
 
 528   $main::lxdebug->enter_sub();
 
 530   my ($self, $myconfig, $form) = @_;
 
 533   # connect to database
 
 534   my $dbh = $form->dbconnect($myconfig);
 
 536   $query = qq|DELETE FROM department
 
 538   do_query($form, $dbh, $query, $form->{id});
 
 542   $main::lxdebug->leave_sub();
 
 546   $main::lxdebug->enter_sub();
 
 548   my ($self, $myconfig, $form) = @_;
 
 550   # connect to database
 
 551   my $dbh = $form->dbconnect($myconfig);
 
 553   my $query = qq|SELECT id, lead
 
 557   my $sth = $dbh->prepare($query);
 
 558   $sth->execute || $form->dberror($query);
 
 560   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 561     push @{ $form->{ALL} }, $ref;
 
 567   $main::lxdebug->leave_sub();
 
 571   $main::lxdebug->enter_sub();
 
 573   my ($self, $myconfig, $form) = @_;
 
 575   # connect to database
 
 576   my $dbh = $form->dbconnect($myconfig);
 
 579     qq|SELECT l.id, l.lead | .
 
 582   my $sth = $dbh->prepare($query);
 
 583   $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
 585   my $ref = $sth->fetchrow_hashref("NAME_lc");
 
 587   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
 593   $main::lxdebug->leave_sub();
 
 597   $main::lxdebug->enter_sub();
 
 599   my ($self, $myconfig, $form) = @_;
 
 602   # connect to database
 
 603   my $dbh = $form->dbconnect($myconfig);
 
 605   my @values = ($form->{description});
 
 606   # id is the old record
 
 608     $query = qq|UPDATE leads SET
 
 611     puhs(@values, $form->{id});
 
 613     $query = qq|INSERT INTO leads
 
 617   do_query($form, $dbh, $query, @values);
 
 621   $main::lxdebug->leave_sub();
 
 625   $main::lxdebug->enter_sub();
 
 627   my ($self, $myconfig, $form) = @_;
 
 630   # connect to database
 
 631   my $dbh = $form->dbconnect($myconfig);
 
 633   $query = qq|DELETE FROM leads
 
 635   do_query($form, $dbh, $query, $form->{id});
 
 639   $main::lxdebug->leave_sub();
 
 643   $main::lxdebug->enter_sub();
 
 645   my ($self, $myconfig, $form) = @_;
 
 647   # connect to database
 
 648   my $dbh = $form->dbconnect($myconfig);
 
 650   my $query = qq|SELECT id, description, discount, customernumberinit, salesman
 
 654   my $sth = $dbh->prepare($query);
 
 655   $sth->execute || $form->dberror($query);
 
 657   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 658     push @{ $form->{ALL} }, $ref;
 
 664   $main::lxdebug->leave_sub();
 
 668   $main::lxdebug->enter_sub();
 
 670   my ($self, $myconfig, $form) = @_;
 
 672   # connect to database
 
 673   my $dbh = $form->dbconnect($myconfig);
 
 676     qq|SELECT b.description, b.discount, b.customernumberinit, b.salesman
 
 679   my $sth = $dbh->prepare($query);
 
 680   $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
 682   my $ref = $sth->fetchrow_hashref("NAME_lc");
 
 684   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
 690   $main::lxdebug->leave_sub();
 
 694   $main::lxdebug->enter_sub();
 
 696   my ($self, $myconfig, $form) = @_;
 
 699   # connect to database
 
 700   my $dbh = $form->dbconnect($myconfig);
 
 702   my @values = ($form->{description}, $form->{discount}, $form->{customernumberinit}, $form->{salesman} ? 't' : 'f');
 
 703   # id is the old record
 
 705     $query = qq|UPDATE business SET
 
 708                 customernumberinit = ?,
 
 711     push(@values, $form->{id});
 
 713     $query = qq|INSERT INTO business
 
 714                 (description, discount, customernumberinit, salesman)
 
 715                 VALUES (?, ?, ?, ?)|;
 
 717   do_query($form, $dbh, $query, @values);
 
 721   $main::lxdebug->leave_sub();
 
 724 sub delete_business {
 
 725   $main::lxdebug->enter_sub();
 
 727   my ($self, $myconfig, $form) = @_;
 
 729   # connect to database
 
 730   my $dbh = $form->dbconnect($myconfig);
 
 732   my $query = qq|DELETE FROM business
 
 734   do_query($form, $dbh, $query, $form->{id});
 
 738   $main::lxdebug->leave_sub();
 
 743   $main::lxdebug->enter_sub();
 
 745   my ($self, $myconfig, $form, $return_list) = @_;
 
 747   # connect to database
 
 748   my $dbh = $form->dbconnect($myconfig);
 
 751     "SELECT id, description, template_code, article_code, " .
 
 752     "  output_numberformat, output_dateformat, output_longdates " .
 
 753     "FROM language ORDER BY description";
 
 755   my $sth = $dbh->prepare($query);
 
 756   $sth->execute || $form->dberror($query);
 
 760   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 761     push(@{ $ary }, $ref);
 
 767   $main::lxdebug->leave_sub();
 
 777   $main::lxdebug->enter_sub();
 
 779   my ($self, $myconfig, $form) = @_;
 
 781   # connect to database
 
 782   my $dbh = $form->dbconnect($myconfig);
 
 785     "SELECT description, template_code, article_code, " .
 
 786     "  output_numberformat, output_dateformat, output_longdates " .
 
 787     "FROM language WHERE id = ?";
 
 788   my $sth = $dbh->prepare($query);
 
 789   $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
 
 791   my $ref = $sth->fetchrow_hashref("NAME_lc");
 
 793   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
 799   $main::lxdebug->leave_sub();
 
 802 sub get_language_details {
 
 803   $main::lxdebug->enter_sub();
 
 805   my ($self, $myconfig, $form, $id) = @_;
 
 807   # connect to database
 
 808   my $dbh = $form->dbconnect($myconfig);
 
 811     "SELECT template_code, " .
 
 812     "  output_numberformat, output_dateformat, output_longdates " .
 
 813     "FROM language WHERE id = ?";
 
 814   my @res = selectrow_query($form, $dbh, $query, $id);
 
 817   $main::lxdebug->leave_sub();
 
 823   $main::lxdebug->enter_sub();
 
 825   my ($self, $myconfig, $form) = @_;
 
 827   # connect to database
 
 828   my $dbh = $form->dbconnect($myconfig);
 
 829   my (@values, $query);
 
 831   map({ push(@values, $form->{$_}); }
 
 832       qw(description template_code article_code
 
 833          output_numberformat output_dateformat output_longdates));
 
 835   # id is the old record
 
 838       "UPDATE language SET " .
 
 839       "  description = ?, template_code = ?, article_code = ?, " .
 
 840       "  output_numberformat = ?, output_dateformat = ?, " .
 
 841       "  output_longdates = ? " .
 
 843     push(@values, $form->{id});
 
 846       "INSERT INTO language (" .
 
 847       "  description, template_code, article_code, " .
 
 848       "  output_numberformat, output_dateformat, output_longdates" .
 
 849       ") VALUES (?, ?, ?, ?, ?, ?)";
 
 851   do_query($form, $dbh, $query, @values);
 
 855   $main::lxdebug->leave_sub();
 
 858 sub delete_language {
 
 859   $main::lxdebug->enter_sub();
 
 861   my ($self, $myconfig, $form) = @_;
 
 864   # connect to database
 
 865   my $dbh = $form->dbconnect_noauto($myconfig);
 
 867   foreach my $table (qw(translation_payment_terms units_language)) {
 
 868     $query = qq|DELETE FROM $table WHERE language_id = ?|;
 
 869     do_query($form, $dbh, $query, $form->{"id"});
 
 872   $query = "DELETE FROM language WHERE id = ?";
 
 873   do_query($form, $dbh, $query, $form->{"id"});
 
 878   $main::lxdebug->leave_sub();
 
 883   $main::lxdebug->enter_sub();
 
 885   my ($self, $myconfig, $form) = @_;
 
 887   # connect to database
 
 888   my $dbh = $form->dbconnect($myconfig);
 
 890   my $query = qq|SELECT id, description,
 
 892                  (SELECT accno FROM chart WHERE id = inventory_accno_id) AS inventory_accno,
 
 894                  (SELECT accno FROM chart WHERE id = income_accno_id_0) AS income_accno_0,
 
 896                  (SELECT accno FROM chart WHERE id = expense_accno_id_0) AS expense_accno_0,
 
 898                  (SELECT accno FROM chart WHERE id = income_accno_id_1) AS income_accno_1,
 
 900                  (SELECT accno FROM chart WHERE id = expense_accno_id_1) AS expense_accno_1,
 
 902                  (SELECT accno FROM chart WHERE id = income_accno_id_2) AS income_accno_2,
 
 904                  (select accno FROM chart WHERE id = expense_accno_id_2) AS expense_accno_2,
 
 906                  (SELECT accno FROM chart WHERE id = income_accno_id_3) AS income_accno_3,
 
 908                  (SELECT accno FROM chart WHERE id = expense_accno_id_3) AS expense_accno_3
 
 912   my $sth = $dbh->prepare($query);
 
 913   $sth->execute || $form->dberror($query);
 
 916   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 917     push @{ $form->{ALL} }, $ref;
 
 923   $main::lxdebug->leave_sub();
 
 926 sub get_buchungsgruppe {
 
 927   $main::lxdebug->enter_sub();
 
 929   my ($self, $myconfig, $form) = @_;
 
 932   # connect to database
 
 933   my $dbh = $form->dbconnect($myconfig);
 
 937       qq|SELECT description, inventory_accno_id,
 
 938          (SELECT accno FROM chart WHERE id = inventory_accno_id) AS inventory_accno,
 
 940          (SELECT accno FROM chart WHERE id = income_accno_id_0) AS income_accno_0,
 
 942          (SELECT accno FROM chart WHERE id = expense_accno_id_0) AS expense_accno_0,
 
 944          (SELECT accno FROM chart WHERE id = income_accno_id_1) AS income_accno_1,
 
 946          (SELECT accno FROM chart WHERE id = expense_accno_id_1) AS expense_accno_1,
 
 948          (SELECT accno FROM chart WHERE id = income_accno_id_2) AS income_accno_2,
 
 950          (select accno FROM chart WHERE id = expense_accno_id_2) AS expense_accno_2,
 
 952          (SELECT accno FROM chart WHERE id = income_accno_id_3) AS income_accno_3,
 
 954          (SELECT accno FROM chart WHERE id = expense_accno_id_3) AS expense_accno_3
 
 957     my $sth = $dbh->prepare($query);
 
 958     $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
 960     my $ref = $sth->fetchrow_hashref("NAME_lc");
 
 962     map { $form->{$_} = $ref->{$_} } keys %$ref;
 
 967       qq|SELECT count(id) = 0 AS orphaned
 
 969          WHERE buchungsgruppen_id = ?|;
 
 970     ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
 
 973   $query = "SELECT inventory_accno_id, income_accno_id, expense_accno_id ".
 
 975   ($form->{"std_inventory_accno_id"}, $form->{"std_income_accno_id"},
 
 976    $form->{"std_expense_accno_id"}) = selectrow_query($form, $dbh, $query);
 
 979   $query = qq|SELECT c.accno, c.description, c.link, c.id,
 
 980               d.inventory_accno_id, d.income_accno_id, d.expense_accno_id
 
 981               FROM chart c, defaults d
 
 982               WHERE c.link LIKE '%$module%'
 
 986   my $sth = $dbh->prepare($query);
 
 987   $sth->execute || $form->dberror($query);
 
 988   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 989     foreach my $key (split(/:/, $ref->{link})) {
 
 990       if (!$form->{"std_inventory_accno_id"} && ($key eq "IC")) {
 
 991         $form->{"std_inventory_accno_id"} = $ref->{"id"};
 
 993       if ($key =~ /$module/) {
 
 994         if (   ($ref->{id} eq $ref->{inventory_accno_id})
 
 995             || ($ref->{id} eq $ref->{income_accno_id})
 
 996             || ($ref->{id} eq $ref->{expense_accno_id})) {
 
 997           push @{ $form->{"${module}_links"}{$key} },
 
 998             { accno       => $ref->{accno},
 
 999               description => $ref->{description},
 
1000               selected    => "selected",
 
1003           push @{ $form->{"${module}_links"}{$key} },
 
1004             { accno       => $ref->{accno},
 
1005               description => $ref->{description},
 
1017   $main::lxdebug->leave_sub();
 
1020 sub save_buchungsgruppe {
 
1021   $main::lxdebug->enter_sub();
 
1023   my ($self, $myconfig, $form) = @_;
 
1025   # connect to database
 
1026   my $dbh = $form->dbconnect($myconfig);
 
1028   my @values = ($form->{description}, $form->{inventory_accno_id},
 
1029                 $form->{income_accno_id_0}, $form->{expense_accno_id_0},
 
1030                 $form->{income_accno_id_1}, $form->{expense_accno_id_1},
 
1031                 $form->{income_accno_id_2}, $form->{expense_accno_id_2},
 
1032                 $form->{income_accno_id_3}, $form->{expense_accno_id_3});
 
1036   # id is the old record
 
1038     $query = qq|UPDATE buchungsgruppen SET
 
1039                 description = ?, inventory_accno_id = ?,
 
1040                 income_accno_id_0 = ?, expense_accno_id_0 = ?,
 
1041                 income_accno_id_1 = ?, expense_accno_id_1 = ?,
 
1042                 income_accno_id_2 = ?, expense_accno_id_2 = ?,
 
1043                 income_accno_id_3 = ?, expense_accno_id_3 = ?
 
1045     push(@values, $form->{id});
 
1047     $query = qq|SELECT COALESCE(MAX(sortkey) + 1, 1) FROM buchungsgruppen|;
 
1048     my ($sortkey) = $dbh->selectrow_array($query);
 
1049     $form->dberror($query) if ($dbh->err);
 
1050     push(@values, $sortkey);
 
1051     $query = qq|INSERT INTO buchungsgruppen
 
1052                 (description, inventory_accno_id,
 
1053                 income_accno_id_0, expense_accno_id_0,
 
1054                 income_accno_id_1, expense_accno_id_1,
 
1055                 income_accno_id_2, expense_accno_id_2,
 
1056                 income_accno_id_3, expense_accno_id_3,
 
1058                 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
 
1060   do_query($form, $dbh, $query, @values);
 
1064   $main::lxdebug->leave_sub();
 
1067 sub delete_buchungsgruppe {
 
1068   $main::lxdebug->enter_sub();
 
1070   my ($self, $myconfig, $form) = @_;
 
1072   # connect to database
 
1073   my $dbh = $form->dbconnect($myconfig);
 
1075   my $query = qq|DELETE FROM buchungsgruppen WHERE id = ?|;
 
1076   do_query($form, $dbh, $query, $form->{id});
 
1080   $main::lxdebug->leave_sub();
 
1084   $main::lxdebug->enter_sub();
 
1086   my ($self, $myconfig, $form, $table) = @_;
 
1088   # connect to database
 
1089   my $dbh = $form->get_standard_dbh($myconfig);
 
1093        (SELECT sortkey FROM $table WHERE id = ?) AS sortkey1,
 
1094        (SELECT sortkey FROM $table WHERE id = ?) AS sortkey2|;
 
1095   my @values   = ($form->{"id1"}, $form->{"id2"});
 
1096   my @sortkeys = selectrow_query($form, $dbh, $query, @values);
 
1098   $query  = qq|UPDATE $table SET sortkey = ? WHERE id = ?|;
 
1099   my $sth = prepare_query($form, $dbh, $query);
 
1101   do_statement($form, $sth, $query, $sortkeys[1], $form->{"id1"});
 
1102   do_statement($form, $sth, $query, $sortkeys[0], $form->{"id2"});
 
1108   $main::lxdebug->leave_sub();
 
1112   $main::lxdebug->enter_sub();
 
1114   my ($self, $myconfig, $form) = @_;
 
1116   # connect to database
 
1117   my $dbh = $form->dbconnect($myconfig);
 
1119   my $query = qq|SELECT id, printer_description, template_code, printer_command
 
1123   my $sth = $dbh->prepare($query);
 
1124   $sth->execute || $form->dberror($query);
 
1126   $form->{"ALL"} = [];
 
1127   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
1128     push @{ $form->{ALL} }, $ref;
 
1134   $main::lxdebug->leave_sub();
 
1138   $main::lxdebug->enter_sub();
 
1140   my ($self, $myconfig, $form) = @_;
 
1142   # connect to database
 
1143   my $dbh = $form->dbconnect($myconfig);
 
1146     qq|SELECT p.printer_description, p.template_code, p.printer_command
 
1149   my $sth = $dbh->prepare($query);
 
1150   $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
1152   my $ref = $sth->fetchrow_hashref("NAME_lc");
 
1154   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
1160   $main::lxdebug->leave_sub();
 
1164   $main::lxdebug->enter_sub();
 
1166   my ($self, $myconfig, $form) = @_;
 
1169   # connect to database
 
1170   my $dbh = $form->dbconnect($myconfig);
 
1172   my @values = ($form->{printer_description},
 
1173                 $form->{template_code},
 
1174                 $form->{printer_command});
 
1176   # id is the old record
 
1178     $query = qq|UPDATE printers SET
 
1179                 printer_description = ?, template_code = ?, printer_command = ?
 
1181     push(@values, $form->{id});
 
1183     $query = qq|INSERT INTO printers
 
1184                 (printer_description, template_code, printer_command)
 
1187   do_query($form, $dbh, $query, @values);
 
1191   $main::lxdebug->leave_sub();
 
1194 sub delete_printer {
 
1195   $main::lxdebug->enter_sub();
 
1197   my ($self, $myconfig, $form) = @_;
 
1199   # connect to database
 
1200   my $dbh = $form->dbconnect($myconfig);
 
1202   my $query = qq|DELETE FROM printers
 
1204   do_query($form, $dbh, $query, $form->{id});
 
1208   $main::lxdebug->leave_sub();
 
1212   $main::lxdebug->enter_sub();
 
1214   my ($self, $myconfig, $form) = @_;
 
1216   # connect to database
 
1217   my $dbh = $form->dbconnect($myconfig);
 
1219   my $query = qq|SELECT * FROM payment_terms ORDER BY sortkey|;
 
1221   my $sth = $dbh->prepare($query);
 
1222   $sth->execute || $form->dberror($query);
 
1225   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
1226     push @{ $form->{ALL} }, $ref;
 
1232   $main::lxdebug->leave_sub();
 
1236   $main::lxdebug->enter_sub();
 
1238   my ($self, $myconfig, $form) = @_;
 
1240   # connect to database
 
1241   my $dbh = $form->dbconnect($myconfig);
 
1243   my $query = qq|SELECT * FROM payment_terms WHERE id = ?|;
 
1244   my $sth = $dbh->prepare($query);
 
1245   $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
 
1247   my $ref = $sth->fetchrow_hashref("NAME_lc");
 
1248   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
1252     qq|SELECT t.language_id, t.description_long, l.description AS language | .
 
1253     qq|FROM translation_payment_terms t | .
 
1254     qq|LEFT JOIN language l ON t.language_id = l.id | .
 
1255     qq|WHERE t.payment_terms_id = ? | .
 
1257     qq|SELECT l.id AS language_id, NULL AS description_long, | .
 
1258     qq|  l.description AS language | .
 
1259     qq|FROM language l|;
 
1260   $sth = $dbh->prepare($query);
 
1261   $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
 
1264   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
1265     $mapping{ $ref->{"language_id"} } = $ref
 
1266       unless (defined($mapping{ $ref->{"language_id"} }));
 
1270   $form->{"TRANSLATION"} = [sort({ $a->{"language"} cmp $b->{"language"} }
 
1275   $main::lxdebug->leave_sub();
 
1279   $main::lxdebug->enter_sub();
 
1281   my ($self, $myconfig, $form) = @_;
 
1283   # connect to database
 
1284   my $dbh = $form->dbconnect_noauto($myconfig);
 
1289     $query = qq|SELECT nextval('id'), COALESCE(MAX(sortkey) + 1, 1) | .
 
1290       qq|FROM payment_terms|;
 
1292     ($form->{id}, $sortkey) = selectrow_query($form, $dbh, $query);
 
1294     $query = qq|INSERT INTO payment_terms (id, sortkey) VALUES (?, ?)|;
 
1295     do_query($form, $dbh, $query, $form->{id}, $sortkey);
 
1299       qq|DELETE FROM translation_payment_terms | .
 
1300       qq|WHERE payment_terms_id = ?|;
 
1301     do_query($form, $dbh, $query, $form->{"id"});
 
1304   $query = qq|UPDATE payment_terms SET
 
1305               description = ?, description_long = ?,
 
1306               terms_netto = ?, terms_skonto = ?,
 
1309   my @values = ($form->{description}, $form->{description_long},
 
1310                 $form->{terms_netto} * 1, $form->{terms_skonto} * 1,
 
1311                 $form->{percent_skonto} * 1,
 
1313   do_query($form, $dbh, $query, @values);
 
1315   $query = qq|SELECT id FROM language|;
 
1317   my $sth = $dbh->prepare($query);
 
1318   $sth->execute() || $form->dberror($query);
 
1320   while (my ($id) = $sth->fetchrow_array()) {
 
1321     push(@language_ids, $id);
 
1326     qq|INSERT INTO translation_payment_terms | .
 
1327     qq|(language_id, payment_terms_id, description_long) | .
 
1328     qq|VALUES (?, ?, ?)|;
 
1329   $sth = $dbh->prepare($query);
 
1331   foreach my $language_id (@language_ids) {
 
1332     do_statement($form, $sth, $query, $language_id, $form->{"id"},
 
1333                  $form->{"description_long_${language_id}"});
 
1340   $main::lxdebug->leave_sub();
 
1343 sub delete_payment {
 
1344   $main::lxdebug->enter_sub();
 
1346   my ($self, $myconfig, $form) = @_;
 
1348   # connect to database
 
1349   my $dbh = $form->dbconnect_noauto($myconfig);
 
1352     qq|DELETE FROM translation_payment_terms WHERE payment_terms_id = ?|;
 
1353   do_query($form, $dbh, $query, $form->{"id"});
 
1355   $query = qq|DELETE FROM payment_terms WHERE id = ?|;
 
1356   do_query($form, $dbh, $query, $form->{"id"});
 
1361   $main::lxdebug->leave_sub();
 
1365 sub prepare_template_filename {
 
1366   $main::lxdebug->enter_sub();
 
1368   my ($self, $myconfig, $form) = @_;
 
1370   my ($filename, $display_filename);
 
1372   if ($form->{type} eq "stylesheet") {
 
1373     $filename = "css/$myconfig->{stylesheet}";
 
1374     $display_filename = $myconfig->{stylesheet};
 
1377     $filename = $form->{formname};
 
1379     if ($form->{language}) {
 
1380       my ($id, $template_code) = split(/--/, $form->{language});
 
1381       $filename .= "_${template_code}";
 
1384     if ($form->{printer}) {
 
1385       my ($id, $template_code) = split(/--/, $form->{printer});
 
1386       $filename .= "_${template_code}";
 
1389     $filename .= "." . ($form->{format} eq "html" ? "html" : "tex");
 
1390     $filename =~ s|.*/||;
 
1391     $display_filename = $filename;
 
1392     $filename = "$myconfig->{templates}/$filename";
 
1395   $main::lxdebug->leave_sub();
 
1397   return ($filename, $display_filename);
 
1402   $main::lxdebug->enter_sub();
 
1404   my ($self, $filename) = @_;
 
1406   my ($content, $lines) = ("", 0);
 
1410   if (open(TEMPLATE, $filename)) {
 
1411     while (<TEMPLATE>) {
 
1418   $main::lxdebug->leave_sub();
 
1420   return ($content, $lines);
 
1424   $main::lxdebug->enter_sub();
 
1426   my ($self, $filename, $content) = @_;
 
1432   if (open(TEMPLATE, ">$filename")) {
 
1433     $content =~ s/\r\n/\n/g;
 
1434     print(TEMPLATE $content);
 
1440   $main::lxdebug->leave_sub();
 
1446   $main::lxdebug->enter_sub();
 
1451   my $myconfig = \%main::myconfig;
 
1452   my $form     = $main::form;
 
1454   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
 
1457   map { ($accnos{$_}) = split(m/--/, $form->{$_}) } qw(inventory_accno income_accno expense_accno fxgain_accno fxloss_accno);
 
1459   $form->{curr}  =~ s/ //g;
 
1460   my @currencies =  grep { $_ ne '' } split m/:/, $form->{curr};
 
1461   my $currency   =  join ':', @currencies;
 
1463   # these defaults are database wide
 
1466     qq|UPDATE defaults SET
 
1467         inventory_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
 
1468         income_accno_id    = (SELECT c.id FROM chart c WHERE c.accno = ?),
 
1469         expense_accno_id   = (SELECT c.id FROM chart c WHERE c.accno = ?),
 
1470         fxgain_accno_id    = (SELECT c.id FROM chart c WHERE c.accno = ?),
 
1471         fxloss_accno_id    = (SELECT c.id FROM chart c WHERE c.accno = ?),
 
1488   my @values = ($accnos{inventory_accno}, $accnos{income_accno}, $accnos{expense_accno},
 
1489                 $accnos{fxgain_accno},    $accnos{fxloss_accno},
 
1490                 $form->{invnumber},       $form->{cnnumber},
 
1491                 $form->{sonumber},        $form->{ponumber},
 
1492                 $form->{sqnumber},        $form->{rfqnumber},
 
1493                 $form->{customernumber},  $form->{vendornumber},
 
1494                 $form->{articlenumber},   $form->{servicenumber},
 
1495                 $form->{sdonumber},       $form->{pdonumber},
 
1496                 $form->{yearend},         $currency,
 
1497                 $form->{businessnumber},  $form->{weightunit});
 
1498   do_query($form, $dbh, $query, @values);
 
1502   $main::lxdebug->leave_sub();
 
1506 sub save_preferences {
 
1507   $main::lxdebug->enter_sub();
 
1509   my ($self, $myconfig, $form, $webdav) = @_;
 
1511   my $dbh = $form->get_standard_dbh($myconfig);
 
1513   my ($currency, $businessnumber) = selectrow_query($form, $dbh, qq|SELECT curr, businessnumber FROM defaults|);
 
1516   my $query = qq|UPDATE employee SET name = ? WHERE login = ?|;
 
1517   do_query($form, $dbh, $query, $form->{name}, $form->{login});
 
1519   my $rc = $dbh->commit();
 
1521   # save first currency in myconfig
 
1522   $currency               =~ s/:.*//;
 
1523   $form->{currency}       =  $currency;
 
1525   $form->{businessnumber} =  $businessnumber;
 
1527   $myconfig = new User($form->{login});
 
1529   foreach my $item (keys %$form) {
 
1530     $myconfig->{$item} = $form->{$item};
 
1533   $myconfig->save_member($main::memberfile);
 
1535   my $auth = $main::auth;
 
1537   if ($auth->can_change_password()
 
1538       && defined $form->{new_password}
 
1539       && ($form->{new_password} ne '********')) {
 
1540     $auth->change_password($form->{login}, $form->{new_password});
 
1542     $form->{password} = $form->{new_password};
 
1543     $auth->set_session_value('password', $form->{password});
 
1544     $auth->create_or_refresh_session();
 
1547   $main::lxdebug->leave_sub();
 
1553   $main::lxdebug->enter_sub();
 
1558   my $myconfig = \%main::myconfig;
 
1559   my $form     = $main::form;
 
1561   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
 
1563   my $defaults = selectfirst_hashref_query($form, $dbh, qq|SELECT * FROM defaults|) || {};
 
1565   $defaults->{weightunit} ||= 'kg';
 
1567   $main::lxdebug->leave_sub();
 
1572 sub defaultaccounts {
 
1573   $main::lxdebug->enter_sub();
 
1575   my ($self, $myconfig, $form) = @_;
 
1577   # connect to database
 
1578   my $dbh = $form->dbconnect($myconfig);
 
1580   # get defaults from defaults table
 
1581   my $query = qq|SELECT * FROM defaults|;
 
1582   my $sth   = $dbh->prepare($query);
 
1583   $sth->execute || $form->dberror($query);
 
1585   $form->{defaults}               = $sth->fetchrow_hashref("NAME_lc");
 
1586   $form->{defaults}{IC}           = $form->{defaults}{inventory_accno_id};
 
1587   $form->{defaults}{IC_income}    = $form->{defaults}{income_accno_id};
 
1588   $form->{defaults}{IC_expense}   = $form->{defaults}{expense_accno_id};
 
1589   $form->{defaults}{FX_gain}      = $form->{defaults}{fxgain_accno_id};
 
1590   $form->{defaults}{FX_loss}      = $form->{defaults}{fxloss_accno_id};
 
1592   $form->{defaults}{weightunit} ||= 'kg';
 
1596   $query = qq|SELECT c.id, c.accno, c.description, c.link
 
1598               WHERE c.link LIKE '%IC%'
 
1600   $sth = $dbh->prepare($query);
 
1601   $sth->execute || $self->dberror($query);
 
1603   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
1604     foreach my $key (split(/:/, $ref->{link})) {
 
1607         if ($key =~ /cogs/) {
 
1608           $nkey = "IC_expense";
 
1610         if ($key =~ /sale/) {
 
1611           $nkey = "IC_income";
 
1613         %{ $form->{IC}{$nkey}{ $ref->{accno} } } = (
 
1615                                              description => $ref->{description}
 
1622   $query = qq|SELECT c.id, c.accno, c.description
 
1624               WHERE c.category = 'I'
 
1625               AND c.charttype = 'A'
 
1627   $sth = $dbh->prepare($query);
 
1628   $sth->execute || $self->dberror($query);
 
1630   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
1631     %{ $form->{IC}{FX_gain}{ $ref->{accno} } } = (
 
1633                                              description => $ref->{description}
 
1638   $query = qq|SELECT c.id, c.accno, c.description
 
1640               WHERE c.category = 'E'
 
1641               AND c.charttype = 'A'
 
1643   $sth = $dbh->prepare($query);
 
1644   $sth->execute || $self->dberror($query);
 
1646   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
1647     %{ $form->{IC}{FX_loss}{ $ref->{accno} } } = (
 
1649                                              description => $ref->{description}
 
1654   # now get the tax rates and numbers
 
1655   $query = qq|SELECT c.id, c.accno, c.description,
 
1656               t.rate * 100 AS rate, t.taxnumber
 
1658               WHERE c.id = t.chart_id|;
 
1660   $sth = $dbh->prepare($query);
 
1661   $sth->execute || $form->dberror($query);
 
1663   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
1664     $form->{taxrates}{ $ref->{accno} }{id}          = $ref->{id};
 
1665     $form->{taxrates}{ $ref->{accno} }{description} = $ref->{description};
 
1666     $form->{taxrates}{ $ref->{accno} }{taxnumber}   = $ref->{taxnumber}
 
1667       if $ref->{taxnumber};
 
1668     $form->{taxrates}{ $ref->{accno} }{rate} = $ref->{rate} if $ref->{rate};
 
1674   $main::lxdebug->leave_sub();
 
1678   $main::lxdebug->enter_sub();
 
1680   my ($self, $myconfig, $form) = @_;
 
1682   my $dbh = $form->dbconnect($myconfig);
 
1684   my $query = qq|SELECT closedto, revtrans FROM defaults|;
 
1685   my $sth   = $dbh->prepare($query);
 
1686   $sth->execute || $form->dberror($query);
 
1688   ($form->{closedto}, $form->{revtrans}) = $sth->fetchrow_array;
 
1694   $main::lxdebug->leave_sub();
 
1698   $main::lxdebug->enter_sub();
 
1700   my ($self, $myconfig, $form) = @_;
 
1702   my $dbh = $form->dbconnect($myconfig);
 
1704   my ($query, @values);
 
1706   if ($form->{revtrans}) {
 
1707     $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '1'|;
 
1709   } elsif ($form->{closedto}) {
 
1710     $query = qq|UPDATE defaults SET closedto = ?, revtrans = '0'|;
 
1711     @values = (conv_date($form->{closedto}));
 
1714     $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '0'|;
 
1717   # set close in defaults
 
1718   do_query($form, $dbh, $query, @values);
 
1722   $main::lxdebug->leave_sub();
 
1726   my ($self, $units, $unit_name, $factor) = @_;
 
1728   $factor = 1 unless ($factor);
 
1730   my $unit = $units->{$unit_name};
 
1732   if (!defined($unit) || !$unit->{"base_unit"} ||
 
1733       ($unit_name eq $unit->{"base_unit"})) {
 
1734     return ($unit_name, $factor);
 
1737   return AM->get_base_unit($units, $unit->{"base_unit"}, $factor * $unit->{"factor"});
 
1740 sub retrieve_units {
 
1741   $main::lxdebug->enter_sub();
 
1743   my ($self, $myconfig, $form, $prefix) = @_;
 
1745   my $dbh = $form->dbconnect($myconfig);
 
1747   my $query = "SELECT *, base_unit AS original_base_unit FROM units";
 
1749   my $sth = prepare_execute_query($form, $dbh, $query);
 
1752   while (my $ref = $sth->fetchrow_hashref()) {
 
1753     $units->{$ref->{"name"}} = $ref;
 
1757   my $query_lang = "SELECT id, template_code FROM language ORDER BY description";
 
1758   $sth = $dbh->prepare($query_lang);
 
1759   $sth->execute() || $form->dberror($query_lang);
 
1761   while (my $ref = $sth->fetchrow_hashref()) {
 
1762     push(@languages, $ref);
 
1766   $query_lang = "SELECT ul.localized, ul.localized_plural, l.id, l.template_code " .
 
1767     "FROM units_language ul " .
 
1768     "LEFT JOIN language l ON ul.language_id = l.id " .
 
1769     "WHERE ul.unit = ?";
 
1770   $sth = $dbh->prepare($query_lang);
 
1772   foreach my $unit (values(%{$units})) {
 
1773     ($unit->{"${prefix}base_unit"}, $unit->{"${prefix}factor"}) = AM->get_base_unit($units, $unit->{"name"});
 
1775     $unit->{"LANGUAGES"} = {};
 
1776     foreach my $lang (@languages) {
 
1777       $unit->{"LANGUAGES"}->{$lang->{"template_code"}} = { "template_code" => $lang->{"template_code"} };
 
1780     $sth->execute($unit->{"name"}) || $form->dberror($query_lang . " (" . $unit->{"name"} . ")");
 
1781     while (my $ref = $sth->fetchrow_hashref()) {
 
1782       map({ $unit->{"LANGUAGES"}->{$ref->{"template_code"}}->{$_} = $ref->{$_} } keys(%{$ref}));
 
1789   $main::lxdebug->leave_sub();
 
1794 sub retrieve_all_units {
 
1795   $main::lxdebug->enter_sub();
 
1799   if (!$main::all_units) {
 
1800     $main::all_units = $self->retrieve_units(\%main::myconfig, $main::form);
 
1803   $main::lxdebug->leave_sub();
 
1805   return $main::all_units;
 
1809 sub translate_units {
 
1810   $main::lxdebug->enter_sub();
 
1812   my ($self, $form, $template_code, $unit, $amount) = @_;
 
1814   my $units = $self->retrieve_units(\%main::myconfig, $form);
 
1816   my $h = $units->{$unit}->{"LANGUAGES"}->{$template_code};
 
1817   my $new_unit = $unit;
 
1819     if (($amount != 1) && $h->{"localized_plural"}) {
 
1820       $new_unit = $h->{"localized_plural"};
 
1821     } elsif ($h->{"localized"}) {
 
1822       $new_unit = $h->{"localized"};
 
1826   $main::lxdebug->leave_sub();
 
1832   $main::lxdebug->enter_sub();
 
1834   my ($self, $myconfig, $form, $units) = @_;
 
1836   my $dbh = $form->dbconnect($myconfig);
 
1838   map({ $_->{"in_use"} = 0; } values(%{$units}));
 
1840   foreach my $unit (values(%{$units})) {
 
1841     my $base_unit = $unit->{"original_base_unit"};
 
1842     while ($base_unit) {
 
1843       $units->{$base_unit}->{"in_use"} = 1;
 
1844       $units->{$base_unit}->{"DEPENDING_UNITS"} = [] unless ($units->{$base_unit}->{"DEPENDING_UNITS"});
 
1845       push(@{$units->{$base_unit}->{"DEPENDING_UNITS"}}, $unit->{"name"});
 
1846       $base_unit = $units->{$base_unit}->{"original_base_unit"};
 
1850   foreach my $unit (values(%{$units})) {
 
1851     map({ $_ = $dbh->quote($_); } @{$unit->{"DEPENDING_UNITS"}});
 
1853     foreach my $table (qw(parts invoice orderitems)) {
 
1854       my $query = "SELECT COUNT(*) FROM $table WHERE unit ";
 
1856       if (0 == scalar(@{$unit->{"DEPENDING_UNITS"}})) {
 
1857         $query .= "= " . $dbh->quote($unit->{"name"});
 
1859         $query .= "IN (" . $dbh->quote($unit->{"name"}) . "," .
 
1860           join(",", map({ $dbh->quote($_) } @{$unit->{"DEPENDING_UNITS"}})) . ")";
 
1863       my ($count) = $dbh->selectrow_array($query);
 
1864       $form->dberror($query) if ($dbh->err);
 
1867         $unit->{"in_use"} = 1;
 
1875   $main::lxdebug->leave_sub();
 
1878 sub convertible_units {
 
1879   $main::lxdebug->enter_sub();
 
1883   my $filter_unit = shift;
 
1884   my $not_smaller = shift;
 
1886   my $conv_units = [];
 
1888   $filter_unit = $units->{$filter_unit};
 
1890   foreach my $name (sort { lc $a cmp lc $b } keys %{ $units }) {
 
1891     my $unit = $units->{$name};
 
1893     if (($unit->{base_unit} eq $filter_unit->{base_unit}) &&
 
1894         (!$not_smaller || ($unit->{factor} >= $filter_unit->{factor}))) {
 
1895       push @{$conv_units}, $unit;
 
1899   my @sorted = sort { $b->{factor} <=> $a->{factor} } @{ $conv_units };
 
1901   $main::lxdebug->leave_sub();
 
1906 # if $a is translatable to $b, return the factor between them.
 
1909   $main::lxdebug->enter_sub(2);
 
1910   my ($this, $a, $b, $all_units) = @_;
 
1912   $main::lxdebug->leave_sub(2) and return 0 unless $a && $b;
 
1913   $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a} && $all_units->{$b};
 
1914   $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a}{base_unit} eq $all_units->{$b}{base_unit};
 
1915   $main::lxdebug->leave_sub(2) and return $all_units->{$a}{factor} / $all_units->{$b}{factor};
 
1918 sub unit_select_data {
 
1919   $main::lxdebug->enter_sub();
 
1921   my ($self, $units, $selected, $empty_entry, $convertible_into) = @_;
 
1926     push(@{$select}, { "name" => "", "base_unit" => "", "factor" => "", "selected" => "" });
 
1929   foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
 
1930     if (!$convertible_into ||
 
1931         ($units->{$convertible_into} &&
 
1932          ($units->{$convertible_into}->{base_unit} eq $units->{$unit}->{base_unit}))) {
 
1933       push @{$select}, { "name"      => $unit,
 
1934                          "base_unit" => $units->{$unit}->{"base_unit"},
 
1935                          "factor"    => $units->{$unit}->{"factor"},
 
1936                          "selected"  => ($unit eq $selected) ? "selected" : "" };
 
1940   $main::lxdebug->leave_sub();
 
1945 sub unit_select_html {
 
1946   $main::lxdebug->enter_sub();
 
1948   my ($self, $units, $name, $selected, $convertible_into) = @_;
 
1950   my $select = "<select name=${name}>";
 
1952   foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
 
1953     if (!$convertible_into ||
 
1954         ($units->{$convertible_into} &&
 
1955          ($units->{$convertible_into}->{"base_unit"} eq $units->{$unit}->{"base_unit"}))) {
 
1956       $select .= "<option" . (($unit eq $selected) ? " selected" : "") . ">${unit}</option>";
 
1959   $select .= "</select>";
 
1961   $main::lxdebug->leave_sub();
 
1967   $main::lxdebug->enter_sub();
 
1971   my $units = $self->retrieve_all_units();
 
1976   while (2 <= scalar(@_)) {
 
1977     my $qty  = shift(@_);
 
1978     my $unit = $units->{shift(@_)};
 
1980     croak "No unit defined with name $unit" if (!defined $unit);
 
1983       $base_unit = $unit->{base_unit};
 
1984     } elsif ($base_unit ne $unit->{base_unit}) {
 
1985       croak "Adding values with incompatible base units $base_unit/$unit->{base_unit}";
 
1988     $sum += $qty * $unit->{factor};
 
1991   $main::lxdebug->leave_sub();
 
1993   return wantarray ? ($sum, $base_unit) : $sum;
 
1997   $main::lxdebug->enter_sub();
 
1999   my ($self, $myconfig, $form, $name, $base_unit, $factor, $languages) = @_;
 
2001   my $dbh = $form->dbconnect_noauto($myconfig);
 
2003   my $query = qq|SELECT COALESCE(MAX(sortkey), 0) + 1 FROM units|;
 
2004   my ($sortkey) = selectrow_query($form, $dbh, $query);
 
2006   $query = "INSERT INTO units (name, base_unit, factor, sortkey) " .
 
2007     "VALUES (?, ?, ?, ?)";
 
2008   do_query($form, $dbh, $query, $name, $base_unit, $factor, $sortkey);
 
2011     $query = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
 
2012     my $sth = $dbh->prepare($query);
 
2013     foreach my $lang (@{$languages}) {
 
2014       my @values = ($name, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
 
2015       $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
 
2023   $main::lxdebug->leave_sub();
 
2027   $main::lxdebug->enter_sub();
 
2029   my ($self, $myconfig, $form, $units, $delete_units) = @_;
 
2031   my $dbh = $form->dbconnect_noauto($myconfig);
 
2033   my ($base_unit, $unit, $sth, $query);
 
2035   $query = "DELETE FROM units_language";
 
2036   $dbh->do($query) || $form->dberror($query);
 
2038   if ($delete_units && (0 != scalar(@{$delete_units}))) {
 
2039     $query = "DELETE FROM units WHERE name IN (";
 
2040     map({ $query .= "?," } @{$delete_units});
 
2041     substr($query, -1, 1) = ")";
 
2042     $dbh->do($query, undef, @{$delete_units}) ||
 
2043       $form->dberror($query . " (" . join(", ", @{$delete_units}) . ")");
 
2046   $query = "UPDATE units SET name = ?, base_unit = ?, factor = ? WHERE name = ?";
 
2047   $sth = $dbh->prepare($query);
 
2049   my $query_lang = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
 
2050   my $sth_lang = $dbh->prepare($query_lang);
 
2052   foreach $unit (values(%{$units})) {
 
2053     $unit->{"depth"} = 0;
 
2054     my $base_unit = $unit;
 
2055     while ($base_unit->{"base_unit"}) {
 
2057       $base_unit = $units->{$base_unit->{"base_unit"}};
 
2061   foreach $unit (sort({ $a->{"depth"} <=> $b->{"depth"} } values(%{$units}))) {
 
2062     if ($unit->{"LANGUAGES"}) {
 
2063       foreach my $lang (@{$unit->{"LANGUAGES"}}) {
 
2064         next unless ($lang->{"id"} && $lang->{"localized"});
 
2065         my @values = ($unit->{"name"}, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
 
2066         $sth_lang->execute(@values) || $form->dberror($query_lang . " (" . join(", ", @values) . ")");
 
2070     next if ($unit->{"unchanged_unit"});
 
2072     my @values = ($unit->{"name"}, $unit->{"base_unit"}, $unit->{"factor"}, $unit->{"old_name"});
 
2073     $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
 
2077   $sth_lang->finish();
 
2081   $main::lxdebug->leave_sub();
 
2085   $main::lxdebug->enter_sub();
 
2087   my ($self, $myconfig, $form, $dir, $name_1) = @_;
 
2089   my $dbh = $form->dbconnect_noauto($myconfig);
 
2093   $query = qq|SELECT sortkey FROM units WHERE name = ?|;
 
2094   my ($sortkey_1) = selectrow_query($form, $dbh, $query, $name_1);
 
2097     qq|SELECT sortkey FROM units | .
 
2098     qq|WHERE sortkey | . ($dir eq "down" ? ">" : "<") . qq| ? | .
 
2099     qq|ORDER BY sortkey | . ($dir eq "down" ? "ASC" : "DESC") . qq| LIMIT 1|;
 
2100   my ($sortkey_2) = selectrow_query($form, $dbh, $query, $sortkey_1);
 
2102   if (defined($sortkey_1)) {
 
2103     $query = qq|SELECT name FROM units WHERE sortkey = ${sortkey_2}|;
 
2104     my ($name_2) = selectrow_query($form, $dbh, $query);
 
2106     if (defined($name_2)) {
 
2107       $query = qq|UPDATE units SET sortkey = ? WHERE name = ?|;
 
2108       my $sth = $dbh->prepare($query);
 
2110       do_statement($form, $sth, $query, $sortkey_1, $name_2);
 
2111       do_statement($form, $sth, $query, $sortkey_2, $name_1);
 
2118   $main::lxdebug->leave_sub();
 
2122   $main::lxdebug->enter_sub();
 
2124   my ($self, $myconfig, $form) = @_;
 
2126   # connect to database
 
2127   my $dbh = $form->dbconnect($myconfig);
 
2129   my $query = qq|SELECT
 
2133                    round(t.rate * 100, 2) AS rate,
 
2134                    (SELECT accno FROM chart WHERE id = chart_id) AS taxnumber,
 
2135                    (SELECT description FROM chart WHERE id = chart_id) AS account_description
 
2139   my $sth = $dbh->prepare($query);
 
2140   $sth->execute || $form->dberror($query);
 
2143   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
2144     push @{ $form->{TAX} }, $ref;
 
2150   $main::lxdebug->leave_sub();
 
2153 sub get_tax_accounts {
 
2154   $main::lxdebug->enter_sub();
 
2156   my ($self, $myconfig, $form) = @_;
 
2158   my $dbh = $form->dbconnect($myconfig);
 
2160   # get Accounts from chart
 
2161   my $query = qq{ SELECT
 
2163                  accno || ' - ' || description AS taxaccount
 
2165                WHERE link LIKE '%_tax%'
 
2169   my $sth = $dbh->prepare($query);
 
2170   $sth->execute || $form->dberror($query);
 
2172   $form->{ACCOUNTS} = [];
 
2173   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
2174     push @{ $form->{ACCOUNTS} }, $ref;
 
2181   $main::lxdebug->leave_sub();
 
2185   $main::lxdebug->enter_sub();
 
2187   my ($self, $myconfig, $form) = @_;
 
2189   # connect to database
 
2190   my $dbh = $form->dbconnect($myconfig);
 
2192   my $query = qq|SELECT
 
2195                    round(rate * 100, 2) AS rate,
 
2200   my $sth = $dbh->prepare($query);
 
2201   $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
2203   my $ref = $sth->fetchrow_hashref("NAME_lc");
 
2205   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
2209   # see if it is used by a taxkey
 
2210   $query = qq|SELECT count(*) FROM taxkeys
 
2211               WHERE tax_id = ? AND chart_id >0|;
 
2213   ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
 
2215   $form->{orphaned} = !$form->{orphaned};
 
2218   if (!$form->{orphaned} ) {
 
2219     $query = qq|SELECT DISTINCT c.id, c.accno
 
2221                 JOIN   tax t ON (t.id = tk.tax_id)
 
2222                 JOIN chart c ON (c.id = tk.chart_id)
 
2223                 WHERE tk.tax_id = ?|;
 
2225     $sth = $dbh->prepare($query);
 
2226     $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
2228     $form->{TAXINUSE} = [];
 
2229     while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
2230       push @{ $form->{TAXINUSE} }, $ref;
 
2238   $main::lxdebug->leave_sub();
 
2242   $main::lxdebug->enter_sub();
 
2244   my ($self, $myconfig, $form) = @_;
 
2247   # connect to database
 
2248   my $dbh = $form->get_standard_dbh($myconfig);
 
2250   $form->{rate} = $form->{rate} / 100;
 
2252   my @values = ($form->{taxkey}, $form->{taxdescription}, $form->{rate}, $form->{chart_id}, $form->{chart_id} );
 
2253   if ($form->{id} ne "") {
 
2254     $query = qq|UPDATE tax SET
 
2259                   taxnumber      = (SELECT accno FROM chart WHERE id= ? )
 
2261     push(@values, $form->{id});
 
2265     $query = qq|INSERT INTO tax (
 
2272                 VALUES (?, ?, ?, ?, (SELECT accno FROM chart WHERE id = ?) )|;
 
2274   do_query($form, $dbh, $query, @values);
 
2278   $main::lxdebug->leave_sub();
 
2282   $main::lxdebug->enter_sub();
 
2284   my ($self, $myconfig, $form) = @_;
 
2287   # connect to database
 
2288   my $dbh = $form->get_standard_dbh($myconfig);
 
2290   $query = qq|DELETE FROM tax
 
2292   do_query($form, $dbh, $query, $form->{id});
 
2296   $main::lxdebug->leave_sub();
 
2299 sub save_price_factor {
 
2300   $main::lxdebug->enter_sub();
 
2302   my ($self, $myconfig, $form) = @_;
 
2304   # connect to database
 
2305   my $dbh = $form->get_standard_dbh($myconfig);
 
2308   my @values = ($form->{description}, conv_i($form->{factor}));
 
2311     $query = qq|UPDATE price_factors SET description = ?, factor = ? WHERE id = ?|;
 
2312     push @values, conv_i($form->{id});
 
2315     $query = qq|INSERT INTO price_factors (description, factor, sortkey) VALUES (?, ?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM price_factors))|;
 
2318   do_query($form, $dbh, $query, @values);
 
2322   $main::lxdebug->leave_sub();
 
2325 sub get_all_price_factors {
 
2326   $main::lxdebug->enter_sub();
 
2328   my ($self, $myconfig, $form) = @_;
 
2330   # connect to database
 
2331   my $dbh = $form->get_standard_dbh($myconfig);
 
2333   $form->{PRICE_FACTORS} = selectall_hashref_query($form, $dbh, qq|SELECT * FROM price_factors ORDER BY sortkey|);
 
2335   $main::lxdebug->leave_sub();
 
2338 sub get_price_factor {
 
2339   $main::lxdebug->enter_sub();
 
2341   my ($self, $myconfig, $form) = @_;
 
2343   # connect to database
 
2344   my $dbh = $form->get_standard_dbh($myconfig);
 
2346   my $query = qq|SELECT description, factor,
 
2347                    ((SELECT COUNT(*) FROM parts      WHERE price_factor_id = ?) +
 
2348                     (SELECT COUNT(*) FROM invoice    WHERE price_factor_id = ?) +
 
2349                     (SELECT COUNT(*) FROM orderitems WHERE price_factor_id = ?)) = 0 AS orphaned
 
2350                  FROM price_factors WHERE id = ?|;
 
2352   ($form->{description}, $form->{factor}, $form->{orphaned}) = selectrow_query($form, $dbh, $query, (conv_i($form->{id})) x 4);
 
2354   $main::lxdebug->leave_sub();
 
2357 sub delete_price_factor {
 
2358   $main::lxdebug->enter_sub();
 
2360   my ($self, $myconfig, $form) = @_;
 
2362   # connect to database
 
2363   my $dbh = $form->get_standard_dbh($myconfig);
 
2365   do_query($form, $dbh, qq|DELETE FROM price_factors WHERE id = ?|, conv_i($form->{id}));
 
2368   $main::lxdebug->leave_sub();
 
2371 sub save_warehouse {
 
2372   $main::lxdebug->enter_sub();
 
2374   my ($self, $myconfig, $form) = @_;
 
2376   # connect to database
 
2377   my $dbh = $form->get_standard_dbh($myconfig);
 
2379   my ($query, @values, $sth);
 
2382     $query        = qq|SELECT nextval('id')|;
 
2383     ($form->{id}) = selectrow_query($form, $dbh, $query);
 
2385     $query        = qq|INSERT INTO warehouse (id, sortkey) VALUES (?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM warehouse))|;
 
2386     do_query($form, $dbh, $query, $form->{id});
 
2389   do_query($form, $dbh, qq|UPDATE warehouse SET description = ?, invalid = ? WHERE id = ?|,
 
2390            $form->{description}, $form->{invalid} ? 't' : 'f', conv_i($form->{id}));
 
2392   if (0 < $form->{number_of_new_bins}) {
 
2393     $query = qq|INSERT INTO bin (warehouse_id, description) VALUES (?, ?)|;
 
2394     $sth   = prepare_query($form, $dbh, $query);
 
2396     foreach my $i (1..$form->{number_of_new_bins}) {
 
2397       do_statement($form, $sth, $query, conv_i($form->{id}), "$form->{prefix}${i}");
 
2405   $main::lxdebug->leave_sub();
 
2409   $main::lxdebug->enter_sub();
 
2411   my ($self, $myconfig, $form) = @_;
 
2413   # connect to database
 
2414   my $dbh = $form->get_standard_dbh($myconfig);
 
2416   my ($query, @values, $commit_necessary, $sth);
 
2418   @values = map { $form->{"id_${_}"} } grep { $form->{"delete_${_}"} } (1..$form->{rowcount});
 
2421     $query = qq|DELETE FROM bin WHERE id IN (| . join(', ', ('?') x scalar(@values)) . qq|)|;
 
2422     do_query($form, $dbh, $query, @values);
 
2424     $commit_necessary = 1;
 
2427   $query = qq|UPDATE bin SET description = ? WHERE id = ?|;
 
2428   $sth   = prepare_query($form, $dbh, $query);
 
2430   foreach my $row (1..$form->{rowcount}) {
 
2431     next if ($form->{"delete_${row}"});
 
2433     do_statement($form, $sth, $query, $form->{"description_${row}"}, conv_i($form->{"id_${row}"}));
 
2435     $commit_necessary = 1;
 
2440   $dbh->commit() if ($commit_necessary);
 
2442   $main::lxdebug->leave_sub();
 
2445 sub delete_warehouse {
 
2446   $main::lxdebug->enter_sub();
 
2448   my ($self, $myconfig, $form) = @_;
 
2450   # connect to database
 
2451   my $dbh = $form->get_standard_dbh($myconfig);
 
2453   my $id      = conv_i($form->{id});
 
2454   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|;
 
2455   my ($count) = selectrow_query($form, $dbh, $query, $id);
 
2458     $main::lxdebug->leave_sub();
 
2462   do_query($form, $dbh, qq|DELETE FROM bin       WHERE warehouse_id = ?|, conv_i($form->{id}));
 
2463   do_query($form, $dbh, qq|DELETE FROM warehouse WHERE id           = ?|, conv_i($form->{id}));
 
2467   $main::lxdebug->leave_sub();
 
2472 sub get_all_warehouses {
 
2473   $main::lxdebug->enter_sub();
 
2475   my ($self, $myconfig, $form) = @_;
 
2477   # connect to database
 
2478   my $dbh = $form->get_standard_dbh($myconfig);
 
2480   my $query = qq|SELECT w.id, w.description, w.invalid,
 
2481                    (SELECT COUNT(b.description) FROM bin b WHERE b.warehouse_id = w.id) AS number_of_bins
 
2483                  ORDER BY w.sortkey|;
 
2485   $form->{WAREHOUSES} = selectall_hashref_query($form, $dbh, $query);
 
2487   $main::lxdebug->leave_sub();
 
2491   $main::lxdebug->enter_sub();
 
2493   my ($self, $myconfig, $form) = @_;
 
2495   # connect to database
 
2496   my $dbh = $form->get_standard_dbh($myconfig);
 
2498   my $id    = conv_i($form->{id});
 
2499   my $query = qq|SELECT w.description, w.invalid
 
2503   my $ref   = selectfirst_hashref_query($form, $dbh, $query, $id);
 
2505   map { $form->{$_} = $ref->{$_} } keys %{ $ref };
 
2507   $query = qq|SELECT b.*, EXISTS
 
2508                 (SELECT i.warehouse_id
 
2510                  WHERE i.bin_id = b.id
 
2514               WHERE b.warehouse_id = ?|;
 
2516   $form->{BINS} = selectall_hashref_query($form, $dbh, $query, conv_i($form->{id}));
 
2518   $main::lxdebug->leave_sub();