75764a4348f44d71ae0f45b771a2c7663ae861af
[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 strict;
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/$::myconfig{login}_arap.pl") {
40   eval { require "bin/mozilla/$::myconfig{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   $main::lxdebug->enter_sub();
51
52   my $form     = $main::form;
53   my %myconfig = %main::myconfig;
54   my $locale   = $main::locale;
55
56   $main::auth->assert('general_ledger               | vendor_invoice_edit       | sales_order_edit    | invoice_edit |' .
57                 'request_quotation_edit       | sales_quotation_edit      | purchase_order_edit | cash         |' .
58                 'purchase_delivery_order_edit | sales_delivery_order_edit');
59
60   my ($name, %params) = @_;
61
62   $name = $name eq "customer" ? "customer" : "vendor";
63
64   my ($new_name,$new_id) = $form->{$name} =~ /^(.*?)--(\d+)$/;
65   my $i = 0;
66   # if we use a selection
67   if ($form->{"select$name"}) {
68     if ($form->{"old$name"} ne $form->{$name}) {
69
70       # this is needed for is, ir and oe
71       $form->{update} = 0;
72       # for credit calculations
73       $form->{oldinvtotal}  = 0;
74       $form->{oldtotalpaid} = 0;
75       $form->{calctax}      = 1;
76
77       $form->{"${name}_id"} = $new_id;
78
79       _reset_salesman_id();
80       delete @{ $form }{qw(payment_id)};
81
82       IS->get_customer(\%myconfig, \%$form) if ($name eq 'customer');
83       IR->get_vendor(\%myconfig, \%$form) if ($name eq 'vendor');
84
85       $form->{$name} = $form->{"old$name"} = "$new_name--$new_id";
86
87       $i = 1;
88     }
89   } else {
90
91     # check name, combine name and id
92     if ($form->{"old$name"} ne qq|$form->{$name}--$form->{"${name}_id"}|) {
93
94       # this is needed for is, ir and oe
95       $form->{update} = 0;
96
97       # for credit calculations
98       $form->{oldinvtotal}  = 0;
99       $form->{oldtotalpaid} = 0;
100       $form->{calctax}      = 1;
101
102       # return one name or a list of names in $form->{name_list}
103       $i = $form->get_name(\%myconfig, $name);
104
105       if ($i > 1) {
106         if ($params{no_select}) {
107           # $locale->text('Customer')
108           # $locale->text('Vendor')
109           $form->error($locale->text("More than one #1 found matching, please be more specific.", $locale->text(ucfirst $name)));
110         } else {
111           &select_name($name);
112           ::end_of_request();
113         }
114       }
115
116       if ($i == 1) {
117
118         # we got one name
119         $form->{"${name}_id"} = $form->{name_list}[0]->{id};
120         $form->{$name}        = $form->{name_list}[0]->{name};
121         $form->{"old$name"}   = qq|$form->{$name}--$form->{"${name}_id"}|;
122
123         _reset_salesman_id();
124         delete @{ $form }{qw(payment_id)};
125
126         IS->get_customer(\%myconfig, \%$form) if ($name eq 'customer');
127         IR->get_vendor(\%myconfig, \%$form) if ($name eq 'vendor');
128
129       } else {
130
131         # name is not on file
132         # $locale->text('Customer not on file or locked!')
133         # $locale->text('Vendor not on file or locked!')
134         my $msg = ucfirst $name . " not on file or locked!";
135         $form->error($locale->text($msg));
136       }
137     }
138   }
139   $form->language_payment(\%myconfig);
140
141   $main::lxdebug->leave_sub();
142
143   return $i;
144 }
145
146 # $locale->text('Customer not on file!')
147 # $locale->text('Vendor not on file!')
148
149 sub select_name {
150   $main::lxdebug->enter_sub();
151
152   my $form     = $main::form;
153   my $locale   = $main::locale;
154
155   $main::auth->assert('general_ledger         | vendor_invoice_edit  | sales_order_edit    | invoice_edit | sales_delivery_order_edit |' .
156                 'request_quotation_edit | sales_quotation_edit | purchase_order_edit | cash');
157
158   my ($table) = @_;
159
160   my @column_index = qw(ndx name address);
161
162   my $label             = ucfirst $table;
163   my %column_data;
164   $column_data{ndx}  = qq|<th>&nbsp;</th>|;
165   $column_data{name} =
166     qq|<th class=listheading>| . $locale->text($label) . qq|</th>|;
167   $column_data{address} =
168     qq|<th class=listheading>| . $locale->text('Address') . qq|</th>|;
169
170   # list items with radio button on a form
171   $form->header;
172
173   my $title = $locale->text('Select from one of the names below');
174
175   print qq|
176     <h1>$title</h1>
177
178 <form method=post action=$form->{script}>
179
180 <table width=100%>
181   <tr>
182     <td>
183       <table width=100%>
184         <tr class=listheading>|;
185
186   map { print "\n$column_data{$_}" } @column_index;
187
188   print qq|
189         </tr>
190 |;
191
192   my $i = 0;
193   my $j;
194   foreach my $ref (@{ $form->{name_list} }) {
195     my $checked = ($i++) ? "" : "checked";
196
197     $ref->{name} =~ s/\"/&quot;/g;
198
199     $column_data{ndx} =
200       qq|<td><input name=ndx class=radio type=radio value=$i $checked></td>|;
201     $column_data{name} =
202       qq|<td><input name="new_name_$i" type=hidden value="$ref->{name}">$ref->{name}</td>|;
203     $column_data{address} = qq|<td>$ref->{address}&nbsp;</td>|;
204
205     $j++;
206     $j %= 2;
207     print qq|
208         <tr class=listrow$j>|;
209
210     map { print "\n$column_data{$_}" } @column_index;
211
212     print qq|
213         </tr>
214
215 <input name="new_id_$i" type=hidden value=$ref->{id}>
216
217 |;
218
219   }
220
221   print qq|
222       </table>
223     </td>
224   </tr>
225   <tr>
226     <td><hr size=3 noshade></td>
227   </tr>
228 </table>
229
230 <input name=lastndx type=hidden value=$i>
231
232 |;
233
234   # delete variables
235   map { delete $form->{$_} } qw(action name_list header);
236
237   # save all other form variables
238   foreach my $key (keys %${form}) {
239     next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
240     $form->{$key} =~ s/\"/&quot;/g;
241     print qq|<input name=$key type=hidden value="$form->{$key}">\n|;
242   }
243
244   print qq|
245 <input type=hidden name=nextsub value=name_selected>
246
247 <input type=hidden name=vc value=$table>
248 <br>
249 <input class=submit type=submit name=action value="|
250     . $locale->text('Continue') . qq|">
251 </form>
252 |;
253
254   $main::lxdebug->leave_sub();
255 }
256
257 sub name_selected {
258   $main::lxdebug->enter_sub();
259
260   my $form     = $main::form;
261   my %myconfig = %main::myconfig;
262
263   $main::auth->assert('general_ledger         | vendor_invoice_edit  | sales_order_edit    | invoice_edit | sales_delivery_order_edit | ' .
264                 'request_quotation_edit | sales_quotation_edit | purchase_order_edit | cash');
265
266   # replace the variable with the one checked
267
268   # index for new item
269   my $i = $form->{ndx};
270
271   _reset_salesman_id();
272
273   $form->{ $form->{vc} }    = $form->{"new_name_$i"};
274   $form->{"$form->{vc}_id"} = $form->{"new_id_$i"};
275   $form->{"old$form->{vc}"} =
276     qq|$form->{$form->{vc}}--$form->{"$form->{vc}_id"}|;
277
278   # delete all the new_ variables
279   for $i (1 .. $form->{lastndx}) {
280     map { delete $form->{"new_${_}_$i"} } qw(id name);
281   }
282
283   map { delete $form->{$_} } qw(ndx lastndx nextsub);
284
285   IS->get_customer(\%myconfig, \%$form) if ($form->{vc} eq 'customer');
286   IR->get_vendor(\%myconfig, \%$form) if ($form->{vc} eq 'vendor');
287
288   &update(1);
289
290   $main::lxdebug->leave_sub();
291 }
292
293 # Reset the $::form field 'salesman_id' to the ID of the currently
294 # logged in user. Useful when changing to a customer/vendor that has
295 # no salesman listed in their master data.
296 sub _reset_salesman_id {
297   my $current_employee   = SL::DB::Manager::Employee->current;
298   $::form->{salesman_id} = $current_employee->id if $current_employee && exists $::form->{salesman_id};
299 }
300
301 sub select_project {
302   $::lxdebug->enter_sub;
303
304   $::auth->assert('general_ledger         | vendor_invoice_edit  | sales_order_edit    | invoice_edit |' .
305                   'request_quotation_edit | sales_quotation_edit | purchase_order_edit | cash         | report');
306
307   my ($is_global, $nextsub) = @_;
308   my $project_list = delete $::form->{project_list};
309
310   map { delete $::form->{$_} } qw(action header update);
311
312   my @hiddens;
313   for my $key (keys %$::form) {
314     next if $key eq 'login' || $key eq 'password' || '' ne ref $::form->{$key};
315     push @hiddens, { key => $key, value => $::form->{$key} };
316   }
317   push @hiddens, { key => 'is_global',                value => $is_global },
318                  { key => 'project_selected_nextsub', value => $nextsub };
319
320   $::form->header;
321   print $::form->parse_html_template('arap/select_project', { hiddens => \@hiddens, project_list => $project_list });
322
323   $::lxdebug->leave_sub;
324 }
325
326 sub project_selected {
327   $main::lxdebug->enter_sub();
328
329   my $form     = $main::form;
330
331   $main::auth->assert('general_ledger         | vendor_invoice_edit  | sales_order_edit    | invoice_edit |' .
332                 'request_quotation_edit | sales_quotation_edit | purchase_order_edit | cash         | report');
333
334   # replace the variable with the one checked
335
336   # index for new item
337   my $i = $form->{ndx};
338
339   my $prefix = $form->{"is_global"} ? "global" : "";
340   my $suffix = $form->{"is_global"} ? "" : "_$form->{rownumber}";
341
342   $form->{"${prefix}projectnumber${suffix}"} =
343     $form->{"new_projectnumber_$i"};
344   $form->{"old${prefix}projectnumber${suffix}"} =
345     $form->{"new_projectnumber_$i"};
346   $form->{"${prefix}project_id${suffix}"} = $form->{"new_id_$i"};
347
348   # delete all the new_ variables
349   for $i (1 .. $form->{lastndx}) {
350     map { delete $form->{"new_${_}_$i"} } qw(id projectnumber description);
351   }
352
353   my $nextsub = $form->{project_selected_nextsub} || 'update';
354
355   map { delete $form->{$_} } qw(ndx lastndx nextsub is_global project_selected_nextsub);
356
357   call_sub($nextsub);
358
359   $main::lxdebug->leave_sub();
360 }
361
362 sub continue       { call_sub($main::form->{"nextsub"}); }
363
364 1;
365
366 __END__
367
368 =head1 NAME
369
370 arap.pl - helper functions or customer/vendor retrieval
371
372 =head1 SYNOPSIS
373
374  check_name('vendor')
375
376 =head1 DESCRIPTION
377
378 Don't use anyting in this file without extreme care, and even then be prepared for massive headaches.
379
380 It's a collection of helper routines that wrap the customer/vendor dropdown/textfield duality into something even complexer.
381
382 =head1 FUNCTIONS
383
384 =head2 check_name customer|vendor
385
386 check_name was originally meant to update the selected customer or vendor. The
387 way it does that has generted more hate than almost any other part of this
388 software.
389
390 What it does is:
391
392 =over 4
393
394 =item *
395
396 It checks if a vendor or customer is given. No failsafe, vendor fallback if
397 $_[0] is something fancy.
398
399 =item *
400
401 It assumes, that there is a field named customer or vendor in $form.
402
403 =item *
404
405 It assumes, that this field is filled with name--id, and tries to split that.
406 sql ledger uses that combination to get ids into the select keys.
407
408 =item *
409
410 It looks for a field selectcustomer or selectvendor in $form. sql ledger used
411 to store a copy of the html select in there. (again, don't ask)
412
413 =item *
414
415 If this field exists, it looks for a field called oldcustomer or oldvendor, in
416 which the old name--id string was stored in sql ledger, and compares those.
417
418 =item *
419
420 if they don't match, it will set customer_id or vendor_id in $form, load the
421 entry (which will clobber everything in $form named like a column in customer
422 oder vendor) and return.
423
424 =item *
425
426 If there was no select* entry, it assumes that vclimit was lower than the
427 number of entries, and that an input field was generated. In that case the
428 splitting is omitted (since users don't generally include ids in entered names)
429
430 =item *
431
432 It looks for a *_id field, and combines it with the given input into a name--id
433 entry and compares it to the old* entry. (Missing any of these will instantly
434 break check_namea.
435
436 =item *
437
438 If those do not match, $form->get_name is called to get matching results.
439 get_name only matches by *number and name, not by id, don't try to get it to do
440 so.
441
442 =item *
443
444 The results are stored in $form>{name_list} but a count is returned, and
445 checked.
446
447 =item *
448
449 If only one result was found, *_id, * and old* are copied into $form, the entry
450 is loaded (like above, clobbering)
451
452 =item *
453
454 If there is more than one, a selection dialog is rendered
455
456 =item *
457
458 If none is found, an error is generated.
459
460 =back
461
462 =head3 I built a customer/vendor box somewhere and it doesn't work, what's wrong?
463
464 Make sure a select* field is given if and only if you render a select box. The
465 actual contents are ignored, but recognition fails if not present.
466
467 Make sure old* and *_id fields are set correctly (name--id form for old*). They
468 are necessary in all steps and branches.
469
470 Since get_customer and get_vendor clobber a lot of fields, make sure what
471 changes exactly.
472
473 =head3 select- version works fine, but things go awry when I use a textbox, any idea?
474
475 If there is more than one match, check_name will display a select form, that
476 will redirect to the original C<nextsub>. Unfortunately any hidden vars or
477 input fields will be lost in the process unless saved before in a callback.
478
479 If you still want to use it, you can disable this feature, like this:
480
481   check_name('customer', no_select => 1)
482
483 In that case multiple matches will trigger an error.
484
485 Otherwise you'll have to care to include a complete state in callback.
486
487 =cut