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 #======================================================================
 
  45   $main::lxdebug->enter_sub();
 
  47   my ($self, $myconfig, $form) = @_;
 
  50   my $dbh = $form->dbconnect($myconfig);
 
  52     SELECT c.accno, c.description, c.charttype, c.category,
 
  53       c.link, c.pos_bilanz, c.pos_eur, c.new_chart_id, c.valid_from,
 
  54       c.pos_bwa, datevautomatik,
 
  55       tk.taxkey_id, tk.pos_ustva, tk.tax_id,
 
  56       tk.tax_id || '--' || tk.taxkey_id AS tax, tk.startdate
 
  59     ON (c.id=tk.chart_id AND tk.id =
 
  60       (SELECT id FROM taxkeys
 
  61        WHERE taxkeys.chart_id = c.id AND startdate <= current_date
 
  62        ORDER BY startdate DESC LIMIT 1))
 
  67   $main::lxdebug->message(LXDebug::QUERY, "\$query=\n $query");
 
  68   my $sth = $dbh->prepare($query);
 
  69   $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
  71   my $ref = $sth->fetchrow_hashref(NAME_lc);
 
  73   foreach my $key (keys %$ref) {
 
  74     $form->{"$key"} = $ref->{"$key"};
 
  79   # get default accounts
 
  80   $query = qq|SELECT inventory_accno_id, income_accno_id, expense_accno_id
 
  82   $main::lxdebug->message(LXDebug::QUERY, "\$query=\n $query");
 
  83   $sth = $dbh->prepare($query);
 
  84   $sth->execute || $form->dberror($query);
 
  86   $ref = $sth->fetchrow_hashref(NAME_lc);
 
  88   map { $form->{$_} = $ref->{$_} } keys %ref;
 
  94   # get taxkeys and description
 
  98       (SELECT accno FROM chart WHERE id=tax.chart_id) AS chart_accno,
 
 100       id||'--'||taxkey AS tax,
 
 103     FROM tax ORDER BY taxkey
 
 105   $main::lxdebug->message(LXDebug::QUERY, "\$query=\n $query");
 
 106   $sth = $dbh->prepare($query);
 
 107   $sth->execute || $form->dberror($query);
 
 109   $form->{TAXKEY} = [];
 
 111   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
 112     push @{ $form->{TAXKEY} }, $ref;
 
 118     $query = qq|SELECT id, accno,description
 
 122     $main::lxdebug->message(LXDebug::QUERY, "\$query=\n $query");
 
 123     $sth = $dbh->prepare($query);
 
 124     $sth->execute($form->{link}) || $form->dberror($query . " ($form->{link})");
 
 126     $form->{NEWACCOUNT} = [];
 
 127     while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
 128       push @{ $form->{NEWACCOUNT} }, $ref;
 
 133     # get the taxkeys of account
 
 147       LEFT JOIN   tax t ON (t.id = tk.tax_id)
 
 148       LEFT JOIN chart c ON (c.id = t.chart_id)
 
 150       WHERE tk.chart_id = ?
 
 151       ORDER BY startdate DESC
 
 153     $main::lxdebug->message(LXDebug::QUERY, "\$query=\n $query");
 
 154     $sth = $dbh->prepare($query);
 
 156     $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
 158     $form->{ACCOUNT_TAXKEYS} = [];
 
 160     while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
 161       push @{ $form->{ACCOUNT_TAXKEYS} }, $ref;
 
 167   # check if we have any transactions
 
 168   $query = qq|SELECT a.trans_id FROM acc_trans a
 
 169               WHERE a.chart_id = ?|;
 
 170   $main::lxdebug->message(LXDebug::QUERY, "\$query=\n $query");
 
 171   $sth = $dbh->prepare($query);
 
 172   $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
 174   ($form->{orphaned}) = $sth->fetchrow_array;
 
 175   $form->{orphaned} = !$form->{orphaned};
 
 178   # check if new account is active
 
 179   $form->{new_chart_valid} = 0;
 
 180   if ($form->{new_chart_id}) {
 
 181     $query = qq|SELECT current_date-valid_from FROM chart
 
 183     $main::lxdebug->message(LXDebug::QUERY, "\$query=\n $query");
 
 184     my ($count) = selectrow_query($form, $dbh, $query, $form->{id});
 
 186       $form->{new_chart_valid} = 1;
 
 193   $main::lxdebug->leave_sub();
 
 197   $main::lxdebug->enter_sub();
 
 199   my ($self, $myconfig, $form) = @_;
 
 201   # connect to database, turn off AutoCommit
 
 202   my $dbh = $form->dbconnect_noauto($myconfig);
 
 204   # sanity check, can't have AR with AR_...
 
 205   if ($form->{AR} || $form->{AP} || $form->{IC}) {
 
 206     map { delete $form->{$_} }
 
 207       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);
 
 211   foreach my $item ($form->{AR},            $form->{AR_amount},
 
 212                     $form->{AR_tax},        $form->{AR_paid},
 
 213                     $form->{AP},            $form->{AP_amount},
 
 214                     $form->{AP_tax},        $form->{AP_paid},
 
 215                     $form->{IC},            $form->{IC_sale},
 
 216                     $form->{IC_cogs},       $form->{IC_taxpart},
 
 217                     $form->{IC_income},     $form->{IC_expense},
 
 218                     $form->{IC_taxservice}, $form->{CT_tax}
 
 220     $form->{link} .= "${item}:" if ($item);
 
 224   # strip blanks from accno
 
 225   map { $form->{$_} =~ s/ //g; } qw(accno);
 
 229   if ($form->{id} eq "NULL") {
 
 233   if (!$form->{id} || $form->{id} eq "") {
 
 234     $query = qq|SELECT nextval('id')|;
 
 235     ($form->{"id"}) = selectrow_query($form, $dbh, $query);
 
 236     $query = qq|INSERT INTO chart (id, accno) VALUES (?, ?)|;
 
 237     do_query($form, $dbh, $query, $form->{"id"}, $form->{"accno"});
 
 243     $query = qq|UPDATE chart SET
 
 259                   $form->{description},
 
 263                   conv_i($form->{pos_bwa}),
 
 264                   conv_i($form->{pos_bilanz}),
 
 265                   conv_i($form->{pos_eur}),
 
 266                   conv_i($form->{new_chart_id}),
 
 267                   conv_date($form->{valid_from}),
 
 268                   ($form->{datevautomatik} eq 'T') ? 'true':'false',
 
 274   do_query($form, $dbh, $query, @values);
 
 280   my $MAX_TRIES = 10; # Maximum count of taxkeys in form
 
 284   for $tk_count (0 .. $MAX_TRIES) {
 
 288     # Check if the account already exists, else cancel
 
 290     print(STDERR "Keine Taxkeys weil ID =: $form->{id}\n");
 
 292     last READTAXKEYS if ( $form->{'id'} == 0);
 
 294     # check if there is a startdate
 
 295     if ( $form->{"taxkey_startdate_$tk_count"} eq '' ) {
 
 300     # Add valid taxkeys into the array
 
 303         id        => ($form->{"taxkey_id_$tk_count"} eq 'NEW') ? conv_i('') : conv_i($form->{"taxkey_id_$tk_count"}),
 
 304         tax_id    => conv_i($form->{"taxkey_tax_$tk_count"}),
 
 305         startdate => conv_date($form->{"taxkey_startdate_$tk_count"}),
 
 306         chart_id  => conv_i($form->{"id"}),
 
 307         pos_ustva => conv_i($form->{"taxkey_pos_ustva_$tk_count"}),
 
 308         delete    => ( $form->{"taxkey_del_$tk_count"} eq 'delete' ) ? '1' : '',
 
 315   for my $j (0 .. $#taxkeys){
 
 316     if ( defined $taxkeys[$j]{'id'} ){
 
 319       if ($taxkeys[$j]{'delete'}){
 
 321           DELETE FROM taxkeys WHERE id = ?
 
 324         @values = ($taxkeys[$j]{'id'});
 
 326         do_query($form, $dbh, $query, @values);
 
 335         SET taxkey_id = (SELECT taxkey FROM tax WHERE tax.id = ?),
 
 343         $taxkeys[$j]{'tax_id'},
 
 344         $taxkeys[$j]{'chart_id'},
 
 345         $taxkeys[$j]{'tax_id'},
 
 346         $taxkeys[$j]{'pos_ustva'},
 
 347         $taxkeys[$j]{'startdate'},
 
 350       do_query($form, $dbh, $query, @values);
 
 356         INSERT INTO taxkeys (
 
 363         VALUES ((SELECT taxkey FROM tax WHERE tax.id = ?), ?, ?, ?, ?)
 
 366         $taxkeys[$j]{'tax_id'},
 
 367         $taxkeys[$j]{'chart_id'},
 
 368         $taxkeys[$j]{'tax_id'},
 
 369         $taxkeys[$j]{'pos_ustva'},
 
 370         $taxkeys[$j]{'startdate'},
 
 373       do_query($form, $dbh, $query, @values);
 
 379   my $rc = $dbh->commit;
 
 382   $main::lxdebug->leave_sub();
 
 388   $main::lxdebug->enter_sub();
 
 390   my ($self, $myconfig, $form) = @_;
 
 392   # connect to database, turn off AutoCommit
 
 393   my $dbh = $form->dbconnect_noauto($myconfig);
 
 395   my $query = qq|SELECT count(*) FROM acc_trans a
 
 396                  WHERE a.chart_id = ?|;
 
 397   my ($count) = selectrow_query($form, $dbh, $query, $form->{id});
 
 401     $main::lxdebug->leave_sub();
 
 405   # set inventory_accno_id, income_accno_id, expense_accno_id to defaults
 
 406   foreach my $type (qw(inventory income expense)) {
 
 409       qq|SET ${type}_accno_id = (SELECT ${type}_accno_id FROM defaults) | .
 
 410       qq|WHERE ${type}_accno_id = ?|;
 
 411     do_query($form, $dbh, $query, $form->{id});
 
 414   foreach my $table (qw(partstax customertax vendortax tax)) {
 
 415     $query = qq|DELETE FROM $table
 
 417     do_query($form, $dbh, $query, $form->{id});
 
 420   # delete chart of account record
 
 421   $query = qq|DELETE FROM chart
 
 423   do_query($form, $dbh, $query, $form->{id});
 
 425   # delete account taxkeys
 
 426   $query = qq|DELETE FROM taxkeys
 
 428   do_query($form, $dbh, $query, $form->{id});
 
 430   # commit and redirect
 
 431   my $rc = $dbh->commit;
 
 434   $main::lxdebug->leave_sub();
 
 440   $main::lxdebug->enter_sub();
 
 442   my ($self, $myconfig, $form) = @_;
 
 444   # connect to database
 
 445   my $dbh = $form->dbconnect($myconfig);
 
 447   my $query = qq|SELECT d.id, d.description, d.role
 
 451   $sth = $dbh->prepare($query);
 
 452   $sth->execute || $form->dberror($query);
 
 455   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
 456     push @{ $form->{ALL} }, $ref;
 
 462   $main::lxdebug->leave_sub();
 
 466   $main::lxdebug->enter_sub();
 
 468   my ($self, $myconfig, $form) = @_;
 
 470   # connect to database
 
 471   my $dbh = $form->dbconnect($myconfig);
 
 473   my $query = qq|SELECT d.description, d.role
 
 476   my $sth = $dbh->prepare($query);
 
 477   $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
 479   my $ref = $sth->fetchrow_hashref(NAME_lc);
 
 481   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
 485   # see if it is in use
 
 486   $query = qq|SELECT count(*) FROM dpt_trans d
 
 487               WHERE d.department_id = ?|;
 
 488   ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
 
 490   $form->{orphaned} = !$form->{orphaned};
 
 495   $main::lxdebug->leave_sub();
 
 498 sub save_department {
 
 499   $main::lxdebug->enter_sub();
 
 501   my ($self, $myconfig, $form) = @_;
 
 503   # connect to database
 
 504   my $dbh = $form->dbconnect($myconfig);
 
 506   my @values = ($form->{description}, $form->{role});
 
 508     $query = qq|UPDATE department SET
 
 509                 description = ?, role = ?
 
 511     push(@values, $form->{id});
 
 513     $query = qq|INSERT INTO department
 
 517   do_query($form, $dbh, $query, @values);
 
 521   $main::lxdebug->leave_sub();
 
 524 sub delete_department {
 
 525   $main::lxdebug->enter_sub();
 
 527   my ($self, $myconfig, $form) = @_;
 
 529   # connect to database
 
 530   my $dbh = $form->dbconnect($myconfig);
 
 532   $query = qq|DELETE FROM department
 
 534   do_query($form, $dbh, $query, $form->{id});
 
 538   $main::lxdebug->leave_sub();
 
 542   $main::lxdebug->enter_sub();
 
 544   my ($self, $myconfig, $form) = @_;
 
 546   # connect to database
 
 547   my $dbh = $form->dbconnect($myconfig);
 
 549   my $query = qq|SELECT id, lead
 
 553   $sth = $dbh->prepare($query);
 
 554   $sth->execute || $form->dberror($query);
 
 556   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
 557     push @{ $form->{ALL} }, $ref;
 
 563   $main::lxdebug->leave_sub();
 
 567   $main::lxdebug->enter_sub();
 
 569   my ($self, $myconfig, $form) = @_;
 
 571   # connect to database
 
 572   my $dbh = $form->dbconnect($myconfig);
 
 575     qq|SELECT l.id, l.lead | .
 
 578   my $sth = $dbh->prepare($query);
 
 579   $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
 581   my $ref = $sth->fetchrow_hashref(NAME_lc);
 
 583   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
 589   $main::lxdebug->leave_sub();
 
 593   $main::lxdebug->enter_sub();
 
 595   my ($self, $myconfig, $form) = @_;
 
 597   # connect to database
 
 598   my $dbh = $form->dbconnect($myconfig);
 
 600   my @values = ($form->{description});
 
 601   # id is the old record
 
 603     $query = qq|UPDATE leads SET
 
 606     puhs(@values, $form->{id});
 
 608     $query = qq|INSERT INTO leads
 
 612   do_query($form, $dbh, $query, @values);
 
 616   $main::lxdebug->leave_sub();
 
 620   $main::lxdebug->enter_sub();
 
 622   my ($self, $myconfig, $form) = @_;
 
 624   # connect to database
 
 625   my $dbh = $form->dbconnect($myconfig);
 
 627   $query = qq|DELETE FROM leads
 
 629   do_query($form, $dbh, $query, $form->{id});
 
 633   $main::lxdebug->leave_sub();
 
 637   $main::lxdebug->enter_sub();
 
 639   my ($self, $myconfig, $form) = @_;
 
 641   # connect to database
 
 642   my $dbh = $form->dbconnect($myconfig);
 
 644   my $query = qq|SELECT id, description, discount, customernumberinit
 
 648   $sth = $dbh->prepare($query);
 
 649   $sth->execute || $form->dberror($query);
 
 651   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
 652     push @{ $form->{ALL} }, $ref;
 
 658   $main::lxdebug->leave_sub();
 
 662   $main::lxdebug->enter_sub();
 
 664   my ($self, $myconfig, $form) = @_;
 
 666   # connect to database
 
 667   my $dbh = $form->dbconnect($myconfig);
 
 670     qq|SELECT b.description, b.discount, b.customernumberinit
 
 673   my $sth = $dbh->prepare($query);
 
 674   $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
 676   my $ref = $sth->fetchrow_hashref(NAME_lc);
 
 678   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
 684   $main::lxdebug->leave_sub();
 
 688   $main::lxdebug->enter_sub();
 
 690   my ($self, $myconfig, $form) = @_;
 
 692   # connect to database
 
 693   my $dbh = $form->dbconnect($myconfig);
 
 695   my @values = ($form->{description}, $form->{discount},
 
 696                 $form->{customernumberinit});
 
 697   # id is the old record
 
 699     $query = qq|UPDATE business SET
 
 702                 customernumberinit = ?
 
 704     push(@values, $form->{id});
 
 706     $query = qq|INSERT INTO business
 
 707                 (description, discount, customernumberinit)
 
 710   do_query($form, $dbh, $query, @values);
 
 714   $main::lxdebug->leave_sub();
 
 717 sub delete_business {
 
 718   $main::lxdebug->enter_sub();
 
 720   my ($self, $myconfig, $form) = @_;
 
 722   # connect to database
 
 723   my $dbh = $form->dbconnect($myconfig);
 
 725   $query = qq|DELETE FROM business
 
 727   do_query($form, $dbh, $query, $form->{id});
 
 731   $main::lxdebug->leave_sub();
 
 736   $main::lxdebug->enter_sub();
 
 738   my ($self, $myconfig, $form, $return_list) = @_;
 
 740   # connect to database
 
 741   my $dbh = $form->dbconnect($myconfig);
 
 744     "SELECT id, description, template_code, article_code, " .
 
 745     "  output_numberformat, output_dateformat, output_longdates " .
 
 746     "FROM language ORDER BY description";
 
 748   $sth = $dbh->prepare($query);
 
 749   $sth->execute || $form->dberror($query);
 
 753   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
 754     push(@{ $ary }, $ref);
 
 760   $main::lxdebug->leave_sub();
 
 770   $main::lxdebug->enter_sub();
 
 772   my ($self, $myconfig, $form) = @_;
 
 774   # connect to database
 
 775   my $dbh = $form->dbconnect($myconfig);
 
 778     "SELECT description, template_code, article_code, " .
 
 779     "  output_numberformat, output_dateformat, output_longdates " .
 
 780     "FROM language WHERE id = ?";
 
 781   my $sth = $dbh->prepare($query);
 
 782   $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
 
 784   my $ref = $sth->fetchrow_hashref(NAME_lc);
 
 786   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
 792   $main::lxdebug->leave_sub();
 
 795 sub get_language_details {
 
 796   $main::lxdebug->enter_sub();
 
 798   my ($self, $myconfig, $form, $id) = @_;
 
 800   # connect to database
 
 801   my $dbh = $form->dbconnect($myconfig);
 
 804     "SELECT template_code, " .
 
 805     "  output_numberformat, output_dateformat, output_longdates " .
 
 806     "FROM language WHERE id = ?";
 
 807   my @res = selectrow_query($form, $dbh, $query, $id);
 
 810   $main::lxdebug->leave_sub();
 
 816   $main::lxdebug->enter_sub();
 
 818   my ($self, $myconfig, $form) = @_;
 
 820   # connect to database
 
 821   my $dbh = $form->dbconnect($myconfig);
 
 822   my (@values, $query);
 
 824   map({ push(@values, $form->{$_}); }
 
 825       qw(description template_code article_code
 
 826          output_numberformat output_dateformat output_longdates));
 
 828   # id is the old record
 
 831       "UPDATE language SET " .
 
 832       "  description = ?, template_code = ?, article_code = ?, " .
 
 833       "  output_numberformat = ?, output_dateformat = ?, " .
 
 834       "  output_longdates = ? " .
 
 836     push(@values, $form->{id});
 
 839       "INSERT INTO language (" .
 
 840       "  description, template_code, article_code, " .
 
 841       "  output_numberformat, output_dateformat, output_longdates" .
 
 842       ") VALUES (?, ?, ?, ?, ?, ?)";
 
 844   do_query($form, $dbh, $query, @values);
 
 848   $main::lxdebug->leave_sub();
 
 851 sub delete_language {
 
 852   $main::lxdebug->enter_sub();
 
 854   my ($self, $myconfig, $form) = @_;
 
 856   # connect to database
 
 857   my $dbh = $form->dbconnect_noauto($myconfig);
 
 859   foreach my $table (qw(translation_payment_terms units_language)) {
 
 860     my $query = qq|DELETE FROM $table WHERE language_id = ?|;
 
 861     do_query($form, $dbh, $query, $form->{"id"});
 
 864   $query = "DELETE FROM language WHERE id = ?";
 
 865   do_query($form, $dbh, $query, $form->{"id"});
 
 870   $main::lxdebug->leave_sub();
 
 875   $main::lxdebug->enter_sub();
 
 877   my ($self, $myconfig, $form) = @_;
 
 879   # connect to database
 
 880   my $dbh = $form->dbconnect($myconfig);
 
 882   my $query = qq|SELECT id, description,
 
 884                  (SELECT accno FROM chart WHERE id = inventory_accno_id) AS inventory_accno,
 
 886                  (SELECT accno FROM chart WHERE id = income_accno_id_0) AS income_accno_0,
 
 888                  (SELECT accno FROM chart WHERE id = expense_accno_id_0) AS expense_accno_0,
 
 890                  (SELECT accno FROM chart WHERE id = income_accno_id_1) AS income_accno_1,
 
 892                  (SELECT accno FROM chart WHERE id = expense_accno_id_1) AS expense_accno_1,
 
 894                  (SELECT accno FROM chart WHERE id = income_accno_id_2) AS income_accno_2,
 
 896                  (select accno FROM chart WHERE id = expense_accno_id_2) AS expense_accno_2,
 
 898                  (SELECT accno FROM chart WHERE id = income_accno_id_3) AS income_accno_3,
 
 900                  (SELECT accno FROM chart WHERE id = expense_accno_id_3) AS expense_accno_3
 
 904   $sth = $dbh->prepare($query);
 
 905   $sth->execute || $form->dberror($query);
 
 908   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
 909     push @{ $form->{ALL} }, $ref;
 
 915   $main::lxdebug->leave_sub();
 
 918 sub get_buchungsgruppe {
 
 919   $main::lxdebug->enter_sub();
 
 921   my ($self, $myconfig, $form) = @_;
 
 924   # connect to database
 
 925   my $dbh = $form->dbconnect($myconfig);
 
 929       qq|SELECT description, inventory_accno_id,
 
 930          (SELECT accno FROM chart WHERE id = inventory_accno_id) AS inventory_accno,
 
 932          (SELECT accno FROM chart WHERE id = income_accno_id_0) AS income_accno_0,
 
 934          (SELECT accno FROM chart WHERE id = expense_accno_id_0) AS expense_accno_0,
 
 936          (SELECT accno FROM chart WHERE id = income_accno_id_1) AS income_accno_1,
 
 938          (SELECT accno FROM chart WHERE id = expense_accno_id_1) AS expense_accno_1,
 
 940          (SELECT accno FROM chart WHERE id = income_accno_id_2) AS income_accno_2,
 
 942          (select accno FROM chart WHERE id = expense_accno_id_2) AS expense_accno_2,
 
 944          (SELECT accno FROM chart WHERE id = income_accno_id_3) AS income_accno_3,
 
 946          (SELECT accno FROM chart WHERE id = expense_accno_id_3) AS expense_accno_3
 
 949     my $sth = $dbh->prepare($query);
 
 950     $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
 952     my $ref = $sth->fetchrow_hashref(NAME_lc);
 
 954     map { $form->{$_} = $ref->{$_} } keys %$ref;
 
 959       qq|SELECT count(id) = 0 AS orphaned
 
 961          WHERE buchungsgruppen_id = ?|;
 
 962     ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
 
 965   $query = "SELECT inventory_accno_id, income_accno_id, expense_accno_id ".
 
 967   ($form->{"std_inventory_accno_id"}, $form->{"std_income_accno_id"},
 
 968    $form->{"std_expense_accno_id"}) = selectrow_query($form, $dbh, $query);
 
 971   $query = qq|SELECT c.accno, c.description, c.link, c.id,
 
 972               d.inventory_accno_id, d.income_accno_id, d.expense_accno_id
 
 973               FROM chart c, defaults d
 
 974               WHERE c.link LIKE '%$module%'
 
 978   my $sth = $dbh->prepare($query);
 
 979   $sth->execute || $form->dberror($query);
 
 980   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
 981     foreach my $key (split(/:/, $ref->{link})) {
 
 982       if (!$form->{"std_inventory_accno_id"} && ($key eq "IC")) {
 
 983         $form->{"std_inventory_accno_id"} = $ref->{"id"};
 
 985       if ($key =~ /$module/) {
 
 986         if (   ($ref->{id} eq $ref->{inventory_accno_id})
 
 987             || ($ref->{id} eq $ref->{income_accno_id})
 
 988             || ($ref->{id} eq $ref->{expense_accno_id})) {
 
 989           push @{ $form->{"${module}_links"}{$key} },
 
 990             { accno       => $ref->{accno},
 
 991               description => $ref->{description},
 
 992               selected    => "selected",
 
 995           push @{ $form->{"${module}_links"}{$key} },
 
 996             { accno       => $ref->{accno},
 
 997               description => $ref->{description},
 
1009   $main::lxdebug->leave_sub();
 
1012 sub save_buchungsgruppe {
 
1013   $main::lxdebug->enter_sub();
 
1015   my ($self, $myconfig, $form) = @_;
 
1017   # connect to database
 
1018   my $dbh = $form->dbconnect($myconfig);
 
1020   my @values = ($form->{description}, $form->{inventory_accno_id},
 
1021                 $form->{income_accno_id_0}, $form->{expense_accno_id_0},
 
1022                 $form->{income_accno_id_1}, $form->{expense_accno_id_1},
 
1023                 $form->{income_accno_id_2}, $form->{expense_accno_id_2},
 
1024                 $form->{income_accno_id_3}, $form->{expense_accno_id_3});
 
1028   # id is the old record
 
1030     $query = qq|UPDATE buchungsgruppen SET
 
1031                 description = ?, inventory_accno_id = ?,
 
1032                 income_accno_id_0 = ?, expense_accno_id_0 = ?,
 
1033                 income_accno_id_1 = ?, expense_accno_id_1 = ?,
 
1034                 income_accno_id_2 = ?, expense_accno_id_2 = ?,
 
1035                 income_accno_id_3 = ?, expense_accno_id_3 = ?
 
1037     push(@values, $form->{id});
 
1039     $query = qq|SELECT COALESCE(MAX(sortkey) + 1, 1) FROM buchungsgruppen|;
 
1040     my ($sortkey) = $dbh->selectrow_array($query);
 
1041     $form->dberror($query) if ($dbh->err);
 
1042     push(@values, $sortkey);
 
1043     $query = qq|INSERT INTO buchungsgruppen
 
1044                 (description, inventory_accno_id,
 
1045                 income_accno_id_0, expense_accno_id_0,
 
1046                 income_accno_id_1, expense_accno_id_1,
 
1047                 income_accno_id_2, expense_accno_id_2,
 
1048                 income_accno_id_3, expense_accno_id_3,
 
1050                 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
 
1052   do_query($form, $dbh, $query, @values);
 
1056   $main::lxdebug->leave_sub();
 
1059 sub delete_buchungsgruppe {
 
1060   $main::lxdebug->enter_sub();
 
1062   my ($self, $myconfig, $form) = @_;
 
1064   # connect to database
 
1065   my $dbh = $form->dbconnect($myconfig);
 
1067   $query = qq|DELETE FROM buchungsgruppen WHERE id = ?|;
 
1068   do_query($form, $dbh, $query, $form->{id});
 
1072   $main::lxdebug->leave_sub();
 
1076   $main::lxdebug->enter_sub();
 
1078   my ($self, $myconfig, $form, $table) = @_;
 
1080   # connect to database
 
1081   my $dbh = $form->get_standard_dbh($myconfig);
 
1085        (SELECT sortkey FROM $table WHERE id = ?) AS sortkey1,
 
1086        (SELECT sortkey FROM $table WHERE id = ?) AS sortkey2|;
 
1087   my @values   = ($form->{"id1"}, $form->{"id2"});
 
1088   my @sortkeys = selectrow_query($form, $dbh, $query, @values);
 
1090   $query  = qq|UPDATE $table SET sortkey = ? WHERE id = ?|;
 
1091   my $sth = prepare_query($form, $dbh, $query);
 
1093   do_statement($form, $sth, $query, $sortkeys[1], $form->{"id1"});
 
1094   do_statement($form, $sth, $query, $sortkeys[0], $form->{"id2"});
 
1100   $main::lxdebug->leave_sub();
 
1104   $main::lxdebug->enter_sub();
 
1106   my ($self, $myconfig, $form) = @_;
 
1108   # connect to database
 
1109   my $dbh = $form->dbconnect($myconfig);
 
1111   my $query = qq|SELECT id, printer_description, template_code, printer_command
 
1115   $sth = $dbh->prepare($query);
 
1116   $sth->execute || $form->dberror($query);
 
1118   $form->{"ALL"} = [];
 
1119   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1120     push @{ $form->{ALL} }, $ref;
 
1126   $main::lxdebug->leave_sub();
 
1130   $main::lxdebug->enter_sub();
 
1132   my ($self, $myconfig, $form) = @_;
 
1134   # connect to database
 
1135   my $dbh = $form->dbconnect($myconfig);
 
1138     qq|SELECT p.printer_description, p.template_code, p.printer_command
 
1141   my $sth = $dbh->prepare($query);
 
1142   $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
1144   my $ref = $sth->fetchrow_hashref(NAME_lc);
 
1146   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
1152   $main::lxdebug->leave_sub();
 
1156   $main::lxdebug->enter_sub();
 
1158   my ($self, $myconfig, $form) = @_;
 
1160   # connect to database
 
1161   my $dbh = $form->dbconnect($myconfig);
 
1163   my @values = ($form->{printer_description},
 
1164                 $form->{template_code},
 
1165                 $form->{printer_command});
 
1167   # id is the old record
 
1169     $query = qq|UPDATE printers SET
 
1170                 printer_description = ?, template_code = ?, printer_command = ?
 
1172     push(@values, $form->{id});
 
1174     $query = qq|INSERT INTO printers
 
1175                 (printer_description, template_code, printer_command)
 
1178   do_query($form, $dbh, $query, @values);
 
1182   $main::lxdebug->leave_sub();
 
1185 sub delete_printer {
 
1186   $main::lxdebug->enter_sub();
 
1188   my ($self, $myconfig, $form) = @_;
 
1190   # connect to database
 
1191   my $dbh = $form->dbconnect($myconfig);
 
1193   $query = qq|DELETE FROM printers
 
1195   do_query($form, $dbh, $query, $form->{id});
 
1199   $main::lxdebug->leave_sub();
 
1203   $main::lxdebug->enter_sub();
 
1205   my ($self, $myconfig, $form) = @_;
 
1207   # connect to database
 
1208   my $dbh = $form->dbconnect($myconfig);
 
1210   my $query = qq|SELECT * FROM payment_terms ORDER BY sortkey|;
 
1212   $sth = $dbh->prepare($query);
 
1213   $sth->execute || $form->dberror($query);
 
1216   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1217     push @{ $form->{ALL} }, $ref;
 
1223   $main::lxdebug->leave_sub();
 
1227   $main::lxdebug->enter_sub();
 
1229   my ($self, $myconfig, $form) = @_;
 
1231   # connect to database
 
1232   my $dbh = $form->dbconnect($myconfig);
 
1234   my $query = qq|SELECT * FROM payment_terms WHERE id = ?|;
 
1235   my $sth = $dbh->prepare($query);
 
1236   $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
 
1238   my $ref = $sth->fetchrow_hashref(NAME_lc);
 
1239   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
1243     qq|SELECT t.language_id, t.description_long, l.description AS language | .
 
1244     qq|FROM translation_payment_terms t | .
 
1245     qq|LEFT JOIN language l ON t.language_id = l.id | .
 
1246     qq|WHERE t.payment_terms_id = ? | .
 
1248     qq|SELECT l.id AS language_id, NULL AS description_long, | .
 
1249     qq|  l.description AS language | .
 
1250     qq|FROM language l|;
 
1251   $sth = $dbh->prepare($query);
 
1252   $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
 
1255   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1256     $mapping{ $ref->{"language_id"} } = $ref
 
1257       unless (defined($mapping{ $ref->{"language_id"} }));
 
1261   $form->{"TRANSLATION"} = [sort({ $a->{"language"} cmp $b->{"language"} }
 
1266   $main::lxdebug->leave_sub();
 
1270   $main::lxdebug->enter_sub();
 
1272   my ($self, $myconfig, $form) = @_;
 
1274   # connect to database
 
1275   my $dbh = $form->dbconnect_noauto($myconfig);
 
1280     $query = qq|SELECT nextval('id'), COALESCE(MAX(sortkey) + 1, 1) | .
 
1281       qq|FROM payment_terms|;
 
1283     ($form->{id}, $sortkey) = selectrow_query($form, $dbh, $query);
 
1285     $query = qq|INSERT INTO payment_terms (id, sortkey) VALUES (?, ?)|;
 
1286     do_query($form, $dbh, $query, $form->{id}, $sortkey);
 
1290       qq|DELETE FROM translation_payment_terms | .
 
1291       qq|WHERE payment_terms_id = ?|;
 
1292     do_query($form, $dbh, $query, $form->{"id"});
 
1295   $query = qq|UPDATE payment_terms SET
 
1296               description = ?, description_long = ?,
 
1297               terms_netto = ?, terms_skonto = ?,
 
1300   my @values = ($form->{description}, $form->{description_long},
 
1301                 $form->{terms_netto} * 1, $form->{terms_skonto} * 1,
 
1302                 $form->{percent_skonto} * 1,
 
1304   do_query($form, $dbh, $query, @values);
 
1306   $query = qq|SELECT id FROM language|;
 
1308   my $sth = $dbh->prepare($query);
 
1309   $sth->execute() || $form->dberror($query);
 
1311   while (my ($id) = $sth->fetchrow_array()) {
 
1312     push(@language_ids, $id);
 
1317     qq|INSERT INTO translation_payment_terms | .
 
1318     qq|(language_id, payment_terms_id, description_long) | .
 
1319     qq|VALUES (?, ?, ?)|;
 
1320   $sth = $dbh->prepare($query);
 
1322   foreach my $language_id (@language_ids) {
 
1323     do_statement($form, $sth, $query, $language_id, $form->{"id"},
 
1324                  $form->{"description_long_${language_id}"});
 
1331   $main::lxdebug->leave_sub();
 
1334 sub delete_payment {
 
1335   $main::lxdebug->enter_sub();
 
1337   my ($self, $myconfig, $form) = @_;
 
1339   # connect to database
 
1340   my $dbh = $form->dbconnect_noauto($myconfig);
 
1343     qq|DELETE FROM translation_payment_terms WHERE payment_terms_id = ?|;
 
1344   do_query($form, $dbh, $query, $form->{"id"});
 
1346   $query = qq|DELETE FROM payment_terms WHERE id = ?|;
 
1347   do_query($form, $dbh, $query, $form->{"id"});
 
1352   $main::lxdebug->leave_sub();
 
1356 sub prepare_template_filename {
 
1357   $main::lxdebug->enter_sub();
 
1359   my ($self, $myconfig, $form) = @_;
 
1361   my ($filename, $display_filename);
 
1363   if ($form->{type} eq "stylesheet") {
 
1364     $filename = "css/$myconfig->{stylesheet}";
 
1365     $display_filename = $myconfig->{stylesheet};
 
1368     $filename = $form->{formname};
 
1370     if ($form->{language}) {
 
1371       my ($id, $template_code) = split(/--/, $form->{language});
 
1372       $filename .= "_${template_code}";
 
1375     if ($form->{printer}) {
 
1376       my ($id, $template_code) = split(/--/, $form->{printer});
 
1377       $filename .= "_${template_code}";
 
1380     $filename .= "." . ($form->{format} eq "html" ? "html" : "tex");
 
1381     $filename =~ s|.*/||;
 
1382     $display_filename = $filename;
 
1383     $filename = "$myconfig->{templates}/$filename";
 
1386   $main::lxdebug->leave_sub();
 
1388   return ($filename, $display_filename);
 
1393   $main::lxdebug->enter_sub();
 
1395   my ($self, $filename) = @_;
 
1397   my ($content, $lines) = ("", 0);
 
1401   if (open(TEMPLATE, $filename)) {
 
1402     while (<TEMPLATE>) {
 
1409   $main::lxdebug->leave_sub();
 
1411   return ($content, $lines);
 
1415   $main::lxdebug->enter_sub();
 
1417   my ($self, $filename, $content) = @_;
 
1423   if (open(TEMPLATE, ">$filename")) {
 
1424     $content =~ s/\r\n/\n/g;
 
1425     print(TEMPLATE $content);
 
1431   $main::lxdebug->leave_sub();
 
1437   $main::lxdebug->enter_sub();
 
1442   my $myconfig = \%main::myconfig;
 
1443   my $form     = $main::form;
 
1445   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
 
1448   map { ($accnos{$_}) = split(m/--/, $form->{$_}) } qw(inventory_accno income_accno expense_accno fxgain_accno fxloss_accno);
 
1450   $form->{curr}  =~ s/ //g;
 
1451   my @currencies =  grep { $_ ne '' } split m/:/, $form->{curr};
 
1452   my $currency   =  join ':', @currencies;
 
1454   # these defaults are database wide
 
1457     qq|UPDATE defaults SET
 
1458         inventory_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
 
1459         income_accno_id    = (SELECT c.id FROM chart c WHERE c.accno = ?),
 
1460         expense_accno_id   = (SELECT c.id FROM chart c WHERE c.accno = ?),
 
1461         fxgain_accno_id    = (SELECT c.id FROM chart c WHERE c.accno = ?),
 
1462         fxloss_accno_id    = (SELECT c.id FROM chart c WHERE c.accno = ?),
 
1479   my @values = ($accnos{inventory_accno}, $accnos{income_accno}, $accnos{expense_accno},
 
1480                 $accnos{fxgain_accno},    $accnos{fxloss_accno},
 
1481                 $form->{invnumber},       $form->{cnnumber},
 
1482                 $form->{sonumber},        $form->{ponumber},
 
1483                 $form->{sqnumber},        $form->{rfqnumber},
 
1484                 $form->{customernumber},  $form->{vendornumber},
 
1485                 $form->{articlenumber},   $form->{servicenumber},
 
1486                 $form->{sdonumber},       $form->{pdonumber},
 
1487                 $form->{yearend},         $currency,
 
1488                 $form->{businessnumber},  $form->{weightunit});
 
1489   do_query($form, $dbh, $query, @values);
 
1493   $main::lxdebug->leave_sub();
 
1497 sub save_preferences {
 
1498   $main::lxdebug->enter_sub();
 
1500   my ($self, $myconfig, $form, $webdav) = @_;
 
1502   my $dbh = $form->get_standard_dbh($myconfig);
 
1504   my ($currency, $businessnumber) = selectrow_query($form, $dbh, qq|SELECT curr, businessnumber FROM defaults|);
 
1507   my $query = qq|UPDATE employee SET name = ? WHERE login = ?|;
 
1508   do_query($form, $dbh, $query, $form->{name}, $form->{login});
 
1510   my $rc = $dbh->commit();
 
1512   # save first currency in myconfig
 
1513   $currency               =~ s/:.*//;
 
1514   $form->{currency}       =  $currency;
 
1516   $form->{businessnumber} =  $businessnumber;
 
1518   my $myconfig = new User($form->{login});
 
1520   foreach my $item (keys %$form) {
 
1521     $myconfig->{$item} = $form->{$item};
 
1524   $myconfig->save_member($memberfile);
 
1526   my $auth = $main::auth;
 
1528   if ($auth->can_change_password()
 
1529       && defined $form->{new_password}
 
1530       && ($form->{new_password} ne '********')) {
 
1531     $auth->change_password($form->{login}, $form->{new_password});
 
1533     $form->{password} = $form->{new_password};
 
1534     $auth->set_session_value('password', $form->{password});
 
1535     $auth->create_or_refresh_session();
 
1540       qw(angebote bestellungen rechnungen anfragen lieferantenbestellungen einkaufsrechnungen);
 
1541     foreach $directory (@webdavdirs) {
 
1542       $file = "webdav/" . $directory . "/webdav-user";
 
1543       if ($myconfig->{$directory}) {
 
1544         open(HTACCESS, "$file") or die "cannot open webdav-user $!\n";
 
1545         while (<HTACCESS>) {
 
1546           ($login, $password) = split(/:/, $_);
 
1547           if ($login ne $form->{login}) {
 
1552         open(HTACCESS, "> $file") or die "cannot open webdav-user $!\n";
 
1553         $newfile .= $myconfig->{login} . ":" . $myconfig->{password} . "\n";
 
1554         print(HTACCESS $newfile);
 
1557         $form->{$directory} = 0;
 
1558         open(HTACCESS, "$file") or die "cannot open webdav-user $!\n";
 
1559         while (<HTACCESS>) {
 
1560           ($login, $password) = split(/:/, $_);
 
1561           if ($login ne $form->{login}) {
 
1566         open(HTACCESS, "> $file") or die "cannot open webdav-user $!\n";
 
1567         print(HTACCESS $newfile);
 
1573   $main::lxdebug->leave_sub();
 
1579   $main::lxdebug->enter_sub();
 
1584   my $myconfig = \%main::myconfig;
 
1585   my $form     = $main::form;
 
1587   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
 
1589   my $defaults = selectfirst_hashref_query($form, $dbh, qq|SELECT * FROM defaults|) || {};
 
1591   $defaults->{weightunit} ||= 'kg';
 
1593   $main::lxdebug->leave_sub();
 
1598 sub defaultaccounts {
 
1599   $main::lxdebug->enter_sub();
 
1601   my ($self, $myconfig, $form) = @_;
 
1603   # connect to database
 
1604   my $dbh = $form->dbconnect($myconfig);
 
1606   # get defaults from defaults table
 
1607   my $query = qq|SELECT * FROM defaults|;
 
1608   my $sth   = $dbh->prepare($query);
 
1609   $sth->execute || $form->dberror($query);
 
1611   $form->{defaults}               = $sth->fetchrow_hashref(NAME_lc);
 
1612   $form->{defaults}{IC}           = $form->{defaults}{inventory_accno_id};
 
1613   $form->{defaults}{IC_income}    = $form->{defaults}{income_accno_id};
 
1614   $form->{defaults}{IC_expense}   = $form->{defaults}{expense_accno_id};
 
1615   $form->{defaults}{FX_gain}      = $form->{defaults}{fxgain_accno_id};
 
1616   $form->{defaults}{FX_loss}      = $form->{defaults}{fxloss_accno_id};
 
1618   $form->{defaults}{weightunit} ||= 'kg';
 
1622   $query = qq|SELECT c.id, c.accno, c.description, c.link
 
1624               WHERE c.link LIKE '%IC%'
 
1626   $sth = $dbh->prepare($query);
 
1627   $sth->execute || $self->dberror($query);
 
1629   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1630     foreach my $key (split(/:/, $ref->{link})) {
 
1633         if ($key =~ /cogs/) {
 
1634           $nkey = "IC_expense";
 
1636         if ($key =~ /sale/) {
 
1637           $nkey = "IC_income";
 
1639         %{ $form->{IC}{$nkey}{ $ref->{accno} } } = (
 
1641                                              description => $ref->{description}
 
1648   $query = qq|SELECT c.id, c.accno, c.description
 
1650               WHERE c.category = 'I'
 
1651               AND c.charttype = 'A'
 
1653   $sth = $dbh->prepare($query);
 
1654   $sth->execute || $self->dberror($query);
 
1656   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1657     %{ $form->{IC}{FX_gain}{ $ref->{accno} } } = (
 
1659                                              description => $ref->{description}
 
1664   $query = qq|SELECT c.id, c.accno, c.description
 
1666               WHERE c.category = 'E'
 
1667               AND c.charttype = 'A'
 
1669   $sth = $dbh->prepare($query);
 
1670   $sth->execute || $self->dberror($query);
 
1672   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1673     %{ $form->{IC}{FX_loss}{ $ref->{accno} } } = (
 
1675                                              description => $ref->{description}
 
1680   # now get the tax rates and numbers
 
1681   $query = qq|SELECT c.id, c.accno, c.description,
 
1682               t.rate * 100 AS rate, t.taxnumber
 
1684               WHERE c.id = t.chart_id|;
 
1686   $sth = $dbh->prepare($query);
 
1687   $sth->execute || $form->dberror($query);
 
1689   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1690     $form->{taxrates}{ $ref->{accno} }{id}          = $ref->{id};
 
1691     $form->{taxrates}{ $ref->{accno} }{description} = $ref->{description};
 
1692     $form->{taxrates}{ $ref->{accno} }{taxnumber}   = $ref->{taxnumber}
 
1693       if $ref->{taxnumber};
 
1694     $form->{taxrates}{ $ref->{accno} }{rate} = $ref->{rate} if $ref->{rate};
 
1700   $main::lxdebug->leave_sub();
 
1704   $main::lxdebug->enter_sub();
 
1706   my ($self, $myconfig, $form) = @_;
 
1708   my $dbh = $form->dbconnect($myconfig);
 
1710   my $query = qq|SELECT closedto, revtrans FROM defaults|;
 
1711   my $sth   = $dbh->prepare($query);
 
1712   $sth->execute || $form->dberror($query);
 
1714   ($form->{closedto}, $form->{revtrans}) = $sth->fetchrow_array;
 
1720   $main::lxdebug->leave_sub();
 
1724   $main::lxdebug->enter_sub();
 
1726   my ($self, $myconfig, $form) = @_;
 
1728   my $dbh = $form->dbconnect($myconfig);
 
1730   my ($query, @values);
 
1732   if ($form->{revtrans}) {
 
1733     $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '1'|;
 
1735   } elsif ($form->{closedto}) {
 
1736     $query = qq|UPDATE defaults SET closedto = ?, revtrans = '0'|;
 
1737     @values = (conv_date($form->{closedto}));
 
1740     $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '0'|;
 
1743   # set close in defaults
 
1744   do_query($form, $dbh, $query, @values);
 
1748   $main::lxdebug->leave_sub();
 
1752   my ($self, $units, $unit_name, $factor) = @_;
 
1754   $factor = 1 unless ($factor);
 
1756   my $unit = $units->{$unit_name};
 
1758   if (!defined($unit) || !$unit->{"base_unit"} ||
 
1759       ($unit_name eq $unit->{"base_unit"})) {
 
1760     return ($unit_name, $factor);
 
1763   return AM->get_base_unit($units, $unit->{"base_unit"}, $factor * $unit->{"factor"});
 
1766 sub retrieve_units {
 
1767   $main::lxdebug->enter_sub();
 
1769   my ($self, $myconfig, $form, $prefix) = @_;
 
1771   my $dbh = $form->dbconnect($myconfig);
 
1773   my $query = "SELECT *, base_unit AS original_base_unit FROM units";
 
1775   my $sth = prepare_execute_query($form, $dbh, $query);
 
1778   while (my $ref = $sth->fetchrow_hashref()) {
 
1779     $units->{$ref->{"name"}} = $ref;
 
1783   my $query_lang = "SELECT id, template_code FROM language ORDER BY description";
 
1784   $sth = $dbh->prepare($query_lang);
 
1785   $sth->execute() || $form->dberror($query_lang);
 
1787   while ($ref = $sth->fetchrow_hashref()) {
 
1788     push(@languages, $ref);
 
1792   $query_lang = "SELECT ul.localized, ul.localized_plural, l.id, l.template_code " .
 
1793     "FROM units_language ul " .
 
1794     "LEFT JOIN language l ON ul.language_id = l.id " .
 
1795     "WHERE ul.unit = ?";
 
1796   $sth = $dbh->prepare($query_lang);
 
1798   foreach my $unit (values(%{$units})) {
 
1799     ($unit->{"${prefix}base_unit"}, $unit->{"${prefix}factor"}) = AM->get_base_unit($units, $unit->{"name"});
 
1801     $unit->{"LANGUAGES"} = {};
 
1802     foreach my $lang (@languages) {
 
1803       $unit->{"LANGUAGES"}->{$lang->{"template_code"}} = { "template_code" => $lang->{"template_code"} };
 
1806     $sth->execute($unit->{"name"}) || $form->dberror($query_lang . " (" . $unit->{"name"} . ")");
 
1807     while ($ref = $sth->fetchrow_hashref()) {
 
1808       map({ $unit->{"LANGUAGES"}->{$ref->{"template_code"}}->{$_} = $ref->{$_} } keys(%{$ref}));
 
1815   $main::lxdebug->leave_sub();
 
1820 sub retrieve_all_units {
 
1821   $main::lxdebug->enter_sub();
 
1825   if (!$main::all_units) {
 
1826     $main::all_units = $self->retrieve_units(\%main::myconfig, $main::form);
 
1829   $main::lxdebug->leave_sub();
 
1831   return $main::all_units;
 
1835 sub translate_units {
 
1836   $main::lxdebug->enter_sub();
 
1838   my ($self, $form, $template_code, $unit, $amount) = @_;
 
1840   my $units = $self->retrieve_units(\%main::myconfig, $form);
 
1842   my $h = $units->{$unit}->{"LANGUAGES"}->{$template_code};
 
1843   my $new_unit = $unit;
 
1845     if (($amount != 1) && $h->{"localized_plural"}) {
 
1846       $new_unit = $h->{"localized_plural"};
 
1847     } elsif ($h->{"localized"}) {
 
1848       $new_unit = $h->{"localized"};
 
1852   $main::lxdebug->leave_sub();
 
1858   $main::lxdebug->enter_sub();
 
1860   my ($self, $myconfig, $form, $units) = @_;
 
1862   my $dbh = $form->dbconnect($myconfig);
 
1864   map({ $_->{"in_use"} = 0; } values(%{$units}));
 
1866   foreach my $unit (values(%{$units})) {
 
1867     my $base_unit = $unit->{"original_base_unit"};
 
1868     while ($base_unit) {
 
1869       $units->{$base_unit}->{"in_use"} = 1;
 
1870       $units->{$base_unit}->{"DEPENDING_UNITS"} = [] unless ($units->{$base_unit}->{"DEPENDING_UNITS"});
 
1871       push(@{$units->{$base_unit}->{"DEPENDING_UNITS"}}, $unit->{"name"});
 
1872       $base_unit = $units->{$base_unit}->{"original_base_unit"};
 
1876   foreach my $unit (values(%{$units})) {
 
1877     map({ $_ = $dbh->quote($_); } @{$unit->{"DEPENDING_UNITS"}});
 
1879     foreach my $table (qw(parts invoice orderitems)) {
 
1880       my $query = "SELECT COUNT(*) FROM $table WHERE unit ";
 
1882       if (0 == scalar(@{$unit->{"DEPENDING_UNITS"}})) {
 
1883         $query .= "= " . $dbh->quote($unit->{"name"});
 
1885         $query .= "IN (" . $dbh->quote($unit->{"name"}) . "," .
 
1886           join(",", map({ $dbh->quote($_) } @{$unit->{"DEPENDING_UNITS"}})) . ")";
 
1889       my ($count) = $dbh->selectrow_array($query);
 
1890       $form->dberror($query) if ($dbh->err);
 
1893         $unit->{"in_use"} = 1;
 
1901   $main::lxdebug->leave_sub();
 
1904 sub convertible_units {
 
1905   $main::lxdebug->enter_sub();
 
1909   my $filter_unit = shift;
 
1910   my $not_smaller = shift;
 
1912   my $conv_units = [];
 
1914   $filter_unit = $units->{$filter_unit};
 
1916   foreach my $name (sort { lc $a cmp lc $b } keys %{ $units }) {
 
1917     my $unit = $units->{$name};
 
1919     if (($unit->{base_unit} eq $filter_unit->{base_unit}) &&
 
1920         (!$not_smaller || ($unit->{factor} >= $filter_unit->{factor}))) {
 
1921       push @{$conv_units}, $unit;
 
1925   my @sorted = sort { $b->{factor} <=> $a->{factor} } @{ $conv_units };
 
1927   $main::lxdebug->leave_sub();
 
1932 # if $a is translatable to $b, return the factor between them.
 
1935   $main::lxdebug->enter_sub(2);
 
1936   ($this, $a, $b, $all_units) = @_;
 
1938   $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a} && $all_units->{$b};
 
1939   $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a}{base_unit} eq $all_units->{$b}{base_unit};
 
1940   $main::lxdebug->leave_sub(2) and return $all_units->{$a}{factor} / $all_units->{$b}{factor}; 
 
1943 sub unit_select_data {
 
1944   $main::lxdebug->enter_sub();
 
1946   my ($self, $units, $selected, $empty_entry, $convertible_into) = @_;
 
1951     push(@{$select}, { "name" => "", "base_unit" => "", "factor" => "", "selected" => "" });
 
1954   foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
 
1955     if (!$convertible_into ||
 
1956         ($units->{$convertible_into} &&
 
1957          ($units->{$convertible_into}->{base_unit} eq $units->{$unit}->{base_unit}))) {
 
1958       push @{$select}, { "name"      => $unit,
 
1959                          "base_unit" => $units->{$unit}->{"base_unit"},
 
1960                          "factor"    => $units->{$unit}->{"factor"},
 
1961                          "selected"  => ($unit eq $selected) ? "selected" : "" };
 
1965   $main::lxdebug->leave_sub();
 
1970 sub unit_select_html {
 
1971   $main::lxdebug->enter_sub();
 
1973   my ($self, $units, $name, $selected, $convertible_into) = @_;
 
1975   my $select = "<select name=${name}>";
 
1977   foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
 
1978     if (!$convertible_into ||
 
1979         ($units->{$convertible_into} &&
 
1980          ($units->{$convertible_into}->{"base_unit"} eq $units->{$unit}->{"base_unit"}))) {
 
1981       $select .= "<option" . (($unit eq $selected) ? " selected" : "") . ">${unit}</option>";
 
1984   $select .= "</select>";
 
1986   $main::lxdebug->leave_sub();
 
1992   $main::lxdebug->enter_sub();
 
1996   my $units = $self->retrieve_all_units();
 
2001   while (2 <= scalar(@_)) {
 
2002     my $qty  = shift(@_);
 
2003     my $unit = $units->{shift(@_)};
 
2005     croak "No unit defined with name $unit" if (!defined $unit);
 
2008       $base_unit = $unit->{base_unit};
 
2009     } elsif ($base_unit ne $unit->{base_unit}) {
 
2010       croak "Adding values with incompatible base units $base_unit/$unit->{base_unit}";
 
2013     $sum += $qty * $unit->{factor};
 
2016   $main::lxdebug->leave_sub();
 
2018   return wantarray ? ($sum, $baseunit) : $sum;
 
2022   $main::lxdebug->enter_sub();
 
2024   my ($self, $myconfig, $form, $name, $base_unit, $factor, $languages) = @_;
 
2026   my $dbh = $form->dbconnect_noauto($myconfig);
 
2028   my $query = qq|SELECT COALESCE(MAX(sortkey), 0) + 1 FROM units|;
 
2029   my ($sortkey) = selectrow_query($form, $dbh, $query);
 
2031   $query = "INSERT INTO units (name, base_unit, factor, sortkey) " .
 
2032     "VALUES (?, ?, ?, ?)";
 
2033   do_query($form, $dbh, $query, $name, $base_unit, $factor, $sortkey);
 
2036     $query = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
 
2037     my $sth = $dbh->prepare($query);
 
2038     foreach my $lang (@{$languages}) {
 
2039       my @values = ($name, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
 
2040       $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
 
2048   $main::lxdebug->leave_sub();
 
2052   $main::lxdebug->enter_sub();
 
2054   my ($self, $myconfig, $form, $units, $delete_units) = @_;
 
2056   my $dbh = $form->dbconnect_noauto($myconfig);
 
2058   my ($base_unit, $unit, $sth, $query);
 
2060   $query = "DELETE FROM units_language";
 
2061   $dbh->do($query) || $form->dberror($query);
 
2063   if ($delete_units && (0 != scalar(@{$delete_units}))) {
 
2064     $query = "DELETE FROM units WHERE name IN (";
 
2065     map({ $query .= "?," } @{$delete_units});
 
2066     substr($query, -1, 1) = ")";
 
2067     $dbh->do($query, undef, @{$delete_units}) ||
 
2068       $form->dberror($query . " (" . join(", ", @{$delete_units}) . ")");
 
2071   $query = "UPDATE units SET name = ?, base_unit = ?, factor = ? WHERE name = ?";
 
2072   $sth = $dbh->prepare($query);
 
2074   my $query_lang = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
 
2075   my $sth_lang = $dbh->prepare($query_lang);
 
2077   foreach $unit (values(%{$units})) {
 
2078     $unit->{"depth"} = 0;
 
2079     my $base_unit = $unit;
 
2080     while ($base_unit->{"base_unit"}) {
 
2082       $base_unit = $units->{$base_unit->{"base_unit"}};
 
2086   foreach $unit (sort({ $a->{"depth"} <=> $b->{"depth"} } values(%{$units}))) {
 
2087     if ($unit->{"LANGUAGES"}) {
 
2088       foreach my $lang (@{$unit->{"LANGUAGES"}}) {
 
2089         next unless ($lang->{"id"} && $lang->{"localized"});
 
2090         my @values = ($unit->{"name"}, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
 
2091         $sth_lang->execute(@values) || $form->dberror($query_lang . " (" . join(", ", @values) . ")");
 
2095     next if ($unit->{"unchanged_unit"});
 
2097     my @values = ($unit->{"name"}, $unit->{"base_unit"}, $unit->{"factor"}, $unit->{"old_name"});
 
2098     $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
 
2102   $sth_lang->finish();
 
2106   $main::lxdebug->leave_sub();
 
2110   $main::lxdebug->enter_sub();
 
2112   my ($self, $myconfig, $form, $dir, $name_1) = @_;
 
2114   my $dbh = $form->dbconnect_noauto($myconfig);
 
2118   $query = qq|SELECT sortkey FROM units WHERE name = ?|;
 
2119   my ($sortkey_1) = selectrow_query($form, $dbh, $query, $name_1);
 
2122     qq|SELECT sortkey FROM units | .
 
2123     qq|WHERE sortkey | . ($dir eq "down" ? ">" : "<") . qq| ? | .
 
2124     qq|ORDER BY sortkey | . ($dir eq "down" ? "ASC" : "DESC") . qq| LIMIT 1|;
 
2125   my ($sortkey_2) = selectrow_query($form, $dbh, $query, $sortkey_1);
 
2127   if (defined($sortkey_1)) {
 
2128     $query = qq|SELECT name FROM units WHERE sortkey = ${sortkey_2}|;
 
2129     my ($name_2) = selectrow_query($form, $dbh, $query);
 
2131     if (defined($name_2)) {
 
2132       $query = qq|UPDATE units SET sortkey = ? WHERE name = ?|;
 
2133       my $sth = $dbh->prepare($query);
 
2135       do_statement($form, $sth, $query, $sortkey_1, $name_2);
 
2136       do_statement($form, $sth, $query, $sortkey_2, $name_1);
 
2143   $main::lxdebug->leave_sub();
 
2147   $main::lxdebug->enter_sub();
 
2149   my ($self, $myconfig, $form) = @_;
 
2151   # connect to database
 
2152   my $dbh = $form->dbconnect($myconfig);
 
2154   my $query = qq|SELECT
 
2158                    round(t.rate * 100, 2) AS rate,
 
2159                    (SELECT accno FROM chart WHERE id = chart_id) AS taxnumber,
 
2160                    (SELECT description FROM chart WHERE id = chart_id) AS account_description
 
2164   $sth = $dbh->prepare($query);
 
2165   $sth->execute || $form->dberror($query);
 
2168   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
2169     push @{ $form->{TAX} }, $ref;
 
2175   $main::lxdebug->leave_sub();
 
2178 sub get_tax_accounts {
 
2179   $main::lxdebug->enter_sub();
 
2181   my ($self, $myconfig, $form) = @_;
 
2183   my $dbh = $form->dbconnect($myconfig);
 
2185   # get Accounts from chart
 
2186   my $query = qq{ SELECT
 
2188                  accno || ' - ' || description AS taxaccount
 
2190                WHERE link LIKE '%_tax%'
 
2194   $sth = $dbh->prepare($query);
 
2195   $sth->execute || $form->dberror($query);
 
2197   $form->{ACCOUNTS} = [];
 
2198   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
2199     push @{ $form->{ACCOUNTS} }, $ref;
 
2206   $main::lxdebug->leave_sub();
 
2210   $main::lxdebug->enter_sub();
 
2212   my ($self, $myconfig, $form) = @_;
 
2214   # connect to database
 
2215   my $dbh = $form->dbconnect($myconfig);
 
2217   my $query = qq|SELECT
 
2220                    round(rate * 100, 2) AS rate,
 
2225   my $sth = $dbh->prepare($query);
 
2226   $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
2228   my $ref = $sth->fetchrow_hashref(NAME_lc);
 
2230   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
2234   # see if it is used by a taxkey
 
2235   $query = qq|SELECT count(*) FROM taxkeys
 
2236               WHERE tax_id = ? AND chart_id >0|;
 
2238   ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
 
2240   $form->{orphaned} = !$form->{orphaned};
 
2243   if (!$form->{orphaned} ) {
 
2244     $query = qq|SELECT DISTINCT c.id, c.accno
 
2246                 JOIN   tax t ON (t.id = tk.tax_id)
 
2247                 JOIN chart c ON (c.id = tk.chart_id)
 
2248                 WHERE tk.tax_id = ?|;
 
2250     $sth = $dbh->prepare($query);
 
2251     $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
2253     $form->{TAXINUSE} = [];
 
2254     while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
2255       push @{ $form->{TAXINUSE} }, $ref;
 
2263   $main::lxdebug->leave_sub();
 
2267   $main::lxdebug->enter_sub();
 
2269   my ($self, $myconfig, $form) = @_;
 
2271   # connect to database
 
2272   my $dbh = $form->get_standard_dbh($myconfig);
 
2274   $form->{rate} = $form->{rate} / 100;
 
2276   my @values = ($form->{taxkey}, $form->{taxdescription}, $form->{rate}, $form->{chart_id}, $form->{chart_id} );
 
2277   if ($form->{id} ne "") {
 
2278     $query = qq|UPDATE tax SET
 
2283                   taxnumber      = (SELECT accno FROM chart WHERE id= ? )
 
2285     push(@values, $form->{id});
 
2289     $query = qq|INSERT INTO tax (
 
2296                 VALUES (?, ?, ?, ?, (SELECT accno FROM chart WHERE id = ?) )|;
 
2298   do_query($form, $dbh, $query, @values);
 
2302   $main::lxdebug->leave_sub();
 
2306   $main::lxdebug->enter_sub();
 
2308   my ($self, $myconfig, $form) = @_;
 
2310   # connect to database
 
2311   my $dbh = $form->get_standard_dbh($myconfig);
 
2313   $query = qq|DELETE FROM tax
 
2315   do_query($form, $dbh, $query, $form->{id});
 
2319   $main::lxdebug->leave_sub();
 
2322 sub save_price_factor {
 
2323   $main::lxdebug->enter_sub();
 
2325   my ($self, $myconfig, $form) = @_;
 
2327   # connect to database
 
2328   my $dbh = $form->get_standard_dbh($myconfig);
 
2331   my @values = ($form->{description}, conv_i($form->{factor}));
 
2334     $query = qq|UPDATE price_factors SET description = ?, factor = ? WHERE id = ?|;
 
2335     push @values, conv_i($form->{id});
 
2338     $query = qq|INSERT INTO price_factors (description, factor, sortkey) VALUES (?, ?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM price_factors))|;
 
2341   do_query($form, $dbh, $query, @values);
 
2345   $main::lxdebug->leave_sub();
 
2348 sub get_all_price_factors {
 
2349   $main::lxdebug->enter_sub();
 
2351   my ($self, $myconfig, $form) = @_;
 
2353   # connect to database
 
2354   my $dbh = $form->get_standard_dbh($myconfig);
 
2356   $form->{PRICE_FACTORS} = selectall_hashref_query($form, $dbh, qq|SELECT * FROM price_factors ORDER BY sortkey|);
 
2358   $main::lxdebug->leave_sub();
 
2361 sub get_price_factor {
 
2362   $main::lxdebug->enter_sub();
 
2364   my ($self, $myconfig, $form) = @_;
 
2366   # connect to database
 
2367   my $dbh = $form->get_standard_dbh($myconfig);
 
2369   my $query = qq|SELECT description, factor,
 
2370                    ((SELECT COUNT(*) FROM parts      WHERE price_factor_id = ?) +
 
2371                     (SELECT COUNT(*) FROM invoice    WHERE price_factor_id = ?) +
 
2372                     (SELECT COUNT(*) FROM orderitems WHERE price_factor_id = ?)) = 0 AS orphaned
 
2373                  FROM price_factors WHERE id = ?|;
 
2375   ($form->{description}, $form->{factor}, $form->{orphaned}) = selectrow_query($form, $dbh, $query, (conv_i($form->{id})) x 4);
 
2377   $main::lxdebug->leave_sub();
 
2380 sub delete_price_factor {
 
2381   $main::lxdebug->enter_sub();
 
2383   my ($self, $myconfig, $form) = @_;
 
2385   # connect to database
 
2386   my $dbh = $form->get_standard_dbh($myconfig);
 
2388   do_query($form, $dbh, qq|DELETE FROM price_factors WHERE id = ?|, conv_i($form->{id}));
 
2391   $main::lxdebug->leave_sub();
 
2394 sub save_warehouse {
 
2395   $main::lxdebug->enter_sub();
 
2397   my ($self, $myconfig, $form) = @_;
 
2399   # connect to database
 
2400   my $dbh = $form->get_standard_dbh($myconfig);
 
2402   my ($query, @values, $sth);
 
2405     $query        = qq|SELECT nextval('id')|;
 
2406     ($form->{id}) = selectrow_query($form, $dbh, $query);
 
2408     $query        = qq|INSERT INTO warehouse (id, sortkey) VALUES (?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM warehouse))|;
 
2409     do_query($form, $dbh, $query, $form->{id});
 
2412   do_query($form, $dbh, qq|UPDATE warehouse SET description = ?, invalid = ? WHERE id = ?|,
 
2413            $form->{description}, $form->{invalid} ? 't' : 'f', conv_i($form->{id}));
 
2415   if (0 < $form->{number_of_new_bins}) {
 
2416     $query = qq|INSERT INTO bin (warehouse_id, description) VALUES (?, ?)|;
 
2417     $sth   = prepare_query($form, $dbh, $query);
 
2419     foreach my $i (1..$form->{number_of_new_bins}) {
 
2420       do_statement($form, $sth, $query, conv_i($form->{id}), "$form->{prefix}${i}");
 
2428   $main::lxdebug->leave_sub();
 
2432   $main::lxdebug->enter_sub();
 
2434   my ($self, $myconfig, $form) = @_;
 
2436   # connect to database
 
2437   my $dbh = $form->get_standard_dbh($myconfig);
 
2439   my ($query, @values, $commit_necessary, $sth);
 
2441   @values = map { $form->{"id_${_}"} } grep { $form->{"delete_${_}"} } (1..$form->{rowcount});
 
2444     $query = qq|DELETE FROM bin WHERE id IN (| . join(', ', ('?') x scalar(@values)) . qq|)|;
 
2445     do_query($form, $dbh, $query, @values);
 
2447     $commit_necessary = 1;
 
2450   $query = qq|UPDATE bin SET description = ? WHERE id = ?|;
 
2451   $sth   = prepare_query($form, $dbh, $query);
 
2453   foreach my $row (1..$form->{rowcount}) {
 
2454     next if ($form->{"delete_${row}"});
 
2456     do_statement($form, $sth, $query, $form->{"description_${row}"}, conv_i($form->{"id_${row}"}));
 
2458     $commit_necessary = 1;
 
2463   $dbh->commit() if ($commit_necessary);
 
2465   $main::lxdebug->leave_sub();
 
2468 sub delete_warehouse {
 
2469   $main::lxdebug->enter_sub();
 
2471   my ($self, $myconfig, $form) = @_;
 
2473   # connect to database
 
2474   my $dbh = $form->get_standard_dbh($myconfig);
 
2476   my $id      = conv_i($form->{id});
 
2477   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|;
 
2478   my ($count) = selectrow_query($form, $dbh, $query, $id);
 
2481     $main::lxdebug->leave_sub();
 
2485   do_query($form, $dbh, qq|DELETE FROM bin       WHERE warehouse_id = ?|, conv_i($form->{id}));
 
2486   do_query($form, $dbh, qq|DELETE FROM warehouse WHERE id           = ?|, conv_i($form->{id}));
 
2490   $main::lxdebug->leave_sub();
 
2495 sub get_all_warehouses {
 
2496   $main::lxdebug->enter_sub();
 
2498   my ($self, $myconfig, $form) = @_;
 
2500   # connect to database
 
2501   my $dbh = $form->get_standard_dbh($myconfig);
 
2503   my $query = qq|SELECT w.id, w.description, w.invalid,
 
2504                    (SELECT COUNT(b.description) FROM bin b WHERE b.warehouse_id = w.id) AS number_of_bins
 
2506                  ORDER BY w.sortkey|;
 
2508   $form->{WAREHOUSES} = selectall_hashref_query($form, $dbh, $query);
 
2510   $main::lxdebug->leave_sub();
 
2514   $main::lxdebug->enter_sub();
 
2516   my ($self, $myconfig, $form) = @_;
 
2518   # connect to database
 
2519   my $dbh = $form->get_standard_dbh($myconfig);
 
2521   my $id    = conv_i($form->{id});
 
2522   my $query = qq|SELECT w.description, w.invalid
 
2526   my $ref   = selectfirst_hashref_query($form, $dbh, $query, $id);
 
2528   map { $form->{$_} = $ref->{$_} } keys %{ $ref };
 
2530   $query = qq|SELECT b.*, EXISTS
 
2531                 (SELECT i.warehouse_id
 
2533                  WHERE i.bin_id = b.id
 
2537               WHERE b.warehouse_id = ?|;
 
2539   $form->{BINS} = selectall_hashref_query($form, $dbh, $query, conv_i($form->{id}));
 
2541   $main::lxdebug->leave_sub();