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;
 
  51 require "bin/mozilla/common.pl";
 
  52 require "bin/mozilla/reportgenerator.pl";
 
  58 # contents of the "transfer_type" table:
 
  59 #  $locale->text('back')
 
  60 #  $locale->text('correction')
 
  61 #  $locale->text('disposed')
 
  62 #  $locale->text('found')
 
  63 #  $locale->text('missing')
 
  64 #  $locale->text('stock')
 
  65 #  $locale->text('shipped')
 
  66 #  $locale->text('transfer')
 
  67 #  $locale->text('used')
 
  68 #  $locale->text('return_material')
 
  69 #  $locale->text('release_material')
 
  71 # --------------------------------------------------------------------
 
  73 # --------------------------------------------------------------------
 
  75 sub transfer_warehouse_selection {
 
  76   $main::lxdebug->enter_sub();
 
  78   $main::auth->assert('warehouse_management');
 
  80   my $form     = $main::form;
 
  81   my %myconfig = %main::myconfig;
 
  82   my $locale   = $main::locale;
 
  84   $form->get_lists('warehouses' => { 'key'    => 'WAREHOUSES',
 
  87   show_no_warehouses_error() if (!scalar @{ $form->{WAREHOUSES} });
 
  89   my $units      = AM->retrieve_units(\%myconfig, $form);
 
  92   if ( $form->{parts_id} ) {
 
  93     $part = SL::DB::Part->new();
 
  94     $part->id($form->{parts_id});
 
  98   # der zweite Parameter von unit_select_data gibt den default-Namen (selected) vor
 
  99   $form->{UNITS} = AM->unit_select_data($units, $form->{unit}, 0, $part ? $part->unit : 0);
 
 101   if (scalar @{ $form->{WAREHOUSES} }) {
 
 102     $form->{warehouse_id} ||= $form->{WAREHOUSES}->[0]->{id};
 
 103     $form->{bin_id}       ||= $form->{WAREHOUSES}->[0]->{BINS}->[0]->{id};
 
 108   $form->{jsscript} = 1;
 
 110   if ($form->{trans_type} eq 'removal') {
 
 111     $form->{nextsub} = "removal_parts_selection";
 
 112     $form->{title}   = $locale->text('Removal from Warehouse');
 
 113     $content         = $form->parse_html_template('wh/warehouse_selection');
 
 115   } elsif ($form->{trans_type} eq 'stock') {
 
 116     $form->{title} = $locale->text('Stock');
 
 117     $content       = $form->parse_html_template('wh/warehouse_selection_stock');
 
 119   } elsif (!$form->{trans_type} || ($form->{trans_type} eq 'transfer')) {
 
 120     $form->{nextsub} = "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     $form->{title} = $locale->text('Produce Assembly');
 
 126     $content       = $form->parse_html_template('wh/warehouse_selection_assembly');
 
 132   $main::lxdebug->leave_sub();
 
 135 sub transfer_parts_selection {
 
 136   $main::lxdebug->enter_sub();
 
 138   $main::auth->assert('warehouse_management');
 
 140   my $form     = $main::form;
 
 141   my %myconfig = %main::myconfig;
 
 142   my $locale   = $main::locale;
 
 144   transfer_or_removal_prepare_contents('direction' => 'transfer');
 
 146   $form->{title} = $locale->text('Transfer');
 
 148   print $form->parse_html_template("wh/transfer_parts_selection");
 
 150   $main::lxdebug->leave_sub();
 
 153 sub transfer_or_removal_prepare_contents {
 
 154   $main::lxdebug->enter_sub();
 
 156   $main::auth->assert('warehouse_management');
 
 160   my $form     = $main::form;
 
 161   my %myconfig = %main::myconfig;
 
 162   my $locale   = $main::locale;
 
 164   $form->get_lists('warehouses' => { 'key'    => 'WAREHOUSES',
 
 165                                      'bins'   => 'BINS', });
 
 167   my $warehouse_idx = get_warehouse_idx($form->{warehouse_id});
 
 168   $form->show_generic_error($locale->text("The selected warehouse does not exist.")) if (-1 == $warehouse_idx);
 
 170   my $warehouse = $form->{WAREHOUSES}->[$warehouse_idx];
 
 172   $form->{initial_warehouse_idx} = $warehouse_idx;
 
 173   $form->{warehouse_description} = $warehouse->{description};
 
 174   $warehouse->{selected}         = 1;
 
 176   $form->show_generic_error($locale->text("The source warehouse does not contain any bins.")) if (0 == scalar @{ $warehouse->{BINS} });
 
 178   map { $form->{"l_$_"} = 'Y' } qw(parts_id qty warehouseid binid partnumber partdescription bindescription chargenumber bestbefore partunit ean);
 
 180   $form->{sort} = 'bindescription';
 
 181   my @contents  = WH->get_warehouse_report("warehouse_id" => $form->{warehouse_id},
 
 182                                            "bin_id"       => $form->{bin_id},
 
 183                                            "chargenumber" => $form->{chargenumber},
 
 184                                            "bestbefore"   => $form->{bestbefore},
 
 185                                            "partnumber"   => $form->{partnumber},
 
 186                                            "ean"          => $form->{ean},
 
 187                                            "description"  => $form->{description});
 
 189   if (0 == scalar(@contents)) {
 
 190     $form->show_generic_error($locale->text("The selected warehouse is empty, or no stocked items where found that match the filter settings."));
 
 193   my $all_units = AM->retrieve_units(\%myconfig, $form);
 
 195   foreach (@contents) {
 
 196     $_->{qty} = $form->format_amount_units('amount'     => $_->{qty},
 
 197                                            'part_unit'  => $_->{partunit},
 
 198                                            'conv_units' => 'convertible');
 
 199     my $this_unit = $_->{partunit};
 
 201     if ($all_units->{$_->{partunit}} && ($all_units->{g}->{base_unit} eq $all_units->{$_->{partunit}}->{base_unit})) {
 
 205     $_->{UNITS} = AM->unit_select_data($all_units, $this_unit, 0, $_->{partunit});
 
 208   my $transfer_types = WH->retrieve_transfer_types($args{direction});
 
 209   map { $_->{description} = $locale->text($_->{description}) } @{ $transfer_types };
 
 211   $form->{CONTENTS}       = \@contents;
 
 212   $form->{TRANSFER_TYPES} = $transfer_types;
 
 214   $main::lxdebug->leave_sub();
 
 219   $main::lxdebug->enter_sub();
 
 221   $main::auth->assert('warehouse_management');
 
 223   my $form     = $main::form;
 
 224   my %myconfig = %main::myconfig;
 
 225   my $locale   = $main::locale;
 
 227   $form->get_lists('warehouses' => { 'key' => 'WAREHOUSES', 'bins' => 'BINS' });
 
 229   my $warehouse_idx = get_warehouse_idx($form->{warehouse_id});
 
 230   $form->show_generic_error($locale->text("The selected warehouse does not exist.")) if (-1 == $warehouse_idx);
 
 232   my $warehouse = $form->{WAREHOUSES}->[$warehouse_idx];
 
 234   $form->show_generic_error($locale->text("The source warehouse does not contain any bins.")) if (0 == scalar @{ $warehouse->{BINS} });
 
 236   map { $form->{"l_$_"} = 'Y' } qw(parts_id qty warehouseid binid partnumber partdescription bindescription chargenumber bestbefore partunit);
 
 238   $form->{sort} = 'bindescription';
 
 239   my @contents  = WH->get_warehouse_report("warehouse_id" => $form->{warehouse_id});
 
 240   my $all_units = AM->retrieve_units(\%myconfig, $form);
 
 244   foreach my $row (1 .. $form->{rowcount}) {
 
 245     $form->{"qty_$row"} =~ s/^\s*//;
 
 246     $form->{"qty_$row"} =~ s/\s*$//;
 
 247     next if (!$form->{"qty_$row"});
 
 249     my $bin_idx = get_bin_idx($warehouse_idx, $form->{"src_bin_id_$row"});
 
 250     $form->show_generic_error($locale->text("The selected bin does not exist.")) if (-1 == $bin_idx);
 
 251     my $bin     = $warehouse->{BINS}->[$bin_idx];
 
 253     my $orig_qty = $form->{"qty_$row"} . " " . $form->{"unit_$row"};
 
 256       'src_warehouse_id' => $form->{warehouse_id},
 
 257       'transfer_type_id' => $form->{transfer_type_id},
 
 260     map { $transfer->{$_} = $form->{"${_}_${row}"} } qw(src_bin_id chargenumber bestbefore parts_id qty dst_warehouse_id dst_bin_id);
 
 264     foreach (@contents) {
 
 265       if (($_->{binid} == $transfer->{src_bin_id}) && ($_->{parts_id} == $transfer->{parts_id}) && ($_->{chargenumber} eq $transfer->{chargenumber}) && $_->{bestbefore} eq $transfer->{bestbefore}) {
 
 272       $form->error($locale->text("There is not enough left of '#1' in bin '#2' for the removal of #3.",
 
 273                                  $form->{"partdescription_$row"}, $bin->{description}, $orig_qty));
 
 276     $transfer->{qty}  = $form->parse_amount(\%myconfig, $transfer->{qty}) * $all_units->{$form->{"unit_$row"}}->{factor};
 
 277     $transfer->{qty} /= $all_units->{$entry->{partunit}}->{factor} || 1;
 
 279     if (($entry->{qty} < $transfer->{qty}) || (0 >= $transfer->{qty})) {
 
 280       $form->error($locale->text("There is not enough left of '#1' in bin '#2' for the removal of #3.",
 
 281                                  $form->{"partdescription_$row"}, $bin->{description}, $orig_qty));
 
 284     $transfer->{comment} = $form->{comment};
 
 286     push @transfers, $transfer;
 
 288     $entry->{qty} -= $transfer->{qty};
 
 291   if (!scalar @transfers) {
 
 292     $form->show_generic_information($locale->text('Nothing has been selected for transfer.'));
 
 296   WH->transfer(@transfers);
 
 298   $form->{trans_type}    = 'transfer';
 
 299   $form->{saved_message} = $locale->text('The parts have been transferred.');
 
 301   transfer_warehouse_selection();
 
 303   $main::lxdebug->leave_sub();
 
 306 # --------------------------------------------------------------------
 
 308 # --------------------------------------------------------------------
 
 310 sub transfer_stock_update_part {
 
 311   $main::lxdebug->enter_sub();
 
 313   my $form     = $main::form;
 
 314   my %myconfig = %main::myconfig;
 
 315   my $locale   = $main::locale;
 
 317   $form->{trans_type} = 'stock';
 
 318   $form->{qty}        = $form->parse_amount(\%myconfig, $form->{qty});
 
 320   if (!$form->{partnumber} && !$form->{description} && !$form->{ean}) {
 
 321     delete @{$form}{qw(parts_id partunit ean)};
 
 322     transfer_warehouse_selection();
 
 324   } elsif (($form->{partnumber} && ($form->{partnumber} ne $form->{old_partnumber})) || $form->{description} || $form->{ean}) {
 
 326 #    $form->{no_services}   = 1; # services may now be transfered. fix for Bug 1383.
 
 327     $form->{no_assemblies} = 0; # assemblies duerfen eingelagert werden (z.B. bei retouren)
 
 329     my $parts = Common->retrieve_parts(\%myconfig, $form, 'description', 1);
 
 331     if (!scalar @{ $parts }) {
 
 332       new_item(action => "transfer_stock_update_part");
 
 333     } elsif (scalar @{ $parts } == 1) {
 
 334       @{$form}{qw(parts_id partnumber description ean)} = @{$parts->[0]}{qw(id partnumber description ean)};
 
 335       transfer_stock_get_partunit();
 
 336       transfer_warehouse_selection();
 
 339       select_part('transfer_stock_part_selected', @{ $parts });
 
 343     transfer_stock_get_partunit();
 
 344     transfer_warehouse_selection();
 
 347   $main::lxdebug->leave_sub();
 
 350 # --------------------------------------------------------------------
 
 351 # Transfer: assemblies
 
 352 # Dies ist die Auswahlmaske für ein assembly.
 
 353 # Die ist einfach von transfer_assembly_update_part kopiert und nur um den trans_type (assembly) korrigiert worden
 
 354 # Es wäre schön, hier nochmal check_assembly_max_create auf, um die max. Fertigungszahl herauszufinden.
 
 355 # Ich lass das mal als auskommentierte Idee bestehen jb 18.3.09
 
 356 # --------------------------------------------------------------------
 
 358 sub transfer_assembly_update_part {
 
 359   $main::lxdebug->enter_sub();
 
 361   my $form     = $main::form;
 
 362   my %myconfig = %main::myconfig;
 
 363   my $locale   = $main::locale;
 
 365   $form->{trans_type} = 'assembly';
 
 366   $form->{qty}        = $form->parse_amount(\%myconfig, $form->{qty});
 
 368   if (!$form->{partnumber} && !$form->{description}) {
 
 369     delete @{$form}{qw(parts_id partunit)};
 
 370     transfer_warehouse_selection();
 
 372   } elsif (($form->{partnumber} && ($form->{partnumber} ne $form->{old_partnumber})) || $form->{description}) {
 
 373     $form->{assemblies} = 1;
 
 374     $form->{no_assemblies} = 0;
 
 375     my $parts = Common->retrieve_parts(\%myconfig, $form, 'description', 1);
 
 376     if (scalar @{ $parts } == 1) {
 
 377       @{$form}{qw(parts_id partnumber description)} = @{$parts->[0]}{qw(id partnumber description)};
 
 378       transfer_stock_get_partunit();
 
 379       transfer_warehouse_selection();
 
 381       select_part('transfer_stock_part_selected', @{ $parts });
 
 385     transfer_stock_get_partunit();
 
 386     transfer_warehouse_selection();
 
 389 # hier die oben benannte idee
 
 390 #    my $maxcreate = Common->check_assembly_max_create(assembly_id => $form->{parts_id}, dbh => $my_dbh);
 
 391   $main::lxdebug->leave_sub();
 
 394 sub transfer_stock_part_selected {
 
 395   $main::lxdebug->enter_sub();
 
 399   my $form     = $main::form;
 
 401   @{$form}{qw(parts_id partnumber description ean)} = @{$part}{qw(id partnumber description ean)};
 
 403   transfer_stock_get_partunit();
 
 404   transfer_warehouse_selection();
 
 406   $main::lxdebug->leave_sub();
 
 409 sub transfer_stock_get_partunit {
 
 410   $main::lxdebug->enter_sub();
 
 412   my $form     = $main::form;
 
 414   if ($form->{parts_id}) {
 
 415     my $part_info     = IC->get_basic_part_info('id' => $form->{parts_id});
 
 416     $form->{partunit} = $part_info->{unit};
 
 419   $main::lxdebug->leave_sub();
 
 422 # vorüberlegung jb 22.2.2009
 
 423 # wir benötigen für diese funktion, die anzahl die vom erzeugnis hergestellt werden soll. vielleicht direkt per js fehleingaben verhindern?
 
 424 # ferner dann nochmal mit check_asssembly_max_create gegenprüfen und dann transaktionssicher wegbuchen.
 
 425 # 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
 
 426 # Manko ist derzeit noch, dass unterschiedliche Lagerplätze, bzw. das Quelllager an sich nicht ausgewählt werden können.
 
 427 # Laut Absprache in KW11 09 übernimmt mb hier den rest im April ... jb 18.3.09
 
 429 sub create_assembly {
 
 430   $main::lxdebug->enter_sub();
 
 432   my $form     = $main::form;
 
 433   my %myconfig = %main::myconfig;
 
 434   my $locale   = $main::locale;
 
 436   $form->{qty} = $form->parse_amount(\%myconfig, $form->{qty});
 
 437   if ($form->{qty} <= 0) {
 
 438     $form->show_generic_error($locale->text('Invalid quantity.'), 'back_button' => 1);
 
 440   # TODO Es wäre schön, hier schon die maximale Anzahl der zu fertigenden Erzeugnisse zu haben
 
 441   #else { if ($form->{qty} > $maxcreate) { #s.o.
 
 442   #     $form->show_generic_error($locale->text('Can not create that quantity with current stock'), 'back_button' => 1);
 
 443   #     $form->show_generic_error('Maximale Stückzahl' . $maxcreate , 'back_button' => 1);
 
 447   if (!$form->{warehouse_id} || !$form->{bin_id}) {
 
 448     $form->error($locale->text('The warehouse or the bin is missing.'));
 
 451   if (!$::instance_conf->get_show_bestbefore) {
 
 452       $form->{bestbefore} = '';
 
 455   # 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
 
 456   # Anm. jb 18.3. vielleicht auch nur meine unwissenheit in perl-datenstrukturen
 
 458     'transfer_type'    => 'assembly',
 
 459     'login'            => $form->{login},
 
 460     'dst_warehouse_id' => $form->{warehouse_id},
 
 461     'dst_bin_id'       => $form->{bin_id},
 
 462     'chargenumber'     => $form->{chargenumber},
 
 463     'bestbefore'       => $form->{bestbefore},
 
 464     'assembly_id'      => $form->{parts_id},
 
 465     'qty'              => $form->{qty},
 
 466     'unit'             => $form->{unit},
 
 467     'comment'          => $form->{comment}
 
 470   my $ret = WH->transfer_assembly (%TRANSFER);
 
 471   # Frage: Ich pack in den return-wert auch gleich die Fehlermeldung. Irgendwelche Nummern als Fehlerkonstanten definieren find ich auch nicht besonders schick...
 
 474     # 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
 
 475     $form->show_generic_error($ret, 'back_button' => 1);
 
 478   delete @{$form}{qw(parts_id partnumber description qty unit chargenumber bestbefore comment)};
 
 480   $form->{saved_message} = $locale->text('The assembly has been created.');
 
 481   $form->{trans_type}    = 'assembly';
 
 483   transfer_warehouse_selection();
 
 485   $main::lxdebug->leave_sub();
 
 489   $main::lxdebug->enter_sub();
 
 491   my $form     = $main::form;
 
 492   my %myconfig = %main::myconfig;
 
 493   my $locale   = $main::locale;
 
 495   $form->{qty} = $form->parse_amount(\%myconfig, $form->{qty});
 
 497   if ($form->{qty} <= 0) {
 
 498     $form->show_generic_error($locale->text('Invalid quantity.'), 'back_button' => 1);
 
 501   if (!$form->{warehouse_id} || !$form->{bin_id}) {
 
 502     $form->error($locale->text('The warehouse or the bin is missing.'));
 
 506     'transfer_type'    => 'stock',
 
 507     'dst_warehouse_id' => $form->{warehouse_id},
 
 508     'dst_bin_id'       => $form->{bin_id},
 
 509     'chargenumber'     => $form->{chargenumber},
 
 510     'bestbefore'       => $form->{bestbefore},
 
 511     'parts_id'         => $form->{parts_id},
 
 512     'qty'              => $form->{qty},
 
 513     'unit'             => $form->{unit},
 
 514     'comment'          => $form->{comment},
 
 517   WH->transfer($transfer);
 
 519   delete @{$form}{qw(parts_id partnumber description qty unit chargenumber bestbefore comment ean)};
 
 521   $form->{saved_message} = $locale->text('The parts have been stocked.');
 
 522   $form->{trans_type}    = 'stock';
 
 524   transfer_warehouse_selection();
 
 526   $main::lxdebug->leave_sub();
 
 529 # --------------------------------------------------------------------
 
 531 # --------------------------------------------------------------------
 
 533 sub removal_parts_selection {
 
 534   $main::lxdebug->enter_sub();
 
 536   $main::auth->assert('warehouse_management');
 
 538   my $form     = $main::form;
 
 539   my %myconfig = %main::myconfig;
 
 540   my $locale   = $main::locale;
 
 542   transfer_or_removal_prepare_contents('direction' => 'out');
 
 544   $form->{title} = $locale->text('Removal');
 
 546   print $form->parse_html_template("wh/removal_parts_selection");
 
 548   $main::lxdebug->leave_sub();
 
 552   $main::lxdebug->enter_sub();
 
 554   $main::auth->assert('warehouse_management');
 
 556   my $form     = $main::form;
 
 557   my %myconfig = %main::myconfig;
 
 558   my $locale   = $main::locale;
 
 560   $form->get_lists('warehouses' => { 'key'    => 'WAREHOUSES',
 
 561                                      'bins'   => 'BINS', });
 
 563   my $warehouse_idx = get_warehouse_idx($form->{warehouse_id});
 
 564   $form->show_generic_error($locale->text("The selected warehouse does not exist.")) if (-1 == $warehouse_idx);
 
 566   my $warehouse = $form->{WAREHOUSES}->[$warehouse_idx];
 
 568   $form->show_generic_error($locale->text("The warehouse does not contain any bins.")) if (0 == scalar @{ $warehouse->{BINS} });
 
 570   map { $form->{"l_$_"} = 'Y' } qw(parts_id qty warehouseid binid partnumber partdescription bindescription chargenumber bestbefore partunit);
 
 572   $form->{sort} = 'bindescription';
 
 573   my @contents  = WH->get_warehouse_report("warehouse_id" => $form->{warehouse_id});
 
 574   my $all_units = AM->retrieve_units(\%myconfig, $form);
 
 578   foreach my $row (1 .. $form->{rowcount}) {
 
 579     $form->{"qty_$row"} =~ s/^\s*//;
 
 580     $form->{"qty_$row"} =~ s/\s*$//;
 
 581     next if (!$form->{"qty_$row"});
 
 583     my $bin_idx = get_bin_idx($warehouse_idx, $form->{"src_bin_id_$row"});
 
 584     $form->show_generic_error($locale->text("The selected bin does not exist.")) if (-1 == $bin_idx);
 
 585     my $bin     = $warehouse->{BINS}->[$bin_idx];
 
 587     my $orig_qty = $form->{"qty_$row"} . " " . $form->{"unit_$row"};
 
 590       'src_warehouse_id' => $form->{warehouse_id},
 
 591       'transfer_type_id' => $form->{transfer_type_id},
 
 594     map { $transfer->{$_} = $form->{"${_}_${row}"} } qw(src_bin_id chargenumber bestbefore parts_id qty);
 
 598     foreach (@contents) {
 
 599       if (($_->{binid} == $transfer->{src_bin_id}) && ($_->{parts_id} == $transfer->{parts_id}) && ($_->{chargenumber} eq $transfer->{chargenumber}) && ($_->{bestbefore} eq $transfer->{bestbefore})) {
 
 606       $form->error($locale->text("There is not enough left of '#1' in bin '#2' for the removal of #3.",
 
 607                                  $form->{"partdescription_$row"}, $bin->{description}, $orig_qty));
 
 610     $transfer->{qty}  = $form->parse_amount(\%myconfig, $transfer->{qty}) * $all_units->{$form->{"unit_$row"}}->{factor};
 
 611     $transfer->{qty} /= $all_units->{$entry->{partunit}}->{factor} || 1;
 
 613     if (($entry->{qty} < $transfer->{qty}) || (0 >= $transfer->{qty})) {
 
 614       $form->error($locale->text("There is not enough left of '#1' in bin '#2' for the removal of #3.",
 
 615                                  $form->{"partdescription_$row"}, $bin->{description}, $orig_qty));
 
 618     $transfer->{comment} = $form->{comment};
 
 620     push @transfers, $transfer;
 
 622     $entry->{qty} -= $transfer->{qty};
 
 625   if (!scalar @transfers) {
 
 626     $form->show_generic_information($locale->text('Nothing has been selected for removal.'));
 
 630   WH->transfer(@transfers);
 
 632   $form->{trans_type}    = 'removal';
 
 633   $form->{saved_message} = $locale->text('The parts have been removed.');
 
 635   transfer_warehouse_selection();
 
 637   $main::lxdebug->leave_sub();
 
 640 # --------------------------------------------------------------------
 
 642 # --------------------------------------------------------------------
 
 645   $main::lxdebug->enter_sub();
 
 647   $main::auth->assert('warehouse_management');
 
 649   my $form     = $main::form;
 
 650   my %myconfig = %main::myconfig;
 
 651   my $locale   = $main::locale;
 
 653   $form->{title} = $locale->text('Report about warehouse transactions');
 
 654   $form->get_lists('warehouses' => { 'key'  => 'WAREHOUSES',
 
 655                                      'bins' => 'BINS', });
 
 657   show_no_warehouses_error() if (!scalar @{ $form->{WAREHOUSES} });
 
 659   $form->{jsscript} = 1;
 
 662   print $form->parse_html_template("wh/journal_filter", { "UNITS" => AM->unit_select_data(AM->retrieve_units(\%myconfig, $form)) });
 
 664   $main::lxdebug->leave_sub();
 
 667 sub generate_journal {
 
 668   $main::lxdebug->enter_sub();
 
 670   $main::auth->assert('warehouse_management');
 
 672   my $form     = $main::form;
 
 673   my %myconfig = %main::myconfig;
 
 674   my $locale   = $main::locale;
 
 676   $form->{title}   = $locale->text("WHJournal");
 
 677   $form->{sort}  ||= 'date';
 
 680   my @columns = qw(trans_id date warehouse_from bin_from warehouse_to bin_to partnumber partdescription chargenumber bestbefore trans_type comment qty employee oe_id projectnumber);
 
 683   map { $filter{$_} = $form->{$_} if ($form->{$_}) } qw(warehouse_id bin_id partnumber description chargenumber bestbefore);
 
 685   $filter{qty_op} = WH->convert_qty_op($form->{qty_op});
 
 686   if ($filter{qty_op}) {
 
 687     $form->isblank("qty",      $locale->text('Quantity missing.'));
 
 688     $form->isblank("qty_unit", $locale->text('Unit missing.'));
 
 690     $filter{qty}      = $form->{qty};
 
 691     $filter{qty_unit} = $form->{qty_unit};
 
 695   my $report = SL::ReportGenerator->new(\%myconfig, $form);
 
 697   my @hidden_variables = map { "l_${_}" } @columns;
 
 698   push @hidden_variables, qw(warehouse_id bin_id partnumber description chargenumber bestbefore qty_op qty qty_unit fromdate todate);
 
 701     'date'            => { 'text' => $locale->text('Date'), },
 
 702     'trans_id'        => { 'text' => $locale->text('Trans Id'), },
 
 703     'trans_type'      => { 'text' => $locale->text('Trans Type'), },
 
 704     'comment'         => { 'text' => $locale->text('Comment'), },
 
 705     'warehouse_from'  => { 'text' => $locale->text('Warehouse From'), },
 
 706     'warehouse_to'    => { 'text' => $locale->text('Warehouse To'), },
 
 707     'bin_from'        => { 'text' => $locale->text('Bin From'), },
 
 708     'bin_to'          => { 'text' => $locale->text('Bin To'), },
 
 709     'partnumber'      => { 'text' => $locale->text('Part Number'), },
 
 710     'partdescription' => { 'text' => $locale->text('Part Description'), },
 
 711     'chargenumber'    => { 'text' => $locale->text('Charge Number'), },
 
 712     'bestbefore'      => { 'text' => $locale->text('Best Before'), },
 
 713     'qty'             => { 'text' => $locale->text('Qty'), },
 
 714     'employee'        => { 'text' => $locale->text('Employee'), },
 
 715     'projectnumber'   => { 'text' => $locale->text('Project Number'), },
 
 716     'oe_id'           => { 'text' => $locale->text('Document'), },
 
 719   my $href = build_std_url('action=generate_journal', grep { $form->{$_} } @hidden_variables);
 
 720   map { $column_defs{$_}->{link} = $href . "&sort=${_}&order=" . Q($_ eq $form->{sort} ? 1 - $form->{order} : $form->{order}) } @columns;
 
 722   my %column_alignment = map { $_ => 'right' } qw(qty);
 
 724   map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
 
 726   $report->set_columns(%column_defs);
 
 727   $report->set_column_order(@columns);
 
 729   $report->set_export_options('generate_journal', @hidden_variables, qw(sort order));
 
 731   $report->set_sort_indicator($form->{sort}, $form->{order});
 
 733   $report->set_options('output_format'        => 'HTML',
 
 734                        'title'                => $form->{title},
 
 735                        'attachment_basename'  => strftime($locale->text('warehouse_journal_list') . '_%Y%m%d', localtime time));
 
 736   $report->set_options_from_form();
 
 737   $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
 
 739   my $all_units = AM->retrieve_units(\%myconfig, $form);
 
 740   my @contents  = WH->get_warehouse_journal(%filter);
 
 742   my %doc_types = ( 'sales_quotation'         => { script => 'oe', title => $locale->text('Sales quotation') },
 
 743                     'sales_order'             => { script => 'oe', title => $locale->text('Sales Order') },
 
 744                     'request_quotation'       => { script => 'oe', title => $locale->text('Request quotation') },
 
 745                     'purchase_order'          => { script => 'oe', title => $locale->text('Purchase Order') },
 
 746                     'sales_delivery_order'    => { script => 'do', title => $locale->text('Sales delivery order') },
 
 747                     'purchase_delivery_order' => { script => 'do', title => $locale->text('Purchase delivery order') },
 
 748                     'sales_invoice'           => { script => 'is', title => $locale->text('Sales Invoice') },
 
 749                     'purchase_invoice'        => { script => 'ir', title => $locale->text('Purchase Invoice') },
 
 752   foreach my $entry (@contents) {
 
 753     $entry->{qty}        = $form->format_amount_units('amount'     => $entry->{qty},
 
 754                                                       'part_unit'  => $entry->{partunit},
 
 755                                                       'conv_units' => 'convertible');
 
 756     $entry->{trans_type} = $locale->text($entry->{trans_type});
 
 760     foreach my $column (@columns) {
 
 762         'data'  => $entry->{$column},
 
 763         'align' => $column_alignment{$column},
 
 767     $row->{trans_type}->{raw_data} = $entry->{trans_type};
 
 769     if ($form->{l_oe_id}) {
 
 770       $row->{oe_id}->{data} = '';
 
 771       my $info              = $entry->{oe_id_info};
 
 773       if ($info && $info->{id} && $info->{type} && $doc_types{$info->{type}}) {
 
 774         $row->{oe_id} = { data => $doc_types{ $info->{type} }->{title} . ' ' . $info->{number},
 
 775                           link => build_std_url('script=' . $doc_types{ $info->{type} }->{script} . '.pl', 'action=edit', 'id=' . $info->{id}, 'type=' . $info->{type}) };
 
 779     $report->add_data($row);
 
 782   $report->generate_with_headers();
 
 784   $main::lxdebug->leave_sub();
 
 787 # --------------------------------------------------------------------
 
 789 # --------------------------------------------------------------------
 
 792   $main::lxdebug->enter_sub();
 
 794   $main::auth->assert('warehouse_contents | warehouse_management');
 
 796   my $form     = $main::form;
 
 797   my %myconfig = %main::myconfig;
 
 798   my $locale   = $main::locale;
 
 800   $form->get_lists('warehouses' => { 'key'    => 'WAREHOUSES',
 
 801                                      'bins'   => 'BINS', });
 
 803   show_no_warehouses_error() if (!scalar @{ $form->{WAREHOUSES} });
 
 805   $form->{jsscript} = 1;
 
 807   $form->{title}   = $locale->text("Report about warehouse contents");
 
 810   print $form->parse_html_template("wh/report_filter",
 
 811                                    { "nextsub"    => "generate_report",
 
 812                                      "WAREHOUSES" => $form->{WAREHOUSES},
 
 813                                      "UNITS"      => AM->unit_select_data(AM->retrieve_units(\%myconfig, $form)) });
 
 815   $main::lxdebug->leave_sub();
 
 818 sub generate_report {
 
 819   $main::lxdebug->enter_sub();
 
 821   $main::auth->assert('warehouse_contents | warehouse_management');
 
 823   my $form     = $main::form;
 
 824   my %myconfig = %main::myconfig;
 
 825   my $locale   = $main::locale;
 
 827   $form->{title}   = $locale->text("Report about warehouse contents");
 
 828   $form->{sort}  ||= 'partnumber';
 
 829   my $sort_col     = $form->{sort};
 
 832   my @columns = qw(warehousedescription bindescription partnumber partdescription chargenumber bestbefore qty stock_value);
 
 835   map { $filter{$_} = $form->{$_} if ($form->{$_}) } qw(warehouse_id bin_id partnumber description chargenumber bestbefore date include_invalid_warehouses);
 
 837   $filter{qty_op} = WH->convert_qty_op($form->{qty_op});
 
 838   if ($filter{qty_op}) {
 
 839     $form->isblank("qty",      $locale->text('Quantity missing.'));
 
 840     $form->isblank("qty_unit", $locale->text('Unit missing.'));
 
 842     $filter{qty}      = $form->{qty};
 
 843     $filter{qty_unit} = $form->{qty_unit};
 
 847   $form->{subtotal} = '' if (!first { $_ eq $sort_col } qw(partnumber partdescription));
 
 849   my $report = SL::ReportGenerator->new(\%myconfig, $form);
 
 851   my @hidden_variables = map { "l_${_}" } @columns;
 
 852   push @hidden_variables, qw(warehouse_id bin_id partnumber description chargenumber bestbefore qty_op qty qty_unit l_warehousedescription l_bindescription);
 
 853   push @hidden_variables, qw(include_empty_bins subtotal include_invalid_warehouses date);
 
 856     'warehousedescription' => { 'text' => $locale->text('Warehouse'), },
 
 857     'bindescription'       => { 'text' => $locale->text('Bin'), },
 
 858     'partnumber'           => { 'text' => $locale->text('Part Number'), },
 
 859     'partdescription'      => { 'text' => $locale->text('Part Description'), },
 
 860     'chargenumber'         => { 'text' => $locale->text('Charge Number'), },
 
 861     'bestbefore'           => { 'text' => $locale->text('Best Before'), },
 
 862     'qty'                  => { 'text' => $locale->text('Qty'), },
 
 863     'stock_value'          => { 'text' => $locale->text('Stock value'), },
 
 866   my $href = build_std_url('action=generate_report', grep { $form->{$_} } @hidden_variables);
 
 867   map { $column_defs{$_}->{link} = $href . "&sort=${_}&order=" . Q($_ eq $sort_col ? 1 - $form->{order} : $form->{order}) } @columns;
 
 869   my %column_alignment = map { $_ => 'right' } qw(qty stock_value);
 
 871   map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
 
 873   $report->set_columns(%column_defs);
 
 874   $report->set_column_order(@columns);
 
 876   $report->set_export_options('generate_report', @hidden_variables, qw(sort order));
 
 878   $report->set_sort_indicator($sort_col, $form->{order});
 
 880   $report->set_options('output_format'        => 'HTML',
 
 881                        'title'                => $form->{title},
 
 882                        'attachment_basename'  => strftime($locale->text('warehouse_report_list') . '_%Y%m%d', localtime time));
 
 883   $report->set_options_from_form();
 
 884   $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
 
 886   my $all_units = AM->retrieve_units(\%myconfig, $form);
 
 887   my @contents  = WH->get_warehouse_report(%filter);
 
 891   my @subtotals_columns = qw(qty stock_value);
 
 892   my %subtotals         = map { $_ => 0 } @subtotals_columns;
 
 894   my $total_stock_value = 0;
 
 896   foreach my $entry (@contents) {
 
 897     map { $subtotals{$_} += $entry->{$_} } @subtotals_columns;
 
 898     $total_stock_value   += $entry->{stock_value} * 1;
 
 900     $entry->{qty}         = $form->format_amount_units('amount'     => $entry->{qty},
 
 901                                                        'part_unit'  => $entry->{partunit},
 
 902                                                        'conv_units' => 'convertible');
 
 903     $entry->{stock_value} = $form->format_amount(\%myconfig, $entry->{stock_value} * 1, 2);
 
 905     my $row_set = [ { map { $_ => { 'data' => $entry->{$_}, 'align' => $column_alignment{$_} } } @columns } ];
 
 907     if ( ($form->{subtotal} eq 'Y' && !$form->{report_generator_csv_options_for_import} )
 
 908         && (($idx == (scalar @contents - 1))
 
 909             || ($entry->{$sort_col} ne $contents[$idx + 1]->{$sort_col}))) {
 
 911       my $row = { map { $_ => { 'data' => '', 'class' => 'listsubtotal', 'align' => $column_alignment{$_}, } } @columns };
 
 912       $row->{qty}->{data}         = $form->format_amount_units('amount'     => $subtotals{qty} * 1,
 
 913                                                                'part_unit'  => $entry->{partunit},
 
 914                                                                'conv_units' => 'convertible');
 
 915       $row->{stock_value}->{data} = $form->format_amount(\%myconfig, $subtotals{stock_value} * 1, 2);
 
 917       %subtotals                  = map { $_ => 0 } @subtotals_columns;
 
 919       push @{ $row_set }, $row;
 
 922     $report->add_data($row_set);
 
 927   if ( $column_defs{stock_value}->{visible} && !$form->{report_generator_csv_options_for_import} ) {
 
 928     $report->add_separator();
 
 930     my $row                      = { map { $_ => { 'data' => '', 'class' => 'listsubtotal', } } @columns };
 
 932     my $left_col                 = first { $column_defs{$_}->{visible} } @columns;
 
 934     $row->{$left_col}->{data}    = $locale->text('Total stock value');
 
 935     $row->{stock_value}->{data}  = $form->format_amount(\%myconfig, $total_stock_value, 2);
 
 936     $row->{stock_value}->{align} = 'right';
 
 938     $report->add_data($row);
 
 941   $report->generate_with_headers();
 
 943   $main::lxdebug->leave_sub();
 
 946 # --------------------------------------------------------------------
 
 948 # --------------------------------------------------------------------
 
 950 sub show_no_warehouses_error {
 
 951   $main::lxdebug->enter_sub();
 
 953   my $form     = $main::form;
 
 954   my %myconfig = %main::myconfig;
 
 955   my $locale   = $main::locale;
 
 957   my $msg = $locale->text('No warehouse has been created yet or the quantity of the bins is not configured yet.') . ' ';
 
 959   if ($main::auth->check_right($form->{login}, 'config')) {
 
 960     $msg .= $locale->text('You can create warehouses and bins via the menu "System -> Warehouses".');
 
 962     $msg .= $locale->text('Please ask your administrator to create warehouses and bins.');
 
 965   $form->show_generic_error($msg);
 
 967   $main::lxdebug->leave_sub();
 
 970 sub get_warehouse_idx {
 
 971   my ($warehouse_id) = @_;
 
 973   my $form     = $main::form;
 
 975   for (my $i = 0; $i < scalar @{$form->{WAREHOUSES}}; $i++) {
 
 976     return $i if ($form->{WAREHOUSES}->[$i]->{id} == $warehouse_id);
 
 983   my ($warehouse_index, $bin_id) = @_;
 
 985   my $form     = $main::form;
 
 987   my $warehouse = $form->{WAREHOUSES}->[$warehouse_index];
 
 989   return -1 if (!$warehouse);
 
 991   for (my $i = 0; $i < scalar @{ $warehouse->{BINS} }; $i++) {
 
 992     return $i if ($warehouse->{BINS}->[$i]->{id} == $bin_id);
 
 999   $main::lxdebug->enter_sub();
 
1002   my $form     = $main::form;
 
1005   $form->{old_callback} = $form->escape($form->{callback}, 1);
 
1006   $form->{callback}     = $form->escape("$form->{script}?action=$params{action}", 1);
 
1008   # save all form variables except action in a previousform variable
 
1009   my $previousform = join '&', map { my $value = $form->{$_}; $value =~ s/&/%26/; "$_=$value" } grep { !/action/ } keys %$form;
 
1012 #  push @HIDDENS,      { 'name' => 'previousform', 'value' => $form->escape($previousform, 1) };
 
1013   push @HIDDENS, map +{ 'name' => $_,             'value' => $form->{$_} }, qw(partnumber description unit vc sellprice ean);
 
1014   push @HIDDENS,      { 'name' => 'taxaccount2',  'value' => $form->{taxaccounts} };
 
1015   push @HIDDENS,      { 'name' => 'notes',        'value' => $form->{longdescription} };
 
1018   print $form->parse_html_template("generic/new_item", { HIDDENS => [ sort { $a->{name} cmp $b->{name} } @HIDDENS ] } );
 
1020   $main::lxdebug->leave_sub();
 
1024   my $form     = $main::form;
 
1025   call_sub($form->{update_nextsub} || $form->{nextsub});
 
1029   my $form     = $main::form;
 
1030   call_sub($form->{continue_nextsub} || $form->{nextsub});
 
1034   my $form     = $main::form;
 
1035   call_sub($form->{stock_nextsub} || $form->{nextsub});
 
1044 bin/mozilla/wh.pl - Warehouse frontend.
 
1052 call new item dialogue from warehouse masks.
 
1055   action  => name of sub to be called when new item is done