Projekt

Allgemein

Profil

Herunterladen (48,5 KB) Statistiken
| Zweig: | Markierung: | Revision:
83914eeb Moritz Bunkus
#=====================================================================
# LX-Office ERP
# Copyright (C) 2004
# Based on SQL-Ledger Version 2.1.9
# Web http://www.lx-office.org
#############################################################################
# SQL-Ledger, Accounting
# Copyright (c) 1998-2002
#
# Author: Dieter Simader
# Email: dsimader@sql-ledger.org
# Web: http://www.sql-ledger.org
#
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
f7b15d43 Christian Wittmer
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1335, USA.
83914eeb Moritz Bunkus
#
#######################################################################
#
# warehouse and packinglist
#
#######################################################################

7fc41a37 Moritz Bunkus
use Carp;
83914eeb Moritz Bunkus
use List::Util qw(min max first);
use POSIX qw(strftime);
7fc41a37 Moritz Bunkus
use Scalar::Util qw(blessed);
83914eeb Moritz Bunkus
use SL::Form;
use SL::User;

use SL::AM;
0908bb6e Jan Büren
use SL::CVar;
83914eeb Moritz Bunkus
use SL::CT;
79b75e07 Jan Büren
use SL::Helper::Flash qw(flash_later);
83914eeb Moritz Bunkus
use SL::IC;
use SL::WH;
use SL::OE;
455d008c Jan Büren
use SL::Helper::Inventory qw(produce_assembly);
3c005fa1 Moritz Bunkus
use SL::Locale::String qw(t8);
83914eeb Moritz Bunkus
use SL::ReportGenerator;
79b75e07 Jan Büren
use SL::Presenter::Tag qw(checkbox_tag);
a97574b8 Sven Schöling
use SL::Presenter::Part;
83914eeb Moritz Bunkus
4e9adbf7 Thomas Heck
use SL::DB::Part;

83914eeb Moritz Bunkus
use Data::Dumper;

require "bin/mozilla/common.pl";
require "bin/mozilla/reportgenerator.pl";

13dcc338 Sven Schöling
use strict;

83914eeb Moritz Bunkus
# parserhappy(R):

# contents of the "transfer_type" table:
# $locale->text('back')
# $locale->text('correction')
# $locale->text('disposed')
# $locale->text('found')
# $locale->text('missing')
# $locale->text('stock')
5d154bd1 Moritz Bunkus
# $locale->text('shipped')
83914eeb Moritz Bunkus
# $locale->text('transfer')
# $locale->text('used')
# $locale->text('return_material')
# $locale->text('release_material')
638f06c4 Jan Büren
# $locale->text('assembled')
78d75869 Bernd Bleßmann
# $locale->text('stocktaking')
83914eeb Moritz Bunkus
# --------------------------------------------------------------------
# Transfer
# --------------------------------------------------------------------

sub transfer_warehouse_selection {
13dcc338 Sven Schöling
$main::lxdebug->enter_sub();
83914eeb Moritz Bunkus
13dcc338 Sven Schöling
$main::auth->assert('warehouse_management');

my $form = $main::form;
my %myconfig = %main::myconfig;
my $locale = $main::locale;
83914eeb Moritz Bunkus
$form->get_lists('warehouses' => { 'key' => 'WAREHOUSES',
'bins' => 'BINS', });

show_no_warehouses_error() if (!scalar @{ $form->{WAREHOUSES} });

07948c34 Moritz Bunkus
my $units = AM->retrieve_units(\%myconfig, $form);
4e9adbf7 Thomas Heck
my $part = 0;
if ( $form->{parts_id} ) {
$part = SL::DB::Part->new();
$part->id($form->{parts_id});
$part->load();
}

8c6da403 Jan Büren
# der zweite Parameter von unit_select_data gibt den default-Namen (selected) vor
4e9adbf7 Thomas Heck
$form->{UNITS} = AM->unit_select_data($units, $form->{unit}, 0, $part ? $part->unit : 0);
83914eeb Moritz Bunkus
if (scalar @{ $form->{WAREHOUSES} }) {
$form->{warehouse_id} ||= $form->{WAREHOUSES}->[0]->{id};
$form->{bin_id} ||= $form->{WAREHOUSES}->[0]->{BINS}->[0]->{id};
}

my $content;

if ($form->{trans_type} eq 'removal') {
3c005fa1 Moritz Bunkus
setup_wh_transfer_warehouse_selection_action_bar("removal_parts_selection");
83914eeb Moritz Bunkus
$form->{title} = $locale->text('Removal from Warehouse');
$content = $form->parse_html_template('wh/warehouse_selection');

} elsif (!$form->{trans_type} || ($form->{trans_type} eq 'transfer')) {
3c005fa1 Moritz Bunkus
setup_wh_transfer_warehouse_selection_action_bar("transfer_parts_selection");
83914eeb Moritz Bunkus
$form->{title} = $locale->text('Transfer');
$content = $form->parse_html_template('wh/warehouse_selection');

4f63ea87 Geoffrey Richardson
} elsif ($form->{trans_type} eq 'assembly') {
3c005fa1 Moritz Bunkus
setup_wh_transfer_warehouse_selection_assembly_action_bar();
eb3f8a46 Geoffrey Richardson
$form->{title} = $locale->text('Produce Assembly');
4f63ea87 Geoffrey Richardson
$content = $form->parse_html_template('wh/warehouse_selection_assembly');
83914eeb Moritz Bunkus
}

$form->header();
print $content;

13dcc338 Sven Schöling
$main::lxdebug->leave_sub();
83914eeb Moritz Bunkus
}

sub transfer_parts_selection {
13dcc338 Sven Schöling
$main::lxdebug->enter_sub();

$main::auth->assert('warehouse_management');
83914eeb Moritz Bunkus
13dcc338 Sven Schöling
my $form = $main::form;
my %myconfig = %main::myconfig;
my $locale = $main::locale;
83914eeb Moritz Bunkus
transfer_or_removal_prepare_contents('direction' => 'transfer');

3c005fa1 Moritz Bunkus
setup_wh_transfer_parts_action_bar();

83914eeb Moritz Bunkus
$form->{title} = $locale->text('Transfer');
$form->header();
print $form->parse_html_template("wh/transfer_parts_selection");

13dcc338 Sven Schöling
$main::lxdebug->leave_sub();
83914eeb Moritz Bunkus
}

sub transfer_or_removal_prepare_contents {
13dcc338 Sven Schöling
$main::lxdebug->enter_sub();
83914eeb Moritz Bunkus
13dcc338 Sven Schöling
$main::auth->assert('warehouse_management');
83914eeb Moritz Bunkus
my %args = @_;

13dcc338 Sven Schöling
my $form = $main::form;
my %myconfig = %main::myconfig;
my $locale = $main::locale;

83914eeb Moritz Bunkus
$form->get_lists('warehouses' => { 'key' => 'WAREHOUSES',
'bins' => 'BINS', });

my $warehouse_idx = get_warehouse_idx($form->{warehouse_id});
$form->show_generic_error($locale->text("The selected warehouse does not exist.")) if (-1 == $warehouse_idx);

my $warehouse = $form->{WAREHOUSES}->[$warehouse_idx];

$form->{initial_warehouse_idx} = $warehouse_idx;
$form->{warehouse_description} = $warehouse->{description};
$warehouse->{selected} = 1;

$form->show_generic_error($locale->text("The source warehouse does not contain any bins.")) if (0 == scalar @{ $warehouse->{BINS} });

2e5114e8 Werner Hahn
map { $form->{"l_$_"} = 'Y' } qw(parts_id qty warehouseid binid partnumber partdescription bin chargenumber bestbefore partunit ean);
83914eeb Moritz Bunkus
$form->{sort} = 'bindescription';
my @contents = WH->get_warehouse_report("warehouse_id" => $form->{warehouse_id},
"bin_id" => $form->{bin_id},
"chargenumber" => $form->{chargenumber},
096f9e3e Bernd Bleßmann
"bestbefore" => $form->{bestbefore},
5d497700 Moritz Bunkus
"partsid" => $form->{part_id},
"ean" => $form->{ean});
83914eeb Moritz Bunkus
b2d4ba3d Moritz Bunkus
if (0 == scalar(@contents)) {
$form->show_generic_error($locale->text("The selected warehouse is empty, or no stocked items where found that match the filter settings."));
}
83914eeb Moritz Bunkus
07948c34 Moritz Bunkus
my $all_units = AM->retrieve_units(\%myconfig, $form);
83914eeb Moritz Bunkus
foreach (@contents) {
1a4d8bb8 Bernd Bleßmann
$_->{qty} = $form->format_amount(\%myconfig, $_->{qty}) . ' ' . $_->{partunit};

83914eeb Moritz Bunkus
my $this_unit = $_->{partunit};

if ($all_units->{$_->{partunit}} && ($all_units->{g}->{base_unit} eq $all_units->{$_->{partunit}}->{base_unit})) {
$this_unit = "kg";
}

$_->{UNITS} = AM->unit_select_data($all_units, $this_unit, 0, $_->{partunit});
}

my $transfer_types = WH->retrieve_transfer_types($args{direction});
map { $_->{description} = $locale->text($_->{description}) } @{ $transfer_types };

$form->{CONTENTS} = \@contents;
$form->{TRANSFER_TYPES} = $transfer_types;

13dcc338 Sven Schöling
$main::lxdebug->leave_sub();
83914eeb Moritz Bunkus
}


