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);
40 use SL::Helper::Flash qw(flash);
41 use SL::Locale::String qw(t8);
42 use SL::ReportGenerator;
44 require "bin/mozilla/common.pl";
45 require "bin/mozilla/reportgenerator.pl";
46 require "bin/mozilla/io.pl";
53 $main::lxdebug->enter_sub();
55 my $form = $main::form;
56 my %myconfig = %main::myconfig;
57 my $locale = $main::locale;
59 $main::auth->assert('config');
61 DN->get_config(\%myconfig, \%$form);
62 $form->get_lists('charts' => { 'key' => 'ALL_CHARTS',
63 'transdate' => 'current_date' });
65 $form->{SELECT_AR_AMOUNT} = [];
66 $form->{SELECT_AR} = [];
68 foreach my $chart (@{ $form->{ALL_CHARTS} }) {
69 $chart->{LINKS} = { map { $_, 1 } split m/:/, $chart->{link} };
71 if ($chart->{LINKS}->{AR}) {
72 $chart->{AR_selected} = "selected" if $chart->{id} == $form->{AR};
73 push @{ $form->{SELECT_AR} }, $chart;
76 if ($chart->{LINKS}->{AR_amount}) {
77 $chart->{AR_amount_fee_selected} = "selected" if $chart->{id} == $form->{AR_amount_fee};
78 $chart->{AR_amount_interest_selected} = "selected" if $chart->{id} == $form->{AR_amount_interest};
79 push @{ $form->{SELECT_AR_AMOUNT} }, $chart;
83 $form->{title} = $locale->text('Edit Dunning Process Config');
84 $form->{callback} ||= build_std_url("action=edit_config");
87 print $form->parse_html_template("dunning/edit_config");
89 $main::lxdebug->leave_sub();
93 $main::lxdebug->enter_sub();
95 my $form = $main::form;
96 my %myconfig = %main::myconfig;
97 my $locale = $main::locale;
99 $main::auth->assert('dunning_edit');
101 # setup customer selection
102 $form->all_vc(\%myconfig, "customer", "AR");
104 DN->get_config(\%myconfig, \%$form);
106 $form->{SHOW_CUSTOMER_SELECTION} = $form->{all_customer} && scalar @{ $form->{all_customer} };
107 $form->{SHOW_DUNNING_LEVEL_SELECTION} = $form->{DUNNING} && scalar @{ $form->{DUNNING} };
108 $form->{SHOW_DEPARTMENT_SELECTION} = $form->{all_departments} && scalar @{ $form->{all_departments} || [] };
110 $form->{title} = $locale->text('Start Dunning Process');
113 print $form->parse_html_template("dunning/add");
115 $main::lxdebug->leave_sub();
119 $main::lxdebug->enter_sub();
121 my $form = $main::form;
122 my %myconfig = %main::myconfig;
123 my $locale = $main::locale;
125 $main::auth->assert('dunning_edit');
127 DN->get_invoices(\%myconfig, \%$form);
128 $form->{title} = $locale->text('Start Dunning Process');
130 foreach my $row (@{ $form->{DUNNINGS} }) {
131 $row->{DUNNING_CONFIG} = [ map +{ %{ $_ } }, @{ $form->{DUNNING_CONFIG} } ];
133 if ($row->{next_dunning_config_id}) {
134 map { $_->{SELECTED} = $_->{id} == $row->{next_dunning_config_id} } @{ $row->{DUNNING_CONFIG } };
136 map { $row->{$_} = $form->format_amount(\%myconfig, $row->{$_} * 1, 2) } qw(amount open_amount fee interest);
138 if ($row->{'language_id'}) {
139 $row->{language} = SL::DB::Manager::Language->find_by_or_create('id' => $row->{'language_id'})->{'description'};
143 $form->get_lists('printers' => 'printers',
144 'languages' => 'languages');
146 $form->{type} = 'dunning';
147 $form->{rowcount} = scalar @{ $form->{DUNNINGS} };
148 $form->{callback} ||= build_std_url("action=show_invoices", qw(customer invnumber ordnumber groupinvoices minamount dunning_level notes));
150 $form->{PRINT_OPTIONS} = print_options('inline' => 1,
152 'no_postscript' => 1,
154 'no_opendocument' => 1,);
156 $::request->layout->add_javascripts("kivi.Dunning.js");
158 print $form->parse_html_template("dunning/show_invoices");
160 $main::lxdebug->leave_sub();
164 $main::lxdebug->enter_sub();
166 my $form = $main::form;
167 my %myconfig = %main::myconfig;
168 my $locale = $main::locale;
170 $main::auth->assert('config');
172 for my $i (1 .. $form->{rowcount}) {
173 if ($form->{"dunning_description_$i"} ne "") {
174 $form->isblank("dunning_level_$i", $locale->text('Dunning Level missing in row '). $i);
175 $form->isblank("dunning_description_$i", $locale->text('Dunning Description missing in row '). $i);
176 $form->isblank("terms_$i", $locale->text('Terms missing in row '). $i);
177 $form->isblank("payment_terms_$i", $locale->text('Payment Terms missing in row '). $i);
181 DN->save_config(\%myconfig, \%$form);
183 if(!exists $form->{addition} && $form->{id} ne "") {
184 $form->{snumbers} = qq|dunning_id_| . $form->{"dunning_id"};
185 $form->{addition} = "SAVED FOR DUNNING";
188 # /saving the history
189 $form->redirect($locale->text('Dunning Process Config saved!'));
191 $main::lxdebug->leave_sub();
195 $main::lxdebug->enter_sub();
197 my $form = $main::form;
198 my %myconfig = %main::myconfig;
199 my $locale = $main::locale;
201 $main::auth->assert('dunning_edit');
205 undef($form->{DUNNING_PDFS});
207 my $saved_language_id = $form->{language_id};
209 if ($form->{groupinvoices}) {
212 for my $i (1 .. $form->{rowcount}) {
213 next unless ($form->{"active_$i"});
215 $dunnings_for{$form->{"customer_id_$i"}} ||= {};
216 my $dunning_levels = $dunnings_for{$form->{"customer_id_$i"}};
218 $dunning_levels->{$form->{"next_dunning_config_id_$i"}} ||= [];
219 my $level = $dunning_levels->{$form->{"next_dunning_config_id_$i"}};
221 push @{ $level }, { "row" => $i,
222 "invoice_id" => $form->{"inv_id_$i"},
223 "customer_id" => $form->{"customer_id_$i"},
224 "language_id" => $form->{"language_id_$i"},
225 "next_dunning_config_id" => $form->{"next_dunning_config_id_$i"},
226 "email" => $form->{"email_$i"}, };
229 foreach my $levels (values %dunnings_for) {
230 foreach my $level (values %{ $levels }) {
231 next unless scalar @{ $level };
232 if (!$form->{force_lang}) {
233 $form->{language_id} = @{$level}[0]->{language_id};
235 DN->save_dunning(\%myconfig, $form, $level);
240 for my $i (1 .. $form->{rowcount}) {
241 next unless $form->{"active_$i"};
243 my $level = [ { "row" => $i,
244 "invoice_id" => $form->{"inv_id_$i"},
245 "customer_id" => $form->{"customer_id_$i"},
246 "language_id" => $form->{"language_id_$i"},
247 "next_dunning_config_id" => $form->{"next_dunning_config_id_$i"},
248 "email" => $form->{"email_$i"}, } ];
249 if (!$form->{force_lang}) {
250 $form->{language_id} = @{$level}[0]->{language_id};
252 DN->save_dunning(\%myconfig, $form, $level);
256 $form->{language_id} = $saved_language_id;
258 if (scalar @{ $form->{DUNNING_PDFS} }) {
259 $form->{dunning_id} = strftime("%Y%m%d", localtime time) if scalar @{ $form->{DUNNING_PDFS}} > 1;
260 DN->melt_pdfs(\%myconfig, $form, $form->{copies});
264 if(!exists $form->{addition} && $form->{id} ne "") {
265 $form->{snumbers} = qq|dunning_id_| . $form->{"dunning_id"};
266 $form->{addition} = "DUNNING STARTED";
269 # /saving the history
271 if ($form->{media} eq 'printer') {
272 delete $form->{callback};
273 $form->redirect($locale->text('Dunning Process started for selected invoices!'));
276 $main::lxdebug->leave_sub();
280 $main::lxdebug->enter_sub();
282 my $form = $main::form;
283 my $locale = $main::locale;
285 $main::auth->assert('dunning_edit');
287 $form->{"title"} = $locale->text("Set eMail text");
288 $form->header(no_layout => 1);
289 print($form->parse_html_template("dunning/set_email"));
291 $main::lxdebug->leave_sub();
295 $main::lxdebug->enter_sub();
297 my $form = $main::form;
298 my %myconfig = %main::myconfig;
299 my $locale = $main::locale;
301 $main::auth->assert('dunning_edit');
303 $form->get_lists("customers" => "ALL_CUSTOMERS",
304 "departments" => "ALL_DEPARTMENTS");
305 $form->{ALL_EMPLOYEES} = SL::DB::Manager::Employee->get_all_sorted(query => [ deleted => 0 ]);
307 DN->get_config(\%myconfig, \%$form);
309 $form->{SHOW_CUSTOMER_DDBOX} = scalar @{ $form->{ALL_CUSTOMERS} } <= $myconfig{vclimit};
310 $form->{SHOW_DEPARTMENT_DDBOX} = scalar @{ $form->{ALL_CUSTOMERS} };
311 $form->{SHOW_DUNNING_LEVELS} = scalar @{ $form->{DUNNING} };
313 $form->{title} = $locale->text('Dunnings');
317 print $form->parse_html_template("dunning/search");
319 $main::lxdebug->leave_sub();
324 $main::lxdebug->enter_sub();
326 my $form = $main::form;
327 my %myconfig = %main::myconfig;
328 my $locale = $main::locale;
329 my $cgi = $::request->{cgi};
331 $main::auth->assert('dunning_edit');
333 my @filter_field_list = qw(customer_id customer dunning_level department_id invnumber ordnumber
334 transdatefrom transdateto dunningfrom dunningto notes showold l_salesman salesman_id);
336 report_generator_set_default_sort('customername', 1);
338 DN->get_dunning(\%myconfig, \%$form);
340 if (!$form->{callback}) {
341 $form->{callback} = build_std_url("action=show_dunning", @filter_field_list);
344 $form->get_lists('printers' => 'printers',
345 'languages' => 'languages');
347 $form->{type} = 'dunning';
348 $form->{PRINT_OPTIONS} = print_options('inline' => 1,
350 'no_postscript' => 1,
352 'no_opendocument' => 1,);
353 $form->{title} = $locale->text('Dunning overview');
355 my $report = SL::ReportGenerator->new(\%myconfig, $form);
357 $report->set_options('std_column_visibility' => 1,
358 'title' => $form->{title});
359 $report->set_export_options('show_dunning', @filter_field_list, qw(sort sortdir));
362 'checkbox' => { 'text' => '', 'visible' => 'HTML' },
363 'dunning_description' => { 'text' => $locale->text('Dunning Level') },
364 'customername' => { 'text' => $locale->text('Customername') },
365 'language' => { 'text' => $locale->text('Language') },
366 'invnumber' => { 'text' => $locale->text('Invnumber') },
367 'transdate' => { 'text' => $locale->text('Invdate') },
368 'duedate' => { 'text' => $locale->text('Invoice Duedate') },
369 'amount' => { 'text' => $locale->text('Amount') },
370 'dunning_date' => { 'text' => $locale->text('Dunning Date') },
371 'dunning_duedate' => { 'text' => $locale->text('Dunning Duedate') },
372 'fee' => { 'text' => $locale->text('Total Fees') },
373 'interest' => { 'text' => $locale->text('Interest') },
374 'salesman' => { 'text' => $locale->text('Salesperson'), 'visible' => $form->{l_salesman} ? 1 : 0 },
377 $report->set_columns(%column_defs);
378 $report->set_column_order(qw(checkbox dunning_description customername language invnumber transdate
379 duedate amount dunning_date dunning_duedate fee interest salesman));
380 $report->set_sort_indicator($form->{sort}, $form->{sortdir});
382 my $edit_url = sub { build_std_url('script=' . ($_[0]->{invoice} ? 'is' : 'ar') . '.pl', 'action=edit', 'callback') . '&id=' . $::form->escape($_[0]->{id}) };
383 my $print_url = sub { build_std_url('action=print_dunning', 'format=pdf', 'media=screen', 'dunning_id='.$_[0]->{dunning_id}, 'language_id=' . $_[0]->{language_id}) };
384 my $sort_url = build_std_url('action=show_dunning', grep { $form->{$_} } @filter_field_list);
386 foreach my $name (qw(dunning_description customername invnumber transdate duedate dunning_date dunning_duedate salesman)) {
387 my $sortdir = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
388 $column_defs{$name}->{link} = $sort_url . "&sort=$name&sortdir=$sortdir";
391 my %alignment = map { $_ => 'right' } qw(transdate duedate amount dunning_date dunning_duedate fee interest salesman);
393 my ($current_dunning_rows, $previous_dunning_id, $first_row_for_dunning);
395 $current_dunning_rows = [];
396 $first_row_for_dunning = 1;
397 $form->{rowcount} = scalar @{ $form->{DUNNINGS} };
401 foreach my $ref (@{ $form->{DUNNINGS} }) {
404 if ($previous_dunning_id != $ref->{dunning_id}) {
405 $report->add_data($current_dunning_rows) if (scalar @{ $current_dunning_rows });
406 $current_dunning_rows = [];
407 $first_row_for_dunning = 1;
410 if ($ref->{'language_id'}) {
411 $ref->{language} = SL::DB::Manager::Language->find_by('id' => $ref->{'language_id'})->{'description'};
415 foreach my $column (keys %{ $ref }) {
417 'data' => $first_row_for_dunning || (($column ne 'dunning_description') && ($column ne 'customername')) ? $ref->{$column} : '',
419 'align' => $alignment{$column},
421 'link' => ( $column eq 'invnumber' ? $edit_url->($ref)
422 : $column eq 'dunning_description' ? $print_url->($ref)
427 $row->{checkbox} = !$first_row_for_dunning ? { } : {
428 'raw_data' => $cgi->hidden('-name' => "dunning_id_$i", '-value' => $ref->{dunning_id})
429 . $cgi->checkbox('-name' => "selected_$i", '-value' => 1, '-label' => ''),
430 'valign' => 'center',
434 if ($first_row_for_dunning) {
435 $row->{language} = {'raw_data' => $cgi->hidden('-name' => "language_id_$i", '-value' => $ref->{language_id})
436 . " $ref->{language}" };
438 $row->{language} = { };
441 push @{ $current_dunning_rows }, $row;
443 $previous_dunning_id = $ref->{dunning_id};
444 $first_row_for_dunning = 0;
447 $report->add_data($current_dunning_rows) if (scalar @{ $current_dunning_rows });
449 $report->set_options('raw_top_info_text' => $form->parse_html_template('dunning/show_dunning_top'),
450 'raw_bottom_info_text' => $form->parse_html_template('dunning/show_dunning_bottom'),
451 'output_format' => 'HTML',
452 'attachment_basename' => $locale->text('dunning_list') . strftime('_%Y%m%d', localtime time),
455 $report->set_options_from_form();
457 $report->generate_with_headers();
459 $main::lxdebug->leave_sub();
464 $main::lxdebug->enter_sub();
466 my $form = $main::form;
468 $main::auth->assert('dunning_edit');
470 $form->{rowcount} = 1;
471 $form->{selected_1} = 1;
472 $form->{dunning_id_1} = $form->{dunning_id};
473 $form->{language_id_1} = $form->{language_id};
477 $main::lxdebug->leave_sub();
481 $main::auth->assert('dunning_edit');
483 my @dunning_ids = map { $::form->{"dunning_id_$_"} } grep { $::form->{"selected_$_"} } (1..$::form->{rowcount});
485 if (!scalar @dunning_ids) {
486 $::form->error($::locale->text('No dunnings have been selected for printing.'));
489 my $dunnings = SL::DB::Manager::Dunning->get_all(query => [ dunning_id => \@dunning_ids ]);
491 SL::DB::Dunning->new->db->with_transaction(sub {
492 for my $dunning (@$dunnings) {
493 SL::DB::Manager::Invoice->find_by(id => $dunning->trans_id)->update_attributes(dunning_config_id => undef);
498 flash('info', t8('#1 dunnings have been deleted', scalar @$dunnings));
504 $main::lxdebug->enter_sub();
506 my $form = $main::form;
507 my %myconfig = %main::myconfig;
508 my $locale = $main::locale;
510 $main::auth->assert('dunning_edit');
512 $form->{title} = $locale->text('Print dunnings');
514 my @dunning_ids = map { $form->{"dunning_id_$_"} } grep { $form->{"selected_$_"} } (1..$form->{rowcount});
515 my @language_ids = map { $form->{"language_id_$_"} } grep { $form->{"selected_$_"} } (1..$form->{rowcount});
517 if (!scalar @dunning_ids) {
518 $form->error($locale->text('No dunnings have been selected for printing.'));
521 $form->{DUNNING_PDFS} = [];
523 my $saved_language_id = $form->{language_id};
525 foreach my $dunning_id (@dunning_ids) {
526 if (!$form->{force_lang}) {
527 $form->{language_id} = $language_ids[$i];
529 $form->{dunning_id} = $dunning_id;
530 DN->print_invoice_for_fees(\%myconfig, $form, $dunning_id);
531 DN->print_dunning(\%myconfig, $form, $dunning_id);
534 $form->{language_id} = $saved_language_id;
536 if (scalar @{ $form->{DUNNING_PDFS} }) {
537 $form->{dunning_id} = strftime("%Y%m%d", localtime time) if scalar @{ $form->{DUNNING_PDFS}} > 1;
538 DN->melt_pdfs(\%myconfig, $form, $form->{copies});
540 if ($form->{media} eq 'printer') {
542 $form->info($locale->text('The dunnings have been printed.'));
546 $form->redirect($locale->text('Could not print dunning.'));
549 $main::lxdebug->leave_sub();
553 call_sub($main::form->{nextsub});
557 foreach my $action (qw(delete print_multiple)) {
558 if ($::form->{"action_${action}"}) {
564 $::form->error($::locale->text('No action defined.'));