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 sub create_assembly {
375 $main::lxdebug->enter_sub();
377 my $form = $main::form;
378 my %myconfig = %main::myconfig;
379 my $locale = $main::locale;
381 $form->{qty} = $form->parse_amount(\%myconfig, $form->{qty});
382 if ($form->{qty} <= 0) {
383 $form->show_generic_error($locale->text('Invalid quantity.'));
385 if (!$form->{warehouse_id} || !$form->{bin_id}) {
386 $form->error($locale->text('The warehouse or the bin is missing.'));
388 # need part and bin object
389 my ($bin, $assembly);
390 $assembly = SL::DB::Manager::Part->find_by(id => $form->{parts_id}, part_type => 'assembly');
391 $form->show_generic_error($locale->text('Invalid assembly')) unless ref $assembly eq 'SL::DB::Part';
393 $bin = SL::DB::Manager::Bin->find_by(id => $form->{bin_id});
394 $form->show_generic_error($locale->text('Invalid bin')) unless ref $bin eq 'SL::DB::Bin';
396 if (!$::instance_conf->get_show_bestbefore) {
397 $form->{bestbefore} = '';
401 part => $assembly, # target assembly
402 qty => $form->{qty}, # qty
404 bin => $bin, # needed unless a global standard target is configured
405 chargenumber => $form->{chargenumber}, # optional
406 bestbefore => $form->{bestbefore},
407 comment => $form->{comment}, # optional
410 delete @{$form}{qw(parts_id partnumber description qty unit chargenumber bestbefore comment)};
412 $form->{saved_message} = $locale->text('The assembly has been created.');
413 $form->{trans_type} = 'assembly';
415 transfer_warehouse_selection();
417 $main::lxdebug->leave_sub();
420 # --------------------------------------------------------------------
422 # --------------------------------------------------------------------
424 sub removal_parts_selection {
425 $main::lxdebug->enter_sub();
427 $main::auth->assert('warehouse_management');
429 my $form = $main::form;
430 my %myconfig = %main::myconfig;
431 my $locale = $main::locale;
433 transfer_or_removal_prepare_contents('direction' => 'out');
435 setup_wh_removal_parts_selection_action_bar();
437 $form->{title} = $locale->text('Removal');
439 print $form->parse_html_template("wh/removal_parts_selection");
441 $main::lxdebug->leave_sub();
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 $form->get_lists('warehouses' => { 'key' => 'WAREHOUSES',
454 'bins' => 'BINS', });
456 my $warehouse_idx = get_warehouse_idx($form->{warehouse_id});
457 $form->show_generic_error($locale->text("The selected warehouse does not exist.")) if (-1 == $warehouse_idx);
459 my $warehouse = $form->{WAREHOUSES}->[$warehouse_idx];
461 $form->show_generic_error($locale->text("The warehouse does not contain any bins.")) if (0 == scalar @{ $warehouse->{BINS} });
463 map { $form->{"l_$_"} = 'Y' } qw(parts_id qty warehouseid binid partnumber partdescription bindescription chargenumber bestbefore partunit);
465 $form->{sort} = 'bindescription';
466 my @contents = WH->get_warehouse_report("warehouse_id" => $form->{warehouse_id});
467 my $all_units = AM->retrieve_units(\%myconfig, $form);
471 foreach my $row (1 .. $form->{rowcount}) {
472 $form->{"qty_$row"} =~ s/^\s*//;
473 $form->{"qty_$row"} =~ s/\s*$//;
474 next if (!$form->{"qty_$row"});
476 my $bin_idx = get_bin_idx($warehouse_idx, $form->{"src_bin_id_$row"});
477 $form->show_generic_error($locale->text("The selected bin does not exist.")) if (-1 == $bin_idx);
478 my $bin = $warehouse->{BINS}->[$bin_idx];
480 my $orig_qty = $form->{"qty_$row"} . " " . $form->{"unit_$row"};
483 'src_warehouse_id' => $form->{warehouse_id},
484 'transfer_type_id' => $form->{transfer_type_id},
487 map { $transfer->{$_} = $form->{"${_}_${row}"} } qw(src_bin_id chargenumber bestbefore parts_id qty);
491 foreach (@contents) {
492 if (($_->{binid} == $transfer->{src_bin_id}) && ($_->{parts_id} == $transfer->{parts_id}) && ($_->{chargenumber} eq $transfer->{chargenumber}) && ($_->{bestbefore} eq $transfer->{bestbefore})) {
499 $form->error($locale->text("There is not enough left of '#1' in bin '#2' for the removal of #3.",
500 $form->{"partdescription_$row"}, $bin->{description}, $orig_qty));
503 $transfer->{qty} = $form->parse_amount(\%myconfig, $transfer->{qty}) * $all_units->{$form->{"unit_$row"}}->{factor};
504 $transfer->{qty} /= $all_units->{$entry->{partunit}}->{factor} || 1;
506 if (($entry->{qty} < $transfer->{qty}) || (0 >= $transfer->{qty})) {
507 $form->error($locale->text("There is not enough left of '#1' in bin '#2' for the removal of #3.",
508 $form->{"partdescription_$row"}, $bin->{description}, $orig_qty));
511 $transfer->{comment} = $form->{comment};
513 push @transfers, $transfer;
515 $entry->{qty} -= $transfer->{qty};
518 if (!scalar @transfers) {
519 $form->show_generic_information($locale->text('Nothing has been selected for removal.'));
520 $::dispatcher->end_request;
523 WH->transfer(@transfers);
525 $form->{trans_type} = 'removal';
526 $form->{saved_message} = $locale->text('The parts have been removed.');
528 transfer_warehouse_selection();
530 $main::lxdebug->leave_sub();
533 sub disassemble_assembly {
534 $main::lxdebug->enter_sub();
536 $main::auth->assert('warehouse_management');
538 my $form = $main::form;
540 croak("No assembly ids") unless scalar @{ $form->{ids}} > 0;
542 # everything in one transaction
543 my $db = SL::DB::Inventory->new->db;
544 $db->with_transaction(sub {
546 foreach my $trans_id (@{ $::form->{ids}} ) {
547 SL::DB::Manager::Inventory->delete_all(where => [ trans_id => $trans_id ]);
548 flash_later('info', t8("Disassembly successful for trans_id #1", $trans_id));
552 }) || die t8('error while disassembling for trans_ids #1 : #2', $form->{ids}) . $db->error . "\n";
554 $main::lxdebug->leave_sub();
558 # --------------------------------------------------------------------
560 # --------------------------------------------------------------------
563 $main::lxdebug->enter_sub();
565 $main::auth->assert('warehouse_management');
567 my $form = $main::form;
568 my %myconfig = %main::myconfig;
569 my $locale = $main::locale;
571 $form->{title} = $locale->text('Report about warehouse transactions');
572 $form->get_lists('warehouses' => { 'key' => 'WAREHOUSES',
573 'bins' => 'BINS', });
575 show_no_warehouses_error() if (!scalar @{ $form->{WAREHOUSES} });
577 my $cvar_configs = CVar->get_configs('module' => 'IC');
578 ($form->{CUSTOM_VARIABLES_FILTER_CODE},
579 $form->{CUSTOM_VARIABLES_INCLUSION_CODE}) = CVar->render_search_options('variables' => $cvar_configs,
580 'include_prefix' => 'l_',
581 'include_value' => 'Y');
583 setup_wh_journal_action_bar();
586 print $form->parse_html_template("wh/journal_filter", { "UNITS" => AM->unit_select_data(AM->retrieve_units(\%myconfig, $form)) });
588 $main::lxdebug->leave_sub();
591 sub generate_journal {
592 $main::lxdebug->enter_sub();
594 $main::auth->assert('warehouse_management');
596 my $form = $main::form;
597 my %myconfig = %main::myconfig;
598 my $locale = $main::locale;
600 setup_wh_journal_list_all_action_bar();
601 $form->{title} = $locale->text("WHJournal");
602 $form->{sort} ||= 'date';
604 $form->{report_generator_output_format} = 'HTML' if !$form->{report_generator_output_format};
607 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);
610 map { $filter{$_} = $form->{$_} if ($form->{$_}) } qw(warehouse_id bin_id classification_id partnumber description chargenumber bestbefore transtype_id transtype_ids comment projectnumber);
612 $filter{qty_op} = WH->convert_qty_op($form->{qty_op});
613 if ($filter{qty_op}) {
614 $form->isblank("qty", $locale->text('Quantity missing.'));
615 $form->isblank("qty_unit", $locale->text('Unit missing.'));
617 $filter{qty} = $form->{qty};
618 $filter{qty_unit} = $form->{qty_unit};
622 my $allrows = !!($form->{report_generator_output_format} ne 'HTML') ;
626 my $page = $::form->{page} || 1;
627 $pages->{per_page} = $::form->{per_page} || 15;
628 my $first_nr = ($page - 1) * $pages->{per_page};
629 my $last_nr = $first_nr + $pages->{per_page};
631 # no optimisation if qty op
632 if ( !$allrows && $form->{maxrows} && !$filter{qty_op}) {
633 $filter{limit} = $pages->{per_page};
634 $filter{offset} = ($page - 1) * $pages->{per_page};
636 $last_nr = $pages->{per_page};
639 my $old_l_trans_id = $form->{l_trans_id};
640 my @contents = WH->get_warehouse_journal(%filter);
641 $form->{l_trans_id} = $old_l_trans_id;
644 if (!$form->{maxrows}) {
645 $form->{maxrows} = scalar @contents ;
648 my $report = SL::ReportGenerator->new(\%myconfig, $form);
650 my $cvar_configs = CVar->get_configs('module' => 'IC');
651 my @includeable_custom_variables = grep { $_->{includeable} } @{ $cvar_configs };
652 my @searchable_custom_variables = grep { $_->{searchable} } @{ $cvar_configs };
653 push @columns, map { "cvar_$_->{name}" } @includeable_custom_variables;
655 my @hidden_variables = map { "l_${_}" } @columns;
656 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);
657 push @hidden_variables, qw(classification_id);
658 push @hidden_variables, map({'cvar_'. $_->{name}} @searchable_custom_variables);
659 push @hidden_variables, map({'cvar_'. $_->{name} .'_from'} grep({$_->{type} eq 'date'} @searchable_custom_variables));
660 push @hidden_variables, map({'cvar_'. $_->{name} .'_to'} grep({$_->{type} eq 'date'} @searchable_custom_variables));
661 push @hidden_variables, map({'cvar_'. $_->{name} .'_qtyop'} grep({$_->{type} eq 'number'} @searchable_custom_variables));
664 'ids' => { raw_header_data => checkbox_tag("", id => "check_all", checkall => "[data-checkall=1]") },
665 'date' => { 'text' => $locale->text('Date'), },
666 'trans_id' => { 'text' => $locale->text('Trans Id'), },
667 'trans_type' => { 'text' => $locale->text('Trans Type'), },
668 'comment' => { 'text' => $locale->text('Comment'), },
669 'warehouse_from' => { 'text' => $locale->text('Warehouse From'), },
670 'warehouse_to' => { 'text' => $locale->text('Warehouse To'), },
671 'bin_from' => { 'text' => $locale->text('Bin From'), },
672 'bin_to' => { 'text' => $locale->text('Bin To'), },
673 'partnumber' => { 'text' => $locale->text('Part Number'), },
675 => { 'text' => $locale->text('Type'), },
676 'partdescription' => { 'text' => $locale->text('Part Description'), },
677 'chargenumber' => { 'text' => $locale->text('Charge Number'), },
678 'bestbefore' => { 'text' => $locale->text('Best Before'), },
679 'qty' => { 'text' => $locale->text('Qty'), },
680 'unit' => { 'text' => $locale->text('Part Unit'), },
681 'partunit' => { 'text' => $locale->text('Unit'), },
682 'employee' => { 'text' => $locale->text('Employee'), },
683 'projectnumber' => { 'text' => $locale->text('Project Number'), },
684 'oe_id' => { 'text' => $locale->text('Document'), },
687 my %column_defs_cvars = map { +"cvar_$_->{name}" => { 'text' => $_->{description} } } @includeable_custom_variables;
688 %column_defs = (%column_defs, %column_defs_cvars);
690 if ($form->{transtype_ids} && 'ARRAY' eq ref $form->{transtype_ids}) {
691 for (my $i = 0; $i < scalar(@{ $form->{transtype_ids} }); $i++) {
692 delete $form->{transtype_ids}[$i] if $form->{transtype_ids}[$i] eq '';
694 $form->{transtype_ids} = join(",", @{ $form->{transtype_ids} });
697 my $href = build_std_url('action=generate_journal', grep { $form->{$_} } @hidden_variables);
698 $href .= "&maxrows=".$form->{maxrows};
700 map { $column_defs{$_}->{link} = $href ."&page=".$page. "&sort=${_}&order=" . Q($_ eq $form->{sort} ? 1 - $form->{order} : $form->{order}) } grep {!/^cvar/} @columns;
702 my %column_alignment = map { $_ => 'right' } qw(qty);
704 map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
705 $column_defs{partunit}->{visible} = 1;
706 $column_defs{type_and_classific}->{visible} = 1;
707 $column_defs{type_and_classific}->{link} ='';
708 $column_defs{ids}->{visible} = 1;
710 $report->set_columns(%column_defs);
711 $report->set_column_order(@columns);
713 $report->set_export_options('generate_journal', @hidden_variables, qw(sort order));
715 $report->set_sort_indicator($form->{sort}, $form->{order});
717 $report->set_options('output_format' => 'HTML',
718 'title' => $form->{title},
719 'attachment_basename' => strftime($locale->text('warehouse_journal_list') . '_%Y%m%d', localtime time));
720 $report->set_options_from_form();
721 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
723 my $all_units = AM->retrieve_units(\%myconfig, $form);
725 CVar->add_custom_variables_to_report('module' => 'IC',
726 'trans_id_field' => 'parts_id',
727 'configs' => $cvar_configs,
728 'column_defs' => \%column_defs,
729 'data' => \@contents);
732 my %doc_types = ( 'sales_quotation' => { script => 'oe', title => $locale->text('Sales quotation') },
733 'sales_order' => { script => 'oe', title => $locale->text('Sales Order') },
734 'request_quotation' => { script => 'oe', title => $locale->text('Request quotation') },
735 'purchase_order' => { script => 'oe', title => $locale->text('Purchase Order') },
736 'sales_delivery_order' => { script => 'do', title => $locale->text('Sales delivery order') },
737 'purchase_delivery_order' => { script => 'do', title => $locale->text('Purchase delivery order') },
738 'sales_invoice' => { script => 'is', title => $locale->text('Sales Invoice') },
739 'purchase_invoice' => { script => 'ir', title => $locale->text('Purchase Invoice') },
743 my $undo_date = DateTime->today->subtract(days => $::instance_conf->get_undo_transfer_interval);
744 foreach my $entry (@contents) {
745 $entry->{type_and_classific} = SL::Presenter::Part::type_abbreviation($entry->{part_type}) .
746 SL::Presenter::Part::classification_abbreviation($entry->{classification_id});
747 $entry->{qty} = $form->format_amount(\%myconfig, $entry->{qty});
748 $entry->{assembled} = $entry->{trans_type} eq 'assembled' ? 1 : '';
749 $entry->{trans_type} = $locale->text($entry->{trans_type});
752 foreach my $column (@columns) {
754 'data' => $entry->{$column},
755 'align' => $column_alignment{$column},
759 if ($entry->{assembled}) {
760 my $insertdate = DateTime->from_kivitendo($entry->{shippingdate});
761 if (ref $undo_date eq 'DateTime' && ref $insertdate eq 'DateTime' && $insertdate > $undo_date) {
762 $row->{ids}->{raw_data} = checkbox_tag("ids[]", value => $entry->{trans_id}, "data-checkall" => 1);
765 $row->{trans_type}->{raw_data} = $entry->{trans_type};
766 if ($form->{l_oe_id}) {
767 $row->{oe_id}->{data} = '';
768 my $info = $entry->{oe_id_info};
770 if ($info && $info->{id} && $info->{type} && $doc_types{$info->{type}}) {
771 $row->{oe_id} = { data => $doc_types{ $info->{type} }->{title} . ' ' . $info->{number},
772 link => build_std_url('script=' . $doc_types{ $info->{type} }->{script} . '.pl', 'action=edit', 'id=' . $info->{id}, 'type=' . $info->{type}) };
776 if ( $allrows || ($idx >= $first_nr && $idx < $last_nr )) {
777 $report->add_data($row);
783 $report->set_options(
784 raw_top_info_text => $form->parse_html_template('wh/report_top'),
785 raw_bottom_info_text => $form->parse_html_template('wh/report_bottom', { callback => $href }),
788 $pages->{max} = SL::DB::Helper::Paginated::ceil($form->{maxrows}, $pages->{per_page}) || 1;
789 $pages->{page} = $page < 1 ? 1: $page > $pages->{max} ? $pages->{max}: $page;
790 $pages->{common} = [ grep { $_->{visible} } @{ SL::DB::Helper::Paginated::make_common_pages($pages->{page}, $pages->{max}) } ];
792 $report->set_options('raw_bottom_info_text' => $form->parse_html_template('wh/report_bottom', { callback => $href }) . $form->parse_html_template('common/paginate',
793 { 'pages' => $pages , 'base_url' => $href.'&sort='.$form->{sort}.'&order='.$form->{order}}) );
795 $report->generate_with_headers();
797 $main::lxdebug->leave_sub();
800 # --------------------------------------------------------------------
802 # --------------------------------------------------------------------
805 $main::lxdebug->enter_sub();
807 $main::auth->assert('warehouse_contents | warehouse_management');
809 my $form = $main::form;
810 my %myconfig = %main::myconfig;
811 my $locale = $main::locale;
813 $form->get_lists('warehouses' => { 'key' => 'WAREHOUSES',
815 'partsgroup' => 'PARTSGROUPS');
817 show_no_warehouses_error() if (!scalar @{ $form->{WAREHOUSES} });
819 my $cvar_configs = CVar->get_configs('module' => 'IC');
820 ($form->{CUSTOM_VARIABLES_FILTER_CODE},
821 $form->{CUSTOM_VARIABLES_INCLUSION_CODE}) = CVar->render_search_options('variables' => $cvar_configs,
822 'include_prefix' => 'l_',
823 'include_value' => 'Y');
825 $form->{title} = $locale->text("Report about warehouse contents");
827 setup_wh_report_action_bar();
830 print $form->parse_html_template("wh/report_filter",
831 { "WAREHOUSES" => $form->{WAREHOUSES},
832 "PARTSGROUPS" => $form->{PARTSGROUPS},
833 "UNITS" => AM->unit_select_data(AM->retrieve_units(\%myconfig, $form)),
836 $main::lxdebug->leave_sub();
839 sub generate_report {
840 $main::lxdebug->enter_sub();
842 $main::auth->assert('warehouse_contents | warehouse_management');
844 my $form = $main::form;
845 my %myconfig = %main::myconfig;
846 my $locale = $main::locale;
848 $form->{title} = $locale->text("Report about warehouse contents");
849 $form->{sort} ||= 'partnumber';
850 my $sort_col = $form->{sort};
853 my @columns = qw(warehousedescription bindescription partnumber type_and_classific partdescription chargenumber bestbefore comment qty partunit list_price purchase_price stock_value);
856 map { $filter{$_} = $form->{$_} if ($form->{$_}) } qw(warehouse_id bin_id classification_id partnumber description partsgroup_id chargenumber bestbefore date include_invalid_warehouses);
858 # show filter stuff also in report
860 my $currentdate = $form->current_date(\%myconfig);
861 push @options, $locale->text('Printdate') . " : ".$locale->date(\%myconfig, $currentdate, 1);
863 # dispatch all options
864 my $dispatch_options = {
865 warehouse_id => sub { push @options, $locale->text('Warehouse') . " : " .
866 SL::DB::Manager::Warehouse->find_by(id => $form->{warehouse_id})->description},
867 bin_id => sub { push @options, $locale->text('Bin') . " : " .
868 SL::DB::Manager::Bin->find_by(id => $form->{bin_id})->description},
869 partnumber => sub { push @options, $locale->text('Partnumber') . " : $form->{partnumber}"},
870 classification_id => sub { push @options, $locale->text('Parts Classification'). " : ".
871 SL::DB::Manager::PartClassification->get_first(where => [ id => $form->{classification_id} ] )->description; },
872 description => sub { push @options, $locale->text('Description') . " : $form->{description}"},
873 partsgroup_id => sub { push @options, $locale->text('Partsgroup') . " : " .
874 SL::DB::PartsGroup->new(id => $form->{partsgroup_id})->load->partsgroup},
875 chargenumber => sub { push @options, $locale->text('Charge Number') . " : $form->{chargenumber}"},
876 bestbefore => sub { push @options, $locale->text('Best Before') . " : $form->{bestbefore}"},
877 include_invalid_warehouses => sub { push @options, $locale->text('Include invalid warehouses ')},
879 foreach (keys %filter) {
880 $dispatch_options->{$_}->() if $dispatch_options->{$_};
882 push @options, $locale->text('Stock Qty for Date') . " " . $locale->date(\%myconfig, $form->{date}?$form->{date}:$currentdate, 1);
884 # / end show filter stuff also in report
886 $filter{qty_op} = WH->convert_qty_op($form->{qty_op});
887 if ($filter{qty_op}) {
888 $form->isblank("qty", $locale->text('Quantity missing.'));
889 $form->isblank("qty_unit", $locale->text('Unit missing.'));
891 $filter{qty} = $form->{qty};
892 $filter{qty_unit} = $form->{qty_unit};
896 $form->{report_generator_output_format} = 'HTML' if !$form->{report_generator_output_format};
899 my $allrows = $form->{report_generator_output_format} eq 'HTML' ? $form->{allrows} : 1;
900 my $page = $::form->{page} || 1;
902 $pages->{per_page} = $::form->{per_page} || 20;
903 my $first_nr = ($page - 1) * $pages->{per_page};
904 my $last_nr = $first_nr + $pages->{per_page};
906 # no optimisation if qty op
907 if ( !$allrows && $form->{maxrows} && !$filter{qty_op}) {
908 $filter{limit} = $pages->{per_page};
909 $filter{offset} = ($page - 1) * $pages->{per_page};
911 $last_nr = $pages->{per_page};
914 my @contents = WH->get_warehouse_report(%filter);
917 if (!$form->{maxrows}) {
918 $form->{maxrows} = scalar @contents ;
921 $form->{subtotal} = '' if (!first { $_ eq $sort_col } qw(partnumber partdescription));
923 my $report = SL::ReportGenerator->new(\%myconfig, $form);
925 my $cvar_configs = CVar->get_configs('module' => 'IC');
926 my @includeable_custom_variables = grep { $_->{includeable} } @{ $cvar_configs };
927 my @searchable_custom_variables = grep { $_->{searchable} } @{ $cvar_configs };
928 push @columns, map { "cvar_$_->{name}" } @includeable_custom_variables;
930 my @hidden_variables = map { "l_${_}" } @columns;
931 push @hidden_variables, qw(warehouse_id bin_id partnumber partstypes_id description partsgroup_id chargenumber bestbefore qty_op qty qty_unit partunit l_warehousedescription l_bindescription);
932 push @hidden_variables, qw(include_empty_bins subtotal include_invalid_warehouses date);
933 push @hidden_variables, qw(classification_id stock_value_basis allrows);
934 push @hidden_variables, map({'cvar_'. $_->{name}} @searchable_custom_variables);
935 push @hidden_variables, map({'cvar_'. $_->{name} .'_from'} grep({$_->{type} eq 'date'} @searchable_custom_variables));
936 push @hidden_variables, map({'cvar_'. $_->{name} .'_to'} grep({$_->{type} eq 'date'} @searchable_custom_variables));
937 push @hidden_variables, map({'cvar_'. $_->{name} .'_qtyop'} grep({$_->{type} eq 'number'} @searchable_custom_variables));
940 'warehousedescription' => { 'text' => $locale->text('Warehouse'), },
941 'bindescription' => { 'text' => $locale->text('Bin'), },
942 'partnumber' => { 'text' => $locale->text('Part Number'), },
943 'type_and_classific' => { 'text' => $locale->text('Type'), },
944 'partdescription' => { 'text' => $locale->text('Part Description'), },
945 'chargenumber' => { 'text' => $locale->text('Charge Number'), },
946 'bestbefore' => { 'text' => $locale->text('Best Before'), },
947 'qty' => { 'text' => $locale->text('Qty'), },
948 'partunit' => { 'text' => $locale->text('Unit'), },
949 'stock_value' => { 'text' => $locale->text('Stock value'), },
950 'purchase_price' => { 'text' => $locale->text('Purchase price'), },
951 'list_price' => { 'text' => $locale->text('List Price'), },
954 my %column_defs_cvars = map { +"cvar_$_->{name}" => { 'text' => $_->{description} } } @includeable_custom_variables;
955 %column_defs = (%column_defs, %column_defs_cvars);
957 my $href = build_std_url('action=generate_report', grep { $form->{$_} } @hidden_variables);
958 $href .= "&maxrows=".$form->{maxrows};
960 map { $column_defs{$_}->{link} = $href . "&page=".$page."&sort=${_}&order=" . Q($_ eq $sort_col ? 1 - $form->{order} : $form->{order}) } grep {!/^cvar_/} @columns;
962 my %column_alignment = map { $_ => 'right' } qw(qty list_price purchase_price stock_value);
964 map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
966 $column_defs{partunit}->{visible} = 1;
967 $column_defs{type_and_classific}->{visible} = 1;
968 $column_defs{type_and_classific}->{link} ='';
970 $report->set_columns(%column_defs);
971 $report->set_column_order(@columns);
973 $report->set_export_options('generate_report', @hidden_variables, qw(sort order));
975 $report->set_sort_indicator($sort_col, $form->{order});
977 $report->set_options('top_info_text' => join("\n", @options),
978 'output_format' => 'HTML',
979 'title' => $form->{title},
980 'attachment_basename' => strftime($locale->text('warehouse_report_list') . '_%Y%m%d', localtime time));
981 $report->set_options_from_form();
982 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
983 CVar->add_custom_variables_to_report('module' => 'IC',
984 'trans_id_field' => 'parts_id',
985 'configs' => $cvar_configs,
986 'column_defs' => \%column_defs,
987 'data' => \@contents);
989 my $all_units = AM->retrieve_units(\%myconfig, $form);
992 my @subtotals_columns = qw(qty stock_value);
993 my %subtotals = map { $_ => 0 } @subtotals_columns;
995 my $total_stock_value = 0;
997 foreach my $entry (@contents) {
999 $entry->{type_and_classific} = SL::Presenter::Part::type_abbreviation($entry->{part_type}).
1000 SL::Presenter::Part::classification_abbreviation($entry->{classification_id});
1001 map { $subtotals{$_} += $entry->{$_} } @subtotals_columns;
1002 $total_stock_value += $entry->{stock_value} * 1;
1003 $entry->{qty} = $form->format_amount(\%myconfig, $entry->{qty});
1004 # $entry->{qty} = $form->format_amount_units('amount' => $entry->{qty},
1005 # 'part_unit' => $entry->{partunit},
1006 # 'conv_units' => 'convertible');
1007 $entry->{stock_value} = $form->format_amount(\%myconfig, $entry->{stock_value} * 1, 2);
1008 $entry->{purchase_price} = $form->format_amount(\%myconfig, $entry->{purchase_price} * 1, 2);
1009 $entry->{list_price} = $form->format_amount(\%myconfig, $entry->{list_price} * 1, 2);
1011 my $row_set = [ { map { $_ => { 'data' => $entry->{$_}, 'align' => $column_alignment{$_} } } @columns } ];
1013 if ( ($form->{subtotal} eq 'Y' && !$form->{report_generator_csv_options_for_import} )
1014 && (($idx == (scalar @contents - 1))
1015 || ($entry->{$sort_col} ne $contents[$idx + 1]->{$sort_col}))) {
1017 my $row = { map { $_ => { 'data' => '', 'class' => 'listsubtotal', 'align' => $column_alignment{$_}, } } @columns };
1018 $row->{qty}->{data} = $form->format_amount(\%myconfig, $subtotals{qty});
1019 # $row->{qty}->{data} = $form->format_amount_units('amount' => $subtotals{qty} * 1,
1020 # 'part_unit' => $entry->{partunit},
1021 # 'conv_units' => 'convertible');
1022 $row->{stock_value}->{data} = $form->format_amount(\%myconfig, $subtotals{stock_value} * 1, 2);
1023 $row->{purchase_price}->{data} = $form->format_amount(\%myconfig, $subtotals{purchase_price} * 1, 2);
1024 $row->{list_price}->{data} = $form->format_amount(\%myconfig, $subtotals{list_price} * 1, 2);
1026 %subtotals = map { $_ => 0 } @subtotals_columns;
1028 push @{ $row_set }, $row;
1031 if ( $allrows || ($idx >= $first_nr && $idx < $last_nr )) {
1032 $report->add_data($row_set);
1037 if ( $column_defs{stock_value}->{visible} && !$form->{report_generator_csv_options_for_import} ) {
1038 $report->add_separator();
1040 my $row = { map { $_ => { 'data' => '', 'class' => 'listsubtotal', } } @columns };
1042 my $left_col = first { $column_defs{$_}->{visible} } @columns;
1044 $row->{$left_col}->{data} = $locale->text('Total stock value');
1045 $row->{stock_value}->{data} = $form->format_amount(\%myconfig, $total_stock_value, 2);
1046 $row->{stock_value}->{align} = 'right';
1048 $report->add_data($row);
1051 $pages->{max} = SL::DB::Helper::Paginated::ceil($form->{maxrows}, $pages->{per_page}) || 1;
1052 $pages->{page} = $page < 1 ? 1: $page > $pages->{max} ? $pages->{max}: $page;
1053 $pages->{common} = [ grep { $_->{visible} } @{ SL::DB::Helper::Paginated::make_common_pages($pages->{page}, $pages->{max}) } ];
1055 $report->set_options('raw_bottom_info_text' => $form->parse_html_template('common/paginate',
1056 {'pages' => $pages , 'base_url' => $href}) );
1059 $report->generate_with_headers();
1061 $main::lxdebug->leave_sub();
1064 # --------------------------------------------------------------------
1066 # --------------------------------------------------------------------
1068 sub show_no_warehouses_error {
1069 $main::lxdebug->enter_sub();
1071 my $form = $main::form;
1072 my %myconfig = %main::myconfig;
1073 my $locale = $main::locale;
1075 my $msg = $locale->text('No warehouse has been created yet or the quantity of the bins is not configured yet.') . ' ';
1077 if ($main::auth->check_right($::myconfig{login}, 'config')) {
1078 $msg .= $locale->text('You can create warehouses and bins via the menu "System -> Warehouses".');
1080 $msg .= $locale->text('Please ask your administrator to create warehouses and bins.');
1083 $form->show_generic_error($msg);
1085 $main::lxdebug->leave_sub();
1088 sub get_warehouse_idx {
1089 my ($warehouse_id) = @_;
1091 my $form = $main::form;
1093 for (my $i = 0; $i < scalar @{$form->{WAREHOUSES}}; $i++) {
1094 return $i if ($form->{WAREHOUSES}->[$i]->{id} == $warehouse_id);
1101 my ($warehouse_index, $bin_id) = @_;
1103 my $form = $main::form;
1105 my $warehouse = $form->{WAREHOUSES}->[$warehouse_index];
1107 return -1 if (!$warehouse);
1109 for (my $i = 0; $i < scalar @{ $warehouse->{BINS} }; $i++) {
1110 return $i if ($warehouse->{BINS}->[$i]->{id} == $bin_id);
1117 $main::lxdebug->enter_sub();
1120 my $form = $main::form;
1123 $form->{old_callback} = $form->escape($form->{callback}, 1);
1124 $form->{callback} = $form->escape("$form->{script}?action=$params{action}", 1);
1126 # save all form variables except action in a previousform variable
1127 my $previousform = join '&', map { my $value = $form->{$_}; $value =~ s/&/%26/; "$_=$value" } grep { !/action/ } keys %$form;
1130 # push @HIDDENS, { 'name' => 'previousform', 'value' => $form->escape($previousform, 1) };
1131 push @HIDDENS, map +{ 'name' => $_, 'value' => $form->{$_} }, qw(partnumber description unit vc sellprice ean);
1132 push @HIDDENS, { 'name' => 'taxaccount2', 'value' => $form->{taxaccounts} };
1133 push @HIDDENS, { 'name' => 'notes', 'value' => $form->{longdescription} };
1136 print $form->parse_html_template("generic/new_item", { HIDDENS => [ sort { $a->{name} cmp $b->{name} } @HIDDENS ] } );
1138 $main::lxdebug->leave_sub();
1142 my $form = $main::form;
1143 call_sub($form->{update_nextsub} || $form->{nextsub});
1147 my $form = $main::form;
1148 call_sub($form->{continue_nextsub} || $form->{nextsub});
1152 my $form = $main::form;
1153 call_sub($form->{stock_nextsub} || $form->{nextsub});
1156 sub setup_wh_transfer_warehouse_selection_action_bar {
1159 for my $bar ($::request->layout->get('actionbar')) {
1163 submit => [ '#form', { action => $action } ],
1164 accesskey => 'enter',
1170 sub setup_wh_transfer_warehouse_selection_assembly_action_bar {
1173 for my $bar ($::request->layout->get('actionbar')) {
1177 submit => [ '#form', { action => 'transfer_assembly_update_part' } ],
1178 accesskey => 'enter',
1182 submit => [ '#form', { action => 'create_assembly' } ],
1183 disabled => $::form->{parts_id} ? undef : $::locale->text('No assembly has been selected yet.'),
1189 sub setup_wh_transfer_parts_action_bar {
1192 for my $bar ($::request->layout->get('actionbar')) {
1196 submit => [ '#form', { action => 'transfer_parts' } ],
1197 accesskey => 'enter',
1201 call => [ 'kivi.history_back' ],
1207 sub setup_wh_removal_parts_selection_action_bar {
1210 for my $bar ($::request->layout->get('actionbar')) {
1214 submit => [ '#form', { action => 'remove_parts' } ],
1215 accesskey => 'enter',
1219 call => [ 'kivi.history_back' ],
1225 sub setup_wh_report_action_bar {
1228 for my $bar ($::request->layout->get('actionbar')) {
1232 submit => [ '#form', { action => 'generate_report' } ],
1233 accesskey => 'enter',
1239 sub setup_wh_journal_action_bar {
1242 for my $bar ($::request->layout->get('actionbar')) {
1246 submit => [ '#form', { action => 'generate_journal' } ],
1247 accesskey => 'enter',
1252 sub setup_wh_journal_list_all_action_bar {
1254 for my $bar ($::request->layout->get('actionbar')) {
1257 action => [ t8('Actions') ],
1259 t8('Disassemble Assembly'),
1260 submit => [ '#form', { action => 'disassemble_assembly' } ],
1261 checks => [ [ 'kivi.check_if_entries_selected', '[name="ids[]"]' ] ],
1275 bin/mozilla/wh.pl - Warehouse frontend.
1283 call new item dialogue from warehouse masks.
1286 action => name of sub to be called when new item is done