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