sub transfer_parts {
13dcc338 Sven Schöling
$main::lxdebug->enter_sub();
83914eeb Moritz Bunkus
13dcc338 Sven Schöling
$main::auth->assert('warehouse_management');

my $form = $main::form;
my %myconfig = %main::myconfig;
my $locale = $main::locale;
83914eeb Moritz Bunkus
$form->get_lists('warehouses' => { 'key' => 'WAREHOUSES', 'bins' => 'BINS' });

my $warehouse_idx = get_warehouse_idx($form->{warehouse_id});
$form->show_generic_error($locale->text("The selected warehouse does not exist.")) if (-1 == $warehouse_idx);

my $warehouse = $form->{WAREHOUSES}->[$warehouse_idx];

$form->show_generic_error($locale->text("The source warehouse does not contain any bins.")) if (0 == scalar @{ $warehouse->{BINS} });

8cd9ac14 Werner Hahn
map { $form->{"l_$_"} = 'Y' } qw(parts_id qty warehouseid binid partnumber partdescription bin chargenumber bestbefore partunit);
83914eeb Moritz Bunkus
$form->{sort} = 'bindescription';
my @contents = WH->get_warehouse_report("warehouse_id" => $form->{warehouse_id});
07948c34 Moritz Bunkus
my $all_units = AM->retrieve_units(\%myconfig, $form);
83914eeb Moritz Bunkus
my @transfers;

foreach my $row (1 .. $form->{rowcount}) {
$form->{"qty_$row"} =~ s/^\s*//;
$form->{"qty_$row"} =~ s/\s*$//;
next if (!$form->{"qty_$row"});

my $bin_idx = get_bin_idx($warehouse_idx, $form->{"src_bin_id_$row"});
$form->show_generic_error($locale->text("The selected bin does not exist.")) if (-1 == $bin_idx);
my $bin = $warehouse->{BINS}->[$bin_idx];

my $orig_qty = $form->{"qty_$row"} . " " . $form->{"unit_$row"};

my $transfer = {
'src_warehouse_id' => $form->{warehouse_id},
'transfer_type_id' => $form->{transfer_type_id},
};

096f9e3e Bernd Bleßmann
map { $transfer->{$_} = $form->{"${_}_${row}"} } qw(src_bin_id chargenumber bestbefore parts_id qty dst_warehouse_id dst_bin_id);
83914eeb Moritz Bunkus
my $entry;

foreach (@contents) {
096f9e3e Bernd Bleßmann
if (($_->{binid} == $transfer->{src_bin_id}) && ($_->{parts_id} == $transfer->{parts_id}) && ($_->{chargenumber} eq $transfer->{chargenumber}) && $_->{bestbefore} eq $transfer->{bestbefore}) {
83914eeb Moritz Bunkus
$entry = $_;
last;
}
}

if (!$entry) {
$form->error($locale->text("There is not enough left of '#1' in bin '#2' for the removal of #3.",
$form->{"partdescription_$row"}, $bin->{description}, $orig_qty));
}

$transfer->{qty} = $form->parse_amount(\%myconfig, $transfer->{qty}) * $all_units->{$form->{"unit_$row"}}->{factor};
$transfer->{qty} /= $all_units->{$entry->{partunit}}->{factor} || 1;

if (($entry->{qty} < $transfer->{qty}) || (0 >= $transfer->{qty})) {
$form->error($locale->text("There is not enough left of '#1' in bin '#2' for the removal of #3.",
$form->{"partdescription_$row"}, $bin->{description}, $orig_qty));
}

$transfer->{comment} = $form->{comment};
5a923b79 Jan Büren
$transfer->{change_default_bin} = $form->{change_default_bin};
83914eeb Moritz Bunkus
push @transfers, $transfer;

$entry->{qty} -= $transfer->{qty};
}

if (!scalar @transfers) {
$form->show_generic_information($locale->text('Nothing has been selected for transfer.'));
09479f02 Moritz Bunkus
$::dispatcher->end_request;
83914eeb Moritz Bunkus
}

WH->transfer(@transfers);

$form->{trans_type} = 'transfer';
$form->{saved_message} = $locale->text('The parts have been transferred.');

transfer_warehouse_selection();

13dcc338 Sven Schöling
$main::lxdebug->leave_sub();
83914eeb Moritz Bunkus
}

# --------------------------------------------------------------------
# Transfer: stock
# --------------------------------------------------------------------

4f63ea87 Geoffrey Richardson
# --------------------------------------------------------------------
# Transfer: assemblies
fa73b837 Sven Schöling
# Dies ist die Auswahlmaske für ein assembly.
4f63ea87 Geoffrey Richardson
# Die ist einfach von transfer_assembly_update_part kopiert und nur um den trans_type (assembly) korrigiert worden
# Es wäre schön, hier nochmal check_assembly_max_create auf, um die max. Fertigungszahl herauszufinden.
# Ich lass das mal als auskommentierte Idee bestehen jb 18.3.09
# --------------------------------------------------------------------

sub transfer_assembly_update_part {
13dcc338 Sven Schöling
my $form = $main::form;
my %myconfig = %main::myconfig;
my $locale = $main::locale;
4f63ea87 Geoffrey Richardson
$form->{trans_type} = 'assembly';
$form->{qty} = $form->parse_amount(\%myconfig, $form->{qty});

5d497700 Moritz Bunkus
if (!$form->{parts_id}) {
delete $form->{partunit};
4f63ea87 Geoffrey Richardson
transfer_warehouse_selection();
5d497700 Moritz Bunkus
return;
4f63ea87 Geoffrey Richardson
}

5d497700 Moritz Bunkus
my $part = SL::DB::Part->new(id => $::form->{parts_id})->load;
@{$form}{qw(parts_id partnumber description)} = ($part->id, $part->partnumber, $part->description);

transfer_stock_get_partunit();
transfer_warehouse_selection();
4f63ea87 Geoffrey Richardson
}
13dcc338 Sven Schöling
83914eeb Moritz Bunkus
sub transfer_stock_part_selected {
13dcc338 Sven Schöling
$main::lxdebug->enter_sub();
83914eeb Moritz Bunkus
my $part = shift;

13dcc338 Sven Schöling
my $form = $main::form;

ccbd47d7 Jan Büren
@{$form}{qw(parts_id partnumber description ean warehouse_id bin_id)} = @{$part}{qw(id partnumber description ean warehouse_id bin_id)};
83914eeb Moritz Bunkus
transfer_stock_get_partunit();
transfer_warehouse_selection();

13dcc338 Sven Schöling
$main::lxdebug->leave_sub();
83914eeb Moritz Bunkus
}

