From 981bfe1710c2c9634356346b4c99c76c6b686a4b Mon Sep 17 00:00:00 2001 From: "G. Richardson" Date: Wed, 1 Feb 2017 09:22:22 +0100 Subject: [PATCH] =?utf8?q?SL::Dev::Part=20-=20create=5Fassembly=20und=20cr?= =?utf8?q?eate=5Fassortment=20=C3=BCberarbeitet?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit create_assembly wie create_assortment erweitert, so daß man direkt beim Erstellen die Erzeugnisbestandteile als Assembly-Objekte im param assembly_items übergeben kann. Refactoring, kein clonen wenn mehrere Bestandteile automatisch erstellt werden. --- SL/Dev/Part.pm | 96 +++++++++++++++++++++++---------------------- t/part/assembly.t | 26 ++++++------ t/part/assortment.t | 6 +-- 3 files changed, 65 insertions(+), 63 deletions(-) diff --git a/SL/Dev/Part.pm b/SL/Dev/Part.pm index 7b67ada5b..45976b6f2 100644 --- a/SL/Dev/Part.pm +++ b/SL/Dev/Part.pm @@ -39,36 +39,34 @@ sub create_service { sub create_assembly { my (%params) = @_; - my @parts; - my $partnumber = delete $params{partnumber} || 'ap1'; - my $part1 = SL::Dev::Part::create_part(partnumber => $partnumber, - description => 'Testpart', - )->save; - push(@parts, $part1); + my $assnumber = delete $params{assnumber}; + my $base_partnumber = delete $params{partnumber} || 'ap'; - my $number_of_parts = delete $params{number_of_parts} || 3; + my $assembly_items = []; - for my $i ( 2 .. $number_of_parts ) { - my $part = $parts[0]->clone_and_reset; - $part->partnumber( $partnumber . " " . $i ); - $part->description( ($part->description // '') . " " . $i ); - $part->save; - push(@parts, $part); + if ( $params{assembly_items} ) { + $assembly_items = delete $params{assembly_items}; + } else { + for my $i ( 1 .. delete $params{number_of_parts} || 3) { + my $part = SL::Dev::Part::create_part(partnumber => "$base_partnumber $i", + description => "Testpart $i", + )->save; + push( @{$assembly_items}, SL::DB::Assembly->new(parts_id => $part->id, + qty => 1, + position => $i, + )); + } } - my $assnumber = delete $params{assnumber} || 'as1'; my $assembly = SL::DB::Part->new_assembly( partnumber => $assnumber, description => 'Test Assembly', sellprice => '10', lastcost => '5', + assemblies => $assembly_items, buchungsgruppen_id => _default_buchungsgruppe()->id, unit => _default_unit()->name ); - - foreach my $part ( @parts ) { - $assembly->add_assemblies( SL::DB::Assembly->new(parts_id => $part->id, qty => 1, bom => 1) ); - } $assembly->assign_attributes( %params ); return $assembly; } @@ -76,40 +74,28 @@ sub create_assembly { sub create_assortment { my (%params) = @_; + my $assnumber = delete $params{assnumber}; + my $base_partnumber = delete $params{partnumber} || 'ap'; + my $assortment_items = []; + if ( $params{assortment_items} ) { - $assortment_items = $params{assortment_items}; + $assortment_items = delete $params{assortment_items}; } else { - # no assortment items were passed, create a part, clone it several times - # and add to assortment as assortment_items - my @parts; - my $part1 = SL::Dev::Part::create_part(partnumber => 'sp1', - description => 'Testpart assortment', - )->save; - push(@parts, $part1); - - my $number_of_parts = delete $params{number_of_parts} || 3; - - for my $i ( 2 .. $number_of_parts ) { - my $part = $parts[0]->clone_and_reset; - $part->partnumber( ($part->partnumber // '') . " " . $i ); - $part->description( ($part->description // '') . " " . $i ); - $part->save; - push(@parts, $part); - } - my $position = 0; - foreach my $part ( @parts ) { + for my $i ( 1 .. delete $params{number_of_parts} || 3) { + my $part = SL::Dev::Part::create_part(partnumber => "$base_partnumber $i", + description => "Testpart $i", + )->save; push( @{$assortment_items}, SL::DB::AssortmentItem->new(parts_id => $part->id, qty => 1, - position => $position++, - charge => 1, + position => $i, unit => $part->unit, )); } } my $assortment = SL::DB::Part->new_assortment( - partnumber => 'as1', + partnumber => $assnumber, description => 'Test Assortment', sellprice => '10', lastcost => '5', @@ -171,13 +157,29 @@ Minimal usage, default values, without saving to database: Create a new assembly (part_type = assembly). +Params: assnumber: the partnumber of the assembly + partnumber: the partnumber of the first assembly part to be created + By default 3 parts (p1, p2, p3) are created and saved as an assembly (as1). my $assembly = SL::Dev::Part::create_assembly->save; -Create a new assembly with 10 parts: +Create a new assembly with 10 parts, the assembly gets partnumber 'Ass1' and the +parts get partnumbers 'Testpart 1' to 'Testpart 10': + + my $assembly = SL::Dev::Part::create_assembly(number_of_parts => 10, + partnumber => 'Testpart', + assnumber => 'Ass1' + )->save; - my $assembly = SL::Dev::Part::create_assembly(number_of_parts => 10)->save; +Create an assembly with specific parts: + my $assembly_item_1 = SL::DB::Assembly->new( parts_id => $part1->id, qty => 3, position => 1); + my $assembly_item_2 = SL::DB::Assembly->new( parts_id => $part2->id, qty => 3, position => 2); + my $assembly_part = SL::Dev::Part::create_assembly( assnumber => 'Assembly 1', + description => 'Assembly test', + sellprice => $part1->sellprice + $part2->sellprice, + assembly_items => [ $assembly_item_1, $assembly_item_2 ], + )->save; =head2 C @@ -199,10 +201,10 @@ from newly created parts: my $part2 = SL::Dev::Part::create_part( sellprice => '6.66')->save; my $assortment_item_1 = SL::DB::AssortmentItem->new( parts_id => $part1->id, qty => 3, unit => $part1->unit, position => 1); my $assortment_item_2 = SL::DB::AssortmentItem->new( parts_id => $part2->id, qty => 3, unit => $part2->unit, position => 2); - my $assortment_part = SL::Dev::Part::create_assortment( partnumber => 'Assortment 1', - description => 'assortment test', - sellprice => '0', - part_type => 'assortment', + my $assortment_part = SL::Dev::Part::create_assortment( assnumber => 'Assortment 1', + description => 'assortment test', + sellprice => (3*$part1->sellprice + 3*$part2->sellprice), + lastcost => (3*$part1->lastcost + 3*$part2->lastcost), assortment_items => [ $assortment_item_1, $assortment_item_2 ], )->save; diff --git a/t/part/assembly.t b/t/part/assembly.t index 6a3a05e24..197dfb580 100644 --- a/t/part/assembly.t +++ b/t/part/assembly.t @@ -16,10 +16,10 @@ $::locale = Locale->new("en"); clear_up(); reset_state(); -is( SL::DB::Manager::Part->get_all_count(), 4, "total number of parts created is 4"); +is( SL::DB::Manager::Part->get_all_count(), 4, "total number of parts created by reset_state() is 4"); -my $assembly_part = SL::DB::Manager::Part->find_by( partnumber => '19000' ) || die "Can't find part 19000"; -my $assembly_item_part = SL::DB::Manager::Part->find_by( partnumber => 'ap1' ); +my $assembly_part = SL::DB::Manager::Part->find_by( partnumber => '19000' ) || die "Can't find assembly 19000"; +my $assembly_item_part = SL::DB::Manager::Part->find_by( partnumber => '19000 1' ) || die "Can't find assembly item part '19000 1'"; is($assembly_part->part_type, 'assembly', 'assembly has correct type'); is( scalar @{$assembly_part->assemblies}, 3, 'assembly consists of three parts' ); @@ -27,18 +27,18 @@ is( scalar @{$assembly_part->assemblies}, 3, 'assembly consists of three parts' # fetch assembly item corresponding to partnumber 19000 my $assembly_items = $assembly_part->find_assemblies( { parts_id => $assembly_item_part->id } ) || die "can't find assembly_item"; my $assembly_item = $assembly_items->[0]; -is($assembly_item->part->partnumber, 'ap1', 'assembly part part relation works'); +is($assembly_item->part->partnumber, '19000 1', 'assembly part part relation works'); is($assembly_item->assembly_part->partnumber, '19000', 'assembly part assembly part relation works'); -my $assembly2_part = SL::Dev::Part::create_assembly( partnumber => '20000', part1number => 'ap2', assnumber => 'as2' )->save; +my $assembly2_part = SL::Dev::Part::create_assembly( partnumber => '20000', assnumber => 'as2' )->save; my $retval = validate_assembly($assembly_part,$assembly2_part); ok(!defined $retval, 'assembly 19000 can be child of assembly 20000' ); $assembly2_part->add_assemblies(SL::DB::Assembly->new(parts_id => $assembly_part->id, qty => 3, bom => 1)); $assembly2_part->save; -my $assembly3_part = SL::Dev::Part::create_assembly( partnumber => '30000', part1number => 'ap3', assnumber => 'as3' )->save; +my $assembly3_part = SL::Dev::Part::create_assembly( partnumber => '30000', assnumber => 'as3' )->save; $retval = validate_assembly($assembly3_part,$assembly_part); ok(!defined $retval, 'assembly 30000 can be child of assembly 19000' ); @@ -56,12 +56,12 @@ $assembly2_part->save; # fetch assembly item corresponding to partnumber 20000 my $assembly2_items = $assembly2_part->find_assemblies() || die "can't find assembly_item"; -is( scalar @{$assembly2_items}, 5, 'assembly2 consists of four parts' ); +is( scalar @{$assembly2_items}, 5, 'assembly2 consists of ive parts' ); my $assembly2_item = $assembly2_items->[3]; -is($assembly2_item->qty, 3, 'count of 3.th assembly is 3' ); -is($assembly2_item->part->part_type, 'assembly', '3.th assembly \''.$assembly2_item->part->partnumber. '\' is also an assembly'); +is($assembly2_item->qty, 3, 'qty of 3rd assembly item is 3' ); +is($assembly2_item->part->part_type, 'assembly', '3rd assembly item \'' . $assembly2_item->part->partnumber . '\' is also an assembly'); my $assembly3_items = $assembly2_item->part->find_assemblies() || die "can't find assembly_item"; -is( scalar @{$assembly3_items}, 4, 'assembly3 consists of three parts' ); +is( scalar @{$assembly3_items}, 4, 'assembly3 consists of four parts' ); @@ -71,7 +71,7 @@ is( $retval,"The assembly '19000' cannot be a part from itself.", 'assembly loop if (!$retval && $assembly_part->add_assemblies( SL::DB::Assembly->new(parts_id => $assembly_part->id, qty => 8, bom => 1))) { $assembly_part->save; } -is( scalar @{$assembly_part->assemblies}, 4, 'assembly consists of three parts' ); +is( scalar @{$assembly_part->assemblies}, 4, 'assembly consists of four parts' ); # check indirekt loop $retval = validate_assembly($assembly2_part,$assembly_part); @@ -79,7 +79,7 @@ ok( $retval, 'assembly indirect loop' ); if (!$retval && $assembly_part->add_assemblies( SL::DB::Assembly->new(parts_id => $assembly2_part->id, qty => 9, bom => 1))) { $assembly_part->save; } -is( scalar @{$assembly_part->assemblies}, 4, 'assembly consists of three parts' ); +is( scalar @{$assembly_part->assemblies}, 4, 'assembly consists of four parts' ); clear_up(); done_testing; @@ -92,7 +92,7 @@ sub clear_up { sub reset_state { my %params = @_; - my $assembly = SL::Dev::Part::create_assembly( partnumber => '19000' )->save; + my $assembly = SL::Dev::Part::create_assembly( assnumber => '19000', partnumber => '19000' )->save; }; 1; diff --git a/t/part/assortment.t b/t/part/assortment.t index 82d5897de..3d6705e3a 100644 --- a/t/part/assortment.t +++ b/t/part/assortment.t @@ -12,9 +12,9 @@ Support::TestSetup::login(); clear_up(); -my $assortment = SL::Dev::Part::create_assortment( partnumber => 'aso1', - description => "Assortment 1", - number_of_parts => 10, +my $assortment = SL::Dev::Part::create_assortment( assnumber => 'aso1', + description => "Assortment 1", + number_of_parts => 10, )->save; is( SL::DB::Manager::Part->get_all_count(), 11, "total number of parts created is 11"); -- 2.20.1