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 #======================================================================
 
  43 use List::MoreUtils qw(any);
 
  52   $main::lxdebug->enter_sub();
 
  54   my ($self, $myconfig, $form) = @_;
 
  57   my $dbh = $form->dbconnect($myconfig);
 
  59     SELECT c.accno, c.description, c.charttype, c.category,
 
  60       c.link, c.pos_bilanz, c.pos_eur, c.new_chart_id, c.valid_from,
 
  61       c.pos_bwa, datevautomatik,
 
  62       tk.taxkey_id, tk.pos_ustva, tk.tax_id,
 
  63       tk.tax_id || '--' || tk.taxkey_id AS tax, tk.startdate
 
  66     ON (c.id=tk.chart_id AND tk.id =
 
  67       (SELECT id FROM taxkeys
 
  68        WHERE taxkeys.chart_id = c.id AND startdate <= current_date
 
  69        ORDER BY startdate DESC LIMIT 1))
 
  74   $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
 
  75   my $sth = $dbh->prepare($query);
 
  76   $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
  78   my $ref = $sth->fetchrow_hashref("NAME_lc");
 
  80   foreach my $key (keys %$ref) {
 
  81     $form->{"$key"} = $ref->{"$key"};
 
  86   # get default accounts
 
  87   $query = qq|SELECT inventory_accno_id, income_accno_id, expense_accno_id
 
  89   $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
 
  90   $sth = $dbh->prepare($query);
 
  91   $sth->execute || $form->dberror($query);
 
  93   $ref = $sth->fetchrow_hashref("NAME_lc");
 
  95   map { $form->{$_} = $ref->{$_} } keys %{ $ref };
 
 101   # get taxkeys and description
 
 105       (SELECT accno FROM chart WHERE id=tax.chart_id) AS chart_accno,
 
 107       id||'--'||taxkey AS tax,
 
 110     FROM tax ORDER BY taxkey
 
 112   $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
 
 113   $sth = $dbh->prepare($query);
 
 114   $sth->execute || $form->dberror($query);
 
 116   $form->{TAXKEY} = [];
 
 118   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 119     push @{ $form->{TAXKEY} }, $ref;
 
 125     $query = qq|SELECT id, accno,description
 
 129     $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
 
 130     $sth = $dbh->prepare($query);
 
 131     $sth->execute($form->{link}) || $form->dberror($query . " ($form->{link})");
 
 133     $form->{NEWACCOUNT} = [];
 
 134     while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 135       push @{ $form->{NEWACCOUNT} }, $ref;
 
 140     # get the taxkeys of account
 
 154       LEFT JOIN   tax t ON (t.id = tk.tax_id)
 
 155       LEFT JOIN chart c ON (c.id = t.chart_id)
 
 157       WHERE tk.chart_id = ?
 
 158       ORDER BY startdate DESC
 
 160     $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
 
 161     $sth = $dbh->prepare($query);
 
 163     $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
 165     $form->{ACCOUNT_TAXKEYS} = [];
 
 167     while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 168       push @{ $form->{ACCOUNT_TAXKEYS} }, $ref;
 
 174   # check if we have any transactions
 
 175   $query = qq|SELECT a.trans_id FROM acc_trans a
 
 176               WHERE a.chart_id = ?|;
 
 177   $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
 
 178   $sth = $dbh->prepare($query);
 
 179   $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
 181   ($form->{orphaned}) = $sth->fetchrow_array;
 
 182   $form->{orphaned} = !$form->{orphaned};
 
 185   # check if new account is active
 
 186   $form->{new_chart_valid} = 0;
 
 187   if ($form->{new_chart_id}) {
 
 188     $query = qq|SELECT current_date-valid_from FROM chart
 
 190     $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query");
 
 191     my ($count) = selectrow_query($form, $dbh, $query, $form->{id});
 
 193       $form->{new_chart_valid} = 1;
 
 200   $main::lxdebug->leave_sub();
 
 204   $main::lxdebug->enter_sub();
 
 206   # TODO: it should be forbidden to change an account to a heading if there
 
 207   # have been bookings to this account in the past
 
 209   my ($self, $myconfig, $form) = @_;
 
 211   # connect to database, turn off AutoCommit
 
 212   my $dbh = $form->dbconnect_noauto($myconfig);
 
 214   for (qw(AR_include_in_dropdown AP_include_in_dropdown summary_account)) {
 
 215     $form->{$form->{$_}} = $form->{$_} if $form->{$_};
 
 218   # sanity check, can't have AR with AR_...
 
 219   if ($form->{AR} || $form->{AP} || $form->{IC}) {
 
 220     if (any { $form->{$_} } 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)) {
 
 221       $form->error($::locale->text('It is not allowed that a summary account occurs in a drop-down menu!'));
 
 226   foreach my $item ($form->{AR},            $form->{AR_amount},
 
 227                     $form->{AR_tax},        $form->{AR_paid},
 
 228                     $form->{AP},            $form->{AP_amount},
 
 229                     $form->{AP_tax},        $form->{AP_paid},
 
 230                     $form->{IC},            $form->{IC_sale},
 
 231                     $form->{IC_cogs},       $form->{IC_taxpart},
 
 232                     $form->{IC_income},     $form->{IC_expense},
 
 233                     $form->{IC_taxservice}
 
 235     $form->{link} .= "${item}:" if ($item);
 
 239   # strip blanks from accno
 
 240   map { $form->{$_} =~ s/ //g; } qw(accno);
 
 244   if ($form->{id} eq "NULL") {
 
 253   my @values = ($form->{accno});
 
 256     $query .= ' AND NOT id = ?';
 
 257     push(@values, $form->{id});
 
 260   my ($accno) = selectrow_query($form, $dbh, $query, @values);
 
 263     $form->error($::locale->text('Account number not unique!'));
 
 267   if (!$form->{id} || $form->{id} eq "") {
 
 268     $query = qq|SELECT nextval('id')|;
 
 269     ($form->{"id"}) = selectrow_query($form, $dbh, $query);
 
 270     $query = qq|INSERT INTO chart (id, accno, link) VALUES (?, ?, ?)|;
 
 271     do_query($form, $dbh, $query, $form->{"id"}, $form->{"accno"}, '');
 
 279     # if charttype is heading make sure certain values are empty
 
 280     # specifically, if charttype is changed from an existing account, empty the
 
 281     # fields unnecessary for headings, so that e.g. heading doesn't appear in
 
 282     # drop-down menues due to still having a valid "link" entry
 
 284     if ( $form->{charttype} eq 'H' ) {
 
 286       $form->{pos_bwa} = '';
 
 287       $form->{pos_bilanz} = '';
 
 288       $form->{pos_eur} = '';
 
 289       $form->{new_chart_id} = '';
 
 290       $form->{valid_from} = '';
 
 293     $query = qq|UPDATE chart SET
 
 309                   $form->{description},
 
 313                   conv_i($form->{pos_bwa}),
 
 314                   conv_i($form->{pos_bilanz}),
 
 315                   conv_i($form->{pos_eur}),
 
 316                   conv_i($form->{new_chart_id}),
 
 317                   conv_date($form->{valid_from}),
 
 318                   ($form->{datevautomatik} eq 'T') ? 'true':'false',
 
 325   do_query($form, $dbh, $query, @values);
 
 331   my $MAX_TRIES = 10; # Maximum count of taxkeys in form
 
 335   for $tk_count (0 .. $MAX_TRIES) {
 
 339     # Check if the account already exists, else cancel
 
 341     print(STDERR "Keine Taxkeys weil ID =: $form->{id}\n");
 
 343     last READTAXKEYS if ( $form->{'id'} == 0);
 
 345     # check if there is a startdate
 
 346     if ( $form->{"taxkey_startdate_$tk_count"} eq '' ) {
 
 351     # Add valid taxkeys into the array
 
 354         id        => ($form->{"taxkey_id_$tk_count"} eq 'NEW') ? conv_i('') : conv_i($form->{"taxkey_id_$tk_count"}),
 
 355         tax_id    => conv_i($form->{"taxkey_tax_$tk_count"}),
 
 356         startdate => conv_date($form->{"taxkey_startdate_$tk_count"}),
 
 357         chart_id  => conv_i($form->{"id"}),
 
 358         pos_ustva => conv_i($form->{"taxkey_pos_ustva_$tk_count"}),
 
 359         delete    => ( $form->{"taxkey_del_$tk_count"} eq 'delete' ) ? '1' : '',
 
 366   for my $j (0 .. $#taxkeys){
 
 367     if ( defined $taxkeys[$j]{'id'} ){
 
 370       if ($taxkeys[$j]{'delete'}){
 
 372           DELETE FROM taxkeys WHERE id = ?
 
 375         @values = ($taxkeys[$j]{'id'});
 
 377         do_query($form, $dbh, $query, @values);
 
 386         SET taxkey_id = (SELECT taxkey FROM tax WHERE tax.id = ?),
 
 394         $taxkeys[$j]{'tax_id'},
 
 395         $taxkeys[$j]{'chart_id'},
 
 396         $taxkeys[$j]{'tax_id'},
 
 397         $taxkeys[$j]{'pos_ustva'},
 
 398         $taxkeys[$j]{'startdate'},
 
 401       do_query($form, $dbh, $query, @values);
 
 407         INSERT INTO taxkeys (
 
 414         VALUES ((SELECT taxkey FROM tax WHERE tax.id = ?), ?, ?, ?, ?)
 
 417         $taxkeys[$j]{'tax_id'},
 
 418         $taxkeys[$j]{'chart_id'},
 
 419         $taxkeys[$j]{'tax_id'},
 
 420         $taxkeys[$j]{'pos_ustva'},
 
 421         $taxkeys[$j]{'startdate'},
 
 424       do_query($form, $dbh, $query, @values);
 
 429   # Update chart.taxkey_id to the latest from taxkeys for this chart.
 
 435       WHERE taxkeys.chart_id = chart.id
 
 436       ORDER BY startdate DESC
 
 442   do_query($form, $dbh, $query, $form->{id});
 
 445   my $rc = $dbh->commit;
 
 448   $main::lxdebug->leave_sub();
 
 454   $main::lxdebug->enter_sub();
 
 456   my ($self, $myconfig, $form) = @_;
 
 458   # connect to database, turn off AutoCommit
 
 459   my $dbh = $form->dbconnect_noauto($myconfig);
 
 461   my $query = qq|SELECT count(*) FROM acc_trans a
 
 462                  WHERE a.chart_id = ?|;
 
 463   my ($count) = selectrow_query($form, $dbh, $query, $form->{id});
 
 467     $main::lxdebug->leave_sub();
 
 471   # set inventory_accno_id, income_accno_id, expense_accno_id to defaults
 
 472   foreach my $type (qw(inventory income expense)) {
 
 475       qq|SET ${type}_accno_id = (SELECT ${type}_accno_id FROM defaults) | .
 
 476       qq|WHERE ${type}_accno_id = ?|;
 
 477     do_query($form, $dbh, $query, $form->{id});
 
 480   $query = qq|DELETE FROM tax
 
 482   do_query($form, $dbh, $query, $form->{id});
 
 484   # delete chart of account record
 
 485   $query = qq|DELETE FROM chart
 
 487   do_query($form, $dbh, $query, $form->{id});
 
 489   # delete account taxkeys
 
 490   $query = qq|DELETE FROM taxkeys
 
 492   do_query($form, $dbh, $query, $form->{id});
 
 494   # commit and redirect
 
 495   my $rc = $dbh->commit;
 
 498   $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 id, lead
 
 515   my $sth = $dbh->prepare($query);
 
 516   $sth->execute || $form->dberror($query);
 
 518   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 519     push @{ $form->{ALL} }, $ref;
 
 525   $main::lxdebug->leave_sub();
 
 529   $main::lxdebug->enter_sub();
 
 531   my ($self, $myconfig, $form) = @_;
 
 533   # connect to database
 
 534   my $dbh = $form->dbconnect($myconfig);
 
 537     qq|SELECT l.id, l.lead | .
 
 540   my $sth = $dbh->prepare($query);
 
 541   $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
 543   my $ref = $sth->fetchrow_hashref("NAME_lc");
 
 545   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
 551   $main::lxdebug->leave_sub();
 
 555   $main::lxdebug->enter_sub();
 
 557   my ($self, $myconfig, $form) = @_;
 
 560   # connect to database
 
 561   my $dbh = $form->dbconnect($myconfig);
 
 563   my @values = ($form->{description});
 
 564   # id is the old record
 
 566     $query = qq|UPDATE leads SET
 
 569     push(@values, $form->{id});
 
 571     $query = qq|INSERT INTO leads
 
 575   do_query($form, $dbh, $query, @values);
 
 579   $main::lxdebug->leave_sub();
 
 583   $main::lxdebug->enter_sub();
 
 585   my ($self, $myconfig, $form) = @_;
 
 588   # connect to database
 
 589   my $dbh = $form->dbconnect($myconfig);
 
 591   $query = qq|DELETE FROM leads
 
 593   do_query($form, $dbh, $query, $form->{id});
 
 597   $main::lxdebug->leave_sub();
 
 601   $main::lxdebug->enter_sub();
 
 603   my ($self, $myconfig, $form, $return_list) = @_;
 
 605   # connect to database
 
 606   my $dbh = $form->dbconnect($myconfig);
 
 609     "SELECT id, description, template_code, article_code, " .
 
 610     "  output_numberformat, output_dateformat, output_longdates " .
 
 611     "FROM language ORDER BY description";
 
 613   my $sth = $dbh->prepare($query);
 
 614   $sth->execute || $form->dberror($query);
 
 618   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 619     push(@{ $ary }, $ref);
 
 625   $main::lxdebug->leave_sub();
 
 635   $main::lxdebug->enter_sub();
 
 637   my ($self, $myconfig, $form) = @_;
 
 639   # connect to database
 
 640   my $dbh = $form->dbconnect($myconfig);
 
 643     "SELECT description, template_code, article_code, " .
 
 644     "  output_numberformat, output_dateformat, output_longdates " .
 
 645     "FROM language WHERE id = ?";
 
 646   my $sth = $dbh->prepare($query);
 
 647   $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
 
 649   my $ref = $sth->fetchrow_hashref("NAME_lc");
 
 651   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
 657   $main::lxdebug->leave_sub();
 
 660 sub get_language_details {
 
 661   $main::lxdebug->enter_sub();
 
 663   my ($self, $myconfig, $form, $id) = @_;
 
 665   # connect to database
 
 666   my $dbh = $form->dbconnect($myconfig);
 
 669     "SELECT template_code, " .
 
 670     "  output_numberformat, output_dateformat, output_longdates " .
 
 671     "FROM language WHERE id = ?";
 
 672   my @res = selectrow_query($form, $dbh, $query, $id);
 
 675   $main::lxdebug->leave_sub();
 
 681   $main::lxdebug->enter_sub();
 
 683   my ($self, $myconfig, $form) = @_;
 
 685   # connect to database
 
 686   my $dbh = $form->dbconnect($myconfig);
 
 687   my (@values, $query);
 
 689   map({ push(@values, $form->{$_}); }
 
 690       qw(description template_code article_code
 
 691          output_numberformat output_dateformat output_longdates));
 
 693   # id is the old record
 
 696       "UPDATE language SET " .
 
 697       "  description = ?, template_code = ?, article_code = ?, " .
 
 698       "  output_numberformat = ?, output_dateformat = ?, " .
 
 699       "  output_longdates = ? " .
 
 701     push(@values, $form->{id});
 
 704       "INSERT INTO language (" .
 
 705       "  description, template_code, article_code, " .
 
 706       "  output_numberformat, output_dateformat, output_longdates" .
 
 707       ") VALUES (?, ?, ?, ?, ?, ?)";
 
 709   do_query($form, $dbh, $query, @values);
 
 713   $main::lxdebug->leave_sub();
 
 716 sub delete_language {
 
 717   $main::lxdebug->enter_sub();
 
 719   my ($self, $myconfig, $form) = @_;
 
 722   # connect to database
 
 723   my $dbh = $form->dbconnect_noauto($myconfig);
 
 725   foreach my $table (qw(generic_translations units_language)) {
 
 726     $query = qq|DELETE FROM $table WHERE language_id = ?|;
 
 727     do_query($form, $dbh, $query, $form->{"id"});
 
 730   $query = "DELETE FROM language WHERE id = ?";
 
 731   do_query($form, $dbh, $query, $form->{"id"});
 
 736   $main::lxdebug->leave_sub();
 
 741   $main::lxdebug->enter_sub();
 
 743   my ($self, $myconfig, $form) = @_;
 
 745   # connect to database
 
 746   my $dbh = $form->dbconnect($myconfig);
 
 748   my $query = qq|SELECT id, description,
 
 750                  (SELECT accno FROM chart WHERE id = inventory_accno_id) AS inventory_accno,
 
 752                  (SELECT accno FROM chart WHERE id = income_accno_id_0) AS income_accno_0,
 
 754                  (SELECT accno FROM chart WHERE id = expense_accno_id_0) AS expense_accno_0,
 
 756                  (SELECT accno FROM chart WHERE id = income_accno_id_1) AS income_accno_1,
 
 758                  (SELECT accno FROM chart WHERE id = expense_accno_id_1) AS expense_accno_1,
 
 760                  (SELECT accno FROM chart WHERE id = income_accno_id_2) AS income_accno_2,
 
 762                  (select accno FROM chart WHERE id = expense_accno_id_2) AS expense_accno_2,
 
 764                  (SELECT accno FROM chart WHERE id = income_accno_id_3) AS income_accno_3,
 
 766                  (SELECT accno FROM chart WHERE id = expense_accno_id_3) AS expense_accno_3
 
 770   my $sth = $dbh->prepare($query);
 
 771   $sth->execute || $form->dberror($query);
 
 774   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 775     push @{ $form->{ALL} }, $ref;
 
 781   $main::lxdebug->leave_sub();
 
 784 sub get_buchungsgruppe {
 
 785   $main::lxdebug->enter_sub();
 
 787   my ($self, $myconfig, $form) = @_;
 
 790   # connect to database
 
 791   my $dbh = $form->dbconnect($myconfig);
 
 795       qq|SELECT description, inventory_accno_id,
 
 796          (SELECT accno FROM chart WHERE id = inventory_accno_id) AS inventory_accno,
 
 798          (SELECT accno FROM chart WHERE id = income_accno_id_0) AS income_accno_0,
 
 800          (SELECT accno FROM chart WHERE id = expense_accno_id_0) AS expense_accno_0,
 
 802          (SELECT accno FROM chart WHERE id = income_accno_id_1) AS income_accno_1,
 
 804          (SELECT accno FROM chart WHERE id = expense_accno_id_1) AS expense_accno_1,
 
 806          (SELECT accno FROM chart WHERE id = income_accno_id_2) AS income_accno_2,
 
 808          (select accno FROM chart WHERE id = expense_accno_id_2) AS expense_accno_2,
 
 810          (SELECT accno FROM chart WHERE id = income_accno_id_3) AS income_accno_3,
 
 812          (SELECT accno FROM chart WHERE id = expense_accno_id_3) AS expense_accno_3
 
 815     my $sth = $dbh->prepare($query);
 
 816     $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
 818     my $ref = $sth->fetchrow_hashref("NAME_lc");
 
 820     map { $form->{$_} = $ref->{$_} } keys %$ref;
 
 825       qq|SELECT count(id) = 0 AS orphaned
 
 827          WHERE buchungsgruppen_id = ?|;
 
 828     ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
 
 831   $query = "SELECT inventory_accno_id, income_accno_id, expense_accno_id ".
 
 833   ($form->{"std_inventory_accno_id"}, $form->{"std_income_accno_id"},
 
 834    $form->{"std_expense_accno_id"}) = selectrow_query($form, $dbh, $query);
 
 837   $query = qq|SELECT c.accno, c.description, c.link, c.id,
 
 838               d.inventory_accno_id, d.income_accno_id, d.expense_accno_id
 
 839               FROM chart c, defaults d
 
 840               WHERE c.link LIKE '%$module%'
 
 844   my $sth = $dbh->prepare($query);
 
 845   $sth->execute || $form->dberror($query);
 
 846   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 847     foreach my $key (split(/:/, $ref->{link})) {
 
 848       if (!$form->{"std_inventory_accno_id"} && ($key eq "IC")) {
 
 849         $form->{"std_inventory_accno_id"} = $ref->{"id"};
 
 851       if ($key =~ /$module/) {
 
 852         if (   ($ref->{id} eq $ref->{inventory_accno_id})
 
 853             || ($ref->{id} eq $ref->{income_accno_id})
 
 854             || ($ref->{id} eq $ref->{expense_accno_id})) {
 
 855           push @{ $form->{"${module}_links"}{$key} },
 
 856             { accno       => $ref->{accno},
 
 857               description => $ref->{description},
 
 858               selected    => "selected",
 
 861           push @{ $form->{"${module}_links"}{$key} },
 
 862             { accno       => $ref->{accno},
 
 863               description => $ref->{description},
 
 875   $main::lxdebug->leave_sub();
 
 878 sub save_buchungsgruppe {
 
 879   $main::lxdebug->enter_sub();
 
 881   my ($self, $myconfig, $form) = @_;
 
 883   # connect to database
 
 884   my $dbh = $form->dbconnect($myconfig);
 
 886   my @values = ($form->{description}, $form->{inventory_accno_id},
 
 887                 $form->{income_accno_id_0}, $form->{expense_accno_id_0},
 
 888                 $form->{income_accno_id_1}, $form->{expense_accno_id_1},
 
 889                 $form->{income_accno_id_2}, $form->{expense_accno_id_2},
 
 890                 $form->{income_accno_id_3}, $form->{expense_accno_id_3});
 
 894   # id is the old record
 
 896     $query = qq|UPDATE buchungsgruppen SET
 
 897                 description = ?, inventory_accno_id = ?,
 
 898                 income_accno_id_0 = ?, expense_accno_id_0 = ?,
 
 899                 income_accno_id_1 = ?, expense_accno_id_1 = ?,
 
 900                 income_accno_id_2 = ?, expense_accno_id_2 = ?,
 
 901                 income_accno_id_3 = ?, expense_accno_id_3 = ?
 
 903     push(@values, $form->{id});
 
 905     $query = qq|SELECT COALESCE(MAX(sortkey) + 1, 1) FROM buchungsgruppen|;
 
 906     my ($sortkey) = $dbh->selectrow_array($query);
 
 907     $form->dberror($query) if ($dbh->err);
 
 908     push(@values, $sortkey);
 
 909     $query = qq|INSERT INTO buchungsgruppen
 
 910                 (description, inventory_accno_id,
 
 911                 income_accno_id_0, expense_accno_id_0,
 
 912                 income_accno_id_1, expense_accno_id_1,
 
 913                 income_accno_id_2, expense_accno_id_2,
 
 914                 income_accno_id_3, expense_accno_id_3,
 
 916                 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
 
 918   do_query($form, $dbh, $query, @values);
 
 922   $main::lxdebug->leave_sub();
 
 925 sub delete_buchungsgruppe {
 
 926   $main::lxdebug->enter_sub();
 
 928   my ($self, $myconfig, $form) = @_;
 
 930   # connect to database
 
 931   my $dbh = $form->dbconnect($myconfig);
 
 933   my $query = qq|DELETE FROM buchungsgruppen WHERE id = ?|;
 
 934   do_query($form, $dbh, $query, $form->{id});
 
 938   $main::lxdebug->leave_sub();
 
 942   $main::lxdebug->enter_sub();
 
 944   my ($self, $myconfig, $form, $table) = @_;
 
 946   # connect to database
 
 947   my $dbh = $form->get_standard_dbh($myconfig);
 
 951        (SELECT sortkey FROM $table WHERE id = ?) AS sortkey1,
 
 952        (SELECT sortkey FROM $table WHERE id = ?) AS sortkey2|;
 
 953   my @values   = ($form->{"id1"}, $form->{"id2"});
 
 954   my @sortkeys = selectrow_query($form, $dbh, $query, @values);
 
 956   $query  = qq|UPDATE $table SET sortkey = ? WHERE id = ?|;
 
 957   my $sth = prepare_query($form, $dbh, $query);
 
 959   do_statement($form, $sth, $query, $sortkeys[1], $form->{"id1"});
 
 960   do_statement($form, $sth, $query, $sortkeys[0], $form->{"id2"});
 
 966   $main::lxdebug->leave_sub();
 
 969 sub prepare_template_filename {
 
 970   $main::lxdebug->enter_sub();
 
 972   my ($self, $myconfig, $form) = @_;
 
 974   my ($filename, $display_filename);
 
 976   if ($form->{type} eq "stylesheet") {
 
 977     $filename = "css/$myconfig->{stylesheet}";
 
 978     $display_filename = $myconfig->{stylesheet};
 
 981     $filename = $form->{formname};
 
 983     if ($form->{language}) {
 
 984       my ($id, $template_code) = split(/--/, $form->{language});
 
 985       $filename .= "_${template_code}";
 
 988     if ($form->{printer}) {
 
 989       my ($id, $template_code) = split(/--/, $form->{printer});
 
 990       $filename .= "_${template_code}";
 
 993     $filename .= "." . ($form->{format} eq "html" ? "html" : "tex");
 
 994     if ($form->{"formname"} =~ m|\.\.| || $form->{"formname"} =~ m|^/|) {
 
 995       $filename =~ s|.*/||;
 
 997     $display_filename = $filename;
 
 998     $filename = SL::DB::Default->get->templates . "/$filename";
 
1001   $main::lxdebug->leave_sub();
 
1003   return ($filename, $display_filename);
 
1008   $main::lxdebug->enter_sub();
 
1010   my ($self, $filename) = @_;
 
1012   my ($content, $lines) = ("", 0);
 
1016   if (open(TEMPLATE, $filename)) {
 
1017     while (<TEMPLATE>) {
 
1024   $content = Encode::decode('utf-8-strict', $content) if $::locale->is_utf8;
 
1026   $main::lxdebug->leave_sub();
 
1028   return ($content, $lines);
 
1032   $main::lxdebug->enter_sub();
 
1034   my ($self, $filename, $content) = @_;
 
1040   if (open(TEMPLATE, ">", $filename)) {
 
1041     $content = Encode::encode('utf-8-strict', $content) if $::locale->is_utf8;
 
1042     $content =~ s/\r\n/\n/g;
 
1043     print(TEMPLATE $content);
 
1049   $main::lxdebug->leave_sub();
 
1054 sub save_preferences {
 
1055   $main::lxdebug->enter_sub();
 
1057   my ($self, $form) = @_;
 
1059   my $employee = SL::DB::Manager::Employee->find_by(login => $form->{login});
 
1060   $employee->update_attributes(name => $form->{name});
 
1062   my $user = SL::DB::Manager::AuthUser->find_by(login => $form->{login});
 
1063   $user->update_attributes(
 
1065       map { ($_ => $form->{$_}) } SL::DB::AuthUser::CONFIG_VARS(),
 
1068   $main::lxdebug->leave_sub();
 
1074   $main::lxdebug->enter_sub();
 
1079   my $myconfig = \%main::myconfig;
 
1080   my $form     = $main::form;
 
1082   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
 
1084   my $defaults = selectfirst_hashref_query($form, $dbh, qq|SELECT * FROM defaults|) || {};
 
1086   $defaults->{weightunit} ||= 'kg';
 
1088   $main::lxdebug->leave_sub();
 
1094   $main::lxdebug->enter_sub();
 
1096   my ($self, $myconfig, $form) = @_;
 
1098   my $dbh = $form->dbconnect($myconfig);
 
1100   my $query = qq|SELECT closedto, max_future_booking_interval, revtrans FROM defaults|;
 
1101   my $sth   = $dbh->prepare($query);
 
1102   $sth->execute || $form->dberror($query);
 
1104   ($form->{closedto}, $form->{max_future_booking_interval}, $form->{revtrans}) = $sth->fetchrow_array;
 
1110   $main::lxdebug->leave_sub();
 
1114   $main::lxdebug->enter_sub();
 
1116   my ($self, $myconfig, $form) = @_;
 
1118   my $dbh = $form->dbconnect($myconfig);
 
1120   my ($query, @values);
 
1122   if ($form->{revtrans}) {
 
1123     $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '1'|;
 
1125   } elsif ($form->{closedto}) {
 
1126     $query = qq|UPDATE defaults SET closedto = ?, max_future_booking_interval = ?, revtrans = '0'|;
 
1127     @values = (conv_date($form->{closedto}), conv_date($form->{max_future_booking_interval}));
 
1130     $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '0'|;
 
1133   # set close in defaults
 
1134   do_query($form, $dbh, $query, @values);
 
1138   $main::lxdebug->leave_sub();
 
1142   my ($self, $units, $unit_name, $factor) = @_;
 
1144   $factor = 1 unless ($factor);
 
1146   my $unit = $units->{$unit_name};
 
1148   if (!defined($unit) || !$unit->{"base_unit"} ||
 
1149       ($unit_name eq $unit->{"base_unit"})) {
 
1150     return ($unit_name, $factor);
 
1153   return AM->get_base_unit($units, $unit->{"base_unit"}, $factor * $unit->{"factor"});
 
1156 sub retrieve_units {
 
1157   $main::lxdebug->enter_sub();
 
1159   my ($self, $myconfig, $form, $prefix) = @_;
 
1162   my $dbh = $form->get_standard_dbh;
 
1164   my $query = "SELECT *, base_unit AS original_base_unit FROM units";
 
1166   my $sth = prepare_execute_query($form, $dbh, $query);
 
1169   while (my $ref = $sth->fetchrow_hashref()) {
 
1170     $units->{$ref->{"name"}} = $ref;
 
1174   my $query_lang = "SELECT id, template_code FROM language ORDER BY description";
 
1175   $sth = $dbh->prepare($query_lang);
 
1176   $sth->execute() || $form->dberror($query_lang);
 
1178   while (my $ref = $sth->fetchrow_hashref()) {
 
1179     push(@languages, $ref);
 
1183   $query_lang = "SELECT ul.localized, ul.localized_plural, l.id, l.template_code " .
 
1184     "FROM units_language ul " .
 
1185     "LEFT JOIN language l ON ul.language_id = l.id " .
 
1186     "WHERE ul.unit = ?";
 
1187   $sth = $dbh->prepare($query_lang);
 
1189   foreach my $unit (values(%{$units})) {
 
1190     ($unit->{"${prefix}base_unit"}, $unit->{"${prefix}factor"}) = AM->get_base_unit($units, $unit->{"name"});
 
1192     $unit->{"LANGUAGES"} = {};
 
1193     foreach my $lang (@languages) {
 
1194       $unit->{"LANGUAGES"}->{$lang->{"template_code"}} = { "template_code" => $lang->{"template_code"} };
 
1197     $sth->execute($unit->{"name"}) || $form->dberror($query_lang . " (" . $unit->{"name"} . ")");
 
1198     while (my $ref = $sth->fetchrow_hashref()) {
 
1199       map({ $unit->{"LANGUAGES"}->{$ref->{"template_code"}}->{$_} = $ref->{$_} } keys(%{$ref}));
 
1204   $main::lxdebug->leave_sub();
 
1209 sub retrieve_all_units {
 
1210   $main::lxdebug->enter_sub();
 
1214   if (!$::request->{cache}{all_units}) {
 
1215     $::request->{cache}{all_units} = $self->retrieve_units(\%main::myconfig, $main::form);
 
1218   $main::lxdebug->leave_sub();
 
1220   return $::request->{cache}{all_units};
 
1224 sub translate_units {
 
1225   $main::lxdebug->enter_sub();
 
1227   my ($self, $form, $template_code, $unit, $amount) = @_;
 
1229   my $units = $self->retrieve_units(\%main::myconfig, $form);
 
1231   my $h = $units->{$unit}->{"LANGUAGES"}->{$template_code};
 
1232   my $new_unit = $unit;
 
1234     if (($amount != 1) && $h->{"localized_plural"}) {
 
1235       $new_unit = $h->{"localized_plural"};
 
1236     } elsif ($h->{"localized"}) {
 
1237       $new_unit = $h->{"localized"};
 
1241   $main::lxdebug->leave_sub();
 
1247   $main::lxdebug->enter_sub();
 
1249   my ($self, $myconfig, $form, $units) = @_;
 
1251   my $dbh = $form->dbconnect($myconfig);
 
1253   map({ $_->{"in_use"} = 0; } values(%{$units}));
 
1255   foreach my $unit (values(%{$units})) {
 
1256     my $base_unit = $unit->{"original_base_unit"};
 
1257     while ($base_unit) {
 
1258       $units->{$base_unit}->{"in_use"} = 1;
 
1259       $units->{$base_unit}->{"DEPENDING_UNITS"} = [] unless ($units->{$base_unit}->{"DEPENDING_UNITS"});
 
1260       push(@{$units->{$base_unit}->{"DEPENDING_UNITS"}}, $unit->{"name"});
 
1261       $base_unit = $units->{$base_unit}->{"original_base_unit"};
 
1265   foreach my $unit (values(%{$units})) {
 
1266     map({ $_ = $dbh->quote($_); } @{$unit->{"DEPENDING_UNITS"}});
 
1268     foreach my $table (qw(parts invoice orderitems)) {
 
1269       my $query = "SELECT COUNT(*) FROM $table WHERE unit ";
 
1271       if (0 == scalar(@{$unit->{"DEPENDING_UNITS"}})) {
 
1272         $query .= "= " . $dbh->quote($unit->{"name"});
 
1274         $query .= "IN (" . $dbh->quote($unit->{"name"}) . "," .
 
1275           join(",", map({ $dbh->quote($_) } @{$unit->{"DEPENDING_UNITS"}})) . ")";
 
1278       my ($count) = $dbh->selectrow_array($query);
 
1279       $form->dberror($query) if ($dbh->err);
 
1282         $unit->{"in_use"} = 1;
 
1290   $main::lxdebug->leave_sub();
 
1293 sub convertible_units {
 
1294   $main::lxdebug->enter_sub();
 
1298   my $filter_unit = shift;
 
1299   my $not_smaller = shift;
 
1301   my $conv_units = [];
 
1303   $filter_unit = $units->{$filter_unit};
 
1305   foreach my $name (sort { lc $a cmp lc $b } keys %{ $units }) {
 
1306     my $unit = $units->{$name};
 
1308     if (($unit->{base_unit} eq $filter_unit->{base_unit}) &&
 
1309         (!$not_smaller || ($unit->{factor} >= $filter_unit->{factor}))) {
 
1310       push @{$conv_units}, $unit;
 
1314   my @sorted = sort { $b->{factor} <=> $a->{factor} } @{ $conv_units };
 
1316   $main::lxdebug->leave_sub();
 
1321 # if $a is translatable to $b, return the factor between them.
 
1324   $main::lxdebug->enter_sub(2);
 
1325   my ($this, $a, $b, $all_units) = @_;
 
1328     $all_units = $this->retrieve_all_units;
 
1331   $main::lxdebug->leave_sub(2) and return 0 unless $a && $b;
 
1332   $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a} && $all_units->{$b};
 
1333   $main::lxdebug->leave_sub(2) and return 0 unless $all_units->{$a}{base_unit} eq $all_units->{$b}{base_unit};
 
1334   $main::lxdebug->leave_sub(2) and return $all_units->{$a}{factor} / $all_units->{$b}{factor};
 
1337 sub unit_select_data {
 
1338   $main::lxdebug->enter_sub();
 
1340   my ($self, $units, $selected, $empty_entry, $convertible_into) = @_;
 
1345     push(@{$select}, { "name" => "", "base_unit" => "", "factor" => "", "selected" => "" });
 
1348   foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
 
1349     if (!$convertible_into ||
 
1350         ($units->{$convertible_into} &&
 
1351          ($units->{$convertible_into}->{base_unit} eq $units->{$unit}->{base_unit}))) {
 
1352       push @{$select}, { "name"      => $unit,
 
1353                          "base_unit" => $units->{$unit}->{"base_unit"},
 
1354                          "factor"    => $units->{$unit}->{"factor"},
 
1355                          "selected"  => ($unit eq $selected) ? "selected" : "" };
 
1359   $main::lxdebug->leave_sub();
 
1364 sub unit_select_html {
 
1365   $main::lxdebug->enter_sub();
 
1367   my ($self, $units, $name, $selected, $convertible_into) = @_;
 
1369   my $select = "<select name=${name}>";
 
1371   foreach my $unit (sort({ $units->{$a}->{"sortkey"} <=> $units->{$b}->{"sortkey"} } keys(%{$units}))) {
 
1372     if (!$convertible_into ||
 
1373         ($units->{$convertible_into} &&
 
1374          ($units->{$convertible_into}->{"base_unit"} eq $units->{$unit}->{"base_unit"}))) {
 
1375       $select .= "<option" . (($unit eq $selected) ? " selected" : "") . ">${unit}</option>";
 
1378   $select .= "</select>";
 
1380   $main::lxdebug->leave_sub();
 
1386   $main::lxdebug->enter_sub();
 
1390   my $units = $self->retrieve_all_units();
 
1395   while (2 <= scalar(@_)) {
 
1396     my $qty  = shift(@_);
 
1397     my $unit = $units->{shift(@_)};
 
1399     croak "No unit defined with name $unit" if (!defined $unit);
 
1402       $base_unit = $unit->{base_unit};
 
1403     } elsif ($base_unit ne $unit->{base_unit}) {
 
1404       croak "Adding values with incompatible base units $base_unit/$unit->{base_unit}";
 
1407     $sum += $qty * $unit->{factor};
 
1410   $main::lxdebug->leave_sub();
 
1412   return wantarray ? ($sum, $base_unit) : $sum;
 
1416   $main::lxdebug->enter_sub();
 
1418   my ($self, $myconfig, $form, $name, $base_unit, $factor, $languages) = @_;
 
1420   my $dbh = $form->dbconnect_noauto($myconfig);
 
1422   my $query = qq|SELECT COALESCE(MAX(sortkey), 0) + 1 FROM units|;
 
1423   my ($sortkey) = selectrow_query($form, $dbh, $query);
 
1425   $query = "INSERT INTO units (name, base_unit, factor, sortkey) " .
 
1426     "VALUES (?, ?, ?, ?)";
 
1427   do_query($form, $dbh, $query, $name, $base_unit, $factor, $sortkey);
 
1430     $query = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
 
1431     my $sth = $dbh->prepare($query);
 
1432     foreach my $lang (@{$languages}) {
 
1433       my @values = ($name, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
 
1434       $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
 
1442   $main::lxdebug->leave_sub();
 
1446   $main::lxdebug->enter_sub();
 
1448   my ($self, $myconfig, $form, $units, $delete_units) = @_;
 
1450   my $dbh = $form->dbconnect_noauto($myconfig);
 
1452   my ($base_unit, $unit, $sth, $query);
 
1454   $query = "DELETE FROM units_language";
 
1455   $dbh->do($query) || $form->dberror($query);
 
1457   if ($delete_units && (0 != scalar(@{$delete_units}))) {
 
1458     $query = "DELETE FROM units WHERE name IN (";
 
1459     map({ $query .= "?," } @{$delete_units});
 
1460     substr($query, -1, 1) = ")";
 
1461     $dbh->do($query, undef, @{$delete_units}) ||
 
1462       $form->dberror($query . " (" . join(", ", @{$delete_units}) . ")");
 
1465   $query = "UPDATE units SET name = ?, base_unit = ?, factor = ? WHERE name = ?";
 
1466   $sth = $dbh->prepare($query);
 
1468   my $query_lang = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
 
1469   my $sth_lang = $dbh->prepare($query_lang);
 
1471   foreach $unit (values(%{$units})) {
 
1472     $unit->{"depth"} = 0;
 
1473     my $base_unit = $unit;
 
1474     while ($base_unit->{"base_unit"}) {
 
1476       $base_unit = $units->{$base_unit->{"base_unit"}};
 
1480   foreach $unit (sort({ $a->{"depth"} <=> $b->{"depth"} } values(%{$units}))) {
 
1481     if ($unit->{"LANGUAGES"}) {
 
1482       foreach my $lang (@{$unit->{"LANGUAGES"}}) {
 
1483         next unless ($lang->{"id"} && $lang->{"localized"});
 
1484         my @values = ($unit->{"name"}, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
 
1485         $sth_lang->execute(@values) || $form->dberror($query_lang . " (" . join(", ", @values) . ")");
 
1489     next if ($unit->{"unchanged_unit"});
 
1491     my @values = ($unit->{"name"}, $unit->{"base_unit"}, $unit->{"factor"}, $unit->{"old_name"});
 
1492     $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
 
1496   $sth_lang->finish();
 
1500   $main::lxdebug->leave_sub();
 
1504   $main::lxdebug->enter_sub();
 
1506   my ($self, $myconfig, $form) = @_;
 
1508   # connect to database
 
1509   my $dbh = $form->dbconnect($myconfig);
 
1511   my $query = qq|SELECT
 
1515                    round(t.rate * 100, 2) AS rate,
 
1516                    (SELECT accno FROM chart WHERE id = chart_id) AS taxnumber,
 
1517                    (SELECT description FROM chart WHERE id = chart_id) AS account_description
 
1519                  ORDER BY taxkey, rate|;
 
1521   my $sth = $dbh->prepare($query);
 
1522   $sth->execute || $form->dberror($query);
 
1525   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
1526     push @{ $form->{TAX} }, $ref;
 
1532   $main::lxdebug->leave_sub();
 
1535 sub get_tax_accounts {
 
1536   $main::lxdebug->enter_sub();
 
1538   my ($self, $myconfig, $form) = @_;
 
1540   my $dbh = $form->dbconnect($myconfig);
 
1542   # get Accounts from chart
 
1543   my $query = qq{ SELECT
 
1545                  accno || ' - ' || description AS taxaccount
 
1547                WHERE link LIKE '%_tax%'
 
1551   my $sth = $dbh->prepare($query);
 
1552   $sth->execute || $form->dberror($query);
 
1554   $form->{ACCOUNTS} = [];
 
1555   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
1556     push @{ $form->{ACCOUNTS} }, $ref;
 
1563   $main::lxdebug->leave_sub();
 
1567   $main::lxdebug->enter_sub();
 
1569   my ($self, $myconfig, $form) = @_;
 
1571   # connect to database
 
1572   my $dbh = $form->dbconnect($myconfig);
 
1574   my $query = qq|SELECT
 
1577                    round(rate * 100, 2) AS rate,
 
1580                    (id IN (SELECT tax_id
 
1581                            FROM acc_trans)) AS tax_already_used
 
1585   my $sth = $dbh->prepare($query);
 
1586   $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
1588   my $ref = $sth->fetchrow_hashref("NAME_lc");
 
1590   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
1594   # see if it is used by a taxkey
 
1595   $query = qq|SELECT count(*) FROM taxkeys
 
1596               WHERE tax_id = ? AND chart_id >0|;
 
1598   ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id});
 
1600   $form->{orphaned} = !$form->{orphaned};
 
1603   if (!$form->{orphaned} ) {
 
1604     $query = qq|SELECT DISTINCT c.id, c.accno
 
1606                 JOIN   tax t ON (t.id = tk.tax_id)
 
1607                 JOIN chart c ON (c.id = tk.chart_id)
 
1608                 WHERE tk.tax_id = ?|;
 
1610     $sth = $dbh->prepare($query);
 
1611     $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
 
1613     $form->{TAXINUSE} = [];
 
1614     while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
1615       push @{ $form->{TAXINUSE} }, $ref;
 
1623   $main::lxdebug->leave_sub();
 
1627   $main::lxdebug->enter_sub();
 
1629   my ($self, $myconfig, $form) = @_;
 
1632   # connect to database
 
1633   my $dbh = $form->get_standard_dbh($myconfig);
 
1635   $form->{rate} = $form->{rate} / 100;
 
1637   my $chart_categories = '';
 
1638   $chart_categories .= 'A' if $form->{asset};
 
1639   $chart_categories .= 'L' if $form->{liability};
 
1640   $chart_categories .= 'Q' if $form->{equity};
 
1641   $chart_categories .= 'I' if $form->{revenue};
 
1642   $chart_categories .= 'E' if $form->{expense};
 
1643   $chart_categories .= 'C' if $form->{costs};
 
1645   my @values = ($form->{taxkey}, $form->{taxdescription}, $form->{rate}, $form->{chart_id}, $form->{chart_id}, $chart_categories);
 
1646   if ($form->{id} ne "") {
 
1647     $query = qq|UPDATE tax SET
 
1652                   taxnumber      = (SELECT accno FROM chart WHERE id= ? ),
 
1653                   chart_categories = ?
 
1655     push(@values, $form->{id});
 
1659     $query = qq|INSERT INTO tax (
 
1667                 VALUES (?, ?, ?, ?, (SELECT accno FROM chart WHERE id = ?), ? )|;
 
1669   do_query($form, $dbh, $query, @values);
 
1673   $main::lxdebug->leave_sub();
 
1677   $main::lxdebug->enter_sub();
 
1679   my ($self, $myconfig, $form) = @_;
 
1682   # connect to database
 
1683   my $dbh = $form->get_standard_dbh($myconfig);
 
1685   $query = qq|DELETE FROM tax
 
1687   do_query($form, $dbh, $query, $form->{id});
 
1691   $main::lxdebug->leave_sub();
 
1694 sub save_price_factor {
 
1695   $main::lxdebug->enter_sub();
 
1697   my ($self, $myconfig, $form) = @_;
 
1699   # connect to database
 
1700   my $dbh = $form->get_standard_dbh($myconfig);
 
1703   my @values = ($form->{description}, conv_i($form->{factor}));
 
1706     $query = qq|UPDATE price_factors SET description = ?, factor = ? WHERE id = ?|;
 
1707     push @values, conv_i($form->{id});
 
1710     $query = qq|INSERT INTO price_factors (description, factor, sortkey) VALUES (?, ?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM price_factors))|;
 
1713   do_query($form, $dbh, $query, @values);
 
1717   $main::lxdebug->leave_sub();
 
1720 sub get_all_price_factors {
 
1721   $main::lxdebug->enter_sub();
 
1723   my ($self, $myconfig, $form) = @_;
 
1725   # connect to database
 
1726   my $dbh = $form->get_standard_dbh($myconfig);
 
1728   $form->{PRICE_FACTORS} = selectall_hashref_query($form, $dbh, qq|SELECT * FROM price_factors ORDER BY sortkey|);
 
1730   $main::lxdebug->leave_sub();
 
1733 sub get_price_factor {
 
1734   $main::lxdebug->enter_sub();
 
1736   my ($self, $myconfig, $form) = @_;
 
1738   # connect to database
 
1739   my $dbh = $form->get_standard_dbh($myconfig);
 
1741   my $query = qq|SELECT description, factor,
 
1742                    ((SELECT COUNT(*) FROM parts      WHERE price_factor_id = ?) +
 
1743                     (SELECT COUNT(*) FROM invoice    WHERE price_factor_id = ?) +
 
1744                     (SELECT COUNT(*) FROM orderitems WHERE price_factor_id = ?)) = 0 AS orphaned
 
1745                  FROM price_factors WHERE id = ?|;
 
1747   ($form->{description}, $form->{factor}, $form->{orphaned}) = selectrow_query($form, $dbh, $query, (conv_i($form->{id})) x 4);
 
1749   $main::lxdebug->leave_sub();
 
1752 sub delete_price_factor {
 
1753   $main::lxdebug->enter_sub();
 
1755   my ($self, $myconfig, $form) = @_;
 
1757   # connect to database
 
1758   my $dbh = $form->get_standard_dbh($myconfig);
 
1760   do_query($form, $dbh, qq|DELETE FROM price_factors WHERE id = ?|, conv_i($form->{id}));
 
1763   $main::lxdebug->leave_sub();
 
1766 sub save_warehouse {
 
1767   $main::lxdebug->enter_sub();
 
1769   my ($self, $myconfig, $form) = @_;
 
1771   # connect to database
 
1772   my $dbh = $form->get_standard_dbh($myconfig);
 
1774   my ($query, @values, $sth);
 
1777     $query        = qq|SELECT nextval('id')|;
 
1778     ($form->{id}) = selectrow_query($form, $dbh, $query);
 
1780     $query        = qq|INSERT INTO warehouse (id, sortkey) VALUES (?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM warehouse))|;
 
1781     do_query($form, $dbh, $query, $form->{id});
 
1784   do_query($form, $dbh, qq|UPDATE warehouse SET description = ?, invalid = ? WHERE id = ?|,
 
1785            $form->{description}, $form->{invalid} ? 't' : 'f', conv_i($form->{id}));
 
1787   if (0 < $form->{number_of_new_bins}) {
 
1788     $query = qq|INSERT INTO bin (warehouse_id, description) VALUES (?, ?)|;
 
1789     $sth   = prepare_query($form, $dbh, $query);
 
1791     foreach my $i (1..$form->{number_of_new_bins}) {
 
1792       do_statement($form, $sth, $query, conv_i($form->{id}), "$form->{prefix}${i}");
 
1800   $main::lxdebug->leave_sub();
 
1804   $main::lxdebug->enter_sub();
 
1806   my ($self, $myconfig, $form) = @_;
 
1808   # connect to database
 
1809   my $dbh = $form->get_standard_dbh($myconfig);
 
1811   my ($query, @values, $commit_necessary, $sth);
 
1813   @values = map { $form->{"id_${_}"} } grep { $form->{"delete_${_}"} } (1..$form->{rowcount});
 
1816     $query = qq|DELETE FROM bin WHERE id IN (| . join(', ', ('?') x scalar(@values)) . qq|)|;
 
1817     do_query($form, $dbh, $query, @values);
 
1819     $commit_necessary = 1;
 
1822   $query = qq|UPDATE bin SET description = ? WHERE id = ?|;
 
1823   $sth   = prepare_query($form, $dbh, $query);
 
1825   foreach my $row (1..$form->{rowcount}) {
 
1826     next if ($form->{"delete_${row}"});
 
1828     do_statement($form, $sth, $query, $form->{"description_${row}"}, conv_i($form->{"id_${row}"}));
 
1830     $commit_necessary = 1;
 
1835   $dbh->commit() if ($commit_necessary);
 
1837   $main::lxdebug->leave_sub();
 
1840 sub delete_warehouse {
 
1841   $main::lxdebug->enter_sub();
 
1843   my ($self, $myconfig, $form) = @_;
 
1845   # connect to database
 
1846   my $dbh = $form->get_standard_dbh($myconfig);
 
1848   my $id      = conv_i($form->{id});
 
1849   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|;
 
1850   my ($count) = selectrow_query($form, $dbh, $query, $id);
 
1853     $main::lxdebug->leave_sub();
 
1857   do_query($form, $dbh, qq|DELETE FROM bin       WHERE warehouse_id = ?|, conv_i($form->{id}));
 
1858   do_query($form, $dbh, qq|DELETE FROM warehouse WHERE id           = ?|, conv_i($form->{id}));
 
1862   $main::lxdebug->leave_sub();
 
1867 sub get_all_warehouses {
 
1868   $main::lxdebug->enter_sub();
 
1870   my ($self, $myconfig, $form) = @_;
 
1872   # connect to database
 
1873   my $dbh = $form->get_standard_dbh($myconfig);
 
1875   my $query = qq|SELECT w.id, w.description, w.invalid,
 
1876                    (SELECT COUNT(b.description) FROM bin b WHERE b.warehouse_id = w.id) AS number_of_bins
 
1878                  ORDER BY w.sortkey|;
 
1880   $form->{WAREHOUSES} = selectall_hashref_query($form, $dbh, $query);
 
1882   $main::lxdebug->leave_sub();
 
1886   $main::lxdebug->enter_sub();
 
1888   my ($self, $myconfig, $form) = @_;
 
1890   # connect to database
 
1891   my $dbh = $form->get_standard_dbh($myconfig);
 
1893   my $id    = conv_i($form->{id});
 
1894   my $query = qq|SELECT w.description, w.invalid
 
1898   my $ref   = selectfirst_hashref_query($form, $dbh, $query, $id);
 
1900   map { $form->{$_} = $ref->{$_} } keys %{ $ref };
 
1902   $query = qq|SELECT b.*, EXISTS
 
1903                 (SELECT i.warehouse_id, p.warehouse_id
 
1904                  FROM inventory i, parts p
 
1905                  WHERE i.bin_id = b.id
 
1910               WHERE b.warehouse_id = ?|;
 
1912   $form->{BINS} = selectall_hashref_query($form, $dbh, $query, conv_i($form->{id}));
 
1914   $main::lxdebug->leave_sub();