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