]> wagnertech.de Git - mfinanz.git/blob - SL/BackgroundJob/InventoryClearAll.pm
date error in mapping
[mfinanz.git] / SL / BackgroundJob / InventoryClearAll.pm
1 package SL::BackgroundJob::InventoryClearAll;
2
3 use strict;
4
5 use parent qw(SL::BackgroundJob::Base);
6
7 use SL::DB::Inventory;
8 use SL::Helper::Inventory qw(:ALL);
9 use SL::Locale::String qw(t8);
10
11
12 sub run {
13   my ($self, $job_obj) = @_;
14   my $data = $job_obj->data_as_hash;
15
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');
23   my $warehouse_id;
24
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;
28   }
29
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;
34
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;
40
41   my @trans;
42   foreach (@stock) {
43     my $qty = $_->{qty} * -1;
44
45     push @trans, "$_->{part}->{id} $qty $_->{part}->{unit}";
46
47     next if $dry_run;
48     my $x = SL::DB::Inventory->new();
49
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});
56     $x->qty         ($qty);
57     $x->shippingdate($date);
58     $x->trans_type  (($qty > 0) ? $trans_type_in : $trans_type_out);
59     $x->warehouse_id($_->{warehouse_id});
60
61     $x->save();
62   }
63
64   return $dry_run
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));
69 }
70
71 1;
72
73
74 __END__
75
76 =encoding utf8
77
78 =head1 NAME
79
80 SL::BackgroundJob::InventoryClearAll —
81 Background job which performs all inventory transactions needed to empty all
82 warehoue bins. Useful before stocktaking
83
84 =head1 SYNOPSIS
85
86 =head1 AUTHOR
87
88 Niklas Schmidt
89
90
91 =cut