sub transfer_stock_get_partunit {
13dcc338 Sven Schöling
$main::lxdebug->enter_sub();

my $form = $main::form;
83914eeb Moritz Bunkus
if ($form->{parts_id}) {
my $part_info = IC->get_basic_part_info('id' => $form->{parts_id});
$form->{partunit} = $part_info->{unit};
}

13dcc338 Sven Schöling
$main::lxdebug->leave_sub();
83914eeb Moritz Bunkus
}

4f63ea87 Geoffrey Richardson
sub create_assembly {
13dcc338 Sven Schöling
$main::lxdebug->enter_sub();

my $form = $main::form;
my %myconfig = %main::myconfig;
my $locale = $main::locale;
4f63ea87 Geoffrey Richardson
$form->{qty} = $form->parse_amount(\%myconfig, $form->{qty});
if ($form->{qty} <= 0) {
a8459f49 Moritz Bunkus
$form->show_generic_error($locale->text('Invalid quantity.'));
64c39b74 Geoffrey Richardson
}
4f63ea87 Geoffrey Richardson
if (!$form->{warehouse_id} || !$form->{bin_id}) {
$form->error($locale->text('The warehouse or the bin is missing.'));
}
455d008c Jan Büren
# need part and bin object
my ($bin, $assembly);
$assembly = SL::DB::Manager::Part->find_by(id => $form->{parts_id}, part_type => 'assembly');
$form->show_generic_error($locale->text('Invalid assembly')) unless ref $assembly eq 'SL::DB::Part';

$bin = SL::DB::Manager::Bin->find_by(id => $form->{bin_id});
$form->show_generic_error($locale->text('Invalid bin')) unless ref $bin eq 'SL::DB::Bin';
f2d34a8e Bernd Bleßmann
4b31e6ba Bernd Bleßmann
if (!$::instance_conf->get_show_bestbefore) {
455d008c Jan Büren
$form->{bestbefore} = '';
f2d34a8e Bernd Bleßmann
}

7fc41a37 Moritz Bunkus
eval {
produce_assembly(
455d008c Jan Büren
part => $assembly, # target assembly
qty => $form->{qty}, # qty
auto_allocate => 1,
bin => $bin, # needed unless a global standard target is configured
chargenumber => $form->{chargenumber}, # optional
bestbefore => $form->{bestbefore},
comment => $form->{comment}, # optional
7fc41a37 Moritz Bunkus
);
1;

} or do {
my $ex = $@;
die $ex unless blessed($ex) && $ex->isa('SL::X::Inventory::Allocation::Multi');

$form->{title} = $locale->text('Produce Assembly');
$form->header;
print $form->parse_html_template(
'wh/produce_assembly_error',
{
missing_qty_exceptions => [ grep { $_->isa('SL::X::Inventory::Allocation::MissingQty') } @{ $ex->errors } ],
other_exceptions => [ grep { !$_->isa('SL::X::Inventory::Allocation::MissingQty') } @{ $ex->errors } ],
});

return $::lxdebug->leave_sub();
};
4f63ea87 Geoffrey Richardson
096f9e3e Bernd Bleßmann
delete @{$form}{qw(parts_id partnumber description qty unit chargenumber bestbefore comment)};
4f63ea87 Geoffrey Richardson
$form->{saved_message} = $locale->text('The assembly has been created.');
$form->{trans_type} = 'assembly';

transfer_warehouse_selection();

13dcc338 Sven Schöling
$main::lxdebug->leave_sub();
4f63ea87 Geoffrey Richardson
}

83914eeb Moritz Bunkus
# --------------------------------------------------------------------
# Transfer: removal
# --------------------------------------------------------------------

sub removal_parts_selection {
13dcc338 Sven Schöling
$main::lxdebug->enter_sub();

$main::auth->assert('warehouse_management');
83914eeb Moritz Bunkus
13dcc338 Sven Schöling
my $form = $main::form;
my %myconfig = %main::myconfig;
my $locale = $main::locale;
83914eeb Moritz Bunkus
transfer_or_removal_prepare_contents('direction' => 'out');

3c005fa1 Moritz Bunkus
setup_wh_removal_parts_selection_action_bar();

83914eeb Moritz Bunkus
$form->{title} = $locale->text('Removal');
$form->header();
print $form->parse_html_template("wh/removal_parts_selection");

13dcc338 Sven Schöling
$main::lxdebug->leave_sub();
83914eeb Moritz Bunkus
}

sub remove_parts {
13dcc338 Sven Schöling
$main::lxdebug->enter_sub();

$main::auth->assert('warehouse_management');
83914eeb Moritz Bunkus
13dcc338 Sven Schöling
my $form = $main::form;
my %myconfig = %main::myconfig;
my $locale = $main::locale;
83914eeb Moritz Bunkus
$form->get_lists('warehouses' => { 'key' => 'WAREHOUSES',
'bins' => 'BINS', });

my $warehouse_idx = get_warehouse_idx($form->{warehouse_id});
$form->show_generic_error($locale->text("The selected warehouse does not exist.")) if (-1 == $warehouse_idx);

my $warehouse = $form->{WAREHOUSES}->[$warehouse_idx];

$form->show_generic_error($locale->text("The warehouse does not contain any bins.")) if (0 == scalar @{ $warehouse->{BINS} });

2e5114e8 Werner Hahn
map { $form->{"l_$_"} = 'Y' } qw(parts_id qty warehouseid binid partnumber partdescription bin chargenumber bestbefore partunit);
83914eeb Moritz Bunkus
$form->{sort} = 'bindescription';
my @contents = WH->get_warehouse_report("warehouse_id" => $form->{warehouse_id});
07948c34 Moritz Bunkus
my $all_units = AM->retrieve_units(\%myconfig, $form);
83914eeb Moritz Bunkus
my @transfers;

foreach my $row (1 .. $form->{rowcount}) {
$form->{"qty_$row"} =~ s/^\s*//;
$form->{"qty_$row"} =~ s/\s*$//;
next if (!$form->{"qty_$row"});

my $bin_idx = get_bin_idx($warehouse_idx, $form->{"src_bin_id_$row"});
$form->show_generic_error($locale->text("The selected bin does not exist.")) if (-1 == $bin_idx);
my $bin = $warehouse->{BINS}->[$bin_idx];

my $orig_qty = $form->{"qty_$row"} . " " . $form->{"unit_$row"};

my $transfer = {
'src_warehouse_id' => $form->{warehouse_id},
'transfer_type_id' => $form->{transfer_type_id},
};

096f9e3e Bernd Bleßmann
map { $transfer->{$_} = $form->{"${_}_${row}"} } qw(src_bin_id chargenumber bestbefore parts_id qty);
83914eeb Moritz Bunkus
my $entry;

foreach (@contents) {
096f9e3e Bernd Bleßmann
if (($_->{binid} == $transfer->{src_bin_id}) && ($_->{parts_id} == $transfer->{parts_id}) && ($_->{chargenumber} eq $transfer->{chargenumber}) && ($_->{bestbefore} eq $transfer->{bestbefore})) {
83914eeb Moritz Bunkus
$entry = $_;
last;
}
}

if (!$entry) {
$form->error($locale->text("There is not enough left of '#1' in bin '#2' for the removal of #3.",
$form->{"partdescription_$row"}, $bin->{description}, $orig_qty));
}

$transfer->{qty} = $form->parse_amount(\%myconfig, $transfer->{qty}) * $all_units->{$form->{"unit_$row"}}->{factor};
$transfer->{qty} /= $all_units->{$entry->{partunit}}->{factor} || 1;

if (($entry->{qty} < $transfer->{qty}) || (0 >= $transfer->{qty})) {
$form->error($locale->text("There is not enough left of '#1' in bin '#2' for the removal of #3.",
$form->{"partdescription_$row"}, $bin->{description}, $orig_qty));
}

$transfer->{comment} = $form->{comment};

push @transfers, $transfer;

$entry->{qty} -= $transfer->{qty};
}

if (!scalar @transfers) {
$form->show_generic_information($locale->text('Nothing has been selected for removal.'));
09479f02 Moritz Bunkus
$::dispatcher->end_request;
83914eeb Moritz Bunkus
}

WH->transfer(@transfers);

$form->{trans_type} = 'removal';
$form->{saved_message} = $locale->text('The parts have been removed.');

transfer_warehouse_selection();

13dcc338 Sven Schöling
$main::lxdebug->leave_sub();
83914eeb Moritz Bunkus
}

