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