Projektverwaltung in eine eigene Datei ausgelagert und auf die Verwendung von Templat...
[kivitendo-erp.git] / bin / mozilla / arap.pl
1 #=====================================================================
2 # LX-Office ERP
3 # Copyright (C) 2004
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) 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 # common routines for gl, ar, ap, is, ir, oe
31 #
32
33 use SL::Projects;
34
35 # any custom scripts for this one
36 if (-f "bin/mozilla/custom_arap.pl") {
37   eval { require "bin/mozilla/custom_arap.pl"; };
38 }
39 if (-f "bin/mozilla/$form->{login}_arap.pl") {
40   eval { require "bin/mozilla/$form->{login}_arap.pl"; };
41 }
42
43 1;
44
45 require "bin/mozilla/common.pl";
46
47 # end of main
48
49 sub check_name {
50   $lxdebug->enter_sub();
51
52   $auth->assert('general_ledger         | vendor_invoice_edit       | sales_order_edit    | invoice_edit |' .
53                 'request_quotation_edit | sales_quotation_edit      | purchase_order_edit | cash');
54
55   my ($name) = @_;
56
57   $name = $name eq "customer" ? "customer" : "vendor";
58
59   my ($new_name, $new_id) = split /--/, $form->{$name};
60   my $i = 0;
61   # if we use a selection
62   if ($form->{"select$name"}) {
63     if ($form->{"old$name"} ne $form->{$name}) {
64
65       # this is needed for is, ir and oe
66       $form->{update} = 0;
67       # for credit calculations
68       $form->{oldinvtotal}  = 0;
69       $form->{oldtotalpaid} = 0;
70       $form->{calctax}      = 1;
71
72       $form->{"${name}_id"} = $new_id;
73
74       IS->get_customer(\%myconfig, \%$form) if ($name eq 'customer');
75       IR->get_vendor(\%myconfig, \%$form) if ($name eq 'vendor');
76
77       $form->{$name} = $form->{"old$name"} = "$new_name--$new_id";
78
79       $i = 1;
80     }
81   } else {
82
83     # check name, combine name and id
84     if ($form->{"old$name"} ne qq|$form->{$name}--$form->{"${name}_id"}|) {
85
86       # this is needed for is, ir and oe
87       $form->{update} = 0;
88
89       # for credit calculations
90       $form->{oldinvtotal}  = 0;
91       $form->{oldtotalpaid} = 0;
92       $form->{calctax}      = 1;
93
94       # return one name or a list of names in $form->{name_list}
95       if (($i = $form->get_name(\%myconfig, $name)) > 1) {
96         &select_name($name);
97         exit;
98       }
99
100       if ($i == 1) {
101
102         # we got one name
103         $form->{"${name}_id"} = $form->{name_list}[0]->{id};
104         $form->{$name}        = $form->{name_list}[0]->{name};
105         $form->{"old$name"}   = qq|$form->{$name}--$form->{"${name}_id"}|;
106
107         IS->get_customer(\%myconfig, \%$form) if ($name eq 'customer');
108         IR->get_vendor(\%myconfig, \%$form) if ($name eq 'vendor');
109
110       } else {
111
112         # name is not on file
113         # $locale->text('Customer not on file or locked!')
114         # $locale->text('Vendor not on file or locked!')
115         $msg = ucfirst $name . " not on file or locked!";
116         $form->error($locale->text($msg));
117       }
118     }
119   }
120   $form->language_payment(\%myconfig);
121
122   $lxdebug->leave_sub();
123
124   return $i;
125 }
126
127 # $locale->text('Customer not on file!')
128 # $locale->text('Vendor not on file!')
129
130 sub select_name {
131   $lxdebug->enter_sub();
132
133   $auth->assert('general_ledger         | vendor_invoice_edit  | sales_order_edit    | invoice_edit |' .
134                 'request_quotation_edit | sales_quotation_edit | purchase_order_edit | cash');
135
136   my ($table) = @_;
137
138   @column_index = qw(ndx name address);
139
140   $label             = ucfirst $table;
141   $column_data{ndx}  = qq|<th>&nbsp;</th>|;
142   $column_data{name} =
143     qq|<th class=listheading>| . $locale->text($label) . qq|</th>|;
144   $column_data{address} =
145     qq|<th class=listheading>| . $locale->text('Address') . qq|</th>|;
146
147   # list items with radio button on a form
148   $form->header;
149
150   $title = $locale->text('Select from one of the names below');
151
152   print qq|
153 <body>
154
155 <form method=post action=$form->{script}>
156
157 <table width=100%>
158   <tr>
159     <th class=listtop>$title</th>
160   </tr>
161   <tr space=5></tr>
162   <tr>
163     <td>
164       <table width=100%>
165         <tr class=listheading>|;
166
167   map { print "\n$column_data{$_}" } @column_index;
168
169   print qq|
170         </tr>
171 |;
172
173   my $i = 0;
174   foreach $ref (@{ $form->{name_list} }) {
175     $checked = ($i++) ? "" : "checked";
176
177     $ref->{name} =~ s/\"/&quot;/g;
178
179     $column_data{ndx} =
180       qq|<td><input name=ndx class=radio type=radio value=$i $checked></td>|;
181     $column_data{name} =
182       qq|<td><input name="new_name_$i" type=hidden value="$ref->{name}">$ref->{name}</td>|;
183     $column_data{address} = qq|<td>$ref->{address}&nbsp;</td>|;
184
185     $j++;
186     $j %= 2;
187     print qq|
188         <tr class=listrow$j>|;
189
190     map { print "\n$column_data{$_}" } @column_index;
191
192     print qq|
193         </tr>
194
195 <input name="new_id_$i" type=hidden value=$ref->{id}>
196
197 |;
198
199   }
200
201   print qq|
202       </table>
203     </td>
204   </tr>
205   <tr>
206     <td><hr size=3 noshade></td>
207   </tr>
208 </table>
209
210 <input name=lastndx type=hidden value=$i>
211
212 |;
213
214   # delete variables
215   map { delete $form->{$_} } qw(action name_list header);
216
217   # save all other form variables
218   foreach $key (keys %${form}) {
219     next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
220     $form->{$key} =~ s/\"/&quot;/g;
221     print qq|<input name=$key type=hidden value="$form->{$key}">\n|;
222   }
223
224   print qq|
225 <input type=hidden name=nextsub value=name_selected>
226
227 <input type=hidden name=vc value=$table>
228 <br>
229 <input class=submit type=submit name=action value="|
230     . $locale->text('Continue') . qq|">
231 </form>
232
233 </body>
234 </html>
235 |;
236
237   $lxdebug->leave_sub();
238 }
239
240 sub name_selected {
241   $lxdebug->enter_sub();
242
243   $auth->assert('general_ledger         | vendor_invoice_edit  | sales_order_edit    | invoice_edit |' .
244                 'request_quotation_edit | sales_quotation_edit | purchase_order_edit | cash');
245
246   # replace the variable with the one checked
247
248   # index for new item
249   $i = $form->{ndx};
250
251   $form->{ $form->{vc} }    = $form->{"new_name_$i"};
252   $form->{"$form->{vc}_id"} = $form->{"new_id_$i"};
253   $form->{"old$form->{vc}"} =
254     qq|$form->{$form->{vc}}--$form->{"$form->{vc}_id"}|;
255
256   # delete all the new_ variables
257   for $i (1 .. $form->{lastndx}) {
258     map { delete $form->{"new_${_}_$i"} } qw(id name);
259   }
260
261   map { delete $form->{$_} } qw(ndx lastndx nextsub);
262
263   IS->get_customer(\%myconfig, \%$form) if ($form->{vc} eq 'customer');
264   IR->get_vendor(\%myconfig, \%$form) if ($form->{vc} eq 'vendor');
265
266   &update(1);
267
268   $lxdebug->leave_sub();
269 }
270
271 sub check_project {
272   $lxdebug->enter_sub();
273
274   $auth->assert('general_ledger         | vendor_invoice_edit  | sales_order_edit    | invoice_edit |' .
275                 'request_quotation_edit | sales_quotation_edit | purchase_order_edit | cash         | report');
276
277   for $i (1 .. $form->{rowcount}) {
278     my $suffix = $i ? "_$i" : "";
279     my $prefix = $i ? "" : "global";
280     $form->{"${prefix}project_id${suffix}"} = "" unless $form->{"${prefix}projectnumber$suffix"};
281     if ($form->{"${prefix}projectnumber${suffix}"} ne $form->{"old${prefix}projectnumber${suffix}"}) {
282       if ($form->{"${prefix}projectnumber${suffix}"}) {
283
284         # get new project
285         $form->{projectnumber} = $form->{"${prefix}projectnumber${suffix}"};
286         my %params             = map { $_ => $form->{$_} } qw(projectnumber description active);
287         if (($rows = Projects->search_projects(%params)) > 1) {
288
289           # check form->{project_list} how many there are
290           $form->{rownumber} = $i;
291           &select_project($i ? undef : 1);
292           exit;
293         }
294
295         if ($rows == 1) {
296           $form->{"${prefix}project_id${suffix}"}       = $form->{project_list}->[0]->{id};
297           $form->{"${prefix}projectnumber${suffix}"}    = $form->{project_list}->[0]->{projectnumber};
298           $form->{"old${prefix}projectnumber${suffix}"} = $form->{project_list}->[0]->{projectnumber};
299         } else {
300
301           # not on file
302           $form->error($locale->text('Project not on file!'));
303         }
304       } else {
305         $form->{"old${prefix}projectnumber${suffix}"} = "";
306       }
307     }
308   }
309
310   $lxdebug->leave_sub();
311 }
312
313 sub select_project {
314   $lxdebug->enter_sub();
315
316   $auth->assert('general_ledger         | vendor_invoice_edit  | sales_order_edit    | invoice_edit |' .
317                 'request_quotation_edit | sales_quotation_edit | purchase_order_edit | cash         | report');
318
319   my ($is_global) = @_;
320
321   @column_index = qw(ndx projectnumber description);
322
323   $column_data{ndx}           = qq|<th>&nbsp;</th>|;
324   $column_data{projectnumber} = qq|<th>| . $locale->text('Number') . qq|</th>|;
325   $column_data{description}   =
326     qq|<th>| . $locale->text('Description') . qq|</th>|;
327
328   # list items with radio button on a form
329   $form->header;
330
331   $title = $locale->text('Select from one of the projects below');
332
333   print qq|
334 <body>
335
336 <form method=post action=$form->{script}>
337
338 <input type=hidden name=rownumber value=$form->{rownumber}>
339
340 <table width=100%>
341   <tr>
342     <th class=listtop>$title</th>
343   </tr>
344   <tr space=5></tr>
345   <tr>
346     <td>
347       <table width=100%>
348         <tr class=listheading>|;
349
350   map { print "\n$column_data{$_}" } @column_index;
351
352   print qq|
353         </tr>
354 |;
355
356   my $i = 0;
357   foreach $ref (@{ $form->{project_list} }) {
358     $checked = ($i++) ? "" : "checked";
359
360     $ref->{name} =~ s/\"/&quot;/g;
361
362     $column_data{ndx} =
363       qq|<td><input name=ndx class=radio type=radio value=$i $checked></td>|;
364     $column_data{projectnumber} =
365       qq|<td><input name="new_projectnumber_$i" type=hidden value="$ref->{projectnumber}">$ref->{projectnumber}</td>|;
366     $column_data{description} = qq|<td>$ref->{description}</td>|;
367
368     $j++;
369     $j %= 2;
370     print qq|
371         <tr class=listrow$j>|;
372
373     map { print "\n$column_data{$_}" } @column_index;
374
375     print qq|
376         </tr>
377
378 <input name="new_id_$i" type=hidden value=$ref->{id}>
379
380 |;
381
382   }
383
384   print qq|
385       </table>
386     </td>
387   </tr>
388   <tr>
389     <td><hr size=3 noshade></td>
390   </tr>
391 </table>
392
393 <input name=lastndx type=hidden value=$i>
394
395 |;
396
397   # delete action variable
398   map { delete $form->{$_} } qw(action project_list header update);
399
400   # save all other form variables
401   foreach $key (keys %${form}) {
402     next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
403     $form->{$key} =~ s/\"/&quot;/g;
404     print qq|<input name=$key type=hidden value="$form->{$key}">\n|;
405   }
406
407   print qq|
408 <input type="hidden" name="is_global" value="$is_global">
409 <input type=hidden name=nextsub value=project_selected>
410
411 <br>
412 <input class=submit type=submit name=action value="|
413     . $locale->text('Continue') . qq|">
414 </form>
415
416 </body>
417 </html>
418 |;
419
420   $lxdebug->leave_sub();
421 }
422
423 sub project_selected {
424   $lxdebug->enter_sub();
425
426   $auth->assert('general_ledger         | vendor_invoice_edit  | sales_order_edit    | invoice_edit |' .
427                 'request_quotation_edit | sales_quotation_edit | purchase_order_edit | cash         | report');
428
429   # replace the variable with the one checked
430
431   # index for new item
432   $i = $form->{ndx};
433
434   my $prefix = $form->{"is_global"} ? "global" : "";
435   my $suffix = $form->{"is_global"} ? "" : "_$form->{rownumber}";
436
437   $form->{"${prefix}projectnumber${suffix}"} =
438     $form->{"new_projectnumber_$i"};
439   $form->{"old${prefix}projectnumber${suffix}"} =
440     $form->{"new_projectnumber_$i"};
441   $form->{"${prefix}project_id${suffix}"} = $form->{"new_id_$i"};
442
443   # delete all the new_ variables
444   for $i (1 .. $form->{lastndx}) {
445     map { delete $form->{"new_${_}_$i"} } qw(id projectnumber description);
446   }
447
448   map { delete $form->{$_} } qw(ndx lastndx nextsub is_global);
449
450   if ($form->{update}) {
451     call_sub($form->{"update"});
452   } else {
453     &update;
454   }
455
456   $lxdebug->leave_sub();
457 }
458
459 sub continue       { call_sub($form->{"nextsub"}); }
460