79b75e07 Jan Büren
sub disassemble_assembly {
$main::lxdebug->enter_sub();

$main::auth->assert('warehouse_management');

my $form = $main::form;

croak("No assembly ids") unless scalar @{ $form->{ids}} > 0;

8453789b Jan Büren
# everything in one transaction
my $db = SL::DB::Inventory->new->db;
$db->with_transaction(sub {
79b75e07 Jan Büren
8453789b Jan Büren
foreach my $trans_id (@{ $::form->{ids}} ) {
SL::DB::Manager::Inventory->delete_all(where => [ trans_id => $trans_id ]);
flash_later('info', t8("Disassembly successful for trans_id #1", $trans_id));
}
79b75e07 Jan Büren
8453789b Jan Büren
1;
}) || die t8('error while disassembling for trans_ids #1 : #2', $form->{ids}) . $db->error . "\n";
79b75e07 Jan Büren
$main::lxdebug->leave_sub();
$form->redirect;
}

83914eeb Moritz Bunkus
# --------------------------------------------------------------------
# Journal
# --------------------------------------------------------------------

sub journal {
13dcc338 Sven Schöling
$main::lxdebug->enter_sub();
83914eeb Moritz Bunkus
13dcc338 Sven Schöling
$main::auth->assert('warehouse_management');

my $form = $main::form;
my %myconfig = %main::myconfig;
1de53cbf Sven Donath
my $locale = $main::locale;
83914eeb Moritz Bunkus
1de53cbf Sven Donath
$form->{title} = $locale->text('Report about warehouse transactions');
83914eeb Moritz Bunkus
$form->get_lists('warehouses' => { 'key' => 'WAREHOUSES',
'bins' => 'BINS', });

show_no_warehouses_error() if (!scalar @{ $form->{WAREHOUSES} });

3193bd4a Bernd Bleßmann
my $cvar_configs = CVar->get_configs('module' => 'IC');
5d2ede53 Bernd Bleßmann
($form->{CUSTOM_VARIABLES_FILTER_CODE},
3193bd4a Bernd Bleßmann
$form->{CUSTOM_VARIABLES_INCLUSION_CODE}) = CVar->render_search_options('variables' => $cvar_configs,
'include_prefix' => 'l_',
'include_value' => 'Y');

3c005fa1 Moritz Bunkus
setup_wh_journal_action_bar();

83914eeb Moritz Bunkus
$form->header();
print $form->parse_html_template("wh/journal_filter", { "UNITS" => AM->unit_select_data(AM->retrieve_units(\%myconfig, $form)) });

13dcc338 Sven Schöling
$main::lxdebug->leave_sub();
83914eeb Moritz Bunkus
}

sub generate_journal {
13dcc338 Sven Schöling
$main::lxdebug->enter_sub();
83914eeb Moritz Bunkus
13dcc338 Sven Schöling
$main::auth->assert('warehouse_management');

my $form = $main::form;
my %myconfig = %main::myconfig;
my $locale = $main::locale;
83914eeb Moritz Bunkus
79b75e07 Jan Büren
setup_wh_journal_list_all_action_bar();
83914eeb Moritz Bunkus
$form->{title} = $locale->text("WHJournal");
$form->{sort} ||= 'date';

34035b33 Martin Helmling
$form->{report_generator_output_format} = 'HTML' if !$form->{report_generator_output_format};

83914eeb Moritz Bunkus
my %filter;
79b75e07 Jan Büren
my @columns = qw(ids 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);
83914eeb Moritz Bunkus
# filter stuff
3c938e03 Martin Helmling
map { $filter{$_} = $form->{$_} if ($form->{$_}) } qw(warehouse_id bin_id classification_id partnumber description chargenumber bestbefore transtype_id transtype_ids comment projectnumber);
83914eeb Moritz Bunkus
$filter{qty_op} = WH->convert_qty_op($form->{qty_op});
if ($filter{qty_op}) {
13dcc338 Sven Schöling
$form->isblank("qty", $locale->text('Quantity missing.'));
$form->isblank("qty_unit", $locale->text('Unit missing.'));
83914eeb Moritz Bunkus
$filter{qty} = $form->{qty};
$filter{qty_unit} = $form->{qty_unit};
}
# /filter stuff

6aaed579 Martin Helmling
my $allrows = !!($form->{report_generator_output_format} ne 'HTML') ;
3c938e03 Martin Helmling
# manual paginating
6aaed579 Martin Helmling
my $pages = {};
my $page = $::form->{page} || 1;
$pages->{per_page} = $::form->{per_page} || 15;
my $first_nr = ($page - 1) * $pages->{per_page};
my $last_nr = $first_nr + $pages->{per_page};
3c938e03 Martin Helmling
# no optimisation if qty op
if ( !$allrows && $form->{maxrows} && !$filter{qty_op}) {
$filter{limit} = $pages->{per_page};
$filter{offset} = ($page - 1) * $pages->{per_page};
6aaed579 Martin Helmling
$first_nr = 0;
$last_nr = $pages->{per_page};
3c938e03 Martin Helmling
}

8453789b Jan Büren
my $old_l_trans_id = $form->{l_trans_id};
3c938e03 Martin Helmling
my @contents = WH->get_warehouse_journal(%filter);
8453789b Jan Büren
$form->{l_trans_id} = $old_l_trans_id;

3c938e03 Martin Helmling
# get maxcount
if (!$form->{maxrows}) {
$form->{maxrows} = scalar @contents ;
}

83914eeb Moritz Bunkus
my $report = SL::ReportGenerator->new(\%myconfig, $form);

3193bd4a Bernd Bleßmann
my $cvar_configs = CVar->get_configs('module' => 'IC');
my @includeable_custom_variables = grep { $_->{includeable} } @{ $cvar_configs };
5d2ede53 Bernd Bleßmann
my @searchable_custom_variables = grep { $_->{searchable} } @{ $cvar_configs };
3193bd4a Bernd Bleßmann
push @columns, map { "cvar_$_->{name}" } @includeable_custom_variables;

83914eeb Moritz Bunkus
my @hidden_variables = map { "l_${_}" } @columns;
3c938e03 Martin Helmling
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);
65d2537d Martin Helmling
push @hidden_variables, qw(classification_id);
5d2ede53 Bernd Bleßmann
push @hidden_variables, map({'cvar_'. $_->{name}} @searchable_custom_variables);
push @hidden_variables, map({'cvar_'. $_->{name} .'_from'} grep({$_->{type} eq 'date'} @searchable_custom_variables));
push @hidden_variables, map({'cvar_'. $_->{name} .'_to'} grep({$_->{type} eq 'date'} @searchable_custom_variables));
push @hidden_variables, map({'cvar_'. $_->{name} .'_qtyop'} grep({$_->{type} eq 'number'} @searchable_custom_variables));
83914eeb Moritz Bunkus
my %column_defs = (
79b75e07 Jan Büren
'ids' => { raw_header_data => checkbox_tag("", id => "check_all", checkall => "[data-checkall=1]") },
83914eeb Moritz Bunkus
'date' => { 'text' => $locale->text('Date'), },
'trans_id' => { 'text' => $locale->text('Trans Id'), },
'trans_type' => { 'text' => $locale->text('Trans Type'), },
'comment' => { 'text' => $locale->text('Comment'), },
'warehouse_from' => { 'text' => $locale->text('Warehouse From'), },
'warehouse_to' => { 'text' => $locale->text('Warehouse To'), },
'bin_from' => { 'text' => $locale->text('Bin From'), },
'bin_to' => { 'text' => $locale->text('Bin To'), },
'partnumber' => { 'text' => $locale->text('Part Number'), },
65d2537d Martin Helmling
'type_and_classific'
=> { 'text' => $locale->text('Type'), },
4b937d1a Sven Donath
'partdescription' => { 'text' => $locale->text('Part Description'), },
83914eeb Moritz Bunkus
'chargenumber' => { 'text' => $locale->text('Charge Number'), },
096f9e3e Bernd Bleßmann
'bestbefore' => { 'text' => $locale->text('Best Before'), },
83914eeb Moritz Bunkus
'qty' => { 'text' => $locale->text('Qty'), },
34035b33 Martin Helmling
'unit' => { 'text' => $locale->text('Part Unit'), },
'partunit' => { 'text' => $locale->text('Unit'), },
83914eeb Moritz Bunkus
'employee' => { 'text' => $locale->text('Employee'), },
'projectnumber' => { 'text' => $locale->text('Project Number'), },
17409513 Moritz Bunkus
'oe_id' => { 'text' => $locale->text('Document'), },
83914eeb Moritz Bunkus
);

