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