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;
 
  41 use SL::Helper::Flash qw(flash);
 
  42 use SL::Locale::String qw(t8);
 
  43 use SL::ReportGenerator;
 
  45 require "bin/mozilla/common.pl";
 
  46 require "bin/mozilla/reportgenerator.pl";
 
  47 require "bin/mozilla/io.pl";
 
  54   $main::lxdebug->enter_sub();
 
  56   my $form     = $main::form;
 
  57   my %myconfig = %main::myconfig;
 
  58   my $locale   = $main::locale;
 
  60   $main::auth->assert('config');
 
  62   DN->get_config(\%myconfig, \%$form);
 
  63   $form->get_lists('charts' => { 'key'       => 'ALL_CHARTS',
 
  64                                  'transdate' => 'current_date' });
 
  66   $form->{SELECT_AR_AMOUNT} = [];
 
  67   $form->{SELECT_AR}        = [];
 
  69   foreach my $chart (@{ $form->{ALL_CHARTS} }) {
 
  70     $chart->{LINKS} = { map { $_, 1 } split m/:/, $chart->{link} };
 
  72     if ($chart->{LINKS}->{AR}) {
 
  73       $chart->{AR_selected} = "selected" if $chart->{id} == $form->{AR};
 
  74       push @{ $form->{SELECT_AR} }, $chart;
 
  77     if ($chart->{LINKS}->{AR_amount}) {
 
  78       $chart->{AR_amount_fee_selected}      = "selected" if $chart->{id} == $form->{AR_amount_fee};
 
  79       $chart->{AR_amount_interest_selected} = "selected" if $chart->{id} == $form->{AR_amount_interest};
 
  80       push @{ $form->{SELECT_AR_AMOUNT} }, $chart;
 
  84   $form->{title}      = $locale->text('Edit Dunning Process Config');
 
  85   $form->{callback} ||= build_std_url("action=edit_config");
 
  87   setup_dn_edit_config_action_bar();
 
  90   print $form->parse_html_template("dunning/edit_config");
 
  92   $main::lxdebug->leave_sub();
 
  96   $main::lxdebug->enter_sub();
 
  98   my $form     = $main::form;
 
  99   my %myconfig = %main::myconfig;
 
 100   my $locale   = $main::locale;
 
 102   $main::auth->assert('dunning_edit');
 
 104   DN->get_config(\%myconfig, \%$form);
 
 106   $form->get_lists("departments" => "ALL_DEPARTMENTS");
 
 108   $form->{SHOW_DUNNING_LEVEL_SELECTION} = $form->{DUNNING}         && scalar @{ $form->{DUNNING} };
 
 109   $form->{SHOW_DEPARTMENT_SELECTION}    = $form->{ALL_DEPARTMENTS} && scalar @{ $form->{ALL_DEPARTMENTS} || [] };
 
 111   $form->{title}    = $locale->text('Start Dunning Process');
 
 113   setup_dn_add_action_bar();
 
 116   print $form->parse_html_template("dunning/add");
 
 118   $main::lxdebug->leave_sub();
 
 122   $main::lxdebug->enter_sub();
 
 124   my $form     = $main::form;
 
 125   my %myconfig = %main::myconfig;
 
 126   my $locale   = $main::locale;
 
 128   $main::auth->assert('dunning_edit');
 
 130   DN->get_invoices(\%myconfig, \%$form);
 
 131   $form->{title} = $locale->text('Start Dunning Process');
 
 133   foreach my $row (@{ $form->{DUNNINGS} }) {
 
 134     $row->{DUNNING_CONFIG} = [ map +{ %{ $_ } }, @{ $form->{DUNNING_CONFIG} } ];
 
 136     if ($row->{next_dunning_config_id}) {
 
 137       map { $_->{SELECTED} = $_->{id} == $row->{next_dunning_config_id} } @{ $row->{DUNNING_CONFIG } };
 
 139     map { $row->{$_} = $form->format_amount(\%myconfig, $row->{$_} * 1, 2) } qw(amount open_amount fee interest);
 
 141     if ($row->{'language_id'}) {
 
 142       $row->{language} = SL::DB::Manager::Language->find_by_or_create('id' => $row->{'language_id'})->{'description'};
 
 146   $form->get_lists('printers'  => 'printers',
 
 147                    'languages' => 'languages');
 
 149   $form->{type}           = 'dunning';
 
 150   $form->{rowcount}       = scalar @{ $form->{DUNNINGS} };
 
 151   $form->{callback}     ||= build_std_url("action=show_invoices", qw(customer invnumber ordnumber groupinvoices minamount dunning_level notes));
 
 153   $form->{PRINT_OPTIONS}  = print_options('inline'          => 1,
 
 155                                           'no_postscript'   => 1,
 
 157                                           'no_opendocument' => 1,);
 
 159   setup_dn_show_invoices_action_bar();
 
 161   print $form->parse_html_template("dunning/show_invoices");
 
 163   $main::lxdebug->leave_sub();
 
 167   $main::lxdebug->enter_sub();
 
 169   my $form     = $main::form;
 
 170   my %myconfig = %main::myconfig;
 
 171   my $locale   = $main::locale;
 
 173   $main::auth->assert('config');
 
 175   for my $i (1 .. $form->{rowcount}) {
 
 176     if ($form->{"dunning_description_$i"} ne "") {
 
 177       $form->isblank("dunning_level_$i", $locale->text('Dunning Level missing in row '). $i);
 
 178       $form->isblank("dunning_description_$i", $locale->text('Dunning Description missing in row '). $i);
 
 179       $form->isblank("terms_$i", $locale->text('Terms missing in row '). $i);
 
 180       $form->isblank("payment_terms_$i", $locale->text('Payment Terms missing in row '). $i);
 
 184   DN->save_config(\%myconfig, \%$form);
 
 186   if(!exists $form->{addition} && $form->{id} ne "") {
 
 187     $form->{snumbers} = qq|dunning_id_| . $form->{"dunning_id"};
 
 188     $form->{addition} = "SAVED FOR DUNNING";
 
 191   # /saving the history
 
 192   $form->redirect($locale->text('Dunning Process Config saved!'));
 
 194   $main::lxdebug->leave_sub();
 
 198   $main::lxdebug->enter_sub();
 
 200   my $form     = $main::form;
 
 201   my %myconfig = %main::myconfig;
 
 202   my $locale   = $main::locale;
 
 204   $main::auth->assert('dunning_edit');
 
 208   undef($form->{DUNNING_PDFS});
 
 210   my $saved_language_id = $form->{language_id};
 
 212   if ($form->{groupinvoices} || $form->{l_include_credit_notes}) {
 
 215     for my $i (1 .. $form->{rowcount}) {
 
 216       next unless ($form->{"active_$i"});
 
 218       $dunnings_for{$form->{"customer_id_$i"}} ||= {};
 
 219       my $dunning_levels = $dunnings_for{$form->{"customer_id_$i"}};
 
 221       $dunning_levels->{$form->{"next_dunning_config_id_$i"}} ||= [];
 
 222       my $level = $dunning_levels->{$form->{"next_dunning_config_id_$i"}};
 
 224       push @{ $level }, { "row"                    => $i,
 
 225                           "invoice_id"             => $form->{"inv_id_$i"},
 
 226                           "credit_note"            => $form->{"credit_note_$i"},
 
 227                           "customer_id"            => $form->{"customer_id_$i"},
 
 228                           "language_id"            => $form->{"language_id_$i"},
 
 229                           "next_dunning_config_id" => $form->{"next_dunning_config_id_$i"},
 
 230                           "print_invoice"          => $form->{"include_invoice_$i"},
 
 231                           "email"                  => $form->{"email_$i"}, };
 
 234     foreach my $levels (values %dunnings_for) {
 
 235       foreach my $level (values %{ $levels }) {
 
 236         next unless scalar @{ $level };
 
 237         if (!$form->{force_lang}) {
 
 238           $form->{language_id} = @{$level}[0]->{language_id};
 
 240         DN->save_dunning(\%myconfig, $form, $level);
 
 245     for my $i (1 .. $form->{rowcount}) {
 
 246       next unless $form->{"active_$i"};
 
 248       my $level = [ { "row"                    => $i,
 
 249                       "invoice_id"             => $form->{"inv_id_$i"},
 
 250                       "customer_id"            => $form->{"customer_id_$i"},
 
 251                       "language_id"            => $form->{"language_id_$i"},
 
 252                       "next_dunning_config_id" => $form->{"next_dunning_config_id_$i"},
 
 253                       "print_invoice"          => $form->{"include_invoice_$i"},
 
 254                       "email"                  => $form->{"email_$i"}, } ];
 
 255       if (!$form->{force_lang}) {
 
 256         $form->{language_id} = @{$level}[0]->{language_id};
 
 258       DN->save_dunning(\%myconfig, $form, $level);
 
 262   $form->{language_id} = $saved_language_id;
 
 264   if (scalar @{ $form->{DUNNING_PDFS} }) {
 
 265     $form->{dunning_id} = strftime("%Y%m%d", localtime time) if scalar @{ $form->{DUNNING_PDFS}} > 1;
 
 266     DN->melt_pdfs(\%myconfig, $form, $form->{copies});
 
 270   if(!exists $form->{addition} && $form->{id} ne "") {
 
 271     $form->{snumbers} = qq|dunning_id_| . $form->{"dunning_id"};
 
 272     $form->{addition} = "DUNNING STARTED";
 
 275   # /saving the history
 
 277   if ($form->{media} eq 'printer') {
 
 278     delete $form->{callback};
 
 279     $form->redirect($locale->text('Dunning Process started for selected invoices!'));
 
 282   $main::lxdebug->leave_sub();
 
 286   $main::lxdebug->enter_sub();
 
 288   my $form     = $main::form;
 
 289   my $locale   = $main::locale;
 
 291   $main::auth->assert('dunning_edit');
 
 293   $form->{"title"} = $locale->text("Set eMail text");
 
 294   $form->header(no_layout => 1);
 
 295   print($form->parse_html_template("dunning/set_email"));
 
 297   $main::lxdebug->leave_sub();
 
 301   $main::lxdebug->enter_sub();
 
 303   my $form     = $main::form;
 
 304   my %myconfig = %main::myconfig;
 
 305   my $locale   = $main::locale;
 
 307   $main::auth->assert('dunning_edit');
 
 309   $form->get_lists("customers"   => "ALL_CUSTOMERS",
 
 310                    "departments" => "ALL_DEPARTMENTS");
 
 311   $form->{ALL_EMPLOYEES} = SL::DB::Manager::Employee->get_all_sorted(query => [ deleted => 0 ]);
 
 313   DN->get_config(\%myconfig, \%$form);
 
 315   $form->{SHOW_DUNNING_LEVELS}   = scalar @{ $form->{DUNNING} };
 
 317   $form->{title}    = $locale->text('Dunnings');
 
 319   setup_dn_search_action_bar();
 
 322   print $form->parse_html_template("dunning/search");
 
 324   $main::lxdebug->leave_sub();
 
 329   $main::lxdebug->enter_sub();
 
 331   my $form     = $main::form;
 
 332   my %myconfig = %main::myconfig;
 
 333   my $locale   = $main::locale;
 
 334   my $cgi      = $::request->{cgi};
 
 336   $main::auth->assert('dunning_edit');
 
 338   my @filter_field_list = qw(customer_id customer dunning_level department_id invnumber ordnumber
 
 339                              transdatefrom transdateto dunningfrom dunningto notes showold l_salesman salesman_id);
 
 341   report_generator_set_default_sort('customername', 1);
 
 343   DN->get_dunning(\%myconfig, \%$form);
 
 345   if (!$form->{callback}) {
 
 346     $form->{callback} = build_std_url("action=show_dunning", @filter_field_list);
 
 349   $form->get_lists('printers'  => 'printers',
 
 350                    'languages' => 'languages');
 
 352   $form->{type}          = 'dunning';
 
 353   $form->{PRINT_OPTIONS} = print_options('inline'          => 1,
 
 355                                          'no_postscript'   => 1,
 
 357                                          'no_opendocument' => 1,);
 
 358   $form->{title}         = $locale->text('Dunning overview');
 
 360   my $report = SL::ReportGenerator->new(\%myconfig, $form);
 
 362   $report->set_options('std_column_visibility' => 1,
 
 363                        'title'                 => $form->{title});
 
 364   $report->set_export_options('show_dunning', @filter_field_list, qw(sort sortdir));
 
 367     'checkbox'            => { 'text' => '', 'visible' => 'HTML' },
 
 368     'dunning_description' => { 'text' => $locale->text('Dunning Level') },
 
 369     'customername'        => { 'text' => $locale->text('Customername') },
 
 370     'departmentname'      => { 'text' => $locale->text('Department') },
 
 371     'language'            => { 'text' => $locale->text('Language') },
 
 372     'invnumber'           => { 'text' => $locale->text('Invnumber') },
 
 373     'transdate'           => { 'text' => $locale->text('Invdate') },
 
 374     'duedate'             => { 'text' => $locale->text('Invoice Duedate') },
 
 375     'amount'              => { 'text' => $locale->text('Amount') },
 
 376     'dunning_date'        => { 'text' => $locale->text('Dunning Date') },
 
 377     'dunning_duedate'     => { 'text' => $locale->text('Dunning Duedate') },
 
 378     'fee'                 => { 'text' => $locale->text('Total Fees') },
 
 379     'interest'            => { 'text' => $locale->text('Interest') },
 
 380     'salesman'            => { 'text' => $locale->text('Salesperson'), 'visible' => $form->{l_salesman} ? 1 : 0 },
 
 383   $report->set_columns(%column_defs);
 
 384   $report->set_column_order(qw(checkbox dunning_description customername language invnumber transdate
 
 385                                duedate amount dunning_date dunning_duedate fee interest salesman));
 
 386   $report->set_sort_indicator($form->{sort}, $form->{sortdir});
 
 388   my $edit_url  = sub { build_std_url('script=' . ($_[0]->{invoice} ? 'is' : 'ar') . '.pl', 'action=edit', 'callback') . '&id=' . $::form->escape($_[0]->{id}) };
 
 389   my $print_url = sub { build_std_url('action=print_dunning', 'format=pdf', 'media=screen', 'dunning_id='.$_[0]->{dunning_id}, 'language_id=' . $_[0]->{language_id}) };
 
 390   my $sort_url  = build_std_url('action=show_dunning', grep { $form->{$_} } @filter_field_list);
 
 392   foreach my $name (qw(dunning_description customername invnumber transdate duedate dunning_date dunning_duedate salesman)) {
 
 393     my $sortdir                 = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
 
 394     $column_defs{$name}->{link} = $sort_url . "&sort=$name&sortdir=$sortdir";
 
 397   my %alignment = map { $_ => 'right' } qw(transdate duedate amount dunning_date dunning_duedate fee interest salesman);
 
 399   my ($current_dunning_rows, $previous_dunning_id, $first_row_for_dunning);
 
 401   $current_dunning_rows  = [];
 
 402   $first_row_for_dunning = 1;
 
 403   $form->{rowcount}      = scalar @{ $form->{DUNNINGS} };
 
 407   foreach my $ref (@{ $form->{DUNNINGS} }) {
 
 410     if ($previous_dunning_id != $ref->{dunning_id}) {
 
 411       $report->add_data($current_dunning_rows) if (scalar @{ $current_dunning_rows });
 
 412       $current_dunning_rows  = [];
 
 413       $first_row_for_dunning = 1;
 
 416     if ($ref->{'language_id'}) {
 
 417       $ref->{language} = SL::DB::Manager::Language->find_by('id' => $ref->{'language_id'})->{'description'};
 
 421     foreach my $column (keys %{ $ref }) {
 
 423         'data'  => $first_row_for_dunning || (($column ne 'dunning_description') && ($column ne 'customername')) ? $ref->{$column} : '',
 
 425         'align' => $alignment{$column},
 
 427         'link'  => (  $column eq 'invnumber'           ? $edit_url->($ref)
 
 428                     : $column eq 'dunning_description' ? $print_url->($ref)
 
 433     $row->{checkbox} = !$first_row_for_dunning ? { } : {
 
 434       'raw_data' =>   $cgi->hidden('-name' => "dunning_id_$i", '-value' => $ref->{dunning_id})
 
 435                     . $cgi->checkbox('-name' => "selected_$i", '-value' => 1, '-label' => ''),
 
 436       'valign'   => 'center',
 
 440     if ($first_row_for_dunning) {
 
 441       $row->{language} = {'raw_data' => $cgi->hidden('-name' => "language_id_$i", '-value' => $ref->{language_id})
 
 442                                         . " $ref->{language}" };
 
 444       $row->{language} = { };
 
 447     push @{ $current_dunning_rows }, $row;
 
 449     $previous_dunning_id   = $ref->{dunning_id};
 
 450     $first_row_for_dunning = 0;
 
 453   $report->add_data($current_dunning_rows) if (scalar @{ $current_dunning_rows });
 
 455   $report->set_options('raw_top_info_text'    => $form->parse_html_template('dunning/show_dunning_top'),
 
 456                        'raw_bottom_info_text' => $form->parse_html_template('dunning/show_dunning_bottom'),
 
 457                        'output_format'        => 'HTML',
 
 458                        'attachment_basename'  => $locale->text('dunning_list') . strftime('_%Y%m%d', localtime time),
 
 461   $report->set_options_from_form();
 
 463   setup_dn_show_dunning_action_bar();
 
 464   $report->generate_with_headers();
 
 466   $main::lxdebug->leave_sub();
 
 471   $main::lxdebug->enter_sub();
 
 473   my $form     = $main::form;
 
 475   $main::auth->assert('dunning_edit');
 
 477   $form->{rowcount}     = 1;
 
 478   $form->{selected_1}   = 1;
 
 479   $form->{dunning_id_1} = $form->{dunning_id};
 
 480   $form->{language_id_1} = $form->{language_id};
 
 484   $main::lxdebug->leave_sub();
 
 488   $main::auth->assert('dunning_edit');
 
 490   my @dunning_ids = map { $::form->{"dunning_id_$_"} } grep { $::form->{"selected_$_"} } (1..$::form->{rowcount});
 
 492   if (!scalar @dunning_ids) {
 
 493     $::form->error($::locale->text('No dunnings have been selected for printing.'));
 
 496   my $dunnings = SL::DB::Manager::Dunning->get_all(query => [ dunning_id => \@dunning_ids ]);
 
 498   SL::DB::Dunning->new->db->with_transaction(sub {
 
 499     for my $dunning (@$dunnings) {
 
 500       SL::DB::Manager::Invoice->find_by(id => $dunning->trans_id)->update_attributes(dunning_config_id => undef);
 
 505   flash('info', t8('#1 dunnings have been deleted', scalar @$dunnings));
 
 511   $main::lxdebug->enter_sub();
 
 513   my $form     = $main::form;
 
 514   my %myconfig = %main::myconfig;
 
 515   my $locale   = $main::locale;
 
 517   $main::auth->assert('dunning_edit');
 
 519   $form->{title} = $locale->text('Print dunnings');
 
 521   my @dunning_ids = map { $form->{"dunning_id_$_"} } grep { $form->{"selected_$_"} } (1..$form->{rowcount});
 
 522   my @language_ids = map { $form->{"language_id_$_"} } grep { $form->{"selected_$_"} } (1..$form->{rowcount});
 
 524   if (!scalar @dunning_ids) {
 
 525     $form->error($locale->text('No dunnings have been selected for printing.'));
 
 528   $form->{DUNNING_PDFS} = [];
 
 530   my $saved_language_id = $form->{language_id};
 
 532   foreach my $dunning_id (@dunning_ids) {
 
 533     if (!$form->{force_lang}) {
 
 534       $form->{language_id} = $language_ids[$i];
 
 536     $form->{dunning_id} = $dunning_id;
 
 537     DN->print_invoice_for_fees(\%myconfig, $form, $dunning_id);
 
 538     DN->print_dunning(\%myconfig, $form, $dunning_id);
 
 541   $form->{language_id} = $saved_language_id;
 
 543   if (scalar @{ $form->{DUNNING_PDFS} }) {
 
 544     $form->{dunning_id} = strftime("%Y%m%d", localtime time) if scalar @{ $form->{DUNNING_PDFS}} > 1;
 
 545     DN->melt_pdfs(\%myconfig, $form, $form->{copies});
 
 547     if ($form->{media} eq 'printer') {
 
 549       $form->info($locale->text('The dunnings have been printed.'));
 
 553     $form->redirect($locale->text('Could not print dunning.'));
 
 556   $main::lxdebug->leave_sub();
 
 560   call_sub($main::form->{nextsub});
 
 564   foreach my $action (qw(delete print_multiple)) {
 
 565     if ($::form->{"action_${action}"}) {
 
 571   $::form->error($::locale->text('No action defined.'));
 
 574 sub setup_dn_add_action_bar {
 
 577   for my $bar ($::request->layout->get('actionbar')) {
 
 581         submit    => [ '#form', { action => "show_invoices" } ],
 
 582         accesskey => 'enter',
 
 588 sub setup_dn_show_invoices_action_bar {
 
 591   for my $bar ($::request->layout->get('actionbar')) {
 
 595         submit    => [ '#form', { action => "save_dunning" } ],
 
 596         checks    => [ [ 'kivi.check_if_entries_selected', '[name^=active_]' ] ],
 
 597         accesskey => 'enter',
 
 604 sub setup_dn_search_action_bar {
 
 607   for my $bar ($::request->layout->get('actionbar')) {
 
 611         submit    => [ '#form', { action => "show_dunning" } ],
 
 612         accesskey => 'enter',
 
 618 sub setup_dn_show_dunning_action_bar {
 
 621   for my $bar ($::request->layout->get('actionbar')) {
 
 625         submit    => [ '#form', { action => "print_multiple" } ],
 
 626         checks    => [ [ 'kivi.check_if_entries_selected', '[name^=selected_]' ] ],
 
 627         accesskey => 'enter',
 
 632         submit  => [ '#form', { action => "delete" } ],
 
 633         checks  => [ [ 'kivi.check_if_entries_selected', '[name^=selected_]' ] ],
 
 634         confirm => $::locale->text('This resets the dunning process for the selected invoices. Posted dunning invoices will not be changed!'),
 
 640 sub setup_dn_edit_config_action_bar {
 
 643   for my $bar ($::request->layout->get('actionbar')) {
 
 647         submit    => [ '#form', { action => "save" } ],
 
 648         accesskey => 'enter',