3193bd4a Bernd Bleßmann
my %column_defs_cvars = map { +"cvar_$_->{name}" => { 'text' => $_->{description} } } @includeable_custom_variables;
%column_defs = (%column_defs, %column_defs_cvars);

3c938e03 Martin Helmling
if ($form->{transtype_ids} && 'ARRAY' eq ref $form->{transtype_ids}) {
for (my $i = 0; $i < scalar(@{ $form->{transtype_ids} }); $i++) {
delete $form->{transtype_ids}[$i] if $form->{transtype_ids}[$i] eq '';
}
$form->{transtype_ids} = join(",", @{ $form->{transtype_ids} });
}

83914eeb Moritz Bunkus
my $href = build_std_url('action=generate_journal', grep { $form->{$_} } @hidden_variables);
3c938e03 Martin Helmling
$href .= "&maxrows=".$form->{maxrows};

3193bd4a Bernd Bleßmann
map { $column_defs{$_}->{link} = $href ."&page=".$page. "&sort=${_}&order=" . Q($_ eq $form->{sort} ? 1 - $form->{order} : $form->{order}) } grep {!/^cvar/} @columns;
83914eeb Moritz Bunkus
my %column_alignment = map { $_ => 'right' } qw(qty);

map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
3c938e03 Martin Helmling
$column_defs{partunit}->{visible} = 1;
65d2537d Martin Helmling
$column_defs{type_and_classific}->{visible} = 1;
$column_defs{type_and_classific}->{link} ='';
79b75e07 Jan Büren
$column_defs{ids}->{visible} = 1;
83914eeb Moritz Bunkus
$report->set_columns(%column_defs);
$report->set_column_order(@columns);

19688fca Moritz Bunkus
$report->set_export_options('generate_journal', @hidden_variables, qw(sort order));
83914eeb Moritz Bunkus
$report->set_sort_indicator($form->{sort}, $form->{order});

$report->set_options('output_format' => 'HTML',
'title' => $form->{title},
19688fca Moritz Bunkus
'attachment_basename' => strftime($locale->text('warehouse_journal_list') . '_%Y%m%d', localtime time));
83914eeb Moritz Bunkus
$report->set_options_from_form();
a873249c Moritz Bunkus
$locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
83914eeb Moritz Bunkus
my $all_units = AM->retrieve_units(\%myconfig, $form);

3193bd4a Bernd Bleßmann
CVar->add_custom_variables_to_report('module' => 'IC',
'trans_id_field' => 'parts_id',
'configs' => $cvar_configs,
'column_defs' => \%column_defs,
'data' => \@contents);


a0ea00e5 Werner Hahn
my %doc_types = ( 'sales_quotation' => { action => 'edit', script => 'oe', title => $locale->text('Sales quotation') },
'sales_order' => { action => 'edit', script => 'oe', title => $locale->text('Sales Order') },
'request_quotation' => { action => 'edit', script => 'oe', title => $locale->text('Request quotation') },
'purchase_order' => { action => 'edit', script => 'oe', title => $locale->text('Purchase Order') },
'sales_delivery_order' => { action => 'edit', script => 'do', title => $locale->text('Sales delivery order') },
'purchase_delivery_order' => { action => 'edit', script => 'do', title => $locale->text('Purchase delivery order') },
'supplier_delivery_order' => { action => 'DeliveryOrder/edit', script => 'controller', title => $locale->text('Supplier delivery order') },
'rma_delivery_order' => { action => 'DeliveryOrder/edit', script => 'controller', title => $locale->text('RMA delivery order') },
'sales_invoice' => { action => 'edit', script => 'is', title => $locale->text('Sales Invoice') },
'purchase_invoice' => { action => 'edit', script => 'ir', title => $locale->text('Purchase Invoice') },
17409513 Moritz Bunkus
);

65d2537d Martin Helmling
my $idx = 0;
8453789b Jan Büren
my $undo_date = DateTime->today->subtract(days => $::instance_conf->get_undo_transfer_interval);
13dcc338 Sven Schöling
foreach my $entry (@contents) {
a97574b8 Sven Schöling
$entry->{type_and_classific} = SL::Presenter::Part::type_abbreviation($entry->{part_type}) .
SL::Presenter::Part::classification_abbreviation($entry->{classification_id});
3c938e03 Martin Helmling
$entry->{qty} = $form->format_amount(\%myconfig, $entry->{qty});
79b75e07 Jan Büren
$entry->{assembled} = $entry->{trans_type} eq 'assembled' ? 1 : '';
83914eeb Moritz Bunkus
$entry->{trans_type} = $locale->text($entry->{trans_type});
my $row = { };

foreach my $column (@columns) {
$row->{$column} = {
'data' => $entry->{$column},
'align' => $column_alignment{$column},
};
}

8453789b Jan Büren
if ($entry->{assembled}) {
my $insertdate = DateTime->from_kivitendo($entry->{shippingdate});
3e79972f Jan Büren
if (ref $undo_date eq 'DateTime' && ref $insertdate eq 'DateTime' && $insertdate > $undo_date) {
$row->{ids}->{raw_data} = checkbox_tag("ids[]", value => $entry->{trans_id}, "data-checkall" => 1);
8453789b Jan Büren
}
}
79b75e07 Jan Büren
$row->{trans_type}->{raw_data} = $entry->{trans_type};
17409513 Moritz Bunkus
if ($form->{l_oe_id}) {
$row->{oe_id}->{data} = '';
my $info = $entry->{oe_id_info};

if ($info && $info->{id} && $info->{type} && $doc_types{$info->{type}}) {
$row->{oe_id} = { data => $doc_types{ $info->{type} }->{title} . ' ' . $info->{number},
a0ea00e5 Werner Hahn
link => build_std_url('script=' . $doc_types{ $info->{type} }->{script} . '.pl', 'action=' . $doc_types{ $info->{type} }->{action}, 'id=' . $info->{id}, 'type=' . $info->{type}) };
17409513 Moritz Bunkus
}
}

34035b33 Martin Helmling
if ( $allrows || ($idx >= $first_nr && $idx < $last_nr )) {
3c938e03 Martin Helmling
$report->add_data($row);
34035b33 Martin Helmling
}
$idx++;
83914eeb Moritz Bunkus
}

79b75e07 Jan Büren
$report->set_options(
raw_top_info_text => $form->parse_html_template('wh/report_top'),
raw_bottom_info_text => $form->parse_html_template('wh/report_bottom', { callback => $href }),
);
34035b33 Martin Helmling
if ( ! $allrows ) {
3c938e03 Martin Helmling
$pages->{max} = SL::DB::Helper::Paginated::ceil($form->{maxrows}, $pages->{per_page}) || 1;
34035b33 Martin Helmling
$pages->{page} = $page < 1 ? 1: $page > $pages->{max} ? $pages->{max}: $page;
$pages->{common} = [ grep { $_->{visible} } @{ SL::DB::Helper::Paginated::make_common_pages($pages->{page}, $pages->{max}) } ];

79b75e07 Jan Büren
$report->set_options('raw_bottom_info_text' => $form->parse_html_template('wh/report_bottom', { callback => $href }) . $form->parse_html_template('common/paginate',
3c938e03 Martin Helmling
{ 'pages' => $pages , 'base_url' => $href.'&sort='.$form->{sort}.'&order='.$form->{order}}) );
34035b33 Martin Helmling
}
e7913c4c Moritz Bunkus
$report->generate_with_headers();
83914eeb Moritz Bunkus
13dcc338 Sven Schöling
$main::lxdebug->leave_sub();
83914eeb Moritz Bunkus
}

