Merge branch 'b-3.6.1' of ../kivitendo-erp_20220811
[kivitendo-erp.git] / t / part / stock.t
1 use strict;
2 use Test::More;
3
4 use lib 't';
5 use Support::TestSetup;
6 use Carp;
7 use Test::Exception;
8 use SL::DB::Part;
9 use SL::Dev::Part qw(new_part);
10 use SL::Dev::Inventory qw(create_warehouse_and_bins set_stock transfer_stock);
11
12 Support::TestSetup::login();
13
14 clear_up();
15
16 my ($wh1, $bin1_1) = create_warehouse_and_bins(
17   warehouse_description => 'Testlager',
18   bin_description       => 'Testlagerplatz',
19   number_of_bins        => 2,
20 );
21 my $bin1_2 = $wh1->bins->[1];
22 my ($wh2, $bin2_1) = create_warehouse_and_bins(
23   warehouse_description => 'Testlager 2',
24   bin_description       => 'Testlagerplatz 2',
25   number_of_bins        => 2,
26 );
27
28 my $today     = DateTime->today;
29 my $yesterday = $today->clone->add(days => -1);
30
31 my $part = new_part()->save;
32 set_stock(part => $part, bin_id => $bin1_1->id, qty => 7, shippingdate => $yesterday);
33 set_stock(part => $part, bin_id => $bin1_1->id, qty => 5);
34 set_stock(part => $part, bin_id => $bin1_1->id, abs_qty => 8); # apply -4 to get qty 8 in bin1_1
35 set_stock(part => $part, bin_id => $bin1_2->id, qty => 9);
36
37 set_stock(part => $part, bin_id => $bin2_1->id, abs_qty => 10);
38 transfer_stock(part     => $part,
39                from_bin => $wh2->bins->[0],
40                to_bin   => $wh2->bins->[1],
41                qty      => 2,
42               );
43
44 is( SL::DB::Manager::Part->get_all_count(), 1,  "total number of parts created is 1");
45 is( $part->get_stock == 27                                     , 1 , "total stock of part is 27");
46 is( $part->get_stock(shippingdate => $yesterday) == 7          , 1 , "total stock of part was 7 yesterday");
47 is( $part->get_stock(shippingdate => $today) == 27             , 1 , "total stock of part is 27");
48 is( $part->get_stock(bin_id       => $bin1_1->id) == 8         , 1 , "total stock of part in bin1_1 is 8");
49 is( $part->get_stock(warehouse_id => $wh1->id) == 17           , 1 , "total stock of part in wh1 is 17");
50 is( $part->get_stock(warehouse_id => $wh2->id) == 10           , 1 , "total stock of part in wh2 is 10");
51 is( $part->get_stock(bin_id       => $wh2->bins->[0]->id) == 8 , 1 , "total stock of part in wh2 2nd bin is 8 after transfer");
52 is( $part->get_stock(bin_id       => $wh2->bins->[1]->id) == 2 , 1 , "total stock of part in wh2 2nd bin is 2 after transfer");
53
54 clear_up();
55 done_testing;
56
57 sub clear_up {
58   "SL::DB::Manager::${_}"->delete_all(all => 1) for qw(Inventory Part Bin Warehouse);
59 }
60
61 1;