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 d.id, d.description, d.role
 
 489   my $sth = $dbh->prepare($query);
 
 490   $sth->execute || $form->dberror($query);
 
 493   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 494     push @{ $form->{ALL} }, $ref;
 
 500   $main::lxdebug->leave_sub();
 
 504   $main::lxdebug->enter_sub();
 
 506   my ($self, $myconfig, $form) = @_;
 
 508   # connect to database
 
 509   my $dbh = $form->dbconnect($myconfig);
 
 511   my $query = qq|SELECT d.description, d.role
 
 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;
 
 523   # see if it is in use
 
 524   $query = qq|SELECT count(*) FROM dpt_trans d
 
 525               WHERE d.department_id = ?|;
 
 526   ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
 
 528   $form->{orphaned} = !$form->{orphaned};
 
 533   $main::lxdebug->leave_sub();
 
 536 sub save_department {
 
 537   $main::lxdebug->enter_sub();
 
 539   my ($self, $myconfig, $form) = @_;
 
 542   # connect to database
 
 543   my $dbh = $form->dbconnect($myconfig);
 
 545   my @values = ($form->{description}, $form->{role});
 
 547     $query = qq|UPDATE department SET
 
 548                 description = ?, role = ?
 
 550     push(@values, $form->{id});
 
 552     $query = qq|INSERT INTO department
 
 556   do_query($form, $dbh, $query, @values);
 
 560   $main::lxdebug->leave_sub();
 
 563 sub delete_department {
 
 564   $main::lxdebug->enter_sub();
 
 566   my ($self, $myconfig, $form) = @_;
 
 569   # connect to database
 
 570   my $dbh = $form->dbconnect($myconfig);
 
 572   $query = qq|DELETE FROM department
 
 574   do_query($form, $dbh, $query, $form->{id});
 
 578   $main::lxdebug->leave_sub();
 
 582   $main::lxdebug->enter_sub();
 
 584   my ($self, $myconfig, $form) = @_;
 
 586   # connect to database
 
 587   my $dbh = $form->dbconnect($myconfig);
 
 589   my $query = qq|SELECT id, lead
 
 593   my $sth = $dbh->prepare($query);
 
 594   $sth->execute || $form->dberror($query);
 
 596   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 597     push @{ $form->{ALL} }, $ref;
 
 603   $main::lxdebug->leave_sub();
 
 607   $main::lxdebug->enter_sub();
 
 609   my ($self, $myconfig, $form) = @_;
 
 611   # connect to database
 
 612   my $dbh = $form->dbconnect($myconfig);
 
 615     qq|SELECT l.id, l.lead | .
 
 618   my $sth = $dbh->prepare($query);
 
 619   $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
 621   my $ref = $sth->fetchrow_hashref("NAME_lc");
 
 623   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
 629   $main::lxdebug->leave_sub();
 
 633   $main::lxdebug->enter_sub();
 
 635   my ($self, $myconfig, $form) = @_;
 
 638   # connect to database
 
 639   my $dbh = $form->dbconnect($myconfig);
 
 641   my @values = ($form->{description});
 
 642   # id is the old record
 
 644     $query = qq|UPDATE leads SET
 
 647     puhs(@values, $form->{id});
 
 649     $query = qq|INSERT INTO leads
 
 653   do_query($form, $dbh, $query, @values);
 
 657   $main::lxdebug->leave_sub();
 
 661   $main::lxdebug->enter_sub();
 
 663   my ($self, $myconfig, $form) = @_;
 
 666   # connect to database
 
 667   my $dbh = $form->dbconnect($myconfig);
 
 669   $query = qq|DELETE FROM leads
 
 671   do_query($form, $dbh, $query, $form->{id});
 
 675   $main::lxdebug->leave_sub();
 
 679   $main::lxdebug->enter_sub();
 
 681   my ($self, $myconfig, $form) = @_;
 
 683   # connect to database
 
 684   my $dbh = $form->dbconnect($myconfig);
 
 686   my $query = qq|SELECT id, description, discount, customernumberinit, salesman
 
 690   my $sth = $dbh->prepare($query);
 
 691   $sth->execute || $form->dberror($query);
 
 693   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 694     push @{ $form->{ALL} }, $ref;
 
 700   $main::lxdebug->leave_sub();
 
 704   $main::lxdebug->enter_sub();
 
 706   my ($self, $myconfig, $form) = @_;
 
 708   # connect to database
 
 709   my $dbh = $form->dbconnect($myconfig);
 
 712     qq|SELECT b.description, b.discount, b.customernumberinit, b.salesman
 
 715   my $sth = $dbh->prepare($query);
 
 716   $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
 718   my $ref = $sth->fetchrow_hashref("NAME_lc");
 
 720   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
 726   $main::lxdebug->leave_sub();
 
 730   $main::lxdebug->enter_sub();
 
 732   my ($self, $myconfig, $form) = @_;
 
 735   # connect to database
 
 736   my $dbh = $form->dbconnect($myconfig);
 
 738   my @values = ($form->{description}, $form->{discount}, $form->{customernumberinit}, $form->{salesman} ? 't' : 'f');
 
 739   # id is the old record
 
 741     $query = qq|UPDATE business SET
 
 744                 customernumberinit = ?,
 
 747     push(@values, $form->{id});
 
 749     $query = qq|INSERT INTO business
 
 750                 (description, discount, customernumberinit, salesman)
 
 751                 VALUES (?, ?, ?, ?)|;
 
 753   do_query($form, $dbh, $query, @values);
 
 757   $main::lxdebug->leave_sub();
 
 760 sub delete_business {
 
 761   $main::lxdebug->enter_sub();
 
 763   my ($self, $myconfig, $form) = @_;
 
 765   # connect to database
 
 766   my $dbh = $form->dbconnect($myconfig);
 
 768   my $query = qq|DELETE FROM business
 
 770   do_query($form, $dbh, $query, $form->{id});
 
 774   $main::lxdebug->leave_sub();
 
 779   $main::lxdebug->enter_sub();
 
 781   my ($self, $myconfig, $form, $return_list) = @_;
 
 783   # connect to database
 
 784   my $dbh = $form->dbconnect($myconfig);
 
 787     "SELECT id, description, template_code, article_code, " .
 
 788     "  output_numberformat, output_dateformat, output_longdates " .
 
 789     "FROM language ORDER BY description";
 
 791   my $sth = $dbh->prepare($query);
 
 792   $sth->execute || $form->dberror($query);
 
 796   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 797     push(@{ $ary }, $ref);
 
 803   $main::lxdebug->leave_sub();
 
 813   $main::lxdebug->enter_sub();
 
 815   my ($self, $myconfig, $form) = @_;
 
 817   # connect to database
 
 818   my $dbh = $form->dbconnect($myconfig);
 
 821     "SELECT description, template_code, article_code, " .
 
 822     "  output_numberformat, output_dateformat, output_longdates " .
 
 823     "FROM language WHERE id = ?";
 
 824   my $sth = $dbh->prepare($query);
 
 825   $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
 
 827   my $ref = $sth->fetchrow_hashref("NAME_lc");
 
 829   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
 835   $main::lxdebug->leave_sub();
 
 838 sub get_language_details {
 
 839   $main::lxdebug->enter_sub();
 
 841   my ($self, $myconfig, $form, $id) = @_;
 
 843   # connect to database
 
 844   my $dbh = $form->dbconnect($myconfig);
 
 847     "SELECT template_code, " .
 
 848     "  output_numberformat, output_dateformat, output_longdates " .
 
 849     "FROM language WHERE id = ?";
 
 850   my @res = selectrow_query($form, $dbh, $query, $id);
 
 853   $main::lxdebug->leave_sub();
 
 859   $main::lxdebug->enter_sub();
 
 861   my ($self, $myconfig, $form) = @_;
 
 863   # connect to database
 
 864   my $dbh = $form->dbconnect($myconfig);
 
 865   my (@values, $query);
 
 867   map({ push(@values, $form->{$_}); }
 
 868       qw(description template_code article_code
 
 869          output_numberformat output_dateformat output_longdates));
 
 871   # id is the old record
 
 874       "UPDATE language SET " .
 
 875       "  description = ?, template_code = ?, article_code = ?, " .
 
 876       "  output_numberformat = ?, output_dateformat = ?, " .
 
 877       "  output_longdates = ? " .
 
 879     push(@values, $form->{id});
 
 882       "INSERT INTO language (" .
 
 883       "  description, template_code, article_code, " .
 
 884       "  output_numberformat, output_dateformat, output_longdates" .
 
 885       ") VALUES (?, ?, ?, ?, ?, ?)";
 
 887   do_query($form, $dbh, $query, @values);
 
 891   $main::lxdebug->leave_sub();
 
 894 sub delete_language {
 
 895   $main::lxdebug->enter_sub();
 
 897   my ($self, $myconfig, $form) = @_;
 
 900   # connect to database
 
 901   my $dbh = $form->dbconnect_noauto($myconfig);
 
 903   foreach my $table (qw(generic_translations units_language)) {
 
 904     $query = qq|DELETE FROM $table WHERE language_id = ?|;
 
 905     do_query($form, $dbh, $query, $form->{"id"});
 
 908   $query = "DELETE FROM language WHERE id = ?";
 
 909   do_query($form, $dbh, $query, $form->{"id"});
 
 914   $main::lxdebug->leave_sub();
 
 919   $main::lxdebug->enter_sub();
 
 921   my ($self, $myconfig, $form) = @_;
 
 923   # connect to database
 
 924   my $dbh = $form->dbconnect($myconfig);
 
 926   my $query = qq|SELECT id, description,
 
 928                  (SELECT accno FROM chart WHERE id = inventory_accno_id) AS inventory_accno,
 
 930                  (SELECT accno FROM chart WHERE id = income_accno_id_0) AS income_accno_0,
 
 932                  (SELECT accno FROM chart WHERE id = expense_accno_id_0) AS expense_accno_0,
 
 934                  (SELECT accno FROM chart WHERE id = income_accno_id_1) AS income_accno_1,
 
 936                  (SELECT accno FROM chart WHERE id = expense_accno_id_1) AS expense_accno_1,
 
 938                  (SELECT accno FROM chart WHERE id = income_accno_id_2) AS income_accno_2,
 
 940                  (select accno FROM chart WHERE id = expense_accno_id_2) AS expense_accno_2,
 
 942                  (SELECT accno FROM chart WHERE id = income_accno_id_3) AS income_accno_3,
 
 944                  (SELECT accno FROM chart WHERE id = expense_accno_id_3) AS expense_accno_3
 
 948   my $sth = $dbh->prepare($query);
 
 949   $sth->execute || $form->dberror($query);
 
 952   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 953     push @{ $form->{ALL} }, $ref;
 
 959   $main::lxdebug->leave_sub();
 
 962 sub get_buchungsgruppe {
 
 963   $main::lxdebug->enter_sub();
 
 965   my ($self, $myconfig, $form) = @_;
 
 968   # connect to database
 
 969   my $dbh = $form->dbconnect($myconfig);
 
 973       qq|SELECT description, inventory_accno_id,
 
 974          (SELECT accno FROM chart WHERE id = inventory_accno_id) AS inventory_accno,
 
 976          (SELECT accno FROM chart WHERE id = income_accno_id_0) AS income_accno_0,
 
 978          (SELECT accno FROM chart WHERE id = expense_accno_id_0) AS expense_accno_0,
 
 980          (SELECT accno FROM chart WHERE id = income_accno_id_1) AS income_accno_1,
 
 982          (SELECT accno FROM chart WHERE id = expense_accno_id_1) AS expense_accno_1,
 
 984          (SELECT accno FROM chart WHERE id = income_accno_id_2) AS income_accno_2,
 
 986          (select accno FROM chart WHERE id = expense_accno_id_2) AS expense_accno_2,
 
 988          (SELECT accno FROM chart WHERE id = income_accno_id_3) AS income_accno_3,
 
 990          (SELECT accno FROM chart WHERE id = expense_accno_id_3) AS expense_accno_3
 
 993     my $sth = $dbh->prepare($query);
 
 994     $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
 996     my $ref = $sth->fetchrow_hashref("NAME_lc");
 
 998     map { $form->{$_} = $ref->{$_} } keys %$ref;
 
1003       qq|SELECT count(id) = 0 AS orphaned
 
1005          WHERE buchungsgruppen_id = ?|;
 
1006     ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
 
1009   $query = "SELECT inventory_accno_id, income_accno_id, expense_accno_id ".
 
1011   ($form->{"std_inventory_accno_id"}, $form->{"std_income_accno_id"},
 
1012    $form->{"std_expense_accno_id"}) = selectrow_query($form, $dbh, $query);
 
1015   $query = qq|SELECT c.accno, c.description, c.link, c.id,
 
1016               d.inventory_accno_id, d.income_accno_id, d.expense_accno_id
 
1017               FROM chart c, defaults d
 
1018               WHERE c.link LIKE '%$module%'
 
1022   my $sth = $dbh->prepare($query);
 
1023   $sth->execute || $form->dberror($query);
 
1024   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
1025     foreach my $key (split(/:/, $ref->{link})) {
 
1026       if (!$form->{"std_inventory_accno_id"} && ($key eq "IC")) {
 
1027         $form->{"std_inventory_accno_id"} = $ref->{"id"};
 
1029       if ($key =~ /$module/) {
 
1030         if (   ($ref->{id} eq $ref->{inventory_accno_id})
 
1031             || ($ref->{id} eq $ref->{income_accno_id})
 
1032             || ($ref->{id} eq $ref->{expense_accno_id})) {
 
1033           push @{ $form->{"${module}_links"}{$key} },
 
1034             { accno       => $ref->{accno},
 
1035               description => $ref->{description},
 
1036               selected    => "selected",
 
1039           push @{ $form->{"${module}_links"}{$key} },
 
1040             { accno       => $ref->{accno},
 
1041               description => $ref->{description},
 
1053   $main::lxdebug->leave_sub();
 
1056 sub save_buchungsgruppe {
 
1057   $main::lxdebug->enter_sub();
 
1059   my ($self, $myconfig, $form) = @_;
 
1061   # connect to database
 
1062   my $dbh = $form->dbconnect($myconfig);
 
1064   my @values = ($form->{description}, $form->{inventory_accno_id},
 
1065                 $form->{income_accno_id_0}, $form->{expense_accno_id_0},
 
1066                 $form->{income_accno_id_1}, $form->{expense_accno_id_1},
 
1067                 $form->{income_accno_id_2}, $form->{expense_accno_id_2},
 
1068                 $form->{income_accno_id_3}, $form->{expense_accno_id_3});
 
1072   # id is the old record
 
1074     $query = qq|UPDATE buchungsgruppen SET
 
1075                 description = ?, inventory_accno_id = ?,
 
1076                 income_accno_id_0 = ?, expense_accno_id_0 = ?,
 
1077                 income_accno_id_1 = ?, expense_accno_id_1 = ?,
 
1078                 income_accno_id_2 = ?, expense_accno_id_2 = ?,
 
1079                 income_accno_id_3 = ?, expense_accno_id_3 = ?
 
1081     push(@values, $form->{id});
 
1083     $query = qq|SELECT COALESCE(MAX(sortkey) + 1, 1) FROM buchungsgruppen|;
 
1084     my ($sortkey) = $dbh->selectrow_array($query);
 
1085     $form->dberror($query) if ($dbh->err);
 
1086     push(@values, $sortkey);
 
1087     $query = qq|INSERT INTO buchungsgruppen
 
1088                 (description, inventory_accno_id,
 
1089                 income_accno_id_0, expense_accno_id_0,
 
1090                 income_accno_id_1, expense_accno_id_1,
 
1091                 income_accno_id_2, expense_accno_id_2,
 
1092                 income_accno_id_3, expense_accno_id_3,
 
1094                 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
 
1096   do_query($form, $dbh, $query, @values);
 
1100   $main::lxdebug->leave_sub();
 
1103 sub delete_buchungsgruppe {
 
1104   $main::lxdebug->enter_sub();
 
1106   my ($self, $myconfig, $form) = @_;
 
1108   # connect to database
 
1109   my $dbh = $form->dbconnect($myconfig);
 
1111   my $query = qq|DELETE FROM buchungsgruppen WHERE id = ?|;
 
1112   do_query($form, $dbh, $query, $form->{id});
 
1116   $main::lxdebug->leave_sub();
 
1120   $main::lxdebug->enter_sub();
 
1122   my ($self, $myconfig, $form, $table) = @_;
 
1124   # connect to database
 
1125   my $dbh = $form->get_standard_dbh($myconfig);
 
1129        (SELECT sortkey FROM $table WHERE id = ?) AS sortkey1,
 
1130        (SELECT sortkey FROM $table WHERE id = ?) AS sortkey2|;
 
1131   my @values   = ($form->{"id1"}, $form->{"id2"});
 
1132   my @sortkeys = selectrow_query($form, $dbh, $query, @values);
 
1134   $query  = qq|UPDATE $table SET sortkey = ? WHERE id = ?|;
 
1135   my $sth = prepare_query($form, $dbh, $query);
 
1137   do_statement($form, $sth, $query, $sortkeys[1], $form->{"id1"});
 
1138   do_statement($form, $sth, $query, $sortkeys[0], $form->{"id2"});
 
1144   $main::lxdebug->leave_sub();
 
1147 sub prepare_template_filename {
 
1148   $main::lxdebug->enter_sub();
 
1150   my ($self, $myconfig, $form) = @_;
 
1152   my ($filename, $display_filename);
 
1154   if ($form->{type} eq "stylesheet") {
 
1155     $filename = "css/$myconfig->{stylesheet}";
 
1156     $display_filename = $myconfig->{stylesheet};
 
1159     $filename = $form->{formname};
 
1161     if ($form->{language}) {
 
1162       my ($id, $template_code) = split(/--/, $form->{language});
 
1163       $filename .= "_${template_code}";
 
1166     if ($form->{printer}) {
 
1167       my ($id, $template_code) = split(/--/, $form->{printer});
 
1168       $filename .= "_${template_code}";
 
1171     $filename .= "." . ($form->{format} eq "html" ? "html" : "tex");
 
1172     $filename =~ s|.*/||;
 
1173     $display_filename = $filename;
 
1174     $filename = "$myconfig->{templates}/$filename";
 
1177   $main::lxdebug->leave_sub();
 
1179   return ($filename, $display_filename);
 
1184   $main::lxdebug->enter_sub();
 
1186   my ($self, $filename) = @_;
 
1188   my ($content, $lines) = ("", 0);
 
1192   if (open(TEMPLATE, $filename)) {
 
1193     while (<TEMPLATE>) {
 
1200   $content = Encode::decode('utf-8-strict', $content) if $::locale->is_utf8;
 
1202   $main::lxdebug->leave_sub();
 
1204   return ($content, $lines);
 
1208   $main::lxdebug->enter_sub();
 
1210   my ($self, $filename, $content) = @_;
 
1216   if (open(TEMPLATE, ">$filename")) {
 
1217     $content = Encode::encode('utf-8-strict', $content) if $::locale->is_utf8;
 
1218     $content =~ s/\r\n/\n/g;
 
1219     print(TEMPLATE $content);
 
1225   $main::lxdebug->leave_sub();
 
1231   $main::lxdebug->enter_sub();
 
1236   my $myconfig = \%main::myconfig;
 
1237   my $form     = $main::form;
 
1239   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
 
1242   map { ($accnos{$_}) = split(m/--/, $form->{$_}) } qw(inventory_accno income_accno expense_accno fxgain_accno fxloss_accno ar_paid_accno);
 
1244   $form->{curr}  =~ s/ //g;
 
1245   my @currencies =  grep { $_ ne '' } split m/:/, $form->{curr};
 
1246   my $currency   =  join ':', @currencies;
 
1248   # these defaults are database wide
 
1251     qq|UPDATE defaults SET
 
1252         inventory_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
 
1253         income_accno_id    = (SELECT c.id FROM chart c WHERE c.accno = ?),
 
1254         expense_accno_id   = (SELECT c.id FROM chart c WHERE c.accno = ?),
 
1255         fxgain_accno_id    = (SELECT c.id FROM chart c WHERE c.accno = ?),
 
1256         fxloss_accno_id    = (SELECT c.id FROM chart c WHERE c.accno = ?),
 
1257         ar_paid_accno_id   = (SELECT c.id FROM chart c WHERE c.accno = ?),
 
1273   my @values = ($accnos{inventory_accno}, $accnos{income_accno}, $accnos{expense_accno},
 
1274                 $accnos{fxgain_accno},    $accnos{fxloss_accno}, $accnos{ar_paid_accno},
 
1275                 $form->{invnumber},       $form->{cnnumber},
 
1276                 $form->{sonumber},        $form->{ponumber},
 
1277                 $form->{sqnumber},        $form->{rfqnumber},
 
1278                 $form->{customernumber},  $form->{vendornumber},
 
1279                 $form->{articlenumber},   $form->{servicenumber},
 
1280                 $form->{sdonumber},       $form->{pdonumber},
 
1282                 $form->{businessnumber},  $form->{weightunit});
 
1283   do_query($form, $dbh, $query, @values);
 
1287   $main::lxdebug->leave_sub();
 
1291 sub save_preferences {
 
1292   $main::lxdebug->enter_sub();
 
1294   my ($self, $myconfig, $form) = @_;
 
1296   my $dbh = $form->get_standard_dbh($myconfig);
 
1298   my ($currency, $businessnumber) = selectrow_query($form, $dbh, qq|SELECT curr, businessnumber FROM defaults|);
 
1301   my $query = qq|UPDATE employee SET name = ? WHERE login = ?|;
 
1302   do_query($form, $dbh, $query, $form->{name}, $form->{login});
 
1304   my $rc = $dbh->commit();
 
1306   # save first currency in myconfig
 
1307   $currency               =~ s/:.*//;
 
1308   $form->{currency}       =  $currency;
 
1310   $form->{businessnumber} =  $businessnumber;
 
1312   $myconfig = new User($form->{login});
 
1314   foreach my $item (keys %$form) {
 
1315     $myconfig->{$item} = $form->{$item};
 
1318   $myconfig->save_member;
 
1320   my $auth = $main::auth;
 
1322   $main::lxdebug->leave_sub();
 
1328   $main::lxdebug->enter_sub();
 
1333   my $myconfig = \%main::myconfig;
 
1334   my $form     = $main::form;
 
1336   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
 
1338   my $defaults = selectfirst_hashref_query($form, $dbh, qq|SELECT * FROM defaults|) || {};
 
1340   $defaults->{weightunit} ||= 'kg';
 
1342   $main::lxdebug->leave_sub();
 
1347 sub defaultaccounts {
 
1348   $main::lxdebug->enter_sub();
 
1350   my ($self, $myconfig, $form) = @_;
 
1352   # connect to database
 
1353   my $dbh = $form->dbconnect($myconfig);
 
1355   # get defaults from defaults table
 
1356   my $query = qq|SELECT * FROM defaults|;
 
1357   my $sth   = $dbh->prepare($query);
 
1358   $sth->execute || $form->dberror($query);
 
1360   $form->{defaults}               = $sth->fetchrow_hashref("NAME_lc");
 
1361   $form->{defaults}{IC}           = $form->{defaults}{inventory_accno_id};
 
1362   $form->{defaults}{IC_income}    = $form->{defaults}{income_accno_id};
 
1363   $form->{defaults}{IC_expense}   = $form->{defaults}{expense_accno_id};
 
1364   $form->{defaults}{FX_gain}      = $form->{defaults}{fxgain_accno_id};
 
1365   $form->{defaults}{FX_loss}      = $form->{defaults}{fxloss_accno_id};
 
1366   $form->{defaults}{AR_paid}      = $form->{defaults}{ar_paid_accno_id};
 
1368   $form->{defaults}{weightunit} ||= 'kg';
 
1372   $query = qq|SELECT c.id, c.accno, c.description, c.link
 
1374               WHERE c.link LIKE '%IC%'
 
1376   $sth = $dbh->prepare($query);
 
1377   $sth->execute || $self->dberror($query);
 
1379   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
1380     foreach my $key (split(/:/, $ref->{link})) {
 
1383         if ($key =~ /cogs/) {
 
1384           $nkey = "IC_expense";
 
1386         if ($key =~ /sale/) {
 
1387           $nkey = "IC_income";
 
1389         %{ $form->{IC}{$nkey}{ $ref->{accno} } } = (
 
1391                                              description => $ref->{description}
 
1398   $query = qq|SELECT c.id, c.accno, c.description
 
1400               WHERE c.category = 'I'
 
1401               AND c.charttype = 'A'
 
1403   $sth = $dbh->prepare($query);
 
1404   $sth->execute || $self->dberror($query);
 
1406   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
1407     %{ $form->{IC}{FX_gain}{ $ref->{accno} } } = (
 
1409                                              description => $ref->{description}
 
1414   $query = qq|SELECT c.id, c.accno, c.description
 
1416               WHERE c.category = 'E'
 
1417               AND c.charttype = 'A'
 
1419   $sth = $dbh->prepare($query);
 
1420   $sth->execute || $self->dberror($query);
 
1422   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
1423     %{ $form->{IC}{FX_loss}{ $ref->{accno} } } = (
 
1425                                              description => $ref->{description}
 
1430   # now get the tax rates and numbers
 
1431   $query = qq|SELECT c.id, c.accno, c.description,
 
1432               t.rate * 100 AS rate, t.taxnumber
 
1434               WHERE c.id = t.chart_id|;
 
1436   $sth = $dbh->prepare($query);
 
1437   $sth->execute || $form->dberror($query);
 
1439   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
1440     $form->{taxrates}{ $ref->{accno} }{id}          = $ref->{id};
 
1441     $form->{taxrates}{ $ref->{accno} }{description} = $ref->{description};
 
1442     $form->{taxrates}{ $ref->{accno} }{taxnumber}   = $ref->{taxnumber}
 
1443       if $ref->{taxnumber};
 
1444     $form->{taxrates}{ $ref->{accno} }{rate} = $ref->{rate} if $ref->{rate};
 
1446   # Abfrage für Standard Umlaufvermögenskonto
 
1448     qq|SELECT id, accno, description, link | .
 
1450     qq|WHERE link LIKE ? |.
 
1452   $sth = prepare_execute_query($form, $dbh, $query, '%AR%');
 
1453   $sth->execute || $form->dberror($query);#
 
1454   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
1455     foreach my $item (split(/:/, $ref->{link})) {
 
1456       if ($item eq "AR_paid") {
 
1457         %{ $form->{IC}{AR_paid}{ $ref->{accno} } } = (
 
1459                                              description => $ref->{description}
 
1468   $main::lxdebug->leave_sub();
 
1472   $main::lxdebug->enter_sub();
 
1474   my ($self, $myconfig, $form) = @_;
 
1476   my $dbh = $form->dbconnect($myconfig);
 
1478   my $query = qq|SELECT closedto, revtrans FROM defaults|;
 
1479   my $sth   = $dbh->prepare($query);
 
1480   $sth->execute || $form->dberror($query);
 
1482   ($form->{closedto}, $form->{revtrans}) = $sth->fetchrow_array;
 
1488   $main::lxdebug->leave_sub();
 
1492   $main::lxdebug->enter_sub();
 
1494   my ($self, $myconfig, $form) = @_;
 
1496   my $dbh = $form->dbconnect($myconfig);
 
1498   my ($query, @values);
 
1500   if ($form->{revtrans}) {
 
1501     $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '1'|;
 
1503   } elsif ($form->{closedto}) {
 
1504     $query = qq|UPDATE defaults SET closedto = ?, revtrans = '0'|;
 
1505     @values = (conv_date($form->{closedto}));
 
1508     $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '0'|;
 
1511   # set close in defaults
 
1512   do_query($form, $dbh, $query, @values);
 
1516   $main::lxdebug->leave_sub();
 
1520   my ($self, $units, $unit_name, $factor) = @_;
 
1522   $factor = 1 unless ($factor);
 
1524   my $unit = $units->{$unit_name};
 
1526   if (!defined($unit) || !$unit->{"base_unit"} ||
 
1527       ($unit_name eq $unit->{"base_unit"})) {
 
1528     return ($unit_name, $factor);
 
1531   return AM->get_base_unit($units, $unit->{"base_unit"}, $factor * $unit->{"factor"});
 
1534 sub retrieve_units {
 
1535   $main::lxdebug->enter_sub();
 
1537   my ($self, $myconfig, $form, $prefix) = @_;
 
1539   my $dbh = $form->get_standard_dbh;
 
1541   my $query = "SELECT *, base_unit AS original_base_unit FROM units";
 
1543   my $sth = prepare_execute_query($form, $dbh, $query);
 
1546   while (my $ref = $sth->fetchrow_hashref()) {
 
1547     $units->{$ref->{"name"}} = $ref;
 
1551   my $query_lang = "SELECT id, template_code FROM language ORDER BY description";
 
1552   $sth = $dbh->prepare($query_lang);
 
1553   $sth->execute() || $form->dberror($query_lang);
 
1555   while (my $ref = $sth->fetchrow_hashref()) {
 
1556     push(@languages, $ref);
 
1560   $query_lang = "SELECT ul.localized, ul.localized_plural, l.id, l.template_code " .
 
1561     "FROM units_language ul " .
 
1562     "LEFT JOIN language l ON ul.language_id = l.id " .
 
1563     "WHERE ul.unit = ?";
 
1564   $sth = $dbh->prepare($query_lang);
 
1566   foreach my $unit (values(%{$units})) {
 
1567     ($unit->{"${prefix}base_unit"}, $unit->{"${prefix}factor"}) = AM->get_base_unit($units, $unit->{"name"});
 
1569     $unit->{"LANGUAGES"} = {};
 
1570     foreach my $lang (@languages) {
 
1571       $unit->{"LANGUAGES"}->{$lang->{"template_code"}} = { "template_code" => $lang->{"template_code"} };
 
1574     $sth->execute($unit->{"name"}) || $form->dberror($query_lang . " (" . $unit->{"name"} . ")");
 
1575     while (my $ref = $sth->fetchrow_hashref()) {
 
1576       map({ $unit->{"LANGUAGES"}->{$ref->{"template_code"}}->{$_} = $ref->{$_} } keys(%{$ref}));
 
1581   $main::lxdebug->leave_sub();
 
1586 sub retrieve_all_units {
 
1587   $main::lxdebug->enter_sub();
 
1591   if (!$main::all_units) {
 
1592     $main::all_units = $self->retrieve_units(\%main::myconfig, $main::form);
 
1595   $main::lxdebug->leave_sub();
 
1597   return $main::all_units;
 
1601 sub translate_units {
 
1602   $main::lxdebug->enter_sub();
 
1604   my ($self, $form, $template_code, $unit, $amount) = @_;
 
1606   my $units = $self->retrieve_units(\%main::myconfig, $form);
 
1608   my $h = $units->{$unit}->{"LANGUAGES"}->{$template_code};
 
1609   my $new_unit = $unit;
 
1611     if (($amount != 1) && $h->{"localized_plural"}) {
 
1612       $new_unit = $h->{"localized_plural"};
 
1613     } elsif ($h->{"localized"}) {
 
1614       $new_unit = $h->{"localized"};
 
1618   $main::lxdebug->leave_sub();
 
1624   $main::lxdebug->enter_sub();
 
1626   my ($self, $myconfig, $form, $units) = @_;
 
1628   my $dbh = $form->dbconnect($myconfig);
 
1630   map({ $_->{"in_use"} = 0; } values(%{$units}));
 
1632   foreach my $unit (values(%{$units})) {
 
1633     my $base_unit = $unit->{"original_base_unit"};
 
1634     while ($base_unit) {
 
1635       $units->{$base_unit}->{"in_use"} = 1;
 
1636       $units->{$base_unit}->{"DEPENDING_UNITS"} = [] unless ($units->{$base_unit}->{"DEPENDING_UNITS"});
 
1637       push(@{$units->{$base_unit}->{"DEPENDING_UNITS"}}, $unit->{"name"});
 
1638       $base_unit = $units->{$base_unit}->{"original_base_unit"};
 
1642   foreach my $unit (values(%{$units})) {
 
1643     map({ $_ = $dbh->quote($_); } @{$unit->{"DEPENDING_UNITS"}});
 
1645     foreach my $table (qw(parts invoice orderitems)) {
 
1646       my $query = "SELECT COUNT(*) FROM $table WHERE unit ";
 
1648       if (0 == scalar(@{$unit->{"DEPENDING_UNITS"}})) {
 
1649         $query .= "= " . $dbh->quote($unit->{"name"});
 
1651         $query .= "IN (" . $dbh->quote($unit->{"name"}) . "," .
 
1652           join(",", map({ $dbh->quote($_) } @{$unit->{"DEPENDING_UNITS"}})) . ")";
 
1655       my ($count) = $dbh->selectrow_array($query);
 
1656       $form->dberror($query) if ($dbh->err);
 
1659         $unit->{"in_use"} = 1;
 
1667   $main::lxdebug->leave_sub();
 
1670 sub convertible_units {
 
1671   $main::lxdebug->enter_sub();
 
1675   my $filter_unit = shift;
 
1676   my $not_smaller = shift;
 
1678   my $conv_units = [];
 
1680   $filter_unit = $units->{$filter_unit};
 
1682   foreach my $name (sort { lc $a cmp lc $b } keys %{ $units }) {
 
1683     my $unit = $units->{$name};
 
1685     if (($unit->{base_unit} eq $filter_unit->{base_unit}) &&
 
1686         (!$not_smaller || ($unit->{factor} >= $filter_unit->{factor}))) {
 
1687       push @{$conv_units}, $unit;
 
1691   my @sorted = sort { $b->{factor} <=> $a->{factor} } @{ $conv_units };
 
1693   $main::lxdebug->leave_sub();
 
1698 # if $a is translatable to $b, return the factor between them.
 
1701   $main::lxdebug->enter_sub(2);
 
1702   my ($this, $a, $b, $all_units) = @_;
 
1704   $main::lxdebug->leave_sub(2) and return 0 unless $a && $b;
 
1705   $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a} && $all_units->{$b};
 
1706   $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a}{base_unit} eq $all_units->{$b}{base_unit};
 
1707   $main::lxdebug->leave_sub(2) and return $all_units->{$a}{factor} / $all_units->{$b}{factor};
 
1710 sub unit_select_data {
 
1711   $main::lxdebug->enter_sub();
 
1713   my ($self, $units, $selected, $empty_entry, $convertible_into) = @_;
 
1718     push(@{$select}, { "name" => "", "base_unit" => "", "factor" => "", "selected" => "" });
 
1721   foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
 
1722     if (!$convertible_into ||
 
1723         ($units->{$convertible_into} &&
 
1724          ($units->{$convertible_into}->{base_unit} eq $units->{$unit}->{base_unit}))) {
 
1725       push @{$select}, { "name"      => $unit,
 
1726                          "base_unit" => $units->{$unit}->{"base_unit"},
 
1727                          "factor"    => $units->{$unit}->{"factor"},
 
1728                          "selected"  => ($unit eq $selected) ? "selected" : "" };
 
1732   $main::lxdebug->leave_sub();
 
1737 sub unit_select_html {
 
1738   $main::lxdebug->enter_sub();
 
1740   my ($self, $units, $name, $selected, $convertible_into) = @_;
 
1742   my $select = "<select name=${name}>";
 
1744   foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
 
1745     if (!$convertible_into ||
 
1746         ($units->{$convertible_into} &&
 
1747          ($units->{$convertible_into}->{"base_unit"} eq $units->{$unit}->{"base_unit"}))) {
 
1748       $select .= "<option" . (($unit eq $selected) ? " selected" : "") . ">${unit}</option>";
 
1751   $select .= "</select>";
 
1753   $main::lxdebug->leave_sub();
 
1759   $main::lxdebug->enter_sub();
 
1763   my $units = $self->retrieve_all_units();
 
1768   while (2 <= scalar(@_)) {
 
1769     my $qty  = shift(@_);
 
1770     my $unit = $units->{shift(@_)};
 
1772     croak "No unit defined with name $unit" if (!defined $unit);
 
1775       $base_unit = $unit->{base_unit};
 
1776     } elsif ($base_unit ne $unit->{base_unit}) {
 
1777       croak "Adding values with incompatible base units $base_unit/$unit->{base_unit}";
 
1780     $sum += $qty * $unit->{factor};
 
1783   $main::lxdebug->leave_sub();
 
1785   return wantarray ? ($sum, $base_unit) : $sum;
 
1789   $main::lxdebug->enter_sub();
 
1791   my ($self, $myconfig, $form, $name, $base_unit, $factor, $languages) = @_;
 
1793   my $dbh = $form->dbconnect_noauto($myconfig);
 
1795   my $query = qq|SELECT COALESCE(MAX(sortkey), 0) + 1 FROM units|;
 
1796   my ($sortkey) = selectrow_query($form, $dbh, $query);
 
1798   $query = "INSERT INTO units (name, base_unit, factor, sortkey) " .
 
1799     "VALUES (?, ?, ?, ?)";
 
1800   do_query($form, $dbh, $query, $name, $base_unit, $factor, $sortkey);
 
1803     $query = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
 
1804     my $sth = $dbh->prepare($query);
 
1805     foreach my $lang (@{$languages}) {
 
1806       my @values = ($name, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
 
1807       $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
 
1815   $main::lxdebug->leave_sub();
 
1819   $main::lxdebug->enter_sub();
 
1821   my ($self, $myconfig, $form, $units, $delete_units) = @_;
 
1823   my $dbh = $form->dbconnect_noauto($myconfig);
 
1825   my ($base_unit, $unit, $sth, $query);
 
1827   $query = "DELETE FROM units_language";
 
1828   $dbh->do($query) || $form->dberror($query);
 
1830   if ($delete_units && (0 != scalar(@{$delete_units}))) {
 
1831     $query = "DELETE FROM units WHERE name IN (";
 
1832     map({ $query .= "?," } @{$delete_units});
 
1833     substr($query, -1, 1) = ")";
 
1834     $dbh->do($query, undef, @{$delete_units}) ||
 
1835       $form->dberror($query . " (" . join(", ", @{$delete_units}) . ")");
 
1838   $query = "UPDATE units SET name = ?, base_unit = ?, factor = ? WHERE name = ?";
 
1839   $sth = $dbh->prepare($query);
 
1841   my $query_lang = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
 
1842   my $sth_lang = $dbh->prepare($query_lang);
 
1844   foreach $unit (values(%{$units})) {
 
1845     $unit->{"depth"} = 0;
 
1846     my $base_unit = $unit;
 
1847     while ($base_unit->{"base_unit"}) {
 
1849       $base_unit = $units->{$base_unit->{"base_unit"}};
 
1853   foreach $unit (sort({ $a->{"depth"} <=> $b->{"depth"} } values(%{$units}))) {
 
1854     if ($unit->{"LANGUAGES"}) {
 
1855       foreach my $lang (@{$unit->{"LANGUAGES"}}) {
 
1856         next unless ($lang->{"id"} && $lang->{"localized"});
 
1857         my @values = ($unit->{"name"}, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
 
1858         $sth_lang->execute(@values) || $form->dberror($query_lang . " (" . join(", ", @values) . ")");
 
1862     next if ($unit->{"unchanged_unit"});
 
1864     my @values = ($unit->{"name"}, $unit->{"base_unit"}, $unit->{"factor"}, $unit->{"old_name"});
 
1865     $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
 
1869   $sth_lang->finish();
 
1873   $main::lxdebug->leave_sub();
 
1877   $main::lxdebug->enter_sub();
 
1879   my ($self, $myconfig, $form) = @_;
 
1881   # connect to database
 
1882   my $dbh = $form->dbconnect($myconfig);
 
1884   my $query = qq|SELECT
 
1888                    round(t.rate * 100, 2) AS rate,
 
1889                    (SELECT accno FROM chart WHERE id = chart_id) AS taxnumber,
 
1890                    (SELECT description FROM chart WHERE id = chart_id) AS account_description
 
1894   my $sth = $dbh->prepare($query);
 
1895   $sth->execute || $form->dberror($query);
 
1898   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
1899     push @{ $form->{TAX} }, $ref;
 
1905   $main::lxdebug->leave_sub();
 
1908 sub get_tax_accounts {
 
1909   $main::lxdebug->enter_sub();
 
1911   my ($self, $myconfig, $form) = @_;
 
1913   my $dbh = $form->dbconnect($myconfig);
 
1915   # get Accounts from chart
 
1916   my $query = qq{ SELECT
 
1918                  accno || ' - ' || description AS taxaccount
 
1920                WHERE link LIKE '%_tax%'
 
1924   my $sth = $dbh->prepare($query);
 
1925   $sth->execute || $form->dberror($query);
 
1927   $form->{ACCOUNTS} = [];
 
1928   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
1929     push @{ $form->{ACCOUNTS} }, $ref;
 
1936   $main::lxdebug->leave_sub();
 
1940   $main::lxdebug->enter_sub();
 
1942   my ($self, $myconfig, $form) = @_;
 
1944   # connect to database
 
1945   my $dbh = $form->dbconnect($myconfig);
 
1947   my $query = qq|SELECT
 
1950                    round(rate * 100, 2) AS rate,
 
1955   my $sth = $dbh->prepare($query);
 
1956   $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
1958   my $ref = $sth->fetchrow_hashref("NAME_lc");
 
1960   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
1964   # see if it is used by a taxkey
 
1965   $query = qq|SELECT count(*) FROM taxkeys
 
1966               WHERE tax_id = ? AND chart_id >0|;
 
1968   ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
 
1970   $form->{orphaned} = !$form->{orphaned};
 
1973   if (!$form->{orphaned} ) {
 
1974     $query = qq|SELECT DISTINCT c.id, c.accno
 
1976                 JOIN   tax t ON (t.id = tk.tax_id)
 
1977                 JOIN chart c ON (c.id = tk.chart_id)
 
1978                 WHERE tk.tax_id = ?|;
 
1980     $sth = $dbh->prepare($query);
 
1981     $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
1983     $form->{TAXINUSE} = [];
 
1984     while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
1985       push @{ $form->{TAXINUSE} }, $ref;
 
1993   $main::lxdebug->leave_sub();
 
1997   $main::lxdebug->enter_sub();
 
1999   my ($self, $myconfig, $form) = @_;
 
2002   # connect to database
 
2003   my $dbh = $form->get_standard_dbh($myconfig);
 
2005   $form->{rate} = $form->{rate} / 100;
 
2007   my @values = ($form->{taxkey}, $form->{taxdescription}, $form->{rate}, $form->{chart_id}, $form->{chart_id} );
 
2008   if ($form->{id} ne "") {
 
2009     $query = qq|UPDATE tax SET
 
2014                   taxnumber      = (SELECT accno FROM chart WHERE id= ? )
 
2016     push(@values, $form->{id});
 
2020     $query = qq|INSERT INTO tax (
 
2027                 VALUES (?, ?, ?, ?, (SELECT accno FROM chart WHERE id = ?) )|;
 
2029   do_query($form, $dbh, $query, @values);
 
2033   $main::lxdebug->leave_sub();
 
2037   $main::lxdebug->enter_sub();
 
2039   my ($self, $myconfig, $form) = @_;
 
2042   # connect to database
 
2043   my $dbh = $form->get_standard_dbh($myconfig);
 
2045   $query = qq|DELETE FROM tax
 
2047   do_query($form, $dbh, $query, $form->{id});
 
2051   $main::lxdebug->leave_sub();
 
2054 sub save_price_factor {
 
2055   $main::lxdebug->enter_sub();
 
2057   my ($self, $myconfig, $form) = @_;
 
2059   # connect to database
 
2060   my $dbh = $form->get_standard_dbh($myconfig);
 
2063   my @values = ($form->{description}, conv_i($form->{factor}));
 
2066     $query = qq|UPDATE price_factors SET description = ?, factor = ? WHERE id = ?|;
 
2067     push @values, conv_i($form->{id});
 
2070     $query = qq|INSERT INTO price_factors (description, factor, sortkey) VALUES (?, ?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM price_factors))|;
 
2073   do_query($form, $dbh, $query, @values);
 
2077   $main::lxdebug->leave_sub();
 
2080 sub get_all_price_factors {
 
2081   $main::lxdebug->enter_sub();
 
2083   my ($self, $myconfig, $form) = @_;
 
2085   # connect to database
 
2086   my $dbh = $form->get_standard_dbh($myconfig);
 
2088   $form->{PRICE_FACTORS} = selectall_hashref_query($form, $dbh, qq|SELECT * FROM price_factors ORDER BY sortkey|);
 
2090   $main::lxdebug->leave_sub();
 
2093 sub get_price_factor {
 
2094   $main::lxdebug->enter_sub();
 
2096   my ($self, $myconfig, $form) = @_;
 
2098   # connect to database
 
2099   my $dbh = $form->get_standard_dbh($myconfig);
 
2101   my $query = qq|SELECT description, factor,
 
2102                    ((SELECT COUNT(*) FROM parts      WHERE price_factor_id = ?) +
 
2103                     (SELECT COUNT(*) FROM invoice    WHERE price_factor_id = ?) +
 
2104                     (SELECT COUNT(*) FROM orderitems WHERE price_factor_id = ?)) = 0 AS orphaned
 
2105                  FROM price_factors WHERE id = ?|;
 
2107   ($form->{description}, $form->{factor}, $form->{orphaned}) = selectrow_query($form, $dbh, $query, (conv_i($form->{id})) x 4);
 
2109   $main::lxdebug->leave_sub();
 
2112 sub delete_price_factor {
 
2113   $main::lxdebug->enter_sub();
 
2115   my ($self, $myconfig, $form) = @_;
 
2117   # connect to database
 
2118   my $dbh = $form->get_standard_dbh($myconfig);
 
2120   do_query($form, $dbh, qq|DELETE FROM price_factors WHERE id = ?|, conv_i($form->{id}));
 
2123   $main::lxdebug->leave_sub();
 
2126 sub save_warehouse {
 
2127   $main::lxdebug->enter_sub();
 
2129   my ($self, $myconfig, $form) = @_;
 
2131   # connect to database
 
2132   my $dbh = $form->get_standard_dbh($myconfig);
 
2134   my ($query, @values, $sth);
 
2137     $query        = qq|SELECT nextval('id')|;
 
2138     ($form->{id}) = selectrow_query($form, $dbh, $query);
 
2140     $query        = qq|INSERT INTO warehouse (id, sortkey) VALUES (?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM warehouse))|;
 
2141     do_query($form, $dbh, $query, $form->{id});
 
2144   do_query($form, $dbh, qq|UPDATE warehouse SET description = ?, invalid = ? WHERE id = ?|,
 
2145            $form->{description}, $form->{invalid} ? 't' : 'f', conv_i($form->{id}));
 
2147   if (0 < $form->{number_of_new_bins}) {
 
2148     $query = qq|INSERT INTO bin (warehouse_id, description) VALUES (?, ?)|;
 
2149     $sth   = prepare_query($form, $dbh, $query);
 
2151     foreach my $i (1..$form->{number_of_new_bins}) {
 
2152       do_statement($form, $sth, $query, conv_i($form->{id}), "$form->{prefix}${i}");
 
2160   $main::lxdebug->leave_sub();
 
2164   $main::lxdebug->enter_sub();
 
2166   my ($self, $myconfig, $form) = @_;
 
2168   # connect to database
 
2169   my $dbh = $form->get_standard_dbh($myconfig);
 
2171   my ($query, @values, $commit_necessary, $sth);
 
2173   @values = map { $form->{"id_${_}"} } grep { $form->{"delete_${_}"} } (1..$form->{rowcount});
 
2176     $query = qq|DELETE FROM bin WHERE id IN (| . join(', ', ('?') x scalar(@values)) . qq|)|;
 
2177     do_query($form, $dbh, $query, @values);
 
2179     $commit_necessary = 1;
 
2182   $query = qq|UPDATE bin SET description = ? WHERE id = ?|;
 
2183   $sth   = prepare_query($form, $dbh, $query);
 
2185   foreach my $row (1..$form->{rowcount}) {
 
2186     next if ($form->{"delete_${row}"});
 
2188     do_statement($form, $sth, $query, $form->{"description_${row}"}, conv_i($form->{"id_${row}"}));
 
2190     $commit_necessary = 1;
 
2195   $dbh->commit() if ($commit_necessary);
 
2197   $main::lxdebug->leave_sub();
 
2200 sub delete_warehouse {
 
2201   $main::lxdebug->enter_sub();
 
2203   my ($self, $myconfig, $form) = @_;
 
2205   # connect to database
 
2206   my $dbh = $form->get_standard_dbh($myconfig);
 
2208   my $id      = conv_i($form->{id});
 
2209   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|;
 
2210   my ($count) = selectrow_query($form, $dbh, $query, $id);
 
2213     $main::lxdebug->leave_sub();
 
2217   do_query($form, $dbh, qq|DELETE FROM bin       WHERE warehouse_id = ?|, conv_i($form->{id}));
 
2218   do_query($form, $dbh, qq|DELETE FROM warehouse WHERE id           = ?|, conv_i($form->{id}));
 
2222   $main::lxdebug->leave_sub();
 
2227 sub get_all_warehouses {
 
2228   $main::lxdebug->enter_sub();
 
2230   my ($self, $myconfig, $form) = @_;
 
2232   # connect to database
 
2233   my $dbh = $form->get_standard_dbh($myconfig);
 
2235   my $query = qq|SELECT w.id, w.description, w.invalid,
 
2236                    (SELECT COUNT(b.description) FROM bin b WHERE b.warehouse_id = w.id) AS number_of_bins
 
2238                  ORDER BY w.sortkey|;
 
2240   $form->{WAREHOUSES} = selectall_hashref_query($form, $dbh, $query);
 
2242   $main::lxdebug->leave_sub();
 
2246   $main::lxdebug->enter_sub();
 
2248   my ($self, $myconfig, $form) = @_;
 
2250   # connect to database
 
2251   my $dbh = $form->get_standard_dbh($myconfig);
 
2253   my $id    = conv_i($form->{id});
 
2254   my $query = qq|SELECT w.description, w.invalid
 
2258   my $ref   = selectfirst_hashref_query($form, $dbh, $query, $id);
 
2260   map { $form->{$_} = $ref->{$_} } keys %{ $ref };
 
2262   $query = qq|SELECT b.*, EXISTS
 
2263                 (SELECT i.warehouse_id
 
2265                  WHERE i.bin_id = b.id
 
2269               WHERE b.warehouse_id = ?|;
 
2271   $form->{BINS} = selectall_hashref_query($form, $dbh, $query, conv_i($form->{id}));
 
2273   $main::lxdebug->leave_sub();