X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FDN.pm;h=72154c24fa08745304161ab98887afc44f81fbab;hb=6f2893dcf199d5e5147cc2020c2ab6984f4274dc;hp=33cb5502f97f6fa026b26a8cf99b04216f378111;hpb=773c2abe4775300e523e582e722fb802e46aa844;p=kivitendo-erp.git diff --git a/SL/DN.pm b/SL/DN.pm index 33cb5502f..72154c24f 100644 --- a/SL/DN.pm +++ b/SL/DN.pm @@ -41,6 +41,8 @@ use SL::Mailer; use SL::MoreCommon; use SL::Template; +use strict; + sub get_config { $main::lxdebug->enter_sub(); @@ -361,7 +363,7 @@ sub send_email { return; } - my $template = PlainTextTemplate->new(undef, $form, $myconfig); + my $template = SL::Template::create(type => 'PlainText', form => $form, myconfig => $myconfig); my $mail = Mailer->new(); $mail->{from} = $myconfig->{email}; $mail->{to} = $ref->{recipient}; @@ -457,7 +459,7 @@ sub get_invoices { push(@values, $form->{minamount}); } - $query = + my $query = qq|SELECT id FROM dunning_config WHERE dunning_level = (SELECT MAX(dunning_level) FROM dunning_config)|; @@ -467,6 +469,7 @@ sub get_invoices { qq|SELECT a.id, a.ordnumber, a.transdate, a.invnumber, a.amount, ct.name AS customername, a.customer_id, a.duedate, + a.amount - a.paid AS open_amount, cfg.dunning_description, cfg.dunning_level, @@ -512,8 +515,8 @@ sub get_invoices { $form->{DUNNINGS} = []; - while (my $ref = $sth->fetchrow_hashref(NAME_lc)) { - next if !$ref->{terms} || ($ref->{pastdue} < $ref->{terms}); + while (my $ref = $sth->fetchrow_hashref("NAME_lc")) { + next if ($ref->{pastdue} < $ref->{terms}); $ref->{interest} = $form->round_amount($ref->{interest}, 2); push(@{ $form->{DUNNINGS} }, $ref); @@ -537,7 +540,7 @@ sub get_dunning { # connect to database my $dbh = $form->dbconnect($myconfig); - $where = qq| WHERE (da.trans_id = a.id)|; + my $where = qq| WHERE (da.trans_id = a.id)|; my @values; @@ -598,16 +601,38 @@ sub get_dunning { push(@values, $form->{dunningto}); } - $query = + if ($form->{salesman_id}) { + $where .= qq| AND a.salesman_id = ?|; + push(@values, conv_i($form->{salesman_id})); + } + + my %sort_columns = ( + 'dunning_description' => [ qw(dn.dunning_description customername invnumber) ], + 'customername' => [ qw(customername invnumber) ], + 'invnumber' => [ qw(a.invnumber) ], + 'transdate' => [ qw(a.transdate a.invnumber) ], + 'duedate' => [ qw(a.duedate a.invnumber) ], + 'dunning_date' => [ qw(dunning_date a.invnumber) ], + 'dunning_duedate' => [ qw(dunning_duedate a.invnumber) ], + 'salesman' => [ qw(salesman) ], + ); + + my $sortdir = !defined $form->{sortdir} ? 'ASC' : $form->{sortdir} ? 'ASC' : 'DESC'; + my $sortkey = $sort_columns{$form->{sort}} ? $form->{sort} : 'customername'; + my $sortorder = join ', ', map { "$_ $sortdir" } @{ $sort_columns{$sortkey} }; + + my $query = qq|SELECT a.id, a.ordnumber, a.invoice, a.transdate, a.invnumber, a.amount, ct.name AS customername, ct.id AS customer_id, a.duedate, da.fee, da.interest, dn.dunning_description, da.transdate AS dunning_date, - da.duedate AS dunning_duedate, da.dunning_id, da.dunning_config_id + da.duedate AS dunning_duedate, da.dunning_id, da.dunning_config_id, + e2.name AS salesman FROM ar a - JOIN customer ct ON (a.customer_id = ct.id), dunning da + JOIN customer ct ON (a.customer_id = ct.id) + LEFT JOIN employee e2 ON (a.salesman_id = e2.id), dunning da LEFT JOIN dunning_config dn ON (da.dunning_config_id = dn.id) $where - ORDER BY name, a.id|; + ORDER BY $sortorder|; $form->{DUNNINGS} = selectall_hashref_query($form, $dbh, $query, @values); @@ -638,28 +663,26 @@ sub melt_pdfs { my $in = IO::File->new("gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=- $inputfiles |"); $form->error($main::locale->text('Could not spawn ghostscript.')) unless $in; - my $out; - if ($form->{media} eq 'printer') { $form->get_printer_code($myconfig); + my $out; if ($form->{printer_command}) { $out = IO::File->new("| $form->{printer_command}"); } + $::locale->with_raw_io($out, sub { $out->print($_) while <$in> }); + $form->error($main::locale->text('Could not spawn the printer command.')) unless $out; } else { - $out = IO::File->new('>-'); - $out->print(qq|Content-Type: Application/PDF\n| . - qq|Content-Disposition: attachment; filename="dunning_${dunning_id}.pdf"\n\n|); - } + my $dunning_filename = $form->get_formname_translation('dunning'); + print qq|Content-Type: Application/PDF\n| . + qq|Content-Disposition: attachment; filename="${dunning_filename}_${dunning_id}.pdf"\n\n|; - while (my $line = <$in>) { - $out->print($line); + $::locale->with_raw_io(\*STDOUT, sub { print while <$in> }); } $in->close(); - $out->close(); map { unlink("${main::spool}/$_") } @{ $form->{DUNNING_PDFS} }; @@ -698,22 +721,24 @@ sub print_dunning { my $sth = prepare_execute_query($form, $dbh, $query, $dunning_id); my $first = 1; - while (my $ref = $sth->fetchrow_hashref(NAME_lc)) { + while (my $ref = $sth->fetchrow_hashref("NAME_lc")) { if ($first) { - map({ $form->{"dn_$_"} = []; } keys(%{$ref})); + $form->{TEMPLATE_ARRAYS} = {}; + map({ $form->{TEMPLATE_ARRAYS}->{"dn_$_"} = []; } keys(%{$ref})); $first = 0; } map { $ref->{$_} = $form->format_amount($myconfig, $ref->{$_}, 2) } qw(amount netamount paid open_amount fee interest linetotal); map { $form->{$_} = $ref->{$_} } keys %$ref; - map { push @{ $form->{"dn_$_"} }, $ref->{$_}} keys %$ref; + map { push @{ $form->{TEMPLATE_ARRAYS}->{"dn_$_"} }, $ref->{$_} } keys %$ref; } $sth->finish(); $query = qq|SELECT - c.id AS customer_id, c.name, c.street, c.zipcode, c.city, - c.country, c.department_1, c.department_2, c.email, c.customernumber, - -- contact information + c.id AS customer_id, c.name, c.street, c.zipcode, c.city, + c.country, c.department_1, c.department_2, c.email, c.customernumber, + c.greeting, c.contact, c.phone, c.fax, c.homepage, + c.email, c.taxincluded, c.business_id, c.taxnumber, c.iban, co.* FROM dunning d LEFT JOIN ar ON (d.trans_id = ar.id) @@ -721,7 +746,7 @@ sub print_dunning { LEFT JOIN contacts co ON (ar.cp_id = co.cp_id) WHERE (d.dunning_id = ?) LIMIT 1|; - $ref = selectfirst_hashref_query($form, $dbh, $query, $dunning_id); + my $ref = selectfirst_hashref_query($form, $dbh, $query, $dunning_id); map { $form->{$_} = $ref->{$_} } keys %{ $ref }; $query = @@ -811,19 +836,19 @@ sub print_invoice_for_fees { c.name, c.department_1, c.department_2, c.street, c.zipcode, c.city, c.country, c.contact, c.customernumber, c.phone, c.fax, c.email, - c.taxnumber, c.sic_code, c.greeting + c.taxnumber, c.greeting FROM ar LEFT JOIN customer c ON (ar.customer_id = c.id) WHERE ar.id = ?|; - $ref = selectfirst_hashref_query($form, $dbh, $query, $ar_id); + my $ref = selectfirst_hashref_query($form, $dbh, $query, $ar_id); map { $form->{$_} = $ref->{$_} } keys %{ $ref }; $query = qq|SELECT * FROM employee WHERE login = ?|; $ref = selectfirst_hashref_query($form, $dbh, $query, $form->{login}); map { $form->{"employee_${_}"} = $ref->{$_} } keys %{ $ref }; - $query = qq|SELECT * FROM acc_trans WHERE trans_id = ? ORDER BY oid ASC|; + $query = qq|SELECT * FROM acc_trans WHERE trans_id = ? ORDER BY acc_trans_id ASC|; $sth = prepare_execute_query($form, $dbh, $query, $ar_id); my ($row, $fee, $interest) = (0, 0, 0); @@ -844,7 +869,7 @@ sub print_invoice_for_fees { $form->{interest} = $form->round_amount($interest, 2); $form->{invamount} = $form->round_amount($fee + $interest, 2); $form->{dunning_id} = $dunning_id; - $form->{formname} = "${template}"; + $form->{formname} = "${template}_invoice"; map { $form->{$_} = $form->format_amount($myconfig, $form->{$_}, 2) } qw(fee interest invamount);