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