Merge branch 'b-3.6.1' of ../kivitendo-erp_20220811
[kivitendo-erp.git] / t / part / stock.t
diff --git a/t/part/stock.t b/t/part/stock.t
new file mode 100644 (file)
index 0000000..39e5756
--- /dev/null
@@ -0,0 +1,61 @@
+use strict;
+use Test::More;
+
+use lib 't';
+use Support::TestSetup;
+use Carp;
+use Test::Exception;
+use SL::DB::Part;
+use SL::Dev::Part qw(new_part);
+use SL::Dev::Inventory qw(create_warehouse_and_bins set_stock transfer_stock);
+
+Support::TestSetup::login();
+
+clear_up();
+
+my ($wh1, $bin1_1) = create_warehouse_and_bins(
+  warehouse_description => 'Testlager',
+  bin_description       => 'Testlagerplatz',
+  number_of_bins        => 2,
+);
+my $bin1_2 = $wh1->bins->[1];
+my ($wh2, $bin2_1) = create_warehouse_and_bins(
+  warehouse_description => 'Testlager 2',
+  bin_description       => 'Testlagerplatz 2',
+  number_of_bins        => 2,
+);
+
+my $today     = DateTime->today;
+my $yesterday = $today->clone->add(days => -1);
+
+my $part = new_part()->save;
+set_stock(part => $part, bin_id => $bin1_1->id, qty => 7, shippingdate => $yesterday);
+set_stock(part => $part, bin_id => $bin1_1->id, qty => 5);
+set_stock(part => $part, bin_id => $bin1_1->id, abs_qty => 8); # apply -4 to get qty 8 in bin1_1
+set_stock(part => $part, bin_id => $bin1_2->id, qty => 9);
+
+set_stock(part => $part, bin_id => $bin2_1->id, abs_qty => 10);
+transfer_stock(part     => $part,
+               from_bin => $wh2->bins->[0],
+               to_bin   => $wh2->bins->[1],
+               qty      => 2,
+              );
+
+is( SL::DB::Manager::Part->get_all_count(), 1,  "total number of parts created is 1");
+is( $part->get_stock == 27                                     , 1 , "total stock of part is 27");
+is( $part->get_stock(shippingdate => $yesterday) == 7          , 1 , "total stock of part was 7 yesterday");
+is( $part->get_stock(shippingdate => $today) == 27             , 1 , "total stock of part is 27");
+is( $part->get_stock(bin_id       => $bin1_1->id) == 8         , 1 , "total stock of part in bin1_1 is 8");
+is( $part->get_stock(warehouse_id => $wh1->id) == 17           , 1 , "total stock of part in wh1 is 17");
+is( $part->get_stock(warehouse_id => $wh2->id) == 10           , 1 , "total stock of part in wh2 is 10");
+is( $part->get_stock(bin_id       => $wh2->bins->[0]->id) == 8 , 1 , "total stock of part in wh2 2nd bin is 8 after transfer");
+is( $part->get_stock(bin_id       => $wh2->bins->[1]->id) == 2 , 1 , "total stock of part in wh2 2nd bin is 2 after transfer");
+
+clear_up();
+done_testing;
+
+sub clear_up {
+  "SL::DB::Manager::${_}"->delete_all(all => 1) for qw(Inventory Part Bin Warehouse);
+}
+
+1;