This commit was generated by cvs2svn to compensate for changes in r3880,
[freeside.git] / sql-ledger / old / sql-ledger / bin / mozilla / arap.pl
1 #=====================================================================
2 # SQL-Ledger Accounting
3 # Copyright (c) 2002
4 #
5 #  Author: Dieter Simader
6 #   Email: dsimader@sql-ledger.org
7 #     Web: http://www.sql-ledger.org
8 #
9 #
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2 of the License, or
13 # (at your option) any later version.
14 #
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with this program; if not, write to the Free Software
21 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #======================================================================
23 #
24 # common routines for gl, ar, ap, is, ir, oe
25 #
26
27 # any custom scripts for this one
28 if (-f "$form->{path}/custom_arap.pl") {
29   eval { require "$form->{path}/custom_arap.pl"; };
30 }
31 if (-f "$form->{path}/$form->{login}_arap.pl") {
32   eval { require "$form->{path}/$form->{login}_arap.pl"; };
33 }
34
35
36 1;
37 # end of main
38
39
40 sub check_name {
41   my ($name) = @_;
42
43   my ($new_name, $new_id) = split /--/, $form->{$name};
44   my $i = 0;
45   
46   # if we use a selection
47   if ($form->{"select$name"}) {
48     if ($form->{"old$name"} ne $form->{$name}) {
49       # this is needed for is, ir and oe
50       map { delete $form->{"${_}_rate"} } (split / /, $form->{taxaccounts});
51       
52       # for credit calculations
53       $form->{oldinvtotal} = 0;
54       $form->{oldtotalpaid} = 0;
55       
56       $form->{"${name}_id"} = $new_id;
57       $form->{"old$name"} = "$new_name--$new_id";
58
59       IS->get_customer(\%myconfig, \%$form) if ($name eq 'customer');
60       IR->get_vendor(\%myconfig, \%$form) if ($name eq 'vendor');
61
62       $i = 1;
63     }
64   } else {
65
66     # check name, combine name and id
67     if ($form->{"old$name"} ne qq|$form->{$name}--$form->{"${name}_id"}|) {
68       # this is needed for is, ir and oe
69       map { delete $form->{"${_}_rate"} } (split / /, $form->{taxaccounts});
70
71       # for credit calculations
72       $form->{oldinvtotal} = 0;
73       $form->{oldtotalpaid} = 0;
74
75       # return one name or a list of names in $form->{name_list}
76       if (($i = $form->get_name(\%myconfig, $name)) > 1) {
77         &select_name($name);
78         exit;
79       }
80
81       if ($i == 1) {
82         # we got one name
83         $form->{"${name}_id"} = $form->{name_list}[0]->{id};
84         $form->{$name} = $form->{name_list}[0]->{name};
85         $form->{"old$name"} = qq|$form->{$name}--$form->{"${name}_id"}|;
86         
87         IS->get_customer(\%myconfig, \%$form) if ($name eq 'customer');
88         IR->get_vendor(\%myconfig, \%$form) if ($name eq 'vendor');
89         
90       } else {
91         # name is not on file
92         $msg = ucfirst $name . " not on file!";
93         $form->error($locale->text($msg));
94       }
95     }
96   }
97
98   $i;
99
100 }
101
102 # $locale->text('Customer not on file!')
103 # $locale->text('Vendor not on file!')
104
105
106
107 sub select_name {
108   my ($table) = @_;
109   
110   @column_index = qw(ndx name address);
111
112   $label = ucfirst $table;
113   $column_data{ndx} = qq|<th>&nbsp;</th>|;
114   $column_data{name} = qq|<th>|.$locale->text($label).qq|</th>|;
115   $column_data{address} = qq|<th>|.$locale->text('Address').qq|</th>|;
116   
117   # list items with radio button on a form
118   $form->header;
119
120   $title = $locale->text('Select from one of the names below');
121
122   print qq|
123 <body>
124
125 <form method=post action=$form->{script}>
126
127 <table width=100%>
128   <tr>
129     <th class=listtop>$title</th>
130   </tr>
131   <tr space=5></tr>
132   <tr>
133     <td>
134       <table width=100%>
135         <tr class=listheading>|;
136
137   map { print "\n$column_data{$_}" } @column_index;
138   
139   print qq|
140         </tr>
141 |;
142
143   my $i = 0;
144   foreach $ref (@{ $form->{name_list} }) {
145     $checked = ($i++) ? "" : "checked";
146
147     $ref->{name} =~ s/"/&quot;/g;
148     
149    $column_data{ndx} = qq|<td><input name=ndx class=radio type=radio value=$i $checked></td>|;
150    $column_data{name} = qq|<td><input name="new_name_$i" type=hidden value="$ref->{name}">$ref->{name}</td>|;
151    $column_data{address} = qq|<td>$ref->{address}</td>|;
152     
153     $j++; $j %= 2;
154     print qq|
155         <tr class=listrow$j>|;
156
157     map { print "\n$column_data{$_}" } @column_index;
158
159     print qq|
160         </tr>
161
162 <input name="new_id_$i" type=hidden value=$ref->{id}>
163
164 |;
165
166   }
167   
168   print qq|
169       </table>
170     </td>
171   </tr>
172   <tr>
173     <td><hr size=3 noshade></td>
174   </tr>
175 </table>
176
177 <input name=lastndx type=hidden value=$i>
178
179 |;
180
181   # delete action variable
182   delete $form->{action};
183   delete $form->{name_list};
184     
185   # save all other form variables
186   foreach $key (keys %${form}) {
187     $form->{$key} =~ s/"/&quot;/g;
188     print qq|<input name=$key type=hidden value="$form->{$key}">\n|;
189   }
190
191   print qq|
192 <input type=hidden name=nextsub value=name_selected>
193
194 <input type=hidden name=vc value=$table>
195 <br>
196 <input class=submit type=submit name=action value="|.$locale->text('Continue').qq|">
197 </form>
198
199 </body>
200 </html>
201 |;
202
203 }
204
205
206
207 sub name_selected {
208
209   # replace the variable with the one checked
210
211   # index for new item
212   $i = $form->{ndx};
213   
214   $form->{$form->{vc}} = $form->{"new_name_$i"};
215   $form->{"$form->{vc}_id"} = $form->{"new_id_$i"};
216   $form->{"old$form->{vc}"} = qq|$form->{$form->{vc}}--$form->{"$form->{vc}_id"}|;
217
218   # delete all the new_ variables
219   for $i (1 .. $form->{lastndx}) {
220     map { delete $form->{"new_${_}_$i"} } (id, name);
221   }
222   
223   map { delete $form->{$_} } qw(ndx lastndx nextsub);
224
225   IS->get_customer(\%myconfig, \%$form) if ($form->{vc} eq 'customer');
226   IR->get_vendor(\%myconfig, \%$form) if ($form->{vc} eq 'vendor');
227
228   &update(1);
229
230 }
231
232
233 sub add_transaction {
234   my ($module) = @_;
235
236   delete $form->{script};
237   $form->{action} = "add";
238   $form->{type} = "invoice" if $module =~ /(is|ir)/;
239
240   $form->{callback} = $form->escape($form->{callback},1);
241   map { $argv .= "$_=$form->{$_}&" } keys %$form;
242
243   exec ("perl", "$module.pl", $argv);
244
245 }
246
247
248
249 sub check_project {
250
251   for $i (1 .. $form->{rowcount}) {
252     $form->{"project_id_$i"} = "" unless $form->{"projectnumber_$i"};
253     if ($form->{"projectnumber_$i"} ne $form->{"oldprojectnumber_$i"}) {
254       if ($form->{"projectnumber_$i"}) {
255         # get new project
256         $form->{projectnumber} = $form->{"projectnumber_$i"};
257         if (($rows = PE->projects(\%myconfig, $form)) > 1) {
258           # check form->{project_list} how many there are
259           $form->{rownumber} = $i;
260           &select_project;
261           exit;
262         }
263
264         if ($rows == 1) {
265           $form->{"project_id_$i"} = $form->{project_list}->[0]->{id};
266           $form->{"projectnumber_$i"} = $form->{project_list}->[0]->{projectnumber};
267           $form->{"oldprojectnumber_$i"} = $form->{project_list}->[0]->{projectnumber};
268         } else {
269           # not on file
270           $form->error($locale->text('Project not on file!'));
271         }
272       }
273     }
274   }
275
276 }
277
278
279 sub select_project {
280   
281   @column_index = qw(ndx projectnumber description);
282
283   $column_data{ndx} = qq|<th>&nbsp;</th>|;
284   $column_data{projectnumber} = qq|<th>|.$locale->text('Number').qq|</th>|;
285   $column_data{description} = qq|<th>|.$locale->text('Description').qq|</th>|;
286   
287   # list items with radio button on a form
288   $form->header;
289
290   $title = $locale->text('Select from one of the projects below');
291
292   print qq|
293 <body>
294
295 <form method=post action=$form->{script}>
296
297 <input type=hidden name=rownumber value=$form->{rownumber}>
298
299 <table width=100%>
300   <tr>
301     <th class=listtop>$title</th>
302   </tr>
303   <tr space=5></tr>
304   <tr>
305     <td>
306       <table width=100%>
307         <tr class=listheading>|;
308
309   map { print "\n$column_data{$_}" } @column_index;
310   
311   print qq|
312         </tr>
313 |;
314
315   my $i = 0;
316   foreach $ref (@{ $form->{project_list} }) {
317     $checked = ($i++) ? "" : "checked";
318
319     $ref->{name} =~ s/"/&quot;/g;
320     
321    $column_data{ndx} = qq|<td><input name=ndx class=radio type=radio value=$i $checked></td>|;
322    $column_data{projectnumber} = qq|<td><input name="new_projectnumber_$i" type=hidden value="$ref->{projectnumber}">$ref->{projectnumber}</td>|;
323    $column_data{description} = qq|<td>$ref->{description}</td>|;
324     
325     $j++; $j %= 2;
326     print qq|
327         <tr class=listrow$j>|;
328
329     map { print "\n$column_data{$_}" } @column_index;
330
331     print qq|
332         </tr>
333
334 <input name="new_id_$i" type=hidden value=$ref->{id}>
335
336 |;
337
338   }
339   
340   print qq|
341       </table>
342     </td>
343   </tr>
344   <tr>
345     <td><hr size=3 noshade></td>
346   </tr>
347 </table>
348
349 <input name=lastndx type=hidden value=$i>
350
351 |;
352
353   # delete action variable
354   delete $form->{action};
355   delete $form->{project_list};
356     
357   # save all other form variables
358   foreach $key (keys %${form}) {
359     $form->{$key} =~ s/"/&quot;/g;
360     print qq|<input name=$key type=hidden value="$form->{$key}">\n|;
361   }
362
363   print qq|
364 <input type=hidden name=nextsub value=project_selected>
365
366 <br>
367 <input class=submit type=submit name=action value="|.$locale->text('Continue').qq|">
368 </form>
369
370 </body>
371 </html>
372 |;
373
374 }
375
376
377 sub project_selected {
378   
379   # replace the variable with the one checked
380
381   # index for new item
382   $i = $form->{ndx};
383   
384   $form->{"projectnumber_$form->{rownumber}"} = $form->{"new_projectnumber_$i"};
385   $form->{"oldprojectnumber_$form->{rownumber}"} = $form->{"new_projectnumber_$i"};
386   $form->{"project_id_$form->{rownumber}"} = $form->{"new_id_$i"};
387
388   # delete all the new_ variables
389   for $i (1 .. $form->{lastndx}) {
390     map { delete $form->{"new_${_}_$i"} } qw(id projectnumber description);
391   }
392   
393   map { delete $form->{$_} } qw(ndx lastndx nextsub);
394
395   &update;
396
397 }
398
399
400 sub continue { &{ $form->{nextsub} } };
401 sub gl_transaction { &add };
402 sub ar_transaction { &add_transaction(ar) };
403 sub ap_transaction { &add_transaction(ap) };
404 sub sales_invoice { &add_transaction(is) };
405 sub vendor_invoice { &add_transaction(ir) };
406