# --------------------------------------------------------------------
# Report
# --------------------------------------------------------------------

sub report {
13dcc338 Sven Schöling
$main::lxdebug->enter_sub();

$main::auth->assert('warehouse_contents | warehouse_management');
83914eeb Moritz Bunkus
13dcc338 Sven Schöling
my $form = $main::form;
my %myconfig = %main::myconfig;
my $locale = $main::locale;
83914eeb Moritz Bunkus
$form->get_lists('warehouses' => { 'key' => 'WAREHOUSES',
db791046 Bernd Bleßmann
'bins' => 'BINS', },
'partsgroup' => 'PARTSGROUPS');
83914eeb Moritz Bunkus
show_no_warehouses_error() if (!scalar @{ $form->{WAREHOUSES} });

ea6ec9c1 Bernd Bleßmann
my $cvar_configs = CVar->get_configs('module' => 'IC');
5d2ede53 Bernd Bleßmann
($form->{CUSTOM_VARIABLES_FILTER_CODE},
ea6ec9c1 Bernd Bleßmann
$form->{CUSTOM_VARIABLES_INCLUSION_CODE}) = CVar->render_search_options('variables' => $cvar_configs,
'include_prefix' => 'l_',
'include_value' => 'Y');
0908bb6e Jan Büren
1de53cbf Sven Donath
$form->{title} = $locale->text("Report about warehouse contents");
83914eeb Moritz Bunkus
3c005fa1 Moritz Bunkus
setup_wh_report_action_bar();

83914eeb Moritz Bunkus
$form->header();
print $form->parse_html_template("wh/report_filter",
db791046 Bernd Bleßmann
{ "WAREHOUSES" => $form->{WAREHOUSES},
"PARTSGROUPS" => $form->{PARTSGROUPS},
"UNITS" => AM->unit_select_data(AM->retrieve_units(\%myconfig, $form)),
0908bb6e Jan Büren
});
83914eeb Moritz Bunkus
13dcc338 Sven Schöling
$main::lxdebug->leave_sub();
83914eeb Moritz Bunkus
}

