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