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