Historie eingefügt
[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        "