1 package SL::Dev::Inventory;
5 our @EXPORT = qw(create_warehouse_and_bins set_stock);
10 use SL::DB::TransferType;
13 sub create_warehouse_and_bins {
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" ) );
22 return ($wh, $wh->bins->[0]);
26 my ($part, %params) = @_;
28 die "first argument is not a part" unless ref($part) eq 'SL::DB::Part';
30 die "no default warehouse" unless $part->warehouse_id or $part->bin_id;
32 die "Can't determine employee" unless SL::DB::Manager::Employee->current;
34 die "qty is missing or not positive" unless $params{qty} and $params{qty} > 0;
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' );
40 if ( $params{shippingdate} ) {
41 $shippingdate = $::locale->parse_date_to_object(delete $params{shippingdate});
43 $shippingdate = DateTime->today;
46 my ($trans_id) = $part->db->dbh->selectrow_array("select nextval('id')", {});
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,
57 trans_id => $trans_id,
67 SL::Dev::Inventory - create inventory-related objects for testing, with minimal
72 =head2 C<create_warehouse_and_bins %PARAMS>
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();
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',
83 To access the second bin:
84 my $bin2 = $wh->bins->[1];
86 =head2 C<set_stock $part, %PARAMS>
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.
92 my $part = SL::DB::Manager::Part->find_by(partnumber => '1');
93 SL::Dev::Inventory::set_stock($part, 5);
102 G. Richardson E<lt>grichardson@kivitendo-premium.deE<gt>