X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FAM.pm;h=e0caa823e46fb08eee7116661446c487e7d66e24;hb=db4680753f5ec1de62bdc0557f850afd810444d1;hp=a2b6ff9bc0fe48e30aa9fe01e2321dbd5f442bfd;hpb=468e86a69732eb57bd0a358ddecb6f80b8fb6b31;p=kivitendo-erp.git diff --git a/SL/AM.pm b/SL/AM.pm index a2b6ff9bc..e0caa823e 100644 --- a/SL/AM.pm +++ b/SL/AM.pm @@ -25,7 +25,8 @@ # GNU General Public License for more details. # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software -# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, +# MA 02110-1335, USA. #====================================================================== # # Administration module @@ -37,201 +38,180 @@ package AM; +use Carp; use Data::Dumper; +use Encode; +use List::MoreUtils qw(any); use SL::DBUtils; +use SL::DB::AuthUser; +use SL::DB::Default; +use SL::DB::Employee; +use SL::DB::Chart; +use SL::DB; +use SL::GenericTranslations; + +use strict; sub get_account { $main::lxdebug->enter_sub(); - my ($self, $myconfig, $form) = @_; - - # connect to database - my $dbh = $form->dbconnect($myconfig); - my $query = qq{ - SELECT c.accno, c.description, c.charttype, c.category, - c.link, c.pos_bilanz, c.pos_eur, c.new_chart_id, c.valid_from, - c.pos_bwa, datevautomatik, - tk.taxkey_id, tk.pos_ustva, tk.tax_id, - tk.tax_id || '--' || tk.taxkey_id AS tax, tk.startdate - FROM chart c - LEFT JOIN taxkeys tk - ON (c.id=tk.chart_id AND tk.id = - (SELECT id FROM taxkeys - WHERE taxkeys.chart_id = c.id AND startdate <= current_date - ORDER BY startdate DESC LIMIT 1)) - WHERE c.id = ? - }; - - - $main::lxdebug->message(LXDebug::QUERY, "\$query=\n $query"); - my $sth = $dbh->prepare($query); - $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})"); - - my $ref = $sth->fetchrow_hashref(NAME_lc); - - foreach my $key (keys %$ref) { - $form->{"$key"} = $ref->{"$key"}; - } + # fetch chart-related data and set form fields + # get_account is called by add_account in am.pl + # always sets $form->{TAXKEY} and default_accounts + # loads chart data when $form->{id} is passed - $sth->finish; + my ($self, $myconfig, $form) = @_; # get default accounts - $query = qq|SELECT inventory_accno_id, income_accno_id, expense_accno_id - FROM defaults|; - $main::lxdebug->message(LXDebug::QUERY, "\$query=\n $query"); - $sth = $dbh->prepare($query); - $sth->execute || $form->dberror($query); - - $ref = $sth->fetchrow_hashref(NAME_lc); - - map { $form->{$_} = $ref->{$_} } keys %ref; - - $sth->finish; - - - - # get taxkeys and description - $query = qq{ - SELECT - id, - (SELECT accno FROM chart WHERE id=tax.chart_id) AS chart_accno, - taxkey, - id||'--'||taxkey AS tax, - taxdescription, - rate - FROM tax ORDER BY taxkey - }; - $main::lxdebug->message(LXDebug::QUERY, "\$query=\n $query"); - $sth = $dbh->prepare($query); - $sth->execute || $form->dberror($query); + map { $form->{$_} = $::instance_conf->{$_} } qw(inventory_accno_id income_accno_id expense_accno_id); + require SL::DB::Tax; + my $taxes = SL::DB::Manager::Tax->get_all( with_objects => ['chart'] , sort_by => 'taxkey' ); $form->{TAXKEY} = []; + foreach my $tk ( @{$taxes} ) { + push @{ $form->{TAXKEY} }, { id => $tk->id, + chart_accno => $tk->chart_id ? $tk->chart->accno : undef, + taxkey => $tk->taxkey, + tax => $tk->id . '--' . $tk->taxkey, + rate => $tk->rate + }; + }; - while (my $ref = $sth->fetchrow_hashref(NAME_lc)) { - push @{ $form->{TAXKEY} }, $ref; - } - - $sth->finish; if ($form->{id}) { - # get new accounts - $query = qq|SELECT id, accno,description - FROM chart - WHERE link = ? - ORDER BY accno|; - $main::lxdebug->message(LXDebug::QUERY, "\$query=\n $query"); - $sth = $dbh->prepare($query); - $sth->execute($form->{link}) || $form->dberror($query . " ($form->{link})"); - $form->{NEWACCOUNT} = []; - while (my $ref = $sth->fetchrow_hashref(NAME_lc)) { - push @{ $form->{NEWACCOUNT} }, $ref; + my $chart_obj = SL::DB::Manager::Chart->find_by(id => $form->{id}) || die "Can't open chart"; + + my @chart_fields = qw(accno description charttype category link pos_bilanz + pos_eur pos_er new_chart_id valid_from pos_bwa datevautomatik); + foreach my $cf ( @chart_fields ) { + $form->{"$cf"} = $chart_obj->$cf; } - $sth->finish; + my $active_taxkey = $chart_obj->get_active_taxkey; + $form->{$_} = $active_taxkey->$_ foreach qw(taxkey_id pos_ustva tax_id startdate); + $form->{tax} = $active_taxkey->tax_id . '--' . $active_taxkey->taxkey_id; - # get the taxkeys of account - - $query = qq{ - SELECT - tk.id, - tk.chart_id, - c.accno, - tk.tax_id, - t.taxdescription, - t.rate, - tk.taxkey_id, - tk.pos_ustva, - tk.startdate - FROM taxkeys tk - LEFT JOIN tax t ON (t.id = tk.tax_id) - LEFT JOIN chart c ON (c.id = t.chart_id) - - WHERE tk.chart_id = ? - ORDER BY startdate DESC - }; - $main::lxdebug->message(LXDebug::QUERY, "\$query=\n $query"); - $sth = $dbh->prepare($query); + # check if there are any transactions for this chart + $form->{orphaned} = $chart_obj->has_transaction ? 0 : 1; - $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})"); + # check if new account is active + # The old sql query was broken since at least 2006 and always returned 0 + $form->{new_chart_valid} = $chart_obj->new_chart_valid; + # get the taxkeys of the account $form->{ACCOUNT_TAXKEYS} = []; - - while (my $ref = $sth->fetchrow_hashref(NAME_lc)) { - push @{ $form->{ACCOUNT_TAXKEYS} }, $ref; + foreach my $taxkey ( @{ $chart_obj->taxkeys } ) { + push @{ $form->{ACCOUNT_TAXKEYS} }, { id => $taxkey->id, + chart_id => $taxkey->chart_id, + tax_id => $taxkey->tax_id, + taxkey_id => $taxkey->taxkey_id, + pos_ustva => $taxkey->pos_ustva, + startdate => $taxkey->startdate->to_kivitendo, + taxdescription => $taxkey->tax->taxdescription, + rate => $taxkey->tax->rate, + accno => defined $taxkey->tax->chart_id ? $taxkey->tax->chart->accno : undef, + }; } - $sth->finish; - - } - # check if we have any transactions - $query = qq|SELECT a.trans_id FROM acc_trans a - WHERE a.chart_id = ?|; - $main::lxdebug->message(LXDebug::QUERY, "\$query=\n $query"); - $sth = $dbh->prepare($query); - $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})"); - - ($form->{orphaned}) = $sth->fetchrow_array; - $form->{orphaned} = !$form->{orphaned}; - $sth->finish; - - # check if new account is active - $form->{new_chart_valid} = 0; - if ($form->{new_chart_id}) { - $query = qq|SELECT current_date-valid_from FROM chart - WHERE id = ?|; - $main::lxdebug->message(LXDebug::QUERY, "\$query=\n $query"); - my ($count) = selectrow_query($form, $dbh, $query, $form->{id}); - if ($count >=0) { - $form->{new_chart_valid} = 1; - } - $sth->finish; - } + # get new accounts (Folgekonto). Find all charts with the same link + $form->{NEWACCOUNT} = $chart_obj->db->dbh->selectall_arrayref('select id, accno,description from chart where link = ? order by accno', {Slice => {}}, $chart_obj->link); - $dbh->disconnect; + } else { # set to orphaned for new charts, so chart_type can be changed (needed by $AccountIsPosted) + $form->{orphaned} = 1; + }; $main::lxdebug->leave_sub(); } sub save_account { + my ($self, $myconfig, $form) = @_; $main::lxdebug->enter_sub(); + my $rc = SL::DB->client->with_transaction(\&_save_account, $self, $myconfig, $form); + + $::lxdebug->leave_sub; + return $rc; +} + +sub _save_account { + # TODO: it should be forbidden to change an account to a heading if there + # have been bookings to this account in the past + my ($self, $myconfig, $form) = @_; - # connect to database, turn off AutoCommit - my $dbh = $form->dbconnect_noauto($myconfig); + my $dbh = SL::DB->client->dbh; + + for (qw(AR_include_in_dropdown AP_include_in_dropdown summary_account)) { + $form->{$form->{$_}} = $form->{$_} if $form->{$_}; + } # sanity check, can't have AR with AR_... if ($form->{AR} || $form->{AP} || $form->{IC}) { - map { delete $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 CT_tax); + 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)) { + $form->error($::locale->text('It is not allowed that a summary account occurs in a drop-down menu!')); + } } - $form->{link} = ""; - foreach my $item ($form->{AR}, $form->{AR_amount}, - $form->{AR_tax}, $form->{AR_paid}, - $form->{AP}, $form->{AP_amount}, - $form->{AP_tax}, $form->{AP_paid}, - $form->{IC}, $form->{IC_sale}, - $form->{IC_cogs}, $form->{IC_taxpart}, - $form->{IC_income}, $form->{IC_expense}, - $form->{IC_taxservice}, $form->{CT_tax} - ) { - $form->{link} .= "${item}:" if ($item); - } - chop $form->{link}; + my @link_order = qw(AR AR_amount AR_tax AR_paid AP AP_amount AP_tax AP_paid IC IC_sale IC_cogs IC_taxpart IC_income IC_expense IC_taxservice); + $form->{link} = join ':', grep $_, map $form->{$_}, @link_order; # strip blanks from accno map { $form->{$_} =~ s/ //g; } qw(accno); + # collapse multiple (horizontal) whitespace in chart description (Ticket 148) + map { $form->{$_} =~ s/\h+/ /g } qw(description); + my ($query, $sth); if ($form->{id} eq "NULL") { $form->{id} = ""; } - my @values; + $query = ' + SELECT accno + FROM chart + WHERE accno = ?'; + + my @values = ($form->{accno}); + + if ( $form->{id} ) { + $query .= ' AND NOT id = ?'; + push(@values, $form->{id}); + } + + my ($accno) = selectrow_query($form, $dbh, $query, @values); + + if ($accno) { + $form->error($::locale->text('Account number not unique!')); + } + + + if (!$form->{id} || $form->{id} eq "") { + $query = qq|SELECT nextval('id')|; + ($form->{"id"}) = selectrow_query($form, $dbh, $query); + $query = qq|INSERT INTO chart (id, accno, link) VALUES (?, ?, ?)|; + do_query($form, $dbh, $query, $form->{"id"}, $form->{"accno"}, ''); + } + + @values = (); + if ($form->{id}) { + + # if charttype is heading make sure certain values are empty + # specifically, if charttype is changed from an existing account, empty the + # fields unnecessary for headings, so that e.g. heading doesn't appear in + # drop-down menues due to still having a valid "link" entry + + if ( $form->{charttype} eq 'H' ) { + $form->{link} = ''; + $form->{pos_bwa} = ''; + $form->{pos_bilanz} = ''; + $form->{pos_eur} = ''; + $form->{new_chart_id} = ''; + $form->{valid_from} = ''; + }; + $query = qq|UPDATE chart SET accno = ?, description = ?, @@ -241,6 +221,7 @@ sub save_account { pos_bwa = ?, pos_bilanz = ?, pos_eur = ?, + pos_er = ?, new_chart_id = ?, valid_from = ?, datevautomatik = ? @@ -255,57 +236,13 @@ sub save_account { conv_i($form->{pos_bwa}), conv_i($form->{pos_bilanz}), conv_i($form->{pos_eur}), + conv_i($form->{pos_er}), conv_i($form->{new_chart_id}), conv_date($form->{valid_from}), ($form->{datevautomatik} eq 'T') ? 'true':'false', $form->{id}, ); - } - elsif ($form->{id} && !$form->{new_chart_valid}) { - - $query = qq| - UPDATE chart - SET new_chart_id = ?, - valid_from = ? - WHERE id = ? - |; - - @values = ( - conv_i($form->{new_chart_id}), - conv_date($form->{valid_from}), - $form->{id} - ); - } - else { - - $query = qq| - INSERT INTO chart ( - accno, - description, - charttype, - category, - link, - pos_bwa, - pos_bilanz, - pos_eur, - new_chart_id, - valid_from, - datevautomatik ) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) - |; - - @values = ( - $form->{accno}, - $form->{description}, - $form->{charttype}, - $form->{category}, $form->{link}, - conv_i($form->{pos_bwa}), - conv_i($form->{pos_bilanz}), conv_i($form->{pos_eur}), - conv_i($form->{new_chart_id}), - conv_date($form->{valid_from}), - ($form->{datevautomatik} eq 'T') ? 'true':'false', - ); } @@ -324,6 +261,9 @@ sub save_account { # Loop control # Check if the account already exists, else cancel + + print(STDERR "Keine Taxkeys weil ID =: $form->{id}\n"); + last READTAXKEYS if ( $form->{'id'} == 0); # check if there is a startdate @@ -410,30 +350,44 @@ sub save_account { } - # commit - my $rc = $dbh->commit; - $dbh->disconnect; + # Update chart.taxkey_id to the latest from taxkeys for this chart. + $query = <leave_sub(); + do_query($form, $dbh, $query, $form->{id}); - return $rc; + return 1; } sub delete_account { + my ($self, $myconfig, $form) = @_; $main::lxdebug->enter_sub(); + my $rc = SL::DB->client->with_transaction(\&_delete_account, $self, $myconfig, $form); + + $::lxdebug->leave_sub; + return $rc; +} + +sub _delete_account { my ($self, $myconfig, $form) = @_; - # connect to database, turn off AutoCommit - my $dbh = $form->dbconnect_noauto($myconfig); + my $dbh = SL::DB->client->dbh; my $query = qq|SELECT count(*) FROM acc_trans a WHERE a.chart_id = ?|; my ($count) = selectrow_query($form, $dbh, $query, $form->{id}); if ($count) { - $dbh->disconnect; - $main::lxdebug->leave_sub(); return; } @@ -446,15 +400,8 @@ sub delete_account { do_query($form, $dbh, $query, $form->{id}); } - foreach my $table (qw(partstax customertax vendortax tax)) { - $query = qq|DELETE FROM $table - WHERE chart_id = ?|; - do_query($form, $dbh, $query, $form->{id}); - } - - # delete chart of account record - $query = qq|DELETE FROM chart - WHERE id = ?|; + $query = qq|DELETE FROM tax + WHERE chart_id = ?|; do_query($form, $dbh, $query, $form->{id}); # delete account taxkeys @@ -462,965 +409,268 @@ sub delete_account { WHERE chart_id = ?|; do_query($form, $dbh, $query, $form->{id}); - # commit and redirect - my $rc = $dbh->commit; - $dbh->disconnect; - - $main::lxdebug->leave_sub(); + # delete chart of account record + # last step delete chart, because we have a constraint + # to taxkeys + $query = qq|DELETE FROM chart + WHERE id = ?|; + do_query($form, $dbh, $query, $form->{id}); - return $rc; + return 1; } -sub departments { +sub lead { $main::lxdebug->enter_sub(); my ($self, $myconfig, $form) = @_; - # connect to database - my $dbh = $form->dbconnect($myconfig); + my $dbh = SL::DB->client->dbh; - my $query = qq|SELECT d.id, d.description, d.role - FROM department d + my $query = qq|SELECT id, lead + FROM leads ORDER BY 2|; - $sth = $dbh->prepare($query); + my $sth = $dbh->prepare($query); $sth->execute || $form->dberror($query); - $form->{ALL} = []; - while (my $ref = $sth->fetchrow_hashref(NAME_lc)) { + while (my $ref = $sth->fetchrow_hashref("NAME_lc")) { push @{ $form->{ALL} }, $ref; } $sth->finish; - $dbh->disconnect; $main::lxdebug->leave_sub(); } -sub get_department { +sub get_lead { $main::lxdebug->enter_sub(); my ($self, $myconfig, $form) = @_; - # connect to database - my $dbh = $form->dbconnect($myconfig); + my $dbh = SL::DB->client->dbh; - my $query = qq|SELECT d.description, d.role - FROM department d - WHERE d.id = ?|; + my $query = + qq|SELECT l.id, l.lead | . + qq|FROM leads l | . + qq|WHERE l.id = ?|; my $sth = $dbh->prepare($query); $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})"); - my $ref = $sth->fetchrow_hashref(NAME_lc); + my $ref = $sth->fetchrow_hashref("NAME_lc"); map { $form->{$_} = $ref->{$_} } keys %$ref; $sth->finish; - # see if it is in use - $query = qq|SELECT count(*) FROM dpt_trans d - WHERE d.department_id = ?|; - ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id}); - - $form->{orphaned} = !$form->{orphaned}; - $sth->finish; - - $dbh->disconnect; - $main::lxdebug->leave_sub(); } -sub save_department { +sub save_lead { $main::lxdebug->enter_sub(); my ($self, $myconfig, $form) = @_; + my ($query); - # connect to database - my $dbh = $form->dbconnect($myconfig); + my $dbh = SL::DB->client->dbh; - my @values = ($form->{description}, $form->{role}); + my @values = ($form->{description}); + # id is the old record if ($form->{id}) { - $query = qq|UPDATE department SET - description = ?, role = ? + $query = qq|UPDATE leads SET + lead = ? WHERE id = ?|; push(@values, $form->{id}); } else { - $query = qq|INSERT INTO department - (description, role) - VALUES (?, ?)|; + $query = qq|INSERT INTO leads + (lead) + VALUES (?)|; } do_query($form, $dbh, $query, @values); - $dbh->disconnect; - $main::lxdebug->leave_sub(); } -sub delete_department { +sub delete_lead { $main::lxdebug->enter_sub(); my ($self, $myconfig, $form) = @_; + my ($query); - # connect to database - my $dbh = $form->dbconnect($myconfig); - - $query = qq|DELETE FROM department - WHERE id = ?|; - do_query($form, $dbh, $query, $form->{id}); - - $dbh->disconnect; + SL::DB->client->with_transaction(sub { + $query = qq|DELETE FROM leads WHERE id = ?|; + do_query($form, SL::DB->client->dbh, $query, $form->{id}); + 1; + }) or do { die SL::DB->client->error }; $main::lxdebug->leave_sub(); } -sub lead { +sub language { $main::lxdebug->enter_sub(); - my ($self, $myconfig, $form) = @_; + my ($self, $myconfig, $form, $return_list) = @_; - # connect to database - my $dbh = $form->dbconnect($myconfig); + my $dbh = SL::DB->client->dbh; - my $query = qq|SELECT id, lead - FROM leads - ORDER BY 2|; + my $query = + "SELECT id, description, template_code, article_code, " . + " output_numberformat, output_dateformat, output_longdates " . + "FROM language ORDER BY description"; - $sth = $dbh->prepare($query); + my $sth = $dbh->prepare($query); $sth->execute || $form->dberror($query); - while (my $ref = $sth->fetchrow_hashref(NAME_lc)) { - push @{ $form->{ALL} }, $ref; + my $ary = []; + + while (my $ref = $sth->fetchrow_hashref("NAME_lc")) { + push(@{ $ary }, $ref); } $sth->finish; - $dbh->disconnect; $main::lxdebug->leave_sub(); + + if ($return_list) { + return @{$ary}; + } else { + $form->{ALL} = $ary; + } } -sub get_lead { +sub get_language { $main::lxdebug->enter_sub(); my ($self, $myconfig, $form) = @_; - # connect to database - my $dbh = $form->dbconnect($myconfig); + my $dbh = SL::DB->client->dbh; my $query = - qq|SELECT l.id, l.lead | . - qq|FROM leads l | . - qq|WHERE l.id = ?|; + "SELECT description, template_code, article_code, " . + " output_numberformat, output_dateformat, output_longdates " . + "FROM language WHERE id = ?"; my $sth = $dbh->prepare($query); - $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})"); + $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})"); - my $ref = $sth->fetchrow_hashref(NAME_lc); + my $ref = $sth->fetchrow_hashref("NAME_lc"); map { $form->{$_} = $ref->{$_} } keys %$ref; $sth->finish; - $dbh->disconnect; - $main::lxdebug->leave_sub(); } -sub save_lead { +sub get_language_details { $main::lxdebug->enter_sub(); - my ($self, $myconfig, $form) = @_; - - # connect to database - my $dbh = $form->dbconnect($myconfig); + my ($self, $myconfig, $form, $id) = @_; - my @values = ($form->{description}); - # id is the old record - if ($form->{id}) { - $query = qq|UPDATE leads SET - lead = ? - WHERE id = ?|; - puhs(@values, $form->{id}); - } else { - $query = qq|INSERT INTO leads - (lead) - VALUES (?)|; - } - do_query($form, $dbh, $query, @values); + my $dbh = SL::DB->client->dbh; - $dbh->disconnect; + my $query = + "SELECT template_code, " . + " output_numberformat, output_dateformat, output_longdates " . + "FROM language WHERE id = ?"; + my @res = selectrow_query($form, $dbh, $query, $id); $main::lxdebug->leave_sub(); + + return @res; } -sub delete_lead { +sub save_language { $main::lxdebug->enter_sub(); my ($self, $myconfig, $form) = @_; - # connect to database - my $dbh = $form->dbconnect($myconfig); - - $query = qq|DELETE FROM leads - WHERE id = ?|; - do_query($form, $dbh, $query, $form->{id}); - - $dbh->disconnect; + SL::DB->client->with_transaction(sub { + my $dbh = SL::DB->client->dbh; + my (@values, $query); + + map({ push(@values, $form->{$_}); } + qw(description template_code article_code + output_numberformat output_dateformat output_longdates)); + + # id is the old record + if ($form->{id}) { + $query = + "UPDATE language SET " . + " description = ?, template_code = ?, article_code = ?, " . + " output_numberformat = ?, output_dateformat = ?, " . + " output_longdates = ? " . + "WHERE id = ?"; + push(@values, $form->{id}); + } else { + $query = + "INSERT INTO language (" . + " description, template_code, article_code, " . + " output_numberformat, output_dateformat, output_longdates" . + ") VALUES (?, ?, ?, ?, ?, ?)"; + } + do_query($form, $dbh, $query, @values); + 1; + }) or do { die SL::DB->client->error }; $main::lxdebug->leave_sub(); } -sub business { +sub delete_language { $main::lxdebug->enter_sub(); my ($self, $myconfig, $form) = @_; + my $query; - # connect to database - my $dbh = $form->dbconnect($myconfig); - - my $query = qq|SELECT id, description, discount, customernumberinit - FROM business - ORDER BY 2|; - - $sth = $dbh->prepare($query); - $sth->execute || $form->dberror($query); + SL::DB->client->with_transaction(sub { + my $dbh = SL::DB->client->dbh; - while (my $ref = $sth->fetchrow_hashref(NAME_lc)) { - push @{ $form->{ALL} }, $ref; - } + foreach my $table (qw(generic_translations units_language)) { + $query = qq|DELETE FROM $table WHERE language_id = ?|; + do_query($form, $dbh, $query, $form->{"id"}); + } - $sth->finish; - $dbh->disconnect; + $query = "DELETE FROM language WHERE id = ?"; + do_query($form, $dbh, $query, $form->{"id"}); + 1; + }) or do { die SL::DB->client->error }; $main::lxdebug->leave_sub(); } -sub get_business { +sub prepare_template_filename { $main::lxdebug->enter_sub(); my ($self, $myconfig, $form) = @_; - # connect to database - my $dbh = $form->dbconnect($myconfig); + my ($filename, $display_filename); - my $query = - qq|SELECT b.description, b.discount, b.customernumberinit - FROM business b - WHERE b.id = ?|; - my $sth = $dbh->prepare($query); - $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})"); + if ($form->{type} eq "stylesheet") { + $filename = "css/$myconfig->{stylesheet}"; + $display_filename = $myconfig->{stylesheet}; - my $ref = $sth->fetchrow_hashref(NAME_lc); + } else { + $filename = $form->{formname}; - map { $form->{$_} = $ref->{$_} } keys %$ref; + if ($form->{language}) { + my ($id, $template_code) = split(/--/, $form->{language}); + $filename .= "_${template_code}"; + } - $sth->finish; + if ($form->{printer}) { + my ($id, $template_code) = split(/--/, $form->{printer}); + $filename .= "_${template_code}"; + } - $dbh->disconnect; + $filename .= "." . ($form->{format} eq "html" ? "html" : "tex"); + if ($form->{"formname"} =~ m|\.\.| || $form->{"formname"} =~ m|^/|) { + $filename =~ s|.*/||; + } + $display_filename = $filename; + $filename = SL::DB::Default->get->templates . "/$filename"; + } $main::lxdebug->leave_sub(); -} - -sub save_business { - $main::lxdebug->enter_sub(); - - my ($self, $myconfig, $form) = @_; - - # connect to database - my $dbh = $form->dbconnect($myconfig); - - my @values = ($form->{description}, $form->{discount}, - $form->{customernumberinit}); - # id is the old record - if ($form->{id}) { - $query = qq|UPDATE business SET - description = ?, - discount = ?, - customernumberinit = ? - WHERE id = ?|; - push(@values, $form->{id}); - } else { - $query = qq|INSERT INTO business - (description, discount, customernumberinit) - VALUES (?, ?, ?)|; - } - do_query($form, $dbh, $query, @values); - - $dbh->disconnect; - - $main::lxdebug->leave_sub(); -} - -sub delete_business { - $main::lxdebug->enter_sub(); - - my ($self, $myconfig, $form) = @_; - - # connect to database - my $dbh = $form->dbconnect($myconfig); - - $query = qq|DELETE FROM business - WHERE id = ?|; - do_query($form, $dbh, $query, $form->{id}); - - $dbh->disconnect; - - $main::lxdebug->leave_sub(); -} - - -sub language { - $main::lxdebug->enter_sub(); - - my ($self, $myconfig, $form, $return_list) = @_; - - # connect to database - my $dbh = $form->dbconnect($myconfig); - - my $query = - "SELECT id, description, template_code, article_code, " . - " output_numberformat, output_dateformat, output_longdates " . - "FROM language ORDER BY description"; - - $sth = $dbh->prepare($query); - $sth->execute || $form->dberror($query); - - my $ary = []; - - while (my $ref = $sth->fetchrow_hashref(NAME_lc)) { - push(@{ $ary }, $ref); - } - - $sth->finish; - $dbh->disconnect; - - $main::lxdebug->leave_sub(); - - if ($return_list) { - return @{$ary}; - } else { - $form->{ALL} = $ary; - } -} - -sub get_language { - $main::lxdebug->enter_sub(); - - my ($self, $myconfig, $form) = @_; - - # connect to database - my $dbh = $form->dbconnect($myconfig); - - my $query = - "SELECT description, template_code, article_code, " . - " output_numberformat, output_dateformat, output_longdates " . - "FROM language WHERE id = ?"; - my $sth = $dbh->prepare($query); - $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})"); - - my $ref = $sth->fetchrow_hashref(NAME_lc); - - map { $form->{$_} = $ref->{$_} } keys %$ref; - - $sth->finish; - - $dbh->disconnect; - - $main::lxdebug->leave_sub(); -} - -sub get_language_details { - $main::lxdebug->enter_sub(); - - my ($self, $myconfig, $form, $id) = @_; - - # connect to database - my $dbh = $form->dbconnect($myconfig); - - my $query = - "SELECT template_code, " . - " output_numberformat, output_dateformat, output_longdates " . - "FROM language WHERE id = ?"; - my @res = selectrow_query($form, $dbh, $query, $id); - $dbh->disconnect; - - $main::lxdebug->leave_sub(); - - return @res; -} - -sub save_language { - $main::lxdebug->enter_sub(); - - my ($self, $myconfig, $form) = @_; - - # connect to database - my $dbh = $form->dbconnect($myconfig); - my (@values, $query); - - map({ push(@values, $form->{$_}); } - qw(description template_code article_code - output_numberformat output_dateformat output_longdates)); - - # id is the old record - if ($form->{id}) { - $query = - "UPDATE language SET " . - " description = ?, template_code = ?, article_code = ?, " . - " output_numberformat = ?, output_dateformat = ?, " . - " output_longdates = ? " . - "WHERE id = ?"; - push(@values, $form->{id}); - } else { - $query = - "INSERT INTO language (" . - " description, template_code, article_code, " . - " output_numberformat, output_dateformat, output_longdates" . - ") VALUES (?, ?, ?, ?, ?, ?)"; - } - do_query($form, $dbh, $query, @values); - - $dbh->disconnect; - - $main::lxdebug->leave_sub(); -} - -sub delete_language { - $main::lxdebug->enter_sub(); - - my ($self, $myconfig, $form) = @_; - - # connect to database - my $dbh = $form->dbconnect_noauto($myconfig); - - foreach my $table (qw(translation_payment_terms units_language)) { - my $query = qq|DELETE FROM $table WHERE language_id = ?|; - do_query($form, $dbh, $query, $form->{"id"}); - } - - $query = "DELETE FROM language WHERE id = ?"; - do_query($form, $dbh, $query, $form->{"id"}); - - $dbh->commit(); - $dbh->disconnect; - - $main::lxdebug->leave_sub(); -} - - -sub buchungsgruppe { - $main::lxdebug->enter_sub(); - - my ($self, $myconfig, $form) = @_; - - # connect to database - my $dbh = $form->dbconnect($myconfig); - - my $query = qq|SELECT id, description, - inventory_accno_id, - (SELECT accno FROM chart WHERE id = inventory_accno_id) AS inventory_accno, - income_accno_id_0, - (SELECT accno FROM chart WHERE id = income_accno_id_0) AS income_accno_0, - expense_accno_id_0, - (SELECT accno FROM chart WHERE id = expense_accno_id_0) AS expense_accno_0, - income_accno_id_1, - (SELECT accno FROM chart WHERE id = income_accno_id_1) AS income_accno_1, - expense_accno_id_1, - (SELECT accno FROM chart WHERE id = expense_accno_id_1) AS expense_accno_1, - income_accno_id_2, - (SELECT accno FROM chart WHERE id = income_accno_id_2) AS income_accno_2, - expense_accno_id_2, - (select accno FROM chart WHERE id = expense_accno_id_2) AS expense_accno_2, - income_accno_id_3, - (SELECT accno FROM chart WHERE id = income_accno_id_3) AS income_accno_3, - expense_accno_id_3, - (SELECT accno FROM chart WHERE id = expense_accno_id_3) AS expense_accno_3 - FROM buchungsgruppen - ORDER BY sortkey|; - - $sth = $dbh->prepare($query); - $sth->execute || $form->dberror($query); - - $form->{ALL} = []; - while (my $ref = $sth->fetchrow_hashref(NAME_lc)) { - push @{ $form->{ALL} }, $ref; - } - - $sth->finish; - $dbh->disconnect; - - $main::lxdebug->leave_sub(); -} - -sub get_buchungsgruppe { - $main::lxdebug->enter_sub(); - - my ($self, $myconfig, $form) = @_; - my $query; - - # connect to database - my $dbh = $form->dbconnect($myconfig); - - if ($form->{id}) { - $query = - qq|SELECT description, inventory_accno_id, - (SELECT accno FROM chart WHERE id = inventory_accno_id) AS inventory_accno, - income_accno_id_0, - (SELECT accno FROM chart WHERE id = income_accno_id_0) AS income_accno_0, - expense_accno_id_0, - (SELECT accno FROM chart WHERE id = expense_accno_id_0) AS expense_accno_0, - income_accno_id_1, - (SELECT accno FROM chart WHERE id = income_accno_id_1) AS income_accno_1, - expense_accno_id_1, - (SELECT accno FROM chart WHERE id = expense_accno_id_1) AS expense_accno_1, - income_accno_id_2, - (SELECT accno FROM chart WHERE id = income_accno_id_2) AS income_accno_2, - expense_accno_id_2, - (select accno FROM chart WHERE id = expense_accno_id_2) AS expense_accno_2, - income_accno_id_3, - (SELECT accno FROM chart WHERE id = income_accno_id_3) AS income_accno_3, - expense_accno_id_3, - (SELECT accno FROM chart WHERE id = expense_accno_id_3) AS expense_accno_3 - FROM buchungsgruppen - WHERE id = ?|; - my $sth = $dbh->prepare($query); - $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})"); - - my $ref = $sth->fetchrow_hashref(NAME_lc); - - map { $form->{$_} = $ref->{$_} } keys %$ref; - - $sth->finish; - - $query = - qq|SELECT count(id) = 0 AS orphaned - FROM parts - WHERE buchungsgruppen_id = ?|; - ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id}); - } - - $query = "SELECT inventory_accno_id, income_accno_id, expense_accno_id ". - "FROM defaults"; - ($form->{"std_inventory_accno_id"}, $form->{"std_income_accno_id"}, - $form->{"std_expense_accno_id"}) = selectrow_query($form, $dbh, $query); - - my $module = "IC"; - $query = qq|SELECT c.accno, c.description, c.link, c.id, - d.inventory_accno_id, d.income_accno_id, d.expense_accno_id - FROM chart c, defaults d - WHERE c.link LIKE '%$module%' - ORDER BY c.accno|; - - - my $sth = $dbh->prepare($query); - $sth->execute || $form->dberror($query); - while (my $ref = $sth->fetchrow_hashref(NAME_lc)) { - foreach my $key (split(/:/, $ref->{link})) { - if (!$form->{"std_inventory_accno_id"} && ($key eq "IC")) { - $form->{"std_inventory_accno_id"} = $ref->{"id"}; - } - if ($key =~ /$module/) { - if ( ($ref->{id} eq $ref->{inventory_accno_id}) - || ($ref->{id} eq $ref->{income_accno_id}) - || ($ref->{id} eq $ref->{expense_accno_id})) { - push @{ $form->{"${module}_links"}{$key} }, - { accno => $ref->{accno}, - description => $ref->{description}, - selected => "selected", - id => $ref->{id} }; - } else { - push @{ $form->{"${module}_links"}{$key} }, - { accno => $ref->{accno}, - description => $ref->{description}, - selected => "", - id => $ref->{id} }; - } - } - } - } - $sth->finish; - - - $dbh->disconnect; - - $main::lxdebug->leave_sub(); -} - -sub save_buchungsgruppe { - $main::lxdebug->enter_sub(); - - my ($self, $myconfig, $form) = @_; - - # connect to database - my $dbh = $form->dbconnect($myconfig); - - my @values = ($form->{description}, $form->{inventory_accno_id}, - $form->{income_accno_id_0}, $form->{expense_accno_id_0}, - $form->{income_accno_id_1}, $form->{expense_accno_id_1}, - $form->{income_accno_id_2}, $form->{expense_accno_id_2}, - $form->{income_accno_id_3}, $form->{expense_accno_id_3}); - - my $query; - - # id is the old record - if ($form->{id}) { - $query = qq|UPDATE buchungsgruppen SET - description = ?, inventory_accno_id = ?, - income_accno_id_0 = ?, expense_accno_id_0 = ?, - income_accno_id_1 = ?, expense_accno_id_1 = ?, - income_accno_id_2 = ?, expense_accno_id_2 = ?, - income_accno_id_3 = ?, expense_accno_id_3 = ? - WHERE id = ?|; - push(@values, $form->{id}); - } else { - $query = qq|SELECT COALESCE(MAX(sortkey) + 1, 1) FROM buchungsgruppen|; - my ($sortkey) = $dbh->selectrow_array($query); - $form->dberror($query) if ($dbh->err); - push(@values, $sortkey); - $query = qq|INSERT INTO buchungsgruppen - (description, inventory_accno_id, - income_accno_id_0, expense_accno_id_0, - income_accno_id_1, expense_accno_id_1, - income_accno_id_2, expense_accno_id_2, - income_accno_id_3, expense_accno_id_3, - sortkey) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|; - } - do_query($form, $dbh, $query, @values); - - $dbh->disconnect; - - $main::lxdebug->leave_sub(); -} - -sub delete_buchungsgruppe { - $main::lxdebug->enter_sub(); - - my ($self, $myconfig, $form) = @_; - - # connect to database - my $dbh = $form->dbconnect($myconfig); - - $query = qq|DELETE FROM buchungsgruppen WHERE id = ?|; - do_query($form, $dbh, $query, $form->{id}); - - $dbh->disconnect; - - $main::lxdebug->leave_sub(); -} - -sub swap_sortkeys { - $main::lxdebug->enter_sub(); - - my ($self, $myconfig, $form, $table) = @_; - - # connect to database - my $dbh = $form->get_standard_dbh($myconfig); - - my $query = - qq|SELECT - (SELECT sortkey FROM $table WHERE id = ?) AS sortkey1, - (SELECT sortkey FROM $table WHERE id = ?) AS sortkey2|; - my @values = ($form->{"id1"}, $form->{"id2"}); - my @sortkeys = selectrow_query($form, $dbh, $query, @values); - - $query = qq|UPDATE $table SET sortkey = ? WHERE id = ?|; - my $sth = prepare_query($form, $dbh, $query); - - do_statement($form, $sth, $query, $sortkeys[1], $form->{"id1"}); - do_statement($form, $sth, $query, $sortkeys[0], $form->{"id2"}); - - $sth->finish(); - - $dbh->commit(); - - $main::lxdebug->leave_sub(); -} - -sub printer { - $main::lxdebug->enter_sub(); - - my ($self, $myconfig, $form) = @_; - - # connect to database - my $dbh = $form->dbconnect($myconfig); - - my $query = qq|SELECT id, printer_description, template_code, printer_command - FROM printers - ORDER BY 2|; - - $sth = $dbh->prepare($query); - $sth->execute || $form->dberror($query); - - $form->{"ALL"} = []; - while (my $ref = $sth->fetchrow_hashref(NAME_lc)) { - push @{ $form->{ALL} }, $ref; - } - - $sth->finish; - $dbh->disconnect; - - $main::lxdebug->leave_sub(); -} - -sub get_printer { - $main::lxdebug->enter_sub(); - - my ($self, $myconfig, $form) = @_; - - # connect to database - my $dbh = $form->dbconnect($myconfig); - - my $query = - qq|SELECT p.printer_description, p.template_code, p.printer_command - FROM printers p - WHERE p.id = ?|; - my $sth = $dbh->prepare($query); - $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})"); - - my $ref = $sth->fetchrow_hashref(NAME_lc); - - map { $form->{$_} = $ref->{$_} } keys %$ref; - - $sth->finish; - - $dbh->disconnect; - - $main::lxdebug->leave_sub(); -} - -sub save_printer { - $main::lxdebug->enter_sub(); - - my ($self, $myconfig, $form) = @_; - - # connect to database - my $dbh = $form->dbconnect($myconfig); - - my @values = ($form->{printer_description}, - $form->{template_code}, - $form->{printer_command}); - - # id is the old record - if ($form->{id}) { - $query = qq|UPDATE printers SET - printer_description = ?, template_code = ?, printer_command = ? - WHERE id = ?|; - push(@values, $form->{id}); - } else { - $query = qq|INSERT INTO printers - (printer_description, template_code, printer_command) - VALUES (?, ?, ?)|; - } - do_query($form, $dbh, $query, @values); - - $dbh->disconnect; - - $main::lxdebug->leave_sub(); -} - -sub delete_printer { - $main::lxdebug->enter_sub(); - - my ($self, $myconfig, $form) = @_; - - # connect to database - my $dbh = $form->dbconnect($myconfig); - - $query = qq|DELETE FROM printers - WHERE id = ?|; - do_query($form, $dbh, $query, $form->{id}); - - $dbh->disconnect; - - $main::lxdebug->leave_sub(); -} - -sub payment { - $main::lxdebug->enter_sub(); - - my ($self, $myconfig, $form) = @_; - - # connect to database - my $dbh = $form->dbconnect($myconfig); - - my $query = qq|SELECT * FROM payment_terms ORDER BY sortkey|; - - $sth = $dbh->prepare($query); - $sth->execute || $form->dberror($query); - - $form->{ALL} = []; - while (my $ref = $sth->fetchrow_hashref(NAME_lc)) { - push @{ $form->{ALL} }, $ref; - } - - $sth->finish; - $dbh->disconnect; - - $main::lxdebug->leave_sub(); -} - -sub get_payment { - $main::lxdebug->enter_sub(); - - my ($self, $myconfig, $form) = @_; - - # connect to database - my $dbh = $form->dbconnect($myconfig); - - my $query = qq|SELECT * FROM payment_terms WHERE id = ?|; - my $sth = $dbh->prepare($query); - $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})"); - - my $ref = $sth->fetchrow_hashref(NAME_lc); - map { $form->{$_} = $ref->{$_} } keys %$ref; - $sth->finish(); - - $query = - qq|SELECT t.language_id, t.description_long, l.description AS language | . - qq|FROM translation_payment_terms t | . - qq|LEFT JOIN language l ON t.language_id = l.id | . - qq|WHERE t.payment_terms_id = ? | . - qq|UNION | . - qq|SELECT l.id AS language_id, NULL AS description_long, | . - qq| l.description AS language | . - qq|FROM language l|; - $sth = $dbh->prepare($query); - $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})"); - - my %mapping; - while (my $ref = $sth->fetchrow_hashref(NAME_lc)) { - $mapping{ $ref->{"language_id"} } = $ref - unless (defined($mapping{ $ref->{"language_id"} })); - } - $sth->finish; - - $form->{"TRANSLATION"} = [sort({ $a->{"language"} cmp $b->{"language"} } - values(%mapping))]; - - $dbh->disconnect; - - $main::lxdebug->leave_sub(); -} - -sub save_payment { - $main::lxdebug->enter_sub(); - - my ($self, $myconfig, $form) = @_; - - # connect to database - my $dbh = $form->dbconnect_noauto($myconfig); - - my $query; - - if (!$form->{id}) { - $query = qq|SELECT nextval('id'), COALESCE(MAX(sortkey) + 1, 1) | . - qq|FROM payment_terms|; - my $sortkey; - ($form->{id}, $sortkey) = selectrow_query($form, $dbh, $query); - - $query = qq|INSERT INTO payment_terms (id, sortkey) VALUES (?, ?)|; - do_query($form, $dbh, $query, $form->{id}, $sortkey); - - } else { - $query = - qq|DELETE FROM translation_payment_terms | . - qq|WHERE payment_terms_id = ?|; - do_query($form, $dbh, $query, $form->{"id"}); - } - - $query = qq|UPDATE payment_terms SET - description = ?, description_long = ?, - terms_netto = ?, terms_skonto = ?, - percent_skonto = ? - WHERE id = ?|; - my @values = ($form->{description}, $form->{description_long}, - $form->{terms_netto} * 1, $form->{terms_skonto} * 1, - $form->{percent_skonto} * 1, - $form->{id}); - do_query($form, $dbh, $query, @values); - - $query = qq|SELECT id FROM language|; - my @language_ids; - my $sth = $dbh->prepare($query); - $sth->execute() || $form->dberror($query); - - while (my ($id) = $sth->fetchrow_array()) { - push(@language_ids, $id); - } - $sth->finish(); - - $query = - qq|INSERT INTO translation_payment_terms | . - qq|(language_id, payment_terms_id, description_long) | . - qq|VALUES (?, ?, ?)|; - $sth = $dbh->prepare($query); - - foreach my $language_id (@language_ids) { - do_statement($form, $sth, $query, $language_id, $form->{"id"}, - $form->{"description_long_${language_id}"}); - } - $sth->finish(); - - $dbh->commit(); - $dbh->disconnect; - - $main::lxdebug->leave_sub(); -} - -sub delete_payment { - $main::lxdebug->enter_sub(); - - my ($self, $myconfig, $form) = @_; - - # connect to database - my $dbh = $form->dbconnect_noauto($myconfig); - - my $query = - qq|DELETE FROM translation_payment_terms WHERE payment_terms_id = ?|; - do_query($form, $dbh, $query, $form->{"id"}); - - $query = qq|DELETE FROM payment_terms WHERE id = ?|; - do_query($form, $dbh, $query, $form->{"id"}); - - $dbh->commit(); - $dbh->disconnect; - - $main::lxdebug->leave_sub(); -} - - -sub prepare_template_filename { - $main::lxdebug->enter_sub(); - - my ($self, $myconfig, $form) = @_; - - my ($filename, $display_filename); - - if ($form->{type} eq "stylesheet") { - $filename = "css/$myconfig->{stylesheet}"; - $display_filename = $myconfig->{stylesheet}; - - } else { - $filename = $form->{formname}; - - if ($form->{language}) { - my ($id, $template_code) = split(/--/, $form->{language}); - $filename .= "_${template_code}"; - } - - if ($form->{printer}) { - my ($id, $template_code) = split(/--/, $form->{printer}); - $filename .= "_${template_code}"; - } - - $filename .= "." . ($form->{format} eq "html" ? "html" : "tex"); - $filename =~ s|.*/||; - $display_filename = $filename; - $filename = "$myconfig->{templates}/$filename"; - } - - $main::lxdebug->leave_sub(); - - return ($filename, $display_filename); + + return ($filename, $display_filename); } @@ -1429,272 +679,86 @@ sub load_template { my ($self, $filename) = @_; - my ($content, $lines) = ("", 0); - - local *TEMPLATE; - - if (open(TEMPLATE, $filename)) { - while (