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