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., 675 Mass Ave, Cambridge, MA 02139, USA.
 
  28 #======================================================================
 
  30 # Dunning process module
 
  32 #======================================================================
 
  39 use SL::ReportGenerator;
 
  41 require "bin/mozilla/common.pl";
 
  42 require "bin/mozilla/reportgenerator.pl";
 
  43 require "bin/mozilla/io.pl";
 
  44 require "bin/mozilla/arap.pl";
 
  49   $lxdebug->enter_sub();
 
  51   DN->get_config(\%myconfig, \%$form);
 
  52   $form->get_lists('charts' => { 'key'       => 'ALL_CHARTS',
 
  53                                  'transdate' => 'current_date' });
 
  55   $form->{SELECT_AR_AMOUNT} = [];
 
  56   $form->{SELECT_AR}        = [];
 
  58   foreach my $chart (@{ $form->{ALL_CHARTS} }) {
 
  59     $chart->{LINKS} = { map { $_, 1 } split m/:/, $chart->{link} };
 
  61     if ($chart->{LINKS}->{AR}) {
 
  62       $chart->{AR_selected} = "selected" if $chart->{id} == $form->{AR};
 
  63       push @{ $form->{SELECT_AR} }, $chart;
 
  66     if ($chart->{LINKS}->{AR_amount}) {
 
  67       $chart->{AR_amount_fee_selected}      = "selected" if $chart->{id} == $form->{AR_amount_fee};
 
  68       $chart->{AR_amount_interest_selected} = "selected" if $chart->{id} == $form->{AR_amount_interest};
 
  69       push @{ $form->{SELECT_AR_AMOUNT} }, $chart;
 
  73   $form->{title}      = $locale->text('Edit Dunning Process Config');
 
  74   $form->{callback} ||= build_std_url("action=edit_config");
 
  77   print $form->parse_html_template("dunning/edit_config");
 
  79   $lxdebug->leave_sub();
 
  83   $lxdebug->enter_sub();
 
  85   # setup customer selection
 
  86   $form->all_vc(\%myconfig, "customer", "AR");
 
  88   DN->get_config(\%myconfig, \%$form);
 
  90   $form->{SHOW_CUSTOMER_SELECTION}      = $form->{all_customer}    && scalar @{ $form->{all_customer} };
 
  91   $form->{SHOW_DUNNING_LEVEL_SELECTION} = $form->{DUNNING}         && scalar @{ $form->{DUNNING} };
 
  92   $form->{SHOW_DEPARTMENT_SELECTION}    = $form->{all_departments} && scalar @{ $form->{all_departments} };
 
  94   $form->{title}    = $locale->text('Start Dunning Process');
 
  95   $form->{jsscript} = 1;
 
  96   $form->{fokus}    = "search.customer";
 
  99   print $form->parse_html_template("dunning/add");
 
 101   $lxdebug->leave_sub();
 
 105   $lxdebug->enter_sub();
 
 107   DN->get_invoices(\%myconfig, \%$form);
 
 108   $form->{title} = $locale->text('Start Dunning Process');
 
 110   foreach my $row (@{ $form->{DUNNINGS} }) {
 
 111     $row->{DUNNING_CONFIG} = [ map +{ %{ $_ } }, @{ $form->{DUNNING_CONFIG} } ];
 
 113     if ($row->{next_dunning_config_id}) {
 
 114       map { $_->{SELECTED} = $_->{id} == $row->{next_dunning_config_id} } @{ $row->{DUNNING_CONFIG } };
 
 116     map { $row->{$_} = $form->format_amount(\%myconfig, $row->{$_} * 1, -2) } qw(amount fee interest);
 
 119   $form->get_lists('printers'  => 'printers',
 
 120                    'languages' => 'languages');
 
 122   $form->{type}           = 'dunning';
 
 123   $form->{rowcount}       = scalar @{ $form->{DUNNINGS} };
 
 124   $form->{jsscript}       = 1;
 
 125   $form->{callback}     ||= build_std_url("action=show_invoices", qw(login password customer invnumber ordnumber groupinvoices minamount dunning_level notes));
 
 127   $form->{PRINT_OPTIONS}  = print_options('inline'          => 1,
 
 129                                           'no_postscript'   => 1,
 
 131                                           'no_opendocument' => 1,);
 
 134   print $form->parse_html_template("dunning/show_invoices");
 
 136   $lxdebug->leave_sub();
 
 140   $lxdebug->enter_sub();
 
 142   for my $i (1 .. $form->{rowcount}) {
 
 143     if ($form->{"dunning_description_$i"} ne "") {
 
 144       $form->isblank("dunning_level_$i", $locale->text('Dunning Level missing in row '). $i);
 
 145       $form->isblank("dunning_description_$i", $locale->text('Dunning Description missing in row '). $i);
 
 146       $form->isblank("terms_$i", $locale->text('Terms missing in row '). $i);
 
 147       $form->isblank("payment_terms_$i", $locale->text('Payment Terms missing in row '). $i);
 
 151   DN->save_config(\%myconfig, \%$form);
 
 153   if(!exists $form->{addition} && $form->{id} ne "") {
 
 154         $form->{snumbers} = qq|dunning_id_| . $form->{"dunning_id"};
 
 155     $form->{addition} = "SAVED FOR DUNNING";
 
 156         $form->save_history($form->dbconnect(\%myconfig));
 
 158   # /saving the history
 
 159   $form->redirect($locale->text('Dunning Process Config saved!'));
 
 161   $lxdebug->leave_sub();
 
 165   $lxdebug->enter_sub();
 
 169   undef($form->{DUNNING_PDFS});
 
 171   if ($form->{groupinvoices}) {
 
 174     for my $i (1 .. $form->{rowcount}) {
 
 175       next unless ($form->{"active_$i"});
 
 177       $dunnings_for{$form->{"customer_id_$i"}} ||= {};
 
 178       my $dunning_levels = $dunnings_for{$form->{"customer_id_$i"}};
 
 180       $dunning_levels->{$form->{"next_dunning_config_id_$i"}} ||= [];
 
 181       my $level = $dunning_levels->{$form->{"next_dunning_config_id_$i"}};
 
 183       push @{ $level }, { "row"                    => $i,
 
 184                           "invoice_id"             => $form->{"inv_id_$i"},
 
 185                           "customer_id"            => $form->{"customer_id_$i"},
 
 186                           "next_dunning_config_id" => $form->{"next_dunning_config_id_$i"},
 
 187                           "email"                  => $form->{"email_$i"}, };
 
 190     foreach my $levels (values %dunnings_for) {
 
 191       foreach my $level (values %{ $levels }) {
 
 192         next unless scalar @{ $level };
 
 194         DN->save_dunning(\%myconfig, $form, $level, $userspath, $spool);
 
 199     for my $i (1 .. $form->{rowcount}) {
 
 200       next unless $form->{"active_$i"};
 
 202       my $level = [ { "row"                    => $i,
 
 203                       "invoice_id"             => $form->{"inv_id_$i"},
 
 204                       "customer_id"            => $form->{"customer_id_$i"},
 
 205                       "next_dunning_config_id" => $form->{"next_dunning_config_id_$i"},
 
 206                       "email"                  => $form->{"email_$i"}, } ];
 
 207       DN->save_dunning(\%myconfig, $form, $level, $userspath, $spool);
 
 211   if($form->{DUNNING_PDFS}) {
 
 212     DN->melt_pdfs(\%myconfig, $form, $form->{copies});
 
 216   if(!exists $form->{addition} && $form->{id} ne "") {
 
 217         $form->{snumbers} = qq|dunning_id_| . $form->{"dunning_id"};
 
 218     $form->{addition} = "DUNNING STARTED";
 
 219         $form->save_history($form->dbconnect(\%myconfig));
 
 221   # /saving the history
 
 223   if ($form->{media} eq 'printer') {
 
 224     delete $form->{callback};
 
 225     $form->redirect($locale->text('Dunning Process started for selected invoices!'));
 
 228   $lxdebug->leave_sub();
 
 232   $lxdebug->enter_sub();
 
 234   $form->{"title"} = $locale->text("Set eMail text");
 
 236   print($form->parse_html_template("dunning/set_email"));
 
 238   $lxdebug->leave_sub();
 
 242   $lxdebug->enter_sub();
 
 244   $form->get_lists("customers"   => "ALL_CUSTOMERS",
 
 245                    "departments" => "ALL_DEPARTMENTS");
 
 247   DN->get_config(\%myconfig, \%$form);
 
 249   $form->{SHOW_CUSTOMER_DDBOX}   = scalar @{ $form->{ALL_CUSTOMERS} } <= $myconfig{vclimit};
 
 250   $form->{SHOW_DEPARTMENT_DDBOX} = scalar @{ $form->{ALL_CUSTOMERS} };
 
 251   $form->{SHOW_DUNNING_LEVELS}   = scalar @{ $form->{DUNNING} };
 
 253   $form->{jsscript} = 1;
 
 254   $form->{title}    = $locale->text('Search Dunning');
 
 255   $form->{fokus}    = "search.customer";
 
 259   $form->{onload} = qq|focus()|
 
 260     . qq|;setupDateFormat('|. $myconfig{dateformat} .qq|', '|. $locale->text("Falsches Datumsformat!") .qq|')|
 
 261     . qq|;setupPoints('|. $myconfig{numberformat} .qq|', '|. $locale->text("wrongformat") .qq|')|;
 
 263   print $form->parse_html_template("dunning/search");
 
 265   $lxdebug->leave_sub();
 
 270   $lxdebug->enter_sub();
 
 272   my @filter_field_list = qw(customer_id customer dunning_level department_id invnumber ordnumber
 
 273                              transdatefrom transdateto dunningfrom dunningto notes showold);
 
 275   DN->get_dunning(\%myconfig, \%$form);
 
 277   if (!$form->{callback}) {
 
 278     $form->{callback} = build_std_url("action=show_dunning", @filter_field_list);
 
 281   $form->get_lists('printers'  => 'printers',
 
 282                    'languages' => 'languages');
 
 284   $form->{type}          = 'dunning';
 
 285   $form->{PRINT_OPTIONS} = print_options('inline'          => 1,
 
 287                                          'no_postscript'   => 1,
 
 289                                          'no_opendocument' => 1,);
 
 290   $form->{title}         = $locale->text('Dunning overview');
 
 292   my $report = SL::ReportGenerator->new(\%myconfig, $form);
 
 294   $report->set_options('std_column_visibility' => 1,
 
 295                        'title'                 => $form->{title});
 
 296   $report->set_export_options('show_dunning', @filter_field_list);
 
 298   $report->set_columns(
 
 299     'checkbox'            => { 'text' => '', 'visible' => 'HTML' },
 
 300     'dunning_description' => { 'text' => $locale->text('Dunning Level') },
 
 301     'customername'        => { 'text' => $locale->text('Customername') },
 
 302     'invnumber'           => { 'text' => $locale->text('Invnumber') },
 
 303     'transdate'           => { 'text' => $locale->text('Invdate') },
 
 304     'duedate'             => { 'text' => $locale->text('Invoice Duedate') },
 
 305     'amount'              => { 'text' => $locale->text('Amount') },
 
 306     'dunning_date'        => { 'text' => $locale->text('Dunning Date') },
 
 307     'dunning_duedate'     => { 'text' => $locale->text('Dunning Duedate') },
 
 308     'fee'                 => { 'text' => $locale->text('Total Fees') },
 
 309     'interest'            => { 'text' => $locale->text('Interest') },
 
 312   $report->set_column_order(qw(checkbox dunning_description customername invnumber transdate
 
 313                                duedate amount dunning_date dunning_duedate fee interest));
 
 315   my $edit_url  = build_std_url('script=is.pl', 'action=edit', 'callback') . '&id=';
 
 316   my $print_url = build_std_url('action=print_dunning', 'format=pdf', 'media=screen') . '&dunning_id=';
 
 318   my %alignment = map { $_ => 'right' } qw(transdate duedate amount dunning_date dunning_duedate fee interest);
 
 320   my ($current_dunning_rows, $previous_dunning_id, $first_row_for_dunning);
 
 322   $current_dunning_rows  = [];
 
 323   $first_row_for_dunning = 1;
 
 324   $form->{rowcount}      = scalar @{ $form->{DUNNINGS} };
 
 328   foreach $ref (@{ $form->{DUNNINGS} }) {
 
 331     if ($previous_dunning_id != $ref->{dunning_id}) {
 
 332       $report->add_data($current_dunning_rows) if (scalar @{ $current_dunning_rows });
 
 333       $current_dunning_rows  = [];
 
 334       $first_row_for_dunning = 1;
 
 338     foreach my $column (keys %{ $ref }) {
 
 340         'data'  => $first_row_for_dunning || (($column ne 'dunning_description') && ($column ne 'customername')) ? $ref->{$column} : '',
 
 342         'align' => $alignment{$column},
 
 344         'link'  => ($column eq 'invnumber'           ? $edit_url  . E($ref->{id})         :
 
 345                     $column eq 'dunning_description' ? $print_url . E($ref->{dunning_id}) : ''),
 
 349     $row->{checkbox} = !$first_row_for_dunning ? { } : {
 
 350       'raw_data' =>   $cgi->hidden('-name' => "dunning_id_$i", '-value' => $ref->{dunning_id})
 
 351                     . $cgi->checkbox('-name' => "selected_$i", '-value' => 1, '-label' => ''),
 
 352       'valign'   => 'center',
 
 356     push @{ $current_dunning_rows }, $row;
 
 358     $previous_dunning_id   = $ref->{dunning_id};
 
 359     $first_row_for_dunning = 0;
 
 362   $report->add_data($current_dunning_rows) if (scalar @{ $current_dunning_rows });
 
 364   $report->set_options('raw_top_info_text'    => $form->parse_html_template('dunning/show_dunning_top'),
 
 365                        'raw_bottom_info_text' => $form->parse_html_template('dunning/show_dunning_bottom'),
 
 366                        'output_format'        => 'HTML',
 
 367                        'attachment_basename'  => $locale->text('dunning_list') . strftime('_%Y%m%d', localtime time),
 
 370   $report->set_options_from_form();
 
 372   $report->generate_with_headers();
 
 374   $lxdebug->leave_sub();
 
 379   $lxdebug->enter_sub();
 
 381   $form->{rowcount}     = 1;
 
 382   $form->{selected_1}   = 1;
 
 383   $form->{dunning_id_1} = $form->{dunning_id};
 
 387   $lxdebug->leave_sub();
 
 391   $lxdebug->enter_sub();
 
 393   $form->{title} = $locale->text('Print dunnings');
 
 395   my @dunning_ids = map { $form->{"dunning_id_$_"} } grep { $form->{"selected_$_"} } (1..$form->{rowcount});
 
 397   if (!scalar @dunning_ids) {
 
 398     $form->error($locale->text('No dunnings have been selected for printing.'));
 
 401   $form->{DUNNING_PDFS} = [];
 
 403   foreach my $dunning_id (@dunning_ids) {
 
 404     DN->print_invoice_for_fees(\%myconfig, $form, $dunning_id);
 
 405     DN->print_dunning(\%myconfig, $form, $dunning_id);
 
 408   if (scalar @{ $form->{DUNNING_PDFS} }) {
 
 409     $form->{dunning_id} = strftime("%Y%m%d", localtime time);
 
 410     DN->melt_pdfs(\%myconfig, $form, $form->{copies});
 
 412     if ($form->{media} eq 'printer') {
 
 414       $form->info($locale->text('The dunnings have been printed.'));
 
 418     $form->redirect($locale->text('Could not print dunning.'));
 
 421   $lxdebug->leave_sub();