6bd04483f6acbe06f8a1fa95139006c2eaa5ba2a
[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 use List::Util qw(max);
37
38 use SL::IC;
39 use SL::ReportGenerator;
40
41 #use SL::PE;
42
43 # use strict;
44 #use warnings;
45
46 # global imports
47 our ($form, $locale, %myconfig, $lxdebug);
48
49 require "bin/mozilla/io.pl";
50 require "bin/mozilla/invoice_io.pl";
51 require "bin/mozilla/common.pl";
52 require "bin/mozilla/reportgenerator.pl";
53
54 1;
55
56 # Parserhappy(R):
57 # type=submit $locale->text('Add Part')
58 # type=submit $locale->text('Add Service')
59 # type=submit $locale->text('Add Assembly')
60 # type=submit $locale->text('Edit Part')
61 # type=submit $locale->text('Edit Service')
62 # type=submit $locale->text('Edit Assembly')
63
64 # end of main
65
66 sub add {
67   $lxdebug->enter_sub();
68
69   $auth->assert('part_service_assembly_edit');
70
71   $form->{title} = $locale->text('Add ' . ucfirst $form->{item});
72
73   $form->{callback} =
74     "$form->{script}?action=add&item=$form->{item}"
75     unless $form->{callback};
76
77   $form->{"unit_changeable"} = 1;
78
79   IC->get_pricegroups(\%myconfig, \%$form);
80   &link_part;
81   &display_form;
82
83   $lxdebug->leave_sub();
84 }
85
86 sub search {
87   $lxdebug->enter_sub();
88
89   $auth->assert('part_service_assembly_edit');
90
91   # switch for backward sorting
92   $form->{revers} = 0;
93
94   # memory for which table was sort at last time
95   $form->{lastsort} = "";
96
97   # counter for added entries to top100
98   $form->{ndxs_counter} = 0;
99
100   # $locale->text('Parts')
101   # $locale->text('Services')
102
103   my $is_service  = $form->{searchitems} eq 'service';
104   my $is_assembly = $form->{searchitems} eq 'assembly';
105
106   $form->{title} = (ucfirst $form->{searchitems}) . "s";
107   $form->{title} = $locale->text($form->{title});
108   $form->{title} = $locale->text('Assemblies') if $is_assembly;
109
110   $form->{jsscript} = 1;
111
112   $form->header;
113
114   print $form->parse_html_template('ic/search', { is_assembly => $is_assembly, 
115                                                   is_service  => $is_service, 
116                                                   dateformat  => $myconfig{dateformat}, });
117
118   $lxdebug->leave_sub();
119 }    #end search()
120
121 sub search_update_prices {
122   $lxdebug->enter_sub();
123
124   $auth->assert('part_service_assembly_edit');
125
126   my $pricegroups = IC->get_pricegroups(\%myconfig, \%$form);
127
128   $form->header;
129
130   print $form->parse_html_template('ic/search_update_prices', { PRICE_ROWS => $pricegroups });
131
132   $lxdebug->leave_sub();
133 }    #end search()
134
135 sub confirm_price_update {
136   $lxdebug->enter_sub();
137
138   $auth->assert('part_service_assembly_edit');
139
140   $form->{nextsub} = "update_prices";
141   $form->header;
142
143   map { delete $form->{$_} } qw(action header);
144   
145   print $form->parse_html_template('ic/confirm_price_update', { HIDDENS => [ map { name => $_, value => $form->{$_} }, keys %$form ] });
146
147   $lxdebug->leave_sub();
148 }
149
150 sub update_prices {
151   $lxdebug->enter_sub();
152
153   $auth->assert('part_service_assembly_edit');
154
155   if (IC->update_prices(\%myconfig, \%$form)) {
156     $form->redirect($form->{update_count} . $locale->text('prices updated!'));
157   } else {
158     $form->error($locale->text('Could not update prices!'));
159   }
160
161   $lxdebug->leave_sub();
162 }
163
164 sub choice {
165   $lxdebug->enter_sub();
166
167   $auth->assert('part_service_assembly_edit');
168
169   our ($j, $lastndx);
170   my ($totop100);
171
172   $form->{title} = $locale->text('Top 100 hinzufuegen');
173
174   $form->header;
175
176   push @custom_hiddens, qw(searchitems title bom titel revers lastsort sort ndxs_counter extras);
177   push @custom_hiddens, qw(itemstatus l_linetotal l_partnumber l_description l_onhand l_unit l_sellprice l_linetotalsellprice);
178   my @HIDDENS = (
179         +{ name => 'row',     value => $j              },
180         +{ name => 'nextsub', value => 'item_selected' },
181         +{ name => 'test',    value => 'item_selected' },
182         +{ name => 'lastndx', value => $lastndx        },
183     map(+{ name => $_,        value => $form->{$_}     }, @custom_hiddens),
184   );
185
186   my ($partnumber, $description, $unit, $sellprice, $soldtotal);
187   # if choice set data
188 #  if ($form->{ndx}) {
189 #    for my $i (0 .. $form->{ndxs_counter}) {
190 #
191 #      # insert data into top100
192 #      push @{ $form->{parts} },
193 #        { number      => "",
194 #          partnumber  => $form->{"totop100_partnumber_$j"},
195 #          description => $form->{"totop100_description_$j"},
196 #          unit        => $form->{"totop100_unit_$j"},
197 #          sellprice   => $form->{"totop100_sellprice_$j"},
198 #          soldtotal   => $form->{"totop100_soldtotal_$j"},
199 #        };
200 #    }    #rof
201 #  }    #fi
202
203   $totop100 = "";
204
205   # set data for next page
206   for my $i (1 .. $form->{ndxs_counter}) {
207     $partnumber  = $form->{"totop100_partnumber_$i"};
208     $description = $form->{"totop100_description_$i"};
209     $unit        = $form->{"totop100_unit_$i"};
210     $sellprice   = $form->{"totop100_sellprice_$i"};
211     $soldtotal   = $form->{"totop100_soldtotal_$i"};
212
213   push @PARTS, {
214     totop100_partnumber  => $form->{"totop100_partnumber_$i"},
215     totop100_description => $form->{"totop100_description_$i"},
216     totop100_unit        => $form->{"totop100_unit_$i"},
217     totop100_sellprice   => $form->{"totop100_sellprice_$i"},
218     totop100_soldtotal   => $form->{"totop100_soldtotal_$i"},
219   }
220
221 #    $totop100 .= qq|
222 #<input type=hidden name=totop100_partnumber_$i value=$form->{"totop100_partnumber_$i"}>
223 #<input type=hidden name=totop100_description_$i value=$form->{"totop100_description_$i"}>
224 #<input type=hidden name=totop100_unit_$i value=$form->{"totop100_unit_$i"}>
225 #<input type=hidden name=totop100_sellprice_$i value=$form->{"totop100_sellprice_$i"}>
226 #<input type=hidden name=totop100_soldtotal_$i value=$form->{"totop100_soldtotal_$i"}>
227 #    |;
228   }    #rof
229
230   print $form->parse_html_template('ic/choice', +{ HIDDENS => \@HIDDENS, PARTS => \@PARTS });
231
232   $lxdebug->leave_sub();
233 }    #end choice
234
235 sub list {
236   $lxdebug->enter_sub();
237
238   $auth->assert('part_service_assembly_edit');
239
240   our ($lastndx);
241   our ($partnumber, $description, $unit, $sellprice, $soldtotal);
242
243   my @sortorders = ("", "partnumber", "description", "all");
244   my $sortorder = $sortorders[($form->{description} ? 2 : 0) + ($form->{partnumber} ? 1 : 0)];
245   IC->get_parts(\%myconfig, \%$form, $sortorder);
246
247   $form->{title} = $locale->text('Top 100 hinzufuegen');
248
249   $form->header;
250
251   print qq|
252 <body>
253   <form method=post action=ic.pl>
254     <table width=100%>
255      <tr>
256       <th class=listtop colspan=6>| . $locale->text('choice part') . qq|</th>
257      </tr>
258         <tr height="5"></tr>
259         <tr class=listheading>
260           <th>&nbsp;</th>
261           <th class=listheading>| . $locale->text('Part Number') . qq|</th>
262           <th class=listheading>| . $locale->text('Part Description') . qq|</th>
263           <th class=listheading>| . $locale->text('Unit of measure') . qq|</th>
264           <th class=listheading>| . $locale->text('Sell Price') . qq|</th>
265           <th class=listheading>| . $locale->text('soldtotal') . qq|</th>
266         </tr>|;
267
268   my $j = 0;
269   my $i = $form->{rows};
270
271   for ($j = 1; $j <= $i; $j++) {
272
273     print qq|
274         <tr class=listrow| . ($j % 2) . qq|>|;
275     if ($j == 1) {
276       print qq|
277             <td><input name=ndx class=radio type=radio value=$j checked></td>|;
278     } else {
279       print qq|
280           <td><input name=ndx class=radio type=radio value=$j></td>|;
281     }
282     print qq|
283           <td><input name="new_partnumber_$j" type=hidden value="$form->{"partnumber_$j"}">$form->{"partnumber_$j"}</td>
284           <td><input name="new_description_$j" type=hidden value="$form->{"description_$j"}">$form->{"description_$j"}</td>
285           <td><input name="new_unit_$j" type=hidden value="$form->{"unit_$j"}">$form->{"unit_$j"}</td>
286           <td><input name="new_sellprice_$j" type=hidden value="$form->{"sellprice_$j"}">$form->{"sellprice_$j"}</td>
287           <td><input name="new_soldtotal_$j" type=hidden value="$form->{"soldtotal_$j"}">$form->{"soldtotal_$j"}</td>
288         </tr>
289
290         <input name="new_id_$j" type=hidden value="$form->{"id_$j"}">|;
291   }
292
293   print qq|
294
295 </table>
296
297 <br>
298
299
300 <input type=hidden name=itemstatus value="$form->{itemstatus}">
301 <input type=hidden name=l_linetotal value="$form->{l_linetotal}">
302 <input type=hidden name=l_partnumber value="$form->{l_partnumber}">
303 <input type=hidden name=l_description value="$form->{l_description}">
304 <input type=hidden name=l_onhand value="$form->{l_onhand}">
305 <input type=hidden name=l_unit value="$form->{l_unit}">
306 <input type=hidden name=l_sellprice value="$form->{l_sellprice}">
307 <input type=hidden name=l_linetotalsellprice value="$form->{l_linetotalsellprice}">
308 <input type=hidden name=sort value="$form->{sort}">
309 <input type=hidden name=revers value="$form->{revers}">
310 <input type=hidden name=lastsort value="$form->{lastsort}">
311
312 <input type=hidden name=bom value="$form->{bom}">
313 <input type=hidden name=titel value="$form->{titel}">
314 <input type=hidden name=searchitems value="$form->{searchitems}">
315
316 <input type=hidden name=row value=$j>
317
318 <input type=hidden name=nextsub value=item_selected>
319
320 <input name=lastndx type=hidden value=$lastndx>
321
322 <input name=ndxs_counter type=hidden value=$form->{ndxs_counter}>|;
323
324   my $totop100 = "";
325
326   if (($form->{ndxs_counter}) > 0) {
327     for ($i = 1; ($i < $form->{ndxs_counter} + 1); $i++) {
328
329       $partnumber  = $form->{"totop100_partnumber_$i"};
330       $description = $form->{"totop100_description_$i"};
331       $unit        = $form->{"totop100_unit_$i"};
332       $sellprice   = $form->{"totop100_sellprice_$i"};
333       $soldtotal   = $form->{"totop100_soldtotal_$i"};
334
335       $totop100 .= qq|
336 <input type=hidden name=totop100_partnumber_$i value=$form->{"totop100_partnumber_$i"}>
337 <input type=hidden name=totop100_description_$i value=$form->{"totop100_description_$i"}>
338 <input type=hidden name=totop100_unit_$i value=$form->{"totop100_unit_$i"}>
339 <input type=hidden name=totop100_sellprice_$i value=$form->{"totop100_sellprice_$i"}>
340 <input type=hidden name=totop100_soldtotal_$i value=$form->{"totop100_soldtotal_$i"}>
341       |;
342     }    #rof
343   }    #fi
344
345   print $totop100;
346
347   print qq|
348 <input class=submit type=submit name=action value="|
349     . $locale->text('TOP100') . qq|">
350
351 </form>
352 </body>
353 </html>
354 |;
355   $lxdebug->leave_sub();
356 }    #end list()
357
358 sub top100 {
359   $lxdebug->enter_sub();
360
361   $auth->assert('part_service_assembly_edit');
362
363   if ($form->{ndx}) {
364     $form->{ndxs_counter}++;
365
366     if ($form->{ndxs_counter} > 0) {
367
368       my $index = $form->{ndx};
369
370       $form->{"totop100_partnumber_$form->{ndxs_counter}"} = $form->{"new_partnumber_$index"};
371       $form->{"totop100_description_$form->{ndxs_counter}"} = $form->{"new_description_$index"};
372       $form->{"totop100_unit_$form->{ndxs_counter}"} = $form->{"new_unit_$index"};
373       $form->{"totop100_sellprice_$form->{ndxs_counter}"} = $form->{"new_sellprice_$index"};
374       $form->{"totop100_soldtotal_$form->{ndxs_counter}"} = $form->{"new_soldtotal_$index"};
375     }    #fi
376   }    #fi
377   &addtop100();
378   $lxdebug->leave_sub();
379 }    #end top100
380
381 sub addtop100 {
382   $lxdebug->enter_sub();
383
384   $auth->assert('part_service_assembly_edit');
385
386   my ($revers, $lastsort, $callback, $option, $description, $sameitem,
387       $partnumber, $unit, $sellprice, $soldtotal, $totop100, $onhand, $align);
388   my (@column_index, %column_header, %column_data);
389   my ($totalsellprice, $totallastcost, $totallistprice, $subtotalonhand, $subtotalsellprice, $subtotallastcost, $subtotallistprice);
390
391   $form->{top100}      = "top100";
392   $form->{l_soldtotal} = "Y";
393 #  $form->{soldtotal}   = "soldtotal";
394   $form->{sort}        = "soldtotal";
395   $form->{l_qty}       = "N";
396   $form->{l_linetotal} = "";
397   $form->{revers}      = 1;
398   $form->{number}      = "position";
399   $form->{l_number}    = "Y";
400
401   $totop100 = "";
402
403   $form->{title} = $locale->text('Top 100');
404
405   $revers   = $form->{revers};
406   $lastsort = $form->{lastsort};
407
408   if (($form->{lastsort} eq "") && ($form->{sort} eq undef)) {
409     $form->{revers}   = 0;
410     $form->{lastsort} = "partnumber";
411     $form->{sort}     = "partnumber";
412   }    #fi
413
414   $callback =
415     "$form->{script}?action=top100&searchitems=$form->{searchitems}&itemstatus=$form->{itemstatus}&bom=$form->{bom}&l_linetotal=$form->{l_linetotal}&title="
416     . $form->escape($form->{title}, 1);
417
418   # if we have a serialnumber limit search
419   if ($form->{serialnumber} || $form->{l_serialnumber}) {
420     $form->{l_serialnumber} = "Y";
421     unless (   $form->{bought}
422             || $form->{sold}
423             || $form->{rfq}
424             || $form->{quoted}) {
425       $form->{bought} = $form->{sold} = 1;
426     }
427   }
428   IC->all_parts(\%myconfig, \%$form);
429
430   if ($form->{itemstatus} eq 'active') {
431     $option .= $locale->text('Active') . " : ";
432   }
433   if ($form->{itemstatus} eq 'obsolete') {
434     $option .= $locale->text('Obsolete') . " : ";
435   }
436   if ($form->{itemstatus} eq 'orphaned') {
437     $option .= $locale->text('Orphaned') . " : ";
438   }
439   if ($form->{itemstatus} eq 'onhand') {
440     $option .= $locale->text('On Hand') . " : ";
441     $form->{l_onhand} = "Y";
442   }
443   if ($form->{itemstatus} eq 'short') {
444     $option .= $locale->text('Short') . " : ";
445     $form->{l_onhand} = "Y";
446   }
447   if ($form->{onorder}) {
448     $form->{l_ordnumber} = "Y";
449     $callback .= "&onorder=$form->{onorder}";
450     $option   .= $locale->text('On Order') . " : ";
451   }
452   if ($form->{ordered}) {
453     $form->{l_ordnumber} = "Y";
454     $callback .= "&ordered=$form->{ordered}";
455     $option   .= $locale->text('Ordered') . " : ";
456   }
457   if ($form->{rfq}) {
458     $form->{l_quonumber} = "Y";
459     $callback .= "&rfq=$form->{rfq}";
460     $option   .= $locale->text('RFQ') . " : ";
461   }
462   if ($form->{quoted}) {
463     $form->{l_quonumber} = "Y";
464     $callback .= "&quoted=$form->{quoted}";
465     $option   .= $locale->text('Quoted') . " : ";
466   }
467   if ($form->{bought}) {
468     $form->{l_invnumber} = "Y";
469     $callback .= "&bought=$form->{bought}";
470     $option   .= $locale->text('Bought') . " : ";
471   }
472   if ($form->{sold}) {
473     $form->{l_invnumber} = "Y";
474     $callback .= "&sold=$form->{sold}";
475     $option   .= $locale->text('Sold') . " : ";
476   }
477   if (   $form->{bought}
478       || $form->{sold}
479       || $form->{onorder}
480       || $form->{ordered}
481       || $form->{rfq}
482       || $form->{quoted}) {
483
484     $form->{l_lastcost} = "";
485     $form->{l_name}     = "Y";
486     if ($form->{transdatefrom}) {
487       $callback .= "&transdatefrom=$form->{transdatefrom}";
488       $option   .= "\n<br>"
489         . $locale->text('From')
490         . "&nbsp;"
491         . $locale->date(\%myconfig, $form->{transdatefrom}, 1);
492     }
493     if ($form->{transdateto}) {
494       $callback .= "&transdateto=$form->{transdateto}";
495       $option   .= "\n<br>"
496         . $locale->text('To')
497         . "&nbsp;"
498         . $locale->date(\%myconfig, $form->{transdateto}, 1);
499     }
500   }
501
502   $option .= "<br>";
503
504   if ($form->{partnumber}) {
505     $callback .= "&partnumber=$form->{partnumber}";
506     $option   .= $locale->text('Part Number') . qq| : $form->{partnumber}<br>|;
507   }
508   if ($form->{ean}) {
509     $callback .= "&partnumber=$form->{ean}";
510     $option   .= $locale->text('EAN') . qq| : $form->{ean}<br>|;
511   }
512   if ($form->{partsgroup}) {
513     $callback .= "&partsgroup=$form->{partsgroup}";
514     $option   .= $locale->text('Group') . qq| : $form->{partsgroup}<br>|;
515   }
516   if ($form->{serialnumber}) {
517     $callback .= "&serialnumber=$form->{serialnumber}";
518     $option   .= $locale->text('Serial Number') . qq| : $form->{serialnumber}<br>|;
519   }
520   if ($form->{description}) {
521     $callback   .= "&description=$form->{description}";
522     $description = $form->{description};
523     $description =~ s/\n/<br>/g;
524     $option     .= $locale->text('Part Description') . qq| : $form->{description}<br>|;
525   }
526   if ($form->{make}) {
527     $callback .= "&make=$form->{make}";
528     $option   .= $locale->text('Make') . qq| : $form->{make}<br>|;
529   }
530   if ($form->{model}) {
531     $callback .= "&model=$form->{model}";
532     $option   .= $locale->text('Model') . qq| : $form->{model}<br>|;
533   }
534   if ($form->{drawing}) {
535     $callback .= "&drawing=$form->{drawing}";
536     $option   .= $locale->text('Drawing') . qq| : $form->{drawing}<br>|;
537   }
538   if ($form->{microfiche}) {
539     $callback .= "&microfiche=$form->{microfiche}";
540     $option   .= $locale->text('Microfiche') . qq| : $form->{microfiche}<br>|;
541   }
542   if ($form->{l_soldtotal}) {
543     $callback .= "&soldtotal=$form->{soldtotal}";
544     $option   .= $locale->text('soldtotal') . qq| : $form->{soldtotal}<br>|;
545   }
546
547   my @columns = $form->sort_columns(
548     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)
549   );
550
551   if ($form->{l_linetotal}) {
552     $form->{l_onhand} = "Y";
553     $form->{l_linetotalsellprice} = "Y" if $form->{l_sellprice};
554     if ($form->{l_lastcost}) {
555       $form->{l_linetotallastcost} = "Y";
556       if (($form->{searchitems} eq 'assembly') && !$form->{bom}) {
557         $form->{l_linetotallastcost} = "";
558       }
559     }
560     $form->{l_linetotallistprice} = "Y" if $form->{l_listprice};
561   }
562
563   if ($form->{searchitems} eq 'service') {
564
565     # remove bin, weight and rop from list
566     map { $form->{"l_$_"} = "" } qw(bin weight rop);
567
568     $form->{l_onhand} = "";
569
570     # qty is irrelevant unless bought or sold
571     if (   $form->{bought}
572         || $form->{sold}
573         || $form->{onorder}
574         || $form->{ordered}
575         || $form->{rfq}
576         || $form->{quoted}) {
577       $form->{l_onhand} = "Y";
578     } else {
579       $form->{l_linetotalsellprice} = "";
580       $form->{l_linetotallastcost}  = "";
581     }
582   }
583
584   $form->{l_lastcost} = ""
585     if ($form->{searchitems} eq 'assembly' && !$form->{bom});
586
587   foreach my $item (@columns) {
588     if ($form->{"l_$item"} eq "Y") {
589       push @column_index, $item;
590
591       # add column to callback
592       $callback .= "&l_$item=Y";
593     }
594   }
595
596   if ($form->{l_subtotal} eq 'Y') {
597     $callback .= "&l_subtotal=Y";
598   }
599
600   $column_header{number} =
601     qq|<th class=listheading nowrap>| . $locale->text('number') . qq|</th>|;
602   $column_header{partnumber} =
603     qq|<th nowrap><a class=listheading href=$callback&sort=partnumber&revers=$form->{revers}&lastsort=$form->{lastsort}>|
604     . $locale->text('Part Number')
605     . qq|</a></th>|;
606   $column_header{description} =
607     qq|<th nowrap><a class=listheading href=$callback&sort=description&revers=$form->{revers}&lastsort=$form->{lastsort}>|
608     . $locale->text('Part Description')
609     . qq|</a></th>|;
610   $column_header{partsgroup} =
611       qq|<th nowrap><a class=listheading href=$callback&sort=partsgroup>|
612     . $locale->text('Group')
613     . qq|</a></th>|;
614   $column_header{bin} =
615       qq|<th><a class=listheading href=$callback&sort=bin>|
616     . $locale->text('Bin')
617     . qq|</a></th>|;
618   $column_header{priceupdate} =
619       qq|<th nowrap><a class=listheading href=$callback&sort=priceupdate>|
620     . $locale->text('Updated')
621     . qq|</a></th>|;
622   $column_header{onhand} =
623     qq|<th nowrap><a  class=listheading href=$callback&sort=onhand&revers=$form->{revers}&lastsort=$form->{lastsort}>|
624     . $locale->text('Qty')
625     . qq|</th>|;
626   $column_header{unit} =
627     qq|<th class=listheading nowrap>| . $locale->text('Unit') . qq|</th>|;
628   $column_header{listprice} =
629       qq|<th class=listheading nowrap>|
630     . $locale->text('List Price')
631     . qq|</th>|;
632   $column_header{lastcost} =
633     qq|<th class=listheading nowrap>| . $locale->text('Last Cost') . qq|</th>|;
634   $column_header{rop} =
635     qq|<th class=listheading nowrap>| . $locale->text('ROP') . qq|</th>|;
636   $column_header{weight} =
637     qq|<th class=listheading nowrap>| . $locale->text('Weight') . qq|</th>|;
638
639   $column_header{invnumber} =
640       qq|<th nowrap><a class=listheading href=$callback&sort=invnumber>|
641     . $locale->text('Invoice Number')
642     . qq|</a></th>|;
643   $column_header{ordnumber} =
644       qq|<th nowrap><a class=listheading href=$callback&sort=ordnumber>|
645     . $locale->text('Order Number')
646     . qq|</a></th>|;
647   $column_header{quonumber} =
648       qq|<th nowrap><a class=listheading href=$callback&sort=quonumber>|
649     . $locale->text('Quotation')
650     . qq|</a></th>|;
651
652   $column_header{name} =
653       qq|<th nowrap><a class=listheading href=$callback&sort=name>|
654     . $locale->text('Name')
655     . qq|</a></th>|;
656
657   $column_header{sellprice} =
658       qq|<th class=listheading nowrap>|
659     . $locale->text('Sell Price')
660     . qq|</th>|;
661   $column_header{linetotalsellprice} =
662     qq|<th class=listheading nowrap>| . $locale->text('Extended') . qq|</th>|;
663   $column_header{linetotallastcost} =
664     qq|<th class=listheading nowrap>| . $locale->text('Extended') . qq|</th>|;
665   $column_header{linetotallistprice} =
666     qq|<th class=listheading nowrap>| . $locale->text('Extended') . qq|</th>|;
667
668   $column_header{image} =
669     qq|<th class=listheading nowrap>| . $locale->text('Image') . qq|</a></th>|;
670   $column_header{drawing} =
671       qq|<th nowrap><a class=listheading href=$callback&sort=drawing>|
672     . $locale->text('Drawing')
673     . qq|</a></th>|;
674   $column_header{microfiche} =
675       qq|<th nowrap><a class=listheading href=$callback&sort=microfiche>|
676     . $locale->text('Microfiche')
677     . qq|</a></th>|;
678
679   $column_header{serialnumber} =
680       qq|<th nowrap><a class=listheading href=$callback&sort=serialnumber>|
681     . $locale->text('Serial Number')
682     . qq|</a></th>|;
683   $column_header{soldtotal} =
684     qq|<th nowrap><a class=listheading href=$callback&sort=soldtotal&revers=$form->{revers}&lastsort=$form->{lastsort}>|
685     . $locale->text('soldtotal')
686     . qq|</a></th>|;
687
688   $form->header;
689   my $colspan = $#column_index + 1;
690
691   print qq|
692 <body>
693
694 <table width=100%>
695   <tr>
696     <th class=listtop colspan=$colspan>$form->{title}</th>
697   </tr>
698   <tr height="5"></tr>
699
700   <tr><td colspan=$colspan>$option</td></tr>
701
702   <tr class=listheading>
703 |;
704
705   map { print "\n$column_header{$_}" } @column_index;
706
707   print qq|
708   </tr>
709   |;
710
711   # add order to callback
712   $form->{callback} = $callback .= "&sort=$form->{sort}";
713
714   # escape callback for href
715   $callback = $form->escape($callback);
716
717   if (@{ $form->{parts} }) {
718     $sameitem = $form->{parts}->[0]->{ $form->{sort} };
719   }
720
721   # insert numbers for top100
722   my $j = 0;
723   foreach my $ref (@{ $form->{parts} }) {
724     $j++;
725     $ref->{number} = $j;
726   }
727
728   # if avaible -> insert choice here
729   if (($form->{ndxs_counter}) > 0) {
730     for (my $i = 1; ($i < $form->{ndxs_counter} + 1); $i++) {
731       $partnumber  = $form->{"totop100_partnumber_$i"};
732       $description = $form->{"totop100_description_$i"};
733       $unit        = $form->{"totop100_unit_$i"};
734       $sellprice   = $form->{"totop100_sellprice_$i"};
735       $soldtotal   = $form->{"totop100_soldtotal_$i"};
736
737       $totop100 .= qq|
738 <input type=hidden name=totop100_partnumber_$i value=$form->{"totop100_partnumber_$i"}>
739 <input type=hidden name=totop100_description_$i value=$form->{"totop100_description_$i"}>
740 <input type=hidden name=totop100_unit_$i value=$form->{"totop100_unit_$i"}>
741 <input type=hidden name=totop100_sellprice_$i value=$form->{"totop100_sellprice_$i"}>
742 <input type=hidden name=totop100_soldtotal_$i value=$form->{"totop100_soldtotal_$i"}>
743       |;
744
745       # insert into list
746       push @{ $form->{parts} },
747         { number      => "",
748           partnumber  => "$partnumber",
749           description => "$description",
750           unit        => "$unit",
751           sellprice   => "$sellprice",
752           soldtotal   => "$soldtotal" };
753     }    #rof
754   }    #fi
755        # build data for columns
756   my $i = 0;
757   foreach my $ref (@{ $form->{parts} }) {
758
759     if ($form->{l_subtotal} eq 'Y' && !$ref->{assemblyitem}) {
760       if ($sameitem ne $ref->{ $form->{sort} }) {
761         &parts_subtotal;
762         $sameitem = $ref->{ $form->{sort} };
763       }
764     }
765
766     $ref->{exchangerate} = 1 unless $ref->{exchangerate};
767     $ref->{sellprice} *= $ref->{exchangerate};
768     $ref->{listprice} *= $ref->{exchangerate};
769     $ref->{lastcost}  *= $ref->{exchangerate};
770
771     # use this for assemblies
772     $onhand = $ref->{onhand};
773
774     $align = "left";
775     if ($ref->{assemblyitem}) {
776       $align = "right";
777       $onhand = 0 if ($form->{sold});
778     }
779
780     $ref->{description} =~ s/\n/<br>/g;
781
782     $column_data{number} =
783         "<td align=right>"
784       . $form->format_amount(\%myconfig, $ref->{number})
785       . "</td>";
786     $column_data{partnumber} =
787       "<td align=$align>$ref->{partnumber}&nbsp;</a></td>";
788     $column_data{description} = "<td>$ref->{description}&nbsp;</td>";
789     $column_data{partsgroup}  = "<td>$ref->{partsgroup}&nbsp;</td>";
790
791     $column_data{onhand} =
792         "<td align=right>"
793       . $form->format_amount(\%myconfig, $ref->{onhand})
794       . "</td>";
795     $column_data{sellprice} =
796         "<td align=right>"
797       . $form->format_amount(\%myconfig, $ref->{sellprice})
798       . "</td>";
799     $column_data{listprice} =
800         "<td align=right>"
801       . $form->format_amount(\%myconfig, $ref->{listprice})
802       . "</td>";
803     $column_data{lastcost} =
804         "<td align=right>"
805       . $form->format_amount(\%myconfig, $ref->{lastcost})
806       . "</td>";
807
808     $column_data{linetotalsellprice} = "<td align=right>"
809       . $form->format_amount(\%myconfig, $ref->{onhand} * $ref->{sellprice}, 2)
810       . "</td>";
811     $column_data{linetotallastcost} = "<td align=right>"
812       . $form->format_amount(\%myconfig, $ref->{onhand} * $ref->{lastcost}, 2)
813       . "</td>";
814     $column_data{linetotallistprice} = "<td align=right>"
815       . $form->format_amount(\%myconfig, $ref->{onhand} * $ref->{listprice}, 2)
816       . "</td>";
817
818     if (!$ref->{assemblyitem}) {
819       $totalsellprice += $onhand * $ref->{sellprice};
820       $totallastcost  += $onhand * $ref->{lastcost};
821       $totallistprice += $onhand * $ref->{listprice};
822
823       $subtotalonhand    += $onhand;
824       $subtotalsellprice += $onhand * $ref->{sellprice};
825       $subtotallastcost  += $onhand * $ref->{lastcost};
826       $subtotallistprice += $onhand * $ref->{listprice};
827     }
828
829     $column_data{rop} =
830       "<td align=right>"
831       . $form->format_amount(\%myconfig, $ref->{rop}) . "</td>";
832     $column_data{weight} =
833         "<td align=right>"
834       . $form->format_amount(\%myconfig, $ref->{weight})
835       . "</td>";
836     $column_data{unit}        = "<td>$ref->{unit}&nbsp;</td>";
837     $column_data{bin}         = "<td>$ref->{bin}&nbsp;</td>";
838     $column_data{priceupdate} = "<td>$ref->{priceupdate}&nbsp;</td>";
839
840     $column_data{invnumber} =
841       ($ref->{module} ne 'oe')
842       ? "<td><a href=$ref->{module}.pl?action=edit&type=invoice&id=$ref->{trans_id}&callback=$callback>$ref->{invnumber}</a></td>"
843       : "<td>$ref->{invnumber}</td>";
844     $column_data{ordnumber} =
845       ($ref->{module} eq 'oe')
846       ? "<td><a href=$ref->{module}.pl?action=edit&type=$ref->{type}&id=$ref->{trans_id}&callback=$callback>$ref->{ordnumber}</a></td>"
847       : "<td>$ref->{ordnumber}</td>";
848     $column_data{quonumber} =
849       ($ref->{module} eq 'oe' && !$ref->{ordnumber})
850       ? "<td><a href=$ref->{module}.pl?action=edit&type=$ref->{type}&id=$ref->{trans_id}&callback=$callback>$ref->{quonumber}</a></td>"
851       : "<td>$ref->{quonumber}</td>";
852
853     $column_data{name} = "<td>$ref->{name}</td>";
854
855     $column_data{image} =
856       ($ref->{image})
857       ? "<td><a href=$ref->{image}><img src=$ref->{image} height=32 border=0></a></td>"
858       : "<td>&nbsp;</td>";
859     $column_data{drawing} =
860       ($ref->{drawing})
861       ? "<td><a href=$ref->{drawing}>$ref->{drawing}</a></td>"
862       : "<td>&nbsp;</td>";
863     $column_data{microfiche} =
864       ($ref->{microfiche})
865       ? "<td><a href=$ref->{microfiche}>$ref->{microfiche}</a></td>"
866       : "<td>&nbsp;</td>";
867
868     $column_data{serialnumber} = "<td>$ref->{serialnumber}</td>";
869
870     $column_data{soldtotal} = "<td  align=right>$ref->{soldtotal}</td>";
871
872     $i++;
873     $i %= 2;
874     print "<tr class=listrow$i>";
875
876     map { print "\n$column_data{$_}" } @column_index;
877
878     print qq|
879     </tr>
880 |;
881   }
882
883   if ($form->{l_subtotal} eq 'Y') {
884     &parts_subtotal;
885   }    #fi
886
887   if ($form->{"l_linetotal"}) {
888     map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
889     $column_data{linetotalsellprice} =
890         "<th class=listtotal align=right>"
891       . $form->format_amount(\%myconfig, $totalsellprice, 2)
892       . "</th>";
893     $column_data{linetotallastcost} =
894         "<th class=listtotal align=right>"
895       . $form->format_amount(\%myconfig, $totallastcost, 2)
896       . "</th>";
897     $column_data{linetotallistprice} =
898         "<th class=listtotal align=right>"
899       . $form->format_amount(\%myconfig, $totallistprice, 2)
900       . "</th>";
901
902     print "<tr class=listtotal>";
903
904     map { print "\n$column_data{$_}" } @column_index;
905
906     print qq|</tr>
907     |;
908   }
909
910   print qq|
911   <tr><td colspan=$colspan><hr size=3 noshade></td></tr>
912 </table>
913
914 |;
915
916   print qq|
917
918 <br>
919
920 <form method=post action=$form->{script}>
921
922 <input type=hidden name=itemstatus value="$form->{itemstatus}">
923 <input type=hidden name=l_linetotal value="$form->{l_linetotal}">
924 <input type=hidden name=l_partnumber value="$form->{l_partnumber}">
925 <input type=hidden name=l_description value="$form->{l_description}">
926 <input type=hidden name=l_onhand value="$form->{l_onhand}">
927 <input type=hidden name=l_unit value="$form->{l_unit}">
928 <input type=hidden name=l_sellprice value="$form->{l_sellprice}">
929 <input type=hidden name=l_linetotalsellprice value="$form->{l_linetotalsellprice}">
930 <input type=hidden name=sort value="$form->{sort}">
931 <input type=hidden name=revers value="$form->{revers}">
932 <input type=hidden name=lastsort value="$form->{lastsort}">
933 <input type=hidden name=parts value="$form->{parts}">
934
935 <input type=hidden name=bom value="$form->{bom}">
936 <input type=hidden name=titel value="$form->{titel}">
937 <input type=hidden name=searchitems value="$form->{searchitems}">|;
938
939   print $totop100;
940
941   print qq|
942     <input type=hidden name=ndxs_counter value="$form->{ndxs_counter}">
943
944     <input class=submit type=submit name=action value="|
945     . $locale->text('choice') . qq|">
946
947   </form>
948
949 </body>
950 </html>
951 |;
952
953   $lxdebug->leave_sub();
954 }    # end addtop100
955
956 #
957 # Report for Wares.
958 # Warning, deep magic ahead.
959 # This function parses the requested details, sanity checks them, and converts them into a format thats usable for IC->all_parts
960 #
961 # flags coming from the form:
962 # hardcoded:
963 #  searchitems=part revers=0 lastsort=''
964 #
965 # filter:
966 # partnumber ean description partsgroup serialnumber make model drawing microfiche
967 # transdatefrom transdateto
968 #
969 # radio:
970 #  itemstatus = active | onhand | short | obsolete | orphaned
971 #  action     = continue | top100
972 #
973 # checkboxes:
974 #  bought sold onorder ordered rfq quoted
975 #  l_partnumber l_description l_serialnumber l_unit l_listprice l_sellprice l_lastcost
976 #  l_linetotal l_priceupdate l_bin l_rop l_weight l_image l_drawing l_microfiche
977 #  l_partsgroup l_subtotal l_soldtotal l_deliverydate
978 #
979 # hiddens: 
980 #  nextsub revers lastsort sort ndxs_counter
981 #
982 sub generate_report {
983   $lxdebug->enter_sub();
984
985   $auth->assert('part_service_assembly_edit');
986
987   my ($revers, $lastsort, $description);
988
989   $form->{title} = (ucfirst $form->{searchitems}) . "s";
990   $form->{title} = $locale->text($form->{title});
991
992   my $revers     = $form->{revers};
993   my $lastsort   = $form->{lastsort};
994
995   # sorting and direction of sorting
996   # ToDO: change this to the simpler field+direction method
997   if (($form->{lastsort} eq "") && ($form->{sort} eq undef)) {
998     $form->{revers}   = 0;
999     $form->{lastsort} = "partnumber";
1000     $form->{sort}     = "partnumber";
1001   } else {
1002     if ($form->{lastsort} eq $form->{sort}) {
1003       $form->{revers} = 1 - $form->{revers};
1004     } else {
1005       $form->{revers} = 0;
1006       $form->{lastsort} = $form->{sort};
1007     }    #fi
1008   }    #fi
1009
1010   # special case if we have a serialnumber limit search
1011   # serialnumbers are only given in invoices and orders, 
1012   # so they can only pop up in bought, sold, rfq, and quoted stuff
1013   $form->{no_sn_joins} = 'Y' if (   !$form->{bought} && !$form->{sold} 
1014                                  && !$form->{rfq}    && !$form->{quoted} 
1015                                  && ($form->{l_serialnumber} || $form->{serialnumber}));
1016
1017   # special case for any checkbox of bought | sold | onorder | ordered | rfq | quoted.
1018   # if any of these are ticked the behavior changes slightly for lastcost
1019   # since all those are aggregation checks for the legder tables this is an internal switch
1020   # refered to as ledgerchecks
1021   $form->{ledgerchecks} = 'Y' if (   $form->{bought} || $form->{sold} || $form->{onorder}
1022                                   || $form->{ordered} || $form->{rfq} || $form->{quoted});
1023
1024   # if something should be activated if something else is active, enter it here
1025   my %dependencies = (
1026     onhand       => [ qw(l_onhand) ],
1027     short        => [ qw(l_onhand) ],
1028     onorder      => [ qw(l_ordnumber) ],
1029     ordered      => [ qw(l_ordnumber) ],
1030     rfq          => [ qw(l_quonumber) ],
1031     quoted       => [ qw(l_quonumber) ],
1032     bought       => [ qw(l_invnumber) ],
1033     sold         => [ qw(l_invnumber) ],
1034     ledgerchecks => [ qw(l_name) ],
1035     serialnumber => [ qw(l_serialnumber) ],
1036     no_sn_joins  => [ qw(bought sold) ],
1037   );
1038
1039   # these strings get displayed at the top of the results to indicate the user which switches were used
1040   my %optiontexts = (
1041     active        => $locale->text('Active'),
1042     obsolete      => $locale->text('Obsolete'),
1043     orphaned      => $locale->text('Orphaned'),
1044     onhand        => $locale->text('On Hand'),
1045     short         => $locale->text('Short'),
1046     onorder       => $locale->text('On Order'),
1047     ordered       => $locale->text('Ordered'),
1048     rfq           => $locale->text('RFQ'),
1049     quoted        => $locale->text('Quoted'),
1050     bought        => $locale->text('Bought'),
1051     sold          => $locale->text('Sold'),
1052     transdatefrom => $locale->text('From')       . " " . $locale->date(\%myconfig, $form->{transdatefrom}, 1),
1053     transdateto   => $locale->text('To (time)')  . " " . $locale->date(\%myconfig, $form->{transdateto}, 1),
1054     partnumber    => $locale->text('Part Number')      . ": '$form->{partnumber}'",
1055     partsgroup    => $locale->text('Group')            . ": '$form->{partsgroup}'",
1056     serialnumber  => $locale->text('Serial Number')    . ": '$form->{serialnumber}'",
1057     description   => $locale->text('Part Description') . ": '$form->{description}'",
1058     make          => $locale->text('Make')             . ": '$form->{make}'",
1059     model         => $locale->text('Model')            . ": '$form->{model}'",
1060     drawing       => $locale->text('Drawing')          . ": '$form->{drawing}'",
1061     microfiche    => $locale->text('Microfiche')       . ": '$form->{microfiche}'",
1062     l_soldtotal   => $locale->text('soldtotal'),
1063   );
1064
1065   my @itemstatus_keys = qw(active obsolete orphaned onhand short);
1066   my @callback_keys   = qw(onorder ordered rfq quoted bought sold partnumber partsgroup serialnumber description make model
1067                            drawing microfiche l_soldtotal l_deliverydate transdatefrom transdateto ean);
1068
1069   # calculate dependencies
1070   for (@itemstatus_keys, @callback_keys) {
1071     next if ($form->{itemstatus} ne $_ && !$form->{$_});
1072     map { $form->{$_} = 'Y' } @{ $dependencies{$_} } if $dependencies{$_};
1073   }
1074
1075   # generate callback and optionstrings
1076   my @options;
1077   for my  $key (@itemstatus_keys, @callback_keys) { 
1078     next if ($form->{itemstatus} ne $key && !$form->{$key});
1079     push @options, $optiontexts{$key};
1080   }
1081
1082   # special case for lastcost
1083   $form->{l_lastcost} = "" if $form->{ledgerchecks};
1084
1085   if ($form->{description}) {
1086     $description = $form->{description};
1087     $description =~ s/\n/<br>/g;
1088   }
1089
1090   if ($form->{l_linetotal}) {
1091     $form->{l_onhand} = "Y";
1092     $form->{l_linetotalsellprice} = "Y" if $form->{l_sellprice};
1093     if ($form->{l_lastcost}) {
1094       $form->{l_linetotallastcost} = "Y";
1095       if (($form->{searchitems} eq 'assembly') && !$form->{bom}) {
1096         $form->{l_linetotallastcost} = "";
1097       }
1098     }
1099     $form->{l_linetotallistprice} = "Y" if $form->{l_listprice};
1100   }
1101
1102   if ($form->{searchitems} eq 'service') {
1103
1104     # remove bin, weight and rop from list
1105     map { $form->{"l_$_"} = "" } qw(bin weight rop);
1106
1107     $form->{l_onhand} = "";
1108
1109     # qty is irrelevant unless bought or sold
1110     if (   $form->{bought}
1111         || $form->{sold}
1112         || $form->{onorder}
1113         || $form->{ordered}
1114         || $form->{rfq}
1115         || $form->{quoted}) {
1116       $form->{l_onhand} = "Y";
1117     } else {
1118       $form->{l_linetotalsellprice} = "";
1119       $form->{l_linetotallastcost}  = "";
1120     }
1121   }
1122
1123   $form->{l_lastcost} = "" if ($form->{searchitems} eq 'assembly' && !$form->{bom});
1124
1125   IC->all_parts(\%myconfig, \%$form);
1126
1127   my @columns =
1128     qw(partnumber description partsgroup bin onhand rop unit listprice linetotallistprice sellprice linetotalsellprice lastcost linetotallastcost
1129        priceupdate weight image drawing microfiche invnumber ordnumber quonumber name serialnumber soldtotal deliverydate);
1130
1131   my %column_defs = (
1132     'bin'                => { 'text' => $locale->text('Bin'), },
1133     'deliverydate'       => { 'text' => $locale->text('deliverydate'), },
1134     'description'        => { 'text' => $locale->text('Part Description'), },
1135     'drawing'            => { 'text' => $locale->text('Drawing'), },
1136     'image'              => { 'text' => $locale->text('Image'), },
1137     'invnumber'          => { 'text' => $locale->text('Invoice Number'), },
1138     'lastcost'           => { 'text' => $locale->text('Last Cost'), },
1139     'linetotallastcost'  => { 'text' => $locale->text('Extended'), },
1140     'linetotallistprice' => { 'text' => $locale->text('Extended'), },
1141     'linetotalsellprice' => { 'text' => $locale->text('Extended'), },
1142     'listprice'          => { 'text' => $locale->text('List Price'), },
1143     'microfiche'         => { 'text' => $locale->text('Microfiche'), },
1144     'name'               => { 'text' => $locale->text('Name'), },
1145     'onhand'             => { 'text' => $locale->text('Qty'), },
1146     'ordnumber'          => { 'text' => $locale->text('Order Number'), },
1147     'partnumber'         => { 'text' => $locale->text('Part Number'), },
1148     'partsgroup'         => { 'text' => $locale->text('Group'), },
1149     'priceupdate'        => { 'text' => $locale->text('Updated'), },
1150     'quonumber'          => { 'text' => $locale->text('Quotation'), },
1151     'rop'                => { 'text' => $locale->text('ROP'), },
1152     'sellprice'          => { 'text' => $locale->text('Sell Price'), },
1153     'serialnumber'       => { 'text' => $locale->text('Serial Number'), },
1154     'soldtotal'          => { 'text' => $locale->text('soldtotal'), },
1155     'unit'               => { 'text' => $locale->text('Unit'), },
1156     'weight'             => { 'text' => $locale->text('Weight'), },
1157   );
1158
1159   map { $column_defs{$_}->{visible} = $form->{"l_$_"} ? 1 : 0 } @columns;
1160   map { $column_defs{$_}->{align}   = 'right' } qw(onhand sellprice listprice lastcost linetotalsellprice linetotallastcost linetotallistprice rop weight soldtotal);
1161
1162   my @hidden_variables = (qw(l_subtotal l_linetotal searchitems itemstatus bom), @itemstatus_keys, @callback_keys, map { "l_$_" } @columns);
1163   my $callback         = build_std_url('action=generate_report', grep { $form->{$_} } @hidden_variables);
1164
1165   my @sort_full        = qw(partnumber description onhand soldtotal deliverydate);
1166   my @sort_no_revers   = qw(partsgroup bin priceupdate invnumber ordnumber quonumber name image drawing serialnumber);
1167
1168   foreach my $col (@sort_full) {
1169     $column_defs{$col}->{link} = join '&', $callback, "sort=$col", map { "$_=" . E($form->{$_}) } qw(revers lastsort);
1170   }
1171   map { $column_defs{$_}->{link} = "${callback}&sort=$_" } @sort_no_revers;
1172
1173   # add order to callback
1174   $form->{callback} = join '&', ($callback, map { "${_}=" . E($form->{$_}) } qw(sort revers));
1175
1176   my $report = SL::ReportGenerator->new(\%myconfig, $form);
1177
1178   my %attachment_basenames = (
1179     'part'     => $locale->text('part_list'),
1180     'service'  => $locale->text('service_list'),
1181     'assembly' => $locale->text('assembly_list'),
1182   );
1183
1184   $report->set_options('top_info_text'         => $locale->text('Options') . ': ' . join(', ', grep $_, @options),
1185                        'raw_bottom_info_text'  => $form->parse_html_template('ic/generate_report_bottom'),
1186                        'output_format'         => 'HTML',
1187                        'title'                 => $form->{title},
1188                        'attachment_basename'   => $attachment_basenames{$form->{searchitems}} . strftime('_%Y%m%d', localtime time),
1189   );
1190   $report->set_options_from_form();
1191
1192   $report->set_columns(%column_defs);
1193   $report->set_column_order(@columns);
1194
1195   $report->set_export_options('generate_report', @hidden_variables, qw(sort revers));
1196
1197   $report->set_sort_indicator($form->{sort}, $form->{revers} ? 0 : 1);
1198
1199   my @subtotal_columns = qw(sellprice listprice lastcost);
1200   my %subtotals = map { $_ => 0 } ('onhand', @subtotal_columns);
1201   my %totals    = map { $_ => 0 } @subtotal_columns;
1202   my $idx       = 0;
1203   my $same_item = $form->{parts}[0]{ $form->{sort} } if (scalar @{ $form->{parts} });
1204
1205   # postprocess parts
1206   foreach my $ref (@{ $form->{parts} }) {
1207
1208     # fresh row, for inserting later
1209     my $row = { map { $_ => { 'data' => $ref->{$_} } } @columns };
1210
1211     $ref->{exchangerate} ||= 1;
1212     $ref->{price_factor} ||= 1;
1213     $ref->{sellprice}     *= $ref->{exchangerate} / $ref->{price_factor};
1214     $ref->{listprice}     *= $ref->{exchangerate} / $ref->{price_factor};
1215     $ref->{lastcost}      *= $ref->{exchangerate} / $ref->{price_factor};
1216
1217     # use this for assemblies
1218     my $onhand = $ref->{onhand};
1219
1220     if ($ref->{assemblyitem}) {
1221       $row->{partnumber}{align}   = 'right';
1222       $row->{onhand}{data}        = 0;
1223       $onhand                     = 0 if ($form->{sold});
1224     }
1225
1226     my $edit_link               = build_std_url('action=edit', 'id=' . E($ref->{id}), 'callback');
1227     $row->{partnumber}->{link}  = $edit_link;
1228     $row->{description}->{link} = $edit_link;
1229
1230     foreach (qw(sellprice listprice lastcost)) {
1231       $row->{$_}{data}            = $form->format_amount(\%myconfig, $ref->{$_}, -2);
1232       $row->{"linetotal$_"}{data} = $form->format_amount(\%myconfig, $ref->{onhand} * $ref->{$_}, 2);
1233     }
1234
1235     map { $row->{$_}{data} = $form->format_amount(\%myconfig, $ref->{$_}); } qw(onhand rop weight soldtotal);
1236
1237     if (!$ref->{assemblyitem}) {
1238       foreach my $col (@subtotal_columns) {
1239         $totals{$col}    += $onhand * $ref->{$col};
1240         $subtotals{$col} += $onhand * $ref->{$col};
1241       }
1242
1243       $subtotals{onhand} += $onhand;
1244     }
1245
1246     # set module stuff
1247     if ($ref->{module} eq 'oe') {
1248       my $edit_oe_link = build_std_url("script=oe.pl", 'action=edit', 'type=' . E($ref->{type}), 'id=' . E($ref->{trans_id}), 'callback');
1249       $row->{ordnumber}{link} = $edit_oe_link;
1250       $row->{quonumber}{link} = $edit_oe_link if (!$ref->{ordnumber});
1251
1252     } else {
1253       $row->{invnumber}{link} = build_std_url("script=$ref->{module}.pl", 'action=edit', 'type=invoice', 'id=' . E($ref->{trans_id}), 'callback');
1254     }
1255
1256     # set properties of images
1257     if ($ref->{image} && (lc $report->{options}->{output_format} eq 'html')) {
1258       $row->{image}{data}     = '';
1259       $row->{image}{raw_data} = '<a href="' . H($ref->{image}) . '"><img src="' . H($ref->{image}) . '" height="32" border="0"></a>';
1260     }
1261     map { $row->{$_}{link} = $ref->{$_} } qw(drawing microfiche);
1262
1263     $report->add_data($row);
1264
1265     my $next_ref = $form->{parts}[$idx + 1];
1266
1267     # insert subtotal rows
1268     if (($form->{l_subtotal} eq 'Y') &&
1269         (!$next_ref ||
1270          (!$next_ref->{assemblyitem} && ($same_item ne $next_ref->{ $form->{sort} })))) {
1271       my $row = { map { $_ => { 'class' => 'listsubtotal', } } @columns };
1272
1273       if (($form->{searchitems} ne 'assembly') || !$form->{bom}) {
1274         $row->{onhand}->{data} = $form->format_amount(\%myconfig, $subtotals{onhand});
1275       }
1276
1277       map { $row->{"linetotal$_"}->{data} = $form->format_amount(\%myconfig, $subtotals{$_}, 2) } @subtotal_columns;
1278       map { $subtotals{$_} = 0 } ('onhand', @subtotal_columns);
1279
1280       $report->add_data($row);
1281
1282       $same_item = $next_ref->{ $form->{sort} };
1283     }
1284
1285     $idx++;
1286   }
1287
1288   if ($form->{"l_linetotal"}) {
1289     my $row = { map { $_ => { 'class' => 'listtotal', } } @columns };
1290
1291     map { $row->{"linetotal$_"}->{data} = $form->format_amount(\%myconfig, $totals{$_}, 2) } @subtotal_columns;
1292
1293     $report->add_separator();
1294     $report->add_data($row);
1295   }
1296
1297   $report->generate_with_headers();
1298
1299   $lxdebug->leave_sub();
1300 }    #end generate_report
1301
1302 sub parts_subtotal {
1303   $lxdebug->enter_sub();
1304   
1305   $auth->assert('part_service_assembly_edit');
1306
1307   # imports
1308   our (%column_data, @column_index);
1309   our ($subtotalonhand, $totalsellprice, $totallastcost, $totallistprice, $subtotalsellprice, $subtotallastcost, $subtotallistprice);
1310
1311   map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
1312   $subtotalonhand = 0 if ($form->{searchitems} eq 'assembly' && $form->{bom});
1313
1314   $column_data{onhand} =
1315       "<th class=listsubtotal align=right>"
1316     . $form->format_amount(\%myconfig, $subtotalonhand)
1317     . "</th>";
1318
1319   $column_data{linetotalsellprice} =
1320       "<th class=listsubtotal align=right>"
1321     . $form->format_amount(\%myconfig, $subtotalsellprice, 2)
1322     . "</th>";
1323   $column_data{linetotallistprice} =
1324       "<th class=listsubtotal align=right>"
1325     . $form->format_amount(\%myconfig, $subtotallistprice, 2)
1326     . "</th>";
1327   $column_data{linetotallastcost} =
1328       "<th class=listsubtotal align=right>"
1329     . $form->format_amount(\%myconfig, $subtotallastcost, 2)
1330     . "</th>";
1331
1332   $subtotalonhand    = 0;
1333   $subtotalsellprice = 0;
1334   $subtotallistprice = 0;
1335   $subtotallastcost  = 0;
1336
1337   print "<tr class=listsubtotal>";
1338
1339   map { print "\n$column_data{$_}" } @column_index;
1340
1341   print qq|
1342   </tr>
1343 |;
1344
1345   $lxdebug->leave_sub();
1346 }
1347
1348 sub edit {
1349   $lxdebug->enter_sub();
1350
1351   $auth->assert('part_service_assembly_edit');
1352
1353   # show history button
1354   $form->{javascript} = qq|<script type="text/javascript" src="js/show_history.js"></script>|;
1355   #/show hhistory button
1356   IC->get_part(\%myconfig, \%$form);
1357
1358   $form->{"original_partnumber"} = $form->{"partnumber"};
1359
1360   $form->{title} = $locale->text('Edit ' . ucfirst $form->{item});
1361
1362   &link_part;
1363   &display_form;
1364
1365   $lxdebug->leave_sub();
1366 }
1367
1368 sub link_part {
1369   $lxdebug->enter_sub();
1370
1371   $auth->assert('part_service_assembly_edit');
1372
1373   IC->create_links("IC", \%myconfig, \%$form);
1374
1375   # currencies
1376   map({ $form->{selectcurrency} .= "<option>$_\n" }
1377       split(/:/, $form->{currencies}));
1378
1379   # parts and assemblies have the same links
1380   my $item = $form->{item};
1381   if ($form->{item} eq 'assembly') {
1382     $item = 'part';
1383   }
1384
1385   # build the popup menus
1386   $form->{taxaccounts} = "";
1387   foreach my $key (keys %{ $form->{IC_links} }) {
1388     foreach my $ref (@{ $form->{IC_links}{$key} }) {
1389
1390       # if this is a tax field
1391       if ($key =~ /IC_tax/) {
1392         if ($key =~ /\Q$item\E/) {
1393           $form->{taxaccounts} .= "$ref->{accno} ";
1394           $form->{"IC_tax_$ref->{accno}_description"} =
1395             "$ref->{accno}--$ref->{description}";
1396
1397           if ($form->{id}) {
1398             if ($form->{amount}{ $ref->{accno} }) {
1399               $form->{"IC_tax_$ref->{accno}"} = "checked";
1400             }
1401           } else {
1402             $form->{"IC_tax_$ref->{accno}"} = "checked";
1403           }
1404         }
1405       } else {
1406
1407         $form->{"select$key"} .=
1408           "<option $ref->{selected}>$ref->{accno}--$ref->{description}\n";
1409         if ($form->{amount}{$key} eq $ref->{accno}) {
1410           $form->{$key} = "$ref->{accno}--$ref->{description}";
1411         }
1412
1413       }
1414     }
1415   }
1416   chop $form->{taxaccounts};
1417
1418   if (($form->{item} eq "part") || ($form->{item} eq "assembly")) {
1419     $form->{selectIC_income}  = $form->{selectIC_sale};
1420     $form->{selectIC_expense} = $form->{selectIC_cogs};
1421     $form->{IC_income}        = $form->{IC_sale};
1422     $form->{IC_expense}       = $form->{IC_cogs};
1423   }
1424
1425   delete $form->{IC_links};
1426   delete $form->{amount};
1427
1428   $form->get_partsgroup(\%myconfig, { all => 1 });
1429
1430   $form->{partsgroup} = "$form->{partsgroup}--$form->{partsgroup_id}";
1431
1432   if (@{ $form->{all_partsgroup} }) {
1433     $form->{selectpartsgroup} = qq|<option>\n|;
1434     map { $form->{selectpartsgroup} .= qq|<option value="$_->{partsgroup}--$_->{id}">$_->{partsgroup}\n| } @{ $form->{all_partsgroup} };
1435   }
1436
1437   if ($form->{item} eq 'assembly') {
1438
1439     foreach my $i (1 .. $form->{assembly_rows}) {
1440       if ($form->{"partsgroup_id_$i"}) {
1441         $form->{"partsgroup_$i"} =
1442           qq|$form->{"partsgroup_$i"}--$form->{"partsgroup_id_$i"}|;
1443       }
1444     }
1445     $form->get_partsgroup(\%myconfig);
1446
1447     if (@{ $form->{all_partsgroup} }) {
1448       $form->{selectassemblypartsgroup} = qq|<option>\n|;
1449
1450       map {
1451         $form->{selectassemblypartsgroup} .=
1452           qq|<option value="$_->{partsgroup}--$_->{id}">$_->{partsgroup}\n|
1453       } @{ $form->{all_partsgroup} };
1454     }
1455   }
1456   $lxdebug->leave_sub();
1457 }
1458
1459 sub form_header {
1460   $lxdebug->enter_sub();
1461
1462   $auth->assert('part_service_assembly_edit');
1463
1464   $form->{eur}              = $eur; # config dumps into namespace - yuck
1465   $form->{pg_keys}          = sub { "$_[0]->{partsgroup}--$_[0]->{id}" };
1466   $form->{description_area} = ($form->{rows} = $form->numtextrows($form->{description}, 40)) > 1;
1467   $form->{notes_rows}       =  max 4, $form->numtextrows($form->{notes}, 40), $form->numtextrows($form->{formel}, 40);
1468
1469   map { $form->{"is_$_"}  = ($form->{item} eq $_) } qw(part service assembly);
1470   map { $form->{$_}       =~ s/"/&quot;/g;        } qw(unit);
1471  
1472   $form->get_lists('price_factors' => 'ALL_PRICE_FACTORS', 
1473                    'partsgroup'    => 'all_partsgroup');
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->{"longdescription_$i"} = $newform{notes};
1958
1959       $form->{"sellprice_$i"} = $newform{lastcost} if ($form->{vendor_id});
1960
1961       if ($form->{exchangerate} != 0) {
1962         $form->{"sellprice_$i"} /= $form->{exchangerate};
1963       }
1964
1965       $lxdebug->message($LXDebug::DEBUG1, qq|sellprice_$i in previousform 2 = | . $form->{"sellprice_$i"} . qq|\n|);
1966
1967       map { $form->{"taxaccounts_$i"} .= "$_ " } split / /, $newform{taxaccount};
1968       chop $form->{"taxaccounts_$i"};
1969       foreach my $item (qw(description rate taxnumber)) {
1970         my $index = $form->{"taxaccounts_$i"} . "_$item";
1971         $form->{$index} = $newform{$index};
1972       }
1973
1974       # credit remaining calculation
1975       $amount = $form->{"sellprice_$i"} * (1 - $form->{"discount_$i"} / 100) * $form->{"qty_$i"};
1976
1977       map { $form->{"${_}_base"} += $amount } (split / /, $form->{"taxaccounts_$i"});
1978       map { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) } split / /, $form->{"taxaccounts_$i"} if !$form->{taxincluded};
1979
1980       $form->{creditremaining} -= $amount;
1981
1982       # redo number formatting, because invoice parse them!
1983       map { $form->{"${_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}) } qw(weight listprice sellprice rop);
1984     }
1985
1986     $form->{"id_$i"} = $parts_id;
1987
1988     # Get the actual price factor (not just the ID) for the marge calculation.
1989     $form->get_lists('price_factors' => 'ALL_PRICE_FACTORS');
1990     foreach my $pfac (@{ $form->{ALL_PRICE_FACTORS} }) {
1991       next if ($pfac->{id} != $newform{price_factor_id});
1992       $form->{"marge_price_factor_$i"} = $pfac->{factor};
1993       last;
1994     }
1995     delete $form->{ALL_PRICE_FACTORS};
1996
1997     delete $form->{action};
1998
1999     # restore original callback
2000     $callback = $form->unescape($form->{callback});
2001     $form->{callback} = $form->unescape($form->{old_callback});
2002     delete $form->{old_callback};
2003
2004     $form->{makemodel_rows}--;
2005
2006     # put callback together
2007     foreach my $key (keys %$form) {
2008
2009       # do single escape for Apache 2.0
2010       my $value = $form->escape($form->{$key}, 1);
2011       $callback .= qq|&$key=$value|;
2012     }
2013     $form->{callback} = $callback;
2014   }
2015   $lxdebug->message($LXDebug::DEBUG1, qq|ic.pl: sellprice_$i nach sub save = | . $form->{"sellprice_$i"} . qq|\n|);
2016
2017   # redirect
2018   $form->redirect;
2019
2020   $lxdebug->leave_sub();
2021 }
2022
2023 sub save_as_new {
2024   $lxdebug->enter_sub();
2025
2026   $auth->assert('part_service_assembly_edit');
2027
2028   # saving the history
2029   if(!exists $form->{addition}) {
2030     $form->{snumbers} = qq|partnumber_| . $form->{partnumber};
2031         $form->{addition} = "SAVED AS NEW";
2032         $form->save_history($form->dbconnect(\%myconfig));
2033   }
2034   # /saving the history
2035   $form->{id} = 0;
2036   if ($form->{"original_partnumber"} &&
2037       ($form->{"partnumber"} eq $form->{"original_partnumber"})) {
2038     $form->{partnumber} = "";
2039   }
2040   &save;
2041   $lxdebug->leave_sub();
2042 }
2043
2044 sub delete {
2045   $lxdebug->enter_sub();
2046
2047   $auth->assert('part_service_assembly_edit');
2048
2049   # saving the history
2050   if(!exists $form->{addition}) {
2051     $form->{snumbers} = qq|partnumber_| . $form->{partnumber};
2052         $form->{addition} = "DELETED";
2053         $form->save_history($form->dbconnect(\%myconfig));
2054   }
2055   # /saving the history
2056   my $rc = IC->delete(\%myconfig, \%$form);
2057
2058   # redirect
2059   $form->redirect($locale->text('Item deleted!')) if ($rc > 0);
2060   $form->error($locale->text('Cannot delete item!'));
2061
2062   $lxdebug->leave_sub();
2063 }
2064
2065 sub price_row {
2066   $lxdebug->enter_sub();
2067
2068   $auth->assert('part_service_assembly_edit');
2069
2070   my ($numrows) = @_;
2071
2072   my @PRICES = map +{
2073     pricegroup    => $form->{"pricegroup_$_"},
2074     pricegroup_id => $form->{"pricegroup_id_$_"},
2075     price         => $form->{"price_$_"},
2076   }, 1 .. $numrows;
2077
2078   print $form->parse_html_template('ic/price_row', { PRICES => \@PRICES });
2079
2080   $lxdebug->leave_sub();
2081 }
2082
2083 sub parts_language_selection {
2084   $lxdebug->enter_sub();
2085
2086   $auth->assert('part_service_assembly_edit');
2087
2088   our ($onload);
2089
2090   my $languages = IC->retrieve_languages(\%myconfig, $form);
2091
2092   if ($form->{language_values} ne "") {
2093     foreach $item (split(/---\+\+\+---/, $form->{language_values})) {
2094       my ($language_id, $translation, $longdescription) = split(/--\+\+--/, $item);
2095
2096       foreach my $language (@{ $languages }) {
2097         next unless ($language->{id} == $language_id);
2098
2099         $language->{translation}     = $translation;
2100         $language->{longdescription} = $longdescription;
2101         last;
2102       }
2103     }
2104   }
2105
2106   my @header_sort = qw(name longdescription);
2107   my %header_title = ( "name" => $locale->text("Name"),
2108                        "longdescription" => $locale->text("Long Description"),
2109                        );
2110
2111   my @header =
2112     map(+{ "column_title" => $header_title{$_},
2113            "column" => $_,
2114            "callback" => $callback,
2115          },
2116         @header_sort);
2117
2118   $form->{"title"} = $locale->text("Language Values");
2119   $form->header();
2120   print $form->parse_html_template("ic/parts_language_selection", { "HEADER"    => \@header,
2121                                                                     "LANGUAGES" => $languages,
2122                                                                     "onload"    => $onload });
2123
2124   $lxdebug->leave_sub();
2125 }
2126
2127 sub continue { call_sub($form->{"nextsub"}); }