+reset_db();
+create_standard_stock();
+
+set_stock(
+ part => $part1,
+ qty => 5,
+ bin => $bin1,
+);
+set_stock(
+ part => $part2,
+ qty => 10,
+ bin => $bin1,
+);
+
+SL::Helper::Inventory::produce_assembly(
+ part => $assembly1,
+ qty => 3,
+ auto_allocate => 1,
+
+ # where to put it
+ bin => $bin1,
+ chargenumber => "537",
+);
+
+is(SL::Helper::Inventory::get_stock(part => $assembly1), "3.00000", 'produce with auto allocation works');
+is(SL::Helper::Inventory::get_stock(part => $part1), "2.00000", 'and consumes...');
+is(SL::Helper::Inventory::get_stock(part => $part2), "7.00000", '..the materials');
+
+# try to produce without allocations dies
+
+dies_ok(sub {
+SL::Helper::Inventory::produce_assembly(
+ part => $assembly1,
+ qty => 3,
+
+ # where to put it
+ bin => $bin1,
+ chargenumber => "537",
+);
+}, "producing without allocations dies");
+
+# try to produce with insufficient allocations dies
+
+@alloc1 = SL::Helper::Inventory::allocate(part => $part1, qty => 1);
+@alloc2 = SL::Helper::Inventory::allocate(part => $part2, qty => 1);
+
+dies_ok(sub {
+SL::Helper::Inventory::produce_assembly(
+ part => $assembly1,
+ qty => 3,
+ allocations => [ @alloc1, @alloc2 ],
+
+ # where to put it
+ bin => $bin1,
+ chargenumber => "537",
+);
+}, "producing with insufficient allocations dies");
+
+
+
+# bestbefore tests
+
+reset_db();
+create_standard_stock();
+
+set_stock(
+ part => $part1,
+ qty => 5,
+ bin => $bin1,
+);
+set_stock(
+ part => $part2,
+ qty => 10,
+ bin => $bin1,
+);
+
+
+
+SL::Helper::Inventory::produce_assembly(
+ part => $assembly1,
+ qty => 3,
+ auto_allocate => 1,
+
+ bin => $bin1,
+ chargenumber => "537",
+ bestbefore => DateTime->today->clone->add(days => -14), # expired 2 weeks ago
+ shippingdate => DateTime->today->clone->add(days => 1),
+);
+
+is(SL::Helper::Inventory::get_stock(part => $assembly1), "3.00000", 'produce with bestbefore works');
+is(SL::Helper::Inventory::get_onhand(part => $assembly1), "3.00000", 'produce with bestbefore works');
+is(SL::Helper::Inventory::get_stock(
+ part => $assembly1,
+ bestbefore => DateTime->today,
+), undef, 'get_stock with bestbefore date skips expired');
+{
+ local $::instance_conf->data->{show_bestbefore} = 1;
+ is(SL::Helper::Inventory::get_onhand(
+ part => $assembly1,
+ ), undef, 'get_onhand with bestbefore skips expired as of today');
+}
+
+{
+ local $::instance_conf->data->{show_bestbefore} = 0;
+ is(SL::Helper::Inventory::get_onhand(
+ part => $assembly1,
+ ), "3.00000", 'get_onhand without bestbefore finds all');
+}
+