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);
47 use SL::Locale::String qw(t8);
48 use SL::ReportGenerator;
49 use SL::Presenter::Part;
55 require "bin/mozilla/common.pl";
56 require "bin/mozilla/reportgenerator.pl";
62 # contents of the "transfer_type" table:
63 # $locale->text('back')
64 # $locale->text('correction')
65 # $locale->text('disposed')
66 # $locale->text('found')
67 # $locale->text('missing')
68 # $locale->text('stock')
69 # $locale->text('shipped')
70 # $locale->text('transfer')
71 # $locale->text('used')
72 # $locale->text('return_material')
73 # $locale->text('release_material')
74 # $locale->text('assembled')
75 # $locale->text('stocktaking')
77 # --------------------------------------------------------------------
79 # --------------------------------------------------------------------
81 sub transfer_warehouse_selection {
82 $main::lxdebug->enter_sub();
84 $main::auth->assert('warehouse_management');
86 my $form = $main::form;
87 my %myconfig = %main::myconfig;
88 my $locale = $main::locale;
90 $form->get_lists('warehouses' => { 'key' => 'WAREHOUSES',
93 show_no_warehouses_error() if (!scalar @{ $form->{WAREHOUSES} });
95 my $units = AM->retrieve_units(\%myconfig, $form);
98 if ( $form->{parts_id} ) {
99 $part = SL::DB::Part->new();
100 $part->id($form->{parts_id});
104 # der zweite Parameter von unit_select_data gibt den default-Namen (selected) vor
105 $form->{UNITS} = AM->unit_select_data($units, $form->{unit}, 0, $part ? $part->unit : 0);
107 if (scalar @{ $form->{WAREHOUSES} }) {
108 $form->{warehouse_id} ||= $form->{WAREHOUSES}->[0]->{id};
109 $form->{bin_id} ||= $form->{WAREHOUSES}->[0]->{BINS}->[0]->{id};
114 if ($form->{trans_type} eq 'removal') {
115 setup_wh_transfer_warehouse_selection_action_bar("removal_parts_selection");
116 $form->{title} = $locale->text('Removal from Warehouse');
117 $content = $form->parse_html_template('wh/warehouse_selection');
119 } elsif (!$form->{trans_type} || ($form->{trans_type} eq 'transfer')) {
120 setup_wh_transfer_warehouse_selection_action_bar("transfer_parts_selection");
121 $form->{title} = $locale->text('Transfer');
122 $content = $form->parse_html_template('wh/warehouse_selection');
124 } elsif ($form->{trans_type} eq 'assembly') {
125 setup_wh_transfer_warehouse_selection_assembly_action_bar();
126 $form->{title} = $locale->text('Produce Assembly');
127 $content = $form->parse_html_template('wh/warehouse_selection_assembly');
133 $main::lxdebug->leave_sub();
136 sub transfer_parts_selection {
137 $main::lxdebug->enter_sub();
139 $main::auth->assert('warehouse_management');
141 my $form = $main::form;
142 my %myconfig = %main::myconfig;
143 my $locale = $main::locale;
145 transfer_or_removal_prepare_contents('direction' => 'transfer');
147 setup_wh_transfer_parts_action_bar();
149 $form->{title} = $locale->text('Transfer');
151 print $form->parse_html_template("wh/transfer_parts_selection");
153 $main::lxdebug->leave_sub();
156 sub transfer_or_removal_prepare_contents {
157 $main::lxdebug->enter_sub();
159 $main::auth->assert('warehouse_management');
163 my $form = $main::form;
164 my %myconfig = %main::myconfig;
165 my $locale = $main::locale;
167 $form->get_lists('warehouses' => { 'key' => 'WAREHOUSES',
168 'bins' => 'BINS', });
170 my $warehouse_idx = get_warehouse_idx($form->{warehouse_id});
171 $form->show_generic_error($locale->text("The selected warehouse does not exist.")) if (-1 == $warehouse_idx);
173 my $warehouse = $form->{WAREHOUSES}->[$warehouse_idx];
175 $form->{initial_warehouse_idx} = $warehouse_idx;
176 $form->{warehouse_description} = $warehouse->{description};
177 $warehouse->{selected} = 1;
179 $form->show_generic_error($locale->text("The source warehouse does not contain any bins.")) if (0 == scalar @{ $warehouse->{BINS} });
181 map { $form->{"l_$_"} = 'Y' } qw(parts_id qty warehouseid binid partnumber partdescription bindescription chargenumber bestbefore partunit ean);
183 $form->{sort} = 'bindescription';
184 my @contents = WH->get_warehouse_report("warehouse_id" => $form->{warehouse_id},
185 "bin_id" => $form->{bin_id},
186 "chargenumber" => $form->{chargenumber},
187 "bestbefore" => $form->{bestbefore},
188 "partsid" => $form->{part_id},
189 "ean" => $form->{ean});
191 if (0 == scalar(@contents)) {
192 $form->show_generic_error($locale->text("The selected warehouse is empty, or no stocked items where found that match the filter settings."));
195 my $all_units = AM->retrieve_units(\%myconfig, $form);
197 foreach (@contents) {
198 $_->{qty} = $form->format_amount_units('amount' => $_->{qty},
199 'part_unit' => $_->{partunit},
200 'conv_units' => 'convertible');
201 my $this_unit = $_->{partunit};
203 if ($all_units->{$_->{partunit}} && ($all_units->{g}->{base_unit} eq $all_units->{$_->{partunit}}->{base_unit})) {
207 $_->{UNITS} = AM->unit_select_data($all_units, $this_unit, 0, $_->{partunit});
210 my $transfer_types = WH->retrieve_transfer_types($args{direction});
211 map { $_->{description} = $locale->text($_->{description}) } @{ $transfer_types };
213 $form->{CONTENTS} = \@contents;
214 $form->{TRANSFER_TYPES} = $transfer_types;
216 $main::lxdebug->leave_sub();
221 $main::lxdebug->enter_sub();
223 $main::auth->assert('warehouse_management');
225 my $form = $main::form;
226 my %myconfig = %main::myconfig;
227 my $locale = $main::locale;
229 $form->get_lists('warehouses' => { 'key' => 'WAREHOUSES', 'bins' => 'BINS' });
231 my $warehouse_idx = get_warehouse_idx($form->{warehouse_id});
232 $form->show_generic_error($locale->text("The selected warehouse does not exist.")) if (-1 == $warehouse_idx);
234 my $warehouse = $form->{WAREHOUSES}->[$warehouse_idx];
236 $form->show_generic_error($locale->text("The source warehouse does not contain any bins.")) if (0 == scalar @{ $warehouse->{BINS} });
238 map { $form->{"l_$_"} = 'Y' } qw(parts_id qty warehouseid binid partnumber partdescription bindescription chargenumber bestbefore partunit);
240 $form->{sort} = 'bindescription';
241 my @contents = WH->get_warehouse_report("warehouse_id" => $form->{warehouse_id});
242 my $all_units = AM->retrieve_units(\%myconfig, $form);
246 foreach my $row (1 .. $form->{rowcount}) {
247 $form->{"qty_$row"} =~ s/^\s*//;
248 $form->{"qty_$row"} =~ s/\s*$//;
249 next if (!$form->{"qty_$row"});
251 my $bin_idx = get_bin_idx($warehouse_idx, $form->{"src_bin_id_$row"});
252 $form->show_generic_error($locale->text("The selected bin does not exist.")) if (-1 == $bin_idx);
253 my $bin = $warehouse->{BINS}->[$bin_idx];
255 my $orig_qty = $form->{"qty_$row"} . " " . $form->{"unit_$row"};
258 'src_warehouse_id' => $form->{warehouse_id},
259 'transfer_type_id' => $form->{transfer_type_id},
262 map { $transfer->{$_} = $form->{"${_}_${row}"} } qw(src_bin_id chargenumber bestbefore parts_id qty dst_warehouse_id dst_bin_id);
266 foreach (@contents) {
267 if (($_->{binid} == $transfer->{src_bin_id}) && ($_->{parts_id} == $transfer->{parts_id}) && ($_->{chargenumber} eq $transfer->{chargenumber}) && $_->{bestbefore} eq $transfer->{bestbefore}) {
274 $form->error($locale->text("There is not enough left of '#1' in bin '#2' for the removal of #3.",
275 $form->{"partdescription_$row"}, $bin->{description}, $orig_qty));
278 $transfer->{qty} = $form->parse_amount(\%myconfig, $transfer->{qty}) * $all_units->{$form->{"unit_$row"}}->{factor};
279 $transfer->{qty} /= $all_units->{$entry->{partunit}}->{factor} || 1;
281 if (($entry->{qty} < $transfer->{qty}) || (0 >= $transfer->{qty})) {
282 $form->error($locale->text("There is not enough left of '#1' in bin '#2' for the removal of #3.",
283 $form->{"partdescription_$row"}, $bin->{description}, $orig_qty));
286 $transfer->{comment} = $form->{comment};
287 $transfer->{change_default_bin} = $form->{change_default_bin};
289 push @transfers, $transfer;
291 $entry->{qty} -= $transfer->{qty};
294 if (!scalar @transfers) {
295 $form->show_generic_information($locale->text('Nothing has been selected for transfer.'));
296 $::dispatcher->end_request;
299 WH->transfer(@transfers);
301 $form->{trans_type} = 'transfer';
302 $form->{saved_message} = $locale->text('The parts have been transferred.');
304 transfer_warehouse_selection();
306 $main::lxdebug->leave_sub();
309 # --------------------------------------------------------------------
311 # --------------------------------------------------------------------
313 # --------------------------------------------------------------------
314 # Transfer: assemblies
315 # Dies ist die Auswahlmaske für ein assembly.
316 # Die ist einfach von transfer_assembly_update_part kopiert und nur um den trans_type (assembly) korrigiert worden
317 # Es wäre schön, hier nochmal check_assembly_max_create auf, um die max. Fertigungszahl herauszufinden.
318 # Ich lass das mal als auskommentierte Idee bestehen jb 18.3.09
319 # --------------------------------------------------------------------
321 sub transfer_assembly_update_part {
322 my $form = $main::form;
323 my %myconfig = %main::myconfig;
324 my $locale = $main::locale;
326 $form->{trans_type} = 'assembly';
327 $form->{qty} = $form->parse_amount(\%myconfig, $form->{qty});
329 if (!$form->{parts_id}) {
330 delete $form->{partunit};
331 transfer_warehouse_selection();
336 my $part = SL::DB::Part->new(id => $::form->{parts_id})->load;
337 @{$form}{qw(parts_id partnumber description)} = ($part->id, $part->partnumber, $part->description);
339 transfer_stock_get_partunit();
340 transfer_warehouse_selection();
343 sub transfer_stock_part_selected {
344 $main::lxdebug->enter_sub();
348 my $form = $main::form;
350 @{$form}{qw(parts_id partnumber description ean warehouse_id bin_id)} = @{$part}{qw(id partnumber description ean warehouse_id bin_id)};
352 transfer_stock_get_partunit();
353 transfer_warehouse_selection();
355 $main::lxdebug->leave_sub();
358 sub transfer_stock_get_partunit {
359 $main::lxdebug->enter_sub();
361 my $form = $main::form;
363 if ($form->{parts_id}) {
364 my $part_info = IC->get_basic_part_info('id' => $form->{parts_id});
365 $form->{partunit} = $part_info->{unit};
368 $main::lxdebug->leave_sub();
371 # vorüberlegung jb 22.2.2009
372 # wir benötigen für diese funktion, die anzahl die vom erzeugnis hergestellt werden soll. vielleicht direkt per js fehleingaben verhindern?
373 # ferner dann nochmal mit check_asssembly_max_create gegenprüfen und dann transaktionssicher wegbuchen.
374 # 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
375 # Manko ist derzeit noch, dass unterschiedliche Lagerplätze, bzw. das Quelllager an sich nicht ausgewählt werden können.
376 # Laut Absprache in KW11 09 übernimmt mb hier den rest im April ... jb 18.3.09
378 sub create_assembly {
379 $main::lxdebug->enter_sub();
381 my $form = $main::form;
382 my %myconfig = %main::myconfig;
383 my $locale = $main::locale;
385 $form->{qty} = $form->parse_amount(\%myconfig, $form->{qty});
386 if ($form->{qty} <= 0) {
387 $form->show_generic_error($locale->text('Invalid quantity.'));
389 # TODO Es wäre schön, hier schon die maximale Anzahl der zu fertigenden Erzeugnisse zu haben
390 #else { if ($form->{qty} > $maxcreate) { #s.o.
391 # $form->show_generic_error($locale->text('Can not create that quantity with current stock'));
392 # $form->show_generic_error('Maximale Stückzahl' . $maxcreate);
396 if (!$form->{warehouse_id} || !$form->{bin_id}) {
397 $form->error($locale->text('The warehouse or the bin is missing.'));
400 if (!$::instance_conf->get_show_bestbefore) {
401 $form->{bestbefore} = '';
404 # 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
405 # Anm. jb 18.3. vielleicht auch nur meine unwissenheit in perl-datenstrukturen
407 'transfer_type' => 'assembly',
408 'login' => $::myconfig{login},
409 'dst_warehouse_id' => $form->{warehouse_id},
410 'dst_bin_id' => $form->{bin_id},
411 'chargenumber' => $form->{chargenumber},
412 'bestbefore' => $form->{bestbefore},
413 'assembly_id' => $form->{parts_id},
414 'qty' => $form->{qty},
415 'unit' => $form->{unit},
416 'comment' => $form->{comment}
419 my $ret = WH->transfer_assembly (%TRANSFER);
420 # Frage: Ich pack in den return-wert auch gleich die Fehlermeldung. Irgendwelche Nummern als Fehlerkonstanten definieren find ich auch nicht besonders schick...
423 # 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
424 $form->show_generic_error($ret);
427 delete @{$form}{qw(parts_id partnumber description qty unit chargenumber bestbefore comment)};
429 $form->{saved_message} = $locale->text('The assembly has been created.');
430 $form->{trans_type} = 'assembly';
432 transfer_warehouse_selection();
434 $main::lxdebug->leave_sub();
437 # --------------------------------------------------------------------
439 # --------------------------------------------------------------------
441 sub removal_parts_selection {
442 $main::lxdebug->enter_sub();
444 $main::auth->assert('warehouse_management');
446 my $form = $main::form;
447 my %myconfig = %main::myconfig;
448 my $locale = $main::locale;
450 transfer_or_removal_prepare_contents('direction' => 'out');
452 setup_wh_removal_parts_selection_action_bar();
454 $form->{title} = $locale->text('Removal');
456 print $form->parse_html_template("wh/removal_parts_selection");
458 $main::lxdebug->leave_sub();
462 $main::lxdebug->enter_sub();
464 $main::auth->assert('warehouse_management');
466 my $form = $main::form;
467 my %myconfig = %main::myconfig;
468 my $locale = $main::locale;
470 $form->get_lists('warehouses' => { 'key' => 'WAREHOUSES',
471 'bins' => 'BINS', });
473 my $warehouse_idx = get_warehouse_idx($form->{warehouse_id});
474 $form->show_generic_error($locale->text("The selected warehouse does not exist.")) if (-1 == $warehouse_idx);
476 my $warehouse = $form->{WAREHOUSES}->[$warehouse_idx];
478 $form->show_generic_error($locale->text("The warehouse does not contain any bins.")) if (0 == scalar @{ $warehouse->{BINS} });
480 map { $form->{"l_$_"} = 'Y' } qw(parts_id qty warehouseid binid partnumber partdescription bindescription chargenumber bestbefore partunit);
482 $form->{sort} = 'bindescription';
483 my @contents = WH->get_warehouse_report("warehouse_id" => $form->{warehouse_id});
484 my $all_units = AM->retrieve_units(\%myconfig, $form);
488 foreach my $row (1 .. $form->{rowcount}) {
489 $form->{"qty_$row"} =~ s/^\s*//;
490 $form->{"qty_$row"} =~ s/\s*$//;
491 next if (!$form->{"qty_$row"});
493 my $bin_idx = get_bin_idx($warehouse_idx, $form->{"src_bin_id_$row"});
494 $form->show_generic_error($locale->text("The selected bin does not exist.")) if (-1 == $bin_idx);
495 my $bin = $warehouse->{BINS}->[$bin_idx];
497 my $orig_qty = $form->{"qty_$row"} . " " . $form->{"unit_$row"};
500 'src_warehouse_id' => $form->{warehouse_id},
501 'transfer_type_id' => $form->{transfer_type_id},
504 map { $transfer->{$_} = $form->{"${_}_${row}"} } qw(src_bin_id chargenumber bestbefore parts_id qty);
508 foreach (@contents) {
509 if (($_->{binid} == $transfer->{src_bin_id}) && ($_->{parts_id} == $transfer->{parts_id}) && ($_->{chargenumber} eq $transfer->{chargenumber}) && ($_->{bestbefore} eq $transfer->{bestbefore})) {
516 $form->error($locale->text("There is not enough left of '#1' in bin '#2' for the removal of #3.",
517 $form->{"partdescription_$row"}, $bin->{description}, $orig_qty));
520 $transfer->{qty} = $form->parse_amount(\%myconfig, $transfer->{qty}) * $all_units->{$form->{"unit_$row"}}->{factor};
521 $transfer->{qty} /= $all_units->{$entry->{partunit}}->{factor} || 1;
523 if (($entry->{qty} < $transfer->{qty}) || (0 >= $transfer->{qty})) {
524 $form->error($locale->text("There is not enough left of '#1' in bin '#2' for the removal of #3.",
525 $form->{"partdescription_$row"}, $bin->{description}, $orig_qty));
528 $transfer->{comment} = $form->{comment};
530 push @transfers, $transfer;
532 $entry->{qty} -= $transfer->{qty};
535 if (!scalar @transfers) {
536 $form->show_generic_information($locale->text('Nothing has been selected for removal.'));
537 $::dispatcher->end_request;
540 WH->transfer(@transfers);
542 $form->{trans_type} = 'removal';
543 $form->{saved_message} = $locale->text('The parts have been removed.');
545 transfer_warehouse_selection();
547 $main::lxdebug->leave_sub();
550 # --------------------------------------------------------------------
552 # --------------------------------------------------------------------
555 $main::lxdebug->enter_sub();
557 $main::auth->assert('warehouse_management');
559 my $form = $main::form;
560 my %myconfig = %main::myconfig;
561 my $locale = $main::locale;
563 $form->{title} = $locale->text('Report about warehouse transactions');
564 $form->get_lists('warehouses' => { 'key' => 'WAREHOUSES',
565 'bins' => 'BINS', });
567 show_no_warehouses_error() if (!scalar @{ $form->{WAREHOUSES} });
569 setup_wh_journal_action_bar();
572 print $form->parse_html_template("wh/journal_filter", { "UNITS" => AM->unit_select_data(AM->retrieve_units(\%myconfig, $form)) });
574 $main::lxdebug->leave_sub();
577 sub generate_journal {
578 $main::lxdebug->enter_sub();
580 $main::auth->assert('warehouse_management');
582 my $form = $main::form;
583 my %myconfig = %main::myconfig;
584 my $locale = $main::locale;
586 $form->{title} = $locale->text("WHJournal");
587 $form->{sort} ||= 'date';
589 $form->{report_generator_output_format} = 'HTML' if !$form->{report_generator_output_format};
592 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);
595 map { $filter{$_} = $form->{$_} if ($form->{$_}) } qw(warehouse_id bin_id classification_id partnumber description chargenumber bestbefore transtype_id transtype_ids comment projectnumber);
597 $filter{qty_op} = WH->convert_qty_op($form->{qty_op});
598 if ($filter{qty_op}) {
599 $form->isblank("qty", $locale->text('Quantity missing.'));
600 $form->isblank("qty_unit", $locale->text('Unit missing.'));
602 $filter{qty} = $form->{qty};
603 $filter{qty_unit} = $form->{qty_unit};
607 my $allrows = !!($form->{report_generator_output_format} ne 'HTML') ;
611 my $page = $::form->{page} || 1;
612 $pages->{per_page} = $::form->{per_page} || 15;
613 my $first_nr = ($page - 1) * $pages->{per_page};
614 my $last_nr = $first_nr + $pages->{per_page};
616 # no optimisation if qty op
617 if ( !$allrows && $form->{maxrows} && !$filter{qty_op}) {
618 $filter{limit} = $pages->{per_page};
619 $filter{offset} = ($page - 1) * $pages->{per_page};
621 $last_nr = $pages->{per_page};
624 my @contents = WH->get_warehouse_journal(%filter);
626 if (!$form->{maxrows}) {
627 $form->{maxrows} = scalar @contents ;
630 my $report = SL::ReportGenerator->new(\%myconfig, $form);
632 my @hidden_variables = map { "l_${_}" } @columns;
633 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);
634 push @hidden_variables, qw(classification_id);
637 'date' => { 'text' => $locale->text('Date'), },
638 'trans_id' => { 'text' => $locale->text('Trans Id'), },
639 'trans_type' => { 'text' => $locale->text('Trans Type'), },
640 'comment' => { 'text' => $locale->text('Comment'), },
641 'warehouse_from' => { 'text' => $locale->text('Warehouse From'), },
642 'warehouse_to' => { 'text' => $locale->text('Warehouse To'), },
643 'bin_from' => { 'text' => $locale->text('Bin From'), },
644 'bin_to' => { 'text' => $locale->text('Bin To'), },
645 'partnumber' => { 'text' => $locale->text('Part Number'), },
647 => { 'text' => $locale->text('Type'), },
648 'partdescription' => { 'text' => $locale->text('Part Description'), },
649 'chargenumber' => { 'text' => $locale->text('Charge Number'), },
650 'bestbefore' => { 'text' => $locale->text('Best Before'), },
651 'qty' => { 'text' => $locale->text('Qty'), },
652 'unit' => { 'text' => $locale->text('Part Unit'), },
653 'partunit' => { 'text' => $locale->text('Unit'), },
654 'employee' => { 'text' => $locale->text('Employee'), },
655 'projectnumber' => { 'text' => $locale->text('Project Number'), },
656 'oe_id' => { 'text' => $locale->text('Document'), },
659 if ($form->{transtype_ids} && 'ARRAY' eq ref $form->{transtype_ids}) {
660 for (my $i = 0; $i < scalar(@{ $form->{transtype_ids} }); $i++) {
661 delete $form->{transtype_ids}[$i] if $form->{transtype_ids}[$i] eq '';
663 $form->{transtype_ids} = join(",", @{ $form->{transtype_ids} });
666 my $href = build_std_url('action=generate_journal', grep { $form->{$_} } @hidden_variables);
667 $href .= "&maxrows=".$form->{maxrows};
669 map { $column_defs{$_}->{link} = $href ."&page=".$page. "&sort=${_}&order=" . Q($_ eq $form->{sort} ? 1 - $form->{order} : $form->{order}) } @columns;
671 my %column_alignment = map { $_ => 'right' } qw(qty);
673 map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
674 $column_defs{partunit}->{visible} = 1;
675 $column_defs{type_and_classific}->{visible} = 1;
676 $column_defs{type_and_classific}->{link} ='';
678 $report->set_columns(%column_defs);
679 $report->set_column_order(@columns);
681 $report->set_export_options('generate_journal', @hidden_variables, qw(sort order));
683 $report->set_sort_indicator($form->{sort}, $form->{order});
685 $report->set_options('output_format' => 'HTML',
686 'title' => $form->{title},
687 'attachment_basename' => strftime($locale->text('warehouse_journal_list') . '_%Y%m%d', localtime time));
688 $report->set_options_from_form();
689 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
691 my $all_units = AM->retrieve_units(\%myconfig, $form);
693 my %doc_types = ( 'sales_quotation' => { script => 'oe', title => $locale->text('Sales quotation') },
694 'sales_order' => { script => 'oe', title => $locale->text('Sales Order') },
695 'request_quotation' => { script => 'oe', title => $locale->text('Request quotation') },
696 'purchase_order' => { script => 'oe', title => $locale->text('Purchase Order') },
697 'sales_delivery_order' => { script => 'do', title => $locale->text('Sales delivery order') },
698 'purchase_delivery_order' => { script => 'do', title => $locale->text('Purchase delivery order') },
699 'sales_invoice' => { script => 'is', title => $locale->text('Sales Invoice') },
700 'purchase_invoice' => { script => 'ir', title => $locale->text('Purchase Invoice') },
705 foreach my $entry (@contents) {
706 $entry->{type_and_classific} = SL::Presenter::Part::type_abbreviation($entry->{part_type}) .
707 SL::Presenter::Part::classification_abbreviation($entry->{classification_id});
708 $entry->{qty} = $form->format_amount(\%myconfig, $entry->{qty});
709 $entry->{trans_type} = $locale->text($entry->{trans_type});
713 foreach my $column (@columns) {
715 'data' => $entry->{$column},
716 'align' => $column_alignment{$column},
720 $row->{trans_type}->{raw_data} = $entry->{trans_type};
722 if ($form->{l_oe_id}) {
723 $row->{oe_id}->{data} = '';
724 my $info = $entry->{oe_id_info};
726 if ($info && $info->{id} && $info->{type} && $doc_types{$info->{type}}) {
727 $row->{oe_id} = { data => $doc_types{ $info->{type} }->{title} . ' ' . $info->{number},
728 link => build_std_url('script=' . $doc_types{ $info->{type} }->{script} . '.pl', 'action=edit', 'id=' . $info->{id}, 'type=' . $info->{type}) };
732 if ( $allrows || ($idx >= $first_nr && $idx < $last_nr )) {
733 $report->add_data($row);
739 $pages->{max} = SL::DB::Helper::Paginated::ceil($form->{maxrows}, $pages->{per_page}) || 1;
740 $pages->{page} = $page < 1 ? 1: $page > $pages->{max} ? $pages->{max}: $page;
741 $pages->{common} = [ grep { $_->{visible} } @{ SL::DB::Helper::Paginated::make_common_pages($pages->{page}, $pages->{max}) } ];
743 $report->set_options('raw_bottom_info_text' => $form->parse_html_template('common/paginate',
744 { 'pages' => $pages , 'base_url' => $href.'&sort='.$form->{sort}.'&order='.$form->{order}}) );
746 $report->generate_with_headers();
748 $main::lxdebug->leave_sub();
751 # --------------------------------------------------------------------
753 # --------------------------------------------------------------------
756 $main::lxdebug->enter_sub();
758 $main::auth->assert('warehouse_contents | warehouse_management');
760 my $form = $main::form;
761 my %myconfig = %main::myconfig;
762 my $locale = $main::locale;
764 $form->get_lists('warehouses' => { 'key' => 'WAREHOUSES',
765 'bins' => 'BINS', });
767 show_no_warehouses_error() if (!scalar @{ $form->{WAREHOUSES} });
769 my $CVAR_CONFIGS = SL::DB::Manager::CustomVariableConfig->get_all_sorted(where => [ module => 'IC' ]);
770 my $INCLUDABLE_CVAR_CONFIGS = [ grep { $_->includeable } @{ $CVAR_CONFIGS } ];
772 $form->{title} = $locale->text("Report about warehouse contents");
774 setup_wh_report_action_bar();
777 print $form->parse_html_template("wh/report_filter",
778 { "WAREHOUSES" => $form->{WAREHOUSES},
779 "UNITS" => AM->unit_select_data(AM->retrieve_units(\%myconfig, $form)),
780 # "CVAR_CONFIGS" => $CVAR_CONFIGS, # nyi searchable cvars
781 "INCLUDABLE_CVAR_CONFIGS" => $INCLUDABLE_CVAR_CONFIGS,
784 $main::lxdebug->leave_sub();
787 sub generate_report {
788 $main::lxdebug->enter_sub();
790 $main::auth->assert('warehouse_contents | warehouse_management');
792 my $form = $main::form;
793 my %myconfig = %main::myconfig;
794 my $locale = $main::locale;
796 my $cvar_configs = CVar->get_configs('module' => 'IC');
798 $form->{title} = $locale->text("Report about warehouse contents");
799 $form->{sort} ||= 'partnumber';
800 my $sort_col = $form->{sort};
803 my @columns = qw(warehousedescription bindescription partnumber type_and_classific partdescription chargenumber bestbefore comment qty partunit list_price purchase_price stock_value);
806 map { $filter{$_} = $form->{$_} if ($form->{$_}) } qw(warehouse_id bin_id classification_id partnumber description chargenumber bestbefore date include_invalid_warehouses);
808 # show filter stuff also in report
810 my $currentdate = $form->current_date(\%myconfig);
811 push @options, $locale->text('Printdate') . " : ".$locale->date(\%myconfig, $currentdate, 1);
813 # dispatch all options
814 my $dispatch_options = {
815 warehouse_id => sub { push @options, $locale->text('Warehouse') . " : " .
816 SL::DB::Manager::Warehouse->find_by(id => $form->{warehouse_id})->description},
817 bin_id => sub { push @options, $locale->text('Bin') . " : " .
818 SL::DB::Manager::Bin->find_by(id => $form->{bin_id})->description},
819 partnumber => sub { push @options, $locale->text('Partnumber') . " : $form->{partnumber}"},
820 classification_id => sub { push @options, $locale->text('Parts Classification'). " : ".
821 SL::DB::Manager::PartClassification->get_first(where => [ id => $form->{classification_id} ] )->description; },
822 description => sub { push @options, $locale->text('Description') . " : $form->{description}"},
823 chargenumber => sub { push @options, $locale->text('Charge Number') . " : $form->{chargenumber}"},
824 bestbefore => sub { push @options, $locale->text('Best Before') . " : $form->{bestbefore}"},
825 include_invalid_warehouses => sub { push @options, $locale->text('Include invalid warehouses ')},
827 foreach (keys %filter) {
828 $dispatch_options->{$_}->() if $dispatch_options->{$_};
830 push @options, $locale->text('Stock Qty for Date') . " " . $locale->date(\%myconfig, $form->{date}?$form->{date}:$currentdate, 1);
832 # / end show filter stuff also in report
834 $filter{qty_op} = WH->convert_qty_op($form->{qty_op});
835 if ($filter{qty_op}) {
836 $form->isblank("qty", $locale->text('Quantity missing.'));
837 $form->isblank("qty_unit", $locale->text('Unit missing.'));
839 $filter{qty} = $form->{qty};
840 $filter{qty_unit} = $form->{qty_unit};
844 $form->{report_generator_output_format} = 'HTML' if !$form->{report_generator_output_format};
847 my $allrows = $form->{report_generator_output_format} eq 'HTML' ? $form->{allrows} : 1;
848 my $page = $::form->{page} || 1;
850 $pages->{per_page} = $::form->{per_page} || 20;
851 my $first_nr = ($page - 1) * $pages->{per_page};
852 my $last_nr = $first_nr + $pages->{per_page};
854 # no optimisation if qty op
855 if ( !$allrows && $form->{maxrows} && !$filter{qty_op}) {
856 $filter{limit} = $pages->{per_page};
857 $filter{offset} = ($page - 1) * $pages->{per_page};
859 $last_nr = $pages->{per_page};
862 my @contents = WH->get_warehouse_report(%filter);
865 if (!$form->{maxrows}) {
866 $form->{maxrows} = scalar @contents ;
869 $form->{subtotal} = '' if (!first { $_ eq $sort_col } qw(partnumber partdescription));
871 my $report = SL::ReportGenerator->new(\%myconfig, $form);
873 my @includeable_custom_variables = grep { $_->{includeable} } @{ $cvar_configs };
874 push @columns, map { "cvar_$_->{name}" } @includeable_custom_variables;
876 my @hidden_variables = map { "l_${_}" } @columns;
877 push @hidden_variables, qw(warehouse_id bin_id partnumber partstypes_id description chargenumber bestbefore qty_op qty qty_unit partunit l_warehousedescription l_bindescription);
878 push @hidden_variables, qw(include_empty_bins subtotal include_invalid_warehouses date);
879 push @hidden_variables, qw(classification_id stock_value_basis allrows);
882 'warehousedescription' => { 'text' => $locale->text('Warehouse'), },
883 'bindescription' => { 'text' => $locale->text('Bin'), },
884 'partnumber' => { 'text' => $locale->text('Part Number'), },
885 'type_and_classific' => { 'text' => $locale->text('Type'), },
886 'partdescription' => { 'text' => $locale->text('Part Description'), },
887 'chargenumber' => { 'text' => $locale->text('Charge Number'), },
888 'bestbefore' => { 'text' => $locale->text('Best Before'), },
889 'qty' => { 'text' => $locale->text('Qty'), },
890 'partunit' => { 'text' => $locale->text('Unit'), },
891 'stock_value' => { 'text' => $locale->text('Stock value'), },
892 'purchase_price' => { 'text' => $locale->text('Purchase price'), },
893 'list_price' => { 'text' => $locale->text('List Price'), },
896 my $href = build_std_url('action=generate_report', grep { $form->{$_} } @hidden_variables);
897 $href .= "&maxrows=".$form->{maxrows};
898 my %column_defs_cvars = map { +"cvar_$_->{name}" => { 'text' => $_->{description} } } @includeable_custom_variables;
899 %column_defs = (%column_defs, %column_defs_cvars);
901 map { $column_defs{$_}->{link} = $href . "&page=".$page."&sort=${_}&order=" . Q($_ eq $sort_col ? 1 - $form->{order} : $form->{order}) } @columns;
903 my %column_alignment = map { $_ => 'right' } qw(qty list_price purchase_price stock_value);
905 map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
907 $column_defs{partunit}->{visible} = 1;
908 $column_defs{type_and_classific}->{visible} = 1;
909 $column_defs{type_and_classific}->{link} ='';
911 $report->set_columns(%column_defs);
912 $report->set_column_order(@columns);
914 $report->set_export_options('generate_report', @hidden_variables, qw(sort order));
916 $report->set_sort_indicator($sort_col, $form->{order});
918 $report->set_options('top_info_text' => join("\n", @options),
919 'output_format' => 'HTML',
920 'title' => $form->{title},
921 'attachment_basename' => strftime($locale->text('warehouse_report_list') . '_%Y%m%d', localtime time));
922 $report->set_options_from_form();
923 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
924 CVar->add_custom_variables_to_report('module' => 'IC',
925 'trans_id_field' => 'parts_id',
926 'configs' => $cvar_configs,
927 'column_defs' => \%column_defs,
928 'data' => \@contents);
930 my $all_units = AM->retrieve_units(\%myconfig, $form);
933 my @subtotals_columns = qw(qty stock_value);
934 my %subtotals = map { $_ => 0 } @subtotals_columns;
936 my $total_stock_value = 0;
938 foreach my $entry (@contents) {
940 $entry->{type_and_classific} = SL::Presenter::Part::type_abbreviation($entry->{part_type}).
941 SL::Presenter::Part::classification_abbreviation($entry->{classification_id});
942 map { $subtotals{$_} += $entry->{$_} } @subtotals_columns;
943 $total_stock_value += $entry->{stock_value} * 1;
944 $entry->{qty} = $form->format_amount(\%myconfig, $entry->{qty});
945 # $entry->{qty} = $form->format_amount_units('amount' => $entry->{qty},
946 # 'part_unit' => $entry->{partunit},
947 # 'conv_units' => 'convertible');
948 $entry->{stock_value} = $form->format_amount(\%myconfig, $entry->{stock_value} * 1, 2);
949 $entry->{purchase_price} = $form->format_amount(\%myconfig, $entry->{purchase_price} * 1, 2);
950 $entry->{list_price} = $form->format_amount(\%myconfig, $entry->{list_price} * 1, 2);
952 my $row_set = [ { map { $_ => { 'data' => $entry->{$_}, 'align' => $column_alignment{$_} } } @columns } ];
954 if ( ($form->{subtotal} eq 'Y' && !$form->{report_generator_csv_options_for_import} )
955 && (($idx == (scalar @contents - 1))
956 || ($entry->{$sort_col} ne $contents[$idx + 1]->{$sort_col}))) {
958 my $row = { map { $_ => { 'data' => '', 'class' => 'listsubtotal', 'align' => $column_alignment{$_}, } } @columns };
959 $row->{qty}->{data} = $form->format_amount(\%myconfig, $subtotals{qty});
960 # $row->{qty}->{data} = $form->format_amount_units('amount' => $subtotals{qty} * 1,
961 # 'part_unit' => $entry->{partunit},
962 # 'conv_units' => 'convertible');
963 $row->{stock_value}->{data} = $form->format_amount(\%myconfig, $subtotals{stock_value} * 1, 2);
964 $row->{purchase_price}->{data} = $form->format_amount(\%myconfig, $subtotals{purchase_price} * 1, 2);
965 $row->{list_price}->{data} = $form->format_amount(\%myconfig, $subtotals{list_price} * 1, 2);
967 %subtotals = map { $_ => 0 } @subtotals_columns;
969 push @{ $row_set }, $row;
972 if ( $allrows || ($idx >= $first_nr && $idx < $last_nr )) {
973 $report->add_data($row_set);
978 if ( $column_defs{stock_value}->{visible} && !$form->{report_generator_csv_options_for_import} ) {
979 $report->add_separator();
981 my $row = { map { $_ => { 'data' => '', 'class' => 'listsubtotal', } } @columns };
983 my $left_col = first { $column_defs{$_}->{visible} } @columns;
985 $row->{$left_col}->{data} = $locale->text('Total stock value');
986 $row->{stock_value}->{data} = $form->format_amount(\%myconfig, $total_stock_value, 2);
987 $row->{stock_value}->{align} = 'right';
989 $report->add_data($row);
992 $pages->{max} = SL::DB::Helper::Paginated::ceil($form->{maxrows}, $pages->{per_page}) || 1;
993 $pages->{page} = $page < 1 ? 1: $page > $pages->{max} ? $pages->{max}: $page;
994 $pages->{common} = [ grep { $_->{visible} } @{ SL::DB::Helper::Paginated::make_common_pages($pages->{page}, $pages->{max}) } ];
996 $report->set_options('raw_bottom_info_text' => $form->parse_html_template('common/paginate',
997 {'pages' => $pages , 'base_url' => $href}) );
1000 $report->generate_with_headers();
1002 $main::lxdebug->leave_sub();
1005 # --------------------------------------------------------------------
1007 # --------------------------------------------------------------------
1009 sub show_no_warehouses_error {
1010 $main::lxdebug->enter_sub();
1012 my $form = $main::form;
1013 my %myconfig = %main::myconfig;
1014 my $locale = $main::locale;
1016 my $msg = $locale->text('No warehouse has been created yet or the quantity of the bins is not configured yet.') . ' ';
1018 if ($main::auth->check_right($::myconfig{login}, 'config')) {
1019 $msg .= $locale->text('You can create warehouses and bins via the menu "System -> Warehouses".');
1021 $msg .= $locale->text('Please ask your administrator to create warehouses and bins.');
1024 $form->show_generic_error($msg);
1026 $main::lxdebug->leave_sub();
1029 sub get_warehouse_idx {
1030 my ($warehouse_id) = @_;
1032 my $form = $main::form;
1034 for (my $i = 0; $i < scalar @{$form->{WAREHOUSES}}; $i++) {
1035 return $i if ($form->{WAREHOUSES}->[$i]->{id} == $warehouse_id);
1042 my ($warehouse_index, $bin_id) = @_;
1044 my $form = $main::form;
1046 my $warehouse = $form->{WAREHOUSES}->[$warehouse_index];
1048 return -1 if (!$warehouse);
1050 for (my $i = 0; $i < scalar @{ $warehouse->{BINS} }; $i++) {
1051 return $i if ($warehouse->{BINS}->[$i]->{id} == $bin_id);
1058 $main::lxdebug->enter_sub();
1061 my $form = $main::form;
1064 $form->{old_callback} = $form->escape($form->{callback}, 1);
1065 $form->{callback} = $form->escape("$form->{script}?action=$params{action}", 1);
1067 # save all form variables except action in a previousform variable
1068 my $previousform = join '&', map { my $value = $form->{$_}; $value =~ s/&/%26/; "$_=$value" } grep { !/action/ } keys %$form;
1071 # push @HIDDENS, { 'name' => 'previousform', 'value' => $form->escape($previousform, 1) };
1072 push @HIDDENS, map +{ 'name' => $_, 'value' => $form->{$_} }, qw(partnumber description unit vc sellprice ean);
1073 push @HIDDENS, { 'name' => 'taxaccount2', 'value' => $form->{taxaccounts} };
1074 push @HIDDENS, { 'name' => 'notes', 'value' => $form->{longdescription} };
1077 print $form->parse_html_template("generic/new_item", { HIDDENS => [ sort { $a->{name} cmp $b->{name} } @HIDDENS ] } );
1079 $main::lxdebug->leave_sub();
1083 my $form = $main::form;
1084 call_sub($form->{update_nextsub} || $form->{nextsub});
1088 my $form = $main::form;
1089 call_sub($form->{continue_nextsub} || $form->{nextsub});
1093 my $form = $main::form;
1094 call_sub($form->{stock_nextsub} || $form->{nextsub});
1097 sub setup_wh_transfer_warehouse_selection_action_bar {
1100 for my $bar ($::request->layout->get('actionbar')) {
1104 submit => [ '#form', { action => $action } ],
1105 accesskey => 'enter',
1111 sub setup_wh_transfer_warehouse_selection_assembly_action_bar {
1114 for my $bar ($::request->layout->get('actionbar')) {
1118 submit => [ '#form', { action => 'transfer_assembly_update_part' } ],
1119 accesskey => 'enter',
1123 submit => [ '#form', { action => 'create_assembly' } ],
1124 disabled => $::form->{parts_id} ? undef : $::locale->text('No assembly has been selected yet.'),
1130 sub setup_wh_transfer_parts_action_bar {
1133 for my $bar ($::request->layout->get('actionbar')) {
1137 submit => [ '#form', { action => 'transfer_parts' } ],
1138 accesskey => 'enter',
1142 call => [ 'kivi.history_back' ],
1148 sub setup_wh_removal_parts_selection_action_bar {
1151 for my $bar ($::request->layout->get('actionbar')) {
1155 submit => [ '#form', { action => 'remove_parts' } ],
1156 accesskey => 'enter',
1160 call => [ 'kivi.history_back' ],
1166 sub setup_wh_report_action_bar {
1169 for my $bar ($::request->layout->get('actionbar')) {
1173 submit => [ '#form', { action => 'generate_report' } ],
1174 accesskey => 'enter',
1180 sub setup_wh_journal_action_bar {
1183 for my $bar ($::request->layout->get('actionbar')) {
1187 submit => [ '#form', { action => 'generate_journal' } ],
1188 accesskey => 'enter',
1200 bin/mozilla/wh.pl - Warehouse frontend.
1208 call new item dialogue from warehouse masks.
1211 action => name of sub to be called when new item is done