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 #======================================================================
 
  47   $main::lxdebug->enter_sub();
 
  49   my ($self, $myconfig, $form) = @_;
 
  52   my $dbh = $form->dbconnect($myconfig);
 
  54     SELECT c.accno, c.description, c.charttype, c.category,
 
  55       c.link, c.pos_bilanz, c.pos_eur, c.new_chart_id, c.valid_from,
 
  56       c.pos_bwa, datevautomatik,
 
  57       tk.taxkey_id, tk.pos_ustva, tk.tax_id,
 
  58       tk.tax_id || '--' || tk.taxkey_id AS tax, tk.startdate
 
  61     ON (c.id=tk.chart_id AND tk.id =
 
  62       (SELECT id FROM taxkeys
 
  63        WHERE taxkeys.chart_id = c.id AND startdate <= current_date
 
  64        ORDER BY startdate DESC LIMIT 1))
 
  69   $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
 
  70   my $sth = $dbh->prepare($query);
 
  71   $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
  73   my $ref = $sth->fetchrow_hashref("NAME_lc");
 
  75   foreach my $key (keys %$ref) {
 
  76     $form->{"$key"} = $ref->{"$key"};
 
  81   # get default accounts
 
  82   $query = qq|SELECT inventory_accno_id, income_accno_id, expense_accno_id
 
  84   $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
 
  85   $sth = $dbh->prepare($query);
 
  86   $sth->execute || $form->dberror($query);
 
  88   $ref = $sth->fetchrow_hashref("NAME_lc");
 
  90   map { $form->{$_} = $ref->{$_} } keys %{ $ref };
 
  96   # get taxkeys and description
 
 100       (SELECT accno FROM chart WHERE id=tax.chart_id) AS chart_accno,
 
 102       id||'--'||taxkey AS tax,
 
 105     FROM tax ORDER BY taxkey
 
 107   $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
 
 108   $sth = $dbh->prepare($query);
 
 109   $sth->execute || $form->dberror($query);
 
 111   $form->{TAXKEY} = [];
 
 113   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 114     push @{ $form->{TAXKEY} }, $ref;
 
 120     $query = qq|SELECT id, accno,description
 
 124     $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
 
 125     $sth = $dbh->prepare($query);
 
 126     $sth->execute($form->{link}) || $form->dberror($query . " ($form->{link})");
 
 128     $form->{NEWACCOUNT} = [];
 
 129     while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 130       push @{ $form->{NEWACCOUNT} }, $ref;
 
 135     # get the taxkeys of account
 
 149       LEFT JOIN   tax t ON (t.id = tk.tax_id)
 
 150       LEFT JOIN chart c ON (c.id = t.chart_id)
 
 152       WHERE tk.chart_id = ?
 
 153       ORDER BY startdate DESC
 
 155     $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
 
 156     $sth = $dbh->prepare($query);
 
 158     $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
 160     $form->{ACCOUNT_TAXKEYS} = [];
 
 162     while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 163       push @{ $form->{ACCOUNT_TAXKEYS} }, $ref;
 
 169   # check if we have any transactions
 
 170   $query = qq|SELECT a.trans_id FROM acc_trans a
 
 171               WHERE a.chart_id = ?|;
 
 172   $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
 
 173   $sth = $dbh->prepare($query);
 
 174   $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
 176   ($form->{orphaned}) = $sth->fetchrow_array;
 
 177   $form->{orphaned} = !$form->{orphaned};
 
 180   # check if new account is active
 
 181   $form->{new_chart_valid} = 0;
 
 182   if ($form->{new_chart_id}) {
 
 183     $query = qq|SELECT current_date-valid_from FROM chart
 
 185     $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
 
 186     my ($count) = selectrow_query($form, $dbh, $query, $form->{id});
 
 188       $form->{new_chart_valid} = 1;
 
 195   $main::lxdebug->leave_sub();
 
 199   $main::lxdebug->enter_sub();
 
 201   my ($self, $myconfig, $form) = @_;
 
 203   # connect to database, turn off AutoCommit
 
 204   my $dbh = $form->dbconnect_noauto($myconfig);
 
 206   # sanity check, can't have AR with AR_...
 
 207   if ($form->{AR} || $form->{AP} || $form->{IC}) {
 
 208     map { delete $form->{$_} }
 
 209       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);
 
 213   foreach my $item ($form->{AR},            $form->{AR_amount},
 
 214                     $form->{AR_tax},        $form->{AR_paid},
 
 215                     $form->{AP},            $form->{AP_amount},
 
 216                     $form->{AP_tax},        $form->{AP_paid},
 
 217                     $form->{IC},            $form->{IC_sale},
 
 218                     $form->{IC_cogs},       $form->{IC_taxpart},
 
 219                     $form->{IC_income},     $form->{IC_expense},
 
 220                     $form->{IC_taxservice}, $form->{CT_tax}
 
 222     $form->{link} .= "${item}:" if ($item);
 
 226   # strip blanks from accno
 
 227   map { $form->{$_} =~ s/ //g; } qw(accno);
 
 231   if ($form->{id} eq "NULL") {
 
 235   if (!$form->{id} || $form->{id} eq "") {
 
 236     $query = qq|SELECT nextval('id')|;
 
 237     ($form->{"id"}) = selectrow_query($form, $dbh, $query);
 
 238     $query = qq|INSERT INTO chart (id, accno) VALUES (?, ?)|;
 
 239     do_query($form, $dbh, $query, $form->{"id"}, $form->{"accno"});
 
 245     $query = qq|UPDATE chart SET
 
 261                   $form->{description},
 
 265                   conv_i($form->{pos_bwa}),
 
 266                   conv_i($form->{pos_bilanz}),
 
 267                   conv_i($form->{pos_eur}),
 
 268                   conv_i($form->{new_chart_id}),
 
 269                   conv_date($form->{valid_from}),
 
 270                   ($form->{datevautomatik} eq 'T') ? 'true':'false',
 
 276   do_query($form, $dbh, $query, @values);
 
 282   my $MAX_TRIES = 10; # Maximum count of taxkeys in form
 
 286   for $tk_count (0 .. $MAX_TRIES) {
 
 290     # Check if the account already exists, else cancel
 
 292     print(STDERR "Keine Taxkeys weil ID =: $form->{id}\n");
 
 294     last READTAXKEYS if ( $form->{'id'} == 0);
 
 296     # check if there is a startdate
 
 297     if ( $form->{"taxkey_startdate_$tk_count"} eq '' ) {
 
 302     # Add valid taxkeys into the array
 
 305         id        => ($form->{"taxkey_id_$tk_count"} eq 'NEW') ? conv_i('') : conv_i($form->{"taxkey_id_$tk_count"}),
 
 306         tax_id    => conv_i($form->{"taxkey_tax_$tk_count"}),
 
 307         startdate => conv_date($form->{"taxkey_startdate_$tk_count"}),
 
 308         chart_id  => conv_i($form->{"id"}),
 
 309         pos_ustva => conv_i($form->{"taxkey_pos_ustva_$tk_count"}),
 
 310         delete    => ( $form->{"taxkey_del_$tk_count"} eq 'delete' ) ? '1' : '',
 
 317   for my $j (0 .. $#taxkeys){
 
 318     if ( defined $taxkeys[$j]{'id'} ){
 
 321       if ($taxkeys[$j]{'delete'}){
 
 323           DELETE FROM taxkeys WHERE id = ?
 
 326         @values = ($taxkeys[$j]{'id'});
 
 328         do_query($form, $dbh, $query, @values);
 
 337         SET taxkey_id = (SELECT taxkey FROM tax WHERE tax.id = ?),
 
 345         $taxkeys[$j]{'tax_id'},
 
 346         $taxkeys[$j]{'chart_id'},
 
 347         $taxkeys[$j]{'tax_id'},
 
 348         $taxkeys[$j]{'pos_ustva'},
 
 349         $taxkeys[$j]{'startdate'},
 
 352       do_query($form, $dbh, $query, @values);
 
 358         INSERT INTO taxkeys (
 
 365         VALUES ((SELECT taxkey FROM tax WHERE tax.id = ?), ?, ?, ?, ?)
 
 368         $taxkeys[$j]{'tax_id'},
 
 369         $taxkeys[$j]{'chart_id'},
 
 370         $taxkeys[$j]{'tax_id'},
 
 371         $taxkeys[$j]{'pos_ustva'},
 
 372         $taxkeys[$j]{'startdate'},
 
 375       do_query($form, $dbh, $query, @values);
 
 381   my $rc = $dbh->commit;
 
 384   $main::lxdebug->leave_sub();
 
 390   $main::lxdebug->enter_sub();
 
 392   my ($self, $myconfig, $form) = @_;
 
 394   # connect to database, turn off AutoCommit
 
 395   my $dbh = $form->dbconnect_noauto($myconfig);
 
 397   my $query = qq|SELECT count(*) FROM acc_trans a
 
 398                  WHERE a.chart_id = ?|;
 
 399   my ($count) = selectrow_query($form, $dbh, $query, $form->{id});
 
 403     $main::lxdebug->leave_sub();
 
 407   # set inventory_accno_id, income_accno_id, expense_accno_id to defaults
 
 408   foreach my $type (qw(inventory income expense)) {
 
 411       qq|SET ${type}_accno_id = (SELECT ${type}_accno_id FROM defaults) | .
 
 412       qq|WHERE ${type}_accno_id = ?|;
 
 413     do_query($form, $dbh, $query, $form->{id});
 
 416   foreach my $table (qw(partstax customertax vendortax tax)) {
 
 417     $query = qq|DELETE FROM $table
 
 419     do_query($form, $dbh, $query, $form->{id});
 
 422   # delete chart of account record
 
 423   $query = qq|DELETE FROM chart
 
 425   do_query($form, $dbh, $query, $form->{id});
 
 427   # delete account taxkeys
 
 428   $query = qq|DELETE FROM taxkeys
 
 430   do_query($form, $dbh, $query, $form->{id});
 
 432   # commit and redirect
 
 433   my $rc = $dbh->commit;
 
 436   $main::lxdebug->leave_sub();
 
 442   $main::lxdebug->enter_sub();
 
 444   my ($self, $myconfig, $form) = @_;
 
 446   # connect to database
 
 447   my $dbh = $form->dbconnect($myconfig);
 
 449   my $query = qq|SELECT d.id, d.description, d.role
 
 453   my $sth = $dbh->prepare($query);
 
 454   $sth->execute || $form->dberror($query);
 
 457   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 458     push @{ $form->{ALL} }, $ref;
 
 464   $main::lxdebug->leave_sub();
 
 468   $main::lxdebug->enter_sub();
 
 470   my ($self, $myconfig, $form) = @_;
 
 472   # connect to database
 
 473   my $dbh = $form->dbconnect($myconfig);
 
 475   my $query = qq|SELECT d.description, d.role
 
 478   my $sth = $dbh->prepare($query);
 
 479   $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
 481   my $ref = $sth->fetchrow_hashref("NAME_lc");
 
 483   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
 487   # see if it is in use
 
 488   $query = qq|SELECT count(*) FROM dpt_trans d
 
 489               WHERE d.department_id = ?|;
 
 490   ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
 
 492   $form->{orphaned} = !$form->{orphaned};
 
 497   $main::lxdebug->leave_sub();
 
 500 sub save_department {
 
 501   $main::lxdebug->enter_sub();
 
 503   my ($self, $myconfig, $form) = @_;
 
 506   # connect to database
 
 507   my $dbh = $form->dbconnect($myconfig);
 
 509   my @values = ($form->{description}, $form->{role});
 
 511     $query = qq|UPDATE department SET
 
 512                 description = ?, role = ?
 
 514     push(@values, $form->{id});
 
 516     $query = qq|INSERT INTO department
 
 520   do_query($form, $dbh, $query, @values);
 
 524   $main::lxdebug->leave_sub();
 
 527 sub delete_department {
 
 528   $main::lxdebug->enter_sub();
 
 530   my ($self, $myconfig, $form) = @_;
 
 533   # connect to database
 
 534   my $dbh = $form->dbconnect($myconfig);
 
 536   $query = qq|DELETE FROM department
 
 538   do_query($form, $dbh, $query, $form->{id});
 
 542   $main::lxdebug->leave_sub();
 
 546   $main::lxdebug->enter_sub();
 
 548   my ($self, $myconfig, $form) = @_;
 
 550   # connect to database
 
 551   my $dbh = $form->dbconnect($myconfig);
 
 553   my $query = qq|SELECT id, lead
 
 557   my $sth = $dbh->prepare($query);
 
 558   $sth->execute || $form->dberror($query);
 
 560   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 561     push @{ $form->{ALL} }, $ref;
 
 567   $main::lxdebug->leave_sub();
 
 571   $main::lxdebug->enter_sub();
 
 573   my ($self, $myconfig, $form) = @_;
 
 575   # connect to database
 
 576   my $dbh = $form->dbconnect($myconfig);
 
 579     qq|SELECT l.id, l.lead | .
 
 582   my $sth = $dbh->prepare($query);
 
 583   $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
 585   my $ref = $sth->fetchrow_hashref("NAME_lc");
 
 587   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
 593   $main::lxdebug->leave_sub();
 
 597   $main::lxdebug->enter_sub();
 
 599   my ($self, $myconfig, $form) = @_;
 
 602   # connect to database
 
 603   my $dbh = $form->dbconnect($myconfig);
 
 605   my @values = ($form->{description});
 
 606   # id is the old record
 
 608     $query = qq|UPDATE leads SET
 
 611     puhs(@values, $form->{id});
 
 613     $query = qq|INSERT INTO leads
 
 617   do_query($form, $dbh, $query, @values);
 
 621   $main::lxdebug->leave_sub();
 
 625   $main::lxdebug->enter_sub();
 
 627   my ($self, $myconfig, $form) = @_;
 
 630   # connect to database
 
 631   my $dbh = $form->dbconnect($myconfig);
 
 633   $query = qq|DELETE FROM leads
 
 635   do_query($form, $dbh, $query, $form->{id});
 
 639   $main::lxdebug->leave_sub();
 
 643   $main::lxdebug->enter_sub();
 
 645   my ($self, $myconfig, $form) = @_;
 
 647   # connect to database
 
 648   my $dbh = $form->dbconnect($myconfig);
 
 650   my $query = qq|SELECT id, description, discount, customernumberinit, salesman
 
 654   my $sth = $dbh->prepare($query);
 
 655   $sth->execute || $form->dberror($query);
 
 657   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 658     push @{ $form->{ALL} }, $ref;
 
 664   $main::lxdebug->leave_sub();
 
 668   $main::lxdebug->enter_sub();
 
 670   my ($self, $myconfig, $form) = @_;
 
 672   # connect to database
 
 673   my $dbh = $form->dbconnect($myconfig);
 
 676     qq|SELECT b.description, b.discount, b.customernumberinit, b.salesman
 
 679   my $sth = $dbh->prepare($query);
 
 680   $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
 682   my $ref = $sth->fetchrow_hashref("NAME_lc");
 
 684   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
 690   $main::lxdebug->leave_sub();
 
 694   $main::lxdebug->enter_sub();
 
 696   my ($self, $myconfig, $form) = @_;
 
 699   # connect to database
 
 700   my $dbh = $form->dbconnect($myconfig);
 
 702   my @values = ($form->{description}, $form->{discount}, $form->{customernumberinit}, $form->{salesman} ? 't' : 'f');
 
 703   # id is the old record
 
 705     $query = qq|UPDATE business SET
 
 708                 customernumberinit = ?,
 
 711     push(@values, $form->{id});
 
 713     $query = qq|INSERT INTO business
 
 714                 (description, discount, customernumberinit, salesman)
 
 715                 VALUES (?, ?, ?, ?)|;
 
 717   do_query($form, $dbh, $query, @values);
 
 721   $main::lxdebug->leave_sub();
 
 724 sub delete_business {
 
 725   $main::lxdebug->enter_sub();
 
 727   my ($self, $myconfig, $form) = @_;
 
 729   # connect to database
 
 730   my $dbh = $form->dbconnect($myconfig);
 
 732   my $query = qq|DELETE FROM business
 
 734   do_query($form, $dbh, $query, $form->{id});
 
 738   $main::lxdebug->leave_sub();
 
 743   $main::lxdebug->enter_sub();
 
 745   my ($self, $myconfig, $form, $return_list) = @_;
 
 747   # connect to database
 
 748   my $dbh = $form->dbconnect($myconfig);
 
 751     "SELECT id, description, template_code, article_code, " .
 
 752     "  output_numberformat, output_dateformat, output_longdates " .
 
 753     "FROM language ORDER BY description";
 
 755   my $sth = $dbh->prepare($query);
 
 756   $sth->execute || $form->dberror($query);
 
 760   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 761     push(@{ $ary }, $ref);
 
 767   $main::lxdebug->leave_sub();
 
 777   $main::lxdebug->enter_sub();
 
 779   my ($self, $myconfig, $form) = @_;
 
 781   # connect to database
 
 782   my $dbh = $form->dbconnect($myconfig);
 
 785     "SELECT description, template_code, article_code, " .
 
 786     "  output_numberformat, output_dateformat, output_longdates " .
 
 787     "FROM language WHERE id = ?";
 
 788   my $sth = $dbh->prepare($query);
 
 789   $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
 
 791   my $ref = $sth->fetchrow_hashref("NAME_lc");
 
 793   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
 799   $main::lxdebug->leave_sub();
 
 802 sub get_language_details {
 
 803   $main::lxdebug->enter_sub();
 
 805   my ($self, $myconfig, $form, $id) = @_;
 
 807   # connect to database
 
 808   my $dbh = $form->dbconnect($myconfig);
 
 811     "SELECT template_code, " .
 
 812     "  output_numberformat, output_dateformat, output_longdates " .
 
 813     "FROM language WHERE id = ?";
 
 814   my @res = selectrow_query($form, $dbh, $query, $id);
 
 817   $main::lxdebug->leave_sub();
 
 823   $main::lxdebug->enter_sub();
 
 825   my ($self, $myconfig, $form) = @_;
 
 827   # connect to database
 
 828   my $dbh = $form->dbconnect($myconfig);
 
 829   my (@values, $query);
 
 831   map({ push(@values, $form->{$_}); }
 
 832       qw(description template_code article_code
 
 833          output_numberformat output_dateformat output_longdates));
 
 835   # id is the old record
 
 838       "UPDATE language SET " .
 
 839       "  description = ?, template_code = ?, article_code = ?, " .
 
 840       "  output_numberformat = ?, output_dateformat = ?, " .
 
 841       "  output_longdates = ? " .
 
 843     push(@values, $form->{id});
 
 846       "INSERT INTO language (" .
 
 847       "  description, template_code, article_code, " .
 
 848       "  output_numberformat, output_dateformat, output_longdates" .
 
 849       ") VALUES (?, ?, ?, ?, ?, ?)";
 
 851   do_query($form, $dbh, $query, @values);
 
 855   $main::lxdebug->leave_sub();
 
 858 sub delete_language {
 
 859   $main::lxdebug->enter_sub();
 
 861   my ($self, $myconfig, $form) = @_;
 
 864   # connect to database
 
 865   my $dbh = $form->dbconnect_noauto($myconfig);
 
 867   foreach my $table (qw(translation_payment_terms units_language)) {
 
 868     $query = qq|DELETE FROM $table WHERE language_id = ?|;
 
 869     do_query($form, $dbh, $query, $form->{"id"});
 
 872   $query = "DELETE FROM language WHERE id = ?";
 
 873   do_query($form, $dbh, $query, $form->{"id"});
 
 878   $main::lxdebug->leave_sub();
 
 883   $main::lxdebug->enter_sub();
 
 885   my ($self, $myconfig, $form) = @_;
 
 887   # connect to database
 
 888   my $dbh = $form->dbconnect($myconfig);
 
 890   my $query = qq|SELECT id, description,
 
 892                  (SELECT accno FROM chart WHERE id = inventory_accno_id) AS inventory_accno,
 
 894                  (SELECT accno FROM chart WHERE id = income_accno_id_0) AS income_accno_0,
 
 896                  (SELECT accno FROM chart WHERE id = expense_accno_id_0) AS expense_accno_0,
 
 898                  (SELECT accno FROM chart WHERE id = income_accno_id_1) AS income_accno_1,
 
 900                  (SELECT accno FROM chart WHERE id = expense_accno_id_1) AS expense_accno_1,
 
 902                  (SELECT accno FROM chart WHERE id = income_accno_id_2) AS income_accno_2,
 
 904                  (select accno FROM chart WHERE id = expense_accno_id_2) AS expense_accno_2,
 
 906                  (SELECT accno FROM chart WHERE id = income_accno_id_3) AS income_accno_3,
 
 908                  (SELECT accno FROM chart WHERE id = expense_accno_id_3) AS expense_accno_3
 
 912   my $sth = $dbh->prepare($query);
 
 913   $sth->execute || $form->dberror($query);
 
 916   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 917     push @{ $form->{ALL} }, $ref;
 
 923   $main::lxdebug->leave_sub();
 
 926 sub get_buchungsgruppe {
 
 927   $main::lxdebug->enter_sub();
 
 929   my ($self, $myconfig, $form) = @_;
 
 932   # connect to database
 
 933   my $dbh = $form->dbconnect($myconfig);
 
 937       qq|SELECT description, inventory_accno_id,
 
 938          (SELECT accno FROM chart WHERE id = inventory_accno_id) AS inventory_accno,
 
 940          (SELECT accno FROM chart WHERE id = income_accno_id_0) AS income_accno_0,
 
 942          (SELECT accno FROM chart WHERE id = expense_accno_id_0) AS expense_accno_0,
 
 944          (SELECT accno FROM chart WHERE id = income_accno_id_1) AS income_accno_1,
 
 946          (SELECT accno FROM chart WHERE id = expense_accno_id_1) AS expense_accno_1,
 
 948          (SELECT accno FROM chart WHERE id = income_accno_id_2) AS income_accno_2,
 
 950          (select accno FROM chart WHERE id = expense_accno_id_2) AS expense_accno_2,
 
 952          (SELECT accno FROM chart WHERE id = income_accno_id_3) AS income_accno_3,
 
 954          (SELECT accno FROM chart WHERE id = expense_accno_id_3) AS expense_accno_3
 
 957     my $sth = $dbh->prepare($query);
 
 958     $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
 960     my $ref = $sth->fetchrow_hashref("NAME_lc");
 
 962     map { $form->{$_} = $ref->{$_} } keys %$ref;
 
 967       qq|SELECT count(id) = 0 AS orphaned
 
 969          WHERE buchungsgruppen_id = ?|;
 
 970     ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
 
 973   $query = "SELECT inventory_accno_id, income_accno_id, expense_accno_id ".
 
 975   ($form->{"std_inventory_accno_id"}, $form->{"std_income_accno_id"},
 
 976    $form->{"std_expense_accno_id"}) = selectrow_query($form, $dbh, $query);
 
 979   $query = qq|SELECT c.accno, c.description, c.link, c.id,
 
 980               d.inventory_accno_id, d.income_accno_id, d.expense_accno_id
 
 981               FROM chart c, defaults d
 
 982               WHERE c.link LIKE '%$module%'
 
 986   my $sth = $dbh->prepare($query);
 
 987   $sth->execute || $form->dberror($query);
 
 988   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 989     foreach my $key (split(/:/, $ref->{link})) {
 
 990       if (!$form->{"std_inventory_accno_id"} && ($key eq "IC")) {
 
 991         $form->{"std_inventory_accno_id"} = $ref->{"id"};
 
 993       if ($key =~ /$module/) {
 
 994         if (   ($ref->{id} eq $ref->{inventory_accno_id})
 
 995             || ($ref->{id} eq $ref->{income_accno_id})
 
 996             || ($ref->{id} eq $ref->{expense_accno_id})) {
 
 997           push @{ $form->{"${module}_links"}{$key} },
 
 998             { accno       => $ref->{accno},
 
 999               description => $ref->{description},
 
1000               selected    => "selected",
 
1003           push @{ $form->{"${module}_links"}{$key} },
 
1004             { accno       => $ref->{accno},
 
1005               description => $ref->{description},
 
1017   $main::lxdebug->leave_sub();
 
1020 sub save_buchungsgruppe {
 
1021   $main::lxdebug->enter_sub();
 
1023   my ($self, $myconfig, $form) = @_;
 
1025   # connect to database
 
1026   my $dbh = $form->dbconnect($myconfig);
 
1028   my @values = ($form->{description}, $form->{inventory_accno_id},
 
1029                 $form->{income_accno_id_0}, $form->{expense_accno_id_0},
 
1030                 $form->{income_accno_id_1}, $form->{expense_accno_id_1},
 
1031                 $form->{income_accno_id_2}, $form->{expense_accno_id_2},
 
1032                 $form->{income_accno_id_3}, $form->{expense_accno_id_3});
 
1036   # id is the old record
 
1038     $query = qq|UPDATE buchungsgruppen SET
 
1039                 description = ?, inventory_accno_id = ?,
 
1040                 income_accno_id_0 = ?, expense_accno_id_0 = ?,
 
1041                 income_accno_id_1 = ?, expense_accno_id_1 = ?,
 
1042                 income_accno_id_2 = ?, expense_accno_id_2 = ?,
 
1043                 income_accno_id_3 = ?, expense_accno_id_3 = ?
 
1045     push(@values, $form->{id});
 
1047     $query = qq|SELECT COALESCE(MAX(sortkey) + 1, 1) FROM buchungsgruppen|;
 
1048     my ($sortkey) = $dbh->selectrow_array($query);
 
1049     $form->dberror($query) if ($dbh->err);
 
1050     push(@values, $sortkey);
 
1051     $query = qq|INSERT INTO buchungsgruppen
 
1052                 (description, inventory_accno_id,
 
1053                 income_accno_id_0, expense_accno_id_0,
 
1054                 income_accno_id_1, expense_accno_id_1,
 
1055                 income_accno_id_2, expense_accno_id_2,
 
1056                 income_accno_id_3, expense_accno_id_3,
 
1058                 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
 
1060   do_query($form, $dbh, $query, @values);
 
1064   $main::lxdebug->leave_sub();
 
1067 sub delete_buchungsgruppe {
 
1068   $main::lxdebug->enter_sub();
 
1070   my ($self, $myconfig, $form) = @_;
 
1072   # connect to database
 
1073   my $dbh = $form->dbconnect($myconfig);
 
1075   my $query = qq|DELETE FROM buchungsgruppen WHERE id = ?|;
 
1076   do_query($form, $dbh, $query, $form->{id});
 
1080   $main::lxdebug->leave_sub();
 
1084   $main::lxdebug->enter_sub();
 
1086   my ($self, $myconfig, $form, $table) = @_;
 
1088   # connect to database
 
1089   my $dbh = $form->get_standard_dbh($myconfig);
 
1093        (SELECT sortkey FROM $table WHERE id = ?) AS sortkey1,
 
1094        (SELECT sortkey FROM $table WHERE id = ?) AS sortkey2|;
 
1095   my @values   = ($form->{"id1"}, $form->{"id2"});
 
1096   my @sortkeys = selectrow_query($form, $dbh, $query, @values);
 
1098   $query  = qq|UPDATE $table SET sortkey = ? WHERE id = ?|;
 
1099   my $sth = prepare_query($form, $dbh, $query);
 
1101   do_statement($form, $sth, $query, $sortkeys[1], $form->{"id1"});
 
1102   do_statement($form, $sth, $query, $sortkeys[0], $form->{"id2"});
 
1108   $main::lxdebug->leave_sub();
 
1112   $main::lxdebug->enter_sub();
 
1114   my ($self, $myconfig, $form) = @_;
 
1116   # connect to database
 
1117   my $dbh = $form->dbconnect($myconfig);
 
1119   my $query = qq|SELECT * FROM payment_terms ORDER BY sortkey|;
 
1121   my $sth = $dbh->prepare($query);
 
1122   $sth->execute || $form->dberror($query);
 
1125   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
1126     push @{ $form->{ALL} }, $ref;
 
1132   $main::lxdebug->leave_sub();
 
1136   $main::lxdebug->enter_sub();
 
1138   my ($self, $myconfig, $form) = @_;
 
1140   # connect to database
 
1141   my $dbh = $form->dbconnect($myconfig);
 
1143   my $query = qq|SELECT * FROM payment_terms WHERE id = ?|;
 
1144   my $sth = $dbh->prepare($query);
 
1145   $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
 
1147   my $ref = $sth->fetchrow_hashref("NAME_lc");
 
1148   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
1152     qq|SELECT t.language_id, t.description_long, l.description AS language | .
 
1153     qq|FROM translation_payment_terms t | .
 
1154     qq|LEFT JOIN language l ON t.language_id = l.id | .
 
1155     qq|WHERE t.payment_terms_id = ? | .
 
1157     qq|SELECT l.id AS language_id, NULL AS description_long, | .
 
1158     qq|  l.description AS language | .
 
1159     qq|FROM language l|;
 
1160   $sth = $dbh->prepare($query);
 
1161   $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
 
1164   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
1165     $mapping{ $ref->{"language_id"} } = $ref
 
1166       unless (defined($mapping{ $ref->{"language_id"} }));
 
1170   $form->{"TRANSLATION"} = [sort({ $a->{"language"} cmp $b->{"language"} }
 
1175   $main::lxdebug->leave_sub();
 
1179   $main::lxdebug->enter_sub();
 
1181   my ($self, $myconfig, $form) = @_;
 
1183   # connect to database
 
1184   my $dbh = $form->dbconnect_noauto($myconfig);
 
1189     $query = qq|SELECT nextval('id'), COALESCE(MAX(sortkey) + 1, 1) | .
 
1190       qq|FROM payment_terms|;
 
1192     ($form->{id}, $sortkey) = selectrow_query($form, $dbh, $query);
 
1194     $query = qq|INSERT INTO payment_terms (id, sortkey) VALUES (?, ?)|;
 
1195     do_query($form, $dbh, $query, $form->{id}, $sortkey);
 
1199       qq|DELETE FROM translation_payment_terms | .
 
1200       qq|WHERE payment_terms_id = ?|;
 
1201     do_query($form, $dbh, $query, $form->{"id"});
 
1204   $query = qq|UPDATE payment_terms SET
 
1205               description = ?, description_long = ?,
 
1206               terms_netto = ?, terms_skonto = ?,
 
1209   my @values = ($form->{description}, $form->{description_long},
 
1210                 $form->{terms_netto} * 1, $form->{terms_skonto} * 1,
 
1211                 $form->{percent_skonto} * 1,
 
1213   do_query($form, $dbh, $query, @values);
 
1215   $query = qq|SELECT id FROM language|;
 
1217   my $sth = $dbh->prepare($query);
 
1218   $sth->execute() || $form->dberror($query);
 
1220   while (my ($id) = $sth->fetchrow_array()) {
 
1221     push(@language_ids, $id);
 
1226     qq|INSERT INTO translation_payment_terms | .
 
1227     qq|(language_id, payment_terms_id, description_long) | .
 
1228     qq|VALUES (?, ?, ?)|;
 
1229   $sth = $dbh->prepare($query);
 
1231   foreach my $language_id (@language_ids) {
 
1232     do_statement($form, $sth, $query, $language_id, $form->{"id"},
 
1233                  $form->{"description_long_${language_id}"});
 
1240   $main::lxdebug->leave_sub();
 
1243 sub delete_payment {
 
1244   $main::lxdebug->enter_sub();
 
1246   my ($self, $myconfig, $form) = @_;
 
1248   # connect to database
 
1249   my $dbh = $form->dbconnect_noauto($myconfig);
 
1252     qq|DELETE FROM translation_payment_terms WHERE payment_terms_id = ?|;
 
1253   do_query($form, $dbh, $query, $form->{"id"});
 
1255   $query = qq|DELETE FROM payment_terms WHERE id = ?|;
 
1256   do_query($form, $dbh, $query, $form->{"id"});
 
1261   $main::lxdebug->leave_sub();
 
1265 sub prepare_template_filename {
 
1266   $main::lxdebug->enter_sub();
 
1268   my ($self, $myconfig, $form) = @_;
 
1270   my ($filename, $display_filename);
 
1272   if ($form->{type} eq "stylesheet") {
 
1273     $filename = "css/$myconfig->{stylesheet}";
 
1274     $display_filename = $myconfig->{stylesheet};
 
1277     $filename = $form->{formname};
 
1279     if ($form->{language}) {
 
1280       my ($id, $template_code) = split(/--/, $form->{language});
 
1281       $filename .= "_${template_code}";
 
1284     if ($form->{printer}) {
 
1285       my ($id, $template_code) = split(/--/, $form->{printer});
 
1286       $filename .= "_${template_code}";
 
1289     $filename .= "." . ($form->{format} eq "html" ? "html" : "tex");
 
1290     $filename =~ s|.*/||;
 
1291     $display_filename = $filename;
 
1292     $filename = "$myconfig->{templates}/$filename";
 
1295   $main::lxdebug->leave_sub();
 
1297   return ($filename, $display_filename);
 
1302   $main::lxdebug->enter_sub();
 
1304   my ($self, $filename) = @_;
 
1306   my ($content, $lines) = ("", 0);
 
1310   if (open(TEMPLATE, $filename)) {
 
1311     while (<TEMPLATE>) {
 
1318   $main::lxdebug->leave_sub();
 
1320   return ($content, $lines);
 
1324   $main::lxdebug->enter_sub();
 
1326   my ($self, $filename, $content) = @_;
 
1332   if (open(TEMPLATE, ">$filename")) {
 
1333     $content =~ s/\r\n/\n/g;
 
1334     print(TEMPLATE $content);
 
1340   $main::lxdebug->leave_sub();
 
1346   $main::lxdebug->enter_sub();
 
1351   my $myconfig = \%main::myconfig;
 
1352   my $form     = $main::form;
 
1354   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
 
1357   map { ($accnos{$_}) = split(m/--/, $form->{$_}) } qw(inventory_accno income_accno expense_accno fxgain_accno fxloss_accno);
 
1359   $form->{curr}  =~ s/ //g;
 
1360   my @currencies =  grep { $_ ne '' } split m/:/, $form->{curr};
 
1361   my $currency   =  join ':', @currencies;
 
1363   # these defaults are database wide
 
1366     qq|UPDATE defaults SET
 
1367         inventory_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
 
1368         income_accno_id    = (SELECT c.id FROM chart c WHERE c.accno = ?),
 
1369         expense_accno_id   = (SELECT c.id FROM chart c WHERE c.accno = ?),
 
1370         fxgain_accno_id    = (SELECT c.id FROM chart c WHERE c.accno = ?),
 
1371         fxloss_accno_id    = (SELECT c.id FROM chart c WHERE c.accno = ?),
 
1388   my @values = ($accnos{inventory_accno}, $accnos{income_accno}, $accnos{expense_accno},
 
1389                 $accnos{fxgain_accno},    $accnos{fxloss_accno},
 
1390                 $form->{invnumber},       $form->{cnnumber},
 
1391                 $form->{sonumber},        $form->{ponumber},
 
1392                 $form->{sqnumber},        $form->{rfqnumber},
 
1393                 $form->{customernumber},  $form->{vendornumber},
 
1394                 $form->{articlenumber},   $form->{servicenumber},
 
1395                 $form->{sdonumber},       $form->{pdonumber},
 
1396                 $form->{yearend},         $currency,
 
1397                 $form->{businessnumber},  $form->{weightunit});
 
1398   do_query($form, $dbh, $query, @values);
 
1402   $main::lxdebug->leave_sub();
 
1406 sub save_preferences {
 
1407   $main::lxdebug->enter_sub();
 
1409   my ($self, $myconfig, $form, $webdav) = @_;
 
1411   my $dbh = $form->get_standard_dbh($myconfig);
 
1413   my ($currency, $businessnumber) = selectrow_query($form, $dbh, qq|SELECT curr, businessnumber FROM defaults|);
 
1416   my $query = qq|UPDATE employee SET name = ? WHERE login = ?|;
 
1417   do_query($form, $dbh, $query, $form->{name}, $form->{login});
 
1419   my $rc = $dbh->commit();
 
1421   # save first currency in myconfig
 
1422   $currency               =~ s/:.*//;
 
1423   $form->{currency}       =  $currency;
 
1425   $form->{businessnumber} =  $businessnumber;
 
1427   $myconfig = new User($form->{login});
 
1429   foreach my $item (keys %$form) {
 
1430     $myconfig->{$item} = $form->{$item};
 
1433   $myconfig->save_member($main::memberfile);
 
1435   my $auth = $main::auth;
 
1437   if ($auth->can_change_password()
 
1438       && defined $form->{new_password}
 
1439       && ($form->{new_password} ne '********')) {
 
1440     $auth->change_password($form->{login}, $form->{new_password});
 
1442     $form->{password} = $form->{new_password};
 
1443     $auth->set_session_value('password', $form->{password});
 
1444     $auth->create_or_refresh_session();
 
1447   $main::lxdebug->leave_sub();
 
1453   $main::lxdebug->enter_sub();
 
1458   my $myconfig = \%main::myconfig;
 
1459   my $form     = $main::form;
 
1461   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
 
1463   my $defaults = selectfirst_hashref_query($form, $dbh, qq|SELECT * FROM defaults|) || {};
 
1465   $defaults->{weightunit} ||= 'kg';
 
1467   $main::lxdebug->leave_sub();
 
1472 sub defaultaccounts {
 
1473   $main::lxdebug->enter_sub();
 
1475   my ($self, $myconfig, $form) = @_;
 
1477   # connect to database
 
1478   my $dbh = $form->dbconnect($myconfig);
 
1480   # get defaults from defaults table
 
1481   my $query = qq|SELECT * FROM defaults|;
 
1482   my $sth   = $dbh->prepare($query);
 
1483   $sth->execute || $form->dberror($query);
 
1485   $form->{defaults}               = $sth->fetchrow_hashref("NAME_lc");
 
1486   $form->{defaults}{IC}           = $form->{defaults}{inventory_accno_id};
 
1487   $form->{defaults}{IC_income}    = $form->{defaults}{income_accno_id};
 
1488   $form->{defaults}{IC_expense}   = $form->{defaults}{expense_accno_id};
 
1489   $form->{defaults}{FX_gain}      = $form->{defaults}{fxgain_accno_id};
 
1490   $form->{defaults}{FX_loss}      = $form->{defaults}{fxloss_accno_id};
 
1492   $form->{defaults}{weightunit} ||= 'kg';
 
1496   $query = qq|SELECT c.id, c.accno, c.description, c.link
 
1498               WHERE c.link LIKE '%IC%'
 
1500   $sth = $dbh->prepare($query);
 
1501   $sth->execute || $self->dberror($query);
 
1503   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
1504     foreach my $key (split(/:/, $ref->{link})) {
 
1507         if ($key =~ /cogs/) {
 
1508           $nkey = "IC_expense";
 
1510         if ($key =~ /sale/) {
 
1511           $nkey = "IC_income";
 
1513         %{ $form->{IC}{$nkey}{ $ref->{accno} } } = (
 
1515                                              description => $ref->{description}
 
1522   $query = qq|SELECT c.id, c.accno, c.description
 
1524               WHERE c.category = 'I'
 
1525               AND c.charttype = 'A'
 
1527   $sth = $dbh->prepare($query);
 
1528   $sth->execute || $self->dberror($query);
 
1530   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
1531     %{ $form->{IC}{FX_gain}{ $ref->{accno} } } = (
 
1533                                              description => $ref->{description}
 
1538   $query = qq|SELECT c.id, c.accno, c.description
 
1540               WHERE c.category = 'E'
 
1541               AND c.charttype = 'A'
 
1543   $sth = $dbh->prepare($query);
 
1544   $sth->execute || $self->dberror($query);
 
1546   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
1547     %{ $form->{IC}{FX_loss}{ $ref->{accno} } } = (
 
1549                                              description => $ref->{description}
 
1554   # now get the tax rates and numbers
 
1555   $query = qq|SELECT c.id, c.accno, c.description,
 
1556               t.rate * 100 AS rate, t.taxnumber
 
1558               WHERE c.id = t.chart_id|;
 
1560   $sth = $dbh->prepare($query);
 
1561   $sth->execute || $form->dberror($query);
 
1563   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
1564     $form->{taxrates}{ $ref->{accno} }{id}          = $ref->{id};
 
1565     $form->{taxrates}{ $ref->{accno} }{description} = $ref->{description};
 
1566     $form->{taxrates}{ $ref->{accno} }{taxnumber}   = $ref->{taxnumber}
 
1567       if $ref->{taxnumber};
 
1568     $form->{taxrates}{ $ref->{accno} }{rate} = $ref->{rate} if $ref->{rate};
 
1574   $main::lxdebug->leave_sub();
 
1578   $main::lxdebug->enter_sub();
 
1580   my ($self, $myconfig, $form) = @_;
 
1582   my $dbh = $form->dbconnect($myconfig);
 
1584   my $query = qq|SELECT closedto, revtrans FROM defaults|;
 
1585   my $sth   = $dbh->prepare($query);
 
1586   $sth->execute || $form->dberror($query);
 
1588   ($form->{closedto}, $form->{revtrans}) = $sth->fetchrow_array;
 
1594   $main::lxdebug->leave_sub();
 
1598   $main::lxdebug->enter_sub();
 
1600   my ($self, $myconfig, $form) = @_;
 
1602   my $dbh = $form->dbconnect($myconfig);
 
1604   my ($query, @values);
 
1606   if ($form->{revtrans}) {
 
1607     $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '1'|;
 
1609   } elsif ($form->{closedto}) {
 
1610     $query = qq|UPDATE defaults SET closedto = ?, revtrans = '0'|;
 
1611     @values = (conv_date($form->{closedto}));
 
1614     $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '0'|;
 
1617   # set close in defaults
 
1618   do_query($form, $dbh, $query, @values);
 
1622   $main::lxdebug->leave_sub();
 
1626   my ($self, $units, $unit_name, $factor) = @_;
 
1628   $factor = 1 unless ($factor);
 
1630   my $unit = $units->{$unit_name};
 
1632   if (!defined($unit) || !$unit->{"base_unit"} ||
 
1633       ($unit_name eq $unit->{"base_unit"})) {
 
1634     return ($unit_name, $factor);
 
1637   return AM->get_base_unit($units, $unit->{"base_unit"}, $factor * $unit->{"factor"});
 
1640 sub retrieve_units {
 
1641   $main::lxdebug->enter_sub();
 
1643   my ($self, $myconfig, $form, $prefix) = @_;
 
1645   my $dbh = $form->get_standard_dbh;
 
1647   my $query = "SELECT *, base_unit AS original_base_unit FROM units";
 
1649   my $sth = prepare_execute_query($form, $dbh, $query);
 
1652   while (my $ref = $sth->fetchrow_hashref()) {
 
1653     $units->{$ref->{"name"}} = $ref;
 
1657   my $query_lang = "SELECT id, template_code FROM language ORDER BY description";
 
1658   $sth = $dbh->prepare($query_lang);
 
1659   $sth->execute() || $form->dberror($query_lang);
 
1661   while (my $ref = $sth->fetchrow_hashref()) {
 
1662     push(@languages, $ref);
 
1666   $query_lang = "SELECT ul.localized, ul.localized_plural, l.id, l.template_code " .
 
1667     "FROM units_language ul " .
 
1668     "LEFT JOIN language l ON ul.language_id = l.id " .
 
1669     "WHERE ul.unit = ?";
 
1670   $sth = $dbh->prepare($query_lang);
 
1672   foreach my $unit (values(%{$units})) {
 
1673     ($unit->{"${prefix}base_unit"}, $unit->{"${prefix}factor"}) = AM->get_base_unit($units, $unit->{"name"});
 
1675     $unit->{"LANGUAGES"} = {};
 
1676     foreach my $lang (@languages) {
 
1677       $unit->{"LANGUAGES"}->{$lang->{"template_code"}} = { "template_code" => $lang->{"template_code"} };
 
1680     $sth->execute($unit->{"name"}) || $form->dberror($query_lang . " (" . $unit->{"name"} . ")");
 
1681     while (my $ref = $sth->fetchrow_hashref()) {
 
1682       map({ $unit->{"LANGUAGES"}->{$ref->{"template_code"}}->{$_} = $ref->{$_} } keys(%{$ref}));
 
1687   $main::lxdebug->leave_sub();
 
1692 sub retrieve_all_units {
 
1693   $main::lxdebug->enter_sub();
 
1697   if (!$main::all_units) {
 
1698     $main::all_units = $self->retrieve_units(\%main::myconfig, $main::form);
 
1701   $main::lxdebug->leave_sub();
 
1703   return $main::all_units;
 
1707 sub translate_units {
 
1708   $main::lxdebug->enter_sub();
 
1710   my ($self, $form, $template_code, $unit, $amount) = @_;
 
1712   my $units = $self->retrieve_units(\%main::myconfig, $form);
 
1714   my $h = $units->{$unit}->{"LANGUAGES"}->{$template_code};
 
1715   my $new_unit = $unit;
 
1717     if (($amount != 1) && $h->{"localized_plural"}) {
 
1718       $new_unit = $h->{"localized_plural"};
 
1719     } elsif ($h->{"localized"}) {
 
1720       $new_unit = $h->{"localized"};
 
1724   $main::lxdebug->leave_sub();
 
1730   $main::lxdebug->enter_sub();
 
1732   my ($self, $myconfig, $form, $units) = @_;
 
1734   my $dbh = $form->dbconnect($myconfig);
 
1736   map({ $_->{"in_use"} = 0; } values(%{$units}));
 
1738   foreach my $unit (values(%{$units})) {
 
1739     my $base_unit = $unit->{"original_base_unit"};
 
1740     while ($base_unit) {
 
1741       $units->{$base_unit}->{"in_use"} = 1;
 
1742       $units->{$base_unit}->{"DEPENDING_UNITS"} = [] unless ($units->{$base_unit}->{"DEPENDING_UNITS"});
 
1743       push(@{$units->{$base_unit}->{"DEPENDING_UNITS"}}, $unit->{"name"});
 
1744       $base_unit = $units->{$base_unit}->{"original_base_unit"};
 
1748   foreach my $unit (values(%{$units})) {
 
1749     map({ $_ = $dbh->quote($_); } @{$unit->{"DEPENDING_UNITS"}});
 
1751     foreach my $table (qw(parts invoice orderitems)) {
 
1752       my $query = "SELECT COUNT(*) FROM $table WHERE unit ";
 
1754       if (0 == scalar(@{$unit->{"DEPENDING_UNITS"}})) {
 
1755         $query .= "= " . $dbh->quote($unit->{"name"});
 
1757         $query .= "IN (" . $dbh->quote($unit->{"name"}) . "," .
 
1758           join(",", map({ $dbh->quote($_) } @{$unit->{"DEPENDING_UNITS"}})) . ")";
 
1761       my ($count) = $dbh->selectrow_array($query);
 
1762       $form->dberror($query) if ($dbh->err);
 
1765         $unit->{"in_use"} = 1;
 
1773   $main::lxdebug->leave_sub();
 
1776 sub convertible_units {
 
1777   $main::lxdebug->enter_sub();
 
1781   my $filter_unit = shift;
 
1782   my $not_smaller = shift;
 
1784   my $conv_units = [];
 
1786   $filter_unit = $units->{$filter_unit};
 
1788   foreach my $name (sort { lc $a cmp lc $b } keys %{ $units }) {
 
1789     my $unit = $units->{$name};
 
1791     if (($unit->{base_unit} eq $filter_unit->{base_unit}) &&
 
1792         (!$not_smaller || ($unit->{factor} >= $filter_unit->{factor}))) {
 
1793       push @{$conv_units}, $unit;
 
1797   my @sorted = sort { $b->{factor} <=> $a->{factor} } @{ $conv_units };
 
1799   $main::lxdebug->leave_sub();
 
1804 # if $a is translatable to $b, return the factor between them.
 
1807   $main::lxdebug->enter_sub(2);
 
1808   my ($this, $a, $b, $all_units) = @_;
 
1810   $main::lxdebug->leave_sub(2) and return 0 unless $a && $b;
 
1811   $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a} && $all_units->{$b};
 
1812   $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a}{base_unit} eq $all_units->{$b}{base_unit};
 
1813   $main::lxdebug->leave_sub(2) and return $all_units->{$a}{factor} / $all_units->{$b}{factor};
 
1816 sub unit_select_data {
 
1817   $main::lxdebug->enter_sub();
 
1819   my ($self, $units, $selected, $empty_entry, $convertible_into) = @_;
 
1824     push(@{$select}, { "name" => "", "base_unit" => "", "factor" => "", "selected" => "" });
 
1827   foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
 
1828     if (!$convertible_into ||
 
1829         ($units->{$convertible_into} &&
 
1830          ($units->{$convertible_into}->{base_unit} eq $units->{$unit}->{base_unit}))) {
 
1831       push @{$select}, { "name"      => $unit,
 
1832                          "base_unit" => $units->{$unit}->{"base_unit"},
 
1833                          "factor"    => $units->{$unit}->{"factor"},
 
1834                          "selected"  => ($unit eq $selected) ? "selected" : "" };
 
1838   $main::lxdebug->leave_sub();
 
1843 sub unit_select_html {
 
1844   $main::lxdebug->enter_sub();
 
1846   my ($self, $units, $name, $selected, $convertible_into) = @_;
 
1848   my $select = "<select name=${name}>";
 
1850   foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
 
1851     if (!$convertible_into ||
 
1852         ($units->{$convertible_into} &&
 
1853          ($units->{$convertible_into}->{"base_unit"} eq $units->{$unit}->{"base_unit"}))) {
 
1854       $select .= "<option" . (($unit eq $selected) ? " selected" : "") . ">${unit}</option>";
 
1857   $select .= "</select>";
 
1859   $main::lxdebug->leave_sub();
 
1865   $main::lxdebug->enter_sub();
 
1869   my $units = $self->retrieve_all_units();
 
1874   while (2 <= scalar(@_)) {
 
1875     my $qty  = shift(@_);
 
1876     my $unit = $units->{shift(@_)};
 
1878     croak "No unit defined with name $unit" if (!defined $unit);
 
1881       $base_unit = $unit->{base_unit};
 
1882     } elsif ($base_unit ne $unit->{base_unit}) {
 
1883       croak "Adding values with incompatible base units $base_unit/$unit->{base_unit}";
 
1886     $sum += $qty * $unit->{factor};
 
1889   $main::lxdebug->leave_sub();
 
1891   return wantarray ? ($sum, $base_unit) : $sum;
 
1895   $main::lxdebug->enter_sub();
 
1897   my ($self, $myconfig, $form, $name, $base_unit, $factor, $languages) = @_;
 
1899   my $dbh = $form->dbconnect_noauto($myconfig);
 
1901   my $query = qq|SELECT COALESCE(MAX(sortkey), 0) + 1 FROM units|;
 
1902   my ($sortkey) = selectrow_query($form, $dbh, $query);
 
1904   $query = "INSERT INTO units (name, base_unit, factor, sortkey) " .
 
1905     "VALUES (?, ?, ?, ?)";
 
1906   do_query($form, $dbh, $query, $name, $base_unit, $factor, $sortkey);
 
1909     $query = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
 
1910     my $sth = $dbh->prepare($query);
 
1911     foreach my $lang (@{$languages}) {
 
1912       my @values = ($name, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
 
1913       $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
 
1921   $main::lxdebug->leave_sub();
 
1925   $main::lxdebug->enter_sub();
 
1927   my ($self, $myconfig, $form, $units, $delete_units) = @_;
 
1929   my $dbh = $form->dbconnect_noauto($myconfig);
 
1931   my ($base_unit, $unit, $sth, $query);
 
1933   $query = "DELETE FROM units_language";
 
1934   $dbh->do($query) || $form->dberror($query);
 
1936   if ($delete_units && (0 != scalar(@{$delete_units}))) {
 
1937     $query = "DELETE FROM units WHERE name IN (";
 
1938     map({ $query .= "?," } @{$delete_units});
 
1939     substr($query, -1, 1) = ")";
 
1940     $dbh->do($query, undef, @{$delete_units}) ||
 
1941       $form->dberror($query . " (" . join(", ", @{$delete_units}) . ")");
 
1944   $query = "UPDATE units SET name = ?, base_unit = ?, factor = ? WHERE name = ?";
 
1945   $sth = $dbh->prepare($query);
 
1947   my $query_lang = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
 
1948   my $sth_lang = $dbh->prepare($query_lang);
 
1950   foreach $unit (values(%{$units})) {
 
1951     $unit->{"depth"} = 0;
 
1952     my $base_unit = $unit;
 
1953     while ($base_unit->{"base_unit"}) {
 
1955       $base_unit = $units->{$base_unit->{"base_unit"}};
 
1959   foreach $unit (sort({ $a->{"depth"} <=> $b->{"depth"} } values(%{$units}))) {
 
1960     if ($unit->{"LANGUAGES"}) {
 
1961       foreach my $lang (@{$unit->{"LANGUAGES"}}) {
 
1962         next unless ($lang->{"id"} && $lang->{"localized"});
 
1963         my @values = ($unit->{"name"}, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
 
1964         $sth_lang->execute(@values) || $form->dberror($query_lang . " (" . join(", ", @values) . ")");
 
1968     next if ($unit->{"unchanged_unit"});
 
1970     my @values = ($unit->{"name"}, $unit->{"base_unit"}, $unit->{"factor"}, $unit->{"old_name"});
 
1971     $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
 
1975   $sth_lang->finish();
 
1979   $main::lxdebug->leave_sub();
 
1983   $main::lxdebug->enter_sub();
 
1985   my ($self, $myconfig, $form, $dir, $name_1) = @_;
 
1987   my $dbh = $form->dbconnect_noauto($myconfig);
 
1991   $query = qq|SELECT sortkey FROM units WHERE name = ?|;
 
1992   my ($sortkey_1) = selectrow_query($form, $dbh, $query, $name_1);
 
1995     qq|SELECT sortkey FROM units | .
 
1996     qq|WHERE sortkey | . ($dir eq "down" ? ">" : "<") . qq| ? | .
 
1997     qq|ORDER BY sortkey | . ($dir eq "down" ? "ASC" : "DESC") . qq| LIMIT 1|;
 
1998   my ($sortkey_2) = selectrow_query($form, $dbh, $query, $sortkey_1);
 
2000   if (defined($sortkey_1)) {
 
2001     $query = qq|SELECT name FROM units WHERE sortkey = ${sortkey_2}|;
 
2002     my ($name_2) = selectrow_query($form, $dbh, $query);
 
2004     if (defined($name_2)) {
 
2005       $query = qq|UPDATE units SET sortkey = ? WHERE name = ?|;
 
2006       my $sth = $dbh->prepare($query);
 
2008       do_statement($form, $sth, $query, $sortkey_1, $name_2);
 
2009       do_statement($form, $sth, $query, $sortkey_2, $name_1);
 
2016   $main::lxdebug->leave_sub();
 
2020   $main::lxdebug->enter_sub();
 
2022   my ($self, $myconfig, $form) = @_;
 
2024   # connect to database
 
2025   my $dbh = $form->dbconnect($myconfig);
 
2027   my $query = qq|SELECT
 
2031                    round(t.rate * 100, 2) AS rate,
 
2032                    (SELECT accno FROM chart WHERE id = chart_id) AS taxnumber,
 
2033                    (SELECT description FROM chart WHERE id = chart_id) AS account_description
 
2037   my $sth = $dbh->prepare($query);
 
2038   $sth->execute || $form->dberror($query);
 
2041   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
2042     push @{ $form->{TAX} }, $ref;
 
2048   $main::lxdebug->leave_sub();
 
2051 sub get_tax_accounts {
 
2052   $main::lxdebug->enter_sub();
 
2054   my ($self, $myconfig, $form) = @_;
 
2056   my $dbh = $form->dbconnect($myconfig);
 
2058   # get Accounts from chart
 
2059   my $query = qq{ SELECT
 
2061                  accno || ' - ' || description AS taxaccount
 
2063                WHERE link LIKE '%_tax%'
 
2067   my $sth = $dbh->prepare($query);
 
2068   $sth->execute || $form->dberror($query);
 
2070   $form->{ACCOUNTS} = [];
 
2071   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
2072     push @{ $form->{ACCOUNTS} }, $ref;
 
2079   $main::lxdebug->leave_sub();
 
2083   $main::lxdebug->enter_sub();
 
2085   my ($self, $myconfig, $form) = @_;
 
2087   # connect to database
 
2088   my $dbh = $form->dbconnect($myconfig);
 
2090   my $query = qq|SELECT
 
2093                    round(rate * 100, 2) AS rate,
 
2098   my $sth = $dbh->prepare($query);
 
2099   $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
2101   my $ref = $sth->fetchrow_hashref("NAME_lc");
 
2103   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
2107   # see if it is used by a taxkey
 
2108   $query = qq|SELECT count(*) FROM taxkeys
 
2109               WHERE tax_id = ? AND chart_id >0|;
 
2111   ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
 
2113   $form->{orphaned} = !$form->{orphaned};
 
2116   if (!$form->{orphaned} ) {
 
2117     $query = qq|SELECT DISTINCT c.id, c.accno
 
2119                 JOIN   tax t ON (t.id = tk.tax_id)
 
2120                 JOIN chart c ON (c.id = tk.chart_id)
 
2121                 WHERE tk.tax_id = ?|;
 
2123     $sth = $dbh->prepare($query);
 
2124     $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
2126     $form->{TAXINUSE} = [];
 
2127     while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
2128       push @{ $form->{TAXINUSE} }, $ref;
 
2136   $main::lxdebug->leave_sub();
 
2140   $main::lxdebug->enter_sub();
 
2142   my ($self, $myconfig, $form) = @_;
 
2145   # connect to database
 
2146   my $dbh = $form->get_standard_dbh($myconfig);
 
2148   $form->{rate} = $form->{rate} / 100;
 
2150   my @values = ($form->{taxkey}, $form->{taxdescription}, $form->{rate}, $form->{chart_id}, $form->{chart_id} );
 
2151   if ($form->{id} ne "") {
 
2152     $query = qq|UPDATE tax SET
 
2157                   taxnumber      = (SELECT accno FROM chart WHERE id= ? )
 
2159     push(@values, $form->{id});
 
2163     $query = qq|INSERT INTO tax (
 
2170                 VALUES (?, ?, ?, ?, (SELECT accno FROM chart WHERE id = ?) )|;
 
2172   do_query($form, $dbh, $query, @values);
 
2176   $main::lxdebug->leave_sub();
 
2180   $main::lxdebug->enter_sub();
 
2182   my ($self, $myconfig, $form) = @_;
 
2185   # connect to database
 
2186   my $dbh = $form->get_standard_dbh($myconfig);
 
2188   $query = qq|DELETE FROM tax
 
2190   do_query($form, $dbh, $query, $form->{id});
 
2194   $main::lxdebug->leave_sub();
 
2197 sub save_price_factor {
 
2198   $main::lxdebug->enter_sub();
 
2200   my ($self, $myconfig, $form) = @_;
 
2202   # connect to database
 
2203   my $dbh = $form->get_standard_dbh($myconfig);
 
2206   my @values = ($form->{description}, conv_i($form->{factor}));
 
2209     $query = qq|UPDATE price_factors SET description = ?, factor = ? WHERE id = ?|;
 
2210     push @values, conv_i($form->{id});
 
2213     $query = qq|INSERT INTO price_factors (description, factor, sortkey) VALUES (?, ?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM price_factors))|;
 
2216   do_query($form, $dbh, $query, @values);
 
2220   $main::lxdebug->leave_sub();
 
2223 sub get_all_price_factors {
 
2224   $main::lxdebug->enter_sub();
 
2226   my ($self, $myconfig, $form) = @_;
 
2228   # connect to database
 
2229   my $dbh = $form->get_standard_dbh($myconfig);
 
2231   $form->{PRICE_FACTORS} = selectall_hashref_query($form, $dbh, qq|SELECT * FROM price_factors ORDER BY sortkey|);
 
2233   $main::lxdebug->leave_sub();
 
2236 sub get_price_factor {
 
2237   $main::lxdebug->enter_sub();
 
2239   my ($self, $myconfig, $form) = @_;
 
2241   # connect to database
 
2242   my $dbh = $form->get_standard_dbh($myconfig);
 
2244   my $query = qq|SELECT description, factor,
 
2245                    ((SELECT COUNT(*) FROM parts      WHERE price_factor_id = ?) +
 
2246                     (SELECT COUNT(*) FROM invoice    WHERE price_factor_id = ?) +
 
2247                     (SELECT COUNT(*) FROM orderitems WHERE price_factor_id = ?)) = 0 AS orphaned
 
2248                  FROM price_factors WHERE id = ?|;
 
2250   ($form->{description}, $form->{factor}, $form->{orphaned}) = selectrow_query($form, $dbh, $query, (conv_i($form->{id})) x 4);
 
2252   $main::lxdebug->leave_sub();
 
2255 sub delete_price_factor {
 
2256   $main::lxdebug->enter_sub();
 
2258   my ($self, $myconfig, $form) = @_;
 
2260   # connect to database
 
2261   my $dbh = $form->get_standard_dbh($myconfig);
 
2263   do_query($form, $dbh, qq|DELETE FROM price_factors WHERE id = ?|, conv_i($form->{id}));
 
2266   $main::lxdebug->leave_sub();
 
2269 sub save_warehouse {
 
2270   $main::lxdebug->enter_sub();
 
2272   my ($self, $myconfig, $form) = @_;
 
2274   # connect to database
 
2275   my $dbh = $form->get_standard_dbh($myconfig);
 
2277   my ($query, @values, $sth);
 
2280     $query        = qq|SELECT nextval('id')|;
 
2281     ($form->{id}) = selectrow_query($form, $dbh, $query);
 
2283     $query        = qq|INSERT INTO warehouse (id, sortkey) VALUES (?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM warehouse))|;
 
2284     do_query($form, $dbh, $query, $form->{id});
 
2287   do_query($form, $dbh, qq|UPDATE warehouse SET description = ?, invalid = ? WHERE id = ?|,
 
2288            $form->{description}, $form->{invalid} ? 't' : 'f', conv_i($form->{id}));
 
2290   if (0 < $form->{number_of_new_bins}) {
 
2291     $query = qq|INSERT INTO bin (warehouse_id, description) VALUES (?, ?)|;
 
2292     $sth   = prepare_query($form, $dbh, $query);
 
2294     foreach my $i (1..$form->{number_of_new_bins}) {
 
2295       do_statement($form, $sth, $query, conv_i($form->{id}), "$form->{prefix}${i}");
 
2303   $main::lxdebug->leave_sub();
 
2307   $main::lxdebug->enter_sub();
 
2309   my ($self, $myconfig, $form) = @_;
 
2311   # connect to database
 
2312   my $dbh = $form->get_standard_dbh($myconfig);
 
2314   my ($query, @values, $commit_necessary, $sth);
 
2316   @values = map { $form->{"id_${_}"} } grep { $form->{"delete_${_}"} } (1..$form->{rowcount});
 
2319     $query = qq|DELETE FROM bin WHERE id IN (| . join(', ', ('?') x scalar(@values)) . qq|)|;
 
2320     do_query($form, $dbh, $query, @values);
 
2322     $commit_necessary = 1;
 
2325   $query = qq|UPDATE bin SET description = ? WHERE id = ?|;
 
2326   $sth   = prepare_query($form, $dbh, $query);
 
2328   foreach my $row (1..$form->{rowcount}) {
 
2329     next if ($form->{"delete_${row}"});
 
2331     do_statement($form, $sth, $query, $form->{"description_${row}"}, conv_i($form->{"id_${row}"}));
 
2333     $commit_necessary = 1;
 
2338   $dbh->commit() if ($commit_necessary);
 
2340   $main::lxdebug->leave_sub();
 
2343 sub delete_warehouse {
 
2344   $main::lxdebug->enter_sub();
 
2346   my ($self, $myconfig, $form) = @_;
 
2348   # connect to database
 
2349   my $dbh = $form->get_standard_dbh($myconfig);
 
2351   my $id      = conv_i($form->{id});
 
2352   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|;
 
2353   my ($count) = selectrow_query($form, $dbh, $query, $id);
 
2356     $main::lxdebug->leave_sub();
 
2360   do_query($form, $dbh, qq|DELETE FROM bin       WHERE warehouse_id = ?|, conv_i($form->{id}));
 
2361   do_query($form, $dbh, qq|DELETE FROM warehouse WHERE id           = ?|, conv_i($form->{id}));
 
2365   $main::lxdebug->leave_sub();
 
2370 sub get_all_warehouses {
 
2371   $main::lxdebug->enter_sub();
 
2373   my ($self, $myconfig, $form) = @_;
 
2375   # connect to database
 
2376   my $dbh = $form->get_standard_dbh($myconfig);
 
2378   my $query = qq|SELECT w.id, w.description, w.invalid,
 
2379                    (SELECT COUNT(b.description) FROM bin b WHERE b.warehouse_id = w.id) AS number_of_bins
 
2381                  ORDER BY w.sortkey|;
 
2383   $form->{WAREHOUSES} = selectall_hashref_query($form, $dbh, $query);
 
2385   $main::lxdebug->leave_sub();
 
2389   $main::lxdebug->enter_sub();
 
2391   my ($self, $myconfig, $form) = @_;
 
2393   # connect to database
 
2394   my $dbh = $form->get_standard_dbh($myconfig);
 
2396   my $id    = conv_i($form->{id});
 
2397   my $query = qq|SELECT w.description, w.invalid
 
2401   my $ref   = selectfirst_hashref_query($form, $dbh, $query, $id);
 
2403   map { $form->{$_} = $ref->{$_} } keys %{ $ref };
 
2405   $query = qq|SELECT b.*, EXISTS
 
2406                 (SELECT i.warehouse_id
 
2408                  WHERE i.bin_id = b.id
 
2412               WHERE b.warehouse_id = ?|;
 
2414   $form->{BINS} = selectall_hashref_query($form, $dbh, $query, conv_i($form->{id}));
 
2416   $main::lxdebug->leave_sub();