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')
75 # --------------------------------------------------------------------
77 # --------------------------------------------------------------------
79 sub transfer_warehouse_selection {
80 $main::lxdebug->enter_sub();
82 $main::auth->assert('warehouse_management');
84 my $form = $main::form;
85 my %myconfig = %main::myconfig;
86 my $locale = $main::locale;
88 $form->get_lists('warehouses' => { 'key' => 'WAREHOUSES',
91 show_no_warehouses_error() if (!scalar @{ $form->{WAREHOUSES} });
93 my $units = AM->retrieve_units(\%myconfig, $form);
96 if ( $form->{parts_id} ) {
97 $part = SL::DB::Part->new();
98 $part->id($form->{parts_id});
102 # der zweite Parameter von unit_select_data gibt den default-Namen (selected) vor
103 $form->{UNITS} = AM->unit_select_data($units, $form->{unit}, 0, $part ? $part->unit : 0);
105 if (scalar @{ $form->{WAREHOUSES} }) {
106 $form->{warehouse_id} ||= $form->{WAREHOUSES}->[0]->{id};
107 $form->{bin_id} ||= $form->{WAREHOUSES}->[0]->{BINS}->[0]->{id};
112 if ($form->{trans_type} eq 'removal') {
113 setup_wh_transfer_warehouse_selection_action_bar("removal_parts_selection");
114 $form->{title} = $locale->text('Removal from Warehouse');
115 $content = $form->parse_html_template('wh/warehouse_selection');
117 } elsif (!$form->{trans_type} || ($form->{trans_type} eq 'transfer')) {
118 setup_wh_transfer_warehouse_selection_action_bar("transfer_parts_selection");
119 $form->{title} = $locale->text('Transfer');
120 $content = $form->parse_html_template('wh/warehouse_selection');
122 } elsif ($form->{trans_type} eq 'assembly') {
123 setup_wh_transfer_warehouse_selection_assembly_action_bar();
124 $form->{title} = $locale->text('Produce Assembly');
125 $content = $form->parse_html_template('wh/warehouse_selection_assembly');
131 $main::lxdebug->leave_sub();
134 sub transfer_parts_selection {
135 $main::lxdebug->enter_sub();
137 $main::auth->assert('warehouse_management');
139 my $form = $main::form;
140 my %myconfig = %main::myconfig;
141 my $locale = $main::locale;
143 transfer_or_removal_prepare_contents('direction' => 'transfer');
145 setup_wh_transfer_parts_action_bar();
147 $form->{title} = $locale->text('Transfer');
149 print $form->parse_html_template("wh/transfer_parts_selection");
151 $main::lxdebug->leave_sub();
154 sub transfer_or_removal_prepare_contents {
155 $main::lxdebug->enter_sub();
157 $main::auth->assert('warehouse_management');
161 my $form = $main::form;
162 my %myconfig = %main::myconfig;
163 my $locale = $main::locale;
165 $form->get_lists('warehouses' => { 'key' => 'WAREHOUSES',
166 'bins' => 'BINS', });
168 my $warehouse_idx = get_warehouse_idx($form->{warehouse_id});
169 $form->show_generic_error($locale->text("The selected warehouse does not exist.")) if (-1 == $warehouse_idx);
171 my $warehouse = $form->{WAREHOUSES}->[$warehouse_idx];
173 $form->{initial_warehouse_idx} = $warehouse_idx;
174 $form->{warehouse_description} = $warehouse->{description};
175 $warehouse->{selected} = 1;
177 $form->show_generic_error($locale->text("The source warehouse does not contain any bins.")) if (0 == scalar @{ $warehouse->{BINS} });
179 map { $form->{"l_$_"} = 'Y' } qw(parts_id qty warehouseid binid partnumber partdescription bindescription chargenumber bestbefore partunit ean);
181 $form->{sort} = 'bindescription';
182 my @contents = WH->get_warehouse_report("warehouse_id" => $form->{warehouse_id},
183 "bin_id" => $form->{bin_id},
184 "chargenumber" => $form->{chargenumber},
185 "bestbefore" => $form->{bestbefore},
186 "partsid" => $form->{part_id},
187 "ean" => $form->{ean});
189 if (0 == scalar(@contents)) {
190 $form->show_generic_error($locale->text("The selected warehouse is empty, or no stocked items where found that match the filter settings."));
193 my $all_units = AM->retrieve_units(\%myconfig, $form);
195 foreach (@contents) {
196 $_->{qty} = $form->format_amount_units('amount' => $_->{qty},
197 'part_unit' => $_->{partunit},
198 'conv_units' => 'convertible');
199 my $this_unit = $_->{partunit};
201 if ($all_units->{$_->{partunit}} && ($all_units->{g}->{base_unit} eq $all_units->{$_->{partunit}}->{base_unit})) {
205 $_->{UNITS} = AM->unit_select_data($all_units, $this_unit, 0, $_->{partunit});
208 my $transfer_types = WH->retrieve_transfer_types($args{direction});
209 map { $_->{description} = $locale->text($_->{description}) } @{ $transfer_types };
211 $form->{CONTENTS} = \@contents;
212 $form->{TRANSFER_TYPES} = $transfer_types;
214 $main::lxdebug->leave_sub();
219 $main::lxdebug->enter_sub();
221 $main::auth->assert('warehouse_management');
223 my $form = $main::form;
224 my %myconfig = %main::myconfig;
225 my $locale = $main::locale;
227 $form->get_lists('warehouses' => { 'key' => 'WAREHOUSES', 'bins' => 'BINS' });
229 my $warehouse_idx = get_warehouse_idx($form->{warehouse_id});
230 $form->show_generic_error($locale->text("The selected warehouse does not exist.")) if (-1 == $warehouse_idx);
232 my $warehouse = $form->{WAREHOUSES}->[$warehouse_idx];
234 $form->show_generic_error($locale->text("The source warehouse does not contain any bins.")) if (0 == scalar @{ $warehouse->{BINS} });
236 map { $form->{"l_$_"} = 'Y' } qw(parts_id qty warehouseid binid partnumber partdescription bindescription chargenumber bestbefore partunit);
238 $form->{sort} = 'bindescription';
239 my @contents = WH->get_warehouse_report("warehouse_id" => $form->{warehouse_id});
240 my $all_units = AM->retrieve_units(\%myconfig, $form);
244 foreach my $row (1 .. $form->{rowcount}) {
245 $form->{"qty_$row"} =~ s/^\s*//;
246 $form->{"qty_$row"} =~ s/\s*$//;
247 next if (!$form->{"qty_$row"});
249 my $bin_idx = get_bin_idx($warehouse_idx, $form->{"src_bin_id_$row"});
250 $form->show_generic_error($locale->text("The selected bin does not exist.")) if (-1 == $bin_idx);
251 my $bin = $warehouse->{BINS}->[$bin_idx];
253 my $orig_qty = $form->{"qty_$row"} . " " . $form->{"unit_$row"};
256 'src_warehouse_id' => $form->{warehouse_id},
257 'transfer_type_id' => $form->{transfer_type_id},
260 map { $transfer->{$_} = $form->{"${_}_${row}"} } qw(src_bin_id chargenumber bestbefore parts_id qty dst_warehouse_id dst_bin_id);
264 foreach (@contents) {
265 if (($_->{binid} == $transfer->{src_bin_id}) && ($_->{parts_id} == $transfer->{parts_id}) && ($_->{chargenumber} eq $transfer->{chargenumber}) && $_->{bestbefore} eq $transfer->{bestbefore}) {
272 $form->error($locale->text("There is not enough left of '#1' in bin '#2' for the removal of #3.",
273 $form->{"partdescription_$row"}, $bin->{description}, $orig_qty));
276 $transfer->{qty} = $form->parse_amount(\%myconfig, $transfer->{qty}) * $all_units->{$form->{"unit_$row"}}->{factor};
277 $transfer->{qty} /= $all_units->{$entry->{partunit}}->{factor} || 1;
279 if (($entry->{qty} < $transfer->{qty}) || (0 >= $transfer->{qty})) {
280 $form->error($locale->text("There is not enough left of '#1' in bin '#2' for the removal of #3.",
281 $form->{"partdescription_$row"}, $bin->{description}, $orig_qty));
284 $transfer->{comment} = $form->{comment};
285 $transfer->{change_default_bin} = $form->{change_default_bin};
287 push @transfers, $transfer;
289 $entry->{qty} -= $transfer->{qty};
292 if (!scalar @transfers) {
293 $form->show_generic_information($locale->text('Nothing has been selected for transfer.'));
294 $::dispatcher->end_request;
297 WH->transfer(@transfers);
299 $form->{trans_type} = 'transfer';
300 $form->{saved_message} = $locale->text('The parts have been transferred.');
302 transfer_warehouse_selection();
304 $main::lxdebug->leave_sub();
307 # --------------------------------------------------------------------
309 # --------------------------------------------------------------------
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 my $form = $main::form;
321 my %myconfig = %main::myconfig;
322 my $locale = $main::locale;
324 $form->{trans_type} = 'assembly';
325 $form->{qty} = $form->parse_amount(\%myconfig, $form->{qty});
327 if (!$form->{parts_id}) {
328 delete $form->{partunit};
329 transfer_warehouse_selection();
334 my $part = SL::DB::Part->new(id => $::form->{parts_id})->load;
335 @{$form}{qw(parts_id partnumber description)} = ($part->id, $part->partnumber, $part->description);
337 transfer_stock_get_partunit();
338 transfer_warehouse_selection();
341 sub transfer_stock_part_selected {
342 $main::lxdebug->enter_sub();
346 my $form = $main::form;
348 @{$form}{qw(parts_id partnumber description ean warehouse_id bin_id)} = @{$part}{qw(id partnumber description ean warehouse_id bin_id)};
350 transfer_stock_get_partunit();
351 transfer_warehouse_selection();
353 $main::lxdebug->leave_sub();
356 sub transfer_stock_get_partunit {
357 $main::lxdebug->enter_sub();
359 my $form = $main::form;
361 if ($form->{parts_id}) {
362 my $part_info = IC->get_basic_part_info('id' => $form->{parts_id});
363 $form->{partunit} = $part_info->{unit};
366 $main::lxdebug->leave_sub();
369 # vorüberlegung jb 22.2.2009
370 # wir benötigen für diese funktion, die anzahl die vom erzeugnis hergestellt werden soll. vielleicht direkt per js fehleingaben verhindern?
371 # ferner dann nochmal mit check_asssembly_max_create gegenprüfen und dann transaktionssicher wegbuchen.
372 # 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
373 # Manko ist derzeit noch, dass unterschiedliche Lagerplätze, bzw. das Quelllager an sich nicht ausgewählt werden können.
374 # Laut Absprache in KW11 09 übernimmt mb hier den rest im April ... jb 18.3.09
376 sub create_assembly {
377 $main::lxdebug->enter_sub();
379 my $form = $main::form;
380 my %myconfig = %main::myconfig;
381 my $locale = $main::locale;
383 $form->{qty} = $form->parse_amount(\%myconfig, $form->{qty});
384 if ($form->{qty} <= 0) {
385 $form->show_generic_error($locale->text('Invalid quantity.'));
387 # TODO Es wäre schön, hier schon die maximale Anzahl der zu fertigenden Erzeugnisse zu haben
388 #else { if ($form->{qty} > $maxcreate) { #s.o.
389 # $form->show_generic_error($locale->text('Can not create that quantity with current stock'));
390 # $form->show_generic_error('Maximale Stückzahl' . $maxcreate);
394 if (!$form->{warehouse_id} || !$form->{bin_id}) {
395 $form->error($locale->text('The warehouse or the bin is missing.'));
398 if (!$::instance_conf->get_show_bestbefore) {
399 $form->{bestbefore} = '';
402 # 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
403 # Anm. jb 18.3. vielleicht auch nur meine unwissenheit in perl-datenstrukturen
405 'transfer_type' => 'assembly',
406 'login' => $::myconfig{login},
407 'dst_warehouse_id' => $form->{warehouse_id},
408 'dst_bin_id' => $form->{bin_id},
409 'chargenumber' => $form->{chargenumber},
410 'bestbefore' => $form->{bestbefore},
411 'assembly_id' => $form->{parts_id},
412 'qty' => $form->{qty},
413 'unit' => $form->{unit},
414 'comment' => $form->{comment}
417 my $ret = WH->transfer_assembly (%TRANSFER);
418 # Frage: Ich pack in den return-wert auch gleich die Fehlermeldung. Irgendwelche Nummern als Fehlerkonstanten definieren find ich auch nicht besonders schick...
421 # 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
422 $form->show_generic_error($ret);
425 delete @{$form}{qw(parts_id partnumber description qty unit chargenumber bestbefore comment)};
427 $form->{saved_message} = $locale->text('The assembly has been created.');
428 $form->{trans_type} = 'assembly';
430 transfer_warehouse_selection();
432 $main::lxdebug->leave_sub();
435 # --------------------------------------------------------------------
437 # --------------------------------------------------------------------
439 sub removal_parts_selection {
440 $main::lxdebug->enter_sub();
442 $main::auth->assert('warehouse_management');
444 my $form = $main::form;
445 my %myconfig = %main::myconfig;
446 my $locale = $main::locale;
448 transfer_or_removal_prepare_contents('direction' => 'out');
450 setup_wh_removal_parts_selection_action_bar();
452 $form->{title} = $locale->text('Removal');
454 print $form->parse_html_template("wh/removal_parts_selection");
456 $main::lxdebug->leave_sub();
460 $main::lxdebug->enter_sub();
462 $main::auth->assert('warehouse_management');
464 my $form = $main::form;
465 my %myconfig = %main::myconfig;
466 my $locale = $main::locale;
468 $form->get_lists('warehouses' => { 'key' => 'WAREHOUSES',
469 'bins' => 'BINS', });
471 my $warehouse_idx = get_warehouse_idx($form->{warehouse_id});
472 $form->show_generic_error($locale->text("The selected warehouse does not exist.")) if (-1 == $warehouse_idx);
474 my $warehouse = $form->{WAREHOUSES}->[$warehouse_idx];
476 $form->show_generic_error($locale->text("The warehouse does not contain any bins.")) if (0 == scalar @{ $warehouse->{BINS} });
478 map { $form->{"l_$_"} = 'Y' } qw(parts_id qty warehouseid binid partnumber partdescription bindescription chargenumber bestbefore partunit);
480 $form->{sort} = 'bindescription';
481 my @contents = WH->get_warehouse_report("warehouse_id" => $form->{warehouse_id});
482 my $all_units = AM->retrieve_units(\%myconfig, $form);
486 foreach my $row (1 .. $form->{rowcount}) {
487 $form->{"qty_$row"} =~ s/^\s*//;
488 $form->{"qty_$row"} =~ s/\s*$//;
489 next if (!$form->{"qty_$row"});
491 my $bin_idx = get_bin_idx($warehouse_idx, $form->{"src_bin_id_$row"});
492 $form->show_generic_error($locale->text("The selected bin does not exist.")) if (-1 == $bin_idx);
493 my $bin = $warehouse->{BINS}->[$bin_idx];
495 my $orig_qty = $form->{"qty_$row"} . " " . $form->{"unit_$row"};
498 'src_warehouse_id' => $form->{warehouse_id},
499 'transfer_type_id' => $form->{transfer_type_id},
502 map { $transfer->{$_} = $form->{"${_}_${row}"} } qw(src_bin_id chargenumber bestbefore parts_id qty);
506 foreach (@contents) {
507 if (($_->{binid} == $transfer->{src_bin_id}) && ($_->{parts_id} == $transfer->{parts_id}) && ($_->{chargenumber} eq $transfer->{chargenumber}) && ($_->{bestbefore} eq $transfer->{bestbefore})) {
514 $form->error($locale->text("There is not enough left of '#1' in bin '#2' for the removal of #3.",
515 $form->{"partdescription_$row"}, $bin->{description}, $orig_qty));
518 $transfer->{qty} = $form->parse_amount(\%myconfig, $transfer->{qty}) * $all_units->{$form->{"unit_$row"}}->{factor};
519 $transfer->{qty} /= $all_units->{$entry->{partunit}}->{factor} || 1;
521 if (($entry->{qty} < $transfer->{qty}) || (0 >= $transfer->{qty})) {
522 $form->error($locale->text("There is not enough left of '#1' in bin '#2' for the removal of #3.",
523 $form->{"partdescription_$row"}, $bin->{description}, $orig_qty));
526 $transfer->{comment} = $form->{comment};
528 push @transfers, $transfer;
530 $entry->{qty} -= $transfer->{qty};
533 if (!scalar @transfers) {
534 $form->show_generic_information($locale->text('Nothing has been selected for removal.'));
535 $::dispatcher->end_request;
538 WH->transfer(@transfers);
540 $form->{trans_type} = 'removal';
541 $form->{saved_message} = $locale->text('The parts have been removed.');
543 transfer_warehouse_selection();
545 $main::lxdebug->leave_sub();
548 # --------------------------------------------------------------------
550 # --------------------------------------------------------------------
553 $main::lxdebug->enter_sub();
555 $main::auth->assert('warehouse_management');
557 my $form = $main::form;
558 my %myconfig = %main::myconfig;
559 my $locale = $main::locale;
561 $form->{title} = $locale->text('Report about warehouse transactions');
562 $form->get_lists('warehouses' => { 'key' => 'WAREHOUSES',
563 'bins' => 'BINS', });
565 show_no_warehouses_error() if (!scalar @{ $form->{WAREHOUSES} });
567 setup_wh_journal_action_bar();
570 print $form->parse_html_template("wh/journal_filter", { "UNITS" => AM->unit_select_data(AM->retrieve_units(\%myconfig, $form)) });
572 $main::lxdebug->leave_sub();
575 sub generate_journal {
576 $main::lxdebug->enter_sub();
578 $main::auth->assert('warehouse_management');
580 my $form = $main::form;
581 my %myconfig = %main::myconfig;
582 my $locale = $main::locale;
584 $form->{title} = $locale->text("WHJournal");
585 $form->{sort} ||= 'date';
587 $form->{report_generator_output_format} = 'HTML' if !$form->{report_generator_output_format};
590 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);
593 map { $filter{$_} = $form->{$_} if ($form->{$_}) } qw(warehouse_id bin_id classification_id partnumber description chargenumber bestbefore transtype_id transtype_ids comment projectnumber);
595 $filter{qty_op} = WH->convert_qty_op($form->{qty_op});
596 if ($filter{qty_op}) {
597 $form->isblank("qty", $locale->text('Quantity missing.'));
598 $form->isblank("qty_unit", $locale->text('Unit missing.'));
600 $filter{qty} = $form->{qty};
601 $filter{qty_unit} = $form->{qty_unit};
605 my $allrows = !!($form->{report_generator_output_format} ne 'HTML') ;
609 my $page = $::form->{page} || 1;
610 $pages->{per_page} = $::form->{per_page} || 15;
611 my $first_nr = ($page - 1) * $pages->{per_page};
612 my $last_nr = $first_nr + $pages->{per_page};
614 # no optimisation if qty op
615 if ( !$allrows && $form->{maxrows} && !$filter{qty_op}) {
616 $filter{limit} = $pages->{per_page};
617 $filter{offset} = ($page - 1) * $pages->{per_page};
619 $last_nr = $pages->{per_page};
622 my @contents = WH->get_warehouse_journal(%filter);
624 if (!$form->{maxrows}) {
625 $form->{maxrows} = scalar @contents ;
628 my $report = SL::ReportGenerator->new(\%myconfig, $form);
630 my @hidden_variables = map { "l_${_}" } @columns;
631 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);
632 push @hidden_variables, qw(classification_id);
635 'date' => { 'text' => $locale->text('Date'), },
636 'trans_id' => { 'text' => $locale->text('Trans Id'), },
637 'trans_type' => { 'text' => $locale->text('Trans Type'), },
638 'comment' => { 'text' => $locale->text('Comment'), },
639 'warehouse_from' => { 'text' => $locale->text('Warehouse From'), },
640 'warehouse_to' => { 'text' => $locale->text('Warehouse To'), },
641 'bin_from' => { 'text' => $locale->text('Bin From'), },
642 'bin_to' => { 'text' => $locale->text('Bin To'), },
643 'partnumber' => { 'text' => $locale->text('Part Number'), },
645 => { 'text' => $locale->text('Type'), },
646 'partdescription' => { 'text' => $locale->text('Part Description'), },
647 'chargenumber' => { 'text' => $locale->text('Charge Number'), },
648 'bestbefore' => { 'text' => $locale->text('Best Before'), },
649 'qty' => { 'text' => $locale->text('Qty'), },
650 'unit' => { 'text' => $locale->text('Part Unit'), },
651 'partunit' => { 'text' => $locale->text('Unit'), },
652 'employee' => { 'text' => $locale->text('Employee'), },
653 'projectnumber' => { 'text' => $locale->text('Project Number'), },
654 'oe_id' => { 'text' => $locale->text('Document'), },
657 if ($form->{transtype_ids} && 'ARRAY' eq ref $form->{transtype_ids}) {
658 for (my $i = 0; $i < scalar(@{ $form->{transtype_ids} }); $i++) {
659 delete $form->{transtype_ids}[$i] if $form->{transtype_ids}[$i] eq '';
661 $form->{transtype_ids} = join(",", @{ $form->{transtype_ids} });
664 my $href = build_std_url('action=generate_journal', grep { $form->{$_} } @hidden_variables);
665 $href .= "&maxrows=".$form->{maxrows};
667 map { $column_defs{$_}->{link} = $href ."&page=".$page. "&sort=${_}&order=" . Q($_ eq $form->{sort} ? 1 - $form->{order} : $form->{order}) } @columns;
669 my %column_alignment = map { $_ => 'right' } qw(qty);
671 map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
672 $column_defs{partunit}->{visible} = 1;
673 $column_defs{type_and_classific}->{visible} = 1;
674 $column_defs{type_and_classific}->{link} ='';
676 $report->set_columns(%column_defs);
677 $report->set_column_order(@columns);
679 $report->set_export_options('generate_journal', @hidden_variables, qw(sort order));
681 $report->set_sort_indicator($form->{sort}, $form->{order});
683 $report->set_options('output_format' => 'HTML',
684 'title' => $form->{title},
685 'attachment_basename' => strftime($locale->text('warehouse_journal_list') . '_%Y%m%d', localtime time));
686 $report->set_options_from_form();
687 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
689 my $all_units = AM->retrieve_units(\%myconfig, $form);
691 my %doc_types = ( 'sales_quotation' => { script => 'oe', title => $locale->text('Sales quotation') },
692 'sales_order' => { script => 'oe', title => $locale->text('Sales Order') },
693 'request_quotation' => { script => 'oe', title => $locale->text('Request quotation') },
694 'purchase_order' => { script => 'oe', title => $locale->text('Purchase Order') },
695 'sales_delivery_order' => { script => 'do', title => $locale->text('Sales delivery order') },
696 'purchase_delivery_order' => { script => 'do', title => $locale->text('Purchase delivery order') },
697 'sales_invoice' => { script => 'is', title => $locale->text('Sales Invoice') },
698 'purchase_invoice' => { script => 'ir', title => $locale->text('Purchase Invoice') },
703 foreach my $entry (@contents) {
704 $entry->{type_and_classific} = SL::Presenter::Part::type_abbreviation($entry->{part_type}) .
705 SL::Presenter::Part::classification_abbreviation($entry->{classification_id});
706 $entry->{qty} = $form->format_amount(\%myconfig, $entry->{qty});
707 $entry->{trans_type} = $locale->text($entry->{trans_type});
711 foreach my $column (@columns) {
713 'data' => $entry->{$column},
714 'align' => $column_alignment{$column},
718 $row->{trans_type}->{raw_data} = $entry->{trans_type};
720 if ($form->{l_oe_id}) {
721 $row->{oe_id}->{data} = '';
722 my $info = $entry->{oe_id_info};
724 if ($info && $info->{id} && $info->{type} && $doc_types{$info->{type}}) {
725 $row->{oe_id} = { data => $doc_types{ $info->{type} }->{title} . ' ' . $info->{number},
726 link => build_std_url('script=' . $doc_types{ $info->{type} }->{script} . '.pl', 'action=edit', 'id=' . $info->{id}, 'type=' . $info->{type}) };
730 if ( $allrows || ($idx >= $first_nr && $idx < $last_nr )) {
731 $report->add_data($row);
737 $pages->{max} = SL::DB::Helper::Paginated::ceil($form->{maxrows}, $pages->{per_page}) || 1;
738 $pages->{page} = $page < 1 ? 1: $page > $pages->{max} ? $pages->{max}: $page;
739 $pages->{common} = [ grep { $_->{visible} } @{ SL::DB::Helper::Paginated::make_common_pages($pages->{page}, $pages->{max}) } ];
741 $report->set_options('raw_bottom_info_text' => $form->parse_html_template('common/paginate',
742 { 'pages' => $pages , 'base_url' => $href.'&sort='.$form->{sort}.'&order='.$form->{order}}) );
744 $report->generate_with_headers();
746 $main::lxdebug->leave_sub();
749 # --------------------------------------------------------------------
751 # --------------------------------------------------------------------
754 $main::lxdebug->enter_sub();
756 $main::auth->assert('warehouse_contents | warehouse_management');
758 my $form = $main::form;
759 my %myconfig = %main::myconfig;
760 my $locale = $main::locale;
762 $form->get_lists('warehouses' => { 'key' => 'WAREHOUSES',
763 'bins' => 'BINS', });
765 show_no_warehouses_error() if (!scalar @{ $form->{WAREHOUSES} });
767 $form->{title} = $locale->text("Report about warehouse contents");
769 setup_wh_report_action_bar();
772 print $form->parse_html_template("wh/report_filter",
773 { "WAREHOUSES" => $form->{WAREHOUSES},
774 "UNITS" => AM->unit_select_data(AM->retrieve_units(\%myconfig, $form)) });
776 $main::lxdebug->leave_sub();
779 sub generate_report {
780 $main::lxdebug->enter_sub();
782 $main::auth->assert('warehouse_contents | warehouse_management');
784 my $form = $main::form;
785 my %myconfig = %main::myconfig;
786 my $locale = $main::locale;
788 $form->{title} = $locale->text("Report about warehouse contents");
789 $form->{sort} ||= 'partnumber';
790 my $sort_col = $form->{sort};
793 my @columns = qw(warehousedescription bindescription partnumber type_and_classific partdescription chargenumber bestbefore comment qty partunit purchase_price stock_value);
796 map { $filter{$_} = $form->{$_} if ($form->{$_}) } qw(warehouse_id bin_id classification_id partnumber description chargenumber bestbefore date include_invalid_warehouses);
798 # show filter stuff also in report
800 my $currentdate = $form->current_date(\%myconfig);
801 push @options, $locale->text('Printdate') . " : ".$locale->date(\%myconfig, $currentdate, 1);
803 # dispatch all options
804 my $dispatch_options = {
805 warehouse_id => sub { push @options, $locale->text('Warehouse') . " : " .
806 SL::DB::Manager::Warehouse->find_by(id => $form->{warehouse_id})->description},
807 bin_id => sub { push @options, $locale->text('Bin') . " : " .
808 SL::DB::Manager::Bin->find_by(id => $form->{bin_id})->description},
809 partnumber => sub { push @options, $locale->text('Partnumber') . " : $form->{partnumber}"},
810 classification_id => sub { push @options, $locale->text('Parts Classification'). " : ".
811 SL::DB::Manager::PartClassification->get_first(where => [ id => $form->{classification_id} ] )->description; },
812 description => sub { push @options, $locale->text('Description') . " : $form->{description}"},
813 chargenumber => sub { push @options, $locale->text('Charge Number') . " : $form->{chargenumber}"},
814 bestbefore => sub { push @options, $locale->text('Best Before') . " : $form->{bestbefore}"},
815 include_invalid_warehouses => sub { push @options, $locale->text('Include invalid warehouses ')},
817 foreach (keys %filter) {
818 $dispatch_options->{$_}->() if $dispatch_options->{$_};
820 push @options, $locale->text('Stock Qty for Date') . " " . $locale->date(\%myconfig, $form->{date}?$form->{date}:$currentdate, 1);
822 # / end show filter stuff also in report
824 $filter{qty_op} = WH->convert_qty_op($form->{qty_op});
825 if ($filter{qty_op}) {
826 $form->isblank("qty", $locale->text('Quantity missing.'));
827 $form->isblank("qty_unit", $locale->text('Unit missing.'));
829 $filter{qty} = $form->{qty};
830 $filter{qty_unit} = $form->{qty_unit};
834 $form->{report_generator_output_format} = 'HTML' if !$form->{report_generator_output_format};
837 my $allrows = !!($form->{report_generator_output_format} ne 'HTML') ;
838 my $page = $::form->{page} || 1;
840 $pages->{per_page} = $::form->{per_page} || 20;
841 my $first_nr = ($page - 1) * $pages->{per_page};
842 my $last_nr = $first_nr + $pages->{per_page};
844 # no optimisation if qty op
845 if ( !$allrows && $form->{maxrows} && !$filter{qty_op}) {
846 $filter{limit} = $pages->{per_page};
847 $filter{offset} = ($page - 1) * $pages->{per_page};
849 $last_nr = $pages->{per_page};
852 my @contents = WH->get_warehouse_report(%filter);
855 if (!$form->{maxrows}) {
856 $form->{maxrows} = scalar @contents ;
859 $form->{subtotal} = '' if (!first { $_ eq $sort_col } qw(partnumber partdescription));
861 my $report = SL::ReportGenerator->new(\%myconfig, $form);
863 my @hidden_variables = map { "l_${_}" } @columns;
864 push @hidden_variables, qw(warehouse_id bin_id partnumber partstypes_id description chargenumber bestbefore qty_op qty qty_unit partunit l_warehousedescription l_bindescription);
865 push @hidden_variables, qw(include_empty_bins subtotal include_invalid_warehouses date);
866 push @hidden_variables, qw(classification_id);
869 'warehousedescription' => { 'text' => $locale->text('Warehouse'), },
870 'bindescription' => { 'text' => $locale->text('Bin'), },
871 'partnumber' => { 'text' => $locale->text('Part Number'), },
872 'type_and_classific' => { 'text' => $locale->text('Type'), },
873 'partdescription' => { 'text' => $locale->text('Part Description'), },
874 'chargenumber' => { 'text' => $locale->text('Charge Number'), },
875 'bestbefore' => { 'text' => $locale->text('Best Before'), },
876 'qty' => { 'text' => $locale->text('Qty'), },
877 'partunit' => { 'text' => $locale->text('Unit'), },
878 'stock_value' => { 'text' => $locale->text('Stock value'), },
879 'purchase_price' => { 'text' => $locale->text('Purchase price'), },
882 my $href = build_std_url('action=generate_report', grep { $form->{$_} } @hidden_variables);
883 $href .= "&maxrows=".$form->{maxrows};
885 map { $column_defs{$_}->{link} = $href . "&page=".$page."&sort=${_}&order=" . Q($_ eq $sort_col ? 1 - $form->{order} : $form->{order}) } @columns;
887 my %column_alignment = map { $_ => 'right' } qw(qty purchase_price stock_value);
889 map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
891 $column_defs{partunit}->{visible} = 1;
892 $column_defs{type_and_classific}->{visible} = 1;
893 $column_defs{type_and_classific}->{link} ='';
895 $report->set_columns(%column_defs);
896 $report->set_column_order(@columns);
898 $report->set_export_options('generate_report', @hidden_variables, qw(sort order));
900 $report->set_sort_indicator($sort_col, $form->{order});
902 $report->set_options('top_info_text' => join("\n", @options),
903 'output_format' => 'HTML',
904 'title' => $form->{title},
905 'attachment_basename' => strftime($locale->text('warehouse_report_list') . '_%Y%m%d', localtime time));
906 $report->set_options_from_form();
907 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
909 my $all_units = AM->retrieve_units(\%myconfig, $form);
912 my @subtotals_columns = qw(qty stock_value);
913 my %subtotals = map { $_ => 0 } @subtotals_columns;
915 my $total_stock_value = 0;
917 foreach my $entry (@contents) {
919 $entry->{type_and_classific} = SL::Presenter::Part::type_abbreviation($entry->{part_type}).
920 SL::Presenter::Part::classification_abbreviation($entry->{classification_id});
921 map { $subtotals{$_} += $entry->{$_} } @subtotals_columns;
922 $total_stock_value += $entry->{stock_value} * 1;
923 $entry->{qty} = $form->format_amount(\%myconfig, $entry->{qty});
924 # $entry->{qty} = $form->format_amount_units('amount' => $entry->{qty},
925 # 'part_unit' => $entry->{partunit},
926 # 'conv_units' => 'convertible');
927 $entry->{stock_value} = $form->format_amount(\%myconfig, $entry->{stock_value} * 1, 2);
928 $entry->{purchase_price} = $form->format_amount(\%myconfig, $entry->{purchase_price} * 1, 2);
930 my $row_set = [ { map { $_ => { 'data' => $entry->{$_}, 'align' => $column_alignment{$_} } } @columns } ];
932 if ( ($form->{subtotal} eq 'Y' && !$form->{report_generator_csv_options_for_import} )
933 && (($idx == (scalar @contents - 1))
934 || ($entry->{$sort_col} ne $contents[$idx + 1]->{$sort_col}))) {
936 my $row = { map { $_ => { 'data' => '', 'class' => 'listsubtotal', 'align' => $column_alignment{$_}, } } @columns };
937 $row->{qty}->{data} = $form->format_amount(\%myconfig, $subtotals{qty});
938 # $row->{qty}->{data} = $form->format_amount_units('amount' => $subtotals{qty} * 1,
939 # 'part_unit' => $entry->{partunit},
940 # 'conv_units' => 'convertible');
941 $row->{stock_value}->{data} = $form->format_amount(\%myconfig, $subtotals{stock_value} * 1, 2);
942 $row->{purchase_price}->{data} = $form->format_amount(\%myconfig, $subtotals{purchase_price} * 1, 2);
944 %subtotals = map { $_ => 0 } @subtotals_columns;
946 push @{ $row_set }, $row;
949 if ( $allrows || ($idx >= $first_nr && $idx < $last_nr )) {
950 $report->add_data($row_set);
955 if ( $column_defs{stock_value}->{visible} && !$form->{report_generator_csv_options_for_import} ) {
956 $report->add_separator();
958 my $row = { map { $_ => { 'data' => '', 'class' => 'listsubtotal', } } @columns };
960 my $left_col = first { $column_defs{$_}->{visible} } @columns;
962 $row->{$left_col}->{data} = $locale->text('Total stock value');
963 $row->{stock_value}->{data} = $form->format_amount(\%myconfig, $total_stock_value, 2);
964 $row->{stock_value}->{align} = 'right';
966 $report->add_data($row);
969 $pages->{max} = SL::DB::Helper::Paginated::ceil($form->{maxrows}, $pages->{per_page}) || 1;
970 $pages->{page} = $page < 1 ? 1: $page > $pages->{max} ? $pages->{max}: $page;
971 $pages->{common} = [ grep { $_->{visible} } @{ SL::DB::Helper::Paginated::make_common_pages($pages->{page}, $pages->{max}) } ];
973 $report->set_options('raw_bottom_info_text' => $form->parse_html_template('common/paginate',
974 {'pages' => $pages , 'base_url' => $href}) );
977 $report->generate_with_headers();
979 $main::lxdebug->leave_sub();
982 # --------------------------------------------------------------------
984 # --------------------------------------------------------------------
986 sub show_no_warehouses_error {
987 $main::lxdebug->enter_sub();
989 my $form = $main::form;
990 my %myconfig = %main::myconfig;
991 my $locale = $main::locale;
993 my $msg = $locale->text('No warehouse has been created yet or the quantity of the bins is not configured yet.') . ' ';
995 if ($main::auth->check_right($::myconfig{login}, 'config')) {
996 $msg .= $locale->text('You can create warehouses and bins via the menu "System -> Warehouses".');
998 $msg .= $locale->text('Please ask your administrator to create warehouses and bins.');
1001 $form->show_generic_error($msg);
1003 $main::lxdebug->leave_sub();
1006 sub get_warehouse_idx {
1007 my ($warehouse_id) = @_;
1009 my $form = $main::form;
1011 for (my $i = 0; $i < scalar @{$form->{WAREHOUSES}}; $i++) {
1012 return $i if ($form->{WAREHOUSES}->[$i]->{id} == $warehouse_id);
1019 my ($warehouse_index, $bin_id) = @_;
1021 my $form = $main::form;
1023 my $warehouse = $form->{WAREHOUSES}->[$warehouse_index];
1025 return -1 if (!$warehouse);
1027 for (my $i = 0; $i < scalar @{ $warehouse->{BINS} }; $i++) {
1028 return $i if ($warehouse->{BINS}->[$i]->{id} == $bin_id);
1035 $main::lxdebug->enter_sub();
1038 my $form = $main::form;
1041 $form->{old_callback} = $form->escape($form->{callback}, 1);
1042 $form->{callback} = $form->escape("$form->{script}?action=$params{action}", 1);
1044 # save all form variables except action in a previousform variable
1045 my $previousform = join '&', map { my $value = $form->{$_}; $value =~ s/&/%26/; "$_=$value" } grep { !/action/ } keys %$form;
1048 # push @HIDDENS, { 'name' => 'previousform', 'value' => $form->escape($previousform, 1) };
1049 push @HIDDENS, map +{ 'name' => $_, 'value' => $form->{$_} }, qw(partnumber description unit vc sellprice ean);
1050 push @HIDDENS, { 'name' => 'taxaccount2', 'value' => $form->{taxaccounts} };
1051 push @HIDDENS, { 'name' => 'notes', 'value' => $form->{longdescription} };
1054 print $form->parse_html_template("generic/new_item", { HIDDENS => [ sort { $a->{name} cmp $b->{name} } @HIDDENS ] } );
1056 $main::lxdebug->leave_sub();
1060 my $form = $main::form;
1061 call_sub($form->{update_nextsub} || $form->{nextsub});
1065 my $form = $main::form;
1066 call_sub($form->{continue_nextsub} || $form->{nextsub});
1070 my $form = $main::form;
1071 call_sub($form->{stock_nextsub} || $form->{nextsub});
1074 sub setup_wh_transfer_warehouse_selection_action_bar {
1077 for my $bar ($::request->layout->get('actionbar')) {
1081 submit => [ '#form', { action => $action } ],
1082 accesskey => 'enter',
1088 sub setup_wh_transfer_warehouse_selection_assembly_action_bar {
1091 for my $bar ($::request->layout->get('actionbar')) {
1095 submit => [ '#form', { action => 'transfer_assembly_update_part' } ],
1096 accesskey => 'enter',
1100 submit => [ '#form', { action => 'create_assembly' } ],
1101 disabled => $::form->{parts_id} ? undef : $::locale->text('No assembly has been selected yet.'),
1107 sub setup_wh_transfer_parts_action_bar {
1110 for my $bar ($::request->layout->get('actionbar')) {
1114 submit => [ '#form', { action => 'transfer_parts' } ],
1115 accesskey => 'enter',
1119 call => [ 'kivi.history_back' ],
1125 sub setup_wh_removal_parts_selection_action_bar {
1128 for my $bar ($::request->layout->get('actionbar')) {
1132 submit => [ '#form', { action => 'remove_parts' } ],
1133 accesskey => 'enter',
1137 call => [ 'kivi.history_back' ],
1143 sub setup_wh_report_action_bar {
1146 for my $bar ($::request->layout->get('actionbar')) {
1150 submit => [ '#form', { action => 'generate_report' } ],
1151 accesskey => 'enter',
1157 sub setup_wh_journal_action_bar {
1160 for my $bar ($::request->layout->get('actionbar')) {
1164 submit => [ '#form', { action => 'generate_journal' } ],
1165 accesskey => 'enter',
1177 bin/mozilla/wh.pl - Warehouse frontend.
1185 call new item dialogue from warehouse masks.
1188 action => name of sub to be called when new item is done