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   # fetch chart-related data and set form fields
 
  57   # get_account is called by add_account in am.pl
 
  58   # always sets $form->{TAXKEY} and default_accounts
 
  59   # loads chart data when $form->{id} is passed
 
  61   my ($self, $myconfig, $form) = @_;
 
  63   # get default accounts
 
  64   map { $form->{$_} = $::instance_conf->{$_} } qw(inventory_accno_id income_accno_id expense_accno_id);
 
  67   my $taxes = SL::DB::Manager::Tax->get_all( with_objects => ['chart'] , sort_by => 'taxkey' );
 
  69   foreach my $tk ( @{$taxes} ) {
 
  70     push @{ $form->{TAXKEY} },  { id          => $tk->id,
 
  71                                   chart_accno => $tk->chart_id ? $tk->chart->accno : undef,
 
  72                                   taxkey      => $tk->taxkey,
 
  73                                   tax         => $tk->id . '--' . $tk->taxkey,
 
  80     my $chart_obj = SL::DB::Manager::Chart->find_by(id => $form->{id}) || die "Can't open chart";
 
  82     my @chart_fields = qw(accno description charttype category link pos_bilanz
 
  83                           pos_eur pos_er new_chart_id valid_from pos_bwa datevautomatik);
 
  84     foreach my $cf ( @chart_fields ) {
 
  85       $form->{"$cf"} = $chart_obj->$cf;
 
  88     my $active_taxkey = $chart_obj->get_active_taxkey;
 
  89     $form->{$_}  = $active_taxkey->$_ foreach qw(taxkey_id pos_ustva tax_id startdate);
 
  90     $form->{tax} = $active_taxkey->tax_id . '--' . $active_taxkey->taxkey_id;
 
  92     # check if there are any transactions for this chart
 
  93     $form->{orphaned} = $chart_obj->has_transaction ? 0 : 1;
 
  95     # check if new account is active
 
  96     # The old sql query was broken since at least 2006 and always returned 0
 
  97     $form->{new_chart_valid} = $chart_obj->new_chart_valid;
 
  99     # get the taxkeys of the account
 
 100     $form->{ACCOUNT_TAXKEYS} = [];
 
 101     foreach my $taxkey ( @{ $chart_obj->taxkeys } ) {
 
 102       push @{ $form->{ACCOUNT_TAXKEYS} }, { id             => $taxkey->id,
 
 103                                             chart_id       => $taxkey->chart_id,
 
 104                                             tax_id         => $taxkey->tax_id,
 
 105                                             taxkey_id      => $taxkey->taxkey_id,
 
 106                                             pos_ustva      => $taxkey->pos_ustva,
 
 107                                             startdate      => $taxkey->startdate->to_kivitendo,
 
 108                                             taxdescription => $taxkey->tax->taxdescription,
 
 109                                             rate           => $taxkey->tax->rate,
 
 110                                             accno          => $taxkey->tax->chart->accno,
 
 114     # get new accounts (Folgekonto). Find all charts with the same link
 
 115     $form->{NEWACCOUNT} = $chart_obj->db->dbh->selectall_arrayref('select id, accno,description from chart where link = ? order by accno', {Slice => {}}, $chart_obj->link);
 
 117   } else { # set to orphaned for new charts, so chart_type can be changed (needed by $AccountIsPosted)
 
 118     $form->{orphaned} = 1;
 
 121   $main::lxdebug->leave_sub();
 
 125   $main::lxdebug->enter_sub();
 
 127   # TODO: it should be forbidden to change an account to a heading if there
 
 128   # have been bookings to this account in the past
 
 130   my ($self, $myconfig, $form) = @_;
 
 132   # connect to database, turn off AutoCommit
 
 133   my $dbh = $form->dbconnect_noauto($myconfig);
 
 135   for (qw(AR_include_in_dropdown AP_include_in_dropdown summary_account)) {
 
 136     $form->{$form->{$_}} = $form->{$_} if $form->{$_};
 
 139   # sanity check, can't have AR with AR_...
 
 140   if ($form->{AR} || $form->{AP} || $form->{IC}) {
 
 141     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)) {
 
 142       $form->error($::locale->text('It is not allowed that a summary account occurs in a drop-down menu!'));
 
 147   foreach my $item ($form->{AR},            $form->{AR_amount},
 
 148                     $form->{AR_tax},        $form->{AR_paid},
 
 149                     $form->{AP},            $form->{AP_amount},
 
 150                     $form->{AP_tax},        $form->{AP_paid},
 
 151                     $form->{IC},            $form->{IC_sale},
 
 152                     $form->{IC_cogs},       $form->{IC_taxpart},
 
 153                     $form->{IC_income},     $form->{IC_expense},
 
 154                     $form->{IC_taxservice}
 
 156     $form->{link} .= "${item}:" if ($item);
 
 160   # strip blanks from accno
 
 161   map { $form->{$_} =~ s/ //g; } qw(accno);
 
 165   if ($form->{id} eq "NULL") {
 
 174   my @values = ($form->{accno});
 
 177     $query .= ' AND NOT id = ?';
 
 178     push(@values, $form->{id});
 
 181   my ($accno) = selectrow_query($form, $dbh, $query, @values);
 
 184     $form->error($::locale->text('Account number not unique!'));
 
 188   if (!$form->{id} || $form->{id} eq "") {
 
 189     $query = qq|SELECT nextval('id')|;
 
 190     ($form->{"id"}) = selectrow_query($form, $dbh, $query);
 
 191     $query = qq|INSERT INTO chart (id, accno, link) VALUES (?, ?, ?)|;
 
 192     do_query($form, $dbh, $query, $form->{"id"}, $form->{"accno"}, '');
 
 200     # if charttype is heading make sure certain values are empty
 
 201     # specifically, if charttype is changed from an existing account, empty the
 
 202     # fields unnecessary for headings, so that e.g. heading doesn't appear in
 
 203     # drop-down menues due to still having a valid "link" entry
 
 205     if ( $form->{charttype} eq 'H' ) {
 
 207       $form->{pos_bwa} = '';
 
 208       $form->{pos_bilanz} = '';
 
 209       $form->{pos_eur} = '';
 
 210       $form->{new_chart_id} = '';
 
 211       $form->{valid_from} = '';
 
 214     $query = qq|UPDATE chart SET
 
 231                   $form->{description},
 
 235                   conv_i($form->{pos_bwa}),
 
 236                   conv_i($form->{pos_bilanz}),
 
 237                   conv_i($form->{pos_eur}),
 
 238                   conv_i($form->{pos_er}),
 
 239                   conv_i($form->{new_chart_id}),
 
 240                   conv_date($form->{valid_from}),
 
 241                   ($form->{datevautomatik} eq 'T') ? 'true':'false',
 
 248   do_query($form, $dbh, $query, @values);
 
 254   my $MAX_TRIES = 10; # Maximum count of taxkeys in form
 
 258   for $tk_count (0 .. $MAX_TRIES) {
 
 262     # Check if the account already exists, else cancel
 
 264     print(STDERR "Keine Taxkeys weil ID =: $form->{id}\n");
 
 266     last READTAXKEYS if ( $form->{'id'} == 0);
 
 268     # check if there is a startdate
 
 269     if ( $form->{"taxkey_startdate_$tk_count"} eq '' ) {
 
 274     # Add valid taxkeys into the array
 
 277         id        => ($form->{"taxkey_id_$tk_count"} eq 'NEW') ? conv_i('') : conv_i($form->{"taxkey_id_$tk_count"}),
 
 278         tax_id    => conv_i($form->{"taxkey_tax_$tk_count"}),
 
 279         startdate => conv_date($form->{"taxkey_startdate_$tk_count"}),
 
 280         chart_id  => conv_i($form->{"id"}),
 
 281         pos_ustva => conv_i($form->{"taxkey_pos_ustva_$tk_count"}),
 
 282         delete    => ( $form->{"taxkey_del_$tk_count"} eq 'delete' ) ? '1' : '',
 
 289   for my $j (0 .. $#taxkeys){
 
 290     if ( defined $taxkeys[$j]{'id'} ){
 
 293       if ($taxkeys[$j]{'delete'}){
 
 295           DELETE FROM taxkeys WHERE id = ?
 
 298         @values = ($taxkeys[$j]{'id'});
 
 300         do_query($form, $dbh, $query, @values);
 
 309         SET taxkey_id = (SELECT taxkey FROM tax WHERE tax.id = ?),
 
 317         $taxkeys[$j]{'tax_id'},
 
 318         $taxkeys[$j]{'chart_id'},
 
 319         $taxkeys[$j]{'tax_id'},
 
 320         $taxkeys[$j]{'pos_ustva'},
 
 321         $taxkeys[$j]{'startdate'},
 
 324       do_query($form, $dbh, $query, @values);
 
 330         INSERT INTO taxkeys (
 
 337         VALUES ((SELECT taxkey FROM tax WHERE tax.id = ?), ?, ?, ?, ?)
 
 340         $taxkeys[$j]{'tax_id'},
 
 341         $taxkeys[$j]{'chart_id'},
 
 342         $taxkeys[$j]{'tax_id'},
 
 343         $taxkeys[$j]{'pos_ustva'},
 
 344         $taxkeys[$j]{'startdate'},
 
 347       do_query($form, $dbh, $query, @values);
 
 352   # Update chart.taxkey_id to the latest from taxkeys for this chart.
 
 358       WHERE taxkeys.chart_id = chart.id
 
 359       ORDER BY startdate DESC
 
 365   do_query($form, $dbh, $query, $form->{id});
 
 368   my $rc = $dbh->commit;
 
 371   $main::lxdebug->leave_sub();
 
 377   $main::lxdebug->enter_sub();
 
 379   my ($self, $myconfig, $form) = @_;
 
 381   # connect to database, turn off AutoCommit
 
 382   my $dbh = $form->dbconnect_noauto($myconfig);
 
 384   my $query = qq|SELECT count(*) FROM acc_trans a
 
 385                  WHERE a.chart_id = ?|;
 
 386   my ($count) = selectrow_query($form, $dbh, $query, $form->{id});
 
 390     $main::lxdebug->leave_sub();
 
 394   # set inventory_accno_id, income_accno_id, expense_accno_id to defaults
 
 395   foreach my $type (qw(inventory income expense)) {
 
 398       qq|SET ${type}_accno_id = (SELECT ${type}_accno_id FROM defaults) | .
 
 399       qq|WHERE ${type}_accno_id = ?|;
 
 400     do_query($form, $dbh, $query, $form->{id});
 
 403   $query = qq|DELETE FROM tax
 
 405   do_query($form, $dbh, $query, $form->{id});
 
 407   # delete account taxkeys
 
 408   $query = qq|DELETE FROM taxkeys
 
 410   do_query($form, $dbh, $query, $form->{id});
 
 412   # delete chart of account record
 
 413   # last step delete chart, because we have a constraint
 
 415   $query = qq|DELETE FROM chart
 
 417   do_query($form, $dbh, $query, $form->{id});
 
 419   # commit and redirect
 
 420   my $rc = $dbh->commit;
 
 423   $main::lxdebug->leave_sub();
 
 429   $main::lxdebug->enter_sub();
 
 431   my ($self, $myconfig, $form) = @_;
 
 433   # connect to database
 
 434   my $dbh = $form->dbconnect($myconfig);
 
 436   my $query = qq|SELECT id, lead
 
 440   my $sth = $dbh->prepare($query);
 
 441   $sth->execute || $form->dberror($query);
 
 443   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 444     push @{ $form->{ALL} }, $ref;
 
 450   $main::lxdebug->leave_sub();
 
 454   $main::lxdebug->enter_sub();
 
 456   my ($self, $myconfig, $form) = @_;
 
 458   # connect to database
 
 459   my $dbh = $form->dbconnect($myconfig);
 
 462     qq|SELECT l.id, l.lead | .
 
 465   my $sth = $dbh->prepare($query);
 
 466   $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
 468   my $ref = $sth->fetchrow_hashref("NAME_lc");
 
 470   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
 476   $main::lxdebug->leave_sub();
 
 480   $main::lxdebug->enter_sub();
 
 482   my ($self, $myconfig, $form) = @_;
 
 485   # connect to database
 
 486   my $dbh = $form->dbconnect($myconfig);
 
 488   my @values = ($form->{description});
 
 489   # id is the old record
 
 491     $query = qq|UPDATE leads SET
 
 494     push(@values, $form->{id});
 
 496     $query = qq|INSERT INTO leads
 
 500   do_query($form, $dbh, $query, @values);
 
 504   $main::lxdebug->leave_sub();
 
 508   $main::lxdebug->enter_sub();
 
 510   my ($self, $myconfig, $form) = @_;
 
 513   # connect to database
 
 514   my $dbh = $form->dbconnect($myconfig);
 
 516   $query = qq|DELETE FROM leads
 
 518   do_query($form, $dbh, $query, $form->{id});
 
 522   $main::lxdebug->leave_sub();
 
 526   $main::lxdebug->enter_sub();
 
 528   my ($self, $myconfig, $form, $return_list) = @_;
 
 530   # connect to database
 
 531   my $dbh = $form->dbconnect($myconfig);
 
 534     "SELECT id, description, template_code, article_code, " .
 
 535     "  output_numberformat, output_dateformat, output_longdates " .
 
 536     "FROM language ORDER BY description";
 
 538   my $sth = $dbh->prepare($query);
 
 539   $sth->execute || $form->dberror($query);
 
 543   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 544     push(@{ $ary }, $ref);
 
 550   $main::lxdebug->leave_sub();
 
 560   $main::lxdebug->enter_sub();
 
 562   my ($self, $myconfig, $form) = @_;
 
 564   # connect to database
 
 565   my $dbh = $form->dbconnect($myconfig);
 
 568     "SELECT description, template_code, article_code, " .
 
 569     "  output_numberformat, output_dateformat, output_longdates " .
 
 570     "FROM language WHERE id = ?";
 
 571   my $sth = $dbh->prepare($query);
 
 572   $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
 
 574   my $ref = $sth->fetchrow_hashref("NAME_lc");
 
 576   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
 582   $main::lxdebug->leave_sub();
 
 585 sub get_language_details {
 
 586   $main::lxdebug->enter_sub();
 
 588   my ($self, $myconfig, $form, $id) = @_;
 
 590   # connect to database
 
 591   my $dbh = $form->dbconnect($myconfig);
 
 594     "SELECT template_code, " .
 
 595     "  output_numberformat, output_dateformat, output_longdates " .
 
 596     "FROM language WHERE id = ?";
 
 597   my @res = selectrow_query($form, $dbh, $query, $id);
 
 600   $main::lxdebug->leave_sub();
 
 606   $main::lxdebug->enter_sub();
 
 608   my ($self, $myconfig, $form) = @_;
 
 610   # connect to database
 
 611   my $dbh = $form->dbconnect($myconfig);
 
 612   my (@values, $query);
 
 614   map({ push(@values, $form->{$_}); }
 
 615       qw(description template_code article_code
 
 616          output_numberformat output_dateformat output_longdates));
 
 618   # id is the old record
 
 621       "UPDATE language SET " .
 
 622       "  description = ?, template_code = ?, article_code = ?, " .
 
 623       "  output_numberformat = ?, output_dateformat = ?, " .
 
 624       "  output_longdates = ? " .
 
 626     push(@values, $form->{id});
 
 629       "INSERT INTO language (" .
 
 630       "  description, template_code, article_code, " .
 
 631       "  output_numberformat, output_dateformat, output_longdates" .
 
 632       ") VALUES (?, ?, ?, ?, ?, ?)";
 
 634   do_query($form, $dbh, $query, @values);
 
 638   $main::lxdebug->leave_sub();
 
 641 sub delete_language {
 
 642   $main::lxdebug->enter_sub();
 
 644   my ($self, $myconfig, $form) = @_;
 
 647   # connect to database
 
 648   my $dbh = $form->dbconnect_noauto($myconfig);
 
 650   foreach my $table (qw(generic_translations units_language)) {
 
 651     $query = qq|DELETE FROM $table WHERE language_id = ?|;
 
 652     do_query($form, $dbh, $query, $form->{"id"});
 
 655   $query = "DELETE FROM language WHERE id = ?";
 
 656   do_query($form, $dbh, $query, $form->{"id"});
 
 661   $main::lxdebug->leave_sub();
 
 664 sub prepare_template_filename {
 
 665   $main::lxdebug->enter_sub();
 
 667   my ($self, $myconfig, $form) = @_;
 
 669   my ($filename, $display_filename);
 
 671   if ($form->{type} eq "stylesheet") {
 
 672     $filename = "css/$myconfig->{stylesheet}";
 
 673     $display_filename = $myconfig->{stylesheet};
 
 676     $filename = $form->{formname};
 
 678     if ($form->{language}) {
 
 679       my ($id, $template_code) = split(/--/, $form->{language});
 
 680       $filename .= "_${template_code}";
 
 683     if ($form->{printer}) {
 
 684       my ($id, $template_code) = split(/--/, $form->{printer});
 
 685       $filename .= "_${template_code}";
 
 688     $filename .= "." . ($form->{format} eq "html" ? "html" : "tex");
 
 689     if ($form->{"formname"} =~ m|\.\.| || $form->{"formname"} =~ m|^/|) {
 
 690       $filename =~ s|.*/||;
 
 692     $display_filename = $filename;
 
 693     $filename = SL::DB::Default->get->templates . "/$filename";
 
 696   $main::lxdebug->leave_sub();
 
 698   return ($filename, $display_filename);
 
 703   $main::lxdebug->enter_sub();
 
 705   my ($self, $filename) = @_;
 
 707   my ($content, $lines) = ("", 0);
 
 711   if (open(TEMPLATE, $filename)) {
 
 719   $content = Encode::decode('utf-8-strict', $content);
 
 721   $main::lxdebug->leave_sub();
 
 723   return ($content, $lines);
 
 727   $main::lxdebug->enter_sub();
 
 729   my ($self, $filename, $content) = @_;
 
 735   if (open(TEMPLATE, ">", $filename)) {
 
 736     $content = Encode::encode('utf-8-strict', $content);
 
 737     $content =~ s/\r\n/\n/g;
 
 738     print(TEMPLATE $content);
 
 744   $main::lxdebug->leave_sub();
 
 749 sub save_preferences {
 
 750   $main::lxdebug->enter_sub();
 
 752   my ($self, $form) = @_;
 
 754   my $employee = SL::DB::Manager::Employee->find_by(login => $::myconfig{login});
 
 755   $employee->update_attributes(name => $form->{name});
 
 757   my $user = SL::DB::Manager::AuthUser->find_by(login => $::myconfig{login});
 
 758   $user->update_attributes(
 
 760       %{ $user->config_values },
 
 761       map { ($_ => $form->{$_}) } SL::DB::AuthUser::CONFIG_VARS(),
 
 764   $main::lxdebug->leave_sub();
 
 770   $main::lxdebug->enter_sub();
 
 775   my $myconfig = \%main::myconfig;
 
 776   my $form     = $main::form;
 
 778   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
 
 780   my $defaults = selectfirst_hashref_query($form, $dbh, qq|SELECT * FROM defaults|) || {};
 
 782   $defaults->{weightunit} ||= 'kg';
 
 784   $main::lxdebug->leave_sub();
 
 790   $main::lxdebug->enter_sub();
 
 792   my ($self, $myconfig, $form) = @_;
 
 794   my $dbh = $form->dbconnect($myconfig);
 
 796   my $query = qq|SELECT closedto, max_future_booking_interval, revtrans FROM defaults|;
 
 797   my $sth   = $dbh->prepare($query);
 
 798   $sth->execute || $form->dberror($query);
 
 800   ($form->{closedto}, $form->{max_future_booking_interval}, $form->{revtrans}) = $sth->fetchrow_array;
 
 806   $main::lxdebug->leave_sub();
 
 810   $main::lxdebug->enter_sub();
 
 812   my ($self, $myconfig, $form) = @_;
 
 814   my $dbh = $form->dbconnect($myconfig);
 
 816   my ($query, @values);
 
 818   # is currently NEVER trueish (no more hidden revtrans in $form)
 
 819   # if ($form->{revtrans}) {
 
 820   #   $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '1'|;
 
 821   # -> therefore you can only set this to false (which is already the default)
 
 822   # and this flag is currently only checked in gl.pl. TOOD Can probably be removed
 
 824     $query = qq|UPDATE defaults SET closedto = ?, max_future_booking_interval = ?, revtrans = '0'|;
 
 825     @values = (conv_date($form->{closedto}), conv_i($form->{max_future_booking_interval}));
 
 827   # set close in defaults
 
 828   do_query($form, $dbh, $query, @values);
 
 832   $main::lxdebug->leave_sub();
 
 836   my ($self, $units, $unit_name, $factor) = @_;
 
 838   $factor = 1 unless ($factor);
 
 840   my $unit = $units->{$unit_name};
 
 842   if (!defined($unit) || !$unit->{"base_unit"} ||
 
 843       ($unit_name eq $unit->{"base_unit"})) {
 
 844     return ($unit_name, $factor);
 
 847   return AM->get_base_unit($units, $unit->{"base_unit"}, $factor * $unit->{"factor"});
 
 851   $main::lxdebug->enter_sub();
 
 853   my ($self, $myconfig, $form, $prefix) = @_;
 
 856   my $dbh = $form->get_standard_dbh;
 
 858   my $query = "SELECT *, base_unit AS original_base_unit FROM units";
 
 860   my $sth = prepare_execute_query($form, $dbh, $query);
 
 863   while (my $ref = $sth->fetchrow_hashref()) {
 
 864     $units->{$ref->{"name"}} = $ref;
 
 868   my $query_lang = "SELECT id, template_code FROM language ORDER BY description";
 
 869   $sth = $dbh->prepare($query_lang);
 
 870   $sth->execute() || $form->dberror($query_lang);
 
 872   while (my $ref = $sth->fetchrow_hashref()) {
 
 873     push(@languages, $ref);
 
 877   $query_lang = "SELECT ul.localized, ul.localized_plural, l.id, l.template_code " .
 
 878     "FROM units_language ul " .
 
 879     "LEFT JOIN language l ON ul.language_id = l.id " .
 
 881   $sth = $dbh->prepare($query_lang);
 
 883   foreach my $unit (values(%{$units})) {
 
 884     ($unit->{"${prefix}base_unit"}, $unit->{"${prefix}factor"}) = AM->get_base_unit($units, $unit->{"name"});
 
 886     $unit->{"LANGUAGES"} = {};
 
 887     foreach my $lang (@languages) {
 
 888       $unit->{"LANGUAGES"}->{$lang->{"template_code"}} = { "template_code" => $lang->{"template_code"} };
 
 891     $sth->execute($unit->{"name"}) || $form->dberror($query_lang . " (" . $unit->{"name"} . ")");
 
 892     while (my $ref = $sth->fetchrow_hashref()) {
 
 893       map({ $unit->{"LANGUAGES"}->{$ref->{"template_code"}}->{$_} = $ref->{$_} } keys(%{$ref}));
 
 898   $main::lxdebug->leave_sub();
 
 903 sub retrieve_all_units {
 
 904   $main::lxdebug->enter_sub();
 
 908   if (!$::request->{cache}{all_units}) {
 
 909     $::request->{cache}{all_units} = $self->retrieve_units(\%main::myconfig, $main::form);
 
 912   $main::lxdebug->leave_sub();
 
 914   return $::request->{cache}{all_units};
 
 918 sub translate_units {
 
 919   $main::lxdebug->enter_sub();
 
 921   my ($self, $form, $template_code, $unit, $amount) = @_;
 
 923   my $units = $self->retrieve_units(\%main::myconfig, $form);
 
 925   my $h = $units->{$unit}->{"LANGUAGES"}->{$template_code};
 
 926   my $new_unit = $unit;
 
 928     if (($amount != 1) && $h->{"localized_plural"}) {
 
 929       $new_unit = $h->{"localized_plural"};
 
 930     } elsif ($h->{"localized"}) {
 
 931       $new_unit = $h->{"localized"};
 
 935   $main::lxdebug->leave_sub();
 
 941   $main::lxdebug->enter_sub();
 
 943   my ($self, $myconfig, $form, $units) = @_;
 
 945   my $dbh = $form->dbconnect($myconfig);
 
 947   map({ $_->{"in_use"} = 0; } values(%{$units}));
 
 949   foreach my $unit (values(%{$units})) {
 
 950     my $base_unit = $unit->{"original_base_unit"};
 
 952       $units->{$base_unit}->{"in_use"} = 1;
 
 953       $units->{$base_unit}->{"DEPENDING_UNITS"} = [] unless ($units->{$base_unit}->{"DEPENDING_UNITS"});
 
 954       push(@{$units->{$base_unit}->{"DEPENDING_UNITS"}}, $unit->{"name"});
 
 955       $base_unit = $units->{$base_unit}->{"original_base_unit"};
 
 959   foreach my $unit (values(%{$units})) {
 
 960     map({ $_ = $dbh->quote($_); } @{$unit->{"DEPENDING_UNITS"}});
 
 962     foreach my $table (qw(parts invoice orderitems)) {
 
 963       my $query = "SELECT COUNT(*) FROM $table WHERE unit ";
 
 965       if (0 == scalar(@{$unit->{"DEPENDING_UNITS"}})) {
 
 966         $query .= "= " . $dbh->quote($unit->{"name"});
 
 968         $query .= "IN (" . $dbh->quote($unit->{"name"}) . "," .
 
 969           join(",", map({ $dbh->quote($_) } @{$unit->{"DEPENDING_UNITS"}})) . ")";
 
 972       my ($count) = $dbh->selectrow_array($query);
 
 973       $form->dberror($query) if ($dbh->err);
 
 976         $unit->{"in_use"} = 1;
 
 984   $main::lxdebug->leave_sub();
 
 987 sub convertible_units {
 
 988   $main::lxdebug->enter_sub();
 
 992   my $filter_unit = shift;
 
 993   my $not_smaller = shift;
 
 997   $filter_unit = $units->{$filter_unit};
 
 999   foreach my $name (sort { lc $a cmp lc $b } keys %{ $units }) {
 
1000     my $unit = $units->{$name};
 
1002     if (($unit->{base_unit} eq $filter_unit->{base_unit}) &&
 
1003         (!$not_smaller || ($unit->{factor} >= $filter_unit->{factor}))) {
 
1004       push @{$conv_units}, $unit;
 
1008   my @sorted = sort { $b->{factor} <=> $a->{factor} } @{ $conv_units };
 
1010   $main::lxdebug->leave_sub();
 
1015 # if $a is translatable to $b, return the factor between them.
 
1018   $main::lxdebug->enter_sub(2);
 
1019   my ($this, $a, $b, $all_units) = @_;
 
1022     $all_units = $this->retrieve_all_units;
 
1025   $main::lxdebug->leave_sub(2) and return 0 unless $a && $b;
 
1026   $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a} && $all_units->{$b};
 
1027   $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a}{base_unit} eq $all_units->{$b}{base_unit};
 
1028   $main::lxdebug->leave_sub(2) and return $all_units->{$a}{factor} / $all_units->{$b}{factor};
 
1031 sub unit_select_data {
 
1032   $main::lxdebug->enter_sub();
 
1034   my ($self, $units, $selected, $empty_entry, $convertible_into) = @_;
 
1039     push(@{$select}, { "name" => "", "base_unit" => "", "factor" => "", "selected" => "" });
 
1042   foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
 
1043     if (!$convertible_into ||
 
1044         ($units->{$convertible_into} &&
 
1045          ($units->{$convertible_into}->{base_unit} eq $units->{$unit}->{base_unit}))) {
 
1046       push @{$select}, { "name"      => $unit,
 
1047                          "base_unit" => $units->{$unit}->{"base_unit"},
 
1048                          "factor"    => $units->{$unit}->{"factor"},
 
1049                          "selected"  => ($unit eq $selected) ? "selected" : "" };
 
1053   $main::lxdebug->leave_sub();
 
1058 sub unit_select_html {
 
1059   $main::lxdebug->enter_sub();
 
1061   my ($self, $units, $name, $selected, $convertible_into) = @_;
 
1063   my $select = "<select name=${name}>";
 
1065   foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
 
1066     if (!$convertible_into ||
 
1067         ($units->{$convertible_into} &&
 
1068          ($units->{$convertible_into}->{"base_unit"} eq $units->{$unit}->{"base_unit"}))) {
 
1069       $select .= "<option" . (($unit eq $selected) ? " selected" : "") . ">${unit}</option>";
 
1072   $select .= "</select>";
 
1074   $main::lxdebug->leave_sub();
 
1080   $main::lxdebug->enter_sub();
 
1084   my $units = $self->retrieve_all_units();
 
1089   while (2 <= scalar(@_)) {
 
1090     my $qty  = shift(@_);
 
1091     my $unit = $units->{shift(@_)};
 
1093     croak "No unit defined with name $unit" if (!defined $unit);
 
1096       $base_unit = $unit->{base_unit};
 
1097     } elsif ($base_unit ne $unit->{base_unit}) {
 
1098       croak "Adding values with incompatible base units $base_unit/$unit->{base_unit}";
 
1101     $sum += $qty * $unit->{factor};
 
1104   $main::lxdebug->leave_sub();
 
1110   $main::lxdebug->enter_sub();
 
1112   my ($self, $myconfig, $form, $name, $base_unit, $factor, $languages) = @_;
 
1114   my $dbh = $form->dbconnect_noauto($myconfig);
 
1116   my $query = qq|SELECT COALESCE(MAX(sortkey), 0) + 1 FROM units|;
 
1117   my ($sortkey) = selectrow_query($form, $dbh, $query);
 
1119   $query = "INSERT INTO units (name, base_unit, factor, sortkey) " .
 
1120     "VALUES (?, ?, ?, ?)";
 
1121   do_query($form, $dbh, $query, $name, $base_unit, $factor, $sortkey);
 
1124     $query = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
 
1125     my $sth = $dbh->prepare($query);
 
1126     foreach my $lang (@{$languages}) {
 
1127       my @values = ($name, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
 
1128       $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
 
1136   $main::lxdebug->leave_sub();
 
1140   $main::lxdebug->enter_sub();
 
1142   my ($self, $myconfig, $form, $units, $delete_units) = @_;
 
1144   my $dbh = $form->dbconnect_noauto($myconfig);
 
1146   my ($base_unit, $unit, $sth, $query);
 
1148   $query = "DELETE FROM units_language";
 
1149   $dbh->do($query) || $form->dberror($query);
 
1151   if ($delete_units && (0 != scalar(@{$delete_units}))) {
 
1152     $query = "DELETE FROM units WHERE name IN (";
 
1153     map({ $query .= "?," } @{$delete_units});
 
1154     substr($query, -1, 1) = ")";
 
1155     $dbh->do($query, undef, @{$delete_units}) ||
 
1156       $form->dberror($query . " (" . join(", ", @{$delete_units}) . ")");
 
1159   $query = "UPDATE units SET name = ?, base_unit = ?, factor = ? WHERE name = ?";
 
1160   $sth = $dbh->prepare($query);
 
1162   my $query_lang = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
 
1163   my $sth_lang = $dbh->prepare($query_lang);
 
1165   foreach $unit (values(%{$units})) {
 
1166     $unit->{"depth"} = 0;
 
1167     my $base_unit = $unit;
 
1168     while ($base_unit->{"base_unit"}) {
 
1170       $base_unit = $units->{$base_unit->{"base_unit"}};
 
1174   foreach $unit (sort({ $a->{"depth"} <=> $b->{"depth"} } values(%{$units}))) {
 
1175     if ($unit->{"LANGUAGES"}) {
 
1176       foreach my $lang (@{$unit->{"LANGUAGES"}}) {
 
1177         next unless ($lang->{"id"} && $lang->{"localized"});
 
1178         my @values = ($unit->{"name"}, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
 
1179         $sth_lang->execute(@values) || $form->dberror($query_lang . " (" . join(", ", @values) . ")");
 
1183     next if ($unit->{"unchanged_unit"});
 
1185     my @values = ($unit->{"name"}, $unit->{"base_unit"}, $unit->{"factor"}, $unit->{"old_name"});
 
1186     $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
 
1190   $sth_lang->finish();
 
1194   $main::lxdebug->leave_sub();
 
1198   $main::lxdebug->enter_sub();
 
1200   my ($self, $myconfig, $form) = @_;
 
1202   # connect to database
 
1203   my $dbh = $form->dbconnect($myconfig);
 
1205   my $query = qq|SELECT
 
1209                    round(t.rate * 100, 2) AS rate,
 
1210                    (SELECT accno FROM chart WHERE id = chart_id) AS taxnumber,
 
1211                    (SELECT description FROM chart WHERE id = chart_id) AS account_description,
 
1212                    (SELECT accno FROM chart WHERE id = skonto_sales_chart_id) AS skonto_chart_accno,
 
1213                    (SELECT description FROM chart WHERE id = skonto_sales_chart_id) AS skonto_chart_description,
 
1214                    (SELECT accno FROM chart WHERE id = skonto_purchase_chart_id) AS skonto_chart_purchase_accno,
 
1215                    (SELECT description FROM chart WHERE id = skonto_purchase_chart_id) AS skonto_chart_purchase_description
 
1217                  ORDER BY taxkey, rate|;
 
1219   my $sth = $dbh->prepare($query);
 
1220   $sth->execute || $form->dberror($query);
 
1223   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
1224     push @{ $form->{TAX} }, $ref;
 
1230   $main::lxdebug->leave_sub();
 
1233 sub get_tax_accounts {
 
1234   $main::lxdebug->enter_sub();
 
1236   my ($self, $myconfig, $form) = @_;
 
1238   my $dbh = $form->dbconnect($myconfig);
 
1240   # get Accounts from chart
 
1241   my $query = qq{ SELECT
 
1243                  accno || ' - ' || description AS taxaccount
 
1245                WHERE link LIKE '%_tax%'
 
1249   my $sth = $dbh->prepare($query);
 
1250   $sth->execute || $form->dberror($query);
 
1252   $form->{ACCOUNTS} = [];
 
1253   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
1254     push @{ $form->{ACCOUNTS} }, $ref;
 
1257   $form->{AR_PAID} = SL::DB::Manager::Chart->get_all(where => [ link => { like => '%AR_paid%' } ], sort_by => 'accno ASC');
 
1258   $form->{AP_PAID} = SL::DB::Manager::Chart->get_all(where => [ link => { like => '%AP_paid%' } ], sort_by => 'accno ASC');
 
1260   $form->{skontochart_value_title_sub} = sub {
 
1264       $item->{accno} .' '. $item->{description},
 
1272   $main::lxdebug->leave_sub();
 
1276   $main::lxdebug->enter_sub();
 
1278   my ($self, $myconfig, $form) = @_;
 
1280   # connect to database
 
1281   my $dbh = $form->dbconnect($myconfig);
 
1283   my $query = qq|SELECT
 
1286                    round(rate * 100, 2) AS rate,
 
1289                    (id IN (SELECT tax_id
 
1290                            FROM acc_trans)) AS tax_already_used,
 
1291                    skonto_sales_chart_id,
 
1292                    skonto_purchase_chart_id
 
1296   my $sth = $dbh->prepare($query);
 
1297   $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
1299   my $ref = $sth->fetchrow_hashref("NAME_lc");
 
1301   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
1305   # see if it is used by a taxkey
 
1306   $query = qq|SELECT count(*) FROM taxkeys
 
1307               WHERE tax_id = ? AND chart_id >0|;
 
1309   ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
 
1311   $form->{orphaned} = !$form->{orphaned};
 
1314   if (!$form->{orphaned} ) {
 
1315     $query = qq|SELECT DISTINCT c.id, c.accno
 
1317                 JOIN   tax t ON (t.id = tk.tax_id)
 
1318                 JOIN chart c ON (c.id = tk.chart_id)
 
1319                 WHERE tk.tax_id = ?|;
 
1321     $sth = $dbh->prepare($query);
 
1322     $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
1324     $form->{TAXINUSE} = [];
 
1325     while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
1326       push @{ $form->{TAXINUSE} }, $ref;
 
1334   $main::lxdebug->leave_sub();
 
1338   $main::lxdebug->enter_sub();
 
1340   my ($self, $myconfig, $form) = @_;
 
1343   # connect to database
 
1344   my $dbh = $form->get_standard_dbh($myconfig);
 
1346   $form->{rate} = $form->{rate} / 100;
 
1348   my $chart_categories = '';
 
1349   $chart_categories .= 'A' if $form->{asset};
 
1350   $chart_categories .= 'L' if $form->{liability};
 
1351   $chart_categories .= 'Q' if $form->{equity};
 
1352   $chart_categories .= 'I' if $form->{revenue};
 
1353   $chart_categories .= 'E' if $form->{expense};
 
1354   $chart_categories .= 'C' if $form->{costs};
 
1356   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);
 
1357   if ($form->{id} ne "") {
 
1358     $query = qq|UPDATE tax SET
 
1363                   taxnumber                = (SELECT accno FROM chart WHERE id = ? ),
 
1364                   skonto_sales_chart_id    = ?,
 
1365                   skonto_purchase_chart_id = ?,
 
1366                   chart_categories         = ?
 
1371     ($form->{id}) = selectfirst_array_query($form, $dbh, qq|SELECT nextval('id')|);
 
1372     $query = qq|INSERT INTO tax (
 
1378                   skonto_sales_chart_id,
 
1379                   skonto_purchase_chart_id,
 
1383                 VALUES (?, ?, ?, ?, (SELECT accno FROM chart WHERE id = ?), ?, ?,  ?, ?)|;
 
1385   push(@values, $form->{id});
 
1386   do_query($form, $dbh, $query, @values);
 
1388   foreach my $language_id (keys %{ $form->{translations} }) {
 
1389     GenericTranslations->save('dbh'              => $dbh,
 
1390                               'translation_type' => 'SL::DB::Tax/taxdescription',
 
1391                               'translation_id'   => $form->{id},
 
1392                               'language_id'      => $language_id,
 
1393                               'translation'      => $form->{translations}->{$language_id});
 
1398   $main::lxdebug->leave_sub();
 
1402   $main::lxdebug->enter_sub();
 
1404   my ($self, $myconfig, $form) = @_;
 
1407   # connect to database
 
1408   my $dbh = $form->get_standard_dbh($myconfig);
 
1410   $query = qq|DELETE FROM tax
 
1412   do_query($form, $dbh, $query, $form->{id});
 
1416   $main::lxdebug->leave_sub();
 
1419 sub save_price_factor {
 
1420   $main::lxdebug->enter_sub();
 
1422   my ($self, $myconfig, $form) = @_;
 
1424   # connect to database
 
1425   my $dbh = $form->get_standard_dbh($myconfig);
 
1428   my @values = ($form->{description}, conv_i($form->{factor}));
 
1431     $query = qq|UPDATE price_factors SET description = ?, factor = ? WHERE id = ?|;
 
1432     push @values, conv_i($form->{id});
 
1435     $query = qq|INSERT INTO price_factors (description, factor, sortkey) VALUES (?, ?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM price_factors))|;
 
1438   do_query($form, $dbh, $query, @values);
 
1442   $main::lxdebug->leave_sub();
 
1445 sub get_all_price_factors {
 
1446   $main::lxdebug->enter_sub();
 
1448   my ($self, $myconfig, $form) = @_;
 
1450   # connect to database
 
1451   my $dbh = $form->get_standard_dbh($myconfig);
 
1453   $form->{PRICE_FACTORS} = selectall_hashref_query($form, $dbh, qq|SELECT * FROM price_factors ORDER BY sortkey|);
 
1455   $main::lxdebug->leave_sub();
 
1458 sub get_price_factor {
 
1459   $main::lxdebug->enter_sub();
 
1461   my ($self, $myconfig, $form) = @_;
 
1463   # connect to database
 
1464   my $dbh = $form->get_standard_dbh($myconfig);
 
1466   my $query = qq|SELECT description, factor,
 
1467                    ((SELECT COUNT(*) FROM parts      WHERE price_factor_id = ?) +
 
1468                     (SELECT COUNT(*) FROM invoice    WHERE price_factor_id = ?) +
 
1469                     (SELECT COUNT(*) FROM orderitems WHERE price_factor_id = ?)) = 0 AS orphaned
 
1470                  FROM price_factors WHERE id = ?|;
 
1472   ($form->{description}, $form->{factor}, $form->{orphaned}) = selectrow_query($form, $dbh, $query, (conv_i($form->{id})) x 4);
 
1474   $main::lxdebug->leave_sub();
 
1477 sub delete_price_factor {
 
1478   $main::lxdebug->enter_sub();
 
1480   my ($self, $myconfig, $form) = @_;
 
1482   # connect to database
 
1483   my $dbh = $form->get_standard_dbh($myconfig);
 
1485   do_query($form, $dbh, qq|DELETE FROM price_factors WHERE id = ?|, conv_i($form->{id}));
 
1488   $main::lxdebug->leave_sub();
 
1491 sub save_warehouse {
 
1492   $main::lxdebug->enter_sub();
 
1494   my ($self, $myconfig, $form) = @_;
 
1496   # connect to database
 
1497   my $dbh = $form->get_standard_dbh($myconfig);
 
1499   my ($query, @values, $sth);
 
1502     $query        = qq|SELECT nextval('id')|;
 
1503     ($form->{id}) = selectrow_query($form, $dbh, $query);
 
1505     $query        = qq|INSERT INTO warehouse (id, sortkey) VALUES (?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM warehouse))|;
 
1506     do_query($form, $dbh, $query, $form->{id});
 
1509   do_query($form, $dbh, qq|UPDATE warehouse SET description = ?, invalid = ? WHERE id = ?|,
 
1510            $form->{description}, $form->{invalid} ? 't' : 'f', conv_i($form->{id}));
 
1512   if (0 < $form->{number_of_new_bins}) {
 
1513     my ($num_existing_bins) = selectfirst_array_query($form, $dbh, qq|SELECT COUNT(*) FROM bin WHERE warehouse_id = ?|, $form->{id});
 
1514     $query = qq|INSERT INTO bin (warehouse_id, description) VALUES (?, ?)|;
 
1515     $sth   = prepare_query($form, $dbh, $query);
 
1517     foreach my $i (1..$form->{number_of_new_bins}) {
 
1518       do_statement($form, $sth, $query, conv_i($form->{id}), "$form->{prefix}" . ($i + $num_existing_bins));
 
1526   $main::lxdebug->leave_sub();
 
1530   $main::lxdebug->enter_sub();
 
1532   my ($self, $myconfig, $form) = @_;
 
1534   # connect to database
 
1535   my $dbh = $form->get_standard_dbh($myconfig);
 
1537   my ($query, @values, $commit_necessary, $sth);
 
1539   @values = map { $form->{"id_${_}"} } grep { $form->{"delete_${_}"} } (1..$form->{rowcount});
 
1542     $query = qq|DELETE FROM bin WHERE id IN (| . join(', ', ('?') x scalar(@values)) . qq|)|;
 
1543     do_query($form, $dbh, $query, @values);
 
1545     $commit_necessary = 1;
 
1548   $query = qq|UPDATE bin SET description = ? WHERE id = ?|;
 
1549   $sth   = prepare_query($form, $dbh, $query);
 
1551   foreach my $row (1..$form->{rowcount}) {
 
1552     next if ($form->{"delete_${row}"});
 
1554     do_statement($form, $sth, $query, $form->{"description_${row}"}, conv_i($form->{"id_${row}"}));
 
1556     $commit_necessary = 1;
 
1561   $dbh->commit() if ($commit_necessary);
 
1563   $main::lxdebug->leave_sub();
 
1566 sub delete_warehouse {
 
1567   $main::lxdebug->enter_sub();
 
1569   my ($self, $myconfig, $form) = @_;
 
1571   # connect to database
 
1572   my $dbh = $form->get_standard_dbh($myconfig);
 
1574   my $id      = conv_i($form->{id});
 
1575   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|;
 
1576   my ($count) = selectrow_query($form, $dbh, $query, $id);
 
1579     $main::lxdebug->leave_sub();
 
1583   do_query($form, $dbh, qq|DELETE FROM bin       WHERE warehouse_id = ?|, conv_i($form->{id}));
 
1584   do_query($form, $dbh, qq|DELETE FROM warehouse WHERE id           = ?|, conv_i($form->{id}));
 
1588   $main::lxdebug->leave_sub();
 
1593 sub get_all_warehouses {
 
1594   $main::lxdebug->enter_sub();
 
1596   my ($self, $myconfig, $form) = @_;
 
1598   # connect to database
 
1599   my $dbh = $form->get_standard_dbh($myconfig);
 
1601   my $query = qq|SELECT w.id, w.description, w.invalid,
 
1602                    (SELECT COUNT(b.description) FROM bin b WHERE b.warehouse_id = w.id) AS number_of_bins
 
1604                  ORDER BY w.sortkey|;
 
1606   $form->{WAREHOUSES} = selectall_hashref_query($form, $dbh, $query);
 
1608   $main::lxdebug->leave_sub();
 
1612   $main::lxdebug->enter_sub();
 
1614   my ($self, $myconfig, $form) = @_;
 
1616   # connect to database
 
1617   my $dbh = $form->get_standard_dbh($myconfig);
 
1619   my $id    = conv_i($form->{id});
 
1620   my $query = qq|SELECT w.description, w.invalid
 
1624   my $ref   = selectfirst_hashref_query($form, $dbh, $query, $id);
 
1626   map { $form->{$_} = $ref->{$_} } keys %{ $ref };
 
1630       (   EXISTS(SELECT i.bin_id FROM inventory i WHERE i.bin_id = b.id LIMIT 1)
 
1631        OR EXISTS(SELECT p.bin_id FROM parts     p WHERE p.bin_id = b.id LIMIT 1))
 
1634     WHERE b.warehouse_id = ?
 
1637   $form->{BINS} = selectall_hashref_query($form, $dbh, $query, conv_i($form->{id}));
 
1639   $main::lxdebug->leave_sub();