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