Inventory Helper: Allocation ausgelagert in eigene Datei
[kivitendo-erp.git] / SL / Helper / Inventory / Allocation.pm
1 package SL::Helper::Inventory::Allocation;
2
3 use strict;
4
5 my @attributes = qw(parts_id qty bin_id warehouse_id chargenumber bestbefore comment for_object_id);
6 my %attributes = map { $_ => 1 } @attributes;
7 my %mapped_attributes = (
8   for_object_id => 'oe_id',
9 );
10
11 for my $name (@attributes) {
12   no strict 'refs';
13   *{"SL::Helper::Inventory::Allocation::$name"} = sub { $_[0]{$name} };
14 }
15
16 sub new {
17   my ($class, %params) = @_;
18
19   Carp::croak("missing attribute $_") for grep { !exists $params{$_}     } @attributes;
20   Carp::croak("unknown attribute $_") for grep { !exists $attributes{$_} } keys %params;
21   Carp::croak("$_ must be set")       for grep { !$params{$_} } qw(parts_id qty bin_id);
22   Carp::croak("$_ must be positive")  for grep { !($params{$_} > 0) } qw(parts_id qty bin_id);
23
24   bless { %params }, $class;
25 }
26
27 sub transfer_object {
28   my ($self, %params) = @_;
29
30   SL::DB::Inventory->new(
31     (map {
32       my $attr = $mapped_attributes{$_} // $_;
33       $attr => $self->{$attr}
34     } @attributes),
35     %params,
36   );
37 }
38
39 1;
40
41 =encoding utf-8
42
43 =head1 NAME
44
45 SL::Helper::Inventory::Allocation - Inventory API allocation data structure
46
47 =head1 SYNOPSIS
48
49   # all of these need to be present
50   my $allocation = SL::Helper::Inventory::Allocation->new(
51     part_id           => $part->id,
52     qty               => 15,
53     bin_id            => $bin_obj->id,
54     warehouse_id      => $bin_obj->warehouse_id,
55     chargenumber      => '1823772365',           # can be undef
56     bestbefore        => undef,                  # can be undef
57     for_object_id     => $order->id,             # can be undef
58   );
59
60
61 =head1 SEE ALSO
62
63 The full documentation can be found in L<SL::Helper::Inventory>
64
65 =cut