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   # The old sql query was broken since at least 2006 and always returned 0
 
 185   $form->{new_chart_valid} = $chart_obj->new_chart_valid;
 
 189   $main::lxdebug->leave_sub();
 
 193   $main::lxdebug->enter_sub();
 
 195   # TODO: it should be forbidden to change an account to a heading if there
 
 196   # have been bookings to this account in the past
 
 198   my ($self, $myconfig, $form) = @_;
 
 200   # connect to database, turn off AutoCommit
 
 201   my $dbh = $form->dbconnect_noauto($myconfig);
 
 203   for (qw(AR_include_in_dropdown AP_include_in_dropdown summary_account)) {
 
 204     $form->{$form->{$_}} = $form->{$_} if $form->{$_};
 
 207   # sanity check, can't have AR with AR_...
 
 208   if ($form->{AR} || $form->{AP} || $form->{IC}) {
 
 209     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)) {
 
 210       $form->error($::locale->text('It is not allowed that a summary account occurs in a drop-down menu!'));
 
 215   foreach my $item ($form->{AR},            $form->{AR_amount},
 
 216                     $form->{AR_tax},        $form->{AR_paid},
 
 217                     $form->{AP},            $form->{AP_amount},
 
 218                     $form->{AP_tax},        $form->{AP_paid},
 
 219                     $form->{IC},            $form->{IC_sale},
 
 220                     $form->{IC_cogs},       $form->{IC_taxpart},
 
 221                     $form->{IC_income},     $form->{IC_expense},
 
 222                     $form->{IC_taxservice}
 
 224     $form->{link} .= "${item}:" if ($item);
 
 228   # strip blanks from accno
 
 229   map { $form->{$_} =~ s/ //g; } qw(accno);
 
 233   if ($form->{id} eq "NULL") {
 
 242   my @values = ($form->{accno});
 
 245     $query .= ' AND NOT id = ?';
 
 246     push(@values, $form->{id});
 
 249   my ($accno) = selectrow_query($form, $dbh, $query, @values);
 
 252     $form->error($::locale->text('Account number not unique!'));
 
 256   if (!$form->{id} || $form->{id} eq "") {
 
 257     $query = qq|SELECT nextval('id')|;
 
 258     ($form->{"id"}) = selectrow_query($form, $dbh, $query);
 
 259     $query = qq|INSERT INTO chart (id, accno, link) VALUES (?, ?, ?)|;
 
 260     do_query($form, $dbh, $query, $form->{"id"}, $form->{"accno"}, '');
 
 268     # if charttype is heading make sure certain values are empty
 
 269     # specifically, if charttype is changed from an existing account, empty the
 
 270     # fields unnecessary for headings, so that e.g. heading doesn't appear in
 
 271     # drop-down menues due to still having a valid "link" entry
 
 273     if ( $form->{charttype} eq 'H' ) {
 
 275       $form->{pos_bwa} = '';
 
 276       $form->{pos_bilanz} = '';
 
 277       $form->{pos_eur} = '';
 
 278       $form->{new_chart_id} = '';
 
 279       $form->{valid_from} = '';
 
 282     $query = qq|UPDATE chart SET
 
 299                   $form->{description},
 
 303                   conv_i($form->{pos_bwa}),
 
 304                   conv_i($form->{pos_bilanz}),
 
 305                   conv_i($form->{pos_eur}),
 
 306                   conv_i($form->{pos_er}),
 
 307                   conv_i($form->{new_chart_id}),
 
 308                   conv_date($form->{valid_from}),
 
 309                   ($form->{datevautomatik} eq 'T') ? 'true':'false',
 
 316   do_query($form, $dbh, $query, @values);
 
 322   my $MAX_TRIES = 10; # Maximum count of taxkeys in form
 
 326   for $tk_count (0 .. $MAX_TRIES) {
 
 330     # Check if the account already exists, else cancel
 
 332     print(STDERR "Keine Taxkeys weil ID =: $form->{id}\n");
 
 334     last READTAXKEYS if ( $form->{'id'} == 0);
 
 336     # check if there is a startdate
 
 337     if ( $form->{"taxkey_startdate_$tk_count"} eq '' ) {
 
 342     # Add valid taxkeys into the array
 
 345         id        => ($form->{"taxkey_id_$tk_count"} eq 'NEW') ? conv_i('') : conv_i($form->{"taxkey_id_$tk_count"}),
 
 346         tax_id    => conv_i($form->{"taxkey_tax_$tk_count"}),
 
 347         startdate => conv_date($form->{"taxkey_startdate_$tk_count"}),
 
 348         chart_id  => conv_i($form->{"id"}),
 
 349         pos_ustva => conv_i($form->{"taxkey_pos_ustva_$tk_count"}),
 
 350         delete    => ( $form->{"taxkey_del_$tk_count"} eq 'delete' ) ? '1' : '',
 
 357   for my $j (0 .. $#taxkeys){
 
 358     if ( defined $taxkeys[$j]{'id'} ){
 
 361       if ($taxkeys[$j]{'delete'}){
 
 363           DELETE FROM taxkeys WHERE id = ?
 
 366         @values = ($taxkeys[$j]{'id'});
 
 368         do_query($form, $dbh, $query, @values);
 
 377         SET taxkey_id = (SELECT taxkey FROM tax WHERE tax.id = ?),
 
 385         $taxkeys[$j]{'tax_id'},
 
 386         $taxkeys[$j]{'chart_id'},
 
 387         $taxkeys[$j]{'tax_id'},
 
 388         $taxkeys[$j]{'pos_ustva'},
 
 389         $taxkeys[$j]{'startdate'},
 
 392       do_query($form, $dbh, $query, @values);
 
 398         INSERT INTO taxkeys (
 
 405         VALUES ((SELECT taxkey FROM tax WHERE tax.id = ?), ?, ?, ?, ?)
 
 408         $taxkeys[$j]{'tax_id'},
 
 409         $taxkeys[$j]{'chart_id'},
 
 410         $taxkeys[$j]{'tax_id'},
 
 411         $taxkeys[$j]{'pos_ustva'},
 
 412         $taxkeys[$j]{'startdate'},
 
 415       do_query($form, $dbh, $query, @values);
 
 420   # Update chart.taxkey_id to the latest from taxkeys for this chart.
 
 426       WHERE taxkeys.chart_id = chart.id
 
 427       ORDER BY startdate DESC
 
 433   do_query($form, $dbh, $query, $form->{id});
 
 436   my $rc = $dbh->commit;
 
 439   $main::lxdebug->leave_sub();
 
 445   $main::lxdebug->enter_sub();
 
 447   my ($self, $myconfig, $form) = @_;
 
 449   # connect to database, turn off AutoCommit
 
 450   my $dbh = $form->dbconnect_noauto($myconfig);
 
 452   my $query = qq|SELECT count(*) FROM acc_trans a
 
 453                  WHERE a.chart_id = ?|;
 
 454   my ($count) = selectrow_query($form, $dbh, $query, $form->{id});
 
 458     $main::lxdebug->leave_sub();
 
 462   # set inventory_accno_id, income_accno_id, expense_accno_id to defaults
 
 463   foreach my $type (qw(inventory income expense)) {
 
 466       qq|SET ${type}_accno_id = (SELECT ${type}_accno_id FROM defaults) | .
 
 467       qq|WHERE ${type}_accno_id = ?|;
 
 468     do_query($form, $dbh, $query, $form->{id});
 
 471   $query = qq|DELETE FROM tax
 
 473   do_query($form, $dbh, $query, $form->{id});
 
 475   # delete account taxkeys
 
 476   $query = qq|DELETE FROM taxkeys
 
 478   do_query($form, $dbh, $query, $form->{id});
 
 480   # delete chart of account record
 
 481   # last step delete chart, because we have a constraint
 
 483   $query = qq|DELETE FROM chart
 
 485   do_query($form, $dbh, $query, $form->{id});
 
 487   # commit and redirect
 
 488   my $rc = $dbh->commit;
 
 491   $main::lxdebug->leave_sub();
 
 497   $main::lxdebug->enter_sub();
 
 499   my ($self, $myconfig, $form) = @_;
 
 501   # connect to database
 
 502   my $dbh = $form->dbconnect($myconfig);
 
 504   my $query = qq|SELECT id, lead
 
 508   my $sth = $dbh->prepare($query);
 
 509   $sth->execute || $form->dberror($query);
 
 511   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 512     push @{ $form->{ALL} }, $ref;
 
 518   $main::lxdebug->leave_sub();
 
 522   $main::lxdebug->enter_sub();
 
 524   my ($self, $myconfig, $form) = @_;
 
 526   # connect to database
 
 527   my $dbh = $form->dbconnect($myconfig);
 
 530     qq|SELECT l.id, l.lead | .
 
 533   my $sth = $dbh->prepare($query);
 
 534   $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
 536   my $ref = $sth->fetchrow_hashref("NAME_lc");
 
 538   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
 544   $main::lxdebug->leave_sub();
 
 548   $main::lxdebug->enter_sub();
 
 550   my ($self, $myconfig, $form) = @_;
 
 553   # connect to database
 
 554   my $dbh = $form->dbconnect($myconfig);
 
 556   my @values = ($form->{description});
 
 557   # id is the old record
 
 559     $query = qq|UPDATE leads SET
 
 562     push(@values, $form->{id});
 
 564     $query = qq|INSERT INTO leads
 
 568   do_query($form, $dbh, $query, @values);
 
 572   $main::lxdebug->leave_sub();
 
 576   $main::lxdebug->enter_sub();
 
 578   my ($self, $myconfig, $form) = @_;
 
 581   # connect to database
 
 582   my $dbh = $form->dbconnect($myconfig);
 
 584   $query = qq|DELETE FROM leads
 
 586   do_query($form, $dbh, $query, $form->{id});
 
 590   $main::lxdebug->leave_sub();
 
 594   $main::lxdebug->enter_sub();
 
 596   my ($self, $myconfig, $form, $return_list) = @_;
 
 598   # connect to database
 
 599   my $dbh = $form->dbconnect($myconfig);
 
 602     "SELECT id, description, template_code, article_code, " .
 
 603     "  output_numberformat, output_dateformat, output_longdates " .
 
 604     "FROM language ORDER BY description";
 
 606   my $sth = $dbh->prepare($query);
 
 607   $sth->execute || $form->dberror($query);
 
 611   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 612     push(@{ $ary }, $ref);
 
 618   $main::lxdebug->leave_sub();
 
 628   $main::lxdebug->enter_sub();
 
 630   my ($self, $myconfig, $form) = @_;
 
 632   # connect to database
 
 633   my $dbh = $form->dbconnect($myconfig);
 
 636     "SELECT description, template_code, article_code, " .
 
 637     "  output_numberformat, output_dateformat, output_longdates " .
 
 638     "FROM language WHERE id = ?";
 
 639   my $sth = $dbh->prepare($query);
 
 640   $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
 
 642   my $ref = $sth->fetchrow_hashref("NAME_lc");
 
 644   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
 650   $main::lxdebug->leave_sub();
 
 653 sub get_language_details {
 
 654   $main::lxdebug->enter_sub();
 
 656   my ($self, $myconfig, $form, $id) = @_;
 
 658   # connect to database
 
 659   my $dbh = $form->dbconnect($myconfig);
 
 662     "SELECT template_code, " .
 
 663     "  output_numberformat, output_dateformat, output_longdates " .
 
 664     "FROM language WHERE id = ?";
 
 665   my @res = selectrow_query($form, $dbh, $query, $id);
 
 668   $main::lxdebug->leave_sub();
 
 674   $main::lxdebug->enter_sub();
 
 676   my ($self, $myconfig, $form) = @_;
 
 678   # connect to database
 
 679   my $dbh = $form->dbconnect($myconfig);
 
 680   my (@values, $query);
 
 682   map({ push(@values, $form->{$_}); }
 
 683       qw(description template_code article_code
 
 684          output_numberformat output_dateformat output_longdates));
 
 686   # id is the old record
 
 689       "UPDATE language SET " .
 
 690       "  description = ?, template_code = ?, article_code = ?, " .
 
 691       "  output_numberformat = ?, output_dateformat = ?, " .
 
 692       "  output_longdates = ? " .
 
 694     push(@values, $form->{id});
 
 697       "INSERT INTO language (" .
 
 698       "  description, template_code, article_code, " .
 
 699       "  output_numberformat, output_dateformat, output_longdates" .
 
 700       ") VALUES (?, ?, ?, ?, ?, ?)";
 
 702   do_query($form, $dbh, $query, @values);
 
 706   $main::lxdebug->leave_sub();
 
 709 sub delete_language {
 
 710   $main::lxdebug->enter_sub();
 
 712   my ($self, $myconfig, $form) = @_;
 
 715   # connect to database
 
 716   my $dbh = $form->dbconnect_noauto($myconfig);
 
 718   foreach my $table (qw(generic_translations units_language)) {
 
 719     $query = qq|DELETE FROM $table WHERE language_id = ?|;
 
 720     do_query($form, $dbh, $query, $form->{"id"});
 
 723   $query = "DELETE FROM language WHERE id = ?";
 
 724   do_query($form, $dbh, $query, $form->{"id"});
 
 729   $main::lxdebug->leave_sub();
 
 732 sub prepare_template_filename {
 
 733   $main::lxdebug->enter_sub();
 
 735   my ($self, $myconfig, $form) = @_;
 
 737   my ($filename, $display_filename);
 
 739   if ($form->{type} eq "stylesheet") {
 
 740     $filename = "css/$myconfig->{stylesheet}";
 
 741     $display_filename = $myconfig->{stylesheet};
 
 744     $filename = $form->{formname};
 
 746     if ($form->{language}) {
 
 747       my ($id, $template_code) = split(/--/, $form->{language});
 
 748       $filename .= "_${template_code}";
 
 751     if ($form->{printer}) {
 
 752       my ($id, $template_code) = split(/--/, $form->{printer});
 
 753       $filename .= "_${template_code}";
 
 756     $filename .= "." . ($form->{format} eq "html" ? "html" : "tex");
 
 757     if ($form->{"formname"} =~ m|\.\.| || $form->{"formname"} =~ m|^/|) {
 
 758       $filename =~ s|.*/||;
 
 760     $display_filename = $filename;
 
 761     $filename = SL::DB::Default->get->templates . "/$filename";
 
 764   $main::lxdebug->leave_sub();
 
 766   return ($filename, $display_filename);
 
 771   $main::lxdebug->enter_sub();
 
 773   my ($self, $filename) = @_;
 
 775   my ($content, $lines) = ("", 0);
 
 779   if (open(TEMPLATE, $filename)) {
 
 787   $content = Encode::decode('utf-8-strict', $content);
 
 789   $main::lxdebug->leave_sub();
 
 791   return ($content, $lines);
 
 795   $main::lxdebug->enter_sub();
 
 797   my ($self, $filename, $content) = @_;
 
 803   if (open(TEMPLATE, ">", $filename)) {
 
 804     $content = Encode::encode('utf-8-strict', $content);
 
 805     $content =~ s/\r\n/\n/g;
 
 806     print(TEMPLATE $content);
 
 812   $main::lxdebug->leave_sub();
 
 817 sub save_preferences {
 
 818   $main::lxdebug->enter_sub();
 
 820   my ($self, $form) = @_;
 
 822   my $employee = SL::DB::Manager::Employee->find_by(login => $::myconfig{login});
 
 823   $employee->update_attributes(name => $form->{name});
 
 825   my $user = SL::DB::Manager::AuthUser->find_by(login => $::myconfig{login});
 
 826   $user->update_attributes(
 
 828       %{ $user->config_values },
 
 829       map { ($_ => $form->{$_}) } SL::DB::AuthUser::CONFIG_VARS(),
 
 832   $main::lxdebug->leave_sub();
 
 838   $main::lxdebug->enter_sub();
 
 843   my $myconfig = \%main::myconfig;
 
 844   my $form     = $main::form;
 
 846   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
 
 848   my $defaults = selectfirst_hashref_query($form, $dbh, qq|SELECT * FROM defaults|) || {};
 
 850   $defaults->{weightunit} ||= 'kg';
 
 852   $main::lxdebug->leave_sub();
 
 858   $main::lxdebug->enter_sub();
 
 860   my ($self, $myconfig, $form) = @_;
 
 862   my $dbh = $form->dbconnect($myconfig);
 
 864   my $query = qq|SELECT closedto, max_future_booking_interval, revtrans FROM defaults|;
 
 865   my $sth   = $dbh->prepare($query);
 
 866   $sth->execute || $form->dberror($query);
 
 868   ($form->{closedto}, $form->{max_future_booking_interval}, $form->{revtrans}) = $sth->fetchrow_array;
 
 874   $main::lxdebug->leave_sub();
 
 878   $main::lxdebug->enter_sub();
 
 880   my ($self, $myconfig, $form) = @_;
 
 882   my $dbh = $form->dbconnect($myconfig);
 
 884   my ($query, @values);
 
 886   # is currently NEVER trueish (no more hidden revtrans in $form)
 
 887   # if ($form->{revtrans}) {
 
 888   #   $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '1'|;
 
 889   # -> therefore you can only set this to false (which is already the default)
 
 890   # and this flag is currently only checked in gl.pl. TOOD Can probably be removed
 
 892     $query = qq|UPDATE defaults SET closedto = ?, max_future_booking_interval = ?, revtrans = '0'|;
 
 893     @values = (conv_date($form->{closedto}), conv_i($form->{max_future_booking_interval}));
 
 895   # set close in defaults
 
 896   do_query($form, $dbh, $query, @values);
 
 900   $main::lxdebug->leave_sub();
 
 904   my ($self, $units, $unit_name, $factor) = @_;
 
 906   $factor = 1 unless ($factor);
 
 908   my $unit = $units->{$unit_name};
 
 910   if (!defined($unit) || !$unit->{"base_unit"} ||
 
 911       ($unit_name eq $unit->{"base_unit"})) {
 
 912     return ($unit_name, $factor);
 
 915   return AM->get_base_unit($units, $unit->{"base_unit"}, $factor * $unit->{"factor"});
 
 919   $main::lxdebug->enter_sub();
 
 921   my ($self, $myconfig, $form, $prefix) = @_;
 
 924   my $dbh = $form->get_standard_dbh;
 
 926   my $query = "SELECT *, base_unit AS original_base_unit FROM units";
 
 928   my $sth = prepare_execute_query($form, $dbh, $query);
 
 931   while (my $ref = $sth->fetchrow_hashref()) {
 
 932     $units->{$ref->{"name"}} = $ref;
 
 936   my $query_lang = "SELECT id, template_code FROM language ORDER BY description";
 
 937   $sth = $dbh->prepare($query_lang);
 
 938   $sth->execute() || $form->dberror($query_lang);
 
 940   while (my $ref = $sth->fetchrow_hashref()) {
 
 941     push(@languages, $ref);
 
 945   $query_lang = "SELECT ul.localized, ul.localized_plural, l.id, l.template_code " .
 
 946     "FROM units_language ul " .
 
 947     "LEFT JOIN language l ON ul.language_id = l.id " .
 
 949   $sth = $dbh->prepare($query_lang);
 
 951   foreach my $unit (values(%{$units})) {
 
 952     ($unit->{"${prefix}base_unit"}, $unit->{"${prefix}factor"}) = AM->get_base_unit($units, $unit->{"name"});
 
 954     $unit->{"LANGUAGES"} = {};
 
 955     foreach my $lang (@languages) {
 
 956       $unit->{"LANGUAGES"}->{$lang->{"template_code"}} = { "template_code" => $lang->{"template_code"} };
 
 959     $sth->execute($unit->{"name"}) || $form->dberror($query_lang . " (" . $unit->{"name"} . ")");
 
 960     while (my $ref = $sth->fetchrow_hashref()) {
 
 961       map({ $unit->{"LANGUAGES"}->{$ref->{"template_code"}}->{$_} = $ref->{$_} } keys(%{$ref}));
 
 966   $main::lxdebug->leave_sub();
 
 971 sub retrieve_all_units {
 
 972   $main::lxdebug->enter_sub();
 
 976   if (!$::request->{cache}{all_units}) {
 
 977     $::request->{cache}{all_units} = $self->retrieve_units(\%main::myconfig, $main::form);
 
 980   $main::lxdebug->leave_sub();
 
 982   return $::request->{cache}{all_units};
 
 986 sub translate_units {
 
 987   $main::lxdebug->enter_sub();
 
 989   my ($self, $form, $template_code, $unit, $amount) = @_;
 
 991   my $units = $self->retrieve_units(\%main::myconfig, $form);
 
 993   my $h = $units->{$unit}->{"LANGUAGES"}->{$template_code};
 
 994   my $new_unit = $unit;
 
 996     if (($amount != 1) && $h->{"localized_plural"}) {
 
 997       $new_unit = $h->{"localized_plural"};
 
 998     } elsif ($h->{"localized"}) {
 
 999       $new_unit = $h->{"localized"};
 
1003   $main::lxdebug->leave_sub();
 
1009   $main::lxdebug->enter_sub();
 
1011   my ($self, $myconfig, $form, $units) = @_;
 
1013   my $dbh = $form->dbconnect($myconfig);
 
1015   map({ $_->{"in_use"} = 0; } values(%{$units}));
 
1017   foreach my $unit (values(%{$units})) {
 
1018     my $base_unit = $unit->{"original_base_unit"};
 
1019     while ($base_unit) {
 
1020       $units->{$base_unit}->{"in_use"} = 1;
 
1021       $units->{$base_unit}->{"DEPENDING_UNITS"} = [] unless ($units->{$base_unit}->{"DEPENDING_UNITS"});
 
1022       push(@{$units->{$base_unit}->{"DEPENDING_UNITS"}}, $unit->{"name"});
 
1023       $base_unit = $units->{$base_unit}->{"original_base_unit"};
 
1027   foreach my $unit (values(%{$units})) {
 
1028     map({ $_ = $dbh->quote($_); } @{$unit->{"DEPENDING_UNITS"}});
 
1030     foreach my $table (qw(parts invoice orderitems)) {
 
1031       my $query = "SELECT COUNT(*) FROM $table WHERE unit ";
 
1033       if (0 == scalar(@{$unit->{"DEPENDING_UNITS"}})) {
 
1034         $query .= "= " . $dbh->quote($unit->{"name"});
 
1036         $query .= "IN (" . $dbh->quote($unit->{"name"}) . "," .
 
1037           join(",", map({ $dbh->quote($_) } @{$unit->{"DEPENDING_UNITS"}})) . ")";
 
1040       my ($count) = $dbh->selectrow_array($query);
 
1041       $form->dberror($query) if ($dbh->err);
 
1044         $unit->{"in_use"} = 1;
 
1052   $main::lxdebug->leave_sub();
 
1055 sub convertible_units {
 
1056   $main::lxdebug->enter_sub();
 
1060   my $filter_unit = shift;
 
1061   my $not_smaller = shift;
 
1063   my $conv_units = [];
 
1065   $filter_unit = $units->{$filter_unit};
 
1067   foreach my $name (sort { lc $a cmp lc $b } keys %{ $units }) {
 
1068     my $unit = $units->{$name};
 
1070     if (($unit->{base_unit} eq $filter_unit->{base_unit}) &&
 
1071         (!$not_smaller || ($unit->{factor} >= $filter_unit->{factor}))) {
 
1072       push @{$conv_units}, $unit;
 
1076   my @sorted = sort { $b->{factor} <=> $a->{factor} } @{ $conv_units };
 
1078   $main::lxdebug->leave_sub();
 
1083 # if $a is translatable to $b, return the factor between them.
 
1086   $main::lxdebug->enter_sub(2);
 
1087   my ($this, $a, $b, $all_units) = @_;
 
1090     $all_units = $this->retrieve_all_units;
 
1093   $main::lxdebug->leave_sub(2) and return 0 unless $a && $b;
 
1094   $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a} && $all_units->{$b};
 
1095   $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a}{base_unit} eq $all_units->{$b}{base_unit};
 
1096   $main::lxdebug->leave_sub(2) and return $all_units->{$a}{factor} / $all_units->{$b}{factor};
 
1099 sub unit_select_data {
 
1100   $main::lxdebug->enter_sub();
 
1102   my ($self, $units, $selected, $empty_entry, $convertible_into) = @_;
 
1107     push(@{$select}, { "name" => "", "base_unit" => "", "factor" => "", "selected" => "" });
 
1110   foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
 
1111     if (!$convertible_into ||
 
1112         ($units->{$convertible_into} &&
 
1113          ($units->{$convertible_into}->{base_unit} eq $units->{$unit}->{base_unit}))) {
 
1114       push @{$select}, { "name"      => $unit,
 
1115                          "base_unit" => $units->{$unit}->{"base_unit"},
 
1116                          "factor"    => $units->{$unit}->{"factor"},
 
1117                          "selected"  => ($unit eq $selected) ? "selected" : "" };
 
1121   $main::lxdebug->leave_sub();
 
1126 sub unit_select_html {
 
1127   $main::lxdebug->enter_sub();
 
1129   my ($self, $units, $name, $selected, $convertible_into) = @_;
 
1131   my $select = "<select name=${name}>";
 
1133   foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
 
1134     if (!$convertible_into ||
 
1135         ($units->{$convertible_into} &&
 
1136          ($units->{$convertible_into}->{"base_unit"} eq $units->{$unit}->{"base_unit"}))) {
 
1137       $select .= "<option" . (($unit eq $selected) ? " selected" : "") . ">${unit}</option>";
 
1140   $select .= "</select>";
 
1142   $main::lxdebug->leave_sub();
 
1148   $main::lxdebug->enter_sub();
 
1152   my $units = $self->retrieve_all_units();
 
1157   while (2 <= scalar(@_)) {
 
1158     my $qty  = shift(@_);
 
1159     my $unit = $units->{shift(@_)};
 
1161     croak "No unit defined with name $unit" if (!defined $unit);
 
1164       $base_unit = $unit->{base_unit};
 
1165     } elsif ($base_unit ne $unit->{base_unit}) {
 
1166       croak "Adding values with incompatible base units $base_unit/$unit->{base_unit}";
 
1169     $sum += $qty * $unit->{factor};
 
1172   $main::lxdebug->leave_sub();
 
1178   $main::lxdebug->enter_sub();
 
1180   my ($self, $myconfig, $form, $name, $base_unit, $factor, $languages) = @_;
 
1182   my $dbh = $form->dbconnect_noauto($myconfig);
 
1184   my $query = qq|SELECT COALESCE(MAX(sortkey), 0) + 1 FROM units|;
 
1185   my ($sortkey) = selectrow_query($form, $dbh, $query);
 
1187   $query = "INSERT INTO units (name, base_unit, factor, sortkey) " .
 
1188     "VALUES (?, ?, ?, ?)";
 
1189   do_query($form, $dbh, $query, $name, $base_unit, $factor, $sortkey);
 
1192     $query = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
 
1193     my $sth = $dbh->prepare($query);
 
1194     foreach my $lang (@{$languages}) {
 
1195       my @values = ($name, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
 
1196       $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
 
1204   $main::lxdebug->leave_sub();
 
1208   $main::lxdebug->enter_sub();
 
1210   my ($self, $myconfig, $form, $units, $delete_units) = @_;
 
1212   my $dbh = $form->dbconnect_noauto($myconfig);
 
1214   my ($base_unit, $unit, $sth, $query);
 
1216   $query = "DELETE FROM units_language";
 
1217   $dbh->do($query) || $form->dberror($query);
 
1219   if ($delete_units && (0 != scalar(@{$delete_units}))) {
 
1220     $query = "DELETE FROM units WHERE name IN (";
 
1221     map({ $query .= "?," } @{$delete_units});
 
1222     substr($query, -1, 1) = ")";
 
1223     $dbh->do($query, undef, @{$delete_units}) ||
 
1224       $form->dberror($query . " (" . join(", ", @{$delete_units}) . ")");
 
1227   $query = "UPDATE units SET name = ?, base_unit = ?, factor = ? WHERE name = ?";
 
1228   $sth = $dbh->prepare($query);
 
1230   my $query_lang = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
 
1231   my $sth_lang = $dbh->prepare($query_lang);
 
1233   foreach $unit (values(%{$units})) {
 
1234     $unit->{"depth"} = 0;
 
1235     my $base_unit = $unit;
 
1236     while ($base_unit->{"base_unit"}) {
 
1238       $base_unit = $units->{$base_unit->{"base_unit"}};
 
1242   foreach $unit (sort({ $a->{"depth"} <=> $b->{"depth"} } values(%{$units}))) {
 
1243     if ($unit->{"LANGUAGES"}) {
 
1244       foreach my $lang (@{$unit->{"LANGUAGES"}}) {
 
1245         next unless ($lang->{"id"} && $lang->{"localized"});
 
1246         my @values = ($unit->{"name"}, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
 
1247         $sth_lang->execute(@values) || $form->dberror($query_lang . " (" . join(", ", @values) . ")");
 
1251     next if ($unit->{"unchanged_unit"});
 
1253     my @values = ($unit->{"name"}, $unit->{"base_unit"}, $unit->{"factor"}, $unit->{"old_name"});
 
1254     $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
 
1258   $sth_lang->finish();
 
1262   $main::lxdebug->leave_sub();
 
1266   $main::lxdebug->enter_sub();
 
1268   my ($self, $myconfig, $form) = @_;
 
1270   # connect to database
 
1271   my $dbh = $form->dbconnect($myconfig);
 
1273   my $query = qq|SELECT
 
1277                    round(t.rate * 100, 2) AS rate,
 
1278                    (SELECT accno FROM chart WHERE id = chart_id) AS taxnumber,
 
1279                    (SELECT description FROM chart WHERE id = chart_id) AS account_description,
 
1280                    (SELECT accno FROM chart WHERE id = skonto_sales_chart_id) AS skonto_chart_accno,
 
1281                    (SELECT description FROM chart WHERE id = skonto_sales_chart_id) AS skonto_chart_description,
 
1282                    (SELECT accno FROM chart WHERE id = skonto_purchase_chart_id) AS skonto_chart_purchase_accno,
 
1283                    (SELECT description FROM chart WHERE id = skonto_purchase_chart_id) AS skonto_chart_purchase_description
 
1285                  ORDER BY taxkey, rate|;
 
1287   my $sth = $dbh->prepare($query);
 
1288   $sth->execute || $form->dberror($query);
 
1291   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
1292     push @{ $form->{TAX} }, $ref;
 
1298   $main::lxdebug->leave_sub();
 
1301 sub get_tax_accounts {
 
1302   $main::lxdebug->enter_sub();
 
1304   my ($self, $myconfig, $form) = @_;
 
1306   my $dbh = $form->dbconnect($myconfig);
 
1308   # get Accounts from chart
 
1309   my $query = qq{ SELECT
 
1311                  accno || ' - ' || description AS taxaccount
 
1313                WHERE link LIKE '%_tax%'
 
1317   my $sth = $dbh->prepare($query);
 
1318   $sth->execute || $form->dberror($query);
 
1320   $form->{ACCOUNTS} = [];
 
1321   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
1322     push @{ $form->{ACCOUNTS} }, $ref;
 
1325   $form->{AR_PAID} = SL::DB::Manager::Chart->get_all(where => [ link => { like => '%AR_paid%' } ], sort_by => 'accno ASC');
 
1326   $form->{AP_PAID} = SL::DB::Manager::Chart->get_all(where => [ link => { like => '%AP_paid%' } ], sort_by => 'accno ASC');
 
1328   $form->{skontochart_value_title_sub} = sub {
 
1332       $item->{accno} .' '. $item->{description},
 
1340   $main::lxdebug->leave_sub();
 
1344   $main::lxdebug->enter_sub();
 
1346   my ($self, $myconfig, $form) = @_;
 
1348   # connect to database
 
1349   my $dbh = $form->dbconnect($myconfig);
 
1351   my $query = qq|SELECT
 
1354                    round(rate * 100, 2) AS rate,
 
1357                    (id IN (SELECT tax_id
 
1358                            FROM acc_trans)) AS tax_already_used,
 
1359                    skonto_sales_chart_id,
 
1360                    skonto_purchase_chart_id
 
1364   my $sth = $dbh->prepare($query);
 
1365   $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
1367   my $ref = $sth->fetchrow_hashref("NAME_lc");
 
1369   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
1373   # see if it is used by a taxkey
 
1374   $query = qq|SELECT count(*) FROM taxkeys
 
1375               WHERE tax_id = ? AND chart_id >0|;
 
1377   ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
 
1379   $form->{orphaned} = !$form->{orphaned};
 
1382   if (!$form->{orphaned} ) {
 
1383     $query = qq|SELECT DISTINCT c.id, c.accno
 
1385                 JOIN   tax t ON (t.id = tk.tax_id)
 
1386                 JOIN chart c ON (c.id = tk.chart_id)
 
1387                 WHERE tk.tax_id = ?|;
 
1389     $sth = $dbh->prepare($query);
 
1390     $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
1392     $form->{TAXINUSE} = [];
 
1393     while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
1394       push @{ $form->{TAXINUSE} }, $ref;
 
1402   $main::lxdebug->leave_sub();
 
1406   $main::lxdebug->enter_sub();
 
1408   my ($self, $myconfig, $form) = @_;
 
1411   # connect to database
 
1412   my $dbh = $form->get_standard_dbh($myconfig);
 
1414   $form->{rate} = $form->{rate} / 100;
 
1416   my $chart_categories = '';
 
1417   $chart_categories .= 'A' if $form->{asset};
 
1418   $chart_categories .= 'L' if $form->{liability};
 
1419   $chart_categories .= 'Q' if $form->{equity};
 
1420   $chart_categories .= 'I' if $form->{revenue};
 
1421   $chart_categories .= 'E' if $form->{expense};
 
1422   $chart_categories .= 'C' if $form->{costs};
 
1424   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);
 
1425   if ($form->{id} ne "") {
 
1426     $query = qq|UPDATE tax SET
 
1431                   taxnumber                = (SELECT accno FROM chart WHERE id = ? ),
 
1432                   skonto_sales_chart_id    = ?,
 
1433                   skonto_purchase_chart_id = ?,
 
1434                   chart_categories         = ?
 
1439     ($form->{id}) = selectfirst_array_query($form, $dbh, qq|SELECT nextval('id')|);
 
1440     $query = qq|INSERT INTO tax (
 
1446                   skonto_sales_chart_id,
 
1447                   skonto_purchase_chart_id,
 
1451                 VALUES (?, ?, ?, ?, (SELECT accno FROM chart WHERE id = ?), ?, ?,  ?, ?)|;
 
1453   push(@values, $form->{id});
 
1454   do_query($form, $dbh, $query, @values);
 
1456   foreach my $language_id (keys %{ $form->{translations} }) {
 
1457     GenericTranslations->save('dbh'              => $dbh,
 
1458                               'translation_type' => 'SL::DB::Tax/taxdescription',
 
1459                               'translation_id'   => $form->{id},
 
1460                               'language_id'      => $language_id,
 
1461                               'translation'      => $form->{translations}->{$language_id});
 
1466   $main::lxdebug->leave_sub();
 
1470   $main::lxdebug->enter_sub();
 
1472   my ($self, $myconfig, $form) = @_;
 
1475   # connect to database
 
1476   my $dbh = $form->get_standard_dbh($myconfig);
 
1478   $query = qq|DELETE FROM tax
 
1480   do_query($form, $dbh, $query, $form->{id});
 
1484   $main::lxdebug->leave_sub();
 
1487 sub save_price_factor {
 
1488   $main::lxdebug->enter_sub();
 
1490   my ($self, $myconfig, $form) = @_;
 
1492   # connect to database
 
1493   my $dbh = $form->get_standard_dbh($myconfig);
 
1496   my @values = ($form->{description}, conv_i($form->{factor}));
 
1499     $query = qq|UPDATE price_factors SET description = ?, factor = ? WHERE id = ?|;
 
1500     push @values, conv_i($form->{id});
 
1503     $query = qq|INSERT INTO price_factors (description, factor, sortkey) VALUES (?, ?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM price_factors))|;
 
1506   do_query($form, $dbh, $query, @values);
 
1510   $main::lxdebug->leave_sub();
 
1513 sub get_all_price_factors {
 
1514   $main::lxdebug->enter_sub();
 
1516   my ($self, $myconfig, $form) = @_;
 
1518   # connect to database
 
1519   my $dbh = $form->get_standard_dbh($myconfig);
 
1521   $form->{PRICE_FACTORS} = selectall_hashref_query($form, $dbh, qq|SELECT * FROM price_factors ORDER BY sortkey|);
 
1523   $main::lxdebug->leave_sub();
 
1526 sub get_price_factor {
 
1527   $main::lxdebug->enter_sub();
 
1529   my ($self, $myconfig, $form) = @_;
 
1531   # connect to database
 
1532   my $dbh = $form->get_standard_dbh($myconfig);
 
1534   my $query = qq|SELECT description, factor,
 
1535                    ((SELECT COUNT(*) FROM parts      WHERE price_factor_id = ?) +
 
1536                     (SELECT COUNT(*) FROM invoice    WHERE price_factor_id = ?) +
 
1537                     (SELECT COUNT(*) FROM orderitems WHERE price_factor_id = ?)) = 0 AS orphaned
 
1538                  FROM price_factors WHERE id = ?|;
 
1540   ($form->{description}, $form->{factor}, $form->{orphaned}) = selectrow_query($form, $dbh, $query, (conv_i($form->{id})) x 4);
 
1542   $main::lxdebug->leave_sub();
 
1545 sub delete_price_factor {
 
1546   $main::lxdebug->enter_sub();
 
1548   my ($self, $myconfig, $form) = @_;
 
1550   # connect to database
 
1551   my $dbh = $form->get_standard_dbh($myconfig);
 
1553   do_query($form, $dbh, qq|DELETE FROM price_factors WHERE id = ?|, conv_i($form->{id}));
 
1556   $main::lxdebug->leave_sub();
 
1559 sub save_warehouse {
 
1560   $main::lxdebug->enter_sub();
 
1562   my ($self, $myconfig, $form) = @_;
 
1564   # connect to database
 
1565   my $dbh = $form->get_standard_dbh($myconfig);
 
1567   my ($query, @values, $sth);
 
1570     $query        = qq|SELECT nextval('id')|;
 
1571     ($form->{id}) = selectrow_query($form, $dbh, $query);
 
1573     $query        = qq|INSERT INTO warehouse (id, sortkey) VALUES (?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM warehouse))|;
 
1574     do_query($form, $dbh, $query, $form->{id});
 
1577   do_query($form, $dbh, qq|UPDATE warehouse SET description = ?, invalid = ? WHERE id = ?|,
 
1578            $form->{description}, $form->{invalid} ? 't' : 'f', conv_i($form->{id}));
 
1580   if (0 < $form->{number_of_new_bins}) {
 
1581     my ($num_existing_bins) = selectfirst_array_query($form, $dbh, qq|SELECT COUNT(*) FROM bin WHERE warehouse_id = ?|, $form->{id});
 
1582     $query = qq|INSERT INTO bin (warehouse_id, description) VALUES (?, ?)|;
 
1583     $sth   = prepare_query($form, $dbh, $query);
 
1585     foreach my $i (1..$form->{number_of_new_bins}) {
 
1586       do_statement($form, $sth, $query, conv_i($form->{id}), "$form->{prefix}" . ($i + $num_existing_bins));
 
1594   $main::lxdebug->leave_sub();
 
1598   $main::lxdebug->enter_sub();
 
1600   my ($self, $myconfig, $form) = @_;
 
1602   # connect to database
 
1603   my $dbh = $form->get_standard_dbh($myconfig);
 
1605   my ($query, @values, $commit_necessary, $sth);
 
1607   @values = map { $form->{"id_${_}"} } grep { $form->{"delete_${_}"} } (1..$form->{rowcount});
 
1610     $query = qq|DELETE FROM bin WHERE id IN (| . join(', ', ('?') x scalar(@values)) . qq|)|;
 
1611     do_query($form, $dbh, $query, @values);
 
1613     $commit_necessary = 1;
 
1616   $query = qq|UPDATE bin SET description = ? WHERE id = ?|;
 
1617   $sth   = prepare_query($form, $dbh, $query);
 
1619   foreach my $row (1..$form->{rowcount}) {
 
1620     next if ($form->{"delete_${row}"});
 
1622     do_statement($form, $sth, $query, $form->{"description_${row}"}, conv_i($form->{"id_${row}"}));
 
1624     $commit_necessary = 1;
 
1629   $dbh->commit() if ($commit_necessary);
 
1631   $main::lxdebug->leave_sub();
 
1634 sub delete_warehouse {
 
1635   $main::lxdebug->enter_sub();
 
1637   my ($self, $myconfig, $form) = @_;
 
1639   # connect to database
 
1640   my $dbh = $form->get_standard_dbh($myconfig);
 
1642   my $id      = conv_i($form->{id});
 
1643   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|;
 
1644   my ($count) = selectrow_query($form, $dbh, $query, $id);
 
1647     $main::lxdebug->leave_sub();
 
1651   do_query($form, $dbh, qq|DELETE FROM bin       WHERE warehouse_id = ?|, conv_i($form->{id}));
 
1652   do_query($form, $dbh, qq|DELETE FROM warehouse WHERE id           = ?|, conv_i($form->{id}));
 
1656   $main::lxdebug->leave_sub();
 
1661 sub get_all_warehouses {
 
1662   $main::lxdebug->enter_sub();
 
1664   my ($self, $myconfig, $form) = @_;
 
1666   # connect to database
 
1667   my $dbh = $form->get_standard_dbh($myconfig);
 
1669   my $query = qq|SELECT w.id, w.description, w.invalid,
 
1670                    (SELECT COUNT(b.description) FROM bin b WHERE b.warehouse_id = w.id) AS number_of_bins
 
1672                  ORDER BY w.sortkey|;
 
1674   $form->{WAREHOUSES} = selectall_hashref_query($form, $dbh, $query);
 
1676   $main::lxdebug->leave_sub();
 
1680   $main::lxdebug->enter_sub();
 
1682   my ($self, $myconfig, $form) = @_;
 
1684   # connect to database
 
1685   my $dbh = $form->get_standard_dbh($myconfig);
 
1687   my $id    = conv_i($form->{id});
 
1688   my $query = qq|SELECT w.description, w.invalid
 
1692   my $ref   = selectfirst_hashref_query($form, $dbh, $query, $id);
 
1694   map { $form->{$_} = $ref->{$_} } keys %{ $ref };
 
1698       (   EXISTS(SELECT i.bin_id FROM inventory i WHERE i.bin_id = b.id LIMIT 1)
 
1699        OR EXISTS(SELECT p.bin_id FROM parts     p WHERE p.bin_id = b.id LIMIT 1))
 
1702     WHERE b.warehouse_id = ?
 
1705   $form->{BINS} = selectall_hashref_query($form, $dbh, $query, conv_i($form->{id}));
 
1707   $main::lxdebug->leave_sub();