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 #======================================================================
 
  43 use List::MoreUtils qw(any);
 
  49 use SL::GenericTranslations;
 
  54   $main::lxdebug->enter_sub();
 
  56   my ($self, $myconfig, $form) = @_;
 
  59   my $chart_obj = SL::DB::Manager::Chart->find_by(id => $form->{id}) || die "Can't open chart";
 
  62   my $dbh = $form->dbconnect($myconfig);
 
  64     SELECT c.accno, c.description, c.charttype, c.category,
 
  65       c.link, c.pos_bilanz, c.pos_eur, c.pos_er, c.new_chart_id, c.valid_from,
 
  66       c.pos_bwa, datevautomatik,
 
  67       tk.taxkey_id, tk.pos_ustva, tk.tax_id,
 
  68       tk.tax_id || '--' || tk.taxkey_id AS tax, tk.startdate
 
  71     ON (c.id=tk.chart_id AND tk.id =
 
  72       (SELECT id FROM taxkeys
 
  73        WHERE taxkeys.chart_id = c.id AND startdate <= current_date
 
  74        ORDER BY startdate DESC LIMIT 1))
 
  79   $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
 
  80   my $sth = $dbh->prepare($query);
 
  81   $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
  83   my $ref = $sth->fetchrow_hashref("NAME_lc");
 
  85   foreach my $key (keys %$ref) {
 
  86     $form->{"$key"} = $ref->{"$key"};
 
  91   # get default accounts
 
  92   $query = qq|SELECT inventory_accno_id, income_accno_id, expense_accno_id
 
  94   $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
 
  95   $sth = $dbh->prepare($query);
 
  96   $sth->execute || $form->dberror($query);
 
  98   $ref = $sth->fetchrow_hashref("NAME_lc");
 
 100   map { $form->{$_} = $ref->{$_} } keys %{ $ref };
 
 106   # get taxkeys and description
 
 110       (SELECT accno FROM chart WHERE id=tax.chart_id) AS chart_accno,
 
 112       id||'--'||taxkey AS tax,
 
 115     FROM tax ORDER BY taxkey
 
 117   $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
 
 118   $sth = $dbh->prepare($query);
 
 119   $sth->execute || $form->dberror($query);
 
 121   $form->{TAXKEY} = [];
 
 123   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 124     push @{ $form->{TAXKEY} }, $ref;
 
 130     $query = qq|SELECT id, accno,description
 
 134     $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
 
 135     $sth = $dbh->prepare($query);
 
 136     $sth->execute($form->{link}) || $form->dberror($query . " ($form->{link})");
 
 138     $form->{NEWACCOUNT} = [];
 
 139     while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 140       push @{ $form->{NEWACCOUNT} }, $ref;
 
 145     # get the taxkeys of account
 
 159       LEFT JOIN   tax t ON (t.id = tk.tax_id)
 
 160       LEFT JOIN chart c ON (c.id = t.chart_id)
 
 162       WHERE tk.chart_id = ?
 
 163       ORDER BY startdate DESC
 
 165     $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
 
 166     $sth = $dbh->prepare($query);
 
 168     $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
 170     $form->{ACCOUNT_TAXKEYS} = [];
 
 172     while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 173       push @{ $form->{ACCOUNT_TAXKEYS} }, $ref;
 
 180   # check if there any transactions for this chart
 
 181   $form->{orphaned} = $chart_obj->has_transaction ? 0 : 1;
 
 183   # check if new account is active
 
 184   $form->{new_chart_valid} = 0;
 
 185   if ($form->{new_chart_id}) {
 
 186     $query = qq|SELECT current_date-valid_from FROM chart
 
 188     $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
 
 189     my ($count) = selectrow_query($form, $dbh, $query, $form->{id});
 
 191       $form->{new_chart_valid} = 1;
 
 198   $main::lxdebug->leave_sub();
 
 202   $main::lxdebug->enter_sub();
 
 204   # TODO: it should be forbidden to change an account to a heading if there
 
 205   # have been bookings to this account in the past
 
 207   my ($self, $myconfig, $form) = @_;
 
 209   # connect to database, turn off AutoCommit
 
 210   my $dbh = $form->dbconnect_noauto($myconfig);
 
 212   for (qw(AR_include_in_dropdown AP_include_in_dropdown summary_account)) {
 
 213     $form->{$form->{$_}} = $form->{$_} if $form->{$_};
 
 216   # sanity check, can't have AR with AR_...
 
 217   if ($form->{AR} || $form->{AP} || $form->{IC}) {
 
 218     if (any { $form->{$_} } 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)) {
 
 219       $form->error($::locale->text('It is not allowed that a summary account occurs in a drop-down menu!'));
 
 224   foreach my $item ($form->{AR},            $form->{AR_amount},
 
 225                     $form->{AR_tax},        $form->{AR_paid},
 
 226                     $form->{AP},            $form->{AP_amount},
 
 227                     $form->{AP_tax},        $form->{AP_paid},
 
 228                     $form->{IC},            $form->{IC_sale},
 
 229                     $form->{IC_cogs},       $form->{IC_taxpart},
 
 230                     $form->{IC_income},     $form->{IC_expense},
 
 231                     $form->{IC_taxservice}
 
 233     $form->{link} .= "${item}:" if ($item);
 
 237   # strip blanks from accno
 
 238   map { $form->{$_} =~ s/ //g; } qw(accno);
 
 242   if ($form->{id} eq "NULL") {
 
 251   my @values = ($form->{accno});
 
 254     $query .= ' AND NOT id = ?';
 
 255     push(@values, $form->{id});
 
 258   my ($accno) = selectrow_query($form, $dbh, $query, @values);
 
 261     $form->error($::locale->text('Account number not unique!'));
 
 265   if (!$form->{id} || $form->{id} eq "") {
 
 266     $query = qq|SELECT nextval('id')|;
 
 267     ($form->{"id"}) = selectrow_query($form, $dbh, $query);
 
 268     $query = qq|INSERT INTO chart (id, accno, link) VALUES (?, ?, ?)|;
 
 269     do_query($form, $dbh, $query, $form->{"id"}, $form->{"accno"}, '');
 
 277     # if charttype is heading make sure certain values are empty
 
 278     # specifically, if charttype is changed from an existing account, empty the
 
 279     # fields unnecessary for headings, so that e.g. heading doesn't appear in
 
 280     # drop-down menues due to still having a valid "link" entry
 
 282     if ( $form->{charttype} eq 'H' ) {
 
 284       $form->{pos_bwa} = '';
 
 285       $form->{pos_bilanz} = '';
 
 286       $form->{pos_eur} = '';
 
 287       $form->{new_chart_id} = '';
 
 288       $form->{valid_from} = '';
 
 291     $query = qq|UPDATE chart SET
 
 308                   $form->{description},
 
 312                   conv_i($form->{pos_bwa}),
 
 313                   conv_i($form->{pos_bilanz}),
 
 314                   conv_i($form->{pos_eur}),
 
 315                   conv_i($form->{pos_er}),
 
 316                   conv_i($form->{new_chart_id}),
 
 317                   conv_date($form->{valid_from}),
 
 318                   ($form->{datevautomatik} eq 'T') ? 'true':'false',
 
 325   do_query($form, $dbh, $query, @values);
 
 331   my $MAX_TRIES = 10; # Maximum count of taxkeys in form
 
 335   for $tk_count (0 .. $MAX_TRIES) {
 
 339     # Check if the account already exists, else cancel
 
 341     print(STDERR "Keine Taxkeys weil ID =: $form->{id}\n");
 
 343     last READTAXKEYS if ( $form->{'id'} == 0);
 
 345     # check if there is a startdate
 
 346     if ( $form->{"taxkey_startdate_$tk_count"} eq '' ) {
 
 351     # Add valid taxkeys into the array
 
 354         id        => ($form->{"taxkey_id_$tk_count"} eq 'NEW') ? conv_i('') : conv_i($form->{"taxkey_id_$tk_count"}),
 
 355         tax_id    => conv_i($form->{"taxkey_tax_$tk_count"}),
 
 356         startdate => conv_date($form->{"taxkey_startdate_$tk_count"}),
 
 357         chart_id  => conv_i($form->{"id"}),
 
 358         pos_ustva => conv_i($form->{"taxkey_pos_ustva_$tk_count"}),
 
 359         delete    => ( $form->{"taxkey_del_$tk_count"} eq 'delete' ) ? '1' : '',
 
 366   for my $j (0 .. $#taxkeys){
 
 367     if ( defined $taxkeys[$j]{'id'} ){
 
 370       if ($taxkeys[$j]{'delete'}){
 
 372           DELETE FROM taxkeys WHERE id = ?
 
 375         @values = ($taxkeys[$j]{'id'});
 
 377         do_query($form, $dbh, $query, @values);
 
 386         SET taxkey_id = (SELECT taxkey FROM tax WHERE tax.id = ?),
 
 394         $taxkeys[$j]{'tax_id'},
 
 395         $taxkeys[$j]{'chart_id'},
 
 396         $taxkeys[$j]{'tax_id'},
 
 397         $taxkeys[$j]{'pos_ustva'},
 
 398         $taxkeys[$j]{'startdate'},
 
 401       do_query($form, $dbh, $query, @values);
 
 407         INSERT INTO taxkeys (
 
 414         VALUES ((SELECT taxkey FROM tax WHERE tax.id = ?), ?, ?, ?, ?)
 
 417         $taxkeys[$j]{'tax_id'},
 
 418         $taxkeys[$j]{'chart_id'},
 
 419         $taxkeys[$j]{'tax_id'},
 
 420         $taxkeys[$j]{'pos_ustva'},
 
 421         $taxkeys[$j]{'startdate'},
 
 424       do_query($form, $dbh, $query, @values);
 
 429   # Update chart.taxkey_id to the latest from taxkeys for this chart.
 
 435       WHERE taxkeys.chart_id = chart.id
 
 436       ORDER BY startdate DESC
 
 442   do_query($form, $dbh, $query, $form->{id});
 
 445   my $rc = $dbh->commit;
 
 448   $main::lxdebug->leave_sub();
 
 454   $main::lxdebug->enter_sub();
 
 456   my ($self, $myconfig, $form) = @_;
 
 458   # connect to database, turn off AutoCommit
 
 459   my $dbh = $form->dbconnect_noauto($myconfig);
 
 461   my $query = qq|SELECT count(*) FROM acc_trans a
 
 462                  WHERE a.chart_id = ?|;
 
 463   my ($count) = selectrow_query($form, $dbh, $query, $form->{id});
 
 467     $main::lxdebug->leave_sub();
 
 471   # set inventory_accno_id, income_accno_id, expense_accno_id to defaults
 
 472   foreach my $type (qw(inventory income expense)) {
 
 475       qq|SET ${type}_accno_id = (SELECT ${type}_accno_id FROM defaults) | .
 
 476       qq|WHERE ${type}_accno_id = ?|;
 
 477     do_query($form, $dbh, $query, $form->{id});
 
 480   $query = qq|DELETE FROM tax
 
 482   do_query($form, $dbh, $query, $form->{id});
 
 484   # delete account taxkeys
 
 485   $query = qq|DELETE FROM taxkeys
 
 487   do_query($form, $dbh, $query, $form->{id});
 
 489   # delete chart of account record
 
 490   # last step delete chart, because we have a constraint
 
 492   $query = qq|DELETE FROM chart
 
 494   do_query($form, $dbh, $query, $form->{id});
 
 496   # commit and redirect
 
 497   my $rc = $dbh->commit;
 
 500   $main::lxdebug->leave_sub();
 
 506   $main::lxdebug->enter_sub();
 
 508   my ($self, $myconfig, $form) = @_;
 
 510   # connect to database
 
 511   my $dbh = $form->dbconnect($myconfig);
 
 513   my $query = qq|SELECT id, lead
 
 517   my $sth = $dbh->prepare($query);
 
 518   $sth->execute || $form->dberror($query);
 
 520   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 521     push @{ $form->{ALL} }, $ref;
 
 527   $main::lxdebug->leave_sub();
 
 531   $main::lxdebug->enter_sub();
 
 533   my ($self, $myconfig, $form) = @_;
 
 535   # connect to database
 
 536   my $dbh = $form->dbconnect($myconfig);
 
 539     qq|SELECT l.id, l.lead | .
 
 542   my $sth = $dbh->prepare($query);
 
 543   $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
 545   my $ref = $sth->fetchrow_hashref("NAME_lc");
 
 547   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
 553   $main::lxdebug->leave_sub();
 
 557   $main::lxdebug->enter_sub();
 
 559   my ($self, $myconfig, $form) = @_;
 
 562   # connect to database
 
 563   my $dbh = $form->dbconnect($myconfig);
 
 565   my @values = ($form->{description});
 
 566   # id is the old record
 
 568     $query = qq|UPDATE leads SET
 
 571     push(@values, $form->{id});
 
 573     $query = qq|INSERT INTO leads
 
 577   do_query($form, $dbh, $query, @values);
 
 581   $main::lxdebug->leave_sub();
 
 585   $main::lxdebug->enter_sub();
 
 587   my ($self, $myconfig, $form) = @_;
 
 590   # connect to database
 
 591   my $dbh = $form->dbconnect($myconfig);
 
 593   $query = qq|DELETE FROM leads
 
 595   do_query($form, $dbh, $query, $form->{id});
 
 599   $main::lxdebug->leave_sub();
 
 603   $main::lxdebug->enter_sub();
 
 605   my ($self, $myconfig, $form, $return_list) = @_;
 
 607   # connect to database
 
 608   my $dbh = $form->dbconnect($myconfig);
 
 611     "SELECT id, description, template_code, article_code, " .
 
 612     "  output_numberformat, output_dateformat, output_longdates " .
 
 613     "FROM language ORDER BY description";
 
 615   my $sth = $dbh->prepare($query);
 
 616   $sth->execute || $form->dberror($query);
 
 620   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 621     push(@{ $ary }, $ref);
 
 627   $main::lxdebug->leave_sub();
 
 637   $main::lxdebug->enter_sub();
 
 639   my ($self, $myconfig, $form) = @_;
 
 641   # connect to database
 
 642   my $dbh = $form->dbconnect($myconfig);
 
 645     "SELECT description, template_code, article_code, " .
 
 646     "  output_numberformat, output_dateformat, output_longdates " .
 
 647     "FROM language WHERE id = ?";
 
 648   my $sth = $dbh->prepare($query);
 
 649   $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
 
 651   my $ref = $sth->fetchrow_hashref("NAME_lc");
 
 653   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
 659   $main::lxdebug->leave_sub();
 
 662 sub get_language_details {
 
 663   $main::lxdebug->enter_sub();
 
 665   my ($self, $myconfig, $form, $id) = @_;
 
 667   # connect to database
 
 668   my $dbh = $form->dbconnect($myconfig);
 
 671     "SELECT template_code, " .
 
 672     "  output_numberformat, output_dateformat, output_longdates " .
 
 673     "FROM language WHERE id = ?";
 
 674   my @res = selectrow_query($form, $dbh, $query, $id);
 
 677   $main::lxdebug->leave_sub();
 
 683   $main::lxdebug->enter_sub();
 
 685   my ($self, $myconfig, $form) = @_;
 
 687   # connect to database
 
 688   my $dbh = $form->dbconnect($myconfig);
 
 689   my (@values, $query);
 
 691   map({ push(@values, $form->{$_}); }
 
 692       qw(description template_code article_code
 
 693          output_numberformat output_dateformat output_longdates));
 
 695   # id is the old record
 
 698       "UPDATE language SET " .
 
 699       "  description = ?, template_code = ?, article_code = ?, " .
 
 700       "  output_numberformat = ?, output_dateformat = ?, " .
 
 701       "  output_longdates = ? " .
 
 703     push(@values, $form->{id});
 
 706       "INSERT INTO language (" .
 
 707       "  description, template_code, article_code, " .
 
 708       "  output_numberformat, output_dateformat, output_longdates" .
 
 709       ") VALUES (?, ?, ?, ?, ?, ?)";
 
 711   do_query($form, $dbh, $query, @values);
 
 715   $main::lxdebug->leave_sub();
 
 718 sub delete_language {
 
 719   $main::lxdebug->enter_sub();
 
 721   my ($self, $myconfig, $form) = @_;
 
 724   # connect to database
 
 725   my $dbh = $form->dbconnect_noauto($myconfig);
 
 727   foreach my $table (qw(generic_translations units_language)) {
 
 728     $query = qq|DELETE FROM $table WHERE language_id = ?|;
 
 729     do_query($form, $dbh, $query, $form->{"id"});
 
 732   $query = "DELETE FROM language WHERE id = ?";
 
 733   do_query($form, $dbh, $query, $form->{"id"});
 
 738   $main::lxdebug->leave_sub();
 
 741 sub prepare_template_filename {
 
 742   $main::lxdebug->enter_sub();
 
 744   my ($self, $myconfig, $form) = @_;
 
 746   my ($filename, $display_filename);
 
 748   if ($form->{type} eq "stylesheet") {
 
 749     $filename = "css/$myconfig->{stylesheet}";
 
 750     $display_filename = $myconfig->{stylesheet};
 
 753     $filename = $form->{formname};
 
 755     if ($form->{language}) {
 
 756       my ($id, $template_code) = split(/--/, $form->{language});
 
 757       $filename .= "_${template_code}";
 
 760     if ($form->{printer}) {
 
 761       my ($id, $template_code) = split(/--/, $form->{printer});
 
 762       $filename .= "_${template_code}";
 
 765     $filename .= "." . ($form->{format} eq "html" ? "html" : "tex");
 
 766     if ($form->{"formname"} =~ m|\.\.| || $form->{"formname"} =~ m|^/|) {
 
 767       $filename =~ s|.*/||;
 
 769     $display_filename = $filename;
 
 770     $filename = SL::DB::Default->get->templates . "/$filename";
 
 773   $main::lxdebug->leave_sub();
 
 775   return ($filename, $display_filename);
 
 780   $main::lxdebug->enter_sub();
 
 782   my ($self, $filename) = @_;
 
 784   my ($content, $lines) = ("", 0);
 
 788   if (open(TEMPLATE, $filename)) {
 
 796   $content = Encode::decode('utf-8-strict', $content);
 
 798   $main::lxdebug->leave_sub();
 
 800   return ($content, $lines);
 
 804   $main::lxdebug->enter_sub();
 
 806   my ($self, $filename, $content) = @_;
 
 812   if (open(TEMPLATE, ">", $filename)) {
 
 813     $content = Encode::encode('utf-8-strict', $content);
 
 814     $content =~ s/\r\n/\n/g;
 
 815     print(TEMPLATE $content);
 
 821   $main::lxdebug->leave_sub();
 
 826 sub save_preferences {
 
 827   $main::lxdebug->enter_sub();
 
 829   my ($self, $form) = @_;
 
 831   my $employee = SL::DB::Manager::Employee->find_by(login => $::myconfig{login});
 
 832   $employee->update_attributes(name => $form->{name});
 
 834   my $user = SL::DB::Manager::AuthUser->find_by(login => $::myconfig{login});
 
 835   $user->update_attributes(
 
 837       %{ $user->config_values },
 
 838       map { ($_ => $form->{$_}) } SL::DB::AuthUser::CONFIG_VARS(),
 
 841   $main::lxdebug->leave_sub();
 
 847   $main::lxdebug->enter_sub();
 
 852   my $myconfig = \%main::myconfig;
 
 853   my $form     = $main::form;
 
 855   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
 
 857   my $defaults = selectfirst_hashref_query($form, $dbh, qq|SELECT * FROM defaults|) || {};
 
 859   $defaults->{weightunit} ||= 'kg';
 
 861   $main::lxdebug->leave_sub();
 
 867   $main::lxdebug->enter_sub();
 
 869   my ($self, $myconfig, $form) = @_;
 
 871   my $dbh = $form->dbconnect($myconfig);
 
 873   my $query = qq|SELECT closedto, max_future_booking_interval, revtrans FROM defaults|;
 
 874   my $sth   = $dbh->prepare($query);
 
 875   $sth->execute || $form->dberror($query);
 
 877   ($form->{closedto}, $form->{max_future_booking_interval}, $form->{revtrans}) = $sth->fetchrow_array;
 
 883   $main::lxdebug->leave_sub();
 
 887   $main::lxdebug->enter_sub();
 
 889   my ($self, $myconfig, $form) = @_;
 
 891   my $dbh = $form->dbconnect($myconfig);
 
 893   my ($query, @values);
 
 895   # is currently NEVER trueish (no more hidden revtrans in $form)
 
 896   # if ($form->{revtrans}) {
 
 897   #   $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '1'|;
 
 898   # -> therefore you can only set this to false (which is already the default)
 
 899   # and this flag is currently only checked in gl.pl. TOOD Can probably be removed
 
 901     $query = qq|UPDATE defaults SET closedto = ?, max_future_booking_interval = ?, revtrans = '0'|;
 
 902     @values = (conv_date($form->{closedto}), conv_i($form->{max_future_booking_interval}));
 
 904   # set close in defaults
 
 905   do_query($form, $dbh, $query, @values);
 
 909   $main::lxdebug->leave_sub();
 
 913   my ($self, $units, $unit_name, $factor) = @_;
 
 915   $factor = 1 unless ($factor);
 
 917   my $unit = $units->{$unit_name};
 
 919   if (!defined($unit) || !$unit->{"base_unit"} ||
 
 920       ($unit_name eq $unit->{"base_unit"})) {
 
 921     return ($unit_name, $factor);
 
 924   return AM->get_base_unit($units, $unit->{"base_unit"}, $factor * $unit->{"factor"});
 
 928   $main::lxdebug->enter_sub();
 
 930   my ($self, $myconfig, $form, $prefix) = @_;
 
 933   my $dbh = $form->get_standard_dbh;
 
 935   my $query = "SELECT *, base_unit AS original_base_unit FROM units";
 
 937   my $sth = prepare_execute_query($form, $dbh, $query);
 
 940   while (my $ref = $sth->fetchrow_hashref()) {
 
 941     $units->{$ref->{"name"}} = $ref;
 
 945   my $query_lang = "SELECT id, template_code FROM language ORDER BY description";
 
 946   $sth = $dbh->prepare($query_lang);
 
 947   $sth->execute() || $form->dberror($query_lang);
 
 949   while (my $ref = $sth->fetchrow_hashref()) {
 
 950     push(@languages, $ref);
 
 954   $query_lang = "SELECT ul.localized, ul.localized_plural, l.id, l.template_code " .
 
 955     "FROM units_language ul " .
 
 956     "LEFT JOIN language l ON ul.language_id = l.id " .
 
 958   $sth = $dbh->prepare($query_lang);
 
 960   foreach my $unit (values(%{$units})) {
 
 961     ($unit->{"${prefix}base_unit"}, $unit->{"${prefix}factor"}) = AM->get_base_unit($units, $unit->{"name"});
 
 963     $unit->{"LANGUAGES"} = {};
 
 964     foreach my $lang (@languages) {
 
 965       $unit->{"LANGUAGES"}->{$lang->{"template_code"}} = { "template_code" => $lang->{"template_code"} };
 
 968     $sth->execute($unit->{"name"}) || $form->dberror($query_lang . " (" . $unit->{"name"} . ")");
 
 969     while (my $ref = $sth->fetchrow_hashref()) {
 
 970       map({ $unit->{"LANGUAGES"}->{$ref->{"template_code"}}->{$_} = $ref->{$_} } keys(%{$ref}));
 
 975   $main::lxdebug->leave_sub();
 
 980 sub retrieve_all_units {
 
 981   $main::lxdebug->enter_sub();
 
 985   if (!$::request->{cache}{all_units}) {
 
 986     $::request->{cache}{all_units} = $self->retrieve_units(\%main::myconfig, $main::form);
 
 989   $main::lxdebug->leave_sub();
 
 991   return $::request->{cache}{all_units};
 
 995 sub translate_units {
 
 996   $main::lxdebug->enter_sub();
 
 998   my ($self, $form, $template_code, $unit, $amount) = @_;
 
1000   my $units = $self->retrieve_units(\%main::myconfig, $form);
 
1002   my $h = $units->{$unit}->{"LANGUAGES"}->{$template_code};
 
1003   my $new_unit = $unit;
 
1005     if (($amount != 1) && $h->{"localized_plural"}) {
 
1006       $new_unit = $h->{"localized_plural"};
 
1007     } elsif ($h->{"localized"}) {
 
1008       $new_unit = $h->{"localized"};
 
1012   $main::lxdebug->leave_sub();
 
1018   $main::lxdebug->enter_sub();
 
1020   my ($self, $myconfig, $form, $units) = @_;
 
1022   my $dbh = $form->dbconnect($myconfig);
 
1024   map({ $_->{"in_use"} = 0; } values(%{$units}));
 
1026   foreach my $unit (values(%{$units})) {
 
1027     my $base_unit = $unit->{"original_base_unit"};
 
1028     while ($base_unit) {
 
1029       $units->{$base_unit}->{"in_use"} = 1;
 
1030       $units->{$base_unit}->{"DEPENDING_UNITS"} = [] unless ($units->{$base_unit}->{"DEPENDING_UNITS"});
 
1031       push(@{$units->{$base_unit}->{"DEPENDING_UNITS"}}, $unit->{"name"});
 
1032       $base_unit = $units->{$base_unit}->{"original_base_unit"};
 
1036   foreach my $unit (values(%{$units})) {
 
1037     map({ $_ = $dbh->quote($_); } @{$unit->{"DEPENDING_UNITS"}});
 
1039     foreach my $table (qw(parts invoice orderitems)) {
 
1040       my $query = "SELECT COUNT(*) FROM $table WHERE unit ";
 
1042       if (0 == scalar(@{$unit->{"DEPENDING_UNITS"}})) {
 
1043         $query .= "= " . $dbh->quote($unit->{"name"});
 
1045         $query .= "IN (" . $dbh->quote($unit->{"name"}) . "," .
 
1046           join(",", map({ $dbh->quote($_) } @{$unit->{"DEPENDING_UNITS"}})) . ")";
 
1049       my ($count) = $dbh->selectrow_array($query);
 
1050       $form->dberror($query) if ($dbh->err);
 
1053         $unit->{"in_use"} = 1;
 
1061   $main::lxdebug->leave_sub();
 
1064 sub convertible_units {
 
1065   $main::lxdebug->enter_sub();
 
1069   my $filter_unit = shift;
 
1070   my $not_smaller = shift;
 
1072   my $conv_units = [];
 
1074   $filter_unit = $units->{$filter_unit};
 
1076   foreach my $name (sort { lc $a cmp lc $b } keys %{ $units }) {
 
1077     my $unit = $units->{$name};
 
1079     if (($unit->{base_unit} eq $filter_unit->{base_unit}) &&
 
1080         (!$not_smaller || ($unit->{factor} >= $filter_unit->{factor}))) {
 
1081       push @{$conv_units}, $unit;
 
1085   my @sorted = sort { $b->{factor} <=> $a->{factor} } @{ $conv_units };
 
1087   $main::lxdebug->leave_sub();
 
1092 # if $a is translatable to $b, return the factor between them.
 
1095   $main::lxdebug->enter_sub(2);
 
1096   my ($this, $a, $b, $all_units) = @_;
 
1099     $all_units = $this->retrieve_all_units;
 
1102   $main::lxdebug->leave_sub(2) and return 0 unless $a && $b;
 
1103   $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a} && $all_units->{$b};
 
1104   $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a}{base_unit} eq $all_units->{$b}{base_unit};
 
1105   $main::lxdebug->leave_sub(2) and return $all_units->{$a}{factor} / $all_units->{$b}{factor};
 
1108 sub unit_select_data {
 
1109   $main::lxdebug->enter_sub();
 
1111   my ($self, $units, $selected, $empty_entry, $convertible_into) = @_;
 
1116     push(@{$select}, { "name" => "", "base_unit" => "", "factor" => "", "selected" => "" });
 
1119   foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
 
1120     if (!$convertible_into ||
 
1121         ($units->{$convertible_into} &&
 
1122          ($units->{$convertible_into}->{base_unit} eq $units->{$unit}->{base_unit}))) {
 
1123       push @{$select}, { "name"      => $unit,
 
1124                          "base_unit" => $units->{$unit}->{"base_unit"},
 
1125                          "factor"    => $units->{$unit}->{"factor"},
 
1126                          "selected"  => ($unit eq $selected) ? "selected" : "" };
 
1130   $main::lxdebug->leave_sub();
 
1135 sub unit_select_html {
 
1136   $main::lxdebug->enter_sub();
 
1138   my ($self, $units, $name, $selected, $convertible_into) = @_;
 
1140   my $select = "<select name=${name}>";
 
1142   foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
 
1143     if (!$convertible_into ||
 
1144         ($units->{$convertible_into} &&
 
1145          ($units->{$convertible_into}->{"base_unit"} eq $units->{$unit}->{"base_unit"}))) {
 
1146       $select .= "<option" . (($unit eq $selected) ? " selected" : "") . ">${unit}</option>";
 
1149   $select .= "</select>";
 
1151   $main::lxdebug->leave_sub();
 
1157   $main::lxdebug->enter_sub();
 
1161   my $units = $self->retrieve_all_units();
 
1166   while (2 <= scalar(@_)) {
 
1167     my $qty  = shift(@_);
 
1168     my $unit = $units->{shift(@_)};
 
1170     croak "No unit defined with name $unit" if (!defined $unit);
 
1173       $base_unit = $unit->{base_unit};
 
1174     } elsif ($base_unit ne $unit->{base_unit}) {
 
1175       croak "Adding values with incompatible base units $base_unit/$unit->{base_unit}";
 
1178     $sum += $qty * $unit->{factor};
 
1181   $main::lxdebug->leave_sub();
 
1187   $main::lxdebug->enter_sub();
 
1189   my ($self, $myconfig, $form, $name, $base_unit, $factor, $languages) = @_;
 
1191   my $dbh = $form->dbconnect_noauto($myconfig);
 
1193   my $query = qq|SELECT COALESCE(MAX(sortkey), 0) + 1 FROM units|;
 
1194   my ($sortkey) = selectrow_query($form, $dbh, $query);
 
1196   $query = "INSERT INTO units (name, base_unit, factor, sortkey) " .
 
1197     "VALUES (?, ?, ?, ?)";
 
1198   do_query($form, $dbh, $query, $name, $base_unit, $factor, $sortkey);
 
1201     $query = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
 
1202     my $sth = $dbh->prepare($query);
 
1203     foreach my $lang (@{$languages}) {
 
1204       my @values = ($name, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
 
1205       $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
 
1213   $main::lxdebug->leave_sub();
 
1217   $main::lxdebug->enter_sub();
 
1219   my ($self, $myconfig, $form, $units, $delete_units) = @_;
 
1221   my $dbh = $form->dbconnect_noauto($myconfig);
 
1223   my ($base_unit, $unit, $sth, $query);
 
1225   $query = "DELETE FROM units_language";
 
1226   $dbh->do($query) || $form->dberror($query);
 
1228   if ($delete_units && (0 != scalar(@{$delete_units}))) {
 
1229     $query = "DELETE FROM units WHERE name IN (";
 
1230     map({ $query .= "?," } @{$delete_units});
 
1231     substr($query, -1, 1) = ")";
 
1232     $dbh->do($query, undef, @{$delete_units}) ||
 
1233       $form->dberror($query . " (" . join(", ", @{$delete_units}) . ")");
 
1236   $query = "UPDATE units SET name = ?, base_unit = ?, factor = ? WHERE name = ?";
 
1237   $sth = $dbh->prepare($query);
 
1239   my $query_lang = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
 
1240   my $sth_lang = $dbh->prepare($query_lang);
 
1242   foreach $unit (values(%{$units})) {
 
1243     $unit->{"depth"} = 0;
 
1244     my $base_unit = $unit;
 
1245     while ($base_unit->{"base_unit"}) {
 
1247       $base_unit = $units->{$base_unit->{"base_unit"}};
 
1251   foreach $unit (sort({ $a->{"depth"} <=> $b->{"depth"} } values(%{$units}))) {
 
1252     if ($unit->{"LANGUAGES"}) {
 
1253       foreach my $lang (@{$unit->{"LANGUAGES"}}) {
 
1254         next unless ($lang->{"id"} && $lang->{"localized"});
 
1255         my @values = ($unit->{"name"}, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
 
1256         $sth_lang->execute(@values) || $form->dberror($query_lang . " (" . join(", ", @values) . ")");
 
1260     next if ($unit->{"unchanged_unit"});
 
1262     my @values = ($unit->{"name"}, $unit->{"base_unit"}, $unit->{"factor"}, $unit->{"old_name"});
 
1263     $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
 
1267   $sth_lang->finish();
 
1271   $main::lxdebug->leave_sub();
 
1275   $main::lxdebug->enter_sub();
 
1277   my ($self, $myconfig, $form) = @_;
 
1279   # connect to database
 
1280   my $dbh = $form->dbconnect($myconfig);
 
1282   my $query = qq|SELECT
 
1286                    round(t.rate * 100, 2) AS rate,
 
1287                    (SELECT accno FROM chart WHERE id = chart_id) AS taxnumber,
 
1288                    (SELECT description FROM chart WHERE id = chart_id) AS account_description,
 
1289                    (SELECT accno FROM chart WHERE id = skonto_sales_chart_id) AS skonto_chart_accno,
 
1290                    (SELECT description FROM chart WHERE id = skonto_sales_chart_id) AS skonto_chart_description,
 
1291                    (SELECT accno FROM chart WHERE id = skonto_purchase_chart_id) AS skonto_chart_purchase_accno,
 
1292                    (SELECT description FROM chart WHERE id = skonto_purchase_chart_id) AS skonto_chart_purchase_description
 
1294                  ORDER BY taxkey, rate|;
 
1296   my $sth = $dbh->prepare($query);
 
1297   $sth->execute || $form->dberror($query);
 
1300   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
1301     push @{ $form->{TAX} }, $ref;
 
1307   $main::lxdebug->leave_sub();
 
1310 sub get_tax_accounts {
 
1311   $main::lxdebug->enter_sub();
 
1313   my ($self, $myconfig, $form) = @_;
 
1315   my $dbh = $form->dbconnect($myconfig);
 
1317   # get Accounts from chart
 
1318   my $query = qq{ SELECT
 
1320                  accno || ' - ' || description AS taxaccount
 
1322                WHERE link LIKE '%_tax%'
 
1326   my $sth = $dbh->prepare($query);
 
1327   $sth->execute || $form->dberror($query);
 
1329   $form->{ACCOUNTS} = [];
 
1330   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
1331     push @{ $form->{ACCOUNTS} }, $ref;
 
1334   $form->{AR_PAID} = SL::DB::Manager::Chart->get_all(where => [ link => { like => '%AR_paid%' } ], sort_by => 'accno ASC');
 
1335   $form->{AP_PAID} = SL::DB::Manager::Chart->get_all(where => [ link => { like => '%AP_paid%' } ], sort_by => 'accno ASC');
 
1337   $form->{skontochart_value_title_sub} = sub {
 
1341       $item->{accno} .' '. $item->{description},
 
1349   $main::lxdebug->leave_sub();
 
1353   $main::lxdebug->enter_sub();
 
1355   my ($self, $myconfig, $form) = @_;
 
1357   # connect to database
 
1358   my $dbh = $form->dbconnect($myconfig);
 
1360   my $query = qq|SELECT
 
1363                    round(rate * 100, 2) AS rate,
 
1366                    (id IN (SELECT tax_id
 
1367                            FROM acc_trans)) AS tax_already_used,
 
1368                    skonto_sales_chart_id,
 
1369                    skonto_purchase_chart_id
 
1373   my $sth = $dbh->prepare($query);
 
1374   $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
1376   my $ref = $sth->fetchrow_hashref("NAME_lc");
 
1378   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
1382   # see if it is used by a taxkey
 
1383   $query = qq|SELECT count(*) FROM taxkeys
 
1384               WHERE tax_id = ? AND chart_id >0|;
 
1386   ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
 
1388   $form->{orphaned} = !$form->{orphaned};
 
1391   if (!$form->{orphaned} ) {
 
1392     $query = qq|SELECT DISTINCT c.id, c.accno
 
1394                 JOIN   tax t ON (t.id = tk.tax_id)
 
1395                 JOIN chart c ON (c.id = tk.chart_id)
 
1396                 WHERE tk.tax_id = ?|;
 
1398     $sth = $dbh->prepare($query);
 
1399     $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
1401     $form->{TAXINUSE} = [];
 
1402     while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
1403       push @{ $form->{TAXINUSE} }, $ref;
 
1411   $main::lxdebug->leave_sub();
 
1415   $main::lxdebug->enter_sub();
 
1417   my ($self, $myconfig, $form) = @_;
 
1420   # connect to database
 
1421   my $dbh = $form->get_standard_dbh($myconfig);
 
1423   $form->{rate} = $form->{rate} / 100;
 
1425   my $chart_categories = '';
 
1426   $chart_categories .= 'A' if $form->{asset};
 
1427   $chart_categories .= 'L' if $form->{liability};
 
1428   $chart_categories .= 'Q' if $form->{equity};
 
1429   $chart_categories .= 'I' if $form->{revenue};
 
1430   $chart_categories .= 'E' if $form->{expense};
 
1431   $chart_categories .= 'C' if $form->{costs};
 
1433   my @values = ($form->{taxkey}, $form->{taxdescription}, $form->{rate}, conv_i($form->{chart_id}), conv_i($form->{chart_id}), conv_i($form->{skonto_sales_chart_id}), conv_i($form->{skonto_purchase_chart_id}), $chart_categories);
 
1434   if ($form->{id} ne "") {
 
1435     $query = qq|UPDATE tax SET
 
1440                   taxnumber                = (SELECT accno FROM chart WHERE id = ? ),
 
1441                   skonto_sales_chart_id    = ?,
 
1442                   skonto_purchase_chart_id = ?,
 
1443                   chart_categories         = ?
 
1448     ($form->{id}) = selectfirst_array_query($form, $dbh, qq|SELECT nextval('id')|);
 
1449     $query = qq|INSERT INTO tax (
 
1455                   skonto_sales_chart_id,
 
1456                   skonto_purchase_chart_id,
 
1460                 VALUES (?, ?, ?, ?, (SELECT accno FROM chart WHERE id = ?), ?, ?,  ?, ?)|;
 
1462   push(@values, $form->{id});
 
1463   do_query($form, $dbh, $query, @values);
 
1465   foreach my $language_id (keys %{ $form->{translations} }) {
 
1466     GenericTranslations->save('dbh'              => $dbh,
 
1467                               'translation_type' => 'SL::DB::Tax/taxdescription',
 
1468                               'translation_id'   => $form->{id},
 
1469                               'language_id'      => $language_id,
 
1470                               'translation'      => $form->{translations}->{$language_id});
 
1475   $main::lxdebug->leave_sub();
 
1479   $main::lxdebug->enter_sub();
 
1481   my ($self, $myconfig, $form) = @_;
 
1484   # connect to database
 
1485   my $dbh = $form->get_standard_dbh($myconfig);
 
1487   $query = qq|DELETE FROM tax
 
1489   do_query($form, $dbh, $query, $form->{id});
 
1493   $main::lxdebug->leave_sub();
 
1496 sub save_price_factor {
 
1497   $main::lxdebug->enter_sub();
 
1499   my ($self, $myconfig, $form) = @_;
 
1501   # connect to database
 
1502   my $dbh = $form->get_standard_dbh($myconfig);
 
1505   my @values = ($form->{description}, conv_i($form->{factor}));
 
1508     $query = qq|UPDATE price_factors SET description = ?, factor = ? WHERE id = ?|;
 
1509     push @values, conv_i($form->{id});
 
1512     $query = qq|INSERT INTO price_factors (description, factor, sortkey) VALUES (?, ?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM price_factors))|;
 
1515   do_query($form, $dbh, $query, @values);
 
1519   $main::lxdebug->leave_sub();
 
1522 sub get_all_price_factors {
 
1523   $main::lxdebug->enter_sub();
 
1525   my ($self, $myconfig, $form) = @_;
 
1527   # connect to database
 
1528   my $dbh = $form->get_standard_dbh($myconfig);
 
1530   $form->{PRICE_FACTORS} = selectall_hashref_query($form, $dbh, qq|SELECT * FROM price_factors ORDER BY sortkey|);
 
1532   $main::lxdebug->leave_sub();
 
1535 sub get_price_factor {
 
1536   $main::lxdebug->enter_sub();
 
1538   my ($self, $myconfig, $form) = @_;
 
1540   # connect to database
 
1541   my $dbh = $form->get_standard_dbh($myconfig);
 
1543   my $query = qq|SELECT description, factor,
 
1544                    ((SELECT COUNT(*) FROM parts      WHERE price_factor_id = ?) +
 
1545                     (SELECT COUNT(*) FROM invoice    WHERE price_factor_id = ?) +
 
1546                     (SELECT COUNT(*) FROM orderitems WHERE price_factor_id = ?)) = 0 AS orphaned
 
1547                  FROM price_factors WHERE id = ?|;
 
1549   ($form->{description}, $form->{factor}, $form->{orphaned}) = selectrow_query($form, $dbh, $query, (conv_i($form->{id})) x 4);
 
1551   $main::lxdebug->leave_sub();
 
1554 sub delete_price_factor {
 
1555   $main::lxdebug->enter_sub();
 
1557   my ($self, $myconfig, $form) = @_;
 
1559   # connect to database
 
1560   my $dbh = $form->get_standard_dbh($myconfig);
 
1562   do_query($form, $dbh, qq|DELETE FROM price_factors WHERE id = ?|, conv_i($form->{id}));
 
1565   $main::lxdebug->leave_sub();
 
1568 sub save_warehouse {
 
1569   $main::lxdebug->enter_sub();
 
1571   my ($self, $myconfig, $form) = @_;
 
1573   # connect to database
 
1574   my $dbh = $form->get_standard_dbh($myconfig);
 
1576   my ($query, @values, $sth);
 
1579     $query        = qq|SELECT nextval('id')|;
 
1580     ($form->{id}) = selectrow_query($form, $dbh, $query);
 
1582     $query        = qq|INSERT INTO warehouse (id, sortkey) VALUES (?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM warehouse))|;
 
1583     do_query($form, $dbh, $query, $form->{id});
 
1586   do_query($form, $dbh, qq|UPDATE warehouse SET description = ?, invalid = ? WHERE id = ?|,
 
1587            $form->{description}, $form->{invalid} ? 't' : 'f', conv_i($form->{id}));
 
1589   if (0 < $form->{number_of_new_bins}) {
 
1590     my ($num_existing_bins) = selectfirst_array_query($form, $dbh, qq|SELECT COUNT(*) FROM bin WHERE warehouse_id = ?|, $form->{id});
 
1591     $query = qq|INSERT INTO bin (warehouse_id, description) VALUES (?, ?)|;
 
1592     $sth   = prepare_query($form, $dbh, $query);
 
1594     foreach my $i (1..$form->{number_of_new_bins}) {
 
1595       do_statement($form, $sth, $query, conv_i($form->{id}), "$form->{prefix}" . ($i + $num_existing_bins));
 
1603   $main::lxdebug->leave_sub();
 
1607   $main::lxdebug->enter_sub();
 
1609   my ($self, $myconfig, $form) = @_;
 
1611   # connect to database
 
1612   my $dbh = $form->get_standard_dbh($myconfig);
 
1614   my ($query, @values, $commit_necessary, $sth);
 
1616   @values = map { $form->{"id_${_}"} } grep { $form->{"delete_${_}"} } (1..$form->{rowcount});
 
1619     $query = qq|DELETE FROM bin WHERE id IN (| . join(', ', ('?') x scalar(@values)) . qq|)|;
 
1620     do_query($form, $dbh, $query, @values);
 
1622     $commit_necessary = 1;
 
1625   $query = qq|UPDATE bin SET description = ? WHERE id = ?|;
 
1626   $sth   = prepare_query($form, $dbh, $query);
 
1628   foreach my $row (1..$form->{rowcount}) {
 
1629     next if ($form->{"delete_${row}"});
 
1631     do_statement($form, $sth, $query, $form->{"description_${row}"}, conv_i($form->{"id_${row}"}));
 
1633     $commit_necessary = 1;
 
1638   $dbh->commit() if ($commit_necessary);
 
1640   $main::lxdebug->leave_sub();
 
1643 sub delete_warehouse {
 
1644   $main::lxdebug->enter_sub();
 
1646   my ($self, $myconfig, $form) = @_;
 
1648   # connect to database
 
1649   my $dbh = $form->get_standard_dbh($myconfig);
 
1651   my $id      = conv_i($form->{id});
 
1652   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|;
 
1653   my ($count) = selectrow_query($form, $dbh, $query, $id);
 
1656     $main::lxdebug->leave_sub();
 
1660   do_query($form, $dbh, qq|DELETE FROM bin       WHERE warehouse_id = ?|, conv_i($form->{id}));
 
1661   do_query($form, $dbh, qq|DELETE FROM warehouse WHERE id           = ?|, conv_i($form->{id}));
 
1665   $main::lxdebug->leave_sub();
 
1670 sub get_all_warehouses {
 
1671   $main::lxdebug->enter_sub();
 
1673   my ($self, $myconfig, $form) = @_;
 
1675   # connect to database
 
1676   my $dbh = $form->get_standard_dbh($myconfig);
 
1678   my $query = qq|SELECT w.id, w.description, w.invalid,
 
1679                    (SELECT COUNT(b.description) FROM bin b WHERE b.warehouse_id = w.id) AS number_of_bins
 
1681                  ORDER BY w.sortkey|;
 
1683   $form->{WAREHOUSES} = selectall_hashref_query($form, $dbh, $query);
 
1685   $main::lxdebug->leave_sub();
 
1689   $main::lxdebug->enter_sub();
 
1691   my ($self, $myconfig, $form) = @_;
 
1693   # connect to database
 
1694   my $dbh = $form->get_standard_dbh($myconfig);
 
1696   my $id    = conv_i($form->{id});
 
1697   my $query = qq|SELECT w.description, w.invalid
 
1701   my $ref   = selectfirst_hashref_query($form, $dbh, $query, $id);
 
1703   map { $form->{$_} = $ref->{$_} } keys %{ $ref };
 
1707       (   EXISTS(SELECT i.bin_id FROM inventory i WHERE i.bin_id = b.id LIMIT 1)
 
1708        OR EXISTS(SELECT p.bin_id FROM parts     p WHERE p.bin_id = b.id LIMIT 1))
 
1711     WHERE b.warehouse_id = ?
 
1714   $form->{BINS} = selectall_hashref_query($form, $dbh, $query, conv_i($form->{id}));
 
1716   $main::lxdebug->leave_sub();