sub save_dunning {
$main::lxdebug->enter_sub();
- my ($self, $myconfig, $form, $rows, $userspath,$spool, $sendmail) = @_;
+ my ($self, $myconfig, $form, $rows, $userspath, $spool, $sendmail) = @_;
# connect to database
my $dbh = $form->dbconnect_noauto($myconfig);
my ($query, @values);
+ my ($dunning_id) = selectrow_query($form, $dbh, qq|SELECT nextval('id')|);
+
foreach my $row (@{ $rows }) {
$form->{"interest_$row"} = $form->parse_amount($myconfig,$form->{"interest_$row"});
$form->{"fee_$row"} = $form->parse_amount($myconfig,$form->{"fee_$row"});
$form->{send_email} = $form->{"email_$row"};
- $query = qq|UPDATE ar SET dunning_id = ? WHERE id = ?|;
- @values = ($form->{"next_dunning_id_$row"}, $form->{"inv_id_$row"});
+ $query = qq|UPDATE ar SET dunning_config_id = ? WHERE id = ?|;
+ @values = ($form->{"next_dunning_config_id_$row"},
+ $form->{"inv_id_$row"});
do_query($form, $dbh, $query, @values);
+
$query =
- qq|INSERT INTO dunning (dunning_id, dunning_level, trans_id, fee,
- interest, transdate, duedate)
- VALUES (?, (SELECT dunning_level FROM dunning_config WHERE id = ?),
+ 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 = ?),
?, ?, ?, current_date, ?)|;
- @values = (conv_i($form->{"next_dunning_id_$row"}),
- conv_i($form->{"next_dunning_id_$row"}),
+ @values = ($dunning_id,
+ conv_i($form->{"next_dunning_config_id_$row"}),
+ conv_i($form->{"next_dunning_config_id_$row"}),
conv_i($form->{"inv_id_$row"}), $form->{"fee_$row"},
$form->{"interest_$row"},
conv_date($form->{"next_duedate_$row"}));
template AS formname, email_subject, email_body, email_attachment,
da.fee, da.interest, da.transdate AS dunning_date,
da.duedate AS dunning_duedate
- FROM ar LEFT JOIN dunning_config ON (dunning_config.id = ar.dunning_id)
+ FROM ar LEFT JOIN dunning_config ON (dunning_config.id = ar.dunning_config_id)
LEFT JOIN dunning da ON (ar.id = da.trans_id AND dunning_config.dunning_level = da.dunning_level)
WHERE ar.id IN (|
. join(", ", map("?", @{ $form->{"inv_ids"} })) . qq|)|;
$form->{keep_tmpfile} = 1;
}
+ delete($form->{tmpfile});
$form->parse_template($myconfig, $userspath);
$dbh->commit;
WHERE dunning_level >
(SELECT
CASE
- WHEN a.dunning_id IS NULL
+ WHEN a.dunning_config_id IS NULL
THEN 0
- ELSE (SELECT dunning_level FROM dunning_config WHERE id = a.dunning_id ORDER BY dunning_level LIMIT 1)
+ ELSE (SELECT dunning_level
+ FROM dunning_config
+ WHERE id = a.dunning_config_id
+ ORDER BY dunning_level
+ LIMIT 1)
END
FROM dunning_config LIMIT 1)
LIMIT 1)) |;
}
if ($form->{dunning_level}) {
- $where .= qq| AND a.dunning_id = ?|;
+ $where .= qq| AND a.dunning_config_id = ?|;
push(@values, conv_i($form->{dunning_level}));
}
a.duedate + dnn.terms - current_date AS nextlevel,
$paymentdate - a.duedate AS pastdue, dn.dunning_level,
current_date + dnn.payment_terms AS next_duedate,
- dnn.dunning_description AS next_dunning_description, dnn.id AS next_dunning_id,
+ dnn.dunning_description AS next_dunning_description, dnn.id AS next_dunning_config_id,
dnn.interest AS interest_rate, dnn.terms
FROM dunning_config dnn, ar a
JOIN customer ct ON (a.customer_id = ct.id)
- LEFT JOIN dunning_config dn ON (dn.id = a.dunning_id)
+ LEFT JOIN dunning_config dn ON (dn.id = a.dunning_config_id)
LEFT JOIN dunning da ON ((da.trans_id = a.id) AND (dn.dunning_level = da.dunning_level))
$where
ORDER BY a.id, transdate, duedate, name|;
}
if ($form->{dunning_level}) {
- $where .= qq| AND a.dunning_id = ?|;
+ $where .= qq| AND a.dunning_config_id = ?|;
push(@values, conv_i($form->{dunning_level}));
}
}
if (!$form->{showold}) {
- $where .= qq| AND (a.amount > a.paid) AND (da.dunning_id = a.dunning_id) |;
+ $where .= qq| AND (a.amount > a.paid) AND (da.dunning_config_id = a.dunning_config_id) |;
}
if ($form->{transdatefrom}) {
$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
- FROM ar a
- JOIN customer ct ON (a.customer_id = ct.id), dunning da
- LEFT JOIN dunning_config dn ON (da.dunning_id = dn.id)
- $where
- ORDER BY name, a.id|;
+ 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|;
$form->{DUNNINGS} = selectall_hashref_query($form, $dbh, $query, @values);
my ($self, $myconfig, $form, $userspath) = @_;
- map({ $_ =~ s|.*/||g; } @{ $form->{DUNNING_PDFS} });
-
foreach my $file (@{ $form->{DUNNING_PDFS} }) {
$inputfiles .= " $userspath/$file ";
}
sub print_dunning {
$main::lxdebug->enter_sub();
- my ($self, $myconfig, $form, $dunning_id, $customer_id, $userspath,$spool, $sendmail) = @_;
+ my ($self, $myconfig, $form, $dunning_id, $userspath,$spool, $sendmail) = @_;
# connect to database
my $dbh = $form->dbconnect_noauto($myconfig);
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
- FROM ar
- LEFT JOIN dunning_config ON (dunning_config.id = ar.dunning_id)
- LEFT JOIN dunning da ON ((ar.id = da.trans_id) AND (dunning_config.dunning_level = da.dunning_level))
- WHERE (ar.dunning_id = ?) AND (customer_id = ?)|;
+ 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 = ?)|;
- my $sth = prepare_execute_query($form, $dbh, $query, $dunning_id, $customer_id);
+ my $sth = prepare_execute_query($form, $dbh, $query, $dunning_id);
my $first = 1;
while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
if ($first) {
$column_data{dunning_level} =
qq|<td><input type=hidden size=2 name=dunning_level_$i value=$i>$i</td>|;
$column_data{dunning_description} = qq|<td><input name=dunning_description_$i ></td>|;
- my $active = "";
$column_data{active} =
- qq|<td><input type=checkbox name=active_$i value=1 $active></td>|;
+ qq|<td><input type=checkbox name=active_$i value=1></td>|;
my $email = "";
$column_data{email} =
qq|<td><input type=checkbox name=email_$i value=1 $email><button type="button" onclick="set_email_window('email_subject_$i', 'email_body_$i', 'email_attachment_$i')">| . $locale->text('L') . qq|</button><input type=hidden name=email_body_$i><input type=hidden name=email_subject_$i><input type=hidden name=email_attachment_$i></td>|;
|;
$form->{selectdunning} =~ s/ selected//g;
- if ($ref->{next_dunning_id} ne "") {
- $form->{selectdunning} =~ s/value=$ref->{next_dunning_id}/value=$ref->{next_dunning_id} selected/;
+ if ($ref->{next_dunning_config_id} ne "") {
+ $form->{selectdunning} =~ s/value=$ref->{next_dunning_config_id}/value=$ref->{next_dunning_config_id} selected/;
}
- $dunning = qq|<select name=next_dunning_id_$i>$form->{selectdunning}</select>|;
+ $dunning = qq|<select name=next_dunning_config_id_$i>$form->{selectdunning}</select>|;
$column_data{dunning_description} = qq|<td><input type=hidden name=inv_id_$i size=2 value="$ref->{id}"><input type=hidden name=customer_id_$i size=2 value="$ref->{customer_id}">$ref->{dunning_level}: $dunning</td>|;
}
}
if (scalar(@{ $form->{inv_ids} }) != 0) {
- DN->save_dunning(\%myconfig, \%$form, \@rows, $userspath,$spool, $sendmail);
+ DN->save_dunning(\%myconfig, \%$form, \@rows, $userspath, $spool, $sendmail);
}
}
} else {
@rows = ();
$form->{inv_ids} = [ $form->{"inv_id_$i"} ];
push(@rows, $i);
- DN->save_dunning(\%myconfig, \%$form, \@rows, $userspath,$spool, $sendmail);
+ DN->save_dunning(\%myconfig, \%$form, \@rows, $userspath, $spool, $sendmail);
}
}
}
print qq|
</tr>
|;
+
+ my %columns = (
+ "dunning_duedate" => "next_duedate",
+ "duedate" => "inv_duedate",
+ "transdate" => "invdate",
+ "amount" => "invamount",
+ );
+
my $i = 0;
my $j = 0;
- my $previous_customer_id;
+ my ($previous_dunning_id, $first_row_for_dunning);
foreach $ref (@{ $form->{DUNNINGS} }) {
$i++;
- $j++ if ($previous_customer_id != $ref->{customer_id});
- $j = $j % 2;
- $previous_customer_id = $ref->{customer_id};
+
+ if ($previous_dunning_id != $ref->{dunning_id}) {
+ $j++;
+ $j = $j % 2;
+ $first_row_for_dunning = 1;
+ } else {
+ $first_row_for_dunning = 0;
+ }
+ $previous_dunning_id = $ref->{dunning_id};
print qq|
<tr valign=top class=listrow$j>
- $dunning = qq|<select name=next_dunning_id_$i>$form->{selectdunning}</select>|;
- my $script = "";
- if ($ref->{invoice}) {
- $script = "is.pl";
- } else {
- $script = "ar.pl";
+ foreach (qw(dunning_date dunning_duedate duedate transdate customername
+ amount fee interest)) {
+ my $col = $columns{$_} ? $columns{$_} : $_;
+ $column_data{$col} = "<td>" . H($ref->{$_}) . "</td>";
}
- $column_data{dunning_description} = qq|<td><a href=dn.pl?action=print_dunning&dunning_id=$ref->{dunning_id}&customer_id=$ref->{customer_id}&format=pdf&media=screen&path=$form->{path}&login=$form->{login}&password=$form->{password}&callback=$form->{callback}>$ref->{dunning_description}</a></td>|;
- my $active = "checked";
- $column_data{dunning_date} = qq|<td>$ref->{dunning_date}</td>|;
- $column_data{next_duedate} = qq|<td>$ref->{dunning_duedate}</td>|;
-
- $column_data{inv_duedate} = qq|<td>$ref->{duedate}</td>|;
- $column_data{invdate} = qq|<td>$ref->{transdate}</td>|;
- $column_data{invnumber} = qq|<td><a href=$script?action=edit&id=$ref->{id}&path=$form->{path}&login=$form->{login}&password=$form->{password}&callback=$form->{callback}>$ref->{invnumber}</a></td>|;
- $column_data{customername} = qq|<td>$ref->{customername}</td>|;
- $column_data{invamount} = qq|<td>$ref->{amount}</td>|;
- $column_data{fee} = qq|<td>$ref->{fee}</td>|;
- $column_data{interest} = qq|<td>$ref->{interest}</td>|;
+ if ($first_row_for_dunning) {
+ $column_data{dunning_description} =
+ qq|<td><a href="dn.pl?action=print_dunning&format=pdf&media=screen&| .
+ qq|dunning_id=| . E($ref->{dunning_id}) .
+ join(map({ "&${_}=" . E($form->{$_}) } qw(login path password callback))) .
+ qq|">| . H($ref->{dunning_description}) . qq|</a></td>|;
+ } else {
+ $column_data{dunning_description} = qq|<td> </td>|;
+ $column_data{customername} = qq|<td> </td>|;
+ }
+ $column_data{invnumber} =
+ qq|<td><a href="| . ($ref->{invoice} ? "is.pl" : "ar.pl" ) .
+ qq|?action=edit&id=| . H($ref->{id}) .
+ join(map({ "&${_}=" . E($form->{$_}) } qw(login path password callback))) .
+ qq|">| . H($ref->{invnumber}) . qq|</a></td>|;
map { print "$column_data{$_}\n" } @column_index;
sub print_dunning {
$lxdebug->enter_sub();
- DN->print_dunning(\%myconfig, \%$form, $form->{dunning_id}, $form->{customer_id}, $userspath, $spool, $sendmail);
+ DN->print_dunning(\%myconfig, \%$form, $form->{dunning_id}, $userspath, $spool, $sendmail);
if($form->{DUNNING_PDFS}) {
DN->melt_pdfs(\%myconfig, \%$form,$spool);