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 #======================================================================
 
  48   $main::lxdebug->enter_sub();
 
  50   my ($self, $myconfig, $form) = @_;
 
  53   my $dbh = $form->dbconnect($myconfig);
 
  55     SELECT c.accno, c.description, c.charttype, c.category,
 
  56       c.link, c.pos_bilanz, c.pos_eur, c.new_chart_id, c.valid_from,
 
  57       c.pos_bwa, datevautomatik,
 
  58       tk.taxkey_id, tk.pos_ustva, tk.tax_id,
 
  59       tk.tax_id || '--' || tk.taxkey_id AS tax, tk.startdate
 
  62     ON (c.id=tk.chart_id AND tk.id =
 
  63       (SELECT id FROM taxkeys
 
  64        WHERE taxkeys.chart_id = c.id AND startdate <= current_date
 
  65        ORDER BY startdate DESC LIMIT 1))
 
  70   $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
 
  71   my $sth = $dbh->prepare($query);
 
  72   $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
  74   my $ref = $sth->fetchrow_hashref("NAME_lc");
 
  76   foreach my $key (keys %$ref) {
 
  77     $form->{"$key"} = $ref->{"$key"};
 
  82   # get default accounts
 
  83   $query = qq|SELECT inventory_accno_id, income_accno_id, expense_accno_id
 
  85   $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
 
  86   $sth = $dbh->prepare($query);
 
  87   $sth->execute || $form->dberror($query);
 
  89   $ref = $sth->fetchrow_hashref("NAME_lc");
 
  91   map { $form->{$_} = $ref->{$_} } keys %{ $ref };
 
  97   # get taxkeys and description
 
 101       (SELECT accno FROM chart WHERE id=tax.chart_id) AS chart_accno,
 
 103       id||'--'||taxkey AS tax,
 
 106     FROM tax ORDER BY taxkey
 
 108   $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
 
 109   $sth = $dbh->prepare($query);
 
 110   $sth->execute || $form->dberror($query);
 
 112   $form->{TAXKEY} = [];
 
 114   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 115     push @{ $form->{TAXKEY} }, $ref;
 
 121     $query = qq|SELECT id, accno,description
 
 125     $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
 
 126     $sth = $dbh->prepare($query);
 
 127     $sth->execute($form->{link}) || $form->dberror($query . " ($form->{link})");
 
 129     $form->{NEWACCOUNT} = [];
 
 130     while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 131       push @{ $form->{NEWACCOUNT} }, $ref;
 
 136     # get the taxkeys of account
 
 150       LEFT JOIN   tax t ON (t.id = tk.tax_id)
 
 151       LEFT JOIN chart c ON (c.id = t.chart_id)
 
 153       WHERE tk.chart_id = ?
 
 154       ORDER BY startdate DESC
 
 156     $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
 
 157     $sth = $dbh->prepare($query);
 
 159     $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
 161     $form->{ACCOUNT_TAXKEYS} = [];
 
 163     while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 164       push @{ $form->{ACCOUNT_TAXKEYS} }, $ref;
 
 170   # check if we have any transactions
 
 171   $query = qq|SELECT a.trans_id FROM acc_trans a
 
 172               WHERE a.chart_id = ?|;
 
 173   $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
 
 174   $sth = $dbh->prepare($query);
 
 175   $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
 177   ($form->{orphaned}) = $sth->fetchrow_array;
 
 178   $form->{orphaned} = !$form->{orphaned};
 
 181   # check if new account is active
 
 182   $form->{new_chart_valid} = 0;
 
 183   if ($form->{new_chart_id}) {
 
 184     $query = qq|SELECT current_date-valid_from FROM chart
 
 186     $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
 
 187     my ($count) = selectrow_query($form, $dbh, $query, $form->{id});
 
 189       $form->{new_chart_valid} = 1;
 
 196   $main::lxdebug->leave_sub();
 
 200   $main::lxdebug->enter_sub();
 
 202   # TODO: it should be forbidden to change an account to a heading if there
 
 203   # have been bookings to this account in the past
 
 205   my ($self, $myconfig, $form) = @_;
 
 207   # connect to database, turn off AutoCommit
 
 208   my $dbh = $form->dbconnect_noauto($myconfig);
 
 210   # sanity check, can't have AR with AR_...
 
 211   if ($form->{AR} || $form->{AP} || $form->{IC}) {
 
 212     map { delete $form->{$_} }
 
 213       qw(AR_amount AR_tax AR_paid AP_amount AP_tax AP_paid IC_sale IC_cogs IC_taxpart IC_income IC_expense IC_taxservice CT_tax);
 
 217   foreach my $item ($form->{AR},            $form->{AR_amount},
 
 218                     $form->{AR_tax},        $form->{AR_paid},
 
 219                     $form->{AP},            $form->{AP_amount},
 
 220                     $form->{AP_tax},        $form->{AP_paid},
 
 221                     $form->{IC},            $form->{IC_sale},
 
 222                     $form->{IC_cogs},       $form->{IC_taxpart},
 
 223                     $form->{IC_income},     $form->{IC_expense},
 
 224                     $form->{IC_taxservice}, $form->{CT_tax}
 
 226     $form->{link} .= "${item}:" if ($item);
 
 230   # strip blanks from accno
 
 231   map { $form->{$_} =~ s/ //g; } qw(accno);
 
 235   if ($form->{id} eq "NULL") {
 
 239   if (!$form->{id} || $form->{id} eq "") {
 
 240     $query = qq|SELECT nextval('id')|;
 
 241     ($form->{"id"}) = selectrow_query($form, $dbh, $query);
 
 242     $query = qq|INSERT INTO chart (id, accno) VALUES (?, ?)|;
 
 243     do_query($form, $dbh, $query, $form->{"id"}, $form->{"accno"});
 
 251     # if charttype is heading make sure certain values are empty
 
 252     # specifically, if charttype is changed from an existing account, empty the
 
 253     # fields unnecessary for headings, so that e.g. heading doesn't appear in
 
 254     # drop-down menues due to still having a valid "link" entry
 
 256     if ( $form->{charttype} eq 'H' ) {
 
 258       $form->{pos_bwa} = '';
 
 259       $form->{pos_bilanz} = '';
 
 260       $form->{pos_eur} = '';
 
 261       $form->{new_chart_id} = '';
 
 262       $form->{valid_from} = '';
 
 265     $query = qq|UPDATE chart SET
 
 281                   $form->{description},
 
 285                   conv_i($form->{pos_bwa}),
 
 286                   conv_i($form->{pos_bilanz}),
 
 287                   conv_i($form->{pos_eur}),
 
 288                   conv_i($form->{new_chart_id}),
 
 289                   conv_date($form->{valid_from}),
 
 290                   ($form->{datevautomatik} eq 'T') ? 'true':'false',
 
 297   do_query($form, $dbh, $query, @values);
 
 303   my $MAX_TRIES = 10; # Maximum count of taxkeys in form
 
 307   for $tk_count (0 .. $MAX_TRIES) {
 
 311     # Check if the account already exists, else cancel
 
 313     print(STDERR "Keine Taxkeys weil ID =: $form->{id}\n");
 
 315     last READTAXKEYS if ( $form->{'id'} == 0);
 
 317     # check if there is a startdate
 
 318     if ( $form->{"taxkey_startdate_$tk_count"} eq '' ) {
 
 323     # Add valid taxkeys into the array
 
 326         id        => ($form->{"taxkey_id_$tk_count"} eq 'NEW') ? conv_i('') : conv_i($form->{"taxkey_id_$tk_count"}),
 
 327         tax_id    => conv_i($form->{"taxkey_tax_$tk_count"}),
 
 328         startdate => conv_date($form->{"taxkey_startdate_$tk_count"}),
 
 329         chart_id  => conv_i($form->{"id"}),
 
 330         pos_ustva => conv_i($form->{"taxkey_pos_ustva_$tk_count"}),
 
 331         delete    => ( $form->{"taxkey_del_$tk_count"} eq 'delete' ) ? '1' : '',
 
 338   for my $j (0 .. $#taxkeys){
 
 339     if ( defined $taxkeys[$j]{'id'} ){
 
 342       if ($taxkeys[$j]{'delete'}){
 
 344           DELETE FROM taxkeys WHERE id = ?
 
 347         @values = ($taxkeys[$j]{'id'});
 
 349         do_query($form, $dbh, $query, @values);
 
 358         SET taxkey_id = (SELECT taxkey FROM tax WHERE tax.id = ?),
 
 366         $taxkeys[$j]{'tax_id'},
 
 367         $taxkeys[$j]{'chart_id'},
 
 368         $taxkeys[$j]{'tax_id'},
 
 369         $taxkeys[$j]{'pos_ustva'},
 
 370         $taxkeys[$j]{'startdate'},
 
 373       do_query($form, $dbh, $query, @values);
 
 379         INSERT INTO taxkeys (
 
 386         VALUES ((SELECT taxkey FROM tax WHERE tax.id = ?), ?, ?, ?, ?)
 
 389         $taxkeys[$j]{'tax_id'},
 
 390         $taxkeys[$j]{'chart_id'},
 
 391         $taxkeys[$j]{'tax_id'},
 
 392         $taxkeys[$j]{'pos_ustva'},
 
 393         $taxkeys[$j]{'startdate'},
 
 396       do_query($form, $dbh, $query, @values);
 
 401   # Update chart.taxkey_id to the latest from taxkeys for this chart.
 
 407       WHERE taxkeys.chart_id = chart.id
 
 408       ORDER BY startdate DESC
 
 414   do_query($form, $dbh, $query, $form->{id});
 
 417   my $rc = $dbh->commit;
 
 420   $main::lxdebug->leave_sub();
 
 426   $main::lxdebug->enter_sub();
 
 428   my ($self, $myconfig, $form) = @_;
 
 430   # connect to database, turn off AutoCommit
 
 431   my $dbh = $form->dbconnect_noauto($myconfig);
 
 433   my $query = qq|SELECT count(*) FROM acc_trans a
 
 434                  WHERE a.chart_id = ?|;
 
 435   my ($count) = selectrow_query($form, $dbh, $query, $form->{id});
 
 439     $main::lxdebug->leave_sub();
 
 443   # set inventory_accno_id, income_accno_id, expense_accno_id to defaults
 
 444   foreach my $type (qw(inventory income expense)) {
 
 447       qq|SET ${type}_accno_id = (SELECT ${type}_accno_id FROM defaults) | .
 
 448       qq|WHERE ${type}_accno_id = ?|;
 
 449     do_query($form, $dbh, $query, $form->{id});
 
 452   foreach my $table (qw(partstax customertax vendortax tax)) {
 
 453     $query = qq|DELETE FROM $table
 
 455     do_query($form, $dbh, $query, $form->{id});
 
 458   # delete chart of account record
 
 459   $query = qq|DELETE FROM chart
 
 461   do_query($form, $dbh, $query, $form->{id});
 
 463   # delete account taxkeys
 
 464   $query = qq|DELETE FROM taxkeys
 
 466   do_query($form, $dbh, $query, $form->{id});
 
 468   # commit and redirect
 
 469   my $rc = $dbh->commit;
 
 472   $main::lxdebug->leave_sub();
 
 478   $main::lxdebug->enter_sub();
 
 480   my ($self, $myconfig, $form) = @_;
 
 482   # connect to database
 
 483   my $dbh = $form->dbconnect($myconfig);
 
 485   my $query = qq|SELECT id, lead
 
 489   my $sth = $dbh->prepare($query);
 
 490   $sth->execute || $form->dberror($query);
 
 492   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 493     push @{ $form->{ALL} }, $ref;
 
 499   $main::lxdebug->leave_sub();
 
 503   $main::lxdebug->enter_sub();
 
 505   my ($self, $myconfig, $form) = @_;
 
 507   # connect to database
 
 508   my $dbh = $form->dbconnect($myconfig);
 
 511     qq|SELECT l.id, l.lead | .
 
 514   my $sth = $dbh->prepare($query);
 
 515   $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
 517   my $ref = $sth->fetchrow_hashref("NAME_lc");
 
 519   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
 525   $main::lxdebug->leave_sub();
 
 529   $main::lxdebug->enter_sub();
 
 531   my ($self, $myconfig, $form) = @_;
 
 534   # connect to database
 
 535   my $dbh = $form->dbconnect($myconfig);
 
 537   my @values = ($form->{description});
 
 538   # id is the old record
 
 540     $query = qq|UPDATE leads SET
 
 543     puhs(@values, $form->{id});
 
 545     $query = qq|INSERT INTO leads
 
 549   do_query($form, $dbh, $query, @values);
 
 553   $main::lxdebug->leave_sub();
 
 557   $main::lxdebug->enter_sub();
 
 559   my ($self, $myconfig, $form) = @_;
 
 562   # connect to database
 
 563   my $dbh = $form->dbconnect($myconfig);
 
 565   $query = qq|DELETE FROM leads
 
 567   do_query($form, $dbh, $query, $form->{id});
 
 571   $main::lxdebug->leave_sub();
 
 575   $main::lxdebug->enter_sub();
 
 577   my ($self, $myconfig, $form, $return_list) = @_;
 
 579   # connect to database
 
 580   my $dbh = $form->dbconnect($myconfig);
 
 583     "SELECT id, description, template_code, article_code, " .
 
 584     "  output_numberformat, output_dateformat, output_longdates " .
 
 585     "FROM language ORDER BY description";
 
 587   my $sth = $dbh->prepare($query);
 
 588   $sth->execute || $form->dberror($query);
 
 592   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 593     push(@{ $ary }, $ref);
 
 599   $main::lxdebug->leave_sub();
 
 609   $main::lxdebug->enter_sub();
 
 611   my ($self, $myconfig, $form) = @_;
 
 613   # connect to database
 
 614   my $dbh = $form->dbconnect($myconfig);
 
 617     "SELECT description, template_code, article_code, " .
 
 618     "  output_numberformat, output_dateformat, output_longdates " .
 
 619     "FROM language WHERE id = ?";
 
 620   my $sth = $dbh->prepare($query);
 
 621   $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
 
 623   my $ref = $sth->fetchrow_hashref("NAME_lc");
 
 625   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
 631   $main::lxdebug->leave_sub();
 
 634 sub get_language_details {
 
 635   $main::lxdebug->enter_sub();
 
 637   my ($self, $myconfig, $form, $id) = @_;
 
 639   # connect to database
 
 640   my $dbh = $form->dbconnect($myconfig);
 
 643     "SELECT template_code, " .
 
 644     "  output_numberformat, output_dateformat, output_longdates " .
 
 645     "FROM language WHERE id = ?";
 
 646   my @res = selectrow_query($form, $dbh, $query, $id);
 
 649   $main::lxdebug->leave_sub();
 
 655   $main::lxdebug->enter_sub();
 
 657   my ($self, $myconfig, $form) = @_;
 
 659   # connect to database
 
 660   my $dbh = $form->dbconnect($myconfig);
 
 661   my (@values, $query);
 
 663   map({ push(@values, $form->{$_}); }
 
 664       qw(description template_code article_code
 
 665          output_numberformat output_dateformat output_longdates));
 
 667   # id is the old record
 
 670       "UPDATE language SET " .
 
 671       "  description = ?, template_code = ?, article_code = ?, " .
 
 672       "  output_numberformat = ?, output_dateformat = ?, " .
 
 673       "  output_longdates = ? " .
 
 675     push(@values, $form->{id});
 
 678       "INSERT INTO language (" .
 
 679       "  description, template_code, article_code, " .
 
 680       "  output_numberformat, output_dateformat, output_longdates" .
 
 681       ") VALUES (?, ?, ?, ?, ?, ?)";
 
 683   do_query($form, $dbh, $query, @values);
 
 687   $main::lxdebug->leave_sub();
 
 690 sub delete_language {
 
 691   $main::lxdebug->enter_sub();
 
 693   my ($self, $myconfig, $form) = @_;
 
 696   # connect to database
 
 697   my $dbh = $form->dbconnect_noauto($myconfig);
 
 699   foreach my $table (qw(generic_translations units_language)) {
 
 700     $query = qq|DELETE FROM $table WHERE language_id = ?|;
 
 701     do_query($form, $dbh, $query, $form->{"id"});
 
 704   $query = "DELETE FROM language WHERE id = ?";
 
 705   do_query($form, $dbh, $query, $form->{"id"});
 
 710   $main::lxdebug->leave_sub();
 
 715   $main::lxdebug->enter_sub();
 
 717   my ($self, $myconfig, $form) = @_;
 
 719   # connect to database
 
 720   my $dbh = $form->dbconnect($myconfig);
 
 722   my $query = qq|SELECT id, description,
 
 724                  (SELECT accno FROM chart WHERE id = inventory_accno_id) AS inventory_accno,
 
 726                  (SELECT accno FROM chart WHERE id = income_accno_id_0) AS income_accno_0,
 
 728                  (SELECT accno FROM chart WHERE id = expense_accno_id_0) AS expense_accno_0,
 
 730                  (SELECT accno FROM chart WHERE id = income_accno_id_1) AS income_accno_1,
 
 732                  (SELECT accno FROM chart WHERE id = expense_accno_id_1) AS expense_accno_1,
 
 734                  (SELECT accno FROM chart WHERE id = income_accno_id_2) AS income_accno_2,
 
 736                  (select accno FROM chart WHERE id = expense_accno_id_2) AS expense_accno_2,
 
 738                  (SELECT accno FROM chart WHERE id = income_accno_id_3) AS income_accno_3,
 
 740                  (SELECT accno FROM chart WHERE id = expense_accno_id_3) AS expense_accno_3
 
 744   my $sth = $dbh->prepare($query);
 
 745   $sth->execute || $form->dberror($query);
 
 748   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 749     push @{ $form->{ALL} }, $ref;
 
 755   $main::lxdebug->leave_sub();
 
 758 sub get_buchungsgruppe {
 
 759   $main::lxdebug->enter_sub();
 
 761   my ($self, $myconfig, $form) = @_;
 
 764   # connect to database
 
 765   my $dbh = $form->dbconnect($myconfig);
 
 769       qq|SELECT description, inventory_accno_id,
 
 770          (SELECT accno FROM chart WHERE id = inventory_accno_id) AS inventory_accno,
 
 772          (SELECT accno FROM chart WHERE id = income_accno_id_0) AS income_accno_0,
 
 774          (SELECT accno FROM chart WHERE id = expense_accno_id_0) AS expense_accno_0,
 
 776          (SELECT accno FROM chart WHERE id = income_accno_id_1) AS income_accno_1,
 
 778          (SELECT accno FROM chart WHERE id = expense_accno_id_1) AS expense_accno_1,
 
 780          (SELECT accno FROM chart WHERE id = income_accno_id_2) AS income_accno_2,
 
 782          (select accno FROM chart WHERE id = expense_accno_id_2) AS expense_accno_2,
 
 784          (SELECT accno FROM chart WHERE id = income_accno_id_3) AS income_accno_3,
 
 786          (SELECT accno FROM chart WHERE id = expense_accno_id_3) AS expense_accno_3
 
 789     my $sth = $dbh->prepare($query);
 
 790     $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
 792     my $ref = $sth->fetchrow_hashref("NAME_lc");
 
 794     map { $form->{$_} = $ref->{$_} } keys %$ref;
 
 799       qq|SELECT count(id) = 0 AS orphaned
 
 801          WHERE buchungsgruppen_id = ?|;
 
 802     ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
 
 805   $query = "SELECT inventory_accno_id, income_accno_id, expense_accno_id ".
 
 807   ($form->{"std_inventory_accno_id"}, $form->{"std_income_accno_id"},
 
 808    $form->{"std_expense_accno_id"}) = selectrow_query($form, $dbh, $query);
 
 811   $query = qq|SELECT c.accno, c.description, c.link, c.id,
 
 812               d.inventory_accno_id, d.income_accno_id, d.expense_accno_id
 
 813               FROM chart c, defaults d
 
 814               WHERE c.link LIKE '%$module%'
 
 818   my $sth = $dbh->prepare($query);
 
 819   $sth->execute || $form->dberror($query);
 
 820   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 821     foreach my $key (split(/:/, $ref->{link})) {
 
 822       if (!$form->{"std_inventory_accno_id"} && ($key eq "IC")) {
 
 823         $form->{"std_inventory_accno_id"} = $ref->{"id"};
 
 825       if ($key =~ /$module/) {
 
 826         if (   ($ref->{id} eq $ref->{inventory_accno_id})
 
 827             || ($ref->{id} eq $ref->{income_accno_id})
 
 828             || ($ref->{id} eq $ref->{expense_accno_id})) {
 
 829           push @{ $form->{"${module}_links"}{$key} },
 
 830             { accno       => $ref->{accno},
 
 831               description => $ref->{description},
 
 832               selected    => "selected",
 
 835           push @{ $form->{"${module}_links"}{$key} },
 
 836             { accno       => $ref->{accno},
 
 837               description => $ref->{description},
 
 849   $main::lxdebug->leave_sub();
 
 852 sub save_buchungsgruppe {
 
 853   $main::lxdebug->enter_sub();
 
 855   my ($self, $myconfig, $form) = @_;
 
 857   # connect to database
 
 858   my $dbh = $form->dbconnect($myconfig);
 
 860   my @values = ($form->{description}, $form->{inventory_accno_id},
 
 861                 $form->{income_accno_id_0}, $form->{expense_accno_id_0},
 
 862                 $form->{income_accno_id_1}, $form->{expense_accno_id_1},
 
 863                 $form->{income_accno_id_2}, $form->{expense_accno_id_2},
 
 864                 $form->{income_accno_id_3}, $form->{expense_accno_id_3});
 
 868   # id is the old record
 
 870     $query = qq|UPDATE buchungsgruppen SET
 
 871                 description = ?, inventory_accno_id = ?,
 
 872                 income_accno_id_0 = ?, expense_accno_id_0 = ?,
 
 873                 income_accno_id_1 = ?, expense_accno_id_1 = ?,
 
 874                 income_accno_id_2 = ?, expense_accno_id_2 = ?,
 
 875                 income_accno_id_3 = ?, expense_accno_id_3 = ?
 
 877     push(@values, $form->{id});
 
 879     $query = qq|SELECT COALESCE(MAX(sortkey) + 1, 1) FROM buchungsgruppen|;
 
 880     my ($sortkey) = $dbh->selectrow_array($query);
 
 881     $form->dberror($query) if ($dbh->err);
 
 882     push(@values, $sortkey);
 
 883     $query = qq|INSERT INTO buchungsgruppen
 
 884                 (description, inventory_accno_id,
 
 885                 income_accno_id_0, expense_accno_id_0,
 
 886                 income_accno_id_1, expense_accno_id_1,
 
 887                 income_accno_id_2, expense_accno_id_2,
 
 888                 income_accno_id_3, expense_accno_id_3,
 
 890                 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
 
 892   do_query($form, $dbh, $query, @values);
 
 896   $main::lxdebug->leave_sub();
 
 899 sub delete_buchungsgruppe {
 
 900   $main::lxdebug->enter_sub();
 
 902   my ($self, $myconfig, $form) = @_;
 
 904   # connect to database
 
 905   my $dbh = $form->dbconnect($myconfig);
 
 907   my $query = qq|DELETE FROM buchungsgruppen WHERE id = ?|;
 
 908   do_query($form, $dbh, $query, $form->{id});
 
 912   $main::lxdebug->leave_sub();
 
 916   $main::lxdebug->enter_sub();
 
 918   my ($self, $myconfig, $form, $table) = @_;
 
 920   # connect to database
 
 921   my $dbh = $form->get_standard_dbh($myconfig);
 
 925        (SELECT sortkey FROM $table WHERE id = ?) AS sortkey1,
 
 926        (SELECT sortkey FROM $table WHERE id = ?) AS sortkey2|;
 
 927   my @values   = ($form->{"id1"}, $form->{"id2"});
 
 928   my @sortkeys = selectrow_query($form, $dbh, $query, @values);
 
 930   $query  = qq|UPDATE $table SET sortkey = ? WHERE id = ?|;
 
 931   my $sth = prepare_query($form, $dbh, $query);
 
 933   do_statement($form, $sth, $query, $sortkeys[1], $form->{"id1"});
 
 934   do_statement($form, $sth, $query, $sortkeys[0], $form->{"id2"});
 
 940   $main::lxdebug->leave_sub();
 
 943 sub prepare_template_filename {
 
 944   $main::lxdebug->enter_sub();
 
 946   my ($self, $myconfig, $form) = @_;
 
 948   my ($filename, $display_filename);
 
 950   if ($form->{type} eq "stylesheet") {
 
 951     $filename = "css/$myconfig->{stylesheet}";
 
 952     $display_filename = $myconfig->{stylesheet};
 
 955     $filename = $form->{formname};
 
 957     if ($form->{language}) {
 
 958       my ($id, $template_code) = split(/--/, $form->{language});
 
 959       $filename .= "_${template_code}";
 
 962     if ($form->{printer}) {
 
 963       my ($id, $template_code) = split(/--/, $form->{printer});
 
 964       $filename .= "_${template_code}";
 
 967     $filename .= "." . ($form->{format} eq "html" ? "html" : "tex");
 
 968     $filename =~ s|.*/||;
 
 969     $display_filename = $filename;
 
 970     $filename = "$myconfig->{templates}/$filename";
 
 973   $main::lxdebug->leave_sub();
 
 975   return ($filename, $display_filename);
 
 980   $main::lxdebug->enter_sub();
 
 982   my ($self, $filename) = @_;
 
 984   my ($content, $lines) = ("", 0);
 
 988   if (open(TEMPLATE, $filename)) {
 
 996   $content = Encode::decode('utf-8-strict', $content) if $::locale->is_utf8;
 
 998   $main::lxdebug->leave_sub();
 
1000   return ($content, $lines);
 
1004   $main::lxdebug->enter_sub();
 
1006   my ($self, $filename, $content) = @_;
 
1012   if (open(TEMPLATE, ">$filename")) {
 
1013     $content = Encode::encode('utf-8-strict', $content) if $::locale->is_utf8;
 
1014     $content =~ s/\r\n/\n/g;
 
1015     print(TEMPLATE $content);
 
1021   $main::lxdebug->leave_sub();
 
1027   $main::lxdebug->enter_sub();
 
1032   my $myconfig = \%main::myconfig;
 
1033   my $form     = $main::form;
 
1035   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
 
1038   map { ($accnos{$_}) = split(m/--/, $form->{$_}) } qw(inventory_accno income_accno expense_accno fxgain_accno fxloss_accno ar_paid_accno);
 
1040   $form->{curr}  =~ s/ //g;
 
1041   my @currencies =  grep { $_ ne '' } split m/:/, $form->{curr};
 
1042   my $currency   =  join ':', @currencies;
 
1044   # these defaults are database wide
 
1047     qq|UPDATE defaults SET
 
1048         inventory_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
 
1049         income_accno_id    = (SELECT c.id FROM chart c WHERE c.accno = ?),
 
1050         expense_accno_id   = (SELECT c.id FROM chart c WHERE c.accno = ?),
 
1051         fxgain_accno_id    = (SELECT c.id FROM chart c WHERE c.accno = ?),
 
1052         fxloss_accno_id    = (SELECT c.id FROM chart c WHERE c.accno = ?),
 
1053         ar_paid_accno_id   = (SELECT c.id FROM chart c WHERE c.accno = ?),
 
1069   my @values = ($accnos{inventory_accno}, $accnos{income_accno}, $accnos{expense_accno},
 
1070                 $accnos{fxgain_accno},    $accnos{fxloss_accno}, $accnos{ar_paid_accno},
 
1071                 $form->{invnumber},       $form->{cnnumber},
 
1072                 $form->{sonumber},        $form->{ponumber},
 
1073                 $form->{sqnumber},        $form->{rfqnumber},
 
1074                 $form->{customernumber},  $form->{vendornumber},
 
1075                 $form->{articlenumber},   $form->{servicenumber},
 
1076                 $form->{sdonumber},       $form->{pdonumber},
 
1078                 $form->{businessnumber},  $form->{weightunit});
 
1079   do_query($form, $dbh, $query, @values);
 
1083   $main::lxdebug->leave_sub();
 
1087 sub save_preferences {
 
1088   $main::lxdebug->enter_sub();
 
1090   my ($self, $myconfig, $form) = @_;
 
1092   my $dbh = $form->get_standard_dbh($myconfig);
 
1094   my ($currency, $businessnumber) = selectrow_query($form, $dbh, qq|SELECT curr, businessnumber FROM defaults|);
 
1097   my $query = qq|UPDATE employee SET name = ? WHERE login = ?|;
 
1098   do_query($form, $dbh, $query, $form->{name}, $form->{login});
 
1100   my $rc = $dbh->commit();
 
1102   # save first currency in myconfig
 
1103   $currency               =~ s/:.*//;
 
1104   $form->{currency}       =  $currency;
 
1106   $form->{businessnumber} =  $businessnumber;
 
1108   $myconfig = new User($form->{login});
 
1110   foreach my $item (keys %$form) {
 
1111     $myconfig->{$item} = $form->{$item};
 
1114   $myconfig->save_member;
 
1116   my $auth = $main::auth;
 
1118   $main::lxdebug->leave_sub();
 
1124   $main::lxdebug->enter_sub();
 
1129   my $myconfig = \%main::myconfig;
 
1130   my $form     = $main::form;
 
1132   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
 
1134   my $defaults = selectfirst_hashref_query($form, $dbh, qq|SELECT * FROM defaults|) || {};
 
1136   $defaults->{weightunit} ||= 'kg';
 
1138   $main::lxdebug->leave_sub();
 
1143 sub defaultaccounts {
 
1144   $main::lxdebug->enter_sub();
 
1146   my ($self, $myconfig, $form) = @_;
 
1148   # connect to database
 
1149   my $dbh = $form->dbconnect($myconfig);
 
1151   # get defaults from defaults table
 
1152   my $query = qq|SELECT * FROM defaults|;
 
1153   my $sth   = $dbh->prepare($query);
 
1154   $sth->execute || $form->dberror($query);
 
1156   $form->{defaults}               = $sth->fetchrow_hashref("NAME_lc");
 
1157   $form->{defaults}{IC}           = $form->{defaults}{inventory_accno_id};
 
1158   $form->{defaults}{IC_income}    = $form->{defaults}{income_accno_id};
 
1159   $form->{defaults}{IC_expense}   = $form->{defaults}{expense_accno_id};
 
1160   $form->{defaults}{FX_gain}      = $form->{defaults}{fxgain_accno_id};
 
1161   $form->{defaults}{FX_loss}      = $form->{defaults}{fxloss_accno_id};
 
1162   $form->{defaults}{AR_paid}      = $form->{defaults}{ar_paid_accno_id};
 
1164   $form->{defaults}{weightunit} ||= 'kg';
 
1168   $query = qq|SELECT c.id, c.accno, c.description, c.link
 
1170               WHERE c.link LIKE '%IC%'
 
1172   $sth = $dbh->prepare($query);
 
1173   $sth->execute || $self->dberror($query);
 
1175   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
1176     foreach my $key (split(/:/, $ref->{link})) {
 
1179         if ($key =~ /cogs/) {
 
1180           $nkey = "IC_expense";
 
1182         if ($key =~ /sale/) {
 
1183           $nkey = "IC_income";
 
1185         %{ $form->{IC}{$nkey}{ $ref->{accno} } } = (
 
1187                                              description => $ref->{description}
 
1194   $query = qq|SELECT c.id, c.accno, c.description
 
1196               WHERE c.category = 'I'
 
1197               AND c.charttype = 'A'
 
1199   $sth = $dbh->prepare($query);
 
1200   $sth->execute || $self->dberror($query);
 
1202   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
1203     %{ $form->{IC}{FX_gain}{ $ref->{accno} } } = (
 
1205                                              description => $ref->{description}
 
1210   $query = qq|SELECT c.id, c.accno, c.description
 
1212               WHERE c.category = 'E'
 
1213               AND c.charttype = 'A'
 
1215   $sth = $dbh->prepare($query);
 
1216   $sth->execute || $self->dberror($query);
 
1218   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
1219     %{ $form->{IC}{FX_loss}{ $ref->{accno} } } = (
 
1221                                              description => $ref->{description}
 
1226   # now get the tax rates and numbers
 
1227   $query = qq|SELECT c.id, c.accno, c.description,
 
1228               t.rate * 100 AS rate, t.taxnumber
 
1230               WHERE c.id = t.chart_id|;
 
1232   $sth = $dbh->prepare($query);
 
1233   $sth->execute || $form->dberror($query);
 
1235   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
1236     $form->{taxrates}{ $ref->{accno} }{id}          = $ref->{id};
 
1237     $form->{taxrates}{ $ref->{accno} }{description} = $ref->{description};
 
1238     $form->{taxrates}{ $ref->{accno} }{taxnumber}   = $ref->{taxnumber}
 
1239       if $ref->{taxnumber};
 
1240     $form->{taxrates}{ $ref->{accno} }{rate} = $ref->{rate} if $ref->{rate};
 
1242   # Abfrage für Standard Umlaufvermögenskonto
 
1244     qq|SELECT id, accno, description, link | .
 
1246     qq|WHERE link LIKE ? |.
 
1248   $sth = prepare_execute_query($form, $dbh, $query, '%AR%');
 
1249   $sth->execute || $form->dberror($query);#
 
1250   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
1251     foreach my $item (split(/:/, $ref->{link})) {
 
1252       if ($item eq "AR_paid") {
 
1253         %{ $form->{IC}{AR_paid}{ $ref->{accno} } } = (
 
1255                                              description => $ref->{description}
 
1264   $main::lxdebug->leave_sub();
 
1268   $main::lxdebug->enter_sub();
 
1270   my ($self, $myconfig, $form) = @_;
 
1272   my $dbh = $form->dbconnect($myconfig);
 
1274   my $query = qq|SELECT closedto, revtrans FROM defaults|;
 
1275   my $sth   = $dbh->prepare($query);
 
1276   $sth->execute || $form->dberror($query);
 
1278   ($form->{closedto}, $form->{revtrans}) = $sth->fetchrow_array;
 
1284   $main::lxdebug->leave_sub();
 
1288   $main::lxdebug->enter_sub();
 
1290   my ($self, $myconfig, $form) = @_;
 
1292   my $dbh = $form->dbconnect($myconfig);
 
1294   my ($query, @values);
 
1296   if ($form->{revtrans}) {
 
1297     $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '1'|;
 
1299   } elsif ($form->{closedto}) {
 
1300     $query = qq|UPDATE defaults SET closedto = ?, revtrans = '0'|;
 
1301     @values = (conv_date($form->{closedto}));
 
1304     $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '0'|;
 
1307   # set close in defaults
 
1308   do_query($form, $dbh, $query, @values);
 
1312   $main::lxdebug->leave_sub();
 
1316   my ($self, $units, $unit_name, $factor) = @_;
 
1318   $factor = 1 unless ($factor);
 
1320   my $unit = $units->{$unit_name};
 
1322   if (!defined($unit) || !$unit->{"base_unit"} ||
 
1323       ($unit_name eq $unit->{"base_unit"})) {
 
1324     return ($unit_name, $factor);
 
1327   return AM->get_base_unit($units, $unit->{"base_unit"}, $factor * $unit->{"factor"});
 
1330 sub retrieve_units {
 
1331   $main::lxdebug->enter_sub();
 
1333   my ($self, $myconfig, $form, $prefix) = @_;
 
1335   my $dbh = $form->get_standard_dbh;
 
1337   my $query = "SELECT *, base_unit AS original_base_unit FROM units";
 
1339   my $sth = prepare_execute_query($form, $dbh, $query);
 
1342   while (my $ref = $sth->fetchrow_hashref()) {
 
1343     $units->{$ref->{"name"}} = $ref;
 
1347   my $query_lang = "SELECT id, template_code FROM language ORDER BY description";
 
1348   $sth = $dbh->prepare($query_lang);
 
1349   $sth->execute() || $form->dberror($query_lang);
 
1351   while (my $ref = $sth->fetchrow_hashref()) {
 
1352     push(@languages, $ref);
 
1356   $query_lang = "SELECT ul.localized, ul.localized_plural, l.id, l.template_code " .
 
1357     "FROM units_language ul " .
 
1358     "LEFT JOIN language l ON ul.language_id = l.id " .
 
1359     "WHERE ul.unit = ?";
 
1360   $sth = $dbh->prepare($query_lang);
 
1362   foreach my $unit (values(%{$units})) {
 
1363     ($unit->{"${prefix}base_unit"}, $unit->{"${prefix}factor"}) = AM->get_base_unit($units, $unit->{"name"});
 
1365     $unit->{"LANGUAGES"} = {};
 
1366     foreach my $lang (@languages) {
 
1367       $unit->{"LANGUAGES"}->{$lang->{"template_code"}} = { "template_code" => $lang->{"template_code"} };
 
1370     $sth->execute($unit->{"name"}) || $form->dberror($query_lang . " (" . $unit->{"name"} . ")");
 
1371     while (my $ref = $sth->fetchrow_hashref()) {
 
1372       map({ $unit->{"LANGUAGES"}->{$ref->{"template_code"}}->{$_} = $ref->{$_} } keys(%{$ref}));
 
1377   $main::lxdebug->leave_sub();
 
1382 sub retrieve_all_units {
 
1383   $main::lxdebug->enter_sub();
 
1387   if (!$main::all_units) {
 
1388     $main::all_units = $self->retrieve_units(\%main::myconfig, $main::form);
 
1391   $main::lxdebug->leave_sub();
 
1393   return $main::all_units;
 
1397 sub translate_units {
 
1398   $main::lxdebug->enter_sub();
 
1400   my ($self, $form, $template_code, $unit, $amount) = @_;
 
1402   my $units = $self->retrieve_units(\%main::myconfig, $form);
 
1404   my $h = $units->{$unit}->{"LANGUAGES"}->{$template_code};
 
1405   my $new_unit = $unit;
 
1407     if (($amount != 1) && $h->{"localized_plural"}) {
 
1408       $new_unit = $h->{"localized_plural"};
 
1409     } elsif ($h->{"localized"}) {
 
1410       $new_unit = $h->{"localized"};
 
1414   $main::lxdebug->leave_sub();
 
1420   $main::lxdebug->enter_sub();
 
1422   my ($self, $myconfig, $form, $units) = @_;
 
1424   my $dbh = $form->dbconnect($myconfig);
 
1426   map({ $_->{"in_use"} = 0; } values(%{$units}));
 
1428   foreach my $unit (values(%{$units})) {
 
1429     my $base_unit = $unit->{"original_base_unit"};
 
1430     while ($base_unit) {
 
1431       $units->{$base_unit}->{"in_use"} = 1;
 
1432       $units->{$base_unit}->{"DEPENDING_UNITS"} = [] unless ($units->{$base_unit}->{"DEPENDING_UNITS"});
 
1433       push(@{$units->{$base_unit}->{"DEPENDING_UNITS"}}, $unit->{"name"});
 
1434       $base_unit = $units->{$base_unit}->{"original_base_unit"};
 
1438   foreach my $unit (values(%{$units})) {
 
1439     map({ $_ = $dbh->quote($_); } @{$unit->{"DEPENDING_UNITS"}});
 
1441     foreach my $table (qw(parts invoice orderitems)) {
 
1442       my $query = "SELECT COUNT(*) FROM $table WHERE unit ";
 
1444       if (0 == scalar(@{$unit->{"DEPENDING_UNITS"}})) {
 
1445         $query .= "= " . $dbh->quote($unit->{"name"});
 
1447         $query .= "IN (" . $dbh->quote($unit->{"name"}) . "," .
 
1448           join(",", map({ $dbh->quote($_) } @{$unit->{"DEPENDING_UNITS"}})) . ")";
 
1451       my ($count) = $dbh->selectrow_array($query);
 
1452       $form->dberror($query) if ($dbh->err);
 
1455         $unit->{"in_use"} = 1;
 
1463   $main::lxdebug->leave_sub();
 
1466 sub convertible_units {
 
1467   $main::lxdebug->enter_sub();
 
1471   my $filter_unit = shift;
 
1472   my $not_smaller = shift;
 
1474   my $conv_units = [];
 
1476   $filter_unit = $units->{$filter_unit};
 
1478   foreach my $name (sort { lc $a cmp lc $b } keys %{ $units }) {
 
1479     my $unit = $units->{$name};
 
1481     if (($unit->{base_unit} eq $filter_unit->{base_unit}) &&
 
1482         (!$not_smaller || ($unit->{factor} >= $filter_unit->{factor}))) {
 
1483       push @{$conv_units}, $unit;
 
1487   my @sorted = sort { $b->{factor} <=> $a->{factor} } @{ $conv_units };
 
1489   $main::lxdebug->leave_sub();
 
1494 # if $a is translatable to $b, return the factor between them.
 
1497   $main::lxdebug->enter_sub(2);
 
1498   my ($this, $a, $b, $all_units) = @_;
 
1500   $main::lxdebug->leave_sub(2) and return 0 unless $a && $b;
 
1501   $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a} && $all_units->{$b};
 
1502   $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a}{base_unit} eq $all_units->{$b}{base_unit};
 
1503   $main::lxdebug->leave_sub(2) and return $all_units->{$a}{factor} / $all_units->{$b}{factor};
 
1506 sub unit_select_data {
 
1507   $main::lxdebug->enter_sub();
 
1509   my ($self, $units, $selected, $empty_entry, $convertible_into) = @_;
 
1514     push(@{$select}, { "name" => "", "base_unit" => "", "factor" => "", "selected" => "" });
 
1517   foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
 
1518     if (!$convertible_into ||
 
1519         ($units->{$convertible_into} &&
 
1520          ($units->{$convertible_into}->{base_unit} eq $units->{$unit}->{base_unit}))) {
 
1521       push @{$select}, { "name"      => $unit,
 
1522                          "base_unit" => $units->{$unit}->{"base_unit"},
 
1523                          "factor"    => $units->{$unit}->{"factor"},
 
1524                          "selected"  => ($unit eq $selected) ? "selected" : "" };
 
1528   $main::lxdebug->leave_sub();
 
1533 sub unit_select_html {
 
1534   $main::lxdebug->enter_sub();
 
1536   my ($self, $units, $name, $selected, $convertible_into) = @_;
 
1538   my $select = "<select name=${name}>";
 
1540   foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
 
1541     if (!$convertible_into ||
 
1542         ($units->{$convertible_into} &&
 
1543          ($units->{$convertible_into}->{"base_unit"} eq $units->{$unit}->{"base_unit"}))) {
 
1544       $select .= "<option" . (($unit eq $selected) ? " selected" : "") . ">${unit}</option>";
 
1547   $select .= "</select>";
 
1549   $main::lxdebug->leave_sub();
 
1555   $main::lxdebug->enter_sub();
 
1559   my $units = $self->retrieve_all_units();
 
1564   while (2 <= scalar(@_)) {
 
1565     my $qty  = shift(@_);
 
1566     my $unit = $units->{shift(@_)};
 
1568     croak "No unit defined with name $unit" if (!defined $unit);
 
1571       $base_unit = $unit->{base_unit};
 
1572     } elsif ($base_unit ne $unit->{base_unit}) {
 
1573       croak "Adding values with incompatible base units $base_unit/$unit->{base_unit}";
 
1576     $sum += $qty * $unit->{factor};
 
1579   $main::lxdebug->leave_sub();
 
1581   return wantarray ? ($sum, $base_unit) : $sum;
 
1585   $main::lxdebug->enter_sub();
 
1587   my ($self, $myconfig, $form, $name, $base_unit, $factor, $languages) = @_;
 
1589   my $dbh = $form->dbconnect_noauto($myconfig);
 
1591   my $query = qq|SELECT COALESCE(MAX(sortkey), 0) + 1 FROM units|;
 
1592   my ($sortkey) = selectrow_query($form, $dbh, $query);
 
1594   $query = "INSERT INTO units (name, base_unit, factor, sortkey) " .
 
1595     "VALUES (?, ?, ?, ?)";
 
1596   do_query($form, $dbh, $query, $name, $base_unit, $factor, $sortkey);
 
1599     $query = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
 
1600     my $sth = $dbh->prepare($query);
 
1601     foreach my $lang (@{$languages}) {
 
1602       my @values = ($name, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
 
1603       $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
 
1611   $main::lxdebug->leave_sub();
 
1615   $main::lxdebug->enter_sub();
 
1617   my ($self, $myconfig, $form, $units, $delete_units) = @_;
 
1619   my $dbh = $form->dbconnect_noauto($myconfig);
 
1621   my ($base_unit, $unit, $sth, $query);
 
1623   $query = "DELETE FROM units_language";
 
1624   $dbh->do($query) || $form->dberror($query);
 
1626   if ($delete_units && (0 != scalar(@{$delete_units}))) {
 
1627     $query = "DELETE FROM units WHERE name IN (";
 
1628     map({ $query .= "?," } @{$delete_units});
 
1629     substr($query, -1, 1) = ")";
 
1630     $dbh->do($query, undef, @{$delete_units}) ||
 
1631       $form->dberror($query . " (" . join(", ", @{$delete_units}) . ")");
 
1634   $query = "UPDATE units SET name = ?, base_unit = ?, factor = ? WHERE name = ?";
 
1635   $sth = $dbh->prepare($query);
 
1637   my $query_lang = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
 
1638   my $sth_lang = $dbh->prepare($query_lang);
 
1640   foreach $unit (values(%{$units})) {
 
1641     $unit->{"depth"} = 0;
 
1642     my $base_unit = $unit;
 
1643     while ($base_unit->{"base_unit"}) {
 
1645       $base_unit = $units->{$base_unit->{"base_unit"}};
 
1649   foreach $unit (sort({ $a->{"depth"} <=> $b->{"depth"} } values(%{$units}))) {
 
1650     if ($unit->{"LANGUAGES"}) {
 
1651       foreach my $lang (@{$unit->{"LANGUAGES"}}) {
 
1652         next unless ($lang->{"id"} && $lang->{"localized"});
 
1653         my @values = ($unit->{"name"}, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
 
1654         $sth_lang->execute(@values) || $form->dberror($query_lang . " (" . join(", ", @values) . ")");
 
1658     next if ($unit->{"unchanged_unit"});
 
1660     my @values = ($unit->{"name"}, $unit->{"base_unit"}, $unit->{"factor"}, $unit->{"old_name"});
 
1661     $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
 
1665   $sth_lang->finish();
 
1669   $main::lxdebug->leave_sub();
 
1673   $main::lxdebug->enter_sub();
 
1675   my ($self, $myconfig, $form) = @_;
 
1677   # connect to database
 
1678   my $dbh = $form->dbconnect($myconfig);
 
1680   my $query = qq|SELECT
 
1684                    round(t.rate * 100, 2) AS rate,
 
1685                    (SELECT accno FROM chart WHERE id = chart_id) AS taxnumber,
 
1686                    (SELECT description FROM chart WHERE id = chart_id) AS account_description
 
1690   my $sth = $dbh->prepare($query);
 
1691   $sth->execute || $form->dberror($query);
 
1694   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
1695     push @{ $form->{TAX} }, $ref;
 
1701   $main::lxdebug->leave_sub();
 
1704 sub get_tax_accounts {
 
1705   $main::lxdebug->enter_sub();
 
1707   my ($self, $myconfig, $form) = @_;
 
1709   my $dbh = $form->dbconnect($myconfig);
 
1711   # get Accounts from chart
 
1712   my $query = qq{ SELECT
 
1714                  accno || ' - ' || description AS taxaccount
 
1716                WHERE link LIKE '%_tax%'
 
1720   my $sth = $dbh->prepare($query);
 
1721   $sth->execute || $form->dberror($query);
 
1723   $form->{ACCOUNTS} = [];
 
1724   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
1725     push @{ $form->{ACCOUNTS} }, $ref;
 
1732   $main::lxdebug->leave_sub();
 
1736   $main::lxdebug->enter_sub();
 
1738   my ($self, $myconfig, $form) = @_;
 
1740   # connect to database
 
1741   my $dbh = $form->dbconnect($myconfig);
 
1743   my $query = qq|SELECT
 
1746                    round(rate * 100, 2) AS rate,
 
1751   my $sth = $dbh->prepare($query);
 
1752   $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
1754   my $ref = $sth->fetchrow_hashref("NAME_lc");
 
1756   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
1760   # see if it is used by a taxkey
 
1761   $query = qq|SELECT count(*) FROM taxkeys
 
1762               WHERE tax_id = ? AND chart_id >0|;
 
1764   ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
 
1766   $form->{orphaned} = !$form->{orphaned};
 
1769   if (!$form->{orphaned} ) {
 
1770     $query = qq|SELECT DISTINCT c.id, c.accno
 
1772                 JOIN   tax t ON (t.id = tk.tax_id)
 
1773                 JOIN chart c ON (c.id = tk.chart_id)
 
1774                 WHERE tk.tax_id = ?|;
 
1776     $sth = $dbh->prepare($query);
 
1777     $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
1779     $form->{TAXINUSE} = [];
 
1780     while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
1781       push @{ $form->{TAXINUSE} }, $ref;
 
1789   $main::lxdebug->leave_sub();
 
1793   $main::lxdebug->enter_sub();
 
1795   my ($self, $myconfig, $form) = @_;
 
1798   # connect to database
 
1799   my $dbh = $form->get_standard_dbh($myconfig);
 
1801   $form->{rate} = $form->{rate} / 100;
 
1803   my @values = ($form->{taxkey}, $form->{taxdescription}, $form->{rate}, $form->{chart_id}, $form->{chart_id} );
 
1804   if ($form->{id} ne "") {
 
1805     $query = qq|UPDATE tax SET
 
1810                   taxnumber      = (SELECT accno FROM chart WHERE id= ? )
 
1812     push(@values, $form->{id});
 
1816     $query = qq|INSERT INTO tax (
 
1823                 VALUES (?, ?, ?, ?, (SELECT accno FROM chart WHERE id = ?) )|;
 
1825   do_query($form, $dbh, $query, @values);
 
1829   $main::lxdebug->leave_sub();
 
1833   $main::lxdebug->enter_sub();
 
1835   my ($self, $myconfig, $form) = @_;
 
1838   # connect to database
 
1839   my $dbh = $form->get_standard_dbh($myconfig);
 
1841   $query = qq|DELETE FROM tax
 
1843   do_query($form, $dbh, $query, $form->{id});
 
1847   $main::lxdebug->leave_sub();
 
1850 sub save_price_factor {
 
1851   $main::lxdebug->enter_sub();
 
1853   my ($self, $myconfig, $form) = @_;
 
1855   # connect to database
 
1856   my $dbh = $form->get_standard_dbh($myconfig);
 
1859   my @values = ($form->{description}, conv_i($form->{factor}));
 
1862     $query = qq|UPDATE price_factors SET description = ?, factor = ? WHERE id = ?|;
 
1863     push @values, conv_i($form->{id});
 
1866     $query = qq|INSERT INTO price_factors (description, factor, sortkey) VALUES (?, ?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM price_factors))|;
 
1869   do_query($form, $dbh, $query, @values);
 
1873   $main::lxdebug->leave_sub();
 
1876 sub get_all_price_factors {
 
1877   $main::lxdebug->enter_sub();
 
1879   my ($self, $myconfig, $form) = @_;
 
1881   # connect to database
 
1882   my $dbh = $form->get_standard_dbh($myconfig);
 
1884   $form->{PRICE_FACTORS} = selectall_hashref_query($form, $dbh, qq|SELECT * FROM price_factors ORDER BY sortkey|);
 
1886   $main::lxdebug->leave_sub();
 
1889 sub get_price_factor {
 
1890   $main::lxdebug->enter_sub();
 
1892   my ($self, $myconfig, $form) = @_;
 
1894   # connect to database
 
1895   my $dbh = $form->get_standard_dbh($myconfig);
 
1897   my $query = qq|SELECT description, factor,
 
1898                    ((SELECT COUNT(*) FROM parts      WHERE price_factor_id = ?) +
 
1899                     (SELECT COUNT(*) FROM invoice    WHERE price_factor_id = ?) +
 
1900                     (SELECT COUNT(*) FROM orderitems WHERE price_factor_id = ?)) = 0 AS orphaned
 
1901                  FROM price_factors WHERE id = ?|;
 
1903   ($form->{description}, $form->{factor}, $form->{orphaned}) = selectrow_query($form, $dbh, $query, (conv_i($form->{id})) x 4);
 
1905   $main::lxdebug->leave_sub();
 
1908 sub delete_price_factor {
 
1909   $main::lxdebug->enter_sub();
 
1911   my ($self, $myconfig, $form) = @_;
 
1913   # connect to database
 
1914   my $dbh = $form->get_standard_dbh($myconfig);
 
1916   do_query($form, $dbh, qq|DELETE FROM price_factors WHERE id = ?|, conv_i($form->{id}));
 
1919   $main::lxdebug->leave_sub();
 
1922 sub save_warehouse {
 
1923   $main::lxdebug->enter_sub();
 
1925   my ($self, $myconfig, $form) = @_;
 
1927   # connect to database
 
1928   my $dbh = $form->get_standard_dbh($myconfig);
 
1930   my ($query, @values, $sth);
 
1933     $query        = qq|SELECT nextval('id')|;
 
1934     ($form->{id}) = selectrow_query($form, $dbh, $query);
 
1936     $query        = qq|INSERT INTO warehouse (id, sortkey) VALUES (?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM warehouse))|;
 
1937     do_query($form, $dbh, $query, $form->{id});
 
1940   do_query($form, $dbh, qq|UPDATE warehouse SET description = ?, invalid = ? WHERE id = ?|,
 
1941            $form->{description}, $form->{invalid} ? 't' : 'f', conv_i($form->{id}));
 
1943   if (0 < $form->{number_of_new_bins}) {
 
1944     $query = qq|INSERT INTO bin (warehouse_id, description) VALUES (?, ?)|;
 
1945     $sth   = prepare_query($form, $dbh, $query);
 
1947     foreach my $i (1..$form->{number_of_new_bins}) {
 
1948       do_statement($form, $sth, $query, conv_i($form->{id}), "$form->{prefix}${i}");
 
1956   $main::lxdebug->leave_sub();
 
1960   $main::lxdebug->enter_sub();
 
1962   my ($self, $myconfig, $form) = @_;
 
1964   # connect to database
 
1965   my $dbh = $form->get_standard_dbh($myconfig);
 
1967   my ($query, @values, $commit_necessary, $sth);
 
1969   @values = map { $form->{"id_${_}"} } grep { $form->{"delete_${_}"} } (1..$form->{rowcount});
 
1972     $query = qq|DELETE FROM bin WHERE id IN (| . join(', ', ('?') x scalar(@values)) . qq|)|;
 
1973     do_query($form, $dbh, $query, @values);
 
1975     $commit_necessary = 1;
 
1978   $query = qq|UPDATE bin SET description = ? WHERE id = ?|;
 
1979   $sth   = prepare_query($form, $dbh, $query);
 
1981   foreach my $row (1..$form->{rowcount}) {
 
1982     next if ($form->{"delete_${row}"});
 
1984     do_statement($form, $sth, $query, $form->{"description_${row}"}, conv_i($form->{"id_${row}"}));
 
1986     $commit_necessary = 1;
 
1991   $dbh->commit() if ($commit_necessary);
 
1993   $main::lxdebug->leave_sub();
 
1996 sub delete_warehouse {
 
1997   $main::lxdebug->enter_sub();
 
1999   my ($self, $myconfig, $form) = @_;
 
2001   # connect to database
 
2002   my $dbh = $form->get_standard_dbh($myconfig);
 
2004   my $id      = conv_i($form->{id});
 
2005   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|;
 
2006   my ($count) = selectrow_query($form, $dbh, $query, $id);
 
2009     $main::lxdebug->leave_sub();
 
2013   do_query($form, $dbh, qq|DELETE FROM bin       WHERE warehouse_id = ?|, conv_i($form->{id}));
 
2014   do_query($form, $dbh, qq|DELETE FROM warehouse WHERE id           = ?|, conv_i($form->{id}));
 
2018   $main::lxdebug->leave_sub();
 
2023 sub get_all_warehouses {
 
2024   $main::lxdebug->enter_sub();
 
2026   my ($self, $myconfig, $form) = @_;
 
2028   # connect to database
 
2029   my $dbh = $form->get_standard_dbh($myconfig);
 
2031   my $query = qq|SELECT w.id, w.description, w.invalid,
 
2032                    (SELECT COUNT(b.description) FROM bin b WHERE b.warehouse_id = w.id) AS number_of_bins
 
2034                  ORDER BY w.sortkey|;
 
2036   $form->{WAREHOUSES} = selectall_hashref_query($form, $dbh, $query);
 
2038   $main::lxdebug->leave_sub();
 
2042   $main::lxdebug->enter_sub();
 
2044   my ($self, $myconfig, $form) = @_;
 
2046   # connect to database
 
2047   my $dbh = $form->get_standard_dbh($myconfig);
 
2049   my $id    = conv_i($form->{id});
 
2050   my $query = qq|SELECT w.description, w.invalid
 
2054   my $ref   = selectfirst_hashref_query($form, $dbh, $query, $id);
 
2056   map { $form->{$_} = $ref->{$_} } keys %{ $ref };
 
2058   $query = qq|SELECT b.*, EXISTS
 
2059                 (SELECT i.warehouse_id
 
2061                  WHERE i.bin_id = b.id
 
2065               WHERE b.warehouse_id = ?|;
 
2067   $form->{BINS} = selectall_hashref_query($form, $dbh, $query, conv_i($form->{id}));
 
2069   $main::lxdebug->leave_sub();