kivitendo/bin/mozilla/dn.pl @ 602a4d98
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
|
||||
#
|
||||
#
|
||||
# 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
|
||||
#
|
||||
#======================================================================
|
||||
232a9153 | Moritz Bunkus | use POSIX;
|
||
54e4131e | Moritz Bunkus | use SL::IS;
|
||
use SL::PE;
|
||||
use SL::DN;
|
||||
35648bc6 | Moritz Bunkus | use SL::ReportGenerator;
|
||
54e4131e | Moritz Bunkus | |||
fec09045 | Moritz Bunkus | require "bin/mozilla/common.pl";
|
||
18da4a96 | Moritz Bunkus | require "bin/mozilla/reportgenerator.pl";
|
||
d629acd8 | Sven Schöling | require "bin/mozilla/io.pl";
|
||
54e4131e | Moritz Bunkus | |||
1;
|
||||
sub edit_config {
|
||||
$lxdebug->enter_sub();
|
||||
8c7e4493 | Moritz Bunkus | $auth->assert('config');
|
||
54e4131e | Moritz Bunkus | DN->get_config(\%myconfig, \%$form);
|
||
232a9153 | Moritz Bunkus | $form->get_lists('charts' => { 'key' => 'ALL_CHARTS',
|
||
'transdate' => 'current_date' });
|
||||
$form->{SELECT_AR_AMOUNT} = [];
|
||||
$form->{SELECT_AR} = [];
|
||||
foreach my $chart (@{ $form->{ALL_CHARTS} }) {
|
||||
$chart->{LINKS} = { map { $_, 1 } split m/:/, $chart->{link} };
|
||||
if ($chart->{LINKS}->{AR}) {
|
||||
$chart->{AR_selected} = "selected" if $chart->{id} == $form->{AR};
|
||||
push @{ $form->{SELECT_AR} }, $chart;
|
||||
}
|
||||
if ($chart->{LINKS}->{AR_amount}) {
|
||||
$chart->{AR_amount_fee_selected} = "selected" if $chart->{id} == $form->{AR_amount_fee};
|
||||
$chart->{AR_amount_interest_selected} = "selected" if $chart->{id} == $form->{AR_amount_interest};
|
||||
push @{ $form->{SELECT_AR_AMOUNT} }, $chart;
|
||||
}
|
||||
}
|
||||
54e4131e | Moritz Bunkus | |||
66631659 | Moritz Bunkus | $form->{title} = $locale->text('Edit Dunning Process Config');
|
||
$form->{callback} ||= build_std_url("action=edit_config");
|
||||
54e4131e | Moritz Bunkus | |||
fec09045 | Moritz Bunkus | $form->header();
|
||
9aaca433 | Moritz Bunkus | print $form->parse_html_template("dunning/edit_config");
|
||
54e4131e | Moritz Bunkus | |||
$lxdebug->leave_sub();
|
||||
}
|
||||
sub add {
|
||||
$lxdebug->enter_sub();
|
||||
2a4ad354 | Moritz Bunkus | |||
8c7e4493 | Moritz Bunkus | $auth->assert('dunning_edit');
|
||
54e4131e | Moritz Bunkus | # setup customer selection
|
||
$form->all_vc(\%myconfig, "customer", "AR");
|
||||
DN->get_config(\%myconfig, \%$form);
|
||||
2a4ad354 | Moritz Bunkus | $form->{SHOW_CUSTOMER_SELECTION} = $form->{all_customer} && scalar @{ $form->{all_customer} };
|
||
$form->{SHOW_DUNNING_LEVEL_SELECTION} = $form->{DUNNING} && scalar @{ $form->{DUNNING} };
|
||||
$form->{SHOW_DEPARTMENT_SELECTION} = $form->{all_departments} && scalar @{ $form->{all_departments} };
|
||||
$form->{title} = $locale->text('Start Dunning Process');
|
||||
$form->{jsscript} = 1;
|
||||
$form->{fokus} = "search.customer";
|
||||
fd6900cc | Moritz Bunkus | $form->header();
|
||
9aaca433 | Moritz Bunkus | print $form->parse_html_template("dunning/add");
|
||
54e4131e | Moritz Bunkus | |||
$lxdebug->leave_sub();
|
||||
}
|
||||
sub show_invoices {
|
||||
$lxdebug->enter_sub();
|
||||
8c7e4493 | Moritz Bunkus | $auth->assert('dunning_edit');
|
||
54e4131e | Moritz Bunkus | DN->get_invoices(\%myconfig, \%$form);
|
||
$form->{title} = $locale->text('Start Dunning Process');
|
||||
fa654209 | Moritz Bunkus | foreach my $row (@{ $form->{DUNNINGS} }) {
|
||
$row->{DUNNING_CONFIG} = [ map +{ %{ $_ } }, @{ $form->{DUNNING_CONFIG} } ];
|
||||
54e4131e | Moritz Bunkus | |||
fa654209 | Moritz Bunkus | if ($row->{next_dunning_config_id}) {
|
||
map { $_->{SELECTED} = $_->{id} == $row->{next_dunning_config_id} } @{ $row->{DUNNING_CONFIG } };
|
||||
fd6900cc | Moritz Bunkus | }
|
||
fa654209 | Moritz Bunkus | map { $row->{$_} = $form->format_amount(\%myconfig, $row->{$_} * 1, -2) } qw(amount fee interest);
|
||
54e4131e | Moritz Bunkus | }
|
||
232a9153 | Moritz Bunkus | $form->get_lists('printers' => 'printers',
|
||
'languages' => 'languages');
|
||||
50887746 | Moritz Bunkus | $form->{type} = 'dunning';
|
||
fa654209 | Moritz Bunkus | $form->{rowcount} = scalar @{ $form->{DUNNINGS} };
|
||
$form->{jsscript} = 1;
|
||||
8c7e4493 | Moritz Bunkus | $form->{callback} ||= build_std_url("action=show_invoices", qw(customer invnumber ordnumber groupinvoices minamount dunning_level notes));
|
||
54e4131e | Moritz Bunkus | |||
52352135 | Moritz Bunkus | $form->{PRINT_OPTIONS} = print_options('inline' => 1,
|
||
'no_queue' => 1,
|
||||
'no_postscript' => 1,
|
||||
'no_html' => 1,
|
||||
'no_opendocument' => 1,);
|
||||
54e4131e | Moritz Bunkus | |||
fa654209 | Moritz Bunkus | $form->header();
|
||
9aaca433 | Moritz Bunkus | print $form->parse_html_template("dunning/show_invoices");
|
||
54e4131e | Moritz Bunkus | |||
$lxdebug->leave_sub();
|
||||
}
|
||||
sub save {
|
||||
$lxdebug->enter_sub();
|
||||
8c7e4493 | Moritz Bunkus | $auth->assert('config');
|
||
54e4131e | Moritz Bunkus | for my $i (1 .. $form->{rowcount}) {
|
||
if ($form->{"dunning_description_$i"} ne "") {
|
||||
$form->isblank("dunning_level_$i", $locale->text('Dunning Level missing in row '). $i);
|
||||
$form->isblank("dunning_description_$i", $locale->text('Dunning Description missing in row '). $i);
|
||||
$form->isblank("terms_$i", $locale->text('Terms missing in row '). $i);
|
||||
$form->isblank("payment_terms_$i", $locale->text('Payment Terms missing in row '). $i);
|
||||
}
|
||||
}
|
||||
DN->save_config(\%myconfig, \%$form);
|
||||
2b89ec97 | Thomas Kasulke | # saving the history
|
||
if(!exists $form->{addition} && $form->{id} ne "") {
|
||||
8cef58b2 | Thomas Kasulke | $form->{snumbers} = qq|dunning_id_| . $form->{"dunning_id"};
|
||
$form->{addition} = "SAVED FOR DUNNING";
|
||||
2b89ec97 | Thomas Kasulke | $form->save_history($form->dbconnect(\%myconfig));
|
||
}
|
||||
35648bc6 | Moritz Bunkus | # /saving the history
|
||
54e4131e | Moritz Bunkus | $form->redirect($locale->text('Dunning Process Config saved!'));
|
||
$lxdebug->leave_sub();
|
||||
}
|
||||
sub save_dunning {
|
||||
$lxdebug->enter_sub();
|
||||
8c7e4493 | Moritz Bunkus | $auth->assert('dunning_edit');
|
||
54e4131e | Moritz Bunkus | my $active=1;
|
||
my @rows = ();
|
||||
undef($form->{DUNNING_PDFS});
|
||||
fd6900cc | Moritz Bunkus | |||
54e4131e | Moritz Bunkus | if ($form->{groupinvoices}) {
|
||
fd6900cc | Moritz Bunkus | my %dunnings_for;
|
||
for my $i (1 .. $form->{rowcount}) {
|
||||
next unless ($form->{"active_$i"});
|
||||
$dunnings_for{$form->{"customer_id_$i"}} ||= {};
|
||||
my $dunning_levels = $dunnings_for{$form->{"customer_id_$i"}};
|
||||
$dunning_levels->{$form->{"next_dunning_config_id_$i"}} ||= [];
|
||||
my $level = $dunning_levels->{$form->{"next_dunning_config_id_$i"}};
|
||||
push @{ $level }, { "row" => $i,
|
||||
"invoice_id" => $form->{"inv_id_$i"},
|
||||
"customer_id" => $form->{"customer_id_$i"},
|
||||
"next_dunning_config_id" => $form->{"next_dunning_config_id_$i"},
|
||||
"email" => $form->{"email_$i"}, };
|
||||
}
|
||||
foreach my $levels (values %dunnings_for) {
|
||||
foreach my $level (values %{ $levels }) {
|
||||
next unless scalar @{ $level };
|
||||
e6e07cb0 | Moritz Bunkus | DN->save_dunning(\%myconfig, $form, $level, $userspath, $spool);
|
||
54e4131e | Moritz Bunkus | }
|
||
}
|
||||
fd6900cc | Moritz Bunkus | |||
54e4131e | Moritz Bunkus | } else {
|
||
for my $i (1 .. $form->{rowcount}) {
|
||||
fd6900cc | Moritz Bunkus | next unless $form->{"active_$i"};
|
||
my $level = [ { "row" => $i,
|
||||
"invoice_id" => $form->{"inv_id_$i"},
|
||||
"customer_id" => $form->{"customer_id_$i"},
|
||||
"next_dunning_config_id" => $form->{"next_dunning_config_id_$i"},
|
||||
"email" => $form->{"email_$i"}, } ];
|
||||
e6e07cb0 | Moritz Bunkus | DN->save_dunning(\%myconfig, $form, $level, $userspath, $spool);
|
||
54e4131e | Moritz Bunkus | }
|
||
}
|
||||
fd6900cc | Moritz Bunkus | |||
54e4131e | Moritz Bunkus | if($form->{DUNNING_PDFS}) {
|
||
63ecb9a7 | Moritz Bunkus | DN->melt_pdfs(\%myconfig, $form, $form->{copies});
|
||
54e4131e | Moritz Bunkus | }
|
||
fd6900cc | Moritz Bunkus | |||
2b89ec97 | Thomas Kasulke | # saving the history
|
||
if(!exists $form->{addition} && $form->{id} ne "") {
|
||||
8cef58b2 | Thomas Kasulke | $form->{snumbers} = qq|dunning_id_| . $form->{"dunning_id"};
|
||
$form->{addition} = "DUNNING STARTED";
|
||||
2b89ec97 | Thomas Kasulke | $form->save_history($form->dbconnect(\%myconfig));
|
||
}
|
||||
fd6900cc | Moritz Bunkus | # /saving the history
|
||
63ecb9a7 | Moritz Bunkus | if ($form->{media} eq 'printer') {
|
||
delete $form->{callback};
|
||||
$form->redirect($locale->text('Dunning Process started for selected invoices!'));
|
||||
}
|
||||
54e4131e | Moritz Bunkus | |||
$lxdebug->leave_sub();
|
||||
}
|
||||
fd6900cc | Moritz Bunkus | |||
54e4131e | Moritz Bunkus | sub set_email {
|
||
$lxdebug->enter_sub();
|
||||
8c7e4493 | Moritz Bunkus | $auth->assert('dunning_edit');
|
||
54e4131e | Moritz Bunkus | $form->{"title"} = $locale->text("Set eMail text");
|
||
$form->header();
|
||||
9aaca433 | Moritz Bunkus | print($form->parse_html_template("dunning/set_email"));
|
||
54e4131e | Moritz Bunkus | |||
$lxdebug->leave_sub();
|
||||
}
|
||||
sub search {
|
||||
$lxdebug->enter_sub();
|
||||
2c5603bb | Moritz Bunkus | |||
8c7e4493 | Moritz Bunkus | $auth->assert('dunning_edit');
|
||
2c5603bb | Moritz Bunkus | $form->get_lists("customers" => "ALL_CUSTOMERS",
|
||
"departments" => "ALL_DEPARTMENTS");
|
||||
54e4131e | Moritz Bunkus | |||
DN->get_config(\%myconfig, \%$form);
|
||||
2c5603bb | Moritz Bunkus | $form->{SHOW_CUSTOMER_DDBOX} = scalar @{ $form->{ALL_CUSTOMERS} } <= $myconfig{vclimit};
|
||
$form->{SHOW_DEPARTMENT_DDBOX} = scalar @{ $form->{ALL_CUSTOMERS} };
|
||||
$form->{SHOW_DUNNING_LEVELS} = scalar @{ $form->{DUNNING} };
|
||||
54e4131e | Moritz Bunkus | |||
fade054f | Moritz Bunkus | $form->{jsscript} = 1;
|
||
$form->{title} = $locale->text('Search Dunning');
|
||||
$form->{fokus} = "search.customer";
|
||||
$form->header();
|
||||
54e4131e | Moritz Bunkus | |||
2c5603bb | Moritz Bunkus | $form->{onload} = qq|focus()|
|
||
. qq|;setupDateFormat('|. $myconfig{dateformat} .qq|', '|. $locale->text("Falsches Datumsformat!") .qq|')|
|
||||
. qq|;setupPoints('|. $myconfig{numberformat} .qq|', '|. $locale->text("wrongformat") .qq|')|;
|
||||
54e4131e | Moritz Bunkus | |||
9aaca433 | Moritz Bunkus | print $form->parse_html_template("dunning/search");
|
||
54e4131e | Moritz Bunkus | |||
$lxdebug->leave_sub();
|
||||
}
|
||||
sub show_dunning {
|
||||
$lxdebug->enter_sub();
|
||||
8c7e4493 | Moritz Bunkus | $auth->assert('dunning_edit');
|
||
35648bc6 | Moritz Bunkus | my @filter_field_list = qw(customer_id customer dunning_level department_id invnumber ordnumber
|
||
transdatefrom transdateto dunningfrom dunningto notes showold);
|
||||
840aea13 | Moritz Bunkus | |||
dde9d68d | Moritz Bunkus | report_generator_set_default_sort('customername', 1);
|
||
35648bc6 | Moritz Bunkus | DN->get_dunning(\%myconfig, \%$form);
|
||
54e4131e | Moritz Bunkus | |||
2c5603bb | Moritz Bunkus | if (!$form->{callback}) {
|
||
35648bc6 | Moritz Bunkus | $form->{callback} = build_std_url("action=show_dunning", @filter_field_list);
|
||
2c5603bb | Moritz Bunkus | }
|
||
54e4131e | Moritz Bunkus | |||
232a9153 | Moritz Bunkus | $form->get_lists('printers' => 'printers',
|
||
'languages' => 'languages');
|
||||
$form->{type} = 'dunning';
|
||||
52352135 | Moritz Bunkus | $form->{PRINT_OPTIONS} = print_options('inline' => 1,
|
||
'no_queue' => 1,
|
||||
'no_postscript' => 1,
|
||||
'no_html' => 1,
|
||||
'no_opendocument' => 1,);
|
||||
232a9153 | Moritz Bunkus | $form->{title} = $locale->text('Dunning overview');
|
||
35648bc6 | Moritz Bunkus | my $report = SL::ReportGenerator->new(\%myconfig, $form);
|
||
$report->set_options('std_column_visibility' => 1,
|
||||
'title' => $form->{title});
|
||||
$report->set_export_options('show_dunning', @filter_field_list);
|
||||
dde9d68d | Moritz Bunkus | my %column_defs = (
|
||
35648bc6 | Moritz Bunkus | 'checkbox' => { 'text' => '', 'visible' => 'HTML' },
|
||
'dunning_description' => { 'text' => $locale->text('Dunning Level') },
|
||||
'customername' => { 'text' => $locale->text('Customername') },
|
||||
'invnumber' => { 'text' => $locale->text('Invnumber') },
|
||||
'transdate' => { 'text' => $locale->text('Invdate') },
|
||||
'duedate' => { 'text' => $locale->text('Invoice Duedate') },
|
||||
'amount' => { 'text' => $locale->text('Amount') },
|
||||
'dunning_date' => { 'text' => $locale->text('Dunning Date') },
|
||||
'dunning_duedate' => { 'text' => $locale->text('Dunning Duedate') },
|
||||
'fee' => { 'text' => $locale->text('Total Fees') },
|
||||
'interest' => { 'text' => $locale->text('Interest') },
|
||||
);
|
||||
dde9d68d | Moritz Bunkus | $report->set_columns(%column_defs);
|
||
35648bc6 | Moritz Bunkus | $report->set_column_order(qw(checkbox dunning_description customername invnumber transdate
|
||
duedate amount dunning_date dunning_duedate fee interest));
|
||||
dde9d68d | Moritz Bunkus | $report->set_sort_indicator($form->{sort}, $form->{sortdir});
|
||
35648bc6 | Moritz Bunkus | |||
my $edit_url = build_std_url('script=is.pl', 'action=edit', 'callback') . '&id=';
|
||||
my $print_url = build_std_url('action=print_dunning', 'format=pdf', 'media=screen') . '&dunning_id=';
|
||||
dde9d68d | Moritz Bunkus | my $sort_url = build_std_url('action=show_dunning', grep { $form->{$_} } @filter_field_list);
|
||
foreach my $name (qw(dunning_description customername invnumber transdate duedate dunning_date dunning_duedate)) {
|
||||
my $sortdir = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
|
||||
$column_defs{$name}->{link} = $sort_url . "&sort=$name&sortdir=$sortdir";
|
||||
}
|
||||
35648bc6 | Moritz Bunkus | |||
my %alignment = map { $_ => 'right' } qw(transdate duedate amount dunning_date dunning_duedate fee interest);
|
||||
my ($current_dunning_rows, $previous_dunning_id, $first_row_for_dunning);
|
||||
$current_dunning_rows = [];
|
||||
$first_row_for_dunning = 1;
|
||||
$form->{rowcount} = scalar @{ $form->{DUNNINGS} };
|
||||
54e4131e | Moritz Bunkus | |||
35648bc6 | Moritz Bunkus | my $i = 0;
|
||
foreach $ref (@{ $form->{DUNNINGS} }) {
|
||||
$i++;
|
||||
if ($previous_dunning_id != $ref->{dunning_id}) {
|
||||
$report->add_data($current_dunning_rows) if (scalar @{ $current_dunning_rows });
|
||||
$current_dunning_rows = [];
|
||||
$first_row_for_dunning = 1;
|
||||
}
|
||||
my $row = { };
|
||||
foreach my $column (keys %{ $ref }) {
|
||||
$row->{$column} = {
|
||||
'data' => $first_row_for_dunning || (($column ne 'dunning_description') && ($column ne 'customername')) ? $ref->{$column} : '',
|
||||
'align' => $alignment{$column},
|
||||
'link' => ($column eq 'invnumber' ? $edit_url . E($ref->{id}) :
|
||||
$column eq 'dunning_description' ? $print_url . E($ref->{dunning_id}) : ''),
|
||||
};
|
||||
}
|
||||
68bd0229 | Moritz Bunkus | $row->{checkbox} = !$first_row_for_dunning ? { } : {
|
||
35648bc6 | Moritz Bunkus | 'raw_data' => $cgi->hidden('-name' => "dunning_id_$i", '-value' => $ref->{dunning_id})
|
||
. $cgi->checkbox('-name' => "selected_$i", '-value' => 1, '-label' => ''),
|
||||
'valign' => 'center',
|
||||
'align' => 'center',
|
||||
};
|
||||
push @{ $current_dunning_rows }, $row;
|
||||
$previous_dunning_id = $ref->{dunning_id};
|
||||
$first_row_for_dunning = 0;
|
||||
}
|
||||
$report->add_data($current_dunning_rows) if (scalar @{ $current_dunning_rows });
|
||||
9aaca433 | Moritz Bunkus | $report->set_options('raw_top_info_text' => $form->parse_html_template('dunning/show_dunning_top'),
|
||
'raw_bottom_info_text' => $form->parse_html_template('dunning/show_dunning_bottom'),
|
||||
35648bc6 | Moritz Bunkus | 'output_format' => 'HTML',
|
||
afe6d1f6 | Moritz Bunkus | 'attachment_basename' => $locale->text('dunning_list') . strftime('_%Y%m%d', localtime time),
|
||
35648bc6 | Moritz Bunkus | );
|
||
$report->set_options_from_form();
|
||||
$report->generate_with_headers();
|
||||
54e4131e | Moritz Bunkus | |||
$lxdebug->leave_sub();
|
||||
}
|
||||
4deefb65 | Philip Reetz | |||
sub print_dunning {
|
||||
$lxdebug->enter_sub();
|
||||
8c7e4493 | Moritz Bunkus | $auth->assert('dunning_edit');
|
||
232a9153 | Moritz Bunkus | $form->{rowcount} = 1;
|
||
$form->{selected_1} = 1;
|
||||
$form->{dunning_id_1} = $form->{dunning_id};
|
||||
print_multiple();
|
||||
$lxdebug->leave_sub();
|
||||
}
|
||||
sub print_multiple {
|
||||
$lxdebug->enter_sub();
|
||||
8c7e4493 | Moritz Bunkus | $auth->assert('dunning_edit');
|
||
232a9153 | Moritz Bunkus | $form->{title} = $locale->text('Print dunnings');
|
||
my @dunning_ids = map { $form->{"dunning_id_$_"} } grep { $form->{"selected_$_"} } (1..$form->{rowcount});
|
||||
if (!scalar @dunning_ids) {
|
||||
$form->error($locale->text('No dunnings have been selected for printing.'));
|
||||
}
|
||||
$form->{DUNNING_PDFS} = [];
|
||||
foreach my $dunning_id (@dunning_ids) {
|
||||
DN->print_invoice_for_fees(\%myconfig, $form, $dunning_id);
|
||||
DN->print_dunning(\%myconfig, $form, $dunning_id);
|
||||
}
|
||||
if (scalar @{ $form->{DUNNING_PDFS} }) {
|
||||
$form->{dunning_id} = strftime("%Y%m%d", localtime time);
|
||||
DN->melt_pdfs(\%myconfig, $form, $form->{copies});
|
||||
if ($form->{media} eq 'printer') {
|
||||
$form->header();
|
||||
$form->info($locale->text('The dunnings have been printed.'));
|
||||
}
|
||||
4deefb65 | Philip Reetz | |||
} else {
|
||||
232a9153 | Moritz Bunkus | $form->redirect($locale->text('Could not print dunning.'));
|
||
4deefb65 | Philip Reetz | }
|
||
$lxdebug->leave_sub();
|
||||
}
|
||||
8c7e4493 | Moritz Bunkus | sub continue {
|
||
call_sub($form->{nextsub});
|
||||
}
|
||||
54e4131e | Moritz Bunkus | # end of main
|