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::Presenter::WebdavObject;
48 use SL::ReportGenerator;
50 require "bin/mozilla/common.pl";
51 require "bin/mozilla/reportgenerator.pl";
52 require "bin/mozilla/io.pl";
59 $main::lxdebug->enter_sub();
61 my $form = $main::form;
62 my %myconfig = %main::myconfig;
63 my $locale = $main::locale;
65 $main::auth->assert('config');
67 DN->get_config(\%myconfig, \%$form);
68 $form->get_lists('charts' => { 'key' => 'ALL_CHARTS',
69 'transdate' => 'current_date' });
71 $form->{SELECT_AR_AMOUNT} = [];
72 $form->{SELECT_AR} = [];
74 foreach my $chart (@{ $form->{ALL_CHARTS} }) {
75 $chart->{LINKS} = { map { $_, 1 } split m/:/, $chart->{link} };
77 if ($chart->{LINKS}->{AR}) {
78 $chart->{AR_selected} = "selected" if $chart->{id} == $form->{AR};
79 push @{ $form->{SELECT_AR} }, $chart;
82 if ($chart->{LINKS}->{AR_amount}) {
83 $chart->{AR_amount_fee_selected} = "selected" if $chart->{id} == $form->{AR_amount_fee};
84 $chart->{AR_amount_interest_selected} = "selected" if $chart->{id} == $form->{AR_amount_interest};
85 push @{ $form->{SELECT_AR_AMOUNT} }, $chart;
89 $form->{title} = $locale->text('Edit Dunning Process Config');
90 $form->{callback} ||= build_std_url("action=edit_config");
92 setup_dn_edit_config_action_bar();
95 print $form->parse_html_template("dunning/edit_config");
97 $main::lxdebug->leave_sub();
101 $main::lxdebug->enter_sub();
103 my $form = $main::form;
104 my %myconfig = %main::myconfig;
105 my $locale = $main::locale;
107 $main::auth->assert('dunning_edit');
109 DN->get_config(\%myconfig, \%$form);
111 $form->get_lists("departments" => "ALL_DEPARTMENTS");
113 $form->{SHOW_DUNNING_LEVEL_SELECTION} = $form->{DUNNING} && scalar @{ $form->{DUNNING} };
114 $form->{SHOW_DEPARTMENT_SELECTION} = $form->{ALL_DEPARTMENTS} && scalar @{ $form->{ALL_DEPARTMENTS} || [] };
116 $form->{title} = $locale->text('Start Dunning Process');
118 setup_dn_add_action_bar();
121 print $form->parse_html_template("dunning/add");
123 $main::lxdebug->leave_sub();
127 $main::lxdebug->enter_sub();
129 my $form = $main::form;
130 my %myconfig = %main::myconfig;
131 my $locale = $main::locale;
133 $main::auth->assert('dunning_edit');
135 DN->get_invoices(\%myconfig, \%$form);
136 $form->{title} = $locale->text('Start Dunning Process');
138 foreach my $row (@{ $form->{DUNNINGS} }) {
139 $row->{DUNNING_CONFIG} = [ map +{ %{ $_ } }, @{ $form->{DUNNING_CONFIG} } ];
141 if ($row->{next_dunning_config_id}) {
142 map { $_->{SELECTED} = $_->{id} == $row->{next_dunning_config_id} } @{ $row->{DUNNING_CONFIG } };
144 map { $row->{$_} = $form->format_amount(\%myconfig, $row->{$_} * 1, 2) } qw(amount open_amount fee interest);
146 if ($row->{'language_id'}) {
147 $row->{language} = SL::DB::Manager::Language->find_by_or_create('id' => $row->{'language_id'})->{'description'};
151 $form->get_lists('printers' => 'printers',
152 'languages' => 'languages');
154 $form->{type} = 'dunning';
155 $form->{rowcount} = scalar @{ $form->{DUNNINGS} };
156 $form->{callback} ||= build_std_url("action=show_invoices", qw(customer invnumber ordnumber groupinvoices minamount dunning_level notes));
158 $form->{PRINT_OPTIONS} = print_options('inline' => 1,
160 'no_postscript' => 1,
162 'no_opendocument' => 1,);
164 setup_dn_show_invoices_action_bar();
166 print $form->parse_html_template("dunning/show_invoices");
168 $main::lxdebug->leave_sub();
172 $main::lxdebug->enter_sub();
174 my $form = $main::form;
175 my %myconfig = %main::myconfig;
176 my $locale = $main::locale;
178 $main::auth->assert('config');
180 for my $i (1 .. $form->{rowcount}) {
181 if ($form->{"dunning_description_$i"} ne "") {
182 $form->isblank("dunning_level_$i", $locale->text('Dunning Level missing in row '). $i);
183 $form->isblank("dunning_description_$i", $locale->text('Dunning Description missing in row '). $i);
184 $form->isblank("terms_$i", $locale->text('Terms missing in row '). $i);
185 $form->isblank("payment_terms_$i", $locale->text('Payment Terms missing in row '). $i);
189 DN->save_config(\%myconfig, \%$form);
191 if(!exists $form->{addition} && $form->{id} ne "") {
192 $form->{snumbers} = qq|dunning_id_| . $form->{"dunning_id"};
193 $form->{addition} = "SAVED FOR DUNNING";
196 # /saving the history
197 $form->redirect($locale->text('Dunning Process Config saved!'));
199 $main::lxdebug->leave_sub();
203 $main::lxdebug->enter_sub();
205 my $form = $main::form;
206 my %myconfig = %main::myconfig;
207 my $locale = $main::locale;
209 $main::auth->assert('dunning_edit');
213 undef($form->{DUNNING_PDFS});
215 my $saved_language_id = $form->{language_id};
217 if ($form->{groupinvoices} || $form->{l_include_credit_notes}) {
220 for my $i (1 .. $form->{rowcount}) {
221 next unless ($form->{"active_$i"});
223 $dunnings_for{$form->{"customer_id_$i"}} ||= {};
224 my $dunning_levels = $dunnings_for{$form->{"customer_id_$i"}};
226 $dunning_levels->{$form->{"next_dunning_config_id_$i"}} ||= [];
227 my $level = $dunning_levels->{$form->{"next_dunning_config_id_$i"}};
229 push @{ $level }, { "row" => $i,
230 "invoice_id" => $form->{"inv_id_$i"},
231 "credit_note" => $form->{"credit_note_$i"},
232 "customer_id" => $form->{"customer_id_$i"},
233 "language_id" => $form->{"language_id_$i"},
234 "next_dunning_config_id" => $form->{"next_dunning_config_id_$i"},
235 "print_invoice" => $form->{"include_invoice_$i"},
236 "email" => $form->{"email_$i"}, };
239 foreach my $levels (values %dunnings_for) {
240 foreach my $level (values %{ $levels }) {
241 next unless scalar @{ $level };
242 if (!$form->{force_lang}) {
243 $form->{language_id} = @{$level}[0]->{language_id};
245 DN->save_dunning(\%myconfig, $form, $level);
250 for my $i (1 .. $form->{rowcount}) {
251 next unless $form->{"active_$i"};
253 my $level = [ { "row" => $i,
254 "invoice_id" => $form->{"inv_id_$i"},
255 "customer_id" => $form->{"customer_id_$i"},
256 "language_id" => $form->{"language_id_$i"},
257 "next_dunning_config_id" => $form->{"next_dunning_config_id_$i"},
258 "print_invoice" => $form->{"include_invoice_$i"},
259 "email" => $form->{"email_$i"}, } ];
260 if (!$form->{force_lang}) {
261 $form->{language_id} = @{$level}[0]->{language_id};
263 DN->save_dunning(\%myconfig, $form, $level);
267 $form->{language_id} = $saved_language_id;
269 if (scalar @{ $form->{DUNNING_PDFS} }) {
270 $form->{dunning_id} = strftime("%Y%m%d", localtime time) if scalar @{ $form->{DUNNING_PDFS}} > 1;
271 DN->melt_pdfs(\%myconfig, $form, $form->{copies});
275 if(!exists $form->{addition} && $form->{id} ne "") {
276 $form->{snumbers} = qq|dunning_id_| . $form->{"dunning_id"};
277 $form->{addition} = "DUNNING STARTED";
280 # /saving the history
282 if ($form->{media} eq 'printer') {
283 delete $form->{callback};
284 $form->redirect($locale->text('Dunning Process started for selected invoices!'));
287 $main::lxdebug->leave_sub();
291 $main::lxdebug->enter_sub();
293 my $form = $main::form;
294 my $locale = $main::locale;
296 $main::auth->assert('dunning_edit');
298 $form->{"title"} = $locale->text("Set eMail text");
299 $form->header(no_layout => 1);
300 print($form->parse_html_template("dunning/set_email"));
302 $main::lxdebug->leave_sub();
306 $main::lxdebug->enter_sub();
308 my $form = $main::form;
309 my %myconfig = %main::myconfig;
310 my $locale = $main::locale;
312 $main::auth->assert('dunning_edit');
314 $form->get_lists("customers" => "ALL_CUSTOMERS",
315 "departments" => "ALL_DEPARTMENTS");
316 $form->{ALL_EMPLOYEES} = SL::DB::Manager::Employee->get_all_sorted(query => [ deleted => 0 ]);
318 DN->get_config(\%myconfig, \%$form);
320 $form->{SHOW_DUNNING_LEVELS} = scalar @{ $form->{DUNNING} };
322 $form->{title} = $locale->text('Dunnings');
324 setup_dn_search_action_bar();
327 print $form->parse_html_template("dunning/search");
329 $main::lxdebug->leave_sub();
334 $main::lxdebug->enter_sub();
336 my $form = $main::form;
337 my %myconfig = %main::myconfig;
338 my $locale = $main::locale;
339 my $cgi = $::request->{cgi};
341 $main::auth->assert('dunning_edit');
343 my @filter_field_list = qw(customer_id customer dunning_id dunning_level department_id invnumber ordnumber
344 transdatefrom transdateto dunningfrom dunningto notes showold l_salesman salesman_id);
346 report_generator_set_default_sort('customername', 1);
348 DN->get_dunning(\%myconfig, \%$form);
350 if (!$form->{callback}) {
351 $form->{callback} = build_std_url("action=show_dunning", @filter_field_list);
354 $form->get_lists('printers' => 'printers',
355 'languages' => 'languages');
357 $form->{type} = 'dunning';
358 $form->{PRINT_OPTIONS} = print_options('inline' => 1,
360 'no_postscript' => 1,
362 'no_opendocument' => 1,);
363 $form->{title} = $locale->text('Dunning overview');
365 my $report = SL::ReportGenerator->new(\%myconfig, $form);
367 $report->set_options('std_column_visibility' => 1,
368 'title' => $form->{title});
369 $report->set_export_options('show_dunning', @filter_field_list, qw(sort sortdir));
372 'checkbox' => { 'text' => '', 'visible' => 'HTML' },
373 'dunning_description' => { 'text' => $locale->text('Dunning Level') },
374 'customername' => { 'text' => $locale->text('Customername') },
375 'departmentname' => { 'text' => $locale->text('Department') },
376 'language' => { 'text' => $locale->text('Language') },
377 'invnumber' => { 'text' => $locale->text('Invnumber') },
378 'transdate' => { 'text' => $locale->text('Invdate') },
379 'duedate' => { 'text' => $locale->text('Invoice Duedate') },
380 'amount' => { 'text' => $locale->text('Amount') },
381 'dunning_id' => { 'text' => $locale->text('Dunning number') },
382 'dunning_date' => { 'text' => $locale->text('Dunning Date') },
383 'dunning_duedate' => { 'text' => $locale->text('Dunning Duedate') },
384 'fee' => { 'text' => $locale->text('Total Fees') },
385 'interest' => { 'text' => $locale->text('Interest') },
386 'salesman' => { 'text' => $locale->text('Salesperson'), 'visible' => $form->{l_salesman} ? 1 : 0 },
387 'documents' => { 'text' => $locale->text('Documents'), 'visible' => $::instance_conf->get_doc_storage ? 1 : 0 },
388 'webdav' => { 'text' => $locale->text('WebDAV'), 'visible' => $::instance_conf->get_webdav ? 1 : 0 },
391 $report->set_columns(%column_defs);
392 $report->set_column_order(qw(checkbox dunning_description dunning_id customername language invnumber transdate
393 duedate amount dunning_date dunning_duedate fee interest salesman));
394 $report->set_sort_indicator($form->{sort}, $form->{sortdir});
396 my $edit_url = sub { build_std_url('script=' . ($_[0]->{invoice} ? 'is' : 'ar') . '.pl', 'action=edit', 'callback') . '&id=' . $::form->escape($_[0]->{id}) };
397 my $print_url = sub { build_std_url('action=print_dunning', 'format=pdf', 'media=screen', 'dunning_id='.$_[0]->{dunning_id}, 'language_id=' . $_[0]->{language_id}) };
398 my $sort_url = build_std_url('action=show_dunning', grep { $form->{$_} } @filter_field_list);
400 foreach my $name (qw(dunning_description customername invnumber transdate duedate dunning_date dunning_duedate salesman dunning_id)) {
401 my $sortdir = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
402 $column_defs{$name}->{link} = $sort_url . "&sort=$name&sortdir=$sortdir";
405 my %alignment = map { $_ => 'right' } qw(transdate duedate amount dunning_date dunning_duedate fee interest salesman dunning_id);
407 my ($current_dunning_rows, $previous_dunning_id, $first_row_for_dunning);
409 $current_dunning_rows = [];
410 $first_row_for_dunning = 1;
411 $form->{rowcount} = scalar @{ $form->{DUNNINGS} };
415 foreach my $ref (@{ $form->{DUNNINGS} }) {
418 if ($previous_dunning_id != $ref->{dunning_id}) {
419 $report->add_data($current_dunning_rows) if (scalar @{ $current_dunning_rows });
420 $current_dunning_rows = [];
421 $first_row_for_dunning = 1;
424 if ($ref->{'language_id'}) {
425 $ref->{language} = SL::DB::Manager::Language->find_by('id' => $ref->{'language_id'})->{'description'};
429 foreach my $column (keys %{ $ref }) {
431 'data' => $first_row_for_dunning || (none { $_ eq $column } qw(dunning_description customername dunning_id)) ? $ref->{$column} : '',
433 'align' => $alignment{$column},
435 'link' => ( $column eq 'invnumber' ? $edit_url->($ref)
436 : $column eq 'dunning_description' ? $print_url->($ref)
437 : $column eq 'dunning_id' ? $print_url->($ref)
442 $row->{checkbox} = !$first_row_for_dunning ? { } : {
443 'raw_data' => $cgi->hidden('-name' => "dunning_id_$i", '-value' => $ref->{dunning_id})
444 . $cgi->checkbox('-name' => "selected_$i", '-value' => 1, '-label' => ''),
445 'valign' => 'center',
449 if ($first_row_for_dunning) {
450 $row->{language} = {'raw_data' => $cgi->hidden('-name' => "language_id_$i", '-value' => $ref->{language_id})
451 . " $ref->{language}" };
453 $row->{language} = { };
456 if ($::instance_conf->get_doc_storage && $first_row_for_dunning) {
457 my @files = SL::File->get_all_versions(object_id => $ref->{dunning_id},
458 object_type => 'dunning',
459 file_type => 'document',);
461 my $html = join '<br>', map { SL::Presenter::FileObject::file_object($_) } @files;
462 my $text = join "\n", map { $_->file_name } @files;
463 $row->{documents} = { 'raw_data' => $html, data => $text };
465 $row->{documents} = { };
468 if ($::instance_conf->get_webdav && $first_row_for_dunning) {
469 my $webdav = SL::Webdav->new(
471 number => $ref->{dunning_id},
473 my @all_objects = $webdav->get_all_objects;
474 if (scalar @all_objects) {
475 my $html = join '<br>', map { SL::Presenter::WebdavObject::webdav_object($_) } @all_objects;
476 my $text = join "\n", map { $_->filename } @all_objects;
477 $row->{webdav} = { 'raw_data' => $html, data => $text };
479 $row->{webdav} = { };
483 push @{ $current_dunning_rows }, $row;
485 $previous_dunning_id = $ref->{dunning_id};
486 $first_row_for_dunning = 0;
489 $report->add_data($current_dunning_rows) if (scalar @{ $current_dunning_rows });
491 $report->set_options('raw_top_info_text' => $form->parse_html_template('dunning/show_dunning_top'),
492 'raw_bottom_info_text' => $form->parse_html_template('dunning/show_dunning_bottom'),
493 'output_format' => 'HTML',
494 'attachment_basename' => $locale->text('dunning_list') . strftime('_%Y%m%d', localtime time),
497 $report->set_options_from_form();
499 setup_dn_show_dunning_action_bar();
500 $report->generate_with_headers();
502 $main::lxdebug->leave_sub();
507 $main::lxdebug->enter_sub();
509 my $form = $main::form;
511 $main::auth->assert('dunning_edit');
513 $form->{rowcount} = 1;
514 $form->{selected_1} = 1;
515 $form->{dunning_id_1} = $form->{dunning_id};
516 $form->{language_id_1} = $form->{language_id};
520 $main::lxdebug->leave_sub();
524 $main::auth->assert('dunning_edit');
526 my @dunning_ids = map { $::form->{"dunning_id_$_"} } grep { $::form->{"selected_$_"} } (1..$::form->{rowcount});
528 if (!scalar @dunning_ids) {
529 $::form->error($::locale->text('No dunnings have been selected for printing.'));
532 my $dunnings = SL::DB::Manager::Dunning->get_all(query => [ dunning_id => \@dunning_ids ]);
534 SL::DB::Dunning->new->db->with_transaction(sub {
535 for my $dunning (@$dunnings) {
536 SL::DB::Manager::Invoice->find_by(id => $dunning->trans_id)->update_attributes(dunning_config_id => undef);
541 flash('info', t8('#1 dunnings have been deleted', scalar @$dunnings));
547 $main::lxdebug->enter_sub();
549 my $form = $main::form;
550 my %myconfig = %main::myconfig;
551 my $locale = $main::locale;
553 $main::auth->assert('dunning_edit');
555 $form->{title} = $locale->text('Print dunnings');
557 my @dunning_ids = map { $form->{"dunning_id_$_"} } grep { $form->{"selected_$_"} } (1..$form->{rowcount});
558 my @language_ids = map { $form->{"language_id_$_"} } grep { $form->{"selected_$_"} } (1..$form->{rowcount});
560 if (!scalar @dunning_ids) {
561 $form->error($locale->text('No dunnings have been selected for printing.'));
564 $form->{DUNNING_PDFS} = [];
566 my $saved_language_id = $form->{language_id};
568 foreach my $dunning_id (@dunning_ids) {
569 if (!$form->{force_lang}) {
570 $form->{language_id} = $language_ids[$i];
572 $form->{dunning_id} = $dunning_id;
573 DN->print_invoice_for_fees(\%myconfig, $form, $dunning_id);
574 DN->print_dunning(\%myconfig, $form, $dunning_id);
576 # print original dunned invoices, if they where printed on dunning run
577 my $dunnings = SL::DB::Manager::Dunning->get_all(where => [dunning_id => $dunning_id, original_invoice_printed => 1]);
578 DN->print_original_invoice(\%myconfig, $form, $dunning_id, $_->trans_id) for @$dunnings;
582 $form->{language_id} = $saved_language_id;
584 if (scalar @{ $form->{DUNNING_PDFS} }) {
585 $form->{dunning_id} = strftime("%Y%m%d", localtime time) if scalar @{ $form->{DUNNING_PDFS}} > 1;
586 DN->melt_pdfs(\%myconfig, $form, $form->{copies});
588 if ($form->{media} eq 'printer') {
590 $form->info($locale->text('The dunnings have been printed.'));
594 $form->redirect($locale->text('Could not print dunning.'));
597 $main::lxdebug->leave_sub();
601 call_sub($main::form->{nextsub});
605 foreach my $action (qw(delete print_multiple)) {
606 if ($::form->{"action_${action}"}) {
612 $::form->error($::locale->text('No action defined.'));
615 sub setup_dn_add_action_bar {
618 for my $bar ($::request->layout->get('actionbar')) {
622 submit => [ '#form', { action => "show_invoices" } ],
623 accesskey => 'enter',
629 sub setup_dn_show_invoices_action_bar {
632 for my $bar ($::request->layout->get('actionbar')) {
636 submit => [ '#form', { action => "save_dunning" } ],
637 checks => [ [ 'kivi.check_if_entries_selected', '[name^=active_]' ] ],
638 accesskey => 'enter',
645 sub setup_dn_search_action_bar {
648 for my $bar ($::request->layout->get('actionbar')) {
652 submit => [ '#form', { action => "show_dunning" } ],
653 accesskey => 'enter',
659 sub setup_dn_show_dunning_action_bar {
662 for my $bar ($::request->layout->get('actionbar')) {
666 submit => [ '#form', { action => "print_multiple" } ],
667 checks => [ [ 'kivi.check_if_entries_selected', '[name^=selected_]' ] ],
668 accesskey => 'enter',
673 submit => [ '#form', { action => "delete" } ],
674 checks => [ [ 'kivi.check_if_entries_selected', '[name^=selected_]' ] ],
675 confirm => $::locale->text('This resets the dunning process for the selected invoices. Posted dunning invoices will not be changed!'),
681 sub setup_dn_edit_config_action_bar {
684 for my $bar ($::request->layout->get('actionbar')) {
688 submit => [ '#form', { action => "save" } ],
689 accesskey => 'enter',