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