kivitendo/SL/DN.pm @ b5157f97
54e4131e | Moritz Bunkus | #======================================================================
|
||
# LX-Office ERP
|
||||
# Copyright (C) 2006
|
||||
# Based on SQL-Ledger Version 2.1.9
|
||||
# Web http://www.lx-office.org
|
||||
#
|
||||
#=====================================================================
|
||||
# SQL-Ledger Accounting
|
||||
# Copyright (C) 1998-2002
|
||||
#
|
||||
# Author: Dieter Simader
|
||||
# Email: dsimader@sql-ledger.org
|
||||
# Web: http://www.sql-ledger.org
|
||||
#
|
||||
# Contributors:
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation; either version 2 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# 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.
|
||||
#======================================================================
|
||||
#
|
||||
# Dunning process module
|
||||
#
|
||||
#======================================================================
|
||||
package DN;
|
||||
use SL::Template;
|
||||
use SL::IS;
|
||||
7dab0cbd | Moritz Bunkus | use SL::Common;
|
||
use SL::DBUtils;
|
||||
54e4131e | Moritz Bunkus | use Data::Dumper;
|
||
sub get_config {
|
||||
$main::lxdebug->enter_sub();
|
||||
my ($self, $myconfig, $form) = @_;
|
||||
# connect to database
|
||||
my $dbh = $form->dbconnect($myconfig);
|
||||
7dab0cbd | Moritz Bunkus | my $query =
|
||
qq|SELECT * | .
|
||||
qq|FROM dunning_config | .
|
||||
qq|ORDER BY dunning_level|;
|
||||
$form->{DUNNING} = selectall_hashref_query($form, $dbh, $query);
|
||||
54e4131e | Moritz Bunkus | |||
7dab0cbd | Moritz Bunkus | foreach my $ref (@{ $form->{DUNNING} }) {
|
||
54e4131e | Moritz Bunkus | $ref->{fee} = $form->format_amount($myconfig, $ref->{fee}, 2);
|
||
fd6900cc | Moritz Bunkus | $ref->{interest_rate} = $form->format_amount($myconfig, ($ref->{interest_rate} * 100));
|
||
54e4131e | Moritz Bunkus | }
|
||
7dab0cbd | Moritz Bunkus | $dbh->disconnect();
|
||
54e4131e | Moritz Bunkus | |||
$main::lxdebug->leave_sub();
|
||||
}
|
||||
sub save_config {
|
||||
$main::lxdebug->enter_sub();
|
||||
my ($self, $myconfig, $form) = @_;
|
||||
# connect to database
|
||||
my $dbh = $form->dbconnect_noauto($myconfig);
|
||||
7dab0cbd | Moritz Bunkus | my ($query, @values);
|
||
54e4131e | Moritz Bunkus | for my $i (1 .. $form->{rowcount}) {
|
||
$form->{"fee_$i"} = $form->parse_amount($myconfig, $form->{"fee_$i"}) * 1;
|
||||
fd6900cc | Moritz Bunkus | $form->{"interest_rate_$i"} = $form->parse_amount($myconfig, $form->{"interest_rate_$i"}) / 100;
|
||
7dab0cbd | Moritz Bunkus | |||
if (($form->{"dunning_level_$i"} ne "") &&
|
||||
($form->{"dunning_description_$i"} ne "")) {
|
||||
@values = (conv_i($form->{"dunning_level_$i"}), $form->{"dunning_description_$i"},
|
||||
$form->{"email_subject_$i"}, $form->{"email_body_$i"},
|
||||
fd6900cc | Moritz Bunkus | $form->{"template_$i"}, $form->{"fee_$i"}, $form->{"interest_rate_$i"},
|
||
7dab0cbd | Moritz Bunkus | $form->{"active_$i"} ? 't' : 'f', $form->{"auto_$i"} ? 't' : 'f', $form->{"email_$i"} ? 't' : 'f',
|
||
$form->{"email_attachment_$i"} ? 't' : 'f', conv_i($form->{"payment_terms_$i"}), conv_i($form->{"terms_$i"}));
|
||||
54e4131e | Moritz Bunkus | if ($form->{"id_$i"}) {
|
||
7dab0cbd | Moritz Bunkus | $query =
|
||
qq|UPDATE dunning_config SET
|
||||
dunning_level = ?, dunning_description = ?,
|
||||
email_subject = ?, email_body = ?,
|
||||
fd6900cc | Moritz Bunkus | template = ?, fee = ?, interest_rate = ?,
|
||
7dab0cbd | Moritz Bunkus | active = ?, auto = ?, email = ?,
|
||
email_attachment = ?, payment_terms = ?, terms = ?
|
||||
WHERE id = ?|;
|
||||
push(@values, conv_i($form->{"id_$i"}));
|
||||
54e4131e | Moritz Bunkus | } else {
|
||
7dab0cbd | Moritz Bunkus | $query =
|
||
qq|INSERT INTO dunning_config
|
||||
(dunning_level, dunning_description, email_subject, email_body,
|
||||
fd6900cc | Moritz Bunkus | template, fee, interest_rate, active, auto, email,
|
||
7dab0cbd | Moritz Bunkus | email_attachment, payment_terms, terms)
|
||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
|
||||
54e4131e | Moritz Bunkus | }
|
||
7dab0cbd | Moritz Bunkus | do_query($form, $dbh, $query, @values);
|
||
54e4131e | Moritz Bunkus | }
|
||
7dab0cbd | Moritz Bunkus | |||
54e4131e | Moritz Bunkus | if (($form->{"dunning_description_$i"} eq "") && ($form->{"id_$i"})) {
|
||
7dab0cbd | Moritz Bunkus | $query = qq|DELETE FROM dunning_config WHERE id = ?|;
|
||
do_query($form, $dbh, $query, $form->{"id_$i"});
|
||||
54e4131e | Moritz Bunkus | }
|
||
}
|
||||
$dbh->commit;
|
||||
$dbh->disconnect;
|
||||
$main::lxdebug->leave_sub();
|
||||
}
|
||||
sub save_dunning {
|
||||
$main::lxdebug->enter_sub();
|
||||
840aea13 | Moritz Bunkus | my ($self, $myconfig, $form, $rows, $userspath, $spool, $sendmail) = @_;
|
||
54e4131e | Moritz Bunkus | # connect to database
|
||
my $dbh = $form->dbconnect_noauto($myconfig);
|
||||
7dab0cbd | Moritz Bunkus | my ($query, @values);
|
||
840aea13 | Moritz Bunkus | my ($dunning_id) = selectrow_query($form, $dbh, qq|SELECT nextval('id')|);
|
||
fd6900cc | Moritz Bunkus | my $q_update_ar = qq|UPDATE ar SET dunning_config_id = ? WHERE id = ?|;
|
||
my $h_update_ar = prepare_query($form, $dbh, $q_update_ar);
|
||||
my $q_insert_dunning =
|
||||
qq|INSERT INTO dunning (dunning_id, dunning_config_id, dunning_level,
|
||||
trans_id, fee, interest, transdate, duedate)
|
||||
VALUES (?, ?,
|
||||
(SELECT dunning_level FROM dunning_config WHERE id = ?),
|
||||
?,
|
||||
(SELECT SUM(fee)
|
||||
FROM dunning_config
|
||||
WHERE dunning_level <= (SELECT dunning_level FROM dunning_config WHERE id = ?)),
|
||||
(SELECT (amount - paid) * (current_date - transdate) FROM ar WHERE id = ?)
|
||||
* (SELECT interest_rate FROM dunning_config WHERE id = ?)
|
||||
/ 360,
|
||||
current_date,
|
||||
current_date + (SELECT payment_terms FROM dunning_config WHERE id = ?))|;
|
||||
my $h_insert_dunning = prepare_query($form, $dbh, $q_insert_dunning);
|
||||
my @invoice_ids;
|
||||
my ($next_dunning_config_id, $customer_id);
|
||||
my $send_email = 0;
|
||||
7dab0cbd | Moritz Bunkus | foreach my $row (@{ $rows }) {
|
||
fd6900cc | Moritz Bunkus | push @invoice_ids, $row->{invoice_id};
|
||
$next_dunning_config_id = $row->{next_dunning_config_id};
|
||||
$customer_id = $row->{customer_id};
|
||||
@values = ($row->{next_dunning_config_id}, $row->{invoice_id});
|
||||
do_statement($form, $h_update_ar, $q_update_ar, @values);
|
||||
$send_email |= $row->{email};
|
||||
7dab0cbd | Moritz Bunkus | |||
fd6900cc | Moritz Bunkus | my $next_config_id = conv_i($row->{next_dunning_config_id});
|
||
my $invoice_id = conv_i($row->{invoice_id});
|
||||
@values = ($dunning_id, $next_config_id, $next_config_id,
|
||||
$invoice_id, $next_config_id, $invoice_id,
|
||||
$next_config_id, $next_config_id);
|
||||
do_statement($form, $h_insert_dunning, $q_insert_dunning, @values);
|
||||
54e4131e | Moritz Bunkus | }
|
||
fd6900cc | Moritz Bunkus | $h_update_ar->finish();
|
||
$h_insert_dunning->finish();
|
||||
7dab0cbd | Moritz Bunkus | my $query =
|
||
fd6900cc | Moritz Bunkus | qq|SELECT
|
||
ar.invnumber, ar.ordnumber, ar.amount, ar.netamount,
|
||||
ar.transdate, ar.duedate, ar.paid, ar.amount - ar.paid AS open_amount,
|
||||
da.fee, da.interest, da.transdate AS dunning_date, da.duedate AS dunning_duedate
|
||||
6b76ebb4 | Moritz Bunkus | FROM ar
|
||
fd6900cc | Moritz Bunkus | LEFT JOIN dunning_config cfg ON (cfg.id = ar.dunning_config_id)
|
||
LEFT JOIN dunning da ON (ar.id = da.trans_id AND cfg.dunning_level = da.dunning_level)
|
||||
7dab0cbd | Moritz Bunkus | WHERE ar.id IN (|
|
||
fd6900cc | Moritz Bunkus | . join(", ", map { "?" } @invoice_ids) . qq|)|;
|
||
7dab0cbd | Moritz Bunkus | |||
fd6900cc | Moritz Bunkus | my $sth = prepare_execute_query($form, $dbh, $query, @invoice_ids);
|
||
54e4131e | Moritz Bunkus | my $first = 1;
|
||
while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
|
||||
if ($first) {
|
||||
map({ $form->{"dn_$_"} = []; } keys(%{$ref}));
|
||||
$first = 0;
|
||||
}
|
||||
fd6900cc | Moritz Bunkus | |||
$ref->{interest_rate} = $form->format_amount($myconfig, $ref->{interest_rate} * 100);
|
||||
54e4131e | Moritz Bunkus | map { $ref->{$_} = $form->format_amount($myconfig, $ref->{$_}, 2) } qw(amount netamount paid open_amount fee interest);
|
||
7dab0cbd | Moritz Bunkus | map { push(@{ $form->{"dn_$_"} }, $ref->{$_})} keys %$ref;
|
||
fd6900cc | Moritz Bunkus | map { $form->{$_} = $ref->{$_} } keys %{ $ref };
|
||
54e4131e | Moritz Bunkus | }
|
||
$sth->finish;
|
||||
fd6900cc | Moritz Bunkus | $query =
|
||
qq|SELECT id AS customer_id, name, street, zipcode, city, country, department_1, department_2, email
|
||||
FROM customer
|
||||
WHERE id = ?|;
|
||||
$ref = selectfirst_hashref_query($form, $dbh, $query, $customer_id);
|
||||
map { $form->{$_} = $ref->{$_} } keys %{ $ref };
|
||||
54e4131e | Moritz Bunkus | |||
fd6900cc | Moritz Bunkus | $query =
|
||
qq|SELECT
|
||||
cfg.interest_rate, cfg.template AS formname,
|
||||
cfg.email_subject, cfg.email_body, cfg.email_attachment,
|
||||
(SELECT fee
|
||||
FROM dunning
|
||||
WHERE dunning_id = ?
|
||||
LIMIT 1)
|
||||
AS fee,
|
||||
(SELECT SUM(interest)
|
||||
FROM dunning
|
||||
WHERE dunning_id = ?)
|
||||
AS total_interest,
|
||||
(SELECT SUM(amount) - SUM(paid)
|
||||
FROM ar
|
||||
WHERE id IN (| . join(", ", map { "?" } @invoice_ids) . qq|))
|
||||
AS total_open_amount
|
||||
FROM dunning_config cfg
|
||||
WHERE id = ?|;
|
||||
$ref = selectfirst_hashref_query($form, $dbh, $query, $dunning_id, $dunning_id, @invoice_ids, $next_dunning_config_id);
|
||||
map { $form->{$_} = $ref->{$_} } keys %{ $ref };
|
||||
$form->{interest_rate} = $form->format_amount($myconfig, $ref->{interest_rate} * 100);
|
||||
$form->{fee} = $form->format_amount($myconfig, $ref->{fee}, 2);
|
||||
$form->{total_interest} = $form->format_amount($myconfig, $form->round_amount($ref->{total_interest}, 2), 2);
|
||||
$form->{total_open_amount} = $form->format_amount($myconfig, $form->round_amount($ref->{total_open_amount}, 2), 2);
|
||||
$form->{total_amount} = $form->format_amount($myconfig, $form->round_amount($ref->{fee} + $ref->{total_interest} + $ref->{total_open_amount}, 2), 2);
|
||||
$form->{templates} = "$myconfig->{templates}";
|
||||
$form->{language} = $form->get_template_language(\%myconfig);
|
||||
54e4131e | Moritz Bunkus | $form->{printer_code} = $form->get_printer_code(\%myconfig);
|
||
if ($form->{language} ne "") {
|
||||
$form->{language} = "_" . $form->{language};
|
||||
}
|
||||
if ($form->{printer_code} ne "") {
|
||||
$form->{printer_code} = "_" . $form->{printer_code};
|
||||
}
|
||||
$form->{IN} = "$form->{formname}$form->{language}$form->{printer_code}.html";
|
||||
if ($form->{format} eq 'postscript') {
|
||||
$form->{postscript} = 1;
|
||||
$form->{IN} =~ s/html$/tex/;
|
||||
} elsif ($form->{"format"} =~ /pdf/) {
|
||||
$form->{pdf} = 1;
|
||||
if ($form->{"format"} =~ /opendocument/) {
|
||||
$form->{IN} =~ s/html$/odt/;
|
||||
} else {
|
||||
$form->{IN} =~ s/html$/tex/;
|
||||
}
|
||||
} elsif ($form->{"format"} =~ /opendocument/) {
|
||||
$form->{"opendocument"} = 1;
|
||||
$form->{"IN"} =~ s/html$/odt/;
|
||||
}
|
||||
fd6900cc | Moritz Bunkus | if ($send_email && ($form->{email} ne "")) {
|
||
54e4131e | Moritz Bunkus | $form->{media} = 'email';
|
||
}
|
||||
$form->{keep_tmpfile} = 0;
|
||||
if ($form->{media} eq 'email') {
|
||||
$form->{subject} = qq|$form->{label} $form->{"${inv}number"}|
|
||||
unless $form->{subject};
|
||||
if (!$form->{email_attachment}) {
|
||||
$form->{do_not_attach} = 1;
|
||||
} else {
|
||||
$form->{do_not_attach} = 0;
|
||||
}
|
||||
$form->{subject} = parse_strings($myconfig, $form, $userspath, $form->{email_subject});
|
||||
$form->{message} = parse_strings($myconfig, $form, $userspath, $form->{email_body});
|
||||
$form->{OUT} = "$sendmail";
|
||||
} else {
|
||||
7dab0cbd | Moritz Bunkus | my $filename = Common::unique_id() . $form->{login} . ".pdf";
|
||
54e4131e | Moritz Bunkus | $form->{OUT} = ">$spool/$filename";
|
||
push(@{ $form->{DUNNING_PDFS} }, $filename);
|
||||
$form->{keep_tmpfile} = 1;
|
||||
}
|
||||
7dab0cbd | Moritz Bunkus | |||
840aea13 | Moritz Bunkus | delete($form->{tmpfile});
|
||
54e4131e | Moritz Bunkus | $form->parse_template($myconfig, $userspath);
|
||
$dbh->commit;
|
||||
$dbh->disconnect;
|
||||
$main::lxdebug->leave_sub();
|
||||
}
|
||||
sub get_invoices {
|
||||
$main::lxdebug->enter_sub();
|
||||
my ($self, $myconfig, $form) = @_;
|
||||
# connect to database
|
||||
my $dbh = $form->dbconnect($myconfig);
|
||||
fd6900cc | Moritz Bunkus | my $where;
|
||
7dab0cbd | Moritz Bunkus | my @values;
|
||
$form->{customer_id} = $1 if ($form->{customer} =~ /--(\d+)$/);
|
||||
if ($form->{customer_id}) {
|
||||
$where .= qq| AND (a.customer_id = ?)|;
|
||||
push(@values, $form->{customer_id});
|
||||
} elsif ($form->{customer}) {
|
||||
$where .= qq| AND (ct.name ILIKE ?)|;
|
||||
push(@values, '%' . $form->{customer} . '%');
|
||||
54e4131e | Moritz Bunkus | }
|
||
7dab0cbd | Moritz Bunkus | my %columns = (
|
||
"ordnumber" => "a.ordnumber",
|
||||
"invnumber" => "a.invnumber",
|
||||
431181fc | Moritz Bunkus | "notes" => "a.notes",
|
||
7dab0cbd | Moritz Bunkus | );
|
||
foreach my $key (keys(%columns)) {
|
||||
next unless ($form->{$key});
|
||||
$where .= qq| AND $columns{$key} ILIKE ?|;
|
||||
push(@values, '%' . $form->{$key} . '%');
|
||||
}
|
||||
54e4131e | Moritz Bunkus | |||
7dab0cbd | Moritz Bunkus | if ($form->{dunning_level}) {
|
||
431181fc | Moritz Bunkus | $where .= qq| AND nextcfg.id = ?|;
|
||
7dab0cbd | Moritz Bunkus | push(@values, conv_i($form->{dunning_level}));
|
||
}
|
||||
54e4131e | Moritz Bunkus | |||
$form->{minamount} = $form->parse_amount($myconfig,$form->{minamount});
|
||||
7dab0cbd | Moritz Bunkus | if ($form->{minamount}) {
|
||
$where .= qq| AND ((a.amount - a.paid) > ?) |;
|
||||
push(@values, $form->{minamount});
|
||||
}
|
||||
54e4131e | Moritz Bunkus | |||
7dab0cbd | Moritz Bunkus | $query =
|
||
fd6900cc | Moritz Bunkus | qq|SELECT
|
||
a.id, a.ordnumber, a.transdate, a.invnumber, a.amount,
|
||||
7dab0cbd | Moritz Bunkus | ct.name AS customername, a.customer_id, a.duedate,
|
||
fd6900cc | Moritz Bunkus | |||
cfg.dunning_description, cfg.dunning_level,
|
||||
d.transdate AS dunning_date, d.duedate AS dunning_duedate,
|
||||
d.fee, d.interest,
|
||||
a.duedate + cfg.terms - current_date AS nextlevel,
|
||||
current_date - COALESCE(d.duedate, a.duedate) AS pastdue,
|
||||
current_date + cfg.payment_terms AS next_duedate,
|
||||
nextcfg.dunning_description AS next_dunning_description,
|
||||
nextcfg.id AS next_dunning_config_id,
|
||||
nextcfg.terms, nextcfg.active, nextcfg.email
|
||||
FROM ar a
|
||||
LEFT JOIN customer ct ON (a.customer_id = ct.id)
|
||||
LEFT JOIN dunning_config cfg ON (a.dunning_config_id = cfg.id)
|
||||
LEFT JOIN dunning_config nextcfg ON
|
||||
(nextcfg.id =
|
||||
(SELECT id
|
||||
FROM dunning_config
|
||||
WHERE dunning_level >
|
||||
COALESCE((SELECT dunning_level
|
||||
FROM dunning_config
|
||||
WHERE id = a.dunning_config_id
|
||||
ORDER BY dunning_level DESC
|
||||
LIMIT 1),
|
||||
0)
|
||||
LIMIT 1))
|
||||
LEFT JOIN dunning d ON ((d.trans_id = a.id) AND (cfg.dunning_level = d.dunning_level))
|
||||
WHERE (a.paid < a.amount)
|
||||
AND (a.duedate < current_date)
|
||||
7dab0cbd | Moritz Bunkus | $where
|
||
fd6900cc | Moritz Bunkus | ORDER BY a.id, transdate, duedate, name|;
|
||
7dab0cbd | Moritz Bunkus | my $sth = prepare_execute_query($form, $dbh, $query, @values);
|
||
$form->{DUNNINGS} = [];
|
||||
54e4131e | Moritz Bunkus | |||
while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
|
||||
fd6900cc | Moritz Bunkus | next if !$ref->{terms} || ($ref->{pastdue} < $ref->{terms});
|
||
7dab0cbd | Moritz Bunkus | $ref->{interest} = $form->round_amount($ref->{interest}, 2);
|
||
fd6900cc | Moritz Bunkus | push(@{ $form->{DUNNINGS} }, $ref);
|
||
54e4131e | Moritz Bunkus | }
|
||
$sth->finish;
|
||||
7dab0cbd | Moritz Bunkus | $query = qq|SELECT id, dunning_description FROM dunning_config ORDER BY dunning_level|;
|
||
$form->{DUNNING_CONFIG} = selectall_hashref_query($form, $dbh, $query);
|
||||
54e4131e | Moritz Bunkus | |||
$dbh->disconnect;
|
||||
$main::lxdebug->leave_sub();
|
||||
}
|
||||
sub get_dunning {
|
||||
$main::lxdebug->enter_sub();
|
||||
my ($self, $myconfig, $form) = @_;
|
||||
# connect to database
|
||||
my $dbh = $form->dbconnect($myconfig);
|
||||
7dab0cbd | Moritz Bunkus | $where = qq| WHERE (da.trans_id = a.id)|;
|
||
54e4131e | Moritz Bunkus | |||
7dab0cbd | Moritz Bunkus | my @values;
|
||
54e4131e | Moritz Bunkus | |||
7dab0cbd | Moritz Bunkus | $form->{customer_id} = $1 if ($form->{customer} =~ /--(\d+)$/);
|
||
54e4131e | Moritz Bunkus | |||
7dab0cbd | Moritz Bunkus | if ($form->{customer_id}) {
|
||
$where .= qq| AND (a.customer_id = ?)|;
|
||||
push(@values, $form->{customer_id});
|
||||
54e4131e | Moritz Bunkus | |||
7dab0cbd | Moritz Bunkus | } elsif ($form->{customer}) {
|
||
$where .= qq| AND (ct.name ILIKE ?)|;
|
||||
push(@values, '%' . $form->{customer} . '%');
|
||||
}
|
||||
54e4131e | Moritz Bunkus | |||
7dab0cbd | Moritz Bunkus | my %columns = (
|
||
"ordnumber" => "a.ordnumber",
|
||||
"invnumber" => "a.invnumber",
|
||||
"notes" => "a.notes",
|
||||
);
|
||||
foreach my $key (keys(%columns)) {
|
||||
next unless ($form->{$key});
|
||||
$where .= qq| AND $columns{$key} ILIKE ?|;
|
||||
push(@values, '%' . $form->{$key} . '%');
|
||||
}
|
||||
54e4131e | Moritz Bunkus | |||
7dab0cbd | Moritz Bunkus | if ($form->{dunning_level}) {
|
||
840aea13 | Moritz Bunkus | $where .= qq| AND a.dunning_config_id = ?|;
|
||
7dab0cbd | Moritz Bunkus | push(@values, conv_i($form->{dunning_level}));
|
||
}
|
||||
54e4131e | Moritz Bunkus | |||
7dab0cbd | Moritz Bunkus | $form->{minamount} = $form->parse_amount($myconfig,$form->{minamount});
|
||
if ($form->{minamount}) {
|
||||
$where .= qq| AND ((a.amount - a.paid) > ?) |;
|
||||
push(@values, $form->{minamount});
|
||||
}
|
||||
54e4131e | Moritz Bunkus | |||
7dab0cbd | Moritz Bunkus | if (!$form->{showold}) {
|
||
840aea13 | Moritz Bunkus | $where .= qq| AND (a.amount > a.paid) AND (da.dunning_config_id = a.dunning_config_id) |;
|
||
7dab0cbd | Moritz Bunkus | }
|
||
54e4131e | Moritz Bunkus | |||
7dab0cbd | Moritz Bunkus | if ($form->{transdatefrom}) {
|
||
$where .= qq| AND a.transdate >= ?|;
|
||||
push(@values, $form->{transdatefrom});
|
||||
}
|
||||
if ($form->{transdateto}) {
|
||||
$where .= qq| AND a.transdate <= ?|;
|
||||
push(@values, $form->{transdateto});
|
||||
}
|
||||
if ($form->{dunningfrom}) {
|
||||
$where .= qq| AND da.transdate >= ?|;
|
||||
push(@values, $form->{dunningfrom});
|
||||
}
|
||||
if ($form->{dunningto}) {
|
||||
$where .= qq| AND da.transdate >= ?|;
|
||||
push(@values, $form->{dunningto});
|
||||
}
|
||||
54e4131e | Moritz Bunkus | |||
7dab0cbd | Moritz Bunkus | $query =
|
||
qq|SELECT a.id, a.ordnumber, a.invoice, a.transdate, a.invnumber, a.amount,
|
||||
840aea13 | Moritz Bunkus | 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
|
||||
FROM ar a
|
||||
JOIN customer ct ON (a.customer_id = ct.id), dunning da
|
||||
LEFT JOIN dunning_config dn ON (da.dunning_config_id = dn.id)
|
||||
$where
|
||||
ORDER BY name, a.id|;
|
||||
54e4131e | Moritz Bunkus | |||
7dab0cbd | Moritz Bunkus | $form->{DUNNINGS} = selectall_hashref_query($form, $dbh, $query, @values);
|
||
54e4131e | Moritz Bunkus | |||
7dab0cbd | Moritz Bunkus | foreach my $ref (@{ $form->{DUNNINGS} }) {
|
||
54e4131e | Moritz Bunkus | map { $ref->{$_} = $form->format_amount($myconfig, $ref->{$_}, 2)} qw(amount fee interest);
|
||
}
|
||||
$dbh->disconnect;
|
||||
$main::lxdebug->leave_sub();
|
||||
}
|
||||
sub parse_strings {
|
||||
$main::lxdebug->enter_sub();
|
||||
my ($myconfig, $form, $userspath, $string) = @_;
|
||||
my $format = $form->{format};
|
||||
$form->{format} = "html";
|
||||
$tmpstring = "parse_string.html";
|
||||
$tmpfile = "$myconfig->{templates}/$tmpstring";
|
||||
open(OUT, ">$tmpfile") or $form->error("$tmpfile : $!");
|
||||
print(OUT $string);
|
||||
close(OUT);
|
||||
my $in = $form->{IN};
|
||||
$form->{IN} = $tmpstring;
|
||||
$template = HTMLTemplate->new($tmpstring, $form, $myconfig, $userspath);
|
||||
my $fileid = time;
|
||||
$form->{tmpfile} = "$userspath/${fileid}.$tmpstring";
|
||||
$out = $form->{OUT};
|
||||
$form->{OUT} = ">$form->{tmpfile}";
|
||||
if ($form->{OUT}) {
|
||||
open(OUT, "$form->{OUT}") or $form->error("$form->{OUT} : $!");
|
||||
}
|
||||
if (!$template->parse(*OUT)) {
|
||||
$form->cleanup();
|
||||
$form->error("$form->{IN} : " . $template->get_error());
|
||||
}
|
||||
7dab0cbd | Moritz Bunkus | |||
54e4131e | Moritz Bunkus | close(OUT);
|
||
my $result = "";
|
||||
open(IN, $form->{tmpfile}) or $form->error($form->cleanup . "$form->{tmpfile} : $!");
|
||||
while (<IN>) {
|
||||
$result .= $_;
|
||||
}
|
||||
close(IN);
|
||||
# unlink($tmpfile);
|
||||
# unlink($form->{tmpfile});
|
||||
$form->{IN} = $in;
|
||||
$form->{format} = $format;
|
||||
$main::lxdebug->leave_sub();
|
||||
return $result;
|
||||
}
|
||||
sub melt_pdfs {
|
||||
$main::lxdebug->enter_sub();
|
||||
my ($self, $myconfig, $form, $userspath) = @_;
|
||||
7dab0cbd | Moritz Bunkus | |||
54e4131e | Moritz Bunkus | foreach my $file (@{ $form->{DUNNING_PDFS} }) {
|
||
$inputfiles .= " $userspath/$file ";
|
||||
}
|
||||
my $outputfile = "$userspath/dunning.pdf";
|
||||
system("gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=$outputfile $inputfiles");
|
||||
foreach my $file (@{ $form->{DUNNING_PDFS} }) {
|
||||
unlink("$userspath/$file");
|
||||
}
|
||||
7dab0cbd | Moritz Bunkus | $out = "";
|
||
54e4131e | Moritz Bunkus | |||
7dab0cbd | Moritz Bunkus | $form->{OUT} = $out;
|
||
54e4131e | Moritz Bunkus | |||
7dab0cbd | Moritz Bunkus | my $numbytes = (-s $outputfile);
|
||
open(IN, $outputfile)
|
||||
or $form->error($self->cleanup . "$outputfile : $!");
|
||||
54e4131e | Moritz Bunkus | |||
7dab0cbd | Moritz Bunkus | $form->{copies} = 1 unless $form->{media} eq 'printer';
|
||
54e4131e | Moritz Bunkus | |||
7dab0cbd | Moritz Bunkus | chdir("$self->{cwd}");
|
||
54e4131e | Moritz Bunkus | |||
7dab0cbd | Moritz Bunkus | for my $i (1 .. $form->{copies}) {
|
||
if ($form->{OUT}) {
|
||||
open(OUT, $form->{OUT})
|
||||
or $form->error($form->cleanup . "$form->{OUT} : $!");
|
||||
} else {
|
||||
54e4131e | Moritz Bunkus | |||
7dab0cbd | Moritz Bunkus | # launch application
|
||
print qq|Content-Type: Application/PDF
|
||||
54e4131e | Moritz Bunkus | Content-Disposition: attachment; filename="$outputfile"
|
||
Content-Length: $numbytes
|
||||
|;
|
||||
7dab0cbd | Moritz Bunkus | open(OUT, ">-") or $form->error($form->cleanup . "$!: STDOUT");
|
||
54e4131e | Moritz Bunkus | |||
7dab0cbd | Moritz Bunkus | }
|
||
54e4131e | Moritz Bunkus | |||
7dab0cbd | Moritz Bunkus | while (<IN>) {
|
||
print OUT $_;
|
||||
}
|
||||
54e4131e | Moritz Bunkus | |||
7dab0cbd | Moritz Bunkus | close(OUT);
|
||
54e4131e | Moritz Bunkus | |||
7dab0cbd | Moritz Bunkus | seek(IN, 0, 0);
|
||
}
|
||||
54e4131e | Moritz Bunkus | |||
7dab0cbd | Moritz Bunkus | close(IN);
|
||
54e4131e | Moritz Bunkus | unlink("$userspath/$outputfile");
|
||
$main::lxdebug->leave_sub();
|
||||
}
|
||||
4deefb65 | Philip Reetz | sub print_dunning {
|
||
$main::lxdebug->enter_sub();
|
||||
fd6900cc | Moritz Bunkus | my ($self, $myconfig, $form, $dunning_id, $userspath, $spool, $sendmail) = @_;
|
||
4deefb65 | Philip Reetz | # connect to database
|
||
my $dbh = $form->dbconnect_noauto($myconfig);
|
||||
7dab0cbd | Moritz Bunkus | my $query =
|
||
qq|SELECT invnumber, ordnumber, customer_id, amount, netamount,
|
||||
ar.transdate, ar.duedate, paid, amount - paid AS open_amount,
|
||||
template AS formname, email_subject, email_body, email_attachment,
|
||||
da.fee, da.interest, da.transdate AS dunning_date, da.duedate AS dunning_duedate
|
||||
840aea13 | Moritz Bunkus | FROM dunning da
|
||
LEFT JOIN dunning_config ON (dunning_config.id = da.dunning_config_id)
|
||||
LEFT JOIN ar ON (ar.id = da.trans_id)
|
||||
WHERE (da.dunning_id = ?)|;
|
||||
7dab0cbd | Moritz Bunkus | |||
840aea13 | Moritz Bunkus | my $sth = prepare_execute_query($form, $dbh, $query, $dunning_id);
|
||
4deefb65 | Philip Reetz | my $first = 1;
|
||
while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
|
||||
if ($first) {
|
||||
map({ $form->{"dn_$_"} = []; } keys(%{$ref}));
|
||||
$first = 0;
|
||||
}
|
||||
map { $ref->{$_} = $form->format_amount($myconfig, $ref->{$_}, 2) } qw(amount netamount paid open_amount fee interest);
|
||||
map { $form->{$_} = $ref->{$_} } keys %$ref;
|
||||
map { push @{ $form->{"dn_$_"} }, $ref->{$_}} keys %$ref;
|
||||
}
|
||||
$sth->finish;
|
||||
fd6900cc | Moritz Bunkus | $query =
|
||
qq|SELECT id AS customer_id, name, street, zipcode, city, country, department_1, department_2, email
|
||||
FROM customer
|
||||
WHERE id =
|
||||
(SELECT customer_id
|
||||
FROM dunning d
|
||||
LEFT JOIN ar ON (d.trans_id = ar.id)
|
||||
WHERE d.id = ?)|;
|
||||
$ref = selectfirst_hashref_query($form, $dbh, $query, $dunning_id);
|
||||
map { $form->{$_} = $ref->{$_} } keys %{ $ref };
|
||||
$query =
|
||||
qq|SELECT
|
||||
cfg.interest_rate, cfg.template AS formname,
|
||||
cfg.email_subject, cfg.email_body, cfg.email_attachment,
|
||||
071a3704 | Moritz Bunkus | d.fee, d.transdate AS dunning_date,
|
||
fd6900cc | Moritz Bunkus | (SELECT SUM(interest)
|
||
FROM dunning
|
||||
WHERE dunning_id = ?)
|
||||
AS total_interest,
|
||||
(SELECT SUM(amount) - SUM(paid)
|
||||
FROM ar
|
||||
WHERE id IN
|
||||
(SELECT trans_id
|
||||
FROM dunning
|
||||
WHERE dunning_id = ?))
|
||||
AS total_open_amount
|
||||
FROM dunning d
|
||||
LEFT JOIN dunning_config cfg ON (d.dunning_config_id = cfg.id)
|
||||
WHERE d.dunning_id = ?
|
||||
LIMIT 1|;
|
||||
$ref = selectfirst_hashref_query($form, $dbh, $query, $dunning_id, $dunning_id, $dunning_id);
|
||||
map { $form->{$_} = $ref->{$_} } keys %{ $ref };
|
||||
$form->{interest_rate} = $form->format_amount($myconfig, $ref->{interest_rate} * 100);
|
||||
$form->{fee} = $form->format_amount($myconfig, $ref->{fee}, 2);
|
||||
$form->{total_interest} = $form->format_amount($myconfig, $form->round_amount($ref->{total_interest}, 2), 2);
|
||||
$form->{total_open_amount} = $form->format_amount($myconfig, $form->round_amount($ref->{total_open_amount}, 2), 2);
|
||||
$form->{total_amount} = $form->format_amount($myconfig, $form->round_amount($ref->{fee} + $ref->{total_interest} + $ref->{total_open_amount}, 2), 2);
|
||||
4deefb65 | Philip Reetz | $form->{templates} = "$myconfig->{templates}";
|
||
$form->{language} = $form->get_template_language(\%myconfig);
|
||||
$form->{printer_code} = $form->get_printer_code(\%myconfig);
|
||||
if ($form->{language} ne "") {
|
||||
$form->{language} = "_" . $form->{language};
|
||||
}
|
||||
if ($form->{printer_code} ne "") {
|
||||
$form->{printer_code} = "_" . $form->{printer_code};
|
||||
}
|
||||
$form->{IN} = "$form->{formname}$form->{language}$form->{printer_code}.html";
|
||||
if ($form->{format} eq 'postscript') {
|
||||
$form->{postscript} = 1;
|
||||
$form->{IN} =~ s/html$/tex/;
|
||||
} elsif ($form->{"format"} =~ /pdf/) {
|
||||
$form->{pdf} = 1;
|
||||
if ($form->{"format"} =~ /opendocument/) {
|
||||
$form->{IN} =~ s/html$/odt/;
|
||||
} else {
|
||||
$form->{IN} =~ s/html$/tex/;
|
||||
}
|
||||
} elsif ($form->{"format"} =~ /opendocument/) {
|
||||
$form->{"opendocument"} = 1;
|
||||
$form->{"IN"} =~ s/html$/odt/;
|
||||
}
|
||||
if ($form->{"send_email"} && ($form->{email} ne "")) {
|
||||
$form->{media} = 'email';
|
||||
}
|
||||
$form->{keep_tmpfile} = 0;
|
||||
if ($form->{media} eq 'email') {
|
||||
$form->{subject} = qq|$form->{label} $form->{"${inv}number"}|
|
||||
unless $form->{subject};
|
||||
if (!$form->{email_attachment}) {
|
||||
$form->{do_not_attach} = 1;
|
||||
} else {
|
||||
$form->{do_not_attach} = 0;
|
||||
}
|
||||
$form->{subject} = parse_strings($myconfig, $form, $userspath, $form->{email_subject});
|
||||
$form->{message} = parse_strings($myconfig, $form, $userspath, $form->{email_body});
|
||||
$form->{OUT} = "$sendmail";
|
||||
} else {
|
||||
7dab0cbd | Moritz Bunkus | my $filename = Common::unique_id() . $form->{login} . ".pdf";
|
||
4deefb65 | Philip Reetz | |||
push(@{ $form->{DUNNING_PDFS} }, $filename);
|
||||
$form->{keep_tmpfile} = 1;
|
||||
}
|
||||
7dab0cbd | Moritz Bunkus | |||
4deefb65 | Philip Reetz | $form->parse_template($myconfig, $userspath);
|
||
$dbh->commit;
|
||||
$dbh->disconnect;
|
||||
$main::lxdebug->leave_sub();
|
||||
}
|
||||
54e4131e | Moritz Bunkus | 1;
|