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);
44 use SL::Helper::Flash qw(flash_later);
48 # use SL::Helper::Inventory qw(produce_assembly);
49 use SL::Locale::String qw(t8);
50 use SL::ReportGenerator;
51 use SL::Presenter::Tag qw(checkbox_tag);
52 use SL::Presenter::Part;
58 require "bin/mozilla/common.pl";
59 require "bin/mozilla/reportgenerator.pl";
65 # contents of the "transfer_type" table:
66 # $locale->text('back')
67 # $locale->text('correction')
68 # $locale->text('disposed')
69 # $locale->text('found')
70 # $locale->text('missing')
71 # $locale->text('stock')
72 # $locale->text('shipped')
73 # $locale->text('transfer')
74 # $locale->text('used')
75 # $locale->text('return_material')
76 # $locale->text('release_material')
77 # $locale->text('assembled')
78 # $locale->text('stocktaking')
80 # --------------------------------------------------------------------
82 # --------------------------------------------------------------------
84 sub transfer_warehouse_selection {
85 $main::lxdebug->enter_sub();
87 $main::auth->assert('warehouse_management');
89 my $form = $main::form;
90 my %myconfig = %main::myconfig;
91 my $locale = $main::locale;
93 $form->get_lists('warehouses' => { 'key' => 'WAREHOUSES',
96 show_no_warehouses_error() if (!scalar @{ $form->{WAREHOUSES} });
98 my $units = AM->retrieve_units(\%myconfig, $form);
101 if ( $form->{parts_id} ) {
102 $part = SL::DB::Part->new();
103 $part->id($form->{parts_id});
107 # der zweite Parameter von unit_select_data gibt den default-Namen (selected) vor
108 $form->{UNITS} = AM->unit_select_data($units, $form->{unit}, 0, $part ? $part->unit : 0);
110 if (scalar @{ $form->{WAREHOUSES} }) {
111 $form->{warehouse_id} ||= $form->{WAREHOUSES}->[0]->{id};
112 $form->{bin_id} ||= $form->{WAREHOUSES}->[0]->{BINS}->[0]->{id};
117 if ($form->{trans_type} eq 'removal') {
118 setup_wh_transfer_warehouse_selection_action_bar("removal_parts_selection");
119 $form->{title} = $locale->text('Removal from Warehouse');
120 $content = $form->parse_html_template('wh/warehouse_selection');
122 } elsif (!$form->{trans_type} || ($form->{trans_type} eq 'transfer')) {
123 setup_wh_transfer_warehouse_selection_action_bar("transfer_parts_selection");
124 $form->{title} = $locale->text('Transfer');
125 $content = $form->parse_html_template('wh/warehouse_selection');
127 } elsif ($form->{trans_type} eq 'assembly') {
128 setup_wh_transfer_warehouse_selection_assembly_action_bar();
129 $form->{title} = $locale->text('Produce Assembly');
130 $content = $form->parse_html_template('wh/warehouse_selection_assembly');
136 $main::lxdebug->leave_sub();
139 sub transfer_parts_selection {
140 $main::lxdebug->enter_sub();
142 $main::auth->assert('warehouse_management');
144 my $form = $main::form;
145 my %myconfig = %main::myconfig;
146 my $locale = $main::locale;
148 transfer_or_removal_prepare_contents('direction' => 'transfer');
150 setup_wh_transfer_parts_action_bar();
152 $form->{title} = $locale->text('Transfer');
154 print $form->parse_html_template("wh/transfer_parts_selection");
156 $main::lxdebug->leave_sub();
159 sub transfer_or_removal_prepare_contents {
160 $main::lxdebug->enter_sub();
162 $main::auth->assert('warehouse_management');
166 my $form = $main::form;
167 my %myconfig = %main::myconfig;
168 my $locale = $main::locale;
170 $form->get_lists('warehouses' => { 'key' => 'WAREHOUSES',
171 'bins' => 'BINS', });
173 my $warehouse_idx = get_warehouse_idx($form->{warehouse_id});
174 $form->show_generic_error($locale->text("The selected warehouse does not exist.")) if (-1 == $warehouse_idx);
176 my $warehouse = $form->{WAREHOUSES}->[$warehouse_idx];
178 $form->{initial_warehouse_idx} = $warehouse_idx;
179 $form->{warehouse_description} = $warehouse->{description};
180 $warehouse->{selected} = 1;
182 $form->show_generic_error($locale->text("The source warehouse does not contain any bins.")) if (0 == scalar @{ $warehouse->{BINS} });
184 map { $form->{"l_$_"} = 'Y' } qw(parts_id qty warehouseid binid partnumber partdescription bindescription chargenumber bestbefore partunit ean);
186 $form->{sort} = 'bindescription';
187 my @contents = WH->get_warehouse_report("warehouse_id" => $form->{warehouse_id},
188 "bin_id" => $form->{bin_id},
189 "chargenumber" => $form->{chargenumber},
190 "bestbefore" => $form->{bestbefore},
191 "partsid" => $form->{part_id},
192 "ean" => $form->{ean});
194 if (0 == scalar(@contents)) {
195 $form->show_generic_error($locale->text("The selected warehouse is empty, or no stocked items where found that match the filter settings."));
198 my $all_units = AM->retrieve_units(\%myconfig, $form);
200 foreach (@contents) {
201 $_->{qty} = $form->format_amount_units('amount' => $_->{qty},
202 'part_unit' => $_->{partunit},
203 'conv_units' => 'convertible');
204 my $this_unit = $_->{partunit};
206 if ($all_units->{$_->{partunit}} && ($all_units->{g}->{base_unit} eq $all_units->{$_->{partunit}}->{base_unit})) {
210 $_->{UNITS} = AM->unit_select_data($all_units, $this_unit, 0, $_->{partunit});
213 my $transfer_types = WH->retrieve_transfer_types($args{direction});
214 map { $_->{description} = $locale->text($_->{description}) } @{ $transfer_types };
216 $form->{CONTENTS} = \@contents;
217 $form->{TRANSFER_TYPES} = $transfer_types;
219 $main::lxdebug->leave_sub();
224 $main::lxdebug->enter_sub();
226 $main::auth->assert('warehouse_management');
228 my $form = $main::form;
229 my %myconfig = %main::myconfig;
230 my $locale = $main::locale;
232 $form->get_lists('warehouses' => { 'key' => 'WAREHOUSES', 'bins' => 'BINS' });
234 my $warehouse_idx = get_warehouse_idx($form->{warehouse_id});
235 $form->show_generic_error($locale->text("The selected warehouse does not exist.")) if (-1 == $warehouse_idx);
237 my $warehouse = $form->{WAREHOUSES}->[$warehouse_idx];
239 $form->show_generic_error($locale->text("The source warehouse does not contain any bins.")) if (0 == scalar @{ $warehouse->{BINS} });
241 map { $form->{"l_$_"} = 'Y' } qw(parts_id qty warehouseid binid partnumber partdescription bindescription chargenumber bestbefore partunit);
243 $form->{sort} = 'bindescription';
244 my @contents = WH->get_warehouse_report("warehouse_id" => $form->{warehouse_id});
245 my $all_units = AM->retrieve_units(\%myconfig, $form);
249 foreach my $row (1 .. $form->{rowcount}) {
250 $form->{"qty_$row"} =~ s/^\s*//;
251 $form->{"qty_$row"} =~ s/\s*$//;
252 next if (!$form->{"qty_$row"});
254 my $bin_idx = get_bin_idx($warehouse_idx, $form->{"src_bin_id_$row"});
255 $form->show_generic_error($locale->text("The selected bin does not exist.")) if (-1 == $bin_idx);
256 my $bin = $warehouse->{BINS}->[$bin_idx];
258 my $orig_qty = $form->{"qty_$row"} . " " . $form->{"unit_$row"};
261 'src_warehouse_id' => $form->{warehouse_id},
262 'transfer_type_id' => $form->{transfer_type_id},
265 map { $transfer->{$_} = $form->{"${_}_${row}"} } qw(src_bin_id chargenumber bestbefore parts_id qty dst_warehouse_id dst_bin_id);
269 foreach (@contents) {
270 if (($_->{binid} == $transfer->{src_bin_id}) && ($_->{parts_id} == $transfer->{parts_id}) && ($_->{chargenumber} eq $transfer->{chargenumber}) && $_->{bestbefore} eq $transfer->{bestbefore}) {
277 $form->error($locale->text("There is not enough left of '#1' in bin '#2' for the removal of #3.",
278 $form->{"partdescription_$row"}, $bin->{description}, $orig_qty));
281 $transfer->{qty} = $form->parse_amount(\%myconfig, $transfer->{qty}) * $all_units->{$form->{"unit_$row"}}->{factor};
282 $transfer->{qty} /= $all_units->{$entry->{partunit}}->{factor} || 1;
284 if (($entry->{qty} < $transfer->{qty}) || (0 >= $transfer->{qty})) {
285 $form->error($locale->text("There is not enough left of '#1' in bin '#2' for the removal of #3.",
286 $form->{"partdescription_$row"}, $bin->{description}, $orig_qty));
289 $transfer->{comment} = $form->{comment};
290 $transfer->{change_default_bin} = $form->{change_default_bin};
292 push @transfers, $transfer;
294 $entry->{qty} -= $transfer->{qty};
297 if (!scalar @transfers) {
298 $form->show_generic_information($locale->text('Nothing has been selected for transfer.'));
299 $::dispatcher->end_request;
302 WH->transfer(@transfers);
304 $form->{trans_type} = 'transfer';
305 $form->{saved_message} = $locale->text('The parts have been transferred.');
307 transfer_warehouse_selection();
309 $main::lxdebug->leave_sub();
312 # --------------------------------------------------------------------
314 # --------------------------------------------------------------------
316 # --------------------------------------------------------------------
317 # Transfer: assemblies
318 # Dies ist die Auswahlmaske für ein assembly.
319 # Die ist einfach von transfer_assembly_update_part kopiert und nur um den trans_type (assembly) korrigiert worden
320 # Es wäre schön, hier nochmal check_assembly_max_create auf, um die max. Fertigungszahl herauszufinden.
321 # Ich lass das mal als auskommentierte Idee bestehen jb 18.3.09
322 # --------------------------------------------------------------------
324 sub transfer_assembly_update_part {
325 my $form = $main::form;
326 my %myconfig = %main::myconfig;
327 my $locale = $main::locale;
329 $form->{trans_type} = 'assembly';
330 $form->{qty} = $form->parse_amount(\%myconfig, $form->{qty});
332 if (!$form->{parts_id}) {
333 delete $form->{partunit};
334 transfer_warehouse_selection();
339 my $part = SL::DB::Part->new(id => $::form->{parts_id})->load;
340 @{$form}{qw(parts_id partnumber description)} = ($part->id, $part->partnumber, $part->description);
342 transfer_stock_get_partunit();
343 transfer_warehouse_selection();
346 sub transfer_stock_part_selected {
347 $main::lxdebug->enter_sub();
351 my $form = $main::form;
353 @{$form}{qw(parts_id partnumber description ean warehouse_id bin_id)} = @{$part}{qw(id partnumber description ean warehouse_id bin_id)};
355 transfer_stock_get_partunit();
356 transfer_warehouse_selection();
358 $main::lxdebug->leave_sub();
361 sub transfer_stock_get_partunit {
362 $main::lxdebug->enter_sub();
364 my $form = $main::form;
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 $main::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 $main::lxdebug->enter_sub();
384 my $form = $main::form;
385 my %myconfig = %main::myconfig;
386 my $locale = $main::locale;
388 $form->{qty} = $form->parse_amount(\%myconfig, $form->{qty});
389 if ($form->{qty} <= 0) {
390 $form->show_generic_error($locale->text('Invalid quantity.'));
392 # TODO Es wäre schön, hier schon die maximale Anzahl der zu fertigenden Erzeugnisse zu haben
393 #else { if ($form->{qty} > $maxcreate) { #s.o.
394 # $form->show_generic_error($locale->text('Can not create that quantity with current stock'));
395 # $form->show_generic_error('Maximale Stückzahl' . $maxcreate);
399 if (!$form->{warehouse_id} || !$form->{bin_id}) {
400 $form->error($locale->text('The warehouse or the bin is missing.'));
403 if (!$::instance_conf->get_show_bestbefore) {
404 $form->{bestbefore} = '';
407 # 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
408 # Anm. jb 18.3. vielleicht auch nur meine unwissenheit in perl-datenstrukturen
410 'transfer_type' => 'assembly',
411 'login' => $::myconfig{login},
412 'dst_warehouse_id' => $form->{warehouse_id},
413 'dst_bin_id' => $form->{bin_id},
414 'chargenumber' => $form->{chargenumber},
415 'bestbefore' => $form->{bestbefore},
416 'assembly_id' => $form->{parts_id},
417 'qty' => $form->{qty},
418 'unit' => $form->{unit},
419 'comment' => $form->{comment}
422 my $ret = WH->transfer_assembly (%TRANSFER);
423 # Frage: Ich pack in den return-wert auch gleich die Fehlermeldung. Irgendwelche Nummern als Fehlerkonstanten definieren find ich auch nicht besonders schick...
426 # 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
427 $form->show_generic_error($ret);
430 delete @{$form}{qw(parts_id partnumber description qty unit chargenumber bestbefore comment)};
432 $form->{saved_message} = $locale->text('The assembly has been created.');
433 $form->{trans_type} = 'assembly';
435 transfer_warehouse_selection();
437 $main::lxdebug->leave_sub();
440 # --------------------------------------------------------------------
442 # --------------------------------------------------------------------
444 sub removal_parts_selection {
445 $main::lxdebug->enter_sub();
447 $main::auth->assert('warehouse_management');
449 my $form = $main::form;
450 my %myconfig = %main::myconfig;
451 my $locale = $main::locale;
453 transfer_or_removal_prepare_contents('direction' => 'out');
455 setup_wh_removal_parts_selection_action_bar();
457 $form->{title} = $locale->text('Removal');
459 print $form->parse_html_template("wh/removal_parts_selection");
461 $main::lxdebug->leave_sub();
465 $main::lxdebug->enter_sub();
467 $main::auth->assert('warehouse_management');
469 my $form = $main::form;
470 my %myconfig = %main::myconfig;
471 my $locale = $main::locale;
473 $form->get_lists('warehouses' => { 'key' => 'WAREHOUSES',
474 'bins' => 'BINS', });
476 my $warehouse_idx = get_warehouse_idx($form->{warehouse_id});
477 $form->show_generic_error($locale->text("The selected warehouse does not exist.")) if (-1 == $warehouse_idx);
479 my $warehouse = $form->{WAREHOUSES}->[$warehouse_idx];
481 $form->show_generic_error($locale->text("The warehouse does not contain any bins.")) if (0 == scalar @{ $warehouse->{BINS} });
483 map { $form->{"l_$_"} = 'Y' } qw(parts_id qty warehouseid binid partnumber partdescription bindescription chargenumber bestbefore partunit);
485 $form->{sort} = 'bindescription';
486 my @contents = WH->get_warehouse_report("warehouse_id" => $form->{warehouse_id});
487 my $all_units = AM->retrieve_units(\%myconfig, $form);
491 foreach my $row (1 .. $form->{rowcount}) {
492 $form->{"qty_$row"} =~ s/^\s*//;
493 $form->{"qty_$row"} =~ s/\s*$//;
494 next if (!$form->{"qty_$row"});
496 my $bin_idx = get_bin_idx($warehouse_idx, $form->{"src_bin_id_$row"});
497 $form->show_generic_error($locale->text("The selected bin does not exist.")) if (-1 == $bin_idx);
498 my $bin = $warehouse->{BINS}->[$bin_idx];
500 my $orig_qty = $form->{"qty_$row"} . " " . $form->{"unit_$row"};
503 'src_warehouse_id' => $form->{warehouse_id},
504 'transfer_type_id' => $form->{transfer_type_id},
507 map { $transfer->{$_} = $form->{"${_}_${row}"} } qw(src_bin_id chargenumber bestbefore parts_id qty);
511 foreach (@contents) {
512 if (($_->{binid} == $transfer->{src_bin_id}) && ($_->{parts_id} == $transfer->{parts_id}) && ($_->{chargenumber} eq $transfer->{chargenumber}) && ($_->{bestbefore} eq $transfer->{bestbefore})) {
519 $form->error($locale->text("There is not enough left of '#1' in bin '#2' for the removal of #3.",
520 $form->{"partdescription_$row"}, $bin->{description}, $orig_qty));
523 $transfer->{qty} = $form->parse_amount(\%myconfig, $transfer->{qty}) * $all_units->{$form->{"unit_$row"}}->{factor};
524 $transfer->{qty} /= $all_units->{$entry->{partunit}}->{factor} || 1;
526 if (($entry->{qty} < $transfer->{qty}) || (0 >= $transfer->{qty})) {
527 $form->error($locale->text("There is not enough left of '#1' in bin '#2' for the removal of #3.",
528 $form->{"partdescription_$row"}, $bin->{description}, $orig_qty));
531 $transfer->{comment} = $form->{comment};
533 push @transfers, $transfer;
535 $entry->{qty} -= $transfer->{qty};
538 if (!scalar @transfers) {
539 $form->show_generic_information($locale->text('Nothing has been selected for removal.'));
540 $::dispatcher->end_request;
543 WH->transfer(@transfers);
545 $form->{trans_type} = 'removal';
546 $form->{saved_message} = $locale->text('The parts have been removed.');
548 transfer_warehouse_selection();
550 $main::lxdebug->leave_sub();
553 sub disassemble_assembly {
554 $main::lxdebug->enter_sub();
556 $main::auth->assert('warehouse_management');
558 my $form = $main::form;
560 croak("No assembly ids") unless scalar @{ $form->{ids}} > 0;
562 # everything in one transaction
563 my $db = SL::DB::Inventory->new->db;
564 $db->with_transaction(sub {
566 foreach my $trans_id (@{ $::form->{ids}} ) {
567 SL::DB::Manager::Inventory->delete_all(where => [ trans_id => $trans_id ]);
568 flash_later('info', t8("Disassembly successful for trans_id #1", $trans_id));
572 }) || die t8('error while disassembling for trans_ids #1 : #2', $form->{ids}) . $db->error . "\n";
574 $main::lxdebug->leave_sub();
578 # --------------------------------------------------------------------
580 # --------------------------------------------------------------------
583 $main::lxdebug->enter_sub();
585 $main::auth->assert('warehouse_management');
587 my $form = $main::form;
588 my %myconfig = %main::myconfig;
589 my $locale = $main::locale;
591 $form->{title} = $locale->text('Report about warehouse transactions');
592 $form->get_lists('warehouses' => { 'key' => 'WAREHOUSES',
593 'bins' => 'BINS', });
595 show_no_warehouses_error() if (!scalar @{ $form->{WAREHOUSES} });
597 setup_wh_journal_action_bar();
600 print $form->parse_html_template("wh/journal_filter", { "UNITS" => AM->unit_select_data(AM->retrieve_units(\%myconfig, $form)) });
602 $main::lxdebug->leave_sub();
605 sub generate_journal {
606 $main::lxdebug->enter_sub();
608 $main::auth->assert('warehouse_management');
610 my $form = $main::form;
611 my %myconfig = %main::myconfig;
612 my $locale = $main::locale;
614 setup_wh_journal_list_all_action_bar();
615 $form->{title} = $locale->text("WHJournal");
616 $form->{sort} ||= 'date';
618 $form->{report_generator_output_format} = 'HTML' if !$form->{report_generator_output_format};
621 my @columns = qw(ids 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);
624 map { $filter{$_} = $form->{$_} if ($form->{$_}) } qw(warehouse_id bin_id classification_id partnumber description chargenumber bestbefore transtype_id transtype_ids comment projectnumber);
626 $filter{qty_op} = WH->convert_qty_op($form->{qty_op});
627 if ($filter{qty_op}) {
628 $form->isblank("qty", $locale->text('Quantity missing.'));
629 $form->isblank("qty_unit", $locale->text('Unit missing.'));
631 $filter{qty} = $form->{qty};
632 $filter{qty_unit} = $form->{qty_unit};
636 my $allrows = !!($form->{report_generator_output_format} ne 'HTML') ;
640 my $page = $::form->{page} || 1;
641 $pages->{per_page} = $::form->{per_page} || 15;
642 my $first_nr = ($page - 1) * $pages->{per_page};
643 my $last_nr = $first_nr + $pages->{per_page};
645 # no optimisation if qty op
646 if ( !$allrows && $form->{maxrows} && !$filter{qty_op}) {
647 $filter{limit} = $pages->{per_page};
648 $filter{offset} = ($page - 1) * $pages->{per_page};
650 $last_nr = $pages->{per_page};
653 my $old_l_trans_id = $form->{l_trans_id};
654 my @contents = WH->get_warehouse_journal(%filter);
655 $form->{l_trans_id} = $old_l_trans_id;
658 if (!$form->{maxrows}) {
659 $form->{maxrows} = scalar @contents ;
662 my $report = SL::ReportGenerator->new(\%myconfig, $form);
664 my @hidden_variables = map { "l_${_}" } @columns;
665 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);
666 push @hidden_variables, qw(classification_id);
669 'ids' => { raw_header_data => checkbox_tag("", id => "check_all", checkall => "[data-checkall=1]") },
670 'date' => { 'text' => $locale->text('Date'), },
671 'trans_id' => { 'text' => $locale->text('Trans Id'), },
672 'trans_type' => { 'text' => $locale->text('Trans Type'), },
673 'comment' => { 'text' => $locale->text('Comment'), },
674 'warehouse_from' => { 'text' => $locale->text('Warehouse From'), },
675 'warehouse_to' => { 'text' => $locale->text('Warehouse To'), },
676 'bin_from' => { 'text' => $locale->text('Bin From'), },
677 'bin_to' => { 'text' => $locale->text('Bin To'), },
678 'partnumber' => { 'text' => $locale->text('Part Number'), },
680 => { 'text' => $locale->text('Type'), },
681 'partdescription' => { 'text' => $locale->text('Part Description'), },
682 'chargenumber' => { 'text' => $locale->text('Charge Number'), },
683 'bestbefore' => { 'text' => $locale->text('Best Before'), },
684 'qty' => { 'text' => $locale->text('Qty'), },
685 'unit' => { 'text' => $locale->text('Part Unit'), },
686 'partunit' => { 'text' => $locale->text('Unit'), },
687 'employee' => { 'text' => $locale->text('Employee'), },
688 'projectnumber' => { 'text' => $locale->text('Project Number'), },
689 'oe_id' => { 'text' => $locale->text('Document'), },
692 if ($form->{transtype_ids} && 'ARRAY' eq ref $form->{transtype_ids}) {
693 for (my $i = 0; $i < scalar(@{ $form->{transtype_ids} }); $i++) {
694 delete $form->{transtype_ids}[$i] if $form->{transtype_ids}[$i] eq '';
696 $form->{transtype_ids} = join(",", @{ $form->{transtype_ids} });
699 my $href = build_std_url('action=generate_journal', grep { $form->{$_} } @hidden_variables);
700 $href .= "&maxrows=".$form->{maxrows};
702 map { $column_defs{$_}->{link} = $href ."&page=".$page. "&sort=${_}&order=" . Q($_ eq $form->{sort} ? 1 - $form->{order} : $form->{order}) } @columns;
704 my %column_alignment = map { $_ => 'right' } qw(qty);
706 map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
707 $column_defs{partunit}->{visible} = 1;
708 $column_defs{type_and_classific}->{visible} = 1;
709 $column_defs{type_and_classific}->{link} ='';
710 $column_defs{ids}->{visible} = 1;
712 $report->set_columns(%column_defs);
713 $report->set_column_order(@columns);
715 $report->set_export_options('generate_journal', @hidden_variables, qw(sort order));
717 $report->set_sort_indicator($form->{sort}, $form->{order});
719 $report->set_options('output_format' => 'HTML',
720 'title' => $form->{title},
721 'attachment_basename' => strftime($locale->text('warehouse_journal_list') . '_%Y%m%d', localtime time));
722 $report->set_options_from_form();
723 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
725 my $all_units = AM->retrieve_units(\%myconfig, $form);
727 my %doc_types = ( 'sales_quotation' => { script => 'oe', title => $locale->text('Sales quotation') },
728 'sales_order' => { script => 'oe', title => $locale->text('Sales Order') },
729 'request_quotation' => { script => 'oe', title => $locale->text('Request quotation') },
730 'purchase_order' => { script => 'oe', title => $locale->text('Purchase Order') },
731 'sales_delivery_order' => { script => 'do', title => $locale->text('Sales delivery order') },
732 'purchase_delivery_order' => { script => 'do', title => $locale->text('Purchase delivery order') },
733 'sales_invoice' => { script => 'is', title => $locale->text('Sales Invoice') },
734 'purchase_invoice' => { script => 'ir', title => $locale->text('Purchase Invoice') },
738 my $undo_date = DateTime->today->subtract(days => $::instance_conf->get_undo_transfer_interval);
739 foreach my $entry (@contents) {
740 $entry->{type_and_classific} = SL::Presenter::Part::type_abbreviation($entry->{part_type}) .
741 SL::Presenter::Part::classification_abbreviation($entry->{classification_id});
742 $entry->{qty} = $form->format_amount(\%myconfig, $entry->{qty});
743 $entry->{assembled} = $entry->{trans_type} eq 'assembled' ? 1 : '';
744 $entry->{trans_type} = $locale->text($entry->{trans_type});
747 foreach my $column (@columns) {
749 'data' => $entry->{$column},
750 'align' => $column_alignment{$column},
754 if ($entry->{assembled}) {
755 my $insertdate = DateTime->from_kivitendo($entry->{shippingdate});
756 if (ref $undo_date eq 'DateTime' && ref $insertdate eq 'DateTime' && $insertdate > $undo_date) {
757 $row->{ids}->{raw_data} = checkbox_tag("ids[]", value => $entry->{trans_id}, "data-checkall" => 1);
760 $row->{trans_type}->{raw_data} = $entry->{trans_type};
761 if ($form->{l_oe_id}) {
762 $row->{oe_id}->{data} = '';
763 my $info = $entry->{oe_id_info};
765 if ($info && $info->{id} && $info->{type} && $doc_types{$info->{type}}) {
766 $row->{oe_id} = { data => $doc_types{ $info->{type} }->{title} . ' ' . $info->{number},
767 link => build_std_url('script=' . $doc_types{ $info->{type} }->{script} . '.pl', 'action=edit', 'id=' . $info->{id}, 'type=' . $info->{type}) };
771 if ( $allrows || ($idx >= $first_nr && $idx < $last_nr )) {
772 $report->add_data($row);
778 $report->set_options(
779 raw_top_info_text => $form->parse_html_template('wh/report_top'),
780 raw_bottom_info_text => $form->parse_html_template('wh/report_bottom', { callback => $href }),
783 $pages->{max} = SL::DB::Helper::Paginated::ceil($form->{maxrows}, $pages->{per_page}) || 1;
784 $pages->{page} = $page < 1 ? 1: $page > $pages->{max} ? $pages->{max}: $page;
785 $pages->{common} = [ grep { $_->{visible} } @{ SL::DB::Helper::Paginated::make_common_pages($pages->{page}, $pages->{max}) } ];
787 $report->set_options('raw_bottom_info_text' => $form->parse_html_template('wh/report_bottom', { callback => $href }) . $form->parse_html_template('common/paginate',
788 { 'pages' => $pages , 'base_url' => $href.'&sort='.$form->{sort}.'&order='.$form->{order}}) );
790 $report->generate_with_headers();
792 $main::lxdebug->leave_sub();
795 # --------------------------------------------------------------------
797 # --------------------------------------------------------------------
800 $main::lxdebug->enter_sub();
802 $main::auth->assert('warehouse_contents | warehouse_management');
804 my $form = $main::form;
805 my %myconfig = %main::myconfig;
806 my $locale = $main::locale;
808 $form->get_lists('warehouses' => { 'key' => 'WAREHOUSES',
809 'bins' => 'BINS', });
811 show_no_warehouses_error() if (!scalar @{ $form->{WAREHOUSES} });
813 my $CVAR_CONFIGS = SL::DB::Manager::CustomVariableConfig->get_all_sorted(where => [ module => 'IC' ]);
814 my $INCLUDABLE_CVAR_CONFIGS = [ grep { $_->includeable } @{ $CVAR_CONFIGS } ];
816 $form->{title} = $locale->text("Report about warehouse contents");
818 setup_wh_report_action_bar();
821 print $form->parse_html_template("wh/report_filter",
822 { "WAREHOUSES" => $form->{WAREHOUSES},
823 "UNITS" => AM->unit_select_data(AM->retrieve_units(\%myconfig, $form)),
824 # "CVAR_CONFIGS" => $CVAR_CONFIGS, # nyi searchable cvars
825 "INCLUDABLE_CVAR_CONFIGS" => $INCLUDABLE_CVAR_CONFIGS,
828 $main::lxdebug->leave_sub();
831 sub generate_report {
832 $main::lxdebug->enter_sub();
834 $main::auth->assert('warehouse_contents | warehouse_management');
836 my $form = $main::form;
837 my %myconfig = %main::myconfig;
838 my $locale = $main::locale;
840 my $cvar_configs = CVar->get_configs('module' => 'IC');
842 $form->{title} = $locale->text("Report about warehouse contents");
843 $form->{sort} ||= 'partnumber';
844 my $sort_col = $form->{sort};
847 my @columns = qw(warehousedescription bindescription partnumber type_and_classific partdescription chargenumber bestbefore comment qty partunit list_price purchase_price stock_value);
850 map { $filter{$_} = $form->{$_} if ($form->{$_}) } qw(warehouse_id bin_id classification_id partnumber description chargenumber bestbefore date include_invalid_warehouses);
852 # show filter stuff also in report
854 my $currentdate = $form->current_date(\%myconfig);
855 push @options, $locale->text('Printdate') . " : ".$locale->date(\%myconfig, $currentdate, 1);
857 # dispatch all options
858 my $dispatch_options = {
859 warehouse_id => sub { push @options, $locale->text('Warehouse') . " : " .
860 SL::DB::Manager::Warehouse->find_by(id => $form->{warehouse_id})->description},
861 bin_id => sub { push @options, $locale->text('Bin') . " : " .
862 SL::DB::Manager::Bin->find_by(id => $form->{bin_id})->description},
863 partnumber => sub { push @options, $locale->text('Partnumber') . " : $form->{partnumber}"},
864 classification_id => sub { push @options, $locale->text('Parts Classification'). " : ".
865 SL::DB::Manager::PartClassification->get_first(where => [ id => $form->{classification_id} ] )->description; },
866 description => sub { push @options, $locale->text('Description') . " : $form->{description}"},
867 chargenumber => sub { push @options, $locale->text('Charge Number') . " : $form->{chargenumber}"},
868 bestbefore => sub { push @options, $locale->text('Best Before') . " : $form->{bestbefore}"},
869 include_invalid_warehouses => sub { push @options, $locale->text('Include invalid warehouses ')},
871 foreach (keys %filter) {
872 $dispatch_options->{$_}->() if $dispatch_options->{$_};
874 push @options, $locale->text('Stock Qty for Date') . " " . $locale->date(\%myconfig, $form->{date}?$form->{date}:$currentdate, 1);
876 # / end show filter stuff also in report
878 $filter{qty_op} = WH->convert_qty_op($form->{qty_op});
879 if ($filter{qty_op}) {
880 $form->isblank("qty", $locale->text('Quantity missing.'));
881 $form->isblank("qty_unit", $locale->text('Unit missing.'));
883 $filter{qty} = $form->{qty};
884 $filter{qty_unit} = $form->{qty_unit};
888 $form->{report_generator_output_format} = 'HTML' if !$form->{report_generator_output_format};
891 my $allrows = $form->{report_generator_output_format} eq 'HTML' ? $form->{allrows} : 1;
892 my $page = $::form->{page} || 1;
894 $pages->{per_page} = $::form->{per_page} || 20;
895 my $first_nr = ($page - 1) * $pages->{per_page};
896 my $last_nr = $first_nr + $pages->{per_page};
898 # no optimisation if qty op
899 if ( !$allrows && $form->{maxrows} && !$filter{qty_op}) {
900 $filter{limit} = $pages->{per_page};
901 $filter{offset} = ($page - 1) * $pages->{per_page};
903 $last_nr = $pages->{per_page};
906 my @contents = WH->get_warehouse_report(%filter);
909 if (!$form->{maxrows}) {
910 $form->{maxrows} = scalar @contents ;
913 $form->{subtotal} = '' if (!first { $_ eq $sort_col } qw(partnumber partdescription));
915 my $report = SL::ReportGenerator->new(\%myconfig, $form);
917 my @includeable_custom_variables = grep { $_->{includeable} } @{ $cvar_configs };
918 push @columns, map { "cvar_$_->{name}" } @includeable_custom_variables;
920 my @hidden_variables = map { "l_${_}" } @columns;
921 push @hidden_variables, qw(warehouse_id bin_id partnumber partstypes_id description chargenumber bestbefore qty_op qty qty_unit partunit l_warehousedescription l_bindescription);
922 push @hidden_variables, qw(include_empty_bins subtotal include_invalid_warehouses date);
923 push @hidden_variables, qw(classification_id stock_value_basis allrows);
926 'warehousedescription' => { 'text' => $locale->text('Warehouse'), },
927 'bindescription' => { 'text' => $locale->text('Bin'), },
928 'partnumber' => { 'text' => $locale->text('Part Number'), },
929 'type_and_classific' => { 'text' => $locale->text('Type'), },
930 'partdescription' => { 'text' => $locale->text('Part Description'), },
931 'chargenumber' => { 'text' => $locale->text('Charge Number'), },
932 'bestbefore' => { 'text' => $locale->text('Best Before'), },
933 'qty' => { 'text' => $locale->text('Qty'), },
934 'partunit' => { 'text' => $locale->text('Unit'), },
935 'stock_value' => { 'text' => $locale->text('Stock value'), },
936 'purchase_price' => { 'text' => $locale->text('Purchase price'), },
937 'list_price' => { 'text' => $locale->text('List Price'), },
940 my $href = build_std_url('action=generate_report', grep { $form->{$_} } @hidden_variables);
941 $href .= "&maxrows=".$form->{maxrows};
942 my %column_defs_cvars = map { +"cvar_$_->{name}" => { 'text' => $_->{description} } } @includeable_custom_variables;
943 %column_defs = (%column_defs, %column_defs_cvars);
945 map { $column_defs{$_}->{link} = $href . "&page=".$page."&sort=${_}&order=" . Q($_ eq $sort_col ? 1 - $form->{order} : $form->{order}) } @columns;
947 my %column_alignment = map { $_ => 'right' } qw(qty list_price purchase_price stock_value);
949 map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
951 $column_defs{partunit}->{visible} = 1;
952 $column_defs{type_and_classific}->{visible} = 1;
953 $column_defs{type_and_classific}->{link} ='';
955 $report->set_columns(%column_defs);
956 $report->set_column_order(@columns);
958 $report->set_export_options('generate_report', @hidden_variables, qw(sort order));
960 $report->set_sort_indicator($sort_col, $form->{order});
962 $report->set_options('top_info_text' => join("\n", @options),
963 'output_format' => 'HTML',
964 'title' => $form->{title},
965 'attachment_basename' => strftime($locale->text('warehouse_report_list') . '_%Y%m%d', localtime time));
966 $report->set_options_from_form();
967 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
968 CVar->add_custom_variables_to_report('module' => 'IC',
969 'trans_id_field' => 'parts_id',
970 'configs' => $cvar_configs,
971 'column_defs' => \%column_defs,
972 'data' => \@contents);
974 my $all_units = AM->retrieve_units(\%myconfig, $form);
977 my @subtotals_columns = qw(qty stock_value);
978 my %subtotals = map { $_ => 0 } @subtotals_columns;
980 my $total_stock_value = 0;
982 foreach my $entry (@contents) {
984 $entry->{type_and_classific} = SL::Presenter::Part::type_abbreviation($entry->{part_type}).
985 SL::Presenter::Part::classification_abbreviation($entry->{classification_id});
986 map { $subtotals{$_} += $entry->{$_} } @subtotals_columns;
987 $total_stock_value += $entry->{stock_value} * 1;
988 $entry->{qty} = $form->format_amount(\%myconfig, $entry->{qty});
989 # $entry->{qty} = $form->format_amount_units('amount' => $entry->{qty},
990 # 'part_unit' => $entry->{partunit},
991 # 'conv_units' => 'convertible');
992 $entry->{stock_value} = $form->format_amount(\%myconfig, $entry->{stock_value} * 1, 2);
993 $entry->{purchase_price} = $form->format_amount(\%myconfig, $entry->{purchase_price} * 1, 2);
994 $entry->{list_price} = $form->format_amount(\%myconfig, $entry->{list_price} * 1, 2);
996 my $row_set = [ { map { $_ => { 'data' => $entry->{$_}, 'align' => $column_alignment{$_} } } @columns } ];
998 if ( ($form->{subtotal} eq 'Y' && !$form->{report_generator_csv_options_for_import} )
999 && (($idx == (scalar @contents - 1))
1000 || ($entry->{$sort_col} ne $contents[$idx + 1]->{$sort_col}))) {
1002 my $row = { map { $_ => { 'data' => '', 'class' => 'listsubtotal', 'align' => $column_alignment{$_}, } } @columns };
1003 $row->{qty}->{data} = $form->format_amount(\%myconfig, $subtotals{qty});
1004 # $row->{qty}->{data} = $form->format_amount_units('amount' => $subtotals{qty} * 1,
1005 # 'part_unit' => $entry->{partunit},
1006 # 'conv_units' => 'convertible');
1007 $row->{stock_value}->{data} = $form->format_amount(\%myconfig, $subtotals{stock_value} * 1, 2);
1008 $row->{purchase_price}->{data} = $form->format_amount(\%myconfig, $subtotals{purchase_price} * 1, 2);
1009 $row->{list_price}->{data} = $form->format_amount(\%myconfig, $subtotals{list_price} * 1, 2);
1011 %subtotals = map { $_ => 0 } @subtotals_columns;
1013 push @{ $row_set }, $row;
1016 if ( $allrows || ($idx >= $first_nr && $idx < $last_nr )) {
1017 $report->add_data($row_set);
1022 if ( $column_defs{stock_value}->{visible} && !$form->{report_generator_csv_options_for_import} ) {
1023 $report->add_separator();
1025 my $row = { map { $_ => { 'data' => '', 'class' => 'listsubtotal', } } @columns };
1027 my $left_col = first { $column_defs{$_}->{visible} } @columns;
1029 $row->{$left_col}->{data} = $locale->text('Total stock value');
1030 $row->{stock_value}->{data} = $form->format_amount(\%myconfig, $total_stock_value, 2);
1031 $row->{stock_value}->{align} = 'right';
1033 $report->add_data($row);
1036 $pages->{max} = SL::DB::Helper::Paginated::ceil($form->{maxrows}, $pages->{per_page}) || 1;
1037 $pages->{page} = $page < 1 ? 1: $page > $pages->{max} ? $pages->{max}: $page;
1038 $pages->{common} = [ grep { $_->{visible} } @{ SL::DB::Helper::Paginated::make_common_pages($pages->{page}, $pages->{max}) } ];
1040 $report->set_options('raw_bottom_info_text' => $form->parse_html_template('common/paginate',
1041 {'pages' => $pages , 'base_url' => $href}) );
1044 $report->generate_with_headers();
1046 $main::lxdebug->leave_sub();
1049 # --------------------------------------------------------------------
1051 # --------------------------------------------------------------------
1053 sub show_no_warehouses_error {
1054 $main::lxdebug->enter_sub();
1056 my $form = $main::form;
1057 my %myconfig = %main::myconfig;
1058 my $locale = $main::locale;
1060 my $msg = $locale->text('No warehouse has been created yet or the quantity of the bins is not configured yet.') . ' ';
1062 if ($main::auth->check_right($::myconfig{login}, 'config')) {
1063 $msg .= $locale->text('You can create warehouses and bins via the menu "System -> Warehouses".');
1065 $msg .= $locale->text('Please ask your administrator to create warehouses and bins.');
1068 $form->show_generic_error($msg);
1070 $main::lxdebug->leave_sub();
1073 sub get_warehouse_idx {
1074 my ($warehouse_id) = @_;
1076 my $form = $main::form;
1078 for (my $i = 0; $i < scalar @{$form->{WAREHOUSES}}; $i++) {
1079 return $i if ($form->{WAREHOUSES}->[$i]->{id} == $warehouse_id);
1086 my ($warehouse_index, $bin_id) = @_;
1088 my $form = $main::form;
1090 my $warehouse = $form->{WAREHOUSES}->[$warehouse_index];
1092 return -1 if (!$warehouse);
1094 for (my $i = 0; $i < scalar @{ $warehouse->{BINS} }; $i++) {
1095 return $i if ($warehouse->{BINS}->[$i]->{id} == $bin_id);
1102 $main::lxdebug->enter_sub();
1105 my $form = $main::form;
1108 $form->{old_callback} = $form->escape($form->{callback}, 1);
1109 $form->{callback} = $form->escape("$form->{script}?action=$params{action}", 1);
1111 # save all form variables except action in a previousform variable
1112 my $previousform = join '&', map { my $value = $form->{$_}; $value =~ s/&/%26/; "$_=$value" } grep { !/action/ } keys %$form;
1115 # push @HIDDENS, { 'name' => 'previousform', 'value' => $form->escape($previousform, 1) };
1116 push @HIDDENS, map +{ 'name' => $_, 'value' => $form->{$_} }, qw(partnumber description unit vc sellprice ean);
1117 push @HIDDENS, { 'name' => 'taxaccount2', 'value' => $form->{taxaccounts} };
1118 push @HIDDENS, { 'name' => 'notes', 'value' => $form->{longdescription} };
1121 print $form->parse_html_template("generic/new_item", { HIDDENS => [ sort { $a->{name} cmp $b->{name} } @HIDDENS ] } );
1123 $main::lxdebug->leave_sub();
1127 my $form = $main::form;
1128 call_sub($form->{update_nextsub} || $form->{nextsub});
1132 my $form = $main::form;
1133 call_sub($form->{continue_nextsub} || $form->{nextsub});
1137 my $form = $main::form;
1138 call_sub($form->{stock_nextsub} || $form->{nextsub});
1141 sub setup_wh_transfer_warehouse_selection_action_bar {
1144 for my $bar ($::request->layout->get('actionbar')) {
1148 submit => [ '#form', { action => $action } ],
1149 accesskey => 'enter',
1155 sub setup_wh_transfer_warehouse_selection_assembly_action_bar {
1158 for my $bar ($::request->layout->get('actionbar')) {
1162 submit => [ '#form', { action => 'transfer_assembly_update_part' } ],
1163 accesskey => 'enter',
1167 submit => [ '#form', { action => 'create_assembly' } ],
1168 disabled => $::form->{parts_id} ? undef : $::locale->text('No assembly has been selected yet.'),
1174 sub setup_wh_transfer_parts_action_bar {
1177 for my $bar ($::request->layout->get('actionbar')) {
1181 submit => [ '#form', { action => 'transfer_parts' } ],
1182 accesskey => 'enter',
1186 call => [ 'kivi.history_back' ],
1192 sub setup_wh_removal_parts_selection_action_bar {
1195 for my $bar ($::request->layout->get('actionbar')) {
1199 submit => [ '#form', { action => 'remove_parts' } ],
1200 accesskey => 'enter',
1204 call => [ 'kivi.history_back' ],
1210 sub setup_wh_report_action_bar {
1213 for my $bar ($::request->layout->get('actionbar')) {
1217 submit => [ '#form', { action => 'generate_report' } ],
1218 accesskey => 'enter',
1224 sub setup_wh_journal_action_bar {
1227 for my $bar ($::request->layout->get('actionbar')) {
1231 submit => [ '#form', { action => 'generate_journal' } ],
1232 accesskey => 'enter',
1237 sub setup_wh_journal_list_all_action_bar {
1239 for my $bar ($::request->layout->get('actionbar')) {
1242 action => [ t8('Actions') ],
1244 t8('Disassemble Assembly'),
1245 submit => [ '#form', { action => 'disassemble_assembly' } ],
1246 checks => [ [ 'kivi.check_if_entries_selected', '[name="ids[]"]' ] ],
1260 bin/mozilla/wh.pl - Warehouse frontend.
1268 call new item dialogue from warehouse masks.
1271 action => name of sub to be called when new item is done