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