Mahnwesen:
[kivitendo-erp.git] / bin / mozilla / dn.pl
1 #=====================================================================
2 # LX-Office ERP
3 # Copyright (C) 2006
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
6 #
7 #=====================================================================
8 # SQL-Ledger Accounting
9 # Copyright (c) 1998-2002
10 #
11 #  Author: Dieter Simader
12 #   Email: dsimader@sql-ledger.org
13 #     Web: http://www.sql-ledger.org
14 #
15 #
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.
20 #
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 #======================================================================
29 #
30 # Dunning process module
31 #
32 #======================================================================
33
34 use POSIX;
35
36 use SL::IS;
37 use SL::PE;
38 use SL::DN;
39
40 require "bin/mozilla/common.pl";
41 require "bin/mozilla/io.pl";
42 require "bin/mozilla/arap.pl";
43
44 1;
45
46 sub edit_config {
47   $lxdebug->enter_sub();
48
49   DN->get_config(\%myconfig, \%$form);
50   $form->get_lists('charts' => { 'key'       => 'ALL_CHARTS',
51                                  'transdate' => 'current_date' });
52
53   $form->{SELECT_AR_AMOUNT} = [];
54   $form->{SELECT_AR}        = [];
55
56   foreach my $chart (@{ $form->{ALL_CHARTS} }) {
57     $chart->{LINKS} = { map { $_, 1 } split m/:/, $chart->{link} };
58
59     if ($chart->{LINKS}->{AR}) {
60       $chart->{AR_selected} = "selected" if $chart->{id} == $form->{AR};
61       push @{ $form->{SELECT_AR} }, $chart;
62     }
63
64     if ($chart->{LINKS}->{AR_amount}) {
65       $chart->{AR_amount_fee_selected}      = "selected" if $chart->{id} == $form->{AR_amount_fee};
66       $chart->{AR_amount_interest_selected} = "selected" if $chart->{id} == $form->{AR_amount_interest};
67       push @{ $form->{SELECT_AR_AMOUNT} }, $chart;
68     }
69   }
70
71   $form->{title}          = $locale->text('Edit Dunning Process Config');
72   $form->{callback}     ||= build_std_url("action=edit_config");
73   $form->{rowcount}       = 1 + scalar @{ $form->{DUNNING} };
74   $form->{rowcount_odd}   = $form->{rowcount} % 2;
75
76   $form->header();
77   print $form->parse_html_template("dunning/edit_config");
78
79   $lxdebug->leave_sub();
80 }
81
82 sub add {
83   $lxdebug->enter_sub();
84
85   # setup customer selection
86   $form->all_vc(\%myconfig, "customer", "AR");
87
88   DN->get_config(\%myconfig, \%$form);
89
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} };
93
94   $form->{title}    = $locale->text('Start Dunning Process');
95   $form->{jsscript} = 1;
96   $form->{fokus}    = "search.customer";
97   $form->header();
98
99   print $form->parse_html_template("dunning/add");
100
101   $lxdebug->leave_sub();
102 }
103
104 sub show_invoices {
105   $lxdebug->enter_sub();
106
107   DN->get_invoices(\%myconfig, \%$form);
108   $form->{title} = $locale->text('Start Dunning Process');
109
110   foreach my $row (@{ $form->{DUNNINGS} }) {
111     $row->{DUNNING_CONFIG} = [ map +{ %{ $_ } }, @{ $form->{DUNNING_CONFIG} } ];
112
113     if ($row->{next_dunning_config_id}) {
114       map { $_->{SELECTED} = $_->{id} == $row->{next_dunning_config_id} } @{ $row->{DUNNING_CONFIG } };
115     }
116     map { $row->{$_} = $form->format_amount(\%myconfig, $row->{$_} * 1, -2) } qw(amount fee interest);
117   }
118
119   $form->get_lists('printers'  => 'printers',
120                    'languages' => 'languages');
121
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));
126
127   $form->{PRINT_OPTIONS}  = print_options({ 'inline'          => 1,
128                                             'no_queue'        => 1,
129                                             'no_postscript'   => 1,
130                                             'no_html'         => 1,
131                                             'no_opendocument' => 1, });
132
133   $form->header();
134   print $form->parse_html_template("dunning/show_invoices");
135
136   $lxdebug->leave_sub();
137 }
138
139 sub save {
140   $lxdebug->enter_sub();
141
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);
148     }
149   }
150
151   DN->save_config(\%myconfig, \%$form);
152   # saving the history
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));
157   }
158   # /saving the history 
159   $form->redirect($locale->text('Dunning Process Config saved!'));
160
161   $lxdebug->leave_sub();
162 }
163
164 sub save_dunning {
165   $lxdebug->enter_sub();
166
167   my $active=1;
168   my @rows = ();
169   undef($form->{DUNNING_PDFS});
170
171   if ($form->{groupinvoices}) {
172     my %dunnings_for;
173
174     for my $i (1 .. $form->{rowcount}) {
175       next unless ($form->{"active_$i"});
176
177       $dunnings_for{$form->{"customer_id_$i"}} ||= {};
178       my $dunning_levels = $dunnings_for{$form->{"customer_id_$i"}};
179
180       $dunning_levels->{$form->{"next_dunning_config_id_$i"}} ||= [];
181       my $level = $dunning_levels->{$form->{"next_dunning_config_id_$i"}};
182
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"}, };
188     }
189
190     foreach my $levels (values %dunnings_for) {
191       foreach my $level (values %{ $levels }) {
192         next unless scalar @{ $level };
193
194         DN->save_dunning(\%myconfig, $form, $level, $userspath, $spool, $sendmail);
195       }
196     }
197
198   } else {
199     for my $i (1 .. $form->{rowcount}) {
200       next unless $form->{"active_$i"};
201
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, $sendmail);
208     }
209   }
210
211   if($form->{DUNNING_PDFS}) {
212     DN->melt_pdfs(\%myconfig, $form, $form->{copies});
213   }
214
215   # saving the history
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));
220   }
221   # /saving the history
222
223   if ($form->{media} eq 'printer') {
224     delete $form->{callback};
225     $form->redirect($locale->text('Dunning Process started for selected invoices!'));
226   }
227
228   $lxdebug->leave_sub();
229 }
230
231 sub set_email {
232   $lxdebug->enter_sub();
233
234
235   my $callback = "$form->{script}?action=set_email&";
236   map({ $callback .= "$_=" . $form->escape($form->{$_}) . "&" }
237       (qw(login password name input_subject input_body input_attachment email_subject email_body email_attachment), grep({ /^[fl]_/ } keys %$form)));
238
239   if ($form->{email_attachment}) {
240     $form->{email_attachment} = "checked";
241   }
242   $form->{"title"} = $locale->text("Set eMail text");
243   $form->header();
244   print($form->parse_html_template("dunning/set_email"));
245
246   $lxdebug->leave_sub();
247 }
248
249 sub search {
250   $lxdebug->enter_sub();
251
252   $form->get_lists("customers"   => "ALL_CUSTOMERS",
253                    "departments" => "ALL_DEPARTMENTS");
254
255   DN->get_config(\%myconfig, \%$form);
256
257   $form->{SHOW_CUSTOMER_DDBOX}   = scalar @{ $form->{ALL_CUSTOMERS} } <= $myconfig{vclimit};
258   $form->{SHOW_DEPARTMENT_DDBOX} = scalar @{ $form->{ALL_CUSTOMERS} };
259   $form->{SHOW_DUNNING_LEVELS}   = scalar @{ $form->{DUNNING} };
260
261   $form->{jsscript} = 1;
262   $form->{title}    = $locale->text('Search Dunning');
263   $form->{fokus}    = "search.customer";
264
265   $form->header();
266
267   $form->{onload} = qq|focus()|
268     . qq|;setupDateFormat('|. $myconfig{dateformat} .qq|', '|. $locale->text("Falsches Datumsformat!") .qq|')|
269     . qq|;setupPoints('|. $myconfig{numberformat} .qq|', '|. $locale->text("wrongformat") .qq|')|;
270
271   print $form->parse_html_template("dunning/search");
272
273   $lxdebug->leave_sub();
274
275 }
276
277 sub show_dunning {
278   $lxdebug->enter_sub();
279
280   DN->get_dunning(\%myconfig, \%$form);
281
282   my $odd_even = 0;
283   my ($previous_dunning_id, $first_row_for_dunning);
284
285   foreach $ref (@{ $form->{DUNNINGS} }) {
286     if ($previous_dunning_id != $ref->{dunning_id}) {
287       $odd_even = ($odd_even + 1) % 2;
288       $ref->{first_row_for_dunning} = 1;
289
290     } else {
291       $ref->{first_row_for_dunning} = 0;
292     }
293
294     $previous_dunning_id     = $ref->{dunning_id};
295     $ref->{listrow_odd_even} = $odd_even;
296   }
297
298   if (!$form->{callback}) {
299     $form->{callback} =
300       build_std_url("action=show_dunning", qw(customer_id customer dunning_level department_id invnumber ordnumber
301                                               ransdatefrom transdateto dunningfrom dunningto notes showold));
302   }
303
304   $form->get_lists('printers'  => 'printers',
305                    'languages' => 'languages');
306
307   $form->{type}          = 'dunning';
308   $form->{PRINT_OPTIONS} = print_options({ 'inline'          => 1,
309                                            'no_queue'        => 1,
310                                            'no_postscript'   => 1,
311                                            'no_html'         => 1,
312                                            'no_opendocument' => 1, });
313   $form->{title}         = $locale->text('Dunning overview');
314
315   $form->header();
316
317   print $form->parse_html_template("dunning/show_dunning");
318
319   $lxdebug->leave_sub();
320
321 }
322
323 sub print_dunning {
324   $lxdebug->enter_sub();
325
326   $form->{rowcount}     = 1;
327   $form->{selected_1}   = 1;
328   $form->{dunning_id_1} = $form->{dunning_id};
329
330   print_multiple();
331
332   $lxdebug->leave_sub();
333 }
334
335 sub print_multiple {
336   $lxdebug->enter_sub();
337
338   $form->{title} = $locale->text('Print dunnings');
339
340   my @dunning_ids = map { $form->{"dunning_id_$_"} } grep { $form->{"selected_$_"} } (1..$form->{rowcount});
341
342   if (!scalar @dunning_ids) {
343     $form->error($locale->text('No dunnings have been selected for printing.'));
344   }
345
346   $form->{DUNNING_PDFS} = [];
347
348   foreach my $dunning_id (@dunning_ids) {
349     DN->print_invoice_for_fees(\%myconfig, $form, $dunning_id);
350     DN->print_dunning(\%myconfig, $form, $dunning_id);
351   }
352
353   if (scalar @{ $form->{DUNNING_PDFS} }) {
354     $form->{dunning_id} = strftime("%Y%m%d", localtime time);
355     DN->melt_pdfs(\%myconfig, $form, $form->{copies});
356
357     if ($form->{media} eq 'printer') {
358       $form->header();
359       $form->info($locale->text('The dunnings have been printed.'));
360     }
361
362   } else {
363     $form->redirect($locale->text('Could not print dunning.'));
364   }
365
366   $lxdebug->leave_sub();
367 }
368
369 # end of main
370