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);
 
  48 use SL::GenericTranslations;
 
  53   $main::lxdebug->enter_sub();
 
  55   my ($self, $myconfig, $form) = @_;
 
  58   my $dbh = $form->dbconnect($myconfig);
 
  60     SELECT c.accno, c.description, c.charttype, c.category,
 
  61       c.link, c.pos_bilanz, c.pos_eur, c.new_chart_id, c.valid_from,
 
  62       c.pos_bwa, datevautomatik,
 
  63       tk.taxkey_id, tk.pos_ustva, tk.tax_id,
 
  64       tk.tax_id || '--' || tk.taxkey_id AS tax, tk.startdate
 
  67     ON (c.id=tk.chart_id AND tk.id =
 
  68       (SELECT id FROM taxkeys
 
  69        WHERE taxkeys.chart_id = c.id AND startdate <= current_date
 
  70        ORDER BY startdate DESC LIMIT 1))
 
  75   $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
 
  76   my $sth = $dbh->prepare($query);
 
  77   $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
  79   my $ref = $sth->fetchrow_hashref("NAME_lc");
 
  81   foreach my $key (keys %$ref) {
 
  82     $form->{"$key"} = $ref->{"$key"};
 
  87   # get default accounts
 
  88   $query = qq|SELECT inventory_accno_id, income_accno_id, expense_accno_id
 
  90   $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
 
  91   $sth = $dbh->prepare($query);
 
  92   $sth->execute || $form->dberror($query);
 
  94   $ref = $sth->fetchrow_hashref("NAME_lc");
 
  96   map { $form->{$_} = $ref->{$_} } keys %{ $ref };
 
 102   # get taxkeys and description
 
 106       (SELECT accno FROM chart WHERE id=tax.chart_id) AS chart_accno,
 
 108       id||'--'||taxkey AS tax,
 
 111     FROM tax ORDER BY taxkey
 
 113   $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
 
 114   $sth = $dbh->prepare($query);
 
 115   $sth->execute || $form->dberror($query);
 
 117   $form->{TAXKEY} = [];
 
 119   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 120     push @{ $form->{TAXKEY} }, $ref;
 
 126     $query = qq|SELECT id, accno,description
 
 130     $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
 
 131     $sth = $dbh->prepare($query);
 
 132     $sth->execute($form->{link}) || $form->dberror($query . " ($form->{link})");
 
 134     $form->{NEWACCOUNT} = [];
 
 135     while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 136       push @{ $form->{NEWACCOUNT} }, $ref;
 
 141     # get the taxkeys of account
 
 155       LEFT JOIN   tax t ON (t.id = tk.tax_id)
 
 156       LEFT JOIN chart c ON (c.id = t.chart_id)
 
 158       WHERE tk.chart_id = ?
 
 159       ORDER BY startdate DESC
 
 161     $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
 
 162     $sth = $dbh->prepare($query);
 
 164     $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
 166     $form->{ACCOUNT_TAXKEYS} = [];
 
 168     while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 169       push @{ $form->{ACCOUNT_TAXKEYS} }, $ref;
 
 175   # check if we have any transactions
 
 176   $query = qq|SELECT a.trans_id FROM acc_trans a
 
 177               WHERE a.chart_id = ?|;
 
 178   $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
 
 179   $sth = $dbh->prepare($query);
 
 180   $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
 182   ($form->{orphaned}) = $sth->fetchrow_array;
 
 183   $form->{orphaned} = !$form->{orphaned};
 
 186   # check if new account is active
 
 187   $form->{new_chart_valid} = 0;
 
 188   if ($form->{new_chart_id}) {
 
 189     $query = qq|SELECT current_date-valid_from FROM chart
 
 191     $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
 
 192     my ($count) = selectrow_query($form, $dbh, $query, $form->{id});
 
 194       $form->{new_chart_valid} = 1;
 
 201   $main::lxdebug->leave_sub();
 
 205   $main::lxdebug->enter_sub();
 
 207   # TODO: it should be forbidden to change an account to a heading if there
 
 208   # have been bookings to this account in the past
 
 210   my ($self, $myconfig, $form) = @_;
 
 212   # connect to database, turn off AutoCommit
 
 213   my $dbh = $form->dbconnect_noauto($myconfig);
 
 215   for (qw(AR_include_in_dropdown AP_include_in_dropdown summary_account)) {
 
 216     $form->{$form->{$_}} = $form->{$_} if $form->{$_};
 
 219   # sanity check, can't have AR with AR_...
 
 220   if ($form->{AR} || $form->{AP} || $form->{IC}) {
 
 221     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)) {
 
 222       $form->error($::locale->text('It is not allowed that a summary account occurs in a drop-down menu!'));
 
 227   foreach my $item ($form->{AR},            $form->{AR_amount},
 
 228                     $form->{AR_tax},        $form->{AR_paid},
 
 229                     $form->{AP},            $form->{AP_amount},
 
 230                     $form->{AP_tax},        $form->{AP_paid},
 
 231                     $form->{IC},            $form->{IC_sale},
 
 232                     $form->{IC_cogs},       $form->{IC_taxpart},
 
 233                     $form->{IC_income},     $form->{IC_expense},
 
 234                     $form->{IC_taxservice}
 
 236     $form->{link} .= "${item}:" if ($item);
 
 240   # strip blanks from accno
 
 241   map { $form->{$_} =~ s/ //g; } qw(accno);
 
 245   if ($form->{id} eq "NULL") {
 
 254   my @values = ($form->{accno});
 
 257     $query .= ' AND NOT id = ?';
 
 258     push(@values, $form->{id});
 
 261   my ($accno) = selectrow_query($form, $dbh, $query, @values);
 
 264     $form->error($::locale->text('Account number not unique!'));
 
 268   if (!$form->{id} || $form->{id} eq "") {
 
 269     $query = qq|SELECT nextval('id')|;
 
 270     ($form->{"id"}) = selectrow_query($form, $dbh, $query);
 
 271     $query = qq|INSERT INTO chart (id, accno, link) VALUES (?, ?, ?)|;
 
 272     do_query($form, $dbh, $query, $form->{"id"}, $form->{"accno"}, '');
 
 280     # if charttype is heading make sure certain values are empty
 
 281     # specifically, if charttype is changed from an existing account, empty the
 
 282     # fields unnecessary for headings, so that e.g. heading doesn't appear in
 
 283     # drop-down menues due to still having a valid "link" entry
 
 285     if ( $form->{charttype} eq 'H' ) {
 
 287       $form->{pos_bwa} = '';
 
 288       $form->{pos_bilanz} = '';
 
 289       $form->{pos_eur} = '';
 
 290       $form->{new_chart_id} = '';
 
 291       $form->{valid_from} = '';
 
 294     $query = qq|UPDATE chart SET
 
 310                   $form->{description},
 
 314                   conv_i($form->{pos_bwa}),
 
 315                   conv_i($form->{pos_bilanz}),
 
 316                   conv_i($form->{pos_eur}),
 
 317                   conv_i($form->{new_chart_id}),
 
 318                   conv_date($form->{valid_from}),
 
 319                   ($form->{datevautomatik} eq 'T') ? 'true':'false',
 
 326   do_query($form, $dbh, $query, @values);
 
 332   my $MAX_TRIES = 10; # Maximum count of taxkeys in form
 
 336   for $tk_count (0 .. $MAX_TRIES) {
 
 340     # Check if the account already exists, else cancel
 
 342     print(STDERR "Keine Taxkeys weil ID =: $form->{id}\n");
 
 344     last READTAXKEYS if ( $form->{'id'} == 0);
 
 346     # check if there is a startdate
 
 347     if ( $form->{"taxkey_startdate_$tk_count"} eq '' ) {
 
 352     # Add valid taxkeys into the array
 
 355         id        => ($form->{"taxkey_id_$tk_count"} eq 'NEW') ? conv_i('') : conv_i($form->{"taxkey_id_$tk_count"}),
 
 356         tax_id    => conv_i($form->{"taxkey_tax_$tk_count"}),
 
 357         startdate => conv_date($form->{"taxkey_startdate_$tk_count"}),
 
 358         chart_id  => conv_i($form->{"id"}),
 
 359         pos_ustva => conv_i($form->{"taxkey_pos_ustva_$tk_count"}),
 
 360         delete    => ( $form->{"taxkey_del_$tk_count"} eq 'delete' ) ? '1' : '',
 
 367   for my $j (0 .. $#taxkeys){
 
 368     if ( defined $taxkeys[$j]{'id'} ){
 
 371       if ($taxkeys[$j]{'delete'}){
 
 373           DELETE FROM taxkeys WHERE id = ?
 
 376         @values = ($taxkeys[$j]{'id'});
 
 378         do_query($form, $dbh, $query, @values);
 
 387         SET taxkey_id = (SELECT taxkey FROM tax WHERE tax.id = ?),
 
 395         $taxkeys[$j]{'tax_id'},
 
 396         $taxkeys[$j]{'chart_id'},
 
 397         $taxkeys[$j]{'tax_id'},
 
 398         $taxkeys[$j]{'pos_ustva'},
 
 399         $taxkeys[$j]{'startdate'},
 
 402       do_query($form, $dbh, $query, @values);
 
 408         INSERT INTO taxkeys (
 
 415         VALUES ((SELECT taxkey FROM tax WHERE tax.id = ?), ?, ?, ?, ?)
 
 418         $taxkeys[$j]{'tax_id'},
 
 419         $taxkeys[$j]{'chart_id'},
 
 420         $taxkeys[$j]{'tax_id'},
 
 421         $taxkeys[$j]{'pos_ustva'},
 
 422         $taxkeys[$j]{'startdate'},
 
 425       do_query($form, $dbh, $query, @values);
 
 430   # Update chart.taxkey_id to the latest from taxkeys for this chart.
 
 436       WHERE taxkeys.chart_id = chart.id
 
 437       ORDER BY startdate DESC
 
 443   do_query($form, $dbh, $query, $form->{id});
 
 446   my $rc = $dbh->commit;
 
 449   $main::lxdebug->leave_sub();
 
 455   $main::lxdebug->enter_sub();
 
 457   my ($self, $myconfig, $form) = @_;
 
 459   # connect to database, turn off AutoCommit
 
 460   my $dbh = $form->dbconnect_noauto($myconfig);
 
 462   my $query = qq|SELECT count(*) FROM acc_trans a
 
 463                  WHERE a.chart_id = ?|;
 
 464   my ($count) = selectrow_query($form, $dbh, $query, $form->{id});
 
 468     $main::lxdebug->leave_sub();
 
 472   # set inventory_accno_id, income_accno_id, expense_accno_id to defaults
 
 473   foreach my $type (qw(inventory income expense)) {
 
 476       qq|SET ${type}_accno_id = (SELECT ${type}_accno_id FROM defaults) | .
 
 477       qq|WHERE ${type}_accno_id = ?|;
 
 478     do_query($form, $dbh, $query, $form->{id});
 
 481   $query = qq|DELETE FROM tax
 
 483   do_query($form, $dbh, $query, $form->{id});
 
 485   # delete account taxkeys
 
 486   $query = qq|DELETE FROM taxkeys
 
 488   do_query($form, $dbh, $query, $form->{id});
 
 490   # delete chart of account record
 
 491   # last step delete chart, because we have a constraint
 
 493   $query = qq|DELETE FROM chart
 
 495   do_query($form, $dbh, $query, $form->{id});
 
 497   # commit and redirect
 
 498   my $rc = $dbh->commit;
 
 501   $main::lxdebug->leave_sub();
 
 507   $main::lxdebug->enter_sub();
 
 509   my ($self, $myconfig, $form) = @_;
 
 511   # connect to database
 
 512   my $dbh = $form->dbconnect($myconfig);
 
 514   my $query = qq|SELECT id, lead
 
 518   my $sth = $dbh->prepare($query);
 
 519   $sth->execute || $form->dberror($query);
 
 521   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 522     push @{ $form->{ALL} }, $ref;
 
 528   $main::lxdebug->leave_sub();
 
 532   $main::lxdebug->enter_sub();
 
 534   my ($self, $myconfig, $form) = @_;
 
 536   # connect to database
 
 537   my $dbh = $form->dbconnect($myconfig);
 
 540     qq|SELECT l.id, l.lead | .
 
 543   my $sth = $dbh->prepare($query);
 
 544   $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
 546   my $ref = $sth->fetchrow_hashref("NAME_lc");
 
 548   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
 554   $main::lxdebug->leave_sub();
 
 558   $main::lxdebug->enter_sub();
 
 560   my ($self, $myconfig, $form) = @_;
 
 563   # connect to database
 
 564   my $dbh = $form->dbconnect($myconfig);
 
 566   my @values = ($form->{description});
 
 567   # id is the old record
 
 569     $query = qq|UPDATE leads SET
 
 572     push(@values, $form->{id});
 
 574     $query = qq|INSERT INTO leads
 
 578   do_query($form, $dbh, $query, @values);
 
 582   $main::lxdebug->leave_sub();
 
 586   $main::lxdebug->enter_sub();
 
 588   my ($self, $myconfig, $form) = @_;
 
 591   # connect to database
 
 592   my $dbh = $form->dbconnect($myconfig);
 
 594   $query = qq|DELETE FROM leads
 
 596   do_query($form, $dbh, $query, $form->{id});
 
 600   $main::lxdebug->leave_sub();
 
 604   $main::lxdebug->enter_sub();
 
 606   my ($self, $myconfig, $form, $return_list) = @_;
 
 608   # connect to database
 
 609   my $dbh = $form->dbconnect($myconfig);
 
 612     "SELECT id, description, template_code, article_code, " .
 
 613     "  output_numberformat, output_dateformat, output_longdates " .
 
 614     "FROM language ORDER BY description";
 
 616   my $sth = $dbh->prepare($query);
 
 617   $sth->execute || $form->dberror($query);
 
 621   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 622     push(@{ $ary }, $ref);
 
 628   $main::lxdebug->leave_sub();
 
 638   $main::lxdebug->enter_sub();
 
 640   my ($self, $myconfig, $form) = @_;
 
 642   # connect to database
 
 643   my $dbh = $form->dbconnect($myconfig);
 
 646     "SELECT description, template_code, article_code, " .
 
 647     "  output_numberformat, output_dateformat, output_longdates " .
 
 648     "FROM language WHERE id = ?";
 
 649   my $sth = $dbh->prepare($query);
 
 650   $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
 
 652   my $ref = $sth->fetchrow_hashref("NAME_lc");
 
 654   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
 660   $main::lxdebug->leave_sub();
 
 663 sub get_language_details {
 
 664   $main::lxdebug->enter_sub();
 
 666   my ($self, $myconfig, $form, $id) = @_;
 
 668   # connect to database
 
 669   my $dbh = $form->dbconnect($myconfig);
 
 672     "SELECT template_code, " .
 
 673     "  output_numberformat, output_dateformat, output_longdates " .
 
 674     "FROM language WHERE id = ?";
 
 675   my @res = selectrow_query($form, $dbh, $query, $id);
 
 678   $main::lxdebug->leave_sub();
 
 684   $main::lxdebug->enter_sub();
 
 686   my ($self, $myconfig, $form) = @_;
 
 688   # connect to database
 
 689   my $dbh = $form->dbconnect($myconfig);
 
 690   my (@values, $query);
 
 692   map({ push(@values, $form->{$_}); }
 
 693       qw(description template_code article_code
 
 694          output_numberformat output_dateformat output_longdates));
 
 696   # id is the old record
 
 699       "UPDATE language SET " .
 
 700       "  description = ?, template_code = ?, article_code = ?, " .
 
 701       "  output_numberformat = ?, output_dateformat = ?, " .
 
 702       "  output_longdates = ? " .
 
 704     push(@values, $form->{id});
 
 707       "INSERT INTO language (" .
 
 708       "  description, template_code, article_code, " .
 
 709       "  output_numberformat, output_dateformat, output_longdates" .
 
 710       ") VALUES (?, ?, ?, ?, ?, ?)";
 
 712   do_query($form, $dbh, $query, @values);
 
 716   $main::lxdebug->leave_sub();
 
 719 sub delete_language {
 
 720   $main::lxdebug->enter_sub();
 
 722   my ($self, $myconfig, $form) = @_;
 
 725   # connect to database
 
 726   my $dbh = $form->dbconnect_noauto($myconfig);
 
 728   foreach my $table (qw(generic_translations units_language)) {
 
 729     $query = qq|DELETE FROM $table WHERE language_id = ?|;
 
 730     do_query($form, $dbh, $query, $form->{"id"});
 
 733   $query = "DELETE FROM language WHERE id = ?";
 
 734   do_query($form, $dbh, $query, $form->{"id"});
 
 739   $main::lxdebug->leave_sub();
 
 742 sub prepare_template_filename {
 
 743   $main::lxdebug->enter_sub();
 
 745   my ($self, $myconfig, $form) = @_;
 
 747   my ($filename, $display_filename);
 
 749   if ($form->{type} eq "stylesheet") {
 
 750     $filename = "css/$myconfig->{stylesheet}";
 
 751     $display_filename = $myconfig->{stylesheet};
 
 754     $filename = $form->{formname};
 
 756     if ($form->{language}) {
 
 757       my ($id, $template_code) = split(/--/, $form->{language});
 
 758       $filename .= "_${template_code}";
 
 761     if ($form->{printer}) {
 
 762       my ($id, $template_code) = split(/--/, $form->{printer});
 
 763       $filename .= "_${template_code}";
 
 766     $filename .= "." . ($form->{format} eq "html" ? "html" : "tex");
 
 767     if ($form->{"formname"} =~ m|\.\.| || $form->{"formname"} =~ m|^/|) {
 
 768       $filename =~ s|.*/||;
 
 770     $display_filename = $filename;
 
 771     $filename = SL::DB::Default->get->templates . "/$filename";
 
 774   $main::lxdebug->leave_sub();
 
 776   return ($filename, $display_filename);
 
 781   $main::lxdebug->enter_sub();
 
 783   my ($self, $filename) = @_;
 
 785   my ($content, $lines) = ("", 0);
 
 789   if (open(TEMPLATE, $filename)) {
 
 797   $content = Encode::decode('utf-8-strict', $content);
 
 799   $main::lxdebug->leave_sub();
 
 801   return ($content, $lines);
 
 805   $main::lxdebug->enter_sub();
 
 807   my ($self, $filename, $content) = @_;
 
 813   if (open(TEMPLATE, ">", $filename)) {
 
 814     $content = Encode::encode('utf-8-strict', $content);
 
 815     $content =~ s/\r\n/\n/g;
 
 816     print(TEMPLATE $content);
 
 822   $main::lxdebug->leave_sub();
 
 827 sub save_preferences {
 
 828   $main::lxdebug->enter_sub();
 
 830   my ($self, $form) = @_;
 
 832   my $employee = SL::DB::Manager::Employee->find_by(login => $form->{login});
 
 833   $employee->update_attributes(name => $form->{name});
 
 835   my $user = SL::DB::Manager::AuthUser->find_by(login => $form->{login});
 
 836   $user->update_attributes(
 
 838       %{ $user->config_values },
 
 839       map { ($_ => $form->{$_}) } SL::DB::AuthUser::CONFIG_VARS(),
 
 842   $main::lxdebug->leave_sub();
 
 848   $main::lxdebug->enter_sub();
 
 853   my $myconfig = \%main::myconfig;
 
 854   my $form     = $main::form;
 
 856   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
 
 858   my $defaults = selectfirst_hashref_query($form, $dbh, qq|SELECT * FROM defaults|) || {};
 
 860   $defaults->{weightunit} ||= 'kg';
 
 862   $main::lxdebug->leave_sub();
 
 868   $main::lxdebug->enter_sub();
 
 870   my ($self, $myconfig, $form) = @_;
 
 872   my $dbh = $form->dbconnect($myconfig);
 
 874   my $query = qq|SELECT closedto, max_future_booking_interval, revtrans FROM defaults|;
 
 875   my $sth   = $dbh->prepare($query);
 
 876   $sth->execute || $form->dberror($query);
 
 878   ($form->{closedto}, $form->{max_future_booking_interval}, $form->{revtrans}) = $sth->fetchrow_array;
 
 884   $main::lxdebug->leave_sub();
 
 888   $main::lxdebug->enter_sub();
 
 890   my ($self, $myconfig, $form) = @_;
 
 892   my $dbh = $form->dbconnect($myconfig);
 
 894   my ($query, @values);
 
 896   # is currently NEVER trueish (no more hidden revtrans in $form)
 
 897   # if ($form->{revtrans}) {
 
 898   #   $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '1'|;
 
 899   # -> therefore you can only set this to false (which is already the default)
 
 900   # and this flag is currently only checked in gl.pl. TOOD Can probably be removed
 
 902     $query = qq|UPDATE defaults SET closedto = ?, max_future_booking_interval = ?, revtrans = '0'|;
 
 903     @values = (conv_date($form->{closedto}), conv_i($form->{max_future_booking_interval}));
 
 905   # set close in defaults
 
 906   do_query($form, $dbh, $query, @values);
 
 910   $main::lxdebug->leave_sub();
 
 914   my ($self, $units, $unit_name, $factor) = @_;
 
 916   $factor = 1 unless ($factor);
 
 918   my $unit = $units->{$unit_name};
 
 920   if (!defined($unit) || !$unit->{"base_unit"} ||
 
 921       ($unit_name eq $unit->{"base_unit"})) {
 
 922     return ($unit_name, $factor);
 
 925   return AM->get_base_unit($units, $unit->{"base_unit"}, $factor * $unit->{"factor"});
 
 929   $main::lxdebug->enter_sub();
 
 931   my ($self, $myconfig, $form, $prefix) = @_;
 
 934   my $dbh = $form->get_standard_dbh;
 
 936   my $query = "SELECT *, base_unit AS original_base_unit FROM units";
 
 938   my $sth = prepare_execute_query($form, $dbh, $query);
 
 941   while (my $ref = $sth->fetchrow_hashref()) {
 
 942     $units->{$ref->{"name"}} = $ref;
 
 946   my $query_lang = "SELECT id, template_code FROM language ORDER BY description";
 
 947   $sth = $dbh->prepare($query_lang);
 
 948   $sth->execute() || $form->dberror($query_lang);
 
 950   while (my $ref = $sth->fetchrow_hashref()) {
 
 951     push(@languages, $ref);
 
 955   $query_lang = "SELECT ul.localized, ul.localized_plural, l.id, l.template_code " .
 
 956     "FROM units_language ul " .
 
 957     "LEFT JOIN language l ON ul.language_id = l.id " .
 
 959   $sth = $dbh->prepare($query_lang);
 
 961   foreach my $unit (values(%{$units})) {
 
 962     ($unit->{"${prefix}base_unit"}, $unit->{"${prefix}factor"}) = AM->get_base_unit($units, $unit->{"name"});
 
 964     $unit->{"LANGUAGES"} = {};
 
 965     foreach my $lang (@languages) {
 
 966       $unit->{"LANGUAGES"}->{$lang->{"template_code"}} = { "template_code" => $lang->{"template_code"} };
 
 969     $sth->execute($unit->{"name"}) || $form->dberror($query_lang . " (" . $unit->{"name"} . ")");
 
 970     while (my $ref = $sth->fetchrow_hashref()) {
 
 971       map({ $unit->{"LANGUAGES"}->{$ref->{"template_code"}}->{$_} = $ref->{$_} } keys(%{$ref}));
 
 976   $main::lxdebug->leave_sub();
 
 981 sub retrieve_all_units {
 
 982   $main::lxdebug->enter_sub();
 
 986   if (!$::request->{cache}{all_units}) {
 
 987     $::request->{cache}{all_units} = $self->retrieve_units(\%main::myconfig, $main::form);
 
 990   $main::lxdebug->leave_sub();
 
 992   return $::request->{cache}{all_units};
 
 996 sub translate_units {
 
 997   $main::lxdebug->enter_sub();
 
 999   my ($self, $form, $template_code, $unit, $amount) = @_;
 
1001   my $units = $self->retrieve_units(\%main::myconfig, $form);
 
1003   my $h = $units->{$unit}->{"LANGUAGES"}->{$template_code};
 
1004   my $new_unit = $unit;
 
1006     if (($amount != 1) && $h->{"localized_plural"}) {
 
1007       $new_unit = $h->{"localized_plural"};
 
1008     } elsif ($h->{"localized"}) {
 
1009       $new_unit = $h->{"localized"};
 
1013   $main::lxdebug->leave_sub();
 
1019   $main::lxdebug->enter_sub();
 
1021   my ($self, $myconfig, $form, $units) = @_;
 
1023   my $dbh = $form->dbconnect($myconfig);
 
1025   map({ $_->{"in_use"} = 0; } values(%{$units}));
 
1027   foreach my $unit (values(%{$units})) {
 
1028     my $base_unit = $unit->{"original_base_unit"};
 
1029     while ($base_unit) {
 
1030       $units->{$base_unit}->{"in_use"} = 1;
 
1031       $units->{$base_unit}->{"DEPENDING_UNITS"} = [] unless ($units->{$base_unit}->{"DEPENDING_UNITS"});
 
1032       push(@{$units->{$base_unit}->{"DEPENDING_UNITS"}}, $unit->{"name"});
 
1033       $base_unit = $units->{$base_unit}->{"original_base_unit"};
 
1037   foreach my $unit (values(%{$units})) {
 
1038     map({ $_ = $dbh->quote($_); } @{$unit->{"DEPENDING_UNITS"}});
 
1040     foreach my $table (qw(parts invoice orderitems)) {
 
1041       my $query = "SELECT COUNT(*) FROM $table WHERE unit ";
 
1043       if (0 == scalar(@{$unit->{"DEPENDING_UNITS"}})) {
 
1044         $query .= "= " . $dbh->quote($unit->{"name"});
 
1046         $query .= "IN (" . $dbh->quote($unit->{"name"}) . "," .
 
1047           join(",", map({ $dbh->quote($_) } @{$unit->{"DEPENDING_UNITS"}})) . ")";
 
1050       my ($count) = $dbh->selectrow_array($query);
 
1051       $form->dberror($query) if ($dbh->err);
 
1054         $unit->{"in_use"} = 1;
 
1062   $main::lxdebug->leave_sub();
 
1065 sub convertible_units {
 
1066   $main::lxdebug->enter_sub();
 
1070   my $filter_unit = shift;
 
1071   my $not_smaller = shift;
 
1073   my $conv_units = [];
 
1075   $filter_unit = $units->{$filter_unit};
 
1077   foreach my $name (sort { lc $a cmp lc $b } keys %{ $units }) {
 
1078     my $unit = $units->{$name};
 
1080     if (($unit->{base_unit} eq $filter_unit->{base_unit}) &&
 
1081         (!$not_smaller || ($unit->{factor} >= $filter_unit->{factor}))) {
 
1082       push @{$conv_units}, $unit;
 
1086   my @sorted = sort { $b->{factor} <=> $a->{factor} } @{ $conv_units };
 
1088   $main::lxdebug->leave_sub();
 
1093 # if $a is translatable to $b, return the factor between them.
 
1096   $main::lxdebug->enter_sub(2);
 
1097   my ($this, $a, $b, $all_units) = @_;
 
1100     $all_units = $this->retrieve_all_units;
 
1103   $main::lxdebug->leave_sub(2) and return 0 unless $a && $b;
 
1104   $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a} && $all_units->{$b};
 
1105   $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a}{base_unit} eq $all_units->{$b}{base_unit};
 
1106   $main::lxdebug->leave_sub(2) and return $all_units->{$a}{factor} / $all_units->{$b}{factor};
 
1109 sub unit_select_data {
 
1110   $main::lxdebug->enter_sub();
 
1112   my ($self, $units, $selected, $empty_entry, $convertible_into) = @_;
 
1117     push(@{$select}, { "name" => "", "base_unit" => "", "factor" => "", "selected" => "" });
 
1120   foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
 
1121     if (!$convertible_into ||
 
1122         ($units->{$convertible_into} &&
 
1123          ($units->{$convertible_into}->{base_unit} eq $units->{$unit}->{base_unit}))) {
 
1124       push @{$select}, { "name"      => $unit,
 
1125                          "base_unit" => $units->{$unit}->{"base_unit"},
 
1126                          "factor"    => $units->{$unit}->{"factor"},
 
1127                          "selected"  => ($unit eq $selected) ? "selected" : "" };
 
1131   $main::lxdebug->leave_sub();
 
1136 sub unit_select_html {
 
1137   $main::lxdebug->enter_sub();
 
1139   my ($self, $units, $name, $selected, $convertible_into) = @_;
 
1141   my $select = "<select name=${name}>";
 
1143   foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
 
1144     if (!$convertible_into ||
 
1145         ($units->{$convertible_into} &&
 
1146          ($units->{$convertible_into}->{"base_unit"} eq $units->{$unit}->{"base_unit"}))) {
 
1147       $select .= "<option" . (($unit eq $selected) ? " selected" : "") . ">${unit}</option>";
 
1150   $select .= "</select>";
 
1152   $main::lxdebug->leave_sub();
 
1158   $main::lxdebug->enter_sub();
 
1162   my $units = $self->retrieve_all_units();
 
1167   while (2 <= scalar(@_)) {
 
1168     my $qty  = shift(@_);
 
1169     my $unit = $units->{shift(@_)};
 
1171     croak "No unit defined with name $unit" if (!defined $unit);
 
1174       $base_unit = $unit->{base_unit};
 
1175     } elsif ($base_unit ne $unit->{base_unit}) {
 
1176       croak "Adding values with incompatible base units $base_unit/$unit->{base_unit}";
 
1179     $sum += $qty * $unit->{factor};
 
1182   $main::lxdebug->leave_sub();
 
1184   return wantarray ? ($sum, $base_unit) : $sum;
 
1188   $main::lxdebug->enter_sub();
 
1190   my ($self, $myconfig, $form, $name, $base_unit, $factor, $languages) = @_;
 
1192   my $dbh = $form->dbconnect_noauto($myconfig);
 
1194   my $query = qq|SELECT COALESCE(MAX(sortkey), 0) + 1 FROM units|;
 
1195   my ($sortkey) = selectrow_query($form, $dbh, $query);
 
1197   $query = "INSERT INTO units (name, base_unit, factor, sortkey) " .
 
1198     "VALUES (?, ?, ?, ?)";
 
1199   do_query($form, $dbh, $query, $name, $base_unit, $factor, $sortkey);
 
1202     $query = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
 
1203     my $sth = $dbh->prepare($query);
 
1204     foreach my $lang (@{$languages}) {
 
1205       my @values = ($name, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
 
1206       $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
 
1214   $main::lxdebug->leave_sub();
 
1218   $main::lxdebug->enter_sub();
 
1220   my ($self, $myconfig, $form, $units, $delete_units) = @_;
 
1222   my $dbh = $form->dbconnect_noauto($myconfig);
 
1224   my ($base_unit, $unit, $sth, $query);
 
1226   $query = "DELETE FROM units_language";
 
1227   $dbh->do($query) || $form->dberror($query);
 
1229   if ($delete_units && (0 != scalar(@{$delete_units}))) {
 
1230     $query = "DELETE FROM units WHERE name IN (";
 
1231     map({ $query .= "?," } @{$delete_units});
 
1232     substr($query, -1, 1) = ")";
 
1233     $dbh->do($query, undef, @{$delete_units}) ||
 
1234       $form->dberror($query . " (" . join(", ", @{$delete_units}) . ")");
 
1237   $query = "UPDATE units SET name = ?, base_unit = ?, factor = ? WHERE name = ?";
 
1238   $sth = $dbh->prepare($query);
 
1240   my $query_lang = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
 
1241   my $sth_lang = $dbh->prepare($query_lang);
 
1243   foreach $unit (values(%{$units})) {
 
1244     $unit->{"depth"} = 0;
 
1245     my $base_unit = $unit;
 
1246     while ($base_unit->{"base_unit"}) {
 
1248       $base_unit = $units->{$base_unit->{"base_unit"}};
 
1252   foreach $unit (sort({ $a->{"depth"} <=> $b->{"depth"} } values(%{$units}))) {
 
1253     if ($unit->{"LANGUAGES"}) {
 
1254       foreach my $lang (@{$unit->{"LANGUAGES"}}) {
 
1255         next unless ($lang->{"id"} && $lang->{"localized"});
 
1256         my @values = ($unit->{"name"}, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
 
1257         $sth_lang->execute(@values) || $form->dberror($query_lang . " (" . join(", ", @values) . ")");
 
1261     next if ($unit->{"unchanged_unit"});
 
1263     my @values = ($unit->{"name"}, $unit->{"base_unit"}, $unit->{"factor"}, $unit->{"old_name"});
 
1264     $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
 
1268   $sth_lang->finish();
 
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
 
1287                    round(t.rate * 100, 2) AS rate,
 
1288                    (SELECT accno FROM chart WHERE id = chart_id) AS taxnumber,
 
1289                    (SELECT description FROM chart WHERE id = chart_id) AS account_description
 
1291                  ORDER BY taxkey, rate|;
 
1293   my $sth = $dbh->prepare($query);
 
1294   $sth->execute || $form->dberror($query);
 
1297   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
1298     push @{ $form->{TAX} }, $ref;
 
1304   $main::lxdebug->leave_sub();
 
1307 sub get_tax_accounts {
 
1308   $main::lxdebug->enter_sub();
 
1310   my ($self, $myconfig, $form) = @_;
 
1312   my $dbh = $form->dbconnect($myconfig);
 
1314   # get Accounts from chart
 
1315   my $query = qq{ SELECT
 
1317                  accno || ' - ' || description AS taxaccount
 
1319                WHERE link LIKE '%_tax%'
 
1323   my $sth = $dbh->prepare($query);
 
1324   $sth->execute || $form->dberror($query);
 
1326   $form->{ACCOUNTS} = [];
 
1327   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
1328     push @{ $form->{ACCOUNTS} }, $ref;
 
1335   $main::lxdebug->leave_sub();
 
1339   $main::lxdebug->enter_sub();
 
1341   my ($self, $myconfig, $form) = @_;
 
1343   # connect to database
 
1344   my $dbh = $form->dbconnect($myconfig);
 
1346   my $query = qq|SELECT
 
1349                    round(rate * 100, 2) AS rate,
 
1352                    (id IN (SELECT tax_id
 
1353                            FROM acc_trans)) AS tax_already_used
 
1357   my $sth = $dbh->prepare($query);
 
1358   $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
1360   my $ref = $sth->fetchrow_hashref("NAME_lc");
 
1362   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
1366   # see if it is used by a taxkey
 
1367   $query = qq|SELECT count(*) FROM taxkeys
 
1368               WHERE tax_id = ? AND chart_id >0|;
 
1370   ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
 
1372   $form->{orphaned} = !$form->{orphaned};
 
1375   if (!$form->{orphaned} ) {
 
1376     $query = qq|SELECT DISTINCT c.id, c.accno
 
1378                 JOIN   tax t ON (t.id = tk.tax_id)
 
1379                 JOIN chart c ON (c.id = tk.chart_id)
 
1380                 WHERE tk.tax_id = ?|;
 
1382     $sth = $dbh->prepare($query);
 
1383     $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
1385     $form->{TAXINUSE} = [];
 
1386     while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
1387       push @{ $form->{TAXINUSE} }, $ref;
 
1395   $main::lxdebug->leave_sub();
 
1399   $main::lxdebug->enter_sub();
 
1401   my ($self, $myconfig, $form) = @_;
 
1404   # connect to database
 
1405   my $dbh = $form->get_standard_dbh($myconfig);
 
1407   $form->{rate} = $form->{rate} / 100;
 
1409   my $chart_categories = '';
 
1410   $chart_categories .= 'A' if $form->{asset};
 
1411   $chart_categories .= 'L' if $form->{liability};
 
1412   $chart_categories .= 'Q' if $form->{equity};
 
1413   $chart_categories .= 'I' if $form->{revenue};
 
1414   $chart_categories .= 'E' if $form->{expense};
 
1415   $chart_categories .= 'C' if $form->{costs};
 
1417   my @values = ($form->{taxkey}, $form->{taxdescription}, $form->{rate}, conv_i($form->{chart_id}), conv_i($form->{chart_id}), $chart_categories);
 
1418   if ($form->{id} ne "") {
 
1419     $query = qq|UPDATE tax SET
 
1424                   taxnumber      = (SELECT accno FROM chart WHERE id= ? ),
 
1425                   chart_categories = ?
 
1430     ($form->{id}) = selectfirst_array_query($form, $dbh, qq|SELECT nextval('id')|);
 
1431     $query = qq|INSERT INTO tax (
 
1440                 VALUES (?, ?, ?, ?, (SELECT accno FROM chart WHERE id = ?), ?, ?)|;
 
1442   push(@values, $form->{id});
 
1443   do_query($form, $dbh, $query, @values);
 
1445   foreach my $language_id (keys %{ $form->{translations} }) {
 
1446     GenericTranslations->save('dbh'              => $dbh,
 
1447                               'translation_type' => 'SL::DB::Tax/taxdescription',
 
1448                               'translation_id'   => $form->{id},
 
1449                               'language_id'      => $language_id,
 
1450                               'translation'      => $form->{translations}->{$language_id});
 
1455   $main::lxdebug->leave_sub();
 
1459   $main::lxdebug->enter_sub();
 
1461   my ($self, $myconfig, $form) = @_;
 
1464   # connect to database
 
1465   my $dbh = $form->get_standard_dbh($myconfig);
 
1467   $query = qq|DELETE FROM tax
 
1469   do_query($form, $dbh, $query, $form->{id});
 
1473   $main::lxdebug->leave_sub();
 
1476 sub save_price_factor {
 
1477   $main::lxdebug->enter_sub();
 
1479   my ($self, $myconfig, $form) = @_;
 
1481   # connect to database
 
1482   my $dbh = $form->get_standard_dbh($myconfig);
 
1485   my @values = ($form->{description}, conv_i($form->{factor}));
 
1488     $query = qq|UPDATE price_factors SET description = ?, factor = ? WHERE id = ?|;
 
1489     push @values, conv_i($form->{id});
 
1492     $query = qq|INSERT INTO price_factors (description, factor, sortkey) VALUES (?, ?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM price_factors))|;
 
1495   do_query($form, $dbh, $query, @values);
 
1499   $main::lxdebug->leave_sub();
 
1502 sub get_all_price_factors {
 
1503   $main::lxdebug->enter_sub();
 
1505   my ($self, $myconfig, $form) = @_;
 
1507   # connect to database
 
1508   my $dbh = $form->get_standard_dbh($myconfig);
 
1510   $form->{PRICE_FACTORS} = selectall_hashref_query($form, $dbh, qq|SELECT * FROM price_factors ORDER BY sortkey|);
 
1512   $main::lxdebug->leave_sub();
 
1515 sub get_price_factor {
 
1516   $main::lxdebug->enter_sub();
 
1518   my ($self, $myconfig, $form) = @_;
 
1520   # connect to database
 
1521   my $dbh = $form->get_standard_dbh($myconfig);
 
1523   my $query = qq|SELECT description, factor,
 
1524                    ((SELECT COUNT(*) FROM parts      WHERE price_factor_id = ?) +
 
1525                     (SELECT COUNT(*) FROM invoice    WHERE price_factor_id = ?) +
 
1526                     (SELECT COUNT(*) FROM orderitems WHERE price_factor_id = ?)) = 0 AS orphaned
 
1527                  FROM price_factors WHERE id = ?|;
 
1529   ($form->{description}, $form->{factor}, $form->{orphaned}) = selectrow_query($form, $dbh, $query, (conv_i($form->{id})) x 4);
 
1531   $main::lxdebug->leave_sub();
 
1534 sub delete_price_factor {
 
1535   $main::lxdebug->enter_sub();
 
1537   my ($self, $myconfig, $form) = @_;
 
1539   # connect to database
 
1540   my $dbh = $form->get_standard_dbh($myconfig);
 
1542   do_query($form, $dbh, qq|DELETE FROM price_factors WHERE id = ?|, conv_i($form->{id}));
 
1545   $main::lxdebug->leave_sub();
 
1548 sub save_warehouse {
 
1549   $main::lxdebug->enter_sub();
 
1551   my ($self, $myconfig, $form) = @_;
 
1553   # connect to database
 
1554   my $dbh = $form->get_standard_dbh($myconfig);
 
1556   my ($query, @values, $sth);
 
1559     $query        = qq|SELECT nextval('id')|;
 
1560     ($form->{id}) = selectrow_query($form, $dbh, $query);
 
1562     $query        = qq|INSERT INTO warehouse (id, sortkey) VALUES (?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM warehouse))|;
 
1563     do_query($form, $dbh, $query, $form->{id});
 
1566   do_query($form, $dbh, qq|UPDATE warehouse SET description = ?, invalid = ? WHERE id = ?|,
 
1567            $form->{description}, $form->{invalid} ? 't' : 'f', conv_i($form->{id}));
 
1569   if (0 < $form->{number_of_new_bins}) {
 
1570     my ($num_existing_bins) = selectfirst_array_query($form, $dbh, qq|SELECT COUNT(*) FROM bin WHERE warehouse_id = ?|, $form->{id});
 
1571     $query = qq|INSERT INTO bin (warehouse_id, description) VALUES (?, ?)|;
 
1572     $sth   = prepare_query($form, $dbh, $query);
 
1574     foreach my $i (1..$form->{number_of_new_bins}) {
 
1575       do_statement($form, $sth, $query, conv_i($form->{id}), "$form->{prefix}" . ($i + $num_existing_bins));
 
1583   $main::lxdebug->leave_sub();
 
1587   $main::lxdebug->enter_sub();
 
1589   my ($self, $myconfig, $form) = @_;
 
1591   # connect to database
 
1592   my $dbh = $form->get_standard_dbh($myconfig);
 
1594   my ($query, @values, $commit_necessary, $sth);
 
1596   @values = map { $form->{"id_${_}"} } grep { $form->{"delete_${_}"} } (1..$form->{rowcount});
 
1599     $query = qq|DELETE FROM bin WHERE id IN (| . join(', ', ('?') x scalar(@values)) . qq|)|;
 
1600     do_query($form, $dbh, $query, @values);
 
1602     $commit_necessary = 1;
 
1605   $query = qq|UPDATE bin SET description = ? WHERE id = ?|;
 
1606   $sth   = prepare_query($form, $dbh, $query);
 
1608   foreach my $row (1..$form->{rowcount}) {
 
1609     next if ($form->{"delete_${row}"});
 
1611     do_statement($form, $sth, $query, $form->{"description_${row}"}, conv_i($form->{"id_${row}"}));
 
1613     $commit_necessary = 1;
 
1618   $dbh->commit() if ($commit_necessary);
 
1620   $main::lxdebug->leave_sub();
 
1623 sub delete_warehouse {
 
1624   $main::lxdebug->enter_sub();
 
1626   my ($self, $myconfig, $form) = @_;
 
1628   # connect to database
 
1629   my $dbh = $form->get_standard_dbh($myconfig);
 
1631   my $id      = conv_i($form->{id});
 
1632   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|;
 
1633   my ($count) = selectrow_query($form, $dbh, $query, $id);
 
1636     $main::lxdebug->leave_sub();
 
1640   do_query($form, $dbh, qq|DELETE FROM bin       WHERE warehouse_id = ?|, conv_i($form->{id}));
 
1641   do_query($form, $dbh, qq|DELETE FROM warehouse WHERE id           = ?|, conv_i($form->{id}));
 
1645   $main::lxdebug->leave_sub();
 
1650 sub get_all_warehouses {
 
1651   $main::lxdebug->enter_sub();
 
1653   my ($self, $myconfig, $form) = @_;
 
1655   # connect to database
 
1656   my $dbh = $form->get_standard_dbh($myconfig);
 
1658   my $query = qq|SELECT w.id, w.description, w.invalid,
 
1659                    (SELECT COUNT(b.description) FROM bin b WHERE b.warehouse_id = w.id) AS number_of_bins
 
1661                  ORDER BY w.sortkey|;
 
1663   $form->{WAREHOUSES} = selectall_hashref_query($form, $dbh, $query);
 
1665   $main::lxdebug->leave_sub();
 
1669   $main::lxdebug->enter_sub();
 
1671   my ($self, $myconfig, $form) = @_;
 
1673   # connect to database
 
1674   my $dbh = $form->get_standard_dbh($myconfig);
 
1676   my $id    = conv_i($form->{id});
 
1677   my $query = qq|SELECT w.description, w.invalid
 
1681   my $ref   = selectfirst_hashref_query($form, $dbh, $query, $id);
 
1683   map { $form->{$_} = $ref->{$_} } keys %{ $ref };
 
1687       (   EXISTS(SELECT i.bin_id FROM inventory i WHERE i.bin_id = b.id LIMIT 1)
 
1688        OR EXISTS(SELECT p.bin_id FROM parts     p WHERE p.bin_id = b.id LIMIT 1))
 
1691     WHERE b.warehouse_id = ?
 
1694   $form->{BINS} = selectall_hashref_query($form, $dbh, $query, conv_i($form->{id}));
 
1696   $main::lxdebug->leave_sub();