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