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);
39 use SL::DB::Department;
42 use SL::Helper::Flash qw(flash);
43 use SL::Locale::String qw(t8);
44 use SL::Presenter::FileObject;
45 use SL::ReportGenerator;
47 require "bin/mozilla/common.pl";
48 require "bin/mozilla/reportgenerator.pl";
49 require "bin/mozilla/io.pl";
56 $main::lxdebug->enter_sub();
58 my $form = $main::form;
59 my %myconfig = %main::myconfig;
60 my $locale = $main::locale;
62 $main::auth->assert('config');
64 DN->get_config(\%myconfig, \%$form);
65 $form->get_lists('charts' => { 'key' => 'ALL_CHARTS',
66 'transdate' => 'current_date' });
68 $form->{SELECT_AR_AMOUNT} = [];
69 $form->{SELECT_AR} = [];
71 foreach my $chart (@{ $form->{ALL_CHARTS} }) {
72 $chart->{LINKS} = { map { $_, 1 } split m/:/, $chart->{link} };
74 if ($chart->{LINKS}->{AR}) {
75 $chart->{AR_selected} = "selected" if $chart->{id} == $form->{AR};
76 push @{ $form->{SELECT_AR} }, $chart;
79 if ($chart->{LINKS}->{AR_amount}) {
80 $chart->{AR_amount_fee_selected} = "selected" if $chart->{id} == $form->{AR_amount_fee};
81 $chart->{AR_amount_interest_selected} = "selected" if $chart->{id} == $form->{AR_amount_interest};
82 push @{ $form->{SELECT_AR_AMOUNT} }, $chart;
86 $form->{title} = $locale->text('Edit Dunning Process Config');
87 $form->{callback} ||= build_std_url("action=edit_config");
89 setup_dn_edit_config_action_bar();
92 print $form->parse_html_template("dunning/edit_config");
94 $main::lxdebug->leave_sub();
98 $main::lxdebug->enter_sub();
100 my $form = $main::form;
101 my %myconfig = %main::myconfig;
102 my $locale = $main::locale;
104 $main::auth->assert('dunning_edit');
106 DN->get_config(\%myconfig, \%$form);
108 $form->get_lists("departments" => "ALL_DEPARTMENTS");
110 $form->{SHOW_DUNNING_LEVEL_SELECTION} = $form->{DUNNING} && scalar @{ $form->{DUNNING} };
111 $form->{SHOW_DEPARTMENT_SELECTION} = $form->{ALL_DEPARTMENTS} && scalar @{ $form->{ALL_DEPARTMENTS} || [] };
113 $form->{title} = $locale->text('Start Dunning Process');
115 setup_dn_add_action_bar();
118 print $form->parse_html_template("dunning/add");
120 $main::lxdebug->leave_sub();
124 $main::lxdebug->enter_sub();
126 my $form = $main::form;
127 my %myconfig = %main::myconfig;
128 my $locale = $main::locale;
130 $main::auth->assert('dunning_edit');
132 DN->get_invoices(\%myconfig, \%$form);
133 $form->{title} = $locale->text('Start Dunning Process');
135 foreach my $row (@{ $form->{DUNNINGS} }) {
136 $row->{DUNNING_CONFIG} = [ map +{ %{ $_ } }, @{ $form->{DUNNING_CONFIG} } ];
138 if ($row->{next_dunning_config_id}) {
139 map { $_->{SELECTED} = $_->{id} == $row->{next_dunning_config_id} } @{ $row->{DUNNING_CONFIG } };
141 map { $row->{$_} = $form->format_amount(\%myconfig, $row->{$_} * 1, 2) } qw(amount open_amount fee interest);
143 if ($row->{'language_id'}) {
144 $row->{language} = SL::DB::Manager::Language->find_by_or_create('id' => $row->{'language_id'})->{'description'};
148 $form->get_lists('printers' => 'printers',
149 'languages' => 'languages');
151 $form->{type} = 'dunning';
152 $form->{rowcount} = scalar @{ $form->{DUNNINGS} };
153 $form->{callback} ||= build_std_url("action=show_invoices", qw(customer invnumber ordnumber groupinvoices minamount dunning_level notes));
155 $form->{PRINT_OPTIONS} = print_options('inline' => 1,
157 'no_postscript' => 1,
159 'no_opendocument' => 1,);
161 setup_dn_show_invoices_action_bar();
163 print $form->parse_html_template("dunning/show_invoices");
165 $main::lxdebug->leave_sub();
169 $main::lxdebug->enter_sub();
171 my $form = $main::form;
172 my %myconfig = %main::myconfig;
173 my $locale = $main::locale;
175 $main::auth->assert('config');
177 for my $i (1 .. $form->{rowcount}) {
178 if ($form->{"dunning_description_$i"} ne "") {
179 $form->isblank("dunning_level_$i", $locale->text('Dunning Level missing in row '). $i);
180 $form->isblank("dunning_description_$i", $locale->text('Dunning Description missing in row '). $i);
181 $form->isblank("terms_$i", $locale->text('Terms missing in row '). $i);
182 $form->isblank("payment_terms_$i", $locale->text('Payment Terms missing in row '). $i);
186 DN->save_config(\%myconfig, \%$form);
188 if(!exists $form->{addition} && $form->{id} ne "") {
189 $form->{snumbers} = qq|dunning_id_| . $form->{"dunning_id"};
190 $form->{addition} = "SAVED FOR DUNNING";
193 # /saving the history
194 $form->redirect($locale->text('Dunning Process Config saved!'));
196 $main::lxdebug->leave_sub();
200 $main::lxdebug->enter_sub();
202 my $form = $main::form;
203 my %myconfig = %main::myconfig;
204 my $locale = $main::locale;
206 $main::auth->assert('dunning_edit');
210 undef($form->{DUNNING_PDFS});
212 my $saved_language_id = $form->{language_id};
214 if ($form->{groupinvoices} || $form->{l_include_credit_notes}) {
217 for my $i (1 .. $form->{rowcount}) {
218 next unless ($form->{"active_$i"});
220 $dunnings_for{$form->{"customer_id_$i"}} ||= {};
221 my $dunning_levels = $dunnings_for{$form->{"customer_id_$i"}};
223 $dunning_levels->{$form->{"next_dunning_config_id_$i"}} ||= [];
224 my $level = $dunning_levels->{$form->{"next_dunning_config_id_$i"}};
226 push @{ $level }, { "row" => $i,
227 "invoice_id" => $form->{"inv_id_$i"},
228 "credit_note" => $form->{"credit_note_$i"},
229 "customer_id" => $form->{"customer_id_$i"},
230 "language_id" => $form->{"language_id_$i"},
231 "next_dunning_config_id" => $form->{"next_dunning_config_id_$i"},
232 "print_invoice" => $form->{"include_invoice_$i"},
233 "email" => $form->{"email_$i"}, };
236 foreach my $levels (values %dunnings_for) {
237 foreach my $level (values %{ $levels }) {
238 next unless scalar @{ $level };
239 if (!$form->{force_lang}) {
240 $form->{language_id} = @{$level}[0]->{language_id};
242 DN->save_dunning(\%myconfig, $form, $level);
247 for my $i (1 .. $form->{rowcount}) {
248 next unless $form->{"active_$i"};
250 my $level = [ { "row" => $i,
251 "invoice_id" => $form->{"inv_id_$i"},
252 "customer_id" => $form->{"customer_id_$i"},
253 "language_id" => $form->{"language_id_$i"},
254 "next_dunning_config_id" => $form->{"next_dunning_config_id_$i"},
255 "print_invoice" => $form->{"include_invoice_$i"},
256 "email" => $form->{"email_$i"}, } ];
257 if (!$form->{force_lang}) {
258 $form->{language_id} = @{$level}[0]->{language_id};
260 DN->save_dunning(\%myconfig, $form, $level);
264 $form->{language_id} = $saved_language_id;
266 if (scalar @{ $form->{DUNNING_PDFS} }) {
267 $form->{dunning_id} = strftime("%Y%m%d", localtime time) if scalar @{ $form->{DUNNING_PDFS}} > 1;
268 DN->melt_pdfs(\%myconfig, $form, $form->{copies});
272 if(!exists $form->{addition} && $form->{id} ne "") {
273 $form->{snumbers} = qq|dunning_id_| . $form->{"dunning_id"};
274 $form->{addition} = "DUNNING STARTED";
277 # /saving the history
279 if ($form->{media} eq 'printer') {
280 delete $form->{callback};
281 $form->redirect($locale->text('Dunning Process started for selected invoices!'));
284 $main::lxdebug->leave_sub();
288 $main::lxdebug->enter_sub();
290 my $form = $main::form;
291 my $locale = $main::locale;
293 $main::auth->assert('dunning_edit');
295 $form->{"title"} = $locale->text("Set eMail text");
296 $form->header(no_layout => 1);
297 print($form->parse_html_template("dunning/set_email"));
299 $main::lxdebug->leave_sub();
303 $main::lxdebug->enter_sub();
305 my $form = $main::form;
306 my %myconfig = %main::myconfig;
307 my $locale = $main::locale;
309 $main::auth->assert('dunning_edit');
311 $form->get_lists("customers" => "ALL_CUSTOMERS",
312 "departments" => "ALL_DEPARTMENTS");
313 $form->{ALL_EMPLOYEES} = SL::DB::Manager::Employee->get_all_sorted(query => [ deleted => 0 ]);
315 DN->get_config(\%myconfig, \%$form);
317 $form->{SHOW_DUNNING_LEVELS} = scalar @{ $form->{DUNNING} };
319 $form->{title} = $locale->text('Dunnings');
321 setup_dn_search_action_bar();
324 print $form->parse_html_template("dunning/search");
326 $main::lxdebug->leave_sub();
331 $main::lxdebug->enter_sub();
333 my $form = $main::form;
334 my %myconfig = %main::myconfig;
335 my $locale = $main::locale;
336 my $cgi = $::request->{cgi};
338 $main::auth->assert('dunning_edit');
340 my @filter_field_list = qw(customer_id customer dunning_level department_id invnumber ordnumber
341 transdatefrom transdateto dunningfrom dunningto notes showold l_salesman salesman_id);
343 report_generator_set_default_sort('customername', 1);
345 DN->get_dunning(\%myconfig, \%$form);
347 if (!$form->{callback}) {
348 $form->{callback} = build_std_url("action=show_dunning", @filter_field_list);
351 $form->get_lists('printers' => 'printers',
352 'languages' => 'languages');
354 $form->{type} = 'dunning';
355 $form->{PRINT_OPTIONS} = print_options('inline' => 1,
357 'no_postscript' => 1,
359 'no_opendocument' => 1,);
360 $form->{title} = $locale->text('Dunning overview');
362 my $report = SL::ReportGenerator->new(\%myconfig, $form);
364 $report->set_options('std_column_visibility' => 1,
365 'title' => $form->{title});
366 $report->set_export_options('show_dunning', @filter_field_list, qw(sort sortdir));
369 'checkbox' => { 'text' => '', 'visible' => 'HTML' },
370 'dunning_description' => { 'text' => $locale->text('Dunning Level') },
371 'customername' => { 'text' => $locale->text('Customername') },
372 'departmentname' => { 'text' => $locale->text('Department') },
373 'language' => { 'text' => $locale->text('Language') },
374 'invnumber' => { 'text' => $locale->text('Invnumber') },
375 'transdate' => { 'text' => $locale->text('Invdate') },
376 'duedate' => { 'text' => $locale->text('Invoice Duedate') },
377 'amount' => { 'text' => $locale->text('Amount') },
378 'dunning_date' => { 'text' => $locale->text('Dunning Date') },
379 'dunning_duedate' => { 'text' => $locale->text('Dunning Duedate') },
380 'fee' => { 'text' => $locale->text('Total Fees') },
381 'interest' => { 'text' => $locale->text('Interest') },
382 'salesman' => { 'text' => $locale->text('Salesperson'), 'visible' => $form->{l_salesman} ? 1 : 0 },
383 'documents' => { 'text' => $locale->text('Documents') , 'visible' => $::instance_conf->get_doc_storage ? 1 : 0 },
386 $report->set_columns(%column_defs);
387 $report->set_column_order(qw(checkbox dunning_description customername language invnumber transdate
388 duedate amount dunning_date dunning_duedate fee interest salesman));
389 $report->set_sort_indicator($form->{sort}, $form->{sortdir});
391 my $edit_url = sub { build_std_url('script=' . ($_[0]->{invoice} ? 'is' : 'ar') . '.pl', 'action=edit', 'callback') . '&id=' . $::form->escape($_[0]->{id}) };
392 my $print_url = sub { build_std_url('action=print_dunning', 'format=pdf', 'media=screen', 'dunning_id='.$_[0]->{dunning_id}, 'language_id=' . $_[0]->{language_id}) };
393 my $sort_url = build_std_url('action=show_dunning', grep { $form->{$_} } @filter_field_list);
395 foreach my $name (qw(dunning_description customername invnumber transdate duedate dunning_date dunning_duedate salesman)) {
396 my $sortdir = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
397 $column_defs{$name}->{link} = $sort_url . "&sort=$name&sortdir=$sortdir";
400 my %alignment = map { $_ => 'right' } qw(transdate duedate amount dunning_date dunning_duedate fee interest salesman);
402 my ($current_dunning_rows, $previous_dunning_id, $first_row_for_dunning);
404 $current_dunning_rows = [];
405 $first_row_for_dunning = 1;
406 $form->{rowcount} = scalar @{ $form->{DUNNINGS} };
410 foreach my $ref (@{ $form->{DUNNINGS} }) {
413 if ($previous_dunning_id != $ref->{dunning_id}) {
414 $report->add_data($current_dunning_rows) if (scalar @{ $current_dunning_rows });
415 $current_dunning_rows = [];
416 $first_row_for_dunning = 1;
419 if ($ref->{'language_id'}) {
420 $ref->{language} = SL::DB::Manager::Language->find_by('id' => $ref->{'language_id'})->{'description'};
424 foreach my $column (keys %{ $ref }) {
426 'data' => $first_row_for_dunning || (($column ne 'dunning_description') && ($column ne 'customername')) ? $ref->{$column} : '',
428 'align' => $alignment{$column},
430 'link' => ( $column eq 'invnumber' ? $edit_url->($ref)
431 : $column eq 'dunning_description' ? $print_url->($ref)
436 $row->{checkbox} = !$first_row_for_dunning ? { } : {
437 'raw_data' => $cgi->hidden('-name' => "dunning_id_$i", '-value' => $ref->{dunning_id})
438 . $cgi->checkbox('-name' => "selected_$i", '-value' => 1, '-label' => ''),
439 'valign' => 'center',
443 if ($first_row_for_dunning) {
444 $row->{language} = {'raw_data' => $cgi->hidden('-name' => "language_id_$i", '-value' => $ref->{language_id})
445 . " $ref->{language}" };
447 $row->{language} = { };
450 if ($::instance_conf->get_doc_storage) {
451 # object_types dunning1-dunning3 are the dunnings for the according levels.
452 my @files = SL::File->get_all_versions(object_id => $ref->{id},
453 object_type => 'dunning' . $ref->{dunning_level},
454 file_type => 'document',);
455 # object_type dunning is the dunning invoice with the fees.
456 push @files, SL::File->get_all_versions(object_id => $ref->{id},
457 object_type => 'dunning',
458 file_type => 'document',);
460 my $html = join '<br>', map { SL::Presenter::FileObject::file_object($_) } @files;
461 my $text = join "\n", map { $_->file_name } @files;
462 $row->{documents} = { 'raw_data' => $html, data => $text };
464 $row->{documents} = { };
468 push @{ $current_dunning_rows }, $row;
470 $previous_dunning_id = $ref->{dunning_id};
471 $first_row_for_dunning = 0;
474 $report->add_data($current_dunning_rows) if (scalar @{ $current_dunning_rows });
476 $report->set_options('raw_top_info_text' => $form->parse_html_template('dunning/show_dunning_top'),
477 'raw_bottom_info_text' => $form->parse_html_template('dunning/show_dunning_bottom'),
478 'output_format' => 'HTML',
479 'attachment_basename' => $locale->text('dunning_list') . strftime('_%Y%m%d', localtime time),
482 $report->set_options_from_form();
484 setup_dn_show_dunning_action_bar();
485 $report->generate_with_headers();
487 $main::lxdebug->leave_sub();
492 $main::lxdebug->enter_sub();
494 my $form = $main::form;
496 $main::auth->assert('dunning_edit');
498 $form->{rowcount} = 1;
499 $form->{selected_1} = 1;
500 $form->{dunning_id_1} = $form->{dunning_id};
501 $form->{language_id_1} = $form->{language_id};
505 $main::lxdebug->leave_sub();
509 $main::auth->assert('dunning_edit');
511 my @dunning_ids = map { $::form->{"dunning_id_$_"} } grep { $::form->{"selected_$_"} } (1..$::form->{rowcount});
513 if (!scalar @dunning_ids) {
514 $::form->error($::locale->text('No dunnings have been selected for printing.'));
517 my $dunnings = SL::DB::Manager::Dunning->get_all(query => [ dunning_id => \@dunning_ids ]);
519 SL::DB::Dunning->new->db->with_transaction(sub {
520 for my $dunning (@$dunnings) {
521 SL::DB::Manager::Invoice->find_by(id => $dunning->trans_id)->update_attributes(dunning_config_id => undef);
526 flash('info', t8('#1 dunnings have been deleted', scalar @$dunnings));
532 $main::lxdebug->enter_sub();
534 my $form = $main::form;
535 my %myconfig = %main::myconfig;
536 my $locale = $main::locale;
538 $main::auth->assert('dunning_edit');
540 $form->{title} = $locale->text('Print dunnings');
542 my @dunning_ids = map { $form->{"dunning_id_$_"} } grep { $form->{"selected_$_"} } (1..$form->{rowcount});
543 my @language_ids = map { $form->{"language_id_$_"} } grep { $form->{"selected_$_"} } (1..$form->{rowcount});
545 if (!scalar @dunning_ids) {
546 $form->error($locale->text('No dunnings have been selected for printing.'));
549 $form->{DUNNING_PDFS} = [];
551 my $saved_language_id = $form->{language_id};
553 foreach my $dunning_id (@dunning_ids) {
554 if (!$form->{force_lang}) {
555 $form->{language_id} = $language_ids[$i];
557 $form->{dunning_id} = $dunning_id;
558 DN->print_invoice_for_fees(\%myconfig, $form, $dunning_id);
559 DN->print_dunning(\%myconfig, $form, $dunning_id);
562 $form->{language_id} = $saved_language_id;
564 if (scalar @{ $form->{DUNNING_PDFS} }) {
565 $form->{dunning_id} = strftime("%Y%m%d", localtime time) if scalar @{ $form->{DUNNING_PDFS}} > 1;
566 DN->melt_pdfs(\%myconfig, $form, $form->{copies});
568 if ($form->{media} eq 'printer') {
570 $form->info($locale->text('The dunnings have been printed.'));
574 $form->redirect($locale->text('Could not print dunning.'));
577 $main::lxdebug->leave_sub();
581 call_sub($main::form->{nextsub});
585 foreach my $action (qw(delete print_multiple)) {
586 if ($::form->{"action_${action}"}) {
592 $::form->error($::locale->text('No action defined.'));
595 sub setup_dn_add_action_bar {
598 for my $bar ($::request->layout->get('actionbar')) {
602 submit => [ '#form', { action => "show_invoices" } ],
603 accesskey => 'enter',
609 sub setup_dn_show_invoices_action_bar {
612 for my $bar ($::request->layout->get('actionbar')) {
616 submit => [ '#form', { action => "save_dunning" } ],
617 checks => [ [ 'kivi.check_if_entries_selected', '[name^=active_]' ] ],
618 accesskey => 'enter',
625 sub setup_dn_search_action_bar {
628 for my $bar ($::request->layout->get('actionbar')) {
632 submit => [ '#form', { action => "show_dunning" } ],
633 accesskey => 'enter',
639 sub setup_dn_show_dunning_action_bar {
642 for my $bar ($::request->layout->get('actionbar')) {
646 submit => [ '#form', { action => "print_multiple" } ],
647 checks => [ [ 'kivi.check_if_entries_selected', '[name^=selected_]' ] ],
648 accesskey => 'enter',
653 submit => [ '#form', { action => "delete" } ],
654 checks => [ [ 'kivi.check_if_entries_selected', '[name^=selected_]' ] ],
655 confirm => $::locale->text('This resets the dunning process for the selected invoices. Posted dunning invoices will not be changed!'),
661 sub setup_dn_edit_config_action_bar {
664 for my $bar ($::request->layout->get('actionbar')) {
668 submit => [ '#form', { action => "save" } ],
669 accesskey => 'enter',