bd02fa761eeacc8f52bfcb5f50c65bde905482a8
[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} =~ s/ys$/ies/;
989   $form->{title} = $locale->text($form->{title});
990
991   my $revers     = $form->{revers};
992   my $lastsort   = $form->{lastsort};
993
994   # sorting and direction of sorting
995   # ToDO: change this to the simpler field+direction method
996   if (($form->{lastsort} eq "") && ($form->{sort} eq undef)) {
997     $form->{revers}   = 0;
998     $form->{lastsort} = "partnumber";
999     $form->{sort}     = "partnumber";
1000   } else {
1001     if ($form->{lastsort} eq $form->{sort}) {
1002       $form->{revers} = 1 - $form->{revers};
1003     } else {
1004       $form->{revers} = 0;
1005       $form->{lastsort} = $form->{sort};
1006     }    #fi
1007   }    #fi
1008
1009   # special case if we have a serialnumber limit search
1010   # serialnumbers are only given in invoices and orders, 
1011   # so they can only pop up in bought, sold, rfq, and quoted stuff
1012   $form->{no_sn_joins} = 'Y' if (   !$form->{bought} && !$form->{sold} 
1013                                  && !$form->{rfq}    && !$form->{quoted} 
1014                                  && ($form->{l_serialnumber} || $form->{serialnumber}));
1015
1016   # special case for any checkbox of bought | sold | onorder | ordered | rfq | quoted.
1017   # if any of these are ticked the behavior changes slightly for lastcost
1018   # since all those are aggregation checks for the legder tables this is an internal switch
1019   # refered to as ledgerchecks
1020   $form->{ledgerchecks} = 'Y' if (   $form->{bought} || $form->{sold} || $form->{onorder}
1021                                   || $form->{ordered} || $form->{rfq} || $form->{quoted});
1022
1023   # if something should be activated if something else is active, enter it here
1024   my %dependencies = (
1025     onhand       => [ qw(l_onhand) ],
1026     short        => [ qw(l_onhand) ],
1027     onorder      => [ qw(l_ordnumber) ],
1028     ordered      => [ qw(l_ordnumber) ],
1029     rfq          => [ qw(l_quonumber) ],
1030     quoted       => [ qw(l_quonumber) ],
1031     bought       => [ qw(l_invnumber) ],
1032     sold         => [ qw(l_invnumber) ],
1033     ledgerchecks => [ qw(l_name) ],
1034     serialnumber => [ qw(l_serialnumber) ],
1035     no_sn_joins  => [ qw(bought sold) ],
1036   );
1037
1038   # these strings get displayed at the top of the results to indicate the user which switches were used
1039   my %optiontexts = (
1040     active        => $locale->text('Active'),
1041     obsolete      => $locale->text('Obsolete'),
1042     orphaned      => $locale->text('Orphaned'),
1043     onhand        => $locale->text('On Hand'),
1044     short         => $locale->text('Short'),
1045     onorder       => $locale->text('On Order'),
1046     ordered       => $locale->text('Ordered'),
1047     rfq           => $locale->text('RFQ'),
1048     quoted        => $locale->text('Quoted'),
1049     bought        => $locale->text('Bought'),
1050     sold          => $locale->text('Sold'),
1051     transdatefrom => $locale->text('From')       . " " . $locale->date(\%myconfig, $form->{transdatefrom}, 1),
1052     transdateto   => $locale->text('To (time)')  . " " . $locale->date(\%myconfig, $form->{transdateto}, 1),
1053     partnumber    => $locale->text('Part Number')      . ": '$form->{partnumber}'",
1054     partsgroup    => $locale->text('Group')            . ": '$form->{partsgroup}'",
1055     serialnumber  => $locale->text('Serial Number')    . ": '$form->{serialnumber}'",
1056     description   => $locale->text('Part Description') . ": '$form->{description}'",
1057     make          => $locale->text('Make')             . ": '$form->{make}'",
1058     model         => $locale->text('Model')            . ": '$form->{model}'",
1059     drawing       => $locale->text('Drawing')          . ": '$form->{drawing}'",
1060     microfiche    => $locale->text('Microfiche')       . ": '$form->{microfiche}'",
1061     l_soldtotal   => $locale->text('soldtotal'),
1062   );
1063
1064   my @itemstatus_keys = qw(active obsolete orphaned onhand short);
1065   my @callback_keys   = qw(onorder ordered rfq quoted bought sold partnumber partsgroup serialnumber description make model
1066                            drawing microfiche l_soldtotal l_deliverydate transdatefrom transdateto ean);
1067
1068   # calculate dependencies
1069   for (@itemstatus_keys, @callback_keys) {
1070     next if ($form->{itemstatus} ne $_ && !$form->{$_});
1071     map { $form->{$_} = 'Y' } @{ $dependencies{$_} } if $dependencies{$_};
1072   }
1073
1074   # generate callback and optionstrings
1075   my @options;
1076   for my  $key (@itemstatus_keys, @callback_keys) { 
1077     next if ($form->{itemstatus} ne $key && !$form->{$key});
1078     push @options, $optiontexts{$key};
1079   }
1080
1081   # special case for lastcost
1082   $form->{l_lastcost} = "" if $form->{ledgerchecks};
1083
1084   if ($form->{description}) {
1085     $description = $form->{description};
1086     $description =~ s/\n/<br>/g;
1087   }
1088
1089   if ($form->{l_linetotal}) {
1090     $form->{l_onhand} = "Y";
1091     $form->{l_linetotalsellprice} = "Y" if $form->{l_sellprice};
1092     if ($form->{l_lastcost}) {
1093       $form->{l_linetotallastcost} = "Y";
1094       if (($form->{searchitems} eq 'assembly') && !$form->{bom}) {
1095         $form->{l_linetotallastcost} = "";
1096       }
1097     }
1098     $form->{l_linetotallistprice} = "Y" if $form->{l_listprice};
1099   }
1100
1101   if ($form->{searchitems} eq 'service') {
1102
1103     # remove bin, weight and rop from list
1104     map { $form->{"l_$_"} = "" } qw(bin weight rop);
1105
1106     $form->{l_onhand} = "";
1107
1108     # qty is irrelevant unless bought or sold
1109     if (   $form->{bought}
1110         || $form->{sold}
1111         || $form->{onorder}
1112         || $form->{ordered}
1113         || $form->{rfq}
1114         || $form->{quoted}) {
1115       $form->{l_onhand} = "Y";
1116     } else {
1117       $form->{l_linetotalsellprice} = "";
1118       $form->{l_linetotallastcost}  = "";
1119     }
1120   }
1121
1122   $form->{l_lastcost} = "" if ($form->{searchitems} eq 'assembly' && !$form->{bom});
1123
1124   IC->all_parts(\%myconfig, \%$form);
1125
1126   my @columns =
1127     qw(partnumber description partsgroup bin onhand rop unit listprice linetotallistprice sellprice linetotalsellprice lastcost linetotallastcost
1128        priceupdate weight image drawing microfiche invnumber ordnumber quonumber name serialnumber soldtotal deliverydate);
1129
1130   my %column_defs = (
1131     'bin'                => { 'text' => $locale->text('Bin'), },
1132     'deliverydate'       => { 'text' => $locale->text('deliverydate'), },
1133     'description'        => { 'text' => $locale->text('Part Description'), },
1134     'drawing'            => { 'text' => $locale->text('Drawing'), },
1135     'image'              => { 'text' => $locale->text('Image'), },
1136     'invnumber'          => { 'text' => $locale->text('Invoice Number'), },
1137     'lastcost'           => { 'text' => $locale->text('Last Cost'), },
1138     'linetotallastcost'  => { 'text' => $locale->text('Extended'), },
1139     'linetotallistprice' => { 'text' => $locale->text('Extended'), },
1140     'linetotalsellprice' => { 'text' => $locale->text('Extended'), },
1141     'listprice'          => { 'text' => $locale->text('List Price'), },
1142     'microfiche'         => { 'text' => $locale->text('Microfiche'), },
1143     'name'               => { 'text' => $locale->text('Name'), },
1144     'onhand'             => { 'text' => $locale->text('Qty'), },
1145     'ordnumber'          => { 'text' => $locale->text('Order Number'), },
1146     'partnumber'         => { 'text' => $locale->text('Part Number'), },
1147     'partsgroup'         => { 'text' => $locale->text('Group'), },
1148     'priceupdate'        => { 'text' => $locale->text('Updated'), },
1149     'quonumber'          => { 'text' => $locale->text('Quotation'), },
1150     'rop'                => { 'text' => $locale->text('ROP'), },
1151     'sellprice'          => { 'text' => $locale->text('Sell Price'), },
1152     'serialnumber'       => { 'text' => $locale->text('Serial Number'), },
1153     'soldtotal'          => { 'text' => $locale->text('soldtotal'), },
1154     'unit'               => { 'text' => $locale->text('Unit'), },
1155     'weight'             => { 'text' => $locale->text('Weight'), },
1156   );
1157
1158   map { $column_defs{$_}->{visible} = $form->{"l_$_"} ? 1 : 0 } @columns;
1159   map { $column_defs{$_}->{align}   = 'right' } qw(onhand sellprice listprice lastcost linetotalsellprice linetotallastcost linetotallistprice rop weight soldtotal);
1160
1161   my @hidden_variables = (qw(l_subtotal l_linetotal searchitems itemstatus bom), @itemstatus_keys, @callback_keys, map { "l_$_" } @columns);
1162   my $callback         = build_std_url('action=generate_report', grep { $form->{$_} } @hidden_variables);
1163
1164   my @sort_full        = qw(partnumber description onhand soldtotal deliverydate);
1165   my @sort_no_revers   = qw(partsgroup bin priceupdate invnumber ordnumber quonumber name image drawing serialnumber);
1166
1167   foreach my $col (@sort_full) {
1168     $column_defs{$col}->{link} = join '&', $callback, "sort=$col", map { "$_=" . E($form->{$_}) } qw(revers lastsort);
1169   }
1170   map { $column_defs{$_}->{link} = "${callback}&sort=$_" } @sort_no_revers;
1171
1172   # add order to callback
1173   $form->{callback} = join '&', ($callback, map { "${_}=" . E($form->{$_}) } qw(sort revers));
1174
1175   my $report = SL::ReportGenerator->new(\%myconfig, $form);
1176
1177   my %attachment_basenames = (
1178     'part'     => $locale->text('part_list'),
1179     'service'  => $locale->text('service_list'),
1180     'assembly' => $locale->text('assembly_list'),
1181   );
1182
1183   $report->set_options('top_info_text'         => $locale->text('Options') . ': ' . join(', ', grep $_, @options),
1184                        'raw_bottom_info_text'  => $form->parse_html_template('ic/generate_report_bottom'),
1185                        'output_format'         => 'HTML',
1186                        'title'                 => $form->{title},
1187                        'attachment_basename'   => $attachment_basenames{$form->{searchitems}} . strftime('_%Y%m%d', localtime time),
1188   );
1189   $report->set_options_from_form();
1190
1191   $report->set_columns(%column_defs);
1192   $report->set_column_order(@columns);
1193
1194   $report->set_export_options('generate_report', @hidden_variables, qw(sort revers));
1195
1196   $report->set_sort_indicator($form->{sort}, $form->{revers} ? 0 : 1);
1197
1198   my @subtotal_columns = qw(sellprice listprice lastcost);
1199   my %subtotals = map { $_ => 0 } ('onhand', @subtotal_columns);
1200   my %totals    = map { $_ => 0 } @subtotal_columns;
1201   my $idx       = 0;
1202   my $same_item = $form->{parts}[0]{ $form->{sort} } if (scalar @{ $form->{parts} });
1203
1204   # postprocess parts
1205   foreach my $ref (@{ $form->{parts} }) {
1206
1207     # fresh row, for inserting later
1208     my $row = { map { $_ => { 'data' => $ref->{$_} } } @columns };
1209
1210     $ref->{exchangerate} ||= 1;
1211     $ref->{price_factor} ||= 1;
1212     $ref->{sellprice}     *= $ref->{exchangerate} / $ref->{price_factor};
1213     $ref->{listprice}     *= $ref->{exchangerate} / $ref->{price_factor};
1214     $ref->{lastcost}      *= $ref->{exchangerate} / $ref->{price_factor};
1215
1216     # use this for assemblies
1217     my $onhand = $ref->{onhand};
1218
1219     if ($ref->{assemblyitem}) {
1220       $row->{partnumber}{align}   = 'right';
1221       $row->{onhand}{data}        = 0;
1222       $onhand                     = 0 if ($form->{sold});
1223     }
1224
1225     my $edit_link               = build_std_url('action=edit', 'id=' . E($ref->{id}), 'callback');
1226     $row->{partnumber}->{link}  = $edit_link;
1227     $row->{description}->{link} = $edit_link;
1228
1229     foreach (qw(sellprice listprice lastcost)) {
1230       $row->{$_}{data}            = $form->format_amount(\%myconfig, $ref->{$_}, -2);
1231       $row->{"linetotal$_"}{data} = $form->format_amount(\%myconfig, $ref->{onhand} * $ref->{$_}, 2);
1232     }
1233
1234     map { $row->{$_}{data} = $form->format_amount(\%myconfig, $ref->{$_}); } qw(onhand rop weight soldtotal);
1235
1236     if (!$ref->{assemblyitem}) {
1237       foreach my $col (@subtotal_columns) {
1238         $totals{$col}    += $onhand * $ref->{$col};
1239         $subtotals{$col} += $onhand * $ref->{$col};
1240       }
1241
1242       $subtotals{onhand} += $onhand;
1243     }
1244
1245     # set module stuff
1246     if ($ref->{module} eq 'oe') {
1247       my $edit_oe_link = build_std_url("script=oe.pl", 'action=edit', 'type=' . E($ref->{type}), 'id=' . E($ref->{trans_id}), 'callback');
1248       $row->{ordnumber}{link} = $edit_oe_link;
1249       $row->{quonumber}{link} = $edit_oe_link if (!$ref->{ordnumber});
1250
1251     } else {
1252       $row->{invnumber}{link} = build_std_url("script=$ref->{module}.pl", 'action=edit', 'type=invoice', 'id=' . E($ref->{trans_id}), 'callback');
1253     }
1254
1255     # set properties of images
1256     if ($ref->{image} && (lc $report->{options}->{output_format} eq 'html')) {
1257       $row->{image}{data}     = '';
1258       $row->{image}{raw_data} = '<a href="' . H($ref->{image}) . '"><img src="' . H($ref->{image}) . '" height="32" border="0"></a>';
1259     }
1260     map { $row->{$_}{link} = $ref->{$_} } qw(drawing microfiche);
1261
1262     $report->add_data($row);
1263
1264     my $next_ref = $form->{parts}[$idx + 1];
1265
1266     # insert subtotal rows
1267     if (($form->{l_subtotal} eq 'Y') &&
1268         (!$next_ref ||
1269          (!$next_ref->{assemblyitem} && ($same_item ne $next_ref->{ $form->{sort} })))) {
1270       my $row = { map { $_ => { 'class' => 'listsubtotal', } } @columns };
1271
1272       if (($form->{searchitems} ne 'assembly') || !$form->{bom}) {
1273         $row->{onhand}->{data} = $form->format_amount(\%myconfig, $subtotals{onhand});
1274       }
1275
1276       map { $row->{"linetotal$_"}->{data} = $form->format_amount(\%myconfig, $subtotals{$_}, 2) } @subtotal_columns;
1277       map { $subtotals{$_} = 0 } ('onhand', @subtotal_columns);
1278
1279       $report->add_data($row);
1280
1281       $same_item = $next_ref->{ $form->{sort} };
1282     }
1283
1284     $idx++;
1285   }
1286
1287   if ($form->{"l_linetotal"}) {
1288     my $row = { map { $_ => { 'class' => 'listtotal', } } @columns };
1289
1290     map { $row->{"linetotal$_"}->{data} = $form->format_amount(\%myconfig, $totals{$_}, 2) } @subtotal_columns;
1291
1292     $report->add_separator();
1293     $report->add_data($row);
1294   }
1295
1296   $report->generate_with_headers();
1297
1298   $lxdebug->leave_sub();
1299 }    #end generate_report
1300
1301 sub parts_subtotal {
1302   $lxdebug->enter_sub();
1303   
1304   $auth->assert('part_service_assembly_edit');
1305
1306   # imports
1307   our (%column_data, @column_index);
1308   our ($subtotalonhand, $totalsellprice, $totallastcost, $totallistprice, $subtotalsellprice, $subtotallastcost, $subtotallistprice);
1309
1310   map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
1311   $subtotalonhand = 0 if ($form->{searchitems} eq 'assembly' && $form->{bom});
1312
1313   $column_data{onhand} =
1314       "<th class=listsubtotal align=right>"
1315     . $form->format_amount(\%myconfig, $subtotalonhand)
1316     . "</th>";
1317
1318   $column_data{linetotalsellprice} =
1319       "<th class=listsubtotal align=right>"
1320     . $form->format_amount(\%myconfig, $subtotalsellprice, 2)
1321     . "</th>";
1322   $column_data{linetotallistprice} =
1323       "<th class=listsubtotal align=right>"
1324     . $form->format_amount(\%myconfig, $subtotallistprice, 2)
1325     . "</th>";
1326   $column_data{linetotallastcost} =
1327       "<th class=listsubtotal align=right>"
1328     . $form->format_amount(\%myconfig, $subtotallastcost, 2)
1329     . "</th>";
1330
1331   $subtotalonhand    = 0;
1332   $subtotalsellprice = 0;
1333   $subtotallistprice = 0;
1334   $subtotallastcost  = 0;
1335
1336   print "<tr class=listsubtotal>";
1337
1338   map { print "\n$column_data{$_}" } @column_index;
1339
1340   print qq|
1341   </tr>
1342 |;
1343
1344   $lxdebug->leave_sub();
1345 }
1346
1347 sub edit {
1348   $lxdebug->enter_sub();
1349
1350   $auth->assert('part_service_assembly_edit');
1351
1352   # show history button
1353   $form->{javascript} = qq|<script type="text/javascript" src="js/show_history.js"></script>|;
1354   #/show hhistory button
1355   IC->get_part(\%myconfig, \%$form);
1356
1357   $form->{"original_partnumber"} = $form->{"partnumber"};
1358
1359   $form->{title} = $locale->text('Edit ' . ucfirst $form->{item});
1360
1361   &link_part;
1362   &display_form;
1363
1364   $lxdebug->leave_sub();
1365 }
1366
1367 sub link_part {
1368   $lxdebug->enter_sub();
1369
1370   $auth->assert('part_service_assembly_edit');
1371
1372   IC->create_links("IC", \%myconfig, \%$form);
1373
1374   # currencies
1375   map({ $form->{selectcurrency} .= "<option>$_\n" }
1376       split(/:/, $form->{currencies}));
1377
1378   # parts and assemblies have the same links
1379   my $item = $form->{item};
1380   if ($form->{item} eq 'assembly') {
1381     $item = 'part';
1382   }
1383
1384   # build the popup menus
1385   $form->{taxaccounts} = "";
1386   foreach my $key (keys %{ $form->{IC_links} }) {
1387     foreach my $ref (@{ $form->{IC_links}{$key} }) {
1388
1389       # if this is a tax field
1390       if ($key =~ /IC_tax/) {
1391         if ($key =~ /\Q$item\E/) {
1392           $form->{taxaccounts} .= "$ref->{accno} ";
1393           $form->{"IC_tax_$ref->{accno}_description"} =
1394             "$ref->{accno}--$ref->{description}";
1395
1396           if ($form->{id}) {
1397             if ($form->{amount}{ $ref->{accno} }) {
1398               $form->{"IC_tax_$ref->{accno}"} = "checked";
1399             }
1400           } else {
1401             $form->{"IC_tax_$ref->{accno}"} = "checked";
1402           }
1403         }
1404       } else {
1405
1406         $form->{"select$key"} .=
1407           "<option $ref->{selected}>$ref->{accno}--$ref->{description}\n";
1408         if ($form->{amount}{$key} eq $ref->{accno}) {
1409           $form->{$key} = "$ref->{accno}--$ref->{description}";
1410         }
1411
1412       }
1413     }
1414   }
1415   chop $form->{taxaccounts};
1416
1417   if (($form->{item} eq "part") || ($form->{item} eq "assembly")) {
1418     $form->{selectIC_income}  = $form->{selectIC_sale};
1419     $form->{selectIC_expense} = $form->{selectIC_cogs};
1420     $form->{IC_income}        = $form->{IC_sale};
1421     $form->{IC_expense}       = $form->{IC_cogs};
1422   }
1423
1424   delete $form->{IC_links};
1425   delete $form->{amount};
1426
1427   $form->get_partsgroup(\%myconfig, { all => 1 });
1428
1429   $form->{partsgroup} = "$form->{partsgroup}--$form->{partsgroup_id}";
1430
1431   if (@{ $form->{all_partsgroup} }) {
1432     $form->{selectpartsgroup} = qq|<option>\n|;
1433     map { $form->{selectpartsgroup} .= qq|<option value="$_->{partsgroup}--$_->{id}">$_->{partsgroup}\n| } @{ $form->{all_partsgroup} };
1434   }
1435
1436   if ($form->{item} eq 'assembly') {
1437
1438     foreach my $i (1 .. $form->{assembly_rows}) {
1439       if ($form->{"partsgroup_id_$i"}) {
1440         $form->{"partsgroup_$i"} =
1441           qq|$form->{"partsgroup_$i"}--$form->{"partsgroup_id_$i"}|;
1442       }
1443     }
1444     $form->get_partsgroup(\%myconfig);
1445
1446     if (@{ $form->{all_partsgroup} }) {
1447       $form->{selectassemblypartsgroup} = qq|<option>\n|;
1448
1449       map {
1450         $form->{selectassemblypartsgroup} .=
1451           qq|<option value="$_->{partsgroup}--$_->{id}">$_->{partsgroup}\n|
1452       } @{ $form->{all_partsgroup} };
1453     }
1454   }
1455   $lxdebug->leave_sub();
1456 }
1457
1458 sub form_header {
1459   $lxdebug->enter_sub();
1460
1461   $auth->assert('part_service_assembly_edit');
1462
1463   $form->{eur}              = $eur; # config dumps into namespace - yuck
1464   $form->{pg_keys}          = sub { "$_[0]->{partsgroup}--$_[0]->{id}" };
1465   $form->{description_area} = ($form->{rows} = $form->numtextrows($form->{description}, 40)) > 1;
1466   $form->{notes_rows}       =  max 4, $form->numtextrows($form->{notes}, 40), $form->numtextrows($form->{formel}, 40);
1467
1468   map { $form->{"is_$_"}  = ($form->{item} eq $_) } qw(part service assembly);
1469   map { $form->{$_}       =~ s/"/&quot;/g;        } qw(unit);
1470  
1471   $form->get_lists('price_factors' => 'ALL_PRICE_FACTORS', 
1472                    'partsgroup'    => 'all_partsgroup');
1473
1474   IC->retrieve_buchungsgruppen(\%myconfig, $form);
1475   @{ $form->{BUCHUNGSGRUPPEN} } = grep { $_->{id} eq $form->{buchungsgruppen_id} || ($form->{id} && $form->{orphaned}) || !$form->{id} } @{ $form->{BUCHUNGSGRUPPEN} };
1476
1477   # use JavaScript Calendar or not (yes!)
1478   $form->{jsscript} = 1;
1479
1480   $units = AM->retrieve_units(\%myconfig, $form, $form->{"item"} eq "service" ? "service" : "dimension");
1481   $form->{ALL_UNITS} = [ map +{ name => $_ }, sort { $units->{$a}{sortkey} <=> $units->{$b}{sortkey} } keys %$units ];
1482
1483   $form->{fokus} = "ic.partnumber";
1484
1485   $form->header;
1486   print $form->parse_html_template('ic/form_header', { ALL_PRICE_FACTORS => $form->{ALL_PRICE_FACTORS}, 
1487                                                        ALL_UNITS         => $form->{ALL_UNITS},
1488                                                        BUCHUNGSGRUPPEN   => $form->{BUCHUNGSGRUPPEN},
1489                                                        payment_terms     => $form->{payment_terms},
1490                                                        all_partsgroup    => $form->{all_partsgroup}});
1491   $lxdebug->leave_sub();
1492 }
1493
1494 sub form_footer {
1495   $lxdebug->enter_sub();
1496
1497   $auth->assert('part_service_assembly_edit');
1498
1499   if ($form->{item} eq "assembly") {
1500
1501     print qq|
1502         <tr>
1503           <td>
1504             <table width="100%">
1505               <tr>
1506                 <th colspan=2 align=right>| . $locale->text('Total') . qq|&nbsp;</th>
1507                 <th align=right>| . $form->format_amount(\%myconfig, $form->{assemblytotal}, 2) . qq|</th>
1508               </tr>
1509             </table>
1510           </td>
1511         </tr>
1512         <input type=hidden name=assembly_rows value=$form->{assembly_rows}>
1513 |;
1514   }
1515
1516   print qq|
1517       <input type=hidden name=callback value="$form->{callback}">
1518       <input type=hidden name=previousform value="$form->{previousform}">
1519       <input type=hidden name=taxaccount2 value="$form->{taxaccount2}">
1520       <input type=hidden name=vc value=$form->{vc}>
1521   <tr>
1522     <td><hr size=3 noshade></td>
1523   </tr>
1524 </table>
1525
1526 <br>
1527 <input class=submit type=submit name=action value="|
1528     . $locale->text('Update') . qq|">
1529   |;
1530
1531   unless ($form->{item} eq "service") {
1532     print qq|
1533       <input type=hidden name=makemodel_rows value=$form->{makemodel_rows}>
1534     |;
1535   }
1536
1537   print qq|
1538      <input type=hidden name=price_rows value=$form->{price_rows}>|;
1539
1540   print qq|
1541       <input class=submit type=submit name=action value="|
1542     . $locale->text('Save') . qq|">|;
1543
1544   if ($form->{id}) {
1545
1546     if (!$form->{previousform}) {
1547       print qq|
1548       <input class=submit type=submit name=action value="|
1549         . $locale->text('Save as new') . qq|">|;
1550     }
1551
1552     if ($form->{orphaned}) {
1553       if (!$form->{previousform}) {
1554         if ($form->{item} eq 'assembly') {
1555           if (!$form->{onhand}) {
1556             print qq|
1557       <input class=submit type=submit name=action value="|
1558               . $locale->text('Delete') . qq|">|;
1559           }
1560         } else {
1561           print qq|
1562       <input class=submit type=submit name=action value="|
1563             . $locale->text('Delete') . qq|">|;
1564         }
1565       }
1566     }
1567   }
1568
1569   if (!$form->{previousform}) {
1570     if ($form->{menubar}) {
1571       require "bin/mozilla/menu.pl";
1572       &menubar;
1573     }
1574   }
1575 # button for saving history
1576   if($form->{id} ne "") {
1577         print qq|
1578                 <input type=button class=submit onclick=set_history_window(|
1579                 . $form->{id} 
1580                 . qq|); name=history id=history value=|
1581                 . $locale->text('history') 
1582                 . qq|>|;
1583   }
1584 # /button for saving history
1585   print qq|
1586
1587 </form>
1588
1589 <script type="text/javascript" src="js/wz_tooltip.js"></script>
1590
1591 </body>
1592 </html>
1593 |;
1594
1595   $lxdebug->leave_sub();
1596 }
1597
1598 sub makemodel_row {
1599   $lxdebug->enter_sub();
1600   my ($numrows) = @_;
1601   
1602   print qq|
1603   <tr>
1604     <td>
1605       <table width=100%>
1606         <tr>
1607           <th class="listheading">| . $locale->text('Make') . qq|</th>
1608           <th class="listheading">| . $locale->text('Model') . qq|</th>
1609         </tr>
1610 |;
1611
1612   for my $i (1 .. $numrows) {
1613     $form->{"make_$i"}  =~ s/\"/&quot;/g;
1614     $form->{"model_$i"} =~ s/\"/&quot;/g;
1615     print qq|
1616         <tr>
1617           <td width=50%><input name="make_$i" size=30 value="$form->{"make_$i"}"></td>
1618           <td width=50%><input name="model_$i" size=30 value="$form->{"model_$i"}"></td>
1619         </tr>
1620 |;
1621   }
1622
1623   print qq|
1624       </table>
1625     </td>
1626   </tr>
1627 |;
1628
1629   $lxdebug->leave_sub();
1630 }
1631
1632 sub assembly_row {
1633   $lxdebug->enter_sub();
1634   my ($numrows) = @_;
1635   my (@column_index, %column_data, %column_header);
1636   my ($nochange, $callback, $previousform, $linetotal, $href);
1637
1638   our ($deliverydate); # ToDO: cjeck if this indeed comes from global context
1639
1640   @column_index =
1641     qw(runningnumber qty unit bom partnumber description partsgroup total);
1642
1643   if ($form->{previousform}) {
1644     $nochange     = 1;
1645     @column_index = qw(qty unit bom partnumber description partsgroup total);
1646   } else {
1647
1648     # change callback
1649     $form->{old_callback} = $form->{callback};
1650     $callback             = $form->{callback};
1651     $form->{callback}     = "$form->{script}?action=display_form";
1652
1653     # delete action
1654     map { delete $form->{$_} } qw(action header);
1655
1656     $previousform = "";
1657
1658     # save form variables in a previousform variable
1659     foreach my $key (sort keys %$form) {
1660
1661       # escape ampersands
1662       $form->{$key} =~ s/&/%26/g;
1663       $previousform .= qq|$key=$form->{$key}&|;
1664     }
1665     chop $previousform;
1666     $previousform = $form->escape($form->escape($previousform, 1));
1667     $form->{callback} = $callback;
1668
1669     $form->{assemblytotal} = 0;
1670     $form->{weight}        = 0;
1671
1672   }
1673   $column_header{runningnumber} =
1674     qq|<th nowrap width=5%>| . $locale->text('No.') . qq|</th>|;
1675   $column_header{qty} =
1676     qq|<th align=left nowrap width=10%>| . $locale->text('Qty') . qq|</th>|;
1677   $column_header{unit} =
1678     qq|<th align=left nowrap width=5%>| . $locale->text('Unit') . qq|</th>|;
1679   $column_header{partnumber} =
1680       qq|<th align=left nowrap width=20%>|
1681     . $locale->text('Part Number')
1682     . qq|</th>|;
1683   $column_header{description} =
1684     qq|<th nowrap width=50%>| . $locale->text('Part Description') . qq|</th>|;
1685   $column_header{total} =
1686     qq|<th align=right nowrap>| . $locale->text('Extended') . qq|</th>|;
1687   $column_header{bom}        = qq|<th>| . $locale->text('BOM') . qq|</th>|;
1688   $column_header{partsgroup} = qq|<th>| . $locale->text('Group') . qq|</th>|;
1689
1690   print qq|
1691   <tr class=listheading>
1692     <th class=listheading>| . $locale->text('Individual Items') . qq|</th>
1693   </tr>
1694   <tr>
1695     <td>
1696       <table width=100%>
1697         <tr>
1698 |;
1699
1700   map { print "\n$column_header{$_}" } @column_index;
1701
1702   print qq|
1703         </tr>
1704 |;
1705
1706   for my $i (1 .. $numrows) {
1707     $form->{"partnumber_$i"} =~ s/\"/&quot;/g;
1708
1709     $linetotal =
1710       $form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"}, 2);
1711     $form->{assemblytotal} += $linetotal;
1712
1713     $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"});
1714
1715     $linetotal = $form->format_amount(\%myconfig, $linetotal, 2);
1716
1717     if (($i >= 1) && ($i == $numrows)) {
1718
1719       if ($nochange) {
1720         map { $column_data{$_} = qq|<td></td>| }
1721           qw(qty unit partnumber description bom partsgroup);
1722       } else {
1723
1724         map { $column_data{$_} = qq|<td></td>| } qw(runningnumber unit bom);
1725
1726         $column_data{qty} =
1727           qq|<td><input name="qty_$i" size=5 value="$form->{"qty_$i"}"></td>|;
1728         $column_data{partnumber} =
1729           qq|<td><input name="partnumber_$i" size=15 value="$form->{"partnumber_$i"}"></td>|;
1730         $column_data{description} =
1731           qq|<td><input name="description_$i" size=40 value="$form->{"description_$i"}"></td>|;
1732         $column_data{partsgroup} =
1733           qq|<td><input name="partsgroup_$i" size=10 value="$form->{"partsgroup_$i"}"></td>|;
1734
1735       }
1736
1737     } else {
1738
1739       if ($form->{previousform}) {
1740         $column_data{partnumber} =
1741           qq|<td><input type=hidden name="partnumber_$i" value="$form->{"partnumber_$i"}">$form->{"partnumber_$i"}</td>|;
1742         $column_data{qty} =
1743           qq|<td align=right><input type=hidden name="qty_$i" value="$form->{"qty_$i"}">$form->{"qty_$i"}</td>|;
1744
1745         $column_data{bom} =
1746           qq|<td align=center><input type=hidden name="bom_$i" value=$form->{"bom_$i"}>|;
1747         $column_data{bom} .= ($form->{"bom_$i"}) ? "x" : "&nbsp;";
1748         $column_data{bom} .= qq|</td>|;
1749
1750         $column_data{partsgroup} =
1751           qq|<td><input type=hidden name="partsgroup_$i" value="$form->{"partsgroup_$i"}">$form->{"partsgroup_$i"}</td>|;
1752
1753       } else {
1754         $href =
1755           qq|$form->{script}?action=edit&id=$form->{"id_$i"}&rowcount=$i&previousform=$previousform|;
1756         $column_data{partnumber} =
1757           qq|<td><input type=hidden name="partnumber_$i" value="$form->{"partnumber_$i"}"><a href=$href>$form->{"partnumber_$i"}</a></td>|;
1758         $column_data{runningnumber} =
1759           qq|<td><input name="runningnumber_$i" size=3 value="$i"></td>|;
1760         $column_data{qty} =
1761           qq|<td><input name="qty_$i" size=5 value="$form->{"qty_$i"}"></td>|;
1762
1763         $form->{"bom_$i"} = ($form->{"bom_$i"}) ? "checked" : "";
1764         $column_data{bom} =
1765           qq|<td align=center><input name="bom_$i" type=checkbox class=checkbox value=1 $form->{"bom_$i"}></td>|;
1766
1767         $column_data{partsgroup} =
1768           qq|<td><input type=hidden name="partsgroup_$i" value="$form->{"partsgroup_$i"}">$form->{"partsgroup_$i"}</td>|;
1769       }
1770
1771       $column_data{unit} =
1772         qq|<td><input type=hidden name="unit_$i" value="$form->{"unit_$i"}">$form->{"unit_$i"}</td>|;
1773       $column_data{description} =
1774         qq|<td><input type=hidden name="description_$i" value="$form->{"description_$i"}">$form->{"description_$i"}</td>|;
1775     }
1776
1777     $column_data{total} = qq|<td align=right>$linetotal</td>|;
1778
1779     $column_data{deliverydate} = qq|<td align=right>$deliverydate</td>|;
1780
1781     print qq|
1782         <tr>|;
1783
1784     map { print "\n$column_data{$_}" } @column_index;
1785
1786     print qq|
1787         </tr>
1788   <input type=hidden name="id_$i" value=$form->{"id_$i"}>
1789   <input type=hidden name="sellprice_$i" value=$form->{"sellprice_$i"}>
1790   <input type=hidden name="weight_$i" value=$form->{"weight_$i"}>
1791 |;
1792   }
1793
1794   print qq|
1795       </table>
1796     </td>
1797   </tr>
1798 |;
1799
1800   $lxdebug->leave_sub();
1801 }
1802
1803 sub update {
1804   $lxdebug->enter_sub();
1805
1806   if ($form->{item} eq "assembly") {
1807     my $i = $form->{assembly_rows};
1808
1809     # if last row is empty check the form otherwise retrieve item
1810     if (   ($form->{"partnumber_$i"} eq "")
1811         && ($form->{"description_$i"} eq "")
1812         && ($form->{"partsgroup_$i"}  eq "")) {
1813
1814       &check_form;
1815
1816     } else {
1817
1818       IC->assembly_item(\%myconfig, \%$form);
1819
1820       my $rows = scalar @{ $form->{item_list} };
1821
1822       if ($rows) {
1823         $form->{"qty_$i"} = 1 unless ($form->{"qty_$i"});
1824
1825         if ($rows > 1) {
1826           $form->{makemodel_rows}--;
1827           &select_item;
1828           exit;
1829         } else {
1830           map { $form->{item_list}[$i]{$_} =~ s/\"/&quot;/g }
1831             qw(partnumber description unit partsgroup);
1832           map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} }
1833             keys %{ $form->{item_list}[0] };
1834           $form->{"runningnumber_$i"} = $form->{assembly_rows};
1835           $form->{assembly_rows}++;
1836
1837           &check_form;
1838
1839         }
1840
1841       } else {
1842
1843         $form->{rowcount} = $i;
1844         $form->{assembly_rows}++;
1845
1846         &new_item;
1847
1848       }
1849     }
1850
1851   } elsif (($form->{item} eq 'part') || ($form->{item} eq 'service')) {
1852     &check_form;
1853   }
1854
1855   $lxdebug->leave_sub();
1856 }
1857
1858 sub save {
1859   $lxdebug->enter_sub();
1860
1861   $auth->assert('part_service_assembly_edit');
1862
1863   my ($parts_id, %newform, $previousform, $amount, $callback);
1864
1865   # check if there is a part number - commented out, cause there is an automatic allocation of numbers
1866   # $form->isblank("partnumber", $locale->text(ucfirst $form->{item}." Part Number missing!"));
1867
1868   # check if there is a description
1869   $form->isblank("description", $locale->text("Part Description missing!"));
1870
1871   if ($form->{obsolete}) {
1872     $form->error(
1873       $locale->text(
1874         "Inventory quantity must be zero before you can set this $form->{item} obsolete!"
1875       ))
1876       if ($form->{onhand} * 1);
1877   }
1878
1879   if (!$form->{buchungsgruppen_id}) {
1880     $form->error($locale->text("Parts must have an entry type.") . " " . 
1881      $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.") 
1882     );
1883   }
1884
1885   $form->error($locale->text('Description must not be empty!')) unless $form->{description};
1886   $form->error($locale->text('Partnumber must not be set to empty!')) if $form->{id} && !$form->{partnumber};
1887
1888   # save part
1889   $lxdebug->message($LXDebug::DEBUG1, "ic.pl: sellprice in save = $form->{sellprice}\n");
1890   if (IC->save(\%myconfig, \%$form) == 3) {
1891     $form->error($locale->text('Partnumber not unique!'));
1892   }
1893   # saving the history
1894   if(!exists $form->{addition}) {
1895     $form->{snumbers} = qq|partnumber_| . $form->{partnumber};
1896         $form->{addition} = "SAVED";
1897         $form->save_history($form->dbconnect(\%myconfig));
1898   }
1899   # /saving the history
1900   $parts_id = $form->{id};
1901
1902   my $i;
1903   # load previous variables
1904   if ($form->{previousform}) {
1905
1906     # save the new form variables before splitting previousform
1907     map { $newform{$_} = $form->{$_} } keys %$form;
1908
1909     $previousform = $form->unescape($form->{previousform});
1910
1911     # don't trample on previous variables
1912     map { delete $form->{$_} } keys %newform;
1913
1914     # now take it apart and restore original values
1915     foreach my $item (split /&/, $previousform) {
1916       my ($key, $value) = split m/=/, $item, 2;
1917       $value =~ s/%26/&/g;
1918       $form->{$key} = $value;
1919     }
1920     $form->{taxaccounts} = $newform{taxaccount2};
1921
1922     if ($form->{item} eq 'assembly') {
1923
1924       # undo number formatting
1925       map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
1926         qw(weight listprice sellprice rop);
1927
1928       $form->{assembly_rows}--;
1929       $i = $newform{rowcount};
1930       $form->{"qty_$i"} = 1 unless ($form->{"qty_$i"});
1931
1932       $form->{sellprice} -= $form->{"sellprice_$i"} * $form->{"qty_$i"};
1933       $form->{weight}    -= $form->{"weight_$i"} * $form->{"qty_$i"};
1934
1935       # change/add values for assembly item
1936       map { $form->{"${_}_$i"} = $newform{$_} } qw(partnumber description bin unit weight listprice sellprice inventory_accno income_accno expense_accno price_factor_id);
1937
1938       $form->{sellprice} += $form->{"sellprice_$i"} * $form->{"qty_$i"};
1939       $form->{weight}    += $form->{"weight_$i"} * $form->{"qty_$i"};
1940
1941     } else {
1942
1943       # set values for last invoice/order item
1944       $i = $form->{rowcount};
1945       $form->{"qty_$i"} = 1 unless ($form->{"qty_$i"});
1946
1947       map { $form->{"${_}_$i"} = $newform{$_} } qw(partnumber description bin unit listprice inventory_accno income_accno expense_accno sellprice lastcost price_factor_id);
1948
1949       $form->{"longdescription_$i"} = $newform{notes};
1950
1951       $form->{"sellprice_$i"} = $newform{lastcost} if ($form->{vendor_id});
1952
1953       if ($form->{exchangerate} != 0) {
1954         $form->{"sellprice_$i"} /= $form->{exchangerate};
1955       }
1956
1957       $lxdebug->message($LXDebug::DEBUG1, qq|sellprice_$i in previousform 2 = | . $form->{"sellprice_$i"} . qq|\n|);
1958
1959       map { $form->{"taxaccounts_$i"} .= "$_ " } split / /, $newform{taxaccount};
1960       chop $form->{"taxaccounts_$i"};
1961       foreach my $item (qw(description rate taxnumber)) {
1962         my $index = $form->{"taxaccounts_$i"} . "_$item";
1963         $form->{$index} = $newform{$index};
1964       }
1965
1966       # credit remaining calculation
1967       $amount = $form->{"sellprice_$i"} * (1 - $form->{"discount_$i"} / 100) * $form->{"qty_$i"};
1968
1969       map { $form->{"${_}_base"} += $amount } (split / /, $form->{"taxaccounts_$i"});
1970       map { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) } split / /, $form->{"taxaccounts_$i"} if !$form->{taxincluded};
1971
1972       $form->{creditremaining} -= $amount;
1973
1974       # redo number formatting, because invoice parse them!
1975       map { $form->{"${_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}) } qw(weight listprice sellprice rop);
1976     }
1977
1978     $form->{"id_$i"} = $parts_id;
1979
1980     # Get the actual price factor (not just the ID) for the marge calculation.
1981     $form->get_lists('price_factors' => 'ALL_PRICE_FACTORS');
1982     foreach my $pfac (@{ $form->{ALL_PRICE_FACTORS} }) {
1983       next if ($pfac->{id} != $newform{price_factor_id});
1984       $form->{"marge_price_factor_$i"} = $pfac->{factor};
1985       last;
1986     }
1987     delete $form->{ALL_PRICE_FACTORS};
1988
1989     delete $form->{action};
1990
1991     # restore original callback
1992     $callback = $form->unescape($form->{callback});
1993     $form->{callback} = $form->unescape($form->{old_callback});
1994     delete $form->{old_callback};
1995
1996     $form->{makemodel_rows}--;
1997
1998     # put callback together
1999     foreach my $key (keys %$form) {
2000
2001       # do single escape for Apache 2.0
2002       my $value = $form->escape($form->{$key}, 1);
2003       $callback .= qq|&$key=$value|;
2004     }
2005     $form->{callback} = $callback;
2006   }
2007   $lxdebug->message($LXDebug::DEBUG1, qq|ic.pl: sellprice_$i nach sub save = | . $form->{"sellprice_$i"} . qq|\n|);
2008
2009   # redirect
2010   $form->redirect;
2011
2012   $lxdebug->leave_sub();
2013 }
2014
2015 sub save_as_new {
2016   $lxdebug->enter_sub();
2017
2018   $auth->assert('part_service_assembly_edit');
2019
2020   # saving the history
2021   if(!exists $form->{addition}) {
2022     $form->{snumbers} = qq|partnumber_| . $form->{partnumber};
2023         $form->{addition} = "SAVED AS NEW";
2024         $form->save_history($form->dbconnect(\%myconfig));
2025   }
2026   # /saving the history
2027   $form->{id} = 0;
2028   if ($form->{"original_partnumber"} &&
2029       ($form->{"partnumber"} eq $form->{"original_partnumber"})) {
2030     $form->{partnumber} = "";
2031   }
2032   &save;
2033   $lxdebug->leave_sub();
2034 }
2035
2036 sub delete {
2037   $lxdebug->enter_sub();
2038
2039   $auth->assert('part_service_assembly_edit');
2040
2041   # saving the history
2042   if(!exists $form->{addition}) {
2043     $form->{snumbers} = qq|partnumber_| . $form->{partnumber};
2044         $form->{addition} = "DELETED";
2045         $form->save_history($form->dbconnect(\%myconfig));
2046   }
2047   # /saving the history
2048   my $rc = IC->delete(\%myconfig, \%$form);
2049
2050   # redirect
2051   $form->redirect($locale->text('Item deleted!')) if ($rc > 0);
2052   $form->error($locale->text('Cannot delete item!'));
2053
2054   $lxdebug->leave_sub();
2055 }
2056
2057 sub price_row {
2058   $lxdebug->enter_sub();
2059
2060   $auth->assert('part_service_assembly_edit');
2061
2062   my ($numrows) = @_;
2063
2064   my @PRICES = map +{
2065     pricegroup    => $form->{"pricegroup_$_"},
2066     pricegroup_id => $form->{"pricegroup_id_$_"},
2067     price         => $form->{"price_$_"},
2068   }, 1 .. $numrows;
2069
2070   print $form->parse_html_template('ic/price_row', { PRICES => \@PRICES });
2071
2072   $lxdebug->leave_sub();
2073 }
2074
2075 sub parts_language_selection {
2076   $lxdebug->enter_sub();
2077
2078   $auth->assert('part_service_assembly_edit');
2079
2080   our ($onload);
2081
2082   my $languages = IC->retrieve_languages(\%myconfig, $form);
2083
2084   if ($form->{language_values} ne "") {
2085     foreach $item (split(/---\+\+\+---/, $form->{language_values})) {
2086       my ($language_id, $translation, $longdescription) = split(/--\+\+--/, $item);
2087
2088       foreach my $language (@{ $languages }) {
2089         next unless ($language->{id} == $language_id);
2090
2091         $language->{translation}     = $translation;
2092         $language->{longdescription} = $longdescription;
2093         last;
2094       }
2095     }
2096   }
2097
2098   my @header_sort = qw(name longdescription);
2099   my %header_title = ( "name" => $locale->text("Name"),
2100                        "longdescription" => $locale->text("Long Description"),
2101                        );
2102
2103   my @header =
2104     map(+{ "column_title" => $header_title{$_},
2105            "column" => $_,
2106            "callback" => $callback,
2107          },
2108         @header_sort);
2109
2110   $form->{"title"} = $locale->text("Language Values");
2111   $form->header();
2112   print $form->parse_html_template("ic/parts_language_selection", { "HEADER"    => \@header,
2113                                                                     "LANGUAGES" => $languages,
2114                                                                     "onload"    => $onload });
2115
2116   $lxdebug->leave_sub();
2117 }
2118
2119 sub continue { call_sub($form->{"nextsub"}); }