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   my ($self, $myconfig, $form) = @_;
 
 204   # connect to database, turn off AutoCommit
 
 205   my $dbh = $form->dbconnect_noauto($myconfig);
 
 207   # sanity check, can't have AR with AR_...
 
 208   if ($form->{AR} || $form->{AP} || $form->{IC}) {
 
 209     map { delete $form->{$_} }
 
 210       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);
 
 214   foreach my $item ($form->{AR},            $form->{AR_amount},
 
 215                     $form->{AR_tax},        $form->{AR_paid},
 
 216                     $form->{AP},            $form->{AP_amount},
 
 217                     $form->{AP_tax},        $form->{AP_paid},
 
 218                     $form->{IC},            $form->{IC_sale},
 
 219                     $form->{IC_cogs},       $form->{IC_taxpart},
 
 220                     $form->{IC_income},     $form->{IC_expense},
 
 221                     $form->{IC_taxservice}, $form->{CT_tax}
 
 223     $form->{link} .= "${item}:" if ($item);
 
 227   # strip blanks from accno
 
 228   map { $form->{$_} =~ s/ //g; } qw(accno);
 
 232   if ($form->{id} eq "NULL") {
 
 236   if (!$form->{id} || $form->{id} eq "") {
 
 237     $query = qq|SELECT nextval('id')|;
 
 238     ($form->{"id"}) = selectrow_query($form, $dbh, $query);
 
 239     $query = qq|INSERT INTO chart (id, accno) VALUES (?, ?)|;
 
 240     do_query($form, $dbh, $query, $form->{"id"}, $form->{"accno"});
 
 246     $query = qq|UPDATE chart SET
 
 262                   $form->{description},
 
 266                   conv_i($form->{pos_bwa}),
 
 267                   conv_i($form->{pos_bilanz}),
 
 268                   conv_i($form->{pos_eur}),
 
 269                   conv_i($form->{new_chart_id}),
 
 270                   conv_date($form->{valid_from}),
 
 271                   ($form->{datevautomatik} eq 'T') ? 'true':'false',
 
 277   do_query($form, $dbh, $query, @values);
 
 283   my $MAX_TRIES = 10; # Maximum count of taxkeys in form
 
 287   for $tk_count (0 .. $MAX_TRIES) {
 
 291     # Check if the account already exists, else cancel
 
 293     print(STDERR "Keine Taxkeys weil ID =: $form->{id}\n");
 
 295     last READTAXKEYS if ( $form->{'id'} == 0);
 
 297     # check if there is a startdate
 
 298     if ( $form->{"taxkey_startdate_$tk_count"} eq '' ) {
 
 303     # Add valid taxkeys into the array
 
 306         id        => ($form->{"taxkey_id_$tk_count"} eq 'NEW') ? conv_i('') : conv_i($form->{"taxkey_id_$tk_count"}),
 
 307         tax_id    => conv_i($form->{"taxkey_tax_$tk_count"}),
 
 308         startdate => conv_date($form->{"taxkey_startdate_$tk_count"}),
 
 309         chart_id  => conv_i($form->{"id"}),
 
 310         pos_ustva => conv_i($form->{"taxkey_pos_ustva_$tk_count"}),
 
 311         delete    => ( $form->{"taxkey_del_$tk_count"} eq 'delete' ) ? '1' : '',
 
 318   for my $j (0 .. $#taxkeys){
 
 319     if ( defined $taxkeys[$j]{'id'} ){
 
 322       if ($taxkeys[$j]{'delete'}){
 
 324           DELETE FROM taxkeys WHERE id = ?
 
 327         @values = ($taxkeys[$j]{'id'});
 
 329         do_query($form, $dbh, $query, @values);
 
 338         SET taxkey_id = (SELECT taxkey FROM tax WHERE tax.id = ?),
 
 346         $taxkeys[$j]{'tax_id'},
 
 347         $taxkeys[$j]{'chart_id'},
 
 348         $taxkeys[$j]{'tax_id'},
 
 349         $taxkeys[$j]{'pos_ustva'},
 
 350         $taxkeys[$j]{'startdate'},
 
 353       do_query($form, $dbh, $query, @values);
 
 359         INSERT INTO taxkeys (
 
 366         VALUES ((SELECT taxkey FROM tax WHERE tax.id = ?), ?, ?, ?, ?)
 
 369         $taxkeys[$j]{'tax_id'},
 
 370         $taxkeys[$j]{'chart_id'},
 
 371         $taxkeys[$j]{'tax_id'},
 
 372         $taxkeys[$j]{'pos_ustva'},
 
 373         $taxkeys[$j]{'startdate'},
 
 376       do_query($form, $dbh, $query, @values);
 
 381   # Update chart.taxkey_id to the latest from taxkeys for this chart.
 
 387       WHERE taxkeys.chart_id = chart.id
 
 388       ORDER BY startdate DESC
 
 394   do_query($form, $dbh, $query, $form->{id});
 
 397   my $rc = $dbh->commit;
 
 400   $main::lxdebug->leave_sub();
 
 406   $main::lxdebug->enter_sub();
 
 408   my ($self, $myconfig, $form) = @_;
 
 410   # connect to database, turn off AutoCommit
 
 411   my $dbh = $form->dbconnect_noauto($myconfig);
 
 413   my $query = qq|SELECT count(*) FROM acc_trans a
 
 414                  WHERE a.chart_id = ?|;
 
 415   my ($count) = selectrow_query($form, $dbh, $query, $form->{id});
 
 419     $main::lxdebug->leave_sub();
 
 423   # set inventory_accno_id, income_accno_id, expense_accno_id to defaults
 
 424   foreach my $type (qw(inventory income expense)) {
 
 427       qq|SET ${type}_accno_id = (SELECT ${type}_accno_id FROM defaults) | .
 
 428       qq|WHERE ${type}_accno_id = ?|;
 
 429     do_query($form, $dbh, $query, $form->{id});
 
 432   foreach my $table (qw(partstax customertax vendortax tax)) {
 
 433     $query = qq|DELETE FROM $table
 
 435     do_query($form, $dbh, $query, $form->{id});
 
 438   # delete chart of account record
 
 439   $query = qq|DELETE FROM chart
 
 441   do_query($form, $dbh, $query, $form->{id});
 
 443   # delete account taxkeys
 
 444   $query = qq|DELETE FROM taxkeys
 
 446   do_query($form, $dbh, $query, $form->{id});
 
 448   # commit and redirect
 
 449   my $rc = $dbh->commit;
 
 452   $main::lxdebug->leave_sub();
 
 458   $main::lxdebug->enter_sub();
 
 460   my ($self, $myconfig, $form) = @_;
 
 462   # connect to database
 
 463   my $dbh = $form->dbconnect($myconfig);
 
 465   my $query = qq|SELECT d.id, d.description, d.role
 
 469   my $sth = $dbh->prepare($query);
 
 470   $sth->execute || $form->dberror($query);
 
 473   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 474     push @{ $form->{ALL} }, $ref;
 
 480   $main::lxdebug->leave_sub();
 
 484   $main::lxdebug->enter_sub();
 
 486   my ($self, $myconfig, $form) = @_;
 
 488   # connect to database
 
 489   my $dbh = $form->dbconnect($myconfig);
 
 491   my $query = qq|SELECT d.description, d.role
 
 494   my $sth = $dbh->prepare($query);
 
 495   $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
 497   my $ref = $sth->fetchrow_hashref("NAME_lc");
 
 499   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
 503   # see if it is in use
 
 504   $query = qq|SELECT count(*) FROM dpt_trans d
 
 505               WHERE d.department_id = ?|;
 
 506   ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
 
 508   $form->{orphaned} = !$form->{orphaned};
 
 513   $main::lxdebug->leave_sub();
 
 516 sub save_department {
 
 517   $main::lxdebug->enter_sub();
 
 519   my ($self, $myconfig, $form) = @_;
 
 522   # connect to database
 
 523   my $dbh = $form->dbconnect($myconfig);
 
 525   my @values = ($form->{description}, $form->{role});
 
 527     $query = qq|UPDATE department SET
 
 528                 description = ?, role = ?
 
 530     push(@values, $form->{id});
 
 532     $query = qq|INSERT INTO department
 
 536   do_query($form, $dbh, $query, @values);
 
 540   $main::lxdebug->leave_sub();
 
 543 sub delete_department {
 
 544   $main::lxdebug->enter_sub();
 
 546   my ($self, $myconfig, $form) = @_;
 
 549   # connect to database
 
 550   my $dbh = $form->dbconnect($myconfig);
 
 552   $query = qq|DELETE FROM department
 
 554   do_query($form, $dbh, $query, $form->{id});
 
 558   $main::lxdebug->leave_sub();
 
 562   $main::lxdebug->enter_sub();
 
 564   my ($self, $myconfig, $form) = @_;
 
 566   # connect to database
 
 567   my $dbh = $form->dbconnect($myconfig);
 
 569   my $query = qq|SELECT id, lead
 
 573   my $sth = $dbh->prepare($query);
 
 574   $sth->execute || $form->dberror($query);
 
 576   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 577     push @{ $form->{ALL} }, $ref;
 
 583   $main::lxdebug->leave_sub();
 
 587   $main::lxdebug->enter_sub();
 
 589   my ($self, $myconfig, $form) = @_;
 
 591   # connect to database
 
 592   my $dbh = $form->dbconnect($myconfig);
 
 595     qq|SELECT l.id, l.lead | .
 
 598   my $sth = $dbh->prepare($query);
 
 599   $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
 601   my $ref = $sth->fetchrow_hashref("NAME_lc");
 
 603   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
 609   $main::lxdebug->leave_sub();
 
 613   $main::lxdebug->enter_sub();
 
 615   my ($self, $myconfig, $form) = @_;
 
 618   # connect to database
 
 619   my $dbh = $form->dbconnect($myconfig);
 
 621   my @values = ($form->{description});
 
 622   # id is the old record
 
 624     $query = qq|UPDATE leads SET
 
 627     puhs(@values, $form->{id});
 
 629     $query = qq|INSERT INTO leads
 
 633   do_query($form, $dbh, $query, @values);
 
 637   $main::lxdebug->leave_sub();
 
 641   $main::lxdebug->enter_sub();
 
 643   my ($self, $myconfig, $form) = @_;
 
 646   # connect to database
 
 647   my $dbh = $form->dbconnect($myconfig);
 
 649   $query = qq|DELETE FROM leads
 
 651   do_query($form, $dbh, $query, $form->{id});
 
 655   $main::lxdebug->leave_sub();
 
 659   $main::lxdebug->enter_sub();
 
 661   my ($self, $myconfig, $form) = @_;
 
 663   # connect to database
 
 664   my $dbh = $form->dbconnect($myconfig);
 
 666   my $query = qq|SELECT id, description, discount, customernumberinit, salesman
 
 670   my $sth = $dbh->prepare($query);
 
 671   $sth->execute || $form->dberror($query);
 
 673   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 674     push @{ $form->{ALL} }, $ref;
 
 680   $main::lxdebug->leave_sub();
 
 684   $main::lxdebug->enter_sub();
 
 686   my ($self, $myconfig, $form) = @_;
 
 688   # connect to database
 
 689   my $dbh = $form->dbconnect($myconfig);
 
 692     qq|SELECT b.description, b.discount, b.customernumberinit, b.salesman
 
 695   my $sth = $dbh->prepare($query);
 
 696   $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
 698   my $ref = $sth->fetchrow_hashref("NAME_lc");
 
 700   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
 706   $main::lxdebug->leave_sub();
 
 710   $main::lxdebug->enter_sub();
 
 712   my ($self, $myconfig, $form) = @_;
 
 715   # connect to database
 
 716   my $dbh = $form->dbconnect($myconfig);
 
 718   my @values = ($form->{description}, $form->{discount}, $form->{customernumberinit}, $form->{salesman} ? 't' : 'f');
 
 719   # id is the old record
 
 721     $query = qq|UPDATE business SET
 
 724                 customernumberinit = ?,
 
 727     push(@values, $form->{id});
 
 729     $query = qq|INSERT INTO business
 
 730                 (description, discount, customernumberinit, salesman)
 
 731                 VALUES (?, ?, ?, ?)|;
 
 733   do_query($form, $dbh, $query, @values);
 
 737   $main::lxdebug->leave_sub();
 
 740 sub delete_business {
 
 741   $main::lxdebug->enter_sub();
 
 743   my ($self, $myconfig, $form) = @_;
 
 745   # connect to database
 
 746   my $dbh = $form->dbconnect($myconfig);
 
 748   my $query = qq|DELETE FROM business
 
 750   do_query($form, $dbh, $query, $form->{id});
 
 754   $main::lxdebug->leave_sub();
 
 759   $main::lxdebug->enter_sub();
 
 761   my ($self, $myconfig, $form, $return_list) = @_;
 
 763   # connect to database
 
 764   my $dbh = $form->dbconnect($myconfig);
 
 767     "SELECT id, description, template_code, article_code, " .
 
 768     "  output_numberformat, output_dateformat, output_longdates " .
 
 769     "FROM language ORDER BY description";
 
 771   my $sth = $dbh->prepare($query);
 
 772   $sth->execute || $form->dberror($query);
 
 776   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 777     push(@{ $ary }, $ref);
 
 783   $main::lxdebug->leave_sub();
 
 793   $main::lxdebug->enter_sub();
 
 795   my ($self, $myconfig, $form) = @_;
 
 797   # connect to database
 
 798   my $dbh = $form->dbconnect($myconfig);
 
 801     "SELECT description, template_code, article_code, " .
 
 802     "  output_numberformat, output_dateformat, output_longdates " .
 
 803     "FROM language WHERE id = ?";
 
 804   my $sth = $dbh->prepare($query);
 
 805   $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
 
 807   my $ref = $sth->fetchrow_hashref("NAME_lc");
 
 809   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
 815   $main::lxdebug->leave_sub();
 
 818 sub get_language_details {
 
 819   $main::lxdebug->enter_sub();
 
 821   my ($self, $myconfig, $form, $id) = @_;
 
 823   # connect to database
 
 824   my $dbh = $form->dbconnect($myconfig);
 
 827     "SELECT template_code, " .
 
 828     "  output_numberformat, output_dateformat, output_longdates " .
 
 829     "FROM language WHERE id = ?";
 
 830   my @res = selectrow_query($form, $dbh, $query, $id);
 
 833   $main::lxdebug->leave_sub();
 
 839   $main::lxdebug->enter_sub();
 
 841   my ($self, $myconfig, $form) = @_;
 
 843   # connect to database
 
 844   my $dbh = $form->dbconnect($myconfig);
 
 845   my (@values, $query);
 
 847   map({ push(@values, $form->{$_}); }
 
 848       qw(description template_code article_code
 
 849          output_numberformat output_dateformat output_longdates));
 
 851   # id is the old record
 
 854       "UPDATE language SET " .
 
 855       "  description = ?, template_code = ?, article_code = ?, " .
 
 856       "  output_numberformat = ?, output_dateformat = ?, " .
 
 857       "  output_longdates = ? " .
 
 859     push(@values, $form->{id});
 
 862       "INSERT INTO language (" .
 
 863       "  description, template_code, article_code, " .
 
 864       "  output_numberformat, output_dateformat, output_longdates" .
 
 865       ") VALUES (?, ?, ?, ?, ?, ?)";
 
 867   do_query($form, $dbh, $query, @values);
 
 871   $main::lxdebug->leave_sub();
 
 874 sub delete_language {
 
 875   $main::lxdebug->enter_sub();
 
 877   my ($self, $myconfig, $form) = @_;
 
 880   # connect to database
 
 881   my $dbh = $form->dbconnect_noauto($myconfig);
 
 883   foreach my $table (qw(translation_payment_terms units_language)) {
 
 884     $query = qq|DELETE FROM $table WHERE language_id = ?|;
 
 885     do_query($form, $dbh, $query, $form->{"id"});
 
 888   $query = "DELETE FROM language WHERE id = ?";
 
 889   do_query($form, $dbh, $query, $form->{"id"});
 
 894   $main::lxdebug->leave_sub();
 
 899   $main::lxdebug->enter_sub();
 
 901   my ($self, $myconfig, $form) = @_;
 
 903   # connect to database
 
 904   my $dbh = $form->dbconnect($myconfig);
 
 906   my $query = qq|SELECT id, description,
 
 908                  (SELECT accno FROM chart WHERE id = inventory_accno_id) AS inventory_accno,
 
 910                  (SELECT accno FROM chart WHERE id = income_accno_id_0) AS income_accno_0,
 
 912                  (SELECT accno FROM chart WHERE id = expense_accno_id_0) AS expense_accno_0,
 
 914                  (SELECT accno FROM chart WHERE id = income_accno_id_1) AS income_accno_1,
 
 916                  (SELECT accno FROM chart WHERE id = expense_accno_id_1) AS expense_accno_1,
 
 918                  (SELECT accno FROM chart WHERE id = income_accno_id_2) AS income_accno_2,
 
 920                  (select accno FROM chart WHERE id = expense_accno_id_2) AS expense_accno_2,
 
 922                  (SELECT accno FROM chart WHERE id = income_accno_id_3) AS income_accno_3,
 
 924                  (SELECT accno FROM chart WHERE id = expense_accno_id_3) AS expense_accno_3
 
 928   my $sth = $dbh->prepare($query);
 
 929   $sth->execute || $form->dberror($query);
 
 932   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 933     push @{ $form->{ALL} }, $ref;
 
 939   $main::lxdebug->leave_sub();
 
 942 sub get_buchungsgruppe {
 
 943   $main::lxdebug->enter_sub();
 
 945   my ($self, $myconfig, $form) = @_;
 
 948   # connect to database
 
 949   my $dbh = $form->dbconnect($myconfig);
 
 953       qq|SELECT description, inventory_accno_id,
 
 954          (SELECT accno FROM chart WHERE id = inventory_accno_id) AS inventory_accno,
 
 956          (SELECT accno FROM chart WHERE id = income_accno_id_0) AS income_accno_0,
 
 958          (SELECT accno FROM chart WHERE id = expense_accno_id_0) AS expense_accno_0,
 
 960          (SELECT accno FROM chart WHERE id = income_accno_id_1) AS income_accno_1,
 
 962          (SELECT accno FROM chart WHERE id = expense_accno_id_1) AS expense_accno_1,
 
 964          (SELECT accno FROM chart WHERE id = income_accno_id_2) AS income_accno_2,
 
 966          (select accno FROM chart WHERE id = expense_accno_id_2) AS expense_accno_2,
 
 968          (SELECT accno FROM chart WHERE id = income_accno_id_3) AS income_accno_3,
 
 970          (SELECT accno FROM chart WHERE id = expense_accno_id_3) AS expense_accno_3
 
 973     my $sth = $dbh->prepare($query);
 
 974     $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
 976     my $ref = $sth->fetchrow_hashref("NAME_lc");
 
 978     map { $form->{$_} = $ref->{$_} } keys %$ref;
 
 983       qq|SELECT count(id) = 0 AS orphaned
 
 985          WHERE buchungsgruppen_id = ?|;
 
 986     ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
 
 989   $query = "SELECT inventory_accno_id, income_accno_id, expense_accno_id ".
 
 991   ($form->{"std_inventory_accno_id"}, $form->{"std_income_accno_id"},
 
 992    $form->{"std_expense_accno_id"}) = selectrow_query($form, $dbh, $query);
 
 995   $query = qq|SELECT c.accno, c.description, c.link, c.id,
 
 996               d.inventory_accno_id, d.income_accno_id, d.expense_accno_id
 
 997               FROM chart c, defaults d
 
 998               WHERE c.link LIKE '%$module%'
 
1002   my $sth = $dbh->prepare($query);
 
1003   $sth->execute || $form->dberror($query);
 
1004   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
1005     foreach my $key (split(/:/, $ref->{link})) {
 
1006       if (!$form->{"std_inventory_accno_id"} && ($key eq "IC")) {
 
1007         $form->{"std_inventory_accno_id"} = $ref->{"id"};
 
1009       if ($key =~ /$module/) {
 
1010         if (   ($ref->{id} eq $ref->{inventory_accno_id})
 
1011             || ($ref->{id} eq $ref->{income_accno_id})
 
1012             || ($ref->{id} eq $ref->{expense_accno_id})) {
 
1013           push @{ $form->{"${module}_links"}{$key} },
 
1014             { accno       => $ref->{accno},
 
1015               description => $ref->{description},
 
1016               selected    => "selected",
 
1019           push @{ $form->{"${module}_links"}{$key} },
 
1020             { accno       => $ref->{accno},
 
1021               description => $ref->{description},
 
1033   $main::lxdebug->leave_sub();
 
1036 sub save_buchungsgruppe {
 
1037   $main::lxdebug->enter_sub();
 
1039   my ($self, $myconfig, $form) = @_;
 
1041   # connect to database
 
1042   my $dbh = $form->dbconnect($myconfig);
 
1044   my @values = ($form->{description}, $form->{inventory_accno_id},
 
1045                 $form->{income_accno_id_0}, $form->{expense_accno_id_0},
 
1046                 $form->{income_accno_id_1}, $form->{expense_accno_id_1},
 
1047                 $form->{income_accno_id_2}, $form->{expense_accno_id_2},
 
1048                 $form->{income_accno_id_3}, $form->{expense_accno_id_3});
 
1052   # id is the old record
 
1054     $query = qq|UPDATE buchungsgruppen SET
 
1055                 description = ?, inventory_accno_id = ?,
 
1056                 income_accno_id_0 = ?, expense_accno_id_0 = ?,
 
1057                 income_accno_id_1 = ?, expense_accno_id_1 = ?,
 
1058                 income_accno_id_2 = ?, expense_accno_id_2 = ?,
 
1059                 income_accno_id_3 = ?, expense_accno_id_3 = ?
 
1061     push(@values, $form->{id});
 
1063     $query = qq|SELECT COALESCE(MAX(sortkey) + 1, 1) FROM buchungsgruppen|;
 
1064     my ($sortkey) = $dbh->selectrow_array($query);
 
1065     $form->dberror($query) if ($dbh->err);
 
1066     push(@values, $sortkey);
 
1067     $query = qq|INSERT INTO buchungsgruppen
 
1068                 (description, inventory_accno_id,
 
1069                 income_accno_id_0, expense_accno_id_0,
 
1070                 income_accno_id_1, expense_accno_id_1,
 
1071                 income_accno_id_2, expense_accno_id_2,
 
1072                 income_accno_id_3, expense_accno_id_3,
 
1074                 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
 
1076   do_query($form, $dbh, $query, @values);
 
1080   $main::lxdebug->leave_sub();
 
1083 sub delete_buchungsgruppe {
 
1084   $main::lxdebug->enter_sub();
 
1086   my ($self, $myconfig, $form) = @_;
 
1088   # connect to database
 
1089   my $dbh = $form->dbconnect($myconfig);
 
1091   my $query = qq|DELETE FROM buchungsgruppen WHERE id = ?|;
 
1092   do_query($form, $dbh, $query, $form->{id});
 
1096   $main::lxdebug->leave_sub();
 
1100   $main::lxdebug->enter_sub();
 
1102   my ($self, $myconfig, $form, $table) = @_;
 
1104   # connect to database
 
1105   my $dbh = $form->get_standard_dbh($myconfig);
 
1109        (SELECT sortkey FROM $table WHERE id = ?) AS sortkey1,
 
1110        (SELECT sortkey FROM $table WHERE id = ?) AS sortkey2|;
 
1111   my @values   = ($form->{"id1"}, $form->{"id2"});
 
1112   my @sortkeys = selectrow_query($form, $dbh, $query, @values);
 
1114   $query  = qq|UPDATE $table SET sortkey = ? WHERE id = ?|;
 
1115   my $sth = prepare_query($form, $dbh, $query);
 
1117   do_statement($form, $sth, $query, $sortkeys[1], $form->{"id1"});
 
1118   do_statement($form, $sth, $query, $sortkeys[0], $form->{"id2"});
 
1124   $main::lxdebug->leave_sub();
 
1128   $main::lxdebug->enter_sub();
 
1130   my ($self, $myconfig, $form) = @_;
 
1132   # connect to database
 
1133   my $dbh = $form->dbconnect($myconfig);
 
1135   my $query = qq|SELECT * FROM payment_terms ORDER BY sortkey|;
 
1137   my $sth = $dbh->prepare($query);
 
1138   $sth->execute || $form->dberror($query);
 
1141   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
1142     push @{ $form->{ALL} }, $ref;
 
1148   $main::lxdebug->leave_sub();
 
1152   $main::lxdebug->enter_sub();
 
1154   my ($self, $myconfig, $form) = @_;
 
1156   # connect to database
 
1157   my $dbh = $form->dbconnect($myconfig);
 
1159   my $query = qq|SELECT * FROM payment_terms WHERE id = ?|;
 
1160   my $sth = $dbh->prepare($query);
 
1161   $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
 
1163   my $ref = $sth->fetchrow_hashref("NAME_lc");
 
1164   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
1168     qq|SELECT t.language_id, t.description_long, l.description AS language | .
 
1169     qq|FROM translation_payment_terms t | .
 
1170     qq|LEFT JOIN language l ON t.language_id = l.id | .
 
1171     qq|WHERE t.payment_terms_id = ? | .
 
1173     qq|SELECT l.id AS language_id, NULL AS description_long, | .
 
1174     qq|  l.description AS language | .
 
1175     qq|FROM language l|;
 
1176   $sth = $dbh->prepare($query);
 
1177   $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
 
1180   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
1181     $mapping{ $ref->{"language_id"} } = $ref
 
1182       unless (defined($mapping{ $ref->{"language_id"} }));
 
1186   $form->{"TRANSLATION"} = [sort({ $a->{"language"} cmp $b->{"language"} }
 
1191   $main::lxdebug->leave_sub();
 
1195   $main::lxdebug->enter_sub();
 
1197   my ($self, $myconfig, $form) = @_;
 
1199   # connect to database
 
1200   my $dbh = $form->dbconnect_noauto($myconfig);
 
1205     $query = qq|SELECT nextval('id'), COALESCE(MAX(sortkey) + 1, 1) | .
 
1206       qq|FROM payment_terms|;
 
1208     ($form->{id}, $sortkey) = selectrow_query($form, $dbh, $query);
 
1210     $query = qq|INSERT INTO payment_terms (id, sortkey) VALUES (?, ?)|;
 
1211     do_query($form, $dbh, $query, $form->{id}, $sortkey);
 
1215       qq|DELETE FROM translation_payment_terms | .
 
1216       qq|WHERE payment_terms_id = ?|;
 
1217     do_query($form, $dbh, $query, $form->{"id"});
 
1220   $query = qq|UPDATE payment_terms SET
 
1221               description = ?, description_long = ?,
 
1222               terms_netto = ?, terms_skonto = ?,
 
1225   my @values = ($form->{description}, $form->{description_long},
 
1226                 $form->{terms_netto} * 1, $form->{terms_skonto} * 1,
 
1227                 $form->{percent_skonto} * 1,
 
1229   do_query($form, $dbh, $query, @values);
 
1231   $query = qq|SELECT id FROM language|;
 
1233   my $sth = $dbh->prepare($query);
 
1234   $sth->execute() || $form->dberror($query);
 
1236   while (my ($id) = $sth->fetchrow_array()) {
 
1237     push(@language_ids, $id);
 
1242     qq|INSERT INTO translation_payment_terms | .
 
1243     qq|(language_id, payment_terms_id, description_long) | .
 
1244     qq|VALUES (?, ?, ?)|;
 
1245   $sth = $dbh->prepare($query);
 
1247   foreach my $language_id (@language_ids) {
 
1248     do_statement($form, $sth, $query, $language_id, $form->{"id"},
 
1249                  $form->{"description_long_${language_id}"});
 
1256   $main::lxdebug->leave_sub();
 
1259 sub delete_payment {
 
1260   $main::lxdebug->enter_sub();
 
1262   my ($self, $myconfig, $form) = @_;
 
1264   # connect to database
 
1265   my $dbh = $form->dbconnect_noauto($myconfig);
 
1268     qq|DELETE FROM translation_payment_terms WHERE payment_terms_id = ?|;
 
1269   do_query($form, $dbh, $query, $form->{"id"});
 
1271   $query = qq|DELETE FROM payment_terms WHERE id = ?|;
 
1272   do_query($form, $dbh, $query, $form->{"id"});
 
1277   $main::lxdebug->leave_sub();
 
1281 sub prepare_template_filename {
 
1282   $main::lxdebug->enter_sub();
 
1284   my ($self, $myconfig, $form) = @_;
 
1286   my ($filename, $display_filename);
 
1288   if ($form->{type} eq "stylesheet") {
 
1289     $filename = "css/$myconfig->{stylesheet}";
 
1290     $display_filename = $myconfig->{stylesheet};
 
1293     $filename = $form->{formname};
 
1295     if ($form->{language}) {
 
1296       my ($id, $template_code) = split(/--/, $form->{language});
 
1297       $filename .= "_${template_code}";
 
1300     if ($form->{printer}) {
 
1301       my ($id, $template_code) = split(/--/, $form->{printer});
 
1302       $filename .= "_${template_code}";
 
1305     $filename .= "." . ($form->{format} eq "html" ? "html" : "tex");
 
1306     $filename =~ s|.*/||;
 
1307     $display_filename = $filename;
 
1308     $filename = "$myconfig->{templates}/$filename";
 
1311   $main::lxdebug->leave_sub();
 
1313   return ($filename, $display_filename);
 
1318   $main::lxdebug->enter_sub();
 
1320   my ($self, $filename) = @_;
 
1322   my ($content, $lines) = ("", 0);
 
1326   if (open(TEMPLATE, $filename)) {
 
1327     while (<TEMPLATE>) {
 
1334   $content = Encode::decode('utf-8-strict', $content) if $::locale->is_utf8;
 
1336   $main::lxdebug->leave_sub();
 
1338   return ($content, $lines);
 
1342   $main::lxdebug->enter_sub();
 
1344   my ($self, $filename, $content) = @_;
 
1350   if (open(TEMPLATE, ">$filename")) {
 
1351     $content = Encode::encode('utf-8-strict', $content) if $::locale->is_utf8;
 
1352     $content =~ s/\r\n/\n/g;
 
1353     print(TEMPLATE $content);
 
1359   $main::lxdebug->leave_sub();
 
1365   $main::lxdebug->enter_sub();
 
1370   my $myconfig = \%main::myconfig;
 
1371   my $form     = $main::form;
 
1373   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
 
1376   map { ($accnos{$_}) = split(m/--/, $form->{$_}) } qw(inventory_accno income_accno expense_accno fxgain_accno fxloss_accno ar_paid_accno);
 
1378   $form->{curr}  =~ s/ //g;
 
1379   my @currencies =  grep { $_ ne '' } split m/:/, $form->{curr};
 
1380   my $currency   =  join ':', @currencies;
 
1382   # these defaults are database wide
 
1385     qq|UPDATE defaults SET
 
1386         inventory_accno_id = (SELECT c.id FROM chart c WHERE c.accno = ?),
 
1387         income_accno_id    = (SELECT c.id FROM chart c WHERE c.accno = ?),
 
1388         expense_accno_id   = (SELECT c.id FROM chart c WHERE c.accno = ?),
 
1389         fxgain_accno_id    = (SELECT c.id FROM chart c WHERE c.accno = ?),
 
1390         fxloss_accno_id    = (SELECT c.id FROM chart c WHERE c.accno = ?),
 
1391         ar_paid_accno_id   = (SELECT c.id FROM chart c WHERE c.accno = ?),
 
1407   my @values = ($accnos{inventory_accno}, $accnos{income_accno}, $accnos{expense_accno},
 
1408                 $accnos{fxgain_accno},    $accnos{fxloss_accno}, $accnos{ar_paid_accno},
 
1409                 $form->{invnumber},       $form->{cnnumber},
 
1410                 $form->{sonumber},        $form->{ponumber},
 
1411                 $form->{sqnumber},        $form->{rfqnumber},
 
1412                 $form->{customernumber},  $form->{vendornumber},
 
1413                 $form->{articlenumber},   $form->{servicenumber},
 
1414                 $form->{sdonumber},       $form->{pdonumber},
 
1416                 $form->{businessnumber},  $form->{weightunit});
 
1417   do_query($form, $dbh, $query, @values);
 
1421   $main::lxdebug->leave_sub();
 
1425 sub save_preferences {
 
1426   $main::lxdebug->enter_sub();
 
1428   my ($self, $myconfig, $form) = @_;
 
1430   my $dbh = $form->get_standard_dbh($myconfig);
 
1432   my ($currency, $businessnumber) = selectrow_query($form, $dbh, qq|SELECT curr, businessnumber FROM defaults|);
 
1435   my $query = qq|UPDATE employee SET name = ? WHERE login = ?|;
 
1436   do_query($form, $dbh, $query, $form->{name}, $form->{login});
 
1438   my $rc = $dbh->commit();
 
1440   # save first currency in myconfig
 
1441   $currency               =~ s/:.*//;
 
1442   $form->{currency}       =  $currency;
 
1444   $form->{businessnumber} =  $businessnumber;
 
1446   $myconfig = new User($form->{login});
 
1448   foreach my $item (keys %$form) {
 
1449     $myconfig->{$item} = $form->{$item};
 
1452   $myconfig->save_member;
 
1454   my $auth = $main::auth;
 
1456   $main::lxdebug->leave_sub();
 
1462   $main::lxdebug->enter_sub();
 
1467   my $myconfig = \%main::myconfig;
 
1468   my $form     = $main::form;
 
1470   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
 
1472   my $defaults = selectfirst_hashref_query($form, $dbh, qq|SELECT * FROM defaults|) || {};
 
1474   $defaults->{weightunit} ||= 'kg';
 
1476   $main::lxdebug->leave_sub();
 
1481 sub defaultaccounts {
 
1482   $main::lxdebug->enter_sub();
 
1484   my ($self, $myconfig, $form) = @_;
 
1486   # connect to database
 
1487   my $dbh = $form->dbconnect($myconfig);
 
1489   # get defaults from defaults table
 
1490   my $query = qq|SELECT * FROM defaults|;
 
1491   my $sth   = $dbh->prepare($query);
 
1492   $sth->execute || $form->dberror($query);
 
1494   $form->{defaults}               = $sth->fetchrow_hashref("NAME_lc");
 
1495   $form->{defaults}{IC}           = $form->{defaults}{inventory_accno_id};
 
1496   $form->{defaults}{IC_income}    = $form->{defaults}{income_accno_id};
 
1497   $form->{defaults}{IC_expense}   = $form->{defaults}{expense_accno_id};
 
1498   $form->{defaults}{FX_gain}      = $form->{defaults}{fxgain_accno_id};
 
1499   $form->{defaults}{FX_loss}      = $form->{defaults}{fxloss_accno_id};
 
1500   $form->{defaults}{AR_paid}      = $form->{defaults}{ar_paid_accno_id};
 
1502   $form->{defaults}{weightunit} ||= 'kg';
 
1506   $query = qq|SELECT c.id, c.accno, c.description, c.link
 
1508               WHERE c.link LIKE '%IC%'
 
1510   $sth = $dbh->prepare($query);
 
1511   $sth->execute || $self->dberror($query);
 
1513   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
1514     foreach my $key (split(/:/, $ref->{link})) {
 
1517         if ($key =~ /cogs/) {
 
1518           $nkey = "IC_expense";
 
1520         if ($key =~ /sale/) {
 
1521           $nkey = "IC_income";
 
1523         %{ $form->{IC}{$nkey}{ $ref->{accno} } } = (
 
1525                                              description => $ref->{description}
 
1532   $query = qq|SELECT c.id, c.accno, c.description
 
1534               WHERE c.category = 'I'
 
1535               AND c.charttype = 'A'
 
1537   $sth = $dbh->prepare($query);
 
1538   $sth->execute || $self->dberror($query);
 
1540   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
1541     %{ $form->{IC}{FX_gain}{ $ref->{accno} } } = (
 
1543                                              description => $ref->{description}
 
1548   $query = qq|SELECT c.id, c.accno, c.description
 
1550               WHERE c.category = 'E'
 
1551               AND c.charttype = 'A'
 
1553   $sth = $dbh->prepare($query);
 
1554   $sth->execute || $self->dberror($query);
 
1556   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
1557     %{ $form->{IC}{FX_loss}{ $ref->{accno} } } = (
 
1559                                              description => $ref->{description}
 
1564   # now get the tax rates and numbers
 
1565   $query = qq|SELECT c.id, c.accno, c.description,
 
1566               t.rate * 100 AS rate, t.taxnumber
 
1568               WHERE c.id = t.chart_id|;
 
1570   $sth = $dbh->prepare($query);
 
1571   $sth->execute || $form->dberror($query);
 
1573   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
1574     $form->{taxrates}{ $ref->{accno} }{id}          = $ref->{id};
 
1575     $form->{taxrates}{ $ref->{accno} }{description} = $ref->{description};
 
1576     $form->{taxrates}{ $ref->{accno} }{taxnumber}   = $ref->{taxnumber}
 
1577       if $ref->{taxnumber};
 
1578     $form->{taxrates}{ $ref->{accno} }{rate} = $ref->{rate} if $ref->{rate};
 
1580   # Abfrage für Standard Umlaufvermögenskonto
 
1582     qq|SELECT id, accno, description, link | .
 
1584     qq|WHERE link LIKE ? |.
 
1586   $sth = prepare_execute_query($form, $dbh, $query, '%AR%');
 
1587   $sth->execute || $form->dberror($query);#
 
1588   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
1589     foreach my $item (split(/:/, $ref->{link})) {
 
1590       if ($item eq "AR_paid") {
 
1591         %{ $form->{IC}{AR_paid}{ $ref->{accno} } } = (
 
1593                                              description => $ref->{description}
 
1602   $main::lxdebug->leave_sub();
 
1606   $main::lxdebug->enter_sub();
 
1608   my ($self, $myconfig, $form) = @_;
 
1610   my $dbh = $form->dbconnect($myconfig);
 
1612   my $query = qq|SELECT closedto, revtrans FROM defaults|;
 
1613   my $sth   = $dbh->prepare($query);
 
1614   $sth->execute || $form->dberror($query);
 
1616   ($form->{closedto}, $form->{revtrans}) = $sth->fetchrow_array;
 
1622   $main::lxdebug->leave_sub();
 
1626   $main::lxdebug->enter_sub();
 
1628   my ($self, $myconfig, $form) = @_;
 
1630   my $dbh = $form->dbconnect($myconfig);
 
1632   my ($query, @values);
 
1634   if ($form->{revtrans}) {
 
1635     $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '1'|;
 
1637   } elsif ($form->{closedto}) {
 
1638     $query = qq|UPDATE defaults SET closedto = ?, revtrans = '0'|;
 
1639     @values = (conv_date($form->{closedto}));
 
1642     $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '0'|;
 
1645   # set close in defaults
 
1646   do_query($form, $dbh, $query, @values);
 
1650   $main::lxdebug->leave_sub();
 
1654   my ($self, $units, $unit_name, $factor) = @_;
 
1656   $factor = 1 unless ($factor);
 
1658   my $unit = $units->{$unit_name};
 
1660   if (!defined($unit) || !$unit->{"base_unit"} ||
 
1661       ($unit_name eq $unit->{"base_unit"})) {
 
1662     return ($unit_name, $factor);
 
1665   return AM->get_base_unit($units, $unit->{"base_unit"}, $factor * $unit->{"factor"});
 
1668 sub retrieve_units {
 
1669   $main::lxdebug->enter_sub();
 
1671   my ($self, $myconfig, $form, $prefix) = @_;
 
1673   my $dbh = $form->get_standard_dbh;
 
1675   my $query = "SELECT *, base_unit AS original_base_unit FROM units";
 
1677   my $sth = prepare_execute_query($form, $dbh, $query);
 
1680   while (my $ref = $sth->fetchrow_hashref()) {
 
1681     $units->{$ref->{"name"}} = $ref;
 
1685   my $query_lang = "SELECT id, template_code FROM language ORDER BY description";
 
1686   $sth = $dbh->prepare($query_lang);
 
1687   $sth->execute() || $form->dberror($query_lang);
 
1689   while (my $ref = $sth->fetchrow_hashref()) {
 
1690     push(@languages, $ref);
 
1694   $query_lang = "SELECT ul.localized, ul.localized_plural, l.id, l.template_code " .
 
1695     "FROM units_language ul " .
 
1696     "LEFT JOIN language l ON ul.language_id = l.id " .
 
1697     "WHERE ul.unit = ?";
 
1698   $sth = $dbh->prepare($query_lang);
 
1700   foreach my $unit (values(%{$units})) {
 
1701     ($unit->{"${prefix}base_unit"}, $unit->{"${prefix}factor"}) = AM->get_base_unit($units, $unit->{"name"});
 
1703     $unit->{"LANGUAGES"} = {};
 
1704     foreach my $lang (@languages) {
 
1705       $unit->{"LANGUAGES"}->{$lang->{"template_code"}} = { "template_code" => $lang->{"template_code"} };
 
1708     $sth->execute($unit->{"name"}) || $form->dberror($query_lang . " (" . $unit->{"name"} . ")");
 
1709     while (my $ref = $sth->fetchrow_hashref()) {
 
1710       map({ $unit->{"LANGUAGES"}->{$ref->{"template_code"}}->{$_} = $ref->{$_} } keys(%{$ref}));
 
1715   $main::lxdebug->leave_sub();
 
1720 sub retrieve_all_units {
 
1721   $main::lxdebug->enter_sub();
 
1725   if (!$main::all_units) {
 
1726     $main::all_units = $self->retrieve_units(\%main::myconfig, $main::form);
 
1729   $main::lxdebug->leave_sub();
 
1731   return $main::all_units;
 
1735 sub translate_units {
 
1736   $main::lxdebug->enter_sub();
 
1738   my ($self, $form, $template_code, $unit, $amount) = @_;
 
1740   my $units = $self->retrieve_units(\%main::myconfig, $form);
 
1742   my $h = $units->{$unit}->{"LANGUAGES"}->{$template_code};
 
1743   my $new_unit = $unit;
 
1745     if (($amount != 1) && $h->{"localized_plural"}) {
 
1746       $new_unit = $h->{"localized_plural"};
 
1747     } elsif ($h->{"localized"}) {
 
1748       $new_unit = $h->{"localized"};
 
1752   $main::lxdebug->leave_sub();
 
1758   $main::lxdebug->enter_sub();
 
1760   my ($self, $myconfig, $form, $units) = @_;
 
1762   my $dbh = $form->dbconnect($myconfig);
 
1764   map({ $_->{"in_use"} = 0; } values(%{$units}));
 
1766   foreach my $unit (values(%{$units})) {
 
1767     my $base_unit = $unit->{"original_base_unit"};
 
1768     while ($base_unit) {
 
1769       $units->{$base_unit}->{"in_use"} = 1;
 
1770       $units->{$base_unit}->{"DEPENDING_UNITS"} = [] unless ($units->{$base_unit}->{"DEPENDING_UNITS"});
 
1771       push(@{$units->{$base_unit}->{"DEPENDING_UNITS"}}, $unit->{"name"});
 
1772       $base_unit = $units->{$base_unit}->{"original_base_unit"};
 
1776   foreach my $unit (values(%{$units})) {
 
1777     map({ $_ = $dbh->quote($_); } @{$unit->{"DEPENDING_UNITS"}});
 
1779     foreach my $table (qw(parts invoice orderitems)) {
 
1780       my $query = "SELECT COUNT(*) FROM $table WHERE unit ";
 
1782       if (0 == scalar(@{$unit->{"DEPENDING_UNITS"}})) {
 
1783         $query .= "= " . $dbh->quote($unit->{"name"});
 
1785         $query .= "IN (" . $dbh->quote($unit->{"name"}) . "," .
 
1786           join(",", map({ $dbh->quote($_) } @{$unit->{"DEPENDING_UNITS"}})) . ")";
 
1789       my ($count) = $dbh->selectrow_array($query);
 
1790       $form->dberror($query) if ($dbh->err);
 
1793         $unit->{"in_use"} = 1;
 
1801   $main::lxdebug->leave_sub();
 
1804 sub convertible_units {
 
1805   $main::lxdebug->enter_sub();
 
1809   my $filter_unit = shift;
 
1810   my $not_smaller = shift;
 
1812   my $conv_units = [];
 
1814   $filter_unit = $units->{$filter_unit};
 
1816   foreach my $name (sort { lc $a cmp lc $b } keys %{ $units }) {
 
1817     my $unit = $units->{$name};
 
1819     if (($unit->{base_unit} eq $filter_unit->{base_unit}) &&
 
1820         (!$not_smaller || ($unit->{factor} >= $filter_unit->{factor}))) {
 
1821       push @{$conv_units}, $unit;
 
1825   my @sorted = sort { $b->{factor} <=> $a->{factor} } @{ $conv_units };
 
1827   $main::lxdebug->leave_sub();
 
1832 # if $a is translatable to $b, return the factor between them.
 
1835   $main::lxdebug->enter_sub(2);
 
1836   my ($this, $a, $b, $all_units) = @_;
 
1838   $main::lxdebug->leave_sub(2) and return 0 unless $a && $b;
 
1839   $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a} && $all_units->{$b};
 
1840   $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a}{base_unit} eq $all_units->{$b}{base_unit};
 
1841   $main::lxdebug->leave_sub(2) and return $all_units->{$a}{factor} / $all_units->{$b}{factor};
 
1844 sub unit_select_data {
 
1845   $main::lxdebug->enter_sub();
 
1847   my ($self, $units, $selected, $empty_entry, $convertible_into) = @_;
 
1852     push(@{$select}, { "name" => "", "base_unit" => "", "factor" => "", "selected" => "" });
 
1855   foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
 
1856     if (!$convertible_into ||
 
1857         ($units->{$convertible_into} &&
 
1858          ($units->{$convertible_into}->{base_unit} eq $units->{$unit}->{base_unit}))) {
 
1859       push @{$select}, { "name"      => $unit,
 
1860                          "base_unit" => $units->{$unit}->{"base_unit"},
 
1861                          "factor"    => $units->{$unit}->{"factor"},
 
1862                          "selected"  => ($unit eq $selected) ? "selected" : "" };
 
1866   $main::lxdebug->leave_sub();
 
1871 sub unit_select_html {
 
1872   $main::lxdebug->enter_sub();
 
1874   my ($self, $units, $name, $selected, $convertible_into) = @_;
 
1876   my $select = "<select name=${name}>";
 
1878   foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
 
1879     if (!$convertible_into ||
 
1880         ($units->{$convertible_into} &&
 
1881          ($units->{$convertible_into}->{"base_unit"} eq $units->{$unit}->{"base_unit"}))) {
 
1882       $select .= "<option" . (($unit eq $selected) ? " selected" : "") . ">${unit}</option>";
 
1885   $select .= "</select>";
 
1887   $main::lxdebug->leave_sub();
 
1893   $main::lxdebug->enter_sub();
 
1897   my $units = $self->retrieve_all_units();
 
1902   while (2 <= scalar(@_)) {
 
1903     my $qty  = shift(@_);
 
1904     my $unit = $units->{shift(@_)};
 
1906     croak "No unit defined with name $unit" if (!defined $unit);
 
1909       $base_unit = $unit->{base_unit};
 
1910     } elsif ($base_unit ne $unit->{base_unit}) {
 
1911       croak "Adding values with incompatible base units $base_unit/$unit->{base_unit}";
 
1914     $sum += $qty * $unit->{factor};
 
1917   $main::lxdebug->leave_sub();
 
1919   return wantarray ? ($sum, $base_unit) : $sum;
 
1923   $main::lxdebug->enter_sub();
 
1925   my ($self, $myconfig, $form, $name, $base_unit, $factor, $languages) = @_;
 
1927   my $dbh = $form->dbconnect_noauto($myconfig);
 
1929   my $query = qq|SELECT COALESCE(MAX(sortkey), 0) + 1 FROM units|;
 
1930   my ($sortkey) = selectrow_query($form, $dbh, $query);
 
1932   $query = "INSERT INTO units (name, base_unit, factor, sortkey) " .
 
1933     "VALUES (?, ?, ?, ?)";
 
1934   do_query($form, $dbh, $query, $name, $base_unit, $factor, $sortkey);
 
1937     $query = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
 
1938     my $sth = $dbh->prepare($query);
 
1939     foreach my $lang (@{$languages}) {
 
1940       my @values = ($name, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
 
1941       $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
 
1949   $main::lxdebug->leave_sub();
 
1953   $main::lxdebug->enter_sub();
 
1955   my ($self, $myconfig, $form, $units, $delete_units) = @_;
 
1957   my $dbh = $form->dbconnect_noauto($myconfig);
 
1959   my ($base_unit, $unit, $sth, $query);
 
1961   $query = "DELETE FROM units_language";
 
1962   $dbh->do($query) || $form->dberror($query);
 
1964   if ($delete_units && (0 != scalar(@{$delete_units}))) {
 
1965     $query = "DELETE FROM units WHERE name IN (";
 
1966     map({ $query .= "?," } @{$delete_units});
 
1967     substr($query, -1, 1) = ")";
 
1968     $dbh->do($query, undef, @{$delete_units}) ||
 
1969       $form->dberror($query . " (" . join(", ", @{$delete_units}) . ")");
 
1972   $query = "UPDATE units SET name = ?, base_unit = ?, factor = ? WHERE name = ?";
 
1973   $sth = $dbh->prepare($query);
 
1975   my $query_lang = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
 
1976   my $sth_lang = $dbh->prepare($query_lang);
 
1978   foreach $unit (values(%{$units})) {
 
1979     $unit->{"depth"} = 0;
 
1980     my $base_unit = $unit;
 
1981     while ($base_unit->{"base_unit"}) {
 
1983       $base_unit = $units->{$base_unit->{"base_unit"}};
 
1987   foreach $unit (sort({ $a->{"depth"} <=> $b->{"depth"} } values(%{$units}))) {
 
1988     if ($unit->{"LANGUAGES"}) {
 
1989       foreach my $lang (@{$unit->{"LANGUAGES"}}) {
 
1990         next unless ($lang->{"id"} && $lang->{"localized"});
 
1991         my @values = ($unit->{"name"}, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
 
1992         $sth_lang->execute(@values) || $form->dberror($query_lang . " (" . join(", ", @values) . ")");
 
1996     next if ($unit->{"unchanged_unit"});
 
1998     my @values = ($unit->{"name"}, $unit->{"base_unit"}, $unit->{"factor"}, $unit->{"old_name"});
 
1999     $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
 
2003   $sth_lang->finish();
 
2007   $main::lxdebug->leave_sub();
 
2011   $main::lxdebug->enter_sub();
 
2013   my ($self, $myconfig, $form, $dir, $name_1) = @_;
 
2015   my $dbh = $form->dbconnect_noauto($myconfig);
 
2019   $query = qq|SELECT sortkey FROM units WHERE name = ?|;
 
2020   my ($sortkey_1) = selectrow_query($form, $dbh, $query, $name_1);
 
2023     qq|SELECT sortkey FROM units | .
 
2024     qq|WHERE sortkey | . ($dir eq "down" ? ">" : "<") . qq| ? | .
 
2025     qq|ORDER BY sortkey | . ($dir eq "down" ? "ASC" : "DESC") . qq| LIMIT 1|;
 
2026   my ($sortkey_2) = selectrow_query($form, $dbh, $query, $sortkey_1);
 
2028   if (defined($sortkey_1)) {
 
2029     $query = qq|SELECT name FROM units WHERE sortkey = ${sortkey_2}|;
 
2030     my ($name_2) = selectrow_query($form, $dbh, $query);
 
2032     if (defined($name_2)) {
 
2033       $query = qq|UPDATE units SET sortkey = ? WHERE name = ?|;
 
2034       my $sth = $dbh->prepare($query);
 
2036       do_statement($form, $sth, $query, $sortkey_1, $name_2);
 
2037       do_statement($form, $sth, $query, $sortkey_2, $name_1);
 
2044   $main::lxdebug->leave_sub();
 
2048   $main::lxdebug->enter_sub();
 
2050   my ($self, $myconfig, $form) = @_;
 
2052   # connect to database
 
2053   my $dbh = $form->dbconnect($myconfig);
 
2055   my $query = qq|SELECT
 
2059                    round(t.rate * 100, 2) AS rate,
 
2060                    (SELECT accno FROM chart WHERE id = chart_id) AS taxnumber,
 
2061                    (SELECT description FROM chart WHERE id = chart_id) AS account_description
 
2065   my $sth = $dbh->prepare($query);
 
2066   $sth->execute || $form->dberror($query);
 
2069   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
2070     push @{ $form->{TAX} }, $ref;
 
2076   $main::lxdebug->leave_sub();
 
2079 sub get_tax_accounts {
 
2080   $main::lxdebug->enter_sub();
 
2082   my ($self, $myconfig, $form) = @_;
 
2084   my $dbh = $form->dbconnect($myconfig);
 
2086   # get Accounts from chart
 
2087   my $query = qq{ SELECT
 
2089                  accno || ' - ' || description AS taxaccount
 
2091                WHERE link LIKE '%_tax%'
 
2095   my $sth = $dbh->prepare($query);
 
2096   $sth->execute || $form->dberror($query);
 
2098   $form->{ACCOUNTS} = [];
 
2099   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
2100     push @{ $form->{ACCOUNTS} }, $ref;
 
2107   $main::lxdebug->leave_sub();
 
2111   $main::lxdebug->enter_sub();
 
2113   my ($self, $myconfig, $form) = @_;
 
2115   # connect to database
 
2116   my $dbh = $form->dbconnect($myconfig);
 
2118   my $query = qq|SELECT
 
2121                    round(rate * 100, 2) AS rate,
 
2126   my $sth = $dbh->prepare($query);
 
2127   $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
2129   my $ref = $sth->fetchrow_hashref("NAME_lc");
 
2131   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
2135   # see if it is used by a taxkey
 
2136   $query = qq|SELECT count(*) FROM taxkeys
 
2137               WHERE tax_id = ? AND chart_id >0|;
 
2139   ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
 
2141   $form->{orphaned} = !$form->{orphaned};
 
2144   if (!$form->{orphaned} ) {
 
2145     $query = qq|SELECT DISTINCT c.id, c.accno
 
2147                 JOIN   tax t ON (t.id = tk.tax_id)
 
2148                 JOIN chart c ON (c.id = tk.chart_id)
 
2149                 WHERE tk.tax_id = ?|;
 
2151     $sth = $dbh->prepare($query);
 
2152     $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
2154     $form->{TAXINUSE} = [];
 
2155     while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
2156       push @{ $form->{TAXINUSE} }, $ref;
 
2164   $main::lxdebug->leave_sub();
 
2168   $main::lxdebug->enter_sub();
 
2170   my ($self, $myconfig, $form) = @_;
 
2173   # connect to database
 
2174   my $dbh = $form->get_standard_dbh($myconfig);
 
2176   $form->{rate} = $form->{rate} / 100;
 
2178   my @values = ($form->{taxkey}, $form->{taxdescription}, $form->{rate}, $form->{chart_id}, $form->{chart_id} );
 
2179   if ($form->{id} ne "") {
 
2180     $query = qq|UPDATE tax SET
 
2185                   taxnumber      = (SELECT accno FROM chart WHERE id= ? )
 
2187     push(@values, $form->{id});
 
2191     $query = qq|INSERT INTO tax (
 
2198                 VALUES (?, ?, ?, ?, (SELECT accno FROM chart WHERE id = ?) )|;
 
2200   do_query($form, $dbh, $query, @values);
 
2204   $main::lxdebug->leave_sub();
 
2208   $main::lxdebug->enter_sub();
 
2210   my ($self, $myconfig, $form) = @_;
 
2213   # connect to database
 
2214   my $dbh = $form->get_standard_dbh($myconfig);
 
2216   $query = qq|DELETE FROM tax
 
2218   do_query($form, $dbh, $query, $form->{id});
 
2222   $main::lxdebug->leave_sub();
 
2225 sub save_price_factor {
 
2226   $main::lxdebug->enter_sub();
 
2228   my ($self, $myconfig, $form) = @_;
 
2230   # connect to database
 
2231   my $dbh = $form->get_standard_dbh($myconfig);
 
2234   my @values = ($form->{description}, conv_i($form->{factor}));
 
2237     $query = qq|UPDATE price_factors SET description = ?, factor = ? WHERE id = ?|;
 
2238     push @values, conv_i($form->{id});
 
2241     $query = qq|INSERT INTO price_factors (description, factor, sortkey) VALUES (?, ?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM price_factors))|;
 
2244   do_query($form, $dbh, $query, @values);
 
2248   $main::lxdebug->leave_sub();
 
2251 sub get_all_price_factors {
 
2252   $main::lxdebug->enter_sub();
 
2254   my ($self, $myconfig, $form) = @_;
 
2256   # connect to database
 
2257   my $dbh = $form->get_standard_dbh($myconfig);
 
2259   $form->{PRICE_FACTORS} = selectall_hashref_query($form, $dbh, qq|SELECT * FROM price_factors ORDER BY sortkey|);
 
2261   $main::lxdebug->leave_sub();
 
2264 sub get_price_factor {
 
2265   $main::lxdebug->enter_sub();
 
2267   my ($self, $myconfig, $form) = @_;
 
2269   # connect to database
 
2270   my $dbh = $form->get_standard_dbh($myconfig);
 
2272   my $query = qq|SELECT description, factor,
 
2273                    ((SELECT COUNT(*) FROM parts      WHERE price_factor_id = ?) +
 
2274                     (SELECT COUNT(*) FROM invoice    WHERE price_factor_id = ?) +
 
2275                     (SELECT COUNT(*) FROM orderitems WHERE price_factor_id = ?)) = 0 AS orphaned
 
2276                  FROM price_factors WHERE id = ?|;
 
2278   ($form->{description}, $form->{factor}, $form->{orphaned}) = selectrow_query($form, $dbh, $query, (conv_i($form->{id})) x 4);
 
2280   $main::lxdebug->leave_sub();
 
2283 sub delete_price_factor {
 
2284   $main::lxdebug->enter_sub();
 
2286   my ($self, $myconfig, $form) = @_;
 
2288   # connect to database
 
2289   my $dbh = $form->get_standard_dbh($myconfig);
 
2291   do_query($form, $dbh, qq|DELETE FROM price_factors WHERE id = ?|, conv_i($form->{id}));
 
2294   $main::lxdebug->leave_sub();
 
2297 sub save_warehouse {
 
2298   $main::lxdebug->enter_sub();
 
2300   my ($self, $myconfig, $form) = @_;
 
2302   # connect to database
 
2303   my $dbh = $form->get_standard_dbh($myconfig);
 
2305   my ($query, @values, $sth);
 
2308     $query        = qq|SELECT nextval('id')|;
 
2309     ($form->{id}) = selectrow_query($form, $dbh, $query);
 
2311     $query        = qq|INSERT INTO warehouse (id, sortkey) VALUES (?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM warehouse))|;
 
2312     do_query($form, $dbh, $query, $form->{id});
 
2315   do_query($form, $dbh, qq|UPDATE warehouse SET description = ?, invalid = ? WHERE id = ?|,
 
2316            $form->{description}, $form->{invalid} ? 't' : 'f', conv_i($form->{id}));
 
2318   if (0 < $form->{number_of_new_bins}) {
 
2319     $query = qq|INSERT INTO bin (warehouse_id, description) VALUES (?, ?)|;
 
2320     $sth   = prepare_query($form, $dbh, $query);
 
2322     foreach my $i (1..$form->{number_of_new_bins}) {
 
2323       do_statement($form, $sth, $query, conv_i($form->{id}), "$form->{prefix}${i}");
 
2331   $main::lxdebug->leave_sub();
 
2335   $main::lxdebug->enter_sub();
 
2337   my ($self, $myconfig, $form) = @_;
 
2339   # connect to database
 
2340   my $dbh = $form->get_standard_dbh($myconfig);
 
2342   my ($query, @values, $commit_necessary, $sth);
 
2344   @values = map { $form->{"id_${_}"} } grep { $form->{"delete_${_}"} } (1..$form->{rowcount});
 
2347     $query = qq|DELETE FROM bin WHERE id IN (| . join(', ', ('?') x scalar(@values)) . qq|)|;
 
2348     do_query($form, $dbh, $query, @values);
 
2350     $commit_necessary = 1;
 
2353   $query = qq|UPDATE bin SET description = ? WHERE id = ?|;
 
2354   $sth   = prepare_query($form, $dbh, $query);
 
2356   foreach my $row (1..$form->{rowcount}) {
 
2357     next if ($form->{"delete_${row}"});
 
2359     do_statement($form, $sth, $query, $form->{"description_${row}"}, conv_i($form->{"id_${row}"}));
 
2361     $commit_necessary = 1;
 
2366   $dbh->commit() if ($commit_necessary);
 
2368   $main::lxdebug->leave_sub();
 
2371 sub delete_warehouse {
 
2372   $main::lxdebug->enter_sub();
 
2374   my ($self, $myconfig, $form) = @_;
 
2376   # connect to database
 
2377   my $dbh = $form->get_standard_dbh($myconfig);
 
2379   my $id      = conv_i($form->{id});
 
2380   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|;
 
2381   my ($count) = selectrow_query($form, $dbh, $query, $id);
 
2384     $main::lxdebug->leave_sub();
 
2388   do_query($form, $dbh, qq|DELETE FROM bin       WHERE warehouse_id = ?|, conv_i($form->{id}));
 
2389   do_query($form, $dbh, qq|DELETE FROM warehouse WHERE id           = ?|, conv_i($form->{id}));
 
2393   $main::lxdebug->leave_sub();
 
2398 sub get_all_warehouses {
 
2399   $main::lxdebug->enter_sub();
 
2401   my ($self, $myconfig, $form) = @_;
 
2403   # connect to database
 
2404   my $dbh = $form->get_standard_dbh($myconfig);
 
2406   my $query = qq|SELECT w.id, w.description, w.invalid,
 
2407                    (SELECT COUNT(b.description) FROM bin b WHERE b.warehouse_id = w.id) AS number_of_bins
 
2409                  ORDER BY w.sortkey|;
 
2411   $form->{WAREHOUSES} = selectall_hashref_query($form, $dbh, $query);
 
2413   $main::lxdebug->leave_sub();
 
2417   $main::lxdebug->enter_sub();
 
2419   my ($self, $myconfig, $form) = @_;
 
2421   # connect to database
 
2422   my $dbh = $form->get_standard_dbh($myconfig);
 
2424   my $id    = conv_i($form->{id});
 
2425   my $query = qq|SELECT w.description, w.invalid
 
2429   my $ref   = selectfirst_hashref_query($form, $dbh, $query, $id);
 
2431   map { $form->{$_} = $ref->{$_} } keys %{ $ref };
 
2433   $query = qq|SELECT b.*, EXISTS
 
2434                 (SELECT i.warehouse_id
 
2436                  WHERE i.bin_id = b.id
 
2440               WHERE b.warehouse_id = ?|;
 
2442   $form->{BINS} = selectall_hashref_query($form, $dbh, $query, conv_i($form->{id}));
 
2444   $main::lxdebug->leave_sub();