Bei der Ausgabe der oldprojectnumber_$i den aktuellen Wert benutzen, damit der Rest...
[kivitendo-erp.git] / bin / mozilla / io.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 # Veraendert 2005-01-05 - Marco Welter <mawe@linux-studio.de> - Neue Optik  #
8 #############################################################################
9 # SQL-Ledger, Accounting
10 # Copyright (c) 1998-2002
11 #
12 #  Author: Dieter Simader
13 #   Email: dsimader@sql-ledger.org
14 #     Web: http://www.sql-ledger.org
15 #
16 #
17 # This program is free software; you can redistribute it and/or modify
18 # it under the terms of the GNU General Public License as published by
19 # the Free Software Foundation; either version 2 of the License, or
20 # (at your option) any later version.
21 #
22 # This program is distributed in the hope that it will be useful,
23 # but WITHOUT ANY WARRANTY; without even the implied warranty of
24 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25 # GNU General Public License for more details.
26 # You should have received a copy of the GNU General Public License
27 # along with this program; if not, write to the Free Software
28 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #
30 #######################################################################
31 #
32 # common routines used in is, ir, oe
33 #
34 #######################################################################
35
36 use SL::CT;
37 use SL::IC;
38 use CGI::Ajax;
39 use CGI;
40
41 require "$form->{path}/common.pl";
42
43 # any custom scripts for this one
44 if (-f "$form->{path}/custom_io.pl") {
45   eval { require "$form->{path}/custom_io.pl"; };
46 }
47 if (-f "$form->{path}/$form->{login}_io.pl") {
48   eval { require "$form->{path}/$form->{login}_io.pl"; };
49 }
50
51 1;
52
53 # end of main
54
55 # this is for our long dates
56 # $locale->text('January')
57 # $locale->text('February')
58 # $locale->text('March')
59 # $locale->text('April')
60 # $locale->text('May ')
61 # $locale->text('June')
62 # $locale->text('July')
63 # $locale->text('August')
64 # $locale->text('September')
65 # $locale->text('October')
66 # $locale->text('November')
67 # $locale->text('December')
68
69 # this is for our short month
70 # $locale->text('Jan')
71 # $locale->text('Feb')
72 # $locale->text('Mar')
73 # $locale->text('Apr')
74 # $locale->text('May')
75 # $locale->text('Jun')
76 # $locale->text('Jul')
77 # $locale->text('Aug')
78 # $locale->text('Sep')
79 # $locale->text('Oct')
80 # $locale->text('Nov')
81 # $locale->text('Dec')
82 use SL::IS;
83 use SL::PE;
84 use SL::AM;
85 use Data::Dumper;
86 ########################################
87 # Eintrag fuer Version 2.2.0 geaendert #
88 # neue Optik im Rechnungsformular      #
89 ########################################
90 sub display_row {
91   $lxdebug->enter_sub();
92   my $numrows = shift;
93
94   if ($lizenzen && $form->{vc} eq "customer") {
95     if ($form->{type} =~ /sales_order/) {
96       @column_index = (runningnumber, partnumber, description, ship, qty);
97     } elsif ($form->{type} =~ /sales_quotation/) {
98       @column_index = (runningnumber, partnumber, description, qty);
99     } else {
100       @column_index = (runningnumber, partnumber, description, qty);
101     }
102   } else {
103     if (   ($form->{type} =~ /purchase_order/)
104         || ($form->{type} =~ /sales_order/)) {
105       @column_index = (runningnumber, partnumber, description, ship, qty);
106         } else {
107       @column_index = (runningnumber, partnumber, description, qty);
108     }
109   }
110 ############## ENDE Neueintrag ##################
111
112   my $dimension_units = AM->retrieve_units(\%myconfig, $form, "dimension");
113   my $service_units = AM->retrieve_units(\%myconfig, $form, "service");
114   my $all_units = AM->retrieve_units(\%myconfig, $form);
115
116   push @column_index, qw(unit);
117
118   #for pricegroups column
119   if (   $form->{type} =~ (/sales_quotation/)
120       or (($form->{level} =~ /Sales/) and ($form->{type} =~ /invoice/))
121       or (($form->{level} eq undef) and ($form->{type} =~ /invoice/))
122       or ($form->{type} =~ /sales_order/)) {
123     push @column_index, qw(sellprice_pg);
124   }
125
126   push @column_index, qw(sellprice);
127
128   if ($form->{vc} eq 'customer') {
129     push @column_index, qw(discount);
130   }
131
132   push @column_index, "linetotal";
133
134   my $colspan = $#column_index + 1;
135
136   $form->{invsubtotal} = 0;
137   map { $form->{"${_}_base"} = 0 } (split / /, $form->{taxaccounts});
138
139 ########################################
140   # Eintrag fuer Version 2.2.0 geaendert #
141   # neue Optik im Rechnungsformular      #
142 ########################################
143   $column_data{runningnumber} =
144       qq|<th align=left nowrap width=5 class=listheading>|
145     . $locale->text('No.')
146     . qq|</th>|;
147   $column_data{partnumber} =
148       qq|<th align=left nowrap width=12 class=listheading>|
149     . $locale->text('Number')
150     . qq|</th>|;
151   $column_data{description} =
152       qq|<th align=left nowrap width=30 class=listheading>|
153     . $locale->text('Part Description')
154     . qq|</th>|;
155   if ($form->{"type"} eq "purchase_order") {
156     $column_data{ship} =
157       qq|<th align=left nowrap width=5 class=listheading>|
158       . $locale->text('Ship rcvd')
159       . qq|</th>|;
160   } else {
161     $column_data{ship} =
162       qq|<th align=left nowrap width=5 class=listheading>|
163       . $locale->text('Ship')
164       . qq|</th>|;
165   }
166   $column_data{qty} =
167       qq|<th align=left nowrap width=5 class=listheading>|
168     . $locale->text('Qty')
169     . qq|</th>|;
170   $column_data{unit} =
171       qq|<th align=left nowrap width=5 class=listheading>|
172     . $locale->text('Unit')
173     . qq|</th>|;
174   $column_data{license} =
175       qq|<th align=left nowrap width=10 class=listheading>|
176     . $locale->text('License')
177     . qq|</th>|;
178   $column_data{serialnr} =
179       qq|<th align=left nowrap width=10 class=listheading>|
180     . $locale->text('Serial No.')
181     . qq|</th>|;
182   $column_data{projectnr} =
183       qq|<th align=left nowrap width=10 class=listheading>|
184     . $locale->text('Project')
185     . qq|</th>|;
186   $column_data{sellprice} =
187       qq|<th align=left nowrap width=15 class=listheading>|
188     . $locale->text('Price')
189     . qq|</th>|;
190   $column_data{sellprice_pg} =
191       qq|<th align=left nowrap width=15 class=listheading>|
192     . $locale->text('Pricegroup')
193     . qq|</th>|;
194   $column_data{discount} =
195       qq|<th align=left class=listheading>|
196     . $locale->text('Discount')
197     . qq|</th>|;
198   $column_data{linetotal} =
199       qq|<th align=left nowrap width=10 class=listheading>|
200     . $locale->text('Extended')
201     . qq|</th>|;
202   $column_data{bin} =
203       qq|<th align=left nowrap width=10 class=listheading>|
204     . $locale->text('Bin')
205     . qq|</th>|;
206 ############## ENDE Neueintrag ##################
207
208   $myconfig{"show_form_details"} = 1
209     unless (defined($myconfig{"show_form_details"}));
210   $form->{"show_details"} = $myconfig{"show_form_details"}
211     unless (defined($form->{"show_details"}));
212   $form->{"show_details"} = $form->{"show_details"} ? 1 : 0;
213   my $show_details_new = 1 - $form->{"show_details"};
214   my $show_details_checked = $form->{"show_details"} ? "checked" : "";
215
216   print qq|
217   <tr>
218     <td>
219       <input type="hidden" name="show_details" value="$form->{show_details}">
220       <input type="checkbox" id="cb_show_details" onclick="show_form_details($show_details_new);" $show_details_checked>
221       <label for="cb_show_details">| . $locale->text("Show details") . qq|</label><br>
222       <table width=100%>
223         <tr class=listheading>|;
224
225   map { print "\n$column_data{$_}" } @column_index;
226
227   print qq|
228         </tr>
229 |;
230
231   $runningnumber = $locale->text('No.');
232   $deliverydate  = $locale->text('Delivery Date');
233   $serialnumber  = $locale->text('Serial No.');
234   $projectnumber = $locale->text('Project');
235   $partsgroup    = $locale->text('Group');
236   $reqdate       = $locale->text('Reqdate');
237
238   $delvar = 'deliverydate';
239
240   if ($form->{type} =~ /_order$/ || $form->{type} =~ /_quotation$/) {
241     $deliverydate = $locale->text('Required by');
242     $delvar       = 'reqdate';
243   }
244
245   for $i (1 .. $numrows) {
246
247     # undo formatting
248     map {
249       $form->{"${_}_$i"} =
250         $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
251     } qw(qty ship discount sellprice price_new price_old) unless ($form->{simple_save});
252
253     if (!$form->{"unit_old_$i"}) {
254       # Neue Ware aus der Datenbank. In diesem Fall ist unit_$i die
255       # Einheit, wie sie in den Stammdaten hinterlegt wurde.
256       # Es sollte also angenommen werden, dass diese ausgewaehlt war.
257       $form->{"unit_old_$i"} = $form->{"unit_$i"};
258     }
259
260
261
262     # Die zuletzt ausgewaehlte mit der aktuell ausgewaehlten Einheit
263     # vergleichen und bei Unterschied den Preis entsprechend umrechnen.
264     $form->{"selected_unit_$i"} = $form->{"unit_$i"} unless ($form->{"selected_unit_$i"});
265
266     my $check_units = $form->{"inventory_accno_$i"} ? $dimension_units : $service_units;
267     if (!$check_units->{$form->{"selected_unit_$i"}} ||
268         ($check_units->{$form->{"selected_unit_$i"}}->{"base_unit"} ne
269          $all_units->{$form->{"unit_old_$i"}}->{"base_unit"})) {
270       # Die ausgewaehlte Einheit ist fuer diesen Artikel nicht gueltig
271       # (z.B. Dimensionseinheit war ausgewaehlt, es handelt sich aber
272       # um eine Dienstleistung). Dann keinerlei Umrechnung vornehmen.
273       $form->{"unit_old_$i"} = $form->{"selected_unit_$i"} = $form->{"unit_$i"};
274     }
275     if ((!$form->{"prices_$i"}) || ($form->{"new_pricegroup_$i"} == $form->{"old_pricegroup_$i"})) {
276       if ($form->{"unit_old_$i"} ne $form->{"selected_unit_$i"}) {
277         my $basefactor = 1;
278         if (defined($all_units->{$form->{"unit_old_$i"}}->{"factor"}) &&
279             $all_units->{$form->{"unit_old_$i"}}->{"factor"}) {
280           $basefactor = $all_units->{$form->{"selected_unit_$i"}}->{"factor"} /
281             $all_units->{$form->{"unit_old_$i"}}->{"factor"};
282         }
283         $form->{"sellprice_$i"} *= $basefactor;
284         $form->{"unit_old_$i"} = $form->{"selected_unit_$i"};
285       }
286     }
287     ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
288     $dec           = length $dec;
289     $decimalplaces = ($dec > 2) ? $dec : 2;
290
291     $discount =
292       $form->round_amount(
293                         $form->{"sellprice_$i"} * $form->{"discount_$i"} / 100,
294                         $decimalplaces);
295
296     $linetotal =
297       $form->round_amount($form->{"sellprice_$i"} - $discount, $decimalplaces);
298     $linetotal = $form->round_amount($linetotal * $form->{"qty_$i"}, 2);
299
300     # convert " to &quot;
301     map { $form->{"${_}_$i"} =~ s/\"/&quot;/g }
302       qw(partnumber description unit unit_old);
303
304 ########################################
305     # Eintrag fuer Version 2.2.0 geaendert #
306     # neue Optik im Rechnungsformular      #
307 ########################################
308     $column_data{runningnumber} =
309       qq|<td><input name="runningnumber_$i" size=5 value=$i></td>|;    # HuT
310 ############## ENDE Neueintrag ##################
311
312     $column_data{partnumber} =
313       qq|<td><input name="partnumber_$i" size=12 value="$form->{"partnumber_$i"}"></td>|;
314
315     if (($rows = $form->numtextrows($form->{"description_$i"}, 30, 6)) > 1) {
316       $column_data{description} =
317         qq|<td><textarea name="description_$i" rows=$rows cols=30 wrap=soft>| . H($form->{"description_$i"}) . qq|</textarea><button type="button" onclick="set_longdescription_window('longdescription_$i')">| . $locale->text('L') . qq|</button></td>|;
318     } else {
319       $column_data{description} =
320         qq|<td><input name="description_$i" size=30 value="| . $form->quote($form->{"description_$i"}) . qq|"><button type="button" onclick="set_longdescription_window('longdescription_$i')">| . $locale->text('L') . qq|</button></td>|;
321     }
322
323     (my $qty_dec) = ($form->{"qty_$i"} =~ /\.(\d+)/);
324     $qty_dec = length $qty_dec;
325
326     $column_data{qty} =
327         qq|<td align=right><input name="qty_$i" size=5 value=|
328       . $form->format_amount(\%myconfig, $form->{"qty_$i"}, $qty_dec) .qq|>|;
329     if ($form->{"formel_$i"}) {
330     $column_data{qty} .= qq|<button type="button" onclick="calculate_qty_selection_window('qty_$i','alu_$i', 'formel_$i', $i)">| . $locale->text('*/') . qq|</button>
331           <input type=hidden name="formel_$i" value="$form->{"formel_$i"}"><input type=hidden name="alu_$i" value="$form->{"alu_$i"}"></td>|;
332     }
333     $column_data{ship} =
334         qq|<td align=right><input name="ship_$i" size=5 value=|
335       . $form->format_amount(\%myconfig, $form->{"ship_$i"})
336       . qq|></td>|;
337
338     my $is_part = $form->{"inventory_accno_$i"};
339     my $is_assigned = $form->{"id_$i"};
340     my $this_unit = $form->{"unit_$i"};
341     if ($form->{"selected_unit_$i"} && $this_unit &&
342         $all_units->{$form->{"selected_unit_$i"}} && $all_units->{$this_unit} &&
343         ($all_units->{$form->{"selected_unit_$i"}}->{"base_unit"} eq $all_units->{$this_unit}->{"base_unit"})) {
344       $this_unit = $form->{"selected_unit_$i"};
345     } elsif (!$is_assigned ||
346              ($is_part && !$this_unit && ($all_units->{$this_unit} && ($all_units->{$this_unit}->{"base_unit"} eq $all_units->{"kg"}->{"base_unit"})))) {
347       $this_unit = "kg";
348     }
349
350     $column_data{"unit"} = "<td>" .
351       ($qty_readonly ? "&nbsp;" :
352        AM->unit_select_html($is_part ? $dimension_units :
353                             $is_assigned ? $service_units : $all_units,
354                             "unit_$i", $this_unit,
355                             $is_assigned ? $form->{"unit_$i"} : undef))
356       . "</td>";
357
358     # build in drop down list for pricesgroups
359     if ($form->{"prices_$i"}) {
360       if  ($form->{"new_pricegroup_$i"} != $form->{"old_pricegroup_$i"}) {
361         $price_tmp = $form->format_amount(\%myconfig, $form->{"price_new_$i"}, $decimalplaces);
362       } else {
363         $price_tmp = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
364       }
365
366       $column_data{sellprice_pg} =
367         qq|<td align=right><select name="sellprice_pg_$i">$form->{"prices_$i"}</select></td>|;
368       $column_data{sellprice} =
369         qq|<td><input name="sellprice_$i" size=10 value=$price_tmp></td>|;
370     } else {
371
372       # for last row and report
373       # set pricegroup drop down list from report menu
374       if ($form->{"sellprice_$i"} != 0) {
375         $prices =
376           qq|<option value="$form->{"sellprice_$i"}--$form->{"pricegroup_id_$i"}" selected>$form->{"pricegroup_$i"}</option>\n|;
377
378         $form->{"pricegroup_old_$i"} = $form->{"pricegroup_id_$i"};
379
380         $column_data{sellprice_pg} =
381           qq|<td align=right><select name="sellprice_pg_$i">$prices</select></td>|;
382
383       } else {
384
385         # for last row
386         $column_data{sellprice_pg} = qq|<td align=right>&nbsp;</td>|;
387       }
388
389       $column_data{sellprice} =
390         qq|<td><input name="sellprice_$i" size=10 value=|
391         . $form->format_amount(\%myconfig, $form->{"sellprice_$i"},
392                                $decimalplaces)
393         . qq|></td>|;
394     }
395     $column_data{discount} =
396         qq|<td align=right><input name="discount_$i" size=3 value=|
397       . $form->format_amount(\%myconfig, $form->{"discount_$i"})
398       . qq|></td>|;
399     $column_data{linetotal} =
400         qq|<td align=right>|
401       . $form->format_amount(\%myconfig, $linetotal, 2)
402       . qq|</td>|;
403     $column_data{bin} = qq|<td>$form->{"bin_$i"}</td>|;
404
405 ########################################
406     # Eintrag fuer Version 2.2.0 geaendert #
407     # neue Optik im Rechnungsformular      #
408 ########################################
409     #     if ($lizenzen &&  $form->{type} eq "invoice" &&  $form->{vc} eq "customer") {
410     #     $column_data{license} = qq|<td><select name="licensenumber_$i">$form->{"lizenzen_$i"}></select></td>|;
411     #     }
412     #
413     #     if ($form->{type} !~ /_quotation/) {
414     #     $column_data{serialnr} = qq|<td><input name="serialnumber_$i" size=10 value="$form->{"serialnumber_$i"}"></td>|;
415     #     }
416     #
417     #     $column_data{projectnr} = qq|<td><input name="projectnumber_$i" size=10 value="$form->{"projectnumber_$i"}"></td>|;
418 ############## ENDE Neueintrag ##################
419     my $j = $i % 2;
420     print qq|
421
422         <tr valign=top class=listrow$j>|;
423
424     map { print "\n$column_data{$_}" } @column_index;
425
426     print qq|
427         </tr>
428
429 <input type=hidden name="orderitems_id_$i" value=$form->{"orderitems_id_$i"}>
430 <input type=hidden name="bo_$i" value=$form->{"bo_$i"}>
431
432 <input type=hidden name="pricegroup_old_$i" value=$form->{"pricegroup_old_$i"}>
433 <input type=hidden name="price_old_$i" value=$form->{"price_old_$i"}>
434 <input type=hidden name="unit_old_$i" value="| . $form->quote($form->{"selected_unit_$i"}) . qq|">
435 <input type=hidden name="price_new_$i" value=|
436       . $form->format_amount(\%myconfig, $form->{"price_new_$i"}) . qq|>
437
438 <input type=hidden name="id_$i" value=$form->{"id_$i"}>
439 <input type=hidden name="inventory_accno_$i" value=$form->{"inventory_accno_$i"}>
440 <input type=hidden name="bin_$i" value="$form->{"bin_$i"}">
441 <input type=hidden name="partsgroup_$i" value="$form->{"partsgroup_$i"}">
442 <input type=hidden name="partnotes_$i" value="| . $form->quote($form->{"partnotes_$i"}) . qq|">
443 <input type=hidden name="income_accno_$i" value=$form->{"income_accno_$i"}>
444 <input type=hidden name="expense_accno_$i" value=$form->{"expense_accno_$i"}>
445 <input type=hidden name="listprice_$i" value="$form->{"listprice_$i"}">
446 <input type=hidden name="assembly_$i" value="$form->{"assembly_$i"}">
447 <input type=hidden name="taxaccounts_$i" value="$form->{"taxaccounts_$i"}">
448 <input type=hidden name="ordnumber_$i" value="$form->{"ordnumber_$i"}">
449 <input type=hidden name="transdate_$i" value="$form->{"transdate_$i"}">
450 <input type=hidden name="cusordnumber_$i" value="$form->{"cusordnumber_$i"}">
451 <input type=hidden name="longdescription_$i" value="| . $form->quote($form->{"longdescription_$i"}) . qq|">
452 <input type=hidden name="basefactor_$i" value="$form->{"basefactor_$i"}">
453
454 |;
455
456 ########################################
457     # Eintrag fuer Version 2.2.0 geaendert #
458     # neue Optik im Rechnungsformular      #
459 ########################################
460
461     my $row_style_attr =
462       'style="display:none;"' if (!$form->{"show_details"});
463
464     # print second row
465     print qq|
466         <tr  class=listrow$j $row_style_attr>
467           <td colspan=$colspan>
468 |;
469     if ($lizenzen && $form->{type} eq "invoice" && $form->{vc} eq "customer") {
470       my $selected = $form->{"licensenumber_$i"};
471       my $lizenzen_quoted;
472       $form->{"lizenzen_$i"} =~ s/ selected//g;
473       $form->{"lizenzen_$i"} =~
474         s/value="${selected}"\>/value="${selected}" selected\>/;
475       $lizenzen_quoted = $form->{"lizenzen_$i"};
476       $lizenzen_quoted =~ s/\"/&quot;/g;
477       print qq|
478         <b>Lizenz\#</b>&nbsp;<select name="licensenumber_$i" size=1>
479         $form->{"lizenzen_$i"}
480         </select>
481         <input type=hidden name="lizenzen_$i" value="${lizenzen_quoted}">
482 |;
483     }
484     if ($form->{type} !~ /_quotation/) {
485       print qq|
486           <b>$serialnumber</b>&nbsp;<input name="serialnumber_$i" size=15 value="$form->{"serialnumber_$i"}">|;
487     }
488
489     print qq|
490           <b>$projectnumber</b>&nbsp;<input name="projectnumber_$i" size=10 value="$form->{"projectnumber_$i"}">
491                   <input type=hidden name="oldprojectnumber_$i" value="$form->{"projectnumber_$i"}">
492                   <input type=hidden name="project_id_$i" value="$form->{"project_id_$i"}">
493 |;
494     if ($form->{type} eq 'invoice' or $form->{type} =~ /order/) {
495       my $reqdate_term =
496         ($form->{type} eq 'invoice')
497         ? 'deliverydate'
498         : 'reqdate';    # invoice uses a different term for the same thing.
499       print qq|
500         <b>${$reqdate_term}</b>&nbsp;<input name="${reqdate_term}_$i" size=11 value="$form->{"${reqdate_term}_$i"}">
501 |;
502     }
503     my $subtotalchecked = ($form->{"subtotal_$i"}) ? "checked" : "";
504     print qq|
505           <b>|.$locale->text('Subtotal').qq|</b>&nbsp;<input type="checkbox" name="subtotal_$i" value="1" "$subtotalchecked">
506           </td>
507         </tr>
508
509 |;
510
511 ############## ENDE Neueintrag ##################
512
513     map { $form->{"${_}_base"} += $linetotal }
514       (split / /, $form->{"taxaccounts_$i"});
515
516     $form->{invsubtotal} += $linetotal;
517   }
518
519   print qq|
520       </table>
521     </td>
522   </tr>
523 |;
524
525   $lxdebug->leave_sub();
526 }
527
528 ##################################################
529 # build html-code for pricegroups in variable $form->{prices_$j}
530
531 sub set_pricegroup {
532   $lxdebug->enter_sub();
533   my $rowcount = shift;
534   for $j (1 .. $rowcount) {
535     my $pricegroup_old = $form->{"pricegroup_old_$i"};
536     if ($form->{PRICES}{$j}) {
537       $len    = 0;
538       $prices = '<option value="--">' . $locale->text("none (pricegroup)") . '</option>';
539       $price  = 0;
540       foreach $item (@{ $form->{PRICES}{$j} }) {
541
542         #$price = $form->round_amount($myconfig,  $item->{price}, 5);
543         #$price = $form->format_amount($myconfig, $item->{price}, 2);
544         $price         = $item->{price};
545         $pricegroup_id = $item->{pricegroup_id};
546         $pricegroup    = $item->{pricegroup};
547
548         # build drop down list for pricegroups
549         $prices .=
550           qq|<option value="$price--$pricegroup_id"$item->{selected}>$pricegroup</option>\n|;
551
552         $len += 1;
553
554         #        map {
555         #               $form->{"${_}_$j"} =
556         #               $form->format_amount(\%myconfig, $form->{"${_}_$j"})
557         #              } qw(sellprice price_new price_old);
558
559         # set new selectedpricegroup_id and prices for "Preis"
560         if ($item->{selected} && ($pricegroup_id != 0)) {
561           $form->{"pricegroup_old_$j"} = $pricegroup_id;
562           $form->{"price_new_$j"}      = $price;
563           $form->{"sellprice_$j"}      = $price;
564         }
565         if ($pricegroup_id == 0) {
566           $form->{"price_new_$j"} = $form->{"sellprice_$j"};
567         }
568       }
569       $form->{"prices_$j"} = $prices;
570     }
571   }
572   $lxdebug->leave_sub();
573 }
574
575 sub select_item {
576   $lxdebug->enter_sub();
577   @column_index = qw(ndx partnumber description onhand unit sellprice);
578
579   $column_data{ndx}        = qq|<th>&nbsp;</th>|;
580   $column_data{partnumber} =
581     qq|<th class=listheading>| . $locale->text('Number') . qq|</th>|;
582   $column_data{description} =
583     qq|<th class=listheading>| . $locale->text('Part Description') . qq|</th>|;
584   $column_data{sellprice} =
585     qq|<th class=listheading>| . $locale->text('Price') . qq|</th>|;
586   $column_data{onhand} =
587     qq|<th class=listheading>| . $locale->text('Qty') . qq|</th>|;
588   $column_data{unit} =
589     qq|<th class=listheading>| . $locale->text('Unit') . qq|</th>|;
590   # list items with radio button on a form
591   $form->header;
592
593   $title   = $locale->text('Select from one of the items below');
594   $colspan = $#column_index + 1;
595
596   print qq|
597 <body>
598
599 <form method=post action=$form->{script}>
600
601 <table width=100%>
602   <tr>
603     <th class=listtop colspan=$colspan>$title</th>
604   </tr>
605   <tr height="5"></tr>
606   <tr class=listheading>|;
607
608   map { print "\n$column_data{$_}" } @column_index;
609
610   print qq|</tr>|;
611
612   my $i = 0;
613   foreach $ref (@{ $form->{item_list} }) {
614     $checked = ($i++) ? "" : "checked";
615
616     if ($lizenzen) {
617       if ($ref->{inventory_accno} > 0) {
618         $ref->{"lizenzen"} = qq|<option></option>|;
619         foreach $item (@{ $form->{LIZENZEN}{ $ref->{"id"} } }) {
620           $ref->{"lizenzen"} .=
621             qq|<option value=\"$item->{"id"}\">$item->{"licensenumber"}</option>|;
622         }
623         $ref->{"lizenzen"} .= qq|<option value=-1>Neue Lizenz</option>|;
624         $ref->{"lizenzen"} =~ s/\"/&quot;/g;
625       }
626     }
627
628     map { $ref->{$_} =~ s/\"/&quot;/g } qw(partnumber description unit);
629
630     #sk tradediscount
631     $ref->{sellprice} =
632       $form->round_amount($ref->{sellprice} * (1 - $form->{tradediscount}), 2);
633     $column_data{ndx} =
634       qq|<td><input name=ndx class=radio type=radio value=$i $checked></td>|;
635     $column_data{partnumber} =
636       qq|<td><input name="new_partnumber_$i" type=hidden value="$ref->{partnumber}">$ref->{partnumber}</td>|;
637     $column_data{description} =
638       qq|<td><input name="new_description_$i" type=hidden value="$ref->{description}">$ref->{description}</td>|;
639     $column_data{sellprice} =
640       qq|<td align=right><input name="new_sellprice_$i" type=hidden value=$ref->{sellprice}>|
641       . $form->format_amount(\%myconfig, $ref->{sellprice}, 2, "&nbsp;")
642       . qq|</td>|;
643     $column_data{onhand} =
644       qq|<td align=right><input name="new_onhand_$i" type=hidden value=$ref->{onhand}>|
645       . $form->format_amount(\%myconfig, $ref->{onhand}, '', "&nbsp;")
646       . qq|</td>|;
647     $column_data{unit} =
648       qq|<td>$ref->{unit}</td>|;
649     $j++;
650     $j %= 2;
651     print qq|
652 <tr class=listrow$j>|;
653
654     map { print "\n$column_data{$_}" } @column_index;
655
656     print qq|
657 </tr>
658
659 <input name="new_bin_$i" type=hidden value="$ref->{bin}">
660 <input name="new_listprice_$i" type=hidden value=$ref->{listprice}>
661 <input name="new_inventory_accno_$i" type=hidden value=$ref->{inventory_accno}>
662 <input name="new_income_accno_$i" type=hidden value=$ref->{income_accno}>
663 <input name="new_expense_accno_$i" type=hidden value=$ref->{expense_accno}>
664 <input name="new_unit_$i" type=hidden value="$ref->{unit}">
665 <input name="new_weight_$i" type=hidden value="$ref->{weight}">
666 <input name="new_assembly_$i" type=hidden value="$ref->{assembly}">
667 <input name="new_taxaccounts_$i" type=hidden value="$ref->{taxaccounts}">
668 <input name="new_partsgroup_$i" type=hidden value="$ref->{partsgroup}">
669 <input name="new_formel_$i" type=hidden value="$ref->{formel}">
670 <input name="new_longdescription_$i" type=hidden value="$ref->{longdescription}">
671 <input name="new_not_discountable_$i" type=hidden value="$ref->{not_discountable}">
672 <input name="new_part_payment_id_$i" type=hidden value="$ref->{part_payment_id}">
673 <input name="new_partnotes_$i" type="hidden" value="| . Q($ref->{"partnotes"}) . qq|">
674
675 <input name="new_id_$i" type=hidden value=$ref->{id}>
676
677 |;
678     if ($lizenzen) {
679       print qq|
680 <input name="new_lizenzen_$i" type=hidden value="$ref->{lizenzen}">
681 |;
682     }
683
684   }
685
686   print qq|
687 <tr><td colspan=8><hr size=3 noshade></td></tr>
688 </table>
689
690 <input name=lastndx type=hidden value=$i>
691
692 |;
693
694   # delete action variable
695   map { delete $form->{$_} } qw(action item_list header);
696
697   # save all other form variables
698   foreach $key (keys %${form}) {
699     $form->{$key} =~ s/\"/&quot;/g;
700     print qq|<input name=$key type=hidden value="$form->{$key}">\n|;
701   }
702
703   print qq|
704 <input type=hidden name=nextsub value=item_selected>
705
706 <br>
707 <input class=submit type=submit name=action value="|
708     . $locale->text('Continue') . qq|">
709 </form>
710
711 </body>
712 </html>
713 |;
714
715   $lxdebug->leave_sub();
716 }
717
718 sub item_selected {
719   $lxdebug->enter_sub();
720
721   # replace the last row with the checked row
722   $i = $form->{rowcount};
723   $i = $form->{assembly_rows} if ($form->{item} eq 'assembly');
724
725   # index for new item
726   $j = $form->{ndx};
727
728   #sk
729   #($form->{"sellprice_$i"},$form->{"$pricegroup_old_$i"}) = split /--/, $form->{"sellprice_$i"};
730   #$form->{"sellprice_$i"} = $form->{"sellprice_$i"};
731
732   # if there was a price entered, override it
733   $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
734
735   map { $form->{"${_}_$i"} = $form->{"new_${_}_$j"} }
736     qw(id partnumber description sellprice listprice inventory_accno
737        income_accno expense_accno bin unit weight assembly taxaccounts
738        partsgroup formel longdescription not_discountable partnotes);
739   if ($form->{"part_payment_id_$i"} ne "") {
740     $form->{payment_id} = $form->{"part_payment_id_$i"};
741   }
742
743   if ($lizenzen) {
744     map { $form->{"${_}_$i"} = $form->{"new_${_}_$j"} } qw(lizenzen);
745   }
746
747   ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
748   $dec           = length $dec;
749   $decimalplaces = ($dec > 2) ? $dec : 2;
750
751   if ($sellprice) {
752     $form->{"sellprice_$i"} = $sellprice;
753   } else {
754
755     # if there is an exchange rate adjust sellprice
756     if (($form->{exchangerate} * 1) != 0) {
757       $form->{"sellprice_$i"} /= $form->{exchangerate};
758       $form->{"sellprice_$i"} =
759         $form->round_amount($form->{"sellprice_$i"}, $decimalplaces);
760     }
761   }
762
763   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
764     qw(sellprice listprice weight);
765
766   $form->{sellprice} += ($form->{"sellprice_$i"} * $form->{"qty_$i"});
767   $form->{weight}    += ($form->{"weight_$i"} * $form->{"qty_$i"});
768   
769   if ($form->{"not_discountable_$i"}) {
770     $form->{"discount_$i"} = 0;
771   }
772
773   $amount =
774     $form->{"sellprice_$i"} * (1 - $form->{"discount_$i"} / 100) *
775     $form->{"qty_$i"};
776   map { $form->{"${_}_base"} += $amount }
777     (split / /, $form->{"taxaccounts_$i"});
778   map { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) } split / /,
779     $form->{"taxaccounts_$i"}
780     if !$form->{taxincluded};
781
782   $form->{creditremaining} -= $amount;
783
784   $form->{"runningnumber_$i"} = $i;
785
786   # delete all the new_ variables
787   for $i (1 .. $form->{lastndx}) {
788     map { delete $form->{"new_${_}_$i"} }
789       qw(partnumber description sellprice bin listprice inventory_accno income_accno expense_accno unit assembly taxaccounts id);
790   }
791
792   map { delete $form->{$_} } qw(ndx lastndx nextsub);
793
794   # format amounts
795   map {
796     $form->{"${_}_$i"} =
797       $form->format_amount(\%myconfig, $form->{"${_}_$i"}, $decimalplaces)
798   } qw(sellprice listprice) if $form->{item} ne 'assembly';
799
800   # get pricegroups for parts
801   IS->get_pricegroups_for_parts(\%myconfig, \%$form);
802
803   # build up html code for prices_$i
804   set_pricegroup($form->{rowcount});
805
806   &display_form;
807
808   $lxdebug->leave_sub();
809 }
810
811 sub new_item {
812   $lxdebug->enter_sub();
813
814   # change callback
815   $form->{old_callback} = $form->escape($form->{callback}, 1);
816   $form->{callback} = $form->escape("$form->{script}?action=display_form", 1);
817
818   # delete action
819   delete $form->{action};
820
821   # save all other form variables in a previousform variable
822   foreach $key (keys %$form) {
823
824     # escape ampersands
825     $form->{$key} =~ s/&/%26/g;
826     $previousform .= qq|$key=$form->{$key}&|;
827   }
828   chop $previousform;
829   $previousform = $form->escape($previousform, 1);
830
831   $i = $form->{rowcount};
832   map { $form->{"${_}_$i"} =~ s/\"/&quot;/g } qw(partnumber description);
833
834   $form->header;
835
836   print qq|
837 <body>
838
839 <h4 class=error>| . $locale->text('Item not on file!') . qq|
840
841 <p>
842 | . $locale->text('What type of item is this?') . qq|</h4>
843
844 <form method=post action=ic.pl>
845
846 <p>
847
848   <input class=radio type=radio name=item value=part checked>&nbsp;|
849     . $locale->text('Part') . qq|<br>
850   <input class=radio type=radio name=item value=service>&nbsp;|
851     . $locale->text('Service')
852
853     . qq|
854 <input type=hidden name=previousform value="$previousform">
855 <input type=hidden name=partnumber value="$form->{"partnumber_$i"}">
856 <input type=hidden name=description value="$form->{"description_$i"}">
857 <input type=hidden name=rowcount value=$form->{rowcount}>
858 <input type=hidden name=taxaccount2 value=$form->{taxaccounts}>
859 <input type=hidden name=vc value=$form->{vc}>
860
861 <input type=hidden name=path value=$form->{path}>
862 <input type=hidden name=login value=$form->{login}>
863 <input type=hidden name=password value=$form->{password}>
864
865 <input type=hidden name=nextsub value=add>
866
867 <p>
868 <input class=submit type=submit name=action value="|
869     . $locale->text('Continue') . qq|">
870 </form>
871
872 </body>
873 </html>
874 |;
875
876   $lxdebug->leave_sub();
877 }
878
879 sub display_form {
880   $lxdebug->enter_sub();
881
882   relink_accounts();
883
884   $form->language_payment(\%myconfig);
885
886   # if we have a display_form
887   if ($form->{display_form}) {
888     &{"$form->{display_form}"};
889     exit;
890   }
891
892   #   if (   $form->{print_and_post}
893   #       && $form->{second_run}
894   #       && ($form->{action} eq "display_form")) {
895   #     for (keys %$form) { $old_form->{$_} = $form->{$_} }
896   #     $old_form->{rowcount}++;
897   #
898   #     #$form->{rowcount}--;
899   #     #$form->{rowcount}--;
900   #
901   #     $form->{print_and_post} = 0;
902   #
903   #     &print_form($old_form);
904   #     exit;
905   #   }
906   #
907   #   $form->{action}   = "";
908   #   $form->{resubmit} = 0;
909   #
910   #   if ($form->{print_and_post} && !$form->{second_run}) {
911   #     $form->{second_run} = 1;
912   #     $form->{action}     = "display_form";
913   #     $form->{rowcount}--;
914   #     my $rowcount = $form->{rowcount};
915   #
916   #     # get pricegroups for parts
917   #     IS->get_pricegroups_for_parts(\%myconfig, \%$form);
918   #
919   #     # build up html code for prices_$i
920   #     set_pricegroup($rowcount);
921   #
922   #     $form->{resubmit} = 1;
923   #
924   #   }
925   &form_header;
926
927   $numrows    = ++$form->{rowcount};
928   $subroutine = "display_row";
929
930   if ($form->{item} eq 'part') {
931
932     #set preisgruppenanzahl
933     $numrows    = $form->{price_rows};
934     $subroutine = "price_row";
935
936     &{$subroutine}($numrows);
937
938     $numrows    = ++$form->{makemodel_rows};
939     $subroutine = "makemodel_row";
940   }
941   if ($form->{item} eq 'assembly') {
942     $numrows    = $form->{price_rows};
943     $subroutine = "price_row";
944
945     &{$subroutine}($numrows);
946
947     $numrows    = ++$form->{makemodel_rows};
948     $subroutine = "makemodel_row";
949
950     # create makemodel rows
951     &{$subroutine}($numrows);
952
953     $numrows    = ++$form->{assembly_rows};
954     $subroutine = "assembly_row";
955   }
956   if ($form->{item} eq 'service') {
957     $numrows    = $form->{price_rows};
958     $subroutine = "price_row";
959
960     &{$subroutine}($numrows);
961
962     $numrows = 0;
963   }
964
965   # create rows
966   &{$subroutine}($numrows) if $numrows;
967
968   &form_footer;
969
970   $lxdebug->leave_sub();
971 }
972
973 sub check_form {
974   $lxdebug->enter_sub();
975   my @a     = ();
976   my $count = 0;
977   my @flds  = (
978     qw(id partnumber description qty ship sellprice unit discount inventory_accno income_accno expense_accno listprice taxaccounts bin assembly weight projectnumber project_id oldprojectnumber runningnumber serialnumber partsgroup payment_id not_discountable shop ve gv buchungsgruppen_id language_values sellprice_pg pricegroup_old price_old price_new unit_old ordnumber transdate longdescription basefactor)
979   );
980
981
982   # remove any makes or model rows
983   if ($form->{item} eq 'part') {
984     $lxdebug->message(0, "a1 $form->{sellprice}");
985     map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
986       qw(listprice sellprice lastcost weight rop);
987     $lxdebug->message(0, "a2 $form->{sellprice}");
988
989     @flds = (make, model);
990     for my $i (1 .. ($form->{makemodel_rows})) {
991       if (($form->{"make_$i"} ne "") || ($form->{"model_$i"} ne "")) {
992         push @a, {};
993         my $j = $#a;
994
995         map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
996         $count++;
997       }
998     }
999
1000     $lxdebug->message(0, "a3 $form->{sellprice}");
1001     $form->redo_rows(\@flds, \@a, $count, $form->{makemodel_rows});
1002     $lxdebug->message(0, "a4 $form->{sellprice}");
1003     $form->{makemodel_rows} = $count;
1004
1005   } elsif ($form->{item} eq 'assembly') {
1006
1007     $form->{sellprice} = 0;
1008     $form->{weight}    = 0;
1009     map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
1010       qw(listprice rop stock);
1011
1012     @flds =
1013       qw(id qty unit bom partnumber description sellprice weight runningnumber partsgroup);
1014
1015     for my $i (1 .. ($form->{assembly_rows} - 1)) {
1016       if ($form->{"qty_$i"}) {
1017         push @a, {};
1018         my $j = $#a;
1019
1020         $form->{"qty_$i"} = $form->parse_amount(\%myconfig, $form->{"qty_$i"});
1021
1022         map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
1023
1024         #($form->{"sellprice_$i"},$form->{"$pricegroup_old_$i"}) = split /--/, $form->{"sellprice_$i"};
1025
1026         $form->{sellprice} += ($form->{"qty_$i"} * $form->{"sellprice_$i"});
1027         $form->{weight}    += ($form->{"qty_$i"} * $form->{"weight_$i"});
1028         $count++;
1029       }
1030     }
1031
1032     $form->{sellprice} = $form->round_amount($form->{sellprice}, 2);
1033
1034     $form->redo_rows(\@flds, \@a, $count, $form->{assembly_rows});
1035     $form->{assembly_rows} = $count;
1036
1037     $count = 0;
1038     @flds  = qw(make model);
1039     @a     = ();
1040
1041     for my $i (1 .. ($form->{makemodel_rows})) {
1042       if (($form->{"make_$i"} ne "") || ($form->{"model_$i"} ne "")) {
1043         push @a, {};
1044         my $j = $#a;
1045
1046         map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
1047         $count++;
1048       }
1049     }
1050
1051     $form->redo_rows(\@flds, \@a, $count, $form->{makemodel_rows});
1052     $form->{makemodel_rows} = $count;
1053
1054   } else {
1055
1056     # this section applies to invoices and orders
1057     # remove any empty numbers
1058     if ($form->{rowcount}) {
1059       for my $i (1 .. $form->{rowcount} - 1) {
1060         if ($form->{"partnumber_$i"}) {
1061           push @a, {};
1062           my $j = $#a;
1063
1064           map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
1065           $count++;
1066           if ($lizenzen) {
1067             if ($form->{"licensenumber_$i"} == -1) {
1068               &new_license($i);
1069               exit;
1070             }
1071           }
1072         }
1073       }
1074
1075       $form->redo_rows(\@flds, \@a, $count, $form->{rowcount});
1076       $form->{rowcount} = $count;
1077
1078       $form->{creditremaining} -= &invoicetotal;
1079
1080     }
1081   }
1082
1083     $lxdebug->message(0, "a5 $form->{sellprice}");
1084   #sk
1085   # if pricegroups
1086   if (   $form->{type} =~ (/sales_quotation/)
1087       or (($form->{level} =~ /Sales/) and ($form->{type} =~ /invoice/))
1088       or (($form->{level} eq undef) and ($form->{type} =~ /invoice/))
1089       or ($form->{type} =~ /sales_order/)) {
1090
1091     # get pricegroups for parts
1092     IS->get_pricegroups_for_parts(\%myconfig, \%$form);
1093
1094     # build up html code for prices_$i
1095     set_pricegroup($form->{rowcount});
1096
1097   }
1098     $lxdebug->message(0, "a6 $form->{sellprice}");
1099
1100   &display_form;
1101     $lxdebug->message(0, "a7 $form->{sellprice}");
1102
1103   $lxdebug->leave_sub();
1104 }
1105
1106 sub invoicetotal {
1107   $lxdebug->enter_sub();
1108
1109   $form->{oldinvtotal} = 0;
1110
1111   # add all parts and deduct paid
1112   map { $form->{"${_}_base"} = 0 } split / /, $form->{taxaccounts};
1113
1114   my ($amount, $sellprice, $discount, $qty);
1115
1116   for my $i (1 .. $form->{rowcount}) {
1117     $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
1118     $discount  = $form->parse_amount(\%myconfig, $form->{"discount_$i"});
1119     $qty       = $form->parse_amount(\%myconfig, $form->{"qty_$i"});
1120
1121     #($form->{"sellprice_$i"}, $form->{"$pricegroup_old_$i"}) = split /--/, $form->{"sellprice_$i"};
1122
1123     $amount = $sellprice * (1 - $discount / 100) * $qty;
1124     map { $form->{"${_}_base"} += $amount }
1125       (split / /, $form->{"taxaccounts_$i"});
1126     $form->{oldinvtotal} += $amount;
1127   }
1128
1129   map { $form->{oldinvtotal} += ($form->{"${_}_base"} * $form->{"${_}_rate"}) }
1130     split / /, $form->{taxaccounts}
1131     if !$form->{taxincluded};
1132
1133   $form->{oldtotalpaid} = 0;
1134   for $i (1 .. $form->{paidaccounts}) {
1135     $form->{oldtotalpaid} += $form->{"paid_$i"};
1136   }
1137
1138   $lxdebug->leave_sub();
1139
1140   # return total
1141   return ($form->{oldinvtotal} - $form->{oldtotalpaid});
1142 }
1143
1144 sub validate_items {
1145   $lxdebug->enter_sub();
1146
1147   # check if items are valid
1148   if ($form->{rowcount} == 1) {
1149     &update;
1150     exit;
1151   }
1152
1153   for $i (1 .. $form->{rowcount} - 1) {
1154     $form->isblank("partnumber_$i",
1155                    $locale->text('Number missing in Row') . " $i");
1156   }
1157
1158   $lxdebug->leave_sub();
1159 }
1160
1161 sub order {
1162   $lxdebug->enter_sub();
1163   if ($form->{second_run}) {
1164     $form->{print_and_post} = 0;
1165   }
1166   $form->{ordnumber} = $form->{invnumber};
1167
1168   map { delete $form->{$_} } qw(id printed emailed queued);
1169   if ($form->{script} eq 'ir.pl' || $form->{type} eq 'request_quotation') {
1170     $form->{title} = $locale->text('Add Purchase Order');
1171     $form->{vc}    = 'vendor';
1172     $form->{type}  = 'purchase_order';
1173     $buysell       = 'sell';
1174   }
1175   if ($form->{script} eq 'is.pl' || $form->{type} eq 'sales_quotation') {
1176     $form->{title} = $locale->text('Add Sales Order');
1177     $form->{vc}    = 'customer';
1178     $form->{type}  = 'sales_order';
1179     $buysell       = 'buy';
1180   }
1181   $form->{script} = 'oe.pl';
1182
1183   $form->{shipto} = 1;
1184
1185   $form->{rowcount}--;
1186
1187   $form->{cp_id} *= 1;
1188
1189   require "$form->{path}/$form->{script}";
1190   my $script = $form->{"script"};
1191   $script =~ s|.*/||;
1192   $script =~ s|.pl$||;
1193   $locale = new Locale($language, $script);
1194
1195   map { $form->{"select$_"} = "" } ($form->{vc}, currency);
1196
1197   $currency = $form->{currency};
1198
1199   &order_links;
1200
1201   $form->{currency}     = $currency;
1202   $form->{exchangerate} = "";
1203   $form->{forex}        = "";
1204   $form->{exchangerate} = $exchangerate
1205     if (
1206         $form->{forex} = (
1207                   $exchangerate =
1208                     $form->check_exchangerate(
1209                     \%myconfig, $form->{currency}, $form->{transdate}, $buysell
1210                     )));
1211
1212   for $i (1 .. $form->{rowcount}) {
1213     map({ $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig,
1214                                                      $form->{"${_}_${i}"})
1215             if ($form->{"${_}_${i}"}) }
1216         qw(ship qty sellprice listprice basefactor));
1217   }
1218
1219   &prepare_order;
1220   &display_form;
1221
1222   $lxdebug->leave_sub();
1223 }
1224
1225 sub quotation {
1226   $lxdebug->enter_sub();
1227   if ($form->{second_run}) {
1228     $form->{print_and_post} = 0;
1229   }
1230   map { delete $form->{$_} } qw(id printed emailed queued);
1231
1232   if ($form->{script} eq 'ir.pl' || $form->{type} eq 'purchase_order') {
1233     $form->{title} = $locale->text('Add Request for Quotation');
1234     $form->{vc}    = 'vendor';
1235     $form->{type}  = 'request_quotation';
1236     $buysell       = 'sell';
1237   }
1238   if ($form->{script} eq 'is.pl' || $form->{type} eq 'sales_order') {
1239     $form->{title} = $locale->text('Add Quotation');
1240     $form->{vc}    = 'customer';
1241     $form->{type}  = 'sales_quotation';
1242     $buysell       = 'buy';
1243   }
1244
1245   $form->{cp_id} *= 1;
1246
1247   $form->{script} = 'oe.pl';
1248
1249   $form->{shipto} = 1;
1250
1251   $form->{rowcount}--;
1252
1253   require "$form->{path}/$form->{script}";
1254
1255   map { $form->{"select$_"} = "" } ($form->{vc}, currency);
1256
1257   $currency = $form->{currency};
1258
1259   &order_links;
1260
1261   $form->{currency}     = $currency;
1262   $form->{exchangerate} = "";
1263   $form->{forex}        = "";
1264   $form->{exchangerate} = $exchangerate
1265     if (
1266         $form->{forex} = (
1267                   $exchangerate =
1268                     $form->check_exchangerate(
1269                     \%myconfig, $form->{currency}, $form->{transdate}, $buysell
1270                     )));
1271
1272   for $i (1 .. $form->{rowcount}) {
1273     map({ $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig,
1274                                                      $form->{"${_}_${i}"})
1275             if ($form->{"${_}_${i}"}) }
1276         qw(ship qty sellprice listprice basefactor));
1277   }
1278
1279   &prepare_order;
1280   &display_form;
1281
1282   $lxdebug->leave_sub();
1283 }
1284
1285 sub request_for_quotation {
1286   quotation();
1287 }
1288
1289 sub e_mail {
1290   $lxdebug->enter_sub();
1291   if ($form->{second_run}) {
1292     $form->{print_and_post} = 0;
1293     $form->{resubmit}       = 0;
1294   }
1295   if ($myconfig{role} eq 'admin') {
1296     $bcc = qq|
1297     <tr>
1298       <th align="right" nowrap="true">| . $locale->text('Bcc') . qq|</th>
1299       <td><input name="bcc" size="30" value="| . Q($form->{bcc}) . qq|"></td>
1300     </tr>
1301 |;
1302   }
1303
1304   if ($form->{formname} =~ /(pick|packing|bin)_list/) {
1305     $form->{email} = $form->{shiptoemail} if $form->{shiptoemail};
1306   }
1307
1308   if ($form->{"cp_id"} && !$form->{"email"}) {
1309     CT->get_contact(\%myconfig, $form);
1310     $form->{"email"} = $form->{"cp_email"};
1311   }
1312
1313   $name = $form->{ $form->{vc} };
1314   $name =~ s/--.*//g;
1315   $title = $locale->text('E-mail') . " $name";
1316
1317   $form->{oldmedia} = $form->{media};
1318   $form->{media}    = "email";
1319
1320   my %formname_translations =
1321     (
1322      "bin_list" => $locale->text('Bin List'),
1323      "credit_note" => $locale->text('Credit Note'),
1324      "invoice" => $locale->text('Invoice'),
1325      "packing_list" => $locale->text('Packing List'),
1326      "pick_list" => $locale->text('Pick List'),
1327      "proforma" => $locale->text('Proforma Invoice'),
1328      "purchase_order" => $locale->text('Purchase Order'),
1329      "request_quotation" => $locale->text('RFQ'),
1330      "sales_order" => $locale->text('Confirmation'),
1331      "sales_quotation" => $locale->text('Quotation'),
1332      "storno_invoice" => $locale->text('Storno Invoice'),
1333      "storno_packing_list" => $locale->text('Storno Packing List'),
1334     );
1335
1336   my $attachment_filename = $formname_translations{$form->{"formname"}};
1337   my $prefix;
1338
1339   if ($form->{"type"} =~ /invoice/) {
1340     $prefix = "inv";
1341   } elsif ($form->{"type"} =~ /_quotation$/) {
1342     $prefix = "quo";
1343   } else {
1344     $prefix = "ord";
1345   }
1346
1347   if ($attachment_filename && $form->{"${prefix}number"}) {
1348     $attachment_filename .= "_" . $form->{"${prefix}number"} .
1349       ($form->{"format"} =~ /pdf/i ? ".pdf" :
1350        $form->{"format"} =~ /postscript/i ? ".ps" :
1351        $form->{"format"} =~ /opendocument/i ? ".odt" :
1352        $form->{"format"} =~ /html/i ? ".html" : "");
1353     $attachment_filename =~ s/ /_/g;
1354     my %umlaute =
1355       (
1356        "ä" => "ae", "ö" => "oe", "ü" => "ue",
1357        "Ä" => "Ae", "Ö" => "Oe", "Ãœ" => "Ue",
1358        "ß" => "ss"
1359       );
1360     map({ $attachment_filename =~ s/$_/$umlaute{$_}/g; } keys(%umlaute));
1361   } else {
1362     $attachment_filename = "";
1363   }
1364
1365   if ($form->{"email"}) {
1366     $form->{"fokus"} = "Form.subject";
1367   } else {
1368     $form->{"fokus"} = "Form.email";
1369   }
1370   $form->header;
1371
1372   print qq|
1373 <body onload="fokus()">
1374
1375 <form name="Form" method="post" action="$form->{script}">
1376
1377 <table width="100%">
1378   <tr class="listtop">
1379     <th class="listtop">$title</th>
1380   </tr>
1381   <tr height="5"></tr>
1382   <tr>
1383     <td>
1384       <table>
1385         <tr>
1386           <th align="right" nowrap>| . $locale->text('To') . qq|</th>
1387           <td><input name="email" size="30" value="| .
1388           Q($form->{"email"}) . qq|"></td>
1389         </tr>
1390         <tr>
1391           <th align="right" nowrap>| . $locale->text('Cc') . qq|</th>
1392           <td><input name="cc" size="30" value="| .
1393           Q($form->{"cc"}) . qq|"></td>
1394         </tr>
1395         $bcc
1396         <tr>
1397           <th align="right" nowrap>| . $locale->text('Subject') . qq|</th>
1398           <td><input name="subject" size="30" value="| .
1399           Q($form->{"subject"}) . qq|"></td>
1400         </tr>
1401         <tr>
1402           <th align="right" nowrap>| . $locale->text('Attachment name') .
1403           qq|</th>
1404           <td><input name="attachment_filename" size="30" value="| .
1405           Q($attachment_filename) . qq|"></td>
1406       </table>
1407     </td>
1408   </tr>
1409   <tr>
1410     <td>
1411       <table>
1412         <tr>
1413           <th align="left" nowrap>| . $locale->text('Message') . qq|</th>
1414         </tr>
1415         <tr>
1416           <td><textarea name="message" rows="15" cols="60" wrap="soft">| .
1417           H($form->{"message"}) . qq|</textarea></td>
1418         </tr>
1419       </table>
1420     </td>
1421   </tr>
1422   <tr>
1423     <td>
1424 |;
1425
1426   &print_options;
1427
1428   map { delete $form->{$_} }
1429     qw(action email cc bcc subject message formname sendmode format header override);
1430
1431   # save all other variables
1432   foreach $key (keys %$form) {
1433     $form->{$key} =~ s/\"/&quot;/g;
1434     print qq|<input type="hidden" name="$key" value="| . Q($form->{$key}) . qq|">\n|;
1435   }
1436
1437   print qq|
1438     </td>
1439   </tr>
1440   <tr>
1441     <td><hr size="3" noshade></td>
1442   </tr>
1443 </table>
1444
1445 <input type="hidden" name="nextsub" value="send_email">
1446
1447 <br>
1448 <input name="action" class="submit" type="submit" value="|
1449     . $locale->text('Continue') . qq|">
1450 </form>
1451
1452 </body>
1453 </html>
1454 |;
1455
1456   $lxdebug->leave_sub();
1457 }
1458
1459 sub send_email {
1460   $lxdebug->enter_sub();
1461
1462   $old_form = new Form;
1463
1464   map { $old_form->{$_} = $form->{$_} } keys %$form;
1465   $old_form->{media} = $form->{oldmedia};
1466
1467   &print_form($old_form);
1468
1469   $lxdebug->leave_sub();
1470 }
1471
1472 sub print_options {
1473   $lxdebug->enter_sub();
1474   $form->{sendmode} = "attachment";
1475
1476   $form->{"format"} =
1477     $form->{"format"} ? $form->{"format"} :
1478     $myconfig{"template_format"} ? $myconfig{"template_format"} :
1479     "pdf";
1480
1481   $form->{"copies"} =
1482     $form->{"copies"} ? $form->{"copies"} :
1483     $myconfig{"copies"} ? $myconfig{"copies"} :
1484     3;
1485
1486   $form->{"media"} =
1487     $form->{"media"} ? $form->{"media"} :
1488     $myconfig{"default_media"} ? $myconfig{"default_media"} :
1489     "screen";
1490
1491   $form->{"printer_id"} =
1492     defined($form->{"printer_id"}) ? $form->{"printer_id"} :
1493     $myconfig{"default_printer_id"} ? $myconfig{"default_printer_id"} :
1494     "";
1495
1496   $form->{PD}{ $form->{formname} } = "selected";
1497   $form->{DF}{ $form->{format} }   = "selected";
1498   $form->{OP}{ $form->{media} }    = "selected";
1499   $form->{SM}{ $form->{sendmode} } = "selected";
1500
1501   if ($form->{type} eq 'purchase_order') {
1502     $type = qq|<select name=formname>
1503             <option value=purchase_order $form->{PD}{purchase_order}>|
1504       . $locale->text('Purchase Order') . qq|
1505             <option value=bin_list $form->{PD}{bin_list}>|
1506       . $locale->text('Bin List');
1507   }
1508
1509   if ($form->{type} eq 'credit_note') {
1510     $type = qq|<select name=formname>
1511             <option value=credit_note $form->{PD}{credit_note}>|
1512       . $locale->text('Credit Note');
1513   }
1514
1515   if ($form->{type} eq 'sales_order') {
1516     $type = qq|<select name=formname>
1517             <option value=sales_order $form->{PD}{sales_order}>|
1518       . $locale->text('Confirmation') . qq|
1519       <option value=proforma $form->{PD}{proforma}>|
1520       . $locale->text('Proforma Invoice') . qq|
1521             <option value=pick_list $form->{PD}{pick_list}>|
1522       . $locale->text('Pick List') . qq|
1523             <option value=packing_list $form->{PD}{packing_list}>|
1524       . $locale->text('Packing List');
1525   }
1526
1527   if ($form->{type} =~ /_quotation$/) {
1528     $type = qq|<select name=formname>
1529             <option value="$`_quotation" $form->{PD}{"$`_quotation"}>|
1530       . $locale->text('Quotation');
1531   }
1532
1533   if ($form->{type} eq 'invoice') {
1534     $type = qq|<select name=formname>
1535             <option value=invoice $form->{PD}{invoice}>|
1536       . $locale->text('Invoice') . qq|
1537       <option value=proforma $form->{PD}{proforma}>|
1538       . $locale->text('Proforma Invoice') . qq|
1539       <option value=packing_list $form->{PD}{packing_list}>|
1540       . $locale->text('Packing List');
1541   }
1542
1543   if ($form->{type} eq 'invoice' && $form->{storno}) {
1544     $type = qq|<select name=formname>
1545             <option value=storno_invoice $form->{PD}{storno_invoice}>|
1546       . $locale->text('Storno Invoice') . qq|
1547       <option value=storno_packing_list $form->{PD}{storno_packing_list}>|
1548       . $locale->text('Storno Packing List');
1549   }
1550
1551   if ($form->{type} eq 'credit_note') {
1552     $type = qq|<select name=formname>
1553             <option value=credit_note $form->{PD}{credit_note}>|
1554       . $locale->text('Credit Note');
1555   }
1556
1557   if ($form->{type} eq 'ship_order') {
1558     $type = qq|<select name=formname>
1559             <option value=pick_list $form->{PD}{pick_list}>|
1560       . $locale->text('Pick List') . qq|
1561             <option value=packing_list $form->{PD}{packing_list}>|
1562       . $locale->text('Packing List');
1563   }
1564
1565   if ($form->{type} eq 'receive_order') {
1566     $type = qq|<select name=formname>
1567             <option value=bin_list $form->{PD}{bin_list}>|
1568       . $locale->text('Bin List');
1569   }
1570
1571   if ($form->{media} eq 'email') {
1572     $media = qq|<select name=sendmode>
1573             <option value=attachment $form->{SM}{attachment}>|
1574       . $locale->text('Attachment') . qq|
1575             <option value=inline $form->{SM}{inline}>| . $locale->text('In-line');
1576   } else {
1577     $media = qq|<select name=media>
1578             <option value=screen $form->{OP}{screen}>| . $locale->text('Screen');
1579     if (scalar(keys (%{ $form->{printers} })) !=0 && $latex_templates) {
1580       $media .= qq|
1581             <option value=printer $form->{OP}{printer}>|
1582         . $locale->text('Printer');
1583     }
1584     if ($latex_templates) {
1585       $media .= qq|
1586             <option value=queue $form->{OP}{queue}>| . $locale->text('Queue');
1587     }
1588   }
1589
1590   $format = qq|<select name=format>|;
1591   if ($opendocument_templates && $openofficeorg_writer_bin &&
1592       $xvfb_bin && (-x $openofficeorg_writer_bin) && (-x $xvfb_bin)) {
1593     $format .= qq|<option value=opendocument_pdf | .
1594       $form->{DF}{"opendocument_pdf"} . qq|>| .
1595       $locale->text("PDF (OpenDocument/OASIS)") . qq|</option>|;
1596   }
1597
1598   if ($latex_templates) {
1599     $format .= qq|<option value=pdf $form->{DF}{pdf}>| .
1600       $locale->text('PDF') . qq|</option>|;
1601   }
1602
1603   $format .= qq|<option value=html $form->{DF}{html}>HTML</option>|;
1604
1605   if ($latex_templates) {
1606     $format .= qq|<option value=postscript $form->{DF}{postscript}>| .
1607       $locale->text('Postscript') . qq|</option>|;
1608   }
1609
1610   if ($opendocument_templates) {
1611     $format .= qq|<option value=opendocument $form->{DF}{opendocument}>| .
1612       $locale->text("OpenDocument/OASIS") . qq|</option>|;
1613   }
1614   $format .= qq|</select>|;
1615
1616   if (scalar(keys (%{ $form->{languages} })) !=0) {
1617     $language_select = qq|<select name=language_id>
1618                 <option value=""></option>}|;
1619     foreach $item (@{ $form->{languages} }) {
1620       if ($form->{language_id} eq $item->{id}) {
1621         $language_select .= qq|<option value="$item->{id}" selected>$item->{description}</option>|;
1622       } else {
1623         $language_select .= qq|<option value="$item->{id}">$item->{description}</option>|;
1624       }
1625     }
1626   }
1627
1628   if (scalar(keys (%{ $form->{printers} })) !=0) {
1629     my $selected = !$form->{"printer_id"} ? "selected" : "";
1630     $printer_select = qq|<select name=printer_id $selected>
1631                   <option value=""></option>|;
1632     foreach $item (@{ $form->{printers} }) {
1633       $selected = $item->{"id"} == $form->{"printer_id"} ? "selected" : "";
1634       $printer_select .= qq|<option value="$item->{id}" $selected>$item->{printer_description}</option>|;
1635     }
1636   }
1637
1638
1639
1640   print qq|
1641 <table width=100% cellspacing=0 cellpadding=0>
1642   <tr>
1643     <td>
1644       <table>
1645         <tr>
1646           <td>$type</select></td>|;
1647   if (scalar(keys (%{ $form->{languages} })) !=0) {
1648   print qq|
1649           <td>${language_select}</select></td>|;
1650   }
1651   print qq|
1652           <td>$format</select></td>
1653           <td>$media</select></td>|;
1654   if (scalar(keys (%{ $form->{printers} })) !=0) {
1655   print qq|
1656           <td>$printer_select</select></td>
1657 |;
1658   }
1659
1660   if (scalar(keys (%{ $form->{printers} })) !=0 && $latex_templates && $form->{media} ne 'email') {
1661     print qq|
1662           <td>| . $locale->text('Copies') . qq|
1663           <input name=copies size=2 value=$form->{copies}></td>
1664 |;
1665   }
1666
1667   $form->{groupitems} = "checked" if $form->{groupitems};
1668
1669   print qq|
1670           <td>| . $locale->text('Group Items') . qq|</td>
1671           <td><input name=groupitems type=checkbox class=checkbox $form->{groupitems}></td>
1672         </tr>
1673       </table>
1674     </td>
1675     <td align=right>
1676       <table>
1677         <tr>
1678 |;
1679
1680   if ($form->{printed} =~ /$form->{formname}/) {
1681     print qq|
1682           <th>\|| . $locale->text('Printed') . qq|\|</th>
1683 |;
1684   }
1685
1686   if ($form->{emailed} =~ /$form->{formname}/) {
1687     print qq|
1688           <th>\|| . $locale->text('E-mailed') . qq|\|</th>
1689 |;
1690   }
1691
1692   if ($form->{queued} =~ /$form->{formname}/) {
1693     print qq|
1694           <th>\|| . $locale->text('Queued') . qq|\|</th>
1695 |;
1696   }
1697
1698   print qq|
1699         </tr>
1700       </table>
1701     </td>
1702   </tr>
1703 </table>
1704 |;
1705
1706   $lxdebug->leave_sub();
1707 }
1708
1709 sub print {
1710   $lxdebug->enter_sub();
1711
1712   # if this goes to the printer pass through
1713   if ($form->{media} eq 'printer' || $form->{media} eq 'queue') {
1714     $form->error($locale->text('Select postscript or PDF!'))
1715       if ($form->{format} !~ /(postscript|pdf)/);
1716
1717     $old_form = new Form;
1718     map { $old_form->{$_} = $form->{$_} } keys %$form;
1719   }
1720
1721   if (!$form->{id} || (($form->{formname} eq "proforma") && !$form->{proforma} && (($form->{type} =~ /_order$/) || ($form->{type} =~ /_quotation$/)))) {
1722     if ($form->{formname} eq "proforma") {
1723       $form->{proforma} = 1;
1724     }
1725     $form->{print_and_save} = 1;
1726     my $formname = $form->{formname};
1727     &save();
1728     $form->{formname} = $formname;
1729     &edit();
1730     exit;
1731   }
1732
1733   &print_form($old_form);
1734
1735   $lxdebug->leave_sub();
1736 }
1737
1738 sub print_form {
1739   $lxdebug->enter_sub();
1740   my ($old_form) = @_;
1741
1742   $inv       = "inv";
1743   $due       = "due";
1744   $numberfld = "invnumber";
1745
1746   $display_form =
1747     ($form->{display_form}) ? $form->{display_form} : "display_form";
1748
1749   # $form->{"notes"} will be overridden by the customer's/vendor's "notes" field. So save it here.
1750   $form->{ $form->{"formname"} . "notes" } = $form->{"notes"};
1751
1752   if ($form->{formname} eq "invoice") {
1753     $form->{label} = $locale->text('Invoice');
1754   }
1755   if ($form->{formname} eq "packing_list") {
1756
1757     # this is from an invoice
1758     $form->{label} = $locale->text('Packing List');
1759   }
1760   if ($form->{formname} eq 'sales_order') {
1761     $inv                  = "ord";
1762     $due                  = "req";
1763     $form->{"${inv}date"} = $form->{transdate};
1764     $form->{label}        = $locale->text('Sales Order');
1765     $numberfld            = "sonumber";
1766     $order                = 1;
1767   }
1768
1769   if (($form->{type} eq 'invoice') && ($form->{formname} eq 'proforma') ) {
1770     $inv                  = "inv";
1771     $due                  = "due";
1772     $form->{"${inv}date"} = $form->{invdate};
1773     $form->{label}        = $locale->text('Proforma Invoice');
1774     $numberfld            = "sonumber";
1775     $order                = 0;
1776   }
1777
1778   if (($form->{type} eq 'sales_order') && ($form->{formname} eq 'proforma') ) {
1779     $inv                  = "inv";
1780     $due                  = "due";
1781     $form->{"${inv}date"} = $form->{transdate};
1782     $form->{"invdate"}    = $form->{transdate};
1783     $form->{invnumber}    = $form->{ordnumber};
1784     $form->{label}        = $locale->text('Proforma Invoice');
1785     $numberfld            = "sonumber";
1786     $order                = 1;
1787   }
1788
1789   if ($form->{formname} eq 'packing_list' && $form->{type} ne 'invoice') {
1790
1791     # we use the same packing list as from an invoice
1792     $inv = "ord";
1793     $due = "req";
1794     $form->{invdate} = $form->{"${inv}date"} = $form->{transdate};
1795     $form->{label} = $locale->text('Packing List');
1796     $order = 1;
1797     # set invnumber for template packing_list 
1798     $form->{invnumber}   = $form->{ordnumber};
1799   }
1800   if ($form->{formname} eq 'pick_list') {
1801     $inv                  = "ord";
1802     $due                  = "req";
1803     $form->{"${inv}date"} =
1804       ($form->{transdate}) ? $form->{transdate} : $form->{invdate};
1805     $form->{label} = $locale->text('Pick List');
1806     $order = 1 unless $form->{type} eq 'invoice';
1807   }
1808   if ($form->{formname} eq 'purchase_order') {
1809     $inv                  = "ord";
1810     $due                  = "req";
1811     $form->{"${inv}date"} = $form->{transdate};
1812     $form->{label}        = $locale->text('Purchase Order');
1813     $numberfld            = "ponumber";
1814     $order                = 1;
1815   }
1816   if ($form->{formname} eq 'bin_list') {
1817     $inv                  = "ord";
1818     $due                  = "req";
1819     $form->{"${inv}date"} = $form->{transdate};
1820     $form->{label}        = $locale->text('Bin List');
1821     $order                = 1;
1822   }
1823   if ($form->{formname} eq 'sales_quotation') {
1824     $inv                  = "quo";
1825     $due                  = "req";
1826     $form->{"${inv}date"} = $form->{transdate};
1827     $form->{label}        = $locale->text('Quotation');
1828     $numberfld            = "sqnumber";
1829     $order                = 1;
1830   }
1831
1832   if (($form->{type} eq 'sales_quotation') && ($form->{formname} eq 'proforma') ) {
1833     $inv                  = "quo";
1834     $due                  = "req";
1835     $form->{"${inv}date"} = $form->{transdate};
1836     $form->{"invdate"}    = $form->{transdate};
1837     $form->{label}        = $locale->text('Proforma Invoice');
1838     $numberfld            = "sqnumber";
1839     $order                = 1;
1840   }
1841
1842   if ($form->{formname} eq 'request_quotation') {
1843     $inv                  = "quo";
1844     $due                  = "req";
1845     $form->{"${inv}date"} = $form->{transdate};
1846     $form->{label}        = $locale->text('Quotation');
1847     $numberfld            = "rfqnumber";
1848     $order                = 1;
1849   }
1850
1851   $form->isblank("email", $locale->text('E-mail address missing!'))
1852     if ($form->{media} eq 'email');
1853   $form->isblank("${inv}date",
1854            $locale->text($form->{label}) 
1855            . ": "
1856            . $locale->text(' Date missing!'));
1857
1858   # $locale->text('Invoice Number missing!')
1859   # $locale->text('Invoice Date missing!')
1860   # $locale->text('Packing List Number missing!')
1861   # $locale->text('Packing List Date missing!')
1862   # $locale->text('Order Number missing!')
1863   # $locale->text('Order Date missing!')
1864   # $locale->text('Quotation Number missing!')
1865   # $locale->text('Quotation Date missing!')
1866
1867   # assign number
1868   if (!$form->{"${inv}number"} && !$form->{preview}) {
1869     $form->{"${inv}number"} = $form->update_defaults(\%myconfig, $numberfld);
1870     if ($form->{media} ne 'email') {
1871
1872       # get pricegroups for parts
1873       IS->get_pricegroups_for_parts(\%myconfig, \%$form);
1874
1875       # build up html code for prices_$i
1876       set_pricegroup($form->{rowcount});
1877
1878       $form->{rowcount}--;
1879
1880       &{"$display_form"};
1881       exit;
1882     }
1883   }
1884
1885   &validate_items;
1886
1887   # Save the email address given in the form because it should override the setting saved for the customer/vendor.
1888   my ($saved_email, $saved_cc, $saved_bcc) =
1889     ($form->{"email"}, $form->{"cc"}, $form->{"bcc"});
1890
1891   $language_saved = $form->{language_id};
1892   $payment_id_saved = $form->{payment_id};
1893
1894   &{"$form->{vc}_details"}();
1895
1896   $form->{language_id} = $language_saved;
1897   $form->{payment_id} = $payment_id_saved;
1898
1899   $form->{"email"} = $saved_email if ($saved_email);
1900   $form->{"cc"}    = $saved_cc    if ($saved_cc);
1901   $form->{"bcc"}   = $saved_bcc   if ($saved_bcc);
1902
1903   my ($language_tc, $output_numberformat, $output_dateformat, $output_longdates);
1904   if ($form->{"language_id"}) {
1905     ($language_tc, $output_numberformat, $output_dateformat, $output_longdates) =
1906       AM->get_language_details(\%myconfig, $form, $form->{language_id});
1907   } else {
1908     $output_dateformat = $myconfig{"dateformat"};
1909     $output_numberformat = $myconfig{"numberformat"};
1910     $output_longdates = 1;
1911   }
1912
1913   ($form->{employee}) = split /--/, $form->{employee};
1914   ($form->{warehouse}, $form->{warehouse_id}) = split /--/, $form->{warehouse};
1915
1916   # create the form variables
1917   if ($order) {
1918     OE->order_details(\%myconfig, \%$form);
1919   } else {
1920     IS->invoice_details(\%myconfig, \%$form, $locale);
1921   }
1922
1923   if ($form->{shipto_id}) {
1924     $form->get_shipto(\%myconfig);
1925   }
1926
1927   @a = qw(name street zipcode city country);
1928
1929   $shipto = 1;
1930
1931   # if there is no shipto fill it in from billto
1932   foreach $item (@a) {
1933     if ($form->{"shipto$item"}) {
1934       $shipto = 0;
1935       last;
1936     }
1937   }
1938
1939   if ($shipto) {
1940     if (   $form->{formname} eq 'purchase_order'
1941         || $form->{formname} eq 'request_quotation') {
1942       $form->{shiptoname}   = $myconfig{company};
1943       $form->{shiptostreet} = $myconfig{address};
1944     } else {
1945       map { $form->{"shipto$_"} = $form->{$_} } @a;
1946     }
1947   }
1948
1949   $form->{notes} =~ s/^\s+//g;
1950
1951   $form->{templates} = "$myconfig{templates}";
1952
1953   $form->{language} = $form->get_template_language(\%myconfig);
1954   $form->{printer_code} = $form->get_printer_code(\%myconfig);
1955
1956   if ($form->{language} ne "") {
1957     map({ $form->{"unit"}->[$_] =
1958             AM->translate_units($form, $form->{"language"},
1959                                 $form->{"unit"}->[$_], $form->{"qty"}->[$_]); }
1960         (0..scalar(@{$form->{"unit"}}) - 1));
1961     $form->{language} = "_" . $form->{language};
1962   }
1963
1964   # Format dates.
1965   format_dates($output_dateformat, $output_longdates,
1966                qw(invdate orddate quodate pldate duedate reqdate transdate
1967                   shippingdate deliverydate validitydate paymentdate
1968                   datepaid transdate_oe deliverydate_oe
1969                   employee_startdate employee_enddate
1970                   ),
1971                grep({ /^datepaid_\d+$/ ||
1972                         /^transdate_oe_\d+$/ ||
1973                         /^deliverydate_oe_\d+$/ ||
1974                         /^reqdate_\d+$/ ||
1975                         /^deliverydate_\d+$/ ||
1976                         /^transdate_\d+$/
1977                     } keys(%{$form})));
1978
1979   reformat_numbers($output_numberformat, 2,
1980                    qw(invtotal ordtotal quototal subtotal linetotal
1981                       listprice sellprice netprice discount
1982                       tax taxbase),
1983                    grep({ /^linetotal_\d+$/ ||
1984                             /^listprice_\d+$/ ||
1985                             /^sellprice_\d+$/ ||
1986                             /^netprice_\d+$/ ||
1987                             /^taxbase_\d+$/ ||
1988                             /^discount_\d+$/ ||
1989                             /^tax_\d+$/
1990                         } keys(%{$form})));
1991
1992   reformat_numbers($output_numberformat, undef,
1993                    qw(qty),
1994                    grep({ /^qty_\d+$/
1995                         } keys(%{$form})));
1996
1997   if ($form->{printer_code} ne "") {
1998     $form->{printer_code} = "_" . $form->{printer_code};
1999   }
2000
2001   $form->{IN} = "$form->{formname}$form->{language}$form->{printer_code}.html";
2002   if ($form->{format} eq 'postscript') {
2003     $form->{postscript} = 1;
2004     $form->{IN} =~ s/html$/tex/;
2005   } elsif ($form->{"format"} =~ /pdf/) {
2006     $form->{pdf} = 1;
2007     if ($form->{"format"} =~ /opendocument/) {
2008       $form->{IN} =~ s/html$/odt/;
2009     } else {
2010       $form->{IN} =~ s/html$/tex/;
2011     }
2012   } elsif ($form->{"format"} =~ /opendocument/) {
2013     $form->{"opendocument"} = 1;
2014     $form->{"IN"} =~ s/html$/odt/;
2015   }
2016
2017   if ($form->{media} eq 'printer') {
2018     $form->{OUT} = "| $form->{printer_command} &>/dev/null";
2019     $form->{printed} .= " $form->{formname}";
2020     $form->{printed} =~ s/^ //;
2021   }
2022   $printed = $form->{printed};
2023
2024   if ($form->{media} eq 'email') {
2025     $form->{subject} = qq|$form->{label} $form->{"${inv}number"}|
2026       unless $form->{subject};
2027
2028     $form->{OUT} = "$sendmail";
2029
2030     $form->{emailed} .= " $form->{formname}";
2031     $form->{emailed} =~ s/^ //;
2032   }
2033   $emailed = $form->{emailed};
2034
2035   if ($form->{media} eq 'queue') {
2036     %queued = split / /, $form->{queued};
2037
2038     if ($filename = $queued{ $form->{formname} }) {
2039       $form->{queued} =~ s/$form->{formname} $filename//;
2040       unlink "$spool/$filename";
2041       $filename =~ s/\..*$//g;
2042     } else {
2043       $filename = time;
2044       $filename .= $$;
2045     }
2046
2047     $filename .= ($form->{postscript}) ? '.ps' : '.pdf';
2048     $form->{OUT} = ">$spool/$filename";
2049
2050     # add type
2051     $form->{queued} .= " $form->{formname} $filename";
2052
2053     $form->{queued} =~ s/^ //;
2054   }
2055   $queued = $form->{queued};
2056
2057   $form->parse_template(\%myconfig, $userspath);
2058
2059   $form->{callback} = "";
2060
2061   if ($form->{media} eq 'email') {
2062     $form->{message} = $locale->text('sent') unless $form->{message};
2063   }
2064   $message = $form->{message};
2065
2066   # if we got back here restore the previous form
2067   if ($form->{media} =~ /(printer|email|queue)/) {
2068
2069     $form->update_status(\%myconfig)
2070       if ($form->{media} eq 'queue' && $form->{id});
2071
2072     if ($old_form) {
2073
2074       $old_form->{"${inv}number"} = $form->{"${inv}number"};
2075
2076       # restore and display form
2077       map { $form->{$_} = $old_form->{$_} } keys %$old_form;
2078
2079       $form->{queued}  = $queued;
2080       $form->{printed} = $printed;
2081       $form->{emailed} = $emailed;
2082       $form->{message} = $message;
2083
2084       $form->{rowcount}--;
2085       map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
2086         qw(exchangerate creditlimit creditremaining);
2087
2088       for $i (1 .. $form->{paidaccounts}) {
2089         map {
2090           $form->{"${_}_$i"} =
2091             $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
2092         } qw(paid exchangerate);
2093       }
2094
2095       &{"$display_form"};
2096       exit;
2097     }
2098
2099     $msg =
2100       ($form->{media} eq 'printer')
2101       ? $locale->text('sent to printer')
2102       : $locale->text('emailed to') . " $form->{email}";
2103     $form->redirect(qq|$form->{label} $form->{"${inv}number"} $msg|);
2104   }
2105   if ($form->{printing}) {
2106    &{"$display_form"};
2107    exit; 
2108   }
2109
2110   $lxdebug->leave_sub();
2111 }
2112
2113 sub customer_details {
2114   $lxdebug->enter_sub();
2115   IS->customer_details(\%myconfig, \%$form, @_);
2116   $lxdebug->leave_sub();
2117 }
2118
2119 sub vendor_details {
2120   $lxdebug->enter_sub();
2121
2122   IR->vendor_details(\%myconfig, \%$form, @_);
2123
2124   $lxdebug->leave_sub();
2125 }
2126
2127 sub post_as_new {
2128   $lxdebug->enter_sub();
2129
2130   $form->{postasnew} = 1;
2131   map { delete $form->{$_} } qw(printed emailed queued);
2132
2133   &post;
2134
2135   $lxdebug->leave_sub();
2136 }
2137
2138 sub ship_to {
2139   $lxdebug->enter_sub();
2140   if ($form->{second_run}) {
2141     $form->{print_and_post} = 0;
2142   }
2143
2144   $title = $form->{title};
2145   $form->{title} = $locale->text('Ship to');
2146
2147   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
2148     qw(exchangerate creditlimit creditremaining);
2149
2150   my @shipto_vars =
2151     qw(shiptoname shiptostreet shiptozipcode shiptocity shiptocountry
2152        shiptocontact shiptophone shiptofax shiptoemail
2153        shiptodepartment_1 shiptodepartment_2);
2154
2155   my @addr_vars =
2156     (qw(name department_1 department_2 street zipcode city country
2157         contact email phone fax));
2158
2159   # get details for name
2160   &{"$form->{vc}_details"}(@addr_vars);
2161
2162   $number =
2163     ($form->{vc} eq 'customer')
2164     ? $locale->text('Customer Number')
2165     : $locale->text('Vendor Number');
2166
2167   # get pricegroups for parts
2168   IS->get_pricegroups_for_parts(\%myconfig, \%$form);
2169
2170   # build up html code for prices_$i
2171   set_pricegroup($form->{rowcount});
2172
2173   $nextsub = ($form->{display_form}) ? $form->{display_form} : "display_form";
2174
2175   $form->{rowcount}--;
2176
2177   $form->header;
2178
2179   print qq|
2180 <body>
2181
2182 <form method=post action=$form->{script}>
2183
2184 <table width=100%>
2185   <tr>
2186     <td>
2187       <table>
2188         <tr class=listheading>
2189           <th class=listheading colspan=2 width=50%>|
2190     . $locale->text('Billing Address') . qq|</th>
2191           <th class=listheading width=50%>|
2192     . $locale->text('Shipping Address') . qq|</th>
2193         </tr>
2194         <tr height="5"></tr>
2195         <tr>
2196           <th align=right nowrap>$number</th>
2197           <td>$form->{"$form->{vc}number"}</td>
2198         </tr>
2199         <tr>
2200           <th align=right nowrap>| . $locale->text('Company Name') . qq|</th>
2201           <td>$form->{name}</td>
2202           <td><input name=shiptoname size=35 value="$form->{shiptoname}"></td>
2203         </tr>
2204         <tr>
2205           <th align=right nowrap>| . $locale->text('Department') . qq|</th>
2206           <td>$form->{department_1}</td>
2207           <td><input name=shiptodepartment_1 size=35 value="$form->{shiptodepartment_1}"></td>
2208         </tr>
2209         <tr>
2210           <th align=right nowrap>&nbsp;</th>
2211           <td>$form->{department_2}</td>
2212           <td><input name=shiptodepartment_2 size=35 value="$form->{shiptodepartment_2}"></td>
2213         </tr>
2214         <tr>
2215           <th align=right nowrap>| . $locale->text('Street') . qq|</th>
2216           <td>$form->{street}</td>
2217           <td><input name=shiptostreet size=35 value="$form->{shiptostreet}"></td>
2218         </tr>
2219         <tr>
2220           <th align=right nowrap>| . $locale->text('Zipcode') . qq|</th>
2221           <td>$form->{zipcode}</td>
2222           <td><input name=shiptozipcode size=35 value="$form->{shiptozipcode}"></td>
2223         </tr>
2224         <tr>
2225           <th align=right nowrap>| . $locale->text('City') . qq|</th>
2226           <td>$form->{city}</td>
2227           <td><input name=shiptocity size=35 value="$form->{shiptocity}"></td>
2228         </tr>
2229         <tr>
2230           <th align=right nowrap>| . $locale->text('Country') . qq|</th>
2231           <td>$form->{country}</td>
2232           <td><input name=shiptocountry size=35 value="$form->{shiptocountry}"></td>
2233         </tr>
2234         <tr>
2235           <th align=right nowrap>| . $locale->text('Contact') . qq|</th>
2236           <td>$form->{contact}</td>
2237           <td><input name=shiptocontact size=35 value="$form->{shiptocontact}"></td>
2238         </tr>
2239         <tr>
2240           <th align=right nowrap>| . $locale->text('Phone') . qq|</th>
2241           <td>$form->{phone}</td>
2242           <td><input name=shiptophone size=20 value="$form->{shiptophone}"></td>
2243         </tr>
2244         <tr>
2245           <th align=right nowrap>| . $locale->text('Fax') . qq|</th>
2246           <td>$form->{fax}</td>
2247           <td><input name=shiptofax size=20 value="$form->{shiptofax}"></td>
2248         </tr>
2249         <tr>
2250           <th align=right nowrap>| . $locale->text('E-mail') . qq|</th>
2251           <td>$form->{email}</td>
2252           <td><input name=shiptoemail size=35 value="$form->{shiptoemail}"></td>
2253         </tr>
2254       </table>
2255     </td>
2256   </tr>
2257 </table>
2258
2259 <input type=hidden name=nextsub value=$nextsub>
2260 |;
2261
2262   # delete shipto
2263   map({ delete $form->{$_} } (@shipto_vars, qw(header)));
2264   $form->{title} = $title;
2265
2266   foreach $key (keys %$form) {
2267     $form->{$key} =~ s/\"/&quot;/g;
2268     print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
2269   }
2270
2271   print qq|
2272
2273 <hr size=3 noshade>
2274
2275 <br>
2276 <input class=submit type=submit name=action value="|
2277     . $locale->text('Continue') . qq|">
2278 </form>
2279
2280 </body>
2281 </html>
2282 |;
2283
2284   $lxdebug->leave_sub();
2285 }
2286
2287 sub new_license {
2288   $lxdebug->enter_sub();
2289
2290   my $row = shift;
2291
2292   # change callback
2293   $form->{old_callback} = $form->escape($form->{callback}, 1);
2294   $form->{callback} = $form->escape("$form->{script}?action=display_form", 1);
2295   $form->{old_callback} = $form->escape($form->{old_callback}, 1);
2296
2297   # delete action
2298   delete $form->{action};
2299   $customer = $form->{customer};
2300   map { $form->{"old_$_"} = $form->{"${_}_$row"} } qw(partnumber description);
2301
2302   # save all other form variables in a previousform variable
2303   $form->{row} = $row;
2304   foreach $key (keys %$form) {
2305
2306     # escape ampersands
2307     $form->{$key} =~ s/&/%26/g;
2308     $previousform .= qq|$key=$form->{$key}&|;
2309   }
2310   chop $previousform;
2311   $previousform = $form->escape($previousform, 1);
2312
2313   $form->{script} = "licenses.pl";
2314
2315   map { $form->{$_} = $form->{"old_$_"} } qw(partnumber description);
2316   map { $form->{$_} = $form->escape($form->{$_}, 1) }
2317     qw(partnumber description);
2318   $form->{callback} =
2319     qq|$form->{script}?login=$form->{login}&path=$form->{path}&password=$form->{password}&action=add&vc=$form->{db}&$form->{db}_id=$form->{id}&$form->{db}=$name&type=$form->{type}&customer=$customer&partnumber=$form->{partnumber}&description=$form->{description}&previousform="$previousform"&initial=1|;
2320   $form->redirect;
2321
2322   $lxdebug->leave_sub();
2323 }
2324
2325 sub relink_accounts {
2326   $lxdebug->enter_sub();
2327
2328   $form->{"taxaccounts"} =~ s/\s*$//;
2329   $form->{"taxaccounts"} =~ s/^\s*//;
2330   foreach my $accno (split(/\s*/, $form->{"taxaccounts"})) {
2331     map({ delete($form->{"${accno}_${_}"}); } qw(rate description taxnumber));
2332   }
2333   $form->{"taxaccounts"} = "";
2334
2335   for (my $i = 1; $i <= $form->{"rowcount"}; $i++) {
2336     if ($form->{"id_$i"}) {
2337       IC->retrieve_accounts(\%myconfig, $form, $form->{"id_$i"}, $i, 1);
2338     }
2339   }
2340
2341   $lxdebug->leave_sub();
2342 }
2343
2344
2345 sub set_duedate {
2346   $lxdebug->enter_sub();
2347
2348   $form->get_duedate(\%myconfig);
2349
2350   my $q = new CGI;
2351   $result = "$form->{duedate}";
2352   print $q->header();
2353   print $result;
2354   $lxdebug->leave_sub();
2355
2356 }
2357