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., 51 Franklin Street, Fifth Floor, Boston,
 
  29 #######################################################################
 
  31 # warehouse and packinglist
 
  33 #######################################################################
 
  35 use List::Util qw(min max first);
 
  36 use POSIX qw(strftime);
 
  46 use SL::Locale::String qw(t8);
 
  47 use SL::ReportGenerator;
 
  53 require "bin/mozilla/common.pl";
 
  54 require "bin/mozilla/reportgenerator.pl";
 
  60 # contents of the "transfer_type" table:
 
  61 #  $locale->text('back')
 
  62 #  $locale->text('correction')
 
  63 #  $locale->text('disposed')
 
  64 #  $locale->text('found')
 
  65 #  $locale->text('missing')
 
  66 #  $locale->text('stock')
 
  67 #  $locale->text('shipped')
 
  68 #  $locale->text('transfer')
 
  69 #  $locale->text('used')
 
  70 #  $locale->text('return_material')
 
  71 #  $locale->text('release_material')
 
  72 #  $locale->text('assembled')
 
  74 # --------------------------------------------------------------------
 
  76 # --------------------------------------------------------------------
 
  78 sub transfer_warehouse_selection {
 
  79   $main::lxdebug->enter_sub();
 
  81   $main::auth->assert('warehouse_management');
 
  83   my $form     = $main::form;
 
  84   my %myconfig = %main::myconfig;
 
  85   my $locale   = $main::locale;
 
  87   $form->get_lists('warehouses' => { 'key'    => 'WAREHOUSES',
 
  90   show_no_warehouses_error() if (!scalar @{ $form->{WAREHOUSES} });
 
  92   my $units      = AM->retrieve_units(\%myconfig, $form);
 
  95   if ( $form->{parts_id} ) {
 
  96     $part = SL::DB::Part->new();
 
  97     $part->id($form->{parts_id});
 
 101   # der zweite Parameter von unit_select_data gibt den default-Namen (selected) vor
 
 102   $form->{UNITS} = AM->unit_select_data($units, $form->{unit}, 0, $part ? $part->unit : 0);
 
 104   if (scalar @{ $form->{WAREHOUSES} }) {
 
 105     $form->{warehouse_id} ||= $form->{WAREHOUSES}->[0]->{id};
 
 106     $form->{bin_id}       ||= $form->{WAREHOUSES}->[0]->{BINS}->[0]->{id};
 
 111   if ($form->{trans_type} eq 'removal') {
 
 112     setup_wh_transfer_warehouse_selection_action_bar("removal_parts_selection");
 
 113     $form->{title}   = $locale->text('Removal from Warehouse');
 
 114     $content         = $form->parse_html_template('wh/warehouse_selection');
 
 116   } elsif (!$form->{trans_type} || ($form->{trans_type} eq 'transfer')) {
 
 117     setup_wh_transfer_warehouse_selection_action_bar("transfer_parts_selection");
 
 118     $form->{title}   = $locale->text('Transfer');
 
 119     $content         = $form->parse_html_template('wh/warehouse_selection');
 
 121   } elsif ($form->{trans_type} eq 'assembly') {
 
 122     setup_wh_transfer_warehouse_selection_assembly_action_bar();
 
 123     $form->{title} = $locale->text('Produce Assembly');
 
 124     $content       = $form->parse_html_template('wh/warehouse_selection_assembly');
 
 130   $main::lxdebug->leave_sub();
 
 133 sub transfer_parts_selection {
 
 134   $main::lxdebug->enter_sub();
 
 136   $main::auth->assert('warehouse_management');
 
 138   my $form     = $main::form;
 
 139   my %myconfig = %main::myconfig;
 
 140   my $locale   = $main::locale;
 
 142   transfer_or_removal_prepare_contents('direction' => 'transfer');
 
 144   setup_wh_transfer_parts_action_bar();
 
 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                                            "partsid"      => $form->{part_id},
 
 186                                            "ean"          => $form->{ean});
 
 188   if (0 == scalar(@contents)) {
 
 189     $form->show_generic_error($locale->text("The selected warehouse is empty, or no stocked items where found that match the filter settings."));
 
 192   my $all_units = AM->retrieve_units(\%myconfig, $form);
 
 194   foreach (@contents) {
 
 195     $_->{qty} = $form->format_amount_units('amount'     => $_->{qty},
 
 196                                            'part_unit'  => $_->{partunit},
 
 197                                            'conv_units' => 'convertible');
 
 198     my $this_unit = $_->{partunit};
 
 200     if ($all_units->{$_->{partunit}} && ($all_units->{g}->{base_unit} eq $all_units->{$_->{partunit}}->{base_unit})) {
 
 204     $_->{UNITS} = AM->unit_select_data($all_units, $this_unit, 0, $_->{partunit});
 
 207   my $transfer_types = WH->retrieve_transfer_types($args{direction});
 
 208   map { $_->{description} = $locale->text($_->{description}) } @{ $transfer_types };
 
 210   $form->{CONTENTS}       = \@contents;
 
 211   $form->{TRANSFER_TYPES} = $transfer_types;
 
 213   $main::lxdebug->leave_sub();
 
 218   $main::lxdebug->enter_sub();
 
 220   $main::auth->assert('warehouse_management');
 
 222   my $form     = $main::form;
 
 223   my %myconfig = %main::myconfig;
 
 224   my $locale   = $main::locale;
 
 226   $form->get_lists('warehouses' => { 'key' => 'WAREHOUSES', 'bins' => 'BINS' });
 
 228   my $warehouse_idx = get_warehouse_idx($form->{warehouse_id});
 
 229   $form->show_generic_error($locale->text("The selected warehouse does not exist.")) if (-1 == $warehouse_idx);
 
 231   my $warehouse = $form->{WAREHOUSES}->[$warehouse_idx];
 
 233   $form->show_generic_error($locale->text("The source warehouse does not contain any bins.")) if (0 == scalar @{ $warehouse->{BINS} });
 
 235   map { $form->{"l_$_"} = 'Y' } qw(parts_id qty warehouseid binid partnumber partdescription bindescription chargenumber bestbefore partunit);
 
 237   $form->{sort} = 'bindescription';
 
 238   my @contents  = WH->get_warehouse_report("warehouse_id" => $form->{warehouse_id});
 
 239   my $all_units = AM->retrieve_units(\%myconfig, $form);
 
 243   foreach my $row (1 .. $form->{rowcount}) {
 
 244     $form->{"qty_$row"} =~ s/^\s*//;
 
 245     $form->{"qty_$row"} =~ s/\s*$//;
 
 246     next if (!$form->{"qty_$row"});
 
 248     my $bin_idx = get_bin_idx($warehouse_idx, $form->{"src_bin_id_$row"});
 
 249     $form->show_generic_error($locale->text("The selected bin does not exist.")) if (-1 == $bin_idx);
 
 250     my $bin     = $warehouse->{BINS}->[$bin_idx];
 
 252     my $orig_qty = $form->{"qty_$row"} . " " . $form->{"unit_$row"};
 
 255       'src_warehouse_id' => $form->{warehouse_id},
 
 256       'transfer_type_id' => $form->{transfer_type_id},
 
 259     map { $transfer->{$_} = $form->{"${_}_${row}"} } qw(src_bin_id chargenumber bestbefore parts_id qty dst_warehouse_id dst_bin_id);
 
 263     foreach (@contents) {
 
 264       if (($_->{binid} == $transfer->{src_bin_id}) && ($_->{parts_id} == $transfer->{parts_id}) && ($_->{chargenumber} eq $transfer->{chargenumber}) && $_->{bestbefore} eq $transfer->{bestbefore}) {
 
 271       $form->error($locale->text("There is not enough left of '#1' in bin '#2' for the removal of #3.",
 
 272                                  $form->{"partdescription_$row"}, $bin->{description}, $orig_qty));
 
 275     $transfer->{qty}  = $form->parse_amount(\%myconfig, $transfer->{qty}) * $all_units->{$form->{"unit_$row"}}->{factor};
 
 276     $transfer->{qty} /= $all_units->{$entry->{partunit}}->{factor} || 1;
 
 278     if (($entry->{qty} < $transfer->{qty}) || (0 >= $transfer->{qty})) {
 
 279       $form->error($locale->text("There is not enough left of '#1' in bin '#2' for the removal of #3.",
 
 280                                  $form->{"partdescription_$row"}, $bin->{description}, $orig_qty));
 
 283     $transfer->{comment} = $form->{comment};
 
 284     $transfer->{change_default_bin} = $form->{change_default_bin};
 
 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.'));
 
 293     $::dispatcher->end_request;
 
 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 # --------------------------------------------------------------------
 
 311 # Transfer: assemblies
 
 312 # Dies ist die Auswahlmaske für ein assembly.
 
 313 # Die ist einfach von transfer_assembly_update_part kopiert und nur um den trans_type (assembly) korrigiert worden
 
 314 # Es wäre schön, hier nochmal check_assembly_max_create auf, um die max. Fertigungszahl herauszufinden.
 
 315 # Ich lass das mal als auskommentierte Idee bestehen jb 18.3.09
 
 316 # --------------------------------------------------------------------
 
 318 sub transfer_assembly_update_part {
 
 319   my $form     = $main::form;
 
 320   my %myconfig = %main::myconfig;
 
 321   my $locale   = $main::locale;
 
 323   $form->{trans_type} = 'assembly';
 
 324   $form->{qty}        = $form->parse_amount(\%myconfig, $form->{qty});
 
 326   if (!$form->{parts_id}) {
 
 327     delete $form->{partunit};
 
 328     transfer_warehouse_selection();
 
 333   my $part = SL::DB::Part->new(id => $::form->{parts_id})->load;
 
 334   @{$form}{qw(parts_id partnumber description)} = ($part->id, $part->partnumber, $part->description);
 
 336   transfer_stock_get_partunit();
 
 337   transfer_warehouse_selection();
 
 340 sub transfer_stock_part_selected {
 
 341   $main::lxdebug->enter_sub();
 
 345   my $form     = $main::form;
 
 347   @{$form}{qw(parts_id partnumber description ean warehouse_id bin_id)} = @{$part}{qw(id partnumber description ean warehouse_id bin_id)};
 
 349   transfer_stock_get_partunit();
 
 350   transfer_warehouse_selection();
 
 352   $main::lxdebug->leave_sub();
 
 355 sub transfer_stock_get_partunit {
 
 356   $main::lxdebug->enter_sub();
 
 358   my $form     = $main::form;
 
 360   if ($form->{parts_id}) {
 
 361     my $part_info     = IC->get_basic_part_info('id' => $form->{parts_id});
 
 362     $form->{partunit} = $part_info->{unit};
 
 365   $main::lxdebug->leave_sub();
 
 368 # vorüberlegung jb 22.2.2009
 
 369 # wir benötigen für diese funktion, die anzahl die vom erzeugnis hergestellt werden soll. vielleicht direkt per js fehleingaben verhindern?
 
 370 # ferner dann nochmal mit check_asssembly_max_create gegenprüfen und dann transaktionssicher wegbuchen.
 
 371 # 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
 
 372 # Manko ist derzeit noch, dass unterschiedliche Lagerplätze, bzw. das Quelllager an sich nicht ausgewählt werden können.
 
 373 # Laut Absprache in KW11 09 übernimmt mb hier den rest im April ... jb 18.3.09
 
 375 sub create_assembly {
 
 376   $main::lxdebug->enter_sub();
 
 378   my $form     = $main::form;
 
 379   my %myconfig = %main::myconfig;
 
 380   my $locale   = $main::locale;
 
 382   $form->{qty} = $form->parse_amount(\%myconfig, $form->{qty});
 
 383   if ($form->{qty} <= 0) {
 
 384     $form->show_generic_error($locale->text('Invalid quantity.'));
 
 386   # TODO Es wäre schön, hier schon die maximale Anzahl der zu fertigenden Erzeugnisse zu haben
 
 387   #else { if ($form->{qty} > $maxcreate) { #s.o.
 
 388   #     $form->show_generic_error($locale->text('Can not create that quantity with current stock'));
 
 389   #     $form->show_generic_error('Maximale Stückzahl' . $maxcreate);
 
 393   if (!$form->{warehouse_id} || !$form->{bin_id}) {
 
 394     $form->error($locale->text('The warehouse or the bin is missing.'));
 
 397   if (!$::instance_conf->get_show_bestbefore) {
 
 398       $form->{bestbefore} = '';
 
 401   # WIESO war das nicht vorher schon ein %HASH?? ein hash ist ein hash! das hat mich mehr als eine Stunde gekostet herauszufinden. grr. jb 3.3.2009
 
 402   # Anm. jb 18.3. vielleicht auch nur meine unwissenheit in perl-datenstrukturen
 
 404     'transfer_type'    => 'assembly',
 
 405     'login'            => $::myconfig{login},
 
 406     'dst_warehouse_id' => $form->{warehouse_id},
 
 407     'dst_bin_id'       => $form->{bin_id},
 
 408     'chargenumber'     => $form->{chargenumber},
 
 409     'bestbefore'       => $form->{bestbefore},
 
 410     'assembly_id'      => $form->{parts_id},
 
 411     'qty'              => $form->{qty},
 
 412     'unit'             => $form->{unit},
 
 413     'comment'          => $form->{comment}
 
 416   my $ret = WH->transfer_assembly (%TRANSFER);
 
 417   # Frage: Ich pack in den return-wert auch gleich die Fehlermeldung. Irgendwelche Nummern als Fehlerkonstanten definieren find ich auch nicht besonders schick...
 
 420     # 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
 
 421     $form->show_generic_error($ret);
 
 424   delete @{$form}{qw(parts_id partnumber description qty unit chargenumber bestbefore comment)};
 
 426   $form->{saved_message} = $locale->text('The assembly has been created.');
 
 427   $form->{trans_type}    = 'assembly';
 
 429   transfer_warehouse_selection();
 
 431   $main::lxdebug->leave_sub();
 
 434 # --------------------------------------------------------------------
 
 436 # --------------------------------------------------------------------
 
 438 sub removal_parts_selection {
 
 439   $main::lxdebug->enter_sub();
 
 441   $main::auth->assert('warehouse_management');
 
 443   my $form     = $main::form;
 
 444   my %myconfig = %main::myconfig;
 
 445   my $locale   = $main::locale;
 
 447   transfer_or_removal_prepare_contents('direction' => 'out');
 
 449   setup_wh_removal_parts_selection_action_bar();
 
 451   $form->{title} = $locale->text('Removal');
 
 453   print $form->parse_html_template("wh/removal_parts_selection");
 
 455   $main::lxdebug->leave_sub();
 
 459   $main::lxdebug->enter_sub();
 
 461   $main::auth->assert('warehouse_management');
 
 463   my $form     = $main::form;
 
 464   my %myconfig = %main::myconfig;
 
 465   my $locale   = $main::locale;
 
 467   $form->get_lists('warehouses' => { 'key'    => 'WAREHOUSES',
 
 468                                      'bins'   => 'BINS', });
 
 470   my $warehouse_idx = get_warehouse_idx($form->{warehouse_id});
 
 471   $form->show_generic_error($locale->text("The selected warehouse does not exist.")) if (-1 == $warehouse_idx);
 
 473   my $warehouse = $form->{WAREHOUSES}->[$warehouse_idx];
 
 475   $form->show_generic_error($locale->text("The warehouse does not contain any bins.")) if (0 == scalar @{ $warehouse->{BINS} });
 
 477   map { $form->{"l_$_"} = 'Y' } qw(parts_id qty warehouseid binid partnumber partdescription bindescription chargenumber bestbefore partunit);
 
 479   $form->{sort} = 'bindescription';
 
 480   my @contents  = WH->get_warehouse_report("warehouse_id" => $form->{warehouse_id});
 
 481   my $all_units = AM->retrieve_units(\%myconfig, $form);
 
 485   foreach my $row (1 .. $form->{rowcount}) {
 
 486     $form->{"qty_$row"} =~ s/^\s*//;
 
 487     $form->{"qty_$row"} =~ s/\s*$//;
 
 488     next if (!$form->{"qty_$row"});
 
 490     my $bin_idx = get_bin_idx($warehouse_idx, $form->{"src_bin_id_$row"});
 
 491     $form->show_generic_error($locale->text("The selected bin does not exist.")) if (-1 == $bin_idx);
 
 492     my $bin     = $warehouse->{BINS}->[$bin_idx];
 
 494     my $orig_qty = $form->{"qty_$row"} . " " . $form->{"unit_$row"};
 
 497       'src_warehouse_id' => $form->{warehouse_id},
 
 498       'transfer_type_id' => $form->{transfer_type_id},
 
 501     map { $transfer->{$_} = $form->{"${_}_${row}"} } qw(src_bin_id chargenumber bestbefore parts_id qty);
 
 505     foreach (@contents) {
 
 506       if (($_->{binid} == $transfer->{src_bin_id}) && ($_->{parts_id} == $transfer->{parts_id}) && ($_->{chargenumber} eq $transfer->{chargenumber}) && ($_->{bestbefore} eq $transfer->{bestbefore})) {
 
 513       $form->error($locale->text("There is not enough left of '#1' in bin '#2' for the removal of #3.",
 
 514                                  $form->{"partdescription_$row"}, $bin->{description}, $orig_qty));
 
 517     $transfer->{qty}  = $form->parse_amount(\%myconfig, $transfer->{qty}) * $all_units->{$form->{"unit_$row"}}->{factor};
 
 518     $transfer->{qty} /= $all_units->{$entry->{partunit}}->{factor} || 1;
 
 520     if (($entry->{qty} < $transfer->{qty}) || (0 >= $transfer->{qty})) {
 
 521       $form->error($locale->text("There is not enough left of '#1' in bin '#2' for the removal of #3.",
 
 522                                  $form->{"partdescription_$row"}, $bin->{description}, $orig_qty));
 
 525     $transfer->{comment} = $form->{comment};
 
 527     push @transfers, $transfer;
 
 529     $entry->{qty} -= $transfer->{qty};
 
 532   if (!scalar @transfers) {
 
 533     $form->show_generic_information($locale->text('Nothing has been selected for removal.'));
 
 534     $::dispatcher->end_request;
 
 537   WH->transfer(@transfers);
 
 539   $form->{trans_type}    = 'removal';
 
 540   $form->{saved_message} = $locale->text('The parts have been removed.');
 
 542   transfer_warehouse_selection();
 
 544   $main::lxdebug->leave_sub();
 
 547 # --------------------------------------------------------------------
 
 549 # --------------------------------------------------------------------
 
 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->{title} = $locale->text('Report about warehouse transactions');
 
 561   $form->get_lists('warehouses' => { 'key'  => 'WAREHOUSES',
 
 562                                      'bins' => 'BINS', });
 
 564   show_no_warehouses_error() if (!scalar @{ $form->{WAREHOUSES} });
 
 566   setup_wh_journal_action_bar();
 
 569   print $form->parse_html_template("wh/journal_filter", { "UNITS" => AM->unit_select_data(AM->retrieve_units(\%myconfig, $form)) });
 
 571   $main::lxdebug->leave_sub();
 
 574 sub generate_journal {
 
 575   $main::lxdebug->enter_sub();
 
 577   $main::auth->assert('warehouse_management');
 
 579   my $form     = $main::form;
 
 580   my %myconfig = %main::myconfig;
 
 581   my $locale   = $main::locale;
 
 583   $form->{title}   = $locale->text("WHJournal");
 
 584   $form->{sort}  ||= 'date';
 
 586   $form->{report_generator_output_format} = 'HTML' if !$form->{report_generator_output_format};
 
 589   my @columns = qw(trans_id date warehouse_from bin_from warehouse_to bin_to partnumber type_and_classific partdescription chargenumber bestbefore trans_type comment qty unit partunit employee oe_id projectnumber);
 
 592   map { $filter{$_} = $form->{$_} if ($form->{$_}) } qw(warehouse_id bin_id classification_id partnumber description chargenumber bestbefore transtype_id transtype_ids comment projectnumber);
 
 594   $filter{qty_op} = WH->convert_qty_op($form->{qty_op});
 
 595   if ($filter{qty_op}) {
 
 596     $form->isblank("qty",      $locale->text('Quantity missing.'));
 
 597     $form->isblank("qty_unit", $locale->text('Unit missing.'));
 
 599     $filter{qty}      = $form->{qty};
 
 600     $filter{qty_unit} = $form->{qty_unit};
 
 604   my $allrows        = !!($form->{report_generator_output_format} ne 'HTML') ;
 
 608   my $page           = $::form->{page} || 1;
 
 609   $pages->{per_page} = $::form->{per_page} || 15;
 
 610   my $first_nr       = ($page - 1) * $pages->{per_page};
 
 611   my $last_nr        = $first_nr + $pages->{per_page};
 
 613   # no optimisation if qty op
 
 614   if ( !$allrows && $form->{maxrows} && !$filter{qty_op}) {
 
 615     $filter{limit}  = $pages->{per_page};
 
 616     $filter{offset} = ($page - 1) * $pages->{per_page};
 
 618     $last_nr        = $pages->{per_page};
 
 621   my @contents  = WH->get_warehouse_journal(%filter);
 
 623   if (!$form->{maxrows}) {
 
 624     $form->{maxrows} = scalar @contents ;
 
 627   my $report = SL::ReportGenerator->new(\%myconfig, $form);
 
 629   my @hidden_variables = map { "l_${_}" } @columns;
 
 630   push @hidden_variables, qw(warehouse_id bin_id partnumber description chargenumber bestbefore qty_op qty qty_unit unit partunit fromdate todate transtype_ids comment projectnumber);
 
 631   push @hidden_variables, qw(classification_id);
 
 634     'date'            => { 'text' => $locale->text('Date'), },
 
 635     'trans_id'        => { 'text' => $locale->text('Trans Id'), },
 
 636     'trans_type'      => { 'text' => $locale->text('Trans Type'), },
 
 637     'comment'         => { 'text' => $locale->text('Comment'), },
 
 638     'warehouse_from'  => { 'text' => $locale->text('Warehouse From'), },
 
 639     'warehouse_to'    => { 'text' => $locale->text('Warehouse To'), },
 
 640     'bin_from'        => { 'text' => $locale->text('Bin From'), },
 
 641     'bin_to'          => { 'text' => $locale->text('Bin To'), },
 
 642     'partnumber'      => { 'text' => $locale->text('Part Number'), },
 
 644                       => { 'text' => $locale->text('Type'), },
 
 645     'partdescription' => { 'text' => $locale->text('Part Description'), },
 
 646     'chargenumber'    => { 'text' => $locale->text('Charge Number'), },
 
 647     'bestbefore'      => { 'text' => $locale->text('Best Before'), },
 
 648     'qty'             => { 'text' => $locale->text('Qty'), },
 
 649     'unit'            => { 'text' => $locale->text('Part Unit'), },
 
 650     'partunit'        => { 'text' => $locale->text('Unit'), },
 
 651     'employee'        => { 'text' => $locale->text('Employee'), },
 
 652     'projectnumber'   => { 'text' => $locale->text('Project Number'), },
 
 653     'oe_id'           => { 'text' => $locale->text('Document'), },
 
 656   if ($form->{transtype_ids} && 'ARRAY' eq ref $form->{transtype_ids}) {
 
 657     for (my $i = 0; $i < scalar(@{ $form->{transtype_ids} }); $i++) {
 
 658       delete $form->{transtype_ids}[$i] if $form->{transtype_ids}[$i] eq '';
 
 660     $form->{transtype_ids} = join(",", @{ $form->{transtype_ids} });
 
 663   my $href = build_std_url('action=generate_journal', grep { $form->{$_} } @hidden_variables);
 
 664   $href .= "&maxrows=".$form->{maxrows};
 
 666   map { $column_defs{$_}->{link} = $href ."&page=".$page. "&sort=${_}&order=" . Q($_ eq $form->{sort} ? 1 - $form->{order} : $form->{order}) } @columns;
 
 668   my %column_alignment = map { $_ => 'right' } qw(qty);
 
 670   map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
 
 671   $column_defs{partunit}->{visible} = 1;
 
 672   $column_defs{type_and_classific}->{visible} = 1;
 
 673   $column_defs{type_and_classific}->{link} ='';
 
 675   $report->set_columns(%column_defs);
 
 676   $report->set_column_order(@columns);
 
 678   $report->set_export_options('generate_journal', @hidden_variables, qw(sort order));
 
 680   $report->set_sort_indicator($form->{sort}, $form->{order});
 
 682   $report->set_options('output_format'        => 'HTML',
 
 683                        'title'                => $form->{title},
 
 684                        'attachment_basename'  => strftime($locale->text('warehouse_journal_list') . '_%Y%m%d', localtime time));
 
 685   $report->set_options_from_form();
 
 686   $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
 
 688   my $all_units = AM->retrieve_units(\%myconfig, $form);
 
 690   my %doc_types = ( 'sales_quotation'         => { script => 'oe', title => $locale->text('Sales quotation') },
 
 691                     'sales_order'             => { script => 'oe', title => $locale->text('Sales Order') },
 
 692                     'request_quotation'       => { script => 'oe', title => $locale->text('Request quotation') },
 
 693                     'purchase_order'          => { script => 'oe', title => $locale->text('Purchase Order') },
 
 694                     'sales_delivery_order'    => { script => 'do', title => $locale->text('Sales delivery order') },
 
 695                     'purchase_delivery_order' => { script => 'do', title => $locale->text('Purchase delivery order') },
 
 696                     'sales_invoice'           => { script => 'is', title => $locale->text('Sales Invoice') },
 
 697                     'purchase_invoice'        => { script => 'ir', title => $locale->text('Purchase Invoice') },
 
 702   foreach my $entry (@contents) {
 
 703     $entry->{type_and_classific} = $::request->presenter->type_abbreviation($entry->{part_type}).
 
 704                                    $::request->presenter->classification_abbreviation($entry->{classification_id});
 
 705     $entry->{qty}        = $form->format_amount(\%myconfig, $entry->{qty});
 
 706     $entry->{trans_type} = $locale->text($entry->{trans_type});
 
 710     foreach my $column (@columns) {
 
 712         'data'  => $entry->{$column},
 
 713         'align' => $column_alignment{$column},
 
 717     $row->{trans_type}->{raw_data} = $entry->{trans_type};
 
 719     if ($form->{l_oe_id}) {
 
 720       $row->{oe_id}->{data} = '';
 
 721       my $info              = $entry->{oe_id_info};
 
 723       if ($info && $info->{id} && $info->{type} && $doc_types{$info->{type}}) {
 
 724         $row->{oe_id} = { data => $doc_types{ $info->{type} }->{title} . ' ' . $info->{number},
 
 725                           link => build_std_url('script=' . $doc_types{ $info->{type} }->{script} . '.pl', 'action=edit', 'id=' . $info->{id}, 'type=' . $info->{type}) };
 
 729     if ( $allrows || ($idx >= $first_nr && $idx < $last_nr )) {
 
 730       $report->add_data($row);
 
 736       $pages->{max}  = SL::DB::Helper::Paginated::ceil($form->{maxrows}, $pages->{per_page}) || 1;
 
 737       $pages->{page} = $page < 1 ? 1: $page > $pages->{max} ? $pages->{max}: $page;
 
 738       $pages->{common} = [ grep { $_->{visible} } @{ SL::DB::Helper::Paginated::make_common_pages($pages->{page}, $pages->{max}) } ];
 
 740       $report->set_options('raw_bottom_info_text' => $form->parse_html_template('common/paginate',
 
 741                                                             { 'pages' => $pages , 'base_url' => $href.'&sort='.$form->{sort}.'&order='.$form->{order}}) );
 
 743   $report->generate_with_headers();
 
 745   $main::lxdebug->leave_sub();
 
 748 # --------------------------------------------------------------------
 
 750 # --------------------------------------------------------------------
 
 753   $main::lxdebug->enter_sub();
 
 755   $main::auth->assert('warehouse_contents | warehouse_management');
 
 757   my $form     = $main::form;
 
 758   my %myconfig = %main::myconfig;
 
 759   my $locale   = $main::locale;
 
 761   $form->get_lists('warehouses' => { 'key'    => 'WAREHOUSES',
 
 762                                      'bins'   => 'BINS', });
 
 764   show_no_warehouses_error() if (!scalar @{ $form->{WAREHOUSES} });
 
 766   $form->{title}   = $locale->text("Report about warehouse contents");
 
 768   setup_wh_report_action_bar();
 
 771   print $form->parse_html_template("wh/report_filter",
 
 772                                    { "WAREHOUSES" => $form->{WAREHOUSES},
 
 773                                      "UNITS"      => AM->unit_select_data(AM->retrieve_units(\%myconfig, $form)) });
 
 775   $main::lxdebug->leave_sub();
 
 778 sub generate_report {
 
 779   $main::lxdebug->enter_sub();
 
 781   $main::auth->assert('warehouse_contents | warehouse_management');
 
 783   my $form     = $main::form;
 
 784   my %myconfig = %main::myconfig;
 
 785   my $locale   = $main::locale;
 
 787   $form->{title}   = $locale->text("Report about warehouse contents");
 
 788   $form->{sort}  ||= 'partnumber';
 
 789   my $sort_col     = $form->{sort};
 
 792   my @columns = qw(warehousedescription bindescription partnumber type_and_classific partdescription chargenumber bestbefore comment qty partunit  purchase_price stock_value);
 
 795   map { $filter{$_} = $form->{$_} if ($form->{$_}) } qw(warehouse_id bin_id classification_id partnumber description chargenumber bestbefore date include_invalid_warehouses);
 
 797   # show filter stuff also in report
 
 799   my $currentdate = $form->current_date(\%myconfig);
 
 800   push @options, $locale->text('Printdate') . " : ".$locale->date(\%myconfig, $currentdate, 1);
 
 802   # dispatch all options
 
 803   my $dispatch_options = {
 
 804    warehouse_id   => sub { push @options, $locale->text('Warehouse') . " : " .
 
 805                                             SL::DB::Manager::Warehouse->find_by(id => $form->{warehouse_id})->description},
 
 806    bin_id         => sub { push @options, $locale->text('Bin') . " : " .
 
 807                                             SL::DB::Manager::Bin->find_by(id => $form->{bin_id})->description},
 
 808    partnumber     => sub { push @options, $locale->text('Partnumber')     . " : $form->{partnumber}"},
 
 809    classification_id => sub { push @options, $locale->text('Parts Classification'). " : ".
 
 810                                                SL::DB::Manager::PartClassification->get_first(where => [ id => $form->{classification_id} ] )->description; },
 
 811    description    => sub { push @options, $locale->text('Description')    . " : $form->{description}"},
 
 812    chargenumber   => sub { push @options, $locale->text('Charge Number')  . " : $form->{chargenumber}"},
 
 813    bestbefore     => sub { push @options, $locale->text('Best Before')    . " : $form->{bestbefore}"},
 
 814    include_invalid_warehouses    => sub { push @options, $locale->text('Include invalid warehouses ')},
 
 816   foreach (keys %filter) {
 
 817    $dispatch_options->{$_}->() if $dispatch_options->{$_};
 
 819   push @options, $locale->text('Stock Qty for Date') . " " . $locale->date(\%myconfig, $form->{date}?$form->{date}:$currentdate, 1);
 
 821   # / end show filter stuff also in report
 
 823   $filter{qty_op} = WH->convert_qty_op($form->{qty_op});
 
 824   if ($filter{qty_op}) {
 
 825     $form->isblank("qty",      $locale->text('Quantity missing.'));
 
 826     $form->isblank("qty_unit", $locale->text('Unit missing.'));
 
 828     $filter{qty}      = $form->{qty};
 
 829     $filter{qty_unit} = $form->{qty_unit};
 
 833   $form->{report_generator_output_format} = 'HTML' if !$form->{report_generator_output_format};
 
 836   my $allrows        = !!($form->{report_generator_output_format} ne 'HTML') ;
 
 837   my $page           = $::form->{page} || 1;
 
 839   $pages->{per_page} = $::form->{per_page} || 20;
 
 840   my $first_nr       = ($page - 1) * $pages->{per_page};
 
 841   my $last_nr        = $first_nr + $pages->{per_page};
 
 843   # no optimisation if qty op
 
 844   if ( !$allrows && $form->{maxrows} && !$filter{qty_op}) {
 
 845     $filter{limit}  = $pages->{per_page};
 
 846     $filter{offset} = ($page - 1) * $pages->{per_page};
 
 848     $last_nr        = $pages->{per_page};
 
 851   my @contents  = WH->get_warehouse_report(%filter);
 
 854   if (!$form->{maxrows}) {
 
 855     $form->{maxrows} = scalar @contents ;
 
 858   $form->{subtotal} = '' if (!first { $_ eq $sort_col } qw(partnumber partdescription));
 
 860   my $report = SL::ReportGenerator->new(\%myconfig, $form);
 
 862   my @hidden_variables = map { "l_${_}" } @columns;
 
 863   push @hidden_variables, qw(warehouse_id bin_id partnumber partstypes_id description chargenumber bestbefore qty_op qty qty_unit partunit l_warehousedescription l_bindescription);
 
 864   push @hidden_variables, qw(include_empty_bins subtotal include_invalid_warehouses date);
 
 865   push @hidden_variables, qw(classification_id);
 
 868     'warehousedescription' => { 'text' => $locale->text('Warehouse'), },
 
 869     'bindescription'       => { 'text' => $locale->text('Bin'), },
 
 870     'partnumber'           => { 'text' => $locale->text('Part Number'), },
 
 871     'type_and_classific'   => { 'text' => $locale->text('Type'), },
 
 872     'partdescription'      => { 'text' => $locale->text('Part Description'), },
 
 873     'chargenumber'         => { 'text' => $locale->text('Charge Number'), },
 
 874     'bestbefore'           => { 'text' => $locale->text('Best Before'), },
 
 875     'qty'                  => { 'text' => $locale->text('Qty'), },
 
 876     'partunit'             => { 'text' => $locale->text('Unit'), },
 
 877     'stock_value'          => { 'text' => $locale->text('Stock value'), },
 
 878     'purchase_price'       => { 'text' => $locale->text('Purchase price'), },
 
 881   my $href = build_std_url('action=generate_report', grep { $form->{$_} } @hidden_variables);
 
 882   $href .= "&maxrows=".$form->{maxrows};
 
 884   map { $column_defs{$_}->{link} = $href . "&page=".$page."&sort=${_}&order=" . Q($_ eq $sort_col ? 1 - $form->{order} : $form->{order}) } @columns;
 
 886   my %column_alignment = map { $_ => 'right' } qw(qty purchase_price stock_value);
 
 888   map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
 
 890   $column_defs{partunit}->{visible}           = 1;
 
 891   $column_defs{type_and_classific}->{visible} = 1;
 
 892   $column_defs{type_and_classific}->{link} ='';
 
 894   $report->set_columns(%column_defs);
 
 895   $report->set_column_order(@columns);
 
 897   $report->set_export_options('generate_report', @hidden_variables, qw(sort order));
 
 899   $report->set_sort_indicator($sort_col, $form->{order});
 
 901   $report->set_options('top_info_text'        => join("\n", @options),
 
 902                        'output_format'        => 'HTML',
 
 903                        'title'                => $form->{title},
 
 904                        'attachment_basename'  => strftime($locale->text('warehouse_report_list') . '_%Y%m%d', localtime time));
 
 905   $report->set_options_from_form();
 
 906   $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
 
 908   my $all_units = AM->retrieve_units(\%myconfig, $form);
 
 911   my @subtotals_columns = qw(qty stock_value);
 
 912   my %subtotals         = map { $_ => 0 } @subtotals_columns;
 
 914   my $total_stock_value = 0;
 
 916   foreach my $entry (@contents) {
 
 918     $entry->{type_and_classific} = $::request->presenter->type_abbreviation($entry->{part_type}).
 
 919                                    $::request->presenter->classification_abbreviation($entry->{classification_id});
 
 920     map { $subtotals{$_} += $entry->{$_} } @subtotals_columns;
 
 921     $total_stock_value   += $entry->{stock_value} * 1;
 
 922     $entry->{qty}         = $form->format_amount(\%myconfig, $entry->{qty});
 
 923 #    $entry->{qty}         = $form->format_amount_units('amount'     => $entry->{qty},
 
 924 #                                                       'part_unit'  => $entry->{partunit},
 
 925 #                                                       'conv_units' => 'convertible');
 
 926     $entry->{stock_value} = $form->format_amount(\%myconfig, $entry->{stock_value} * 1, 2);
 
 927     $entry->{purchase_price} = $form->format_amount(\%myconfig, $entry->{purchase_price} * 1, 2);
 
 929     my $row_set = [ { map { $_ => { 'data' => $entry->{$_}, 'align' => $column_alignment{$_} } } @columns } ];
 
 931     if ( ($form->{subtotal} eq 'Y' && !$form->{report_generator_csv_options_for_import} )
 
 932         && (($idx == (scalar @contents - 1))
 
 933             || ($entry->{$sort_col} ne $contents[$idx + 1]->{$sort_col}))) {
 
 935       my $row = { map { $_ => { 'data' => '', 'class' => 'listsubtotal', 'align' => $column_alignment{$_}, } } @columns };
 
 936       $row->{qty}->{data}         = $form->format_amount(\%myconfig, $subtotals{qty});
 
 937 #      $row->{qty}->{data}         = $form->format_amount_units('amount'     => $subtotals{qty} * 1,
 
 938 #                                                               'part_unit'  => $entry->{partunit},
 
 939 #                                                               'conv_units' => 'convertible');
 
 940       $row->{stock_value}->{data} = $form->format_amount(\%myconfig, $subtotals{stock_value} * 1, 2);
 
 941       $row->{purchase_price}->{data} = $form->format_amount(\%myconfig, $subtotals{purchase_price} * 1, 2);
 
 943       %subtotals                  = map { $_ => 0 } @subtotals_columns;
 
 945       push @{ $row_set }, $row;
 
 948     if ( $allrows || ($idx >= $first_nr && $idx < $last_nr )) {
 
 949         $report->add_data($row_set);
 
 954   if ( $column_defs{stock_value}->{visible} && !$form->{report_generator_csv_options_for_import} ) {
 
 955     $report->add_separator();
 
 957     my $row                      = { map { $_ => { 'data' => '', 'class' => 'listsubtotal', } } @columns };
 
 959     my $left_col                 = first { $column_defs{$_}->{visible} } @columns;
 
 961     $row->{$left_col}->{data}    = $locale->text('Total stock value');
 
 962     $row->{stock_value}->{data}  = $form->format_amount(\%myconfig, $total_stock_value, 2);
 
 963     $row->{stock_value}->{align} = 'right';
 
 965     $report->add_data($row);
 
 968     $pages->{max}  = SL::DB::Helper::Paginated::ceil($form->{maxrows}, $pages->{per_page}) || 1;
 
 969     $pages->{page} = $page < 1 ? 1: $page > $pages->{max} ? $pages->{max}: $page;
 
 970     $pages->{common} = [ grep { $_->{visible} } @{ SL::DB::Helper::Paginated::make_common_pages($pages->{page}, $pages->{max}) } ];
 
 972     $report->set_options('raw_bottom_info_text' => $form->parse_html_template('common/paginate',
 
 973                                                                               {'pages' => $pages , 'base_url' => $href}) );
 
 976   $report->generate_with_headers();
 
 978   $main::lxdebug->leave_sub();
 
 981 # --------------------------------------------------------------------
 
 983 # --------------------------------------------------------------------
 
 985 sub show_no_warehouses_error {
 
 986   $main::lxdebug->enter_sub();
 
 988   my $form     = $main::form;
 
 989   my %myconfig = %main::myconfig;
 
 990   my $locale   = $main::locale;
 
 992   my $msg = $locale->text('No warehouse has been created yet or the quantity of the bins is not configured yet.') . ' ';
 
 994   if ($main::auth->check_right($::myconfig{login}, 'config')) {
 
 995     $msg .= $locale->text('You can create warehouses and bins via the menu "System -> Warehouses".');
 
 997     $msg .= $locale->text('Please ask your administrator to create warehouses and bins.');
 
1000   $form->show_generic_error($msg);
 
1002   $main::lxdebug->leave_sub();
 
1005 sub get_warehouse_idx {
 
1006   my ($warehouse_id) = @_;
 
1008   my $form     = $main::form;
 
1010   for (my $i = 0; $i < scalar @{$form->{WAREHOUSES}}; $i++) {
 
1011     return $i if ($form->{WAREHOUSES}->[$i]->{id} == $warehouse_id);
 
1018   my ($warehouse_index, $bin_id) = @_;
 
1020   my $form     = $main::form;
 
1022   my $warehouse = $form->{WAREHOUSES}->[$warehouse_index];
 
1024   return -1 if (!$warehouse);
 
1026   for (my $i = 0; $i < scalar @{ $warehouse->{BINS} }; $i++) {
 
1027     return $i if ($warehouse->{BINS}->[$i]->{id} == $bin_id);
 
1034   $main::lxdebug->enter_sub();
 
1037   my $form     = $main::form;
 
1040   $form->{old_callback} = $form->escape($form->{callback}, 1);
 
1041   $form->{callback}     = $form->escape("$form->{script}?action=$params{action}", 1);
 
1043   # save all form variables except action in a previousform variable
 
1044   my $previousform = join '&', map { my $value = $form->{$_}; $value =~ s/&/%26/; "$_=$value" } grep { !/action/ } keys %$form;
 
1047 #  push @HIDDENS,      { 'name' => 'previousform', 'value' => $form->escape($previousform, 1) };
 
1048   push @HIDDENS, map +{ 'name' => $_,             'value' => $form->{$_} }, qw(partnumber description unit vc sellprice ean);
 
1049   push @HIDDENS,      { 'name' => 'taxaccount2',  'value' => $form->{taxaccounts} };
 
1050   push @HIDDENS,      { 'name' => 'notes',        'value' => $form->{longdescription} };
 
1053   print $form->parse_html_template("generic/new_item", { HIDDENS => [ sort { $a->{name} cmp $b->{name} } @HIDDENS ] } );
 
1055   $main::lxdebug->leave_sub();
 
1059   my $form     = $main::form;
 
1060   call_sub($form->{update_nextsub} || $form->{nextsub});
 
1064   my $form     = $main::form;
 
1065   call_sub($form->{continue_nextsub} || $form->{nextsub});
 
1069   my $form     = $main::form;
 
1070   call_sub($form->{stock_nextsub} || $form->{nextsub});
 
1073 sub setup_wh_transfer_warehouse_selection_action_bar {
 
1076   for my $bar ($::request->layout->get('actionbar')) {
 
1080         submit    => [ '#form', { action => $action } ],
 
1081         accesskey => 'enter',
 
1087 sub setup_wh_transfer_warehouse_selection_assembly_action_bar {
 
1090   for my $bar ($::request->layout->get('actionbar')) {
 
1094         submit    => [ '#form', { action => 'transfer_assembly_update_part' } ],
 
1095         accesskey => 'enter',
 
1099         submit   => [ '#form', { action => 'create_assembly' } ],
 
1100         disabled => $::form->{parts_id} ? undef : $::locale->text('No assembly has been selected yet.'),
 
1106 sub setup_wh_transfer_parts_action_bar {
 
1109   for my $bar ($::request->layout->get('actionbar')) {
 
1113         submit    => [ '#form', { action => 'transfer_parts' } ],
 
1114         accesskey => 'enter',
 
1118         call => [ 'kivi.history_back' ],
 
1124 sub setup_wh_removal_parts_selection_action_bar {
 
1127   for my $bar ($::request->layout->get('actionbar')) {
 
1131         submit    => [ '#form', { action => 'remove_parts' } ],
 
1132         accesskey => 'enter',
 
1136         call => [ 'kivi.history_back' ],
 
1142 sub setup_wh_report_action_bar {
 
1145   for my $bar ($::request->layout->get('actionbar')) {
 
1149         submit    => [ '#form', { action => 'generate_report' } ],
 
1150         accesskey => 'enter',
 
1156 sub setup_wh_journal_action_bar {
 
1159   for my $bar ($::request->layout->get('actionbar')) {
 
1163         submit    => [ '#form', { action => 'generate_journal' } ],
 
1164         accesskey => 'enter',
 
1176 bin/mozilla/wh.pl - Warehouse frontend.
 
1184 call new item dialogue from warehouse masks.
 
1187   action  => name of sub to be called when new item is done