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