Der Transfertyp "verschickt" wird bei Lieferscheinen benötigt.
[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, 'dimension');
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, 'dimension');
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, 'dimension');
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     $form->{stockable}     = 1;
289
290     my $parts = Common->retrieve_parts(\%myconfig, $form, 'description', 1);
291
292     if (scalar @{ $parts } == 1) {
293       @{$form}{qw(parts_id partnumber description)} = @{$parts->[0]}{qw(id partnumber description)};
294       transfer_stock_get_partunit();
295       transfer_warehouse_selection();
296
297     } else {
298       select_part('transfer_stock_part_selected', @{ $parts });
299     }
300
301   } else {
302     transfer_stock_get_partunit();
303     transfer_warehouse_selection();
304   }
305
306   $lxdebug->leave_sub();
307 }
308
309 sub transfer_stock_part_selected {
310   $lxdebug->enter_sub();
311
312   my $part = shift;
313
314   @{$form}{qw(parts_id partnumber description)} = @{$part}{qw(id partnumber description)};
315
316   transfer_stock_get_partunit();
317   transfer_warehouse_selection();
318
319   $lxdebug->leave_sub();
320 }
321
322 sub transfer_stock_get_partunit {
323   $lxdebug->enter_sub();
324
325   if ($form->{parts_id}) {
326     my $part_info     = IC->get_basic_part_info('id' => $form->{parts_id});
327     $form->{partunit} = $part_info->{unit};
328   }
329
330   $lxdebug->leave_sub();
331 }
332
333 sub transfer_stock {
334   $lxdebug->enter_sub();
335
336   $form->{qty} = $form->parse_amount(\%myconfig, $form->{qty});
337
338   if ($form->{qty} <= 0) {
339     $form->show_generic_error($locale->text('Invalid quantity.'), 'back_button' => 1);
340   }
341
342   if (!$form->{warehouse_id} || !$form->{bin_id}) {
343     $form->error($locale->text('The warehouse or the bin is missing.'));
344   }
345
346   my $transfer = {
347     'transfer_type'    => 'stock',
348     'dst_warehouse_id' => $form->{warehouse_id},
349     'dst_bin_id'       => $form->{bin_id},
350     'chargenumber'     => $form->{chargenumber},
351     'parts_id'         => $form->{parts_id},
352     'qty'              => $form->{qty},
353     'unit'             => $form->{unit},
354     'comment'          => $form->{comment},
355   };
356
357   WH->transfer($transfer);
358
359   delete @{$form}{qw(parts_id partnumber description qty unit chargenumber comment)};
360
361   $form->{saved_message} = $locale->text('The parts have been stocked.');
362   $form->{trans_type}    = 'stock';
363
364   transfer_warehouse_selection();
365
366   $lxdebug->leave_sub();
367 }
368
369 # --------------------------------------------------------------------
370 # Transfer: removal
371 # --------------------------------------------------------------------
372
373 sub removal_parts_selection {
374   $lxdebug->enter_sub();
375
376   $auth->assert('warehouse_management');
377
378   transfer_or_removal_prepare_contents('direction' => 'out');
379
380   $form->{title} = $locale->text('Removal');
381   $form->header();
382   print $form->parse_html_template("wh/removal_parts_selection");
383
384   $lxdebug->leave_sub();
385 }
386
387 sub remove_parts {
388   $lxdebug->enter_sub();
389
390   $auth->assert('warehouse_management');
391
392   $form->get_lists('warehouses' => { 'key'    => 'WAREHOUSES',
393                                      'bins'   => 'BINS', });
394
395   my $warehouse_idx = get_warehouse_idx($form->{warehouse_id});
396   $form->show_generic_error($locale->text("The selected warehouse does not exist.")) if (-1 == $warehouse_idx);
397
398   my $warehouse = $form->{WAREHOUSES}->[$warehouse_idx];
399
400   $form->show_generic_error($locale->text("The warehouse does not contain any bins.")) if (0 == scalar @{ $warehouse->{BINS} });
401
402   map { $form->{"l_$_"} = 'Y' } qw(parts_id qty warehouseid binid partnumber partdescription bindescription chargenumber partunit);
403
404   $form->{sort} = 'bindescription';
405   my @contents  = WH->get_warehouse_report("warehouse_id" => $form->{warehouse_id});
406   my $all_units = AM->retrieve_units(\%myconfig, $form, 'dimension');
407
408   my @transfers;
409
410   foreach my $row (1 .. $form->{rowcount}) {
411     $form->{"qty_$row"} =~ s/^\s*//;
412     $form->{"qty_$row"} =~ s/\s*$//;
413     next if (!$form->{"qty_$row"});
414
415     my $bin_idx = get_bin_idx($warehouse_idx, $form->{"src_bin_id_$row"});
416     $form->show_generic_error($locale->text("The selected bin does not exist.")) if (-1 == $bin_idx);
417     my $bin     = $warehouse->{BINS}->[$bin_idx];
418
419     my $orig_qty = $form->{"qty_$row"} . " " . $form->{"unit_$row"};
420
421     my $transfer = {
422       'src_warehouse_id' => $form->{warehouse_id},
423       'transfer_type_id' => $form->{transfer_type_id},
424     };
425
426     map { $transfer->{$_} = $form->{"${_}_${row}"} } qw(src_bin_id chargenumber parts_id qty);
427
428     my $entry;
429
430     foreach (@contents) {
431       if (($_->{binid} == $transfer->{src_bin_id}) && ($_->{parts_id} == $transfer->{parts_id}) && ($_->{chargenumber} eq $transfer->{chargenumber})) {
432         $entry = $_;
433         last;
434       }
435     }
436
437     if (!$entry) {
438       $form->error($locale->text("There is not enough left of '#1' in bin '#2' for the removal of #3.",
439                                  $form->{"partdescription_$row"}, $bin->{description}, $orig_qty));
440     }
441
442     $transfer->{qty}  = $form->parse_amount(\%myconfig, $transfer->{qty}) * $all_units->{$form->{"unit_$row"}}->{factor};
443     $transfer->{qty} /= $all_units->{$entry->{partunit}}->{factor} || 1;
444
445     if (($entry->{qty} < $transfer->{qty}) || (0 >= $transfer->{qty})) {
446       $form->error($locale->text("There is not enough left of '#1' in bin '#2' for the removal of #3.",
447                                  $form->{"partdescription_$row"}, $bin->{description}, $orig_qty));
448     }
449
450     $transfer->{comment} = $form->{comment};
451
452     push @transfers, $transfer;
453
454     $entry->{qty} -= $transfer->{qty};
455   }
456
457   if (!scalar @transfers) {
458     $form->show_generic_information($locale->text('Nothing has been selected for removal.'));
459     exit 0;
460   }
461
462   WH->transfer(@transfers);
463
464   $form->{trans_type}    = 'removal';
465   $form->{saved_message} = $locale->text('The parts have been removed.');
466
467   transfer_warehouse_selection();
468
469   $lxdebug->leave_sub();
470 }
471
472 # --------------------------------------------------------------------
473 # Journal
474 # --------------------------------------------------------------------
475
476 sub journal {
477   $lxdebug->enter_sub();
478
479   $auth->assert('warehouse_management');
480
481   $form->get_lists('warehouses' => { 'key'  => 'WAREHOUSES',
482                                      'bins' => 'BINS', });
483
484   show_no_warehouses_error() if (!scalar @{ $form->{WAREHOUSES} });
485
486   $form->{jsscript} = 1;
487
488   $form->header();
489   print $form->parse_html_template("wh/journal_filter", { "UNITS" => AM->unit_select_data(AM->retrieve_units(\%myconfig, $form)) });
490
491   $lxdebug->leave_sub();
492 }
493
494 sub generate_journal {
495   $lxdebug->enter_sub();
496
497   $auth->assert('warehouse_management');
498
499   $form->{title}   = $locale->text("WHJournal");
500   $form->{sort}  ||= 'date';
501
502   my %filter;
503   my @columns = qw(trans_id date warehouse_from bin_from warehouse_to bin_to partnumber partdescription chargenumber trans_type comment qty employee projectnumber);
504
505   # filter stuff
506   map { $filter{$_} = $form->{$_} if ($form->{$_}) } qw(warehouse_id bin_id partnumber description chargenumber);
507
508   $filter{qty_op} = WH->convert_qty_op($form->{qty_op});
509   if ($filter{qty_op}) {
510     $form->isblank(qty,      $locale->text('Quantity missing.'));
511     $form->isblank(qty_unit, $locale->text('Unit missing.'));
512
513     $filter{qty}      = $form->{qty};
514     $filter{qty_unit} = $form->{qty_unit};
515   }
516   # /filter stuff
517
518   my $report = SL::ReportGenerator->new(\%myconfig, $form);
519
520   my @hidden_variables = map { "l_${_}" } @columns;
521   push @hidden_variables, qw(warehouse_id bin_id partnumber description chargenumber qty_op qty qty_unit fromdate todate);
522
523   my %column_defs = (
524     'date'            => { 'text' => $locale->text('Date'), },
525     'trans_id'        => { 'text' => $locale->text('Trans Id'), },
526     'trans_type'      => { 'text' => $locale->text('Trans Type'), },
527     'comment'         => { 'text' => $locale->text('Comment'), },
528     'warehouse_from'  => { 'text' => $locale->text('Warehouse From'), },
529     'warehouse_to'    => { 'text' => $locale->text('Warehouse To'), },
530     'bin_from'        => { 'text' => $locale->text('Bin From'), },
531     'bin_to'          => { 'text' => $locale->text('Bin To'), },
532     'partnumber'      => { 'text' => $locale->text('Part Number'), },
533     'partdescription' => { 'text' => $locale->text('Description'), },
534     'chargenumber'    => { 'text' => $locale->text('Charge Number'), },
535     'qty'             => { 'text' => $locale->text('Qty'), },
536     'employee'        => { 'text' => $locale->text('Employee'), },
537     'projectnumber'   => { 'text' => $locale->text('Project Number'), },
538   );
539
540   my $href = build_std_url('action=generate_journal', grep { $form->{$_} } @hidden_variables);
541   map { $column_defs{$_}->{link} = $href . "&sort=${_}&order=" . Q($_ eq $form->{sort} ? 1 - $form->{order} : $form->{order}) } @columns;
542
543   my %column_alignment = map { $_ => 'right' } qw(qty);
544
545   map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
546
547   $report->set_columns(%column_defs);
548   $report->set_column_order(@columns);
549
550   $report->set_export_options('generate_journal', @hidden_variables);
551
552   $report->set_sort_indicator($form->{sort}, $form->{order});
553
554   $report->set_options('output_format'        => 'HTML',
555                        'title'                => $form->{title},
556                        'attachment_basename'  => strftime('warehouse_journal_%Y%m%d', localtime time));
557   $report->set_options_from_form();
558
559   my $all_units = AM->retrieve_units(\%myconfig, $form);
560   my @contents  = WH->get_warehouse_journal(%filter);
561
562   foreach $entry (@contents) {
563     $entry->{qty}        = $form->format_amount_units('amount'     => $entry->{qty},
564                                                       'part_unit'  => $entry->{partunit},
565                                                       'conv_units' => 'convertible');
566     $entry->{trans_type} = $locale->text($entry->{trans_type});
567
568     my $row = { };
569
570     foreach my $column (@columns) {
571       next if ($column eq 'trans_type');
572
573       $row->{$column} = {
574         'data'  => $entry->{$column},
575         'align' => $column_alignment{$column},
576       };
577     }
578
579     $row->{trans_type} = {
580       'raw_data' => $entry->{trans_type},
581       'align'    => $column_alignment{trans_type},
582     };
583
584     $report->add_data($row);
585   }
586
587   $report->generate_with_headers();
588
589   $lxdebug->leave_sub();
590 }
591
592 # --------------------------------------------------------------------
593 # Report
594 # --------------------------------------------------------------------
595
596 sub report {
597   $lxdebug->enter_sub();
598
599   $auth->assert('warehouse_content | warehouse_management');
600
601   $form->get_lists('warehouses' => { 'key'    => 'WAREHOUSES',
602                                      'bins'   => 'BINS', });
603
604   show_no_warehouses_error() if (!scalar @{ $form->{WAREHOUSES} });
605
606   $form->{onload} .= "fokus('partnumber');";
607   $form->{title}   = $locale->text("Report about wareouse contents");
608
609   $form->header();
610   print $form->parse_html_template("wh/report_filter",
611                                    { "nextsub"    => "generate_report",
612                                      "WAREHOUSES" => $form->{WAREHOUSES},
613                                      "UNITS"      => AM->unit_select_data(AM->retrieve_units(\%myconfig, $form)) });
614
615   $lxdebug->leave_sub();
616 }
617
618 sub generate_report {
619   $lxdebug->enter_sub();
620
621   $auth->assert('warehouse_content | warehouse_management');
622
623   $form->{title}   = $locale->text("Report about wareouse contents");
624   $form->{sort}  ||= 'partnumber';
625   my $sort_col     = $form->{sort};
626
627   my %filter;
628   my @columns = qw(warehousedescription bindescription partnumber partdescription chargenumber qty);
629
630   # filter stuff
631   map { $filter{$_} = $form->{$_} if ($form->{$_}) } qw(warehouse_id bin_id partnumber description chargenumber);
632
633   $filter{qty_op} = WH->convert_qty_op($form->{qty_op});
634   if ($filter{qty_op}) {
635     $form->isblank(qty,      $locale->text('Quantity missing.'));
636     $form->isblank(qty_unit, $locale->text('Unit missing.'));
637
638     $filter{qty}      = $form->{qty};
639     $filter{qty_unit} = $form->{qty_unit};
640   }
641   # /filter stuff
642
643   $form->{subtotal} = '' if (!first { $_ eq $sort_col } qw(partnumber partdescription));
644
645   my $report = SL::ReportGenerator->new(\%myconfig, $form);
646
647   my @hidden_variables = map { "l_${_}" } @columns;
648   push @hidden_variables, qw(warehouse_id bin_id partnumber description chargenumber qty_op qty qty_unit l_warehousedescription l_bindescription);
649   push @hidden_variables, qw(include_empty_bins subtotal);
650
651   my %column_defs = (
652     'warehousedescription' => { 'text' => $locale->text('Warehouse'), },
653     'bindescription'       => { 'text' => $locale->text('Bin'), },
654     'partnumber'           => { 'text' => $locale->text('Part Number'), },
655     'partdescription'      => { 'text' => $locale->text('Description'), },
656     'chargenumber'         => { 'text' => $locale->text('Charge Number'), },
657     'qty'                  => { 'text' => $locale->text('Qty'), },
658   );
659
660   my $href = build_std_url('action=generate_report', grep { $form->{$_} } @hidden_variables);
661   map { $column_defs{$_}->{link} = $href . "&sort=${_}&order=" . Q($_ eq $sort_col ? 1 - $form->{order} : $form->{order}) } @columns;
662
663   my %column_alignment = map { $_ => 'right' } qw(qty);
664
665   map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
666
667   $report->set_columns(%column_defs);
668   $report->set_column_order(@columns);
669
670   $report->set_export_options('generate_report', @hidden_variables);
671
672   $report->set_sort_indicator($sort_col, $form->{order});
673
674   $report->set_options('output_format'        => 'HTML',
675                        'title'                => $form->{title},
676                        'attachment_basename'  => strftime('warehouse_report_%Y%m%d', localtime time));
677   $report->set_options_from_form();
678
679   my $all_units = AM->retrieve_units(\%myconfig, $form);
680   my @contents  = WH->get_warehouse_report(%filter);
681
682   my $subtotal  = 0;
683   my $idx       = 0;
684
685   foreach $entry (@contents) {
686     $subtotal     += $entry->{qty};
687     $entry->{qty}  = $form->format_amount_units('amount'     => $entry->{qty},
688                                                 'part_unit'  => $entry->{partunit},
689                                                 'conv_units' => 'convertible');
690
691     $row_set = [ { map { $_ => { 'data' => $entry->{$_}, 'align' => $column_alignment{$_} } } @columns } ];
692
693     if (($form->{subtotal} eq 'Y')
694         && (($idx == (scalar @contents - 1))
695             || ($entry->{$sort_col} ne $contents[$idx + 1]->{$sort_col}))) {
696
697       my $row = { map { $_ => { 'data' => '', 'class' => 'listsubtotal', 'align' => $column_alignment{$_}, } } @columns };
698       $row->{qty}->{data} = $form->format_amount_units('amount'     => $subtotal,
699                                                        'part_unit'  => $entry->{partunit},
700                                                        'conv_units' => 'convertible');
701       $subtotal = 0;
702
703       push @{ $row_set }, $row;
704     }
705
706     $report->add_data($row_set);
707
708     $idx++;
709   }
710
711   $report->generate_with_headers();
712
713   $lxdebug->leave_sub();
714 }
715
716 # --------------------------------------------------------------------
717 # Utility functions
718 # --------------------------------------------------------------------
719
720 sub show_no_warehouses_error {
721   $lxdebug->enter_sub();
722
723   my $msg = $locale->text('No warehouse has been created yet.') . ' ';
724
725   if ($auth->check_right($form->{login}, 'config')) {
726     $msg .= $locale->text('You can create warehouses and bins via the menu "System -> Warehouses".');
727   } else {
728     $msg .= $locale->text('Please ask your administrator to create warehouses and bins.');
729   }
730
731   $form->show_generic_error($msg);
732
733   $lxdebug->leave_sub();
734 }
735
736 sub get_warehouse_idx {
737   my ($warehouse_id) = @_;
738
739   for (my $i = 0; $i < scalar @{$form->{WAREHOUSES}}; $i++) {
740     return $i if ($form->{WAREHOUSES}->[$i]->{id} == $warehouse_id);
741   }
742
743   return -1;
744 }
745
746 sub get_bin_idx {
747   my ($warehouse_index, $bin_id) = @_;
748
749   my $warehouse = $form->{WAREHOUSES}->[$warehouse_index];
750
751   return -1 if (!$warehouse);
752
753   for (my $i = 0; $i < scalar @{ $warehouse->{BINS} }; $i++) {
754     return $i if ($warehouse->{BINS}->[$i]->{id} == $bin_id);
755   }
756
757   return -1;
758 }
759
760 sub update {
761   call_sub($form->{update_nextsub} || $form->{nextsub});
762 }
763
764 sub continue {
765   call_sub($form->{continue_nextsub} || $form->{nextsub});
766 }
767
768 sub stock {
769   call_sub($form->{stock_nextsub} || $form->{nextsub});
770 }
771
772 1;