X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FController%2FCsvImport%2FInventory.pm;h=238abf33add0cca6565f087e86d7ab48f1f3d957;hb=1cbc459da604c31d21593df298a522e6cbe69e2b;hp=593185eb2beadd24f852ba76de9566e05f8bc51b;hpb=11c945f26a44f4cb5c36ee1f013e7583562e7651;p=kivitendo-erp.git diff --git a/SL/Controller/CsvImport/Inventory.pm b/SL/Controller/CsvImport/Inventory.pm index 593185eb2..238abf33a 100644 --- a/SL/Controller/CsvImport/Inventory.pm +++ b/SL/Controller/CsvImport/Inventory.pm @@ -28,11 +28,15 @@ sub init_class { $self->class('SL::DB::Inventory'); } +sub set_profile_defaults { +}; + sub init_profile { my ($self) = @_; my $profile = $self->SUPER::init_profile; - delete @{$profile}{qw(trans_id oe_id orderitems_id bestbefore trans_type_id project_id)}; + delete @{$profile}{qw(trans_id oe_id delivery_order_items_stock_id trans_type_id project_id)}; + delete @{$profile}{qw(bestbefore)} if !$::instance_conf->get_show_bestbefore; return $profile; } @@ -64,8 +68,8 @@ sub init_bins_by { my $all_bins = SL::DB::Manager::Bin->get_all(); my $bins_by; - $bins_by->{'wh_id+id'} = { map { ( $_->warehouse_id . '+' . $_->id => $_ ) } @{ $all_bins } }; - $bins_by->{'wh_id+description'} = { map { ( $_->warehouse_id . '+' . $_->description => $_ ) } @{ $all_bins } }; + $bins_by->{_wh_id_and_id_ident()} = { map { ( _wh_id_and_id_maker($_->warehouse_id, $_->id) => $_ ) } @{ $all_bins } }; + $bins_by->{_wh_id_and_description_ident()} = { map { ( _wh_id_and_description_maker($_->warehouse_id, $_->description) => $_ ) } @{ $all_bins } }; return $bins_by; } @@ -113,6 +117,9 @@ sub setup_displayable_columns { { name => 'warehouse', description => $::locale->text('Warehouse') }, { name => 'warehouse_id', description => $::locale->text('Warehouse (database ID)') }, ); + if ($::instance_conf->get_show_bestbefore) { + $self->add_displayable_columns({ name => 'bestbefore', description => $::locale->text('Best Before') }); + } } sub check_warehouse { @@ -145,7 +152,7 @@ sub check_warehouse { $object->warehouse_id($wh->id); } - # Check wether or not warehouse ID is valid. + # Check whether or not warehouse ID is valid. if ($object->warehouse_id && !$self->warehouses_by->{id}->{ $object->warehouse_id }) { push @{ $entry->{errors} }, $::locale->text('Error: Invalid warehouse'); return 0; @@ -172,7 +179,7 @@ sub check_warehouse { return 1; } -# Check bin fior given warehouse, so check_warehouse must be called first. +# Check bin for given warehouse, so check_warehouse must be called first. sub check_bin { my ($self, $entry) = @_; @@ -181,7 +188,7 @@ sub check_bin { # If bin from front-end is enforced for all transfers, use this, if valid. if ($self->settings->{apply_bin} eq 'all') { $object->bin_id(undef); - my $bin = $self->bins_by->{'wh_id+description'}->{ $object->warehouse_id . '+' . $self->settings->{bin} }; + my $bin = $self->bins_by->{_wh_id_and_description_ident()}->{ _wh_id_and_description_maker($object->warehouse_id, $self->settings->{bin}) }; if (!$bin) { push @{ $entry->{errors} }, $::locale->text('Error: Invalid bin'); return 0; @@ -194,7 +201,7 @@ sub check_bin { if ( $self->settings->{apply_bin} eq 'missing' && ! $object->bin_id && ! $entry->{raw_data}->{bin} ) { - my $bin = $self->bins_by->{'wh_id+description'}->{ $object->warehouse_id . '+' . $self->settings->{bin} }; + my $bin = $self->bins_by->{_wh_id_and_description_ident()}->{ _wh_id_and_description_maker($object->warehouse_id, $self->settings->{bin}) }; if (!$bin) { push @{ $entry->{errors} }, $::locale->text('Error: Invalid bin'); return 0; @@ -203,15 +210,15 @@ sub check_bin { $object->bin_id($bin->id); } - # Check wether or not bin ID is valid. - if ($object->bin_id && !$self->bins_by->{'wh_id+id'}->{ $object->warehouse_id . '+' . $object->bin_id }) { + # Check whether or not bin ID is valid. + if ($object->bin_id && !$self->bins_by->{_wh_id_and_id_ident()}->{ _wh_id_and_id_maker($object->warehouse_id, $object->bin_id) }) { push @{ $entry->{errors} }, $::locale->text('Error: Invalid bin'); return 0; } - + # Map description to ID if given. if (!$object->bin_id && $entry->{raw_data}->{bin}) { - my $bin = $self->bins_by->{'wh_id+description'}->{ $object->warehouse_id . '+' . $entry->{raw_data}->{bin} }; + my $bin = $self->bins_by->{_wh_id_and_description_ident()}->{ _wh_id_and_description_maker($object->warehouse_id, $entry->{raw_data}->{bin}) }; if (!$bin) { push @{ $entry->{errors} }, $::locale->text('Error: Invalid bin'); return 0; @@ -221,7 +228,7 @@ sub check_bin { } if ($object->bin_id) { - $entry->{info_data}->{bin} = $self->bins_by->{'wh_id+id'}->{ $object->warehouse_id . '+' . $object->bin_id }->description; + $entry->{info_data}->{bin} = $self->bins_by->{_wh_id_and_id_ident()}->{ _wh_id_and_id_maker($object->warehouse_id, $object->bin_id) }->description; } else { push @{ $entry->{errors} }, $::locale->text('Error: Bin not found'); return 0; @@ -235,7 +242,7 @@ sub check_part { my $object = $entry->{object}; - # Check wether or non part ID is valid. + # Check whether or not part ID is valid. if ($object->parts_id && !$self->parts_by->{id}->{ $object->parts_id }) { push @{ $entry->{errors} }, $::locale->text('Error: Invalid part'); return 0; @@ -286,16 +293,13 @@ sub check_qty{ } # Actual quantity is read from stock or is the result of transfers for the - # same part, warehouse and bin done before. - my $key = $object->parts_id . '+' . $object->warehouse_id . '+' . $object->bin_id; + # same part, warehouse, bin, chargenumber and bestbefore date (if + # show_bestbefore is enabled) done before. + my $key = join '+', $object->parts_id, $object->warehouse_id, $object->bin_id, $object->chargenumber; + $key .= join '+', $key, $object->bestbefore if $::instance_conf->get_show_bestbefore; + if (!exists $self->{resulting_quantities}->{$key}) { - my $stock = $object->part->get_simple_stock; - my @stocked = grep { $_->{warehouse_id} == $object->warehouse_id && $_->{bin_id} == $object->bin_id } @$stock; - my $stocked_qty = 0; - foreach (@stocked) { - $stocked_qty += $stocked[0]->{sum} * 1; - } - $self->{resulting_quantities}->{$key} = $stocked_qty; + $self->{resulting_quantities}->{$key} = _get_stocked_qty($object); } my $actual_qty = $self->{resulting_quantities}->{$key}; @@ -391,11 +395,54 @@ sub save_objects { my $data = $params{data} || $self->controller->data; foreach my $entry (@{ $data }) { - my ($trans_id) = selectrow_query($::form, $::form->get_standard_dbh, qq|SELECT nextval('id')|); + my ($trans_id) = selectrow_query($::form,$entry->{object}->db->dbh, qq|SELECT nextval('id')|); $entry->{object}->trans_id($trans_id); } $self->SUPER::save_objects(%params); } +sub _get_stocked_qty { + my ($object) = @_; + + my $bestbefore_filter = ''; + my $bestbefore_val_cnt = 0; + if ($::instance_conf->get_show_bestbefore) { + $bestbefore_filter = ($object->bestbefore) ? 'AND bestbefore = ?' : 'AND bestbefore IS NULL'; + $bestbefore_val_cnt = ($object->bestbefore) ? 1 : 0; + } + + my $query = <parts_id, + $object->warehouse_id, + $object->bin_id, + $object->chargenumber); + push @values, $object->bestbefore if $bestbefore_val_cnt; + + my ($stocked_qty) = selectrow_query($::form, $object->db->dbh, $query, @values); + + return $stocked_qty; +} + +sub _wh_id_and_description_ident { + return 'wh_id+description'; +} + +sub _wh_id_and_description_maker { + return join '+', $_[0], $_[1] +} + +sub _wh_id_and_id_ident { + return 'wh_id+id'; +} + +sub _wh_id_and_id_maker { + return join '+', $_[0], $_[1] +} + 1;