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., 51 Franklin Street, Fifth Floor, Boston,
29 #######################################################################
31 # warehouse and packinglist
33 #######################################################################
35 use List::Util qw(min max first);
36 use POSIX qw(strftime);
46 use SL::Locale::String qw(t8);
47 use SL::ReportGenerator;
48 use SL::Presenter::Part;
54 require "bin/mozilla/common.pl";
55 require "bin/mozilla/reportgenerator.pl";
61 # contents of the "transfer_type" table:
62 # $locale->text('back')
63 # $locale->text('correction')
64 # $locale->text('disposed')
65 # $locale->text('found')
66 # $locale->text('missing')
67 # $locale->text('stock')
68 # $locale->text('shipped')
69 # $locale->text('transfer')
70 # $locale->text('used')
71 # $locale->text('return_material')
72 # $locale->text('release_material')
73 # $locale->text('assembled')
74 # $locale->text('stocktaking')
76 # --------------------------------------------------------------------
78 # --------------------------------------------------------------------
80 sub transfer_warehouse_selection {
81 $main::lxdebug->enter_sub();
83 $main::auth->assert('warehouse_management');
85 my $form = $main::form;
86 my %myconfig = %main::myconfig;
87 my $locale = $main::locale;
89 $form->get_lists('warehouses' => { 'key' => 'WAREHOUSES',
92 show_no_warehouses_error() if (!scalar @{ $form->{WAREHOUSES} });
94 my $units = AM->retrieve_units(\%myconfig, $form);
97 if ( $form->{parts_id} ) {
98 $part = SL::DB::Part->new();
99 $part->id($form->{parts_id});
103 # der zweite Parameter von unit_select_data gibt den default-Namen (selected) vor
104 $form->{UNITS} = AM->unit_select_data($units, $form->{unit}, 0, $part ? $part->unit : 0);
106 if (scalar @{ $form->{WAREHOUSES} }) {
107 $form->{warehouse_id} ||= $form->{WAREHOUSES}->[0]->{id};
108 $form->{bin_id} ||= $form->{WAREHOUSES}->[0]->{BINS}->[0]->{id};
113 if ($form->{trans_type} eq 'removal') {
114 setup_wh_transfer_warehouse_selection_action_bar("removal_parts_selection");
115 $form->{title} = $locale->text('Removal from Warehouse');
116 $content = $form->parse_html_template('wh/warehouse_selection');
118 } elsif (!$form->{trans_type} || ($form->{trans_type} eq 'transfer')) {
119 setup_wh_transfer_warehouse_selection_action_bar("transfer_parts_selection");
120 $form->{title} = $locale->text('Transfer');
121 $content = $form->parse_html_template('wh/warehouse_selection');
123 } elsif ($form->{trans_type} eq 'assembly') {
124 setup_wh_transfer_warehouse_selection_assembly_action_bar();
125 $form->{title} = $locale->text('Produce Assembly');
126 $content = $form->parse_html_template('wh/warehouse_selection_assembly');
132 $main::lxdebug->leave_sub();
135 sub transfer_parts_selection {
136 $main::lxdebug->enter_sub();
138 $main::auth->assert('warehouse_management');
140 my $form = $main::form;
141 my %myconfig = %main::myconfig;
142 my $locale = $main::locale;
144 transfer_or_removal_prepare_contents('direction' => 'transfer');
146 setup_wh_transfer_parts_action_bar();
148 $form->{title} = $locale->text('Transfer');
150 print $form->parse_html_template("wh/transfer_parts_selection");
152 $main::lxdebug->leave_sub();
155 sub transfer_or_removal_prepare_contents {
156 $main::lxdebug->enter_sub();
158 $main::auth->assert('warehouse_management');
162 my $form = $main::form;
163 my %myconfig = %main::myconfig;
164 my $locale = $main::locale;
166 $form->get_lists('warehouses' => { 'key' => 'WAREHOUSES',
167 'bins' => 'BINS', });
169 my $warehouse_idx = get_warehouse_idx($form->{warehouse_id});
170 $form->show_generic_error($locale->text("The selected warehouse does not exist.")) if (-1 == $warehouse_idx);
172 my $warehouse = $form->{WAREHOUSES}->[$warehouse_idx];
174 $form->{initial_warehouse_idx} = $warehouse_idx;
175 $form->{warehouse_description} = $warehouse->{description};
176 $warehouse->{selected} = 1;
178 $form->show_generic_error($locale->text("The source warehouse does not contain any bins.")) if (0 == scalar @{ $warehouse->{BINS} });
180 map { $form->{"l_$_"} = 'Y' } qw(parts_id qty warehouseid binid partnumber partdescription bindescription chargenumber bestbefore partunit ean);
182 $form->{sort} = 'bindescription';
183 my @contents = WH->get_warehouse_report("warehouse_id" => $form->{warehouse_id},
184 "bin_id" => $form->{bin_id},
185 "chargenumber" => $form->{chargenumber},
186 "bestbefore" => $form->{bestbefore},
187 "partsid" => $form->{part_id},
188 "ean" => $form->{ean});
190 if (0 == scalar(@contents)) {
191 $form->show_generic_error($locale->text("The selected warehouse is empty, or no stocked items where found that match the filter settings."));
194 my $all_units = AM->retrieve_units(\%myconfig, $form);
196 foreach (@contents) {
197 $_->{qty} = $form->format_amount_units('amount' => $_->{qty},
198 'part_unit' => $_->{partunit},
199 'conv_units' => 'convertible');
200 my $this_unit = $_->{partunit};
202 if ($all_units->{$_->{partunit}} && ($all_units->{g}->{base_unit} eq $all_units->{$_->{partunit}}->{base_unit})) {
206 $_->{UNITS} = AM->unit_select_data($all_units, $this_unit, 0, $_->{partunit});
209 my $transfer_types = WH->retrieve_transfer_types($args{direction});
210 map { $_->{description} = $locale->text($_->{description}) } @{ $transfer_types };
212 $form->{CONTENTS} = \@contents;
213 $form->{TRANSFER_TYPES} = $transfer_types;
215 $main::lxdebug->leave_sub();
220 $main::lxdebug->enter_sub();
222 $main::auth->assert('warehouse_management');
224 my $form = $main::form;
225 my %myconfig = %main::myconfig;
226 my $locale = $main::locale;
228 $form->get_lists('warehouses' => { 'key' => 'WAREHOUSES', 'bins' => 'BINS' });
230 my $warehouse_idx = get_warehouse_idx($form->{warehouse_id});
231 $form->show_generic_error($locale->text("The selected warehouse does not exist.")) if (-1 == $warehouse_idx);
233 my $warehouse = $form->{WAREHOUSES}->[$warehouse_idx];
235 $form->show_generic_error($locale->text("The source warehouse does not contain any bins.")) if (0 == scalar @{ $warehouse->{BINS} });
237 map { $form->{"l_$_"} = 'Y' } qw(parts_id qty warehouseid binid partnumber partdescription bindescription chargenumber bestbefore partunit);
239 $form->{sort} = 'bindescription';
240 my @contents = WH->get_warehouse_report("warehouse_id" => $form->{warehouse_id});
241 my $all_units = AM->retrieve_units(\%myconfig, $form);
245 foreach my $row (1 .. $form->{rowcount}) {
246 $form->{"qty_$row"} =~ s/^\s*//;
247 $form->{"qty_$row"} =~ s/\s*$//;
248 next if (!$form->{"qty_$row"});
250 my $bin_idx = get_bin_idx($warehouse_idx, $form->{"src_bin_id_$row"});
251 $form->show_generic_error($locale->text("The selected bin does not exist.")) if (-1 == $bin_idx);
252 my $bin = $warehouse->{BINS}->[$bin_idx];
254 my $orig_qty = $form->{"qty_$row"} . " " . $form->{"unit_$row"};
257 'src_warehouse_id' => $form->{warehouse_id},
258 'transfer_type_id' => $form->{transfer_type_id},
261 map { $transfer->{$_} = $form->{"${_}_${row}"} } qw(src_bin_id chargenumber bestbefore parts_id qty dst_warehouse_id dst_bin_id);
265 foreach (@contents) {
266 if (($_->{binid} == $transfer->{src_bin_id}) && ($_->{parts_id} == $transfer->{parts_id}) && ($_->{chargenumber} eq $transfer->{chargenumber}) && $_->{bestbefore} eq $transfer->{bestbefore}) {
273 $form->error($locale->text("There is not enough left of '#1' in bin '#2' for the removal of #3.",
274 $form->{"partdescription_$row"}, $bin->{description}, $orig_qty));
277 $transfer->{qty} = $form->parse_amount(\%myconfig, $transfer->{qty}) * $all_units->{$form->{"unit_$row"}}->{factor};
278 $transfer->{qty} /= $all_units->{$entry->{partunit}}->{factor} || 1;
280 if (($entry->{qty} < $transfer->{qty}) || (0 >= $transfer->{qty})) {
281 $form->error($locale->text("There is not enough left of '#1' in bin '#2' for the removal of #3.",
282 $form->{"partdescription_$row"}, $bin->{description}, $orig_qty));
285 $transfer->{comment} = $form->{comment};
286 $transfer->{change_default_bin} = $form->{change_default_bin};
288 push @transfers, $transfer;
290 $entry->{qty} -= $transfer->{qty};
293 if (!scalar @transfers) {
294 $form->show_generic_information($locale->text('Nothing has been selected for transfer.'));
295 $::dispatcher->end_request;
298 WH->transfer(@transfers);
300 $form->{trans_type} = 'transfer';
301 $form->{saved_message} = $locale->text('The parts have been transferred.');
303 transfer_warehouse_selection();
305 $main::lxdebug->leave_sub();
308 # --------------------------------------------------------------------
310 # --------------------------------------------------------------------
312 # --------------------------------------------------------------------
313 # Transfer: assemblies
314 # Dies ist die Auswahlmaske für ein assembly.
315 # Die ist einfach von transfer_assembly_update_part kopiert und nur um den trans_type (assembly) korrigiert worden
316 # Es wäre schön, hier nochmal check_assembly_max_create auf, um die max. Fertigungszahl herauszufinden.
317 # Ich lass das mal als auskommentierte Idee bestehen jb 18.3.09
318 # --------------------------------------------------------------------
320 sub transfer_assembly_update_part {
321 my $form = $main::form;
322 my %myconfig = %main::myconfig;
323 my $locale = $main::locale;
325 $form->{trans_type} = 'assembly';
326 $form->{qty} = $form->parse_amount(\%myconfig, $form->{qty});
328 if (!$form->{parts_id}) {
329 delete $form->{partunit};
330 transfer_warehouse_selection();
335 my $part = SL::DB::Part->new(id => $::form->{parts_id})->load;
336 @{$form}{qw(parts_id partnumber description)} = ($part->id, $part->partnumber, $part->description);
338 transfer_stock_get_partunit();
339 transfer_warehouse_selection();
342 sub transfer_stock_part_selected {
343 $main::lxdebug->enter_sub();
347 my $form = $main::form;
349 @{$form}{qw(parts_id partnumber description ean warehouse_id bin_id)} = @{$part}{qw(id partnumber description ean warehouse_id bin_id)};
351 transfer_stock_get_partunit();
352 transfer_warehouse_selection();
354 $main::lxdebug->leave_sub();
357 sub transfer_stock_get_partunit {
358 $main::lxdebug->enter_sub();
360 my $form = $main::form;
362 if ($form->{parts_id}) {
363 my $part_info = IC->get_basic_part_info('id' => $form->{parts_id});
364 $form->{partunit} = $part_info->{unit};
367 $main::lxdebug->leave_sub();
370 # vorüberlegung jb 22.2.2009
371 # wir benötigen für diese funktion, die anzahl die vom erzeugnis hergestellt werden soll. vielleicht direkt per js fehleingaben verhindern?
372 # ferner dann nochmal mit check_asssembly_max_create gegenprüfen und dann transaktionssicher wegbuchen.
373 # 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
374 # Manko ist derzeit noch, dass unterschiedliche Lagerplätze, bzw. das Quelllager an sich nicht ausgewählt werden können.
375 # Laut Absprache in KW11 09 übernimmt mb hier den rest im April ... jb 18.3.09
377 sub create_assembly {
378 $main::lxdebug->enter_sub();
380 my $form = $main::form;
381 my %myconfig = %main::myconfig;
382 my $locale = $main::locale;
384 $form->{qty} = $form->parse_amount(\%myconfig, $form->{qty});
385 if ($form->{qty} <= 0) {
386 $form->show_generic_error($locale->text('Invalid quantity.'));
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'));
391 # $form->show_generic_error('Maximale Stückzahl' . $maxcreate);
395 if (!$form->{warehouse_id} || !$form->{bin_id}) {
396 $form->error($locale->text('The warehouse or the bin is missing.'));
399 if (!$::instance_conf->get_show_bestbefore) {
400 $form->{bestbefore} = '';
403 # 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
404 # Anm. jb 18.3. vielleicht auch nur meine unwissenheit in perl-datenstrukturen
406 'transfer_type' => 'assembly',
407 'login' => $::myconfig{login},
408 'dst_warehouse_id' => $form->{warehouse_id},
409 'dst_bin_id' => $form->{bin_id},
410 'chargenumber' => $form->{chargenumber},
411 'bestbefore' => $form->{bestbefore},
412 'assembly_id' => $form->{parts_id},
413 'qty' => $form->{qty},
414 'unit' => $form->{unit},
415 'comment' => $form->{comment}
418 my $ret = WH->transfer_assembly (%TRANSFER);
419 # Frage: Ich pack in den return-wert auch gleich die Fehlermeldung. Irgendwelche Nummern als Fehlerkonstanten definieren find ich auch nicht besonders schick...
422 # 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
423 $form->show_generic_error($ret);
426 delete @{$form}{qw(parts_id partnumber description qty unit chargenumber bestbefore comment)};
428 $form->{saved_message} = $locale->text('The assembly has been created.');
429 $form->{trans_type} = 'assembly';
431 transfer_warehouse_selection();
433 $main::lxdebug->leave_sub();
436 # --------------------------------------------------------------------
438 # --------------------------------------------------------------------
440 sub removal_parts_selection {
441 $main::lxdebug->enter_sub();
443 $main::auth->assert('warehouse_management');
445 my $form = $main::form;
446 my %myconfig = %main::myconfig;
447 my $locale = $main::locale;
449 transfer_or_removal_prepare_contents('direction' => 'out');
451 setup_wh_removal_parts_selection_action_bar();
453 $form->{title} = $locale->text('Removal');
455 print $form->parse_html_template("wh/removal_parts_selection");
457 $main::lxdebug->leave_sub();
461 $main::lxdebug->enter_sub();
463 $main::auth->assert('warehouse_management');
465 my $form = $main::form;
466 my %myconfig = %main::myconfig;
467 my $locale = $main::locale;
469 $form->get_lists('warehouses' => { 'key' => 'WAREHOUSES',
470 'bins' => 'BINS', });
472 my $warehouse_idx = get_warehouse_idx($form->{warehouse_id});
473 $form->show_generic_error($locale->text("The selected warehouse does not exist.")) if (-1 == $warehouse_idx);
475 my $warehouse = $form->{WAREHOUSES}->[$warehouse_idx];
477 $form->show_generic_error($locale->text("The warehouse does not contain any bins.")) if (0 == scalar @{ $warehouse->{BINS} });
479 map { $form->{"l_$_"} = 'Y' } qw(parts_id qty warehouseid binid partnumber partdescription bindescription chargenumber bestbefore partunit);
481 $form->{sort} = 'bindescription';
482 my @contents = WH->get_warehouse_report("warehouse_id" => $form->{warehouse_id});
483 my $all_units = AM->retrieve_units(\%myconfig, $form);
487 foreach my $row (1 .. $form->{rowcount}) {
488 $form->{"qty_$row"} =~ s/^\s*//;
489 $form->{"qty_$row"} =~ s/\s*$//;
490 next if (!$form->{"qty_$row"});
492 my $bin_idx = get_bin_idx($warehouse_idx, $form->{"src_bin_id_$row"});
493 $form->show_generic_error($locale->text("The selected bin does not exist.")) if (-1 == $bin_idx);
494 my $bin = $warehouse->{BINS}->[$bin_idx];
496 my $orig_qty = $form->{"qty_$row"} . " " . $form->{"unit_$row"};
499 'src_warehouse_id' => $form->{warehouse_id},
500 'transfer_type_id' => $form->{transfer_type_id},
503 map { $transfer->{$_} = $form->{"${_}_${row}"} } qw(src_bin_id chargenumber bestbefore parts_id qty);
507 foreach (@contents) {
508 if (($_->{binid} == $transfer->{src_bin_id}) && ($_->{parts_id} == $transfer->{parts_id}) && ($_->{chargenumber} eq $transfer->{chargenumber}) && ($_->{bestbefore} eq $transfer->{bestbefore})) {
515 $form->error($locale->text("There is not enough left of '#1' in bin '#2' for the removal of #3.",
516 $form->{"partdescription_$row"}, $bin->{description}, $orig_qty));
519 $transfer->{qty} = $form->parse_amount(\%myconfig, $transfer->{qty}) * $all_units->{$form->{"unit_$row"}}->{factor};
520 $transfer->{qty} /= $all_units->{$entry->{partunit}}->{factor} || 1;
522 if (($entry->{qty} < $transfer->{qty}) || (0 >= $transfer->{qty})) {
523 $form->error($locale->text("There is not enough left of '#1' in bin '#2' for the removal of #3.",
524 $form->{"partdescription_$row"}, $bin->{description}, $orig_qty));
527 $transfer->{comment} = $form->{comment};
529 push @transfers, $transfer;
531 $entry->{qty} -= $transfer->{qty};
534 if (!scalar @transfers) {
535 $form->show_generic_information($locale->text('Nothing has been selected for removal.'));
536 $::dispatcher->end_request;
539 WH->transfer(@transfers);
541 $form->{trans_type} = 'removal';
542 $form->{saved_message} = $locale->text('The parts have been removed.');
544 transfer_warehouse_selection();
546 $main::lxdebug->leave_sub();
549 # --------------------------------------------------------------------
551 # --------------------------------------------------------------------
554 $main::lxdebug->enter_sub();
556 $main::auth->assert('warehouse_management');
558 my $form = $main::form;
559 my %myconfig = %main::myconfig;
560 my $locale = $main::locale;
562 $form->{title} = $locale->text('Report about warehouse transactions');
563 $form->get_lists('warehouses' => { 'key' => 'WAREHOUSES',
564 'bins' => 'BINS', });
566 show_no_warehouses_error() if (!scalar @{ $form->{WAREHOUSES} });
568 setup_wh_journal_action_bar();
571 print $form->parse_html_template("wh/journal_filter", { "UNITS" => AM->unit_select_data(AM->retrieve_units(\%myconfig, $form)) });
573 $main::lxdebug->leave_sub();
576 sub generate_journal {
577 $main::lxdebug->enter_sub();
579 $main::auth->assert('warehouse_management');
581 my $form = $main::form;
582 my %myconfig = %main::myconfig;
583 my $locale = $main::locale;
585 $form->{title} = $locale->text("WHJournal");
586 $form->{sort} ||= 'date';
588 $form->{report_generator_output_format} = 'HTML' if !$form->{report_generator_output_format};
591 my @columns = qw(trans_id date warehouse_from bin_from warehouse_to bin_to partnumber type_and_classific partdescription chargenumber bestbefore trans_type comment qty unit partunit employee oe_id projectnumber);
594 map { $filter{$_} = $form->{$_} if ($form->{$_}) } qw(warehouse_id bin_id classification_id partnumber description chargenumber bestbefore transtype_id transtype_ids comment projectnumber);
596 $filter{qty_op} = WH->convert_qty_op($form->{qty_op});
597 if ($filter{qty_op}) {
598 $form->isblank("qty", $locale->text('Quantity missing.'));
599 $form->isblank("qty_unit", $locale->text('Unit missing.'));
601 $filter{qty} = $form->{qty};
602 $filter{qty_unit} = $form->{qty_unit};
606 my $allrows = !!($form->{report_generator_output_format} ne 'HTML') ;
610 my $page = $::form->{page} || 1;
611 $pages->{per_page} = $::form->{per_page} || 15;
612 my $first_nr = ($page - 1) * $pages->{per_page};
613 my $last_nr = $first_nr + $pages->{per_page};
615 # no optimisation if qty op
616 if ( !$allrows && $form->{maxrows} && !$filter{qty_op}) {
617 $filter{limit} = $pages->{per_page};
618 $filter{offset} = ($page - 1) * $pages->{per_page};
620 $last_nr = $pages->{per_page};
623 my @contents = WH->get_warehouse_journal(%filter);
625 if (!$form->{maxrows}) {
626 $form->{maxrows} = scalar @contents ;
629 my $report = SL::ReportGenerator->new(\%myconfig, $form);
631 my @hidden_variables = map { "l_${_}" } @columns;
632 push @hidden_variables, qw(warehouse_id bin_id partnumber description chargenumber bestbefore qty_op qty qty_unit unit partunit fromdate todate transtype_ids comment projectnumber);
633 push @hidden_variables, qw(classification_id);
636 'date' => { 'text' => $locale->text('Date'), },
637 'trans_id' => { 'text' => $locale->text('Trans Id'), },
638 'trans_type' => { 'text' => $locale->text('Trans Type'), },
639 'comment' => { 'text' => $locale->text('Comment'), },
640 'warehouse_from' => { 'text' => $locale->text('Warehouse From'), },
641 'warehouse_to' => { 'text' => $locale->text('Warehouse To'), },
642 'bin_from' => { 'text' => $locale->text('Bin From'), },
643 'bin_to' => { 'text' => $locale->text('Bin To'), },
644 'partnumber' => { 'text' => $locale->text('Part Number'), },
646 => { 'text' => $locale->text('Type'), },
647 'partdescription' => { 'text' => $locale->text('Part Description'), },
648 'chargenumber' => { 'text' => $locale->text('Charge Number'), },
649 'bestbefore' => { 'text' => $locale->text('Best Before'), },
650 'qty' => { 'text' => $locale->text('Qty'), },
651 'unit' => { 'text' => $locale->text('Part Unit'), },
652 'partunit' => { 'text' => $locale->text('Unit'), },
653 'employee' => { 'text' => $locale->text('Employee'), },
654 'projectnumber' => { 'text' => $locale->text('Project Number'), },
655 'oe_id' => { 'text' => $locale->text('Document'), },
658 if ($form->{transtype_ids} && 'ARRAY' eq ref $form->{transtype_ids}) {
659 for (my $i = 0; $i < scalar(@{ $form->{transtype_ids} }); $i++) {
660 delete $form->{transtype_ids}[$i] if $form->{transtype_ids}[$i] eq '';
662 $form->{transtype_ids} = join(",", @{ $form->{transtype_ids} });
665 my $href = build_std_url('action=generate_journal', grep { $form->{$_} } @hidden_variables);
666 $href .= "&maxrows=".$form->{maxrows};
668 map { $column_defs{$_}->{link} = $href ."&page=".$page. "&sort=${_}&order=" . Q($_ eq $form->{sort} ? 1 - $form->{order} : $form->{order}) } @columns;
670 my %column_alignment = map { $_ => 'right' } qw(qty);
672 map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
673 $column_defs{partunit}->{visible} = 1;
674 $column_defs{type_and_classific}->{visible} = 1;
675 $column_defs{type_and_classific}->{link} ='';
677 $report->set_columns(%column_defs);
678 $report->set_column_order(@columns);
680 $report->set_export_options('generate_journal', @hidden_variables, qw(sort order));
682 $report->set_sort_indicator($form->{sort}, $form->{order});
684 $report->set_options('output_format' => 'HTML',
685 'title' => $form->{title},
686 'attachment_basename' => strftime($locale->text('warehouse_journal_list') . '_%Y%m%d', localtime time));
687 $report->set_options_from_form();
688 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
690 my $all_units = AM->retrieve_units(\%myconfig, $form);
692 my %doc_types = ( 'sales_quotation' => { script => 'oe', title => $locale->text('Sales quotation') },
693 'sales_order' => { script => 'oe', title => $locale->text('Sales Order') },
694 'request_quotation' => { script => 'oe', title => $locale->text('Request quotation') },
695 'purchase_order' => { script => 'oe', title => $locale->text('Purchase Order') },
696 'sales_delivery_order' => { script => 'do', title => $locale->text('Sales delivery order') },
697 'purchase_delivery_order' => { script => 'do', title => $locale->text('Purchase delivery order') },
698 'sales_invoice' => { script => 'is', title => $locale->text('Sales Invoice') },
699 'purchase_invoice' => { script => 'ir', title => $locale->text('Purchase Invoice') },
704 foreach my $entry (@contents) {
705 $entry->{type_and_classific} = SL::Presenter::Part::type_abbreviation($entry->{part_type}) .
706 SL::Presenter::Part::classification_abbreviation($entry->{classification_id});
707 $entry->{qty} = $form->format_amount(\%myconfig, $entry->{qty});
708 $entry->{trans_type} = $locale->text($entry->{trans_type});
712 foreach my $column (@columns) {
714 'data' => $entry->{$column},
715 'align' => $column_alignment{$column},
719 $row->{trans_type}->{raw_data} = $entry->{trans_type};
721 if ($form->{l_oe_id}) {
722 $row->{oe_id}->{data} = '';
723 my $info = $entry->{oe_id_info};
725 if ($info && $info->{id} && $info->{type} && $doc_types{$info->{type}}) {
726 $row->{oe_id} = { data => $doc_types{ $info->{type} }->{title} . ' ' . $info->{number},
727 link => build_std_url('script=' . $doc_types{ $info->{type} }->{script} . '.pl', 'action=edit', 'id=' . $info->{id}, 'type=' . $info->{type}) };
731 if ( $allrows || ($idx >= $first_nr && $idx < $last_nr )) {
732 $report->add_data($row);
738 $pages->{max} = SL::DB::Helper::Paginated::ceil($form->{maxrows}, $pages->{per_page}) || 1;
739 $pages->{page} = $page < 1 ? 1: $page > $pages->{max} ? $pages->{max}: $page;
740 $pages->{common} = [ grep { $_->{visible} } @{ SL::DB::Helper::Paginated::make_common_pages($pages->{page}, $pages->{max}) } ];
742 $report->set_options('raw_bottom_info_text' => $form->parse_html_template('common/paginate',
743 { 'pages' => $pages , 'base_url' => $href.'&sort='.$form->{sort}.'&order='.$form->{order}}) );
745 $report->generate_with_headers();
747 $main::lxdebug->leave_sub();
750 # --------------------------------------------------------------------
752 # --------------------------------------------------------------------
755 $main::lxdebug->enter_sub();
757 $main::auth->assert('warehouse_contents | warehouse_management');
759 my $form = $main::form;
760 my %myconfig = %main::myconfig;
761 my $locale = $main::locale;
763 $form->get_lists('warehouses' => { 'key' => 'WAREHOUSES',
764 'bins' => 'BINS', });
766 show_no_warehouses_error() if (!scalar @{ $form->{WAREHOUSES} });
768 $form->{title} = $locale->text("Report about warehouse contents");
770 setup_wh_report_action_bar();
773 print $form->parse_html_template("wh/report_filter",
774 { "WAREHOUSES" => $form->{WAREHOUSES},
775 "UNITS" => AM->unit_select_data(AM->retrieve_units(\%myconfig, $form)) });
777 $main::lxdebug->leave_sub();
780 sub generate_report {
781 $main::lxdebug->enter_sub();
783 $main::auth->assert('warehouse_contents | warehouse_management');
785 my $form = $main::form;
786 my %myconfig = %main::myconfig;
787 my $locale = $main::locale;
789 $form->{title} = $locale->text("Report about warehouse contents");
790 $form->{sort} ||= 'partnumber';
791 my $sort_col = $form->{sort};
794 my @columns = qw(warehousedescription bindescription partnumber type_and_classific partdescription chargenumber bestbefore comment qty partunit list_price purchase_price stock_value);
797 map { $filter{$_} = $form->{$_} if ($form->{$_}) } qw(warehouse_id bin_id classification_id partnumber description chargenumber bestbefore date include_invalid_warehouses);
799 # show filter stuff also in report
801 my $currentdate = $form->current_date(\%myconfig);
802 push @options, $locale->text('Printdate') . " : ".$locale->date(\%myconfig, $currentdate, 1);
804 # dispatch all options
805 my $dispatch_options = {
806 warehouse_id => sub { push @options, $locale->text('Warehouse') . " : " .
807 SL::DB::Manager::Warehouse->find_by(id => $form->{warehouse_id})->description},
808 bin_id => sub { push @options, $locale->text('Bin') . " : " .
809 SL::DB::Manager::Bin->find_by(id => $form->{bin_id})->description},
810 partnumber => sub { push @options, $locale->text('Partnumber') . " : $form->{partnumber}"},
811 classification_id => sub { push @options, $locale->text('Parts Classification'). " : ".
812 SL::DB::Manager::PartClassification->get_first(where => [ id => $form->{classification_id} ] )->description; },
813 description => sub { push @options, $locale->text('Description') . " : $form->{description}"},
814 chargenumber => sub { push @options, $locale->text('Charge Number') . " : $form->{chargenumber}"},
815 bestbefore => sub { push @options, $locale->text('Best Before') . " : $form->{bestbefore}"},
816 include_invalid_warehouses => sub { push @options, $locale->text('Include invalid warehouses ')},
818 foreach (keys %filter) {
819 $dispatch_options->{$_}->() if $dispatch_options->{$_};
821 push @options, $locale->text('Stock Qty for Date') . " " . $locale->date(\%myconfig, $form->{date}?$form->{date}:$currentdate, 1);
823 # / end show filter stuff also in report
825 $filter{qty_op} = WH->convert_qty_op($form->{qty_op});
826 if ($filter{qty_op}) {
827 $form->isblank("qty", $locale->text('Quantity missing.'));
828 $form->isblank("qty_unit", $locale->text('Unit missing.'));
830 $filter{qty} = $form->{qty};
831 $filter{qty_unit} = $form->{qty_unit};
835 $form->{report_generator_output_format} = 'HTML' if !$form->{report_generator_output_format};
838 my $allrows = !!($form->{report_generator_output_format} ne 'HTML') ;
839 my $page = $::form->{page} || 1;
841 $pages->{per_page} = $::form->{per_page} || 20;
842 my $first_nr = ($page - 1) * $pages->{per_page};
843 my $last_nr = $first_nr + $pages->{per_page};
845 # no optimisation if qty op
846 if ( !$allrows && $form->{maxrows} && !$filter{qty_op}) {
847 $filter{limit} = $pages->{per_page};
848 $filter{offset} = ($page - 1) * $pages->{per_page};
850 $last_nr = $pages->{per_page};
853 my @contents = WH->get_warehouse_report(%filter);
856 if (!$form->{maxrows}) {
857 $form->{maxrows} = scalar @contents ;
860 $form->{subtotal} = '' if (!first { $_ eq $sort_col } qw(partnumber partdescription));
862 my $report = SL::ReportGenerator->new(\%myconfig, $form);
864 my @hidden_variables = map { "l_${_}" } @columns;
865 push @hidden_variables, qw(warehouse_id bin_id partnumber partstypes_id description chargenumber bestbefore qty_op qty qty_unit partunit l_warehousedescription l_bindescription);
866 push @hidden_variables, qw(include_empty_bins subtotal include_invalid_warehouses date);
867 push @hidden_variables, qw(classification_id stock_value_basis);
870 'warehousedescription' => { 'text' => $locale->text('Warehouse'), },
871 'bindescription' => { 'text' => $locale->text('Bin'), },
872 'partnumber' => { 'text' => $locale->text('Part Number'), },
873 'type_and_classific' => { 'text' => $locale->text('Type'), },
874 'partdescription' => { 'text' => $locale->text('Part Description'), },
875 'chargenumber' => { 'text' => $locale->text('Charge Number'), },
876 'bestbefore' => { 'text' => $locale->text('Best Before'), },
877 'qty' => { 'text' => $locale->text('Qty'), },
878 'partunit' => { 'text' => $locale->text('Unit'), },
879 'stock_value' => { 'text' => $locale->text('Stock value'), },
880 'purchase_price' => { 'text' => $locale->text('Purchase price'), },
881 'list_price' => { 'text' => $locale->text('List Price'), },
884 my $href = build_std_url('action=generate_report', grep { $form->{$_} } @hidden_variables);
885 $href .= "&maxrows=".$form->{maxrows};
887 map { $column_defs{$_}->{link} = $href . "&page=".$page."&sort=${_}&order=" . Q($_ eq $sort_col ? 1 - $form->{order} : $form->{order}) } @columns;
889 my %column_alignment = map { $_ => 'right' } qw(qty list_price purchase_price stock_value);
891 map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
893 $column_defs{partunit}->{visible} = 1;
894 $column_defs{type_and_classific}->{visible} = 1;
895 $column_defs{type_and_classific}->{link} ='';
897 $report->set_columns(%column_defs);
898 $report->set_column_order(@columns);
900 $report->set_export_options('generate_report', @hidden_variables, qw(sort order));
902 $report->set_sort_indicator($sort_col, $form->{order});
904 $report->set_options('top_info_text' => join("\n", @options),
905 'output_format' => 'HTML',
906 'title' => $form->{title},
907 'attachment_basename' => strftime($locale->text('warehouse_report_list') . '_%Y%m%d', localtime time));
908 $report->set_options_from_form();
909 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
911 my $all_units = AM->retrieve_units(\%myconfig, $form);
914 my @subtotals_columns = qw(qty stock_value);
915 my %subtotals = map { $_ => 0 } @subtotals_columns;
917 my $total_stock_value = 0;
919 foreach my $entry (@contents) {
921 $entry->{type_and_classific} = SL::Presenter::Part::type_abbreviation($entry->{part_type}).
922 SL::Presenter::Part::classification_abbreviation($entry->{classification_id});
923 map { $subtotals{$_} += $entry->{$_} } @subtotals_columns;
924 $total_stock_value += $entry->{stock_value} * 1;
925 $entry->{qty} = $form->format_amount(\%myconfig, $entry->{qty});
926 # $entry->{qty} = $form->format_amount_units('amount' => $entry->{qty},
927 # 'part_unit' => $entry->{partunit},
928 # 'conv_units' => 'convertible');
929 $entry->{stock_value} = $form->format_amount(\%myconfig, $entry->{stock_value} * 1, 2);
930 $entry->{purchase_price} = $form->format_amount(\%myconfig, $entry->{purchase_price} * 1, 2);
931 $entry->{list_price} = $form->format_amount(\%myconfig, $entry->{list_price} * 1, 2);
933 my $row_set = [ { map { $_ => { 'data' => $entry->{$_}, 'align' => $column_alignment{$_} } } @columns } ];
935 if ( ($form->{subtotal} eq 'Y' && !$form->{report_generator_csv_options_for_import} )
936 && (($idx == (scalar @contents - 1))
937 || ($entry->{$sort_col} ne $contents[$idx + 1]->{$sort_col}))) {
939 my $row = { map { $_ => { 'data' => '', 'class' => 'listsubtotal', 'align' => $column_alignment{$_}, } } @columns };
940 $row->{qty}->{data} = $form->format_amount(\%myconfig, $subtotals{qty});
941 # $row->{qty}->{data} = $form->format_amount_units('amount' => $subtotals{qty} * 1,
942 # 'part_unit' => $entry->{partunit},
943 # 'conv_units' => 'convertible');
944 $row->{stock_value}->{data} = $form->format_amount(\%myconfig, $subtotals{stock_value} * 1, 2);
945 $row->{purchase_price}->{data} = $form->format_amount(\%myconfig, $subtotals{purchase_price} * 1, 2);
946 $row->{list_price}->{data} = $form->format_amount(\%myconfig, $subtotals{list_price} * 1, 2);
948 %subtotals = map { $_ => 0 } @subtotals_columns;
950 push @{ $row_set }, $row;
953 if ( $allrows || ($idx >= $first_nr && $idx < $last_nr )) {
954 $report->add_data($row_set);
959 if ( $column_defs{stock_value}->{visible} && !$form->{report_generator_csv_options_for_import} ) {
960 $report->add_separator();
962 my $row = { map { $_ => { 'data' => '', 'class' => 'listsubtotal', } } @columns };
964 my $left_col = first { $column_defs{$_}->{visible} } @columns;
966 $row->{$left_col}->{data} = $locale->text('Total stock value');
967 $row->{stock_value}->{data} = $form->format_amount(\%myconfig, $total_stock_value, 2);
968 $row->{stock_value}->{align} = 'right';
970 $report->add_data($row);
973 $pages->{max} = SL::DB::Helper::Paginated::ceil($form->{maxrows}, $pages->{per_page}) || 1;
974 $pages->{page} = $page < 1 ? 1: $page > $pages->{max} ? $pages->{max}: $page;
975 $pages->{common} = [ grep { $_->{visible} } @{ SL::DB::Helper::Paginated::make_common_pages($pages->{page}, $pages->{max}) } ];
977 $report->set_options('raw_bottom_info_text' => $form->parse_html_template('common/paginate',
978 {'pages' => $pages , 'base_url' => $href}) );
981 $report->generate_with_headers();
983 $main::lxdebug->leave_sub();
986 # --------------------------------------------------------------------
988 # --------------------------------------------------------------------
990 sub show_no_warehouses_error {
991 $main::lxdebug->enter_sub();
993 my $form = $main::form;
994 my %myconfig = %main::myconfig;
995 my $locale = $main::locale;
997 my $msg = $locale->text('No warehouse has been created yet or the quantity of the bins is not configured yet.') . ' ';
999 if ($main::auth->check_right($::myconfig{login}, 'config')) {
1000 $msg .= $locale->text('You can create warehouses and bins via the menu "System -> Warehouses".');
1002 $msg .= $locale->text('Please ask your administrator to create warehouses and bins.');
1005 $form->show_generic_error($msg);
1007 $main::lxdebug->leave_sub();
1010 sub get_warehouse_idx {
1011 my ($warehouse_id) = @_;
1013 my $form = $main::form;
1015 for (my $i = 0; $i < scalar @{$form->{WAREHOUSES}}; $i++) {
1016 return $i if ($form->{WAREHOUSES}->[$i]->{id} == $warehouse_id);
1023 my ($warehouse_index, $bin_id) = @_;
1025 my $form = $main::form;
1027 my $warehouse = $form->{WAREHOUSES}->[$warehouse_index];
1029 return -1 if (!$warehouse);
1031 for (my $i = 0; $i < scalar @{ $warehouse->{BINS} }; $i++) {
1032 return $i if ($warehouse->{BINS}->[$i]->{id} == $bin_id);
1039 $main::lxdebug->enter_sub();
1042 my $form = $main::form;
1045 $form->{old_callback} = $form->escape($form->{callback}, 1);
1046 $form->{callback} = $form->escape("$form->{script}?action=$params{action}", 1);
1048 # save all form variables except action in a previousform variable
1049 my $previousform = join '&', map { my $value = $form->{$_}; $value =~ s/&/%26/; "$_=$value" } grep { !/action/ } keys %$form;
1052 # push @HIDDENS, { 'name' => 'previousform', 'value' => $form->escape($previousform, 1) };
1053 push @HIDDENS, map +{ 'name' => $_, 'value' => $form->{$_} }, qw(partnumber description unit vc sellprice ean);
1054 push @HIDDENS, { 'name' => 'taxaccount2', 'value' => $form->{taxaccounts} };
1055 push @HIDDENS, { 'name' => 'notes', 'value' => $form->{longdescription} };
1058 print $form->parse_html_template("generic/new_item", { HIDDENS => [ sort { $a->{name} cmp $b->{name} } @HIDDENS ] } );
1060 $main::lxdebug->leave_sub();
1064 my $form = $main::form;
1065 call_sub($form->{update_nextsub} || $form->{nextsub});
1069 my $form = $main::form;
1070 call_sub($form->{continue_nextsub} || $form->{nextsub});
1074 my $form = $main::form;
1075 call_sub($form->{stock_nextsub} || $form->{nextsub});
1078 sub setup_wh_transfer_warehouse_selection_action_bar {
1081 for my $bar ($::request->layout->get('actionbar')) {
1085 submit => [ '#form', { action => $action } ],
1086 accesskey => 'enter',
1092 sub setup_wh_transfer_warehouse_selection_assembly_action_bar {
1095 for my $bar ($::request->layout->get('actionbar')) {
1099 submit => [ '#form', { action => 'transfer_assembly_update_part' } ],
1100 accesskey => 'enter',
1104 submit => [ '#form', { action => 'create_assembly' } ],
1105 disabled => $::form->{parts_id} ? undef : $::locale->text('No assembly has been selected yet.'),
1111 sub setup_wh_transfer_parts_action_bar {
1114 for my $bar ($::request->layout->get('actionbar')) {
1118 submit => [ '#form', { action => 'transfer_parts' } ],
1119 accesskey => 'enter',
1123 call => [ 'kivi.history_back' ],
1129 sub setup_wh_removal_parts_selection_action_bar {
1132 for my $bar ($::request->layout->get('actionbar')) {
1136 submit => [ '#form', { action => 'remove_parts' } ],
1137 accesskey => 'enter',
1141 call => [ 'kivi.history_back' ],
1147 sub setup_wh_report_action_bar {
1150 for my $bar ($::request->layout->get('actionbar')) {
1154 submit => [ '#form', { action => 'generate_report' } ],
1155 accesskey => 'enter',
1161 sub setup_wh_journal_action_bar {
1164 for my $bar ($::request->layout->get('actionbar')) {
1168 submit => [ '#form', { action => 'generate_journal' } ],
1169 accesskey => 'enter',
1181 bin/mozilla/wh.pl - Warehouse frontend.
1189 call new item dialogue from warehouse masks.
1192 action => name of sub to be called when new item is done