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