Trennung zwischen Dienstleistungs- und Wareneinheiten aufgehoben.
[kivitendo-erp.git] / bin / mozilla / wh.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 # SQL-Ledger, Accounting
8 # Copyright (c) 1998-2002
9 #
10 #  Author: Dieter Simader
11 #   Email: dsimader@sql-ledger.org
12 #     Web: http://www.sql-ledger.org
13 #
14 #
15 # This program is free software; you can redistribute it and/or modify
16 # it under the terms of the GNU General Public License as published by
17 # the Free Software Foundation; either version 2 of the License, or
18 # (at your option) any later version.
19 #
20 # This program is distributed in the hope that it will be useful,
21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23 # GNU General Public License for more details.
24 # You should have received a copy of the GNU General Public License
25 # along with this program; if not, write to the Free Software
26 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #
28 #######################################################################
29 #
30 # warehouse and packinglist
31 #
32 #######################################################################
33
34 use List::Util qw(min max first);
35 use POSIX qw(strftime);
36
37 use SL::Form;
38 use SL::User;
39
40 use SL::AM;
41 use SL::CT;
42 use SL::IC;
43 use SL::WH;
44 use SL::OE;
45 use SL::ReportGenerator;
46
47 use Data::Dumper;
48
49 require "bin/mozilla/common.pl";
50 require "bin/mozilla/reportgenerator.pl";
51
52 # parserhappy(R):
53
54 # contents of the "transfer_type" table:
55 #  $locale->text('back')
56 #  $locale->text('correction')
57 #  $locale->text('disposed')
58 #  $locale->text('found')
59 #  $locale->text('missing')
60 #  $locale->text('stock')
61 #  $locale->text('shipped')
62 #  $locale->text('transfer')
63 #  $locale->text('used')
64 #  $locale->text('return_material')
65 #  $locale->text('release_material')
66
67 # --------------------------------------------------------------------
68 # Transfer
69 # --------------------------------------------------------------------
70
71 sub transfer_warehouse_selection {
72   $lxdebug->enter_sub();
73
74   $auth->assert('warehouse_management');
75
76   $form->get_lists('warehouses' => { 'key'    => 'WAREHOUSES',
77                                      'bins'   => 'BINS', });
78
79   show_no_warehouses_error() if (!scalar @{ $form->{WAREHOUSES} });
80
81   my $units      = AM->retrieve_units(\%myconfig, $form);
82   $form->{UNITS} = AM->unit_select_data($units, $form->{unit}, 0, $form->{partunit});
83
84   if (scalar @{ $form->{WAREHOUSES} }) {
85     $form->{warehouse_id} ||= $form->{WAREHOUSES}->[0]->{id};
86     $form->{bin_id}       ||= $form->{WAREHOUSES}->[0]->{BINS}->[0]->{id};
87   }
88
89   my $content;
90
91   $form->{jsscript} = 1;
92
93   if ($form->{trans_type} eq 'removal') {
94     $form->{nextsub} = "removal_parts_selection";
95     $form->{title}   = $locale->text('Removal from Warehouse');
96     $content         = $form->parse_html_template('wh/warehouse_selection');
97
98   } elsif ($form->{trans_type} eq 'stock') {
99     $form->{title} = $locale->text('Stock');
100     $content       = $form->parse_html_template('wh/warehouse_selection_stock');
101
102   } elsif (!$form->{trans_type} || ($form->{trans_type} eq 'transfer')) {
103     $form->{nextsub} = "transfer_parts_selection";
104     $form->{title}   = $locale->text('Transfer');
105     $content         = $form->parse_html_template('wh/warehouse_selection');
106
107   }
108
109   $form->header();
110   print $content;
111
112   $lxdebug->leave_sub();
113 }
114
115 sub transfer_parts_selection {
116   $lxdebug->enter_sub();
117
118   $auth->assert('warehouse_management');
119
120   transfer_or_removal_prepare_contents('direction' => 'transfer');
121
122   $form->{title} = $locale->text('Transfer');
123   $form->header();
124   print $form->parse_html_template("wh/transfer_parts_selection");
125
126   $lxdebug->leave_sub();
127 }
128
129 sub transfer_or_removal_prepare_contents {
130   $lxdebug->enter_sub();
131
132   $auth->assert('warehouse_management');
133
134   my %args = @_;
135
136   $form->get_lists('warehouses' => { 'key'    => 'WAREHOUSES',
137                                      'bins'   => 'BINS', });
138
139   my $warehouse_idx = get_warehouse_idx($form->{warehouse_id});
140   $form->show_generic_error($locale->text("The selected warehouse does not exist.")) if (-1 == $warehouse_idx);
141
142   my $warehouse = $form->{WAREHOUSES}->[$warehouse_idx];
143
144   $form->{initial_warehouse_idx} = $warehouse_idx;
145   $form->{warehouse_description} = $warehouse->{description};
146   $warehouse->{selected}         = 1;
147
148   $form->show_generic_error($locale->text("The source warehouse does not contain any bins.")) if (0 == scalar @{ $warehouse->{BINS} });
149
150   map { $form->{"l_$_"} = 'Y' } qw(parts_id qty warehouseid binid partnumber partdescription bindescription chargenumber partunit);
151
152   $form->{sort} = 'bindescription';
153   my @contents  = WH->get_warehouse_report("warehouse_id" => $form->{warehouse_id},
154                                            "bin_id"       => $form->{bin_id},
155                                            "chargenumber" => $form->{chargenumber},
156                                            "partnumber"   => $form->{partnumber},
157                                            "description"  => $form->{description});
158
159   $form->show_generic_error($locale->text("The selected warehouse is empty.")) if (0 == scalar(@contents));
160
161   my $all_units = AM->retrieve_units(\%myconfig, $form);
162
163   foreach (@contents) {
164     $_->{qty} = $form->format_amount_units('amount'     => $_->{qty},
165                                            'part_unit'  => $_->{partunit},
166                                            'conv_units' => 'convertible');
167     my $this_unit = $_->{partunit};
168
169     if ($all_units->{$_->{partunit}} && ($all_units->{g}->{base_unit} eq $all_units->{$_->{partunit}}->{base_unit})) {
170       $this_unit = "kg";
171     }
172
173     $_->{UNITS} = AM->unit_select_data($all_units, $this_unit, 0, $_->{partunit});
174   }
175
176   my $transfer_types = WH->retrieve_transfer_types($args{direction});
177   map { $_->{description} = $locale->text($_->{description}) } @{ $transfer_types };
178
179   $form->{CONTENTS}       = \@contents;
180   $form->{TRANSFER_TYPES} = $transfer_types;
181
182   $lxdebug->leave_sub();
183 }
184
185
186 sub transfer_parts {
187   $lxdebug->enter_sub();
188
189   $auth->assert('warehouse_management');
190
191   $form->get_lists('warehouses' => { 'key' => 'WAREHOUSES', 'bins' => 'BINS' });
192
193   my $warehouse_idx = get_warehouse_idx($form->{warehouse_id});
194   $form->show_generic_error($locale->text("The selected warehouse does not exist.")) if (-1 == $warehouse_idx);
195
196   my $warehouse = $form->{WAREHOUSES}->[$warehouse_idx];
197
198   $form->show_generic_error($locale->text("The source warehouse does not contain any bins.")) if (0 == scalar @{ $warehouse->{BINS} });
199
200   map { $form->{"l_$_"} = 'Y' } qw(parts_id qty warehouseid binid partnumber partdescription bindescription chargenumber partunit);
201
202   $form->{sort} = 'bindescription';
203   my @contents  = WH->get_warehouse_report("warehouse_id" => $form->{warehouse_id});
204   my $all_units = AM->retrieve_units(\%myconfig, $form);
205
206   my @transfers;
207
208   foreach my $row (1 .. $form->{rowcount}) {
209     $form->{"qty_$row"} =~ s/^\s*//;
210     $form->{"qty_$row"} =~ s/\s*$//;
211     next if (!$form->{"qty_$row"});
212
213     my $bin_idx = get_bin_idx($warehouse_idx, $form->{"src_bin_id_$row"});
214     $form->show_generic_error($locale->text("The selected bin does not exist.")) if (-1 == $bin_idx);
215     my $bin     = $warehouse->{BINS}->[$bin_idx];
216
217     my $orig_qty = $form->{"qty_$row"} . " " . $form->{"unit_$row"};
218
219     my $transfer = {
220       'src_warehouse_id' => $form->{warehouse_id},
221       'transfer_type_id' => $form->{transfer_type_id},
222     };
223
224     map { $transfer->{$_} = $form->{"${_}_${row}"} } qw(src_bin_id chargenumber parts_id qty dst_warehouse_id dst_bin_id);
225
226     my $entry;
227
228     foreach (@contents) {
229       if (($_->{binid} == $transfer->{src_bin_id}) && ($_->{parts_id} == $transfer->{parts_id}) && ($_->{chargenumber} eq $transfer->{chargenumber})) {
230         $entry = $_;
231         last;
232       }
233     }
234
235     if (!$entry) {
236       $form->error($locale->text("There is not enough left of '#1' in bin '#2' for the removal of #3.",
237                                  $form->{"partdescription_$row"}, $bin->{description}, $orig_qty));
238     }
239
240     $transfer->{qty}  = $form->parse_amount(\%myconfig, $transfer->{qty}) * $all_units->{$form->{"unit_$row"}}->{factor};
241     $transfer->{qty} /= $all_units->{$entry->{partunit}}->{factor} || 1;
242
243     if (($entry->{qty} < $transfer->{qty}) || (0 >= $transfer->{qty})) {
244       $form->error($locale->text("There is not enough left of '#1' in bin '#2' for the removal of #3.",
245                                  $form->{"partdescription_$row"}, $bin->{description}, $orig_qty));
246     }
247
248     $transfer->{comment} = $form->{comment};
249
250     push @transfers, $transfer;
251
252     $entry->{qty} -= $transfer->{qty};
253   }
254
255   if (!scalar @transfers) {
256     $form->show_generic_information($locale->text('Nothing has been selected for transfer.'));
257     exit 0;
258   }
259
260   WH->transfer(@transfers);
261
262   $form->{trans_type}    = 'transfer';
263   $form->{saved_message} = $locale->text('The parts have been transferred.');
264
265   transfer_warehouse_selection();
266
267   $lxdebug->leave_sub();
268 }
269
270 # --------------------------------------------------------------------
271 # Transfer: stock
272 # --------------------------------------------------------------------
273
274 sub transfer_stock_update_part {
275   $lxdebug->enter_sub();
276
277   $form->{trans_type} = 'stock';
278   $form->{qty}        = $form->parse_amount(\%myconfig, $form->{qty});
279
280   if (!$form->{partnumber} && !$form->{description}) {
281     delete @{$form}{qw(parts_id partunit)};
282     transfer_warehouse_selection();
283
284   } elsif (($form->{partnumber} && ($form->{partnumber} ne $form->{old_partnumber})) || $form->{description}) {
285
286     $form->{no_services}   = 1;
287     $form->{no_assemblies} = 1;
288
289     my $parts = Common->retrieve_parts(\%myconfig, $form, 'description', 1);
290
291     if (scalar @{ $parts } == 1) {
292       @{$form}{qw(parts_id partnumber description)} = @{$parts->[0]}{qw(id partnumber description)};
293       transfer_stock_get_partunit();
294       transfer_warehouse_selection();
295
296     } else {
297       select_part('transfer_stock_part_selected', @{ $parts });
298     }
299
300   } else {
301     transfer_stock_get_partunit();
302     transfer_warehouse_selection();
303   }
304
305   $lxdebug->leave_sub();
306 }
307
308 sub transfer_stock_part_selected {
309   $lxdebug->enter_sub();
310
311   my $part = shift;
312
313   @{$form}{qw(parts_id partnumber description)} = @{$part}{qw(id partnumber description)};
314
315   transfer_stock_get_partunit();
316   transfer_warehouse_selection();
317
318   $lxdebug->leave_sub();
319 }
320
321 sub transfer_stock_get_partunit {
322   $lxdebug->enter_sub();
323
324   if ($form->{parts_id}) {
325     my $part_info     = IC->get_basic_part_info('id' => $form->{parts_id});
326     $form->{partunit} = $part_info->{unit};
327   }
328
329   $lxdebug->leave_sub();
330 }
331
332 sub transfer_stock {
333   $lxdebug->enter_sub();
334
335   $form->{qty} = $form->parse_amount(\%myconfig, $form->{qty});
336
337   if ($form->{qty} <= 0) {
338     $form->show_generic_error($locale->text('Invalid quantity.'), 'back_button' => 1);
339   }
340
341   if (!$form->{warehouse_id} || !$form->{bin_id}) {
342     $form->error($locale->text('The warehouse or the bin is missing.'));
343   }
344
345   my $transfer = {
346     'transfer_type'    => 'stock',
347     'dst_warehouse_id' => $form->{warehouse_id},
348     'dst_bin_id'       => $form->{bin_id},
349     'chargenumber'     => $form->{chargenumber},
350     'parts_id'         => $form->{parts_id},
351     'qty'              => $form->{qty},
352     'unit'             => $form->{unit},
353     'comment'          => $form->{comment},
354   };
355
356   WH->transfer($transfer);
357
358   delete @{$form}{qw(parts_id partnumber description qty unit chargenumber comment)};
359
360   $form->{saved_message} = $locale->text('The parts have been stocked.');
361   $form->{trans_type}    = 'stock';
362
363   transfer_warehouse_selection();
364
365   $lxdebug->leave_sub();
366 }
367
368 # --------------------------------------------------------------------
369 # Transfer: removal
370 # --------------------------------------------------------------------
371
372 sub removal_parts_selection {
373   $lxdebug->enter_sub();
374
375   $auth->assert('warehouse_management');
376
377   transfer_or_removal_prepare_contents('direction' => 'out');
378
379   $form->{title} = $locale->text('Removal');
380   $form->header();
381   print $form->parse_html_template("wh/removal_parts_selection");
382
383   $lxdebug->leave_sub();
384 }
385
386 sub remove_parts {
387   $lxdebug->enter_sub();
388
389   $auth->assert('warehouse_management');
390
391   $form->get_lists('warehouses' => { 'key'    => 'WAREHOUSES',
392                                      'bins'   => 'BINS', });
393
394   my $warehouse_idx = get_warehouse_idx($form->{warehouse_id});
395   $form->show_generic_error($locale->text("The selected warehouse does not exist.")) if (-1 == $warehouse_idx);
396
397   my $warehouse = $form->{WAREHOUSES}->[$warehouse_idx];
398
399   $form->show_generic_error($locale->text("The warehouse does not contain any bins.")) if (0 == scalar @{ $warehouse->{BINS} });
400
401   map { $form->{"l_$_"} = 'Y' } qw(parts_id qty warehouseid binid partnumber partdescription bindescription chargenumber partunit);
402
403   $form->{sort} = 'bindescription';
404   my @contents  = WH->get_warehouse_report("warehouse_id" => $form->{warehouse_id});
405   my $all_units = AM->retrieve_units(\%myconfig, $form);
406
407   my @transfers;
408
409   foreach my $row (1 .. $form->{rowcount}) {
410     $form->{"qty_$row"} =~ s/^\s*//;
411     $form->{"qty_$row"} =~ s/\s*$//;
412     next if (!$form->{"qty_$row"});
413
414     my $bin_idx = get_bin_idx($warehouse_idx, $form->{"src_bin_id_$row"});
415     $form->show_generic_error($locale->text("The selected bin does not exist.")) if (-1 == $bin_idx);
416     my $bin     = $warehouse->{BINS}->[$bin_idx];
417
418     my $orig_qty = $form->{"qty_$row"} . " " . $form->{"unit_$row"};
419
420     my $transfer = {
421       'src_warehouse_id' => $form->{warehouse_id},
422       'transfer_type_id' => $form->{transfer_type_id},
423     };
424
425     map { $transfer->{$_} = $form->{"${_}_${row}"} } qw(src_bin_id chargenumber parts_id qty);
426
427     my $entry;
428
429     foreach (@contents) {
430       if (($_->{binid} == $transfer->{src_bin_id}) && ($_->{parts_id} == $transfer->{parts_id}) && ($_->{chargenumber} eq $transfer->{chargenumber})) {
431         $entry = $_;
432         last;
433       }
434     }
435
436     if (!$entry) {
437       $form->error($locale->text("There is not enough left of '#1' in bin '#2' for the removal of #3.",
438                                  $form->{"partdescription_$row"}, $bin->{description}, $orig_qty));
439     }
440
441     $transfer->{qty}  = $form->parse_amount(\%myconfig, $transfer->{qty}) * $all_units->{$form->{"unit_$row"}}->{factor};
442     $transfer->{qty} /= $all_units->{$entry->{partunit}}->{factor} || 1;
443
444     if (($entry->{qty} < $transfer->{qty}) || (0 >= $transfer->{qty})) {
445       $form->error($locale->text("There is not enough left of '#1' in bin '#2' for the removal of #3.",
446                                  $form->{"partdescription_$row"}, $bin->{description}, $orig_qty));
447     }
448
449     $transfer->{comment} = $form->{comment};
450
451     push @transfers, $transfer;
452
453     $entry->{qty} -= $transfer->{qty};
454   }
455
456   if (!scalar @transfers) {
457     $form->show_generic_information($locale->text('Nothing has been selected for removal.'));
458     exit 0;
459   }
460
461   WH->transfer(@transfers);
462
463   $form->{trans_type}    = 'removal';
464   $form->{saved_message} = $locale->text('The parts have been removed.');
465
466   transfer_warehouse_selection();
467
468   $lxdebug->leave_sub();
469 }
470
471 # --------------------------------------------------------------------
472 # Journal
473 # --------------------------------------------------------------------
474
475 sub journal {
476   $lxdebug->enter_sub();
477
478   $auth->assert('warehouse_management');
479
480   $form->get_lists('warehouses' => { 'key'  => 'WAREHOUSES',
481                                      'bins' => 'BINS', });
482
483   show_no_warehouses_error() if (!scalar @{ $form->{WAREHOUSES} });
484
485   $form->{jsscript} = 1;
486
487   $form->header();
488   print $form->parse_html_template("wh/journal_filter", { "UNITS" => AM->unit_select_data(AM->retrieve_units(\%myconfig, $form)) });
489
490   $lxdebug->leave_sub();
491 }
492
493 sub generate_journal {
494   $lxdebug->enter_sub();
495
496   $auth->assert('warehouse_management');
497
498   $form->{title}   = $locale->text("WHJournal");
499   $form->{sort}  ||= 'date';
500
501   my %filter;
502   my @columns = qw(trans_id date warehouse_from bin_from warehouse_to bin_to partnumber partdescription chargenumber trans_type comment qty employee projectnumber);
503
504   # filter stuff
505   map { $filter{$_} = $form->{$_} if ($form->{$_}) } qw(warehouse_id bin_id partnumber description chargenumber);
506
507   $filter{qty_op} = WH->convert_qty_op($form->{qty_op});
508   if ($filter{qty_op}) {
509     $form->isblank(qty,      $locale->text('Quantity missing.'));
510     $form->isblank(qty_unit, $locale->text('Unit missing.'));
511
512     $filter{qty}      = $form->{qty};
513     $filter{qty_unit} = $form->{qty_unit};
514   }
515   # /filter stuff
516
517   my $report = SL::ReportGenerator->new(\%myconfig, $form);
518
519   my @hidden_variables = map { "l_${_}" } @columns;
520   push @hidden_variables, qw(warehouse_id bin_id partnumber description chargenumber qty_op qty qty_unit fromdate todate);
521
522   my %column_defs = (
523     'date'            => { 'text' => $locale->text('Date'), },
524     'trans_id'        => { 'text' => $locale->text('Trans Id'), },
525     'trans_type'      => { 'text' => $locale->text('Trans Type'), },
526     'comment'         => { 'text' => $locale->text('Comment'), },
527     'warehouse_from'  => { 'text' => $locale->text('Warehouse From'), },
528     'warehouse_to'    => { 'text' => $locale->text('Warehouse To'), },
529     'bin_from'        => { 'text' => $locale->text('Bin From'), },
530     'bin_to'          => { 'text' => $locale->text('Bin To'), },
531     'partnumber'      => { 'text' => $locale->text('Part Number'), },
532     'partdescription' => { 'text' => $locale->text('Description'), },
533     'chargenumber'    => { 'text' => $locale->text('Charge Number'), },
534     'qty'             => { 'text' => $locale->text('Qty'), },
535     'employee'        => { 'text' => $locale->text('Employee'), },
536     'projectnumber'   => { 'text' => $locale->text('Project Number'), },
537   );
538
539   my $href = build_std_url('action=generate_journal', grep { $form->{$_} } @hidden_variables);
540   map { $column_defs{$_}->{link} = $href . "&sort=${_}&order=" . Q($_ eq $form->{sort} ? 1 - $form->{order} : $form->{order}) } @columns;
541
542   my %column_alignment = map { $_ => 'right' } qw(qty);
543
544   map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
545
546   $report->set_columns(%column_defs);
547   $report->set_column_order(@columns);
548
549   $report->set_export_options('generate_journal', @hidden_variables);
550
551   $report->set_sort_indicator($form->{sort}, $form->{order});
552
553   $report->set_options('output_format'        => 'HTML',
554                        'title'                => $form->{title},
555                        'attachment_basename'  => strftime('warehouse_journal_%Y%m%d', localtime time));
556   $report->set_options_from_form();
557
558   my $all_units = AM->retrieve_units(\%myconfig, $form);
559   my @contents  = WH->get_warehouse_journal(%filter);
560
561   foreach $entry (@contents) {
562     $entry->{qty}        = $form->format_amount_units('amount'     => $entry->{qty},
563                                                       'part_unit'  => $entry->{partunit},
564                                                       'conv_units' => 'convertible');
565     $entry->{trans_type} = $locale->text($entry->{trans_type});
566
567     my $row = { };
568
569     foreach my $column (@columns) {
570       next if ($column eq 'trans_type');
571
572       $row->{$column} = {
573         'data'  => $entry->{$column},
574         'align' => $column_alignment{$column},
575       };
576     }
577
578     $row->{trans_type} = {
579       'raw_data' => $entry->{trans_type},
580       'align'    => $column_alignment{trans_type},
581     };
582
583     $report->add_data($row);
584   }
585
586   $report->generate_with_headers();
587
588   $lxdebug->leave_sub();
589 }
590
591 # --------------------------------------------------------------------
592 # Report
593 # --------------------------------------------------------------------
594
595 sub report {
596   $lxdebug->enter_sub();
597
598   $auth->assert('warehouse_content | warehouse_management');
599
600   $form->get_lists('warehouses' => { 'key'    => 'WAREHOUSES',
601                                      'bins'   => 'BINS', });
602
603   show_no_warehouses_error() if (!scalar @{ $form->{WAREHOUSES} });
604
605   $form->{onload} .= "fokus('partnumber');";
606   $form->{title}   = $locale->text("Report about wareouse contents");
607
608   $form->header();
609   print $form->parse_html_template("wh/report_filter",
610                                    { "nextsub"    => "generate_report",
611                                      "WAREHOUSES" => $form->{WAREHOUSES},
612                                      "UNITS"      => AM->unit_select_data(AM->retrieve_units(\%myconfig, $form)) });
613
614   $lxdebug->leave_sub();
615 }
616
617 sub generate_report {
618   $lxdebug->enter_sub();
619
620   $auth->assert('warehouse_content | warehouse_management');
621
622   $form->{title}   = $locale->text("Report about wareouse contents");
623   $form->{sort}  ||= 'partnumber';
624   my $sort_col     = $form->{sort};
625
626   my %filter;
627   my @columns = qw(warehousedescription bindescription partnumber partdescription chargenumber qty);
628
629   # filter stuff
630   map { $filter{$_} = $form->{$_} if ($form->{$_}) } qw(warehouse_id bin_id partnumber description chargenumber);
631
632   $filter{qty_op} = WH->convert_qty_op($form->{qty_op});
633   if ($filter{qty_op}) {
634     $form->isblank(qty,      $locale->text('Quantity missing.'));
635     $form->isblank(qty_unit, $locale->text('Unit missing.'));
636
637     $filter{qty}      = $form->{qty};
638     $filter{qty_unit} = $form->{qty_unit};
639   }
640   # /filter stuff
641
642   $form->{subtotal} = '' if (!first { $_ eq $sort_col } qw(partnumber partdescription));
643
644   my $report = SL::ReportGenerator->new(\%myconfig, $form);
645
646   my @hidden_variables = map { "l_${_}" } @columns;
647   push @hidden_variables, qw(warehouse_id bin_id partnumber description chargenumber qty_op qty qty_unit l_warehousedescription l_bindescription);
648   push @hidden_variables, qw(include_empty_bins subtotal);
649
650   my %column_defs = (
651     'warehousedescription' => { 'text' => $locale->text('Warehouse'), },
652     'bindescription'       => { 'text' => $locale->text('Bin'), },
653     'partnumber'           => { 'text' => $locale->text('Part Number'), },
654     'partdescription'      => { 'text' => $locale->text('Description'), },
655     'chargenumber'         => { 'text' => $locale->text('Charge Number'), },
656     'qty'                  => { 'text' => $locale->text('Qty'), },
657   );
658
659   my $href = build_std_url('action=generate_report', grep { $form->{$_} } @hidden_variables);
660   map { $column_defs{$_}->{link} = $href . "&sort=${_}&order=" . Q($_ eq $sort_col ? 1 - $form->{order} : $form->{order}) } @columns;
661
662   my %column_alignment = map { $_ => 'right' } qw(qty);
663
664   map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
665
666   $report->set_columns(%column_defs);
667   $report->set_column_order(@columns);
668
669   $report->set_export_options('generate_report', @hidden_variables);
670
671   $report->set_sort_indicator($sort_col, $form->{order});
672
673   $report->set_options('output_format'        => 'HTML',
674                        'title'                => $form->{title},
675                        'attachment_basename'  => strftime('warehouse_report_%Y%m%d', localtime time));
676   $report->set_options_from_form();
677
678   my $all_units = AM->retrieve_units(\%myconfig, $form);
679   my @contents  = WH->get_warehouse_report(%filter);
680
681   my $subtotal  = 0;
682   my $idx       = 0;
683
684   foreach $entry (@contents) {
685     $subtotal     += $entry->{qty};
686     $entry->{qty}  = $form->format_amount_units('amount'     => $entry->{qty},
687                                                 'part_unit'  => $entry->{partunit},
688                                                 'conv_units' => 'convertible');
689
690     $row_set = [ { map { $_ => { 'data' => $entry->{$_}, 'align' => $column_alignment{$_} } } @columns } ];
691
692     if (($form->{subtotal} eq 'Y')
693         && (($idx == (scalar @contents - 1))
694             || ($entry->{$sort_col} ne $contents[$idx + 1]->{$sort_col}))) {
695
696       my $row = { map { $_ => { 'data' => '', 'class' => 'listsubtotal', 'align' => $column_alignment{$_}, } } @columns };
697       $row->{qty}->{data} = $form->format_amount_units('amount'     => $subtotal,
698                                                        'part_unit'  => $entry->{partunit},
699                                                        'conv_units' => 'convertible');
700       $subtotal = 0;
701
702       push @{ $row_set }, $row;
703     }
704
705     $report->add_data($row_set);
706
707     $idx++;
708   }
709
710   $report->generate_with_headers();
711
712   $lxdebug->leave_sub();
713 }
714
715 # --------------------------------------------------------------------
716 # Utility functions
717 # --------------------------------------------------------------------
718
719 sub show_no_warehouses_error {
720   $lxdebug->enter_sub();
721
722   my $msg = $locale->text('No warehouse has been created yet.') . ' ';
723
724   if ($auth->check_right($form->{login}, 'config')) {
725     $msg .= $locale->text('You can create warehouses and bins via the menu "System -> Warehouses".');
726   } else {
727     $msg .= $locale->text('Please ask your administrator to create warehouses and bins.');
728   }
729
730   $form->show_generic_error($msg);
731
732   $lxdebug->leave_sub();
733 }
734
735 sub get_warehouse_idx {
736   my ($warehouse_id) = @_;
737
738   for (my $i = 0; $i < scalar @{$form->{WAREHOUSES}}; $i++) {
739     return $i if ($form->{WAREHOUSES}->[$i]->{id} == $warehouse_id);
740   }
741
742   return -1;
743 }
744
745 sub get_bin_idx {
746   my ($warehouse_index, $bin_id) = @_;
747
748   my $warehouse = $form->{WAREHOUSES}->[$warehouse_index];
749
750   return -1 if (!$warehouse);
751
752   for (my $i = 0; $i < scalar @{ $warehouse->{BINS} }; $i++) {
753     return $i if ($warehouse->{BINS}->[$i]->{id} == $bin_id);
754   }
755
756   return -1;
757 }
758
759 sub update {
760   call_sub($form->{update_nextsub} || $form->{nextsub});
761 }
762
763 sub continue {
764   call_sub($form->{continue_nextsub} || $form->{nextsub});
765 }
766
767 sub stock {
768   call_sub($form->{stock_nextsub} || $form->{nextsub});
769 }
770
771 1;