1 #=====================================================================
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
10 # Author: Dieter Simader
11 # Email: dsimader@sql-ledger.org
12 # Web: http://www.sql-ledger.org
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.
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.
28 #######################################################################
30 # warehouse and packinglist
32 #######################################################################
34 use List::Util qw(min max first);
35 use POSIX qw(strftime);
45 use SL::ReportGenerator;
49 require "bin/mozilla/common.pl";
50 require "bin/mozilla/reportgenerator.pl";
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')
67 # --------------------------------------------------------------------
69 # --------------------------------------------------------------------
71 sub transfer_warehouse_selection {
72 $lxdebug->enter_sub();
74 $auth->assert('warehouse_management');
76 $form->get_lists('warehouses' => { 'key' => 'WAREHOUSES',
79 show_no_warehouses_error() if (!scalar @{ $form->{WAREHOUSES} });
81 my $units = AM->retrieve_units(\%myconfig, $form);
82 $form->{UNITS} = AM->unit_select_data($units, $form->{unit}, 0, $form->{partunit});
84 if (scalar @{ $form->{WAREHOUSES} }) {
85 $form->{warehouse_id} ||= $form->{WAREHOUSES}->[0]->{id};
86 $form->{bin_id} ||= $form->{WAREHOUSES}->[0]->{BINS}->[0]->{id};
91 $form->{jsscript} = 1;
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');
98 } elsif ($form->{trans_type} eq 'stock') {
99 $form->{title} = $locale->text('Stock');
100 $content = $form->parse_html_template('wh/warehouse_selection_stock');
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');
107 } elsif ($form->{trans_type} eq 'assembly') {
108 $form->{title} = $locale->text('Assembly');
109 $content = $form->parse_html_template('wh/warehouse_selection_assembly');
115 $lxdebug->leave_sub();
118 sub transfer_parts_selection {
119 $lxdebug->enter_sub();
121 $auth->assert('warehouse_management');
123 transfer_or_removal_prepare_contents('direction' => 'transfer');
125 $form->{title} = $locale->text('Transfer');
127 print $form->parse_html_template("wh/transfer_parts_selection");
129 $lxdebug->leave_sub();
132 sub transfer_or_removal_prepare_contents {
133 $lxdebug->enter_sub();
135 $auth->assert('warehouse_management');
139 $form->get_lists('warehouses' => { 'key' => 'WAREHOUSES',
140 'bins' => 'BINS', });
142 my $warehouse_idx = get_warehouse_idx($form->{warehouse_id});
143 $form->show_generic_error($locale->text("The selected warehouse does not exist.")) if (-1 == $warehouse_idx);
145 my $warehouse = $form->{WAREHOUSES}->[$warehouse_idx];
147 $form->{initial_warehouse_idx} = $warehouse_idx;
148 $form->{warehouse_description} = $warehouse->{description};
149 $warehouse->{selected} = 1;
151 $form->show_generic_error($locale->text("The source warehouse does not contain any bins.")) if (0 == scalar @{ $warehouse->{BINS} });
153 map { $form->{"l_$_"} = 'Y' } qw(parts_id qty warehouseid binid partnumber partdescription bindescription chargenumber partunit);
155 $form->{sort} = 'bindescription';
156 my @contents = WH->get_warehouse_report("warehouse_id" => $form->{warehouse_id},
157 "bin_id" => $form->{bin_id},
158 "chargenumber" => $form->{chargenumber},
159 "partnumber" => $form->{partnumber},
160 "description" => $form->{description});
162 $form->show_generic_error($locale->text("The selected warehouse is empty.")) if (0 == scalar(@contents));
164 my $all_units = AM->retrieve_units(\%myconfig, $form);
166 foreach (@contents) {
167 $_->{qty} = $form->format_amount_units('amount' => $_->{qty},
168 'part_unit' => $_->{partunit},
169 'conv_units' => 'convertible');
170 my $this_unit = $_->{partunit};
172 if ($all_units->{$_->{partunit}} && ($all_units->{g}->{base_unit} eq $all_units->{$_->{partunit}}->{base_unit})) {
176 $_->{UNITS} = AM->unit_select_data($all_units, $this_unit, 0, $_->{partunit});
179 my $transfer_types = WH->retrieve_transfer_types($args{direction});
180 map { $_->{description} = $locale->text($_->{description}) } @{ $transfer_types };
182 $form->{CONTENTS} = \@contents;
183 $form->{TRANSFER_TYPES} = $transfer_types;
185 $lxdebug->leave_sub();
190 $lxdebug->enter_sub();
192 $auth->assert('warehouse_management');
194 $form->get_lists('warehouses' => { 'key' => 'WAREHOUSES', 'bins' => 'BINS' });
196 my $warehouse_idx = get_warehouse_idx($form->{warehouse_id});
197 $form->show_generic_error($locale->text("The selected warehouse does not exist.")) if (-1 == $warehouse_idx);
199 my $warehouse = $form->{WAREHOUSES}->[$warehouse_idx];
201 $form->show_generic_error($locale->text("The source warehouse does not contain any bins.")) if (0 == scalar @{ $warehouse->{BINS} });
203 map { $form->{"l_$_"} = 'Y' } qw(parts_id qty warehouseid binid partnumber partdescription bindescription chargenumber partunit);
205 $form->{sort} = 'bindescription';
206 my @contents = WH->get_warehouse_report("warehouse_id" => $form->{warehouse_id});
207 my $all_units = AM->retrieve_units(\%myconfig, $form);
211 foreach my $row (1 .. $form->{rowcount}) {
212 $form->{"qty_$row"} =~ s/^\s*//;
213 $form->{"qty_$row"} =~ s/\s*$//;
214 next if (!$form->{"qty_$row"});
216 my $bin_idx = get_bin_idx($warehouse_idx, $form->{"src_bin_id_$row"});
217 $form->show_generic_error($locale->text("The selected bin does not exist.")) if (-1 == $bin_idx);
218 my $bin = $warehouse->{BINS}->[$bin_idx];
220 my $orig_qty = $form->{"qty_$row"} . " " . $form->{"unit_$row"};
223 'src_warehouse_id' => $form->{warehouse_id},
224 'transfer_type_id' => $form->{transfer_type_id},
227 map { $transfer->{$_} = $form->{"${_}_${row}"} } qw(src_bin_id chargenumber parts_id qty dst_warehouse_id dst_bin_id);
231 foreach (@contents) {
232 if (($_->{binid} == $transfer->{src_bin_id}) && ($_->{parts_id} == $transfer->{parts_id}) && ($_->{chargenumber} eq $transfer->{chargenumber})) {
239 $form->error($locale->text("There is not enough left of '#1' in bin '#2' for the removal of #3.",
240 $form->{"partdescription_$row"}, $bin->{description}, $orig_qty));
243 $transfer->{qty} = $form->parse_amount(\%myconfig, $transfer->{qty}) * $all_units->{$form->{"unit_$row"}}->{factor};
244 $transfer->{qty} /= $all_units->{$entry->{partunit}}->{factor} || 1;
246 if (($entry->{qty} < $transfer->{qty}) || (0 >= $transfer->{qty})) {
247 $form->error($locale->text("There is not enough left of '#1' in bin '#2' for the removal of #3.",
248 $form->{"partdescription_$row"}, $bin->{description}, $orig_qty));
251 $transfer->{comment} = $form->{comment};
253 push @transfers, $transfer;
255 $entry->{qty} -= $transfer->{qty};
258 if (!scalar @transfers) {
259 $form->show_generic_information($locale->text('Nothing has been selected for transfer.'));
263 WH->transfer(@transfers);
265 $form->{trans_type} = 'transfer';
266 $form->{saved_message} = $locale->text('The parts have been transferred.');
268 transfer_warehouse_selection();
270 $lxdebug->leave_sub();
273 # --------------------------------------------------------------------
275 # --------------------------------------------------------------------
277 sub transfer_stock_update_part {
278 $lxdebug->enter_sub();
280 $form->{trans_type} = 'stock';
281 $form->{qty} = $form->parse_amount(\%myconfig, $form->{qty});
283 if (!$form->{partnumber} && !$form->{description}) {
284 delete @{$form}{qw(parts_id partunit)};
285 transfer_warehouse_selection();
287 } elsif (($form->{partnumber} && ($form->{partnumber} ne $form->{old_partnumber})) || $form->{description}) {
289 $form->{no_services} = 1;
290 $form->{no_assemblies} = 1;
292 my $parts = Common->retrieve_parts(\%myconfig, $form, 'description', 1);
294 if (scalar @{ $parts } == 1) {
295 @{$form}{qw(parts_id partnumber description)} = @{$parts->[0]}{qw(id partnumber description)};
296 transfer_stock_get_partunit();
297 transfer_warehouse_selection();
300 select_part('transfer_stock_part_selected', @{ $parts });
304 transfer_stock_get_partunit();
305 transfer_warehouse_selection();
308 $lxdebug->leave_sub();
311 # --------------------------------------------------------------------
312 # Transfer: assemblies
313 # Dies ist die Auswahlmaske für ein assembly.
314 # Die ist einfach von transfer_assembly_update_part kopiert und nur um den trans_type (assembly) korrigiert worden
315 # Es wäre schön, hier nochmal check_assembly_max_create auf, um die max. Fertigungszahl herauszufinden.
316 # Ich lass das mal als auskommentierte Idee bestehen jb 18.3.09
317 # --------------------------------------------------------------------
319 sub transfer_assembly_update_part {
320 $lxdebug->enter_sub();
322 $form->{trans_type} = 'assembly';
323 $form->{qty} = $form->parse_amount(\%myconfig, $form->{qty});
325 if (!$form->{partnumber} && !$form->{description}) {
326 delete @{$form}{qw(parts_id partunit)};
327 transfer_warehouse_selection();
329 } elsif (($form->{partnumber} && ($form->{partnumber} ne $form->{old_partnumber})) || $form->{description}) {
330 $form->{assemblies} = 1;
331 $form->{no_assemblies} = 0;
332 my $parts = Common->retrieve_parts(\%myconfig, $form, 'description', 1);
333 if (scalar @{ $parts } == 1) {
334 @{$form}{qw(parts_id partnumber description)} = @{$parts->[0]}{qw(id partnumber description)};
335 transfer_stock_get_partunit();
336 transfer_warehouse_selection();
338 select_part('transfer_stock_part_selected', @{ $parts });
342 transfer_stock_get_partunit();
343 transfer_warehouse_selection();
346 # hier die oben benannte idee
347 # my $maxcreate = Common->check_assembly_max_create(assembly_id => $form->{parts_id}, dbh => $my_dbh);
348 $lxdebug->leave_sub();
350 sub transfer_stock_part_selected {
351 $lxdebug->enter_sub();
355 @{$form}{qw(parts_id partnumber description)} = @{$part}{qw(id partnumber description)};
357 transfer_stock_get_partunit();
358 transfer_warehouse_selection();
360 $lxdebug->leave_sub();
363 sub transfer_stock_get_partunit {
364 $lxdebug->enter_sub();
366 if ($form->{parts_id}) {
367 my $part_info = IC->get_basic_part_info('id' => $form->{parts_id});
368 $form->{partunit} = $part_info->{unit};
371 $lxdebug->leave_sub();
374 # vorüberlegung jb 22.2.2009
375 # wir benötigen für diese funktion, die anzahl die vom erzeugnis hergestellt werden soll. vielleicht direkt per js fehleingaben verhindern?
376 # ferner dann nochmal mit check_asssembly_max_create gegenprüfen und dann transaktionssicher wegbuchen.
377 # wir brauchen eine hilfsfunktion, die nee. brauchen wir nicht. der algorithmus läuft genau wie bei check max_create, nur dass hier auch eine lagerbewegung (verbraucht) stattfindet
378 # Manko ist derzeit noch, dass unterschiedliche Lagerplätze, bzw. das Quelllager an sich nicht ausgewählt werden können.
379 # Laut Absprache in KW11 09 übernimmt mb hier den rest im April ... jb 18.3.09
381 sub create_assembly {
382 $lxdebug->enter_sub();
384 $form->{qty} = $form->parse_amount(\%myconfig, $form->{qty});
385 if ($form->{qty} <= 0) {
386 $form->show_generic_error($locale->text('Invalid quantity.'), 'back_button' => 1);
388 # TODO Es wäre schön, hier schon die maximale Anzahl der zu fertigenden Erzeugnisse zu haben
389 #else { if ($form->{qty} > $maxcreate) { #s.o.
390 # $form->show_generic_error($locale->text('Can not create that quantity with current stock'), 'back_button' => 1);
391 # $form->show_generic_error('Maximale Stückzahl' . $maxcreate , 'back_button' => 1);
395 if (!$form->{warehouse_id} || !$form->{bin_id}) {
396 $form->error($locale->text('The warehouse or the bin is missing.'));
398 # WIESO war das nicht vorher schon ein %HASH?? ein hash ist ein hash! das hat mich mehr als eine Stunde gekostet herauszufinden. grr. jb 3.3.2009
399 # Anm. jb 18.3. vielleicht auch nur meine unwissenheit in perl-datenstrukturen
401 'transfer_type' => 'assembly',
402 'login' => $form->{login},
403 'dst_warehouse_id' => $form->{warehouse_id},
404 'dst_bin_id' => $form->{bin_id},
405 'chargenumber' => $form->{chargenumber},
406 'assembly_id' => $form->{parts_id},
407 'qty' => $form->{qty},
408 'unit' => $form->{unit},
409 'comment' => $form->{comment}
412 my $ret = WH->transfer_assembly (%TRANSFER);
413 # Frage: Ich pack in den return-wert auch gleich die Fehlermeldung. Irgendwelche Nummern als Fehlerkonstanten definieren find ich auch nicht besonders schick...
416 # Die locale-Funktion kann keine Double-Quotes escapen, deswegen hier erstmal so (ein wahrscheinlich immerwährender Hotfix) s.a. Frage davor jb 25.4.09
417 $form->show_generic_error($ret, 'back_button' => 1);
420 delete @{$form}{qw(parts_id partnumber description qty unit chargenumber comment)};
422 $form->{saved_message} = $locale->text('The assembly has been created.');
423 $form->{trans_type} = 'assembly';
425 transfer_warehouse_selection();
427 $lxdebug->leave_sub();
431 $lxdebug->enter_sub();
433 $form->{qty} = $form->parse_amount(\%myconfig, $form->{qty});
435 if ($form->{qty} <= 0) {
436 $form->show_generic_error($locale->text('Invalid quantity.'), 'back_button' => 1);
439 if (!$form->{warehouse_id} || !$form->{bin_id}) {
440 $form->error($locale->text('The warehouse or the bin is missing.'));
444 'transfer_type' => 'stock',
445 'dst_warehouse_id' => $form->{warehouse_id},
446 'dst_bin_id' => $form->{bin_id},
447 'chargenumber' => $form->{chargenumber},
448 'parts_id' => $form->{parts_id},
449 'qty' => $form->{qty},
450 'unit' => $form->{unit},
451 'comment' => $form->{comment},
454 WH->transfer($transfer);
456 delete @{$form}{qw(parts_id partnumber description qty unit chargenumber comment)};
458 $form->{saved_message} = $locale->text('The parts have been stocked.');
459 $form->{trans_type} = 'stock';
461 transfer_warehouse_selection();
463 $lxdebug->leave_sub();
466 # --------------------------------------------------------------------
468 # --------------------------------------------------------------------
470 sub removal_parts_selection {
471 $lxdebug->enter_sub();
473 $auth->assert('warehouse_management');
475 transfer_or_removal_prepare_contents('direction' => 'out');
477 $form->{title} = $locale->text('Removal');
479 print $form->parse_html_template("wh/removal_parts_selection");
481 $lxdebug->leave_sub();
485 $lxdebug->enter_sub();
487 $auth->assert('warehouse_management');
489 $form->get_lists('warehouses' => { 'key' => 'WAREHOUSES',
490 'bins' => 'BINS', });
492 my $warehouse_idx = get_warehouse_idx($form->{warehouse_id});
493 $form->show_generic_error($locale->text("The selected warehouse does not exist.")) if (-1 == $warehouse_idx);
495 my $warehouse = $form->{WAREHOUSES}->[$warehouse_idx];
497 $form->show_generic_error($locale->text("The warehouse does not contain any bins.")) if (0 == scalar @{ $warehouse->{BINS} });
499 map { $form->{"l_$_"} = 'Y' } qw(parts_id qty warehouseid binid partnumber partdescription bindescription chargenumber partunit);
501 $form->{sort} = 'bindescription';
502 my @contents = WH->get_warehouse_report("warehouse_id" => $form->{warehouse_id});
503 my $all_units = AM->retrieve_units(\%myconfig, $form);
507 foreach my $row (1 .. $form->{rowcount}) {
508 $form->{"qty_$row"} =~ s/^\s*//;
509 $form->{"qty_$row"} =~ s/\s*$//;
510 next if (!$form->{"qty_$row"});
512 my $bin_idx = get_bin_idx($warehouse_idx, $form->{"src_bin_id_$row"});
513 $form->show_generic_error($locale->text("The selected bin does not exist.")) if (-1 == $bin_idx);
514 my $bin = $warehouse->{BINS}->[$bin_idx];
516 my $orig_qty = $form->{"qty_$row"} . " " . $form->{"unit_$row"};
519 'src_warehouse_id' => $form->{warehouse_id},
520 'transfer_type_id' => $form->{transfer_type_id},
523 map { $transfer->{$_} = $form->{"${_}_${row}"} } qw(src_bin_id chargenumber parts_id qty);
527 foreach (@contents) {
528 if (($_->{binid} == $transfer->{src_bin_id}) && ($_->{parts_id} == $transfer->{parts_id}) && ($_->{chargenumber} eq $transfer->{chargenumber})) {
535 $form->error($locale->text("There is not enough left of '#1' in bin '#2' for the removal of #3.",
536 $form->{"partdescription_$row"}, $bin->{description}, $orig_qty));
539 $transfer->{qty} = $form->parse_amount(\%myconfig, $transfer->{qty}) * $all_units->{$form->{"unit_$row"}}->{factor};
540 $transfer->{qty} /= $all_units->{$entry->{partunit}}->{factor} || 1;
542 if (($entry->{qty} < $transfer->{qty}) || (0 >= $transfer->{qty})) {
543 $form->error($locale->text("There is not enough left of '#1' in bin '#2' for the removal of #3.",
544 $form->{"partdescription_$row"}, $bin->{description}, $orig_qty));
547 $transfer->{comment} = $form->{comment};
549 push @transfers, $transfer;
551 $entry->{qty} -= $transfer->{qty};
554 if (!scalar @transfers) {
555 $form->show_generic_information($locale->text('Nothing has been selected for removal.'));
559 WH->transfer(@transfers);
561 $form->{trans_type} = 'removal';
562 $form->{saved_message} = $locale->text('The parts have been removed.');
564 transfer_warehouse_selection();
566 $lxdebug->leave_sub();
569 # --------------------------------------------------------------------
571 # --------------------------------------------------------------------
574 $lxdebug->enter_sub();
576 $auth->assert('warehouse_management');
578 $form->get_lists('warehouses' => { 'key' => 'WAREHOUSES',
579 'bins' => 'BINS', });
581 show_no_warehouses_error() if (!scalar @{ $form->{WAREHOUSES} });
583 $form->{jsscript} = 1;
586 print $form->parse_html_template("wh/journal_filter", { "UNITS" => AM->unit_select_data(AM->retrieve_units(\%myconfig, $form)) });
588 $lxdebug->leave_sub();
591 sub generate_journal {
592 $lxdebug->enter_sub();
594 $auth->assert('warehouse_management');
596 $form->{title} = $locale->text("WHJournal");
597 $form->{sort} ||= 'date';
600 my @columns = qw(trans_id date warehouse_from bin_from warehouse_to bin_to partnumber partdescription chargenumber trans_type comment qty employee projectnumber);
603 map { $filter{$_} = $form->{$_} if ($form->{$_}) } qw(warehouse_id bin_id partnumber description chargenumber);
605 $filter{qty_op} = WH->convert_qty_op($form->{qty_op});
606 if ($filter{qty_op}) {
607 $form->isblank(qty, $locale->text('Quantity missing.'));
608 $form->isblank(qty_unit, $locale->text('Unit missing.'));
610 $filter{qty} = $form->{qty};
611 $filter{qty_unit} = $form->{qty_unit};
615 my $report = SL::ReportGenerator->new(\%myconfig, $form);
617 my @hidden_variables = map { "l_${_}" } @columns;
618 push @hidden_variables, qw(warehouse_id bin_id partnumber description chargenumber qty_op qty qty_unit fromdate todate);
621 'date' => { 'text' => $locale->text('Date'), },
622 'trans_id' => { 'text' => $locale->text('Trans Id'), },
623 'trans_type' => { 'text' => $locale->text('Trans Type'), },
624 'comment' => { 'text' => $locale->text('Comment'), },
625 'warehouse_from' => { 'text' => $locale->text('Warehouse From'), },
626 'warehouse_to' => { 'text' => $locale->text('Warehouse To'), },
627 'bin_from' => { 'text' => $locale->text('Bin From'), },
628 'bin_to' => { 'text' => $locale->text('Bin To'), },
629 'partnumber' => { 'text' => $locale->text('Part Number'), },
630 'partdescription' => { 'text' => $locale->text('Description'), },
631 'chargenumber' => { 'text' => $locale->text('Charge Number'), },
632 'qty' => { 'text' => $locale->text('Qty'), },
633 'employee' => { 'text' => $locale->text('Employee'), },
634 'projectnumber' => { 'text' => $locale->text('Project Number'), },
637 my $href = build_std_url('action=generate_journal', grep { $form->{$_} } @hidden_variables);
638 map { $column_defs{$_}->{link} = $href . "&sort=${_}&order=" . Q($_ eq $form->{sort} ? 1 - $form->{order} : $form->{order}) } @columns;
640 my %column_alignment = map { $_ => 'right' } qw(qty);
642 map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
644 $report->set_columns(%column_defs);
645 $report->set_column_order(@columns);
647 $report->set_export_options('generate_journal', @hidden_variables);
649 $report->set_sort_indicator($form->{sort}, $form->{order});
651 $report->set_options('output_format' => 'HTML',
652 'title' => $form->{title},
653 'attachment_basename' => strftime('warehouse_journal_%Y%m%d', localtime time));
654 $report->set_options_from_form();
656 my $all_units = AM->retrieve_units(\%myconfig, $form);
657 my @contents = WH->get_warehouse_journal(%filter);
659 foreach $entry (@contents) {
660 $entry->{qty} = $form->format_amount_units('amount' => $entry->{qty},
661 'part_unit' => $entry->{partunit},
662 'conv_units' => 'convertible');
663 $entry->{trans_type} = $locale->text($entry->{trans_type});
667 foreach my $column (@columns) {
668 next if ($column eq 'trans_type');
671 'data' => $entry->{$column},
672 'align' => $column_alignment{$column},
676 $row->{trans_type} = {
677 'raw_data' => $entry->{trans_type},
678 'align' => $column_alignment{trans_type},
681 $report->add_data($row);
684 $report->generate_with_headers();
686 $lxdebug->leave_sub();
689 # --------------------------------------------------------------------
691 # --------------------------------------------------------------------
694 $lxdebug->enter_sub();
696 $auth->assert('warehouse_contents | warehouse_management');
698 $form->get_lists('warehouses' => { 'key' => 'WAREHOUSES',
699 'bins' => 'BINS', });
701 show_no_warehouses_error() if (!scalar @{ $form->{WAREHOUSES} });
703 $form->{fokus} = "partnumber";
704 $form->{onload} .= "focus();";
705 $form->{title} = $locale->text("Report about wareouse contents");
708 print $form->parse_html_template("wh/report_filter",
709 { "nextsub" => "generate_report",
710 "WAREHOUSES" => $form->{WAREHOUSES},
711 "UNITS" => AM->unit_select_data(AM->retrieve_units(\%myconfig, $form)) });
713 $lxdebug->leave_sub();
716 sub generate_report {
717 $lxdebug->enter_sub();
719 $auth->assert('warehouse_contents | warehouse_management');
721 $form->{title} = $locale->text("Report about wareouse contents");
722 $form->{sort} ||= 'partnumber';
723 my $sort_col = $form->{sort};
726 my @columns = qw(warehousedescription bindescription partnumber partdescription chargenumber qty stock_value);
729 map { $filter{$_} = $form->{$_} if ($form->{$_}) } qw(warehouse_id bin_id partnumber description chargenumber);
731 $filter{qty_op} = WH->convert_qty_op($form->{qty_op});
732 if ($filter{qty_op}) {
733 $form->isblank(qty, $locale->text('Quantity missing.'));
734 $form->isblank(qty_unit, $locale->text('Unit missing.'));
736 $filter{qty} = $form->{qty};
737 $filter{qty_unit} = $form->{qty_unit};
741 $form->{subtotal} = '' if (!first { $_ eq $sort_col } qw(partnumber partdescription));
743 my $report = SL::ReportGenerator->new(\%myconfig, $form);
745 my @hidden_variables = map { "l_${_}" } @columns;
746 push @hidden_variables, qw(warehouse_id bin_id partnumber description chargenumber qty_op qty qty_unit l_warehousedescription l_bindescription);
747 push @hidden_variables, qw(include_empty_bins subtotal);
750 'warehousedescription' => { 'text' => $locale->text('Warehouse'), },
751 'bindescription' => { 'text' => $locale->text('Bin'), },
752 'partnumber' => { 'text' => $locale->text('Part Number'), },
753 'partdescription' => { 'text' => $locale->text('Description'), },
754 'chargenumber' => { 'text' => $locale->text('Charge Number'), },
755 'qty' => { 'text' => $locale->text('Qty'), },
756 'stock_value' => { 'text' => $locale->text('Stock value'), },
759 my $href = build_std_url('action=generate_report', grep { $form->{$_} } @hidden_variables);
760 map { $column_defs{$_}->{link} = $href . "&sort=${_}&order=" . Q($_ eq $sort_col ? 1 - $form->{order} : $form->{order}) } @columns;
762 my %column_alignment = map { $_ => 'right' } qw(qty stock_value);
764 map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
766 $report->set_columns(%column_defs);
767 $report->set_column_order(@columns);
769 $report->set_export_options('generate_report', @hidden_variables);
771 $report->set_sort_indicator($sort_col, $form->{order});
773 $report->set_options('output_format' => 'HTML',
774 'title' => $form->{title},
775 'attachment_basename' => strftime('warehouse_report_%Y%m%d', localtime time));
776 $report->set_options_from_form();
778 my $all_units = AM->retrieve_units(\%myconfig, $form);
779 my @contents = WH->get_warehouse_report(%filter);
783 my @subtotals_columns = qw(qty stock_value);
784 my %subtotals = map { $_ => 0 } @subtotals_columns;
786 my $total_stock_value = 0;
788 foreach $entry (@contents) {
789 map { $subtotals{$_} += $entry->{$_} } @subtotals_columns;
790 $total_stock_value += $entry->{stock_value} * 1;
792 $entry->{qty} = $form->format_amount_units('amount' => $entry->{qty},
793 'part_unit' => $entry->{partunit},
794 'conv_units' => 'convertible');
795 $entry->{stock_value} = $form->format_amount(\%myconfig, $entry->{stock_value} * 1, 2);
797 $row_set = [ { map { $_ => { 'data' => $entry->{$_}, 'align' => $column_alignment{$_} } } @columns } ];
799 if (($form->{subtotal} eq 'Y')
800 && (($idx == (scalar @contents - 1))
801 || ($entry->{$sort_col} ne $contents[$idx + 1]->{$sort_col}))) {
803 my $row = { map { $_ => { 'data' => '', 'class' => 'listsubtotal', 'align' => $column_alignment{$_}, } } @columns };
804 $row->{qty}->{data} = $form->format_amount_units('amount' => $subtotals{qty} * 1,
805 'part_unit' => $entry->{partunit},
806 'conv_units' => 'convertible');
807 $row->{stock_value}->{data} = $form->format_amount(\%myconfig, $subtotals{stock_value} * 1, 2);
809 %subtotals = map { $_ => 0 } @subtotals_columns;
811 push @{ $row_set }, $row;
814 $report->add_data($row_set);
819 if ($column_defs{stock_value}->{visible}) {
820 $report->add_separator();
822 my $row = { map { $_ => { 'data' => '', 'class' => 'listsubtotal', } } @columns };
824 my $left_col = first { $column_defs{$_}->{visible} } @columns;
826 $row->{$left_col}->{data} = $locale->text('Total stock value');
827 $row->{stock_value}->{data} = $form->format_amount(\%myconfig, $total_stock_value, 2);
828 $row->{stock_value}->{align} = 'right';
830 $report->add_data($row);
833 $report->generate_with_headers();
835 $lxdebug->leave_sub();
838 # --------------------------------------------------------------------
840 # --------------------------------------------------------------------
842 sub show_no_warehouses_error {
843 $lxdebug->enter_sub();
845 my $msg = $locale->text('No warehouse has been created yet.') . ' ';
847 if ($auth->check_right($form->{login}, 'config')) {
848 $msg .= $locale->text('You can create warehouses and bins via the menu "System -> Warehouses".');
850 $msg .= $locale->text('Please ask your administrator to create warehouses and bins.');
853 $form->show_generic_error($msg);
855 $lxdebug->leave_sub();
858 sub get_warehouse_idx {
859 my ($warehouse_id) = @_;
861 for (my $i = 0; $i < scalar @{$form->{WAREHOUSES}}; $i++) {
862 return $i if ($form->{WAREHOUSES}->[$i]->{id} == $warehouse_id);
869 my ($warehouse_index, $bin_id) = @_;
871 my $warehouse = $form->{WAREHOUSES}->[$warehouse_index];
873 return -1 if (!$warehouse);
875 for (my $i = 0; $i < scalar @{ $warehouse->{BINS} }; $i++) {
876 return $i if ($warehouse->{BINS}->[$i]->{id} == $bin_id);
883 call_sub($form->{update_nextsub} || $form->{nextsub});
887 call_sub($form->{continue_nextsub} || $form->{nextsub});
891 call_sub($form->{stock_nextsub} || $form->{nextsub});