Um die Benutzung des Template Systems mal ein wenig zu foerdern.
[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 CGI;
37 use CGI::Ajax;
38 use List::Util qw(max);
39
40 use SL::Common;
41 use SL::CT;
42 use SL::IC;
43
44 require "bin/mozilla/common.pl";
45
46 # any custom scripts for this one
47 if (-f "bin/mozilla/custom_io.pl") {
48   eval { require "bin/mozilla/custom_io.pl"; };
49 }
50 if (-f "bin/mozilla/$form->{login}_io.pl") {
51   eval { require "bin/mozilla/$form->{login}_io.pl"; };
52 }
53
54 1;
55
56 # end of main
57
58 # this is for our long dates
59 # $locale->text('January')
60 # $locale->text('February')
61 # $locale->text('March')
62 # $locale->text('April')
63 # $locale->text('May ')
64 # $locale->text('June')
65 # $locale->text('July')
66 # $locale->text('August')
67 # $locale->text('September')
68 # $locale->text('October')
69 # $locale->text('November')
70 # $locale->text('December')
71
72 # this is for our short month
73 # $locale->text('Jan')
74 # $locale->text('Feb')
75 # $locale->text('Mar')
76 # $locale->text('Apr')
77 # $locale->text('May')
78 # $locale->text('Jun')
79 # $locale->text('Jul')
80 # $locale->text('Aug')
81 # $locale->text('Sep')
82 # $locale->text('Oct')
83 # $locale->text('Nov')
84 # $locale->text('Dec')
85 use SL::IS;
86 use SL::PE;
87 use SL::AM;
88 use Data::Dumper;
89 ########################################
90 # Eintrag fuer Version 2.2.0 geaendert #
91 # neue Optik im Rechnungsformular      #
92 ########################################
93 sub display_row {
94   $lxdebug->enter_sub();
95   my $numrows = shift;
96
97   # column_index
98   my @header_sort = qw(runningnumber partnumber description ship qty unit sellprice_pg sellprice discount linetotal);
99   my @HEADER = (
100     {  id => 'runningnumber', width => 5,     value => $locale->text('No.'),                  display => 1, },
101     {  id => 'partnumber',    width => 12,    value => $locale->text('Number'),               display => 1, },
102     {  id => 'description',   width => 30,    value => $locale->text('Part Description'),     display => 1, },
103     {  id => 'ship',          width => 5,     value => ($form->{type} eq 'purchase_order' ? $locale->text('Ship rcvd') : $locale->text('Ship')),                 
104        display => $form->{type} =~ /sales_order/ || ($form->{type} =~ /purchase_order/ && !($lizenzen && $form->{vc} eq "customer")) , },
105     {  id => 'qty',           width => 5,     value => $locale->text('Qty'),                  display => 1, },
106     {  id => 'unit',          width => 5,     value => $locale->text('Unit'),                 display => 1, },
107     {  id => 'license',       width => 10,    value => $locale->text('License'),              display => 0, },
108     {  id => 'serialnr',      width => 10,    value => $locale->text('Serial No.'),           display => 0, },
109     {  id => 'projectnr',     width => 10,    value => $locale->text('Project'),              display => 0, },
110     {  id => 'sellprice',     width => 15,    value => $locale->text('Price'),                display => 1, },
111     {  id => 'sellprice_pg',  width => 15,    value => $locale->text('Pricegroup'),           display => $form->{type} =~ /^sales_/,  },
112     {  id => 'discount',      width => 5,     value => $locale->text('Discount'),             display => $form->{vc} eq 'customer', },
113     {  id => 'linetotal',     width => 10,    value => $locale->text('Extended'),             display => 1, },
114     {  id => 'bin',           width => 10,    value => $locale->text('Bin'),                  display => 0, },
115   ); 
116   my @column_index = map { $_->{id} } grep { $_->{display} } @HEADER;
117
118   # cache units
119   my $dimension_units = AM->retrieve_units(\%myconfig, $form, "dimension");
120   my $service_units   = AM->retrieve_units(\%myconfig, $form, "service");
121   my $all_units       = AM->retrieve_units(\%myconfig, $form);
122
123   my $colspan = scalar @column_index;
124
125   $form->{invsubtotal} = 0;
126   map { $form->{"${_}_base"} = 0 } (split(/ /, $form->{taxaccounts}));
127
128   # about details 
129   $myconfig{show_form_details} = 1                            unless (defined($myconfig{show_form_details}));
130   $form->{show_details}        = $myconfig{show_form_details} unless (defined($form->{show_details}));
131   # /about details
132
133   # translations, unused commented out
134 #  $runningnumber = $locale->text('No.');
135   $deliverydate  = $locale->text('Delivery Date');
136   $serialnumber  = $locale->text('Serial No.');
137   $projectnumber = $locale->text('Project');
138 #  $partsgroup    = $locale->text('Group');
139   $reqdate       = $locale->text('Reqdate');
140   $deliverydate  = $locale->text('Required by');
141
142   # special alignings
143   my %align = map { $_ => 'right' } qw(qty ship right sellprice_pg discount linetotal);
144
145   $form->{marge_total}           = 0;
146   $form->{sellprice_total}       = 0;
147   $form->{lastcost_total}        = 0;
148   my %projectnumber_labels = ();
149   my @projectnumber_values = ("");
150
151   foreach my $item (@{ $form->{"ALL_PROJECTS"} }) {
152     push(@projectnumber_values, $item->{"id"});
153     $projectnumber_labels{$item->{"id"}} = $item->{"projectnumber"};
154   }
155
156   # rows
157   for $i (1 .. $numrows) {
158
159     # undo formatting
160     map { $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) } qw(qty ship discount sellprice price_new price_old) unless ($form->{simple_save});
161
162 # unit begin
163     $form->{"unit_old_$i"}      ||= $form->{"unit_$i"};
164     $form->{"selected_unit_$i"} ||= $form->{"unit_$i"};
165
166     my $local_units = $form->{"inventory_accno_$i"} || $form->{"assembly_$i"} ? $dimension_units 
167                     : $form->{"id_$i"}                                        ? $service_units 
168                     :                                                           $all_units;
169     if (   !$local_units->{$form->{"selected_unit_$i"}}                                          # Die ausgewaehlte Einheit ist fuer diesen Artikel nicht gueltig
170         || !AM->convert_unit($form->{"selected_unit_$i"}, $form->{"unit_old_$i"}, $all_units)) { # (z.B. Dimensionseinheit war ausgewaehlt, es handelt sich aber
171       $form->{"unit_old_$i"} = $form->{"selected_unit_$i"} = $form->{"unit_$i"};                 # um eine Dienstleistung). Dann keinerlei Umrechnung vornehmen.
172     }
173     # adjust prices by unit, ignore if pricegroup changed
174     if ((!$form->{"prices_$i"}) || ($form->{"new_pricegroup_$i"} == $form->{"old_pricegroup_$i"})) {
175         $form->{"sellprice_$i"} *= AM->convert_unit($form->{"selected_unit_$i"}, $form->{"unit_old_$i"}, $all_units) || 1;
176         $form->{"unit_old_$i"}   = $form->{"selected_unit_$i"};
177     }
178     my $this_unit = $form->{"unit_$i"};
179     $this_unit    = $form->{"selected_unit_$i"} if AM->convert_unit($this_unit, $form->{"selected_unit_$i"}, $all_units);
180     $this_unit  ||= "kg";
181
182     $column_data{"unit"} = AM->unit_select_html($local_units, "unit_$i", $this_unit, $form->{"id_$i"} ? $form->{"unit_$i"} : undef);
183 # / unit ending
184
185     $form->{"sellprice_$i"} =~ /\.(\d+)/;
186     $decimalplaces = max 2, length $1;
187
188     $discount  = $form->round_amount($form->{"sellprice_$i"} * $form->{"discount_$i"} / 100, $decimalplaces);
189     $linetotal = $form->round_amount($form->{"sellprice_$i"} - $discount, $decimalplaces);
190     $linetotal = $form->round_amount($linetotal * $form->{"qty_$i"}, 2);
191
192     # convert " to &quot;
193     map { $form->{"${_}_$i"} =~ s/\"/&quot;/g } qw(partnumber description unit unit_old);
194
195     $column_data{runningnumber} = $cgi->textfield(-name => "runningnumber_$i", -size => 5,  -value => $i);    # HuT
196     $column_data{partnumber}    = $cgi->textfield(-name => "partnumber_$i",    -size => 12, -value => $form->{"partnumber_$i"});
197     $column_data{description} = ((($rows = $form->numtextrows($form->{"description_$i"}, 30, 6)) > 1) # if description is too large, use a textbox instead
198                                 ? $cgi->textarea( -name => "description_$i", -default => H($form->{"description_$i"}), -rows => $rows, -columns => 30)
199                                 : $cgi->textfield(-name => "description_$i",   -size => 30, -value => $form->quote($form->{"description_$i"})))
200                                 . $cgi->button(-value => $locale->text('L'), -onClick => "set_longdescription_window('longdescription_$i')");
201
202     $form->{"qty_$i"} =~ /\.(\d+)/;
203     my $qty_dec = length $1;
204
205     $column_data{qty}  = $cgi->textfield(-name => "qty_$i", -size => 5, -value => $form->format_amount(\%myconfig, $form->{"qty_$i"}, $qty_dec));
206     $column_data{qty} .= $cgi->button(-onclick => "calculate_qty_selection_window('qty_$i','alu_$i', 'formel_$i', $i)", -value => $locale->text('*/'))
207                        . $cgi->hidden(-name => "formel_$i", -value => $form->{"formel_$i"}) . $cgi->hidden("-name" => "alu_$i", "-value" => $form->{"alu_$i"})
208       if $form->{"formel_$i"};
209     $column_data{ship} = $cgi->textfield(-name => "ship_$i", -size => 5, -value => $form->format_amount(\%myconfig, $form->{"ship_$i"}));
210
211     # build in drop down list for pricesgroups
212     if ($form->{"prices_$i"}) {
213       $column_data{sellprice_pg} = qq|<select name="sellprice_pg_$i">$form->{"prices_$i"}</select>|;
214       $column_data{sellprice}    = $cgi->textfield(-name => "sellprice_$i", -size => 10, -onBlur => 'check_right_number_format(this)', -value =>
215                                    (($form->{"new_pricegroup_$i"} != $form->{"old_pricegroup_$i"})
216                                       ? $form->format_amount(\%myconfig, $form->{"price_new_$i"}, $decimalplaces)
217                                       : $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces)));
218     } else {
219       # for last row and report
220       # set pricegroup drop down list from report menu
221       if ($form->{"sellprice_$i"} != 0) {
222         $form->{"pricegroup_old_$i"} = $form->{"pricegroup_id_$i"};
223         my $default_option           = $form->{"sellprice_$i"}.'--'.$form->{"pricegroup_id_$i"};
224         $column_data{sellprice_pg}   = NTI($cgi->popup_menu("sellpricepg_$i", [ $default_option ], $default_option, { $default_option => $form->{"pricegroup_$i"} || '' }));
225       } else {
226         $column_data{sellprice_pg} = qq|&nbsp;|;
227       }
228       $column_data{sellprice} = $cgi->textfield(-name => "sellprice_$i", -size => 10, -onBlur => "check_right_number_format(this)", -value =>
229                                                 $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces));
230     }
231     $column_data{discount}    = $cgi->textfield(-name => "discount_$i", -size => 3, -value => $form->format_amount(\%myconfig, $form->{"discount_$i"}));
232     $column_data{linetotal}   = $form->format_amount(\%myconfig, $linetotal, 2);
233     $column_data{bin}         = $form->{"bin_$i"};
234
235     my @ROW1 = map { value => $column_data{$_}, align => $align{$_} }, @column_index;
236
237     # second row
238     my @ROW2 = ();
239     push @ROW2, { value => qq|<b>$serialnumber</b> <input name="serialnumber_$i" size="15" value="$form->{"serialnumber_$i"}">| } 
240       if $form->{type} !~ /_quotation/;
241     push @ROW2, { value => qq|<b>$projectnumber</b> | . NTI($cgi->popup_menu('-name'  => "project_id_$i",        '-values'  => \@projectnumber_values,
242                                                                              '-labels' => \%projectnumber_labels, '-default' => $form->{"project_id_$i"})) };
243     push @ROW2, { value => qq|<b>$reqdate</b> <input name="reqdate_$i" size="11" onBlur="check_right_date_format(this)" value="$form->{"reqdate_$i"}">| }
244       if $form->{type} =~ /order/;
245     push @ROW2, { value => sprintf qq|<b>%s</b>&nbsp;<input type="checkbox" name="subtotal_$i" value="1" %s>|, 
246                    $locale->text('Subtotal'), $form->{"subtotal_$i"} ? 'checked' : '' };
247
248 # begin marge calculations
249     my $marge_color;
250     my $real_sellprice = $form->{"sellprice_$i"} - $discount;
251
252     $form->{"lastcost_$i"} *= 1;
253     $form->{"marge_percent_$i"} = 0;
254
255     if ($real_sellprice && ($form->{"qty_$i"} * 1)) {
256       $form->{"marge_percent_$i"}     = ($real_sellprice - $form->{"lastcost_$i"}) * 100 / $real_sellprice;
257       $myconfig{marge_percent_warn} ||= 15;
258       $marge_color                    = 'color="#ff0000"' if $form->{"id_$i"} && ($form->{"marge_percent_$i"} < (1 * $myconfig{marge_percent_warn}));
259     }
260
261     my $marge_adjust_credit_note = $form->{type} eq 'credit_note' ? -1 : 1;
262     $form->{"marge_absolut_$i"}  = ($real_sellprice - $form->{"lastcost_$i"}) * $form->{"qty_$i"} * $marge_adjust_credit_note;
263     $form->{"marge_total"}      += $form->{"marge_absolut_$i"};
264     $form->{"lastcost_total"}   += $form->{"lastcost_$i"} * $form->{"qty_$i"};
265     $form->{"sellprice_total"}  += $real_sellprice * $form->{"qty_$i"};
266
267     map { $form->{"${_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}, 2) } qw(marge_absolut marge_percent);
268
269     push @ROW2, { value => sprintf qq|<font %s><b>%s</b> %s &nbsp;%s%% </font> &nbsp;<b>%s</b> %s &nbsp;<b>%s</b> %s|,
270                    $marge_color, $locale->text('Ertrag'),$form->{"marge_absolut_$i"}, $form->{"marge_percent_$i"},
271                    $locale->text('LP'), $form->format_amount(\%myconfig, $form->{"listprice_$i"}, 2),
272                    $locale->text('EK'), $form->format_amount(\%myconfig, $form->{"lastcost_$i"}, 2) }
273       if $form->{"id_$i"} && $form->{type} =~ /^sales_/;
274 # / marge calculations ending
275
276     my @HIDDENS = map { value => $_}, (
277           $cgi->hidden("-name" => "unit_old_$i", "-value" => $form->{"selected_unit_$i"}),
278           $cgi->hidden("-name" => "price_new_$i", "-value" => $form->format_amount(\%myconfig, $form->{"price_new_$i"})),
279           map { ($cgi->hidden("-name" => $_, "-value" => $form->{$_})); } map { $_."_$i" } 
280             qw(orderitems_id bo pricegroup_old price_old id inventory_accno bin partsgroup partnotes
281                income_accno expense_accno listprice assembly taxaccounts ordnumber transdate cusordnumber
282                longdescription basefactor marge_absolut marge_percent lastcost)
283     );
284
285     map { $form->{"${_}_base"} += $linetotal } (split(/ /, $form->{"taxaccounts_$i"}));
286
287     $form->{invsubtotal} += $linetotal;
288
289     push @ROWS, { ROW1 => \@ROW1, ROW2 => \@ROW2, HIDDENS => \@HIDDENS, colspan => $colspan, };
290   }
291
292   print $form->parse_html_template('oe/sales_order', { ROWS   => \@ROWS,
293                                                        HEADER => \@HEADER,
294                                                      });
295
296   if (0 != ($form->{sellprice_total} * 1)) {
297     $form->{marge_percent} = ($form->{sellprice_total} - $form->{lastcost_total}) / $form->{sellprice_total} * 100;
298   }
299
300   $lxdebug->leave_sub();
301 }
302
303 ##################################################
304 # build html-code for pricegroups in variable $form->{prices_$j}
305
306 sub set_pricegroup {
307   $lxdebug->enter_sub();
308   my $rowcount = shift;
309   for $j (1 .. $rowcount) {
310     next unless $form->{PRICES}{$j};
311     # build drop down list for pricegroups
312     my $option_tmpl = qq|<option value="%s--%s" %s>%s</option>|;
313     $form->{"prices_$j"}  = join '', map { sprintf $option_tmpl, @$_{qw(price pricegroup_id selected pricegroup)} } 
314                                          (+{ pricegroup => $locale->text("none (pricegroup)") }, @{ $form->{PRICES}{$j} });
315
316     foreach $item (@{ $form->{PRICES}{$j} }) {
317       # set new selectedpricegroup_id and prices for "Preis"
318       $form->{"pricegroup_old_$j"} = $item->{pricegroup_id}   if $item->{selected} &&  $item->{pricegroup_id};
319       $form->{"sellprice_$j"}      = $item->{price}           if $item->{selected} &&  $item->{pricegroup_id};
320       $form->{"price_new_$j"}      = $form->{"sellprice_$j"}  if $item->{selected} || !$item->{pricegroup_id};
321     }
322   }
323   $lxdebug->leave_sub();
324 }
325
326 sub select_item {
327   $lxdebug->enter_sub();
328   @column_index = qw(ndx partnumber description onhand unit sellprice);
329
330   $column_data{ndx}        = qq|<th>&nbsp;</th>|;
331   $column_data{partnumber} =
332     qq|<th class="listheading">| . $locale->text('Number') . qq|</th>|;
333   $column_data{description} =
334     qq|<th class="listheading">| . $locale->text('Part Description') . qq|</th>|;
335   $column_data{sellprice} =
336     qq|<th class="listheading">| . $locale->text('Price') . qq|</th>|;
337   $column_data{onhand} =
338     qq|<th class="listheading">| . $locale->text('Qty') . qq|</th>|;
339   $column_data{unit} =
340     qq|<th class="listheading">| . $locale->text('Unit') . qq|</th>|;
341   # list items with radio button on a form
342   $form->header;
343
344   $title   = $locale->text('Select from one of the items below');
345   $colspan = $#column_index + 1;
346
347   print qq|
348   <body>
349
350 <form method="post" action="$form->{script}">
351
352 <table width="100%">
353   <tr>
354     <th class="listtop" colspan="$colspan">$title</th>
355   </tr>
356   <tr height="5"></tr>
357   <tr class="listheading">|;
358
359   map { print "\n$column_data{$_}" } @column_index;
360
361   print qq|</tr>|;
362
363   my $i = 0;
364   foreach $ref (@{ $form->{item_list} }) {
365     $checked = ($i++) ? "" : "checked";
366
367     if ($lizenzen) {
368       if ($ref->{inventory_accno} > 0) {
369         $ref->{"lizenzen"} = qq|<option></option>|;
370         foreach $item (@{ $form->{LIZENZEN}{ $ref->{"id"} } }) {
371           $ref->{"lizenzen"} .=
372             qq|<option value=\"$item->{"id"}\">$item->{"licensenumber"}</option>|;
373         }
374         $ref->{"lizenzen"} .= qq|<option value="-1">Neue Lizenz</option>|;
375         $ref->{"lizenzen"} =~ s/\"/&quot;/g;
376       }
377     }
378
379     map { $ref->{$_} =~ s/\"/&quot;/g } qw(partnumber description unit);
380
381     my $display_sellprice  = $ref->{sellprice} * (1 - $form->{tradediscount});
382     $display_sellprice    /= $ref->{price_factor} if ($ref->{price_factor});
383     $display_sellprice     = $form->format_amount(\%myconfig, $display_sellprice, 2);
384
385     $column_data{ndx} =
386       qq|<td><input name="ndx" class="radio" type="radio" value="$i" $checked></td>|;
387     $column_data{partnumber} =
388       qq|<td><input name="new_partnumber_$i" type="hidden" value="$ref->{partnumber}">$ref->{partnumber}</td>|;
389     $column_data{description} =
390       qq|<td><input name="new_description_$i" type="hidden" value="$ref->{description}">$ref->{description}</td>|;
391     $column_data{sellprice} =
392       qq|<td align="right"><input name="new_sellprice_$i" type="hidden" value="$ref->{sellprice}">|
393       . $display_sellprice
394       . qq|</td>|;
395     $column_data{onhand} =
396       qq|<td align="right"><input name="new_onhand_$i" type="hidden" value="$ref->{onhand}">|
397       . $form->format_amount(\%myconfig, $ref->{onhand}, '', "&nbsp;")
398       . qq|</td>|;
399     $column_data{unit} =
400       qq|<td>$ref->{unit}</td>|;
401     $j++;
402     $j %= 2;
403     print qq|
404 <tr class=listrow$j>|;
405
406     map { print "\n$column_data{$_}" } @column_index;
407
408     print("</tr>\n");
409
410     my @new_fields =
411       qw(bin listprice inventory_accno income_accno expense_accno unit weight
412          assembly taxaccounts partsgroup formel longdescription not_discountable
413          part_payment_id partnotes id lastcost price_factor_id price_factor);
414     push(@new_fields, "lizenzen") if ($lizenzen);
415
416     print join "\n", map { $cgi->hidden("-name" => "new_${_}_$i", "-value" => $ref->{$_}) } @new_fields;
417     print "\n";
418   }
419
420   print qq|
421 <tr><td colspan="8"><hr size="3" noshade></td></tr>
422 </table>
423
424 <input name="lastndx" type="hidden" value="$i">
425
426 |;
427
428   # delete action variable
429   map { delete $form->{$_} } qw(action item_list header);
430
431   # save all other form variables
432   foreach $key (keys %${form}) {
433     $form->{$key} =~ s/\"/&quot;/g;
434     print qq|<input name="$key" type="hidden" value="$form->{$key}">\n|;
435   }
436
437   print qq|
438 <input type="hidden" name="nextsub" value="item_selected">
439
440 <br>
441 <input class="submit" type="submit" name="action" value="|
442     . $locale->text('Continue') . qq|">
443 </form>
444
445 </body>
446 </html>
447 |;
448
449   $lxdebug->leave_sub();
450 }
451
452 sub item_selected {
453   $lxdebug->enter_sub();
454
455   # replace the last row with the checked row
456   $i = $form->{rowcount};
457   $i = $form->{assembly_rows} if ($form->{item} eq 'assembly');
458
459   # index for new item
460   $j = $form->{ndx};
461
462   #sk
463   #($form->{"sellprice_$i"},$form->{"$pricegroup_old_$i"}) = split /--/, $form->{"sellprice_$i"};
464   #$form->{"sellprice_$i"} = $form->{"sellprice_$i"};
465
466   # if there was a price entered, override it
467   $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
468
469   my @new_fields =
470     qw(id partnumber description sellprice listprice inventory_accno
471        income_accno expense_accno bin unit weight assembly taxaccounts
472        partsgroup formel longdescription not_discountable partnotes lastcost
473        price_factor_id price_factor);
474
475   map { $form->{"${_}_$i"} = $form->{"new_${_}_$j"} } @new_fields;
476
477   $form->{"marge_price_factor_$i"} = $form->{"new_price_factor_$j"};
478
479   if ($form->{"part_payment_id_$i"} ne "") {
480     $form->{payment_id} = $form->{"part_payment_id_$i"};
481   }
482
483   if ($lizenzen) {
484     map { $form->{"${_}_$i"} = $form->{"new_${_}_$j"} } qw(lizenzen);
485   }
486
487   ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
488   $dec           = length $dec;
489   $decimalplaces = ($dec > 2) ? $dec : 2;
490
491   if ($sellprice) {
492     $form->{"sellprice_$i"} = $sellprice;
493   } else {
494
495     # if there is an exchange rate adjust sellprice
496     if (($form->{exchangerate} * 1) != 0) {
497       $form->{"sellprice_$i"} /= $form->{exchangerate};
498       $form->{"sellprice_$i"} =
499         $form->round_amount($form->{"sellprice_$i"}, $decimalplaces);
500     }
501   }
502
503   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
504     qw(sellprice listprice weight);
505
506   $form->{sellprice} += ($form->{"sellprice_$i"} * $form->{"qty_$i"});
507   $form->{weight}    += ($form->{"weight_$i"} * $form->{"qty_$i"});
508   
509   if ($form->{"not_discountable_$i"}) {
510     $form->{"discount_$i"} = 0;
511   }
512
513   $amount =
514     $form->{"sellprice_$i"} * (1 - $form->{"discount_$i"} / 100) *
515     $form->{"qty_$i"};
516   map { $form->{"${_}_base"} += $amount }
517     (split / /, $form->{"taxaccounts_$i"});
518   map { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) } split / /,
519     $form->{"taxaccounts_$i"}
520     if !$form->{taxincluded};
521
522   $form->{creditremaining} -= $amount;
523
524   $form->{"runningnumber_$i"} = $i;
525
526   # delete all the new_ variables
527   for $i (1 .. $form->{lastndx}) {
528     map { delete $form->{"new_${_}_$i"} } @new_fields;
529   }
530
531   map { delete $form->{$_} } qw(ndx lastndx nextsub);
532
533   # format amounts
534   map {
535     $form->{"${_}_$i"} =
536       $form->format_amount(\%myconfig, $form->{"${_}_$i"}, $decimalplaces)
537   } qw(sellprice listprice) if $form->{item} ne 'assembly';
538
539   # get pricegroups for parts
540   IS->get_pricegroups_for_parts(\%myconfig, \%$form);
541
542   # build up html code for prices_$i
543   set_pricegroup($form->{rowcount});
544
545   &display_form;
546
547   $lxdebug->leave_sub();
548 }
549
550 sub new_item {
551   $lxdebug->enter_sub();
552
553   # change callback
554   $form->{old_callback} = $form->escape($form->{callback}, 1);
555   $form->{callback}     = $form->escape("$form->{script}?action=display_form", 1);
556
557   # save all form variables except action in a previousform variable
558   my $previousform = join '&', map { $form->{$_} =~ s/&/%26/; "$_=$form->{$_}" } grep { !/action/ } keys %$form;
559
560   push @HIDDENS,      { 'name' => 'previousform', 'value' => $form->escape($previousform, 1) };
561   push @HIDDENS, map +{ 'name' => $_,             'value' => $form->{$_} },                       qw(rowcount vc login password);
562   push @HIDDENS, map +{ 'name' => $_,             'value' => $form->{"${_}_$form->{rowcount}"} }, qw(partnumber description unit sellprice);
563   push @HIDDENS,      { 'name' => 'taxaccount2',  'value' => $form->{taxaccounts} };
564
565   $form->header();
566   print $form->parse_html_template("generic/new_item", { HIDDENS => [ sort { $a->{name} cmp $b->{name} } @HIDDENS ] } );
567
568   $lxdebug->leave_sub();
569 }
570
571 sub check_form {
572   $lxdebug->enter_sub();
573   my @a     = ();
574   my $count = 0;
575   my @flds  = (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 marge_total marge_percent marge_price_factor lastcost price_factor_id));
576
577   # remove any makes or model rows
578   if ($form->{item} eq 'part') {
579     map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
580       qw(listprice sellprice lastcost weight rop);
581
582     @flds = (make, model);
583     for my $i (1 .. ($form->{makemodel_rows})) {
584       if (($form->{"make_$i"} ne "") || ($form->{"model_$i"} ne "")) {
585         push @a, {};
586         my $j = $#a;
587
588         map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
589         $count++;
590       }
591     }
592
593     $form->redo_rows(\@flds, \@a, $count, $form->{makemodel_rows});
594     $form->{makemodel_rows} = $count;
595
596   } elsif ($form->{item} eq 'assembly') {
597
598     $form->{sellprice} = 0;
599     $form->{weight}    = 0;
600     map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
601       qw(listprice rop stock);
602
603     @flds =
604       qw(id qty unit bom partnumber description sellprice weight runningnumber partsgroup);
605
606     for my $i (1 .. ($form->{assembly_rows} - 1)) {
607       if ($form->{"qty_$i"}) {
608         push @a, {};
609         my $j = $#a;
610
611         $form->{"qty_$i"} = $form->parse_amount(\%myconfig, $form->{"qty_$i"});
612
613         map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
614
615         #($form->{"sellprice_$i"},$form->{"$pricegroup_old_$i"}) = split /--/, $form->{"sellprice_$i"};
616
617         $form->{sellprice} += ($form->{"qty_$i"} * $form->{"sellprice_$i"});
618         $form->{weight}    += ($form->{"qty_$i"} * $form->{"weight_$i"});
619         $count++;
620       }
621     }
622
623     $form->{sellprice} = $form->round_amount($form->{sellprice}, 2);
624
625     $form->redo_rows(\@flds, \@a, $count, $form->{assembly_rows});
626     $form->{assembly_rows} = $count;
627
628     $count = 0;
629     @flds  = qw(make model);
630     @a     = ();
631
632     for my $i (1 .. ($form->{makemodel_rows})) {
633       if (($form->{"make_$i"} ne "") || ($form->{"model_$i"} ne "")) {
634         push @a, {};
635         my $j = $#a;
636
637         map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
638         $count++;
639       }
640     }
641
642     $form->redo_rows(\@flds, \@a, $count, $form->{makemodel_rows});
643     $form->{makemodel_rows} = $count;
644
645   } elsif ($form->{item} eq 'service') {
646     map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) } qw(listprice sellprice lastcost);
647
648   } else {
649
650     # this section applies to invoices and orders
651     # remove any empty numbers
652     if ($form->{rowcount}) {
653       for my $i (1 .. $form->{rowcount} - 1) {
654         if ($form->{"partnumber_$i"}) {
655           push @a, {};
656           my $j = $#a;
657
658           map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
659           $count++;
660           if ($lizenzen) {
661             if ($form->{"licensenumber_$i"} == -1) {
662               &new_license($i);
663               exit;
664             }
665           }
666         }
667       }
668
669       $form->redo_rows(\@flds, \@a, $count, $form->{rowcount});
670       $form->{rowcount} = $count;
671
672       $form->{creditremaining} -= &invoicetotal;
673
674     }
675   }
676
677   #sk
678   # if pricegroups
679   if (   $form->{type} =~ (/sales_quotation/)
680       or (($form->{level} =~ /Sales/) and ($form->{type} =~ /invoice/))
681       or (($form->{level} eq undef) and ($form->{type} =~ /invoice/))
682       or ($form->{type} =~ /sales_order/)) {
683
684     # get pricegroups for parts
685     IS->get_pricegroups_for_parts(\%myconfig, \%$form);
686
687     # build up html code for prices_$i
688     set_pricegroup($form->{rowcount});
689
690   }
691
692   &display_form;
693
694   $lxdebug->leave_sub();
695 }
696
697 sub invoicetotal {
698   $lxdebug->enter_sub();
699
700   $form->{oldinvtotal} = 0;
701
702   # add all parts and deduct paid
703   map { $form->{"${_}_base"} = 0 } split / /, $form->{taxaccounts};
704
705   my ($amount, $sellprice, $discount, $qty);
706
707   for my $i (1 .. $form->{rowcount}) {
708     $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
709     $discount  = $form->parse_amount(\%myconfig, $form->{"discount_$i"});
710     $qty       = $form->parse_amount(\%myconfig, $form->{"qty_$i"});
711
712     #($form->{"sellprice_$i"}, $form->{"$pricegroup_old_$i"}) = split /--/, $form->{"sellprice_$i"};
713
714     $amount = $sellprice * (1 - $discount / 100) * $qty;
715     map { $form->{"${_}_base"} += $amount }
716       (split (/ /, $form->{"taxaccounts_$i"}));
717     $form->{oldinvtotal} += $amount;
718   }
719
720   map { $form->{oldinvtotal} += ($form->{"${_}_base"} * $form->{"${_}_rate"}) }
721     split(/ /, $form->{taxaccounts})
722     if !$form->{taxincluded};
723
724   $form->{oldtotalpaid} = 0;
725   for $i (1 .. $form->{paidaccounts}) {
726     $form->{oldtotalpaid} += $form->{"paid_$i"};
727   }
728
729   $lxdebug->leave_sub();
730
731   # return total
732   return ($form->{oldinvtotal} - $form->{oldtotalpaid});
733 }
734
735 sub validate_items {
736   $lxdebug->enter_sub();
737
738   # check if items are valid
739   if ($form->{rowcount} == 1) {
740     &update;
741     exit;
742   }
743
744   for $i (1 .. $form->{rowcount} - 1) {
745     $form->isblank("partnumber_$i",
746                    $locale->text('Number missing in Row') . " $i");
747   }
748
749   $lxdebug->leave_sub();
750 }
751
752 sub order {
753   $lxdebug->enter_sub();
754   if ($form->{second_run}) {
755     $form->{print_and_post} = 0;
756   }
757   $form->{ordnumber} = $form->{invnumber};
758
759   $form->{old_employee_id} = $form->{employee_id};
760   $form->{old_salesman_id} = $form->{salesman_id};
761
762   map { delete $form->{$_} } qw(id printed emailed queued);
763   if ($form->{script} eq 'ir.pl' || $form->{type} eq 'request_quotation') {
764     $form->{title} = $locale->text('Add Purchase Order');
765     $form->{vc}    = 'vendor';
766     $form->{type}  = 'purchase_order';
767     $buysell       = 'sell';
768   }
769   if ($form->{script} eq 'is.pl' || $form->{type} eq 'sales_quotation') {
770     $form->{title} = $locale->text('Add Sales Order');
771     $form->{vc}    = 'customer';
772     $form->{type}  = 'sales_order';
773     $buysell       = 'buy';
774   }
775   $form->{script} = 'oe.pl';
776
777   $form->{shipto} = 1;
778
779   $form->{rowcount}--;
780
781   $form->{cp_id} *= 1;
782
783   require "bin/mozilla/$form->{script}";
784   my $script = $form->{"script"};
785   $script =~ s|.*/||;
786   $script =~ s|.pl$||;
787   $locale = new Locale($language, $script);
788
789   map { $form->{"select$_"} = "" } ($form->{vc}, currency);
790
791   $currency = $form->{currency};
792
793   &order_links;
794
795   $form->{currency}     = $currency;
796   $form->{exchangerate} = "";
797   $form->{forex}        = "";
798   $form->{exchangerate} = $exchangerate
799     if (
800         $form->{forex} = (
801                   $exchangerate =
802                     $form->check_exchangerate(
803                     \%myconfig, $form->{currency}, $form->{transdate}, $buysell
804                     )));
805
806   for $i (1 .. $form->{rowcount}) {
807     map({ $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig,
808                                                      $form->{"${_}_${i}"})
809             if ($form->{"${_}_${i}"}) }
810         qw(ship qty sellprice listprice basefactor));
811   }
812
813   &prepare_order;
814   &display_form;
815
816   $lxdebug->leave_sub();
817 }
818
819 sub quotation {
820   $lxdebug->enter_sub();
821   if ($form->{second_run}) {
822     $form->{print_and_post} = 0;
823   }
824   map { delete $form->{$_} } qw(id printed emailed queued);
825
826   if ($form->{script} eq 'ir.pl' || $form->{type} eq 'purchase_order') {
827     $form->{title} = $locale->text('Add Request for Quotation');
828     $form->{vc}    = 'vendor';
829     $form->{type}  = 'request_quotation';
830     $buysell       = 'sell';
831   }
832   if ($form->{script} eq 'is.pl' || $form->{type} eq 'sales_order') {
833     $form->{title} = $locale->text('Add Quotation');
834     $form->{vc}    = 'customer';
835     $form->{type}  = 'sales_quotation';
836     $buysell       = 'buy';
837   }
838
839   $form->{cp_id} *= 1;
840
841   $form->{script} = 'oe.pl';
842
843   $form->{shipto} = 1;
844
845   $form->{rowcount}--;
846
847   require "bin/mozilla/$form->{script}";
848
849   map { $form->{"select$_"} = "" } ($form->{vc}, currency);
850
851   $currency = $form->{currency};
852
853   &order_links;
854
855   $form->{currency}     = $currency;
856   $form->{exchangerate} = "";
857   $form->{forex}        = "";
858   $form->{exchangerate} = $exchangerate
859     if (
860         $form->{forex} = (
861                   $exchangerate =
862                     $form->check_exchangerate(
863                     \%myconfig, $form->{currency}, $form->{transdate}, $buysell
864                     )));
865
866   for $i (1 .. $form->{rowcount}) {
867     map({ $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig,
868                                                      $form->{"${_}_${i}"})
869             if ($form->{"${_}_${i}"}) }
870         qw(ship qty sellprice listprice basefactor));
871   }
872
873   &prepare_order;
874   &display_form;
875
876   $lxdebug->leave_sub();
877 }
878
879 sub request_for_quotation {
880   quotation();
881 }
882
883 sub edit_e_mail {
884   $lxdebug->enter_sub();
885
886   if ($form->{second_run}) {
887     $form->{print_and_post} = 0;
888     $form->{resubmit}       = 0;
889   }
890
891   $form->{email} = $form->{shiptoemail} if $form->{shiptoemail} && $form->{formname} =~ /(pick|packing|bin)_list/;
892
893   if ($form->{"cp_id"} && !$form->{"email"}) {
894     CT->get_contact(\%myconfig, $form);
895     $form->{"email"} = $form->{"cp_email"};
896   }
897
898   $title = $locale->text('E-mail') . " " . $form->get_formname_translation();
899
900   $form->{oldmedia} = $form->{media};
901   $form->{media}    = "email";
902
903   my $attachment_filename = $form->generate_attachment_filename();
904
905   $form->{"fokus"} = $form->{"email"} ? "Form.subject" : "Form.email";
906   $form->header;
907
908   my (@dont_hide_key_list, %dont_hide_key, @hidden_keys);
909   @dont_hide_key_list = qw(action email cc bcc subject message sendmode format header override);
910   @dont_hide_key{@dont_hide_key_list} = (1) x @dont_hide_key_list;
911   @hidden_keys = sort grep { !$dont_hide_key{$_} } grep { !ref $form->{$_} } keys %$form;
912
913   print $form->parse_html_template('generic/edit_email',
914                                    { title           => $title,
915                                      a_filename      => $attachment_filename,
916                                      _print_options_ => print_options('inline' => 1),
917                                      HIDDEN          => [ map +{ name => $_, value => $form->{$_} }, @hidden_keys ],
918                                      SHOW_BCC        => $myconfig{role} eq 'admin' });
919
920   $lxdebug->leave_sub();
921 }
922
923 sub send_email {
924   $lxdebug->enter_sub();
925
926   my $callback = $form->{script} . "?action=edit";
927   map({ $callback .= "\&${_}=" . E($form->{$_}); }
928       qw(login password type id));
929
930   print_form("return");
931
932   Common->save_email_status(\%myconfig, $form);
933
934   $form->{callback} = $callback;
935   $form->redirect();
936
937   $lxdebug->leave_sub();
938 }
939
940 # generate the printing options displayed at the bottom of oe and is forms.
941 # this function will attempt to guess what type of form is displayed, and will generate according options
942 #
943 # about the coding: 
944 # this version builds the arrays of options pretty directly. if you have trouble understanding how,
945 # the opthash function builds hashrefs which are then pieced together for the template arrays.
946 # unneeded options are "undef"ed out, and then grepped out. 
947 #
948 # the inline options is untested, but intended to be used later in metatemplating
949 sub print_options {
950   $lxdebug->enter_sub();
951
952   my %options = @_;
953
954   # names 3 parameters and returns a hashref, for use in templates
955   sub opthash { +{ value => shift, selected => shift, oname => shift } }
956   (@FORMNAME, @FORMNAME, @LANGUAGE_ID, @FORMAT, @SENDMODE, @MEDIA, @PRINTER_ID, @SELECTS) = ();
957
958   # note: "||"-selection is only correct for values where "0" is _not_ a correct entry
959   $form->{sendmode}   = "attachment";
960   $form->{format}     = $form->{format} || $myconfig{template_format} || "pdf";
961   $form->{copies}     = $form->{copies} || $myconfig{copies}          || 3;
962   $form->{media}      = $form->{media}  || $myconfig{default_media}   || "screen";
963   $form->{printer_id} = defined $form->{printer_id}           ? $form->{printer_id} :
964                         defined $myconfig{default_printer_id} ? $myconfig{default_printer_id} : "";
965
966   $form->{PD}{ $form->{formname} } = "selected";
967   $form->{DF}{ $form->{format} }   = "selected";
968   $form->{OP}{ $form->{media} }    = "selected";
969   $form->{SM}{ $form->{formname} } = "selected";
970
971   push @FORMNAME, grep $_,
972     ($form->{type} eq 'purchase_order') ? (
973       opthash("purchase_order",      $form->{PD}{purchase_order},      $locale->text('Purchase Order')),
974       opthash("bin_list",            $form->{PD}{bin_list},            $locale->text('Bin List')) 
975     ) : undef,
976     ($form->{type} eq 'credit_note') ?
977       opthash("credit_note",         $form->{PD}{credit_note},         $locale->text('Credit Note')) : undef,
978     ($form->{type} eq 'sales_order') ? (
979       opthash("sales_order",         $form->{PD}{sales_order},         $locale->text('Confirmation')),
980       opthash("proforma",            $form->{PD}{proforma},            $locale->text('Proforma Invoice')),
981       opthash("pick_list",           $form->{PD}{pick_list},           $locale->text('Pick List')),
982       opthash("packing_list",        $form->{PD}{packing_list},        $locale->text('Packing List')) 
983     ) : undef,
984     ($form->{type} =~ /_quotation$/) ?
985       opthash("$`_quotation",        $form->{PD}{"$`_quotation"},      $locale->text('Quotation')) : undef,
986     ($form->{type} eq 'invoice') ? (
987       opthash("invoice",             $form->{PD}{invoice},             $locale->text('Invoice')),
988       opthash("proforma",            $form->{PD}{proforma},            $locale->text('Proforma Invoice')),
989       opthash("packing_list",        $form->{PD}{packing_list},        $locale->text('Packing List'))
990     ) : undef,
991     ($form->{type} eq 'invoice' && $form->{storno}) ? (
992       opthash("storno_invoice",      $form->{PD}{storno_invoice},      $locale->text('Storno Invoice')),
993       opthash("storno_packing_list", $form->{PD}{storno_packing_list}, $locale->text('Storno Packing List')) 
994     ) : undef,
995     ($form->{type} eq 'credit_note') ?
996       opthash("credit_note",         $form->{PD}{credit_note},         $locale->text('Credit Note')) : undef;
997
998   push @SENDMODE, 
999     opthash("attachment",            $form->{SM}{attachment},          $locale->text('Attachment')),
1000     opthash("inline",                $form->{SM}{inline},              $locale->text('In-line'))
1001       if ($form->{media} eq 'email');
1002
1003   push @MEDIA, grep $_,
1004       opthash("screen",              $form->{OP}{screen},              $locale->text('Screen')),
1005     (scalar @{ $form->{printers} } && $latex_templates) ?
1006       opthash("printer",             $form->{OP}{printer},             $locale->text('Printer')) : undef,
1007     ($latex_templates && !$options->{no_queue}) ?
1008       opthash("queue",               $form->{OP}{queue},               $locale->text('Queue')) : undef
1009         if ($form->{media} ne 'email');
1010
1011   push @FORMAT, grep $_,
1012     ($opendocument_templates && $openofficeorg_writer_bin && $xvfb_bin && (-x $openofficeorg_writer_bin) && (-x $xvfb_bin)
1013      && !$options->{no_opendocument_pdf}) ?
1014       opthash("opendocument_pdf",    $form->{DF}{"opendocument_pdf"},  $locale->text("PDF (OpenDocument/OASIS)")) : undef,
1015     ($latex_templates) ?
1016       opthash("pdf",                 $form->{DF}{pdf},                 $locale->text('PDF')) : undef,
1017     ($latex_templates && !$options->{no_postscript}) ?
1018       opthash("postscript",          $form->{DF}{postscript},          $locale->text('Postscript')) : undef,
1019     (!$options->{no_html}) ?
1020       opthash("html", $form->{DF}{html}, "HTML") : undef,
1021     ($opendocument_templates && !$options->{no_opendocument}) ?
1022       opthash("opendocument",        $form->{DF}{opendocument},        $locale->text("OpenDocument/OASIS")) : undef;
1023
1024   push @LANGUAGE_ID, 
1025     map { opthash($_->{id}, ($_->{id} eq $form->{language_id} ? 'selected' : ''), $_->{description}) } +{}, @{ $form->{languages} }
1026       if (ref $form->{languages} eq 'ARRAY');
1027
1028   push @PRINTER_ID, 
1029     map { opthash($_->{id}, ($_->{id} eq $form->{printer_id} ? 'selected' : ''), $_->{printer_description}) } +{}, @{ $form->{printers} }
1030       if ((ref $form->{printers} eq 'ARRAY') && scalar @{ $form->{printers } });
1031
1032   @SELECTS = map { sname => lc $_, DATA => \@$_, show => !$options{"hide_" . lc($_)} && scalar @$_ }, qw(FORMNAME LANGUAGE_ID FORMAT SENDMODE MEDIA PRINTER_ID);
1033
1034   my %dont_display_groupitems = (
1035     'dunning' => 1,
1036     );
1037
1038   %template_vars = (
1039     display_copies       => scalar @{ $form->{printers} } && $latex_templates && $form->{media} ne 'email',
1040     display_remove_draft => (!$form->{id} && $form->{draft_id}),
1041     display_groupitems   => !$dont_display_groupitems{$form->{type}},
1042     groupitems_checked   => $form->{groupitems} ? "checked" : '',
1043     remove_draft_checked => $form->{remove_draft} ? "checked" : ''
1044   );
1045
1046   my $print_options = $form->parse_html_template("generic/print_options", { SELECTS  => \@SELECTS, %template_vars } );
1047
1048   if ($options{inline}) {
1049     $lxdebug->leave_sub();
1050     return $print_options;
1051   }
1052
1053   print $print_options;
1054
1055   $lxdebug->leave_sub();
1056 }
1057
1058 sub print {
1059   $lxdebug->enter_sub();
1060
1061   if ($form->{print_nextsub}) {
1062     call_sub($form->{print_nextsub});
1063     $lxdebug->leave_sub();
1064     return;
1065   }
1066
1067   # if this goes to the printer pass through
1068   if ($form->{media} eq 'printer' || $form->{media} eq 'queue') {
1069     $form->error($locale->text('Select postscript or PDF!'))
1070       if ($form->{format} !~ /(postscript|pdf)/);
1071
1072     $old_form = new Form;
1073     map { $old_form->{$_} = $form->{$_} } keys %$form;
1074   }
1075
1076   if (!$form->{id} || (($form->{formname} eq "proforma") && !$form->{proforma} && (($form->{type} =~ /_order$/) || ($form->{type} =~ /_quotation$/)))) {
1077     if ($form->{formname} eq "proforma") {
1078       $form->{proforma} = 1;
1079     }
1080     $form->{print_and_save} = 1;
1081     my $formname = $form->{formname};
1082     &save();
1083     $form->{formname} = $formname;
1084     &edit();
1085     exit;
1086   }
1087
1088   &print_form($old_form);
1089
1090   $lxdebug->leave_sub();
1091 }
1092
1093 sub print_form {
1094   $lxdebug->enter_sub();
1095   my ($old_form) = @_;
1096
1097   $inv       = "inv";
1098   $due       = "due";
1099   $numberfld = "invnumber";
1100
1101   $display_form =
1102     ($form->{display_form}) ? $form->{display_form} : "display_form";
1103
1104   # $form->{"notes"} will be overridden by the customer's/vendor's "notes" field. So save it here.
1105   $form->{ $form->{"formname"} . "notes" } = $form->{"notes"};
1106
1107   if ($form->{formname} eq "invoice") {
1108     $form->{label} = $locale->text('Invoice');
1109   }
1110   if ($form->{formname} eq "packing_list") {
1111
1112     # this is from an invoice
1113     $form->{label} = $locale->text('Packing List');
1114   }
1115   if ($form->{formname} eq 'sales_order') {
1116     $inv                  = "ord";
1117     $due                  = "req";
1118     $form->{"${inv}date"} = $form->{transdate};
1119     $form->{label}        = $locale->text('Confirmation');
1120     $numberfld            = "sonumber";
1121     $order                = 1;
1122   }
1123
1124   if (($form->{type} eq 'invoice') && ($form->{formname} eq 'proforma') ) {
1125     $inv                  = "inv";
1126     $due                  = "due";
1127     $form->{"${inv}date"} = $form->{invdate};
1128     $form->{label}        = $locale->text('Proforma Invoice');
1129     $numberfld            = "sonumber";
1130     $order                = 0;
1131   }
1132
1133   if (($form->{type} eq 'sales_order') && ($form->{formname} eq 'proforma') ) {
1134     $inv                  = "inv";
1135     $due                  = "due";
1136     $form->{"${inv}date"} = $form->{transdate};
1137     $form->{"invdate"}    = $form->{transdate};
1138     $form->{invnumber}    = $form->{ordnumber};
1139     $form->{label}        = $locale->text('Proforma Invoice');
1140     $numberfld            = "sonumber";
1141     $order                = 1;
1142   }
1143
1144   if ($form->{formname} eq 'packing_list' && $form->{type} ne 'invoice') {
1145
1146     # we use the same packing list as from an invoice
1147     $inv = "ord";
1148     $due = "req";
1149     $form->{invdate} = $form->{"${inv}date"} = $form->{transdate};
1150     $form->{label} = $locale->text('Packing List');
1151     $order = 1;
1152     # set invnumber for template packing_list 
1153     $form->{invnumber}   = $form->{ordnumber};
1154   }
1155   if ($form->{formname} eq 'pick_list') {
1156     $inv                  = "ord";
1157     $due                  = "req";
1158     $form->{"${inv}date"} =
1159       ($form->{transdate}) ? $form->{transdate} : $form->{invdate};
1160     $form->{label} = $locale->text('Pick List');
1161     $order = 1 unless $form->{type} eq 'invoice';
1162   }
1163   if ($form->{formname} eq 'purchase_order') {
1164     $inv                  = "ord";
1165     $due                  = "req";
1166     $form->{"${inv}date"} = $form->{transdate};
1167     $form->{label}        = $locale->text('Purchase Order');
1168     $numberfld            = "ponumber";
1169     $order                = 1;
1170   }
1171   if ($form->{formname} eq 'bin_list') {
1172     $inv                  = "ord";
1173     $due                  = "req";
1174     $form->{"${inv}date"} = $form->{transdate};
1175     $form->{label}        = $locale->text('Bin List');
1176     $order                = 1;
1177   }
1178   if ($form->{formname} eq 'sales_quotation') {
1179     $inv                  = "quo";
1180     $due                  = "req";
1181     $form->{"${inv}date"} = $form->{transdate};
1182     $form->{label}        = $locale->text('Quotation');
1183     $numberfld            = "sqnumber";
1184     $order                = 1;
1185   }
1186
1187   if (($form->{type} eq 'sales_quotation') && ($form->{formname} eq 'proforma') ) {
1188     $inv                  = "quo";
1189     $due                  = "req";
1190     $form->{"${inv}date"} = $form->{transdate};
1191     $form->{"invdate"}    = $form->{transdate};
1192     $form->{label}        = $locale->text('Proforma Invoice');
1193     $numberfld            = "sqnumber";
1194     $order                = 1;
1195   }
1196
1197   if ($form->{formname} eq 'request_quotation') {
1198     $inv                  = "quo";
1199     $due                  = "req";
1200     $form->{"${inv}date"} = $form->{transdate};
1201     $form->{label}        = $locale->text('RFQ');
1202     $numberfld            = "rfqnumber";
1203     $order                = 1;
1204   }
1205
1206   $form->isblank("email", $locale->text('E-mail address missing!'))
1207     if ($form->{media} eq 'email');
1208   $form->isblank("${inv}date",
1209            $locale->text($form->{label}) 
1210            . ": "
1211            . $locale->text(' Date missing!'));
1212
1213   # $locale->text('Invoice Number missing!')
1214   # $locale->text('Invoice Date missing!')
1215   # $locale->text('Packing List Number missing!')
1216   # $locale->text('Packing List Date missing!')
1217   # $locale->text('Order Number missing!')
1218   # $locale->text('Order Date missing!')
1219   # $locale->text('Quotation Number missing!')
1220   # $locale->text('Quotation Date missing!')
1221
1222   # assign number
1223   $form->{what_done} = $form->{formname};
1224   if (!$form->{"${inv}number"} && !$form->{preview} && !$form->{id}) {
1225     $form->{"${inv}number"} = $form->update_defaults(\%myconfig, $numberfld);
1226     if ($form->{media} ne 'email') {
1227
1228       # get pricegroups for parts
1229       IS->get_pricegroups_for_parts(\%myconfig, \%$form);
1230
1231       # build up html code for prices_$i
1232       set_pricegroup($form->{rowcount});
1233
1234       $form->{rowcount}--;
1235
1236       call_sub($display_form);
1237       # saving the history
1238           if(!exists $form->{addition}) {
1239         $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber}; 
1240             $form->{addition} = "PRINTED";
1241             $form->save_history($form->dbconnect(\%myconfig));
1242       }
1243       # /saving the history
1244       exit;
1245     }
1246   }
1247
1248   &validate_items;
1249
1250   # Save the email address given in the form because it should override the setting saved for the customer/vendor.
1251   my ($saved_email, $saved_cc, $saved_bcc) =
1252     ($form->{"email"}, $form->{"cc"}, $form->{"bcc"});
1253
1254   $language_saved = $form->{language_id};
1255   $payment_id_saved = $form->{payment_id};
1256   $salesman_id_saved = $form->{salesman_id};
1257   $cp_id_saved = $form->{cp_id};
1258
1259   call_sub("$form->{vc}_details");
1260
1261   $form->{language_id} = $language_saved;
1262   $form->{payment_id} = $payment_id_saved;
1263
1264   $form->{"email"} = $saved_email if ($saved_email);
1265   $form->{"cc"}    = $saved_cc    if ($saved_cc);
1266   $form->{"bcc"}   = $saved_bcc   if ($saved_bcc);
1267
1268   if (!$cp_id_saved) {
1269     # No contact was selected. Delete all contact variables because
1270     # IS->customer_details() and IR->vendor_details() get the default
1271     # contact anyway.
1272     map({ delete($form->{$_}); } grep(/^cp_/, keys(%{ $form })));
1273   }
1274
1275   my ($language_tc, $output_numberformat, $output_dateformat, $output_longdates);
1276   if ($form->{"language_id"}) {
1277     ($language_tc, $output_numberformat, $output_dateformat, $output_longdates) =
1278       AM->get_language_details(\%myconfig, $form, $form->{language_id});
1279   } else {
1280     $output_dateformat = $myconfig{"dateformat"};
1281     $output_numberformat = $myconfig{"numberformat"};
1282     $output_longdates = 1;
1283   }
1284
1285   ($form->{employee}) = split /--/, $form->{employee};
1286
1287   # create the form variables
1288   if ($order) {
1289     OE->order_details(\%myconfig, \%$form);
1290   } else {
1291     IS->invoice_details(\%myconfig, \%$form, $locale);
1292   }
1293
1294   $form->get_salesman(\%myconfig, $salesman_id_saved);
1295
1296   if ($form->{shipto_id}) {
1297     $form->get_shipto(\%myconfig);
1298   }
1299
1300   @a = qw(name street zipcode city country);
1301
1302   $shipto = 1;
1303
1304   # if there is no shipto fill it in from billto
1305   foreach $item (@a) {
1306     if ($form->{"shipto$item"}) {
1307       $shipto = 0;
1308       last;
1309     }
1310   }
1311
1312   if ($shipto) {
1313     if (   $form->{formname} eq 'purchase_order'
1314         || $form->{formname} eq 'request_quotation') {
1315       $form->{shiptoname}   = $myconfig{company};
1316       $form->{shiptostreet} = $myconfig{address};
1317     } else {
1318       map { $form->{"shipto$_"} = $form->{$_} } @a;
1319     }
1320   }
1321
1322   $form->{notes} =~ s/^\s+//g;
1323
1324   $form->{templates} = "$myconfig{templates}";
1325
1326   delete $form->{printer_command};
1327
1328   $form->{language} = $form->get_template_language(\%myconfig);
1329
1330   my $printer_code;
1331   if ($form->{media} ne 'email') {
1332     $printer_code = $form->get_printer_code(\%myconfig);
1333     if ($printer_code ne "") {
1334       $printer_code = "_" . $printer_code;
1335     }
1336   }
1337
1338   if ($form->{language} ne "") {
1339     map({ $form->{"unit"}->[$_] =
1340             AM->translate_units($form, $form->{"language"},
1341                                 $form->{"unit"}->[$_], $form->{"qty"}->[$_]); }
1342         (0..scalar(@{$form->{"unit"}}) - 1));
1343     $form->{language} = "_" . $form->{language};
1344   }
1345
1346   # Format dates.
1347   format_dates($output_dateformat, $output_longdates,
1348                qw(invdate orddate quodate pldate duedate reqdate transdate
1349                   shippingdate deliverydate validitydate paymentdate
1350                   datepaid transdate_oe deliverydate_oe
1351                   employee_startdate employee_enddate
1352                   ),
1353                grep({ /^datepaid_\d+$/ ||
1354                         /^transdate_oe_\d+$/ ||
1355                         /^deliverydate_oe_\d+$/ ||
1356                         /^reqdate_\d+$/ ||
1357                         /^deliverydate_\d+$/ ||
1358                         /^transdate_\d+$/
1359                     } keys(%{$form})));
1360
1361   reformat_numbers($output_numberformat, 2,
1362                    qw(invtotal ordtotal quototal subtotal linetotal
1363                       listprice sellprice netprice discount
1364                       tax taxbase total paid),
1365                    grep({ /^linetotal_\d+$/ ||
1366                             /^listprice_\d+$/ ||
1367                             /^sellprice_\d+$/ ||
1368                             /^netprice_\d+$/ ||
1369                             /^taxbase_\d+$/ ||
1370                             /^discount_\d+$/ ||
1371                             /^paid_\d+$/ ||
1372                             /^subtotal_\d+$/ ||
1373                             /^total_\d+$/ ||
1374                             /^tax_\d+$/
1375                         } keys(%{$form})));
1376
1377   reformat_numbers($output_numberformat, undef,
1378                    qw(qty price_factor),
1379                    grep({ /^qty_\d+$/
1380                         } keys(%{$form})));
1381
1382   $form->{IN} = "$form->{formname}$form->{language}${printer_code}.html";
1383   if ($form->{format} eq 'postscript') {
1384     $form->{postscript} = 1;
1385     $form->{IN} =~ s/html$/tex/;
1386   } elsif ($form->{"format"} =~ /pdf/) {
1387     $form->{pdf} = 1;
1388     if ($form->{"format"} =~ /opendocument/) {
1389       $form->{IN} =~ s/html$/odt/;
1390     } else {
1391       $form->{IN} =~ s/html$/tex/;
1392     }
1393   } elsif ($form->{"format"} =~ /opendocument/) {
1394     $form->{"opendocument"} = 1;
1395     $form->{"IN"} =~ s/html$/odt/;
1396   }
1397
1398   delete $form->{OUT};
1399
1400   if ($form->{media} eq 'printer') {
1401     $form->{OUT} = "| $form->{printer_command} &>/dev/null";
1402     $form->{printed} .= " $form->{formname}";
1403     $form->{printed} =~ s/^ //;
1404   }
1405   $printed = $form->{printed};
1406
1407   if ($form->{media} eq 'email') {
1408     $form->{subject} = qq|$form->{label} $form->{"${inv}number"}|
1409       unless $form->{subject};
1410
1411     $form->{emailed} .= " $form->{formname}";
1412     $form->{emailed} =~ s/^ //;
1413   }
1414   $emailed = $form->{emailed};
1415
1416   if ($form->{media} eq 'queue') {
1417     %queued = map { s|.*/|| } split / /, $form->{queued};
1418
1419     if ($filename = $queued{ $form->{formname} }) {
1420       $form->{queued} =~ s/\Q$form->{formname} $filename\E//;
1421       unlink "$spool/$filename";
1422       $filename =~ s/\..*$//g;
1423     } else {
1424       $filename = time;
1425       $filename .= $$;
1426     }
1427
1428     $filename .= ($form->{postscript}) ? '.ps' : '.pdf';
1429     $form->{OUT} = ">$spool/$filename";
1430
1431     # add type
1432     $form->{queued} .= " $form->{formname} $filename";
1433
1434     $form->{queued} =~ s/^ //;
1435   }
1436   $queued = $form->{queued};
1437
1438 # saving the history
1439   if(!exists $form->{addition}) {
1440     $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
1441     if($form->{media} =~ /printer/) {
1442         $form->{addition} = "PRINTED";
1443     }
1444     elsif($form->{media} =~ /email/) {
1445         $form->{addition} = "MAILED";
1446     }
1447     elsif($form->{media} =~ /queue/) {
1448         $form->{addition} = "QUEUED";
1449     }
1450     elsif($form->{media} =~ /screen/) {
1451         $form->{addition} = "SCREENED";
1452     }
1453     $form->save_history($form->dbconnect(\%myconfig));
1454   }
1455   # /saving the history
1456
1457   $form->parse_template(\%myconfig, $userspath);
1458
1459   $form->{callback} = "";
1460
1461   if ($form->{media} eq 'email') {
1462     $form->{message} = $locale->text('sent') unless $form->{message};
1463   }
1464   $message = $form->{message};
1465
1466   # if we got back here restore the previous form
1467   if ($form->{media} =~ /(printer|email|queue)/) {
1468
1469     $form->update_status(\%myconfig)
1470       if ($form->{media} eq 'queue' && $form->{id});
1471
1472     return $lxdebug->leave_sub() if ($old_form eq "return");
1473
1474     if ($old_form) {
1475
1476       $old_form->{"${inv}number"} = $form->{"${inv}number"};
1477
1478       # restore and display form
1479       map { $form->{$_} = $old_form->{$_} } keys %$old_form;
1480
1481       $form->{queued}  = $queued;
1482       $form->{printed} = $printed;
1483       $form->{emailed} = $emailed;
1484       $form->{message} = $message;
1485
1486       $form->{rowcount}--;
1487       map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
1488         qw(exchangerate creditlimit creditremaining);
1489
1490       for $i (1 .. $form->{paidaccounts}) {
1491         map {
1492           $form->{"${_}_$i"} =
1493             $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
1494         } qw(paid exchangerate);
1495       }
1496
1497       call_sub($display_form);
1498       exit;
1499     }
1500
1501     $msg =
1502       ($form->{media} eq 'printer')
1503       ? $locale->text('sent to printer')
1504       : $locale->text('emailed to') . " $form->{email}";
1505     $form->redirect(qq|$form->{label} $form->{"${inv}number"} $msg|);
1506   }
1507   if ($form->{printing}) {
1508    call_sub($display_form);
1509    exit; 
1510   }
1511
1512   $lxdebug->leave_sub();
1513 }
1514
1515 sub customer_details {
1516   $lxdebug->enter_sub();
1517   IS->customer_details(\%myconfig, \%$form, @_);
1518   $lxdebug->leave_sub();
1519 }
1520
1521 sub vendor_details {
1522   $lxdebug->enter_sub();
1523
1524   IR->vendor_details(\%myconfig, \%$form, @_);
1525
1526   $lxdebug->leave_sub();
1527 }
1528
1529 sub post_as_new {
1530   $lxdebug->enter_sub();
1531
1532   $form->{postasnew} = 1;
1533   map { delete $form->{$_} } qw(printed emailed queued);
1534
1535   &post;
1536
1537   $lxdebug->leave_sub();
1538 }
1539
1540 sub ship_to {
1541   $lxdebug->enter_sub();
1542   if ($form->{second_run}) {
1543     $form->{print_and_post} = 0;
1544   }
1545
1546   $title = $form->{title};
1547   $form->{title} = $locale->text('Ship to');
1548
1549   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
1550     qw(exchangerate creditlimit creditremaining);
1551
1552   my @shipto_vars =
1553     qw(shiptoname shiptostreet shiptozipcode shiptocity shiptocountry
1554        shiptocontact shiptophone shiptofax shiptoemail
1555        shiptodepartment_1 shiptodepartment_2);
1556
1557   my @addr_vars =
1558     (qw(name department_1 department_2 street zipcode city country
1559         contact email phone fax));
1560
1561   # get details for name
1562   call_sub("$form->{vc}_details", @addr_vars);
1563
1564   $number =
1565     ($form->{vc} eq 'customer')
1566     ? $locale->text('Customer Number')
1567     : $locale->text('Vendor Number');
1568
1569   # get pricegroups for parts
1570   IS->get_pricegroups_for_parts(\%myconfig, \%$form);
1571
1572   # build up html code for prices_$i
1573   set_pricegroup($form->{rowcount});
1574
1575   $nextsub = ($form->{display_form}) ? $form->{display_form} : "display_form";
1576
1577   $form->{rowcount}--;
1578
1579   $form->header;
1580
1581   print qq|
1582 <body>
1583
1584 <form method="post" action="$form->{script}">
1585
1586 <table width="100%">
1587   <tr>
1588     <td>
1589       <table>
1590         <tr class="listheading">
1591           <th class="listheading" colspan="2" width="50%">|
1592     . $locale->text('Billing Address') . qq|</th>
1593           <th class="listheading" width="50%">|
1594     . $locale->text('Shipping Address') . qq|</th>
1595         </tr>
1596         <tr height="5"></tr>
1597         <tr>
1598           <th align="right" nowrap>$number</th>
1599           <td>$form->{"$form->{vc}number"}</td>
1600         </tr>
1601         <tr>
1602           <th align="right" nowrap>| . $locale->text('Company Name') . qq|</th>
1603           <td>$form->{name}</td>
1604           <td><input name="shiptoname" size="35" value="$form->{shiptoname}"></td>
1605         </tr>
1606         <tr>
1607           <th align="right" nowrap>| . $locale->text('Department') . qq|</th>
1608           <td>$form->{department_1}</td>
1609           <td><input name="shiptodepartment_1" size="35" value="$form->{shiptodepartment_1}"></td>
1610         </tr>
1611         <tr>
1612           <th align="right" nowrap>&nbsp;</th>
1613           <td>$form->{department_2}</td>
1614           <td><input name="shiptodepartment_2" size="35" value="$form->{shiptodepartment_2}"></td>
1615         </tr>
1616         <tr>
1617           <th align="right" nowrap>| . $locale->text('Street') . qq|</th>
1618           <td>$form->{street}</td>
1619           <td><input name="shiptostreet" size="35" value="$form->{shiptostreet}"></td>
1620         </tr>
1621         <tr>
1622           <th align="right" nowrap>| . $locale->text('Zipcode') . qq|</th>
1623           <td>$form->{zipcode}</td>
1624           <td><input name="shiptozipcode" size="35" value="$form->{shiptozipcode}"></td>
1625         </tr>
1626         <tr>
1627           <th align="right" nowrap>| . $locale->text('City') . qq|</th>
1628           <td>$form->{city}</td>
1629           <td><input name="shiptocity" size="35" value="$form->{shiptocity}"></td>
1630         </tr>
1631         <tr>
1632           <th align="right" nowrap>| . $locale->text('Country') . qq|</th>
1633           <td>$form->{country}</td>
1634           <td><input name="shiptocountry" size="35" value="$form->{shiptocountry}"></td>
1635         </tr>
1636         <tr>
1637           <th align="right" nowrap>| . $locale->text('Contact') . qq|</th>
1638           <td>$form->{contact}</td>
1639           <td><input name="shiptocontact" size="35" value="$form->{shiptocontact}"></td>
1640         </tr>
1641         <tr>
1642           <th align="right" nowrap>| . $locale->text('Phone') . qq|</th>
1643           <td>$form->{phone}</td>
1644           <td><input name="shiptophone" size="20" value="$form->{shiptophone}"></td>
1645         </tr>
1646         <tr>
1647           <th align="right" nowrap>| . $locale->text('Fax') . qq|</th>
1648           <td>$form->{fax}</td>
1649           <td><input name="shiptofax" size="20" value="$form->{shiptofax}"></td>
1650         </tr>
1651         <tr>
1652           <th align="right" nowrap>| . $locale->text('E-mail') . qq|</th>
1653           <td>$form->{email}</td>
1654           <td><input name="shiptoemail" size="35" value="$form->{shiptoemail}"></td>
1655         </tr>
1656       </table>
1657     </td>
1658   </tr>
1659 </table>
1660 | . $cgi->hidden("-name" => "nextsub", "-value" => $nextsub);
1661 ;
1662
1663
1664
1665   # delete shipto
1666   map({ delete $form->{$_} } (@shipto_vars, qw(header)));
1667   $form->{title} = $title;
1668
1669   foreach $key (keys %$form) {
1670     $form->{$key} =~ s/\"/&quot;/g;
1671     print qq|<input type="hidden" name="$key" value="$form->{$key}">\n|;
1672   }
1673
1674   print qq|
1675
1676 <hr size="3" noshade>
1677
1678 <br>
1679 <input class="submit" type="submit" name="action" value="|
1680     . $locale->text('Continue') . qq|">
1681 </form>
1682
1683 </body>
1684 </html>
1685 |;
1686
1687   $lxdebug->leave_sub();
1688 }
1689
1690 sub new_license {
1691   $lxdebug->enter_sub();
1692
1693   my $row = shift;
1694
1695   # change callback
1696   $form->{old_callback} = $form->escape($form->{callback}, 1);
1697   $form->{callback} = $form->escape("$form->{script}?action=display_form", 1);
1698   $form->{old_callback} = $form->escape($form->{old_callback}, 1);
1699
1700   # delete action
1701   delete $form->{action};
1702   $customer = $form->{customer};
1703   map { $form->{"old_$_"} = $form->{"${_}_$row"} } qw(partnumber description);
1704
1705   # save all other form variables in a previousform variable
1706   $form->{row} = $row;
1707   foreach $key (keys %$form) {
1708
1709     # escape ampersands
1710     $form->{$key} =~ s/&/%26/g;
1711     $previousform .= qq|$key=$form->{$key}&|;
1712   }
1713   chop $previousform;
1714   $previousform = $form->escape($previousform, 1);
1715
1716   $form->{script} = "licenses.pl";
1717
1718   map { $form->{$_} = $form->{"old_$_"} } qw(partnumber description);
1719   map { $form->{$_} = $form->escape($form->{$_}, 1) }
1720     qw(partnumber description);
1721   $form->{callback} =
1722     qq|$form->{script}?login=$form->{login}&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|;
1723   $form->redirect;
1724
1725   $lxdebug->leave_sub();
1726 }
1727
1728 sub relink_accounts {
1729   $lxdebug->enter_sub();
1730
1731   $form->{"taxaccounts"} =~ s/\s*$//;
1732   $form->{"taxaccounts"} =~ s/^\s*//;
1733   foreach my $accno (split(/\s*/, $form->{"taxaccounts"})) {
1734     map({ delete($form->{"${accno}_${_}"}); } qw(rate description taxnumber));
1735   }
1736   $form->{"taxaccounts"} = "";
1737
1738   for (my $i = 1; $i <= $form->{"rowcount"}; $i++) {
1739     if ($form->{"id_$i"}) {
1740       IC->retrieve_accounts(\%myconfig, $form, $form->{"id_$i"}, $i, 1);
1741     }
1742   }
1743
1744   $lxdebug->leave_sub();
1745 }
1746
1747 sub set_duedate {
1748   $lxdebug->enter_sub();
1749
1750   $form->get_duedate(\%myconfig);
1751
1752   my $q = new CGI;
1753   $result = "$form->{duedate}";
1754   print $q->header();
1755   print $result;
1756   $lxdebug->leave_sub();
1757
1758 }
1759