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