Umstellung der Benutzerverwaltung von Dateien im Verzeichnis "users" auf die Verwendu...
[kivitendo-erp.git] / bin / mozilla / oe.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) 1998-2003
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 # Order entry module
31 # Quotation module
32 #======================================================================
33
34 use POSIX qw(strftime);
35
36 use SL::OE;
37 use SL::IR;
38 use SL::IS;
39 use SL::PE;
40 use SL::ReportGenerator;
41 use List::Util qw(max reduce);
42
43 require "bin/mozilla/io.pl";
44 require "bin/mozilla/arap.pl";
45 require "bin/mozilla/reportgenerator.pl";
46
47 1;
48
49 # end of main
50
51 # For locales.pl:
52 # $locale->text('Edit the purchase_order');
53 # $locale->text('Edit the sales_order');
54 # $locale->text('Edit the request_quotation');
55 # $locale->text('Edit the sales_quotation');
56
57 # $locale->text('Workflow purchase_order');
58 # $locale->text('Workflow sales_order');
59 # $locale->text('Workflow request_quotation');
60 # $locale->text('Workflow sales_quotation');
61
62 my $oe_access_map = {
63   'sales_order'       => 'sales_order_edit',
64   'purchase_order'    => 'purchase_order_edit',
65   'request_quotation' => 'request_quotation_edit',
66   'sales_quotation'   => 'sales_quotation_edit',
67 };
68
69 sub check_oe_access {
70   my $right   = $oe_access_map->{$form->{type}};
71   $right    ||= 'DOES_NOT_EXIST';
72
73   $auth->assert($right);
74 }
75
76 sub set_headings {
77   $lxdebug->enter_sub();
78
79   check_oe_access();
80
81   my ($action) = @_;
82
83   if ($form->{type} eq 'purchase_order') {
84     $form->{title}   = $action eq "edit" ?
85       $locale->text('Edit Purchase Order') :
86       $locale->text('Add Purchase Order');
87     $form->{heading} = $locale->text('Purchase Order');
88     $form->{vc}      = 'vendor';
89   }
90   if ($form->{type} eq 'sales_order') {
91     $form->{title}   = $action eq "edit" ?
92       $locale->text('Edit Sales Order') :
93       $locale->text('Add Sales Order');
94     $form->{heading} = $locale->text('Sales Order');
95     $form->{vc}      = 'customer';
96   }
97   if ($form->{type} eq 'request_quotation') {
98     $form->{title}   = $action eq "edit" ?
99       $locale->text('Edit Request for Quotation') :
100       $locale->text('Add Request for Quotation');
101     $form->{heading} = $locale->text('Request for Quotation');
102     $form->{vc}      = 'vendor';
103   }
104   if ($form->{type} eq 'sales_quotation') {
105     $form->{title}   = $action eq "edit" ?
106       $locale->text('Edit Quotation') :
107       $locale->text('Add Quotation');
108     $form->{heading} = $locale->text('Quotation');
109     $form->{vc}      = 'customer';
110   }
111
112   $lxdebug->leave_sub();
113 }
114
115 sub add {
116   $lxdebug->enter_sub();
117
118   check_oe_access();
119
120   set_headings("add");
121
122   $form->{callback} =
123     "$form->{script}?action=add&type=$form->{type}&vc=$form->{vc}"
124     unless $form->{callback};
125
126   &order_links;
127   &prepare_order;
128   &display_form;
129
130   $lxdebug->leave_sub();
131 }
132
133 sub edit {
134   $lxdebug->enter_sub();
135
136   check_oe_access();
137
138   # show history button
139   $form->{javascript} = qq|<script type="text/javascript" src="js/show_history.js"></script>|;
140   #/show hhistory button
141
142   $form->{simple_save} = 0;
143
144   set_headings("edit");
145
146   # editing without stuff to edit? try adding it first
147   if ($form->{rowcount} && !$form->{print_and_save}) {
148     map { $id++ if $form->{"multi_id_$_"} } (1 .. $form->{rowcount});
149     if (!$id) {
150
151       # reset rowcount
152       undef $form->{rowcount};
153       &add;
154       $lxdebug->leave_sub();
155       return;
156     }
157   } elsif (!$form->{id}) {
158     &add;
159     $lxdebug->leave_sub();
160     return;
161   }
162
163   if ($form->{print_and_save}) {
164     $form->{action}   = "print";
165     $form->{resubmit} = 1;
166     $language_id = $form->{language_id};
167     $printer_id = $form->{printer_id};
168   }
169
170   set_headings("edit");
171
172   &order_links;
173   &prepare_order;
174   if ($form->{print_and_save}) {
175     $form->{language_id} = $language_id;
176     $form->{printer_id} = $printer_id;
177   }
178   &display_form;
179
180   $lxdebug->leave_sub();
181 }
182
183 sub order_links {
184   $lxdebug->enter_sub();
185
186   check_oe_access();
187
188   # get customer/vendor
189   $form->all_vc(\%myconfig, $form->{vc}, ($form->{vc} eq 'customer') ? "AR" : "AP");
190
191   # retrieve order/quotation
192   $form->{webdav}   = $webdav;
193   $form->{jsscript} = 1;
194
195   my $editing = $form->{id};
196
197   OE->retrieve(\%myconfig, \%$form);
198
199   if ($form->{payment_id}) {
200     $payment_id = $form->{payment_id};
201   }
202   if ($form->{language_id}) {
203     $language_id = $form->{language_id};
204   }
205   if ($form->{taxzone_id}) {
206     $taxzone_id = $form->{taxzone_id};
207   }
208
209   $salesman_id = $form->{salesman_id} if ($editing);
210
211
212   # if multiple rowcounts (== collective order) then check if the
213   # there were more than one customer (in that case OE::retrieve removes
214   # the content from the field)
215   if (   $form->{rowcount}
216       && $form->{type} eq 'sales_order'
217       && defined $form->{customer}
218       && $form->{customer} eq '') {
219
220     #    $main::lxdebug->message(0, "Detected Edit order with concurrent customers");
221     $form->error(
222                  $locale->text(
223                    'Collective Orders only work for orders from one customer!')
224     );
225   }
226
227   $taxincluded = $form->{taxincluded};
228   $form->{shipto} = 1 if $form->{id};
229
230   if ($form->{"all_$form->{vc}"}) {
231     unless ($form->{"$form->{vc}_id"}) {
232       $form->{"$form->{vc}_id"} = $form->{"all_$form->{vc}"}->[0]->{id};
233     }
234   }
235
236   $cp_id    = $form->{cp_id};
237   $intnotes = $form->{intnotes};
238
239   # get customer / vendor
240   if ($form->{type} =~ /(purchase_order|request_quotation)/) {
241     IR->get_vendor(\%myconfig, \%$form);
242
243     #quote all_vendor Bug 133
244     foreach $ref (@{ $form->{all_vendor} }) {
245       $ref->{name} = $form->quote($ref->{name});
246     }
247
248   }
249   if ($form->{type} =~ /sales_(order|quotation)/) {
250     IS->get_customer(\%myconfig, \%$form);
251
252     #quote all_vendor Bug 133
253     foreach $ref (@{ $form->{all_customer} }) {
254       $ref->{name} = $form->quote($ref->{name});
255     }
256
257   }
258   $form->{cp_id} = $cp_id;
259
260   if ($payment_id) {
261     $form->{payment_id} = $payment_id;
262   }
263   if ($language_id) {
264     $form->{language_id} = $language_id;
265   }
266   if ($taxzone_id) {
267     $form->{taxzone_id} = $taxzone_id;
268   }
269   $form->{intnotes} = $intnotes if $intnotes;
270   ($form->{ $form->{vc} }) = split /--/, $form->{ $form->{vc} };
271   $form->{"old$form->{vc}"} = qq|$form->{$form->{vc}}--$form->{"$form->{vc}_id"}|;
272
273   # build the popup menus
274   if (@{ $form->{"all_$form->{vc}"} }) {
275     $form->{ $form->{vc} } =
276       qq|$form->{$form->{vc}}--$form->{"$form->{vc}_id"}|;
277     map { $form->{"select$form->{vc}"} .=
278 "<option>$_->{name}--$_->{id}</option>\n" }
279       (@{ $form->{"all_$form->{vc}"} });
280   }
281
282   $form->{taxincluded} = $taxincluded if ($form->{id});
283
284   # departments
285   if (@{ $form->{all_departments} }) {
286     $form->{selectdepartment} = "<option>\n";
287     $form->{department}       = "$form->{department}--$form->{department_id}";
288
289     map {
290       $form->{selectdepartment} .= "<option>$_->{description}--$_->{id}</option>\n"
291     } @{ $form->{all_departments} };
292   }
293
294   $form->{employee} = "$form->{employee}--$form->{employee_id}";
295
296   # forex
297   $form->{forex} = $form->{exchangerate};
298
299   $form->{salesman_id} = $salesman_id if ($editing);
300
301   $lxdebug->leave_sub();
302 }
303
304 sub prepare_order {
305   $lxdebug->enter_sub();
306
307   check_oe_access();
308
309   $form->{formname} = $form->{type} unless $form->{formname};
310
311   my $i = 0;
312   foreach $ref (@{ $form->{form_details} }) {
313     $form->{rowcount} = ++$i;
314     map { $form->{"${_}_$i"} = $ref->{$_} } keys %{$ref};
315   }
316   for my $i (1 .. $form->{rowcount}) {
317     $form->{"reqdate_$i"}   = $form->{"deliverydate_$i"} unless $form->{"reqdate_$i"};
318     $form->{"discount_$i"}  = $form->format_amount(\%myconfig, $form->{"discount_$i"} * ($form->{id} ? 100 : 1));
319     $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"});
320     $form->{"qty_$i"}       = $form->format_amount(\%myconfig, $form->{"qty_$i"});
321     map { $form->{"${_}_$i"} =~ s/\"/&quot;/g } qw(partnumber description unit);
322   }
323
324   $lxdebug->leave_sub();
325 }
326
327 sub form_header {
328   $lxdebug->enter_sub();
329   my @custom_hiddens;
330
331   check_oe_access();
332
333   # Container for template variables. Unfortunately this has to be visible in form_footer too, so not my.
334   our %TMPL_VAR = ();
335
336   $form->{employee_id} = $form->{old_employee_id} if $form->{old_employee_id};
337   $form->{salesman_id} = $form->{old_salesman_id} if $form->{old_salesman_id};
338
339   $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
340
341   $form->{employee_id} = $form->{old_employee_id} if $form->{old_employee_id};
342   $form->{salesman_id} = $form->{old_salesman_id} if $form->{old_salesman_id};
343
344   map { $form->{$_} = H($form->{$_}) }
345     qw(shippingpoint shipvia notes intnotes shiptoname
346        shiptostreet shiptozipcode shiptocity shiptocountry shiptocontact
347        shiptophone shiptofax shiptodepartment_1 shiptodepartment_2);
348  
349   # use JavaScript Calendar or not
350   $form->{jsscript} = 1;
351   $TMPL_VAR{button1} = qq|
352      <td nowrap><input name=transdate id=transdate size=11 title="$myconfig{dateformat}" value="$form->{transdate}" onBlur=\"check_right_date_format(this)\">
353       <input type=button name=transdate id="trigger1" value=| . $locale->text('button') . qq|></td>
354     |;
355   $TMPL_VAR{button2} = qq|
356      <td nowrap width="13"><input name=reqdate id=reqdate size=11 title="$myconfig{dateformat}" value="$form->{reqdate}" onBlur=\"check_right_date_format(this)\">
357       <input type=button name=reqdate name=reqdate id="trigger2" value=| . $locale->text('button') . qq|></td>
358    |;
359   #write Trigger
360   $TMPL_VAR{jsscript} = Form->write_trigger(\%myconfig, "2", "transdate", "BL", "trigger1", "reqdate", "BL", "trigger2");
361
362   # openclosed checkboxes
363   my @tmp;
364   push @tmp, sprintf qq|<input name="delivered" id="delivered" type="checkbox" class="checkbox" value="1" %s><label for="delivered">%s</label>|,
365                         $form->{"delivered"} ? "checked" : "",  $locale->text('Delivered') if $form->{"type"} =~ /_order$/;
366   push @tmp, sprintf qq|<input name="closed" id="closed" type="checkbox" class="checkbox" value="1" %s><label for="closed">%s</label>|,
367                         $form->{"closed"}    ? "checked" : "",  $locale->text('Closed')    if $form->{id};
368   $TMPL_VAR{openclosed} = sprintf qq|<tr><td colspan=%d align=center>%s</td></tr>\n|, 2 * scalar @tmp, join "\n", @tmp if @tmp;
369
370   # set option selected
371   foreach $item ($form->{vc}, 'currency', 'department', ($form->{vc} eq "customer" ? 'customer' : 'vendor')) {
372     $form->{"select$item"} =~ s/ selected//;
373     $form->{"select$item"} =~ s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
374   }
375
376   #quote select[customer|vendor] Bug 133
377   $form->{"select$form->{vc}"} = $form->quote($form->{"select$form->{vc}"});
378
379   #substitute \n and \r to \s (bug 543)
380   $form->{"select$form->{vc}"} =~ s/[\n\r]/&nbsp;/g;
381   
382   my @old_project_ids = ($form->{"globalproject_id"}, grep { $_ } map { $form->{"project_id_$_"} } 1..$form->{"rowcount"});
383
384   my $vc = $form->{vc} eq "customer" ? "customers" : "vendors";
385   $form->get_lists("contacts"      => "ALL_CONTACTS",
386                    "shipto"        => "ALL_SHIPTO",
387                    "projects"      => { "key"      => "ALL_PROJECTS",
388                                         "all"      => 0,
389                                         "old_id"   => \@old_project_ids },
390                    "employees"     => "ALL_EMPLOYEES",
391                    "salesmen"      => "ALL_SALESMEN",
392                    "taxzones"      => "ALL_TAXZONES",
393                    "payments"      => "ALL_PAYMENTS",
394                    "currencies"    => "ALL_CURRENCIES",
395                    $vc             => { key   => "ALL_" . uc($vc),
396                                         limit => $myconfig{vclimit} + 1 },
397                    "price_factors" => "ALL_PRICE_FACTORS");
398
399   # label subs
400   $TMPL_VAR{sales_employee_labels} = sub { $_[0]->{name} || $_[0]->{login} };
401   $TMPL_VAR{shipto_labels} = sub { join "; ", grep { $_ } map { $_[0]->{"shipto${_}" } } qw(name department_1 street city) };
402   $TMPL_VAR{contact_labels} = sub { $_[0]->{"cp_name"} . ($_[0]->{cp_abteilung} ? " ($_[0]->{cp_abteilung})" : "") };
403
404   # vendor/customer
405   $TMPL_VAR{vc_keys} = sub { "$_[0]->{name}--$_[0]->{id}" };
406   $TMPL_VAR{vclimit} = $myconfig{vclimit};
407   $TMPL_VAR{vc_select} = "customer_or_vendor_selection_window('$form->{vc}', '', @{[ $form->{vc} eq 'vendor' ? 1 : 0 ]}, 0)";
408   push @custom_hiddens, "$form->{vc}_id";
409   push @custom_hiddens, "old$form->{vc}";
410   push @custom_hiddens, "select$form->{vc}";
411
412   # currencies and exchangerate
413   @values = map { $_ } @{ $form->{ALL_CURRENCIES} };
414   %labels = map { $_ => $_ } @{ $form->{ALL_CURRENCIES} };
415   $form->{currency}            = $form->{defaultcurrency} unless $form->{currency};
416   $TMPL_VAR{show_exchangerate} = $form->{currency} ne $form->{defaultcurrency} && $form->{exchangerate};
417   $TMPL_VAR{currencies}        = NTI($cgi->popup_menu('-name' => 'currency', '-default' => $form->{"currency"},
418                                                       '-values' => \@values, '-labels' => \%labels)) if scalar @values;
419   push @custom_hiddens, "forex";
420   push @custom_hiddens, "exchangerate" if $form->{forex};
421
422   # credit remaining
423   $creditwarning = (($form->{creditlimit} != 0) && ($form->{creditremaining} < 0) && !$form->{update}) ? 1 : 0;
424   $TMPL_VAR{is_credit_remaining_negativ} = ($form->{creditremaining} =~ /-/) ? "0" : "1";
425   
426   # business
427   $TMPL_VAR{business_label} = ($form->{vc} eq "customer" ? $locale->text('Customer type') : $locale->text('Vendor type'));
428
429   push @custom_hiddens, "ordnumber"      if $form->{type} =~ /_quotation$/;
430   push @custom_hiddens, "customer_klass" if $form->{vc} eq 'customer';
431
432   $credittext = $locale->text('Credit Limit exceeded!!!');
433
434   $onload = ($form->{resubmit} && ($form->{format} eq "html")) ? "window.open('about:blank','Beleg'); document.oe.target = 'Beleg';document.oe.submit()"
435           : ($form->{resubmit})                                ? "document.oe.submit()"
436           : ($creditwarning)                                   ? "alert('$credittext')"
437           :                                                      "focus()";
438
439   $onload .= qq|;setupDateFormat('|. $myconfig{dateformat} .qq|', '|. $locale->text("Falsches Datumsformat!") .qq|')|;
440   $onload .= qq|;setupPoints('|.   $myconfig{numberformat} .qq|', '|. $locale->text("wrongformat") .qq|')|;
441   $TMPL_VAR{onload} = $onload;
442   
443   $form->{javascript} .= qq|<script type="text/javascript" src="js/show_form_details.js"></script>|;
444   $form->{javascript} .= qq|<script type="text/javascript" src="js/show_history.js"></script>|;
445   $form->{javascript} .= qq|<script type="text/javascript" src="js/show_vc_details.js"></script>|;
446
447   $form->header;
448
449   $TMPL_VAR{HIDDENS} = [ map { name => $_, value => $form->{$_} },
450      qw(id action type vc formname media format proforma queued printed emailed
451         title creditlimit creditremaining tradediscount business
452         max_dunning_level dunning_amount shiptoname shiptostreet shiptozipcode
453         shiptocity shiptocountry shiptocontact shiptophone shiptofax
454         shiptodepartment_1 shiptodepartment_2 shiptoemail
455         message email subject cc bcc taxpart taxservice taxaccounts),
456         @custom_hiddens,
457         map { $_.'_rate', $_.'_description' } split / /, $form->{taxaccounts} ];  # deleted: discount
458
459   %TMPL_VAR = (
460      %TMPL_VAR,
461      is_sales        => scalar ($form->{type} =~ /^sales_/),              # these vars are exported, so that the template
462      is_order        => scalar ($form->{type} =~ /_order$/),              # may determine what to show
463      is_sales_quo    => scalar ($form->{type} =~ /sales_quotation$/),
464      is_req_quo      => scalar ($form->{type} =~ /request_quotation$/),
465      is_sales_ord    => scalar ($form->{type} =~ /sales_order$/),
466      is_pur_ord      => scalar ($form->{type} =~ /purchase_order$/),
467   );
468
469   print $form->parse_html_template("oe/orders_header", { %TMPL_VAR });
470
471   $lxdebug->leave_sub();
472 }
473
474 sub form_footer {
475   $lxdebug->enter_sub();
476
477   check_oe_access();
478
479   $form->{invtotal} = $form->{invsubtotal};
480
481   $rows    = max 2, $form->numtextrows($form->{notes}, 25, 8);
482   $introws = max 2, $form->numtextrows($form->{intnotes}, 35, 8);
483   $rows    = max $rows, $introws;
484
485   $TMPL_VAR{notes} = qq|<textarea name=notes rows=$rows cols=25 wrap=soft>$form->{notes}</textarea>|;
486   $TMPL_VAR{intnotes} = qq|<textarea name=intnotes rows=$rows cols=35 wrap=soft>$form->{intnotes}</textarea>|;
487
488   if (!$form->{taxincluded}) {
489
490     foreach $item (split / /, $form->{taxaccounts}) {
491       if ($form->{"${item}_base"}) {
492         $form->{invtotal} += $form->{"${item}_total"} = $form->round_amount( $form->{"${item}_base"} * $form->{"${item}_rate"}, 2);
493         $form->{"${item}_total"} = $form->format_amount(\%myconfig, $form->{"${item}_total"}, 2);
494
495         $TMPL_VAR{tax} .= qq|
496               <tr>
497                 <th align=right>$form->{"${item}_description"}&nbsp;| . $form->{"${item}_rate"} * 100 .qq|%</th>
498                 <td align=right>$form->{"${item}_total"}</td>
499               </tr> |;
500       }
501     }
502
503     $form->{invsubtotal} = $form->format_amount(\%myconfig, $form->{invsubtotal}, 2, 0);
504
505   } else {
506     foreach $item (split / /, $form->{taxaccounts}) {
507       if ($form->{"${item}_base"}) {
508         $form->{"${item}_total"} = $form->round_amount( ($form->{"${item}_base"} * $form->{"${item}_rate"} / (1 + $form->{"${item}_rate"})), 2);
509         $form->{"${item}_netto"} = $form->round_amount( ($form->{"${item}_base"} - $form->{"${item}_total"}), 2);
510         $form->{"${item}_total"} = $form->format_amount(\%myconfig, $form->{"${item}_total"}, 2);
511         $form->{"${item}_netto"} = $form->format_amount(\%myconfig, $form->{"${item}_netto"}, 2);
512
513         $TMPL_VAR{tax} .= qq|
514               <tr>
515                 <th align=right>Enthaltene $form->{"${item}_description"}&nbsp;| . $form->{"${item}_rate"} * 100 .qq|%</th>
516                 <td align=right>$form->{"${item}_total"}</td>
517               </tr>
518               <tr>
519                 <th align=right>Nettobetrag</th>
520                 <td align=right>$form->{"${item}_netto"}</td>
521               </tr> |;
522       }
523     }
524   }
525
526   $form->{oldinvtotal} = $form->{invtotal};
527
528   print $form->parse_html_template("oe/orders_footer", {
529      %TMPL_VAR,
530      webdav          => $webdav,
531      print_options   => print_options(inline => 1),
532      label_edit      => $locale->text("Edit the $form->{type}"),
533      label_workflow  => $locale->text("Workflow $form->{type}"),
534   });
535
536   $lxdebug->leave_sub();
537 }
538
539 sub update {
540   $lxdebug->enter_sub();
541   
542   my ($recursive_call) = shift;
543
544   check_oe_access();
545
546   set_headings($form->{"id"} ? "edit" : "add");
547
548   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) } qw(exchangerate creditlimit creditremaining) unless $recursive_call;
549   $form->{update} = 1;
550       
551   $payment_id = $form->{payment_id} if $form->{payment_id};
552   
553   &check_name($form->{vc});
554   
555   $form->{payment_id} = $payment_id if $form->{payment_id} eq "";
556   
557   $buysell              = 'buy';
558   $buysell              = 'sell' if ($form->{vc} eq 'vendor');
559   $form->{exchangerate} = $exchangerate if 
560     $form->{forex} = $exchangerate = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{transdate}, $buysell);
561
562   $exchangerate = $form->{exchangerate} || 1;
563
564 ##################### process items ######################################
565   # for pricegroups
566   $i = $form->{rowcount};
567   if (   ($form->{"partnumber_$i"} eq "")
568       && ($form->{"description_$i"} eq "")
569       && ($form->{"partsgroup_$i"}  eq "")) {
570
571     $form->{creditremaining} += ($form->{oldinvtotal} - $form->{oldtotalpaid});
572     &check_form;
573
574   } else {
575
576     if ($form->{type} =~ /^sales/) {
577       IS->retrieve_item(\%myconfig, \%$form);
578     } else {
579       IR->retrieve_item(\%myconfig, \%$form);
580     }
581
582     my $rows = scalar @{ $form->{item_list} };
583
584     $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{discount} * 100);
585
586     if ($rows) {
587       $form->{"qty_$i"} = 1 unless ($form->{"qty_$i"});
588
589       if ($rows > 1) {
590
591         &select_item;
592         exit;
593
594       } else {
595
596         $sellprice             = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
597         $form->{"discount_$i"} = 0 if $form->{"not_discountable_$i"};
598         map { $form->{item_list}[$i]{$_} =~ s/\"/&quot;/g }    qw(partnumber description unit);
599         map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} } keys %{ $form->{item_list}[0] };
600         $form->{payment_id} = $form->{"part_payment_id_$i"} if $form->{"part_payment_id_$i"} ne "";
601
602         $form->{"marge_price_factor_$i"} = $form->{item_list}->[0]->{price_factor};
603
604         ($sellprice || $form->{"sellprice_$i"}) =~ /\.(\d+)/;
605         $decimalplaces = max 2, length $1;
606
607         if ($sellprice) {
608           $form->{"sellprice_$i"} = $sellprice;
609         } else {
610           $form->{"sellprice_$i"} *= (1 - $form->{tradediscount});
611           $form->{"sellprice_$i"} /= $exchangerate;   # if there is an exchange rate adjust sellprice
612         }
613
614         $amount = $form->{"sellprice_$i"} * $form->{"qty_$i"} * (1 - $form->{"discount_$i"} / 100);
615         map { $form->{"${_}_base"} = 0 }                                 split / /, $form->{taxaccounts};
616         map { $form->{"${_}_base"} += $amount }                          split / /, $form->{"taxaccounts_$i"};
617         map { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) } split / /, $form->{taxaccounts} if !$form->{taxincluded};
618
619         $form->{creditremaining} -= $amount;
620
621         $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
622         $form->{"qty_$i"}       = $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
623
624         # get pricegroups for parts
625         IS->get_pricegroups_for_parts(\%myconfig, \%$form);
626
627         # build up html code for prices_$i
628         &set_pricegroup($i);
629       }
630
631       display_form();
632     } else {
633
634       # ok, so this is a new part
635       # ask if it is a part or service item
636
637       if (   $form->{"partsgroup_$i"}
638           && ($form->{"partsnumber_$i"} eq "")
639           && ($form->{"description_$i"} eq "")) {
640         $form->{rowcount}--;
641         $form->{"discount_$i"} = "";
642
643         display_form();
644       } else {
645         $form->{"id_$i"}   = 0;
646         new_item();
647       }
648     }
649   }
650 ##################### process items ######################################
651
652
653   $lxdebug->leave_sub();
654 }
655
656 sub search {
657   $lxdebug->enter_sub();
658
659   check_oe_access();
660
661   if ($form->{type} eq 'purchase_order') {
662     $form->{title} = $locale->text('Purchase Orders');
663     $form->{vc}    = 'vendor';
664     $ordlabel      = $locale->text('Order Number');
665     $ordnumber     = 'ordnumber';
666     $employee      = $locale->text('Employee');
667   }
668
669   if ($form->{type} eq 'request_quotation') {
670     $form->{title} = $locale->text('Request for Quotations');
671     $form->{vc}    = 'vendor';
672     $ordlabel      = $locale->text('RFQ Number');
673     $ordnumber     = 'quonumber';
674     $employee      = $locale->text('Employee');
675   }
676
677   if ($form->{type} eq 'sales_order') {
678     $form->{title} = $locale->text('Sales Orders');
679     $form->{vc}    = 'customer';
680     $ordlabel      = $locale->text('Order Number');
681     $ordnumber     = 'ordnumber';
682     $employee      = $locale->text('Employee');
683   }
684
685   if ($form->{type} eq 'sales_quotation') {
686     $form->{title} = $locale->text('Quotations');
687     $form->{vc}    = 'customer';
688     $ordlabel      = $locale->text('Quotation Number');
689     $ordnumber     = 'quonumber';
690     $employee      = $locale->text('Employee');
691   }
692
693   # setup vendor / customer selection
694   $form->all_vc(\%myconfig, $form->{vc},
695                 ($form->{vc} eq 'customer') ? "AR" : "AP");
696
697   # departments
698   if (@{ $form->{all_departments} }) {
699     $form->{selectdepartment} = "<option>\n";
700
701     map {
702       $form->{selectdepartment} .=
703         "<option>$_->{description}--$_->{id}</option>\n"
704     } (@{ $form->{all_departments} });
705   }
706
707   $department = qq|
708         <tr>
709           <th align=right nowrap>| . $locale->text('Department') . qq|</th>
710           <td colspan=3><select name=department>$form->{selectdepartment}</select></td>
711         </tr>
712 | if $form->{selectdepartment};
713
714   my $delivered;
715   if (($form->{"type"} eq "sales_order") ||
716       ($form->{"type"} eq "purchase_order")) {
717     $delivered = qq|
718         <tr>
719           <td><input name="notdelivered" id="notdelivered" class="checkbox" type="checkbox" value="1" checked>
720             <label for="notdelivered">|. $locale->text('Not delivered') . qq|</label></td>
721           <td><input name="delivered" id="delivered" class="checkbox" type="checkbox" value="1" checked>
722             <label for="delivered">| . $locale->text('Delivered') . qq|</label></td>
723         </tr>
724 |;
725   }
726
727   # use JavaScript Calendar or not
728   $form->{jsscript} = 1;
729   $jsscript = "";
730
731   $button1 = qq|
732      <td><input name=transdatefrom id=transdatefrom size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">
733      <input type=button name=transdatefrom id="trigger3" value=|
734     . $locale->text('button') . qq|></td>
735     |;
736   $button2 = qq|
737      <td><input name=transdateto id=transdateto size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">
738      <input type=button name=transdateto name=transdateto id="trigger4" value=|
739     . $locale->text('button') . qq|></td>
740    |;
741
742   #write Trigger
743   $jsscript =
744     Form->write_trigger(\%myconfig, "2", "transdatefrom", "BR", "trigger3",
745                         "transdateto", "BL", "trigger4");
746
747   my $vc = $form->{vc} eq "customer" ? "customers" : "vendors";
748
749   $form->get_lists("projects"  => { "key" => "ALL_PROJECTS",
750                                     "all" => 1 },
751                    "employees" => "ALL_EMPLOYEES",
752                    "salesmen"  => "ALL_SALESMEN",
753                    $vc         => "ALL_" . uc($vc)
754                   );
755
756   my %labels = ();
757   my @values = ("");
758   foreach my $item (@{ $form->{"ALL_PROJECTS"} }) {
759     push(@values, $item->{"id"});
760     $labels{$item->{"id"}} = $item->{"projectnumber"};
761   }
762   my $projectnumber =
763     NTI($cgi->popup_menu('-name' => 'project_id', '-values' => \@values,
764                          '-labels' => \%labels));
765
766   #employees
767   %labels = ();
768   @values = ("");
769   foreach my $item (@{ $form->{"ALL_EMPLOYEES"} }) {
770     push(@values, $item->{"id"});
771     $labels{$item->{"id"}} = $item->{"name"} ne "" ? $item->{"name"} : $item->{"login"};
772   }
773   
774   #salesmen
775   my %labels_salesmen = ();
776   my @values_salesmen = ('');
777   foreach my $item (@{ $form->{"ALL_SALESMEN"} }) {
778     push(@values_salesmen, $item->{"id"});
779     $labels_salesmen{$item->{"id"}} = $item->{"name"} ne "" ? $item->{"name"} : $item->{"login"};
780   }
781
782   my $employee_block = qq|
783     <tr>
784       <th align="right">| . $locale->text('Employee') . qq|</th>
785       <td>| .  NTI($cgi->popup_menu('-name'   => 'employee_id', '-values' => \@values, '-labels' => \%labels)) . qq|</td>
786     </tr>
787     <tr>
788       <th align="right">| . $locale->text('Salesman') . qq|</th>
789       <td>| .
790         NTI($cgi->popup_menu('-name'   => 'salesman_id',
791                              '-values' => \@values_salesmen,
792                              '-labels' => \%labels_salesmen)) . qq|
793       </td>
794     </tr>|;
795
796   %labels = ();
797   @values = ("");
798
799   foreach my $item (@{ $form->{($form->{vc} eq "customer" ? "ALL_CUSTOMERS" : "ALL_VENDORS")}}) {
800     push(@values, $item->{name}.qq|--|.$item->{"id"});
801     $labels{$item->{name}.qq|--|.$item->{"id"}} = $item->{"name"};
802   }
803
804   my $vc_label = $form->{vc} eq "customer" ? $locale->text('Customer') : $locale->text('Vendor');
805   $vc =
806     $myconfig{vclimit} <=  scalar(@values)
807     ? qq|<input type="text" value="| . H(($form->{"old$form->{vc}"} =~ /^(.*)\-\-.*$/)) . qq|" name="$form->{vc}">| 
808     : NTI($cgi->popup_menu('-name' => "$form->{vc}",
809                            '-default' => $form->{"old$form->{vc}"},
810                            '-onChange' => 'document.getElementById(\'update_button\').click();',
811                            '-values' => \@values,
812                            '-labels' => \%labels));
813   $form->header;
814
815   print qq|
816 <body>
817
818 <form method=post action=$form->{script}>
819
820 <table width=100%>
821   <tr>
822     <th class=listtop>$form->{title}</th>
823   </tr>
824   <tr height="5"></tr>
825   <tr>
826     <td>
827       <table>
828         <tr>
829           <th align=right>$vc_label</th>
830           <td colspan=3>$vc</td>
831         </tr>
832 $department
833         <tr>
834           <th align=right>$ordlabel</th>
835           <td colspan=3><input name="$ordnumber" size=20></td>
836         </tr>
837 $employee_block
838         <tr>
839           <th align="right">| . $locale->text('Transaction description') . qq|</th>
840           <td colspan="3"><input name="transaction_description" size=20></td>
841         </tr>
842         <tr>
843           <th align="right">| . $locale->text("Project Number") . qq|</th>
844           <td colspan="3">$projectnumber</td>
845         </tr>
846         <tr>
847           <th align=right>| . $locale->text('From') . qq|</th> $button1
848           <th align=right>| . $locale->text('Bis') . qq|</th> $button2
849         </tr>
850         <input type=hidden name=sort value=transdate>
851         <tr>
852           <th align=right>| . $locale->text('Include in Report') . qq|</th>
853           <td colspan=5>
854             <table>
855         <tr>
856           <td><input type="checkbox" name="open" value="1" id="open" checked> <label for="open">| . $locale->text("Open") . qq|</td>
857           <td><input type="checkbox" name="closed" value="1" id="closed"> <label for="closed">| . $locale->text("Closed") . qq|</td>
858         </tr>
859         $delivered
860               <tr>
861                 <td><input name="l_id" class=checkbox type=checkbox value=Y> | . $locale->text('ID') . qq|</td>
862                 <td><input name="l_$ordnumber" class=checkbox type=checkbox value=Y checked> $ordlabel</td>
863               </tr>
864               <tr>
865                 <td><input name="l_transdate" class=checkbox type=checkbox value=Y checked> | . $locale->text('Date') . qq|</td>
866                 <td><input name="l_reqdate" class=checkbox type=checkbox value=Y checked> | . $locale->text('Required by') . qq|</td>
867               </tr>
868               <tr>
869                 <td><input name="l_name" class=checkbox type=checkbox value=Y checked> $vc_label</td>
870                 <td><input name="l_employee" class=checkbox type=checkbox value=Y checked> $employee</td>
871                 
872                 <td><input name="l_shipvia" class=checkbox type=checkbox value=Y> | . $locale->text('Ship via') . qq|</td>
873               </tr>
874               <tr>
875                 <td><input name="l_netamount" class=checkbox type=checkbox value=Y> | . $locale->text('Amount') . qq|</td>
876                 <td><input name="l_tax" class=checkbox type=checkbox value=Y> | . $locale->text('Tax') . qq|</td>
877                 <td><input name="l_amount" class=checkbox type=checkbox value=Y checked> | . $locale->text('Total') . qq|</td>
878               </tr>
879               <tr>
880                 <td><input name="l_marge_total" class=checkbox type=checkbox value=Y> | .             $locale->text('Ertrag') . qq|</td>
881                 <td><input name="l_marge_percent" class=checkbox type=checkbox value=Y> | .             $locale->text('Ertrag prozentual') . qq|</td>
882               </tr>
883               <tr>
884           <td><input name="l_globalprojectnumber" class=checkbox type=checkbox value=Y> | . $locale->text('Project Number') . qq|</td>
885           <td><input name="l_transaction_description" class=checkbox type=checkbox value=Y> | . $locale->text('Transaction description') . qq|</td>
886               </tr>
887               <tr>
888                 <td><input name="l_subtotal" class=checkbox type=checkbox value=Y> | . $locale->text('Subtotal') . qq|</td>
889                 <td><input name="l_salesman" class="checkbox" type="checkbox" value="Y"> | . $locale->text('Salesman') . qq|</td>
890               </tr>
891             </table>
892           </td>
893         </tr>
894       </table>
895     </td>
896   </tr>
897   <tr><td colspan=4><hr size=3 noshade></td></tr>
898 </table>
899
900 $jsscript
901
902 <br>
903 <input type=hidden name=nextsub value=orders>
904 <input type=hidden name=vc value=$form->{vc}>
905 <input type=hidden name=type value=$form->{type}>
906
907 <input class=submit type=submit name=action value="|
908     . $locale->text('Continue') . qq|">
909 </form>
910
911 </body>
912 </html>
913 |;
914
915   $lxdebug->leave_sub();
916 }
917
918 sub create_subtotal_row {
919   $lxdebug->enter_sub();
920
921   my ($totals, $columns, $column_alignment, $subtotal_columns, $class) = @_;
922
923   my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
924
925   map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } @{ $subtotal_columns };
926
927   $row->{tax}->{data} = $form->format_amount(\%myconfig, $totals->{amount} - $totals->{netamount}, 2);
928
929   map { $totals->{$_} = 0 } @{ $subtotal_columns };
930
931   $lxdebug->leave_sub();
932
933   return $row;
934 }
935
936 sub orders {
937   $lxdebug->enter_sub();
938
939   check_oe_access();
940
941   $ordnumber = ($form->{type} =~ /_order$/) ? "ordnumber" : "quonumber";
942
943   ($form->{ $form->{vc} }, $form->{"${form->{vc}}_id"}) = split(/--/, $form->{ $form->{vc} });
944
945   $form->{sort} ||= 'transdate';
946
947   OE->transactions(\%myconfig, \%$form);
948
949   $form->{rowcount} = scalar @{ $form->{OE} };
950
951   my @columns = (
952     "transdate",               "reqdate",
953     "id",                      $ordnumber,
954     "name",                    "netamount",
955     "tax",                     "amount",
956     "curr",                    "employee",
957     "salesman",
958     "shipvia",                 "globalprojectnumber",
959     "transaction_description", "open",
960     "delivered", "marge_total", "marge_percent"
961   );
962
963   # only show checkboxes if gotten here via sales_order form.
964   my $allow_multiple_orders = $form->{type} eq 'sales_order';
965   if ($allow_multiple_orders) {
966     unshift @columns, "ids";
967   }
968
969   $form->{l_open}      = $form->{l_closed} = "Y" if ($form->{open}      && $form->{closed});
970   $form->{l_delivered} = "Y"                     if ($form->{delivered} && $form->{notdelivered});
971
972   my $attachment_basename;
973   if ($form->{vc} eq 'vendor') {
974     if ($form->{type} eq 'purchase_order') {
975       $form->{title}       = $locale->text('Purchase Orders');
976       $attachment_basename = $locale->text('purchase_order_list');
977     } else {
978       $form->{title}       = $locale->text('Request for Quotations');
979       $attachment_basename = $locale->text('rfq_list');
980     }
981
982   } else {
983     if ($form->{type} eq 'sales_order') {
984       $form->{title}       = $locale->text('Sales Orders');
985       $attachment_basename = $locale->text('sales_order_list');
986     } else {
987       $form->{title}       = $locale->text('Quotations');
988       $attachment_basename = $locale->text('quotation_list');
989     }
990   }
991
992   my $report = SL::ReportGenerator->new(\%myconfig, $form);
993
994   my @hidden_variables = map { "l_${_}" } @columns;
995   push @hidden_variables, "l_subtotal", $form->{vc}, qw(l_closed l_notdelivered open closed delivered notdelivered ordnumber quonumber
996                                                         transaction_description transdatefrom transdateto type vc employee_id salesman_id);
997
998   my $href = build_std_url('action=orders', grep { $form->{$_} } @hidden_variables);
999
1000   my %column_defs = (
1001     'ids'                     => { 'text' => '', },
1002     'transdate'               => { 'text' => $locale->text('Date'), },
1003     'reqdate'                 => { 'text' => $locale->text('Required by'), },
1004     'id'                      => { 'text' => $locale->text('ID'), },
1005     'ordnumber'               => { 'text' => $locale->text('Order'), },
1006     'quonumber'               => { 'text' => $form->{type} eq "request_quotation" ? $locale->text('RFQ') : $locale->text('Quotation'), },
1007     'name'                    => { 'text' => $form->{vc} eq 'customer' ? $locale->text('Customer') : $locale->text('Vendor'), },
1008     'netamount'               => { 'text' => $locale->text('Amount'), },
1009     'tax'                     => { 'text' => $locale->text('Tax'), },
1010     'amount'                  => { 'text' => $locale->text('Total'), },
1011     'curr'                    => { 'text' => $locale->text('Curr'), },
1012     'employee'                => { 'text' => $locale->text('Employee'), },
1013     'salesman'                => { 'text' => $locale->text('Salesman'), },
1014     'shipvia'                 => { 'text' => $locale->text('Ship via'), },
1015     'globalprojectnumber'     => { 'text' => $locale->text('Project Number'), },
1016     'transaction_description' => { 'text' => $locale->text('Transaction description'), },
1017     'open'                    => { 'text' => $locale->text('Open'), },
1018     'delivered'               => { 'text' => $locale->text('Delivered'), },
1019     'marge_total'             => { 'text' => $locale->text('Ertrag'), },
1020     'marge_percent'           => { 'text' => $locale->text('Ertrag prozentual'), }
1021   );
1022
1023   foreach my $name (qw(id transdate reqdate quonumber ordnumber name employee salesman shipvia)) {
1024     $column_defs{$name}->{link} = $href . "&sort=$name";
1025   }
1026
1027   my %column_alignment = map { $_ => 'right' } qw(netamount tax amount curr);
1028
1029   $form->{"l_type"} = "Y";
1030   map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
1031   $column_defs{ids}->{visible} = $allow_multiple_orders ? 'HTML' : 0;
1032
1033   $report->set_columns(%column_defs);
1034   $report->set_column_order(@columns);
1035
1036   $report->set_export_options('orders', @hidden_variables);
1037
1038   $report->set_sort_indicator($form->{sort}, 1);
1039
1040   my @options;
1041   if ($form->{customer}) {
1042     push @options, $locale->text('Customer') . " : $form->{customer}";
1043   }
1044   if ($form->{vendor}) {
1045     push @options, $locale->text('Vendor') . " : $form->{vendor}";
1046   }
1047   if ($form->{department}) {
1048     ($department) = split /--/, $form->{department};
1049     push @options, $locale->text('Department') . " : $department";
1050   }
1051   if ($form->{ordnumber}) {
1052     push @options, $locale->text('Order Number') . " : $form->{ordnumber}";
1053   }
1054   if ($form->{notes}) {
1055     push @options, $locale->text('Notes') . " : $form->{notes}";
1056   }
1057   if ($form->{transaction_description}) {
1058     push @options, $locale->text('Transaction description') . " : $form->{transaction_description}";
1059   }
1060   if ($form->{transdatefrom}) {
1061     push @options, $locale->text('From') . "&nbsp;" . $locale->date(\%myconfig, $form->{transdatefrom}, 1);
1062   }
1063   if ($form->{transdateto}) {
1064     push @options, $locale->text('Bis') . "&nbsp;" . $locale->date(\%myconfig, $form->{transdateto}, 1);
1065   }
1066   if ($form->{open}) {
1067     push @options, $locale->text('Open');
1068   }
1069   if ($form->{closed}) {
1070     push @options, $locale->text('Closed');
1071   }
1072   if ($form->{delivered}) {
1073     push @options, $locale->text('Delivered');
1074   }
1075   if ($form->{notdelivered}) {
1076     push @options, $locale->text('Not delivered');
1077   }
1078
1079   $report->set_options('top_info_text'        => join("\n", @options),
1080                        'raw_top_info_text'    => $form->parse_html_template('oe/orders_top'),
1081                        'raw_bottom_info_text' => $form->parse_html_template('oe/orders_bottom', { 'SHOW_CONTINUE_BUTTON' => $allow_multiple_orders }),
1082                        'output_format'        => 'HTML',
1083                        'title'                => $form->{title},
1084                        'attachment_basename'  => $attachment_basename . strftime('_%Y%m%d', localtime time),
1085     );
1086   $report->set_options_from_form();
1087
1088   # add sort and escape callback, this one we use for the add sub
1089   $form->{callback} = $href .= "&sort=$form->{sort}";
1090
1091   # escape callback for href
1092   $callback = $form->escape($href);
1093
1094   my @subtotal_columns = qw(netamount amount marge_total marge_percent);
1095
1096   my %totals    = map { $_ => 0 } @subtotal_columns;
1097   my %subtotals = map { $_ => 0 } @subtotal_columns;
1098
1099   my $idx = 0;
1100
1101   my $edit_url = build_std_url('action=edit', 'type', 'vc');
1102
1103   foreach $oe (@{ $form->{OE} }) {
1104     map { $oe->{$_} *= $oe->{exchangerate} } @subtotal_columns;
1105
1106     $oe->{tax}       = $oe->{amount} - $oe->{netamount};
1107     $oe->{open}      = $oe->{closed}    ? $locale->text('No')  : $locale->text('Yes');
1108     $oe->{delivered} = $oe->{delivered} ? $locale->text('Yes') : $locale->text('No');
1109
1110     map { $subtotals{$_} += $oe->{$_};
1111           $totals{$_}    += $oe->{$_} } @subtotal_columns;
1112
1113     $subtotals{marge_percent} = $subtotals{netamount} ? ($subtotals{marge_total} * 100 / $subtotals{netamount}) : 0;
1114     $totals{marge_percent}    = $totals{netamount}    ? ($totals{marge_total}    * 100 / $totals{netamount}   ) : 0;
1115
1116     map { $oe->{$_} = $form->format_amount(\%myconfig, $oe->{$_}, 2) } qw(netamount tax amount marge_total marge_percent);
1117
1118     my $row = { };
1119
1120     foreach my $column (@columns) {
1121       next if ($column eq 'ids');
1122       $row->{$column} = {
1123         'data'  => $oe->{$column},
1124         'align' => $column_alignment{$column},
1125       };
1126     }
1127
1128     $row->{ids} = {
1129       'raw_data' =>   $cgi->hidden('-name' => "trans_id_${idx}", '-value' => $oe->{id})
1130                     . $cgi->checkbox('-name' => "multi_id_${idx}", '-value' => 1, '-label' => ''),
1131       'valign'   => 'center',
1132       'align'    => 'center',
1133     };
1134
1135     $row->{$ordnumber}->{link} = $edit_url . "&id=" . E($oe->{id}) . "&callback=${callback}";
1136
1137     my $row_set = [ $row ];
1138
1139     if (($form->{l_subtotal} eq 'Y')
1140         && (($idx == (scalar @{ $form->{OE} } - 1))
1141             || ($oe->{ $form->{sort} } ne $form->{OE}->[$idx + 1]->{ $form->{sort} }))) {
1142       push @{ $row_set }, create_subtotal_row(\%subtotals, \@columns, \%column_alignment, \@subtotal_columns, 'listsubtotal');
1143     }
1144
1145     $report->add_data($row_set);
1146
1147     $idx++;
1148   }
1149
1150   $report->add_separator();
1151   $report->add_data(create_subtotal_row(\%totals, \@columns, \%column_alignment, \@subtotal_columns, 'listtotal'));
1152
1153   $report->generate_with_headers();
1154
1155   $lxdebug->leave_sub();
1156 }
1157
1158 sub check_delivered_flag {
1159   $lxdebug->enter_sub();
1160
1161   check_oe_access();
1162
1163   if (($form->{type} ne 'sales_order') && ($form->{type} ne 'purchase_order')) {
1164     return $lxdebug->leave_sub();
1165   }
1166
1167   my $all_delivered = 0;
1168
1169   foreach my $i (1 .. $form->{rowcount}) {
1170     next if (!$form->{"id_$i"});
1171
1172     if ($form->parse_amount(\%myconfig, $form->{"qty_$i"}) == $form->parse_amount(\%myconfig, $form->{"ship_$i"})) {
1173       $all_delivered = 1;
1174       next;
1175     }
1176
1177     $all_delivered = 0;
1178     last;
1179   }
1180
1181   $form->{delivered} = 1 if $all_delivered;
1182
1183   $lxdebug->leave_sub();
1184 }
1185
1186 sub save_and_close {
1187   $lxdebug->enter_sub();
1188
1189   check_oe_access();
1190
1191   $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
1192
1193   if ($form->{type} =~ /_order$/) {
1194     $form->isblank("transdate", $locale->text('Order Date missing!'));
1195   } else {
1196     $form->isblank("transdate", $locale->text('Quotation Date missing!'));
1197   }
1198
1199   my $idx = $form->{type} =~ /_quotation$/ ? "quonumber" : "ordnumber";
1200   $form->{$idx} =~ s/^\s*//g;
1201   $form->{$idx} =~ s/\s*$//g;
1202
1203   $msg = ucfirst $form->{vc};
1204   $form->isblank($form->{vc}, $locale->text($msg . " missing!"));
1205
1206   # $locale->text('Customer missing!');
1207   # $locale->text('Vendor missing!');
1208
1209   $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
1210     if ($form->{currency} ne $form->{defaultcurrency});
1211
1212   &validate_items;
1213   
1214   if($form->{payment_id}) { 
1215     $payment_id = $form->{payment_id};
1216   }
1217   
1218   # if the name changed get new values
1219   if (&check_name($form->{vc})) {
1220     if($form->{payment_id} eq "") { 
1221       $form->{payment_id} = $payment_id;
1222     }
1223     &update;
1224     exit;
1225   }
1226
1227   $form->{id} = 0 if $form->{saveasnew};
1228
1229   # this is for the internal notes section for the [email] Subject
1230   if ($form->{type} =~ /_order$/) {
1231     if ($form->{type} eq 'sales_order') {
1232       $form->{label} = $locale->text('Sales Order');
1233
1234       $numberfld = "sonumber";
1235       $ordnumber = "ordnumber";
1236     } else {
1237       $form->{label} = $locale->text('Purchase Order');
1238
1239       $numberfld = "ponumber";
1240       $ordnumber = "ordnumber";
1241     }
1242
1243     $err = $locale->text('Cannot save order!');
1244
1245     check_delivered_flag();
1246
1247   } else {
1248     if ($form->{type} eq 'sales_quotation') {
1249       $form->{label} = $locale->text('Quotation');
1250
1251       $numberfld = "sqnumber";
1252       $ordnumber = "quonumber";
1253     } else {
1254       $form->{label} = $locale->text('Request for Quotation');
1255
1256       $numberfld = "rfqnumber";
1257       $ordnumber = "quonumber";
1258     }
1259
1260     $err = $locale->text('Cannot save quotation!');
1261
1262   }
1263
1264   # get new number in sequence if no number is given or if saveasnew was requested
1265   if (!$form->{$ordnumber} || $form->{saveasnew}) {
1266     $form->{$ordnumber} = $form->update_defaults(\%myconfig, $numberfld);
1267   }
1268
1269   relink_accounts();
1270
1271   $form->error($err) if (!OE->save(\%myconfig, \%$form));
1272
1273   # saving the history
1274   if(!exists $form->{addition}) {
1275     $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
1276         $form->{addition} = "SAVED";
1277         $form->save_history($form->dbconnect(\%myconfig));
1278   }
1279   # /saving the history
1280
1281   $form->redirect($form->{label} . " $form->{$ordnumber} " .
1282                   $locale->text('saved!'));
1283
1284   $lxdebug->leave_sub();
1285 }
1286
1287 sub save {
1288   $lxdebug->enter_sub();
1289
1290   check_oe_access();
1291
1292   $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
1293
1294
1295   if ($form->{type} =~ /_order$/) {
1296     $form->isblank("transdate", $locale->text('Order Date missing!'));
1297   } else {
1298     $form->isblank("transdate", $locale->text('Quotation Date missing!'));
1299   }
1300
1301   my $idx = $form->{type} =~ /_quotation$/ ? "quonumber" : "ordnumber";
1302   $form->{$idx} =~ s/^\s*//g;
1303   $form->{$idx} =~ s/\s*$//g;
1304
1305   $msg = ucfirst $form->{vc};
1306   $form->isblank($form->{vc}, $locale->text($msg . " missing!"));
1307
1308   # $locale->text('Customer missing!');
1309   # $locale->text('Vendor missing!');
1310
1311   $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
1312     if ($form->{currency} ne $form->{defaultcurrency});
1313
1314   &validate_items;
1315   
1316   if($form->{payment_id}) { 
1317     $payment_id = $form->{payment_id};
1318   }
1319   
1320   # if the name changed get new values
1321   if (&check_name($form->{vc})) {
1322     if($form->{payment_id} eq "") { 
1323       $form->{payment_id} = $payment_id;
1324     }
1325     &update;
1326     exit;
1327   }
1328
1329   $form->{id} = 0 if $form->{saveasnew};
1330
1331   # this is for the internal notes section for the [email] Subject
1332   if ($form->{type} =~ /_order$/) {
1333     if ($form->{type} eq 'sales_order') {
1334       $form->{label} = $locale->text('Sales Order');
1335
1336       $numberfld = "sonumber";
1337       $ordnumber = "ordnumber";
1338     } else {
1339       $form->{label} = $locale->text('Purchase Order');
1340
1341       $numberfld = "ponumber";
1342       $ordnumber = "ordnumber";
1343     }
1344
1345     $err = $locale->text('Cannot save order!');
1346
1347     check_delivered_flag();
1348
1349   } else {
1350     if ($form->{type} eq 'sales_quotation') {
1351       $form->{label} = $locale->text('Quotation');
1352
1353       $numberfld = "sqnumber";
1354       $ordnumber = "quonumber";
1355     } else {
1356       $form->{label} = $locale->text('Request for Quotation');
1357
1358       $numberfld = "rfqnumber";
1359       $ordnumber = "quonumber";
1360     }
1361
1362     $err = $locale->text('Cannot save quotation!');
1363
1364   }
1365
1366   $form->{$ordnumber} = $form->update_defaults(\%myconfig, $numberfld)
1367     unless $form->{$ordnumber};
1368
1369   relink_accounts();
1370
1371   OE->save(\%myconfig, \%$form);
1372
1373   # saving the history
1374   if(!exists $form->{addition}) {
1375     $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
1376         $form->{addition} = "SAVED";
1377         $form->save_history($form->dbconnect(\%myconfig));
1378   }
1379   # /saving the history 
1380
1381   $form->{simple_save} = 1;
1382   if(!$form->{print_and_save}) {
1383     set_headings("edit");
1384     &update;
1385     exit;
1386   }
1387   $lxdebug->leave_sub();
1388 }
1389
1390 sub delete {
1391   $lxdebug->enter_sub();
1392
1393   check_oe_access();
1394
1395   $form->header;
1396
1397   if ($form->{type} =~ /_order$/) {
1398     $msg       = $locale->text('Are you sure you want to delete Order Number');
1399     $ordnumber = 'ordnumber';
1400   } else {
1401     $msg = $locale->text('Are you sure you want to delete Quotation Number');
1402     $ordnumber = 'quonumber';
1403   }
1404
1405   print qq|
1406 <body>
1407
1408 <form method=post action=$form->{script}>
1409 |;
1410
1411   # delete action variable
1412   map { delete $form->{$_} } qw(action header);
1413
1414   foreach $key (keys %$form) {
1415     next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
1416     $form->{$key} =~ s/\"/&quot;/g;
1417     print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
1418   }
1419
1420   print qq|
1421 <h2 class=confirm>| . $locale->text('Confirm!') . qq|</h2>
1422
1423 <h4>$msg $form->{$ordnumber}</h4>
1424 <p>
1425 <input type="hidden" name="yes_nextsub" value="delete_order_quotation">
1426 <input name=action class=submit type=submit value="|
1427     . $locale->text('Yes') . qq|">
1428 <button class=submit type=button onclick="history.back()">|
1429     . $locale->text('No') . qq|</button>
1430 </form>
1431
1432 </body>
1433 </html>
1434 |;
1435
1436   $lxdebug->leave_sub();
1437 }
1438
1439 sub delete_order_quotation {
1440   $lxdebug->enter_sub();
1441
1442   check_oe_access();
1443
1444   if ($form->{type} =~ /_order$/) {
1445     $msg = $locale->text('Order deleted!');
1446     $err = $locale->text('Cannot delete order!');
1447   } else {
1448     $msg = $locale->text('Quotation deleted!');
1449     $err = $locale->text('Cannot delete quotation!');
1450   }
1451   if (OE->delete(\%myconfig, \%$form, $spool)){
1452     # saving the history
1453     if(!exists $form->{addition}) {
1454       $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
1455           $form->{addition} = "DELETED";
1456           $form->save_history($form->dbconnect(\%myconfig));
1457     }
1458     # /saving the history 
1459     $form->info($msg);
1460     exit();
1461   }
1462   $form->error($err);
1463
1464   $lxdebug->leave_sub();
1465 }
1466
1467 sub invoice {
1468   $lxdebug->enter_sub();
1469
1470   check_oe_access();
1471   $auth->assert($form->{type} eq 'purchase_order' || $form->{type} eq 'request_quotation' ? 'vendor_invoice_edit' : 'invoice_edit');
1472
1473   $form->{old_employee_id} = $form->{employee_id};
1474   $form->{old_salesman_id} = $form->{salesman_id};
1475
1476   if ($form->{type} =~ /_order$/) {
1477
1478     # these checks only apply if the items don't bring their own ordnumbers/transdates.
1479     # The if clause ensures that by searching for empty ordnumber_#/transdate_# fields.
1480     $form->isblank("ordnumber", $locale->text('Order Number missing!'))
1481       if (+{ map { $form->{"ordnumber_$_"}, 1 } (1 .. $form->{rowcount} - 1) }->{''});
1482     $form->isblank("transdate", $locale->text('Order Date missing!'))
1483       if (+{ map { $form->{"transdate_$_"}, 1 } (1 .. $form->{rowcount} - 1) }->{''});
1484
1485     # also copy deliverydate from the order
1486     $form->{deliverydate} = $form->{reqdate} if $form->{reqdate};
1487     $form->{orddate}      = $form->{transdate};
1488   } else {
1489     $form->isblank("quonumber", $locale->text('Quotation Number missing!'));
1490     $form->isblank("transdate", $locale->text('Quotation Date missing!'));
1491     $form->{ordnumber}    = "";
1492     $form->{quodate}      = $form->{transdate};
1493   }
1494   
1495   $payment_id = $form->{payment_id} if $form->{payment_id};
1496   
1497   # if the name changed get new values
1498   if (&check_name($form->{vc})) {
1499     $form->{payment_id} = $payment_id if $form->{payment_id} eq "";
1500     &update;
1501     exit;
1502   }
1503
1504   $form->{cp_id} *= 1;
1505
1506   for $i (1 .. $form->{rowcount}) {
1507     for (qw(ship qty sellprice listprice basefactor)) {
1508       $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig, $form->{"${_}_${i}"}) if $form->{"${_}_${i}"};
1509     }
1510   }
1511
1512   if (   $form->{type} =~ /_order/
1513       && $form->{currency} ne $form->{defaultcurrency}) {
1514
1515     # check if we need a new exchangerate
1516     $buysell = ($form->{type} eq 'sales_order') ? "buy" : "sell";
1517
1518     $orddate      = $form->current_date(\%myconfig);
1519     $exchangerate = $form->check_exchangerate(\%myconfig, $form->{currency}, $orddate, $buysell);
1520
1521     if (!$exchangerate) {
1522       &backorder_exchangerate($orddate, $buysell);
1523       exit;
1524     }
1525   }
1526
1527   # close orders/quotations
1528   $form->{closed} = 1;
1529
1530   # save order if one ordnumber has been given
1531   # if not it's most likely a collective order, which can't be saved back
1532   # so they just have to be closed
1533   if (($form->{ordnumber} ne '') || ($form->{quonumber} ne '')) {
1534     OE->close_order(\%myconfig, \%$form) if ($form->{id});
1535   } else {
1536     OE->close_orders(\%myconfig, \%$form);
1537   }
1538
1539   $form->{transdate} = $form->{invdate} = $form->current_date(\%myconfig);
1540   $form->{duedate}   = $form->current_date(\%myconfig, $form->{invdate}, $form->{terms} * 1);
1541
1542   $form->{id}     = '';
1543   $form->{closed} = 0;
1544   $form->{rowcount}--;
1545   $form->{shipto} = 1;
1546
1547   $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
1548
1549   if ($form->{type} =~ /_order$/) {
1550     $form->{exchangerate} = $exchangerate;
1551     &create_backorder;
1552   }
1553
1554   if (   $form->{type} eq 'purchase_order'
1555       || $form->{type} eq 'request_quotation') {
1556     $form->{title}  = $locale->text('Add Vendor Invoice');
1557     $form->{script} = 'ir.pl';
1558     $script         = "ir";
1559     $buysell        = 'sell';
1560   }
1561
1562   if (   $form->{type} eq 'sales_order' 
1563       || $form->{type} eq 'sales_quotation') {
1564     $form->{title}  = $locale->text('Add Sales Invoice');
1565     $form->{script} = 'is.pl';
1566     $script         = "is";
1567     $buysell        = 'buy';
1568   }
1569
1570   # bo creates the id, reset it
1571   map { delete $form->{$_} } qw(id subject message cc bcc printed emailed queued);
1572   $form->{ $form->{vc} } =~ s/--.*//g;
1573   $form->{type} = "invoice";
1574
1575   # locale messages
1576   $locale = new Locale "$myconfig{countrycode}", "$script";
1577
1578   require "bin/mozilla/$form->{script}";
1579
1580   map { $form->{"select$_"} = "" } ($form->{vc}, currency);
1581
1582   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
1583     qw(creditlimit creditremaining);
1584
1585   $currency = $form->{currency};
1586   &invoice_links;
1587
1588   $form->{currency}     = $currency;
1589   $form->{exchangerate} = "";
1590   $form->{forex}        = "";
1591   $form->{exchangerate} = $exchangerate
1592     if (
1593         $form->{forex} = (
1594                     $exchangerate =
1595                       $form->check_exchangerate(
1596                       \%myconfig, $form->{currency}, $form->{invdate}, $buysell
1597                       )));
1598
1599   $form->{creditremaining} -= ($form->{oldinvtotal} - $form->{ordtotal});
1600
1601   &prepare_invoice;
1602
1603   # format amounts
1604   for $i (1 .. $form->{rowcount}) {
1605     $form->{"discount_$i"} =
1606       $form->format_amount(\%myconfig, $form->{"discount_$i"});
1607
1608     ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
1609     $dec           = length $dec;
1610     $decimalplaces = ($dec > 2) ? $dec : 2;
1611
1612     # copy delivery date from reqdate for order -> invoice conversion
1613     $form->{"deliverydate_$i"} = $form->{"reqdate_$i"}
1614       unless $form->{"deliverydate_$i"};
1615
1616     $form->{"sellprice_$i"} =
1617       $form->format_amount(\%myconfig, $form->{"sellprice_$i"},
1618                            $decimalplaces);
1619
1620     (my $dec_qty) = ($form->{"qty_$i"} =~ /\.(\d+)/);
1621     $dec_qty = length $dec_qty;
1622     $form->{"qty_$i"} =
1623       $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
1624
1625     map { $form->{"${_}_$i"} =~ s/\"/&quot;/g }
1626       qw(partnumber description unit);
1627
1628   }
1629
1630   &display_form;
1631
1632   $lxdebug->leave_sub();
1633 }
1634
1635 sub backorder_exchangerate {
1636   $lxdebug->enter_sub();
1637
1638   check_oe_access();
1639
1640   my ($orddate, $buysell) = @_;
1641
1642   $form->header;
1643
1644   print qq|
1645 <body>
1646
1647 <form method=post action=$form->{script}>
1648 |;
1649
1650   # delete action variable
1651   map { delete $form->{$_} } qw(action header exchangerate);
1652
1653   foreach $key (keys %$form) {
1654     next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
1655     $form->{$key} =~ s/\"/&quot;/g;
1656     print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
1657   }
1658
1659   $form->{title} = $locale->text('Add Exchangerate');
1660
1661   print qq|
1662
1663 <input type=hidden name=exchangeratedate value=$orddate>
1664 <input type=hidden name=buysell value=$buysell>
1665
1666 <table width=100%>
1667   <tr><th class=listtop>$form->{title}</th></tr>
1668   <tr height="5"></tr>
1669   <tr>
1670     <td>
1671       <table>
1672         <tr>
1673           <th align=right>| . $locale->text('Currency') . qq|</th>
1674           <td>$form->{currency}</td>
1675         </tr>
1676         <tr>
1677           <th align=right>| . $locale->text('Date') . qq|</th>
1678           <td>$orddate</td>
1679         </tr>
1680         <tr>
1681           <th align=right>| . $locale->text('Exchangerate') . qq|</th>
1682           <td><input name=exchangerate size=11></td>
1683         </tr>
1684       </table>
1685     </td>
1686   </tr>
1687 </table>
1688
1689 <hr size=3 noshade>
1690
1691 <br>
1692 <input type=hidden name=nextsub value=save_exchangerate>
1693
1694 <input name=action class=submit type=submit value="|
1695     . $locale->text('Continue') . qq|">
1696
1697 </form>
1698
1699 </body>
1700 </html>
1701 |;
1702
1703   $lxdebug->leave_sub();
1704 }
1705
1706 sub save_exchangerate {
1707   $lxdebug->enter_sub();
1708
1709   $form->isblank("exchangerate", $locale->text('Exchangerate missing!'));
1710   $form->{exchangerate} =
1711     $form->parse_amount(\%myconfig, $form->{exchangerate});
1712   $form->save_exchangerate(\%myconfig, $form->{currency},
1713                            $form->{exchangeratedate},
1714                            $form->{exchangerate}, $form->{buysell});
1715
1716   &invoice;
1717
1718   $lxdebug->leave_sub();
1719 }
1720
1721 sub create_backorder {
1722   $lxdebug->enter_sub();
1723
1724   $form->{shipped} = 1;
1725
1726   # figure out if we need to create a backorder
1727   # items aren't saved if qty != 0
1728
1729   for $i (1 .. $form->{rowcount}) {
1730     $totalqty  += $qty  = $form->{"qty_$i"};
1731     $totalship += $ship = $form->{"ship_$i"};
1732
1733     $form->{"qty_$i"} = $qty - $ship;
1734   }
1735
1736   if ($totalship == 0) {
1737     map { $form->{"ship_$_"} = $form->{"qty_$_"} } (1 .. $form->{rowcount});
1738     $form->{ordtotal} = 0;
1739     $form->{shipped}  = 0;
1740     return;
1741   }
1742
1743   if ($totalqty == $totalship) {
1744     map { $form->{"qty_$_"} = $form->{"ship_$_"} } (1 .. $form->{rowcount});
1745     $form->{ordtotal} = 0;
1746     return;
1747   }
1748
1749   @flds = (
1750     qw(partnumber description qty ship unit sellprice discount id inventory_accno bin income_accno expense_accno listprice assembly taxaccounts partsgroup)
1751   );
1752
1753   for $i (1 .. $form->{rowcount}) {
1754     map {
1755       $form->{"${_}_$i"} =
1756         $form->format_amount(\%myconfig, $form->{"${_}_$i"})
1757     } qw(sellprice discount);
1758   }
1759
1760   relink_accounts();
1761
1762   OE->save(\%myconfig, \%$form);
1763
1764   # rebuild rows for invoice
1765   @a     = ();
1766   $count = 0;
1767
1768   for $i (1 .. $form->{rowcount}) {
1769     $form->{"qty_$i"} = $form->{"ship_$i"};
1770
1771     if ($form->{"qty_$i"}) {
1772       push @a, {};
1773       $j = $#a;
1774       map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
1775       $count++;
1776     }
1777   }
1778
1779   $form->redo_rows(\@flds, \@a, $count, $form->{rowcount});
1780   $form->{rowcount} = $count;
1781
1782   $lxdebug->leave_sub();
1783 }
1784
1785 sub save_as_new {
1786   $lxdebug->enter_sub();
1787
1788   check_oe_access();
1789
1790   $form->{saveasnew} = 1;
1791   $form->{closed}    = 0;
1792   map { delete $form->{$_} } qw(printed emailed queued);
1793
1794   # Let Lx-Office assign a new order number if the user hasn't changed the
1795   # previous one. If it has been changed manually then use it as-is.
1796   my $idx = $form->{type} =~ /_quotation$/ ? "quonumber" : "ordnumber";
1797   $form->{$idx} =~ s/^\s*//g;
1798   $form->{$idx} =~ s/\s*$//g;
1799   if ($form->{saved_xyznumber} &&
1800       ($form->{saved_xyznumber} eq $form->{$idx})) {
1801     delete($form->{$idx});
1802   }
1803
1804   &save;
1805
1806   $lxdebug->leave_sub();
1807 }
1808
1809 sub check_for_direct_delivery_yes {
1810   $lxdebug->enter_sub();
1811
1812   check_oe_access();
1813
1814   $form->{direct_delivery_checked} = 1;
1815   delete @{$form}{grep /^shipto/, keys %{ $form }};
1816   map { s/^CFDD_//; $form->{$_} = $form->{"CFDD_${_}"} } grep /^CFDD_/, keys %{ $form };
1817   $form->{shipto} = 1;
1818   purchase_order();
1819   $lxdebug->leave_sub();
1820 }
1821
1822 sub check_for_direct_delivery_no {
1823   $lxdebug->enter_sub();
1824
1825   check_oe_access();
1826
1827   $form->{direct_delivery_checked} = 1;
1828   delete @{$form}{grep /^shipto/, keys %{ $form }};
1829   purchase_order();
1830
1831   $lxdebug->leave_sub();
1832 }
1833
1834 sub check_for_direct_delivery {
1835   $lxdebug->enter_sub();
1836
1837   check_oe_access();
1838
1839   if ($form->{direct_delivery_checked}
1840       || (!$form->{shiptoname} && !$form->{shiptostreet} && !$form->{shipto_id})) {
1841     $lxdebug->leave_sub();
1842     return;
1843   }
1844
1845   if ($form->{shipto_id}) {
1846     Common->get_shipto_by_id(\%myconfig, $form, $form->{shipto_id}, "CFDD_");
1847
1848   } else {
1849     map { $form->{"CFDD_${_}"} = $form->{$_ } } grep /^shipto/, keys %{ $form };
1850   }
1851
1852   delete $form->{action};
1853   $form->{VARIABLES} = [ map { { "key" => $_, "value" => $form->{$_} } } grep { ($_ ne 'login') && ($_ ne 'password') && (ref $_ eq "") } keys %{ $form } ];
1854
1855   $form->header();
1856   print $form->parse_html_template("oe/check_for_direct_delivery");
1857
1858   $lxdebug->leave_sub();
1859
1860   exit 0;
1861 }
1862
1863 sub purchase_order {
1864   $lxdebug->enter_sub();
1865
1866   check_oe_access();
1867   $auth->assert('purchase_order_edit');
1868
1869   if ($form->{type} eq 'sales_order') {
1870     check_for_direct_delivery();
1871   }
1872
1873   if (   $form->{type} eq 'sales_quotation'
1874       || $form->{type} eq 'request_quotation') {
1875     OE->close_order(\%myconfig, \%$form);
1876   }
1877
1878   if ($form->{type} =~ /^sales_/) {
1879     delete($form->{ordnumber});
1880   }
1881
1882   $form->{cp_id} *= 1;
1883
1884   $form->{title} = $locale->text('Add Purchase Order');
1885   $form->{vc}    = "vendor";
1886   $form->{type}  = "purchase_order";
1887
1888   &poso;
1889
1890   $lxdebug->leave_sub();
1891 }
1892
1893 sub sales_order {
1894   $lxdebug->enter_sub();
1895
1896   check_oe_access();
1897   $auth->assert('sales_order_edit');
1898
1899   if (   $form->{type} eq 'sales_quotation'
1900       || $form->{type} eq 'request_quotation') {
1901     OE->close_order(\%myconfig, $form);
1902   }
1903
1904   if ($form->{type} eq "purchase_order") {
1905     delete($form->{ordnumber});
1906   }
1907
1908   $form->{cp_id} *= 1;
1909
1910   $form->{title} = $locale->text('Add Sales Order');
1911   $form->{vc}    = "customer";
1912   $form->{type}  = "sales_order";
1913
1914   &poso;
1915
1916   $lxdebug->leave_sub();
1917 }
1918
1919 sub poso {
1920   $lxdebug->enter_sub();
1921
1922   check_oe_access();
1923   $auth->assert('purchase_order_edit | sales_order_edit');
1924
1925   $form->{transdate} = $form->current_date(\%myconfig);
1926   delete $form->{duedate};
1927
1928   $form->{closed} = 0;
1929
1930   $form->{old_employee_id} = $form->{employee_id};
1931   $form->{old_salesman_id} = $form->{salesman_id};
1932
1933   # reset
1934   map { delete $form->{$_} }
1935     qw(id subject message cc bcc printed emailed queued customer vendor creditlimit creditremaining discount tradediscount oldinvtotal);
1936
1937   for $i (1 .. $form->{rowcount}) {
1938     map({ $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig,
1939                                                      $form->{"${_}_${i}"})
1940             if ($form->{"${_}_${i}"}) }
1941         qw(ship qty sellprice listprice basefactor));
1942   }
1943
1944   &order_links;
1945
1946   &prepare_order;
1947
1948   # format amounts
1949   for $i (1 .. $form->{rowcount} - 1) {
1950     map { $form->{"${_}_$i"} =~ s/\"/&quot;/g }
1951       qw(partnumber description unit);
1952   }
1953
1954   map { $form->{$_} = $form->format_amount(\%myconfig, $form->{$_}, 0, "0") }
1955     qw(creditlimit creditremaining);
1956
1957   &update;
1958
1959   $lxdebug->leave_sub();
1960 }
1961
1962 sub e_mail {
1963   $lxdebug->enter_sub();
1964
1965   check_oe_access();
1966
1967   $form->{print_and_save} = 1;
1968
1969   $print_post = 1;
1970
1971   my $saved_form = save_form();
1972
1973   save();
1974
1975   restore_form($saved_form, 0, qw(id ordnumber quonumber));
1976
1977   edit_e_mail();
1978
1979   $lxdebug->leave_sub();
1980 }
1981
1982 sub yes {
1983   call_sub($form->{yes_nextsub});
1984 }
1985
1986 sub no {
1987   call_sub($form->{no_nextsub});
1988 }
1989
1990 ######################################################################################################
1991 # IO ENTKOPPLUNG
1992 # ###############################################################################################
1993 sub display_form {
1994   $lxdebug->enter_sub();
1995
1996   check_oe_access();
1997
1998   $form->{"taxaccounts"} =~ s/\s*$//;
1999   $form->{"taxaccounts"} =~ s/^\s*//;
2000   foreach my $accno (split(/\s*/, $form->{"taxaccounts"})) {
2001     map({ delete($form->{"${accno}_${_}"}); } qw(rate description taxnumber));
2002   }
2003   $form->{"taxaccounts"} = "";
2004
2005   for my $i (1 .. $form->{"rowcount"}) {
2006     IC->retrieve_accounts(\%myconfig, $form, $form->{"id_$i"}, $i, 1) if $form->{"id_$i"};
2007   }
2008
2009   $form->{rowcount}++;
2010   $form->{"project_id_$form->{rowcount}"} = $form->{globalproject_id};
2011
2012   $form->language_payment(\%myconfig);
2013
2014   Common::webdav_folder($form) if ($webdav);
2015
2016   &form_header;
2017
2018   # create rows
2019   display_row($form->{rowcount}) if $form->{rowcount};
2020
2021   &form_footer;
2022
2023   $lxdebug->leave_sub();
2024 }
2025