sub generate_report {
13dcc338 Sven Schöling
$main::lxdebug->enter_sub();

$main::auth->assert('warehouse_contents | warehouse_management');
83914eeb Moritz Bunkus
13dcc338 Sven Schöling
my $form = $main::form;
my %myconfig = %main::myconfig;
my $locale = $main::locale;
83914eeb Moritz Bunkus
1de53cbf Sven Donath
$form->{title} = $locale->text("Report about warehouse contents");
83914eeb Moritz Bunkus
$form->{sort} ||= 'partnumber';
my $sort_col = $form->{sort};

my %filter;
a3153d12 Bernd Bleßmann
my @columns = qw(warehouse bin partnumber type_and_classific partdescription chargenumber bestbefore comment qty partunit list_price purchase_price stock_value);
83914eeb Moritz Bunkus
# filter stuff
db791046 Bernd Bleßmann
map { $filter{$_} = $form->{$_} if ($form->{$_}) } qw(warehouse_id bin_id classification_id partnumber description partsgroup_id chargenumber bestbefore date include_invalid_warehouses);
83914eeb Moritz Bunkus
bea5a6c5 Jan Büren
# show filter stuff also in report
my @options;
65d2537d Martin Helmling
my $currentdate = $form->current_date(\%myconfig);
push @options, $locale->text('Printdate') . " : ".$locale->date(\%myconfig, $currentdate, 1);

bea5a6c5 Jan Büren
# dispatch all options
my $dispatch_options = {
71d8ea65 Jan Büren
warehouse_id => sub { push @options, $locale->text('Warehouse') . " : " .
SL::DB::Manager::Warehouse->find_by(id => $form->{warehouse_id})->description},
bin_id => sub { push @options, $locale->text('Bin') . " : " .
SL::DB::Manager::Bin->find_by(id => $form->{bin_id})->description},
partnumber => sub { push @options, $locale->text('Partnumber') . " : $form->{partnumber}"},
65d2537d Martin Helmling
classification_id => sub { push @options, $locale->text('Parts Classification'). " : ".
SL::DB::Manager::PartClassification->get_first(where => [ id => $form->{classification_id} ] )->description; },
71d8ea65 Jan Büren
description => sub { push @options, $locale->text('Description') . " : $form->{description}"},
db791046 Bernd Bleßmann
partsgroup_id => sub { push @options, $locale->text('Partsgroup') . " : " .
SL::DB::PartsGroup->new(id => $form->{partsgroup_id})->load->partsgroup},
71d8ea65 Jan Büren
chargenumber => sub { push @options, $locale->text('Charge Number') . " : $form->{chargenumber}"},
bestbefore => sub { push @options, $locale->text('Best Before') . " : $form->{bestbefore}"},
bea5a6c5 Jan Büren
include_invalid_warehouses => sub { push @options, $locale->text('Include invalid warehouses ')},
};
foreach (keys %filter) {
71d8ea65 Jan Büren
$dispatch_options->{$_}->() if $dispatch_options->{$_};
bea5a6c5 Jan Büren
}
65d2537d Martin Helmling
push @options, $locale->text('Stock Qty for Date') . " " . $locale->date(\%myconfig, $form->{date}?$form->{date}:$currentdate, 1);

bea5a6c5 Jan Büren
# / end show filter stuff also in report

83914eeb Moritz Bunkus
$filter{qty_op} = WH->convert_qty_op($form->{qty_op});
if ($filter{qty_op}) {
13dcc338 Sven Schöling
$form->isblank("qty", $locale->text('Quantity missing.'));
$form->isblank("qty_unit", $locale->text('Unit missing.'));
83914eeb Moritz Bunkus
$filter{qty} = $form->{qty};
$filter{qty_unit} = $form->{qty_unit};
}
# /filter stuff

3c938e03 Martin Helmling
$form->{report_generator_output_format} = 'HTML' if !$form->{report_generator_output_format};

# manual paginating
cae732b8 Jan Büren
my $allrows = $form->{report_generator_output_format} eq 'HTML' ? $form->{allrows} : 1;
6aaed579 Martin Helmling
my $page = $::form->{page} || 1;
my $pages = {};
$pages->{per_page} = $::form->{per_page} || 20;
my $first_nr = ($page - 1) * $pages->{per_page};
my $last_nr = $first_nr + $pages->{per_page};
3c938e03 Martin Helmling
# no optimisation if qty op
if ( !$allrows && $form->{maxrows} && !$filter{qty_op}) {
$filter{limit} = $pages->{per_page};
$filter{offset} = ($page - 1) * $pages->{per_page};
6aaed579 Martin Helmling
$first_nr = 0;
$last_nr = $pages->{per_page};
3c938e03 Martin Helmling
}

my @contents = WH->get_warehouse_report(%filter);

# get maxcount
if (!$form->{maxrows}) {
$form->{maxrows} = scalar @contents ;
}

83914eeb Moritz Bunkus
$form->{subtotal} = '' if (!first { $_ eq $sort_col } qw(partnumber partdescription));

my $report = SL::ReportGenerator->new(\%myconfig, $form);

5ee61c6f Bernd Bleßmann
my $cvar_configs = CVar->get_configs('module' => 'IC');
96aa399e Jan Büren
my @includeable_custom_variables = grep { $_->{includeable} } @{ $cvar_configs };
5d2ede53 Bernd Bleßmann
my @searchable_custom_variables = grep { $_->{searchable} } @{ $cvar_configs };
96aa399e Jan Büren
push @columns, map { "cvar_$_->{name}" } @includeable_custom_variables;

83914eeb Moritz Bunkus
my @hidden_variables = map { "l_${_}" } @columns;
a3153d12 Bernd Bleßmann
push @hidden_variables, qw(warehouse_id bin_id partnumber partstypes_id description partsgroup_id chargenumber bestbefore qty_op qty qty_unit partunit l_warehouse l_bin);
3cdcdc91 Jan Büren
push @hidden_variables, qw(include_empty_bins subtotal include_invalid_warehouses date);
989d69a8 Jan Büren
push @hidden_variables, qw(classification_id stock_value_basis allrows);
5d2ede53 Bernd Bleßmann
push @hidden_variables, map({'cvar_'. $_->{name}} @searchable_custom_variables);
push @hidden_variables, map({'cvar_'. $_->{name} .'_from'} grep({$_->{type} eq 'date'} @searchable_custom_variables));
push @hidden_variables, map({'cvar_'. $_->{name} .'_to'} grep({$_->{type} eq 'date'} @searchable_custom_variables));
push @hidden_variables, map({'cvar_'. $_->{name} .'_qtyop'} grep({$_->{type} eq 'number'} @searchable_custom_variables));
83914eeb Moritz Bunkus
my %column_defs = (
a3153d12 Bernd Bleßmann
'warehouse' => { 'text' => $locale->text('Warehouse'), },
'bin' => { 'text' => $locale->text('Bin'), },
83914eeb Moritz Bunkus
'partnumber' => { 'text' => $locale->text('Part Number'), },
65d2537d Martin Helmling
'type_and_classific' => { 'text' => $locale->text('Type'), },
4b937d1a Sven Donath
'partdescription' => { 'text' => $locale->text('Part Description'), },
83914eeb Moritz Bunkus
'chargenumber' => { 'text' => $locale->text('Charge Number'), },
096f9e3e Bernd Bleßmann
'bestbefore' => { 'text' => $locale->text('Best Before'), },
83914eeb Moritz Bunkus
'qty' => { 'text' => $locale->text('Qty'), },
34035b33 Martin Helmling
'partunit' => { 'text' => $locale->text('Unit'), },
82498fb7 Moritz Bunkus
'stock_value' => { 'text' => $locale->text('Stock value'), },
848add30 Moritz Bunkus
'purchase_price' => { 'text' => $locale->text('Purchase price'), },
7464fcbd Bernd Bleßmann
'list_price' => { 'text' => $locale->text('List Price'), },
83914eeb Moritz Bunkus
);

748a66d0 Bernd Bleßmann
my %column_defs_cvars = map { +"cvar_$_->{name}" => { 'text' => $_->{description} } } @includeable_custom_variables;
%column_defs = (%column_defs, %column_defs_cvars);

83914eeb Moritz Bunkus
my $href = build_std_url('action=generate_report', grep { $form->{$_} } @hidden_variables);
3c938e03 Martin Helmling
$href .= "&maxrows=".$form->{maxrows};

9cb34cff Bernd Bleßmann
map { $column_defs{$_}->{link} = $href . "&page=".$page."&sort=${_}&order=" . Q($_ eq $sort_col ? 1 - $form->{order} : $form->{order}) } grep {!/^cvar_/} @columns;
83914eeb Moritz Bunkus
7464fcbd Bernd Bleßmann
my %column_alignment = map { $_ => 'right' } qw(qty list_price purchase_price stock_value);
83914eeb Moritz Bunkus
map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;

3c938e03 Martin Helmling
$column_defs{partunit}->{visible} = 1;
65d2537d Martin Helmling
$column_defs{type_and_classific}->{visible} = 1;
$column_defs{type_and_classific}->{link} ='';

83914eeb Moritz Bunkus
$report->set_columns(%column_defs);
$report->set_column_order(@columns);

19688fca Moritz Bunkus
$report->set_export_options('generate_report', @hidden_variables, qw(sort order));
83914eeb Moritz Bunkus
$report->set_sort_indicator($sort_col, $form->{order});

bea5a6c5 Jan Büren
$report->set_options('top_info_text' => join("\n", @options),
'output_format' => 'HTML',
83914eeb Moritz Bunkus
'title' => $form->{title},
19688fca Moritz Bunkus
'attachment_basename' => strftime($locale->text('warehouse_report_list') . '_%Y%m%d', localtime time));
83914eeb Moritz Bunkus
$report->set_options_from_form();
a873249c Moritz Bunkus
$locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
0908bb6e Jan Büren
CVar->add_custom_variables_to_report('module' => 'IC',
'trans_id_field' => 'parts_id',
'configs' => $cvar_configs,
'column_defs' => \%column_defs,
'data' => \@contents);
83914eeb Moritz Bunkus
my $all_units = AM->retrieve_units(\%myconfig, $form);
my $idx = 0;

82498fb7 Moritz Bunkus
my @subtotals_columns = qw(qty stock_value);
my %subtotals = map { $_ => 0 } @subtotals_columns;

my $total_stock_value = 0;

13dcc338 Sven Schöling
foreach my $entry (@contents) {
65d2537d Martin Helmling
a97574b8 Sven Schöling
$entry->{type_and_classific} = SL::Presenter::Part::type_abbreviation($entry->{part_type}).
SL::Presenter::Part::classification_abbreviation($entry->{classification_id});
82498fb7 Moritz Bunkus
map { $subtotals{$_} += $entry->{$_} } @subtotals_columns;
$total_stock_value += $entry->{stock_value} * 1;
34035b33 Martin Helmling
$entry->{qty} = $form->format_amount(\%myconfig, $entry->{qty});
82498fb7 Moritz Bunkus
$entry->{stock_value} = $form->format_amount(\%myconfig, $entry->{stock_value} * 1, 2);
3c938e03 Martin Helmling
$entry->{purchase_price} = $form->format_amount(\%myconfig, $entry->{purchase_price} * 1, 2);
7464fcbd Bernd Bleßmann
$entry->{list_price} = $form->format_amount(\%myconfig, $entry->{list_price} * 1, 2);
83914eeb Moritz Bunkus
13dcc338 Sven Schöling
my $row_set = [ { map { $_ => { 'data' => $entry->{$_}, 'align' => $column_alignment{$_} } } @columns } ];
83914eeb Moritz Bunkus
0185267e Thomas Heck
if ( ($form->{subtotal} eq 'Y' && !$form->{report_generator_csv_options_for_import} )
83914eeb Moritz Bunkus
&& (($idx == (scalar @contents - 1))
|| ($entry->{$sort_col} ne $contents[$idx + 1]->{$sort_col}))) {

my $row = { map { $_ => { 'data' => '', 'class' => 'listsubtotal', 'align' => $column_alignment{$_}, } } @columns };
34035b33 Martin Helmling
$row->{qty}->{data} = $form->format_amount(\%myconfig, $subtotals{qty});
82498fb7 Moritz Bunkus
$row->{stock_value}->{data} = $form->format_amount(\%myconfig, $subtotals{stock_value} * 1, 2);
3c938e03 Martin Helmling
$row->{purchase_price}->{data} = $form->format_amount(\%myconfig, $subtotals{purchase_price} * 1, 2);
7464fcbd Bernd Bleßmann
$row->{list_price}->{data} = $form->format_amount(\%myconfig, $subtotals{list_price} * 1, 2);
82498fb7 Moritz Bunkus
%subtotals = map { $_ => 0 } @subtotals_columns;
83914eeb Moritz Bunkus
push @{ $row_set }, $row;
}

34035b33 Martin Helmling
if ( $allrows || ($idx >= $first_nr && $idx < $last_nr )) {
3c938e03 Martin Helmling
$report->add_data($row_set);
34035b33 Martin Helmling
}
83914eeb Moritz Bunkus
$idx++;
}

0185267e Thomas Heck
if ( $column_defs{stock_value}->{visible} && !$form->{report_generator_csv_options_for_import} ) {
82498fb7 Moritz Bunkus
$report->add_separator();

my $row = { map { $_ => { 'data' => '', 'class' => 'listsubtotal', } } @columns };

my $left_col = first { $column_defs{$_}->{visible} } @columns;

$row->{$left_col}->{data} = $locale->text('Total stock value');
$row->{stock_value}->{data} = $form->format_amount(\%myconfig, $total_stock_value, 2);
$row->{stock_value}->{align} = 'right';

$report->add_data($row);
}
3c938e03 Martin Helmling
if ( ! $allrows ) {
$pages->{max} = SL::DB::Helper::Paginated::ceil($form->{maxrows}, $pages->{per_page}) || 1;
$pages->{page} = $page < 1 ? 1: $page > $pages->{max} ? $pages->{max}: $page;
$pages->{common} = [ grep { $_->{visible} } @{ SL::DB::Helper::Paginated::make_common_pages($pages->{page}, $pages->{max}) } ];

$report->set_options('raw_bottom_info_text' => $form->parse_html_template('common/paginate',
{'pages' => $pages , 'base_url' => $href}) );
}
82498fb7 Moritz Bunkus
e7913c4c Moritz Bunkus
$report->generate_with_headers();
83914eeb Moritz Bunkus
13dcc338 Sven Schöling
$main::lxdebug->leave_sub();
83914eeb Moritz Bunkus
}

