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::Util qw(notall);
38 use List::MoreUtils qw(none);
42 use SL::DB::Department;
45 use SL::Helper::Flash qw(flash);
46 use SL::Locale::String qw(t8);
47 use SL::Presenter::EmailJournal;
48 use SL::Presenter::FileObject;
49 use SL::Presenter::WebdavObject;
50 use SL::ReportGenerator;
52 require "bin/mozilla/common.pl";
53 require "bin/mozilla/reportgenerator.pl";
54 require "bin/mozilla/io.pl";
61 $main::lxdebug->enter_sub();
63 my $form = $main::form;
64 my %myconfig = %main::myconfig;
65 my $locale = $main::locale;
67 $main::auth->assert('config');
69 DN->get_config(\%myconfig, \%$form);
70 $form->get_lists('charts' => { 'key' => 'ALL_CHARTS',
71 'transdate' => 'current_date' });
73 $form->{SELECT_AR_AMOUNT} = [];
74 $form->{SELECT_AR} = [];
76 foreach my $chart (@{ $form->{ALL_CHARTS} }) {
77 $chart->{LINKS} = { map { $_, 1 } split m/:/, $chart->{link} };
79 if ($chart->{LINKS}->{AR}) {
80 $chart->{AR_selected} = "selected" if $chart->{id} == $form->{AR};
81 push @{ $form->{SELECT_AR} }, $chart;
84 if ($chart->{LINKS}->{AR_amount}) {
85 $chart->{AR_amount_fee_selected} = "selected" if $chart->{id} == $form->{AR_amount_fee};
86 $chart->{AR_amount_interest_selected} = "selected" if $chart->{id} == $form->{AR_amount_interest};
87 push @{ $form->{SELECT_AR_AMOUNT} }, $chart;
91 $form->{title} = $locale->text('Edit Dunning Process Config');
92 $form->{callback} ||= build_std_url("action=edit_config");
94 setup_dn_edit_config_action_bar();
97 print $form->parse_html_template("dunning/edit_config");
99 $main::lxdebug->leave_sub();
103 $main::lxdebug->enter_sub();
105 my $form = $main::form;
106 my %myconfig = %main::myconfig;
107 my $locale = $main::locale;
109 $main::auth->assert('dunning_edit');
111 DN->get_config(\%myconfig, \%$form);
113 $form->get_lists("departments" => "ALL_DEPARTMENTS");
115 $form->{SHOW_DUNNING_LEVEL_SELECTION} = $form->{DUNNING} && scalar @{ $form->{DUNNING} };
116 $form->{SHOW_DEPARTMENT_SELECTION} = $form->{ALL_DEPARTMENTS} && scalar @{ $form->{ALL_DEPARTMENTS} || [] };
118 $form->{title} = $locale->text('Start Dunning Process');
120 setup_dn_add_action_bar();
123 print $form->parse_html_template("dunning/add");
125 $main::lxdebug->leave_sub();
129 $main::lxdebug->enter_sub();
131 my $form = $main::form;
132 my %myconfig = %main::myconfig;
133 my $locale = $main::locale;
135 $main::auth->assert('dunning_edit');
137 DN->get_invoices(\%myconfig, \%$form);
138 $form->{title} = $locale->text('Start Dunning Process');
140 foreach my $row (@{ $form->{DUNNINGS} }) {
141 $row->{DUNNING_CONFIG} = [ map +{ %{ $_ } }, @{ $form->{DUNNING_CONFIG} } ];
143 if ($row->{next_dunning_config_id}) {
144 map { $_->{SELECTED} = $_->{id} == $row->{next_dunning_config_id} } @{ $row->{DUNNING_CONFIG } };
146 map { $row->{$_} = $form->format_amount(\%myconfig, $row->{$_} * 1, 2) } qw(amount open_amount fee interest);
148 if ($row->{'language_id'}) {
149 $row->{language} = SL::DB::Manager::Language->find_by_or_create('id' => $row->{'language_id'})->{'description'};
153 $form->get_lists('printers' => 'printers',
154 'languages' => 'languages');
156 $form->{type} = 'dunning';
157 $form->{rowcount} = scalar @{ $form->{DUNNINGS} };
158 $form->{callback} ||= build_std_url("action=show_invoices", qw(customer invnumber ordnumber groupinvoices minamount dunning_level notes));
160 $form->{PRINT_OPTIONS} = print_options('inline' => 1,
162 'no_postscript' => 1,
164 'no_opendocument' => 1,);
166 setup_dn_show_invoices_action_bar();
168 print $form->parse_html_template("dunning/show_invoices");
170 $main::lxdebug->leave_sub();
174 $main::lxdebug->enter_sub();
176 my $form = $main::form;
177 my %myconfig = %main::myconfig;
178 my $locale = $main::locale;
180 $main::auth->assert('config');
182 for my $i (1 .. $form->{rowcount}) {
183 if ($form->{"dunning_description_$i"} ne "") {
184 $form->isblank("dunning_level_$i", $locale->text('Dunning Level missing in row '). $i);
185 $form->isblank("dunning_description_$i", $locale->text('Dunning Description missing in row '). $i);
186 $form->isblank("terms_$i", $locale->text('Terms missing in row '). $i);
187 $form->isblank("payment_terms_$i", $locale->text('Payment Terms missing in row '). $i);
191 DN->save_config(\%myconfig, \%$form);
193 if(!exists $form->{addition} && $form->{id} ne "") {
194 $form->{snumbers} = qq|dunning_id_| . $form->{"dunning_id"};
195 $form->{addition} = "SAVED FOR DUNNING";
198 # /saving the history
199 $form->redirect($locale->text('Dunning Process Config saved!'));
201 $main::lxdebug->leave_sub();
205 $main::lxdebug->enter_sub();
207 my $form = $main::form;
208 my %myconfig = %main::myconfig;
209 my $locale = $main::locale;
211 $main::auth->assert('dunning_edit');
216 undef($form->{DUNNING_PDFS});
218 my $saved_language_id = $form->{language_id};
220 if ($form->{groupinvoices} || $form->{l_include_credit_notes}) {
223 for my $i (1 .. $form->{rowcount}) {
224 next unless ($form->{"active_$i"});
226 $dunnings_for{$form->{"customer_id_$i"}} ||= {};
227 my $dunning_levels = $dunnings_for{$form->{"customer_id_$i"}};
229 $dunning_levels->{$form->{"next_dunning_config_id_$i"}} ||= [];
230 my $level = $dunning_levels->{$form->{"next_dunning_config_id_$i"}};
232 push @{ $level }, { "row" => $i,
233 "invoice_id" => $form->{"inv_id_$i"},
234 "credit_note" => $form->{"credit_note_$i"},
235 "customer_id" => $form->{"customer_id_$i"},
236 "language_id" => $form->{"language_id_$i"},
237 "next_dunning_config_id" => $form->{"next_dunning_config_id_$i"},
238 "print_invoice" => $form->{"include_invoice_$i"},
239 "email" => $form->{"email_$i"}, };
242 foreach my $levels (values %dunnings_for) {
243 foreach my $level (values %{ $levels }) {
244 next unless scalar @{ $level };
245 if (!$form->{force_lang}) {
246 $form->{language_id} = @{$level}[0]->{language_id};
248 my $rc = DN->save_dunning(\%myconfig, $form, $level);
249 $rc->{error} =~ s{\n}{<br />}g if $rc->{error};
250 push @status, { invnumbers => [map { $form->{'invnumber_' . $_->{row}} } @$level],
251 map { ( $_ => $rc->{$_} ) } qw(error dunning_id print_original_invoice send_email), };
256 for my $i (1 .. $form->{rowcount}) {
257 next unless $form->{"active_$i"};
259 my $level = [ { "row" => $i,
260 "invoice_id" => $form->{"inv_id_$i"},
261 "customer_id" => $form->{"customer_id_$i"},
262 "language_id" => $form->{"language_id_$i"},
263 "next_dunning_config_id" => $form->{"next_dunning_config_id_$i"},
264 "print_invoice" => $form->{"include_invoice_$i"},
265 "email" => $form->{"email_$i"}, } ];
266 if (!$form->{force_lang}) {
267 $form->{language_id} = @{$level}[0]->{language_id};
269 my $rc = DN->save_dunning(\%myconfig, $form, $level);
270 $rc->{error} =~ s{\n}{<br />}g if $rc->{error};
271 push @status, { invnumbers => [map { $form->{'invnumber_' . $_->{row}} } @$level],
272 map { ( $_ => $rc->{$_} ) } qw(error dunning_id print_original_invoice send_email), };
276 $form->{language_id} = $saved_language_id;
280 if ($form->{DUNNING_PDFS} && scalar @{ $form->{DUNNING_PDFS} }) {
281 $form->{dunning_id} = strftime("%Y%m%d", localtime time) if scalar @{ $form->{DUNNING_PDFS}} > 1;
282 ($pdf_filename, $pdf_content) = DN->melt_pdfs(\%myconfig, $form, $form->{copies}, return_content => $form->{media} ne 'printer');
284 flash('info', t8('Dunning Process started for selected invoices!'));
285 if ($form->{media} eq 'printer') {
286 flash('info', t8('The PDF has been printed'));
288 flash('info', t8('The PDF has been created'));
293 if(!exists $form->{addition} && $form->{id} ne "") {
294 $form->{snumbers} = qq|dunning_id_| . $form->{"dunning_id"};
295 $form->{addition} = "DUNNING STARTED";
298 # /saving the history
300 setup_dn_status_action_bar();
301 $form->{"title"} = $locale->text("Dunning status");
303 print $form->parse_html_template('dunning/status', {
304 pdf_filename => $pdf_filename,
305 pdf_content => $pdf_content,
306 status => \@status, });
308 $main::lxdebug->leave_sub();
312 $main::lxdebug->enter_sub();
314 my $form = $main::form;
315 my $locale = $main::locale;
317 $main::auth->assert('dunning_edit');
319 $form->{"title"} = $locale->text("Set eMail text");
322 use_javascripts => [ qw(ckeditor/ckeditor ckeditor/adapters/jquery) ],
324 print($form->parse_html_template("dunning/set_email"));
326 $main::lxdebug->leave_sub();
330 $main::lxdebug->enter_sub();
332 my $form = $main::form;
333 my %myconfig = %main::myconfig;
334 my $locale = $main::locale;
336 $main::auth->assert('dunning_edit');
338 $form->get_lists("customers" => "ALL_CUSTOMERS",
339 "departments" => "ALL_DEPARTMENTS");
340 $form->{ALL_EMPLOYEES} = SL::DB::Manager::Employee->get_all_sorted(query => [ deleted => 0 ]);
342 DN->get_config(\%myconfig, \%$form);
344 $form->{SHOW_DUNNING_LEVELS} = scalar @{ $form->{DUNNING} };
346 $form->{title} = $locale->text('Dunnings');
348 setup_dn_search_action_bar();
351 print $form->parse_html_template("dunning/search");
353 $main::lxdebug->leave_sub();
358 $main::lxdebug->enter_sub();
360 my $form = $main::form;
361 my %myconfig = %main::myconfig;
362 my $locale = $main::locale;
363 my $cgi = $::request->{cgi};
365 $main::auth->assert('dunning_edit');
367 my @filter_field_list = qw(customer_id customer dunning_id dunning_level department_id invnumber ordnumber
368 transdatefrom transdateto dunningfrom dunningto notes showold l_salesman salesman_id
369 l_mails l_webdav l_documents);
371 report_generator_set_default_sort('customername', 1);
373 DN->get_dunning(\%myconfig, \%$form);
375 if (!$form->{callback}) {
376 $form->{callback} = build_std_url("action=show_dunning", @filter_field_list);
379 $form->get_lists('printers' => 'printers',
380 'languages' => 'languages');
382 $form->{type} = 'dunning';
383 $form->{PRINT_OPTIONS} = print_options('inline' => 1,
385 'no_postscript' => 1,
387 'no_opendocument' => 1,);
388 $form->{title} = $locale->text('Dunning overview');
390 my $report = SL::ReportGenerator->new(\%myconfig, $form);
392 $report->set_options('std_column_visibility' => 1,
393 'title' => $form->{title});
394 $report->set_export_options('show_dunning', @filter_field_list, qw(sort sortdir));
397 'checkbox' => { 'text' => '', 'visible' => 'HTML' },
398 'dunning_description' => { 'text' => $locale->text('Dunning Level') },
399 'customername' => { 'text' => $locale->text('Customername') },
400 'departmentname' => { 'text' => $locale->text('Department') },
401 'language' => { 'text' => $locale->text('Language') },
402 'invnumber' => { 'text' => $locale->text('Invnumber') },
403 'transdate' => { 'text' => $locale->text('Invdate') },
404 'duedate' => { 'text' => $locale->text('Invoice Duedate') },
405 'amount' => { 'text' => $locale->text('Amount') },
406 'dunning_id' => { 'text' => $locale->text('Dunning number') },
407 'dunning_date' => { 'text' => $locale->text('Dunning Date') },
408 'dunning_duedate' => { 'text' => $locale->text('Dunning Duedate') },
409 'fee' => { 'text' => $locale->text('Total Fees') },
410 'interest' => { 'text' => $locale->text('Interest') },
411 'salesman' => { 'text' => $locale->text('Salesperson'), 'visible' => $form->{l_salesman} ? 1 : 0 },
412 'documents' => { 'text' => $locale->text('Documents'), 'visible' => $form->{l_documents}? 1 : 0 },
413 'webdav' => { 'text' => $locale->text('WebDAV'), 'visible' => $form->{l_webdav} ? 1 : 0 },
414 'mails' => { 'text' => $locale->text('Mails'), 'visible' => $form->{l_mails} ? 1 : 0 },
417 $report->set_columns(%column_defs);
418 $report->set_column_order(qw(checkbox dunning_description dunning_id customername language invnumber transdate
419 duedate amount dunning_date dunning_duedate fee interest salesman departmentname mails webdav documents));
420 $report->set_sort_indicator($form->{sort}, $form->{sortdir});
422 my $edit_url = sub { build_std_url('script=' . ($_[0]->{invoice} ? 'is' : 'ar') . '.pl', 'action=edit', 'callback') . '&id=' . $::form->escape($_[0]->{id}) };
423 my $print_url = sub { build_std_url('action=print_dunning', 'format=pdf', 'media=screen', 'dunning_id='.$_[0]->{dunning_id}, 'language_id=' . $_[0]->{language_id}) };
424 my $sort_url = build_std_url('action=show_dunning', grep { $form->{$_} } @filter_field_list);
426 foreach my $name (qw(dunning_description customername invnumber transdate duedate dunning_date dunning_duedate salesman dunning_id)) {
427 my $sortdir = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
428 $column_defs{$name}->{link} = $sort_url . "&sort=$name&sortdir=$sortdir";
431 my %alignment = map { $_ => 'right' } qw(transdate duedate amount dunning_date dunning_duedate fee interest salesman dunning_id);
433 my ($current_dunning_rows, $previous_dunning_id, $first_row_for_dunning);
435 $current_dunning_rows = [];
436 $first_row_for_dunning = 1;
437 $form->{rowcount} = scalar @{ $form->{DUNNINGS} };
441 foreach my $ref (@{ $form->{DUNNINGS} }) {
444 if ($previous_dunning_id != $ref->{dunning_id}) {
445 $report->add_data($current_dunning_rows) if (scalar @{ $current_dunning_rows });
446 $current_dunning_rows = [];
447 $first_row_for_dunning = 1;
450 if ($ref->{'language_id'}) {
451 $ref->{language} = SL::DB::Manager::Language->find_by('id' => $ref->{'language_id'})->{'description'};
455 foreach my $column (keys %{ $ref }) {
457 'data' => $first_row_for_dunning || (none { $_ eq $column } qw(dunning_description customername dunning_id)) ? $ref->{$column} : '',
459 'align' => $alignment{$column},
461 'link' => ( $column eq 'invnumber' ? $edit_url->($ref)
462 : $column eq 'dunning_description' ? $print_url->($ref)
463 : $column eq 'dunning_id' ? $print_url->($ref)
468 $row->{checkbox} = !$first_row_for_dunning ? { } : {
469 'raw_data' => $cgi->hidden('-name' => "dunning_id_$i", '-value' => $ref->{dunning_id})
470 . $cgi->checkbox('-name' => "selected_$i", '-value' => 1, '-label' => ''),
471 'valign' => 'center',
475 if ($first_row_for_dunning) {
476 $row->{language} = {'raw_data' => $cgi->hidden('-name' => "language_id_$i", '-value' => $ref->{language_id})
477 . " $ref->{language}" };
479 $row->{language} = { };
482 if ($form->{l_documents} && $first_row_for_dunning) {
483 my @files = SL::File->get_all_versions(object_id => $ref->{dunning_id},
484 object_type => 'dunning',
485 file_type => 'document',);
487 my $html = join '<br>', map { SL::Presenter::FileObject::file_object($_) } @files;
488 my $text = join "\n", map { $_->file_name } @files;
489 $row->{documents} = { 'raw_data' => $html, data => $text };
491 $row->{documents} = { };
494 if ($form->{l_webdav} && $first_row_for_dunning) {
495 my $webdav = SL::Webdav->new(
497 number => $ref->{dunning_id},
499 my @all_objects = $webdav->get_all_objects;
500 if (scalar @all_objects) {
501 my $html = join '<br>', map { SL::Presenter::WebdavObject::webdav_object($_) } @all_objects;
502 my $text = join "\n", map { $_->filename } @all_objects;
503 $row->{webdav} = { 'raw_data' => $html, data => $text };
505 $row->{webdav} = { };
509 if ($form->{l_mails}) {
510 my @mail_links = RecordLinks->get_links(from_table => 'dunning', to_table => 'email_journal', from_id => $ref->{dunning_table_id});
511 if (scalar @mail_links) {
512 my $email_journals = SL::DB::Manager::EmailJournal->get_all(where => [id => [ map { $_->{to_id} } @mail_links ]]);
513 my $html = join '<br>', map { SL::Presenter::EmailJournal::email_journal($_) } @$email_journals;
514 my $text = join "\n", map { $_->subject } @$email_journals;
515 $row->{mails} = { 'raw_data' => $html, data => $text };
521 push @{ $current_dunning_rows }, $row;
523 $previous_dunning_id = $ref->{dunning_id};
524 $first_row_for_dunning = 0;
527 $report->add_data($current_dunning_rows) if (scalar @{ $current_dunning_rows });
529 $report->set_options('raw_top_info_text' => $form->parse_html_template('dunning/show_dunning_top'),
530 'raw_bottom_info_text' => $form->parse_html_template('dunning/show_dunning_bottom'),
531 'output_format' => 'HTML',
532 'attachment_basename' => $locale->text('dunning_list') . strftime('_%Y%m%d', localtime time),
535 $report->set_options_from_form();
537 setup_dn_show_dunning_action_bar();
538 $report->generate_with_headers();
540 $main::lxdebug->leave_sub();
545 $main::lxdebug->enter_sub();
547 my $form = $main::form;
549 $main::auth->assert('dunning_edit');
551 $form->{rowcount} = 1;
552 $form->{selected_1} = 1;
553 $form->{dunning_id_1} = $form->{dunning_id};
554 $form->{language_id_1} = $form->{language_id};
558 $main::lxdebug->leave_sub();
562 $main::auth->assert('dunning_edit');
564 my @dunning_ids = map { $::form->{"dunning_id_$_"} } grep { $::form->{"selected_$_"} } (1..$::form->{rowcount});
566 if (!scalar @dunning_ids) {
567 $::form->error($::locale->text('No dunnings have been selected for printing.'));
570 my $dunnings = SL::DB::Manager::Dunning->get_all(query => [ dunning_id => \@dunning_ids ]);
572 SL::DB::Dunning->new->db->with_transaction(sub {
573 for my $dunning (@$dunnings) {
574 SL::DB::Manager::Invoice->find_by(id => $dunning->trans_id)->update_attributes(dunning_config_id => undef);
579 flash('info', t8('#1 dunnings have been deleted', scalar @$dunnings));
585 $main::lxdebug->enter_sub();
587 my $form = $main::form;
588 my %myconfig = %main::myconfig;
589 my $locale = $main::locale;
591 $main::auth->assert('dunning_edit');
593 $form->{title} = $locale->text('Print dunnings');
595 my @dunning_ids = map { $form->{"dunning_id_$_"} } grep { $form->{"selected_$_"} } (1..$form->{rowcount});
596 my @language_ids = map { $form->{"language_id_$_"} } grep { $form->{"selected_$_"} } (1..$form->{rowcount});
598 if (!scalar @dunning_ids) {
599 $form->error($locale->text('No dunnings have been selected for printing.'));
602 $form->{DUNNING_PDFS} = [];
604 my $saved_language_id = $form->{language_id};
606 foreach my $dunning_id (@dunning_ids) {
607 if (!$form->{force_lang}) {
608 $form->{language_id} = $language_ids[$i];
610 $form->{dunning_id} = $dunning_id;
611 DN->print_invoice_for_fees(\%myconfig, $form, $dunning_id);
612 DN->print_dunning(\%myconfig, $form, $dunning_id);
614 # print original dunned invoices, if they where printed on dunning run
615 my $dunnings = SL::DB::Manager::Dunning->get_all(where => [dunning_id => $dunning_id, original_invoice_printed => 1]);
616 DN->print_original_invoice(\%myconfig, $form, $dunning_id, $_->trans_id) for @$dunnings;
620 $form->{language_id} = $saved_language_id;
622 if (scalar @{ $form->{DUNNING_PDFS} }) {
623 $form->{dunning_id} = strftime("%Y%m%d", localtime time) if scalar @{ $form->{DUNNING_PDFS}} > 1;
624 DN->melt_pdfs(\%myconfig, $form, $form->{copies});
626 if ($form->{media} eq 'printer') {
628 $form->info($locale->text('The dunnings have been printed.'));
632 $form->redirect($locale->text('Could not print dunning.'));
635 $main::lxdebug->leave_sub();
639 call_sub($main::form->{nextsub});
643 foreach my $action (qw(delete print_multiple)) {
644 if ($::form->{"action_${action}"}) {
650 $::form->error($::locale->text('No action defined.'));
653 sub setup_dn_add_action_bar {
656 for my $bar ($::request->layout->get('actionbar')) {
660 submit => [ '#form', { action => "show_invoices" } ],
661 accesskey => 'enter',
667 sub setup_dn_show_invoices_action_bar {
670 for my $bar ($::request->layout->get('actionbar')) {
674 submit => [ '#form', { action => "save_dunning" } ],
675 checks => [ [ 'kivi.check_if_entries_selected', '[name^=active_]' ] ],
676 accesskey => 'enter',
683 sub setup_dn_search_action_bar {
686 for my $bar ($::request->layout->get('actionbar')) {
690 submit => [ '#form', { action => "show_dunning" } ],
691 accesskey => 'enter',
697 sub setup_dn_show_dunning_action_bar {
700 for my $bar ($::request->layout->get('actionbar')) {
704 submit => [ '#form', { action => "print_multiple" } ],
705 checks => [ [ 'kivi.check_if_entries_selected', '[name^=selected_]' ] ],
706 accesskey => 'enter',
711 submit => [ '#form', { action => "delete" } ],
712 checks => [ [ 'kivi.check_if_entries_selected', '[name^=selected_]' ] ],
713 confirm => $::locale->text('This resets the dunning process for the selected invoices. Posted dunning invoices will not be changed!'),
719 sub setup_dn_edit_config_action_bar {
722 for my $bar ($::request->layout->get('actionbar')) {
726 submit => [ '#form', { action => "save" } ],
727 accesskey => 'enter',
733 sub setup_dn_status_action_bar {
734 for my $bar ($::request->layout->get('actionbar')) {
738 link => $::form->{callback},
739 accesskey => 'enter',