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('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);
 
 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                                            "description"  => $form->{description});
 
 162   $form->show_generic_error($locale->text("The selected warehouse is empty.")) if (0 == scalar(@contents));
 
 164   my $all_units = AM->retrieve_units(\%myconfig, $form);
 
 166   foreach (@contents) {
 
 167     $_->{qty} = $form->format_amount_units('amount'     => $_->{qty},
 
 168                                            'part_unit'  => $_->{partunit},
 
 169                                            'conv_units' => 'convertible');
 
 170     my $this_unit = $_->{partunit};
 
 172     if ($all_units->{$_->{partunit}} && ($all_units->{g}->{base_unit} eq $all_units->{$_->{partunit}}->{base_unit})) {
 
 176     $_->{UNITS} = AM->unit_select_data($all_units, $this_unit, 0, $_->{partunit});
 
 179   my $transfer_types = WH->retrieve_transfer_types($args{direction});
 
 180   map { $_->{description} = $locale->text($_->{description}) } @{ $transfer_types };
 
 182   $form->{CONTENTS}       = \@contents;
 
 183   $form->{TRANSFER_TYPES} = $transfer_types;
 
 185   $lxdebug->leave_sub();
 
 190   $lxdebug->enter_sub();
 
 192   $auth->assert('warehouse_management');
 
 194   $form->get_lists('warehouses' => { 'key' => 'WAREHOUSES', 'bins' => 'BINS' });
 
 196   my $warehouse_idx = get_warehouse_idx($form->{warehouse_id});
 
 197   $form->show_generic_error($locale->text("The selected warehouse does not exist.")) if (-1 == $warehouse_idx);
 
 199   my $warehouse = $form->{WAREHOUSES}->[$warehouse_idx];
 
 201   $form->show_generic_error($locale->text("The source warehouse does not contain any bins.")) if (0 == scalar @{ $warehouse->{BINS} });
 
 203   map { $form->{"l_$_"} = 'Y' } qw(parts_id qty warehouseid binid partnumber partdescription bindescription chargenumber partunit);
 
 205   $form->{sort} = 'bindescription';
 
 206   my @contents  = WH->get_warehouse_report("warehouse_id" => $form->{warehouse_id});
 
 207   my $all_units = AM->retrieve_units(\%myconfig, $form);
 
 211   foreach my $row (1 .. $form->{rowcount}) {
 
 212     $form->{"qty_$row"} =~ s/^\s*//;
 
 213     $form->{"qty_$row"} =~ s/\s*$//;
 
 214     next if (!$form->{"qty_$row"});
 
 216     my $bin_idx = get_bin_idx($warehouse_idx, $form->{"src_bin_id_$row"});
 
 217     $form->show_generic_error($locale->text("The selected bin does not exist.")) if (-1 == $bin_idx);
 
 218     my $bin     = $warehouse->{BINS}->[$bin_idx];
 
 220     my $orig_qty = $form->{"qty_$row"} . " " . $form->{"unit_$row"};
 
 223       'src_warehouse_id' => $form->{warehouse_id},
 
 224       'transfer_type_id' => $form->{transfer_type_id},
 
 227     map { $transfer->{$_} = $form->{"${_}_${row}"} } qw(src_bin_id chargenumber parts_id qty dst_warehouse_id dst_bin_id);
 
 231     foreach (@contents) {
 
 232       if (($_->{binid} == $transfer->{src_bin_id}) && ($_->{parts_id} == $transfer->{parts_id}) && ($_->{chargenumber} eq $transfer->{chargenumber})) {
 
 239       $form->error($locale->text("There is not enough left of '#1' in bin '#2' for the removal of #3.",
 
 240                                  $form->{"partdescription_$row"}, $bin->{description}, $orig_qty));
 
 243     $transfer->{qty}  = $form->parse_amount(\%myconfig, $transfer->{qty}) * $all_units->{$form->{"unit_$row"}}->{factor};
 
 244     $transfer->{qty} /= $all_units->{$entry->{partunit}}->{factor} || 1;
 
 246     if (($entry->{qty} < $transfer->{qty}) || (0 >= $transfer->{qty})) {
 
 247       $form->error($locale->text("There is not enough left of '#1' in bin '#2' for the removal of #3.",
 
 248                                  $form->{"partdescription_$row"}, $bin->{description}, $orig_qty));
 
 251     $transfer->{comment} = $form->{comment};
 
 253     push @transfers, $transfer;
 
 255     $entry->{qty} -= $transfer->{qty};
 
 258   if (!scalar @transfers) {
 
 259     $form->show_generic_information($locale->text('Nothing has been selected for transfer.'));
 
 263   WH->transfer(@transfers);
 
 265   $form->{trans_type}    = 'transfer';
 
 266   $form->{saved_message} = $locale->text('The parts have been transferred.');
 
 268   transfer_warehouse_selection();
 
 270   $lxdebug->leave_sub();
 
 273 # --------------------------------------------------------------------
 
 275 # --------------------------------------------------------------------
 
 277 sub transfer_stock_update_part {
 
 278   $lxdebug->enter_sub();
 
 280   $form->{trans_type} = 'stock';
 
 281   $form->{qty}        = $form->parse_amount(\%myconfig, $form->{qty});
 
 283   if (!$form->{partnumber} && !$form->{description}) {
 
 284     delete @{$form}{qw(parts_id partunit)};
 
 285     transfer_warehouse_selection();
 
 287   } elsif (($form->{partnumber} && ($form->{partnumber} ne $form->{old_partnumber})) || $form->{description}) {
 
 289     $form->{no_services}   = 1;
 
 290     $form->{no_assemblies} = 1;
 
 292     my $parts = Common->retrieve_parts(\%myconfig, $form, 'description', 1);
 
 294     if (scalar @{ $parts } == 1) {
 
 295       @{$form}{qw(parts_id partnumber description)} = @{$parts->[0]}{qw(id partnumber description)};
 
 296       transfer_stock_get_partunit();
 
 297       transfer_warehouse_selection();
 
 300       select_part('transfer_stock_part_selected', @{ $parts });
 
 304     transfer_stock_get_partunit();
 
 305     transfer_warehouse_selection();
 
 308   $lxdebug->leave_sub();
 
 311 # --------------------------------------------------------------------
 
 312 # Transfer: assemblies
 
 313 # Dies ist die Auswahlmaske für ein assembly. 
 
 314 # Die ist einfach von transfer_assembly_update_part kopiert und nur um den trans_type (assembly) korrigiert worden
 
 315 # Es wäre schön, hier nochmal check_assembly_max_create auf, um die max. Fertigungszahl herauszufinden.
 
 316 # Ich lass das mal als auskommentierte Idee bestehen jb 18.3.09
 
 317 # --------------------------------------------------------------------
 
 319 sub transfer_assembly_update_part {
 
 320   $lxdebug->enter_sub();
 
 322   $form->{trans_type} = 'assembly';
 
 323   $form->{qty}        = $form->parse_amount(\%myconfig, $form->{qty});
 
 325   if (!$form->{partnumber} && !$form->{description}) {
 
 326     delete @{$form}{qw(parts_id partunit)};
 
 327     transfer_warehouse_selection();
 
 329   } elsif (($form->{partnumber} && ($form->{partnumber} ne $form->{old_partnumber})) || $form->{description}) {
 
 330     $form->{assemblies} = 1;
 
 331     $form->{no_assemblies} = 0;
 
 332     my $parts = Common->retrieve_parts(\%myconfig, $form, 'description', 1);
 
 333     if (scalar @{ $parts } == 1) {
 
 334       @{$form}{qw(parts_id partnumber description)} = @{$parts->[0]}{qw(id partnumber description)};
 
 335       transfer_stock_get_partunit();
 
 336       transfer_warehouse_selection();
 
 338       select_part('transfer_stock_part_selected', @{ $parts });
 
 342     transfer_stock_get_partunit();
 
 343     transfer_warehouse_selection();
 
 346 # hier die oben benannte idee
 
 347 #    my $maxcreate = Common->check_assembly_max_create(assembly_id => $form->{parts_id}, dbh => $my_dbh); 
 
 348   $lxdebug->leave_sub();
 
 350 sub transfer_stock_part_selected {
 
 351   $lxdebug->enter_sub();
 
 355   @{$form}{qw(parts_id partnumber description)} = @{$part}{qw(id partnumber description)};
 
 357   transfer_stock_get_partunit();
 
 358   transfer_warehouse_selection();
 
 360   $lxdebug->leave_sub();
 
 363 sub transfer_stock_get_partunit {
 
 364   $lxdebug->enter_sub();
 
 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   $lxdebug->leave_sub();
 
 374 # vorüberlegung jb 22.2.2009
 
 375 # wir benötigen für diese funktion, die anzahl die vom erzeugnis hergestellt werden soll. vielleicht direkt per js fehleingaben verhindern?
 
 376 # ferner dann nochmal mit check_asssembly_max_create gegenprüfen und dann transaktionssicher wegbuchen.
 
 377 # wir brauchen eine hilfsfunktion, die nee. brauchen wir nicht. der algorithmus läuft genau wie bei check max_create, nur dass hier auch eine lagerbewegung (verbraucht) stattfindet
 
 378 # Manko ist derzeit noch, dass unterschiedliche Lagerplätze, bzw. das Quelllager an sich nicht ausgewählt werden können.
 
 379 # Laut Absprache in KW11 09 Ã¼bernimmt mb hier den rest im April ... jb 18.3.09
 
 380 sub create_assembly {
 
 381 #  my $maxcreate=shift; # oben begonnene auskommentierte idee, hier als motiv weiterverfolgen (umkehrungen und sequenzierungen als stilmittel nicht vergessen)
 
 382   $lxdebug->enter_sub();
 
 384   $form->{qty} = $form->parse_amount(\%myconfig, $form->{qty});
 
 385 #  my $maxcreate = WH->check_assembly_max_create(assembly_id    =>      $form->{parts_id});
 
 386   if ($form->{qty} <= 0) {
 
 387     $form->show_generic_error($locale->text('Invalid quantity.'), 'back_button' => 1);
 
 388   } #else { if ($form->{qty} > $maxcreate) {    #s.o.
 
 389 #           $form->show_generic_error($locale->text('Can not create that quantity with current stock'), 'back_button' => 1);
 
 390 #           $form->show_generic_error('Maximale Stückzahl' . $maxcreate , 'back_button' => 1);
 
 394   if (!$form->{warehouse_id} || !$form->{bin_id}) {
 
 395     $form->error($locale->text('The warehouse or the bin is missing.'));
 
 397 # 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
 
 398 # Anm. jb 18.3. vielleicht auch nur meine unwissenheit in perl-datenstrukturen
 
 400     'transfer_type'    => 'assembly',
 
 401     'login'     => $form->{login},
 
 402     'dst_warehouse_id' => $form->{warehouse_id},
 
 403     'dst_bin_id'       => $form->{bin_id},
 
 404     'chargenumber'     => $form->{chargenumber},
 
 405     'assembly_id'         => $form->{parts_id},
 
 406     'qty'              => $form->{qty},
 
 407     'unit'             => $form->{unit},
 
 408     'comment'          => $form->{comment},
 
 411   my $ret = WH->transfer_assembly (%TRANSFER);
 
 412 # Frage: Ich pack in den return-wert auch gleich die Fehlermeldung. Irgendwelche Nummern als Fehlerkonstanten definieren find ich auch nicht besonders schick...
 
 415     $form->show_generic_error($locale->text($ret), 'back_button' => 1);
 
 418   delete @{$form}{qw(parts_id partnumber description qty unit chargenumber comment)};
 
 420   $form->{saved_message} = $locale->text('The assembly has been created.');
 
 421   $form->{trans_type}    = 'assembly';
 
 423   transfer_warehouse_selection();
 
 425   $lxdebug->leave_sub();
 
 429   $lxdebug->enter_sub();
 
 431   $form->{qty} = $form->parse_amount(\%myconfig, $form->{qty});
 
 433   if ($form->{qty} <= 0) {
 
 434     $form->show_generic_error($locale->text('Invalid quantity.'), 'back_button' => 1);
 
 437   if (!$form->{warehouse_id} || !$form->{bin_id}) {
 
 438     $form->error($locale->text('The warehouse or the bin is missing.'));
 
 442     'transfer_type'    => 'stock',
 
 443     'dst_warehouse_id' => $form->{warehouse_id},
 
 444     'dst_bin_id'       => $form->{bin_id},
 
 445     'chargenumber'     => $form->{chargenumber},
 
 446     'parts_id'         => $form->{parts_id},
 
 447     'qty'              => $form->{qty},
 
 448     'unit'             => $form->{unit},
 
 449     'comment'          => $form->{comment},
 
 452   WH->transfer($transfer);
 
 454   delete @{$form}{qw(parts_id partnumber description qty unit chargenumber comment)};
 
 456   $form->{saved_message} = $locale->text('The parts have been stocked.');
 
 457   $form->{trans_type}    = 'stock';
 
 459   transfer_warehouse_selection();
 
 461   $lxdebug->leave_sub();
 
 464 # --------------------------------------------------------------------
 
 466 # --------------------------------------------------------------------
 
 468 sub removal_parts_selection {
 
 469   $lxdebug->enter_sub();
 
 471   $auth->assert('warehouse_management');
 
 473   transfer_or_removal_prepare_contents('direction' => 'out');
 
 475   $form->{title} = $locale->text('Removal');
 
 477   print $form->parse_html_template("wh/removal_parts_selection");
 
 479   $lxdebug->leave_sub();
 
 483   $lxdebug->enter_sub();
 
 485   $auth->assert('warehouse_management');
 
 487   $form->get_lists('warehouses' => { 'key'    => 'WAREHOUSES',
 
 488                                      'bins'   => 'BINS', });
 
 490   my $warehouse_idx = get_warehouse_idx($form->{warehouse_id});
 
 491   $form->show_generic_error($locale->text("The selected warehouse does not exist.")) if (-1 == $warehouse_idx);
 
 493   my $warehouse = $form->{WAREHOUSES}->[$warehouse_idx];
 
 495   $form->show_generic_error($locale->text("The warehouse does not contain any bins.")) if (0 == scalar @{ $warehouse->{BINS} });
 
 497   map { $form->{"l_$_"} = 'Y' } qw(parts_id qty warehouseid binid partnumber partdescription bindescription chargenumber partunit);
 
 499   $form->{sort} = 'bindescription';
 
 500   my @contents  = WH->get_warehouse_report("warehouse_id" => $form->{warehouse_id});
 
 501   my $all_units = AM->retrieve_units(\%myconfig, $form);
 
 505   foreach my $row (1 .. $form->{rowcount}) {
 
 506     $form->{"qty_$row"} =~ s/^\s*//;
 
 507     $form->{"qty_$row"} =~ s/\s*$//;
 
 508     next if (!$form->{"qty_$row"});
 
 510     my $bin_idx = get_bin_idx($warehouse_idx, $form->{"src_bin_id_$row"});
 
 511     $form->show_generic_error($locale->text("The selected bin does not exist.")) if (-1 == $bin_idx);
 
 512     my $bin     = $warehouse->{BINS}->[$bin_idx];
 
 514     my $orig_qty = $form->{"qty_$row"} . " " . $form->{"unit_$row"};
 
 517       'src_warehouse_id' => $form->{warehouse_id},
 
 518       'transfer_type_id' => $form->{transfer_type_id},
 
 521     map { $transfer->{$_} = $form->{"${_}_${row}"} } qw(src_bin_id chargenumber parts_id qty);
 
 525     foreach (@contents) {
 
 526       if (($_->{binid} == $transfer->{src_bin_id}) && ($_->{parts_id} == $transfer->{parts_id}) && ($_->{chargenumber} eq $transfer->{chargenumber})) {
 
 533       $form->error($locale->text("There is not enough left of '#1' in bin '#2' for the removal of #3.",
 
 534                                  $form->{"partdescription_$row"}, $bin->{description}, $orig_qty));
 
 537     $transfer->{qty}  = $form->parse_amount(\%myconfig, $transfer->{qty}) * $all_units->{$form->{"unit_$row"}}->{factor};
 
 538     $transfer->{qty} /= $all_units->{$entry->{partunit}}->{factor} || 1;
 
 540     if (($entry->{qty} < $transfer->{qty}) || (0 >= $transfer->{qty})) {
 
 541       $form->error($locale->text("There is not enough left of '#1' in bin '#2' for the removal of #3.",
 
 542                                  $form->{"partdescription_$row"}, $bin->{description}, $orig_qty));
 
 545     $transfer->{comment} = $form->{comment};
 
 547     push @transfers, $transfer;
 
 549     $entry->{qty} -= $transfer->{qty};
 
 552   if (!scalar @transfers) {
 
 553     $form->show_generic_information($locale->text('Nothing has been selected for removal.'));
 
 557   WH->transfer(@transfers);
 
 559   $form->{trans_type}    = 'removal';
 
 560   $form->{saved_message} = $locale->text('The parts have been removed.');
 
 562   transfer_warehouse_selection();
 
 564   $lxdebug->leave_sub();
 
 567 # --------------------------------------------------------------------
 
 569 # --------------------------------------------------------------------
 
 572   $lxdebug->enter_sub();
 
 574   $auth->assert('warehouse_management');
 
 576   $form->get_lists('warehouses' => { 'key'  => 'WAREHOUSES',
 
 577                                      'bins' => 'BINS', });
 
 579   show_no_warehouses_error() if (!scalar @{ $form->{WAREHOUSES} });
 
 581   $form->{jsscript} = 1;
 
 584   print $form->parse_html_template("wh/journal_filter", { "UNITS" => AM->unit_select_data(AM->retrieve_units(\%myconfig, $form)) });
 
 586   $lxdebug->leave_sub();
 
 589 sub generate_journal {
 
 590   $lxdebug->enter_sub();
 
 592   $auth->assert('warehouse_management');
 
 594   $form->{title}   = $locale->text("WHJournal");
 
 595   $form->{sort}  ||= 'date';
 
 598   my @columns = qw(trans_id date warehouse_from bin_from warehouse_to bin_to partnumber partdescription chargenumber trans_type comment qty employee projectnumber);
 
 601   map { $filter{$_} = $form->{$_} if ($form->{$_}) } qw(warehouse_id bin_id partnumber description chargenumber);
 
 603   $filter{qty_op} = WH->convert_qty_op($form->{qty_op});
 
 604   if ($filter{qty_op}) {
 
 605     $form->isblank(qty,      $locale->text('Quantity missing.'));
 
 606     $form->isblank(qty_unit, $locale->text('Unit missing.'));
 
 608     $filter{qty}      = $form->{qty};
 
 609     $filter{qty_unit} = $form->{qty_unit};
 
 613   my $report = SL::ReportGenerator->new(\%myconfig, $form);
 
 615   my @hidden_variables = map { "l_${_}" } @columns;
 
 616   push @hidden_variables, qw(warehouse_id bin_id partnumber description chargenumber qty_op qty qty_unit fromdate todate);
 
 619     'date'            => { 'text' => $locale->text('Date'), },
 
 620     'trans_id'        => { 'text' => $locale->text('Trans Id'), },
 
 621     'trans_type'      => { 'text' => $locale->text('Trans Type'), },
 
 622     'comment'         => { 'text' => $locale->text('Comment'), },
 
 623     'warehouse_from'  => { 'text' => $locale->text('Warehouse From'), },
 
 624     'warehouse_to'    => { 'text' => $locale->text('Warehouse To'), },
 
 625     'bin_from'        => { 'text' => $locale->text('Bin From'), },
 
 626     'bin_to'          => { 'text' => $locale->text('Bin To'), },
 
 627     'partnumber'      => { 'text' => $locale->text('Part Number'), },
 
 628     'partdescription' => { 'text' => $locale->text('Description'), },
 
 629     'chargenumber'    => { 'text' => $locale->text('Charge Number'), },
 
 630     'qty'             => { 'text' => $locale->text('Qty'), },
 
 631     'employee'        => { 'text' => $locale->text('Employee'), },
 
 632     'projectnumber'   => { 'text' => $locale->text('Project Number'), },
 
 635   my $href = build_std_url('action=generate_journal', grep { $form->{$_} } @hidden_variables);
 
 636   map { $column_defs{$_}->{link} = $href . "&sort=${_}&order=" . Q($_ eq $form->{sort} ? 1 - $form->{order} : $form->{order}) } @columns;
 
 638   my %column_alignment = map { $_ => 'right' } qw(qty);
 
 640   map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
 
 642   $report->set_columns(%column_defs);
 
 643   $report->set_column_order(@columns);
 
 645   $report->set_export_options('generate_journal', @hidden_variables);
 
 647   $report->set_sort_indicator($form->{sort}, $form->{order});
 
 649   $report->set_options('output_format'        => 'HTML',
 
 650                        'title'                => $form->{title},
 
 651                        'attachment_basename'  => strftime('warehouse_journal_%Y%m%d', localtime time));
 
 652   $report->set_options_from_form();
 
 654   my $all_units = AM->retrieve_units(\%myconfig, $form);
 
 655   my @contents  = WH->get_warehouse_journal(%filter);
 
 657   foreach $entry (@contents) {
 
 658     $entry->{qty}        = $form->format_amount_units('amount'     => $entry->{qty},
 
 659                                                       'part_unit'  => $entry->{partunit},
 
 660                                                       'conv_units' => 'convertible');
 
 661     $entry->{trans_type} = $locale->text($entry->{trans_type});
 
 665     foreach my $column (@columns) {
 
 666       next if ($column eq 'trans_type');
 
 669         'data'  => $entry->{$column},
 
 670         'align' => $column_alignment{$column},
 
 674     $row->{trans_type} = {
 
 675       'raw_data' => $entry->{trans_type},
 
 676       'align'    => $column_alignment{trans_type},
 
 679     $report->add_data($row);
 
 682   $report->generate_with_headers();
 
 684   $lxdebug->leave_sub();
 
 687 # --------------------------------------------------------------------
 
 689 # --------------------------------------------------------------------
 
 692   $lxdebug->enter_sub();
 
 694   $auth->assert('warehouse_content | warehouse_management');
 
 696   $form->get_lists('warehouses' => { 'key'    => 'WAREHOUSES',
 
 697                                      'bins'   => 'BINS', });
 
 699   show_no_warehouses_error() if (!scalar @{ $form->{WAREHOUSES} });
 
 701   $form->{fokus}   = "partnumber";
 
 702   $form->{onload} .= "focus();";
 
 703   $form->{title}   = $locale->text("Report about wareouse contents");
 
 706   print $form->parse_html_template("wh/report_filter",
 
 707                                    { "nextsub"    => "generate_report",
 
 708                                      "WAREHOUSES" => $form->{WAREHOUSES},
 
 709                                      "UNITS"      => AM->unit_select_data(AM->retrieve_units(\%myconfig, $form)) });
 
 711   $lxdebug->leave_sub();
 
 714 sub generate_report {
 
 715   $lxdebug->enter_sub();
 
 717   $auth->assert('warehouse_content | warehouse_management');
 
 719   $form->{title}   = $locale->text("Report about wareouse contents");
 
 720   $form->{sort}  ||= 'partnumber';
 
 721   my $sort_col     = $form->{sort};
 
 724   my @columns = qw(warehousedescription bindescription partnumber partdescription chargenumber qty stock_value);
 
 727   map { $filter{$_} = $form->{$_} if ($form->{$_}) } qw(warehouse_id bin_id partnumber description chargenumber);
 
 729   $filter{qty_op} = WH->convert_qty_op($form->{qty_op});
 
 730   if ($filter{qty_op}) {
 
 731     $form->isblank(qty,      $locale->text('Quantity missing.'));
 
 732     $form->isblank(qty_unit, $locale->text('Unit missing.'));
 
 734     $filter{qty}      = $form->{qty};
 
 735     $filter{qty_unit} = $form->{qty_unit};
 
 739   $form->{subtotal} = '' if (!first { $_ eq $sort_col } qw(partnumber partdescription));
 
 741   my $report = SL::ReportGenerator->new(\%myconfig, $form);
 
 743   my @hidden_variables = map { "l_${_}" } @columns;
 
 744   push @hidden_variables, qw(warehouse_id bin_id partnumber description chargenumber qty_op qty qty_unit l_warehousedescription l_bindescription);
 
 745   push @hidden_variables, qw(include_empty_bins subtotal);
 
 748     'warehousedescription' => { 'text' => $locale->text('Warehouse'), },
 
 749     'bindescription'       => { 'text' => $locale->text('Bin'), },
 
 750     'partnumber'           => { 'text' => $locale->text('Part Number'), },
 
 751     'partdescription'      => { 'text' => $locale->text('Description'), },
 
 752     'chargenumber'         => { 'text' => $locale->text('Charge Number'), },
 
 753     'qty'                  => { 'text' => $locale->text('Qty'), },
 
 754     'stock_value'          => { 'text' => $locale->text('Stock value'), },
 
 757   my $href = build_std_url('action=generate_report', grep { $form->{$_} } @hidden_variables);
 
 758   map { $column_defs{$_}->{link} = $href . "&sort=${_}&order=" . Q($_ eq $sort_col ? 1 - $form->{order} : $form->{order}) } @columns;
 
 760   my %column_alignment = map { $_ => 'right' } qw(qty stock_value);
 
 762   map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
 
 764   $report->set_columns(%column_defs);
 
 765   $report->set_column_order(@columns);
 
 767   $report->set_export_options('generate_report', @hidden_variables);
 
 769   $report->set_sort_indicator($sort_col, $form->{order});
 
 771   $report->set_options('output_format'        => 'HTML',
 
 772                        'title'                => $form->{title},
 
 773                        'attachment_basename'  => strftime('warehouse_report_%Y%m%d', localtime time));
 
 774   $report->set_options_from_form();
 
 776   my $all_units = AM->retrieve_units(\%myconfig, $form);
 
 777   my @contents  = WH->get_warehouse_report(%filter);
 
 781   my @subtotals_columns = qw(qty stock_value);
 
 782   my %subtotals         = map { $_ => 0 } @subtotals_columns;
 
 784   my $total_stock_value = 0;
 
 786   foreach $entry (@contents) {
 
 787     map { $subtotals{$_} += $entry->{$_} } @subtotals_columns;
 
 788     $total_stock_value   += $entry->{stock_value} * 1;
 
 790     $entry->{qty}         = $form->format_amount_units('amount'     => $entry->{qty},
 
 791                                                        'part_unit'  => $entry->{partunit},
 
 792                                                        'conv_units' => 'convertible');
 
 793     $entry->{stock_value} = $form->format_amount(\%myconfig, $entry->{stock_value} * 1, 2);
 
 795     $row_set = [ { map { $_ => { 'data' => $entry->{$_}, 'align' => $column_alignment{$_} } } @columns } ];
 
 797     if (($form->{subtotal} eq 'Y')
 
 798         && (($idx == (scalar @contents - 1))
 
 799             || ($entry->{$sort_col} ne $contents[$idx + 1]->{$sort_col}))) {
 
 801       my $row = { map { $_ => { 'data' => '', 'class' => 'listsubtotal', 'align' => $column_alignment{$_}, } } @columns };
 
 802       $row->{qty}->{data}         = $form->format_amount_units('amount'     => $subtotals{qty} * 1,
 
 803                                                                'part_unit'  => $entry->{partunit},
 
 804                                                                'conv_units' => 'convertible');
 
 805       $row->{stock_value}->{data} = $form->format_amount(\%myconfig, $subtotals{stock_value} * 1, 2);
 
 807       %subtotals                  = map { $_ => 0 } @subtotals_columns;
 
 809       push @{ $row_set }, $row;
 
 812     $report->add_data($row_set);
 
 817   if ($column_defs{stock_value}->{visible}) {
 
 818     $report->add_separator();
 
 820     my $row                      = { map { $_ => { 'data' => '', 'class' => 'listsubtotal', } } @columns };
 
 822     my $left_col                 = first { $column_defs{$_}->{visible} } @columns;
 
 824     $row->{$left_col}->{data}    = $locale->text('Total stock value');
 
 825     $row->{stock_value}->{data}  = $form->format_amount(\%myconfig, $total_stock_value, 2);
 
 826     $row->{stock_value}->{align} = 'right';
 
 828     $report->add_data($row);
 
 831   $report->generate_with_headers();
 
 833   $lxdebug->leave_sub();
 
 836 # --------------------------------------------------------------------
 
 838 # --------------------------------------------------------------------
 
 840 sub show_no_warehouses_error {
 
 841   $lxdebug->enter_sub();
 
 843   my $msg = $locale->text('No warehouse has been created yet.') . ' ';
 
 845   if ($auth->check_right($form->{login}, 'config')) {
 
 846     $msg .= $locale->text('You can create warehouses and bins via the menu "System -> Warehouses".');
 
 848     $msg .= $locale->text('Please ask your administrator to create warehouses and bins.');
 
 851   $form->show_generic_error($msg);
 
 853   $lxdebug->leave_sub();
 
 856 sub get_warehouse_idx {
 
 857   my ($warehouse_id) = @_;
 
 859   for (my $i = 0; $i < scalar @{$form->{WAREHOUSES}}; $i++) {
 
 860     return $i if ($form->{WAREHOUSES}->[$i]->{id} == $warehouse_id);
 
 867   my ($warehouse_index, $bin_id) = @_;
 
 869   my $warehouse = $form->{WAREHOUSES}->[$warehouse_index];
 
 871   return -1 if (!$warehouse);
 
 873   for (my $i = 0; $i < scalar @{ $warehouse->{BINS} }; $i++) {
 
 874     return $i if ($warehouse->{BINS}->[$i]->{id} == $bin_id);
 
 881   call_sub($form->{update_nextsub} || $form->{nextsub});
 
 885   call_sub($form->{continue_nextsub} || $form->{nextsub});
 
 889   call_sub($form->{stock_nextsub} || $form->{nextsub});