# --------------------------------------------------------------------
# Utility functions
# --------------------------------------------------------------------

sub show_no_warehouses_error {
13dcc338 Sven Schöling
$main::lxdebug->enter_sub();

my $form = $main::form;
my %myconfig = %main::myconfig;
my $locale = $main::locale;
83914eeb Moritz Bunkus
e1bfe807 Geoffrey Richardson
my $msg = $locale->text('No warehouse has been created yet or the quantity of the bins is not configured yet.') . ' ';
83914eeb Moritz Bunkus
4bd1e2f8 Sven Schöling
if ($main::auth->check_right($::myconfig{login}, 'config')) {
83914eeb Moritz Bunkus
$msg .= $locale->text('You can create warehouses and bins via the menu "System -> Warehouses".');
} else {
$msg .= $locale->text('Please ask your administrator to create warehouses and bins.');
}

$form->show_generic_error($msg);

13dcc338 Sven Schöling
$main::lxdebug->leave_sub();
83914eeb Moritz Bunkus
}

sub get_warehouse_idx {
my ($warehouse_id) = @_;

13dcc338 Sven Schöling
my $form = $main::form;

83914eeb Moritz Bunkus
for (my $i = 0; $i < scalar @{$form->{WAREHOUSES}}; $i++) {
return $i if ($form->{WAREHOUSES}->[$i]->{id} == $warehouse_id);
}

return -1;
}

sub get_bin_idx {
my ($warehouse_index, $bin_id) = @_;

13dcc338 Sven Schöling
my $form = $main::form;

83914eeb Moritz Bunkus
my $warehouse = $form->{WAREHOUSES}->[$warehouse_index];

return -1 if (!$warehouse);

for (my $i = 0; $i < scalar @{ $warehouse->{BINS} }; $i++) {
return $i if ($warehouse->{BINS}->[$i]->{id} == $bin_id);
}

return -1;
}

fa73b837 Sven Schöling
sub new_item {
$main::lxdebug->enter_sub();
my %params = @_;

13dcc338 Sven Schöling
my $form = $main::form;

fa73b837 Sven Schöling
# change callback
$form->{old_callback} = $form->escape($form->{callback}, 1);
$form->{callback} = $form->escape("$form->{script}?action=$params{action}", 1);

# save all form variables except action in a previousform variable
my $previousform = join '&', map { my $value = $form->{$_}; $value =~ s/&/%26/; "$_=$value" } grep { !/action/ } keys %$form;
13dcc338 Sven Schöling
my @HIDDENS = ();
fa73b837 Sven Schöling
# push @HIDDENS, { 'name' => 'previousform', 'value' => $form->escape($previousform, 1) };
push @HIDDENS, map +{ 'name' => $_, 'value' => $form->{$_} }, qw(partnumber description unit vc sellprice ean);
push @HIDDENS, { 'name' => 'taxaccount2', 'value' => $form->{taxaccounts} };
push @HIDDENS, { 'name' => 'notes', 'value' => $form->{longdescription} };

$form->header();
print $form->parse_html_template("generic/new_item", { HIDDENS => [ sort { $a->{name} cmp $b->{name} } @HIDDENS ] } );

$main::lxdebug->leave_sub();
}

83914eeb Moritz Bunkus
sub update {
13dcc338 Sven Schöling
my $form = $main::form;
83914eeb Moritz Bunkus
call_sub($form->{update_nextsub} || $form->{nextsub});
}

sub continue {
13dcc338 Sven Schöling
my $form = $main::form;
83914eeb Moritz Bunkus
call_sub($form->{continue_nextsub} || $form->{nextsub});
}

sub stock {
13dcc338 Sven Schöling
my $form = $main::form;
83914eeb Moritz Bunkus
call_sub($form->{stock_nextsub} || $form->{nextsub});
}

3c005fa1 Moritz Bunkus
sub setup_wh_transfer_warehouse_selection_action_bar {
my ($action) = @_;

for my $bar ($::request->layout->get('actionbar')) {
$bar->add(
action => [
t8('Update'),
submit => [ '#form', { action => $action } ],
accesskey => 'enter',
],
);
}
}

sub setup_wh_transfer_warehouse_selection_assembly_action_bar {
my ($action) = @_;

for my $bar ($::request->layout->get('actionbar')) {
$bar->add(
action => [
t8('Update'),
submit => [ '#form', { action => 'transfer_assembly_update_part' } ],
accesskey => 'enter',
],
action => [
t8('Produce'),
submit => [ '#form', { action => 'create_assembly' } ],
disabled => $::form->{parts_id} ? undef : $::locale->text('No assembly has been selected yet.'),
],
);
}
}

sub setup_wh_transfer_parts_action_bar {
my ($action) = @_;

for my $bar ($::request->layout->get('actionbar')) {
$bar->add(
action => [
t8('Transfer'),
submit => [ '#form', { action => 'transfer_parts' } ],
accesskey => 'enter',
],
action => [
t8('Back'),
call => [ 'kivi.history_back' ],
],
);
}
}

sub setup_wh_removal_parts_selection_action_bar {
my ($action) = @_;

for my $bar ($::request->layout->get('actionbar')) {
$bar->add(
action => [
t8('Transfer out'),
submit => [ '#form', { action => 'remove_parts' } ],
accesskey => 'enter',
],
action => [
t8('Back'),
call => [ 'kivi.history_back' ],
],
);
}
}

sub setup_wh_report_action_bar {
my ($action) = @_;

for my $bar ($::request->layout->get('actionbar')) {
$bar->add(
action => [
t8('Show'),
submit => [ '#form', { action => 'generate_report' } ],
accesskey => 'enter',
],
);
}
}

sub setup_wh_journal_action_bar {
my ($action) = @_;

for my $bar ($::request->layout->get('actionbar')) {
$bar->add(
action => [
t8('Show'),
submit => [ '#form', { action => 'generate_journal' } ],
accesskey => 'enter',
],
);
}
}
79b75e07 Jan Büren
sub setup_wh_journal_list_all_action_bar {
my ($action) = @_;
for my $bar ($::request->layout->get('actionbar')) {
$bar->add(
combobox => [
action => [ t8('Actions') ],
action => [
t8('Disassemble Assembly'),
submit => [ '#form', { action => 'disassemble_assembly' } ],
checks => [ [ 'kivi.check_if_entries_selected', '[name="ids[]"]' ] ],
],
],
);
}
}

3c005fa1 Moritz Bunkus
83914eeb Moritz Bunkus
1;
66022cbd Sven Schöling
__END__

=head1 NAME

bin/mozilla/wh.pl - Warehouse frontend.

=head1 FUNCTIONS

=over 4

=item new_item

call new item dialogue from warehouse masks.

PARAMS:
action => name of sub to be called when new item is done

=back

=cut