68dcdbf9f85210e18b61db35181fbfaac9494c82
[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('transfer')
62 #  $locale->text('used')
63 #  $locale->text('return_material')
64 #  $locale->text('release_material')
65
66 # --------------------------------------------------------------------
67 # Transfer
68 # --------------------------------------------------------------------
69
70 sub transfer_warehouse_selection {
71   $lxdebug->enter_sub();
72
73   $auth->assert('warehouse_management');
74
75   $form->get_lists('warehouses' => { 'key'    => 'WAREHOUSES',
76                                      'bins'   => 'BINS', });
77
78   show_no_warehouses_error() if (!scalar @{ $form->{WAREHOUSES} });
79
80   my $units      = AM->retrieve_units(\%myconfig, $form, 'dimension');
81   $form->{UNITS} = AM->unit_select_data($units, $form->{unit}, 0, $form->{partunit});
82
83   if (scalar @{ $form->{WAREHOUSES} }) {
84     $form->{warehouse_id} ||= $form->{WAREHOUSES}->[0]->{id};
85     $form->{bin_id}       ||= $form->{WAREHOUSES}->[0]->{BINS}->[0]->{id};
86   }
87
88   my $content;
89
90   $form->{jsscript} = 1;
91
92   if ($form->{trans_type} eq 'removal') {
93     $form->{nextsub} = "removal_parts_selection";
94     $form->{title}   = $locale->text('Removal from Warehouse');
95     $content         = $form->parse_html_template('wh/warehouse_selection');
96
97   } elsif ($form->{trans_type} eq 'stock') {
98     $form->{title} = $locale->text('Stock');
99     $content       = $form->parse_html_template('wh/warehouse_selection_stock');
100
101   } elsif (!$form->{trans_type} || ($form->{trans_type} eq 'transfer')) {
102     $form->{nextsub} = "transfer_parts_selection";
103     $form->{title}   = $locale->text('Transfer');
104     $content         = $form->parse_html_template('wh/warehouse_selection');
105
106   }
107
108   $form->header();
109   print $content;
110
111   $lxdebug->leave_sub();
112 }
113
114 sub transfer_parts_selection {
115   $lxdebug->enter_sub();
116
117   $auth->assert('warehouse_management');
118
119   transfer_or_removal_prepare_contents('direction' => 'transfer');
120
121   $form->{title} = $locale->text('Transfer');
122   $form->header();
123   print $form->parse_html_template("wh/transfer_parts_selection");
124
125   $lxdebug->leave_sub();
126 }
127
128 sub transfer_or_removal_prepare_contents {
129   $lxdebug->enter_sub();
130
131   $auth->assert('warehouse_management');
132
133   my %args = @_;
134
135   $form->get_lists('warehouses' => { 'key'    => 'WAREHOUSES',
136                                      'bins'   => 'BINS', });
137
138   my $warehouse_idx = get_warehouse_idx($form->{warehouse_id});
139   $form->show_generic_error($locale->text("The selected warehouse does not exist.")) if (-1 == $warehouse_idx);
140
141   my $warehouse = $form->{WAREHOUSES}->[$warehouse_idx];
142
143   $form->{initial_warehouse_idx} = $warehouse_idx;
144   $form->{warehouse_description} = $warehouse->{description};
145   $warehouse->{selected}         = 1;
146
147   $form->show_generic_error($locale->text("The source warehouse does not contain any bins.")) if (0 == scalar @{ $warehouse->{BINS} });
148
149   map { $form->{"l_$_"} = 'Y' } qw(parts_id qty warehouseid binid partnumber partdescription bindescription chargenumber partunit);
150
151   $form->{sort} = 'bindescription';
152   my @contents  = WH->get_warehouse_report("warehouse_id" => $form->{warehouse_id},
153                                            "bin_id"       => $form->{bin_id},
154                                            "chargenumber" => $form->{chargenumber},
155                                            "partnumber"   => $form->{partnumber},
156                                            "description"  => $form->{description});
157
158   $form->show_generic_error($locale->text("The selected warehouse is empty.")) if (0 == scalar(@contents));
159
160   my $all_units = AM->retrieve_units(\%myconfig, $form, 'dimension');
161
162   foreach (@contents) {
163     $_->{qty} = $form->format_amount_units('amount'     => $_->{qty},
164                                            'part_unit'  => $_->{partunit},
165                                            'conv_units' => 'convertible');
166     my $this_unit = $_->{partunit};
167
168     if ($all_units->{$_->{partunit}} && ($all_units->{g}->{base_unit} eq $all_units->{$_->{partunit}}->{base_unit})) {
169       $this_unit = "kg";
170     }
171
172     $_->{UNITS} = AM->unit_select_data($all_units, $this_unit, 0, $_->{partunit});
173   }
174
175   my $transfer_types = WH->retrieve_transfer_types($args{direction});
176   map { $_->{description} = $locale->text($_->{description}) } @{ $transfer_types };
177
178   $form->{CONTENTS}       = \@contents;
179   $form->{TRANSFER_TYPES} = $transfer_types;
180
181   $lxdebug->leave_sub();
182 }
183
184
185 sub transfer_parts {
186   $lxdebug->enter_sub();
187
188   $auth->assert('warehouse_management');
189
190   $form->get_lists('warehouses' => { 'key' => 'WAREHOUSES', 'bins' => 'BINS' });
191
192   my $warehouse_idx = get_warehouse_idx($form->{warehouse_id});
193   $form->show_generic_error($locale->text("The selected warehouse does not exist.")) if (-1 == $warehouse_idx);
194
195   my $warehouse = $form->{WAREHOUSES}->[$warehouse_idx];
196
197   $form->show_generic_error($locale->text("The source warehouse does not contain any bins.")) if (0 == scalar @{ $warehouse->{BINS} });
198
199   map { $form->{"l_$_"} = 'Y' } qw(parts_id qty warehouseid binid partnumber partdescription bindescription chargenumber partunit);
200
201   $form->{sort} = 'bindescription';
202   my @contents  = WH->get_warehouse_report("warehouse_id" => $form->{warehouse_id});
203   my $all_units = AM->retrieve_units(\%myconfig, $form, 'dimension');
204
205   my @transfers;
206
207   foreach my $row (1 .. $form->{rowcount}) {
208     $form->{"qty_$row"} =~ s/^\s*//;
209     $form->{"qty_$row"} =~ s/\s*$//;
210     next if (!$form->{"qty_$row"});
211
212     my $bin_idx = get_bin_idx($warehouse_idx, $form->{"src_bin_id_$row"});
213     $form->show_generic_error($locale->text("The selected bin does not exist.")) if (-1 == $bin_idx);
214     my $bin     = $warehouse->{BINS}->[$bin_idx];
215
216     my $orig_qty = $form->{"qty_$row"} . " " . $form->{"unit_$row"};
217
218     my $transfer = {
219       'src_warehouse_id' => $form->{warehouse_id},
220       'transfer_type_id' => $form->{transfer_type_id},
221     };
222
223     map { $transfer->{$_} = $form->{"${_}_${row}"} } qw(src_bin_id chargenumber parts_id qty dst_warehouse_id dst_bin_id);
224
225     my $entry;
226
227     foreach (@contents) {
228       if (($_->{binid} == $transfer->{src_bin_id}) && ($_->{parts_id} == $transfer->{parts_id}) && ($_->{chargenumber} eq $transfer->{chargenumber})) {
229         $entry = $_;
230         last;
231       }
232     }
233
234     if (!$entry) {
235       $form->error($locale->text("There is not enough left of '#1' in bin '#2' for the removal of #3.",
236                                  $form->{"partdescription_$row"}, $bin->{description}, $orig_qty));
237     }
238
239     $transfer->{qty}  = $form->parse_amount(\%myconfig, $transfer->{qty}) * $all_units->{$form->{"unit_$row"}}->{factor};
240     $transfer->{qty} /= $all_units->{$entry->{partunit}}->{factor} || 1;
241
242     if (($entry->{qty} < $transfer->{qty}) || (0 >= $transfer->{qty})) {
243       $form->error($locale->text("There is not enough left of '#1' in bin '#2' for the removal of #3.",
244                                  $form->{"partdescription_$row"}, $bin->{description}, $orig_qty));
245     }
246
247     $transfer->{comment} = $form->{comment};
248
249     push @transfers, $transfer;
250
251     $entry->{qty} -= $transfer->{qty};
252   }
253
254   if (!scalar @transfers) {
255     $form->show_generic_information($locale->text('Nothing has been selected for transfer.'));
256     exit 0;
257   }
258
259   WH->transfer(@transfers);
260
261   $form->{trans_type}    = 'transfer';
262   $form->{saved_message} = $locale->text('The parts have been transferred.');
263
264   transfer_warehouse_selection();
265
266   $lxdebug->leave_sub();
267 }
268
269 # --------------------------------------------------------------------
270 # Transfer: stock
271 # --------------------------------------------------------------------
272
273 sub transfer_stock_update_part {
274   $lxdebug->enter_sub();
275
276   $form->{trans_type} = 'stock';
277   $form->{qty}        = $form->parse_amount(\%myconfig, $form->{qty});
278
279   if (!$form->{partnumber} && !$form->{description}) {
280     delete @{$form}{qw(parts_id partunit)};
281     transfer_warehouse_selection();
282
283   } elsif (($form->{partnumber} && ($form->{partnumber} ne $form->{old_partnumber})) || $form->{description}) {
284
285     $form->{no_services}   = 1;
286     $form->{no_assemblies} = 1;
287     $form->{stockable}     = 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, 'dimension');
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;