Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

20240213 fix produce assembly #254

Merged
merged 4 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion SL/Helper/Inventory.pm
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,8 @@ sub produce_assembly {

my $allocations = $params{allocations};
my $strict_wh = $::instance_conf->get_produce_assembly_same_warehouse ? $bin->warehouse : undef;
my $consume_service = $::instance_conf->get_produce_assembly_transfer_service;

if ($params{auto_allocate}) {
Carp::croak("produce_assembly: can't have both allocations and auto_allocate") if $params{allocations};
$allocations = [ allocate_for_assembly(part => $part, qty => $qty, warehouse => $strict_wh, chargenumber => $params{chargenumber}) ];
Expand All @@ -352,8 +354,14 @@ sub produce_assembly {

# check whether allocations are sane
if (!$params{no_check_allocations} && !$params{auto_allocate}) {
my %allocations_by_part = map { $_->parts_id => $_->qty } @$allocations;
my %allocations_by_part;
for (@$allocations) {
$allocations_by_part{$_->parts_id} //= 0;
$allocations_by_part{$_->parts_id} += $_->qty;
}

for my $assembly ($part->assemblies) {
next if $assembly->part->type eq 'service' && !$consume_service;
$allocations_by_part{ $assembly->parts_id } -= $assembly->qty * $qty;
}

Expand Down
156 changes: 153 additions & 3 deletions t/wh/inventory.t
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,36 @@ is(SL::Helper::Inventory::get_stock(part => $assembly_service), "1.00000", 'prod
is(SL::Helper::Inventory::get_stock(part => $part1), "0.00000", 'and consumes...');
is(SL::Helper::Inventory::get_stock(part => $part2), "0.00000", '..the materials');


# check with own allocations
set_stock(
part => $part1,
qty => 12,
bin => $bin2,
);
set_stock(
part => $part2,
qty => 6.34,
bin => $bin2,
);
@alloc1 = SL::Helper::Inventory::allocate(part => $part1, qty => 12);
@alloc2 = SL::Helper::Inventory::allocate(part => $part2, qty => 6.34);

lives_ok {
SL::Helper::Inventory::produce_assembly(
part => $assembly_service,
qty => 1,
allocations => [ @alloc1, @alloc2 ],

# where to put it
bin => $bin1,
);
} 'no exception on produce_assembly with own allocations (no service)';

is(SL::Helper::Inventory::get_stock(part => $assembly_service), "2.00000", 'produce with own allocations works');
is(SL::Helper::Inventory::get_stock(part => $part1), "0.00000", 'and consumes...');
is(SL::Helper::Inventory::get_stock(part => $part2), "0.00000", '..the materials');

# check comments and warehouses
$::form->{l_comment} = 'Y';
$::form->{l_warehouse_from} = 'Y';
Expand All @@ -316,6 +346,19 @@ cmp_deeply(\@contents,
'part_type' => 'assembly',
'warehouse_to' => 'Warehouse'
}),
ignore(), ignore(),
superhashof({
'comment' => 'Used for assembly '. $assembly_service->partnumber .' Ein Erzeugnis mit Dienstleistungen',
'warehouse_from' => 'Warehouse'
}),
superhashof({
'comment' => 'Used for assembly '. $assembly_service->partnumber .' Ein Erzeugnis mit Dienstleistungen',
'warehouse_from' => 'Warehouse'
}),
superhashof({
'part_type' => 'assembly',
'warehouse_to' => 'Warehouse'
}),
],
"Comments for assembly with service productions are ok"
);
Expand Down Expand Up @@ -349,7 +392,7 @@ like $e, qr/multiple errors during allocation/, "producing assembly with service
like $e->errors->[0]->message, qr/can not allocate 1,2 units of service number 1 We really need this service, missing 1,2 units/,
"producing assembly with services and unstocked service throws correct error message";

is(SL::Helper::Inventory::get_stock(part => $assembly_service), "1.00000", 'produce without service does not work');
is(SL::Helper::Inventory::get_stock(part => $assembly_service), "2.00000", 'produce without service does not work');
is(SL::Helper::Inventory::get_stock(part => $part1), "12.00000", 'and does not consume...');
is(SL::Helper::Inventory::get_stock(part => $part2), "6.34000", '..the materials');

Expand All @@ -370,7 +413,45 @@ SL::Helper::Inventory::produce_assembly(
bin => $bin1,
);

is(SL::Helper::Inventory::get_stock(part => $assembly_service), "2.00000", 'produce with service does work if services is needed and stocked');
is(SL::Helper::Inventory::get_stock(part => $assembly_service), "3.00000", 'produce with service does work if services is needed and stocked');
is(SL::Helper::Inventory::get_stock(part => $part1), "0.00000", 'and does consume...');
is(SL::Helper::Inventory::get_stock(part => $part2), "0.00000", '..the materials');
is(SL::Helper::Inventory::get_stock(part => $service1), "0.00000", '..and service');

# check with own allocations
set_stock(
part => $part1,
qty => 12,
bin => $bin2,
);
set_stock(
part => $part2,
qty => 6.34,
bin => $bin2,
);
is('SL::DB::Part', ref $service1);
set_stock(
part => $service1,
qty => 1.2,
bin => $bin2,
);

@alloc1 = SL::Helper::Inventory::allocate(part => $part1, qty => 12);
@alloc2 = SL::Helper::Inventory::allocate(part => $part2, qty => 6.34);
my @alloc3 = SL::Helper::Inventory::allocate(part => $service1, qty => 1.2);

lives_ok {
SL::Helper::Inventory::produce_assembly(
part => $assembly_service,
qty => 1,
allocations => [ @alloc1, @alloc2, @alloc3 ],

# where to put it
bin => $bin1,
);
} 'no exception on produce_assembly with own allocations (with service)';

is(SL::Helper::Inventory::get_stock(part => $assembly_service), "4.00000", 'produce with own allocations and service does work if services is needed and stocked');
is(SL::Helper::Inventory::get_stock(part => $part1), "0.00000", 'and does consume...');
is(SL::Helper::Inventory::get_stock(part => $part2), "0.00000", '..the materials');
is(SL::Helper::Inventory::get_stock(part => $service1), "0.00000", '..and service');
Expand All @@ -385,8 +466,27 @@ local $::instance_conf->data->{produce_assembly_same_warehouse} = 1;
#use Data::Dumper;
#diag("hier" . Dumper(@contents));
cmp_deeply(\@contents,
[ ignore(), ignore(), ignore(), ignore(), ignore(), ignore(), ignore(), ignore(),
[ ignore(), ignore(), ignore(), ignore(), ignore(), ignore(), ignore(), ignore(), ignore(), ignore(), ignore(), ignore(), ignore(),
superhashof({
'comment' => 'Used for assembly '. $assembly_service->partnumber .' Ein Erzeugnis mit Dienstleistungen',
'warehouse_from' => 'Warehouse'
}),
superhashof({
'comment' => 'Used for assembly '. $assembly_service->partnumber .' Ein Erzeugnis mit Dienstleistungen',
'warehouse_from' => 'Warehouse'
}),
superhashof({
'comment' => 'Used for assembly '. $assembly_service->partnumber .' Ein Erzeugnis mit Dienstleistungen',
'warehouse_from' => 'Warehouse',
'part_type' => 'service',
'qty' => '1.20000',
}),
superhashof({
'part_type' => 'assembly',
'warehouse_to' => 'Warehouse'
}),
ignore(), ignore(), ignore(),
superhashof({
'comment' => 'Used for assembly '. $assembly_service->partnumber .' Ein Erzeugnis mit Dienstleistungen',
'warehouse_from' => 'Warehouse'
}),
Expand All @@ -408,6 +508,56 @@ cmp_deeply(\@contents,
"Comments for assembly with service productions are ok"
);

# check own allocations with more than one allocation per part
reset_db();
create_standard_stock();

set_stock(
part => $part1,
qty => 5,
bin => $bin1,
# no chargenumber
);
set_stock(
part => $part1,
qty => 5,
bin => $bin1,
chargenumber => 'CH1',
);
set_stock(
part => $part1,
qty => 2,
bin => $bin1,
chargenumber => 'CH2',
);
set_stock(
part => $part2,
qty => 6.34,
bin => $bin1,
);

my @alloc1_1 = SL::Helper::Inventory::allocate(part => $part1, qty => 5);
my @alloc1_2 = SL::Helper::Inventory::allocate(part => $part1, qty => 5, chargenumber => 'CH1');
my @alloc1_3 = SL::Helper::Inventory::allocate(part => $part1, qty => 2, chargenumber => 'CH2');
@alloc2 = SL::Helper::Inventory::allocate(part => $part2, qty => 6.34);

local $::instance_conf->data->{produce_assembly_transfer_service} = 0;

lives_ok {
SL::Helper::Inventory::produce_assembly(
part => $assembly_service,
qty => 1,
allocations => [ @alloc1_1, @alloc1_2, @alloc1_3, @alloc2 ],

# where to put it
bin => $bin1,
);
} 'no exception on produce_assembly with more than one own allocation per part';

is(SL::Helper::Inventory::get_stock(part => $assembly_service), "1.00000", 'produce with own allocations works');
is(SL::Helper::Inventory::get_stock(part => $part1), "0.00000", 'and consumes...');
is(SL::Helper::Inventory::get_stock(part => $part2), "0.00000", '..the materials');



# bestbefore tests
Expand Down
Loading