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