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   $form->{UNITS} = AM->unit_select_data($units, $form->{unit}, 0, $form->{partunit});
 
  90   if (scalar @{ $form->{WAREHOUSES} }) {
 
  91     $form->{warehouse_id} ||= $form->{WAREHOUSES}->[0]->{id};
 
  92     $form->{bin_id}       ||= $form->{WAREHOUSES}->[0]->{BINS}->[0]->{id};
 
  97   $form->{jsscript} = 1;
 
  99   if ($form->{trans_type} eq 'removal') {
 
 100     $form->{nextsub} = "removal_parts_selection";
 
 101     $form->{title}   = $locale->text('Removal from Warehouse');
 
 102     $content         = $form->parse_html_template('wh/warehouse_selection');
 
 104   } elsif ($form->{trans_type} eq 'stock') {
 
 105     $form->{title} = $locale->text('Stock');
 
 106     $content       = $form->parse_html_template('wh/warehouse_selection_stock');
 
 108   } elsif (!$form->{trans_type} || ($form->{trans_type} eq 'transfer')) {
 
 109     $form->{nextsub} = "transfer_parts_selection";
 
 110     $form->{title}   = $locale->text('Transfer');
 
 111     $content         = $form->parse_html_template('wh/warehouse_selection');
 
 113   } elsif ($form->{trans_type} eq 'assembly') {
 
 114     $form->{title} = $locale->text('Produce Assembly');
 
 115     $content       = $form->parse_html_template('wh/warehouse_selection_assembly');
 
 121   $main::lxdebug->leave_sub();
 
 124 sub transfer_parts_selection {
 
 125   $main::lxdebug->enter_sub();
 
 127   $main::auth->assert('warehouse_management');
 
 129   my $form     = $main::form;
 
 130   my %myconfig = %main::myconfig;
 
 131   my $locale   = $main::locale;
 
 133   transfer_or_removal_prepare_contents('direction' => 'transfer');
 
 135   $form->{title} = $locale->text('Transfer');
 
 137   print $form->parse_html_template("wh/transfer_parts_selection");
 
 139   $main::lxdebug->leave_sub();
 
 142 sub transfer_or_removal_prepare_contents {
 
 143   $main::lxdebug->enter_sub();
 
 145   $main::auth->assert('warehouse_management');
 
 149   my $form     = $main::form;
 
 150   my %myconfig = %main::myconfig;
 
 151   my $locale   = $main::locale;
 
 153   $form->get_lists('warehouses' => { 'key'    => 'WAREHOUSES',
 
 154                                      'bins'   => 'BINS', });
 
 156   my $warehouse_idx = get_warehouse_idx($form->{warehouse_id});
 
 157   $form->show_generic_error($locale->text("The selected warehouse does not exist.")) if (-1 == $warehouse_idx);
 
 159   my $warehouse = $form->{WAREHOUSES}->[$warehouse_idx];
 
 161   $form->{initial_warehouse_idx} = $warehouse_idx;
 
 162   $form->{warehouse_description} = $warehouse->{description};
 
 163   $warehouse->{selected}         = 1;
 
 165   $form->show_generic_error($locale->text("The source warehouse does not contain any bins.")) if (0 == scalar @{ $warehouse->{BINS} });
 
 167   map { $form->{"l_$_"} = 'Y' } qw(parts_id qty warehouseid binid partnumber partdescription bindescription chargenumber partunit ean);
 
 169   $form->{sort} = 'bindescription';
 
 170   my @contents  = WH->get_warehouse_report("warehouse_id" => $form->{warehouse_id},
 
 171                                            "bin_id"       => $form->{bin_id},
 
 172                                            "chargenumber" => $form->{chargenumber},
 
 173                                            "partnumber"   => $form->{partnumber},
 
 174                                            "ean"          => $form->{ean},
 
 175                                            "description"  => $form->{description});
 
 177   $form->show_generic_error($locale->text("The selected warehouse is empty.")) if (0 == scalar(@contents));
 
 179   my $all_units = AM->retrieve_units(\%myconfig, $form);
 
 181   foreach (@contents) {
 
 182     $_->{qty} = $form->format_amount_units('amount'     => $_->{qty},
 
 183                                            'part_unit'  => $_->{partunit},
 
 184                                            'conv_units' => 'convertible');
 
 185     my $this_unit = $_->{partunit};
 
 187     if ($all_units->{$_->{partunit}} && ($all_units->{g}->{base_unit} eq $all_units->{$_->{partunit}}->{base_unit})) {
 
 191     $_->{UNITS} = AM->unit_select_data($all_units, $this_unit, 0, $_->{partunit});
 
 194   my $transfer_types = WH->retrieve_transfer_types($args{direction});
 
 195   map { $_->{description} = $locale->text($_->{description}) } @{ $transfer_types };
 
 197   $form->{CONTENTS}       = \@contents;
 
 198   $form->{TRANSFER_TYPES} = $transfer_types;
 
 200   $main::lxdebug->leave_sub();
 
 205   $main::lxdebug->enter_sub();
 
 207   $main::auth->assert('warehouse_management');
 
 209   my $form     = $main::form;
 
 210   my %myconfig = %main::myconfig;
 
 211   my $locale   = $main::locale;
 
 213   $form->get_lists('warehouses' => { 'key' => 'WAREHOUSES', 'bins' => 'BINS' });
 
 215   my $warehouse_idx = get_warehouse_idx($form->{warehouse_id});
 
 216   $form->show_generic_error($locale->text("The selected warehouse does not exist.")) if (-1 == $warehouse_idx);
 
 218   my $warehouse = $form->{WAREHOUSES}->[$warehouse_idx];
 
 220   $form->show_generic_error($locale->text("The source warehouse does not contain any bins.")) if (0 == scalar @{ $warehouse->{BINS} });
 
 222   map { $form->{"l_$_"} = 'Y' } qw(parts_id qty warehouseid binid partnumber partdescription bindescription chargenumber partunit);
 
 224   $form->{sort} = 'bindescription';
 
 225   my @contents  = WH->get_warehouse_report("warehouse_id" => $form->{warehouse_id});
 
 226   my $all_units = AM->retrieve_units(\%myconfig, $form);
 
 230   foreach my $row (1 .. $form->{rowcount}) {
 
 231     $form->{"qty_$row"} =~ s/^\s*//;
 
 232     $form->{"qty_$row"} =~ s/\s*$//;
 
 233     next if (!$form->{"qty_$row"});
 
 235     my $bin_idx = get_bin_idx($warehouse_idx, $form->{"src_bin_id_$row"});
 
 236     $form->show_generic_error($locale->text("The selected bin does not exist.")) if (-1 == $bin_idx);
 
 237     my $bin     = $warehouse->{BINS}->[$bin_idx];
 
 239     my $orig_qty = $form->{"qty_$row"} . " " . $form->{"unit_$row"};
 
 242       'src_warehouse_id' => $form->{warehouse_id},
 
 243       'transfer_type_id' => $form->{transfer_type_id},
 
 246     map { $transfer->{$_} = $form->{"${_}_${row}"} } qw(src_bin_id chargenumber parts_id qty dst_warehouse_id dst_bin_id);
 
 250     foreach (@contents) {
 
 251       if (($_->{binid} == $transfer->{src_bin_id}) && ($_->{parts_id} == $transfer->{parts_id}) && ($_->{chargenumber} eq $transfer->{chargenumber})) {
 
 258       $form->error($locale->text("There is not enough left of '#1' in bin '#2' for the removal of #3.",
 
 259                                  $form->{"partdescription_$row"}, $bin->{description}, $orig_qty));
 
 262     $transfer->{qty}  = $form->parse_amount(\%myconfig, $transfer->{qty}) * $all_units->{$form->{"unit_$row"}}->{factor};
 
 263     $transfer->{qty} /= $all_units->{$entry->{partunit}}->{factor} || 1;
 
 265     if (($entry->{qty} < $transfer->{qty}) || (0 >= $transfer->{qty})) {
 
 266       $form->error($locale->text("There is not enough left of '#1' in bin '#2' for the removal of #3.",
 
 267                                  $form->{"partdescription_$row"}, $bin->{description}, $orig_qty));
 
 270     $transfer->{comment} = $form->{comment};
 
 272     push @transfers, $transfer;
 
 274     $entry->{qty} -= $transfer->{qty};
 
 277   if (!scalar @transfers) {
 
 278     $form->show_generic_information($locale->text('Nothing has been selected for transfer.'));
 
 282   WH->transfer(@transfers);
 
 284   $form->{trans_type}    = 'transfer';
 
 285   $form->{saved_message} = $locale->text('The parts have been transferred.');
 
 287   transfer_warehouse_selection();
 
 289   $main::lxdebug->leave_sub();
 
 292 # --------------------------------------------------------------------
 
 294 # --------------------------------------------------------------------
 
 296 sub transfer_stock_update_part {
 
 297   $main::lxdebug->enter_sub();
 
 299   my $form     = $main::form;
 
 300   my %myconfig = %main::myconfig;
 
 301   my $locale   = $main::locale;
 
 303   $form->{trans_type} = 'stock';
 
 304   $form->{qty}        = $form->parse_amount(\%myconfig, $form->{qty});
 
 306   if (!$form->{partnumber} && !$form->{description} && !$form->{ean}) {
 
 307     delete @{$form}{qw(parts_id partunit ean)};
 
 308     transfer_warehouse_selection();
 
 310   } elsif (($form->{partnumber} && ($form->{partnumber} ne $form->{old_partnumber})) || $form->{description} || $form->{ean}) {
 
 312     $form->{no_services}   = 1;
 
 313     $form->{no_assemblies} = 0; # assemblies duerfen eingelagert werden (z.B. bei retouren)
 
 315     my $parts = Common->retrieve_parts(\%myconfig, $form, 'description', 1);
 
 317     if (!scalar @{ $parts }) {
 
 318       new_item(action => "transfer_stock_update_part");
 
 319     } elsif (scalar @{ $parts } == 1) {
 
 320       @{$form}{qw(parts_id partnumber description ean)} = @{$parts->[0]}{qw(id partnumber description ean)};
 
 321       transfer_stock_get_partunit();
 
 322       transfer_warehouse_selection();
 
 325       select_part('transfer_stock_part_selected', @{ $parts });
 
 329     transfer_stock_get_partunit();
 
 330     transfer_warehouse_selection();
 
 333   $main::lxdebug->leave_sub();
 
 336 # --------------------------------------------------------------------
 
 337 # Transfer: assemblies
 
 338 # Dies ist die Auswahlmaske für ein assembly.
 
 339 # Die ist einfach von transfer_assembly_update_part kopiert und nur um den trans_type (assembly) korrigiert worden
 
 340 # Es wäre schön, hier nochmal check_assembly_max_create auf, um die max. Fertigungszahl herauszufinden.
 
 341 # Ich lass das mal als auskommentierte Idee bestehen jb 18.3.09
 
 342 # --------------------------------------------------------------------
 
 344 sub transfer_assembly_update_part {
 
 345   $main::lxdebug->enter_sub();
 
 347   my $form     = $main::form;
 
 348   my %myconfig = %main::myconfig;
 
 349   my $locale   = $main::locale;
 
 351   $form->{trans_type} = 'assembly';
 
 352   $form->{qty}        = $form->parse_amount(\%myconfig, $form->{qty});
 
 354   if (!$form->{partnumber} && !$form->{description}) {
 
 355     delete @{$form}{qw(parts_id partunit)};
 
 356     transfer_warehouse_selection();
 
 358   } elsif (($form->{partnumber} && ($form->{partnumber} ne $form->{old_partnumber})) || $form->{description}) {
 
 359     $form->{assemblies} = 1;
 
 360     $form->{no_assemblies} = 0;
 
 361     my $parts = Common->retrieve_parts(\%myconfig, $form, 'description', 1);
 
 362     if (scalar @{ $parts } == 1) {
 
 363       @{$form}{qw(parts_id partnumber description)} = @{$parts->[0]}{qw(id partnumber description)};
 
 364       transfer_stock_get_partunit();
 
 365       transfer_warehouse_selection();
 
 367       select_part('transfer_stock_part_selected', @{ $parts });
 
 371     transfer_stock_get_partunit();
 
 372     transfer_warehouse_selection();
 
 375 # hier die oben benannte idee
 
 376 #    my $maxcreate = Common->check_assembly_max_create(assembly_id => $form->{parts_id}, dbh => $my_dbh);
 
 377   $main::lxdebug->leave_sub();
 
 380 sub transfer_stock_part_selected {
 
 381   $main::lxdebug->enter_sub();
 
 385   my $form     = $main::form;
 
 387   @{$form}{qw(parts_id partnumber description ean)} = @{$part}{qw(id partnumber description ean)};
 
 389   transfer_stock_get_partunit();
 
 390   transfer_warehouse_selection();
 
 392   $main::lxdebug->leave_sub();
 
 395 sub transfer_stock_get_partunit {
 
 396   $main::lxdebug->enter_sub();
 
 398   my $form     = $main::form;
 
 400   if ($form->{parts_id}) {
 
 401     my $part_info     = IC->get_basic_part_info('id' => $form->{parts_id});
 
 402     $form->{partunit} = $part_info->{unit};
 
 405   $main::lxdebug->leave_sub();
 
 408 # vorüberlegung jb 22.2.2009
 
 409 # wir benötigen für diese funktion, die anzahl die vom erzeugnis hergestellt werden soll. vielleicht direkt per js fehleingaben verhindern?
 
 410 # ferner dann nochmal mit check_asssembly_max_create gegenprüfen und dann transaktionssicher wegbuchen.
 
 411 # 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
 
 412 # Manko ist derzeit noch, dass unterschiedliche Lagerplätze, bzw. das Quelllager an sich nicht ausgewählt werden können.
 
 413 # Laut Absprache in KW11 09 Ã¼bernimmt mb hier den rest im April ... jb 18.3.09
 
 415 sub create_assembly {
 
 416   $main::lxdebug->enter_sub();
 
 418   my $form     = $main::form;
 
 419   my %myconfig = %main::myconfig;
 
 420   my $locale   = $main::locale;
 
 422   $form->{qty} = $form->parse_amount(\%myconfig, $form->{qty});
 
 423   if ($form->{qty} <= 0) {
 
 424     $form->show_generic_error($locale->text('Invalid quantity.'), 'back_button' => 1);
 
 426   # TODO Es wäre schön, hier schon die maximale Anzahl der zu fertigenden Erzeugnisse zu haben
 
 427   #else { if ($form->{qty} > $maxcreate) {      #s.o.
 
 428   #         $form->show_generic_error($locale->text('Can not create that quantity with current stock'), 'back_button' => 1);
 
 429   #         $form->show_generic_error('Maximale Stückzahl' . $maxcreate , 'back_button' => 1);
 
 433   if (!$form->{warehouse_id} || !$form->{bin_id}) {
 
 434     $form->error($locale->text('The warehouse or the bin is missing.'));
 
 436   # 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
 
 437   # Anm. jb 18.3. vielleicht auch nur meine unwissenheit in perl-datenstrukturen
 
 439     'transfer_type'    => 'assembly',
 
 440     'login'                => $form->{login},
 
 441     'dst_warehouse_id' => $form->{warehouse_id},
 
 442     'dst_bin_id'       => $form->{bin_id},
 
 443     'chargenumber'     => $form->{chargenumber},
 
 444     'assembly_id'      => $form->{parts_id},
 
 445     'qty'              => $form->{qty},
 
 446     'unit'             => $form->{unit},
 
 447     'comment'          => $form->{comment}
 
 450   my $ret = WH->transfer_assembly (%TRANSFER);
 
 451   # Frage: Ich pack in den return-wert auch gleich die Fehlermeldung. Irgendwelche Nummern als Fehlerkonstanten definieren find ich auch nicht besonders schick...
 
 454     # 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
 
 455     $form->show_generic_error($ret, 'back_button' => 1);
 
 458   delete @{$form}{qw(parts_id partnumber description qty unit chargenumber comment)};
 
 460   $form->{saved_message} = $locale->text('The assembly has been created.');
 
 461   $form->{trans_type}    = 'assembly';
 
 463   transfer_warehouse_selection();
 
 465   $main::lxdebug->leave_sub();
 
 469   $main::lxdebug->enter_sub();
 
 471   my $form     = $main::form;
 
 472   my %myconfig = %main::myconfig;
 
 473   my $locale   = $main::locale;
 
 475   $form->{qty} = $form->parse_amount(\%myconfig, $form->{qty});
 
 477   if ($form->{qty} <= 0) {
 
 478     $form->show_generic_error($locale->text('Invalid quantity.'), 'back_button' => 1);
 
 481   if (!$form->{warehouse_id} || !$form->{bin_id}) {
 
 482     $form->error($locale->text('The warehouse or the bin is missing.'));
 
 486     'transfer_type'    => 'stock',
 
 487     'dst_warehouse_id' => $form->{warehouse_id},
 
 488     'dst_bin_id'       => $form->{bin_id},
 
 489     'chargenumber'     => $form->{chargenumber},
 
 490     'parts_id'         => $form->{parts_id},
 
 491     'qty'              => $form->{qty},
 
 492     'unit'             => $form->{unit},
 
 493     'comment'          => $form->{comment},
 
 496   WH->transfer($transfer);
 
 498   delete @{$form}{qw(parts_id partnumber description qty unit chargenumber comment)};
 
 500   $form->{saved_message} = $locale->text('The parts have been stocked.');
 
 501   $form->{trans_type}    = 'stock';
 
 503   transfer_warehouse_selection();
 
 505   $main::lxdebug->leave_sub();
 
 508 # --------------------------------------------------------------------
 
 510 # --------------------------------------------------------------------
 
 512 sub removal_parts_selection {
 
 513   $main::lxdebug->enter_sub();
 
 515   $main::auth->assert('warehouse_management');
 
 517   my $form     = $main::form;
 
 518   my %myconfig = %main::myconfig;
 
 519   my $locale   = $main::locale;
 
 521   transfer_or_removal_prepare_contents('direction' => 'out');
 
 523   $form->{title} = $locale->text('Removal');
 
 525   print $form->parse_html_template("wh/removal_parts_selection");
 
 527   $main::lxdebug->leave_sub();
 
 531   $main::lxdebug->enter_sub();
 
 533   $main::auth->assert('warehouse_management');
 
 535   my $form     = $main::form;
 
 536   my %myconfig = %main::myconfig;
 
 537   my $locale   = $main::locale;
 
 539   $form->get_lists('warehouses' => { 'key'    => 'WAREHOUSES',
 
 540                                      'bins'   => 'BINS', });
 
 542   my $warehouse_idx = get_warehouse_idx($form->{warehouse_id});
 
 543   $form->show_generic_error($locale->text("The selected warehouse does not exist.")) if (-1 == $warehouse_idx);
 
 545   my $warehouse = $form->{WAREHOUSES}->[$warehouse_idx];
 
 547   $form->show_generic_error($locale->text("The warehouse does not contain any bins.")) if (0 == scalar @{ $warehouse->{BINS} });
 
 549   map { $form->{"l_$_"} = 'Y' } qw(parts_id qty warehouseid binid partnumber partdescription bindescription chargenumber partunit);
 
 551   $form->{sort} = 'bindescription';
 
 552   my @contents  = WH->get_warehouse_report("warehouse_id" => $form->{warehouse_id});
 
 553   my $all_units = AM->retrieve_units(\%myconfig, $form);
 
 557   foreach my $row (1 .. $form->{rowcount}) {
 
 558     $form->{"qty_$row"} =~ s/^\s*//;
 
 559     $form->{"qty_$row"} =~ s/\s*$//;
 
 560     next if (!$form->{"qty_$row"});
 
 562     my $bin_idx = get_bin_idx($warehouse_idx, $form->{"src_bin_id_$row"});
 
 563     $form->show_generic_error($locale->text("The selected bin does not exist.")) if (-1 == $bin_idx);
 
 564     my $bin     = $warehouse->{BINS}->[$bin_idx];
 
 566     my $orig_qty = $form->{"qty_$row"} . " " . $form->{"unit_$row"};
 
 569       'src_warehouse_id' => $form->{warehouse_id},
 
 570       'transfer_type_id' => $form->{transfer_type_id},
 
 573     map { $transfer->{$_} = $form->{"${_}_${row}"} } qw(src_bin_id chargenumber parts_id qty);
 
 577     foreach (@contents) {
 
 578       if (($_->{binid} == $transfer->{src_bin_id}) && ($_->{parts_id} == $transfer->{parts_id}) && ($_->{chargenumber} eq $transfer->{chargenumber})) {
 
 585       $form->error($locale->text("There is not enough left of '#1' in bin '#2' for the removal of #3.",
 
 586                                  $form->{"partdescription_$row"}, $bin->{description}, $orig_qty));
 
 589     $transfer->{qty}  = $form->parse_amount(\%myconfig, $transfer->{qty}) * $all_units->{$form->{"unit_$row"}}->{factor};
 
 590     $transfer->{qty} /= $all_units->{$entry->{partunit}}->{factor} || 1;
 
 592     if (($entry->{qty} < $transfer->{qty}) || (0 >= $transfer->{qty})) {
 
 593       $form->error($locale->text("There is not enough left of '#1' in bin '#2' for the removal of #3.",
 
 594                                  $form->{"partdescription_$row"}, $bin->{description}, $orig_qty));
 
 597     $transfer->{comment} = $form->{comment};
 
 599     push @transfers, $transfer;
 
 601     $entry->{qty} -= $transfer->{qty};
 
 604   if (!scalar @transfers) {
 
 605     $form->show_generic_information($locale->text('Nothing has been selected for removal.'));
 
 609   WH->transfer(@transfers);
 
 611   $form->{trans_type}    = 'removal';
 
 612   $form->{saved_message} = $locale->text('The parts have been removed.');
 
 614   transfer_warehouse_selection();
 
 616   $main::lxdebug->leave_sub();
 
 619 # --------------------------------------------------------------------
 
 621 # --------------------------------------------------------------------
 
 624   $main::lxdebug->enter_sub();
 
 626   $main::auth->assert('warehouse_management');
 
 628   my $form     = $main::form;
 
 629   my %myconfig = %main::myconfig;
 
 631   $form->get_lists('warehouses' => { 'key'  => 'WAREHOUSES',
 
 632                                      'bins' => 'BINS', });
 
 634   show_no_warehouses_error() if (!scalar @{ $form->{WAREHOUSES} });
 
 636   $form->{jsscript} = 1;
 
 639   print $form->parse_html_template("wh/journal_filter", { "UNITS" => AM->unit_select_data(AM->retrieve_units(\%myconfig, $form)) });
 
 641   $main::lxdebug->leave_sub();
 
 644 sub generate_journal {
 
 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("WHJournal");
 
 654   $form->{sort}  ||= 'date';
 
 657   my @columns = qw(trans_id date warehouse_from bin_from warehouse_to bin_to partnumber partdescription chargenumber trans_type comment qty employee oe_id projectnumber);
 
 660   map { $filter{$_} = $form->{$_} if ($form->{$_}) } qw(warehouse_id bin_id partnumber description chargenumber);
 
 662   $filter{qty_op} = WH->convert_qty_op($form->{qty_op});
 
 663   if ($filter{qty_op}) {
 
 664     $form->isblank("qty",      $locale->text('Quantity missing.'));
 
 665     $form->isblank("qty_unit", $locale->text('Unit missing.'));
 
 667     $filter{qty}      = $form->{qty};
 
 668     $filter{qty_unit} = $form->{qty_unit};
 
 672   my $report = SL::ReportGenerator->new(\%myconfig, $form);
 
 674   my @hidden_variables = map { "l_${_}" } @columns;
 
 675   push @hidden_variables, qw(warehouse_id bin_id partnumber description chargenumber qty_op qty qty_unit fromdate todate);
 
 678     'date'            => { 'text' => $locale->text('Date'), },
 
 679     'trans_id'        => { 'text' => $locale->text('Trans Id'), },
 
 680     'trans_type'      => { 'text' => $locale->text('Trans Type'), },
 
 681     'comment'         => { 'text' => $locale->text('Comment'), },
 
 682     'warehouse_from'  => { 'text' => $locale->text('Warehouse From'), },
 
 683     'warehouse_to'    => { 'text' => $locale->text('Warehouse To'), },
 
 684     'bin_from'        => { 'text' => $locale->text('Bin From'), },
 
 685     'bin_to'          => { 'text' => $locale->text('Bin To'), },
 
 686     'partnumber'      => { 'text' => $locale->text('Part Number'), },
 
 687     'partdescription' => { 'text' => $locale->text('Description'), },
 
 688     'chargenumber'    => { 'text' => $locale->text('Charge Number'), },
 
 689     'qty'             => { 'text' => $locale->text('Qty'), },
 
 690     'employee'        => { 'text' => $locale->text('Employee'), },
 
 691     'projectnumber'   => { 'text' => $locale->text('Project Number'), },
 
 692     'oe_id'           => { 'text' => $locale->text('Document'), },
 
 695   my $href = build_std_url('action=generate_journal', grep { $form->{$_} } @hidden_variables);
 
 696   map { $column_defs{$_}->{link} = $href . "&sort=${_}&order=" . Q($_ eq $form->{sort} ? 1 - $form->{order} : $form->{order}) } @columns;
 
 698   my %column_alignment = map { $_ => 'right' } qw(qty);
 
 700   map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
 
 702   $report->set_columns(%column_defs);
 
 703   $report->set_column_order(@columns);
 
 705   $report->set_export_options('generate_journal', @hidden_variables, qw(sort order));
 
 707   $report->set_sort_indicator($form->{sort}, $form->{order});
 
 709   $report->set_options('output_format'        => 'HTML',
 
 710                        'title'                => $form->{title},
 
 711                        'attachment_basename'  => strftime($locale->text('warehouse_journal_list') . '_%Y%m%d', localtime time));
 
 712   $report->set_options_from_form();
 
 714   my $all_units = AM->retrieve_units(\%myconfig, $form);
 
 715   my @contents  = WH->get_warehouse_journal(%filter);
 
 717   my %doc_types = ( 'sales_quotation'         => { script => 'oe', title => $locale->text('Sales quotation') },
 
 718                     'sales_order'             => { script => 'oe', title => $locale->text('Sales Order') },
 
 719                     'request_quotation'       => { script => 'oe', title => $locale->text('Request quotation') },
 
 720                     'purchase_order'          => { script => 'oe', title => $locale->text('Purchase Order') },
 
 721                     'sales_delivery_order'    => { script => 'do', title => $locale->text('Sales delivery order') },
 
 722                     'purchase_delivery_order' => { script => 'do', title => $locale->text('Purchase delivery order') },
 
 723                     'sales_invoice'           => { script => 'is', title => $locale->text('Sales Invoice') },
 
 724                     'purchase_invoice'        => { script => 'ir', title => $locale->text('Purchase Invoice') },
 
 727   foreach my $entry (@contents) {
 
 728     $entry->{qty}        = $form->format_amount_units('amount'     => $entry->{qty},
 
 729                                                       'part_unit'  => $entry->{partunit},
 
 730                                                       'conv_units' => 'convertible');
 
 731     $entry->{trans_type} = $locale->text($entry->{trans_type});
 
 735     foreach my $column (@columns) {
 
 736       next if ($column eq 'trans_type');
 
 739         'data'  => $entry->{$column},
 
 740         'align' => $column_alignment{$column},
 
 744     $row->{trans_type} = {
 
 745       'raw_data' => $entry->{trans_type},
 
 746       'align'    => $column_alignment{trans_type},
 
 749     if ($form->{l_oe_id}) {
 
 750       $row->{oe_id}->{data} = '';
 
 751       my $info              = $entry->{oe_id_info};
 
 753       if ($info && $info->{id} && $info->{type} && $doc_types{$info->{type}}) {
 
 754         $row->{oe_id} = { data => $doc_types{ $info->{type} }->{title} . ' ' . $info->{number},
 
 755                           link => build_std_url('script=' . $doc_types{ $info->{type} }->{script} . '.pl', 'action=edit', 'id=' . $info->{id}, 'type=' . $info->{type}) };
 
 759     $report->add_data($row);
 
 762   $report->generate_with_headers();
 
 764   $main::lxdebug->leave_sub();
 
 767 # --------------------------------------------------------------------
 
 769 # --------------------------------------------------------------------
 
 772   $main::lxdebug->enter_sub();
 
 774   $main::auth->assert('warehouse_contents | warehouse_management');
 
 776   my $form     = $main::form;
 
 777   my %myconfig = %main::myconfig;
 
 778   my $locale   = $main::locale;
 
 780   $form->get_lists('warehouses' => { 'key'    => 'WAREHOUSES',
 
 781                                      'bins'   => 'BINS', });
 
 783   show_no_warehouses_error() if (!scalar @{ $form->{WAREHOUSES} });
 
 785   $form->{fokus}   = "partnumber";
 
 786   $form->{onload} .= "focus();";
 
 787   $form->{title}   = $locale->text("Report about wareouse contents");
 
 790   print $form->parse_html_template("wh/report_filter",
 
 791                                    { "nextsub"    => "generate_report",
 
 792                                      "WAREHOUSES" => $form->{WAREHOUSES},
 
 793                                      "UNITS"      => AM->unit_select_data(AM->retrieve_units(\%myconfig, $form)) });
 
 795   $main::lxdebug->leave_sub();
 
 798 sub generate_report {
 
 799   $main::lxdebug->enter_sub();
 
 801   $main::auth->assert('warehouse_contents | warehouse_management');
 
 803   my $form     = $main::form;
 
 804   my %myconfig = %main::myconfig;
 
 805   my $locale   = $main::locale;
 
 807   $form->{title}   = $locale->text("Report about wareouse contents");
 
 808   $form->{sort}  ||= 'partnumber';
 
 809   my $sort_col     = $form->{sort};
 
 812   my @columns = qw(warehousedescription bindescription partnumber partdescription chargenumber qty stock_value);
 
 815   map { $filter{$_} = $form->{$_} if ($form->{$_}) } qw(warehouse_id bin_id partnumber description chargenumber);
 
 817   $filter{qty_op} = WH->convert_qty_op($form->{qty_op});
 
 818   if ($filter{qty_op}) {
 
 819     $form->isblank("qty",      $locale->text('Quantity missing.'));
 
 820     $form->isblank("qty_unit", $locale->text('Unit missing.'));
 
 822     $filter{qty}      = $form->{qty};
 
 823     $filter{qty_unit} = $form->{qty_unit};
 
 827   $form->{subtotal} = '' if (!first { $_ eq $sort_col } qw(partnumber partdescription));
 
 829   my $report = SL::ReportGenerator->new(\%myconfig, $form);
 
 831   my @hidden_variables = map { "l_${_}" } @columns;
 
 832   push @hidden_variables, qw(warehouse_id bin_id partnumber description chargenumber qty_op qty qty_unit l_warehousedescription l_bindescription);
 
 833   push @hidden_variables, qw(include_empty_bins subtotal);
 
 836     'warehousedescription' => { 'text' => $locale->text('Warehouse'), },
 
 837     'bindescription'       => { 'text' => $locale->text('Bin'), },
 
 838     'partnumber'           => { 'text' => $locale->text('Part Number'), },
 
 839     'partdescription'      => { 'text' => $locale->text('Description'), },
 
 840     'chargenumber'         => { 'text' => $locale->text('Charge Number'), },
 
 841     'qty'                  => { 'text' => $locale->text('Qty'), },
 
 842     'stock_value'          => { 'text' => $locale->text('Stock value'), },
 
 845   my $href = build_std_url('action=generate_report', grep { $form->{$_} } @hidden_variables);
 
 846   map { $column_defs{$_}->{link} = $href . "&sort=${_}&order=" . Q($_ eq $sort_col ? 1 - $form->{order} : $form->{order}) } @columns;
 
 848   my %column_alignment = map { $_ => 'right' } qw(qty stock_value);
 
 850   map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
 
 852   $report->set_columns(%column_defs);
 
 853   $report->set_column_order(@columns);
 
 855   $report->set_export_options('generate_report', @hidden_variables, qw(sort order));
 
 857   $report->set_sort_indicator($sort_col, $form->{order});
 
 859   $report->set_options('output_format'        => 'HTML',
 
 860                        'title'                => $form->{title},
 
 861                        'attachment_basename'  => strftime($locale->text('warehouse_report_list') . '_%Y%m%d', localtime time));
 
 862   $report->set_options_from_form();
 
 864   my $all_units = AM->retrieve_units(\%myconfig, $form);
 
 865   my @contents  = WH->get_warehouse_report(%filter);
 
 869   my @subtotals_columns = qw(qty stock_value);
 
 870   my %subtotals         = map { $_ => 0 } @subtotals_columns;
 
 872   my $total_stock_value = 0;
 
 874   foreach my $entry (@contents) {
 
 875     map { $subtotals{$_} += $entry->{$_} } @subtotals_columns;
 
 876     $total_stock_value   += $entry->{stock_value} * 1;
 
 878     $entry->{qty}         = $form->format_amount_units('amount'     => $entry->{qty},
 
 879                                                        'part_unit'  => $entry->{partunit},
 
 880                                                        'conv_units' => 'convertible');
 
 881     $entry->{stock_value} = $form->format_amount(\%myconfig, $entry->{stock_value} * 1, 2);
 
 883     my $row_set = [ { map { $_ => { 'data' => $entry->{$_}, 'align' => $column_alignment{$_} } } @columns } ];
 
 885     if (($form->{subtotal} eq 'Y')
 
 886         && (($idx == (scalar @contents - 1))
 
 887             || ($entry->{$sort_col} ne $contents[$idx + 1]->{$sort_col}))) {
 
 889       my $row = { map { $_ => { 'data' => '', 'class' => 'listsubtotal', 'align' => $column_alignment{$_}, } } @columns };
 
 890       $row->{qty}->{data}         = $form->format_amount_units('amount'     => $subtotals{qty} * 1,
 
 891                                                                'part_unit'  => $entry->{partunit},
 
 892                                                                'conv_units' => 'convertible');
 
 893       $row->{stock_value}->{data} = $form->format_amount(\%myconfig, $subtotals{stock_value} * 1, 2);
 
 895       %subtotals                  = map { $_ => 0 } @subtotals_columns;
 
 897       push @{ $row_set }, $row;
 
 900     $report->add_data($row_set);
 
 905   if ($column_defs{stock_value}->{visible}) {
 
 906     $report->add_separator();
 
 908     my $row                      = { map { $_ => { 'data' => '', 'class' => 'listsubtotal', } } @columns };
 
 910     my $left_col                 = first { $column_defs{$_}->{visible} } @columns;
 
 912     $row->{$left_col}->{data}    = $locale->text('Total stock value');
 
 913     $row->{stock_value}->{data}  = $form->format_amount(\%myconfig, $total_stock_value, 2);
 
 914     $row->{stock_value}->{align} = 'right';
 
 916     $report->add_data($row);
 
 919   $report->generate_with_headers();
 
 921   $main::lxdebug->leave_sub();
 
 924 # --------------------------------------------------------------------
 
 926 # --------------------------------------------------------------------
 
 928 sub show_no_warehouses_error {
 
 929   $main::lxdebug->enter_sub();
 
 931   my $form     = $main::form;
 
 932   my %myconfig = %main::myconfig;
 
 933   my $locale   = $main::locale;
 
 935   my $msg = $locale->text('No warehouse has been created yet or the quantity of the bins is not configured yet.') . ' ';
 
 937   if ($main::auth->check_right($form->{login}, 'config')) {
 
 938     $msg .= $locale->text('You can create warehouses and bins via the menu "System -> Warehouses".');
 
 940     $msg .= $locale->text('Please ask your administrator to create warehouses and bins.');
 
 943   $form->show_generic_error($msg);
 
 945   $main::lxdebug->leave_sub();
 
 948 sub get_warehouse_idx {
 
 949   my ($warehouse_id) = @_;
 
 951   my $form     = $main::form;
 
 953   for (my $i = 0; $i < scalar @{$form->{WAREHOUSES}}; $i++) {
 
 954     return $i if ($form->{WAREHOUSES}->[$i]->{id} == $warehouse_id);
 
 961   my ($warehouse_index, $bin_id) = @_;
 
 963   my $form     = $main::form;
 
 965   my $warehouse = $form->{WAREHOUSES}->[$warehouse_index];
 
 967   return -1 if (!$warehouse);
 
 969   for (my $i = 0; $i < scalar @{ $warehouse->{BINS} }; $i++) {
 
 970     return $i if ($warehouse->{BINS}->[$i]->{id} == $bin_id);
 
 978 call new item dialogue from warehouse masks.
 
 981   action  => name of sub to be called when new item is done
 
 985   $main::lxdebug->enter_sub();
 
 988   my $form     = $main::form;
 
 991   $form->{old_callback} = $form->escape($form->{callback}, 1);
 
 992   $form->{callback}     = $form->escape("$form->{script}?action=$params{action}", 1);
 
 994   # save all form variables except action in a previousform variable
 
 995   my $previousform = join '&', map { my $value = $form->{$_}; $value =~ s/&/%26/; "$_=$value" } grep { !/action/ } keys %$form;
 
 998 #  push @HIDDENS,      { 'name' => 'previousform', 'value' => $form->escape($previousform, 1) };
 
 999   push @HIDDENS, map +{ 'name' => $_,             'value' => $form->{$_} }, qw(partnumber description unit vc sellprice ean);
 
1000   push @HIDDENS,      { 'name' => 'taxaccount2',  'value' => $form->{taxaccounts} };
 
1001   push @HIDDENS,      { 'name' => 'notes',        'value' => $form->{longdescription} };
 
1004   print $form->parse_html_template("generic/new_item", { HIDDENS => [ sort { $a->{name} cmp $b->{name} } @HIDDENS ] } );
 
1006   $main::lxdebug->leave_sub();
 
1010   my $form     = $main::form;
 
1011   call_sub($form->{update_nextsub} || $form->{nextsub});
 
1015   my $form     = $main::form;
 
1016   call_sub($form->{continue_nextsub} || $form->{nextsub});
 
1020   my $form     = $main::form;
 
1021   call_sub($form->{stock_nextsub} || $form->{nextsub});