e8e242a61716c2e83a440e336f1bd7725d4f6541
[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->{rowcount}       = scalar @{ $form->{DUNNINGS} };
99   $form->{jsscript}       = 1;
100   $form->{callback}     ||= build_std_url("action=show_invoices", qw(login password customer invnumber ordnumber groupinvoices minamount dunning_level notes));
101
102   $form->{PRINT_OPTIONS}  = print_options(1);
103
104   $form->header();
105   print $form->parse_html_template("dunning/show_invoices");
106
107   $lxdebug->leave_sub();
108 }
109
110 sub save {
111   $lxdebug->enter_sub();
112
113   for my $i (1 .. $form->{rowcount}) {
114     if ($form->{"dunning_description_$i"} ne "") {
115       $form->isblank("dunning_level_$i", $locale->text('Dunning Level missing in row '). $i);
116       $form->isblank("dunning_description_$i", $locale->text('Dunning Description missing in row '). $i);
117       $form->isblank("terms_$i", $locale->text('Terms missing in row '). $i);
118       $form->isblank("payment_terms_$i", $locale->text('Payment Terms missing in row '). $i);
119     }
120   }
121
122   DN->save_config(\%myconfig, \%$form);
123   # saving the history
124   if(!exists $form->{addition} && $form->{id} ne "") {
125         $form->{snumbers} = qq|dunning_id_| . $form->{"dunning_id"};
126     $form->{addition} = "SAVED FOR DUNNING";
127         $form->save_history($form->dbconnect(\%myconfig));
128   }
129   # /saving the history 
130   $form->redirect($locale->text('Dunning Process Config saved!'));
131
132   $lxdebug->leave_sub();
133 }
134
135 sub save_dunning {
136   $lxdebug->enter_sub();
137
138   my $active=1;
139   my @rows = ();
140   undef($form->{DUNNING_PDFS});
141
142   if ($form->{groupinvoices}) {
143     my %dunnings_for;
144
145     for my $i (1 .. $form->{rowcount}) {
146       next unless ($form->{"active_$i"});
147
148       $dunnings_for{$form->{"customer_id_$i"}} ||= {};
149       my $dunning_levels = $dunnings_for{$form->{"customer_id_$i"}};
150
151       $dunning_levels->{$form->{"next_dunning_config_id_$i"}} ||= [];
152       my $level = $dunning_levels->{$form->{"next_dunning_config_id_$i"}};
153
154       push @{ $level }, { "row"                    => $i,
155                           "invoice_id"             => $form->{"inv_id_$i"},
156                           "customer_id"            => $form->{"customer_id_$i"},
157                           "next_dunning_config_id" => $form->{"next_dunning_config_id_$i"},
158                           "email"                  => $form->{"email_$i"}, };
159     }
160
161     foreach my $levels (values %dunnings_for) {
162       foreach my $level (values %{ $levels }) {
163         next unless scalar @{ $level };
164
165         DN->save_dunning(\%myconfig, \%$form, $level, $userspath, $spool, $sendmail);
166       }
167     }
168
169   } else {
170     for my $i (1 .. $form->{rowcount}) {
171       next unless $form->{"active_$i"};
172
173       my $level = [ { "row"                    => $i,
174                       "invoice_id"             => $form->{"inv_id_$i"},
175                       "customer_id"            => $form->{"customer_id_$i"},
176                       "next_dunning_config_id" => $form->{"next_dunning_config_id_$i"},
177                       "email"                  => $form->{"email_$i"}, } ];
178       DN->save_dunning(\%myconfig, \%$form, $level, $userspath, $spool, $sendmail);
179     }
180   }
181
182   if($form->{DUNNING_PDFS}) {
183     DN->melt_pdfs(\%myconfig, \%$form,$spool);
184   }
185
186   # saving the history
187   if(!exists $form->{addition} && $form->{id} ne "") {
188         $form->{snumbers} = qq|dunning_id_| . $form->{"dunning_id"};
189     $form->{addition} = "DUNNING STARTED";
190         $form->save_history($form->dbconnect(\%myconfig));
191   }
192   # /saving the history
193
194   $form->redirect($locale->text('Dunning Process started for selected invoices!'));
195
196   $lxdebug->leave_sub();
197 }
198
199 sub set_email {
200   $lxdebug->enter_sub();
201
202
203   my $callback = "$form->{script}?action=set_email&";
204   map({ $callback .= "$_=" . $form->escape($form->{$_}) . "&" }
205       (qw(login password name input_subject input_body input_attachment email_subject email_body email_attachment), grep({ /^[fl]_/ } keys %$form)));
206
207   if ($form->{email_attachment}) {
208     $form->{email_attachment} = "checked";
209   }
210   $form->{"title"} = $locale->text("Set eMail text");
211   $form->header();
212   print($form->parse_html_template("dunning/set_email"));
213
214   $lxdebug->leave_sub();
215 }
216
217 sub search {
218   $lxdebug->enter_sub();
219
220   $form->get_lists("customers"   => "ALL_CUSTOMERS",
221                    "departments" => "ALL_DEPARTMENTS");
222
223   DN->get_config(\%myconfig, \%$form);
224
225   $form->{SHOW_CUSTOMER_DDBOX}   = scalar @{ $form->{ALL_CUSTOMERS} } <= $myconfig{vclimit};
226   $form->{SHOW_DEPARTMENT_DDBOX} = scalar @{ $form->{ALL_CUSTOMERS} };
227   $form->{SHOW_DUNNING_LEVELS}   = scalar @{ $form->{DUNNING} };
228
229   $form->{jsscript}    = 1;
230   $form->{title}       = $locale->text('Search Dunning');
231   $form->{fokus}       = "search.customer";
232   $form->{javascript} .= qq||;
233   $form->header;
234
235   $form->{onload} = qq|focus()|
236     . qq|;setupDateFormat('|. $myconfig{dateformat} .qq|', '|. $locale->text("Falsches Datumsformat!") .qq|')|
237     . qq|;setupPoints('|. $myconfig{numberformat} .qq|', '|. $locale->text("wrongformat") .qq|')|;
238
239   print $form->parse_html_template("dunning/search");
240
241   $lxdebug->leave_sub();
242
243 }
244
245 sub show_dunning {
246   $lxdebug->enter_sub();
247
248   DN->get_dunning(\%myconfig, \%$form);
249
250   my $odd_even = 0;
251   my ($previous_dunning_id, $first_row_for_dunning);
252
253   foreach $ref (@{ $form->{DUNNINGS} }) {
254     if ($previous_dunning_id != $ref->{dunning_id}) {
255       $odd_even = ($odd_even + 1) % 2;
256       $ref->{first_row_for_dunning} = 1;
257
258     } else {
259       $ref->{first_row_for_dunning} = 0;
260     }
261
262     $previous_dunning_id     = $ref->{dunning_id};
263     $ref->{listrow_odd_even} = $odd_even;
264   }
265
266   if (!$form->{callback}) {
267     $form->{callback} =
268       build_std_url("action=show_dunning", qw(customer_id customer dunning_level department_id invnumber ordnumber
269                                               ransdatefrom transdateto dunningfrom dunningto notes showold));
270   }
271
272   $form->{title} = $locale->text('Dunning overview');
273   $form->header();
274
275   print $form->parse_html_template("dunning/show_dunning");
276
277   $lxdebug->leave_sub();
278
279 }
280
281 sub print_dunning {
282   $lxdebug->enter_sub();
283
284   DN->print_dunning(\%myconfig, \%$form, $form->{dunning_id}, $userspath, $spool, $sendmail);
285
286   if($form->{DUNNING_PDFS}) {
287     DN->melt_pdfs(\%myconfig, \%$form,$spool);
288   } else {
289     $form->redirect($locale->text('Could not create dunning copy!'));
290   }
291
292   $lxdebug->leave_sub();
293
294 }
295
296 # end of main
297