1 #=====================================================================
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
7 #=====================================================================
8 # SQL-Ledger Accounting
9 # Copyright (c) 1998-2002
11 # Author: Dieter Simader
12 # Email: dsimader@sql-ledger.org
13 # Web: http://www.sql-ledger.org
16 # This program is free software; you can redistribute it and/or modify
17 # it under the terms of the GNU General Public License as published by
18 # the Free Software Foundation; either version 2 of the License, or
19 # (at your option) any later version.
21 # This program is distributed in the hope that it will be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 # GNU General Public License for more details.
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not, write to the Free Software
27 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
29 #======================================================================
31 # Dunning process module
33 #======================================================================
35 use POSIX qw(strftime);
37 use List::MoreUtils qw(none);
41 use SL::DB::Department;
44 use SL::Helper::Flash qw(flash);
45 use SL::Locale::String qw(t8);
46 use SL::Presenter::FileObject;
47 use SL::ReportGenerator;
49 require "bin/mozilla/common.pl";
50 require "bin/mozilla/reportgenerator.pl";
51 require "bin/mozilla/io.pl";
58 $main::lxdebug->enter_sub();
60 my $form = $main::form;
61 my %myconfig = %main::myconfig;
62 my $locale = $main::locale;
64 $main::auth->assert('config');
66 DN->get_config(\%myconfig, \%$form);
67 $form->get_lists('charts' => { 'key' => 'ALL_CHARTS',
68 'transdate' => 'current_date' });
70 $form->{SELECT_AR_AMOUNT} = [];
71 $form->{SELECT_AR} = [];
73 foreach my $chart (@{ $form->{ALL_CHARTS} }) {
74 $chart->{LINKS} = { map { $_, 1 } split m/:/, $chart->{link} };
76 if ($chart->{LINKS}->{AR}) {
77 $chart->{AR_selected} = "selected" if $chart->{id} == $form->{AR};
78 push @{ $form->{SELECT_AR} }, $chart;
81 if ($chart->{LINKS}->{AR_amount}) {
82 $chart->{AR_amount_fee_selected} = "selected" if $chart->{id} == $form->{AR_amount_fee};
83 $chart->{AR_amount_interest_selected} = "selected" if $chart->{id} == $form->{AR_amount_interest};
84 push @{ $form->{SELECT_AR_AMOUNT} }, $chart;
88 $form->{title} = $locale->text('Edit Dunning Process Config');
89 $form->{callback} ||= build_std_url("action=edit_config");
91 setup_dn_edit_config_action_bar();
94 print $form->parse_html_template("dunning/edit_config");
96 $main::lxdebug->leave_sub();
100 $main::lxdebug->enter_sub();
102 my $form = $main::form;
103 my %myconfig = %main::myconfig;
104 my $locale = $main::locale;
106 $main::auth->assert('dunning_edit');
108 DN->get_config(\%myconfig, \%$form);
110 $form->get_lists("departments" => "ALL_DEPARTMENTS");
112 $form->{SHOW_DUNNING_LEVEL_SELECTION} = $form->{DUNNING} && scalar @{ $form->{DUNNING} };
113 $form->{SHOW_DEPARTMENT_SELECTION} = $form->{ALL_DEPARTMENTS} && scalar @{ $form->{ALL_DEPARTMENTS} || [] };
115 $form->{title} = $locale->text('Start Dunning Process');
117 setup_dn_add_action_bar();
120 print $form->parse_html_template("dunning/add");
122 $main::lxdebug->leave_sub();
126 $main::lxdebug->enter_sub();
128 my $form = $main::form;
129 my %myconfig = %main::myconfig;
130 my $locale = $main::locale;
132 $main::auth->assert('dunning_edit');
134 DN->get_invoices(\%myconfig, \%$form);
135 $form->{title} = $locale->text('Start Dunning Process');
137 foreach my $row (@{ $form->{DUNNINGS} }) {
138 $row->{DUNNING_CONFIG} = [ map +{ %{ $_ } }, @{ $form->{DUNNING_CONFIG} } ];
140 if ($row->{next_dunning_config_id}) {
141 map { $_->{SELECTED} = $_->{id} == $row->{next_dunning_config_id} } @{ $row->{DUNNING_CONFIG } };
143 map { $row->{$_} = $form->format_amount(\%myconfig, $row->{$_} * 1, 2) } qw(amount open_amount fee interest);
145 if ($row->{'language_id'}) {
146 $row->{language} = SL::DB::Manager::Language->find_by_or_create('id' => $row->{'language_id'})->{'description'};
150 $form->get_lists('printers' => 'printers',
151 'languages' => 'languages');
153 $form->{type} = 'dunning';
154 $form->{rowcount} = scalar @{ $form->{DUNNINGS} };
155 $form->{callback} ||= build_std_url("action=show_invoices", qw(customer invnumber ordnumber groupinvoices minamount dunning_level notes));
157 $form->{PRINT_OPTIONS} = print_options('inline' => 1,
159 'no_postscript' => 1,
161 'no_opendocument' => 1,);
163 setup_dn_show_invoices_action_bar();
165 print $form->parse_html_template("dunning/show_invoices");
167 $main::lxdebug->leave_sub();
171 $main::lxdebug->enter_sub();
173 my $form = $main::form;
174 my %myconfig = %main::myconfig;
175 my $locale = $main::locale;
177 $main::auth->assert('config');
179 for my $i (1 .. $form->{rowcount}) {
180 if ($form->{"dunning_description_$i"} ne "") {
181 $form->isblank("dunning_level_$i", $locale->text('Dunning Level missing in row '). $i);
182 $form->isblank("dunning_description_$i", $locale->text('Dunning Description missing in row '). $i);
183 $form->isblank("terms_$i", $locale->text('Terms missing in row '). $i);
184 $form->isblank("payment_terms_$i", $locale->text('Payment Terms missing in row '). $i);
188 DN->save_config(\%myconfig, \%$form);
190 if(!exists $form->{addition} && $form->{id} ne "") {
191 $form->{snumbers} = qq|dunning_id_| . $form->{"dunning_id"};
192 $form->{addition} = "SAVED FOR DUNNING";
195 # /saving the history
196 $form->redirect($locale->text('Dunning Process Config saved!'));
198 $main::lxdebug->leave_sub();
202 $main::lxdebug->enter_sub();
204 my $form = $main::form;
205 my %myconfig = %main::myconfig;
206 my $locale = $main::locale;
208 $main::auth->assert('dunning_edit');
212 undef($form->{DUNNING_PDFS});
214 my $saved_language_id = $form->{language_id};
216 if ($form->{groupinvoices} || $form->{l_include_credit_notes}) {
219 for my $i (1 .. $form->{rowcount}) {
220 next unless ($form->{"active_$i"});
222 $dunnings_for{$form->{"customer_id_$i"}} ||= {};
223 my $dunning_levels = $dunnings_for{$form->{"customer_id_$i"}};
225 $dunning_levels->{$form->{"next_dunning_config_id_$i"}} ||= [];
226 my $level = $dunning_levels->{$form->{"next_dunning_config_id_$i"}};
228 push @{ $level }, { "row" => $i,
229 "invoice_id" => $form->{"inv_id_$i"},
230 "credit_note" => $form->{"credit_note_$i"},
231 "customer_id" => $form->{"customer_id_$i"},
232 "language_id" => $form->{"language_id_$i"},
233 "next_dunning_config_id" => $form->{"next_dunning_config_id_$i"},
234 "print_invoice" => $form->{"include_invoice_$i"},
235 "email" => $form->{"email_$i"}, };
238 foreach my $levels (values %dunnings_for) {
239 foreach my $level (values %{ $levels }) {
240 next unless scalar @{ $level };
241 if (!$form->{force_lang}) {
242 $form->{language_id} = @{$level}[0]->{language_id};
244 DN->save_dunning(\%myconfig, $form, $level);
249 for my $i (1 .. $form->{rowcount}) {
250 next unless $form->{"active_$i"};
252 my $level = [ { "row" => $i,
253 "invoice_id" => $form->{"inv_id_$i"},
254 "customer_id" => $form->{"customer_id_$i"},
255 "language_id" => $form->{"language_id_$i"},
256 "next_dunning_config_id" => $form->{"next_dunning_config_id_$i"},
257 "print_invoice" => $form->{"include_invoice_$i"},
258 "email" => $form->{"email_$i"}, } ];
259 if (!$form->{force_lang}) {
260 $form->{language_id} = @{$level}[0]->{language_id};
262 DN->save_dunning(\%myconfig, $form, $level);
266 $form->{language_id} = $saved_language_id;
268 if (scalar @{ $form->{DUNNING_PDFS} }) {
269 $form->{dunning_id} = strftime("%Y%m%d", localtime time) if scalar @{ $form->{DUNNING_PDFS}} > 1;
270 DN->melt_pdfs(\%myconfig, $form, $form->{copies});
274 if(!exists $form->{addition} && $form->{id} ne "") {
275 $form->{snumbers} = qq|dunning_id_| . $form->{"dunning_id"};
276 $form->{addition} = "DUNNING STARTED";
279 # /saving the history
281 if ($form->{media} eq 'printer') {
282 delete $form->{callback};
283 $form->redirect($locale->text('Dunning Process started for selected invoices!'));
286 $main::lxdebug->leave_sub();
290 $main::lxdebug->enter_sub();
292 my $form = $main::form;
293 my $locale = $main::locale;
295 $main::auth->assert('dunning_edit');
297 $form->{"title"} = $locale->text("Set eMail text");
298 $form->header(no_layout => 1);
299 print($form->parse_html_template("dunning/set_email"));
301 $main::lxdebug->leave_sub();
305 $main::lxdebug->enter_sub();
307 my $form = $main::form;
308 my %myconfig = %main::myconfig;
309 my $locale = $main::locale;
311 $main::auth->assert('dunning_edit');
313 $form->get_lists("customers" => "ALL_CUSTOMERS",
314 "departments" => "ALL_DEPARTMENTS");
315 $form->{ALL_EMPLOYEES} = SL::DB::Manager::Employee->get_all_sorted(query => [ deleted => 0 ]);
317 DN->get_config(\%myconfig, \%$form);
319 $form->{SHOW_DUNNING_LEVELS} = scalar @{ $form->{DUNNING} };
321 $form->{title} = $locale->text('Dunnings');
323 setup_dn_search_action_bar();
326 print $form->parse_html_template("dunning/search");
328 $main::lxdebug->leave_sub();
333 $main::lxdebug->enter_sub();
335 my $form = $main::form;
336 my %myconfig = %main::myconfig;
337 my $locale = $main::locale;
338 my $cgi = $::request->{cgi};
340 $main::auth->assert('dunning_edit');
342 my @filter_field_list = qw(customer_id customer dunning_id dunning_level department_id invnumber ordnumber
343 transdatefrom transdateto dunningfrom dunningto notes showold l_salesman salesman_id);
345 report_generator_set_default_sort('customername', 1);
347 DN->get_dunning(\%myconfig, \%$form);
349 if (!$form->{callback}) {
350 $form->{callback} = build_std_url("action=show_dunning", @filter_field_list);
353 $form->get_lists('printers' => 'printers',
354 'languages' => 'languages');
356 $form->{type} = 'dunning';
357 $form->{PRINT_OPTIONS} = print_options('inline' => 1,
359 'no_postscript' => 1,
361 'no_opendocument' => 1,);
362 $form->{title} = $locale->text('Dunning overview');
364 my $report = SL::ReportGenerator->new(\%myconfig, $form);
366 $report->set_options('std_column_visibility' => 1,
367 'title' => $form->{title});
368 $report->set_export_options('show_dunning', @filter_field_list, qw(sort sortdir));
371 'checkbox' => { 'text' => '', 'visible' => 'HTML' },
372 'dunning_description' => { 'text' => $locale->text('Dunning Level') },
373 'customername' => { 'text' => $locale->text('Customername') },
374 'departmentname' => { 'text' => $locale->text('Department') },
375 'language' => { 'text' => $locale->text('Language') },
376 'invnumber' => { 'text' => $locale->text('Invnumber') },
377 'transdate' => { 'text' => $locale->text('Invdate') },
378 'duedate' => { 'text' => $locale->text('Invoice Duedate') },
379 'amount' => { 'text' => $locale->text('Amount') },
380 'dunning_id' => { 'text' => $locale->text('Dunning number') },
381 'dunning_date' => { 'text' => $locale->text('Dunning Date') },
382 'dunning_duedate' => { 'text' => $locale->text('Dunning Duedate') },
383 'fee' => { 'text' => $locale->text('Total Fees') },
384 'interest' => { 'text' => $locale->text('Interest') },
385 'salesman' => { 'text' => $locale->text('Salesperson'), 'visible' => $form->{l_salesman} ? 1 : 0 },
386 'documents' => { 'text' => $locale->text('Documents') , 'visible' => $::instance_conf->get_doc_storage ? 1 : 0 },
389 $report->set_columns(%column_defs);
390 $report->set_column_order(qw(checkbox dunning_description dunning_id customername language invnumber transdate
391 duedate amount dunning_date dunning_duedate fee interest salesman));
392 $report->set_sort_indicator($form->{sort}, $form->{sortdir});
394 my $edit_url = sub { build_std_url('script=' . ($_[0]->{invoice} ? 'is' : 'ar') . '.pl', 'action=edit', 'callback') . '&id=' . $::form->escape($_[0]->{id}) };
395 my $print_url = sub { build_std_url('action=print_dunning', 'format=pdf', 'media=screen', 'dunning_id='.$_[0]->{dunning_id}, 'language_id=' . $_[0]->{language_id}) };
396 my $sort_url = build_std_url('action=show_dunning', grep { $form->{$_} } @filter_field_list);
398 foreach my $name (qw(dunning_description customername invnumber transdate duedate dunning_date dunning_duedate salesman dunning_id)) {
399 my $sortdir = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
400 $column_defs{$name}->{link} = $sort_url . "&sort=$name&sortdir=$sortdir";
403 my %alignment = map { $_ => 'right' } qw(transdate duedate amount dunning_date dunning_duedate fee interest salesman dunning_id);
405 my ($current_dunning_rows, $previous_dunning_id, $first_row_for_dunning);
407 $current_dunning_rows = [];
408 $first_row_for_dunning = 1;
409 $form->{rowcount} = scalar @{ $form->{DUNNINGS} };
413 foreach my $ref (@{ $form->{DUNNINGS} }) {
416 if ($previous_dunning_id != $ref->{dunning_id}) {
417 $report->add_data($current_dunning_rows) if (scalar @{ $current_dunning_rows });
418 $current_dunning_rows = [];
419 $first_row_for_dunning = 1;
422 if ($ref->{'language_id'}) {
423 $ref->{language} = SL::DB::Manager::Language->find_by('id' => $ref->{'language_id'})->{'description'};
427 foreach my $column (keys %{ $ref }) {
429 'data' => $first_row_for_dunning || (none { $_ eq $column } qw(dunning_description customername dunning_id)) ? $ref->{$column} : '',
431 'align' => $alignment{$column},
433 'link' => ( $column eq 'invnumber' ? $edit_url->($ref)
434 : $column eq 'dunning_description' ? $print_url->($ref)
435 : $column eq 'dunning_id' ? $print_url->($ref)
440 $row->{checkbox} = !$first_row_for_dunning ? { } : {
441 'raw_data' => $cgi->hidden('-name' => "dunning_id_$i", '-value' => $ref->{dunning_id})
442 . $cgi->checkbox('-name' => "selected_$i", '-value' => 1, '-label' => ''),
443 'valign' => 'center',
447 if ($first_row_for_dunning) {
448 $row->{language} = {'raw_data' => $cgi->hidden('-name' => "language_id_$i", '-value' => $ref->{language_id})
449 . " $ref->{language}" };
451 $row->{language} = { };
454 if ($::instance_conf->get_doc_storage && $first_row_for_dunning) {
455 my @files = SL::File->get_all_versions(object_id => $ref->{dunning_id},
456 object_type => 'dunning',
457 file_type => 'document',);
459 my $html = join '<br>', map { SL::Presenter::FileObject::file_object($_) } @files;
460 my $text = join "\n", map { $_->file_name } @files;
461 $row->{documents} = { 'raw_data' => $html, data => $text };
463 $row->{documents} = { };
467 push @{ $current_dunning_rows }, $row;
469 $previous_dunning_id = $ref->{dunning_id};
470 $first_row_for_dunning = 0;
473 $report->add_data($current_dunning_rows) if (scalar @{ $current_dunning_rows });
475 $report->set_options('raw_top_info_text' => $form->parse_html_template('dunning/show_dunning_top'),
476 'raw_bottom_info_text' => $form->parse_html_template('dunning/show_dunning_bottom'),
477 'output_format' => 'HTML',
478 'attachment_basename' => $locale->text('dunning_list') . strftime('_%Y%m%d', localtime time),
481 $report->set_options_from_form();
483 setup_dn_show_dunning_action_bar();
484 $report->generate_with_headers();
486 $main::lxdebug->leave_sub();
491 $main::lxdebug->enter_sub();
493 my $form = $main::form;
495 $main::auth->assert('dunning_edit');
497 $form->{rowcount} = 1;
498 $form->{selected_1} = 1;
499 $form->{dunning_id_1} = $form->{dunning_id};
500 $form->{language_id_1} = $form->{language_id};
504 $main::lxdebug->leave_sub();
508 $main::auth->assert('dunning_edit');
510 my @dunning_ids = map { $::form->{"dunning_id_$_"} } grep { $::form->{"selected_$_"} } (1..$::form->{rowcount});
512 if (!scalar @dunning_ids) {
513 $::form->error($::locale->text('No dunnings have been selected for printing.'));
516 my $dunnings = SL::DB::Manager::Dunning->get_all(query => [ dunning_id => \@dunning_ids ]);
518 SL::DB::Dunning->new->db->with_transaction(sub {
519 for my $dunning (@$dunnings) {
520 SL::DB::Manager::Invoice->find_by(id => $dunning->trans_id)->update_attributes(dunning_config_id => undef);
525 flash('info', t8('#1 dunnings have been deleted', scalar @$dunnings));
531 $main::lxdebug->enter_sub();
533 my $form = $main::form;
534 my %myconfig = %main::myconfig;
535 my $locale = $main::locale;
537 $main::auth->assert('dunning_edit');
539 $form->{title} = $locale->text('Print dunnings');
541 my @dunning_ids = map { $form->{"dunning_id_$_"} } grep { $form->{"selected_$_"} } (1..$form->{rowcount});
542 my @language_ids = map { $form->{"language_id_$_"} } grep { $form->{"selected_$_"} } (1..$form->{rowcount});
544 if (!scalar @dunning_ids) {
545 $form->error($locale->text('No dunnings have been selected for printing.'));
548 $form->{DUNNING_PDFS} = [];
550 my $saved_language_id = $form->{language_id};
552 foreach my $dunning_id (@dunning_ids) {
553 if (!$form->{force_lang}) {
554 $form->{language_id} = $language_ids[$i];
556 $form->{dunning_id} = $dunning_id;
557 DN->print_invoice_for_fees(\%myconfig, $form, $dunning_id);
558 DN->print_dunning(\%myconfig, $form, $dunning_id);
560 # print original dunned invoices, if they where printed on dunning run
561 my $dunnings = SL::DB::Manager::Dunning->get_all(where => [dunning_id => $dunning_id, original_invoice_printed => 1]);
562 DN->print_original_invoice(\%myconfig, $form, $dunning_id, $_->trans_id) for @$dunnings;
566 $form->{language_id} = $saved_language_id;
568 if (scalar @{ $form->{DUNNING_PDFS} }) {
569 $form->{dunning_id} = strftime("%Y%m%d", localtime time) if scalar @{ $form->{DUNNING_PDFS}} > 1;
570 DN->melt_pdfs(\%myconfig, $form, $form->{copies});
572 if ($form->{media} eq 'printer') {
574 $form->info($locale->text('The dunnings have been printed.'));
578 $form->redirect($locale->text('Could not print dunning.'));
581 $main::lxdebug->leave_sub();
585 call_sub($main::form->{nextsub});
589 foreach my $action (qw(delete print_multiple)) {
590 if ($::form->{"action_${action}"}) {
596 $::form->error($::locale->text('No action defined.'));
599 sub setup_dn_add_action_bar {
602 for my $bar ($::request->layout->get('actionbar')) {
606 submit => [ '#form', { action => "show_invoices" } ],
607 accesskey => 'enter',
613 sub setup_dn_show_invoices_action_bar {
616 for my $bar ($::request->layout->get('actionbar')) {
620 submit => [ '#form', { action => "save_dunning" } ],
621 checks => [ [ 'kivi.check_if_entries_selected', '[name^=active_]' ] ],
622 accesskey => 'enter',
629 sub setup_dn_search_action_bar {
632 for my $bar ($::request->layout->get('actionbar')) {
636 submit => [ '#form', { action => "show_dunning" } ],
637 accesskey => 'enter',
643 sub setup_dn_show_dunning_action_bar {
646 for my $bar ($::request->layout->get('actionbar')) {
650 submit => [ '#form', { action => "print_multiple" } ],
651 checks => [ [ 'kivi.check_if_entries_selected', '[name^=selected_]' ] ],
652 accesskey => 'enter',
657 submit => [ '#form', { action => "delete" } ],
658 checks => [ [ 'kivi.check_if_entries_selected', '[name^=selected_]' ] ],
659 confirm => $::locale->text('This resets the dunning process for the selected invoices. Posted dunning invoices will not be changed!'),
665 sub setup_dn_edit_config_action_bar {
668 for my $bar ($::request->layout->get('actionbar')) {
672 submit => [ '#form', { action => "save" } ],
673 accesskey => 'enter',