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., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #######################################################################
30 # warehouse and packinglist
32 #######################################################################
34 use List::Util qw(min max first);
35 use POSIX qw(strftime);
45 use SL::ReportGenerator;
49 require "bin/mozilla/common.pl";
50 require "bin/mozilla/reportgenerator.pl";
54 # contents of the "transfer_type" table:
55 # $locale->text('back')
56 # $locale->text('correction')
57 # $locale->text('disposed')
58 # $locale->text('found')
59 # $locale->text('missing')
60 # $locale->text('stock')
61 # $locale->text('shipped')
62 # $locale->text('transfer')
63 # $locale->text('used')
64 # $locale->text('return_material')
65 # $locale->text('release_material')
67 # --------------------------------------------------------------------
69 # --------------------------------------------------------------------
71 sub transfer_warehouse_selection {
72 $lxdebug->enter_sub();
74 $auth->assert('warehouse_management');
76 $form->get_lists('warehouses' => { 'key' => 'WAREHOUSES',
79 show_no_warehouses_error() if (!scalar @{ $form->{WAREHOUSES} });
81 my $units = AM->retrieve_units(\%myconfig, $form);
82 $form->{UNITS} = AM->unit_select_data($units, $form->{unit}, 0, $form->{partunit});
84 if (scalar @{ $form->{WAREHOUSES} }) {
85 $form->{warehouse_id} ||= $form->{WAREHOUSES}->[0]->{id};
86 $form->{bin_id} ||= $form->{WAREHOUSES}->[0]->{BINS}->[0]->{id};
91 $form->{jsscript} = 1;
93 if ($form->{trans_type} eq 'removal') {
94 $form->{nextsub} = "removal_parts_selection";
95 $form->{title} = $locale->text('Removal from Warehouse');
96 $content = $form->parse_html_template('wh/warehouse_selection');
98 } elsif ($form->{trans_type} eq 'stock') {
99 $form->{title} = $locale->text('Stock');
100 $content = $form->parse_html_template('wh/warehouse_selection_stock');
102 } elsif (!$form->{trans_type} || ($form->{trans_type} eq 'transfer')) {
103 $form->{nextsub} = "transfer_parts_selection";
104 $form->{title} = $locale->text('Transfer');
105 $content = $form->parse_html_template('wh/warehouse_selection');
107 } elsif ($form->{trans_type} eq 'assembly') {
108 $form->{title} = $locale->text('Produce Assembly');
109 $content = $form->parse_html_template('wh/warehouse_selection_assembly');
115 $lxdebug->leave_sub();
118 sub transfer_parts_selection {
119 $lxdebug->enter_sub();
121 $auth->assert('warehouse_management');
123 transfer_or_removal_prepare_contents('direction' => 'transfer');
125 $form->{title} = $locale->text('Transfer');
127 print $form->parse_html_template("wh/transfer_parts_selection");
129 $lxdebug->leave_sub();
132 sub transfer_or_removal_prepare_contents {
133 $lxdebug->enter_sub();
135 $auth->assert('warehouse_management');
139 $form->get_lists('warehouses' => { 'key' => 'WAREHOUSES',
140 'bins' => 'BINS', });
142 my $warehouse_idx = get_warehouse_idx($form->{warehouse_id});
143 $form->show_generic_error($locale->text("The selected warehouse does not exist.")) if (-1 == $warehouse_idx);
145 my $warehouse = $form->{WAREHOUSES}->[$warehouse_idx];
147 $form->{initial_warehouse_idx} = $warehouse_idx;
148 $form->{warehouse_description} = $warehouse->{description};
149 $warehouse->{selected} = 1;
151 $form->show_generic_error($locale->text("The source warehouse does not contain any bins.")) if (0 == scalar @{ $warehouse->{BINS} });
153 map { $form->{"l_$_"} = 'Y' } qw(parts_id qty warehouseid binid partnumber partdescription bindescription chargenumber partunit ean);
155 $form->{sort} = 'bindescription';
156 my @contents = WH->get_warehouse_report("warehouse_id" => $form->{warehouse_id},
157 "bin_id" => $form->{bin_id},
158 "chargenumber" => $form->{chargenumber},
159 "partnumber" => $form->{partnumber},
160 "ean" => $form->{ean},
161 "description" => $form->{description});
163 $form->show_generic_error($locale->text("The selected warehouse is empty.")) if (0 == scalar(@contents));
165 my $all_units = AM->retrieve_units(\%myconfig, $form);
167 foreach (@contents) {
168 $_->{qty} = $form->format_amount_units('amount' => $_->{qty},
169 'part_unit' => $_->{partunit},
170 'conv_units' => 'convertible');
171 my $this_unit = $_->{partunit};
173 if ($all_units->{$_->{partunit}} && ($all_units->{g}->{base_unit} eq $all_units->{$_->{partunit}}->{base_unit})) {
177 $_->{UNITS} = AM->unit_select_data($all_units, $this_unit, 0, $_->{partunit});
180 my $transfer_types = WH->retrieve_transfer_types($args{direction});
181 map { $_->{description} = $locale->text($_->{description}) } @{ $transfer_types };
183 $form->{CONTENTS} = \@contents;
184 $form->{TRANSFER_TYPES} = $transfer_types;
186 $lxdebug->leave_sub();
191 $lxdebug->enter_sub();
193 $auth->assert('warehouse_management');
195 $form->get_lists('warehouses' => { 'key' => 'WAREHOUSES', 'bins' => 'BINS' });
197 my $warehouse_idx = get_warehouse_idx($form->{warehouse_id});
198 $form->show_generic_error($locale->text("The selected warehouse does not exist.")) if (-1 == $warehouse_idx);
200 my $warehouse = $form->{WAREHOUSES}->[$warehouse_idx];
202 $form->show_generic_error($locale->text("The source warehouse does not contain any bins.")) if (0 == scalar @{ $warehouse->{BINS} });
204 map { $form->{"l_$_"} = 'Y' } qw(parts_id qty warehouseid binid partnumber partdescription bindescription chargenumber partunit);
206 $form->{sort} = 'bindescription';
207 my @contents = WH->get_warehouse_report("warehouse_id" => $form->{warehouse_id});
208 my $all_units = AM->retrieve_units(\%myconfig, $form);
212 foreach my $row (1 .. $form->{rowcount}) {
213 $form->{"qty_$row"} =~ s/^\s*//;
214 $form->{"qty_$row"} =~ s/\s*$//;
215 next if (!$form->{"qty_$row"});
217 my $bin_idx = get_bin_idx($warehouse_idx, $form->{"src_bin_id_$row"});
218 $form->show_generic_error($locale->text("The selected bin does not exist.")) if (-1 == $bin_idx);
219 my $bin = $warehouse->{BINS}->[$bin_idx];
221 my $orig_qty = $form->{"qty_$row"} . " " . $form->{"unit_$row"};
224 'src_warehouse_id' => $form->{warehouse_id},
225 'transfer_type_id' => $form->{transfer_type_id},
228 map { $transfer->{$_} = $form->{"${_}_${row}"} } qw(src_bin_id chargenumber parts_id qty dst_warehouse_id dst_bin_id);
232 foreach (@contents) {
233 if (($_->{binid} == $transfer->{src_bin_id}) && ($_->{parts_id} == $transfer->{parts_id}) && ($_->{chargenumber} eq $transfer->{chargenumber})) {
240 $form->error($locale->text("There is not enough left of '#1' in bin '#2' for the removal of #3.",
241 $form->{"partdescription_$row"}, $bin->{description}, $orig_qty));
244 $transfer->{qty} = $form->parse_amount(\%myconfig, $transfer->{qty}) * $all_units->{$form->{"unit_$row"}}->{factor};
245 $transfer->{qty} /= $all_units->{$entry->{partunit}}->{factor} || 1;
247 if (($entry->{qty} < $transfer->{qty}) || (0 >= $transfer->{qty})) {
248 $form->error($locale->text("There is not enough left of '#1' in bin '#2' for the removal of #3.",
249 $form->{"partdescription_$row"}, $bin->{description}, $orig_qty));
252 $transfer->{comment} = $form->{comment};
254 push @transfers, $transfer;
256 $entry->{qty} -= $transfer->{qty};
259 if (!scalar @transfers) {
260 $form->show_generic_information($locale->text('Nothing has been selected for transfer.'));
264 WH->transfer(@transfers);
266 $form->{trans_type} = 'transfer';
267 $form->{saved_message} = $locale->text('The parts have been transferred.');
269 transfer_warehouse_selection();
271 $lxdebug->leave_sub();
274 # --------------------------------------------------------------------
276 # --------------------------------------------------------------------
278 sub transfer_stock_update_part {
279 $lxdebug->enter_sub();
281 $form->{trans_type} = 'stock';
282 $form->{qty} = $form->parse_amount(\%myconfig, $form->{qty});
284 if (!$form->{partnumber} && !$form->{description} && !$form->{ean}) {
285 delete @{$form}{qw(parts_id partunit ean)};
286 transfer_warehouse_selection();
288 } elsif (($form->{partnumber} && ($form->{partnumber} ne $form->{old_partnumber})) || $form->{description} || $form->{ean}) {
290 $form->{no_services} = 1;
291 $form->{no_assemblies} = 0; # assemblies duerfen eingelagert werden (z.B. bei retouren)
293 my $parts = Common->retrieve_parts(\%myconfig, $form, 'description', 1);
295 if (!scalar @{ $parts }) {
296 new_item(action => "transfer_stock_update_part");
297 } elsif (scalar @{ $parts } == 1) {
298 @{$form}{qw(parts_id partnumber description ean)} = @{$parts->[0]}{qw(id partnumber description ean)};
299 transfer_stock_get_partunit();
300 transfer_warehouse_selection();
303 select_part('transfer_stock_part_selected', @{ $parts });
307 transfer_stock_get_partunit();
308 transfer_warehouse_selection();
311 $lxdebug->leave_sub();
314 # --------------------------------------------------------------------
315 # Transfer: assemblies
316 # Dies ist die Auswahlmaske für ein assembly.
317 # Die ist einfach von transfer_assembly_update_part kopiert und nur um den trans_type (assembly) korrigiert worden
318 # Es wäre schön, hier nochmal check_assembly_max_create auf, um die max. Fertigungszahl herauszufinden.
319 # Ich lass das mal als auskommentierte Idee bestehen jb 18.3.09
320 # --------------------------------------------------------------------
322 sub transfer_assembly_update_part {
323 $lxdebug->enter_sub();
325 $form->{trans_type} = 'assembly';
326 $form->{qty} = $form->parse_amount(\%myconfig, $form->{qty});
328 if (!$form->{partnumber} && !$form->{description}) {
329 delete @{$form}{qw(parts_id partunit)};
330 transfer_warehouse_selection();
332 } elsif (($form->{partnumber} && ($form->{partnumber} ne $form->{old_partnumber})) || $form->{description}) {
333 $form->{assemblies} = 1;
334 $form->{no_assemblies} = 0;
335 my $parts = Common->retrieve_parts(\%myconfig, $form, 'description', 1);
336 if (scalar @{ $parts } == 1) {
337 @{$form}{qw(parts_id partnumber description)} = @{$parts->[0]}{qw(id partnumber description)};
338 transfer_stock_get_partunit();
339 transfer_warehouse_selection();
341 select_part('transfer_stock_part_selected', @{ $parts });
345 transfer_stock_get_partunit();
346 transfer_warehouse_selection();
349 # hier die oben benannte idee
350 # my $maxcreate = Common->check_assembly_max_create(assembly_id => $form->{parts_id}, dbh => $my_dbh);
351 $lxdebug->leave_sub();
353 sub transfer_stock_part_selected {
354 $lxdebug->enter_sub();
358 @{$form}{qw(parts_id partnumber description ean)} = @{$part}{qw(id partnumber description ean)};
360 transfer_stock_get_partunit();
361 transfer_warehouse_selection();
363 $lxdebug->leave_sub();
366 sub transfer_stock_get_partunit {
367 $lxdebug->enter_sub();
369 if ($form->{parts_id}) {
370 my $part_info = IC->get_basic_part_info('id' => $form->{parts_id});
371 $form->{partunit} = $part_info->{unit};
374 $lxdebug->leave_sub();
377 # vorüberlegung jb 22.2.2009
378 # wir benötigen für diese funktion, die anzahl die vom erzeugnis hergestellt werden soll. vielleicht direkt per js fehleingaben verhindern?
379 # ferner dann nochmal mit check_asssembly_max_create gegenprüfen und dann transaktionssicher wegbuchen.
380 # 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
381 # Manko ist derzeit noch, dass unterschiedliche Lagerplätze, bzw. das Quelllager an sich nicht ausgewählt werden können.
382 # Laut Absprache in KW11 09 übernimmt mb hier den rest im April ... jb 18.3.09
384 sub create_assembly {
385 $lxdebug->enter_sub();
387 $form->{qty} = $form->parse_amount(\%myconfig, $form->{qty});
388 if ($form->{qty} <= 0) {
389 $form->show_generic_error($locale->text('Invalid quantity.'), 'back_button' => 1);
391 # TODO Es wäre schön, hier schon die maximale Anzahl der zu fertigenden Erzeugnisse zu haben
392 #else { if ($form->{qty} > $maxcreate) { #s.o.
393 # $form->show_generic_error($locale->text('Can not create that quantity with current stock'), 'back_button' => 1);
394 # $form->show_generic_error('Maximale Stückzahl' . $maxcreate , 'back_button' => 1);
398 if (!$form->{warehouse_id} || !$form->{bin_id}) {
399 $form->error($locale->text('The warehouse or the bin is missing.'));
401 # 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
402 # Anm. jb 18.3. vielleicht auch nur meine unwissenheit in perl-datenstrukturen
404 'transfer_type' => 'assembly',
405 'login' => $form->{login},
406 'dst_warehouse_id' => $form->{warehouse_id},
407 'dst_bin_id' => $form->{bin_id},
408 'chargenumber' => $form->{chargenumber},
409 'assembly_id' => $form->{parts_id},
410 'qty' => $form->{qty},
411 'unit' => $form->{unit},
412 'comment' => $form->{comment}
415 my $ret = WH->transfer_assembly (%TRANSFER);
416 # Frage: Ich pack in den return-wert auch gleich die Fehlermeldung. Irgendwelche Nummern als Fehlerkonstanten definieren find ich auch nicht besonders schick...
419 # 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
420 $form->show_generic_error($ret, 'back_button' => 1);
423 delete @{$form}{qw(parts_id partnumber description qty unit chargenumber comment)};
425 $form->{saved_message} = $locale->text('The assembly has been created.');
426 $form->{trans_type} = 'assembly';
428 transfer_warehouse_selection();
430 $lxdebug->leave_sub();
434 $lxdebug->enter_sub();
436 $form->{qty} = $form->parse_amount(\%myconfig, $form->{qty});
438 if ($form->{qty} <= 0) {
439 $form->show_generic_error($locale->text('Invalid quantity.'), 'back_button' => 1);
442 if (!$form->{warehouse_id} || !$form->{bin_id}) {
443 $form->error($locale->text('The warehouse or the bin is missing.'));
447 'transfer_type' => 'stock',
448 'dst_warehouse_id' => $form->{warehouse_id},
449 'dst_bin_id' => $form->{bin_id},
450 'chargenumber' => $form->{chargenumber},
451 'parts_id' => $form->{parts_id},
452 'qty' => $form->{qty},
453 'unit' => $form->{unit},
454 'comment' => $form->{comment},
457 WH->transfer($transfer);
459 delete @{$form}{qw(parts_id partnumber description qty unit chargenumber comment)};
461 $form->{saved_message} = $locale->text('The parts have been stocked.');
462 $form->{trans_type} = 'stock';
464 transfer_warehouse_selection();
466 $lxdebug->leave_sub();
469 # --------------------------------------------------------------------
471 # --------------------------------------------------------------------
473 sub removal_parts_selection {
474 $lxdebug->enter_sub();
476 $auth->assert('warehouse_management');
478 transfer_or_removal_prepare_contents('direction' => 'out');
480 $form->{title} = $locale->text('Removal');
482 print $form->parse_html_template("wh/removal_parts_selection");
484 $lxdebug->leave_sub();
488 $lxdebug->enter_sub();
490 $auth->assert('warehouse_management');
492 $form->get_lists('warehouses' => { 'key' => 'WAREHOUSES',
493 'bins' => 'BINS', });
495 my $warehouse_idx = get_warehouse_idx($form->{warehouse_id});
496 $form->show_generic_error($locale->text("The selected warehouse does not exist.")) if (-1 == $warehouse_idx);
498 my $warehouse = $form->{WAREHOUSES}->[$warehouse_idx];
500 $form->show_generic_error($locale->text("The warehouse does not contain any bins.")) if (0 == scalar @{ $warehouse->{BINS} });
502 map { $form->{"l_$_"} = 'Y' } qw(parts_id qty warehouseid binid partnumber partdescription bindescription chargenumber partunit);
504 $form->{sort} = 'bindescription';
505 my @contents = WH->get_warehouse_report("warehouse_id" => $form->{warehouse_id});
506 my $all_units = AM->retrieve_units(\%myconfig, $form);
510 foreach my $row (1 .. $form->{rowcount}) {
511 $form->{"qty_$row"} =~ s/^\s*//;
512 $form->{"qty_$row"} =~ s/\s*$//;
513 next if (!$form->{"qty_$row"});
515 my $bin_idx = get_bin_idx($warehouse_idx, $form->{"src_bin_id_$row"});
516 $form->show_generic_error($locale->text("The selected bin does not exist.")) if (-1 == $bin_idx);
517 my $bin = $warehouse->{BINS}->[$bin_idx];
519 my $orig_qty = $form->{"qty_$row"} . " " . $form->{"unit_$row"};
522 'src_warehouse_id' => $form->{warehouse_id},
523 'transfer_type_id' => $form->{transfer_type_id},
526 map { $transfer->{$_} = $form->{"${_}_${row}"} } qw(src_bin_id chargenumber parts_id qty);
530 foreach (@contents) {
531 if (($_->{binid} == $transfer->{src_bin_id}) && ($_->{parts_id} == $transfer->{parts_id}) && ($_->{chargenumber} eq $transfer->{chargenumber})) {
538 $form->error($locale->text("There is not enough left of '#1' in bin '#2' for the removal of #3.",
539 $form->{"partdescription_$row"}, $bin->{description}, $orig_qty));
542 $transfer->{qty} = $form->parse_amount(\%myconfig, $transfer->{qty}) * $all_units->{$form->{"unit_$row"}}->{factor};
543 $transfer->{qty} /= $all_units->{$entry->{partunit}}->{factor} || 1;
545 if (($entry->{qty} < $transfer->{qty}) || (0 >= $transfer->{qty})) {
546 $form->error($locale->text("There is not enough left of '#1' in bin '#2' for the removal of #3.",
547 $form->{"partdescription_$row"}, $bin->{description}, $orig_qty));
550 $transfer->{comment} = $form->{comment};
552 push @transfers, $transfer;
554 $entry->{qty} -= $transfer->{qty};
557 if (!scalar @transfers) {
558 $form->show_generic_information($locale->text('Nothing has been selected for removal.'));
562 WH->transfer(@transfers);
564 $form->{trans_type} = 'removal';
565 $form->{saved_message} = $locale->text('The parts have been removed.');
567 transfer_warehouse_selection();
569 $lxdebug->leave_sub();
572 # --------------------------------------------------------------------
574 # --------------------------------------------------------------------
577 $lxdebug->enter_sub();
579 $auth->assert('warehouse_management');
581 $form->get_lists('warehouses' => { 'key' => 'WAREHOUSES',
582 'bins' => 'BINS', });
584 show_no_warehouses_error() if (!scalar @{ $form->{WAREHOUSES} });
586 $form->{jsscript} = 1;
589 print $form->parse_html_template("wh/journal_filter", { "UNITS" => AM->unit_select_data(AM->retrieve_units(\%myconfig, $form)) });
591 $lxdebug->leave_sub();
594 sub generate_journal {
595 $lxdebug->enter_sub();
597 $auth->assert('warehouse_management');
599 $form->{title} = $locale->text("WHJournal");
600 $form->{sort} ||= 'date';
603 my @columns = qw(trans_id date warehouse_from bin_from warehouse_to bin_to partnumber partdescription chargenumber trans_type comment qty employee oe_id projectnumber);
606 map { $filter{$_} = $form->{$_} if ($form->{$_}) } qw(warehouse_id bin_id partnumber description chargenumber);
608 $filter{qty_op} = WH->convert_qty_op($form->{qty_op});
609 if ($filter{qty_op}) {
610 $form->isblank(qty, $locale->text('Quantity missing.'));
611 $form->isblank(qty_unit, $locale->text('Unit missing.'));
613 $filter{qty} = $form->{qty};
614 $filter{qty_unit} = $form->{qty_unit};
618 my $report = SL::ReportGenerator->new(\%myconfig, $form);
620 my @hidden_variables = map { "l_${_}" } @columns;
621 push @hidden_variables, qw(warehouse_id bin_id partnumber description chargenumber qty_op qty qty_unit fromdate todate);
624 'date' => { 'text' => $locale->text('Date'), },
625 'trans_id' => { 'text' => $locale->text('Trans Id'), },
626 'trans_type' => { 'text' => $locale->text('Trans Type'), },
627 'comment' => { 'text' => $locale->text('Comment'), },
628 'warehouse_from' => { 'text' => $locale->text('Warehouse From'), },
629 'warehouse_to' => { 'text' => $locale->text('Warehouse To'), },
630 'bin_from' => { 'text' => $locale->text('Bin From'), },
631 'bin_to' => { 'text' => $locale->text('Bin To'), },
632 'partnumber' => { 'text' => $locale->text('Part Number'), },
633 'partdescription' => { 'text' => $locale->text('Description'), },
634 'chargenumber' => { 'text' => $locale->text('Charge Number'), },
635 'qty' => { 'text' => $locale->text('Qty'), },
636 'employee' => { 'text' => $locale->text('Employee'), },
637 'projectnumber' => { 'text' => $locale->text('Project Number'), },
638 'oe_id' => { 'text' => $locale->text('Document'), },
641 my $href = build_std_url('action=generate_journal', grep { $form->{$_} } @hidden_variables);
642 map { $column_defs{$_}->{link} = $href . "&sort=${_}&order=" . Q($_ eq $form->{sort} ? 1 - $form->{order} : $form->{order}) } @columns;
644 my %column_alignment = map { $_ => 'right' } qw(qty);
646 map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
648 $report->set_columns(%column_defs);
649 $report->set_column_order(@columns);
651 $report->set_export_options('generate_journal', @hidden_variables, qw(sort order));
653 $report->set_sort_indicator($form->{sort}, $form->{order});
655 $report->set_options('output_format' => 'HTML',
656 'title' => $form->{title},
657 'attachment_basename' => strftime($locale->text('warehouse_journal_list') . '_%Y%m%d', localtime time));
658 $report->set_options_from_form();
660 my $all_units = AM->retrieve_units(\%myconfig, $form);
661 my @contents = WH->get_warehouse_journal(%filter);
663 my %doc_types = ( 'sales_quotation' => { script => 'oe', title => $locale->text('Sales quotation') },
664 'sales_order' => { script => 'oe', title => $locale->text('Sales Order') },
665 'request_quotation' => { script => 'oe', title => $locale->text('Request quotation') },
666 'purchase_order' => { script => 'oe', title => $locale->text('Purchase Order') },
667 'sales_delivery_order' => { script => 'do', title => $locale->text('Sales delivery order') },
668 'purchase_delivery_order' => { script => 'do', title => $locale->text('Purchase delivery order') },
669 'sales_invoice' => { script => 'is', title => $locale->text('Sales Invoice') },
670 'purchase_invoice' => { script => 'ir', title => $locale->text('Purchase Invoice') },
673 foreach $entry (@contents) {
674 $entry->{qty} = $form->format_amount_units('amount' => $entry->{qty},
675 'part_unit' => $entry->{partunit},
676 'conv_units' => 'convertible');
677 $entry->{trans_type} = $locale->text($entry->{trans_type});
681 foreach my $column (@columns) {
682 next if ($column eq 'trans_type');
685 'data' => $entry->{$column},
686 'align' => $column_alignment{$column},
690 $row->{trans_type} = {
691 'raw_data' => $entry->{trans_type},
692 'align' => $column_alignment{trans_type},
695 if ($form->{l_oe_id}) {
696 $row->{oe_id}->{data} = '';
697 my $info = $entry->{oe_id_info};
699 if ($info && $info->{id} && $info->{type} && $doc_types{$info->{type}}) {
700 $row->{oe_id} = { data => $doc_types{ $info->{type} }->{title} . ' ' . $info->{number},
701 link => build_std_url('script=' . $doc_types{ $info->{type} }->{script} . '.pl', 'action=edit', 'id=' . $info->{id}, 'type=' . $info->{type}) };
705 $report->add_data($row);
708 $report->generate_with_headers();
710 $lxdebug->leave_sub();
713 # --------------------------------------------------------------------
715 # --------------------------------------------------------------------
718 $lxdebug->enter_sub();
720 $auth->assert('warehouse_contents | warehouse_management');
722 $form->get_lists('warehouses' => { 'key' => 'WAREHOUSES',
723 'bins' => 'BINS', });
725 show_no_warehouses_error() if (!scalar @{ $form->{WAREHOUSES} });
727 $form->{fokus} = "partnumber";
728 $form->{onload} .= "focus();";
729 $form->{title} = $locale->text("Report about wareouse contents");
732 print $form->parse_html_template("wh/report_filter",
733 { "nextsub" => "generate_report",
734 "WAREHOUSES" => $form->{WAREHOUSES},
735 "UNITS" => AM->unit_select_data(AM->retrieve_units(\%myconfig, $form)) });
737 $lxdebug->leave_sub();
740 sub generate_report {
741 $lxdebug->enter_sub();
743 $auth->assert('warehouse_contents | warehouse_management');
745 $form->{title} = $locale->text("Report about wareouse contents");
746 $form->{sort} ||= 'partnumber';
747 my $sort_col = $form->{sort};
750 my @columns = qw(warehousedescription bindescription partnumber partdescription chargenumber qty stock_value);
753 map { $filter{$_} = $form->{$_} if ($form->{$_}) } qw(warehouse_id bin_id partnumber description chargenumber);
755 $filter{qty_op} = WH->convert_qty_op($form->{qty_op});
756 if ($filter{qty_op}) {
757 $form->isblank(qty, $locale->text('Quantity missing.'));
758 $form->isblank(qty_unit, $locale->text('Unit missing.'));
760 $filter{qty} = $form->{qty};
761 $filter{qty_unit} = $form->{qty_unit};
765 $form->{subtotal} = '' if (!first { $_ eq $sort_col } qw(partnumber partdescription));
767 my $report = SL::ReportGenerator->new(\%myconfig, $form);
769 my @hidden_variables = map { "l_${_}" } @columns;
770 push @hidden_variables, qw(warehouse_id bin_id partnumber description chargenumber qty_op qty qty_unit l_warehousedescription l_bindescription);
771 push @hidden_variables, qw(include_empty_bins subtotal);
774 'warehousedescription' => { 'text' => $locale->text('Warehouse'), },
775 'bindescription' => { 'text' => $locale->text('Bin'), },
776 'partnumber' => { 'text' => $locale->text('Part Number'), },
777 'partdescription' => { 'text' => $locale->text('Description'), },
778 'chargenumber' => { 'text' => $locale->text('Charge Number'), },
779 'qty' => { 'text' => $locale->text('Qty'), },
780 'stock_value' => { 'text' => $locale->text('Stock value'), },
783 my $href = build_std_url('action=generate_report', grep { $form->{$_} } @hidden_variables);
784 map { $column_defs{$_}->{link} = $href . "&sort=${_}&order=" . Q($_ eq $sort_col ? 1 - $form->{order} : $form->{order}) } @columns;
786 my %column_alignment = map { $_ => 'right' } qw(qty stock_value);
788 map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
790 $report->set_columns(%column_defs);
791 $report->set_column_order(@columns);
793 $report->set_export_options('generate_report', @hidden_variables, qw(sort order));
795 $report->set_sort_indicator($sort_col, $form->{order});
797 $report->set_options('output_format' => 'HTML',
798 'title' => $form->{title},
799 'attachment_basename' => strftime($locale->text('warehouse_report_list') . '_%Y%m%d', localtime time));
800 $report->set_options_from_form();
802 my $all_units = AM->retrieve_units(\%myconfig, $form);
803 my @contents = WH->get_warehouse_report(%filter);
807 my @subtotals_columns = qw(qty stock_value);
808 my %subtotals = map { $_ => 0 } @subtotals_columns;
810 my $total_stock_value = 0;
812 foreach $entry (@contents) {
813 map { $subtotals{$_} += $entry->{$_} } @subtotals_columns;
814 $total_stock_value += $entry->{stock_value} * 1;
816 $entry->{qty} = $form->format_amount_units('amount' => $entry->{qty},
817 'part_unit' => $entry->{partunit},
818 'conv_units' => 'convertible');
819 $entry->{stock_value} = $form->format_amount(\%myconfig, $entry->{stock_value} * 1, 2);
821 $row_set = [ { map { $_ => { 'data' => $entry->{$_}, 'align' => $column_alignment{$_} } } @columns } ];
823 if (($form->{subtotal} eq 'Y')
824 && (($idx == (scalar @contents - 1))
825 || ($entry->{$sort_col} ne $contents[$idx + 1]->{$sort_col}))) {
827 my $row = { map { $_ => { 'data' => '', 'class' => 'listsubtotal', 'align' => $column_alignment{$_}, } } @columns };
828 $row->{qty}->{data} = $form->format_amount_units('amount' => $subtotals{qty} * 1,
829 'part_unit' => $entry->{partunit},
830 'conv_units' => 'convertible');
831 $row->{stock_value}->{data} = $form->format_amount(\%myconfig, $subtotals{stock_value} * 1, 2);
833 %subtotals = map { $_ => 0 } @subtotals_columns;
835 push @{ $row_set }, $row;
838 $report->add_data($row_set);
843 if ($column_defs{stock_value}->{visible}) {
844 $report->add_separator();
846 my $row = { map { $_ => { 'data' => '', 'class' => 'listsubtotal', } } @columns };
848 my $left_col = first { $column_defs{$_}->{visible} } @columns;
850 $row->{$left_col}->{data} = $locale->text('Total stock value');
851 $row->{stock_value}->{data} = $form->format_amount(\%myconfig, $total_stock_value, 2);
852 $row->{stock_value}->{align} = 'right';
854 $report->add_data($row);
857 $report->generate_with_headers();
859 $lxdebug->leave_sub();
862 # --------------------------------------------------------------------
864 # --------------------------------------------------------------------
866 sub show_no_warehouses_error {
867 $lxdebug->enter_sub();
869 my $msg = $locale->text('No warehouse has been created yet or the quantity of the bins is not configured yet.') . ' ';
871 if ($auth->check_right($form->{login}, 'config')) {
872 $msg .= $locale->text('You can create warehouses and bins via the menu "System -> Warehouses".');
874 $msg .= $locale->text('Please ask your administrator to create warehouses and bins.');
877 $form->show_generic_error($msg);
879 $lxdebug->leave_sub();
882 sub get_warehouse_idx {
883 my ($warehouse_id) = @_;
885 for (my $i = 0; $i < scalar @{$form->{WAREHOUSES}}; $i++) {
886 return $i if ($form->{WAREHOUSES}->[$i]->{id} == $warehouse_id);
893 my ($warehouse_index, $bin_id) = @_;
895 my $warehouse = $form->{WAREHOUSES}->[$warehouse_index];
897 return -1 if (!$warehouse);
899 for (my $i = 0; $i < scalar @{ $warehouse->{BINS} }; $i++) {
900 return $i if ($warehouse->{BINS}->[$i]->{id} == $bin_id);
908 call new item dialogue from warehouse masks.
911 action => name of sub to be called when new item is done
915 $main::lxdebug->enter_sub();
919 $form->{old_callback} = $form->escape($form->{callback}, 1);
920 $form->{callback} = $form->escape("$form->{script}?action=$params{action}", 1);
922 # save all form variables except action in a previousform variable
923 my $previousform = join '&', map { my $value = $form->{$_}; $value =~ s/&/%26/; "$_=$value" } grep { !/action/ } keys %$form;
925 # push @HIDDENS, { 'name' => 'previousform', 'value' => $form->escape($previousform, 1) };
926 push @HIDDENS, map +{ 'name' => $_, 'value' => $form->{$_} }, qw(partnumber description unit vc sellprice ean);
927 push @HIDDENS, { 'name' => 'taxaccount2', 'value' => $form->{taxaccounts} };
928 push @HIDDENS, { 'name' => 'notes', 'value' => $form->{longdescription} };
931 print $form->parse_html_template("generic/new_item", { HIDDENS => [ sort { $a->{name} cmp $b->{name} } @HIDDENS ] } );
933 $main::lxdebug->leave_sub();
937 call_sub($form->{update_nextsub} || $form->{nextsub});
941 call_sub($form->{continue_nextsub} || $form->{nextsub});
945 call_sub($form->{stock_nextsub} || $form->{nextsub});