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