...und die neuen Dateien
[kivitendo-erp.git] / bin / mozilla / invoice_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 but not in 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_invoice_io.pl") {
48   eval { require "bin/mozilla/custom_ivvoice_io.pl"; };
49 }
50 if (-f "bin/mozilla/$form->{login}_invoice_io.pl") {
51   eval { require "bin/mozilla/$form->{login}_invoice_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   my $is_sales =
98     (substr($form->{type}, 0, 6) eq "sales_")
99     || (($form->{type} eq "invoice") && ($form->{script} eq "is.pl"))
100     || ($form->{type} eq 'credit_note');
101
102   if ($lizenzen && $form->{vc} eq "customer") {
103     if ($form->{type} =~ /sales_order/) {
104       @column_index = (runningnumber, partnumber, description, ship, qty);
105     } elsif ($form->{type} =~ /sales_quotation/) {
106       @column_index = (runningnumber, partnumber, description, qty);
107     } else {
108       @column_index = (runningnumber, partnumber, description, qty);
109     }
110   } else {
111     if (   ($form->{type} =~ /purchase_order/)
112         || ($form->{type} =~ /sales_order/)) {
113       @column_index = (runningnumber, partnumber, description, ship, qty);
114         } else {
115       @column_index = (runningnumber, partnumber, description, qty);
116     }
117   }
118 ############## ENDE Neueintrag ##################
119
120   my $dimension_units = AM->retrieve_units(\%myconfig, $form, "dimension");
121   my $service_units = AM->retrieve_units(\%myconfig, $form, "service");
122   my $all_units = AM->retrieve_units(\%myconfig, $form);
123
124   my %price_factors = map { $_->{id} => $_->{factor} } @{ $form->{ALL_PRICE_FACTORS} };
125
126   push @column_index, qw(unit);
127
128   #for pricegroups column
129   if (   $form->{type} =~ (/sales_quotation/)
130       or (($form->{level} =~ /Sales/) and ($form->{type} =~ /invoice/))
131       or (($form->{level} eq undef) and ($form->{type} =~ /invoice/))
132       or ($form->{type} =~ /sales_order/)) {
133     push @column_index, qw(sellprice_pg);
134   }
135
136   push @column_index, qw(sellprice);
137
138   if ($form->{vc} eq 'customer') {
139     push @column_index, qw(discount);
140   }
141
142   push @column_index, "linetotal";
143
144   my $colspan = $#column_index + 1;
145
146   $form->{invsubtotal} = 0;
147   map { $form->{"${_}_base"} = 0 } (split(/ /, $form->{taxaccounts}));
148
149 ########################################
150   # Eintrag fuer Version 2.2.0 geaendert #
151   # neue Optik im Rechnungsformular      #
152 ########################################
153   $column_data{runningnumber} =
154       qq|<th align="left" nowrap width="5" class="listheading">|
155     . $locale->text('No.')
156     . qq|</th>|;
157   $column_data{partnumber} =
158       qq|<th align="left" nowrap width="12" class="listheading">|
159     . $locale->text('Number')
160     . qq|</th>|;
161   $column_data{description} =
162       qq|<th align="left" nowrap width="30" class="listheading">|
163     . $locale->text('Part Description')
164     . qq|</th>|;
165   if ($form->{"type"} eq "purchase_order") {
166     $column_data{ship} =
167       qq|<th align="left" nowrap width="5" class="listheading">|
168       . $locale->text('Ship rcvd')
169       . qq|</th>|;
170   } else {
171     $column_data{ship} =
172       qq|<th align="left" nowrap width="5" class="listheading">|
173       . $locale->text('Ship')
174       . qq|</th>|;
175   }
176   $column_data{qty} =
177       qq|<th align="left" nowrap width="5" class="listheading">|
178     . $locale->text('Qty')
179     . qq|</th>|;
180   $column_data{unit} =
181       qq|<th align="left" nowrap width="5" class="listheading">|
182     . $locale->text('Unit')
183     . qq|</th>|;
184   $column_data{license} =
185       qq|<th align="left" nowrap width="10" class="listheading">|
186     . $locale->text('License')
187     . qq|</th>|;
188   $column_data{serialnr} =
189       qq|<th align="left" nowrap width="10" class="listheading">|
190     . $locale->text('Serial No.')
191     . qq|</th>|;
192   $column_data{projectnr} =
193       qq|<th align="left" nowrap width="10" class="listheading">|
194     . $locale->text('Project')
195     . qq|</th>|;
196   $column_data{sellprice} =
197       qq|<th align="left" nowrap width="15" class="listheading">|
198     . $locale->text('Price')
199     . qq|</th>|;
200   $column_data{sellprice_pg} =
201       qq|<th align="left" nowrap width="15" class="listheading">|
202     . $locale->text('Pricegroup')
203     . qq|</th>|;
204   $column_data{discount} =
205       qq|<th align="left" class="listheading">|
206     . $locale->text('Discount')
207     . qq|</th>|;
208   $column_data{linetotal} =
209       qq|<th align="left" nowrap width="10" class="listheading">|
210     . $locale->text('Extended')
211     . qq|</th>|;
212   $column_data{bin} =
213       qq|<th align="left" nowrap width="10" class="listheading">|
214     . $locale->text('Bin')
215     . qq|</th>|;
216 ############## ENDE Neueintrag ##################
217
218   $myconfig{"show_form_details"} = 1
219     unless (defined($myconfig{"show_form_details"}));
220   $form->{"show_details"} = $myconfig{"show_form_details"}
221     unless (defined($form->{"show_details"}));
222   $form->{"show_details"} = $form->{"show_details"} ? 1 : 0;
223   my $show_details_new = 1 - $form->{"show_details"};
224   my $show_details_checked = $form->{"show_details"} ? "checked" : "";
225
226   print qq|
227   <tr>
228     <td>| . $cgi->hidden("-name" => "show_details", "-value" => $form->{show_details}) . qq|
229       <input type="checkbox" id="cb_show_details" onclick="show_form_details($show_details_new);" $show_details_checked>
230       <label for="cb_show_details">| . $locale->text("Show details") . qq|</label><br>
231       <table width="100%">
232         <tr class="listheading">|;
233
234   map { print "\n$column_data{$_}" } @column_index;
235
236   print qq|
237         </tr>
238 |;
239
240   $runningnumber = $locale->text('No.');
241   $deliverydate  = $locale->text('Delivery Date');
242   $serialnumber  = $locale->text('Serial No.');
243   $projectnumber = $locale->text('Project');
244   $partsgroup    = $locale->text('Group');
245   $reqdate       = $locale->text('Reqdate');
246
247   $delvar = 'deliverydate';
248
249   if ($form->{type} =~ /_order$/ || $form->{type} =~ /_quotation$/) {
250     $deliverydate = $locale->text('Required by');
251     $delvar       = 'reqdate';
252   }
253
254   $form->{marge_total} = 0;
255   $form->{sellprice_total} = 0;
256   $form->{lastcost_total} = 0;
257   my %projectnumber_labels = ();
258   my @projectnumber_values = ("");
259   foreach my $item (@{ $form->{"ALL_PROJECTS"} }) {
260     push(@projectnumber_values, $item->{"id"});
261     $projectnumber_labels{$item->{"id"}} = $item->{"projectnumber"};
262   }
263
264   for $i (1 .. $numrows) {
265
266     # undo formatting
267     map {
268       $form->{"${_}_$i"} =
269         $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
270     } qw(qty ship discount sellprice price_new price_old) unless ($form->{simple_save});
271
272     if (!$form->{"unit_old_$i"}) {
273       # Neue Ware aus der Datenbank. In diesem Fall ist unit_$i die
274       # Einheit, wie sie in den Stammdaten hinterlegt wurde.
275       # Es sollte also angenommen werden, dass diese ausgewaehlt war.
276       $form->{"unit_old_$i"} = $form->{"unit_$i"};
277     }
278
279     # Die zuletzt ausgewaehlte mit der aktuell ausgewaehlten Einheit
280     # vergleichen und bei Unterschied den Preis entsprechend umrechnen.
281     $form->{"selected_unit_$i"} = $form->{"unit_$i"} unless ($form->{"selected_unit_$i"});
282
283     my $check_units = $form->{"inventory_accno_$i"} ? $dimension_units : $service_units;
284     if (!$check_units->{$form->{"selected_unit_$i"}} ||
285         ($check_units->{$form->{"selected_unit_$i"}}->{"base_unit"} ne
286          $all_units->{$form->{"unit_old_$i"}}->{"base_unit"})) {
287       # Die ausgewaehlte Einheit ist fuer diesen Artikel nicht gueltig
288       # (z.B. Dimensionseinheit war ausgewaehlt, es handelt sich aber
289       # um eine Dienstleistung). Dann keinerlei Umrechnung vornehmen.
290       $form->{"unit_old_$i"} = $form->{"selected_unit_$i"} = $form->{"unit_$i"};
291     }
292     if ((!$form->{"prices_$i"}) || ($form->{"new_pricegroup_$i"} == $form->{"old_pricegroup_$i"})) {
293       if ($form->{"unit_old_$i"} ne $form->{"selected_unit_$i"}) {
294         my $basefactor = 1;
295         if (defined($all_units->{$form->{"unit_old_$i"}}->{"factor"}) &&
296             $all_units->{$form->{"unit_old_$i"}}->{"factor"}) {
297           $basefactor = $all_units->{$form->{"selected_unit_$i"}}->{"factor"} /
298             $all_units->{$form->{"unit_old_$i"}}->{"factor"};
299         }
300         $form->{"sellprice_$i"} *= $basefactor;
301         $form->{"unit_old_$i"} = $form->{"selected_unit_$i"};
302       }
303     }
304
305     ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
306     $decimalplaces = max length($dec), 2;
307
308     $price_factor = $price_factors{$form->{"price_factor_id_$i"}} || 1;
309     $discount     = (100 - $form->{"discount_$i"} * 1) / 100;
310
311     $linetotal    = $form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"} * $discount / $price_factor, $decimalplaces);
312
313     my $real_sellprice = $form->{"sellprice_$i"} * $discount / $price_factor;
314
315     # marge calculations
316     my ($marge_font_start, $marge_font_end);
317
318     $form->{"lastcost_$i"} *= 1;
319
320     $marge_price_factor = $form->{"marge_price_factor_$i"} * 1 || 1;
321
322     if ($real_sellprice && ($form->{"qty_$i"} * 1)) {
323       $form->{"marge_percent_$i"}     = ($real_sellprice - $form->{"lastcost_$i"} / $marge_price_factor) * 100 / $real_sellprice;
324       $myconfig{"marge_percent_warn"} = 15 unless (defined($myconfig{"marge_percent_warn"}));
325
326       if ($form->{"id_$i"} &&
327           ($form->{"marge_percent_$i"} < (1 * $myconfig{"marge_percent_warn"}))) {
328         $marge_font_start = "<font color=\"#ff0000\">";
329         $marge_font_end   = "</font>";
330       }
331
332     } else {
333       $form->{"marge_percent_$i"} = 0;
334     }
335
336     my $marge_adjust_credit_note = $form->{type} eq 'credit_note' ? -1 : 1;
337     $form->{"marge_total_$i"}  = ($real_sellprice - $form->{"lastcost_$i"} / $marge_price_factor) * $form->{"qty_$i"} * $marge_adjust_credit_note;
338     $form->{"marge_total"}      += $form->{"marge_total_$i"};
339     $form->{"lastcost_total"}   += $form->{"lastcost_$i"} * $form->{"qty_$i"} / $marge_price_factor;
340     $form->{"sellprice_total"}  += $real_sellprice * $form->{"qty_$i"};
341
342     map { $form->{"${_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}, 2) } qw(marge_total marge_percent);
343
344     # convert " to &quot;
345     map { $form->{"${_}_$i"} =~ s/\"/&quot;/g }
346       qw(partnumber description unit unit_old);
347
348 ########################################
349     # Eintrag fuer Version 2.2.0 geaendert #
350     # neue Optik im Rechnungsformular      #
351 ########################################
352     $column_data{runningnumber} =
353       qq|<td><input name="runningnumber_$i" size="5" value="$i"></td>|;    # HuT
354 ############## ENDE Neueintrag ##################
355
356     $column_data{partnumber} =
357       qq|<td><input name="partnumber_$i" size=12 value="$form->{"partnumber_$i"}"></td>|;
358
359     if (($rows = $form->numtextrows($form->{"description_$i"}, 30, 6)) > 1) {
360       $column_data{description} =
361         qq|<td><textarea name="description_$i" rows="$rows" cols="30" wrap="soft">| . H($form->{"description_$i"}) . qq|</textarea><button type="button" onclick="set_longdescription_window('longdescription_$i')">| . $locale->text('L') . qq|</button></td>|;
362     } else {
363       $column_data{description} =
364         qq|<td><input name="description_$i" size="30" value="| . $form->quote($form->{"description_$i"}) . qq|"><button type="button" onclick="set_longdescription_window('longdescription_$i')">| . $locale->text('L') . qq|</button></td>|;
365     }
366
367     (my $qty_dec) = ($form->{"qty_$i"} =~ /\.(\d+)/);
368     $qty_dec = length $qty_dec;
369
370     $column_data{qty} =
371         qq|<td align="right"><input name="qty_$i" size="5" value="|
372       . $form->format_amount(\%myconfig, $form->{"qty_$i"}, $qty_dec) .qq|">|;
373     if ($form->{"formel_$i"}) {
374       $column_data{qty} .= qq|<button type="button" onclick="calculate_qty_selection_window('qty_$i','alu_$i', 'formel_$i', $i)">| . $locale->text('*/') . qq|</button>|
375         . $cgi->hidden("-name" => "formel_$i", "-value" => $form->{"formel_$i"}) . $cgi->hidden("-name" => "alu_$i", "-value" => $form->{"alu_$i"});
376     }
377     $column_data{qty} .= qq|</td>|;
378     $column_data{ship} =
379         qq|<td align="right"><input name="ship_$i" size=5 value="|
380       . $form->format_amount(\%myconfig, $form->{"ship_$i"})
381       . qq|"></td>|;
382
383     my $is_part     = $form->{"inventory_accno_$i"};
384     my $is_assembly = $form->{"assembly_$i"};
385     my $is_assigned = $form->{"id_$i"};
386     my $this_unit = $form->{"unit_$i"};
387     if ($form->{"selected_unit_$i"} && $this_unit &&
388         $all_units->{$form->{"selected_unit_$i"}} && $all_units->{$this_unit} &&
389         ($all_units->{$form->{"selected_unit_$i"}}->{"base_unit"} eq $all_units->{$this_unit}->{"base_unit"})) {
390       $this_unit = $form->{"selected_unit_$i"};
391     } elsif (!$is_assigned ||
392              ($is_part && !$this_unit && ($all_units->{$this_unit} && ($all_units->{$this_unit}->{"base_unit"} eq $all_units->{"kg"}->{"base_unit"})))) {
393       $this_unit = "kg";
394     }
395
396     my $price_factor_select;
397     if (0 < scalar @{ $form->{ALL_PRICE_FACTORS} }) {
398       my @values = ('', map { $_->{id}                      } @{ $form->{ALL_PRICE_FACTORS} });
399       my %labels =      map { $_->{id} => $_->{description} } @{ $form->{ALL_PRICE_FACTORS} };
400
401       $price_factor_select =
402         NTI($cgi->popup_menu('-name'    => "price_factor_id_$i",
403                              '-default' => $form->{"price_factor_id_$i"},
404                              '-values'  => \@values,
405                              '-labels'  => \%labels,
406                              '-style'   => 'width:90px'))
407         . ' ';
408     }
409
410     $column_data{"unit"} = "<td>" .
411       $price_factor_select .
412        AM->unit_select_html($is_part || $is_assembly ? $dimension_units :
413                             $is_assigned ? $service_units : $all_units,
414                             "unit_$i", $this_unit,
415                             $is_assigned ? $form->{"unit_$i"} : undef)
416       . "</td>";
417
418     # build in drop down list for pricesgroups
419     if ($form->{"prices_$i"}) {
420       if  ($form->{"new_pricegroup_$i"} != $form->{"old_pricegroup_$i"}) {
421         $price_tmp = $form->format_amount(\%myconfig, $form->{"price_new_$i"}, $decimalplaces);
422       } else {
423         $price_tmp = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
424       }
425
426       $column_data{sellprice_pg} =
427       qq|<td align="right"><select name="sellprice_pg_$i">$form->{"prices_$i"}</select></td>|;
428       $column_data{sellprice} =
429       qq|<td><input name="sellprice_$i" size="10" value="$price_tmp" onBlur=\"check_right_number_format(this)\"></td>|;
430     } else {
431
432       # for last row and report
433       # set pricegroup drop down list from report menu
434       if ($form->{"sellprice_$i"} != 0) {
435         $prices =
436           qq|<option value="$form->{"sellprice_$i"}--$form->{"pricegroup_id_$i"}" selected>$form->{"pricegroup_$i"}</option>\n|;
437
438         $form->{"pricegroup_old_$i"} = $form->{"pricegroup_id_$i"};
439
440         $column_data{sellprice_pg} =
441           qq|<td align="right"><select name="sellprice_pg_$i">$prices</select></td>|;
442
443       } else {
444
445         # for last row
446         $column_data{sellprice_pg} = qq|<td align="right">&nbsp;</td>|;
447         }
448         
449       $column_data{sellprice} =
450       qq|<td><input name="sellprice_$i" size="10" onBlur=\"check_right_number_format(this)\" value="|
451         . $form->format_amount(\%myconfig, $form->{"sellprice_$i"},
452                                $decimalplaces)
453         . qq|"></td>|;
454     }
455     $column_data{discount} =
456         qq|<td align="right"><input name="discount_$i" size=3 value="|
457       . $form->format_amount(\%myconfig, $form->{"discount_$i"})
458       . qq|"></td>|;
459     $column_data{linetotal} =
460         qq|<td align="right">|
461       . $form->format_amount(\%myconfig, $linetotal, 2)
462       . qq|</td>|;
463     $column_data{bin} = qq|<td>$form->{"bin_$i"}</td>|;
464
465 ########################################
466     # Eintrag fuer Version 2.2.0 geaendert #
467     # neue Optik im Rechnungsformular      #
468 ########################################
469     #     if ($lizenzen &&  $form->{type} eq "invoice" &&  $form->{vc} eq "customer") {
470     #     $column_data{license} = qq|<td><select name="licensenumber_$i">$form->{"lizenzen_$i"}></select></td>|;
471     #     }
472     #
473     #     if ($form->{type} !~ /_quotation/) {
474     #     $column_data{serialnr} = qq|<td><input name="serialnumber_$i" size=10 value="$form->{"serialnumber_$i"}"></td>|;
475     #     }
476     #
477     #     $column_data{projectnr} = qq|<td><input name="projectnumber_$i" size=10 value="$form->{"projectnumber_$i"}"></td>|;
478 ############## ENDE Neueintrag ##################
479     my $j = $i % 2;
480     print qq|
481
482         <tr valign="top" class="listrow$j">|;
483
484     map { print "\n$column_data{$_}" } @column_index;
485
486     print("</tr>\n" .
487           $cgi->hidden("-name" => "unit_old_$i",
488                        "-value" => $form->{"selected_unit_$i"})
489           . "\n" .
490           $cgi->hidden("-name" => "price_new_$i",
491                        "-value" => $form->format_amount(\%myconfig, $form->{"price_new_$i"}))
492           . "\n");
493     map({ print($cgi->hidden("-name" => $_, "-value" => $form->{$_}) . "\n"); }
494         ("orderitems_id_$i", "bo_$i", "pricegroup_old_$i", "price_old_$i",
495          "id_$i", "inventory_accno_$i", "bin_$i", "partsgroup_$i", "partnotes_$i",
496          "income_accno_$i", "expense_accno_$i", "listprice_$i", "assembly_$i",
497          "taxaccounts_$i", "ordnumber_$i", "transdate_$i", "cusordnumber_$i",
498          "longdescription_$i", "basefactor_$i", "marge_total_$i", "marge_percent_$i", "lastcost_$i",
499          "marge_price_factor_$i"));
500
501 ########################################
502     # Eintrag fuer Version 2.2.0 geaendert #
503     # neue Optik im Rechnungsformular      #
504 ########################################
505
506     my $row_style_attr =
507       'style="display:none;"' if (!$form->{"show_details"});
508
509     # print second row
510     print qq|
511         <tr  class="listrow$j" $row_style_attr>
512           <td colspan="$colspan">
513 |;
514     if ($lizenzen && $form->{type} eq "invoice" && $form->{vc} eq "customer") {
515       my $selected = $form->{"licensenumber_$i"};
516       my $lizenzen_quoted;
517       $form->{"lizenzen_$i"} =~ s/ selected//g;
518       $form->{"lizenzen_$i"} =~
519         s/value="${selected}"\>/value="${selected}" selected\>/;
520       $lizenzen_quoted = $form->{"lizenzen_$i"};
521       $lizenzen_quoted =~ s/\"/&quot;/g;
522       print qq|
523         <b>Lizenz\#</b>&nbsp;<select name="licensenumber_$i" size="1">
524         $form->{"lizenzen_$i"}
525         </select>
526         <input type="hidden" name="lizenzen_$i" value="${lizenzen_quoted}">
527 |;
528     }
529     if ($form->{type} !~ /_quotation/) {
530       print qq|
531           <b>$serialnumber</b>&nbsp;<input name="serialnumber_$i" size="15" value="$form->{"serialnumber_$i"}">|;
532     }
533
534     print qq|<b>$projectnumber</b>&nbsp;| .
535       NTI($cgi->popup_menu('-name' => "project_id_$i",
536                            '-values' => \@projectnumber_values,
537                            '-labels' => \%projectnumber_labels,
538                            '-default' => $form->{"project_id_$i"}));
539
540     if ($form->{type} eq 'invoice' or $form->{type} =~ /order/) {
541       my $reqdate_term =
542         ($form->{type} eq 'invoice')
543         ? 'deliverydate'
544         : 'reqdate';    # invoice uses a different term for the same thing.
545       print qq|
546         <b>${$reqdate_term}</b>&nbsp;<input name="${reqdate_term}_$i" size="11" onBlur="check_right_date_format(this)" value="$form->{"${reqdate_term}_$i"}">
547 |;
548     }
549     my $subtotalchecked = ($form->{"subtotal_$i"}) ? "checked" : "";
550     print qq|
551           <b>|.$locale->text('Subtotal').qq|</b>&nbsp;<input type="checkbox" name="subtotal_$i" value="1" $subtotalchecked>
552 |;
553
554     if ($form->{"id_$i"} && $is_sales) {
555       my $marge_price_factor;
556
557       $form->{"marge_price_factor_$i"} *= 1;
558
559       if ($form->{"marge_price_factor_$i"} && (1 != $form->{"marge_price_factor_$i"})) {
560         $marge_price_factor = '/' . $form->format_amount(\%myconfig, $form->{"marge_price_factor_$i"});
561       }
562
563       print qq|
564           ${marge_font_start}<b>| . $locale->text('Ertrag') . qq|</b>&nbsp;$form->{"marge_total_$i"}&nbsp;$form->{"marge_percent_$i"} % ${marge_font_end}|;
565    }
566    print qq|
567           &nbsp;<b>| . $locale->text('LP') . qq|</b>&nbsp;| . $form->format_amount(\%myconfig, $form->{"listprice_$i"}, 2) . qq|
568           &nbsp;<b>| . $locale->text('EK') . qq|</b>&nbsp;| . $form->format_amount(\%myconfig, $form->{"lastcost_$i"}, 2) . $marge_price_factor;
569
570
571     print qq|
572           </td>
573         </tr>
574 |;
575
576 ############## ENDE Neueintrag ##################
577
578     map { $form->{"${_}_base"} += $linetotal }
579       (split(/ /, $form->{"taxaccounts_$i"}));
580
581     $form->{invsubtotal} += $linetotal;
582   }
583
584   print qq|
585       </table>
586     </td>
587   </tr>
588 |;
589
590   if (0 != ($form->{sellprice_total} * 1)) {
591     $form->{marge_percent} = ($form->{sellprice_total} - $form->{lastcost_total}) / $form->{sellprice_total} * 100;
592   }
593
594   $lxdebug->leave_sub();
595 }
596
597 sub set_pricegroup {
598   $lxdebug->enter_sub();
599   my $rowcount = shift;
600   for $j (1 .. $rowcount) {
601     my $pricegroup_old = $form->{"pricegroup_old_$i"};
602     if ($form->{PRICES}{$j}) {
603       $len    = 0;
604       $prices = '<option value="--">' . $locale->text("none (pricegroup)") . '</option>';
605       $price  = 0;
606       foreach $item (@{ $form->{PRICES}{$j} }) {
607
608         #$price = $form->round_amount($myconfig,  $item->{price}, 5);
609         #$price = $form->format_amount($myconfig, $item->{price}, 2);
610         $price         = $item->{price};
611         $pricegroup_id = $item->{pricegroup_id};
612         $pricegroup    = $item->{pricegroup};
613
614         # build drop down list for pricegroups
615         $prices .=
616           qq|<option value="$price--$pricegroup_id"$item->{selected}>$pricegroup</option>\n|;
617
618         $len += 1;
619
620         #        map {
621         #               $form->{"${_}_$j"} =
622         #               $form->format_amount(\%myconfig, $form->{"${_}_$j"})
623         #              } qw(sellprice price_new price_old);
624
625         # set new selectedpricegroup_id and prices for "Preis"
626         if ($item->{selected} && ($pricegroup_id != 0)) {
627           $form->{"pricegroup_old_$j"} = $pricegroup_id;
628           $form->{"price_new_$j"}      = $price;
629           $form->{"sellprice_$j"}      = $price;
630         }
631         if ($pricegroup_id == 0) {
632           $form->{"price_new_$j"} = $form->{"sellprice_$j"};
633         }
634       }
635       $form->{"prices_$j"} = $prices;
636     }
637   }
638   $lxdebug->leave_sub();
639 }
640
641 sub display_form {
642   $lxdebug->enter_sub();
643
644   relink_accounts();
645
646   my $new_rowcount = $form->{"rowcount"} * 1 + 1;
647   $form->{"project_id_${new_rowcount}"} = $form->{"globalproject_id"};
648
649   $form->language_payment(\%myconfig);
650
651   # if we have a display_form
652   if ($form->{display_form}) {
653     call_sub($form->{"display_form"});
654     exit;
655   }
656
657   Common::webdav_folder($form) if ($webdav);
658
659   #   if (   $form->{print_and_post}
660   #       && $form->{second_run}
661   #       && ($form->{action} eq "display_form")) {
662   #     for (keys %$form) { $old_form->{$_} = $form->{$_} }
663   #     $old_form->{rowcount}++;
664   #
665   #     #$form->{rowcount}--;
666   #     #$form->{rowcount}--;
667   #
668   #     $form->{print_and_post} = 0;
669   #
670   #     &print_form($old_form);
671   #     exit;
672   #   }
673   #
674   #   $form->{action}   = "";
675   #   $form->{resubmit} = 0;
676   #
677   #   if ($form->{print_and_post} && !$form->{second_run}) {
678   #     $form->{second_run} = 1;
679   #     $form->{action}     = "display_form";
680   #     $form->{rowcount}--;
681   #     my $rowcount = $form->{rowcount};
682   #
683   #     # get pricegroups for parts
684   #     IS->get_pricegroups_for_parts(\%myconfig, \%$form);
685   #
686   #     # build up html code for prices_$i
687   #     set_pricegroup($rowcount);
688   #
689   #     $form->{resubmit} = 1;
690   #
691   #   }
692   &form_header;
693
694   $numrows    = ++$form->{rowcount};
695   $subroutine = "display_row";
696
697   if ($form->{item} eq 'part') {
698
699     #set preisgruppenanzahl
700     $numrows    = $form->{price_rows};
701     $subroutine = "price_row";
702
703     &{$subroutine}($numrows);
704
705     $numrows    = ++$form->{makemodel_rows};
706     $subroutine = "makemodel_row";
707   }
708   if ($form->{item} eq 'assembly') {
709     $numrows    = $form->{price_rows};
710     $subroutine = "price_row";
711
712     &{$subroutine}($numrows);
713
714     $numrows    = ++$form->{makemodel_rows};
715     $subroutine = "makemodel_row";
716
717     # create makemodel rows
718     &{$subroutine}($numrows);
719
720     $numrows    = ++$form->{assembly_rows};
721     $subroutine = "assembly_row";
722   }
723   if ($form->{item} eq 'service') {
724     $numrows    = $form->{price_rows};
725     $subroutine = "price_row";
726
727     &{$subroutine}($numrows);
728
729     $numrows = 0;
730   }
731
732   # create rows
733   &{$subroutine}($numrows) if $numrows;
734
735   &form_footer;
736
737   $lxdebug->leave_sub();
738 }