X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FAM.pm;h=df8bf23e510f5414a6baf0ffcf5309919264e54f;hb=a00cdb315e4f27fb5074fda82fd0d270a2fc77c0;hp=fa6e128ea7d57424c86df0c702fa30cf3ccc0b3b;hpb=b6dc5623d93c1be1c54248d4512e80f495af2899;p=kivitendo-erp.git diff --git a/SL/AM.pm b/SL/AM.pm index fa6e128ea..df8bf23e5 100644 --- a/SL/AM.pm +++ b/SL/AM.pm @@ -37,9 +37,13 @@ package AM; +use Carp; use Data::Dumper; +use Encode; use SL::DBUtils; +use strict; + sub get_account { $main::lxdebug->enter_sub(); @@ -63,11 +67,11 @@ sub get_account { }; - $main::lxdebug->message(LXDebug::QUERY, "\$query=\n $query"); + $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); + my $ref = $sth->fetchrow_hashref("NAME_lc"); foreach my $key (keys %$ref) { $form->{"$key"} = $ref->{"$key"}; @@ -78,13 +82,13 @@ sub get_account { # 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"); + $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query"); $sth = $dbh->prepare($query); $sth->execute || $form->dberror($query); - $ref = $sth->fetchrow_hashref(NAME_lc); + $ref = $sth->fetchrow_hashref("NAME_lc"); - map { $form->{$_} = $ref->{$_} } keys %ref; + map { $form->{$_} = $ref->{$_} } keys %{ $ref }; $sth->finish; @@ -101,13 +105,13 @@ sub get_account { rate FROM tax ORDER BY taxkey }; - $main::lxdebug->message(LXDebug::QUERY, "\$query=\n $query"); + $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query"); $sth = $dbh->prepare($query); $sth->execute || $form->dberror($query); $form->{TAXKEY} = []; - while (my $ref = $sth->fetchrow_hashref(NAME_lc)) { + while (my $ref = $sth->fetchrow_hashref("NAME_lc")) { push @{ $form->{TAXKEY} }, $ref; } @@ -118,12 +122,12 @@ sub get_account { FROM chart WHERE link = ? ORDER BY accno|; - $main::lxdebug->message(LXDebug::QUERY, "\$query=\n $query"); + $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)) { + while (my $ref = $sth->fetchrow_hashref("NAME_lc")) { push @{ $form->{NEWACCOUNT} }, $ref; } @@ -149,14 +153,14 @@ sub get_account { WHERE tk.chart_id = ? ORDER BY startdate DESC }; - $main::lxdebug->message(LXDebug::QUERY, "\$query=\n $query"); + $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query"); $sth = $dbh->prepare($query); $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})"); $form->{ACCOUNT_TAXKEYS} = []; - while (my $ref = $sth->fetchrow_hashref(NAME_lc)) { + while (my $ref = $sth->fetchrow_hashref("NAME_lc")) { push @{ $form->{ACCOUNT_TAXKEYS} }, $ref; } @@ -166,7 +170,7 @@ sub get_account { # 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"); + $main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query"); $sth = $dbh->prepare($query); $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})"); @@ -179,7 +183,7 @@ sub get_account { if ($form->{new_chart_id}) { $query = qq|SELECT current_date-valid_from FROM chart WHERE id = ?|; - $main::lxdebug->message(LXDebug::QUERY, "\$query=\n $query"); + $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; @@ -195,6 +199,9 @@ sub get_account { sub save_account { $main::lxdebug->enter_sub(); + # 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 @@ -203,7 +210,14 @@ sub save_account { # 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); + 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); + } + + if ($form->{AR_include_in_dropdown}) { + $form->{$form->{AR_include_in_dropdown}} = $form->{AR_include_in_dropdown}; + } + if ($form->{AP_include_in_dropdown}) { + $form->{$form->{AP_include_in_dropdown}} = $form->{AP_include_in_dropdown}; } $form->{link} = ""; @@ -214,7 +228,7 @@ sub save_account { $form->{IC}, $form->{IC_sale}, $form->{IC_cogs}, $form->{IC_taxpart}, $form->{IC_income}, $form->{IC_expense}, - $form->{IC_taxservice}, $form->{CT_tax} + $form->{IC_taxservice} ) { $form->{link} .= "${item}:" if ($item); } @@ -229,9 +243,32 @@ sub save_account { $form->{id} = ""; } + 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) VALUES (?, ?)|; + do_query($form, $dbh, $query, $form->{"id"}, $form->{"accno"}); + } + my @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 = ?, @@ -261,51 +298,6 @@ sub save_account { $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 +316,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 @@ -332,12 +327,6 @@ sub save_account { next READTAXKEYS; } - # check if there is at least one relation to pos_ustva or tax_id - if ( $form->{"taxkey_pos_ustva_$tk_count"} eq '' && $form->{"taxkey_tax_$tk_count"} == 0 ) { - $tk_count++; - next READTAXKEYS; - } - # Add valid taxkeys into the array push @taxkeys , { @@ -345,7 +334,7 @@ sub save_account { tax_id => conv_i($form->{"taxkey_tax_$tk_count"}), startdate => conv_date($form->{"taxkey_startdate_$tk_count"}), chart_id => conv_i($form->{"id"}), - pos_ustva => $form->{"taxkey_pos_ustva_$tk_count"}, + pos_ustva => conv_i($form->{"taxkey_pos_ustva_$tk_count"}), delete => ( $form->{"taxkey_del_$tk_count"} eq 'delete' ) ? '1' : '', }; @@ -416,6 +405,21 @@ sub save_account { } + # Update chart.taxkey_id to the latest from taxkeys for this chart. + $query = <{id}); + # commit my $rc = $dbh->commit; $dbh->disconnect; @@ -477,108 +481,6 @@ sub delete_account { return $rc; } -sub departments { - $main::lxdebug->enter_sub(); - - my ($self, $myconfig, $form) = @_; - - # connect to database - my $dbh = $form->dbconnect($myconfig); - - my $query = qq|SELECT d.id, d.description, d.role - FROM department d - 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_department { - $main::lxdebug->enter_sub(); - - my ($self, $myconfig, $form) = @_; - - # connect to database - my $dbh = $form->dbconnect($myconfig); - - my $query = qq|SELECT d.description, d.role - FROM department d - WHERE d.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; - - # 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 { - $main::lxdebug->enter_sub(); - - my ($self, $myconfig, $form) = @_; - - # connect to database - my $dbh = $form->dbconnect($myconfig); - - my @values = ($form->{description}, $form->{role}); - if ($form->{id}) { - $query = qq|UPDATE department SET - description = ?, role = ? - WHERE id = ?|; - push(@values, $form->{id}); - } else { - $query = qq|INSERT INTO department - (description, role) - VALUES (?, ?)|; - } - do_query($form, $dbh, $query, @values); - - $dbh->disconnect; - - $main::lxdebug->leave_sub(); -} - -sub delete_department { - $main::lxdebug->enter_sub(); - - my ($self, $myconfig, $form) = @_; - - # connect to database - my $dbh = $form->dbconnect($myconfig); - - $query = qq|DELETE FROM department - WHERE id = ?|; - do_query($form, $dbh, $query, $form->{id}); - - $dbh->disconnect; - - $main::lxdebug->leave_sub(); -} - sub lead { $main::lxdebug->enter_sub(); @@ -591,10 +493,10 @@ sub lead { FROM leads ORDER BY 2|; - $sth = $dbh->prepare($query); + my $sth = $dbh->prepare($query); $sth->execute || $form->dberror($query); - while (my $ref = $sth->fetchrow_hashref(NAME_lc)) { + while (my $ref = $sth->fetchrow_hashref("NAME_lc")) { push @{ $form->{ALL} }, $ref; } @@ -619,7 +521,7 @@ sub get_lead { 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; @@ -634,6 +536,7 @@ sub save_lead { $main::lxdebug->enter_sub(); my ($self, $myconfig, $form) = @_; + my ($query); # connect to database my $dbh = $form->dbconnect($myconfig); @@ -661,6 +564,7 @@ sub delete_lead { $main::lxdebug->enter_sub(); my ($self, $myconfig, $form) = @_; + my ($query); # connect to database my $dbh = $form->dbconnect($myconfig); @@ -674,105 +578,6 @@ sub delete_lead { $main::lxdebug->leave_sub(); } -sub business { - $main::lxdebug->enter_sub(); - - my ($self, $myconfig, $form) = @_; - - # 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); - - while (my $ref = $sth->fetchrow_hashref(NAME_lc)) { - push @{ $form->{ALL} }, $ref; - } - - $sth->finish; - $dbh->disconnect; - - $main::lxdebug->leave_sub(); -} - -sub get_business { - $main::lxdebug->enter_sub(); - - my ($self, $myconfig, $form) = @_; - - # connect to database - my $dbh = $form->dbconnect($myconfig); - - 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})"); - - my $ref = $sth->fetchrow_hashref(NAME_lc); - - map { $form->{$_} = $ref->{$_} } keys %$ref; - - $sth->finish; - - $dbh->disconnect; - - $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(); @@ -786,12 +591,12 @@ sub language { " 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); my $ary = []; - while (my $ref = $sth->fetchrow_hashref(NAME_lc)) { + while (my $ref = $sth->fetchrow_hashref("NAME_lc")) { push(@{ $ary }, $ref); } @@ -822,7 +627,7 @@ sub get_language { 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; @@ -893,12 +698,13 @@ sub delete_language { $main::lxdebug->enter_sub(); my ($self, $myconfig, $form) = @_; + my $query; # 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 = ?|; + foreach my $table (qw(generic_translations units_language)) { + $query = qq|DELETE FROM $table WHERE language_id = ?|; do_query($form, $dbh, $query, $form->{"id"}); } @@ -942,11 +748,11 @@ sub buchungsgruppe { FROM buchungsgruppen ORDER BY sortkey|; - $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; } @@ -990,7 +796,7 @@ sub get_buchungsgruppe { 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; @@ -1018,7 +824,7 @@ sub get_buchungsgruppe { my $sth = $dbh->prepare($query); $sth->execute || $form->dberror($query); - while (my $ref = $sth->fetchrow_hashref(NAME_lc)) { + 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"}; @@ -1105,7 +911,7 @@ sub delete_buchungsgruppe { # connect to database my $dbh = $form->dbconnect($myconfig); - $query = qq|DELETE FROM buchungsgruppen WHERE id = ?|; + my $query = qq|DELETE FROM buchungsgruppen WHERE id = ?|; do_query($form, $dbh, $query, $form->{id}); $dbh->disconnect; @@ -1141,448 +947,208 @@ sub swap_sortkeys { $main::lxdebug->leave_sub(); } -sub printer { +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 id, printer_description, template_code, printer_command - FROM printers - ORDER BY 2|; + if ($form->{type} eq "stylesheet") { + $filename = "css/$myconfig->{stylesheet}"; + $display_filename = $myconfig->{stylesheet}; - $sth = $dbh->prepare($query); - $sth->execute || $form->dberror($query); + } else { + $filename = $form->{formname}; - $form->{"ALL"} = []; - while (my $ref = $sth->fetchrow_hashref(NAME_lc)) { - push @{ $form->{ALL} }, $ref; - } + if ($form->{language}) { + my ($id, $template_code) = split(/--/, $form->{language}); + $filename .= "_${template_code}"; + } - $sth->finish; - $dbh->disconnect; + if ($form->{printer}) { + my ($id, $template_code) = split(/--/, $form->{printer}); + $filename .= "_${template_code}"; + } + + $filename .= "." . ($form->{format} eq "html" ? "html" : "tex"); + if ($form->{"formname"} =~ m|\.\.| || $form->{"formname"} =~ m|^/|) { + $filename =~ s|.*/||; + } + $display_filename = $filename; + $filename = "$myconfig->{templates}/$filename"; + } $main::lxdebug->leave_sub(); -} -sub get_printer { - $main::lxdebug->enter_sub(); + return ($filename, $display_filename); +} - my ($self, $myconfig, $form) = @_; - # connect to database - my $dbh = $form->dbconnect($myconfig); +sub load_template { + $main::lxdebug->enter_sub(); - 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 ($self, $filename) = @_; - my $ref = $sth->fetchrow_hashref(NAME_lc); + my ($content, $lines) = ("", 0); - map { $form->{$_} = $ref->{$_} } keys %$ref; + local *TEMPLATE; - $sth->finish; + if (open(TEMPLATE, $filename)) { + while (