Kosmetik: Eine überflüssige Zeile entfernt; Einrückung.
[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 SL::IS;
35 use SL::PE;
36 use SL::DN;
37 use Data::Dumper;
38
39 require "bin/mozilla/common.pl";
40 require "bin/mozilla/io.pl";
41 require "bin/mozilla/arap.pl";
42
43 1;
44
45 sub edit_config {
46   $lxdebug->enter_sub();
47
48   DN->get_config(\%myconfig, \%$form);
49
50   $form->{title}          = $locale->text('Edit Dunning Process Config');
51   $form->{callback}     ||= build_std_url("action=edit_config");
52   $form->{rowcount}       = 1 + scalar @{ $form->{DUNNING} };
53   $form->{rowcount_odd}   = $form->{rowcount} % 2;
54
55   $form->header();
56   print $form->parse_html_template("dunning/edit_config");
57
58   $lxdebug->leave_sub();
59 }
60
61 sub add {
62   $lxdebug->enter_sub();
63
64   # setup customer selection
65   $form->all_vc(\%myconfig, "customer", "AR");
66
67   DN->get_config(\%myconfig, \%$form);
68
69   $form->{SHOW_CUSTOMER_SELECTION}      = $form->{all_customer}    && scalar @{ $form->{all_customer} };
70   $form->{SHOW_DUNNING_LEVEL_SELECTION} = $form->{DUNNING}         && scalar @{ $form->{DUNNING} };
71   $form->{SHOW_DEPARTMENT_SELECTION}    = $form->{all_departments} && scalar @{ $form->{all_departments} };
72
73   $form->{title}    = $locale->text('Start Dunning Process');
74   $form->{jsscript} = 1;
75   $form->{fokus}    = "search.customer";
76   $form->header();
77
78   print $form->parse_html_template("dunning/add");
79
80   $lxdebug->leave_sub();
81 }
82
83 sub show_invoices {
84   $lxdebug->enter_sub();
85
86   DN->get_invoices(\%myconfig, \%$form);
87   $form->{title} = $locale->text('Start Dunning Process');
88
89   foreach my $row (@{ $form->{DUNNINGS} }) {
90     $row->{DUNNING_CONFIG} = [ map +{ %{ $_ } }, @{ $form->{DUNNING_CONFIG} } ];
91
92     if ($row->{next_dunning_config_id}) {
93       map { $_->{SELECTED} = $_->{id} == $row->{next_dunning_config_id} } @{ $row->{DUNNING_CONFIG } };
94     }
95     map { $row->{$_} = $form->format_amount(\%myconfig, $row->{$_} * 1, -2) } qw(amount fee interest);
96   }
97
98   $form->{type}           = 'dunning';
99   $form->{rowcount}       = scalar @{ $form->{DUNNINGS} };
100   $form->{jsscript}       = 1;
101   $form->{callback}     ||= build_std_url("action=show_invoices", qw(login password customer invnumber ordnumber groupinvoices minamount dunning_level notes));
102
103   $form->{PRINT_OPTIONS}  = print_options({ 'inline' => 1 });
104
105   $form->header();
106   print $form->parse_html_template("dunning/show_invoices");
107
108   $lxdebug->leave_sub();
109 }
110
111 sub save {
112   $lxdebug->enter_sub();
113
114   for my $i (1 .. $form->{rowcount}) {
115     if ($form->{"dunning_description_$i"} ne "") {
116       $form->isblank("dunning_level_$i", $locale->text('Dunning Level missing in row '). $i);
117       $form->isblank("dunning_description_$i", $locale->text('Dunning Description missing in row '). $i);
118       $form->isblank("terms_$i", $locale->text('Terms missing in row '). $i);
119       $form->isblank("payment_terms_$i", $locale->text('Payment Terms missing in row '). $i);
120     }
121   }
122
123   DN->save_config(\%myconfig, \%$form);
124   # saving the history
125   if(!exists $form->{addition} && $form->{id} ne "") {
126         $form->{snumbers} = qq|dunning_id_| . $form->{"dunning_id"};
127     $form->{addition} = "SAVED FOR DUNNING";
128         $form->save_history($form->dbconnect(\%myconfig));
129   }
130   # /saving the history 
131   $form->redirect($locale->text('Dunning Process Config saved!'));
132
133   $lxdebug->leave_sub();
134 }
135
136 sub save_dunning {
137   $lxdebug->enter_sub();
138
139   my $active=1;
140   my @rows = ();
141   undef($form->{DUNNING_PDFS});
142
143   if ($form->{groupinvoices}) {
144     my %dunnings_for;
145
146     for my $i (1 .. $form->{rowcount}) {
147       next unless ($form->{"active_$i"});
148
149       $dunnings_for{$form->{"customer_id_$i"}} ||= {};
150       my $dunning_levels = $dunnings_for{$form->{"customer_id_$i"}};
151
152       $dunning_levels->{$form->{"next_dunning_config_id_$i"}} ||= [];
153       my $level = $dunning_levels->{$form->{"next_dunning_config_id_$i"}};
154
155       push @{ $level }, { "row"                    => $i,
156                           "invoice_id"             => $form->{"inv_id_$i"},
157                           "customer_id"            => $form->{"customer_id_$i"},
158                           "next_dunning_config_id" => $form->{"next_dunning_config_id_$i"},
159                           "email"                  => $form->{"email_$i"}, };
160     }
161
162     foreach my $levels (values %dunnings_for) {
163       foreach my $level (values %{ $levels }) {
164         next unless scalar @{ $level };
165
166         DN->save_dunning(\%myconfig, \%$form, $level, $userspath, $spool, $sendmail);
167       }
168     }
169
170   } else {
171     for my $i (1 .. $form->{rowcount}) {
172       next unless $form->{"active_$i"};
173
174       my $level = [ { "row"                    => $i,
175                       "invoice_id"             => $form->{"inv_id_$i"},
176                       "customer_id"            => $form->{"customer_id_$i"},
177                       "next_dunning_config_id" => $form->{"next_dunning_config_id_$i"},
178                       "email"                  => $form->{"email_$i"}, } ];
179       DN->save_dunning(\%myconfig, \%$form, $level, $userspath, $spool, $sendmail);
180     }
181   }
182
183   if($form->{DUNNING_PDFS}) {
184     DN->melt_pdfs(\%myconfig, \%$form,$spool);
185   }
186
187   # saving the history
188   if(!exists $form->{addition} && $form->{id} ne "") {
189         $form->{snumbers} = qq|dunning_id_| . $form->{"dunning_id"};
190     $form->{addition} = "DUNNING STARTED";
191         $form->save_history($form->dbconnect(\%myconfig));
192   }
193   # /saving the history
194
195   $form->redirect($locale->text('Dunning Process started for selected invoices!'));
196
197   $lxdebug->leave_sub();
198 }
199
200 sub set_email {
201   $lxdebug->enter_sub();
202
203
204   my $callback = "$form->{script}?action=set_email&";
205   map({ $callback .= "$_=" . $form->escape($form->{$_}) . "&" }
206       (qw(login password name input_subject input_body input_attachment email_subject email_body email_attachment), grep({ /^[fl]_/ } keys %$form)));
207
208   if ($form->{email_attachment}) {
209     $form->{email_attachment} = "checked";
210   }
211   $form->{"title"} = $locale->text("Set eMail text");
212   $form->header();
213   print($form->parse_html_template("dunning/set_email"));
214
215   $lxdebug->leave_sub();
216 }
217
218 sub search {
219   $lxdebug->enter_sub();
220
221   $form->get_lists("customers"   => "ALL_CUSTOMERS",
222                    "departments" => "ALL_DEPARTMENTS");
223
224   DN->get_config(\%myconfig, \%$form);
225
226   $form->{SHOW_CUSTOMER_DDBOX}   = scalar @{ $form->{ALL_CUSTOMERS} } <= $myconfig{vclimit};
227   $form->{SHOW_DEPARTMENT_DDBOX} = scalar @{ $form->{ALL_CUSTOMERS} };
228   $form->{SHOW_DUNNING_LEVELS}   = scalar @{ $form->{DUNNING} };
229
230   $form->{jsscript} = 1;
231   $form->{title}    = $locale->text('Search Dunning');
232   $form->{fokus}    = "search.customer";
233
234   $form->header();
235
236   $form->{onload} = qq|focus()|
237     . qq|;setupDateFormat('|. $myconfig{dateformat} .qq|', '|. $locale->text("Falsches Datumsformat!") .qq|')|
238     . qq|;setupPoints('|. $myconfig{numberformat} .qq|', '|. $locale->text("wrongformat") .qq|')|;
239
240   print $form->parse_html_template("dunning/search");
241
242   $lxdebug->leave_sub();
243
244 }
245
246 sub show_dunning {
247   $lxdebug->enter_sub();
248
249   DN->get_dunning(\%myconfig, \%$form);
250
251   my $odd_even = 0;
252   my ($previous_dunning_id, $first_row_for_dunning);
253
254   foreach $ref (@{ $form->{DUNNINGS} }) {
255     if ($previous_dunning_id != $ref->{dunning_id}) {
256       $odd_even = ($odd_even + 1) % 2;
257       $ref->{first_row_for_dunning} = 1;
258
259     } else {
260       $ref->{first_row_for_dunning} = 0;
261     }
262
263     $previous_dunning_id     = $ref->{dunning_id};
264     $ref->{listrow_odd_even} = $odd_even;
265   }
266
267   if (!$form->{callback}) {
268     $form->{callback} =
269       build_std_url("action=show_dunning", qw(customer_id customer dunning_level department_id invnumber ordnumber
270                                               ransdatefrom transdateto dunningfrom dunningto notes showold));
271   }
272
273   $form->{title} = $locale->text('Dunning overview');
274   $form->header();
275
276   print $form->parse_html_template("dunning/show_dunning");
277
278   $lxdebug->leave_sub();
279
280 }
281
282 sub print_dunning {
283   $lxdebug->enter_sub();
284
285   DN->print_dunning(\%myconfig, \%$form, $form->{dunning_id}, $userspath, $spool, $sendmail);
286
287   if($form->{DUNNING_PDFS}) {
288     DN->melt_pdfs(\%myconfig, \%$form,$spool);
289   } else {
290     $form->redirect($locale->text('Could not create dunning copy!'));
291   }
292
293   $lxdebug->leave_sub();
294
295 }
296
297 # end of main
298