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