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