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