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