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