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