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