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 = ?),
 
1070   my @values = ($accnos{inventory_accno}, $accnos{income_accno}, $accnos{expense_accno},
 
1071                 $accnos{fxgain_accno},    $accnos{fxloss_accno}, $accnos{ar_paid_accno},
 
1072                 $form->{invnumber},       $form->{cnnumber},
 
1073                 $form->{sonumber},        $form->{ponumber},
 
1074                 $form->{sqnumber},        $form->{rfqnumber},
 
1075                 $form->{customernumber},  $form->{vendornumber},
 
1076                 $form->{articlenumber},   $form->{servicenumber},
 
1077                 $form->{sdonumber},       $form->{pdonumber},
 
1079                 $form->{businessnumber},  $form->{weightunit},
 
1080                 conv_i($form->{language_id}));
 
1081   do_query($form, $dbh, $query, @values);
 
1085   $main::lxdebug->leave_sub();
 
1089 sub save_preferences {
 
1090   $main::lxdebug->enter_sub();
 
1092   my ($self, $myconfig, $form) = @_;
 
1094   my $dbh = $form->get_standard_dbh($myconfig);
 
1096   my ($currency, $businessnumber) = selectrow_query($form, $dbh, qq|SELECT curr, businessnumber FROM defaults|);
 
1099   my $query = qq|UPDATE employee SET name = ? WHERE login = ?|;
 
1100   do_query($form, $dbh, $query, $form->{name}, $form->{login});
 
1102   my $rc = $dbh->commit();
 
1104   # save first currency in myconfig
 
1105   $currency               =~ s/:.*//;
 
1106   $form->{currency}       =  $currency;
 
1108   $form->{businessnumber} =  $businessnumber;
 
1110   $myconfig = new User($form->{login});
 
1112   foreach my $item (keys %$form) {
 
1113     $myconfig->{$item} = $form->{$item};
 
1116   $myconfig->save_member;
 
1118   my $auth = $main::auth;
 
1120   $main::lxdebug->leave_sub();
 
1126   $main::lxdebug->enter_sub();
 
1131   my $myconfig = \%main::myconfig;
 
1132   my $form     = $main::form;
 
1134   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
 
1136   my $defaults = selectfirst_hashref_query($form, $dbh, qq|SELECT * FROM defaults|) || {};
 
1138   $defaults->{weightunit} ||= 'kg';
 
1140   $main::lxdebug->leave_sub();
 
1145 sub defaultaccounts {
 
1146   $main::lxdebug->enter_sub();
 
1148   my ($self, $myconfig, $form) = @_;
 
1150   # connect to database
 
1151   my $dbh = $form->dbconnect($myconfig);
 
1153   # get defaults from defaults table
 
1154   my $query = qq|SELECT * FROM defaults|;
 
1155   my $sth   = $dbh->prepare($query);
 
1156   $sth->execute || $form->dberror($query);
 
1158   $form->{defaults}               = $sth->fetchrow_hashref("NAME_lc");
 
1159   $form->{defaults}{IC}           = $form->{defaults}{inventory_accno_id};
 
1160   $form->{defaults}{IC_income}    = $form->{defaults}{income_accno_id};
 
1161   $form->{defaults}{IC_expense}   = $form->{defaults}{expense_accno_id};
 
1162   $form->{defaults}{FX_gain}      = $form->{defaults}{fxgain_accno_id};
 
1163   $form->{defaults}{FX_loss}      = $form->{defaults}{fxloss_accno_id};
 
1164   $form->{defaults}{AR_paid}      = $form->{defaults}{ar_paid_accno_id};
 
1166   $form->{defaults}{weightunit} ||= 'kg';
 
1170   $query = qq|SELECT c.id, c.accno, c.description, c.link
 
1172               WHERE c.link LIKE '%IC%'
 
1174   $sth = $dbh->prepare($query);
 
1175   $sth->execute || $self->dberror($query);
 
1177   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
1178     foreach my $key (split(/:/, $ref->{link})) {
 
1181         if ($key =~ /cogs/) {
 
1182           $nkey = "IC_expense";
 
1184         if ($key =~ /sale/) {
 
1185           $nkey = "IC_income";
 
1187         %{ $form->{IC}{$nkey}{ $ref->{accno} } } = (
 
1189                                              description => $ref->{description}
 
1196   $query = qq|SELECT c.id, c.accno, c.description
 
1198               WHERE c.category = 'I'
 
1199               AND c.charttype = 'A'
 
1201   $sth = $dbh->prepare($query);
 
1202   $sth->execute || $self->dberror($query);
 
1204   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
1205     %{ $form->{IC}{FX_gain}{ $ref->{accno} } } = (
 
1207                                              description => $ref->{description}
 
1212   $query = qq|SELECT c.id, c.accno, c.description
 
1214               WHERE c.category = 'E'
 
1215               AND c.charttype = 'A'
 
1217   $sth = $dbh->prepare($query);
 
1218   $sth->execute || $self->dberror($query);
 
1220   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
1221     %{ $form->{IC}{FX_loss}{ $ref->{accno} } } = (
 
1223                                              description => $ref->{description}
 
1228   # now get the tax rates and numbers
 
1229   $query = qq|SELECT c.id, c.accno, c.description,
 
1230               t.rate * 100 AS rate, t.taxnumber
 
1232               WHERE c.id = t.chart_id|;
 
1234   $sth = $dbh->prepare($query);
 
1235   $sth->execute || $form->dberror($query);
 
1237   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
1238     $form->{taxrates}{ $ref->{accno} }{id}          = $ref->{id};
 
1239     $form->{taxrates}{ $ref->{accno} }{description} = $ref->{description};
 
1240     $form->{taxrates}{ $ref->{accno} }{taxnumber}   = $ref->{taxnumber}
 
1241       if $ref->{taxnumber};
 
1242     $form->{taxrates}{ $ref->{accno} }{rate} = $ref->{rate} if $ref->{rate};
 
1244   # Abfrage für Standard Umlaufvermögenskonto
 
1246     qq|SELECT id, accno, description, link | .
 
1248     qq|WHERE link LIKE ? |.
 
1250   $sth = prepare_execute_query($form, $dbh, $query, '%AR%');
 
1251   $sth->execute || $form->dberror($query);#
 
1252   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
1253     foreach my $item (split(/:/, $ref->{link})) {
 
1254       if ($item eq "AR_paid") {
 
1255         %{ $form->{IC}{AR_paid}{ $ref->{accno} } } = (
 
1257                                              description => $ref->{description}
 
1266   $main::lxdebug->leave_sub();
 
1270   $main::lxdebug->enter_sub();
 
1272   my ($self, $myconfig, $form) = @_;
 
1274   my $dbh = $form->dbconnect($myconfig);
 
1276   my $query = qq|SELECT closedto, revtrans FROM defaults|;
 
1277   my $sth   = $dbh->prepare($query);
 
1278   $sth->execute || $form->dberror($query);
 
1280   ($form->{closedto}, $form->{revtrans}) = $sth->fetchrow_array;
 
1286   $main::lxdebug->leave_sub();
 
1290   $main::lxdebug->enter_sub();
 
1292   my ($self, $myconfig, $form) = @_;
 
1294   my $dbh = $form->dbconnect($myconfig);
 
1296   my ($query, @values);
 
1298   if ($form->{revtrans}) {
 
1299     $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '1'|;
 
1301   } elsif ($form->{closedto}) {
 
1302     $query = qq|UPDATE defaults SET closedto = ?, revtrans = '0'|;
 
1303     @values = (conv_date($form->{closedto}));
 
1306     $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '0'|;
 
1309   # set close in defaults
 
1310   do_query($form, $dbh, $query, @values);
 
1314   $main::lxdebug->leave_sub();
 
1318   my ($self, $units, $unit_name, $factor) = @_;
 
1320   $factor = 1 unless ($factor);
 
1322   my $unit = $units->{$unit_name};
 
1324   if (!defined($unit) || !$unit->{"base_unit"} ||
 
1325       ($unit_name eq $unit->{"base_unit"})) {
 
1326     return ($unit_name, $factor);
 
1329   return AM->get_base_unit($units, $unit->{"base_unit"}, $factor * $unit->{"factor"});
 
1332 sub retrieve_units {
 
1333   $main::lxdebug->enter_sub();
 
1335   my ($self, $myconfig, $form, $prefix) = @_;
 
1337   my $dbh = $form->get_standard_dbh;
 
1339   my $query = "SELECT *, base_unit AS original_base_unit FROM units";
 
1341   my $sth = prepare_execute_query($form, $dbh, $query);
 
1344   while (my $ref = $sth->fetchrow_hashref()) {
 
1345     $units->{$ref->{"name"}} = $ref;
 
1349   my $query_lang = "SELECT id, template_code FROM language ORDER BY description";
 
1350   $sth = $dbh->prepare($query_lang);
 
1351   $sth->execute() || $form->dberror($query_lang);
 
1353   while (my $ref = $sth->fetchrow_hashref()) {
 
1354     push(@languages, $ref);
 
1358   $query_lang = "SELECT ul.localized, ul.localized_plural, l.id, l.template_code " .
 
1359     "FROM units_language ul " .
 
1360     "LEFT JOIN language l ON ul.language_id = l.id " .
 
1361     "WHERE ul.unit = ?";
 
1362   $sth = $dbh->prepare($query_lang);
 
1364   foreach my $unit (values(%{$units})) {
 
1365     ($unit->{"${prefix}base_unit"}, $unit->{"${prefix}factor"}) = AM->get_base_unit($units, $unit->{"name"});
 
1367     $unit->{"LANGUAGES"} = {};
 
1368     foreach my $lang (@languages) {
 
1369       $unit->{"LANGUAGES"}->{$lang->{"template_code"}} = { "template_code" => $lang->{"template_code"} };
 
1372     $sth->execute($unit->{"name"}) || $form->dberror($query_lang . " (" . $unit->{"name"} . ")");
 
1373     while (my $ref = $sth->fetchrow_hashref()) {
 
1374       map({ $unit->{"LANGUAGES"}->{$ref->{"template_code"}}->{$_} = $ref->{$_} } keys(%{$ref}));
 
1379   $main::lxdebug->leave_sub();
 
1384 sub retrieve_all_units {
 
1385   $main::lxdebug->enter_sub();
 
1389   if (!$::request->{cache}{all_units}) {
 
1390     $::request->{cache}{all_units} = $self->retrieve_units(\%main::myconfig, $main::form);
 
1393   $main::lxdebug->leave_sub();
 
1395   return $::request->{cache}{all_units};
 
1399 sub translate_units {
 
1400   $main::lxdebug->enter_sub();
 
1402   my ($self, $form, $template_code, $unit, $amount) = @_;
 
1404   my $units = $self->retrieve_units(\%main::myconfig, $form);
 
1406   my $h = $units->{$unit}->{"LANGUAGES"}->{$template_code};
 
1407   my $new_unit = $unit;
 
1409     if (($amount != 1) && $h->{"localized_plural"}) {
 
1410       $new_unit = $h->{"localized_plural"};
 
1411     } elsif ($h->{"localized"}) {
 
1412       $new_unit = $h->{"localized"};
 
1416   $main::lxdebug->leave_sub();
 
1422   $main::lxdebug->enter_sub();
 
1424   my ($self, $myconfig, $form, $units) = @_;
 
1426   my $dbh = $form->dbconnect($myconfig);
 
1428   map({ $_->{"in_use"} = 0; } values(%{$units}));
 
1430   foreach my $unit (values(%{$units})) {
 
1431     my $base_unit = $unit->{"original_base_unit"};
 
1432     while ($base_unit) {
 
1433       $units->{$base_unit}->{"in_use"} = 1;
 
1434       $units->{$base_unit}->{"DEPENDING_UNITS"} = [] unless ($units->{$base_unit}->{"DEPENDING_UNITS"});
 
1435       push(@{$units->{$base_unit}->{"DEPENDING_UNITS"}}, $unit->{"name"});
 
1436       $base_unit = $units->{$base_unit}->{"original_base_unit"};
 
1440   foreach my $unit (values(%{$units})) {
 
1441     map({ $_ = $dbh->quote($_); } @{$unit->{"DEPENDING_UNITS"}});
 
1443     foreach my $table (qw(parts invoice orderitems)) {
 
1444       my $query = "SELECT COUNT(*) FROM $table WHERE unit ";
 
1446       if (0 == scalar(@{$unit->{"DEPENDING_UNITS"}})) {
 
1447         $query .= "= " . $dbh->quote($unit->{"name"});
 
1449         $query .= "IN (" . $dbh->quote($unit->{"name"}) . "," .
 
1450           join(",", map({ $dbh->quote($_) } @{$unit->{"DEPENDING_UNITS"}})) . ")";
 
1453       my ($count) = $dbh->selectrow_array($query);
 
1454       $form->dberror($query) if ($dbh->err);
 
1457         $unit->{"in_use"} = 1;
 
1465   $main::lxdebug->leave_sub();
 
1468 sub convertible_units {
 
1469   $main::lxdebug->enter_sub();
 
1473   my $filter_unit = shift;
 
1474   my $not_smaller = shift;
 
1476   my $conv_units = [];
 
1478   $filter_unit = $units->{$filter_unit};
 
1480   foreach my $name (sort { lc $a cmp lc $b } keys %{ $units }) {
 
1481     my $unit = $units->{$name};
 
1483     if (($unit->{base_unit} eq $filter_unit->{base_unit}) &&
 
1484         (!$not_smaller || ($unit->{factor} >= $filter_unit->{factor}))) {
 
1485       push @{$conv_units}, $unit;
 
1489   my @sorted = sort { $b->{factor} <=> $a->{factor} } @{ $conv_units };
 
1491   $main::lxdebug->leave_sub();
 
1496 # if $a is translatable to $b, return the factor between them.
 
1499   $main::lxdebug->enter_sub(2);
 
1500   my ($this, $a, $b, $all_units) = @_;
 
1502   $main::lxdebug->leave_sub(2) and return 0 unless $a && $b;
 
1503   $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a} && $all_units->{$b};
 
1504   $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a}{base_unit} eq $all_units->{$b}{base_unit};
 
1505   $main::lxdebug->leave_sub(2) and return $all_units->{$a}{factor} / $all_units->{$b}{factor};
 
1508 sub unit_select_data {
 
1509   $main::lxdebug->enter_sub();
 
1511   my ($self, $units, $selected, $empty_entry, $convertible_into) = @_;
 
1516     push(@{$select}, { "name" => "", "base_unit" => "", "factor" => "", "selected" => "" });
 
1519   foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
 
1520     if (!$convertible_into ||
 
1521         ($units->{$convertible_into} &&
 
1522          ($units->{$convertible_into}->{base_unit} eq $units->{$unit}->{base_unit}))) {
 
1523       push @{$select}, { "name"      => $unit,
 
1524                          "base_unit" => $units->{$unit}->{"base_unit"},
 
1525                          "factor"    => $units->{$unit}->{"factor"},
 
1526                          "selected"  => ($unit eq $selected) ? "selected" : "" };
 
1530   $main::lxdebug->leave_sub();
 
1535 sub unit_select_html {
 
1536   $main::lxdebug->enter_sub();
 
1538   my ($self, $units, $name, $selected, $convertible_into) = @_;
 
1540   my $select = "<select name=${name}>";
 
1542   foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
 
1543     if (!$convertible_into ||
 
1544         ($units->{$convertible_into} &&
 
1545          ($units->{$convertible_into}->{"base_unit"} eq $units->{$unit}->{"base_unit"}))) {
 
1546       $select .= "<option" . (($unit eq $selected) ? " selected" : "") . ">${unit}</option>";
 
1549   $select .= "</select>";
 
1551   $main::lxdebug->leave_sub();
 
1557   $main::lxdebug->enter_sub();
 
1561   my $units = $self->retrieve_all_units();
 
1566   while (2 <= scalar(@_)) {
 
1567     my $qty  = shift(@_);
 
1568     my $unit = $units->{shift(@_)};
 
1570     croak "No unit defined with name $unit" if (!defined $unit);
 
1573       $base_unit = $unit->{base_unit};
 
1574     } elsif ($base_unit ne $unit->{base_unit}) {
 
1575       croak "Adding values with incompatible base units $base_unit/$unit->{base_unit}";
 
1578     $sum += $qty * $unit->{factor};
 
1581   $main::lxdebug->leave_sub();
 
1583   return wantarray ? ($sum, $base_unit) : $sum;
 
1587   $main::lxdebug->enter_sub();
 
1589   my ($self, $myconfig, $form, $name, $base_unit, $factor, $languages) = @_;
 
1591   my $dbh = $form->dbconnect_noauto($myconfig);
 
1593   my $query = qq|SELECT COALESCE(MAX(sortkey), 0) + 1 FROM units|;
 
1594   my ($sortkey) = selectrow_query($form, $dbh, $query);
 
1596   $query = "INSERT INTO units (name, base_unit, factor, sortkey) " .
 
1597     "VALUES (?, ?, ?, ?)";
 
1598   do_query($form, $dbh, $query, $name, $base_unit, $factor, $sortkey);
 
1601     $query = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
 
1602     my $sth = $dbh->prepare($query);
 
1603     foreach my $lang (@{$languages}) {
 
1604       my @values = ($name, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
 
1605       $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
 
1613   $main::lxdebug->leave_sub();
 
1617   $main::lxdebug->enter_sub();
 
1619   my ($self, $myconfig, $form, $units, $delete_units) = @_;
 
1621   my $dbh = $form->dbconnect_noauto($myconfig);
 
1623   my ($base_unit, $unit, $sth, $query);
 
1625   $query = "DELETE FROM units_language";
 
1626   $dbh->do($query) || $form->dberror($query);
 
1628   if ($delete_units && (0 != scalar(@{$delete_units}))) {
 
1629     $query = "DELETE FROM units WHERE name IN (";
 
1630     map({ $query .= "?," } @{$delete_units});
 
1631     substr($query, -1, 1) = ")";
 
1632     $dbh->do($query, undef, @{$delete_units}) ||
 
1633       $form->dberror($query . " (" . join(", ", @{$delete_units}) . ")");
 
1636   $query = "UPDATE units SET name = ?, base_unit = ?, factor = ? WHERE name = ?";
 
1637   $sth = $dbh->prepare($query);
 
1639   my $query_lang = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
 
1640   my $sth_lang = $dbh->prepare($query_lang);
 
1642   foreach $unit (values(%{$units})) {
 
1643     $unit->{"depth"} = 0;
 
1644     my $base_unit = $unit;
 
1645     while ($base_unit->{"base_unit"}) {
 
1647       $base_unit = $units->{$base_unit->{"base_unit"}};
 
1651   foreach $unit (sort({ $a->{"depth"} <=> $b->{"depth"} } values(%{$units}))) {
 
1652     if ($unit->{"LANGUAGES"}) {
 
1653       foreach my $lang (@{$unit->{"LANGUAGES"}}) {
 
1654         next unless ($lang->{"id"} && $lang->{"localized"});
 
1655         my @values = ($unit->{"name"}, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
 
1656         $sth_lang->execute(@values) || $form->dberror($query_lang . " (" . join(", ", @values) . ")");
 
1660     next if ($unit->{"unchanged_unit"});
 
1662     my @values = ($unit->{"name"}, $unit->{"base_unit"}, $unit->{"factor"}, $unit->{"old_name"});
 
1663     $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
 
1667   $sth_lang->finish();
 
1671   $main::lxdebug->leave_sub();
 
1675   $main::lxdebug->enter_sub();
 
1677   my ($self, $myconfig, $form) = @_;
 
1679   # connect to database
 
1680   my $dbh = $form->dbconnect($myconfig);
 
1682   my $query = qq|SELECT
 
1686                    round(t.rate * 100, 2) AS rate,
 
1687                    (SELECT accno FROM chart WHERE id = chart_id) AS taxnumber,
 
1688                    (SELECT description FROM chart WHERE id = chart_id) AS account_description
 
1692   my $sth = $dbh->prepare($query);
 
1693   $sth->execute || $form->dberror($query);
 
1696   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
1697     push @{ $form->{TAX} }, $ref;
 
1703   $main::lxdebug->leave_sub();
 
1706 sub get_tax_accounts {
 
1707   $main::lxdebug->enter_sub();
 
1709   my ($self, $myconfig, $form) = @_;
 
1711   my $dbh = $form->dbconnect($myconfig);
 
1713   # get Accounts from chart
 
1714   my $query = qq{ SELECT
 
1716                  accno || ' - ' || description AS taxaccount
 
1718                WHERE link LIKE '%_tax%'
 
1722   my $sth = $dbh->prepare($query);
 
1723   $sth->execute || $form->dberror($query);
 
1725   $form->{ACCOUNTS} = [];
 
1726   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
1727     push @{ $form->{ACCOUNTS} }, $ref;
 
1734   $main::lxdebug->leave_sub();
 
1738   $main::lxdebug->enter_sub();
 
1740   my ($self, $myconfig, $form) = @_;
 
1742   # connect to database
 
1743   my $dbh = $form->dbconnect($myconfig);
 
1745   my $query = qq|SELECT
 
1748                    round(rate * 100, 2) AS rate,
 
1753   my $sth = $dbh->prepare($query);
 
1754   $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
1756   my $ref = $sth->fetchrow_hashref("NAME_lc");
 
1758   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
1762   # see if it is used by a taxkey
 
1763   $query = qq|SELECT count(*) FROM taxkeys
 
1764               WHERE tax_id = ? AND chart_id >0|;
 
1766   ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
 
1768   $form->{orphaned} = !$form->{orphaned};
 
1771   if (!$form->{orphaned} ) {
 
1772     $query = qq|SELECT DISTINCT c.id, c.accno
 
1774                 JOIN   tax t ON (t.id = tk.tax_id)
 
1775                 JOIN chart c ON (c.id = tk.chart_id)
 
1776                 WHERE tk.tax_id = ?|;
 
1778     $sth = $dbh->prepare($query);
 
1779     $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
1781     $form->{TAXINUSE} = [];
 
1782     while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
1783       push @{ $form->{TAXINUSE} }, $ref;
 
1791   $main::lxdebug->leave_sub();
 
1795   $main::lxdebug->enter_sub();
 
1797   my ($self, $myconfig, $form) = @_;
 
1800   # connect to database
 
1801   my $dbh = $form->get_standard_dbh($myconfig);
 
1803   $form->{rate} = $form->{rate} / 100;
 
1805   my @values = ($form->{taxkey}, $form->{taxdescription}, $form->{rate}, $form->{chart_id}, $form->{chart_id} );
 
1806   if ($form->{id} ne "") {
 
1807     $query = qq|UPDATE tax SET
 
1812                   taxnumber      = (SELECT accno FROM chart WHERE id= ? )
 
1814     push(@values, $form->{id});
 
1818     $query = qq|INSERT INTO tax (
 
1825                 VALUES (?, ?, ?, ?, (SELECT accno FROM chart WHERE id = ?) )|;
 
1827   do_query($form, $dbh, $query, @values);
 
1831   $main::lxdebug->leave_sub();
 
1835   $main::lxdebug->enter_sub();
 
1837   my ($self, $myconfig, $form) = @_;
 
1840   # connect to database
 
1841   my $dbh = $form->get_standard_dbh($myconfig);
 
1843   $query = qq|DELETE FROM tax
 
1845   do_query($form, $dbh, $query, $form->{id});
 
1849   $main::lxdebug->leave_sub();
 
1852 sub save_price_factor {
 
1853   $main::lxdebug->enter_sub();
 
1855   my ($self, $myconfig, $form) = @_;
 
1857   # connect to database
 
1858   my $dbh = $form->get_standard_dbh($myconfig);
 
1861   my @values = ($form->{description}, conv_i($form->{factor}));
 
1864     $query = qq|UPDATE price_factors SET description = ?, factor = ? WHERE id = ?|;
 
1865     push @values, conv_i($form->{id});
 
1868     $query = qq|INSERT INTO price_factors (description, factor, sortkey) VALUES (?, ?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM price_factors))|;
 
1871   do_query($form, $dbh, $query, @values);
 
1875   $main::lxdebug->leave_sub();
 
1878 sub get_all_price_factors {
 
1879   $main::lxdebug->enter_sub();
 
1881   my ($self, $myconfig, $form) = @_;
 
1883   # connect to database
 
1884   my $dbh = $form->get_standard_dbh($myconfig);
 
1886   $form->{PRICE_FACTORS} = selectall_hashref_query($form, $dbh, qq|SELECT * FROM price_factors ORDER BY sortkey|);
 
1888   $main::lxdebug->leave_sub();
 
1891 sub get_price_factor {
 
1892   $main::lxdebug->enter_sub();
 
1894   my ($self, $myconfig, $form) = @_;
 
1896   # connect to database
 
1897   my $dbh = $form->get_standard_dbh($myconfig);
 
1899   my $query = qq|SELECT description, factor,
 
1900                    ((SELECT COUNT(*) FROM parts      WHERE price_factor_id = ?) +
 
1901                     (SELECT COUNT(*) FROM invoice    WHERE price_factor_id = ?) +
 
1902                     (SELECT COUNT(*) FROM orderitems WHERE price_factor_id = ?)) = 0 AS orphaned
 
1903                  FROM price_factors WHERE id = ?|;
 
1905   ($form->{description}, $form->{factor}, $form->{orphaned}) = selectrow_query($form, $dbh, $query, (conv_i($form->{id})) x 4);
 
1907   $main::lxdebug->leave_sub();
 
1910 sub delete_price_factor {
 
1911   $main::lxdebug->enter_sub();
 
1913   my ($self, $myconfig, $form) = @_;
 
1915   # connect to database
 
1916   my $dbh = $form->get_standard_dbh($myconfig);
 
1918   do_query($form, $dbh, qq|DELETE FROM price_factors WHERE id = ?|, conv_i($form->{id}));
 
1921   $main::lxdebug->leave_sub();
 
1924 sub save_warehouse {
 
1925   $main::lxdebug->enter_sub();
 
1927   my ($self, $myconfig, $form) = @_;
 
1929   # connect to database
 
1930   my $dbh = $form->get_standard_dbh($myconfig);
 
1932   my ($query, @values, $sth);
 
1935     $query        = qq|SELECT nextval('id')|;
 
1936     ($form->{id}) = selectrow_query($form, $dbh, $query);
 
1938     $query        = qq|INSERT INTO warehouse (id, sortkey) VALUES (?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM warehouse))|;
 
1939     do_query($form, $dbh, $query, $form->{id});
 
1942   do_query($form, $dbh, qq|UPDATE warehouse SET description = ?, invalid = ? WHERE id = ?|,
 
1943            $form->{description}, $form->{invalid} ? 't' : 'f', conv_i($form->{id}));
 
1945   if (0 < $form->{number_of_new_bins}) {
 
1946     $query = qq|INSERT INTO bin (warehouse_id, description) VALUES (?, ?)|;
 
1947     $sth   = prepare_query($form, $dbh, $query);
 
1949     foreach my $i (1..$form->{number_of_new_bins}) {
 
1950       do_statement($form, $sth, $query, conv_i($form->{id}), "$form->{prefix}${i}");
 
1958   $main::lxdebug->leave_sub();
 
1962   $main::lxdebug->enter_sub();
 
1964   my ($self, $myconfig, $form) = @_;
 
1966   # connect to database
 
1967   my $dbh = $form->get_standard_dbh($myconfig);
 
1969   my ($query, @values, $commit_necessary, $sth);
 
1971   @values = map { $form->{"id_${_}"} } grep { $form->{"delete_${_}"} } (1..$form->{rowcount});
 
1974     $query = qq|DELETE FROM bin WHERE id IN (| . join(', ', ('?') x scalar(@values)) . qq|)|;
 
1975     do_query($form, $dbh, $query, @values);
 
1977     $commit_necessary = 1;
 
1980   $query = qq|UPDATE bin SET description = ? WHERE id = ?|;
 
1981   $sth   = prepare_query($form, $dbh, $query);
 
1983   foreach my $row (1..$form->{rowcount}) {
 
1984     next if ($form->{"delete_${row}"});
 
1986     do_statement($form, $sth, $query, $form->{"description_${row}"}, conv_i($form->{"id_${row}"}));
 
1988     $commit_necessary = 1;
 
1993   $dbh->commit() if ($commit_necessary);
 
1995   $main::lxdebug->leave_sub();
 
1998 sub delete_warehouse {
 
1999   $main::lxdebug->enter_sub();
 
2001   my ($self, $myconfig, $form) = @_;
 
2003   # connect to database
 
2004   my $dbh = $form->get_standard_dbh($myconfig);
 
2006   my $id      = conv_i($form->{id});
 
2007   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|;
 
2008   my ($count) = selectrow_query($form, $dbh, $query, $id);
 
2011     $main::lxdebug->leave_sub();
 
2015   do_query($form, $dbh, qq|DELETE FROM bin       WHERE warehouse_id = ?|, conv_i($form->{id}));
 
2016   do_query($form, $dbh, qq|DELETE FROM warehouse WHERE id           = ?|, conv_i($form->{id}));
 
2020   $main::lxdebug->leave_sub();
 
2025 sub get_all_warehouses {
 
2026   $main::lxdebug->enter_sub();
 
2028   my ($self, $myconfig, $form) = @_;
 
2030   # connect to database
 
2031   my $dbh = $form->get_standard_dbh($myconfig);
 
2033   my $query = qq|SELECT w.id, w.description, w.invalid,
 
2034                    (SELECT COUNT(b.description) FROM bin b WHERE b.warehouse_id = w.id) AS number_of_bins
 
2036                  ORDER BY w.sortkey|;
 
2038   $form->{WAREHOUSES} = selectall_hashref_query($form, $dbh, $query);
 
2040   $main::lxdebug->leave_sub();
 
2044   $main::lxdebug->enter_sub();
 
2046   my ($self, $myconfig, $form) = @_;
 
2048   # connect to database
 
2049   my $dbh = $form->get_standard_dbh($myconfig);
 
2051   my $id    = conv_i($form->{id});
 
2052   my $query = qq|SELECT w.description, w.invalid
 
2056   my $ref   = selectfirst_hashref_query($form, $dbh, $query, $id);
 
2058   map { $form->{$_} = $ref->{$_} } keys %{ $ref };
 
2060   $query = qq|SELECT b.*, EXISTS
 
2061                 (SELECT i.warehouse_id
 
2063                  WHERE i.bin_id = b.id
 
2067               WHERE b.warehouse_id = ?|;
 
2069   $form->{BINS} = selectall_hashref_query($form, $dbh, $query, conv_i($form->{id}));
 
2071   $main::lxdebug->leave_sub();