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";
 
  56 # contents of the "transfer_type" table:
 
  57 #  $locale->text('back')
 
  58 #  $locale->text('correction')
 
  59 #  $locale->text('disposed')
 
  60 #  $locale->text('found')
 
  61 #  $locale->text('missing')
 
  62 #  $locale->text('stock')
 
  63 #  $locale->text('shipped')
 
  64 #  $locale->text('transfer')
 
  65 #  $locale->text('used')
 
  66 #  $locale->text('return_material')
 
  67 #  $locale->text('release_material')
 
  69 # --------------------------------------------------------------------
 
  71 # --------------------------------------------------------------------
 
  73 sub transfer_warehouse_selection {
 
  74   $main::lxdebug->enter_sub();
 
  76   $main::auth->assert('warehouse_management');
 
  78   my $form     = $main::form;
 
  79   my %myconfig = %main::myconfig;
 
  80   my $locale   = $main::locale;
 
  82   $form->get_lists('warehouses' => { 'key'    => 'WAREHOUSES',
 
  85   show_no_warehouses_error() if (!scalar @{ $form->{WAREHOUSES} });
 
  87   my $units      = AM->retrieve_units(\%myconfig, $form);
 
  88   # der zweite Parameter von unit_select_data gibt den default-Namen (selected) vor
 
  89   $form->{UNITS} = AM->unit_select_data($units, $form->{partunit}, 0, $form->{partunit});
 
  91   if (scalar @{ $form->{WAREHOUSES} }) {
 
  92     $form->{warehouse_id} ||= $form->{WAREHOUSES}->[0]->{id};
 
  93     $form->{bin_id}       ||= $form->{WAREHOUSES}->[0]->{BINS}->[0]->{id};
 
  98   $form->{jsscript} = 1;
 
 100   if ($form->{trans_type} eq 'removal') {
 
 101     $form->{nextsub} = "removal_parts_selection";
 
 102     $form->{title}   = $locale->text('Removal from Warehouse');
 
 103     $content         = $form->parse_html_template('wh/warehouse_selection');
 
 105   } elsif ($form->{trans_type} eq 'stock') {
 
 106     $form->{title} = $locale->text('Stock');
 
 107     $content       = $form->parse_html_template('wh/warehouse_selection_stock');
 
 109   } elsif (!$form->{trans_type} || ($form->{trans_type} eq 'transfer')) {
 
 110     $form->{nextsub} = "transfer_parts_selection";
 
 111     $form->{title}   = $locale->text('Transfer');
 
 112     $content         = $form->parse_html_template('wh/warehouse_selection');
 
 114   } elsif ($form->{trans_type} eq 'assembly') {
 
 115     $form->{title} = $locale->text('Produce Assembly');
 
 116     $content       = $form->parse_html_template('wh/warehouse_selection_assembly');
 
 122   $main::lxdebug->leave_sub();
 
 125 sub transfer_parts_selection {
 
 126   $main::lxdebug->enter_sub();
 
 128   $main::auth->assert('warehouse_management');
 
 130   my $form     = $main::form;
 
 131   my %myconfig = %main::myconfig;
 
 132   my $locale   = $main::locale;
 
 134   transfer_or_removal_prepare_contents('direction' => 'transfer');
 
 136   $form->{title} = $locale->text('Transfer');
 
 138   print $form->parse_html_template("wh/transfer_parts_selection");
 
 140   $main::lxdebug->leave_sub();
 
 143 sub transfer_or_removal_prepare_contents {
 
 144   $main::lxdebug->enter_sub();
 
 146   $main::auth->assert('warehouse_management');
 
 150   my $form     = $main::form;
 
 151   my %myconfig = %main::myconfig;
 
 152   my $locale   = $main::locale;
 
 154   $form->get_lists('warehouses' => { 'key'    => 'WAREHOUSES',
 
 155                                      'bins'   => 'BINS', });
 
 157   my $warehouse_idx = get_warehouse_idx($form->{warehouse_id});
 
 158   $form->show_generic_error($locale->text("The selected warehouse does not exist.")) if (-1 == $warehouse_idx);
 
 160   my $warehouse = $form->{WAREHOUSES}->[$warehouse_idx];
 
 162   $form->{initial_warehouse_idx} = $warehouse_idx;
 
 163   $form->{warehouse_description} = $warehouse->{description};
 
 164   $warehouse->{selected}         = 1;
 
 166   $form->show_generic_error($locale->text("The source warehouse does not contain any bins.")) if (0 == scalar @{ $warehouse->{BINS} });
 
 168   map { $form->{"l_$_"} = 'Y' } qw(parts_id qty warehouseid binid partnumber partdescription bindescription chargenumber bestbefore partunit ean);
 
 170   $form->{sort} = 'bindescription';
 
 171   my @contents  = WH->get_warehouse_report("warehouse_id" => $form->{warehouse_id},
 
 172                                            "bin_id"       => $form->{bin_id},
 
 173                                            "chargenumber" => $form->{chargenumber},
 
 174                                            "bestbefore"   => $form->{bestbefore},
 
 175                                            "partnumber"   => $form->{partnumber},
 
 176                                            "ean"          => $form->{ean},
 
 177                                            "description"  => $form->{description});
 
 179   $form->show_generic_error($locale->text("The selected warehouse is empty.")) if (0 == scalar(@contents));
 
 181   my $all_units = AM->retrieve_units(\%myconfig, $form);
 
 183   foreach (@contents) {
 
 184     $_->{qty} = $form->format_amount_units('amount'     => $_->{qty},
 
 185                                            'part_unit'  => $_->{partunit},
 
 186                                            'conv_units' => 'convertible');
 
 187     my $this_unit = $_->{partunit};
 
 189     if ($all_units->{$_->{partunit}} && ($all_units->{g}->{base_unit} eq $all_units->{$_->{partunit}}->{base_unit})) {
 
 193     $_->{UNITS} = AM->unit_select_data($all_units, $this_unit, 0, $_->{partunit});
 
 196   my $transfer_types = WH->retrieve_transfer_types($args{direction});
 
 197   map { $_->{description} = $locale->text($_->{description}) } @{ $transfer_types };
 
 199   $form->{CONTENTS}       = \@contents;
 
 200   $form->{TRANSFER_TYPES} = $transfer_types;
 
 202   $main::lxdebug->leave_sub();
 
 207   $main::lxdebug->enter_sub();
 
 209   $main::auth->assert('warehouse_management');
 
 211   my $form     = $main::form;
 
 212   my %myconfig = %main::myconfig;
 
 213   my $locale   = $main::locale;
 
 215   $form->get_lists('warehouses' => { 'key' => 'WAREHOUSES', 'bins' => 'BINS' });
 
 217   my $warehouse_idx = get_warehouse_idx($form->{warehouse_id});
 
 218   $form->show_generic_error($locale->text("The selected warehouse does not exist.")) if (-1 == $warehouse_idx);
 
 220   my $warehouse = $form->{WAREHOUSES}->[$warehouse_idx];
 
 222   $form->show_generic_error($locale->text("The source warehouse does not contain any bins.")) if (0 == scalar @{ $warehouse->{BINS} });
 
 224   map { $form->{"l_$_"} = 'Y' } qw(parts_id qty warehouseid binid partnumber partdescription bindescription chargenumber bestbefore partunit);
 
 226   $form->{sort} = 'bindescription';
 
 227   my @contents  = WH->get_warehouse_report("warehouse_id" => $form->{warehouse_id});
 
 228   my $all_units = AM->retrieve_units(\%myconfig, $form);
 
 232   foreach my $row (1 .. $form->{rowcount}) {
 
 233     $form->{"qty_$row"} =~ s/^\s*//;
 
 234     $form->{"qty_$row"} =~ s/\s*$//;
 
 235     next if (!$form->{"qty_$row"});
 
 237     my $bin_idx = get_bin_idx($warehouse_idx, $form->{"src_bin_id_$row"});
 
 238     $form->show_generic_error($locale->text("The selected bin does not exist.")) if (-1 == $bin_idx);
 
 239     my $bin     = $warehouse->{BINS}->[$bin_idx];
 
 241     my $orig_qty = $form->{"qty_$row"} . " " . $form->{"unit_$row"};
 
 244       'src_warehouse_id' => $form->{warehouse_id},
 
 245       'transfer_type_id' => $form->{transfer_type_id},
 
 248     map { $transfer->{$_} = $form->{"${_}_${row}"} } qw(src_bin_id chargenumber bestbefore parts_id qty dst_warehouse_id dst_bin_id);
 
 252     foreach (@contents) {
 
 253       if (($_->{binid} == $transfer->{src_bin_id}) && ($_->{parts_id} == $transfer->{parts_id}) && ($_->{chargenumber} eq $transfer->{chargenumber}) && $_->{bestbefore} eq $transfer->{bestbefore}) {
 
 260       $form->error($locale->text("There is not enough left of '#1' in bin '#2' for the removal of #3.",
 
 261                                  $form->{"partdescription_$row"}, $bin->{description}, $orig_qty));
 
 264     $transfer->{qty}  = $form->parse_amount(\%myconfig, $transfer->{qty}) * $all_units->{$form->{"unit_$row"}}->{factor};
 
 265     $transfer->{qty} /= $all_units->{$entry->{partunit}}->{factor} || 1;
 
 267     if (($entry->{qty} < $transfer->{qty}) || (0 >= $transfer->{qty})) {
 
 268       $form->error($locale->text("There is not enough left of '#1' in bin '#2' for the removal of #3.",
 
 269                                  $form->{"partdescription_$row"}, $bin->{description}, $orig_qty));
 
 272     $transfer->{comment} = $form->{comment};
 
 274     push @transfers, $transfer;
 
 276     $entry->{qty} -= $transfer->{qty};
 
 279   if (!scalar @transfers) {
 
 280     $form->show_generic_information($locale->text('Nothing has been selected for transfer.'));
 
 284   WH->transfer(@transfers);
 
 286   $form->{trans_type}    = 'transfer';
 
 287   $form->{saved_message} = $locale->text('The parts have been transferred.');
 
 289   transfer_warehouse_selection();
 
 291   $main::lxdebug->leave_sub();
 
 294 # --------------------------------------------------------------------
 
 296 # --------------------------------------------------------------------
 
 298 sub transfer_stock_update_part {
 
 299   $main::lxdebug->enter_sub();
 
 301   my $form     = $main::form;
 
 302   my %myconfig = %main::myconfig;
 
 303   my $locale   = $main::locale;
 
 305   $form->{trans_type} = 'stock';
 
 306   $form->{qty}        = $form->parse_amount(\%myconfig, $form->{qty});
 
 308   if (!$form->{partnumber} && !$form->{description} && !$form->{ean}) {
 
 309     delete @{$form}{qw(parts_id partunit ean)};
 
 310     transfer_warehouse_selection();
 
 312   } elsif (($form->{partnumber} && ($form->{partnumber} ne $form->{old_partnumber})) || $form->{description} || $form->{ean}) {
 
 314 #    $form->{no_services}   = 1; # services may now be transfered. fix for Bug 1383.
 
 315     $form->{no_assemblies} = 0; # assemblies duerfen eingelagert werden (z.B. bei retouren)
 
 317     my $parts = Common->retrieve_parts(\%myconfig, $form, 'description', 1);
 
 319     if (!scalar @{ $parts }) {
 
 320       new_item(action => "transfer_stock_update_part");
 
 321     } elsif (scalar @{ $parts } == 1) {
 
 322       @{$form}{qw(parts_id partnumber description ean)} = @{$parts->[0]}{qw(id partnumber description ean)};
 
 323       transfer_stock_get_partunit();
 
 324       transfer_warehouse_selection();
 
 327       select_part('transfer_stock_part_selected', @{ $parts });
 
 331     transfer_stock_get_partunit();
 
 332     transfer_warehouse_selection();
 
 335   $main::lxdebug->leave_sub();
 
 338 # --------------------------------------------------------------------
 
 339 # Transfer: assemblies
 
 340 # Dies ist die Auswahlmaske für ein assembly.
 
 341 # Die ist einfach von transfer_assembly_update_part kopiert und nur um den trans_type (assembly) korrigiert worden
 
 342 # Es wäre schön, hier nochmal check_assembly_max_create auf, um die max. Fertigungszahl herauszufinden.
 
 343 # Ich lass das mal als auskommentierte Idee bestehen jb 18.3.09
 
 344 # --------------------------------------------------------------------
 
 346 sub transfer_assembly_update_part {
 
 347   $main::lxdebug->enter_sub();
 
 349   my $form     = $main::form;
 
 350   my %myconfig = %main::myconfig;
 
 351   my $locale   = $main::locale;
 
 353   $form->{trans_type} = 'assembly';
 
 354   $form->{qty}        = $form->parse_amount(\%myconfig, $form->{qty});
 
 356   if (!$form->{partnumber} && !$form->{description}) {
 
 357     delete @{$form}{qw(parts_id partunit)};
 
 358     transfer_warehouse_selection();
 
 360   } elsif (($form->{partnumber} && ($form->{partnumber} ne $form->{old_partnumber})) || $form->{description}) {
 
 361     $form->{assemblies} = 1;
 
 362     $form->{no_assemblies} = 0;
 
 363     my $parts = Common->retrieve_parts(\%myconfig, $form, 'description', 1);
 
 364     if (scalar @{ $parts } == 1) {
 
 365       @{$form}{qw(parts_id partnumber description)} = @{$parts->[0]}{qw(id partnumber description)};
 
 366       transfer_stock_get_partunit();
 
 367       transfer_warehouse_selection();
 
 369       select_part('transfer_stock_part_selected', @{ $parts });
 
 373     transfer_stock_get_partunit();
 
 374     transfer_warehouse_selection();
 
 377 # hier die oben benannte idee
 
 378 #    my $maxcreate = Common->check_assembly_max_create(assembly_id => $form->{parts_id}, dbh => $my_dbh);
 
 379   $main::lxdebug->leave_sub();
 
 382 sub transfer_stock_part_selected {
 
 383   $main::lxdebug->enter_sub();
 
 387   my $form     = $main::form;
 
 389   @{$form}{qw(parts_id partnumber description ean)} = @{$part}{qw(id partnumber description ean)};
 
 391   transfer_stock_get_partunit();
 
 392   transfer_warehouse_selection();
 
 394   $main::lxdebug->leave_sub();
 
 397 sub transfer_stock_get_partunit {
 
 398   $main::lxdebug->enter_sub();
 
 400   my $form     = $main::form;
 
 402   if ($form->{parts_id}) {
 
 403     my $part_info     = IC->get_basic_part_info('id' => $form->{parts_id});
 
 404     $form->{partunit} = $part_info->{unit};
 
 407   $main::lxdebug->leave_sub();
 
 410 # vorüberlegung jb 22.2.2009
 
 411 # wir benötigen für diese funktion, die anzahl die vom erzeugnis hergestellt werden soll. vielleicht direkt per js fehleingaben verhindern?
 
 412 # ferner dann nochmal mit check_asssembly_max_create gegenprüfen und dann transaktionssicher wegbuchen.
 
 413 # 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
 
 414 # Manko ist derzeit noch, dass unterschiedliche Lagerplätze, bzw. das Quelllager an sich nicht ausgewählt werden können.
 
 415 # Laut Absprache in KW11 09 Ã¼bernimmt mb hier den rest im April ... jb 18.3.09
 
 417 sub create_assembly {
 
 418   $main::lxdebug->enter_sub();
 
 420   my $form     = $main::form;
 
 421   my %myconfig = %main::myconfig;
 
 422   my $locale   = $main::locale;
 
 424   $form->{qty} = $form->parse_amount(\%myconfig, $form->{qty});
 
 425   if ($form->{qty} <= 0) {
 
 426     $form->show_generic_error($locale->text('Invalid quantity.'), 'back_button' => 1);
 
 428   # TODO Es wäre schön, hier schon die maximale Anzahl der zu fertigenden Erzeugnisse zu haben
 
 429   #else { if ($form->{qty} > $maxcreate) { #s.o.
 
 430   #     $form->show_generic_error($locale->text('Can not create that quantity with current stock'), 'back_button' => 1);
 
 431   #     $form->show_generic_error('Maximale Stückzahl' . $maxcreate , 'back_button' => 1);
 
 435   if (!$form->{warehouse_id} || !$form->{bin_id}) {
 
 436     $form->error($locale->text('The warehouse or the bin is missing.'));
 
 439   if (!$main::show_best_before) {
 
 440       $form->{bestbefore} = '';
 
 443   # 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
 
 444   # Anm. jb 18.3. vielleicht auch nur meine unwissenheit in perl-datenstrukturen
 
 446     'transfer_type'    => 'assembly',
 
 447     'login'            => $form->{login},
 
 448     'dst_warehouse_id' => $form->{warehouse_id},
 
 449     'dst_bin_id'       => $form->{bin_id},
 
 450     'chargenumber'     => $form->{chargenumber},
 
 451     'bestbefore'       => $form->{bestbefore},
 
 452     'assembly_id'      => $form->{parts_id},
 
 453     'qty'              => $form->{qty},
 
 454     'unit'             => $form->{unit},
 
 455     'comment'          => $form->{comment}
 
 458   my $ret = WH->transfer_assembly (%TRANSFER);
 
 459   # Frage: Ich pack in den return-wert auch gleich die Fehlermeldung. Irgendwelche Nummern als Fehlerkonstanten definieren find ich auch nicht besonders schick...
 
 462     # 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
 
 463     $form->show_generic_error($ret, 'back_button' => 1);
 
 466   delete @{$form}{qw(parts_id partnumber description qty unit chargenumber bestbefore comment)};
 
 468   $form->{saved_message} = $locale->text('The assembly has been created.');
 
 469   $form->{trans_type}    = 'assembly';
 
 471   transfer_warehouse_selection();
 
 473   $main::lxdebug->leave_sub();
 
 477   $main::lxdebug->enter_sub();
 
 479   my $form     = $main::form;
 
 480   my %myconfig = %main::myconfig;
 
 481   my $locale   = $main::locale;
 
 483   $form->{qty} = $form->parse_amount(\%myconfig, $form->{qty});
 
 485   if ($form->{qty} <= 0) {
 
 486     $form->show_generic_error($locale->text('Invalid quantity.'), 'back_button' => 1);
 
 489   if (!$form->{warehouse_id} || !$form->{bin_id}) {
 
 490     $form->error($locale->text('The warehouse or the bin is missing.'));
 
 494     'transfer_type'    => 'stock',
 
 495     'dst_warehouse_id' => $form->{warehouse_id},
 
 496     'dst_bin_id'       => $form->{bin_id},
 
 497     'chargenumber'     => $form->{chargenumber},
 
 498     'bestbefore'       => $form->{bestbefore},
 
 499     'parts_id'         => $form->{parts_id},
 
 500     'qty'              => $form->{qty},
 
 501     'unit'             => $form->{unit},
 
 502     'comment'          => $form->{comment},
 
 505   WH->transfer($transfer);
 
 507   delete @{$form}{qw(parts_id partnumber description qty unit chargenumber bestbefore comment ean)};
 
 509   $form->{saved_message} = $locale->text('The parts have been stocked.');
 
 510   $form->{trans_type}    = 'stock';
 
 512   transfer_warehouse_selection();
 
 514   $main::lxdebug->leave_sub();
 
 517 # --------------------------------------------------------------------
 
 519 # --------------------------------------------------------------------
 
 521 sub removal_parts_selection {
 
 522   $main::lxdebug->enter_sub();
 
 524   $main::auth->assert('warehouse_management');
 
 526   my $form     = $main::form;
 
 527   my %myconfig = %main::myconfig;
 
 528   my $locale   = $main::locale;
 
 530   transfer_or_removal_prepare_contents('direction' => 'out');
 
 532   $form->{title} = $locale->text('Removal');
 
 534   print $form->parse_html_template("wh/removal_parts_selection");
 
 536   $main::lxdebug->leave_sub();
 
 540   $main::lxdebug->enter_sub();
 
 542   $main::auth->assert('warehouse_management');
 
 544   my $form     = $main::form;
 
 545   my %myconfig = %main::myconfig;
 
 546   my $locale   = $main::locale;
 
 548   $form->get_lists('warehouses' => { 'key'    => 'WAREHOUSES',
 
 549                                      'bins'   => 'BINS', });
 
 551   my $warehouse_idx = get_warehouse_idx($form->{warehouse_id});
 
 552   $form->show_generic_error($locale->text("The selected warehouse does not exist.")) if (-1 == $warehouse_idx);
 
 554   my $warehouse = $form->{WAREHOUSES}->[$warehouse_idx];
 
 556   $form->show_generic_error($locale->text("The warehouse does not contain any bins.")) if (0 == scalar @{ $warehouse->{BINS} });
 
 558   map { $form->{"l_$_"} = 'Y' } qw(parts_id qty warehouseid binid partnumber partdescription bindescription chargenumber bestbefore partunit);
 
 560   $form->{sort} = 'bindescription';
 
 561   my @contents  = WH->get_warehouse_report("warehouse_id" => $form->{warehouse_id});
 
 562   my $all_units = AM->retrieve_units(\%myconfig, $form);
 
 566   foreach my $row (1 .. $form->{rowcount}) {
 
 567     $form->{"qty_$row"} =~ s/^\s*//;
 
 568     $form->{"qty_$row"} =~ s/\s*$//;
 
 569     next if (!$form->{"qty_$row"});
 
 571     my $bin_idx = get_bin_idx($warehouse_idx, $form->{"src_bin_id_$row"});
 
 572     $form->show_generic_error($locale->text("The selected bin does not exist.")) if (-1 == $bin_idx);
 
 573     my $bin     = $warehouse->{BINS}->[$bin_idx];
 
 575     my $orig_qty = $form->{"qty_$row"} . " " . $form->{"unit_$row"};
 
 578       'src_warehouse_id' => $form->{warehouse_id},
 
 579       'transfer_type_id' => $form->{transfer_type_id},
 
 582     map { $transfer->{$_} = $form->{"${_}_${row}"} } qw(src_bin_id chargenumber bestbefore parts_id qty);
 
 586     foreach (@contents) {
 
 587       if (($_->{binid} == $transfer->{src_bin_id}) && ($_->{parts_id} == $transfer->{parts_id}) && ($_->{chargenumber} eq $transfer->{chargenumber}) && ($_->{bestbefore} eq $transfer->{bestbefore})) {
 
 594       $form->error($locale->text("There is not enough left of '#1' in bin '#2' for the removal of #3.",
 
 595                                  $form->{"partdescription_$row"}, $bin->{description}, $orig_qty));
 
 598     $transfer->{qty}  = $form->parse_amount(\%myconfig, $transfer->{qty}) * $all_units->{$form->{"unit_$row"}}->{factor};
 
 599     $transfer->{qty} /= $all_units->{$entry->{partunit}}->{factor} || 1;
 
 601     if (($entry->{qty} < $transfer->{qty}) || (0 >= $transfer->{qty})) {
 
 602       $form->error($locale->text("There is not enough left of '#1' in bin '#2' for the removal of #3.",
 
 603                                  $form->{"partdescription_$row"}, $bin->{description}, $orig_qty));
 
 606     $transfer->{comment} = $form->{comment};
 
 608     push @transfers, $transfer;
 
 610     $entry->{qty} -= $transfer->{qty};
 
 613   if (!scalar @transfers) {
 
 614     $form->show_generic_information($locale->text('Nothing has been selected for removal.'));
 
 618   WH->transfer(@transfers);
 
 620   $form->{trans_type}    = 'removal';
 
 621   $form->{saved_message} = $locale->text('The parts have been removed.');
 
 623   transfer_warehouse_selection();
 
 625   $main::lxdebug->leave_sub();
 
 628 # --------------------------------------------------------------------
 
 630 # --------------------------------------------------------------------
 
 633   $main::lxdebug->enter_sub();
 
 635   $main::auth->assert('warehouse_management');
 
 637   my $form     = $main::form;
 
 638   my %myconfig = %main::myconfig;
 
 639   my $locale   = $main::locale;
 
 641   $form->{title} = $locale->text('Report about warehouse transactions');
 
 642   $form->get_lists('warehouses' => { 'key'  => 'WAREHOUSES',
 
 643                                      'bins' => 'BINS', });
 
 645   show_no_warehouses_error() if (!scalar @{ $form->{WAREHOUSES} });
 
 647   $form->{jsscript} = 1;
 
 650   print $form->parse_html_template("wh/journal_filter", { "UNITS" => AM->unit_select_data(AM->retrieve_units(\%myconfig, $form)) });
 
 652   $main::lxdebug->leave_sub();
 
 655 sub generate_journal {
 
 656   $main::lxdebug->enter_sub();
 
 658   $main::auth->assert('warehouse_management');
 
 660   my $form     = $main::form;
 
 661   my %myconfig = %main::myconfig;
 
 662   my $locale   = $main::locale;
 
 664   $form->{title}   = $locale->text("WHJournal");
 
 665   $form->{sort}  ||= 'date';
 
 668   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);
 
 671   map { $filter{$_} = $form->{$_} if ($form->{$_}) } qw(warehouse_id bin_id partnumber description chargenumber bestbefore);
 
 673   $filter{qty_op} = WH->convert_qty_op($form->{qty_op});
 
 674   if ($filter{qty_op}) {
 
 675     $form->isblank("qty",      $locale->text('Quantity missing.'));
 
 676     $form->isblank("qty_unit", $locale->text('Unit missing.'));
 
 678     $filter{qty}      = $form->{qty};
 
 679     $filter{qty_unit} = $form->{qty_unit};
 
 683   my $report = SL::ReportGenerator->new(\%myconfig, $form);
 
 685   my @hidden_variables = map { "l_${_}" } @columns;
 
 686   push @hidden_variables, qw(warehouse_id bin_id partnumber description chargenumber bestbefore qty_op qty qty_unit fromdate todate);
 
 689     'date'            => { 'text' => $locale->text('Date'), },
 
 690     'trans_id'        => { 'text' => $locale->text('Trans Id'), },
 
 691     'trans_type'      => { 'text' => $locale->text('Trans Type'), },
 
 692     'comment'         => { 'text' => $locale->text('Comment'), },
 
 693     'warehouse_from'  => { 'text' => $locale->text('Warehouse From'), },
 
 694     'warehouse_to'    => { 'text' => $locale->text('Warehouse To'), },
 
 695     'bin_from'        => { 'text' => $locale->text('Bin From'), },
 
 696     'bin_to'          => { 'text' => $locale->text('Bin To'), },
 
 697     'partnumber'      => { 'text' => $locale->text('Part Number'), },
 
 698     'partdescription' => { 'text' => $locale->text('Part Description'), },
 
 699     'chargenumber'    => { 'text' => $locale->text('Charge Number'), },
 
 700     'bestbefore'      => { 'text' => $locale->text('Best Before'), },
 
 701     'qty'             => { 'text' => $locale->text('Qty'), },
 
 702     'employee'        => { 'text' => $locale->text('Employee'), },
 
 703     'projectnumber'   => { 'text' => $locale->text('Project Number'), },
 
 704     'oe_id'           => { 'text' => $locale->text('Document'), },
 
 707   my $href = build_std_url('action=generate_journal', grep { $form->{$_} } @hidden_variables);
 
 708   map { $column_defs{$_}->{link} = $href . "&sort=${_}&order=" . Q($_ eq $form->{sort} ? 1 - $form->{order} : $form->{order}) } @columns;
 
 710   my %column_alignment = map { $_ => 'right' } qw(qty);
 
 712   map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
 
 714   $report->set_columns(%column_defs);
 
 715   $report->set_column_order(@columns);
 
 717   $report->set_export_options('generate_journal', @hidden_variables, qw(sort order));
 
 719   $report->set_sort_indicator($form->{sort}, $form->{order});
 
 721   $report->set_options('output_format'        => 'HTML',
 
 722                        'title'                => $form->{title},
 
 723                        'attachment_basename'  => strftime($locale->text('warehouse_journal_list') . '_%Y%m%d', localtime time));
 
 724   $report->set_options_from_form();
 
 725   $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
 
 727   my $all_units = AM->retrieve_units(\%myconfig, $form);
 
 728   my @contents  = WH->get_warehouse_journal(%filter);
 
 730   my %doc_types = ( 'sales_quotation'         => { script => 'oe', title => $locale->text('Sales quotation') },
 
 731                     'sales_order'             => { script => 'oe', title => $locale->text('Sales Order') },
 
 732                     'request_quotation'       => { script => 'oe', title => $locale->text('Request quotation') },
 
 733                     'purchase_order'          => { script => 'oe', title => $locale->text('Purchase Order') },
 
 734                     'sales_delivery_order'    => { script => 'do', title => $locale->text('Sales delivery order') },
 
 735                     'purchase_delivery_order' => { script => 'do', title => $locale->text('Purchase delivery order') },
 
 736                     'sales_invoice'           => { script => 'is', title => $locale->text('Sales Invoice') },
 
 737                     'purchase_invoice'        => { script => 'ir', title => $locale->text('Purchase Invoice') },
 
 740   foreach my $entry (@contents) {
 
 741     $entry->{qty}        = $form->format_amount_units('amount'     => $entry->{qty},
 
 742                                                       'part_unit'  => $entry->{partunit},
 
 743                                                       'conv_units' => 'convertible');
 
 744     $entry->{trans_type} = $locale->text($entry->{trans_type});
 
 748     foreach my $column (@columns) {
 
 750         'data'  => $entry->{$column},
 
 751         'align' => $column_alignment{$column},
 
 755     $row->{trans_type}->{raw_data} = $entry->{trans_type};
 
 757     if ($form->{l_oe_id}) {
 
 758       $row->{oe_id}->{data} = '';
 
 759       my $info              = $entry->{oe_id_info};
 
 761       if ($info && $info->{id} && $info->{type} && $doc_types{$info->{type}}) {
 
 762         $row->{oe_id} = { data => $doc_types{ $info->{type} }->{title} . ' ' . $info->{number},
 
 763                           link => build_std_url('script=' . $doc_types{ $info->{type} }->{script} . '.pl', 'action=edit', 'id=' . $info->{id}, 'type=' . $info->{type}) };
 
 767     $report->add_data($row);
 
 770   $report->generate_with_headers();
 
 772   $main::lxdebug->leave_sub();
 
 775 # --------------------------------------------------------------------
 
 777 # --------------------------------------------------------------------
 
 780   $main::lxdebug->enter_sub();
 
 782   $main::auth->assert('warehouse_contents | warehouse_management');
 
 784   my $form     = $main::form;
 
 785   my %myconfig = %main::myconfig;
 
 786   my $locale   = $main::locale;
 
 788   $form->get_lists('warehouses' => { 'key'    => 'WAREHOUSES',
 
 789                                      'bins'   => 'BINS', });
 
 791   show_no_warehouses_error() if (!scalar @{ $form->{WAREHOUSES} });
 
 793   $form->{jsscript} = 1;
 
 795 #  $form->{fokus}   = "partnumber";
 
 796 #  $form->{onload} .= "focus();";
 
 797   $form->{title}   = $locale->text("Report about warehouse contents");
 
 800   print $form->parse_html_template("wh/report_filter",
 
 801                                    { "nextsub"    => "generate_report",
 
 802                                      "WAREHOUSES" => $form->{WAREHOUSES},
 
 803                                      "UNITS"      => AM->unit_select_data(AM->retrieve_units(\%myconfig, $form)) });
 
 805   $main::lxdebug->leave_sub();
 
 808 sub generate_report {
 
 809   $main::lxdebug->enter_sub();
 
 811   $main::auth->assert('warehouse_contents | warehouse_management');
 
 813   my $form     = $main::form;
 
 814   my %myconfig = %main::myconfig;
 
 815   my $locale   = $main::locale;
 
 817   $form->{title}   = $locale->text("Report about warehouse contents");
 
 818   $form->{sort}  ||= 'partnumber';
 
 819   my $sort_col     = $form->{sort};
 
 822   my @columns = qw(warehousedescription bindescription partnumber partdescription chargenumber bestbefore qty stock_value);
 
 825   map { $filter{$_} = $form->{$_} if ($form->{$_}) } qw(warehouse_id bin_id partnumber description chargenumber bestbefore);
 
 827   $filter{qty_op} = WH->convert_qty_op($form->{qty_op});
 
 828   if ($filter{qty_op}) {
 
 829     $form->isblank("qty",      $locale->text('Quantity missing.'));
 
 830     $form->isblank("qty_unit", $locale->text('Unit missing.'));
 
 832     $filter{qty}      = $form->{qty};
 
 833     $filter{qty_unit} = $form->{qty_unit};
 
 837   $form->{subtotal} = '' if (!first { $_ eq $sort_col } qw(partnumber partdescription));
 
 839   my $report = SL::ReportGenerator->new(\%myconfig, $form);
 
 841   my @hidden_variables = map { "l_${_}" } @columns;
 
 842   push @hidden_variables, qw(warehouse_id bin_id partnumber description chargenumber bestbefore qty_op qty qty_unit l_warehousedescription l_bindescription);
 
 843   push @hidden_variables, qw(include_empty_bins subtotal);
 
 846     'warehousedescription' => { 'text' => $locale->text('Warehouse'), },
 
 847     'bindescription'       => { 'text' => $locale->text('Bin'), },
 
 848     'partnumber'           => { 'text' => $locale->text('Part Number'), },
 
 849     'partdescription'      => { 'text' => $locale->text('Part Description'), },
 
 850     'chargenumber'         => { 'text' => $locale->text('Charge Number'), },
 
 851     'bestbefore'           => { 'text' => $locale->text('Best Before'), },
 
 852     'qty'                  => { 'text' => $locale->text('Qty'), },
 
 853     'stock_value'          => { 'text' => $locale->text('Stock value'), },
 
 856   my $href = build_std_url('action=generate_report', grep { $form->{$_} } @hidden_variables);
 
 857   map { $column_defs{$_}->{link} = $href . "&sort=${_}&order=" . Q($_ eq $sort_col ? 1 - $form->{order} : $form->{order}) } @columns;
 
 859   my %column_alignment = map { $_ => 'right' } qw(qty stock_value);
 
 861   map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
 
 863   $report->set_columns(%column_defs);
 
 864   $report->set_column_order(@columns);
 
 866   $report->set_export_options('generate_report', @hidden_variables, qw(sort order));
 
 868   $report->set_sort_indicator($sort_col, $form->{order});
 
 870   $report->set_options('output_format'        => 'HTML',
 
 871                        'title'                => $form->{title},
 
 872                        'attachment_basename'  => strftime($locale->text('warehouse_report_list') . '_%Y%m%d', localtime time));
 
 873   $report->set_options_from_form();
 
 874   $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
 
 876   my $all_units = AM->retrieve_units(\%myconfig, $form);
 
 877   my @contents  = WH->get_warehouse_report(%filter);
 
 881   my @subtotals_columns = qw(qty stock_value);
 
 882   my %subtotals         = map { $_ => 0 } @subtotals_columns;
 
 884   my $total_stock_value = 0;
 
 886   foreach my $entry (@contents) {
 
 887     map { $subtotals{$_} += $entry->{$_} } @subtotals_columns;
 
 888     $total_stock_value   += $entry->{stock_value} * 1;
 
 890     $entry->{qty}         = $form->format_amount_units('amount'     => $entry->{qty},
 
 891                                                        'part_unit'  => $entry->{partunit},
 
 892                                                        'conv_units' => 'convertible');
 
 893     $entry->{stock_value} = $form->format_amount(\%myconfig, $entry->{stock_value} * 1, 2);
 
 895     my $row_set = [ { map { $_ => { 'data' => $entry->{$_}, 'align' => $column_alignment{$_} } } @columns } ];
 
 897     if (($form->{subtotal} eq 'Y')
 
 898         && (($idx == (scalar @contents - 1))
 
 899             || ($entry->{$sort_col} ne $contents[$idx + 1]->{$sort_col}))) {
 
 901       my $row = { map { $_ => { 'data' => '', 'class' => 'listsubtotal', 'align' => $column_alignment{$_}, } } @columns };
 
 902       $row->{qty}->{data}         = $form->format_amount_units('amount'     => $subtotals{qty} * 1,
 
 903                                                                'part_unit'  => $entry->{partunit},
 
 904                                                                'conv_units' => 'convertible');
 
 905       $row->{stock_value}->{data} = $form->format_amount(\%myconfig, $subtotals{stock_value} * 1, 2);
 
 907       %subtotals                  = map { $_ => 0 } @subtotals_columns;
 
 909       push @{ $row_set }, $row;
 
 912     $report->add_data($row_set);
 
 917   if ($column_defs{stock_value}->{visible}) {
 
 918     $report->add_separator();
 
 920     my $row                      = { map { $_ => { 'data' => '', 'class' => 'listsubtotal', } } @columns };
 
 922     my $left_col                 = first { $column_defs{$_}->{visible} } @columns;
 
 924     $row->{$left_col}->{data}    = $locale->text('Total stock value');
 
 925     $row->{stock_value}->{data}  = $form->format_amount(\%myconfig, $total_stock_value, 2);
 
 926     $row->{stock_value}->{align} = 'right';
 
 928     $report->add_data($row);
 
 931   $report->generate_with_headers();
 
 933   $main::lxdebug->leave_sub();
 
 936 # --------------------------------------------------------------------
 
 938 # --------------------------------------------------------------------
 
 940 sub show_no_warehouses_error {
 
 941   $main::lxdebug->enter_sub();
 
 943   my $form     = $main::form;
 
 944   my %myconfig = %main::myconfig;
 
 945   my $locale   = $main::locale;
 
 947   my $msg = $locale->text('No warehouse has been created yet or the quantity of the bins is not configured yet.') . ' ';
 
 949   if ($main::auth->check_right($form->{login}, 'config')) {
 
 950     $msg .= $locale->text('You can create warehouses and bins via the menu "System -> Warehouses".');
 
 952     $msg .= $locale->text('Please ask your administrator to create warehouses and bins.');
 
 955   $form->show_generic_error($msg);
 
 957   $main::lxdebug->leave_sub();
 
 960 sub get_warehouse_idx {
 
 961   my ($warehouse_id) = @_;
 
 963   my $form     = $main::form;
 
 965   for (my $i = 0; $i < scalar @{$form->{WAREHOUSES}}; $i++) {
 
 966     return $i if ($form->{WAREHOUSES}->[$i]->{id} == $warehouse_id);
 
 973   my ($warehouse_index, $bin_id) = @_;
 
 975   my $form     = $main::form;
 
 977   my $warehouse = $form->{WAREHOUSES}->[$warehouse_index];
 
 979   return -1 if (!$warehouse);
 
 981   for (my $i = 0; $i < scalar @{ $warehouse->{BINS} }; $i++) {
 
 982     return $i if ($warehouse->{BINS}->[$i]->{id} == $bin_id);
 
 989   $main::lxdebug->enter_sub();
 
 992   my $form     = $main::form;
 
 995   $form->{old_callback} = $form->escape($form->{callback}, 1);
 
 996   $form->{callback}     = $form->escape("$form->{script}?action=$params{action}", 1);
 
 998   # save all form variables except action in a previousform variable
 
 999   my $previousform = join '&', map { my $value = $form->{$_}; $value =~ s/&/%26/; "$_=$value" } grep { !/action/ } keys %$form;
 
1002 #  push @HIDDENS,      { 'name' => 'previousform', 'value' => $form->escape($previousform, 1) };
 
1003   push @HIDDENS, map +{ 'name' => $_,             'value' => $form->{$_} }, qw(partnumber description unit vc sellprice ean);
 
1004   push @HIDDENS,      { 'name' => 'taxaccount2',  'value' => $form->{taxaccounts} };
 
1005   push @HIDDENS,      { 'name' => 'notes',        'value' => $form->{longdescription} };
 
1008   print $form->parse_html_template("generic/new_item", { HIDDENS => [ sort { $a->{name} cmp $b->{name} } @HIDDENS ] } );
 
1010   $main::lxdebug->leave_sub();
 
1014   my $form     = $main::form;
 
1015   call_sub($form->{update_nextsub} || $form->{nextsub});
 
1019   my $form     = $main::form;
 
1020   call_sub($form->{continue_nextsub} || $form->{nextsub});
 
1024   my $form     = $main::form;
 
1025   call_sub($form->{stock_nextsub} || $form->{nextsub});
 
1034 bin/mozilla/wh.pl - Warehouse frontend.
 
1042 call new item dialogue from warehouse masks.
 
1045   action  => name of sub to be called when new item is done