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