1 package SL::BackgroundJob::InventoryClearAll;
5 use parent qw(SL::BackgroundJob::Base);
8 use SL::Helper::Inventory qw(:ALL);
9 use SL::Locale::String qw(t8);
13 my ($self, $job_obj) = @_;
14 my $data = $job_obj->data_as_hash;
16 my $comment = $data->{comment} // 'vor Inventur';
17 my $date = DateTime->today_local();
18 my $date_str = $date->format_cldr('yyyy-MM-dd');
19 my $dry_run = ($data->{dry_run}) ? 1 : 0;
20 my $employee_id = SL::DB::Manager::Employee->current->id;
21 my $trans_type_in = SL::DB::Manager::TransferType->find_by(description => 'correction', direction => 'in');
22 my $trans_type_out = SL::DB::Manager::TransferType->find_by(description => 'correction', direction => 'out');
25 if (exists $data->{warehouse}) {
26 $warehouse_id = SL::DB::Manager::Warehouse->find_by(description => $data->{warehouse});
27 die "Lager existiert nicht: $data->{warehouse}" if !defined $warehouse_id;
30 die "No parameter correction_date given"
31 if !exists $data->{correction_date};
32 die "Parameter correction_date $data->{correction_date} is not today $date_str"
33 if $data->{correction_date} ne $date_str;
35 my $stock_all = get_stock(warehouse => $warehouse_id,
36 by => [ qw(bin chargenumber part) ],
37 with_objects => [ qw(part) ]);
38 my @stock = grep { $_->{qty} != 0 } @{ $stock_all };
39 my $ntransactions = scalar @stock;
43 my $qty = $_->{qty} * -1;
45 push @trans, "$_->{part}->{id} $qty $_->{part}->{unit}";
48 my $x = SL::DB::Inventory->new();
50 $x->bestbefore ($_->{bestbefore});
51 $x->bin_id ($_->{bin_id});
52 $x->chargenumber($_->{chargenumber});
53 $x->comment ($comment);
54 $x->employee_id ($employee_id);
55 $x->parts_id ($_->{parts_id});
57 $x->shippingdate($date);
58 $x->trans_type (($qty > 0) ? $trans_type_in : $trans_type_out);
59 $x->warehouse_id($_->{warehouse_id});
65 ? t8('Inventory: #1 transactions not yet executed to clear all inventory slots. Parts: #2',
66 $ntransactions, join(',', @trans))
67 : t8('Inventory: #1 transactions executed to clear all inventory slots. Parts: #2',
68 $ntransactions, join(',', @trans));
80 SL::BackgroundJob::InventoryClearAll —
81 Background job which performs all inventory transactions needed to empty all
82 warehoue bins. Useful before stocktaking