X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FAM.pm;h=30b1d9301936d3337fb8301e719fc103cf8552ed;hb=f7c4665fd435b94e599ea1ac2ba23d8e21a8420f;hp=1dbf9d82f5b85364ec00013ac1bdff619b56f587;hpb=8f29628f54e8b6669cdefa6e43e2f54220760c3c;p=kivitendo-erp.git diff --git a/SL/AM.pm b/SL/AM.pm index 1dbf9d82f..30b1d9301 100644 --- a/SL/AM.pm +++ b/SL/AM.pm @@ -39,7 +39,13 @@ 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::GenericTranslations; use strict; @@ -198,15 +204,23 @@ 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 my $dbh = $form->dbconnect_noauto($myconfig); + 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} = ""; @@ -217,7 +231,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); } @@ -232,16 +246,51 @@ sub save_account { $form->{id} = ""; } + $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) VALUES (?, ?)|; - do_query($form, $dbh, $query, $form->{"id"}, $form->{"accno"}); + $query = qq|INSERT INTO chart (id, accno, link) VALUES (?, ?, ?)|; + do_query($form, $dbh, $query, $form->{"id"}, $form->{"accno"}, ''); } - my @values; + @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 = ?, @@ -271,6 +320,7 @@ sub save_account { $form->{id}, ); + } do_query($form, $dbh, $query, @values); @@ -377,6 +427,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; @@ -413,15 +478,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 @@ -429,6 +487,13 @@ sub delete_account { WHERE chart_id = ?|; do_query($form, $dbh, $query, $form->{id}); + # 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}); + # commit and redirect my $rc = $dbh->commit; $dbh->disconnect; @@ -438,110 +503,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|; - - my $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) = @_; - my ($query); - - # 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) = @_; - 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; - - $main::lxdebug->leave_sub(); -} - sub lead { $main::lxdebug->enter_sub(); @@ -608,7 +569,7 @@ sub save_lead { $query = qq|UPDATE leads SET lead = ? WHERE id = ?|; - puhs(@values, $form->{id}); + push(@values, $form->{id}); } else { $query = qq|INSERT INTO leads (lead) @@ -639,106 +600,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, salesman - FROM business - ORDER BY 2|; - - my $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, b.salesman - 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) = @_; - my ($query); - - # connect to database - my $dbh = $form->dbconnect($myconfig); - - my @values = ($form->{description}, $form->{discount}, $form->{customernumberinit}, $form->{salesman} ? 't' : 'f'); - # id is the old record - if ($form->{id}) { - $query = qq|UPDATE business SET - description = ?, - discount = ?, - customernumberinit = ?, - salesman = ? - WHERE id = ?|; - push(@values, $form->{id}); - } else { - $query = qq|INSERT INTO business - (description, discount, customernumberinit, salesman) - 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); - - my $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(); @@ -864,7 +725,7 @@ sub delete_language { # connect to database my $dbh = $form->dbconnect_noauto($myconfig); - foreach my $table (qw(translation_payment_terms units_language)) { + foreach my $table (qw(generic_translations units_language)) { $query = qq|DELETE FROM $table WHERE language_id = ?|; do_query($form, $dbh, $query, $form->{"id"}); } @@ -1108,314 +969,64 @@ 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}; - my $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 = SL::DB::Default->get->templates . "/$filename"; + } $main::lxdebug->leave_sub(); + + return ($filename, $display_filename); } -sub get_printer { + +sub load_template { $main::lxdebug->enter_sub(); - my ($self, $myconfig, $form) = @_; + my ($self, $filename) = @_; - # connect to database - my $dbh = $form->dbconnect($myconfig); + my ($content, $lines) = ("", 0); - 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})"); + local *TEMPLATE; - my $ref = $sth->fetchrow_hashref("NAME_lc"); + if (open(TEMPLATE, $filename)) { + while (