72b8d7661f8c7192fbb470bf6eae3f10ea9c00f3
[kivitendo-erp.git] / SL / Dev / Inventory.pm
1 package SL::Dev::Inventory;
2
3 use strict;
4 use base qw(Exporter);
5 our @EXPORT = qw(create_warehouse_and_bins set_stock);
6
7 use SL::DB::Warehouse;
8 use SL::DB::Bin;
9 use SL::DB::Inventory;
10 use SL::DB::TransferType;
11 use SL::DB::Employee;
12
13 sub create_warehouse_and_bins {
14   my (%params) = @_;
15
16   my $number_of_bins = $params{number_of_bins} || 5;
17   my $wh = SL::DB::Warehouse->new(description => $params{warehouse_description} || "Warehouse", invalid => 0);
18   for my $i ( 1 .. $number_of_bins ) {
19     $wh->add_bins( SL::DB::Bin->new(description => ( $params{bin_description} || "Bin" ) . " $i" ) );
20   }
21   $wh->save;
22   return ($wh, $wh->bins->[0]);
23 }
24
25 sub set_stock {
26   my ($part, %params) = @_;
27
28   die "first argument is not a part" unless ref($part) eq 'SL::DB::Part';
29
30   die "no default warehouse" unless $part->warehouse_id or $part->bin_id;
31
32   die "Can't determine employee" unless SL::DB::Manager::Employee->current;
33
34   die "qty is missing or not positive" unless $params{qty} and $params{qty} > 0;
35
36   my $transfer_type_description = delete $params{transfer_type} || 'stock';
37   my $transfer_type = SL::DB::Manager::TransferType->find_by( description => $transfer_type_description, direction => 'in' );
38
39   my $shippingdate;
40   if ( $params{shippingdate} ) {
41     $shippingdate = $::locale->parse_date_to_object(delete $params{shippingdate});
42   } else {
43     $shippingdate = DateTime->today;
44   };
45
46   my ($trans_id) = $part->db->dbh->selectrow_array("select nextval('id')", {});
47
48   SL::DB::Inventory->new(
49     parts_id         => $part->id,
50     bin_id           => $part->bin_id,
51     warehouse_id     => $part->warehouse_id,
52     employee_id      => $params{employee_id} || SL::DB::Manager::Employee->current->id,
53     trans_type_id    => $transfer_type->id,
54     comment          => $params{comment},
55     shippingdate     => $shippingdate,
56     qty              => $params{qty},
57     trans_id         => $trans_id,
58   )->save;
59 }
60
61 1;
62
63 __END__
64
65 =head1 NAME
66
67 SL::Dev::Inventory - create inventory-related objects for testing, with minimal
68 defaults
69
70 =head1 FUNCTIONS
71
72 =head2 C<create_warehouse_and_bins %PARAMS>
73
74 Creates a new warehouse and bins, and immediately saves them. Returns the
75 warehouse and the first bin object.
76   my ($wh, $bin) = SL::Dev::Inventory::create_warehouse_and_bins();
77
78 Create named warehouse with 10 bins:
79   my ($wh, $bin) = SL::Dev::Inventory::create_warehouse_and_bins(warehouse_description => 'Testlager',
80                                                                  bin_description       => 'Testlagerplatz',
81                                                                  number_of_bins        => 10,
82                                                                 );
83 To access the second bin:
84   my $bin2 = $wh->bins->[1];
85
86 =head2 C<set_stock $part, %PARAMS>
87
88 Increase the stock level of a certain part by creating an inventory event. Currently
89 only positive stock levels can be set. To access the updated onhand the part
90 object needs to be loaded afterwards.
91
92   my $part = SL::DB::Manager::Part->find_by(partnumber => '1');
93   SL::Dev::Inventory::set_stock($part, 5);
94   $part->load;
95
96 =head1 BUGS
97
98 Nothing here yet.
99
100 =head1 AUTHOR
101
102 G. Richardson E<lt>grichardson@kivitendo-premium.deE<gt>
103
104 =cut