ct->get_delivery auf template umgestellt
[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 # Parserhappy(R):
56 # type=submit $locale->text('Add Part')
57 # type=submit $locale->text('Add Service')
58 # type=submit $locale->text('Add Assembly')
59 # type=submit $locale->text('Edit Part')
60 # type=submit $locale->text('Edit Service')
61 # type=submit $locale->text('Edit Assembly')
62
63 # end of main
64
65 sub add {
66   $lxdebug->enter_sub();
67
68   $form->{title} = $locale->text('Add ' . ucfirst $form->{item});
69
70   $form->{callback} =
71     "$form->{script}?action=add&item=$form->{item}&login=$form->{login}&password=$form->{password}"
72     unless $form->{callback};
73
74   $form->{"unit_changeable"} = 1;
75
76   IC->get_pricegroups(\%myconfig, \%$form);
77   &link_part;
78   &display_form;
79
80   $lxdebug->leave_sub();
81 }
82
83 sub search {
84   $lxdebug->enter_sub();
85
86   # switch for backward sorting
87   $form->{revers} = 0;
88
89   # memory for which table was sort at last time
90   $form->{lastsort} = "";
91
92   # counter for added entries to top100
93   $form->{ndxs_counter} = 0;
94
95   # $locale->text('Parts')
96   # $locale->text('Services')
97
98   my $is_service  = $form->{searchitems} eq 'service';
99   my $is_assembly = $form->{searchitems} eq 'assembly';
100
101   $form->{title} = (ucfirst $form->{searchitems}) . "s";
102   $form->{title} = $locale->text($form->{title});
103   $form->{title} = $locale->text('Assemblies') if $is_assembly;
104
105   $form->{jsscript} = 1;
106
107   $form->header;
108
109   print $form->parse_html_template('ic/search', { is_assembly => $is_assembly, 
110                                                   is_service  => $is_service, 
111                                                   dateformat  => $myconfig{dateformat}, });
112
113   $lxdebug->leave_sub();
114 }    #end search()
115
116 sub search_update_prices {
117   $lxdebug->enter_sub();
118
119   my $pricegroups = IC->get_pricegroups(\%myconfig, \%$form);
120
121   $form->header;
122
123   print $form->parse_html_template('ic/search_update_prices', { PRICE_ROWS => $pricegroups });
124
125   $lxdebug->leave_sub();
126 }    #end search()
127
128 sub confirm_price_update {
129   $lxdebug->enter_sub();
130
131   $form->{nextsub} = "update_prices";
132   $form->header;
133
134   map { delete $form->{$_} } qw(action header);
135   
136   print $form->parse_html_template('ic/confirm_price_update', { HIDDENS => [ map { name => $_, value => $form->{$_} }, keys %$form ] });
137
138   $lxdebug->leave_sub();
139 }
140
141 sub update_prices {
142   $lxdebug->enter_sub();
143
144   if (IC->update_prices(\%myconfig, \%$form)) {
145     $form->redirect($form->{update_count} . $locale->text('prices updated!'));
146   } else {
147     $form->error($locale->text('Could not update prices!'));
148   }
149
150   $lxdebug->leave_sub();
151 }
152
153 sub choice {
154   $lxdebug->enter_sub();
155
156   our ($j, $lastndx);
157   my ($totop100);
158
159   $form->{title} = $locale->text('Top 100 hinzufuegen');
160
161   $form->header;
162
163   push @custom_hiddens, qw(login password searchitems title bom titel revers lastsort sort ndxs_counter extras);
164   push @custom_hiddens, qw(itemstatus l_linetotal l_partnumber l_description l_onhand l_unit l_sellprice l_linetotalsellprice);
165   my @HIDDENS = (
166         +{ name => 'row',     value => $j              },
167         +{ name => 'nextsub', value => 'item_selected' },
168         +{ name => 'test',    value => 'item_selected' },
169         +{ name => 'lastndx', value => $lastndx        },
170     map(+{ name => $_,        value => $form->{$_}     }, @custom_hiddens),
171   );
172
173   my ($partnumber, $description, $unit, $sellprice, $soldtotal);
174   # if choice set data
175 #  if ($form->{ndx}) {
176 #    for my $i (0 .. $form->{ndxs_counter}) {
177 #
178 #      # insert data into top100
179 #      push @{ $form->{parts} },
180 #        { number      => "",
181 #          partnumber  => $form->{"totop100_partnumber_$j"},
182 #          description => $form->{"totop100_description_$j"},
183 #          unit        => $form->{"totop100_unit_$j"},
184 #          sellprice   => $form->{"totop100_sellprice_$j"},
185 #          soldtotal   => $form->{"totop100_soldtotal_$j"},
186 #        };
187 #    }    #rof
188 #  }    #fi
189
190   $totop100 = "";
191
192   # set data for next page
193   for my $i (1 .. $form->{ndxs_counter}) {
194     $partnumber  = $form->{"totop100_partnumber_$i"};
195     $description = $form->{"totop100_description_$i"};
196     $unit        = $form->{"totop100_unit_$i"};
197     $sellprice   = $form->{"totop100_sellprice_$i"};
198     $soldtotal   = $form->{"totop100_soldtotal_$i"};
199
200   push @PARTS, {
201     totop100_partnumber  => $form->{"totop100_partnumber_$i"},
202     totop100_description => $form->{"totop100_description_$i"},
203     totop100_unit        => $form->{"totop100_unit_$i"},
204     totop100_sellprice   => $form->{"totop100_sellprice_$i"},
205     totop100_soldtotal   => $form->{"totop100_soldtotal_$i"},
206   }
207
208 #    $totop100 .= qq|
209 #<input type=hidden name=totop100_partnumber_$i value=$form->{"totop100_partnumber_$i"}>
210 #<input type=hidden name=totop100_description_$i value=$form->{"totop100_description_$i"}>
211 #<input type=hidden name=totop100_unit_$i value=$form->{"totop100_unit_$i"}>
212 #<input type=hidden name=totop100_sellprice_$i value=$form->{"totop100_sellprice_$i"}>
213 #<input type=hidden name=totop100_soldtotal_$i value=$form->{"totop100_soldtotal_$i"}>
214 #    |;
215   }    #rof
216
217   print $form->parse_html_template('ic/choice', +{ HIDDENS => \@HIDDENS, PARTS => \@PARTS });
218
219   $lxdebug->leave_sub();
220 }    #end choice
221
222 sub list {
223   $lxdebug->enter_sub();
224
225   our ($lastndx);
226   our ($partnumber, $description, $unit, $sellprice, $soldtotal);
227
228   my @sortorders = ("", "partnumber", "description", "all");
229   my $sortorder = $sortorders[($form->{description} ? 2 : 0) + ($form->{partnumber} ? 1 : 0)];
230   IC->get_parts(\%myconfig, \%$form, $sortorder);
231
232   $form->{title} = $locale->text('Top 100 hinzufuegen');
233
234   $form->header;
235
236   print qq|
237 <body>
238   <form method=post action=ic.pl>
239     <table width=100%>
240      <tr>
241       <th class=listtop colspan=6>| . $locale->text('choice part') . qq|</th>
242      </tr>
243         <tr height="5"></tr>
244         <tr class=listheading>
245           <th>&nbsp;</th>
246           <th class=listheading>| . $locale->text('Part Number') . qq|</th>
247           <th class=listheading>| . $locale->text('Part Description') . qq|</th>
248           <th class=listheading>| . $locale->text('Unit of measure') . qq|</th>
249           <th class=listheading>| . $locale->text('Sell Price') . qq|</th>
250           <th class=listheading>| . $locale->text('soldtotal') . qq|</th>
251         </tr>|;
252
253   my $j = 0;
254   my $i = $form->{rows};
255
256   for ($j = 1; $j <= $i; $j++) {
257
258     print qq|
259         <tr class=listrow1>|;
260     if ($j == 1) {
261       print qq|
262             <td><input name=ndx class=radio type=radio value=$j checked></td>|;
263     } else {
264       print qq|
265           <td><input name=ndx class=radio type=radio value=$j></td>|;
266     }
267     print qq|
268           <td><input name="new_partnumber_$j" type=hidden value="$form->{"partnumber_$j"}">$form->{"partnumber_$j"}</td>
269           <td><input name="new_description_$j" type=hidden value="$form->{"description_$j"}">$form->{"description_$j"}</td>
270           <td><input name="new_unit_$j" type=hidden value="$form->{"unit_$j"}">$form->{"unit_$j"}</td>
271           <td><input name="new_sellprice_$j" type=hidden value="$form->{"sellprice_$j"}">$form->{"sellprice_$j"}</td>
272           <td><input name="new_soldtotal_$j" type=hidden value="$form->{"soldtotal_$j"}">$form->{"soldtotal_$j"}</td>
273         </tr>
274
275         <input name="new_id_$j" type=hidden value="$form->{"id_$j"}">|;
276   }
277
278   print qq|
279
280 </table>
281
282 <br>
283
284
285 <input type=hidden name=login value=$form->{login}>
286 <input type=hidden name=password value=$form->{password}>
287
288 <input type=hidden name=itemstatus value="$form->{itemstatus}">
289 <input type=hidden name=l_linetotal value="$form->{l_linetotal}">
290 <input type=hidden name=l_partnumber value="$form->{l_partnumber}">
291 <input type=hidden name=l_description value="$form->{l_description}">
292 <input type=hidden name=l_onhand value="$form->{l_onhand}">
293 <input type=hidden name=l_unit value="$form->{l_unit}">
294 <input type=hidden name=l_sellprice value="$form->{l_sellprice}">
295 <input type=hidden name=l_linetotalsellprice value="$form->{l_linetotalsellprice}">
296 <input type=hidden name=sort value="$form->{sort}">
297 <input type=hidden name=revers value="$form->{revers}">
298 <input type=hidden name=lastsort value="$form->{lastsort}">
299
300 <input type=hidden name=bom value="$form->{bom}">
301 <input type=hidden name=titel value="$form->{titel}">
302 <input type=hidden name=searchitems value="$form->{searchitems}">
303
304 <input type=hidden name=row value=$j>
305
306 <input type=hidden name=nextsub value=item_selected>
307
308 <input name=lastndx type=hidden value=$lastndx>
309
310 <input name=ndxs_counter type=hidden value=$form->{ndxs_counter}>|;
311
312   my $totop100 = "";
313
314   if (($form->{ndxs_counter}) > 0) {
315     for ($i = 1; ($i < $form->{ndxs_counter} + 1); $i++) {
316
317       $partnumber  = $form->{"totop100_partnumber_$i"};
318       $description = $form->{"totop100_description_$i"};
319       $unit        = $form->{"totop100_unit_$i"};
320       $sellprice   = $form->{"totop100_sellprice_$i"};
321       $soldtotal   = $form->{"totop100_soldtotal_$i"};
322
323       $totop100 .= qq|
324 <input type=hidden name=totop100_partnumber_$i value=$form->{"totop100_partnumber_$i"}>
325 <input type=hidden name=totop100_description_$i value=$form->{"totop100_description_$i"}>
326 <input type=hidden name=totop100_unit_$i value=$form->{"totop100_unit_$i"}>
327 <input type=hidden name=totop100_sellprice_$i value=$form->{"totop100_sellprice_$i"}>
328 <input type=hidden name=totop100_soldtotal_$i value=$form->{"totop100_soldtotal_$i"}>
329       |;
330     }    #rof
331   }    #fi
332
333   print $totop100;
334
335   print qq|
336 <input class=submit type=submit name=action value="|
337     . $locale->text('TOP100') . qq|">
338
339 </form>
340 </body>
341 </html>
342 |;
343   $lxdebug->leave_sub();
344 }    #end list()
345
346 sub top100 {
347   $lxdebug->enter_sub();
348
349   if ($form->{ndx}) {
350     $form->{ndxs_counter}++;
351
352     if ($form->{ndxs_counter} > 0) {
353
354       my $index = $form->{ndx};
355
356       $form->{"totop100_partnumber_$form->{ndxs_counter}"} = $form->{"new_partnumber_$index"};
357       $form->{"totop100_description_$form->{ndxs_counter}"} = $form->{"new_description_$index"};
358       $form->{"totop100_unit_$form->{ndxs_counter}"} = $form->{"new_unit_$index"};
359       $form->{"totop100_sellprice_$form->{ndxs_counter}"} = $form->{"new_sellprice_$index"};
360       $form->{"totop100_soldtotal_$form->{ndxs_counter}"} = $form->{"new_soldtotal_$index"};
361     }    #fi
362   }    #fi
363   &addtop100();
364   $lxdebug->leave_sub();
365 }    #end top100
366
367 sub addtop100 {
368   $lxdebug->enter_sub();
369
370   my ($revers, $lastsort, $callback, $option, $description, $sameitem,
371       $partnumber, $unit, $sellprice, $soldtotal, $totop100, $onhand, $align);
372   my (@column_index, %column_header, %column_data);
373   my ($totalsellprice, $totallastcost, $totallistprice, $subtotalonhand, $subtotalsellprice, $subtotallastcost, $subtotallistprice);
374
375   $form->{top100}      = "top100";
376   $form->{l_soldtotal} = "Y";
377 #  $form->{soldtotal}   = "soldtotal";
378   $form->{sort}        = "soldtotal";
379   $form->{l_qty}       = "N";
380   $form->{l_linetotal} = "";
381   $form->{revers}      = 1;
382   $form->{number}      = "position";
383   $form->{l_number}    = "Y";
384
385   $totop100 = "";
386
387   $form->{title} = $locale->text('Top 100');
388
389   $revers   = $form->{revers};
390   $lastsort = $form->{lastsort};
391
392   if (($form->{lastsort} eq "") && ($form->{sort} eq undef)) {
393     $form->{revers}   = 0;
394     $form->{lastsort} = "partnumber";
395     $form->{sort}     = "partnumber";
396   }    #fi
397
398   $callback =
399     "$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="
400     . $form->escape($form->{title}, 1);
401
402   # if we have a serialnumber limit search
403   if ($form->{serialnumber} || $form->{l_serialnumber}) {
404     $form->{l_serialnumber} = "Y";
405     unless (   $form->{bought}
406             || $form->{sold}
407             || $form->{rfq}
408             || $form->{quoted}) {
409       $form->{bought} = $form->{sold} = 1;
410     }
411   }
412   IC->all_parts(\%myconfig, \%$form);
413
414   if ($form->{itemstatus} eq 'active') {
415     $option .= $locale->text('Active') . " : ";
416   }
417   if ($form->{itemstatus} eq 'obsolete') {
418     $option .= $locale->text('Obsolete') . " : ";
419   }
420   if ($form->{itemstatus} eq 'orphaned') {
421     $option .= $locale->text('Orphaned') . " : ";
422   }
423   if ($form->{itemstatus} eq 'onhand') {
424     $option .= $locale->text('On Hand') . " : ";
425     $form->{l_onhand} = "Y";
426   }
427   if ($form->{itemstatus} eq 'short') {
428     $option .= $locale->text('Short') . " : ";
429     $form->{l_onhand} = "Y";
430   }
431   if ($form->{onorder}) {
432     $form->{l_ordnumber} = "Y";
433     $callback .= "&onorder=$form->{onorder}";
434     $option   .= $locale->text('On Order') . " : ";
435   }
436   if ($form->{ordered}) {
437     $form->{l_ordnumber} = "Y";
438     $callback .= "&ordered=$form->{ordered}";
439     $option   .= $locale->text('Ordered') . " : ";
440   }
441   if ($form->{rfq}) {
442     $form->{l_quonumber} = "Y";
443     $callback .= "&rfq=$form->{rfq}";
444     $option   .= $locale->text('RFQ') . " : ";
445   }
446   if ($form->{quoted}) {
447     $form->{l_quonumber} = "Y";
448     $callback .= "&quoted=$form->{quoted}";
449     $option   .= $locale->text('Quoted') . " : ";
450   }
451   if ($form->{bought}) {
452     $form->{l_invnumber} = "Y";
453     $callback .= "&bought=$form->{bought}";
454     $option   .= $locale->text('Bought') . " : ";
455   }
456   if ($form->{sold}) {
457     $form->{l_invnumber} = "Y";
458     $callback .= "&sold=$form->{sold}";
459     $option   .= $locale->text('Sold') . " : ";
460   }
461   if (   $form->{bought}
462       || $form->{sold}
463       || $form->{onorder}
464       || $form->{ordered}
465       || $form->{rfq}
466       || $form->{quoted}) {
467
468     $form->{l_lastcost} = "";
469     $form->{l_name}     = "Y";
470     if ($form->{transdatefrom}) {
471       $callback .= "&transdatefrom=$form->{transdatefrom}";
472       $option   .= "\n<br>"
473         . $locale->text('From')
474         . "&nbsp;"
475         . $locale->date(\%myconfig, $form->{transdatefrom}, 1);
476     }
477     if ($form->{transdateto}) {
478       $callback .= "&transdateto=$form->{transdateto}";
479       $option   .= "\n<br>"
480         . $locale->text('To')
481         . "&nbsp;"
482         . $locale->date(\%myconfig, $form->{transdateto}, 1);
483     }
484   }
485
486   $option .= "<br>";
487
488   if ($form->{partnumber}) {
489     $callback .= "&partnumber=$form->{partnumber}";
490     $option   .= $locale->text('Part Number') . qq| : $form->{partnumber}<br>|;
491   }
492   if ($form->{ean}) {
493     $callback .= "&partnumber=$form->{ean}";
494     $option   .= $locale->text('EAN') . qq| : $form->{ean}<br>|;
495   }
496   if ($form->{partsgroup}) {
497     $callback .= "&partsgroup=$form->{partsgroup}";
498     $option   .= $locale->text('Group') . qq| : $form->{partsgroup}<br>|;
499   }
500   if ($form->{serialnumber}) {
501     $callback .= "&serialnumber=$form->{serialnumber}";
502     $option   .= $locale->text('Serial Number') . qq| : $form->{serialnumber}<br>|;
503   }
504   if ($form->{description}) {
505     $callback   .= "&description=$form->{description}";
506     $description = $form->{description};
507     $description =~ s/\n/<br>/g;
508     $option     .= $locale->text('Part Description') . qq| : $form->{description}<br>|;
509   }
510   if ($form->{make}) {
511     $callback .= "&make=$form->{make}";
512     $option   .= $locale->text('Make') . qq| : $form->{make}<br>|;
513   }
514   if ($form->{model}) {
515     $callback .= "&model=$form->{model}";
516     $option   .= $locale->text('Model') . qq| : $form->{model}<br>|;
517   }
518   if ($form->{drawing}) {
519     $callback .= "&drawing=$form->{drawing}";
520     $option   .= $locale->text('Drawing') . qq| : $form->{drawing}<br>|;
521   }
522   if ($form->{microfiche}) {
523     $callback .= "&microfiche=$form->{microfiche}";
524     $option   .= $locale->text('Microfiche') . qq| : $form->{microfiche}<br>|;
525   }
526   if ($form->{l_soldtotal}) {
527     $callback .= "&soldtotal=$form->{soldtotal}";
528     $option   .= $locale->text('soldtotal') . qq| : $form->{soldtotal}<br>|;
529   }
530
531   my @columns = $form->sort_columns(
532     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)
533   );
534
535   if ($form->{l_linetotal}) {
536     $form->{l_onhand} = "Y";
537     $form->{l_linetotalsellprice} = "Y" if $form->{l_sellprice};
538     if ($form->{l_lastcost}) {
539       $form->{l_linetotallastcost} = "Y";
540       if (($form->{searchitems} eq 'assembly') && !$form->{bom}) {
541         $form->{l_linetotallastcost} = "";
542       }
543     }
544     $form->{l_linetotallistprice} = "Y" if $form->{l_listprice};
545   }
546
547   if ($form->{searchitems} eq 'service') {
548
549     # remove bin, weight and rop from list
550     map { $form->{"l_$_"} = "" } qw(bin weight rop);
551
552     $form->{l_onhand} = "";
553
554     # qty is irrelevant unless bought or sold
555     if (   $form->{bought}
556         || $form->{sold}
557         || $form->{onorder}
558         || $form->{ordered}
559         || $form->{rfq}
560         || $form->{quoted}) {
561       $form->{l_onhand} = "Y";
562     } else {
563       $form->{l_linetotalsellprice} = "";
564       $form->{l_linetotallastcost}  = "";
565     }
566   }
567
568   $form->{l_lastcost} = ""
569     if ($form->{searchitems} eq 'assembly' && !$form->{bom});
570
571   foreach my $item (@columns) {
572     if ($form->{"l_$item"} eq "Y") {
573       push @column_index, $item;
574
575       # add column to callback
576       $callback .= "&l_$item=Y";
577     }
578   }
579
580   if ($form->{l_subtotal} eq 'Y') {
581     $callback .= "&l_subtotal=Y";
582   }
583
584   $column_header{number} =
585     qq|<th class=listheading nowrap>| . $locale->text('number') . qq|</th>|;
586   $column_header{partnumber} =
587     qq|<th nowrap><a class=listheading href=$callback&sort=partnumber&revers=$form->{revers}&lastsort=$form->{lastsort}>|
588     . $locale->text('Part Number')
589     . qq|</a></th>|;
590   $column_header{description} =
591     qq|<th nowrap><a class=listheading href=$callback&sort=description&revers=$form->{revers}&lastsort=$form->{lastsort}>|
592     . $locale->text('Part Description')
593     . qq|</a></th>|;
594   $column_header{partsgroup} =
595       qq|<th nowrap><a class=listheading href=$callback&sort=partsgroup>|
596     . $locale->text('Group')
597     . qq|</a></th>|;
598   $column_header{bin} =
599       qq|<th><a class=listheading href=$callback&sort=bin>|
600     . $locale->text('Bin')
601     . qq|</a></th>|;
602   $column_header{priceupdate} =
603       qq|<th nowrap><a class=listheading href=$callback&sort=priceupdate>|
604     . $locale->text('Updated')
605     . qq|</a></th>|;
606   $column_header{onhand} =
607     qq|<th nowrap><a  class=listheading href=$callback&sort=onhand&revers=$form->{revers}&lastsort=$form->{lastsort}>|
608     . $locale->text('Qty')
609     . qq|</th>|;
610   $column_header{unit} =
611     qq|<th class=listheading nowrap>| . $locale->text('Unit') . qq|</th>|;
612   $column_header{listprice} =
613       qq|<th class=listheading nowrap>|
614     . $locale->text('List Price')
615     . qq|</th>|;
616   $column_header{lastcost} =
617     qq|<th class=listheading nowrap>| . $locale->text('Last Cost') . qq|</th>|;
618   $column_header{rop} =
619     qq|<th class=listheading nowrap>| . $locale->text('ROP') . qq|</th>|;
620   $column_header{weight} =
621     qq|<th class=listheading nowrap>| . $locale->text('Weight') . qq|</th>|;
622
623   $column_header{invnumber} =
624       qq|<th nowrap><a class=listheading href=$callback&sort=invnumber>|
625     . $locale->text('Invoice Number')
626     . qq|</a></th>|;
627   $column_header{ordnumber} =
628       qq|<th nowrap><a class=listheading href=$callback&sort=ordnumber>|
629     . $locale->text('Order Number')
630     . qq|</a></th>|;
631   $column_header{quonumber} =
632       qq|<th nowrap><a class=listheading href=$callback&sort=quonumber>|
633     . $locale->text('Quotation')
634     . qq|</a></th>|;
635
636   $column_header{name} =
637       qq|<th nowrap><a class=listheading href=$callback&sort=name>|
638     . $locale->text('Name')
639     . qq|</a></th>|;
640
641   $column_header{sellprice} =
642       qq|<th class=listheading nowrap>|
643     . $locale->text('Sell Price')
644     . qq|</th>|;
645   $column_header{linetotalsellprice} =
646     qq|<th class=listheading nowrap>| . $locale->text('Extended') . qq|</th>|;
647   $column_header{linetotallastcost} =
648     qq|<th class=listheading nowrap>| . $locale->text('Extended') . qq|</th>|;
649   $column_header{linetotallistprice} =
650     qq|<th class=listheading nowrap>| . $locale->text('Extended') . qq|</th>|;
651
652   $column_header{image} =
653     qq|<th class=listheading nowrap>| . $locale->text('Image') . qq|</a></th>|;
654   $column_header{drawing} =
655       qq|<th nowrap><a class=listheading href=$callback&sort=drawing>|
656     . $locale->text('Drawing')
657     . qq|</a></th>|;
658   $column_header{microfiche} =
659       qq|<th nowrap><a class=listheading href=$callback&sort=microfiche>|
660     . $locale->text('Microfiche')
661     . qq|</a></th>|;
662
663   $column_header{serialnumber} =
664       qq|<th nowrap><a class=listheading href=$callback&sort=serialnumber>|
665     . $locale->text('Serial Number')
666     . qq|</a></th>|;
667   $column_header{soldtotal} =
668     qq|<th nowrap><a class=listheading href=$callback&sort=soldtotal&revers=$form->{revers}&lastsort=$form->{lastsort}>|
669     . $locale->text('soldtotal')
670     . qq|</a></th>|;
671
672   $form->header;
673   my $colspan = $#column_index + 1;
674
675   print qq|
676 <body>
677
678 <table width=100%>
679   <tr>
680     <th class=listtop colspan=$colspan>$form->{title}</th>
681   </tr>
682   <tr height="5"></tr>
683
684   <tr><td colspan=$colspan>$option</td></tr>
685
686   <tr class=listheading>
687 |;
688
689   map { print "\n$column_header{$_}" } @column_index;
690
691   print qq|
692   </tr>
693   |;
694
695   # add order to callback
696   $form->{callback} = $callback .= "&sort=$form->{sort}";
697
698   # escape callback for href
699   $callback = $form->escape($callback);
700
701   if (@{ $form->{parts} }) {
702     $sameitem = $form->{parts}->[0]->{ $form->{sort} };
703   }
704
705   # insert numbers for top100
706   my $j = 0;
707   foreach my $ref (@{ $form->{parts} }) {
708     $j++;
709     $ref->{number} = $j;
710   }
711
712   # if avaible -> insert choice here
713   if (($form->{ndxs_counter}) > 0) {
714     for (my $i = 1; ($i < $form->{ndxs_counter} + 1); $i++) {
715       $partnumber  = $form->{"totop100_partnumber_$i"};
716       $description = $form->{"totop100_description_$i"};
717       $unit        = $form->{"totop100_unit_$i"};
718       $sellprice   = $form->{"totop100_sellprice_$i"};
719       $soldtotal   = $form->{"totop100_soldtotal_$i"};
720
721       $totop100 .= qq|
722 <input type=hidden name=totop100_partnumber_$i value=$form->{"totop100_partnumber_$i"}>
723 <input type=hidden name=totop100_description_$i value=$form->{"totop100_description_$i"}>
724 <input type=hidden name=totop100_unit_$i value=$form->{"totop100_unit_$i"}>
725 <input type=hidden name=totop100_sellprice_$i value=$form->{"totop100_sellprice_$i"}>
726 <input type=hidden name=totop100_soldtotal_$i value=$form->{"totop100_soldtotal_$i"}>
727       |;
728
729       # insert into list
730       push @{ $form->{parts} },
731         { number      => "",
732           partnumber  => "$partnumber",
733           description => "$description",
734           unit        => "$unit",
735           sellprice   => "$sellprice",
736           soldtotal   => "$soldtotal" };
737     }    #rof
738   }    #fi
739        # build data for columns
740   foreach my $ref (@{ $form->{parts} }) {
741     my $i = 0;
742
743     if ($form->{l_subtotal} eq 'Y' && !$ref->{assemblyitem}) {
744       if ($sameitem ne $ref->{ $form->{sort} }) {
745         &parts_subtotal;
746         $sameitem = $ref->{ $form->{sort} };
747       }
748     }
749
750     $ref->{exchangerate} = 1 unless $ref->{exchangerate};
751     $ref->{sellprice} *= $ref->{exchangerate};
752     $ref->{listprice} *= $ref->{exchangerate};
753     $ref->{lastcost}  *= $ref->{exchangerate};
754
755     # use this for assemblies
756     $onhand = $ref->{onhand};
757
758     $align = "left";
759     if ($ref->{assemblyitem}) {
760       $align = "right";
761       $onhand = 0 if ($form->{sold});
762     }
763
764     $ref->{description} =~ s/\n/<br>/g;
765
766     $column_data{number} =
767         "<td align=right>"
768       . $form->format_amount(\%myconfig, $ref->{number})
769       . "</td>";
770     $column_data{partnumber} =
771       "<td align=$align>$ref->{partnumber}&nbsp;</a></td>";
772     $column_data{description} = "<td>$ref->{description}&nbsp;</td>";
773     $column_data{partsgroup}  = "<td>$ref->{partsgroup}&nbsp;</td>";
774
775     $column_data{onhand} =
776         "<td align=right>"
777       . $form->format_amount(\%myconfig, $ref->{onhand})
778       . "</td>";
779     $column_data{sellprice} =
780         "<td align=right>"
781       . $form->format_amount(\%myconfig, $ref->{sellprice})
782       . "</td>";
783     $column_data{listprice} =
784         "<td align=right>"
785       . $form->format_amount(\%myconfig, $ref->{listprice})
786       . "</td>";
787     $column_data{lastcost} =
788         "<td align=right>"
789       . $form->format_amount(\%myconfig, $ref->{lastcost})
790       . "</td>";
791
792     $column_data{linetotalsellprice} = "<td align=right>"
793       . $form->format_amount(\%myconfig, $ref->{onhand} * $ref->{sellprice}, 2)
794       . "</td>";
795     $column_data{linetotallastcost} = "<td align=right>"
796       . $form->format_amount(\%myconfig, $ref->{onhand} * $ref->{lastcost}, 2)
797       . "</td>";
798     $column_data{linetotallistprice} = "<td align=right>"
799       . $form->format_amount(\%myconfig, $ref->{onhand} * $ref->{listprice}, 2)
800       . "</td>";
801
802     if (!$ref->{assemblyitem}) {
803       $totalsellprice += $onhand * $ref->{sellprice};
804       $totallastcost  += $onhand * $ref->{lastcost};
805       $totallistprice += $onhand * $ref->{listprice};
806
807       $subtotalonhand    += $onhand;
808       $subtotalsellprice += $onhand * $ref->{sellprice};
809       $subtotallastcost  += $onhand * $ref->{lastcost};
810       $subtotallistprice += $onhand * $ref->{listprice};
811     }
812
813     $column_data{rop} =
814       "<td align=right>"
815       . $form->format_amount(\%myconfig, $ref->{rop}) . "</td>";
816     $column_data{weight} =
817         "<td align=right>"
818       . $form->format_amount(\%myconfig, $ref->{weight})
819       . "</td>";
820     $column_data{unit}        = "<td>$ref->{unit}&nbsp;</td>";
821     $column_data{bin}         = "<td>$ref->{bin}&nbsp;</td>";
822     $column_data{priceupdate} = "<td>$ref->{priceupdate}&nbsp;</td>";
823
824     $column_data{invnumber} =
825       ($ref->{module} ne 'oe')
826       ? "<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>"
827       : "<td>$ref->{invnumber}</td>";
828     $column_data{ordnumber} =
829       ($ref->{module} eq 'oe')
830       ? "<td><a href=$ref->{module}.pl?action=edit&type=$ref->{type}&id=$ref->{trans_id}&login=$form->{login}&password=$form->{password}&callback=$callback>$ref->{ordnumber}</a></td>"
831       : "<td>$ref->{ordnumber}</td>";
832     $column_data{quonumber} =
833       ($ref->{module} eq 'oe' && !$ref->{ordnumber})
834       ? "<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>"
835       : "<td>$ref->{quonumber}</td>";
836
837     $column_data{name} = "<td>$ref->{name}</td>";
838
839     $column_data{image} =
840       ($ref->{image})
841       ? "<td><a href=$ref->{image}><img src=$ref->{image} height=32 border=0></a></td>"
842       : "<td>&nbsp;</td>";
843     $column_data{drawing} =
844       ($ref->{drawing})
845       ? "<td><a href=$ref->{drawing}>$ref->{drawing}</a></td>"
846       : "<td>&nbsp;</td>";
847     $column_data{microfiche} =
848       ($ref->{microfiche})
849       ? "<td><a href=$ref->{microfiche}>$ref->{microfiche}</a></td>"
850       : "<td>&nbsp;</td>";
851
852     $column_data{serialnumber} = "<td>$ref->{serialnumber}</td>";
853
854     $column_data{soldtotal} = "<td  align=right>$ref->{soldtotal}</td>";
855
856     $i++;
857     $i %= 2;
858     print "<tr class=listrow$i>";
859
860     map { print "\n$column_data{$_}" } @column_index;
861
862     print qq|
863     </tr>
864 |;
865   }
866
867   if ($form->{l_subtotal} eq 'Y') {
868     &parts_subtotal;
869   }    #fi
870
871   if ($form->{"l_linetotal"}) {
872     map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
873     $column_data{linetotalsellprice} =
874         "<th class=listtotal align=right>"
875       . $form->format_amount(\%myconfig, $totalsellprice, 2)
876       . "</th>";
877     $column_data{linetotallastcost} =
878         "<th class=listtotal align=right>"
879       . $form->format_amount(\%myconfig, $totallastcost, 2)
880       . "</th>";
881     $column_data{linetotallistprice} =
882         "<th class=listtotal align=right>"
883       . $form->format_amount(\%myconfig, $totallistprice, 2)
884       . "</th>";
885
886     print "<tr class=listtotal>";
887
888     map { print "\n$column_data{$_}" } @column_index;
889
890     print qq|</tr>
891     |;
892   }
893
894   print qq|
895   <tr><td colspan=$colspan><hr size=3 noshade></td></tr>
896 </table>
897
898 |;
899
900   print qq|
901
902 <br>
903
904 <form method=post action=$form->{script}>
905
906 <input type=hidden name=login value=$form->{login}>
907 <input type=hidden name=password value=$form->{password}>
908
909 <input type=hidden name=itemstatus value="$form->{itemstatus}">
910 <input type=hidden name=l_linetotal value="$form->{l_linetotal}">
911 <input type=hidden name=l_partnumber value="$form->{l_partnumber}">
912 <input type=hidden name=l_description value="$form->{l_description}">
913 <input type=hidden name=l_onhand value="$form->{l_onhand}">
914 <input type=hidden name=l_unit value="$form->{l_unit}">
915 <input type=hidden name=l_sellprice value="$form->{l_sellprice}">
916 <input type=hidden name=l_linetotalsellprice value="$form->{l_linetotalsellprice}">
917 <input type=hidden name=sort value="$form->{sort}">
918 <input type=hidden name=revers value="$form->{revers}">
919 <input type=hidden name=lastsort value="$form->{lastsort}">
920 <input type=hidden name=parts value="$form->{parts}">
921
922 <input type=hidden name=bom value="$form->{bom}">
923 <input type=hidden name=titel value="$form->{titel}">
924 <input type=hidden name=searchitems value="$form->{searchitems}">|;
925
926   print $totop100;
927
928   print qq|
929     <input type=hidden name=ndxs_counter value="$form->{ndxs_counter}">
930
931     <input class=submit type=submit name=action value="|
932     . $locale->text('choice') . qq|">
933
934   </form>
935
936 </body>
937 </html>
938 |;
939
940   $lxdebug->leave_sub();
941 }    # end addtop100
942
943 #
944 # Report for Wares.
945 # Warning, deep magic ahead.
946 # This function parses the requested details, sanity checks them, and converts them into a format thats usable for IC->all_parts
947 #
948 # flags coming from the form:
949 # hardcoded:
950 #  searchitems=part revers=0 lastsort=''
951 #
952 # filter:
953 # partnumber ean description partsgroup serialnumber make model drawing microfiche
954 # transdatefrom transdateto
955 #
956 # radio:
957 #  itemstatus = active | onhand | short | obsolete | orphaned
958 #  action     = continue | top100
959 #
960 # checkboxes:
961 #  bought sold onorder ordered rfq quoted
962 #  l_partnumber l_description l_serialnumber l_unit l_listprice l_sellprice l_lastcost
963 #  l_linetotal l_priceupdate l_bin l_rop l_weight l_image l_drawing l_microfiche
964 #  l_partsgroup l_subtotal l_soldtotal l_deliverydate
965 #
966 # hiddens: 
967 #  nextsub login password revers lastsort sort ndxs_counter
968 #
969 sub generate_report {
970   $lxdebug->enter_sub();
971
972   my ($revers, $lastsort, $description);
973
974   $form->{title} = (ucfirst $form->{searchitems}) . "s";
975   $form->{title} = $locale->text($form->{title});
976
977   my $revers     = $form->{revers};
978   my $lastsort   = $form->{lastsort};
979
980   # sorting and direction of sorting
981   # ToDO: change this to the simpler field+direction method
982   if (($form->{lastsort} eq "") && ($form->{sort} eq undef)) {
983     $form->{revers}   = 0;
984     $form->{lastsort} = "partnumber";
985     $form->{sort}     = "partnumber";
986   } else {
987     if ($form->{lastsort} eq $form->{sort}) {
988       $form->{revers} = 1 - $form->{revers};
989     } else {
990       $form->{revers} = 0;
991       $form->{lastsort} = $form->{sort};
992     }    #fi
993   }    #fi
994
995   # special case if we have a serialnumber limit search
996   # serialnumbers are only given in invoices and orders, 
997   # so they can only pop up in bought, sold, rfq, and quoted stuff
998   $form->{no_sn_joins} = 'Y' if (   !$form->{bought} && !$form->{sold} 
999                                  && !$form->{rfq}    && !$form->{quoted} 
1000                                  && ($form->{l_serialnumber} || $form->{serialnumber}));
1001
1002   # special case for any checkbox of bought | sold | onorder | ordered | rfq | quoted.
1003   # if any of these are ticked the behavior changes slightly for lastcost
1004   # since all those are aggregation checks for the legder tables this is an internal switch
1005   # refered to as ledgerchecks
1006   $form->{ledgerchecks} = 'Y' if (   $form->{bought} || $form->{sold} || $form->{onorder}
1007                                   || $form->{ordered} || $form->{rfq} || $form->{quoted});
1008
1009   # if something should be activated if something else is active, enter it here
1010   my %dependencies = (
1011     onhand       => [ qw(l_onhand) ],
1012     short        => [ qw(l_onhand) ],
1013     onorder      => [ qw(l_ordnumber) ],
1014     ordered      => [ qw(l_ordnumber) ],
1015     rfq          => [ qw(l_quonumber) ],
1016     quoted       => [ qw(l_quonumber) ],
1017     bought       => [ qw(l_invnumber) ],
1018     sold         => [ qw(l_invnumber) ],
1019     ledgerchecks => [ qw(l_name) ],
1020     serialnumber => [ qw(l_serialnumber) ],
1021     no_sn_joins  => [ qw(bought sold) ],
1022   );
1023
1024   # these strings get displayed at the top of the results to indicate the user which switches were used
1025   my %optiontexts = (
1026     active        => $locale->text('Active'),
1027     obsolete      => $locale->text('Obsolete'),
1028     orphaned      => $locale->text('Orphaned'),
1029     onhand        => $locale->text('On Hand'),
1030     short         => $locale->text('Short'),
1031     onorder       => $locale->text('On Order'),
1032     ordered       => $locale->text('Ordered'),
1033     rfq           => $locale->text('RFQ'),
1034     quoted        => $locale->text('Quoted'),
1035     bought        => $locale->text('Bought'),
1036     sold          => $locale->text('Sold'),
1037     transdatefrom => $locale->text('From')       . " " . $locale->date(\%myconfig, $form->{transdatefrom}, 1),
1038     transdateto   => $locale->text('To (time)')  . " " . $locale->date(\%myconfig, $form->{transdateto}, 1),
1039     partnumber    => $locale->text('Part Number')      . ": '$form->{partnumber}'",
1040     partsgroup    => $locale->text('Group')            . ": '$form->{partsgroup}'",
1041     serialnumber  => $locale->text('Serial Number')    . ": '$form->{serialnumber}'",
1042     description   => $locale->text('Part Description') . ": '$form->{description}'",
1043     make          => $locale->text('Make')             . ": '$form->{make}'",
1044     model         => $locale->text('Model')            . ": '$form->{model}'",
1045     drawing       => $locale->text('Drawing')          . ": '$form->{drawing}'",
1046     microfiche    => $locale->text('Microfiche')       . ": '$form->{microfiche}'",
1047     l_soldtotal   => $locale->text('soldtotal'),
1048   );
1049
1050   my @itemstatus_keys = qw(active obsolete orphaned onhand short);
1051   my @callback_keys   = qw(onorder ordered rfq quoted bought sold partnumber partsgroup serialnumber description make model
1052                            drawing microfiche l_soldtotal l_deliverydate transdatefrom transdateto ean);
1053
1054   # calculate dependencies
1055   for (@itemstatus_keys, @callback_keys) {
1056     next if ($form->{itemstatus} ne $_ && !$form->{$_});
1057     map { $form->{$_} = 'Y' } @{ $dependencies{$_} } if $dependencies{$_};
1058   }
1059
1060   # generate callback and optionstrings
1061   my @options;
1062   for my  $key (@itemstatus_keys, @callback_keys) { 
1063     next if ($form->{itemstatus} ne $key && !$form->{$key});
1064     push @options, $optiontexts{$key};
1065   }
1066
1067   # special case for lastcost
1068   $form->{l_lastcost} = "" if $form->{ledgerchecks};
1069
1070   if ($form->{description}) {
1071     $description = $form->{description};
1072     $description =~ s/\n/<br>/g;
1073   }
1074
1075   if ($form->{l_linetotal}) {
1076     $form->{l_onhand} = "Y";
1077     $form->{l_linetotalsellprice} = "Y" if $form->{l_sellprice};
1078     if ($form->{l_lastcost}) {
1079       $form->{l_linetotallastcost} = "Y";
1080       if (($form->{searchitems} eq 'assembly') && !$form->{bom}) {
1081         $form->{l_linetotallastcost} = "";
1082       }
1083     }
1084     $form->{l_linetotallistprice} = "Y" if $form->{l_listprice};
1085   }
1086
1087   if ($form->{searchitems} eq 'service') {
1088
1089     # remove bin, weight and rop from list
1090     map { $form->{"l_$_"} = "" } qw(bin weight rop);
1091
1092     $form->{l_onhand} = "";
1093
1094     # qty is irrelevant unless bought or sold
1095     if (   $form->{bought}
1096         || $form->{sold}
1097         || $form->{onorder}
1098         || $form->{ordered}
1099         || $form->{rfq}
1100         || $form->{quoted}) {
1101       $form->{l_onhand} = "Y";
1102     } else {
1103       $form->{l_linetotalsellprice} = "";
1104       $form->{l_linetotallastcost}  = "";
1105     }
1106   }
1107
1108   $form->{l_lastcost} = "" if ($form->{searchitems} eq 'assembly' && !$form->{bom});
1109
1110   IC->all_parts(\%myconfig, \%$form);
1111
1112   my @columns =
1113     qw(partnumber description partsgroup bin onhand rop unit listprice linetotallistprice sellprice linetotalsellprice lastcost linetotallastcost
1114        priceupdate weight image drawing microfiche invnumber ordnumber quonumber name serialnumber soldtotal deliverydate);
1115
1116   my %column_defs = (
1117     'bin'                => { 'text' => $locale->text('Bin'), },
1118     'deliverydate'       => { 'text' => $locale->text('deliverydate'), },
1119     'description'        => { 'text' => $locale->text('Part Description'), },
1120     'drawing'            => { 'text' => $locale->text('Drawing'), },
1121     'image'              => { 'text' => $locale->text('Image'), },
1122     'invnumber'          => { 'text' => $locale->text('Invoice Number'), },
1123     'lastcost'           => { 'text' => $locale->text('Last Cost'), },
1124     'linetotallastcost'  => { 'text' => $locale->text('Extended'), },
1125     'linetotallistprice' => { 'text' => $locale->text('Extended'), },
1126     'linetotalsellprice' => { 'text' => $locale->text('Extended'), },
1127     'listprice'          => { 'text' => $locale->text('List Price'), },
1128     'microfiche'         => { 'text' => $locale->text('Microfiche'), },
1129     'name'               => { 'text' => $locale->text('Name'), },
1130     'onhand'             => { 'text' => $locale->text('Qty'), },
1131     'ordnumber'          => { 'text' => $locale->text('Order Number'), },
1132     'partnumber'         => { 'text' => $locale->text('Part Number'), },
1133     'partsgroup'         => { 'text' => $locale->text('Group'), },
1134     'priceupdate'        => { 'text' => $locale->text('Updated'), },
1135     'quonumber'          => { 'text' => $locale->text('Quotation'), },
1136     'rop'                => { 'text' => $locale->text('ROP'), },
1137     'sellprice'          => { 'text' => $locale->text('Sell Price'), },
1138     'serialnumber'       => { 'text' => $locale->text('Serial Number'), },
1139     'soldtotal'          => { 'text' => $locale->text('soldtotal'), },
1140     'unit'               => { 'text' => $locale->text('Unit'), },
1141     'weight'             => { 'text' => $locale->text('Weight'), },
1142   );
1143
1144   map { $column_defs{$_}->{visible} = $form->{"l_$_"} ? 1 : 0 } @columns;
1145   map { $column_defs{$_}->{align}   = 'right' } qw(onhand sellprice listprice lastcost linetotalsellprice linetotallastcost linetotallistprice rop weight soldtotal);
1146
1147   my @hidden_variables = (qw(l_subtotal l_linetotal searchitems itemstatus bom), @itemstatus_keys, @callback_keys, map { "l_$_" } @columns);
1148   my $callback         = build_std_url('action=generate_report', grep { $form->{$_} } @hidden_variables);
1149
1150   my @sort_full        = qw(partnumber description onhand soldtotal deliverydate);
1151   my @sort_no_revers   = qw(partsgroup bin priceupdate invnumber ordnumber quonumber name image drawing serialnumber);
1152
1153   foreach my $col (@sort_full) {
1154     $column_defs{$col}->{link} = join '&', $callback, "sort=$col", map { "$_=" . E($form->{$_}) } qw(revers lastsort);
1155   }
1156   map { $column_defs{$_}->{link} = "${callback}&sort=$_" } @sort_no_revers;
1157
1158   # add order to callback
1159   $form->{callback} = join '&', ($callback, map { "${_}=" . E($form->{$_}) } qw(sort revers));
1160
1161   my $report = SL::ReportGenerator->new(\%myconfig, $form);
1162
1163   my %attachment_basenames = (
1164     'part'     => $locale->text('part_list'),
1165     'service'  => $locale->text('service_list'),
1166     'assembly' => $locale->text('assembly_list'),
1167   );
1168
1169   $report->set_options('top_info_text'         => $locale->text('Options') . ': ' . join(', ', grep $_, @options),
1170                        'raw_bottom_info_text'  => $form->parse_html_template('ic/generate_report_bottom'),
1171                        'output_format'         => 'HTML',
1172                        'title'                 => $form->{title},
1173                        'attachment_basename'   => $attachment_basenames{$form->{searchitems}} . strftime('_%Y%m%d', localtime time),
1174   );
1175   $report->set_options_from_form();
1176
1177   $report->set_columns(%column_defs);
1178   $report->set_column_order(@columns);
1179
1180   $report->set_export_options('generate_report', @hidden_variables, qw(sort revers));
1181
1182   $report->set_sort_indicator($form->{sort}, $form->{revers} ? 0 : 1);
1183
1184   my @subtotal_columns = qw(sellprice listprice lastcost);
1185   my %subtotals = map { $_ => 0 } ('onhand', @subtotal_columns);
1186   my %totals    = map { $_ => 0 } @subtotal_columns;
1187   my $idx       = 0;
1188   my $same_item = $form->{parts}[0]{ $form->{sort} } if (scalar @{ $form->{parts} });
1189
1190   # postprocess parts
1191   foreach my $ref (@{ $form->{parts} }) {
1192
1193     # fresh row, for inserting later
1194     my $row = { map { $_ => { 'data' => $ref->{$_} } } @columns };
1195
1196     $ref->{exchangerate} ||= 1;
1197     $ref->{price_factor} ||= 1;
1198     $ref->{sellprice}     *= $ref->{exchangerate} / $ref->{price_factor};
1199     $ref->{listprice}     *= $ref->{exchangerate} / $ref->{price_factor};
1200     $ref->{lastcost}      *= $ref->{exchangerate} / $ref->{price_factor};
1201
1202     # use this for assemblies
1203     my $onhand = $ref->{onhand};
1204
1205     if ($ref->{assemblyitem}) {
1206       $row->{partnumber}{align}   = 'right';
1207       $row->{onhand}{data}        = 0;
1208       $onhand                     = 0 if ($form->{sold});
1209     }
1210
1211     my $edit_link               = build_std_url('action=edit', 'id=' . E($ref->{id}), 'callback');
1212     $row->{partnumber}->{link}  = $edit_link;
1213     $row->{description}->{link} = $edit_link;
1214
1215     foreach (qw(sellprice listprice lastcost)) {
1216       $row->{$_}{data}            = $form->format_amount(\%myconfig, $ref->{$_}, -2);
1217       $row->{"linetotal$_"}{data} = $form->format_amount(\%myconfig, $ref->{onhand} * $ref->{$_}, 2);
1218     }
1219
1220     map { $row->{$_}{data} = $form->format_amount(\%myconfig, $ref->{$_}); } qw(onhand rop weight soldtotal);
1221
1222     if (!$ref->{assemblyitem}) {
1223       foreach my $col (@subtotal_columns) {
1224         $totals{$col}    += $onhand * $ref->{$col};
1225         $subtotals{$col} += $onhand * $ref->{$col};
1226       }
1227
1228       $subtotals{onhand} += $onhand;
1229     }
1230
1231     # set module stuff
1232     if ($ref->{module} eq 'oe') {
1233       my $edit_oe_link = build_std_url("script=oe.pl", 'action=edit', 'type=' . E($ref->{type}), 'id=' . E($ref->{trans_id}), 'callback');
1234       $row->{ordnumber}{link} = $edit_oe_link;
1235       $row->{quonumber}{link} = $edit_oe_link if (!$ref->{ordnumber});
1236
1237     } else {
1238       $row->{invnumber}{link} = build_std_url("script=$ref->{module}.pl", 'action=edit', 'type=invoice', 'id=' . E($ref->{trans_id}), 'callback');
1239     }
1240
1241     # set properties of images
1242     if ($ref->{image} && (lc $report->{options}->{output_format} eq 'html')) {
1243       $row->{image}{data}     = '';
1244       $row->{image}{raw_data} = '<a href="' . H($ref->{image}) . '"><img src="' . H($ref->{image}) . '" height="32" border="0"></a>';
1245     }
1246     map { $row->{$_}{link} = $ref->{$_} } qw(drawing microfiche);
1247
1248     $report->add_data($row);
1249
1250     my $next_ref = $form->{parts}[$idx + 1];
1251
1252     # insert subtotal rows
1253     if (($form->{l_subtotal} eq 'Y') &&
1254         (!$next_ref ||
1255          (!$next_ref->{assemblyitem} && ($same_item ne $next_ref->{ $form->{sort} })))) {
1256       my $row = { map { $_ => { 'class' => 'listsubtotal', } } @columns };
1257
1258       if (($form->{searchitems} ne 'assembly') || !$form->{bom}) {
1259         $row->{onhand}->{data} = $form->format_amount(\%myconfig, $subtotals{onhand});
1260       }
1261
1262       map { $row->{"linetotal$_"}->{data} = $form->format_amount(\%myconfig, $subtotals{$_}, 2) } @subtotal_columns;
1263       map { $subtotals{$_} = 0 } ('onhand', @subtotal_columns);
1264
1265       $report->add_data($row);
1266
1267       $same_item = $next_ref->{ $form->{sort} };
1268     }
1269
1270     $idx++;
1271   }
1272
1273   if ($form->{"l_linetotal"}) {
1274     my $row = { map { $_ => { 'class' => 'listtotal', } } @columns };
1275
1276     map { $row->{"linetotal$_"}->{data} = $form->format_amount(\%myconfig, $totals{$_}, 2) } @subtotal_columns;
1277
1278     $report->add_separator();
1279     $report->add_data($row);
1280   }
1281
1282   $report->generate_with_headers();
1283
1284   $lxdebug->leave_sub();
1285 }    #end generate_report
1286
1287 sub parts_subtotal {
1288   $lxdebug->enter_sub();
1289   
1290   # imports
1291   our (%column_data, @column_index);
1292   our ($subtotalonhand, $totalsellprice, $totallastcost, $totallistprice, $subtotalsellprice, $subtotallastcost, $subtotallistprice);
1293
1294   map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
1295   $subtotalonhand = 0 if ($form->{searchitems} eq 'assembly' && $form->{bom});
1296
1297   $column_data{onhand} =
1298       "<th class=listsubtotal align=right>"
1299     . $form->format_amount(\%myconfig, $subtotalonhand)
1300     . "</th>";
1301
1302   $column_data{linetotalsellprice} =
1303       "<th class=listsubtotal align=right>"
1304     . $form->format_amount(\%myconfig, $subtotalsellprice, 2)
1305     . "</th>";
1306   $column_data{linetotallistprice} =
1307       "<th class=listsubtotal align=right>"
1308     . $form->format_amount(\%myconfig, $subtotallistprice, 2)
1309     . "</th>";
1310   $column_data{linetotallastcost} =
1311       "<th class=listsubtotal align=right>"
1312     . $form->format_amount(\%myconfig, $subtotallastcost, 2)
1313     . "</th>";
1314
1315   $subtotalonhand    = 0;
1316   $subtotalsellprice = 0;
1317   $subtotallistprice = 0;
1318   $subtotallastcost  = 0;
1319
1320   print "<tr class=listsubtotal>";
1321
1322   map { print "\n$column_data{$_}" } @column_index;
1323
1324   print qq|
1325   </tr>
1326 |;
1327
1328   $lxdebug->leave_sub();
1329 }
1330
1331 sub edit {
1332   $lxdebug->enter_sub();
1333   # show history button
1334   $form->{javascript} = qq|<script type="text/javascript" src="js/show_history.js"></script>|;
1335   #/show hhistory button
1336   IC->get_part(\%myconfig, \%$form);
1337
1338   $form->{"original_partnumber"} = $form->{"partnumber"};
1339
1340   $form->{title} = $locale->text('Edit ' . ucfirst $form->{item});
1341
1342   &link_part;
1343   &display_form;
1344
1345   $lxdebug->leave_sub();
1346 }
1347
1348 sub link_part {
1349   $lxdebug->enter_sub();
1350
1351   IC->create_links("IC", \%myconfig, \%$form);
1352
1353   # currencies
1354   map({ $form->{selectcurrency} .= "<option>$_\n" }
1355       split(/:/, $form->{currencies}));
1356
1357   # parts and assemblies have the same links
1358   my $item = $form->{item};
1359   if ($form->{item} eq 'assembly') {
1360     $item = 'part';
1361   }
1362
1363   # build the popup menus
1364   $form->{taxaccounts} = "";
1365   foreach my $key (keys %{ $form->{IC_links} }) {
1366     foreach my $ref (@{ $form->{IC_links}{$key} }) {
1367
1368       # if this is a tax field
1369       if ($key =~ /IC_tax/) {
1370         if ($key =~ /\Q$item\E/) {
1371           $form->{taxaccounts} .= "$ref->{accno} ";
1372           $form->{"IC_tax_$ref->{accno}_description"} =
1373             "$ref->{accno}--$ref->{description}";
1374
1375           if ($form->{id}) {
1376             if ($form->{amount}{ $ref->{accno} }) {
1377               $form->{"IC_tax_$ref->{accno}"} = "checked";
1378             }
1379           } else {
1380             $form->{"IC_tax_$ref->{accno}"} = "checked";
1381           }
1382         }
1383       } else {
1384
1385         $form->{"select$key"} .=
1386           "<option $ref->{selected}>$ref->{accno}--$ref->{description}\n";
1387         if ($form->{amount}{$key} eq $ref->{accno}) {
1388           $form->{$key} = "$ref->{accno}--$ref->{description}";
1389         }
1390
1391       }
1392     }
1393   }
1394   chop $form->{taxaccounts};
1395
1396   if (($form->{item} eq "part") || ($form->{item} eq "assembly")) {
1397     $form->{selectIC_income}  = $form->{selectIC_sale};
1398     $form->{selectIC_expense} = $form->{selectIC_cogs};
1399     $form->{IC_income}        = $form->{IC_sale};
1400     $form->{IC_expense}       = $form->{IC_cogs};
1401   }
1402
1403   delete $form->{IC_links};
1404   delete $form->{amount};
1405
1406   $form->get_partsgroup(\%myconfig, { all => 1 });
1407
1408   $form->{partsgroup} = "$form->{partsgroup}--$form->{partsgroup_id}";
1409
1410   if (@{ $form->{all_partsgroup} }) {
1411     $form->{selectpartsgroup} = qq|<option>\n|;
1412     map { $form->{selectpartsgroup} .= qq|<option value="$_->{partsgroup}--$_->{id}">$_->{partsgroup}\n| } @{ $form->{all_partsgroup} };
1413   }
1414
1415   if ($form->{item} eq 'assembly') {
1416
1417     foreach my $i (1 .. $form->{assembly_rows}) {
1418       if ($form->{"partsgroup_id_$i"}) {
1419         $form->{"partsgroup_$i"} =
1420           qq|$form->{"partsgroup_$i"}--$form->{"partsgroup_id_$i"}|;
1421       }
1422     }
1423     $form->get_partsgroup(\%myconfig);
1424
1425     if (@{ $form->{all_partsgroup} }) {
1426       $form->{selectassemblypartsgroup} = qq|<option>\n|;
1427
1428       map {
1429         $form->{selectassemblypartsgroup} .=
1430           qq|<option value="$_->{partsgroup}--$_->{id}">$_->{partsgroup}\n|
1431       } @{ $form->{all_partsgroup} };
1432     }
1433   }
1434   $lxdebug->leave_sub();
1435 }
1436
1437 sub form_header {
1438   $lxdebug->enter_sub();
1439
1440   $form->{eur}              = $eur; # config dumps into namespace - yuck
1441   $form->{pg_keys}          = sub { "$_[0]->{partsgroup}--$_[0]->{id}" };
1442   $form->{description_area} = ($form->{rows} = $form->numtextrows($form->{description}, 40)) > 1;
1443
1444   map { $form->{"is_$_"}  = ($form->{item} eq $_) } qw(part service assembly);
1445   map { $form->{$_}       =~ s/"/&quot;/g;        } qw(unit);
1446  
1447   $form->get_lists('price_factors' => 'ALL_PRICE_FACTORS');
1448
1449   $rows = 4 if $rows = $form->numtextrows($form->{notes}, 40) < 2;
1450   $form->{notes_rows} = $rows;
1451
1452   IC->retrieve_buchungsgruppen(\%myconfig, $form);
1453   @{ $form->{BUCHUNGSGRUPPEN} } = grep { $_->{id} eq $form->{buchungsgruppen_id} || ($form->{id} && $form->{orphaned}) || $form->{id} } @{ $form->{BUCHUNGSGRUPPEN} };
1454
1455   # use JavaScript Calendar or not (yes!)
1456   $form->{jsscript} = 1;
1457
1458   $units = AM->retrieve_units(\%myconfig, $form, $form->{"item"} eq "service" ? "service" : "dimension");
1459   $form->{ALL_UNITS} = [ map +{ name => $_ }, sort { $units->{$a}{sortkey} <=> $units->{$b}{sortkey} } keys %$units ];
1460
1461   $form->{fokus} = "ic.partnumber";
1462
1463   $form->header;
1464   print $form->parse_html_template('ic/form_header', { ALL_PRICE_FACTORS => $form->{ALL_PRICE_FACTORS}, 
1465                                                        ALL_UNITS         => $form->{ALL_UNITS},
1466                                                        BUCHUNGSGRUPPEN   => $form->{BUCHUNGSGRUPPEN},
1467                                                        payment_terms     => $form->{payment_terms},
1468                                                        all_partsgroup    => $form->{all_partsgroup}});
1469   $lxdebug->leave_sub();
1470 }
1471
1472 sub form_footer {
1473   $lxdebug->enter_sub();
1474
1475   if ($form->{item} eq "assembly") {
1476
1477     print qq|
1478         <tr>
1479           <td>
1480             <table width="100%">
1481               <tr>
1482                 <th colspan=2 align=right>| . $locale->text('Total') . qq|&nbsp;</th>
1483                 <th align=right>| . $form->format_amount(\%myconfig, $form->{assemblytotal}, 2) . qq|</th>
1484               </tr>
1485             </table>
1486           </td>
1487         </tr>
1488         <input type=hidden name=assembly_rows value=$form->{assembly_rows}>
1489 |;
1490   }
1491
1492   print qq|
1493       <input type=hidden name=login value=$form->{login}>
1494       <input type=hidden name=password value=$form->{password}>
1495       <input type=hidden name=callback value="$form->{callback}">
1496       <input type=hidden name=previousform value="$form->{previousform}">
1497       <input type=hidden name=taxaccount2 value="$form->{taxaccount2}">
1498       <input type=hidden name=vc value=$form->{vc}>
1499   <tr>
1500     <td><hr size=3 noshade></td>
1501   </tr>
1502 </table>
1503
1504 <br>
1505 <input class=submit type=submit name=action value="|
1506     . $locale->text('Update') . qq|">
1507   |;
1508
1509   unless ($form->{item} eq "service") {
1510     print qq|
1511       <input type=hidden name=makemodel_rows value=$form->{makemodel_rows}>
1512     |;
1513   }
1514
1515   print qq|
1516      <input type=hidden name=price_rows value=$form->{price_rows}>|;
1517
1518   print qq|
1519       <input class=submit type=submit name=action value="|
1520     . $locale->text('Save') . qq|">|;
1521
1522   if ($form->{id}) {
1523
1524     if (!$form->{previousform}) {
1525       print qq|
1526       <input class=submit type=submit name=action value="|
1527         . $locale->text('Save as new') . qq|">|;
1528     }
1529
1530     if ($form->{orphaned}) {
1531       if (!$form->{previousform}) {
1532         if ($form->{item} eq 'assembly') {
1533           if (!$form->{onhand}) {
1534             print qq|
1535       <input class=submit type=submit name=action value="|
1536               . $locale->text('Delete') . qq|">|;
1537           }
1538         } else {
1539           print qq|
1540       <input class=submit type=submit name=action value="|
1541             . $locale->text('Delete') . qq|">|;
1542         }
1543       }
1544     }
1545   }
1546
1547   if (!$form->{previousform}) {
1548     if ($form->{menubar}) {
1549       require "bin/mozilla/menu.pl";
1550       &menubar;
1551     }
1552   }
1553 # button for saving history
1554   if($form->{id} ne "") {
1555         print qq|
1556                 <input type=button class=submit onclick=set_history_window(|
1557                 . $form->{id} 
1558                 . qq|); name=history id=history value=|
1559                 . $locale->text('history') 
1560                 . qq|>|;
1561   }
1562 # /button for saving history
1563   print qq|
1564
1565 </form>
1566
1567 <script type="text/javascript" src="js/wz_tooltip.js"></script>
1568
1569 </body>
1570 </html>
1571 |;
1572
1573   $lxdebug->leave_sub();
1574 }
1575
1576 sub makemodel_row {
1577   $lxdebug->enter_sub();
1578   my ($numrows) = @_;
1579   
1580   print qq|
1581   <tr>
1582     <td>
1583       <table width=100%>
1584         <tr>
1585           <th class="listheading">| . $locale->text('Make') . qq|</th>
1586           <th class="listheading">| . $locale->text('Model') . qq|</th>
1587         </tr>
1588 |;
1589
1590   for my $i (1 .. $numrows) {
1591     $form->{"make_$i"}  =~ s/\"/&quot;/g;
1592     $form->{"model_$i"} =~ s/\"/&quot;/g;
1593     print qq|
1594         <tr>
1595           <td width=50%><input name="make_$i" size=30 value="$form->{"make_$i"}"></td>
1596           <td width=50%><input name="model_$i" size=30 value="$form->{"model_$i"}"></td>
1597         </tr>
1598 |;
1599   }
1600
1601   print qq|
1602       </table>
1603     </td>
1604   </tr>
1605 |;
1606
1607   $lxdebug->leave_sub();
1608 }
1609
1610 sub assembly_row {
1611   $lxdebug->enter_sub();
1612   my ($numrows) = @_;
1613   my (@column_index, %column_data, %column_header);
1614   my ($nochange, $callback, $previousform, $linetotal, $href);
1615
1616   our ($deliverydate); # ToDO: cjeck if this indeed comes from global context
1617
1618   @column_index =
1619     qw(runningnumber qty unit bom partnumber description partsgroup total);
1620
1621   if ($form->{previousform}) {
1622     $nochange     = 1;
1623     @column_index = qw(qty unit bom partnumber description partsgroup total);
1624   } else {
1625
1626     # change callback
1627     $form->{old_callback} = $form->{callback};
1628     $callback             = $form->{callback};
1629     $form->{callback}     = "$form->{script}?action=display_form";
1630
1631     # delete action
1632     map { delete $form->{$_} } qw(action header);
1633
1634     $previousform = "";
1635
1636     # save form variables in a previousform variable
1637     foreach my $key (sort keys %$form) {
1638
1639       # escape ampersands
1640       $form->{$key} =~ s/&/%26/g;
1641       $previousform .= qq|$key=$form->{$key}&|;
1642     }
1643     chop $previousform;
1644     $previousform = $form->escape($form->escape($previousform, 1));
1645     $form->{callback} = $callback;
1646
1647     $form->{assemblytotal} = 0;
1648     $form->{weight}        = 0;
1649
1650   }
1651   $column_header{runningnumber} =
1652     qq|<th nowrap width=5%>| . $locale->text('No.') . qq|</th>|;
1653   $column_header{qty} =
1654     qq|<th align=left nowrap width=10%>| . $locale->text('Qty') . qq|</th>|;
1655   $column_header{unit} =
1656     qq|<th align=left nowrap width=5%>| . $locale->text('Unit') . qq|</th>|;
1657   $column_header{partnumber} =
1658       qq|<th align=left nowrap width=20%>|
1659     . $locale->text('Part Number')
1660     . qq|</th>|;
1661   $column_header{description} =
1662     qq|<th nowrap width=50%>| . $locale->text('Part Description') . qq|</th>|;
1663   $column_header{total} =
1664     qq|<th align=right nowrap>| . $locale->text('Extended') . qq|</th>|;
1665   $column_header{bom}        = qq|<th>| . $locale->text('BOM') . qq|</th>|;
1666   $column_header{partsgroup} = qq|<th>| . $locale->text('Group') . qq|</th>|;
1667
1668   print qq|
1669   <tr class=listheading>
1670     <th class=listheading>| . $locale->text('Individual Items') . qq|</th>
1671   </tr>
1672   <tr>
1673     <td>
1674       <table width=100%>
1675         <tr>
1676 |;
1677
1678   map { print "\n$column_header{$_}" } @column_index;
1679
1680   print qq|
1681         </tr>
1682 |;
1683
1684   for my $i (1 .. $numrows) {
1685     $form->{"partnumber_$i"} =~ s/\"/&quot;/g;
1686
1687     $linetotal =
1688       $form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"}, 2);
1689     $form->{assemblytotal} += $linetotal;
1690
1691     $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"});
1692
1693     $linetotal = $form->format_amount(\%myconfig, $linetotal, 2);
1694
1695     if (($i >= 1) && ($i == $numrows)) {
1696
1697       if ($nochange) {
1698         map { $column_data{$_} = qq|<td></td>| }
1699           qw(qty unit partnumber description bom partsgroup);
1700       } else {
1701
1702         map { $column_data{$_} = qq|<td></td>| } qw(runningnumber unit bom);
1703
1704         $column_data{qty} =
1705           qq|<td><input name="qty_$i" size=5 value="$form->{"qty_$i"}"></td>|;
1706         $column_data{partnumber} =
1707           qq|<td><input name="partnumber_$i" size=15 value="$form->{"partnumber_$i"}"></td>|;
1708         $column_data{description} =
1709           qq|<td><input name="description_$i" size=40 value="$form->{"description_$i"}"></td>|;
1710         $column_data{partsgroup} =
1711           qq|<td><input name="partsgroup_$i" size=10 value="$form->{"partsgroup_$i"}"></td>|;
1712
1713       }
1714
1715     } else {
1716
1717       if ($form->{previousform}) {
1718         $column_data{partnumber} =
1719           qq|<td><input type=hidden name="partnumber_$i" value="$form->{"partnumber_$i"}">$form->{"partnumber_$i"}</td>|;
1720         $column_data{qty} =
1721           qq|<td align=right><input type=hidden name="qty_$i" value="$form->{"qty_$i"}">$form->{"qty_$i"}</td>|;
1722
1723         $column_data{bom} =
1724           qq|<td align=center><input type=hidden name="bom_$i" value=$form->{"bom_$i"}>|;
1725         $column_data{bom} .= ($form->{"bom_$i"}) ? "x" : "&nbsp;";
1726         $column_data{bom} .= qq|</td>|;
1727
1728         $column_data{partsgroup} =
1729           qq|<td><input type=hidden name="partsgroup_$i" value="$form->{"partsgroup_$i"}">$form->{"partsgroup_$i"}</td>|;
1730
1731       } else {
1732         $href =
1733           qq|$form->{script}?action=edit&id=$form->{"id_$i"}&login=$form->{login}&password=$form->{password}&rowcount=$i&previousform=$previousform|;
1734         $column_data{partnumber} =
1735           qq|<td><input type=hidden name="partnumber_$i" value="$form->{"partnumber_$i"}"><a href=$href>$form->{"partnumber_$i"}</a></td>|;
1736         $column_data{runningnumber} =
1737           qq|<td><input name="runningnumber_$i" size=3 value="$i"></td>|;
1738         $column_data{qty} =
1739           qq|<td><input name="qty_$i" size=5 value="$form->{"qty_$i"}"></td>|;
1740
1741         $form->{"bom_$i"} = ($form->{"bom_$i"}) ? "checked" : "";
1742         $column_data{bom} =
1743           qq|<td align=center><input name="bom_$i" type=checkbox class=checkbox value=1 $form->{"bom_$i"}></td>|;
1744
1745         $column_data{partsgroup} =
1746           qq|<td><input type=hidden name="partsgroup_$i" value="$form->{"partsgroup_$i"}">$form->{"partsgroup_$i"}</td>|;
1747       }
1748
1749       $column_data{unit} =
1750         qq|<td><input type=hidden name="unit_$i" value="$form->{"unit_$i"}">$form->{"unit_$i"}</td>|;
1751       $column_data{description} =
1752         qq|<td><input type=hidden name="description_$i" value="$form->{"description_$i"}">$form->{"description_$i"}</td>|;
1753     }
1754
1755     $column_data{total} = qq|<td align=right>$linetotal</td>|;
1756
1757     $column_data{deliverydate} = qq|<td align=right>$deliverydate</td>|;
1758
1759     print qq|
1760         <tr>|;
1761
1762     map { print "\n$column_data{$_}" } @column_index;
1763
1764     print qq|
1765         </tr>
1766   <input type=hidden name="id_$i" value=$form->{"id_$i"}>
1767   <input type=hidden name="sellprice_$i" value=$form->{"sellprice_$i"}>
1768   <input type=hidden name="weight_$i" value=$form->{"weight_$i"}>
1769 |;
1770   }
1771
1772   print qq|
1773       </table>
1774     </td>
1775   </tr>
1776 |;
1777
1778   $lxdebug->leave_sub();
1779 }
1780
1781 sub update {
1782   $lxdebug->enter_sub();
1783
1784   if ($form->{item} eq "assembly") {
1785     my $i = $form->{assembly_rows};
1786
1787     # if last row is empty check the form otherwise retrieve item
1788     if (   ($form->{"partnumber_$i"} eq "")
1789         && ($form->{"description_$i"} eq "")
1790         && ($form->{"partsgroup_$i"}  eq "")) {
1791
1792       &check_form;
1793
1794     } else {
1795
1796       IC->assembly_item(\%myconfig, \%$form);
1797
1798       my $rows = scalar @{ $form->{item_list} };
1799
1800       if ($rows) {
1801         $form->{"qty_$i"} = 1 unless ($form->{"qty_$i"});
1802
1803         if ($rows > 1) {
1804           $form->{makemodel_rows}--;
1805           &select_item;
1806           exit;
1807         } else {
1808           map { $form->{item_list}[$i]{$_} =~ s/\"/&quot;/g }
1809             qw(partnumber description unit partsgroup);
1810           map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} }
1811             keys %{ $form->{item_list}[0] };
1812           $form->{"runningnumber_$i"} = $form->{assembly_rows};
1813           $form->{assembly_rows}++;
1814
1815           &check_form;
1816
1817         }
1818
1819       } else {
1820
1821         $form->{rowcount} = $i;
1822         $form->{assembly_rows}++;
1823
1824         &new_item;
1825
1826       }
1827     }
1828
1829   } elsif (($form->{item} eq 'part') || ($form->{item} eq 'service')) {
1830     &check_form;
1831   }
1832
1833   $lxdebug->leave_sub();
1834 }
1835
1836 sub save {
1837   $lxdebug->enter_sub();
1838
1839   my ($parts_id, %newform, $previousform, $amount, $callback);
1840
1841   # check if there is a part number - commented out, cause there is an automatic allocation of numbers
1842   # $form->isblank("partnumber", $locale->text(ucfirst $form->{item}." Part Number missing!"));
1843
1844   # check if there is a description
1845   $form->isblank("description", $locale->text("Part Description missing!"));
1846
1847   if ($form->{obsolete}) {
1848     $form->error(
1849       $locale->text(
1850         "Inventory quantity must be zero before you can set this $form->{item} obsolete!"
1851       ))
1852       if ($form->{onhand} * 1);
1853   }
1854
1855   if (!$form->{buchungsgruppen_id}) {
1856     $form->error($locale->text("Parts must have an entry type.") . " " . 
1857      $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.") 
1858     );
1859   }
1860
1861   $form->error($locale->text('Description must not be empty!')) unless $form->{description};
1862   $form->error($locale->text('Partnumber must not be set to empty!')) if $form->{id} && !$form->{partnumber};
1863
1864   # expand dynamic strings
1865   # $locale->text('Inventory quantity must be zero before you can set this part obsolete!')
1866   # $locale->text('Inventory quantity must be zero before you can set this assembly obsolete!')
1867   # $locale->text('Part Number missing!')
1868   # $locale->text('Service Number missing!')
1869   # $locale->text('Assembly Number missing!')
1870
1871   # save part
1872   $lxdebug->message($LXDebug::DEBUG1, "ic.pl: sellprice in save = $form->{sellprice}\n");
1873   if (IC->save(\%myconfig, \%$form) == 3) {
1874     $form->error($locale->text('Partnumber not unique!'));
1875   }
1876   # saving the history
1877   if(!exists $form->{addition}) {
1878     $form->{snumbers} = qq|partnumber_| . $form->{partnumber};
1879         $form->{addition} = "SAVED";
1880         $form->save_history($form->dbconnect(\%myconfig));
1881   }
1882   # /saving the history
1883   $parts_id = $form->{id};
1884
1885   my $i;
1886   # load previous variables
1887   if ($form->{previousform}) {
1888
1889     # save the new form variables before splitting previousform
1890     map { $newform{$_} = $form->{$_} } keys %$form;
1891
1892     $previousform = $form->unescape($form->{previousform});
1893
1894     # don't trample on previous variables
1895     map { delete $form->{$_} } keys %newform;
1896
1897     # now take it apart and restore original values
1898     foreach my $item (split /&/, $previousform) {
1899       my ($key, $value) = split m/=/, $item, 2;
1900       $value =~ s/%26/&/g;
1901       $form->{$key} = $value;
1902     }
1903     $form->{taxaccounts} = $newform{taxaccount2};
1904
1905     if ($form->{item} eq 'assembly') {
1906
1907       # undo number formatting
1908       map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
1909         qw(weight listprice sellprice rop);
1910
1911       $form->{assembly_rows}--;
1912       $i = $newform{rowcount};
1913       $form->{"qty_$i"} = 1 unless ($form->{"qty_$i"});
1914
1915       $form->{sellprice} -= $form->{"sellprice_$i"} * $form->{"qty_$i"};
1916       $form->{weight}    -= $form->{"weight_$i"} * $form->{"qty_$i"};
1917
1918       # change/add values for assembly item
1919       map { $form->{"${_}_$i"} = $newform{$_} } qw(partnumber description bin unit weight listprice sellprice inventory_accno income_accno expense_accno price_factor_id);
1920
1921       $form->{sellprice} += $form->{"sellprice_$i"} * $form->{"qty_$i"};
1922       $form->{weight}    += $form->{"weight_$i"} * $form->{"qty_$i"};
1923
1924     } else {
1925
1926       # set values for last invoice/order item
1927       $i = $form->{rowcount};
1928       $form->{"qty_$i"} = 1 unless ($form->{"qty_$i"});
1929
1930       map { $form->{"${_}_$i"} = $newform{$_} } qw(partnumber description bin unit listprice inventory_accno income_accno expense_accno sellprice lastcost price_factor_id);
1931
1932       $form->{"sellprice_$i"} = $newform{lastcost} if ($form->{vendor_id});
1933
1934       if ($form->{exchangerate} != 0) {
1935         $form->{"sellprice_$i"} /= $form->{exchangerate};
1936       }
1937
1938       $lxdebug->message($LXDebug::DEBUG1, qq|sellprice_$i in previousform 2 = | . $form->{"sellprice_$i"} . qq|\n|);
1939
1940       map { $form->{"taxaccounts_$i"} .= "$_ " } split / /, $newform{taxaccount};
1941       chop $form->{"taxaccounts_$i"};
1942       foreach my $item (qw(description rate taxnumber)) {
1943         my $index = $form->{"taxaccounts_$i"} . "_$item";
1944         $form->{$index} = $newform{$index};
1945       }
1946
1947       # credit remaining calculation
1948       $amount = $form->{"sellprice_$i"} * (1 - $form->{"discount_$i"} / 100) * $form->{"qty_$i"};
1949
1950       map { $form->{"${_}_base"} += $amount } (split / /, $form->{"taxaccounts_$i"});
1951       map { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) } split / /, $form->{"taxaccounts_$i"} if !$form->{taxincluded};
1952
1953       $form->{creditremaining} -= $amount;
1954
1955       # redo number formatting, because invoice parse them!
1956       map { $form->{"${_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}) } qw(weight listprice sellprice rop);
1957     }
1958
1959     $form->{"id_$i"} = $parts_id;
1960
1961     # Get the actual price factor (not just the ID) for the marge calculation.
1962     $form->get_lists('price_factors' => 'ALL_PRICE_FACTORS');
1963     foreach my $pfac (@{ $form->{ALL_PRICE_FACTORS} }) {
1964       next if ($pfac->{id} != $newform{price_factor_id});
1965       $form->{"marge_price_factor_$i"} = $pfac->{factor};
1966       last;
1967     }
1968     delete $form->{ALL_PRICE_FACTORS};
1969
1970     delete $form->{action};
1971
1972     # restore original callback
1973     $callback = $form->unescape($form->{callback});
1974     $form->{callback} = $form->unescape($form->{old_callback});
1975     delete $form->{old_callback};
1976
1977     $form->{makemodel_rows}--;
1978
1979     # put callback together
1980     foreach my $key (keys %$form) {
1981
1982       # do single escape for Apache 2.0
1983       my $value = $form->escape($form->{$key}, 1);
1984       $callback .= qq|&$key=$value|;
1985     }
1986     $form->{callback} = $callback;
1987   }
1988   $lxdebug->message($LXDebug::DEBUG1, qq|ic.pl: sellprice_$i nach sub save = | . $form->{"sellprice_$i"} . qq|\n|);
1989
1990   # redirect
1991   $form->redirect;
1992
1993   $lxdebug->leave_sub();
1994 }
1995
1996 sub save_as_new {
1997   $lxdebug->enter_sub();
1998
1999   # saving the history
2000   if(!exists $form->{addition}) {
2001     $form->{snumbers} = qq|partnumber_| . $form->{partnumber};
2002         $form->{addition} = "SAVED AS NEW";
2003         $form->save_history($form->dbconnect(\%myconfig));
2004   }
2005   # /saving the history
2006   $form->{id} = 0;
2007   if ($form->{"original_partnumber"} &&
2008       ($form->{"partnumber"} eq $form->{"original_partnumber"})) {
2009     $form->{partnumber} = "";
2010   }
2011   &save;
2012   $lxdebug->leave_sub();
2013 }
2014
2015 sub delete {
2016   $lxdebug->enter_sub();
2017   # saving the history
2018   if(!exists $form->{addition}) {
2019     $form->{snumbers} = qq|partnumber_| . $form->{partnumber};
2020         $form->{addition} = "DELETED";
2021         $form->save_history($form->dbconnect(\%myconfig));
2022   }
2023   # /saving the history
2024   my $rc = IC->delete(\%myconfig, \%$form);
2025
2026   # redirect
2027   $form->redirect($locale->text('Item deleted!')) if ($rc > 0);
2028   $form->error($locale->text('Cannot delete item!'));
2029
2030   $lxdebug->leave_sub();
2031 }
2032
2033 sub price_row {
2034   $lxdebug->enter_sub();
2035
2036   my ($numrows) = @_;
2037
2038   print qq|
2039   <tr>
2040     <td>
2041       <table width=100%>
2042         <tr>
2043           <th class="listheading">| . $locale->text('Preisklasse') . qq|</th>
2044           <th class="listheading">| . $locale->text('Preis') . qq|</th>
2045         </tr>
2046 |;
2047   for my $i (1 .. $numrows) {
2048     print qq|
2049         <tr>
2050           <td width=50%><input type=hidden name="pricegroup_$i" size=30  value="$form->{"pricegroup_$i"}">$form->{"pricegroup_$i"}</td>
2051           <td width=50%><input name="price_$i" size=11 value="$form->{"price_$i"}"></td>
2052           <input type=hidden name="pricegroup_id_$i" value="$form->{"pricegroup_id_$i"}">
2053         </tr>
2054 |;
2055   }
2056
2057   print qq|
2058       </table>
2059     </td>
2060   </tr>
2061 |;
2062
2063   $lxdebug->leave_sub();
2064 }
2065
2066 sub parts_language_selection {
2067   $lxdebug->enter_sub();
2068
2069   our ($onload);
2070
2071   my $languages = IC->retrieve_languages(\%myconfig, $form);
2072
2073   if ($form->{language_values} ne "") {
2074     foreach $item (split(/---\+\+\+---/, $form->{language_values})) {
2075       my ($language_id, $translation, $longdescription) = split(/--\+\+--/, $item);
2076
2077       foreach my $language (@{ $languages }) {
2078         next unless ($language->{id} == $language_id);
2079
2080         $language->{translation}     = $translation;
2081         $language->{longdescription} = $longdescription;
2082         last;
2083       }
2084     }
2085   }
2086
2087   my @header_sort = qw(name longdescription);
2088   my %header_title = ( "name" => $locale->text("Name"),
2089                        "longdescription" => $locale->text("Long Description"),
2090                        );
2091
2092   my @header =
2093     map(+{ "column_title" => $header_title{$_},
2094            "column" => $_,
2095            "callback" => $callback,
2096          },
2097         @header_sort);
2098
2099   $form->{"title"} = $locale->text("Language Values");
2100   $form->header();
2101   print $form->parse_html_template("ic/parts_language_selection", { "HEADER"    => \@header,
2102                                                                     "LANGUAGES" => $languages,
2103                                                                     "onload"    => $onload });
2104
2105   $lxdebug->leave_sub();
2106 }
2107
2108 sub continue { call_sub($form->{"nextsub"}); }