8c5d843660a0abf9145944666f1836d82418888d
[kivitendo-erp.git] / t / wh / inventory.t
1 use strict;
2 use Test::More;
3
4 use lib 't';
5
6 use SL::Dev::Part qw(new_part new_assembly);
7 use SL::Dev::Inventory qw(create_warehouse_and_bins set_stock);
8 use SL::Dev::Record qw(create_sales_order);
9
10 use_ok 'Support::TestSetup';
11 use_ok 'SL::DB::Bin';
12 use_ok 'SL::DB::Part';
13 use_ok 'SL::DB::Warehouse';
14 use_ok 'SL::DB::Inventory';
15 use_ok 'SL::WH';
16 use_ok 'SL::Helper::Inventory';
17
18 Support::TestSetup::login();
19
20 my ($wh, $bin1, $bin2, $assembly1);
21
22 reset_db();
23 create_standard_stock();
24
25
26 # simple stock in, get_stock, get_onhand
27 set_stock(
28   part => $assembly1,
29   qty => 25,
30   bin => $bin1,
31 );
32
33 is(SL::Helper::Inventory::get_stock(part => $assembly1), "25.00000", 'simple get_stock works');
34 is(SL::Helper::Inventory::get_onhand(part => $assembly1), "25.00000", 'simple get_onhand works');
35
36 # stock on some more, get_stock, get_onhand
37
38 WH->transfer({
39   parts_id          => $assembly1->id,
40   qty               => 15,
41   transfer_type     => 'stock',
42   dst_warehouse_id  => $bin1->warehouse_id,
43   dst_bin_id        => $bin1->id,
44   comment           => 'more',
45 });
46
47 WH->transfer({
48   parts_id          => $assembly1->id,
49   qty               => 20,
50   transfer_type     => 'stock',
51   chargenumber      => '298345',
52   dst_warehouse_id  => $bin1->warehouse_id,
53   dst_bin_id        => $bin1->id,
54   comment           => 'more',
55 });
56
57 is(SL::Helper::Inventory::get_stock(part => $assembly1), "60.00000", 'normal get_stock works');
58 is(SL::Helper::Inventory::get_onhand(part => $assembly1), "60.00000", 'normal get_onhand works');
59
60 # allocate some stuff
61
62 my @allocations = SL::Helper::Inventory::allocate(
63   part => $assembly1,
64   qty  => 12,
65 );
66
67 is_deeply(\%{ $allocations[0] }, {
68    bestbefore        => undef,
69    bin_id            => $bin1->id,
70    chargenumber      => '',
71    parts_id          => $assembly1->id,
72    qty               => 12,
73    warehouse_id      => $wh->id,
74    comment           => undef,
75    for_object_id     => undef,
76  }, 'allocation works');
77
78 # simple
79
80 # more than exists
81
82 # produce something
83
84 # produce the same using auto_allocation
85
86
87 sub reset_db {
88   SL::DB::Manager::Order->delete_all(all => 1);
89   SL::DB::Manager::Inventory->delete_all(all => 1);
90   SL::DB::Manager::Assembly->delete_all(all => 1);
91   SL::DB::Manager::Part->delete_all(all => 1);
92   SL::DB::Manager::Bin->delete_all(all => 1);
93   SL::DB::Manager::Warehouse->delete_all(all => 1);
94 }
95
96 sub create_standard_stock {
97   ($wh, $bin1) = create_warehouse_and_bins();
98   $bin2 = SL::DB::Bin->new(description => "Bin 2", warehouse => $wh)->save;
99   $wh->load;
100
101   $assembly1  =  new_assembly()->save;
102 }
103
104
105 reset();
106
107 done_testing();
108
109 1;