e9b13a3271555b922cd99d808e8644106eb7cc68
[kivitendo-erp.git] / bin / mozilla / ic.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 #=====================================================================
8 # SQL-Ledger, Accounting
9 # Copyright (c) 2001
10 #
11 #  Author: Dieter Simader
12 #   Email: dsimader@sql-ledger.org
13 #     Web: http://www.sql-ledger.org
14 #
15 #
16 # This program is free software; you can redistribute it and/or modify
17 # it under the terms of the GNU General Public License as published by
18 # the Free Software Foundation; either version 2 of the License, or
19 # (at your option) any later version.
20 #
21 # This program is distributed in the hope that it will be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 # GNU General Public License for more details.
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not, write to the Free Software
27 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #======================================================================
29 #
30 # Inventory Control module
31 #
32 #======================================================================
33 #$locale->text('ea');
34
35 use POSIX qw(strftime);
36
37 use SL::IC;
38 use SL::ReportGenerator;
39
40 #use SL::PE;
41
42 # use strict;
43 #use warnings;
44
45 # global imports
46 our ($form, $locale, %myconfig, $lxdebug);
47
48 require "bin/mozilla/io.pl";
49 require "bin/mozilla/invoice_io.pl";
50 require "bin/mozilla/common.pl";
51 require "bin/mozilla/reportgenerator.pl";
52
53 1;
54
55 # end of main
56
57 sub add {
58   $lxdebug->enter_sub();
59
60   $form->{title} = $locale->text('Add ' . ucfirst $form->{item});
61
62   $form->{callback} =
63     "$form->{script}?action=add&item=$form->{item}&login=$form->{login}&password=$form->{password}"
64     unless $form->{callback};
65
66   $form->{"unit_changeable"} = 1;
67
68   IC->get_pricegroups(\%myconfig, \%$form);
69   &link_part;
70   &display_form;
71
72   $lxdebug->leave_sub();
73 }
74
75 sub search {
76   $lxdebug->enter_sub();
77
78   # switch for backward sorting
79   $form->{revers} = 0;
80
81   # memory for which table was sort at last time
82   $form->{lastsort} = "";
83
84   # counter for added entries to top100
85   $form->{ndxs_counter} = 0;
86
87   # $locale->text('Parts')
88   # $locale->text('Services')
89
90   my $is_service  = $form->{searchitems} eq 'service';
91   my $is_assembly = $form->{searchitems} eq 'assembly';
92
93   $form->{title} = (ucfirst $form->{searchitems}) . "s";
94   $form->{title} = $locale->text($form->{title});
95   $form->{title} = $locale->text('Assemblies') if $is_assembly;
96
97   $form->{jsscript} = 1;
98
99   $form->header;
100
101   print $form->parse_html_template('ic/search', { is_assembly => $is_assembly, 
102                                                   is_service  => $is_service, 
103                                                   dateformat  => $myconfig{dateformat}, });
104
105   $lxdebug->leave_sub();
106 }    #end search()
107
108 sub search_update_prices {
109   $lxdebug->enter_sub();
110
111   my $pricegroups = IC->get_pricegroups(\%myconfig, \%$form);
112
113   $form->header;
114
115   print $form->parse_html_template('ic/search_update_prices', { PRICE_ROWS => $pricegroups });
116
117   $lxdebug->leave_sub();
118 }    #end search()
119
120 sub confirm_price_update {
121   $lxdebug->enter_sub();
122
123   $form->{nextsub} = "update_prices";
124   $form->header;
125
126   map { delete $form->{$_} } qw(action header);
127   
128   print $form->parse_html_template('ic/confirm_price_update', { HIDDENS => [ map { name => $_, value => $form->{$_} }, keys %$form ] });
129
130   $lxdebug->leave_sub();
131 }
132
133 sub update_prices {
134   $lxdebug->enter_sub();
135
136   if (IC->update_prices(\%myconfig, \%$form)) {
137     $form->redirect($form->{update_count} . $locale->text('prices updated!'));
138   } else {
139     $form->error($locale->text('Could not update prices!'));
140   }
141
142   $lxdebug->leave_sub();
143 }
144
145 sub choice {
146   $lxdebug->enter_sub();
147
148   our ($j, $lastndx);
149   my ($totop100);
150
151   $form->{title} = $locale->text('Top 100 hinzufuegen');
152
153   $form->header;
154
155   push @custom_hiddens, qw(login password searchitems title bom titel revers lastsort sort ndxs_counter extras);
156   push @custom_hiddens, qw(itemstatus l_linetotal l_partnumber l_description l_onhand l_unit l_sellprice l_linetotalsellprice);
157   my @HIDDENS = (
158         +{ name => 'row',     value => $j              },
159         +{ name => 'nextsub', value => 'item_selected' },
160         +{ name => 'test',    value => 'item_selected' },
161         +{ name => 'lastndx', value => $lastndx        },
162     map(+{ name => $_,        value => $form->{$_}     }, @custom_hiddens),
163   );
164
165   my ($partnumber, $description, $unit, $sellprice, $soldtotal);
166   # if choice set data
167   if ($form->{ndx}) {
168     for (my $i = 0; $i < $form->{ndxs_counter}; $i++) {
169
170       # prepeare data
171       $partnumber  = $form->{"totop100_partnumber_$j"};
172       $description = $form->{"totop100_description_$j"};
173       $unit        = $form->{"totop100_unit_$j"};
174       $sellprice   = $form->{"totop100_sellprice_$j"};
175       $soldtotal   = $form->{"totop100_soldtotal_$j"};
176
177       # insert data into top100
178       push @{ $form->{parts} },
179         { number      => "",
180           partnumber  => "$partnumber",
181           description => "$description",
182           unit        => "$unit",
183           sellprice   => "$sellprice",
184           soldtotal   => "$soldtotal" };
185     }    #rof
186   }    #fi
187
188   $totop100 = "";
189
190   # set data for next page
191   if (($form->{ndxs_counter}) > 0) {
192     for (my $i = 1; ($i < $form->{ndxs_counter} + 1); $i++) {
193       $partnumber  = $form->{"totop100_partnumber_$i"};
194       $description = $form->{"totop100_description_$i"};
195       $unit        = $form->{"totop100_unit_$i"};
196       $sellprice   = $form->{"totop100_sellprice_$i"};
197       $soldtotal   = $form->{"totop100_soldtotal_$i"};
198
199       $totop100 .= qq|
200 <input type=hidden name=totop100_partnumber_$i value=$form->{"totop100_partnumber_$i"}>
201 <input type=hidden name=totop100_description_$i value=$form->{"totop100_description_$i"}>
202 <input type=hidden name=totop100_unit_$i value=$form->{"totop100_unit_$i"}>
203 <input type=hidden name=totop100_sellprice_$i value=$form->{"totop100_sellprice_$i"}>
204 <input type=hidden name=totop100_soldtotal_$i value=$form->{"totop100_soldtotal_$i"}>
205       |;
206     }    #rof
207   }    #fi
208
209   print $form->parse_html_template('ic/choice', +{ HIDDENS => \@HIDDENS, totop100 => $totop100 });
210
211   $lxdebug->leave_sub();
212 }    #end choice
213
214 sub list {
215   $lxdebug->enter_sub();
216
217   our ($lastndx);
218   our ($partnumber, $description, $unit, $sellprice, $soldtotal);
219
220   my @sortorders = ("", "partnumber", "description", "all");
221   my $sortorder = $sortorders[($form->{description} ? 2 : 0) + ($form->{partnumber} ? 1 : 0)];
222   IC->get_parts(\%myconfig, \%$form, $sortorder);
223
224   $form->{title} = $locale->text('Top 100 hinzufuegen');
225
226   $form->header;
227
228   print qq|
229 <body>
230   <form method=post action=ic.pl>
231     <table width=100%>
232      <tr>
233       <th class=listtop colspan=6>| . $locale->text('choice part') . qq|</th>
234      </tr>
235         <tr height="5"></tr>
236         <tr class=listheading>
237           <th>&nbsp;</th>
238           <th class=listheading>| . $locale->text('Part Number') . qq|</th>
239           <th class=listheading>| . $locale->text('Part Description') . qq|</th>
240           <th class=listheading>| . $locale->text('Unit of measure') . qq|</th>
241           <th class=listheading>| . $locale->text('Sell Price') . qq|</th>
242           <th class=listheading>| . $locale->text('soldtotal') . qq|</th>
243         </tr>|;
244
245   my $j = 0;
246   my $i = $form->{rows};
247
248   for ($j = 1; $j <= $i; $j++) {
249
250     print qq|
251         <tr class=listrow1>|;
252     if ($j == 1) {
253       print qq|
254             <td><input name=ndx class=radio type=radio value=$j checked></td>|;
255     } else {
256       print qq|
257           <td><input name=ndx class=radio type=radio value=$j></td>|;
258     }
259     print qq|
260           <td><input name="new_partnumber_$j" type=hidden value="$form->{"partnumber_$j"}">$form->{"partnumber_$j"}</td>
261           <td><input name="new_description_$j" type=hidden value="$form->{"description_$j"}">$form->{"description_$j"}</td>
262           <td><input name="new_unit_$j" type=hidden value="$form->{"unit_$j"}">$form->{"unit_$j"}</td>
263           <td><input name="new_sellprice_$j" type=hidden value="$form->{"sellprice_$j"}">$form->{"sellprice_$j"}</td>
264           <td><input name="new_soldtotal_$j" type=hidden value="$form->{"soldtotal_$j"}">$form->{"soldtotal_$j"}</td>
265         </tr>
266
267         <input name="new_id_$j" type=hidden value="$form->{"id_$j"}">|;
268   }
269
270   print qq|
271
272 </table>
273
274 <br>
275
276
277 <input type=hidden name=login value=$form->{login}>
278 <input type=hidden name=password value=$form->{password}>
279
280 <input type=hidden name=itemstatus value="$form->{itemstatus}">
281 <input type=hidden name=l_linetotal value="$form->{l_linetotal}">
282 <input type=hidden name=l_partnumber value="$form->{l_partnumber}">
283 <input type=hidden name=l_description value="$form->{l_description}">
284 <input type=hidden name=l_onhand value="$form->{l_onhand}">
285 <input type=hidden name=l_unit value="$form->{l_unit}">
286 <input type=hidden name=l_sellprice value="$form->{l_sellprice}">
287 <input type=hidden name=l_linetotalsellprice value="$form->{l_linetotalsellprice}">
288 <input type=hidden name=sort value="$form->{sort}">
289 <input type=hidden name=revers value="$form->{revers}">
290 <input type=hidden name=lastsort value="$form->{lastsort}">
291
292 <input type=hidden name=bom value="$form->{bom}">
293 <input type=hidden name=titel value="$form->{titel}">
294 <input type=hidden name=searchitems value="$form->{searchitems}">
295
296 <input type=hidden name=row value=$j>
297
298 <input type=hidden name=nextsub value=item_selected>
299
300 <input name=lastndx type=hidden value=$lastndx>
301
302 <input name=ndxs_counter type=hidden value=$form->{ndxs_counter}>|;
303
304   my $totop100 = "";
305
306   if (($form->{ndxs_counter}) > 0) {
307     for ($i = 1; ($i < $form->{ndxs_counter} + 1); $i++) {
308
309       $partnumber  = $form->{"totop100_partnumber_$i"};
310       $description = $form->{"totop100_description_$i"};
311       $unit        = $form->{"totop100_unit_$i"};
312       $sellprice   = $form->{"totop100_sellprice_$i"};
313       $soldtotal   = $form->{"totop100_soldtotal_$i"};
314
315       $totop100 .= qq|
316 <input type=hidden name=totop100_partnumber_$i value=$form->{"totop100_partnumber_$i"}>
317 <input type=hidden name=totop100_description_$i value=$form->{"totop100_description_$i"}>
318 <input type=hidden name=totop100_unit_$i value=$form->{"totop100_unit_$i"}>
319 <input type=hidden name=totop100_sellprice_$i value=$form->{"totop100_sellprice_$i"}>
320 <input type=hidden name=totop100_soldtotal_$i value=$form->{"totop100_soldtotal_$i"}>
321       |;
322     }    #rof
323   }    #fi
324
325   print $totop100;
326
327   print qq|
328 <input class=submit type=submit name=action value="|
329     . $locale->text('TOP100') . qq|">
330
331 </form>
332 </body>
333 </html>
334 |;
335   $lxdebug->leave_sub();
336 }    #end list()
337
338 sub top100 {
339   $lxdebug->enter_sub();
340
341   if ($form->{ndx}) {
342     $form->{ndxs_counter}++;
343
344     if ($form->{ndxs_counter} > 0) {
345
346       my $index = $form->{ndx};
347
348       $form->{"totop100_partnumber_$form->{ndxs_counter}"} = $form->{"new_partnumber_$index"};
349       $form->{"totop100_description_$form->{ndxs_counter}"} = $form->{"new_description_$index"};
350       $form->{"totop100_unit_$form->{ndxs_counter}"} = $form->{"new_unit_$index"};
351       $form->{"totop100_sellprice_$form->{ndxs_counter}"} = $form->{"new_sellprice_$index"};
352       $form->{"totop100_soldtotal_$form->{ndxs_counter}"} = $form->{"new_soldtotal_$index"};
353     }    #fi
354   }    #fi
355   &addtop100();
356   $lxdebug->leave_sub();
357 }    #end top100
358
359 sub addtop100 {
360   $lxdebug->enter_sub();
361
362   my ($revers, $lastsort, $callback, $option, $description, $sameitem,
363       $partnumber, $unit, $sellprice, $soldtotal, $totop100, $onhand, $align);
364   my (@column_index, %column_header, %column_data);
365   my ($totalsellprice, $totallastcost, $totallistprice, $subtotalonhand, $subtotalsellprice, $subtotallastcost, $subtotallistprice);
366
367   $form->{top100}      = "top100";
368   $form->{l_soldtotal} = "Y";
369 #  $form->{soldtotal}   = "soldtotal";
370   $form->{sort}        = "soldtotal";
371   $form->{l_qty}       = "N";
372   $form->{l_linetotal} = "";
373   $form->{revers}      = 1;
374   $form->{number}      = "position";
375   $form->{l_number}    = "Y";
376
377   $totop100 = "";
378
379   $form->{title} = $locale->text('Top 100');
380
381   $revers   = $form->{revers};
382   $lastsort = $form->{lastsort};
383
384   if (($form->{lastsort} eq "") && ($form->{sort} eq undef)) {
385     $form->{revers}   = 0;
386     $form->{lastsort} = "partnumber";
387     $form->{sort}     = "partnumber";
388   }    #fi
389
390   $callback =
391     "$form->{script}?action=top100&login=$form->{login}&password=$form->{password}&searchitems=$form->{searchitems}&itemstatus=$form->{itemstatus}&bom=$form->{bom}&l_linetotal=$form->{l_linetotal}&title="
392     . $form->escape($form->{title}, 1);
393
394   # if we have a serialnumber limit search
395   if ($form->{serialnumber} || $form->{l_serialnumber}) {
396     $form->{l_serialnumber} = "Y";
397     unless (   $form->{bought}
398             || $form->{sold}
399             || $form->{rfq}
400             || $form->{quoted}) {
401       $form->{bought} = $form->{sold} = 1;
402     }
403   }
404   IC->all_parts(\%myconfig, \%$form);
405
406   if ($form->{itemstatus} eq 'active') {
407     $option .= $locale->text('Active') . " : ";
408   }
409   if ($form->{itemstatus} eq 'obsolete') {
410     $option .= $locale->text('Obsolete') . " : ";
411   }
412   if ($form->{itemstatus} eq 'orphaned') {
413     $option .= $locale->text('Orphaned') . " : ";
414   }
415   if ($form->{itemstatus} eq 'onhand') {
416     $option .= $locale->text('On Hand') . " : ";
417     $form->{l_onhand} = "Y";
418   }
419   if ($form->{itemstatus} eq 'short') {
420     $option .= $locale->text('Short') . " : ";
421     $form->{l_onhand} = "Y";
422   }
423   if ($form->{onorder}) {
424     $form->{l_ordnumber} = "Y";
425     $callback .= "&onorder=$form->{onorder}";
426     $option   .= $locale->text('On Order') . " : ";
427   }
428   if ($form->{ordered}) {
429     $form->{l_ordnumber} = "Y";
430     $callback .= "&ordered=$form->{ordered}";
431     $option   .= $locale->text('Ordered') . " : ";
432   }
433   if ($form->{rfq}) {
434     $form->{l_quonumber} = "Y";
435     $callback .= "&rfq=$form->{rfq}";
436     $option   .= $locale->text('RFQ') . " : ";
437   }
438   if ($form->{quoted}) {
439     $form->{l_quonumber} = "Y";
440     $callback .= "&quoted=$form->{quoted}";
441     $option   .= $locale->text('Quoted') . " : ";
442   }
443   if ($form->{bought}) {
444     $form->{l_invnumber} = "Y";
445     $callback .= "&bought=$form->{bought}";
446     $option   .= $locale->text('Bought') . " : ";
447   }
448   if ($form->{sold}) {
449     $form->{l_invnumber} = "Y";
450     $callback .= "&sold=$form->{sold}";
451     $option   .= $locale->text('Sold') . " : ";
452   }
453   if (   $form->{bought}
454       || $form->{sold}
455       || $form->{onorder}
456       || $form->{ordered}
457       || $form->{rfq}
458       || $form->{quoted}) {
459
460     $form->{l_lastcost} = "";
461     $form->{l_name}     = "Y";
462     if ($form->{transdatefrom}) {
463       $callback .= "&transdatefrom=$form->{transdatefrom}";
464       $option   .= "\n<br>"
465         . $locale->text('From')
466         . "&nbsp;"
467         . $locale->date(\%myconfig, $form->{transdatefrom}, 1);
468     }
469     if ($form->{transdateto}) {
470       $callback .= "&transdateto=$form->{transdateto}";
471       $option   .= "\n<br>"
472         . $locale->text('To')
473         . "&nbsp;"
474         . $locale->date(\%myconfig, $form->{transdateto}, 1);
475     }
476   }
477
478   $option .= "<br>";
479
480   if ($form->{partnumber}) {
481     $callback .= "&partnumber=$form->{partnumber}";
482     $option   .= $locale->text('Part Number') . qq| : $form->{partnumber}<br>|;
483   }
484   if ($form->{ean}) {
485     $callback .= "&partnumber=$form->{ean}";
486     $option   .= $locale->text('EAN') . qq| : $form->{ean}<br>|;
487   }
488   if ($form->{partsgroup}) {
489     $callback .= "&partsgroup=$form->{partsgroup}";
490     $option   .= $locale->text('Group') . qq| : $form->{partsgroup}<br>|;
491   }
492   if ($form->{serialnumber}) {
493     $callback .= "&serialnumber=$form->{serialnumber}";
494     $option   .=
495       $locale->text('Serial Number') . qq| : $form->{serialnumber}<br>|;
496   }
497   if ($form->{description}) {
498     $callback .= "&description=$form->{description}";
499     $description = $form->{description};
500     $description =~ s/
501 /<br>/g;
502     $option .=
503       $locale->text('Part Description') . qq| : $form->{description}<br>|;
504   }
505   if ($form->{make}) {
506     $callback .= "&make=$form->{make}";
507     $option   .= $locale->text('Make') . qq| : $form->{make}<br>|;
508   }
509   if ($form->{model}) {
510     $callback .= "&model=$form->{model}";
511     $option   .= $locale->text('Model') . qq| : $form->{model}<br>|;
512   }
513   if ($form->{drawing}) {
514     $callback .= "&drawing=$form->{drawing}";
515     $option   .= $locale->text('Drawing') . qq| : $form->{drawing}<br>|;
516   }
517   if ($form->{microfiche}) {
518     $callback .= "&microfiche=$form->{microfiche}";
519     $option   .= $locale->text('Microfiche') . qq| : $form->{microfiche}<br>|;
520   }
521   if ($form->{l_soldtotal}) {
522     $callback .= "&soldtotal=$form->{soldtotal}";
523     $option   .= $locale->text('soldtotal') . qq| : $form->{soldtotal}<br>|;
524   }
525
526   my @columns = $form->sort_columns(
527     qw(number partnumber ean description partsgroup bin onhand rop unit listprice linetotallistprice sellprice linetotalsellprice lastcost linetotallastcost priceupdate weight image drawing microfiche invnumber ordnumber quonumber name serialnumber soldtotal)
528   );
529
530   if ($form->{l_linetotal}) {
531     $form->{l_onhand} = "Y";
532     $form->{l_linetotalsellprice} = "Y" if $form->{l_sellprice};
533     if ($form->{l_lastcost}) {
534       $form->{l_linetotallastcost} = "Y";
535       if (($form->{searchitems} eq 'assembly') && !$form->{bom}) {
536         $form->{l_linetotallastcost} = "";
537       }
538     }
539     $form->{l_linetotallistprice} = "Y" if $form->{l_listprice};
540   }
541
542   if ($form->{searchitems} eq 'service') {
543
544     # remove bin, weight and rop from list
545     map { $form->{"l_$_"} = "" } qw(bin weight rop);
546
547     $form->{l_onhand} = "";
548
549     # qty is irrelevant unless bought or sold
550     if (   $form->{bought}
551         || $form->{sold}
552         || $form->{onorder}
553         || $form->{ordered}
554         || $form->{rfq}
555         || $form->{quoted}) {
556       $form->{l_onhand} = "Y";
557     } else {
558       $form->{l_linetotalsellprice} = "";
559       $form->{l_linetotallastcost}  = "";
560     }
561   }
562
563   $form->{l_lastcost} = ""
564     if ($form->{searchitems} eq 'assembly' && !$form->{bom});
565
566   foreach my $item (@columns) {
567     if ($form->{"l_$item"} eq "Y") {
568       push @column_index, $item;
569
570       # add column to callback
571       $callback .= "&l_$item=Y";
572     }
573   }
574
575   if ($form->{l_subtotal} eq 'Y') {
576     $callback .= "&l_subtotal=Y";
577   }
578
579   $column_header{number} =
580     qq|<th class=listheading nowrap>| . $locale->text('number') . qq|</th>|;
581   $column_header{partnumber} =
582     qq|<th nowrap><a class=listheading href=$callback&sort=partnumber&revers=$form->{revers}&lastsort=$form->{lastsort}>|
583     . $locale->text('Part Number')
584     . qq|</a></th>|;
585   $column_header{description} =
586     qq|<th nowrap><a class=listheading href=$callback&sort=description&revers=$form->{revers}&lastsort=$form->{lastsort}>|
587     . $locale->text('Part Description')
588     . qq|</a></th>|;
589   $column_header{partsgroup} =
590       qq|<th nowrap><a class=listheading href=$callback&sort=partsgroup>|
591     . $locale->text('Group')
592     . qq|</a></th>|;
593   $column_header{bin} =
594       qq|<th><a class=listheading href=$callback&sort=bin>|
595     . $locale->text('Bin')
596     . qq|</a></th>|;
597   $column_header{priceupdate} =
598       qq|<th nowrap><a class=listheading href=$callback&sort=priceupdate>|
599     . $locale->text('Updated')
600     . qq|</a></th>|;
601   $column_header{onhand} =
602     qq|<th nowrap><a  class=listheading href=$callback&sort=onhand&revers=$form->{revers}&lastsort=$form->{lastsort}>|
603     . $locale->text('Qty')
604     . qq|</th>|;
605   $column_header{unit} =
606     qq|<th class=listheading nowrap>| . $locale->text('Unit') . qq|</th>|;
607   $column_header{listprice} =
608       qq|<th class=listheading nowrap>|
609     . $locale->text('List Price')
610     . qq|</th>|;
611   $column_header{lastcost} =
612     qq|<th class=listheading nowrap>| . $locale->text('Last Cost') . qq|</th>|;
613   $column_header{rop} =
614     qq|<th class=listheading nowrap>| . $locale->text('ROP') . qq|</th>|;
615   $column_header{weight} =
616     qq|<th class=listheading nowrap>| . $locale->text('Weight') . qq|</th>|;
617
618   $column_header{invnumber} =
619       qq|<th nowrap><a class=listheading href=$callback&sort=invnumber>|
620     . $locale->text('Invoice Number')
621     . qq|</a></th>|;
622   $column_header{ordnumber} =
623       qq|<th nowrap><a class=listheading href=$callback&sort=ordnumber>|
624     . $locale->text('Order Number')
625     . qq|</a></th>|;
626   $column_header{quonumber} =
627       qq|<th nowrap><a class=listheading href=$callback&sort=quonumber>|
628     . $locale->text('Quotation')
629     . qq|</a></th>|;
630
631   $column_header{name} =
632       qq|<th nowrap><a class=listheading href=$callback&sort=name>|
633     . $locale->text('Name')
634     . qq|</a></th>|;
635
636   $column_header{sellprice} =
637       qq|<th class=listheading nowrap>|
638     . $locale->text('Sell Price')
639     . qq|</th>|;
640   $column_header{linetotalsellprice} =
641     qq|<th class=listheading nowrap>| . $locale->text('Extended') . qq|</th>|;
642   $column_header{linetotallastcost} =
643     qq|<th class=listheading nowrap>| . $locale->text('Extended') . qq|</th>|;
644   $column_header{linetotallistprice} =
645     qq|<th class=listheading nowrap>| . $locale->text('Extended') . qq|</th>|;
646
647   $column_header{image} =
648     qq|<th class=listheading nowrap>| . $locale->text('Image') . qq|</a></th>|;
649   $column_header{drawing} =
650       qq|<th nowrap><a class=listheading href=$callback&sort=drawing>|
651     . $locale->text('Drawing')
652     . qq|</a></th>|;
653   $column_header{microfiche} =
654       qq|<th nowrap><a class=listheading href=$callback&sort=microfiche>|
655     . $locale->text('Microfiche')
656     . qq|</a></th>|;
657
658   $column_header{serialnumber} =
659       qq|<th nowrap><a class=listheading href=$callback&sort=serialnumber>|
660     . $locale->text('Serial Number')
661     . qq|</a></th>|;
662   $column_header{soldtotal} =
663     qq|<th nowrap><a class=listheading href=$callback&sort=soldtotal&revers=$form->{revers}&lastsort=$form->{lastsort}>|
664     . $locale->text('soldtotal')
665     . qq|</a></th>|;
666
667   $form->header;
668   my $colspan = $#column_index + 1;
669
670   print qq|
671 <body>
672
673 <table width=100%>
674   <tr>
675     <th class=listtop colspan=$colspan>$form->{title}</th>
676   </tr>
677   <tr height="5"></tr>
678
679   <tr><td colspan=$colspan>$option</td></tr>
680
681   <tr class=listheading>
682 |;
683
684   map { print "\n$column_header{$_}" } @column_index;
685
686   print qq|
687   </tr>
688   |;
689
690   # add order to callback
691   $form->{callback} = $callback .= "&sort=$form->{sort}";
692
693   # escape callback for href
694   $callback = $form->escape($callback);
695
696   if (@{ $form->{parts} }) {
697     $sameitem = $form->{parts}->[0]->{ $form->{sort} };
698   }
699
700   # insert numbers for top100
701   my $j = 0;
702   foreach my $ref (@{ $form->{parts} }) {
703     $j++;
704     $ref->{number} = $j;
705   }
706
707   # if avaible -> insert choice here
708   if (($form->{ndxs_counter}) > 0) {
709     for (my $i = 1; ($i < $form->{ndxs_counter} + 1); $i++) {
710       $partnumber  = $form->{"totop100_partnumber_$i"};
711       $description = $form->{"totop100_description_$i"};
712       $unit        = $form->{"totop100_unit_$i"};
713       $sellprice   = $form->{"totop100_sellprice_$i"};
714       $soldtotal   = $form->{"totop100_soldtotal_$i"};
715
716       $totop100 .= qq|
717 <input type=hidden name=totop100_partnumber_$i value=$form->{"totop100_partnumber_$i"}>
718 <input type=hidden name=totop100_description_$i value=$form->{"totop100_description_$i"}>
719 <input type=hidden name=totop100_unit_$i value=$form->{"totop100_unit_$i"}>
720 <input type=hidden name=totop100_sellprice_$i value=$form->{"totop100_sellprice_$i"}>
721 <input type=hidden name=totop100_soldtotal_$i value=$form->{"totop100_soldtotal_$i"}>
722       |;
723
724       # insert into list
725       push @{ $form->{parts} },
726         { number      => "",
727           partnumber  => "$partnumber",
728           description => "$description",
729           unit        => "$unit",
730           sellprice   => "$sellprice",
731           soldtotal   => "$soldtotal" };
732     }    #rof
733   }    #fi
734        # build data for columns
735   foreach my $ref (@{ $form->{parts} }) {
736     my $i = 0;
737
738     if ($form->{l_subtotal} eq 'Y' && !$ref->{assemblyitem}) {
739       if ($sameitem ne $ref->{ $form->{sort} }) {
740         &parts_subtotal;
741         $sameitem = $ref->{ $form->{sort} };
742       }
743     }
744
745     $ref->{exchangerate} = 1 unless $ref->{exchangerate};
746     $ref->{sellprice} *= $ref->{exchangerate};
747     $ref->{listprice} *= $ref->{exchangerate};
748     $ref->{lastcost}  *= $ref->{exchangerate};
749
750     # use this for assemblies
751     $onhand = $ref->{onhand};
752
753     $align = "left";
754     if ($ref->{assemblyitem}) {
755       $align = "right";
756       $onhand = 0 if ($form->{sold});
757     }
758
759     $ref->{description} =~ s/
760 /<br>/g;
761
762     $column_data{number} =
763         "<td align=right>"
764       . $form->format_amount(\%myconfig, $ref->{number})
765       . "</td>";
766     $column_data{partnumber} =
767       "<td align=$align>$ref->{partnumber}&nbsp;</a></td>";
768     $column_data{description} = "<td>$ref->{description}&nbsp;</td>";
769     $column_data{partsgroup}  = "<td>$ref->{partsgroup}&nbsp;</td>";
770
771     $column_data{onhand} =
772         "<td align=right>"
773       . $form->format_amount(\%myconfig, $ref->{onhand})
774       . "</td>";
775     $column_data{sellprice} =
776         "<td align=right>"
777       . $form->format_amount(\%myconfig, $ref->{sellprice})
778       . "</td>";
779     $column_data{listprice} =
780         "<td align=right>"
781       . $form->format_amount(\%myconfig, $ref->{listprice})
782       . "</td>";
783     $column_data{lastcost} =
784         "<td align=right>"
785       . $form->format_amount(\%myconfig, $ref->{lastcost})
786       . "</td>";
787
788     $column_data{linetotalsellprice} = "<td align=right>"
789       . $form->format_amount(\%myconfig, $ref->{onhand} * $ref->{sellprice}, 2)
790       . "</td>";
791     $column_data{linetotallastcost} = "<td align=right>"
792       . $form->format_amount(\%myconfig, $ref->{onhand} * $ref->{lastcost}, 2)
793       . "</td>";
794     $column_data{linetotallistprice} = "<td align=right>"
795       . $form->format_amount(\%myconfig, $ref->{onhand} * $ref->{listprice}, 2)
796       . "</td>";
797
798     if (!$ref->{assemblyitem}) {
799       $totalsellprice += $onhand * $ref->{sellprice};
800       $totallastcost  += $onhand * $ref->{lastcost};
801       $totallistprice += $onhand * $ref->{listprice};
802
803       $subtotalonhand    += $onhand;
804       $subtotalsellprice += $onhand * $ref->{sellprice};
805       $subtotallastcost  += $onhand * $ref->{lastcost};
806       $subtotallistprice += $onhand * $ref->{listprice};
807     }
808
809     $column_data{rop} =
810       "<td align=right>"
811       . $form->format_amount(\%myconfig, $ref->{rop}) . "</td>";
812     $column_data{weight} =
813         "<td align=right>"
814       . $form->format_amount(\%myconfig, $ref->{weight})
815       . "</td>";
816     $column_data{unit}        = "<td>$ref->{unit}&nbsp;</td>";
817     $column_data{bin}         = "<td>$ref->{bin}&nbsp;</td>";
818     $column_data{priceupdate} = "<td>$ref->{priceupdate}&nbsp;</td>";
819
820     $column_data{invnumber} =
821       ($ref->{module} ne 'oe')
822       ? "<td><a href=$ref->{module}.pl?action=edit&type=invoice&id=$ref->{trans_id}&login=$form->{login}&password=$form->{password}&callback=$callback>$ref->{invnumber}</a></td>"
823       : "<td>$ref->{invnumber}</td>";
824     $column_data{ordnumber} =
825       ($ref->{module} eq 'oe')
826       ? "<td><a href=$ref->{module}.pl?action=edit&type=$ref->{type}&id=$ref->{trans_id}&login=$form->{login}&password=$form->{password}&callback=$callback>$ref->{ordnumber}</a></td>"
827       : "<td>$ref->{ordnumber}</td>";
828     $column_data{quonumber} =
829       ($ref->{module} eq 'oe' && !$ref->{ordnumber})
830       ? "<td><a href=$ref->{module}.pl?action=edit&type=$ref->{type}&id=$ref->{trans_id}&login=$form->{login}&password=$form->{password}&callback=$callback>$ref->{quonumber}</a></td>"
831       : "<td>$ref->{quonumber}</td>";
832
833     $column_data{name} = "<td>$ref->{name}</td>";
834
835     $column_data{image} =
836       ($ref->{image})
837       ? "<td><a href=$ref->{image}><img src=$ref->{image} height=32 border=0></a></td>"
838       : "<td>&nbsp;</td>";
839     $column_data{drawing} =
840       ($ref->{drawing})
841       ? "<td><a href=$ref->{drawing}>$ref->{drawing}</a></td>"
842       : "<td>&nbsp;</td>";
843     $column_data{microfiche} =
844       ($ref->{microfiche})
845       ? "<td><a href=$ref->{microfiche}>$ref->{microfiche}</a></td>"
846       : "<td>&nbsp;</td>";
847
848     $column_data{serialnumber} = "<td>$ref->{serialnumber}</td>";
849
850     $column_data{soldtotal} = "<td  align=right>$ref->{soldtotal}</td>";
851
852     $i++;
853     $i %= 2;
854     print "<tr class=listrow$i>";
855
856     map { print "\n$column_data{$_}" } @column_index;
857
858     print qq|
859     </tr>
860 |;
861   }
862
863   if ($form->{l_subtotal} eq 'Y') {
864     &parts_subtotal;
865   }    #fi
866
867   if ($form->{"l_linetotal"}) {
868     map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
869     $column_data{linetotalsellprice} =
870         "<th class=listtotal align=right>"
871       . $form->format_amount(\%myconfig, $totalsellprice, 2)
872       . "</th>";
873     $column_data{linetotallastcost} =
874         "<th class=listtotal align=right>"
875       . $form->format_amount(\%myconfig, $totallastcost, 2)
876       . "</th>";
877     $column_data{linetotallistprice} =
878         "<th class=listtotal align=right>"
879       . $form->format_amount(\%myconfig, $totallistprice, 2)
880       . "</th>";
881
882     print "<tr class=listtotal>";
883
884     map { print "\n$column_data{$_}" } @column_index;
885
886     print qq|</tr>
887     |;
888   }
889
890   print qq|
891   <tr><td colspan=$colspan><hr size=3 noshade></td></tr>
892 </table>
893
894 |;
895
896   print qq|
897
898 <br>
899
900 <form method=post action=$form->{script}>
901
902 <input type=hidden name=login value=$form->{login}>
903 <input type=hidden name=password value=$form->{password}>
904
905 <input type=hidden name=itemstatus value="$form->{itemstatus}">
906 <input type=hidden name=l_linetotal value="$form->{l_linetotal}">
907 <input type=hidden name=l_partnumber value="$form->{l_partnumber}">
908 <input type=hidden name=l_description value="$form->{l_description}">
909 <input type=hidden name=l_onhand value="$form->{l_onhand}">
910 <input type=hidden name=l_unit value="$form->{l_unit}">
911 <input type=hidden name=l_sellprice value="$form->{l_sellprice}">
912 <input type=hidden name=l_linetotalsellprice value="$form->{l_linetotalsellprice}">
913 <input type=hidden name=sort value="$form->{sort}">
914 <input type=hidden name=revers value="$form->{revers}">
915 <input type=hidden name=lastsort value="$form->{lastsort}">
916 <input type=hidden name=parts value="$form->{parts}">
917
918 <input type=hidden name=bom value="$form->{bom}">
919 <input type=hidden name=titel value="$form->{titel}">
920 <input type=hidden name=searchitems value="$form->{searchitems}">|;
921
922   print $totop100;
923
924   print qq|
925     <input type=hidden name=ndxs_counter value="$form->{ndxs_counter}">
926
927     <input class=submit type=submit name=action value="|
928     . $locale->text('choice') . qq|">
929
930   </form>
931
932 </body>
933 </html>
934 |;
935
936   $lxdebug->leave_sub();
937 }    # end addtop100
938
939 #
940 # Report for Wares.
941 # Warning, deep magic ahead.
942 # This function parses the requested details, sanity checks them, and converts them into a format thats usable for IC->all_parts
943 #
944 # flags coming from the form:
945 # hardcoded:
946 #  searchitems=part revers=0 lastsort=''
947 #
948 # filter:
949 # partnumber ean description partsgroup serialnumber make model drawing microfiche
950 # transdatefrom transdateto
951 #
952 # radio:
953 #  itemstatus = active | onhand | short | obsolete | orphaned
954 #  action     = continue | top100
955 #
956 # checkboxes:
957 #  bought sold onorder ordered rfq quoted
958 #  l_partnumber l_description l_serialnumber l_unit l_listprice l_sellprice l_lastcost
959 #  l_linetotal l_priceupdate l_bin l_rop l_weight l_image l_drawing l_microfiche
960 #  l_partsgroup l_subtotal l_soldtotal l_deliverydate
961 #
962 # hiddens: 
963 #  nextsub login password revers lastsort sort ndxs_counter
964 #
965 sub generate_report {
966   $lxdebug->enter_sub();
967
968   my ($revers, $lastsort, $description);
969
970   $form->{title} = (ucfirst $form->{searchitems}) . "s";
971   $form->{title} = $locale->text($form->{title});
972
973   my $revers     = $form->{revers};
974   my $lastsort   = $form->{lastsort};
975
976   # sorting and direction of sorting
977   # ToDO: change this to the simpler field+direction method
978   if (($form->{lastsort} eq "") && ($form->{sort} eq undef)) {
979     $form->{revers}   = 0;
980     $form->{lastsort} = "partnumber";
981     $form->{sort}     = "partnumber";
982   } else {
983     if ($form->{lastsort} eq $form->{sort}) {
984       $form->{revers} = 1 - $form->{revers};
985     } else {
986       $form->{revers} = 0;
987       $form->{lastsort} = $form->{sort};
988     }    #fi
989   }    #fi
990
991   # special case if we have a serialnumber limit search
992   # serialnumbers are only given in invoices and orders, 
993   # so they can only pop up in bought, sold, rfq, and quoted stuff
994   $form->{no_sn_joins} = 'Y' if (   !$form->{bought} && !$form->{sold} 
995                                  && !$form->{rfq}    && !$form->{quoted} 
996                                  && ($form->{l_serialnumber} || $form->{serialnumber}));
997
998   # special case for any checkbox of bought | sold | onorder | ordered | rfq | quoted.
999   # if any of these are ticked the behavior changes slightly for lastcost
1000   # since all those are aggregation checks for the legder tables this is an internal switch
1001   # refered to as ledgerchecks
1002   $form->{ledgerchecks} = 'Y' if (   $form->{bought} || $form->{sold} || $form->{onorder}
1003                                   || $form->{ordered} || $form->{rfq} || $form->{quoted});
1004
1005   # if something should be activated if something else is active, enter it here
1006   my %dependencies = (
1007     onhand       => [ qw(l_onhand) ],
1008     short        => [ qw(l_onhand) ],
1009     onorder      => [ qw(l_ordnumber) ],
1010     ordered      => [ qw(l_ordnumber) ],
1011     rfq          => [ qw(l_quonumber) ],
1012     quoted       => [ qw(l_quonumber) ],
1013     bought       => [ qw(l_invnumber) ],
1014     sold         => [ qw(l_invnumber) ],
1015     ledgerchecks => [ qw(l_name) ],
1016     serialnumber => [ qw(l_serialnumber) ],
1017     no_sn_joins  => [ qw(bought sold) ],
1018   );
1019
1020   # these strings get displayed at the top of the results to indicate the user which switches were used
1021   my %optiontexts = (
1022     active        => $locale->text('Active'),
1023     obsolete      => $locale->text('Obsolete'),
1024     orphaned      => $locale->text('Orphaned'),
1025     onhand        => $locale->text('On Hand'),
1026     short         => $locale->text('Short'),
1027     onorder       => $locale->text('On Order'),
1028     ordered       => $locale->text('Ordered'),
1029     rfq           => $locale->text('RFQ'),
1030     quoted        => $locale->text('Quoted'),
1031     bought        => $locale->text('Bought'),
1032     sold          => $locale->text('Sold'),
1033     transdatefrom => $locale->text('From')       . " " . $locale->date(\%myconfig, $form->{transdatefrom}, 1),
1034     transdateto   => $locale->text('To (time)')  . " " . $locale->date(\%myconfig, $form->{transdateto}, 1),
1035     partnumber    => $locale->text('Part Number')      . ": '$form->{partnumber}'",
1036     partsgroup    => $locale->text('Group')            . ": '$form->{partsgroup}'",
1037     serialnumber  => $locale->text('Serial Number')    . ": '$form->{serialnumber}'",
1038     description   => $locale->text('Part Description') . ": '$form->{description}'",
1039     make          => $locale->text('Make')             . ": '$form->{make}'",
1040     model         => $locale->text('Model')            . ": '$form->{model}'",
1041     drawing       => $locale->text('Drawing')          . ": '$form->{drawing}'",
1042     microfiche    => $locale->text('Microfiche')       . ": '$form->{microfiche}'",
1043     l_soldtotal   => $locale->text('soldtotal'),
1044   );
1045
1046   my @itemstatus_keys = qw(active obsolete orphaned onhand short);
1047   my @callback_keys   = qw(onorder ordered rfq quoted bought sold partnumber partsgroup serialnumber description make model
1048                            drawing microfiche l_soldtotal l_deliverydate transdatefrom transdateto ean);
1049
1050   # calculate dependencies
1051   for (@itemstatus_keys, @callback_keys) {
1052     next if ($form->{itemstatus} ne $_ && !$form->{$_});
1053     map { $form->{$_} = 'Y' } @{ $dependencies{$_} } if $dependencies{$_};
1054   }
1055
1056   # generate callback and optionstrings
1057   my @options;
1058   for my  $key (@itemstatus_keys, @callback_keys) { 
1059     next if ($form->{itemstatus} ne $key && !$form->{$key});
1060     push @options, $optiontexts{$key};
1061   }
1062
1063   # special case for lastcost
1064   $form->{l_lastcost} = "" if $form->{ledgerchecks};
1065
1066   if ($form->{description}) {
1067     $description = $form->{description};
1068     $description =~ s/\n/<br>/g;
1069   }
1070
1071   if ($form->{l_linetotal}) {
1072     $form->{l_onhand} = "Y";
1073     $form->{l_linetotalsellprice} = "Y" if $form->{l_sellprice};
1074     if ($form->{l_lastcost}) {
1075       $form->{l_linetotallastcost} = "Y";
1076       if (($form->{searchitems} eq 'assembly') && !$form->{bom}) {
1077         $form->{l_linetotallastcost} = "";
1078       }
1079     }
1080     $form->{l_linetotallistprice} = "Y" if $form->{l_listprice};
1081   }
1082
1083   if ($form->{searchitems} eq 'service') {
1084
1085     # remove bin, weight and rop from list
1086     map { $form->{"l_$_"} = "" } qw(bin weight rop);
1087
1088     $form->{l_onhand} = "";
1089
1090     # qty is irrelevant unless bought or sold
1091     if (   $form->{bought}
1092         || $form->{sold}
1093         || $form->{onorder}
1094         || $form->{ordered}
1095         || $form->{rfq}
1096         || $form->{quoted}) {
1097       $form->{l_onhand} = "Y";
1098     } else {
1099       $form->{l_linetotalsellprice} = "";
1100       $form->{l_linetotallastcost}  = "";
1101     }
1102   }
1103
1104   $form->{l_lastcost} = "" if ($form->{searchitems} eq 'assembly' && !$form->{bom});
1105
1106   IC->all_parts(\%myconfig, \%$form);
1107
1108   my @columns =
1109     qw(partnumber description partsgroup bin onhand rop unit listprice linetotallistprice sellprice linetotalsellprice lastcost linetotallastcost
1110        priceupdate weight image drawing microfiche invnumber ordnumber quonumber name serialnumber soldtotal deliverydate);
1111
1112   my %column_defs = (
1113     'bin'                => { 'text' => $locale->text('Bin'), },
1114     'deliverydate'       => { 'text' => $locale->text('deliverydate'), },
1115     'description'        => { 'text' => $locale->text('Part Description'), },
1116     'drawing'            => { 'text' => $locale->text('Drawing'), },
1117     'image'              => { 'text' => $locale->text('Image'), },
1118     'invnumber'          => { 'text' => $locale->text('Invoice Number'), },
1119     'lastcost'           => { 'text' => $locale->text('Last Cost'), },
1120     'linetotallastcost'  => { 'text' => $locale->text('Extended'), },
1121     'linetotallistprice' => { 'text' => $locale->text('Extended'), },
1122     'linetotalsellprice' => { 'text' => $locale->text('Extended'), },
1123     'listprice'          => { 'text' => $locale->text('List Price'), },
1124     'microfiche'         => { 'text' => $locale->text('Microfiche'), },
1125     'name'               => { 'text' => $locale->text('Name'), },
1126     'onhand'             => { 'text' => $locale->text('Qty'), },
1127     'ordnumber'          => { 'text' => $locale->text('Order Number'), },
1128     'partnumber'         => { 'text' => $locale->text('Part Number'), },
1129     'partsgroup'         => { 'text' => $locale->text('Group'), },
1130     'priceupdate'        => { 'text' => $locale->text('Updated'), },
1131     'quonumber'          => { 'text' => $locale->text('Quotation'), },
1132     'rop'                => { 'text' => $locale->text('ROP'), },
1133     'sellprice'          => { 'text' => $locale->text('Sell Price'), },
1134     'serialnumber'       => { 'text' => $locale->text('Serial Number'), },
1135     'soldtotal'          => { 'text' => $locale->text('soldtotal'), },
1136     'unit'               => { 'text' => $locale->text('Unit'), },
1137     'weight'             => { 'text' => $locale->text('Weight'), },
1138   );
1139
1140   map { $column_defs{$_}->{visible} = $form->{"l_$_"} ? 1 : 0 } @columns;
1141   map { $column_defs{$_}->{align}   = 'right' } qw(onhand sellprice listprice lastcost linetotalsellprice linetotallastcost linetotallistprice rop weight soldtotal);
1142
1143   my @hidden_variables = (qw(l_subtotal l_linetotal searchitems itemstatus bom), @itemstatus_keys, @callback_keys, map { "l_$_" } @columns);
1144   my $callback         = build_std_url('action=generate_report', grep { $form->{$_} } @hidden_variables);
1145
1146   my @sort_full        = qw(partnumber description onhand soldtotal deliverydate);
1147   my @sort_no_revers   = qw(partsgroup bin priceupdate invnumber ordnumber quonumber name image drawing serialnumber);
1148
1149   foreach my $col (@sort_full) {
1150     $column_defs{$col}->{link} = join '&', $callback, "sort=$col", map { "$_=" . E($form->{$_}) } qw(revers lastsort);
1151   }
1152   map { $column_defs{$_}->{link} = "${callback}&sort=$_" } @sort_no_revers;
1153
1154   # add order to callback
1155   $form->{callback} = join '&', ($callback, map { "${_}=" . E($form->{$_}) } qw(sort revers));
1156
1157   my $report = SL::ReportGenerator->new(\%myconfig, $form);
1158
1159   my %attachment_basenames = (
1160     'part'     => $locale->text('part_list'),
1161     'service'  => $locale->text('service_list'),
1162     'assembly' => $locale->text('assembly_list'),
1163   );
1164
1165   $report->set_options('top_info_text'         => $locale->text('Options') . ': ' . join(', ', grep $_, @options),
1166                        'raw_bottom_info_text'  => $form->parse_html_template('ic/generate_report_bottom'),
1167                        'output_format'         => 'HTML',
1168                        'title'                 => $form->{title},
1169                        'attachment_basename'   => $attachment_basenames{$form->{searchitems}} . strftime('_%Y%m%d', localtime time),
1170   );
1171   $report->set_options_from_form();
1172
1173   $report->set_columns(%column_defs);
1174   $report->set_column_order(@columns);
1175
1176   $report->set_export_options('generate_report', @hidden_variables, qw(sort revers));
1177
1178   $report->set_sort_indicator($form->{sort}, $form->{revers} ? 0 : 1);
1179
1180   my @subtotal_columns = qw(sellprice listprice lastcost);
1181   my %subtotals = map { $_ => 0 } ('onhand', @subtotal_columns);
1182   my %totals    = map { $_ => 0 } @subtotal_columns;
1183   my $idx       = 0;
1184   my $same_item = $form->{parts}[0]{ $form->{sort} } if (scalar @{ $form->{parts} });
1185
1186   # postprocess parts
1187   foreach my $ref (@{ $form->{parts} }) {
1188
1189     # fresh row, for inserting later
1190     my $row = { map { $_ => { 'data' => $ref->{$_} } } @columns };
1191
1192     $ref->{exchangerate} ||= 1;
1193     $ref->{price_factor} ||= 1;
1194     $ref->{sellprice}     *= $ref->{exchangerate} / $ref->{price_factor};
1195     $ref->{listprice}     *= $ref->{exchangerate} / $ref->{price_factor};
1196     $ref->{lastcost}      *= $ref->{exchangerate} / $ref->{price_factor};
1197
1198     # use this for assemblies
1199     my $onhand = $ref->{onhand};
1200
1201     if ($ref->{assemblyitem}) {
1202       $row->{partnumber}{align}   = 'right';
1203       $row->{onhand}{data}        = 0;
1204       $onhand                     = 0 if ($form->{sold});
1205     }
1206
1207     my $edit_link               = build_std_url('action=edit', 'id=' . E($ref->{id}), 'callback');
1208     $row->{partnumber}->{link}  = $edit_link;
1209     $row->{description}->{link} = $edit_link;
1210
1211     foreach (qw(sellprice listprice lastcost)) {
1212       $row->{$_}{data}            = $form->format_amount(\%myconfig, $ref->{$_}, -2);
1213       $row->{"linetotal$_"}{data} = $form->format_amount(\%myconfig, $ref->{onhand} * $ref->{$_}, 2);
1214     }
1215
1216     map { $row->{$_}{data} = $form->format_amount(\%myconfig, $ref->{$_}); } qw(onhand rop weight soldtotal);
1217
1218     if (!$ref->{assemblyitem}) {
1219       foreach my $col (@subtotal_columns) {
1220         $totals{$col}    += $onhand * $ref->{$col};
1221         $subtotals{$col} += $onhand * $ref->{$col};
1222       }
1223
1224       $subtotals{onhand} += $onhand;
1225     }
1226
1227     # set module stuff
1228     if ($ref->{module} eq 'oe') {
1229       my $edit_oe_link = build_std_url("script=oe.pl", 'action=edit', 'type=' . E($ref->{type}), 'id=' . E($ref->{trans_id}), 'callback');
1230       $row->{ordnumber}{link} = $edit_oe_link;
1231       $row->{quonumber}{link} = $edit_oe_link if (!$ref->{ordnumber});
1232
1233     } else {
1234       $row->{invnumber}{link} = build_std_url("script=$ref->{module}.pl", 'action=edit', 'type=invoice', 'id=' . E($ref->{trans_id}), 'callback');
1235     }
1236
1237     # set properties of images
1238     if ($ref->{image} && (lc $report->{options}->{output_format} eq 'html')) {
1239       $row->{image}{data}     = '';
1240       $row->{image}{raw_data} = '<a href="' . H($ref->{image}) . '"><img src="' . H($ref->{image}) . '" height="32" border="0"></a>';
1241     }
1242     map { $row->{$_}{link} = $ref->{$_} } qw(drawing microfiche);
1243
1244     $report->add_data($row);
1245
1246     my $next_ref = $form->{parts}[$idx + 1];
1247
1248     # insert subtotal rows
1249     if (($form->{l_subtotal} eq 'Y') &&
1250         (!$next_ref ||
1251          (!$next_ref->{assemblyitem} && ($same_item ne $next_ref->{ $form->{sort} })))) {
1252       my $row = { map { $_ => { 'class' => 'listsubtotal', } } @columns };
1253
1254       if (($form->{searchitems} ne 'assembly') || !$form->{bom}) {
1255         $row->{onhand}->{data} = $form->format_amount(\%myconfig, $subtotals{onhand});
1256       }
1257
1258       map { $row->{"linetotal$_"}->{data} = $form->format_amount(\%myconfig, $subtotals{$_}, 2) } @subtotal_columns;
1259       map { $subtotals{$_} = 0 } ('onhand', @subtotal_columns);
1260
1261       $report->add_data($row);
1262
1263       $same_item = $next_ref->{ $form->{sort} };
1264     }
1265
1266     $idx++;
1267   }
1268
1269   if ($form->{"l_linetotal"}) {
1270     my $row = { map { $_ => { 'class' => 'listtotal', } } @columns };
1271
1272     map { $row->{"linetotal$_"}->{data} = $form->format_amount(\%myconfig, $totals{$_}, 2) } @subtotal_columns;
1273
1274     $report->add_separator();
1275     $report->add_data($row);
1276   }
1277
1278   $report->generate_with_headers();
1279
1280   $lxdebug->leave_sub();
1281 }    #end generate_report
1282
1283 sub parts_subtotal {
1284   $lxdebug->enter_sub();
1285   
1286   # imports
1287   our (%column_data, @column_index);
1288   our ($subtotalonhand, $totalsellprice, $totallastcost, $totallistprice, $subtotalsellprice, $subtotallastcost, $subtotallistprice);
1289
1290   map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
1291   $subtotalonhand = 0 if ($form->{searchitems} eq 'assembly' && $form->{bom});
1292
1293   $column_data{onhand} =
1294       "<th class=listsubtotal align=right>"
1295     . $form->format_amount(\%myconfig, $subtotalonhand)
1296     . "</th>";
1297
1298   $column_data{linetotalsellprice} =
1299       "<th class=listsubtotal align=right>"
1300     . $form->format_amount(\%myconfig, $subtotalsellprice, 2)
1301     . "</th>";
1302   $column_data{linetotallistprice} =
1303       "<th class=listsubtotal align=right>"
1304     . $form->format_amount(\%myconfig, $subtotallistprice, 2)
1305     . "</th>";
1306   $column_data{linetotallastcost} =
1307       "<th class=listsubtotal align=right>"
1308     . $form->format_amount(\%myconfig, $subtotallastcost, 2)
1309     . "</th>";
1310
1311   $subtotalonhand    = 0;
1312   $subtotalsellprice = 0;
1313   $subtotallistprice = 0;
1314   $subtotallastcost  = 0;
1315
1316   print "<tr class=listsubtotal>";
1317
1318   map { print "\n$column_data{$_}" } @column_index;
1319
1320   print qq|
1321   </tr>
1322 |;
1323
1324   $lxdebug->leave_sub();
1325 }
1326
1327 sub edit {
1328   $lxdebug->enter_sub();
1329   # show history button
1330   $form->{javascript} = qq|<script type="text/javascript" src="js/show_history.js"></script>|;
1331   #/show hhistory button
1332   IC->get_part(\%myconfig, \%$form);
1333
1334   $form->{"original_partnumber"} = $form->{"partnumber"};
1335
1336   $form->{title} = $locale->text('Edit ' . ucfirst $form->{item});
1337
1338   &link_part;
1339   &display_form;
1340
1341   $lxdebug->leave_sub();
1342 }
1343
1344 sub link_part {
1345   $lxdebug->enter_sub();
1346
1347   IC->create_links("IC", \%myconfig, \%$form);
1348
1349   # currencies
1350   map({ $form->{selectcurrency} .= "<option>$_\n" }
1351       split(/:/, $form->{currencies}));
1352
1353   # parts and assemblies have the same links
1354   my $item = $form->{item};
1355   if ($form->{item} eq 'assembly') {
1356     $item = 'part';
1357   }
1358
1359   # build the popup menus
1360   $form->{taxaccounts} = "";
1361   foreach my $key (keys %{ $form->{IC_links} }) {
1362     foreach my $ref (@{ $form->{IC_links}{$key} }) {
1363
1364       # if this is a tax field
1365       if ($key =~ /IC_tax/) {
1366         if ($key =~ /\Q$item\E/) {
1367           $form->{taxaccounts} .= "$ref->{accno} ";
1368           $form->{"IC_tax_$ref->{accno}_description"} =
1369             "$ref->{accno}--$ref->{description}";
1370
1371           if ($form->{id}) {
1372             if ($form->{amount}{ $ref->{accno} }) {
1373               $form->{"IC_tax_$ref->{accno}"} = "checked";
1374             }
1375           } else {
1376             $form->{"IC_tax_$ref->{accno}"} = "checked";
1377           }
1378         }
1379       } else {
1380
1381         $form->{"select$key"} .=
1382           "<option $ref->{selected}>$ref->{accno}--$ref->{description}\n";
1383         if ($form->{amount}{$key} eq $ref->{accno}) {
1384           $form->{$key} = "$ref->{accno}--$ref->{description}";
1385         }
1386
1387       }
1388     }
1389   }
1390   chop $form->{taxaccounts};
1391
1392   if (($form->{item} eq "part") || ($form->{item} eq "assembly")) {
1393     $form->{selectIC_income}  = $form->{selectIC_sale};
1394     $form->{selectIC_expense} = $form->{selectIC_cogs};
1395     $form->{IC_income}        = $form->{IC_sale};
1396     $form->{IC_expense}       = $form->{IC_cogs};
1397   }
1398
1399   delete $form->{IC_links};
1400   delete $form->{amount};
1401
1402   $form->get_partsgroup(\%myconfig, { all => 1 });
1403
1404   $form->{partsgroup} = "$form->{partsgroup}--$form->{partsgroup_id}";
1405
1406   if (@{ $form->{all_partsgroup} }) {
1407     $form->{selectpartsgroup} = qq|<option>\n|;
1408     map {
1409       $form->{selectpartsgroup} .=
1410         qq|<option value="$_->{partsgroup}--$_->{id}">$_->{partsgroup}\n|
1411     } @{ $form->{all_partsgroup} };
1412   }
1413
1414   if ($form->{item} eq 'assembly') {
1415
1416     foreach my $i (1 .. $form->{assembly_rows}) {
1417       if ($form->{"partsgroup_id_$i"}) {
1418         $form->{"partsgroup_$i"} =
1419           qq|$form->{"partsgroup_$i"}--$form->{"partsgroup_id_$i"}|;
1420       }
1421     }
1422     $form->get_partsgroup(\%myconfig);
1423
1424     if (@{ $form->{all_partsgroup} }) {
1425       $form->{selectassemblypartsgroup} = qq|<option>\n|;
1426
1427       map {
1428         $form->{selectassemblypartsgroup} .=
1429           qq|<option value="$_->{partsgroup}--$_->{id}">$_->{partsgroup}\n|
1430       } @{ $form->{all_partsgroup} };
1431     }
1432   }
1433   $lxdebug->leave_sub();
1434 }
1435
1436 sub form_header {
1437   $lxdebug->enter_sub();
1438
1439   my ($payment, $rows, $notes, $description, $ean, $buchungsgruppe, $partsgroup, $group, $tax, $lastcost, $eur, $linkaccounts, $weight, $n, $rop, $bin, $vegv);
1440   my ($notdiscountableok, $notdiscountable);
1441   my ($formula, $formula_label, $imagelinks, $obsolete, $shopok, $shop);
1442
1443   $form->get_lists('price_factors' => 'ALL_PRICE_FACTORS');
1444
1445   map({ $form->{$_} = $form->format_amount(\%myconfig, $form->{$_}, -2) }
1446       qw(sellprice listprice lastcost gv));
1447
1448   map { $form->{$_} = $form->format_amount(\%myconfig, $form->{$_}) }
1449     qw(weight rop stock);
1450
1451   foreach my $item (qw(partnumber description unit notes)) {
1452     $form->{$item} =~ s/\"/&quot;/g;
1453   }
1454
1455   $payment = qq|<option value=""></option>|;
1456   foreach my $item (@{ $form->{payment_terms} }) {
1457     if ($form->{payment_id} eq $item->{id}) {
1458       $payment .= qq|<option value="$item->{id}" selected>$item->{description}</option>|;
1459     } else {
1460       $payment .= qq|<option value="$item->{id}">$item->{description}</option>|;
1461     }
1462   }
1463
1464
1465   if (($rows = $form->numtextrows($form->{notes}, 40)) < 2) {
1466     $rows = 4;
1467   }
1468
1469   $notes =
1470     qq|<textarea name=notes rows=$rows cols=45 wrap=soft>$form->{notes}</textarea>|;
1471   if (($rows = $form->numtextrows($form->{description}, 40)) > 1) {
1472     $description =
1473       qq|<textarea name="description" rows=$rows cols=40 wrap=soft>$form->{description}</textarea>|;
1474   } else {
1475     $description =
1476       qq|<input name=description size=40 value="$form->{description}">|;
1477   }
1478
1479   $ean =  qq|<input name=ean size=40 value="$form->{ean}">|;
1480
1481   foreach my $item (split / /, $form->{taxaccounts}) {
1482     $form->{"IC_tax_$item"} = ($form->{"IC_tax_$item"}) ? "checked" : "";
1483   }
1484
1485   IC->retrieve_buchungsgruppen(\%myconfig, $form);
1486   if (@{ $form->{BUCHUNGSGRUPPEN} }) {
1487     foreach my $item (@{ $form->{BUCHUNGSGRUPPEN} }) {
1488       if ($item->{id} == $form->{buchungsgruppen_id}) {
1489         $form->{selectbuchungsgruppe} .=
1490           "<option value=$item->{id} selected>$item->{description}\n";
1491       } elsif (($form->{id} && $form->{orphaned}) || (!$form->{id})) {
1492         $form->{selectbuchungsgruppe} .=
1493           "<option value=$item->{id}>$item->{description}\n";
1494       }
1495
1496     }
1497   }
1498
1499   $buchungsgruppe = qq|
1500               <tr>
1501                 <th align=right>| . $locale->text('Buchungsgruppe') . qq|</th>
1502                 <td><select name=buchungsgruppen_id>$form->{selectbuchungsgruppe}</select></td>
1503               </tr>|;
1504
1505
1506   # set option
1507   foreach my $item (qw(IC IC_income IC_expense)) {
1508     if ($form->{$item}) {
1509       if ($form->{id} && $form->{orphaned}) {
1510         $form->{"select$item"} =~ s/ selected//;
1511         $form->{"select$item"} =~
1512           s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
1513       } else {
1514         $form->{"select$item"} = qq|<option selected>$form->{$item}|;
1515       }
1516     }
1517   }
1518
1519   if ($form->{selectpartsgroup}) {
1520     $form->{selectpartsgroup} = $form->unescape($form->{selectpartsgroup});
1521     $partsgroup =
1522       qq|<input type=hidden name=selectpartsgroup value="|
1523       . $form->escape($form->{selectpartsgroup}, 1) . qq|">|;
1524     $form->{selectpartsgroup} =~
1525       s/(<option value="\Q$form->{partsgroup}\E")/$1 selected/;
1526
1527     $partsgroup .=
1528       qq|<select name=partsgroup>$form->{selectpartsgroup}</select>|;
1529     $group = $locale->text('Group');
1530   }
1531
1532   # tax fields
1533   foreach my $item (split / /, $form->{taxaccounts}) {
1534     $tax .= qq|
1535       <input class=checkbox type=checkbox name="IC_tax_$item" value=1 $form->{"IC_tax_$item"}>&nbsp;<b>$form->{"IC_tax_${item}_description"}</b>
1536       <br><input type=hidden name=IC_tax_${item}_description value="$form->{"IC_tax_${item}_description"}">
1537 |;
1538   }
1539
1540   $form->{obsolete} = "checked" if $form->{obsolete};
1541
1542   $lastcost = qq|
1543               <tr>
1544                 <th align="right" nowrap="true">|
1545     . $locale->text('Last Cost') . qq|</th>
1546                 <td><input name=lastcost size=11 value=$form->{lastcost}></td>
1547               </tr>
1548 |;
1549   if (!$eur) {
1550     $linkaccounts = qq|
1551                <tr>
1552                 <th align=right>| . $locale->text('Inventory') . qq|</th>
1553                 <td><select name=IC>$form->{selectIC}</select></td>
1554                 <input name=selectIC type=hidden value="$form->{selectIC}">
1555               </tr>|;
1556   }
1557
1558   if ($form->{item} eq "part") {
1559
1560     $linkaccounts .= qq|
1561               <tr>
1562                 <th align=right>| . $locale->text('Revenue') . qq|</th>
1563                 <td><select name=IC_income>$form->{selectIC_income}</select></td>
1564                 <input name=selectIC_income type=hidden value="$form->{selectIC_income}">
1565               </tr>
1566               <tr>
1567                 <th align=right>| . $locale->text('Expense') . qq|</th>
1568                 <td><select name=IC_expense>$form->{selectIC_expense}</select></td>
1569                 <input name=selectIC_expense type=hidden value="$form->{selectIC_expense}">
1570               </tr>
1571 |;
1572
1573     $weight = qq|
1574               <tr>
1575                 <th align="right" nowrap="true">| . $locale->text('Weight') . qq|</th>
1576                 <td>
1577                   <table>
1578                     <tr>
1579                       <td>
1580                         <input name=weight size=10 value=$form->{weight}>
1581                       </td>
1582                     </tr>
1583                   </table>
1584                 </td>
1585               </tr>
1586 |;
1587
1588   }
1589
1590   if ($form->{item} eq "assembly") {
1591
1592     $lastcost = "";
1593
1594     $linkaccounts = qq|
1595               <tr>
1596                 <th align=right>| . $locale->text('Revenue') . qq|</th>
1597                 <td><select name=IC_income>$form->{selectIC_income}</select></td>
1598                 <input name=selectIC_income type=hidden value="$form->{selectIC_income}">
1599               </tr>
1600 |;
1601
1602     $weight = qq|
1603               <tr>
1604                 <th align="right" nowrap="true">| . $locale->text('Weight') . qq|</th>
1605                 <td>
1606                   <table>
1607                     <tr>
1608                       <td>
1609                         &nbsp;$form->{weight}
1610                         <input type=hidden name=weight value=$form->{weight}>
1611                       </td>
1612                     </tr>
1613                   </table>
1614                 </td>
1615               </tr>
1616 |;
1617
1618   }
1619
1620   if ($form->{item} eq "service") {
1621
1622     $linkaccounts = qq|
1623               <tr>
1624                 <th align=right>| . $locale->text('Revenue') . qq|</th>
1625                 <td><select name=IC_income>$form->{selectIC_income}</select></td>
1626                 <input name=selectIC_income type=hidden value="$form->{selectIC_income}">
1627               </tr>
1628               <tr>
1629                 <th align=right>| . $locale->text('Expense') . qq|</th>
1630                 <td><select name=IC_expense>$form->{selectIC_expense}</select></td>
1631                 <input name=selectIC_expense type=hidden value="$form->{selectIC_expense}">
1632               </tr>
1633 |;
1634
1635   }
1636   $linkaccounts = qq|<input type=hidden name=IC_income value="$form->{IC_income_default}">|;
1637
1638   if ($form->{IC_cogs_default}) {
1639     $form->{IC_expense_default} = $form->{IC_cogs_default};
1640   }
1641
1642   if (($form->{item} eq "service") || ($form->{item} eq "part")) {
1643     $linkaccounts .= qq|<input type=hidden name=IC_expense value="$form->{IC_expense_default}">|;
1644   }
1645   if ($form->{item} eq "part") {
1646     $linkaccounts .= qq|<input type=hidden name=IC value="$form->{IC_default}">|;
1647   }
1648
1649   if ($form->{item} ne 'service') {
1650     $n   = ($form->{onhand} > 0) ? "1" : "0";
1651     $rop = qq|
1652               <tr>
1653                 <th align="right" nowrap>| . $locale->text('On Hand') . qq|</th>
1654                 <th align=left nowrap class="plus$n">&nbsp;|
1655       . $form->format_amount(\%myconfig, $form->{onhand}) . qq|</th>
1656               </tr>
1657 |;
1658
1659     if ($form->{item} eq 'assembly') {
1660       $rop .= qq|
1661               <tr>
1662                 <th align="right" nowrap>| . $locale->text('Stock') . qq|</th>
1663                 <td><input name=stock size=10 value=$form->{stock}></td>
1664               </tr>
1665 |;
1666     }
1667
1668     $rop .= qq|
1669               <tr>
1670                 <th align="right" nowrap="true">| . $locale->text('ROP') . qq|</th>
1671                 <td><input name=rop size=10 value=$form->{rop}></td>
1672               </tr>
1673 |;
1674
1675     $bin = qq|
1676               <tr>
1677                 <th align="right" nowrap="true">| . $locale->text('Bin') . qq|</th>
1678                 <td><input name=bin size=10 value=$form->{bin}></td>
1679               </tr>
1680 |;
1681     $vegv = qq|
1682               <tr>
1683                 <th align="right" nowrap="true">|
1684       . $locale->text('Verrechnungseinheit') . qq|</th>
1685                 <td><input name=ve size=10 value=$form->{ve}></td>
1686               </tr>
1687               <tr>
1688                 <th align="right" nowrap="true">|
1689       . $locale->text('Business Volume') . qq|</th>
1690                 <td><input name=gv size=10 value=$form->{gv}></td>
1691               </tr>
1692 |;
1693     $notdiscountableok = ($form->{not_discountable}) == 1 ? "checked" : "";
1694     $notdiscountable .= qq|
1695               <tr>
1696                 <th align=right nowrap>|
1697     . $locale->text('Not Discountable') . qq|</th>
1698                 <td><input class=checkbox type=checkbox name=not_discountable value=1 $notdiscountableok></td>
1699              </tr>
1700 |;
1701
1702     $formula =
1703       qq|<ilayer><layer  onmouseover="this.T_STICKY=true;this.T_STATIC=true;return escape('| . $locale->text('The formula needs the following syntax:<br>For regular article:<br>Variablename= Variable Unit;<br>Variablename2= Variable2 Unit2;<br>...<br>###<br>Variable + ( Variable2 / Variable )<br><b>Please be beware of the spaces in the formula</b><br>') . qq|')"><textarea name=formel rows=4 cols=30 wrap=soft>$form->{formel}</textarea></layer><ilayer>|;
1704
1705     $formula_label = $locale->text('Formula');
1706
1707     $imagelinks = qq|
1708   <tr>
1709     <td>
1710       <table width=100%>
1711         <tr>
1712           <th align=right nowrap>| . $locale->text('Image') . qq|</th>
1713           <td><input name=image size=40 value="$form->{image}"></td>
1714           <th align=right nowrap>| . $locale->text('Microfiche') . qq|</th>
1715           <td><input name=microfiche size=20 value="$form->{microfiche}"></td>
1716         </tr>
1717         <tr>
1718           <th align=right nowrap>| . $locale->text('Drawing') . qq|</th>
1719           <td><input name=drawing size=40 value="$form->{drawing}"></td>
1720         </tr>
1721       </table>
1722     </td>
1723   </tr>
1724 |;
1725
1726   }
1727
1728   if ($form->{id}) {
1729     $obsolete = qq|
1730               <tr>
1731                 <th align="right" nowrap="true">| . $locale->text('Obsolete') . qq|</th>
1732                 <td><input name=obsolete type=checkbox class=checkbox value=1 $form->{obsolete}></td>
1733               </tr>
1734 |;
1735   }
1736   $shopok = $form->{shop} == 1 ? "checked" : "";
1737   $shop .= qq|
1738               <tr>
1739                 <th align=right nowrap>|
1740     . $locale->text('Shopartikel') . qq|</th>
1741                 <td><input class=checkbox type=checkbox name=shop value=1 $shopok></td>
1742              </tr>
1743 |;
1744
1745
1746   # type=submit $locale->text('Add Part')
1747   # type=submit $locale->text('Add Service')
1748   # type=submit $locale->text('Add Assembly')
1749
1750   # type=submit $locale->text('Edit Part')
1751   # type=submit $locale->text('Edit Service')
1752   # type=submit $locale->text('Edit Assembly')
1753   # use JavaScript Calendar or not
1754   my ($jsscript, $button1);
1755   $form->{jsscript} = 1;
1756   $jsscript = "";
1757   if ($form->{jsscript}) {
1758
1759     # with JavaScript Calendar
1760     $button1 = qq|
1761        <td width="13"><input name=priceupdate id=priceupdate size=11  title="$myconfig{dateformat}" value="$form->{priceupdate}"></td>
1762        <td width="4" align="left"><input type=button name=priceupdate id="trigger1" value=|
1763       . $locale->text('button') . qq|></td>
1764       |;
1765
1766     #write Trigger
1767     $jsscript =
1768       Form->write_trigger(\%myconfig, "1", "priceupdate", "BL", "trigger1");
1769   } else {
1770
1771     # without JavaScript Calendar
1772     $button1 = qq|
1773                               <td><input name=transdatefrom id=transdatefrom size=11 title="$myconfig{dateformat}"></td>|;
1774   }
1775
1776   my $unit_select = '<input type="hidden" name="unit_changeable" value="' . $form->{"unit_changeable"} . '">';
1777   if (!$form->{"unit_changeable"}) {
1778     $unit_select .= '<input type="hidden" name="unit" value="' . $form->{"unit"} . '">' . $form->{"unit"};
1779   } else {
1780     my $units = AM->retrieve_units(\%myconfig, $form, $form->{"item"} eq "service" ? "service" : "dimension");
1781     $unit_select .= AM->unit_select_html($units, "unit", $form->{"unit"});
1782   }
1783
1784   my $price_factor;
1785   if (0 < scalar @{ $form->{ALL_PRICE_FACTORS} }) {
1786     my @values = ('', map { $_->{id}                      } @{ $form->{ALL_PRICE_FACTORS} });
1787     my %labels =      map { $_->{id} => $_->{description} } @{ $form->{ALL_PRICE_FACTORS} };
1788
1789     $price_factor =
1790         qq|<tr><th align="right">|
1791       . $locale->text('Price Factor')
1792       . qq|</th><td>|
1793       . NTI($cgi->popup_menu('-name'    => 'price_factor_id',
1794                              '-default' => $form->{price_factor_id},
1795                              '-values'  => \@values,
1796                              '-labels'  => \%labels))
1797       . qq|</td></tr>|;
1798   }
1799
1800   $form->{fokus} = "ic.partnumber";
1801   $form->header;
1802
1803   print qq|
1804   <body onLoad="fokus()">
1805  <script type="text/javascript" src="js/common.js"></script>
1806  <script type="text/javascript" src="js/parts_language_selection.js"></script>
1807
1808 <form method=post name="ic" action=$form->{script}>
1809
1810 <input name=id type=hidden value=$form->{id}>
1811 <input name=item type=hidden value=$form->{item}>
1812 <input name=title type=hidden value="$form->{title}">
1813 <input name=makemodel type=hidden value="$form->{makemodel}">
1814 <input name=alternate type=hidden value="$form->{alternate}">
1815 <input name=onhand type=hidden value=$form->{onhand}>
1816 <input name=orphaned type=hidden value=$form->{orphaned}>
1817 <input name=taxaccounts type=hidden value="$form->{taxaccounts}">
1818 <input name=rowcount type=hidden value=$form->{rowcount}>
1819 <input name=eur type=hidden value=$eur>
1820 <input name=language_values type=hidden value="$form->{language_values}">
1821 <input name="original_partnumber" type="hidden" value="| . $form->quote($form->{"original_partnumber"}) . qq|">
1822
1823 <table width="100%">
1824   <tr>
1825     <th class=listtop>$form->{title}</th>
1826   </tr>
1827   <tr height="5"></tr>
1828   <tr>
1829     <td>
1830       <table width="100%">
1831         <tr valign=top>
1832           <th align=left>| . $locale->text('Part Number') . qq|</th>
1833           <th align=left>| . $locale->text('Part Description') . qq|</th>
1834           <th align=left>$group</th>
1835         </tr>
1836         <tr valign=top>
1837           <td><input name=partnumber value="$form->{partnumber}" size=20></td>
1838           <td>$description</td>
1839           <td>$partsgroup</td>
1840           <input type=hidden name=oldpartsgroup value="$form->{oldpartsgroup}">
1841         </tr>
1842       </table>
1843     </td>
1844   </tr>
1845   <tr>
1846     <td>
1847       <table width="100%" height="100%">
1848         <tr valign=top>
1849           <td width=70%>
1850             <table width="100%" height="100%">
1851               <tr>
1852                 <td colspan=2>
1853                   <table>
1854                     $buchungsgruppe
1855                     $linkaccounts
1856                   </table>
1857                 </td>
1858               </tr>
1859               <tr>
1860                 <th align="left">| . $locale->text('Notes') . qq|</th>
1861                 <th align="left">$formula_label</th>
1862               </tr>
1863               <tr>
1864                 <td>
1865                   $notes
1866                 </td>
1867                 <td>
1868                   $formula
1869                 </td>
1870               </tr>
1871               <tr>
1872                 <th align="left"></th>
1873                 <th align="left">| . $locale->text('EAN-Code') . qq|</th>
1874               </tr>
1875               <tr>
1876                 <td>
1877                   <button type="button" onclick="parts_language_selection_window('language_values')">| . $locale->text('Set Language Values') . qq|</button>
1878                 </td>
1879                 <td>
1880                   $ean  
1881                 </td>
1882               </tr>
1883               <tr>
1884               <td colspan=2>
1885                 <table>
1886                 <tr>
1887                <th align=right>| . $locale->text('Payment Terms') . qq|</th>
1888                  <td><select name=payment_id>$payment</select></td>
1889                 </tr>
1890                </table>
1891               </td>
1892               </tr>
1893             </table>
1894           </td>
1895           <td width="30%">
1896             <table width="100%">
1897               <tr>
1898                 <th align="right" nowrap="true">|
1899     . $locale->text('Updated') . qq|</th>
1900                 $button1
1901               </tr>
1902               <tr>
1903                 <th align="right" nowrap="true">| . $locale->text('List Price') . qq|</th>
1904                 <td><input name=listprice size=11 value=$form->{listprice}></td>
1905               </tr>
1906               <tr>
1907                 <th align="right" nowrap="true">| . $locale->text('Sell Price') . qq|</th>
1908                 <td><input name=sellprice size=11 value=$form->{sellprice}></td>
1909               </tr>
1910               $lastcost
1911               $price_factor
1912               <tr>
1913                 <th align="right" nowrap="true">| . $locale->text('Unit') . qq|</th>
1914                 <td>$unit_select</td>
1915               </tr>
1916               $weight
1917               $rop
1918               $bin
1919               $notdiscountable
1920               $vegv
1921               $shop
1922               $obsolete
1923             </table>
1924           </td>
1925         </tr>
1926       </table>
1927     </td>
1928   </tr>
1929   $imagelinks
1930
1931 $jsscript
1932 |;
1933   $lxdebug->leave_sub();
1934 }
1935
1936 sub form_footer {
1937   $lxdebug->enter_sub();
1938
1939   if ($form->{item} eq "assembly") {
1940
1941     print qq|
1942         <tr>
1943           <td>
1944             <table width="100%">
1945               <tr>
1946                 <th colspan=2 align=right>|
1947       . $locale->text('Total') . qq|&nbsp;</th>
1948                 <th align=right>|
1949       . $form->format_amount(\%myconfig, $form->{assemblytotal}, 2) . qq|</th>
1950               </tr>
1951             </table>
1952           </td>
1953         </tr>
1954         <input type=hidden name=assembly_rows value=$form->{assembly_rows}>
1955 |;
1956   }
1957
1958   print qq|
1959       <input type=hidden name=login value=$form->{login}>
1960       <input type=hidden name=password value=$form->{password}>
1961       <input type=hidden name=callback value="$form->{callback}">
1962       <input type=hidden name=previousform value="$form->{previousform}">
1963       <input type=hidden name=taxaccount2 value="$form->{taxaccount2}">
1964       <input type=hidden name=vc value=$form->{vc}>
1965   <tr>
1966     <td><hr size=3 noshade></td>
1967   </tr>
1968 </table>
1969
1970 <br>
1971 <input class=submit type=submit name=action value="|
1972     . $locale->text('Update') . qq|">
1973   |;
1974
1975   unless ($form->{item} eq "service") {
1976     print qq|
1977       <input type=hidden name=makemodel_rows value=$form->{makemodel_rows}>
1978     |;
1979   }
1980
1981   print qq|
1982      <input type=hidden name=price_rows value=$form->{price_rows}>|;
1983
1984   print qq|
1985       <input class=submit type=submit name=action value="|
1986     . $locale->text('Save') . qq|">|;
1987
1988   if ($form->{id}) {
1989
1990     if (!$form->{previousform}) {
1991       print qq|
1992       <input class=submit type=submit name=action value="|
1993         . $locale->text('Save as new') . qq|">|;
1994     }
1995
1996     if ($form->{orphaned}) {
1997       if (!$form->{previousform}) {
1998         if ($form->{item} eq 'assembly') {
1999           if (!$form->{onhand}) {
2000             print qq|
2001       <input class=submit type=submit name=action value="|
2002               . $locale->text('Delete') . qq|">|;
2003           }
2004         } else {
2005           print qq|
2006       <input class=submit type=submit name=action value="|
2007             . $locale->text('Delete') . qq|">|;
2008         }
2009       }
2010     }
2011   }
2012
2013   if (!$form->{previousform}) {
2014     if ($form->{menubar}) {
2015       require "bin/mozilla/menu.pl";
2016       &menubar;
2017     }
2018   }
2019 # button for saving history
2020   if($form->{id} ne "") {
2021         print qq|
2022                 <input type=button class=submit onclick=set_history_window(|
2023                 . $form->{id} 
2024                 . qq|); name=history id=history value=|
2025                 . $locale->text('history') 
2026                 . qq|>|;
2027   }
2028 # /button for saving history
2029   print qq|
2030
2031 </form>
2032
2033 <script type="text/javascript" src="js/wz_tooltip.js"></script>
2034
2035 </body>
2036 </html>
2037 |;
2038
2039   $lxdebug->leave_sub();
2040 }
2041
2042 sub makemodel_row {
2043   $lxdebug->enter_sub();
2044   my ($numrows) = @_;
2045   
2046   print qq|
2047   <tr>
2048     <td>
2049       <table width=100%>
2050         <tr>
2051           <th class="listheading">| . $locale->text('Make') . qq|</th>
2052           <th class="listheading">| . $locale->text('Model') . qq|</th>
2053         </tr>
2054 |;
2055
2056   for my $i (1 .. $numrows) {
2057     $form->{"make_$i"}  =~ s/\"/&quot;/g;
2058     $form->{"model_$i"} =~ s/\"/&quot;/g;
2059     print qq|
2060         <tr>
2061           <td width=50%><input name="make_$i" size=30 value="$form->{"make_$i"}"></td>
2062           <td width=50%><input name="model_$i" size=30 value="$form->{"model_$i"}"></td>
2063         </tr>
2064 |;
2065   }
2066
2067   print qq|
2068       </table>
2069     </td>
2070   </tr>
2071 |;
2072
2073   $lxdebug->leave_sub();
2074 }
2075
2076 sub assembly_row {
2077   $lxdebug->enter_sub();
2078   my ($numrows) = @_;
2079   my (@column_index, %column_data, %column_header);
2080   my ($nochange, $callback, $previousform, $linetotal, $href);
2081
2082   our ($deliverydate); # ToDO: cjeck if this indeed comes from global context
2083
2084   @column_index =
2085     qw(runningnumber qty unit bom partnumber description partsgroup total);
2086
2087   if ($form->{previousform}) {
2088     $nochange     = 1;
2089     @column_index = qw(qty unit bom partnumber description partsgroup total);
2090   } else {
2091
2092     # change callback
2093     $form->{old_callback} = $form->{callback};
2094     $callback             = $form->{callback};
2095     $form->{callback}     = "$form->{script}?action=display_form";
2096
2097     # delete action
2098     map { delete $form->{$_} } qw(action header);
2099
2100     $previousform = "";
2101
2102     # save form variables in a previousform variable
2103     foreach my $key (sort keys %$form) {
2104
2105       # escape ampersands
2106       $form->{$key} =~ s/&/%26/g;
2107       $previousform .= qq|$key=$form->{$key}&|;
2108     }
2109     chop $previousform;
2110     $previousform = $form->escape($form->escape($previousform, 1));
2111     $form->{callback} = $callback;
2112
2113     $form->{assemblytotal} = 0;
2114     $form->{weight}        = 0;
2115
2116   }
2117   $column_header{runningnumber} =
2118     qq|<th nowrap width=5%>| . $locale->text('No.') . qq|</th>|;
2119   $column_header{qty} =
2120     qq|<th align=left nowrap width=10%>| . $locale->text('Qty') . qq|</th>|;
2121   $column_header{unit} =
2122     qq|<th align=left nowrap width=5%>| . $locale->text('Unit') . qq|</th>|;
2123   $column_header{partnumber} =
2124       qq|<th align=left nowrap width=20%>|
2125     . $locale->text('Part Number')
2126     . qq|</th>|;
2127   $column_header{description} =
2128     qq|<th nowrap width=50%>| . $locale->text('Part Description') . qq|</th>|;
2129   $column_header{total} =
2130     qq|<th align=right nowrap>| . $locale->text('Extended') . qq|</th>|;
2131   $column_header{bom}        = qq|<th>| . $locale->text('BOM') . qq|</th>|;
2132   $column_header{partsgroup} = qq|<th>| . $locale->text('Group') . qq|</th>|;
2133
2134   print qq|
2135   <tr class=listheading>
2136     <th class=listheading>| . $locale->text('Individual Items') . qq|</th>
2137   </tr>
2138   <tr>
2139     <td>
2140       <table width=100%>
2141         <tr>
2142 |;
2143
2144   map { print "\n$column_header{$_}" } @column_index;
2145
2146   print qq|
2147         </tr>
2148 |;
2149
2150   for my $i (1 .. $numrows) {
2151     $form->{"partnumber_$i"} =~ s/\"/&quot;/g;
2152
2153     $linetotal =
2154       $form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"}, 2);
2155     $form->{assemblytotal} += $linetotal;
2156
2157     $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"});
2158
2159     $linetotal = $form->format_amount(\%myconfig, $linetotal, 2);
2160
2161     if (($i >= 1) && ($i == $numrows)) {
2162
2163       if ($nochange) {
2164         map { $column_data{$_} = qq|<td></td>| }
2165           qw(qty unit partnumber description bom partsgroup);
2166       } else {
2167
2168         map { $column_data{$_} = qq|<td></td>| } qw(runningnumber unit bom);
2169
2170         $column_data{qty} =
2171           qq|<td><input name="qty_$i" size=5 value="$form->{"qty_$i"}"></td>|;
2172         $column_data{partnumber} =
2173           qq|<td><input name="partnumber_$i" size=15 value="$form->{"partnumber_$i"}"></td>|;
2174         $column_data{description} =
2175           qq|<td><input name="description_$i" size=40 value="$form->{"description_$i"}"></td>|;
2176         $column_data{partsgroup} =
2177           qq|<td><input name="partsgroup_$i" size=10 value="$form->{"partsgroup_$i"}"></td>|;
2178
2179       }
2180
2181     } else {
2182
2183       if ($form->{previousform}) {
2184         $column_data{partnumber} =
2185           qq|<td><input type=hidden name="partnumber_$i" value="$form->{"partnumber_$i"}">$form->{"partnumber_$i"}</td>|;
2186         $column_data{qty} =
2187           qq|<td align=right><input type=hidden name="qty_$i" value="$form->{"qty_$i"}">$form->{"qty_$i"}</td>|;
2188
2189         $column_data{bom} =
2190           qq|<td align=center><input type=hidden name="bom_$i" value=$form->{"bom_$i"}>|;
2191         $column_data{bom} .= ($form->{"bom_$i"}) ? "x" : "&nbsp;";
2192         $column_data{bom} .= qq|</td>|;
2193
2194         $column_data{partsgroup} =
2195           qq|<td><input type=hidden name="partsgroup_$i" value="$form->{"partsgroup_$i"}">$form->{"partsgroup_$i"}</td>|;
2196
2197       } else {
2198         $href =
2199           qq|$form->{script}?action=edit&id=$form->{"id_$i"}&login=$form->{login}&password=$form->{password}&rowcount=$i&previousform=$previousform|;
2200         $column_data{partnumber} =
2201           qq|<td><input type=hidden name="partnumber_$i" value="$form->{"partnumber_$i"}"><a href=$href>$form->{"partnumber_$i"}</a></td>|;
2202         $column_data{runningnumber} =
2203           qq|<td><input name="runningnumber_$i" size=3 value="$i"></td>|;
2204         $column_data{qty} =
2205           qq|<td><input name="qty_$i" size=5 value="$form->{"qty_$i"}"></td>|;
2206
2207         $form->{"bom_$i"} = ($form->{"bom_$i"}) ? "checked" : "";
2208         $column_data{bom} =
2209           qq|<td align=center><input name="bom_$i" type=checkbox class=checkbox value=1 $form->{"bom_$i"}></td>|;
2210
2211         $column_data{partsgroup} =
2212           qq|<td><input type=hidden name="partsgroup_$i" value="$form->{"partsgroup_$i"}">$form->{"partsgroup_$i"}</td>|;
2213       }
2214
2215       $column_data{unit} =
2216         qq|<td><input type=hidden name="unit_$i" value="$form->{"unit_$i"}">$form->{"unit_$i"}</td>|;
2217       $column_data{description} =
2218         qq|<td><input type=hidden name="description_$i" value="$form->{"description_$i"}">$form->{"description_$i"}</td>|;
2219     }
2220
2221     $column_data{total} = qq|<td align=right>$linetotal</td>|;
2222
2223     $column_data{deliverydate} = qq|<td align=right>$deliverydate</td>|;
2224
2225     print qq|
2226         <tr>|;
2227
2228     map { print "\n$column_data{$_}" } @column_index;
2229
2230     print qq|
2231         </tr>
2232   <input type=hidden name="id_$i" value=$form->{"id_$i"}>
2233   <input type=hidden name="sellprice_$i" value=$form->{"sellprice_$i"}>
2234   <input type=hidden name="weight_$i" value=$form->{"weight_$i"}>
2235 |;
2236   }
2237
2238   print qq|
2239       </table>
2240     </td>
2241   </tr>
2242 |;
2243
2244   $lxdebug->leave_sub();
2245 }
2246
2247 sub update {
2248   $lxdebug->enter_sub();
2249
2250   if ($form->{item} eq "assembly") {
2251     my $i = $form->{assembly_rows};
2252
2253     # if last row is empty check the form otherwise retrieve item
2254     if (   ($form->{"partnumber_$i"} eq "")
2255         && ($form->{"description_$i"} eq "")
2256         && ($form->{"partsgroup_$i"}  eq "")) {
2257
2258       &check_form;
2259
2260     } else {
2261
2262       IC->assembly_item(\%myconfig, \%$form);
2263
2264       my $rows = scalar @{ $form->{item_list} };
2265
2266       if ($rows) {
2267         $form->{"qty_$i"} = 1 unless ($form->{"qty_$i"});
2268
2269         if ($rows > 1) {
2270           $form->{makemodel_rows}--;
2271           &select_item;
2272           exit;
2273         } else {
2274           map { $form->{item_list}[$i]{$_} =~ s/\"/&quot;/g }
2275             qw(partnumber description unit partsgroup);
2276           map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} }
2277             keys %{ $form->{item_list}[0] };
2278           $form->{"runningnumber_$i"} = $form->{assembly_rows};
2279           $form->{assembly_rows}++;
2280
2281           &check_form;
2282
2283         }
2284
2285       } else {
2286
2287         $form->{rowcount} = $i;
2288         $form->{assembly_rows}++;
2289
2290         &new_item;
2291
2292       }
2293     }
2294
2295   } elsif (($form->{item} eq 'part') || ($form->{item} eq 'service')) {
2296     &check_form;
2297   }
2298
2299   $lxdebug->leave_sub();
2300 }
2301
2302 sub save {
2303   $lxdebug->enter_sub();
2304
2305   my ($parts_id, %newform, $previousform, $amount, $callback);
2306
2307   # check if there is a part number - commented out, cause there is an automatic allocation of numbers
2308   # $form->isblank("partnumber", $locale->text(ucfirst $form->{item}." Part Number missing!"));
2309
2310   # check if there is a description
2311   $form->isblank("description", $locale->text("Part Description missing!"));
2312
2313   if ($form->{obsolete}) {
2314     $form->error(
2315       $locale->text(
2316         "Inventory quantity must be zero before you can set this $form->{item} obsolete!"
2317       ))
2318       if ($form->{onhand} * 1);
2319   }
2320
2321   if (!$form->{buchungsgruppen_id}) {
2322     $form->error($locale->text("Parts must have an entry type.") . " " . 
2323      $locale->text("If you see this message, you most likely just setup your LX-Office and haven't added any entry types. If this is the case, the option is accessible for administrators in the System menu.") 
2324     );
2325   }
2326
2327   $form->error($locale->text('Description must not be empty!')) unless $form->{description};
2328   $form->error($locale->text('Partnumber must not be set to empty!')) if $form->{id} && !$form->{partnumber};
2329
2330   # expand dynamic strings
2331   # $locale->text('Inventory quantity must be zero before you can set this part obsolete!')
2332   # $locale->text('Inventory quantity must be zero before you can set this assembly obsolete!')
2333   # $locale->text('Part Number missing!')
2334   # $locale->text('Service Number missing!')
2335   # $locale->text('Assembly Number missing!')
2336
2337   # save part
2338   $lxdebug->message($LXDebug::DEBUG1, "ic.pl: sellprice in save = $form->{sellprice}\n");
2339   if (IC->save(\%myconfig, \%$form) == 3) {
2340     $form->error($locale->text('Partnumber not unique!'));
2341   }
2342   # saving the history
2343   if(!exists $form->{addition}) {
2344     $form->{snumbers} = qq|partnumber_| . $form->{partnumber};
2345         $form->{addition} = "SAVED";
2346         $form->save_history($form->dbconnect(\%myconfig));
2347   }
2348   # /saving the history
2349   $parts_id = $form->{id};
2350
2351   my $i;
2352   # load previous variables
2353   if ($form->{previousform}) {
2354
2355     # save the new form variables before splitting previousform
2356     map { $newform{$_} = $form->{$_} } keys %$form;
2357
2358     $previousform = $form->unescape($form->{previousform});
2359
2360     # don't trample on previous variables
2361     map { delete $form->{$_} } keys %newform;
2362
2363     # now take it apart and restore original values
2364     foreach my $item (split /&/, $previousform) {
2365       my ($key, $value) = split m/=/, $item, 2;
2366       $value =~ s/%26/&/g;
2367       $form->{$key} = $value;
2368     }
2369     $form->{taxaccounts} = $newform{taxaccount2};
2370
2371     if ($form->{item} eq 'assembly') {
2372
2373       # undo number formatting
2374       map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
2375         qw(weight listprice sellprice rop);
2376
2377       $form->{assembly_rows}--;
2378       $i = $newform{rowcount};
2379       $form->{"qty_$i"} = 1 unless ($form->{"qty_$i"});
2380
2381       $form->{sellprice} -= $form->{"sellprice_$i"} * $form->{"qty_$i"};
2382       $form->{weight}    -= $form->{"weight_$i"} * $form->{"qty_$i"};
2383
2384       # change/add values for assembly item
2385       map { $form->{"${_}_$i"} = $newform{$_} } qw(partnumber description bin unit weight listprice sellprice inventory_accno income_accno expense_accno price_factor_id);
2386
2387       $form->{sellprice} += $form->{"sellprice_$i"} * $form->{"qty_$i"};
2388       $form->{weight}    += $form->{"weight_$i"} * $form->{"qty_$i"};
2389
2390     } else {
2391
2392       # set values for last invoice/order item
2393       $i = $form->{rowcount};
2394       $form->{"qty_$i"} = 1 unless ($form->{"qty_$i"});
2395
2396       map { $form->{"${_}_$i"} = $newform{$_} } qw(partnumber description bin unit listprice inventory_accno income_accno expense_accno sellprice lastcost price_factor_id);
2397
2398       $form->{"sellprice_$i"} = $newform{lastcost} if ($form->{vendor_id});
2399
2400       if ($form->{exchangerate} != 0) {
2401         $form->{"sellprice_$i"} /= $form->{exchangerate};
2402       }
2403
2404       $lxdebug->message($LXDebug::DEBUG1, qq|sellprice_$i in previousform 2 = | . $form->{"sellprice_$i"} . qq|\n|);
2405
2406       map { $form->{"taxaccounts_$i"} .= "$_ " } split / /, $newform{taxaccount};
2407       chop $form->{"taxaccounts_$i"};
2408       foreach my $item (qw(description rate taxnumber)) {
2409         my $index = $form->{"taxaccounts_$i"} . "_$item";
2410         $form->{$index} = $newform{$index};
2411       }
2412
2413       # credit remaining calculation
2414       $amount = $form->{"sellprice_$i"} * (1 - $form->{"discount_$i"} / 100) * $form->{"qty_$i"};
2415
2416       map { $form->{"${_}_base"} += $amount } (split / /, $form->{"taxaccounts_$i"});
2417       map { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) } split / /, $form->{"taxaccounts_$i"} if !$form->{taxincluded};
2418
2419       $form->{creditremaining} -= $amount;
2420
2421       # redo number formatting, because invoice parse them!
2422       map { $form->{"${_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}) } qw(weight listprice sellprice rop);
2423     }
2424
2425     $form->{"id_$i"} = $parts_id;
2426
2427     # Get the actual price factor (not just the ID) for the marge calculation.
2428     $form->get_lists('price_factors' => 'ALL_PRICE_FACTORS');
2429     foreach my $pfac (@{ $form->{ALL_PRICE_FACTORS} }) {
2430       next if ($pfac->{id} != $newform{price_factor_id});
2431       $form->{"marge_price_factor_$i"} = $pfac->{factor};
2432       last;
2433     }
2434     delete $form->{ALL_PRICE_FACTORS};
2435
2436     delete $form->{action};
2437
2438     # restore original callback
2439     $callback = $form->unescape($form->{callback});
2440     $form->{callback} = $form->unescape($form->{old_callback});
2441     delete $form->{old_callback};
2442
2443     $form->{makemodel_rows}--;
2444
2445     # put callback together
2446     foreach my $key (keys %$form) {
2447
2448       # do single escape for Apache 2.0
2449       my $value = $form->escape($form->{$key}, 1);
2450       $callback .= qq|&$key=$value|;
2451     }
2452     $form->{callback} = $callback;
2453   }
2454   $lxdebug->message($LXDebug::DEBUG1, qq|ic.pl: sellprice_$i nach sub save = | . $form->{"sellprice_$i"} . qq|\n|);
2455
2456   # redirect
2457   $form->redirect;
2458
2459   $lxdebug->leave_sub();
2460 }
2461
2462 sub save_as_new {
2463   $lxdebug->enter_sub();
2464
2465   # saving the history
2466   if(!exists $form->{addition}) {
2467     $form->{snumbers} = qq|partnumber_| . $form->{partnumber};
2468         $form->{addition} = "SAVED AS NEW";
2469         $form->save_history($form->dbconnect(\%myconfig));
2470   }
2471   # /saving the history
2472   $form->{id} = 0;
2473   if ($form->{"original_partnumber"} &&
2474       ($form->{"partnumber"} eq $form->{"original_partnumber"})) {
2475     $form->{partnumber} = "";
2476   }
2477   &save;
2478   $lxdebug->leave_sub();
2479 }
2480
2481 sub delete {
2482   $lxdebug->enter_sub();
2483   # saving the history
2484   if(!exists $form->{addition}) {
2485     $form->{snumbers} = qq|partnumber_| . $form->{partnumber};
2486         $form->{addition} = "DELETED";
2487         $form->save_history($form->dbconnect(\%myconfig));
2488   }
2489   # /saving the history
2490   my $rc = IC->delete(\%myconfig, \%$form);
2491
2492   # redirect
2493   $form->redirect($locale->text('Item deleted!')) if ($rc > 0);
2494   $form->error($locale->text('Cannot delete item!'));
2495
2496   $lxdebug->leave_sub();
2497 }
2498
2499 sub price_row {
2500   $lxdebug->enter_sub();
2501
2502   my ($numrows) = @_;
2503
2504   print qq|
2505   <tr>
2506     <td>
2507       <table width=100%>
2508         <tr>
2509           <th class="listheading">| . $locale->text('Preisklasse') . qq|</th>
2510           <th class="listheading">| . $locale->text('Preis') . qq|</th>
2511         </tr>
2512 |;
2513   for my $i (1 .. $numrows) {
2514     print qq|
2515         <tr>
2516           <td width=50%><input type=hidden name="pricegroup_$i" size=30  value="$form->{"pricegroup_$i"}">$form->{"pricegroup_$i"}</td>
2517           <td width=50%><input name="price_$i" size=11 value="$form->{"price_$i"}"></td>
2518           <input type=hidden name="pricegroup_id_$i" value="$form->{"pricegroup_id_$i"}">
2519         </tr>
2520 |;
2521   }
2522
2523   print qq|
2524       </table>
2525     </td>
2526   </tr>
2527 |;
2528
2529   $lxdebug->leave_sub();
2530 }
2531
2532 sub parts_language_selection {
2533   $lxdebug->enter_sub();
2534
2535   our ($onload);
2536
2537   my $languages = IC->retrieve_languages(\%myconfig, $form);
2538
2539   if ($form->{language_values} ne "") {
2540     foreach $item (split(/---\+\+\+---/, $form->{language_values})) {
2541       my ($language_id, $translation, $longdescription) = split(/--\+\+--/, $item);
2542
2543       foreach my $language (@{ $languages }) {
2544         next unless ($language->{id} == $language_id);
2545
2546         $language->{translation}     = $translation;
2547         $language->{longdescription} = $longdescription;
2548         last;
2549       }
2550     }
2551   }
2552
2553   my @header_sort = qw(name longdescription);
2554   my %header_title = ( "name" => $locale->text("Name"),
2555                        "longdescription" => $locale->text("Long Description"),
2556                        );
2557
2558   my @header =
2559     map(+{ "column_title" => $header_title{$_},
2560            "column" => $_,
2561            "callback" => $callback,
2562          },
2563         @header_sort);
2564
2565   $form->{"title"} = $locale->text("Language Values");
2566   $form->header();
2567   print $form->parse_html_template("ic/parts_language_selection", { "HEADER"    => \@header,
2568                                                                     "LANGUAGES" => $languages,
2569                                                                     "onload"    => $onload });
2570
2571   $lxdebug->leave_sub();
2572 }
2573
2574 sub continue { call_sub($form->{"nextsub"}); }