Revision 981bfe17
Von Kivitendo Admin vor fast 8 Jahren hinzugefügt
SL/Dev/Part.pm | ||
---|---|---|
39 | 39 |
sub create_assembly { |
40 | 40 |
my (%params) = @_; |
41 | 41 |
|
42 |
my @parts; |
|
43 |
my $partnumber = delete $params{partnumber} || 'ap1'; |
|
44 |
my $part1 = SL::Dev::Part::create_part(partnumber => $partnumber, |
|
45 |
description => 'Testpart', |
|
46 |
)->save; |
|
47 |
push(@parts, $part1); |
|
42 |
my $assnumber = delete $params{assnumber}; |
|
43 |
my $base_partnumber = delete $params{partnumber} || 'ap'; |
|
48 | 44 |
|
49 |
my $number_of_parts = delete $params{number_of_parts} || 3;
|
|
45 |
my $assembly_items = [];
|
|
50 | 46 |
|
51 |
for my $i ( 2 .. $number_of_parts ) { |
|
52 |
my $part = $parts[0]->clone_and_reset; |
|
53 |
$part->partnumber( $partnumber . " " . $i ); |
|
54 |
$part->description( ($part->description // '') . " " . $i ); |
|
55 |
$part->save; |
|
56 |
push(@parts, $part); |
|
47 |
if ( $params{assembly_items} ) { |
|
48 |
$assembly_items = delete $params{assembly_items}; |
|
49 |
} else { |
|
50 |
for my $i ( 1 .. delete $params{number_of_parts} || 3) { |
|
51 |
my $part = SL::Dev::Part::create_part(partnumber => "$base_partnumber $i", |
|
52 |
description => "Testpart $i", |
|
53 |
)->save; |
|
54 |
push( @{$assembly_items}, SL::DB::Assembly->new(parts_id => $part->id, |
|
55 |
qty => 1, |
|
56 |
position => $i, |
|
57 |
)); |
|
58 |
} |
|
57 | 59 |
} |
58 | 60 |
|
59 |
my $assnumber = delete $params{assnumber} || 'as1'; |
|
60 | 61 |
my $assembly = SL::DB::Part->new_assembly( |
61 | 62 |
partnumber => $assnumber, |
62 | 63 |
description => 'Test Assembly', |
63 | 64 |
sellprice => '10', |
64 | 65 |
lastcost => '5', |
66 |
assemblies => $assembly_items, |
|
65 | 67 |
buchungsgruppen_id => _default_buchungsgruppe()->id, |
66 | 68 |
unit => _default_unit()->name |
67 | 69 |
); |
68 |
|
|
69 |
foreach my $part ( @parts ) { |
|
70 |
$assembly->add_assemblies( SL::DB::Assembly->new(parts_id => $part->id, qty => 1, bom => 1) ); |
|
71 |
} |
|
72 | 70 |
$assembly->assign_attributes( %params ); |
73 | 71 |
return $assembly; |
74 | 72 |
} |
... | ... | |
76 | 74 |
sub create_assortment { |
77 | 75 |
my (%params) = @_; |
78 | 76 |
|
77 |
my $assnumber = delete $params{assnumber}; |
|
78 |
my $base_partnumber = delete $params{partnumber} || 'ap'; |
|
79 |
|
|
79 | 80 |
my $assortment_items = []; |
81 |
|
|
80 | 82 |
if ( $params{assortment_items} ) { |
81 |
$assortment_items = $params{assortment_items}; |
|
83 |
$assortment_items = delete $params{assortment_items};
|
|
82 | 84 |
} else { |
83 |
# no assortment items were passed, create a part, clone it several times |
|
84 |
# and add to assortment as assortment_items |
|
85 |
my @parts; |
|
86 |
my $part1 = SL::Dev::Part::create_part(partnumber => 'sp1', |
|
87 |
description => 'Testpart assortment', |
|
88 |
)->save; |
|
89 |
push(@parts, $part1); |
|
90 |
|
|
91 |
my $number_of_parts = delete $params{number_of_parts} || 3; |
|
92 |
|
|
93 |
for my $i ( 2 .. $number_of_parts ) { |
|
94 |
my $part = $parts[0]->clone_and_reset; |
|
95 |
$part->partnumber( ($part->partnumber // '') . " " . $i ); |
|
96 |
$part->description( ($part->description // '') . " " . $i ); |
|
97 |
$part->save; |
|
98 |
push(@parts, $part); |
|
99 |
} |
|
100 |
my $position = 0; |
|
101 |
foreach my $part ( @parts ) { |
|
85 |
for my $i ( 1 .. delete $params{number_of_parts} || 3) { |
|
86 |
my $part = SL::Dev::Part::create_part(partnumber => "$base_partnumber $i", |
|
87 |
description => "Testpart $i", |
|
88 |
)->save; |
|
102 | 89 |
push( @{$assortment_items}, SL::DB::AssortmentItem->new(parts_id => $part->id, |
103 | 90 |
qty => 1, |
104 |
position => $position++, |
|
105 |
charge => 1, |
|
91 |
position => $i, |
|
106 | 92 |
unit => $part->unit, |
107 | 93 |
)); |
108 | 94 |
} |
109 | 95 |
} |
110 | 96 |
|
111 | 97 |
my $assortment = SL::DB::Part->new_assortment( |
112 |
partnumber => 'as1',
|
|
98 |
partnumber => $assnumber,
|
|
113 | 99 |
description => 'Test Assortment', |
114 | 100 |
sellprice => '10', |
115 | 101 |
lastcost => '5', |
... | ... | |
171 | 157 |
|
172 | 158 |
Create a new assembly (part_type = assembly). |
173 | 159 |
|
160 |
Params: assnumber: the partnumber of the assembly |
|
161 |
partnumber: the partnumber of the first assembly part to be created |
|
162 |
|
|
174 | 163 |
By default 3 parts (p1, p2, p3) are created and saved as an assembly (as1). |
175 | 164 |
|
176 | 165 |
my $assembly = SL::Dev::Part::create_assembly->save; |
177 | 166 |
|
178 |
Create a new assembly with 10 parts: |
|
167 |
Create a new assembly with 10 parts, the assembly gets partnumber 'Ass1' and the |
|
168 |
parts get partnumbers 'Testpart 1' to 'Testpart 10': |
|
169 |
|
|
170 |
my $assembly = SL::Dev::Part::create_assembly(number_of_parts => 10, |
|
171 |
partnumber => 'Testpart', |
|
172 |
assnumber => 'Ass1' |
|
173 |
)->save; |
|
179 | 174 |
|
180 |
my $assembly = SL::Dev::Part::create_assembly(number_of_parts => 10)->save; |
|
175 |
Create an assembly with specific parts: |
|
176 |
my $assembly_item_1 = SL::DB::Assembly->new( parts_id => $part1->id, qty => 3, position => 1); |
|
177 |
my $assembly_item_2 = SL::DB::Assembly->new( parts_id => $part2->id, qty => 3, position => 2); |
|
178 |
my $assembly_part = SL::Dev::Part::create_assembly( assnumber => 'Assembly 1', |
|
179 |
description => 'Assembly test', |
|
180 |
sellprice => $part1->sellprice + $part2->sellprice, |
|
181 |
assembly_items => [ $assembly_item_1, $assembly_item_2 ], |
|
182 |
)->save; |
|
181 | 183 |
|
182 | 184 |
=head2 C<create_assortment %PARAMS> |
183 | 185 |
|
... | ... | |
199 | 201 |
my $part2 = SL::Dev::Part::create_part( sellprice => '6.66')->save; |
200 | 202 |
my $assortment_item_1 = SL::DB::AssortmentItem->new( parts_id => $part1->id, qty => 3, unit => $part1->unit, position => 1); |
201 | 203 |
my $assortment_item_2 = SL::DB::AssortmentItem->new( parts_id => $part2->id, qty => 3, unit => $part2->unit, position => 2); |
202 |
my $assortment_part = SL::Dev::Part::create_assortment( partnumber => 'Assortment 1',
|
|
203 |
description => 'assortment test', |
|
204 |
sellprice => '0',
|
|
205 |
part_type => 'assortment',
|
|
204 |
my $assortment_part = SL::Dev::Part::create_assortment( assnumber => 'Assortment 1',
|
|
205 |
description => 'assortment test',
|
|
206 |
sellprice => (3*$part1->sellprice + 3*$part2->sellprice),
|
|
207 |
lastcost => (3*$part1->lastcost + 3*$part2->lastcost),
|
|
206 | 208 |
assortment_items => [ $assortment_item_1, $assortment_item_2 ], |
207 | 209 |
)->save; |
208 | 210 |
|
t/part/assembly.t | ||
---|---|---|
16 | 16 |
clear_up(); |
17 | 17 |
reset_state(); |
18 | 18 |
|
19 |
is( SL::DB::Manager::Part->get_all_count(), 4, "total number of parts created is 4"); |
|
19 |
is( SL::DB::Manager::Part->get_all_count(), 4, "total number of parts created by reset_state() is 4");
|
|
20 | 20 |
|
21 |
my $assembly_part = SL::DB::Manager::Part->find_by( partnumber => '19000' ) || die "Can't find part 19000";
|
|
22 |
my $assembly_item_part = SL::DB::Manager::Part->find_by( partnumber => 'ap1' );
|
|
21 |
my $assembly_part = SL::DB::Manager::Part->find_by( partnumber => '19000' ) || die "Can't find assembly 19000";
|
|
22 |
my $assembly_item_part = SL::DB::Manager::Part->find_by( partnumber => '19000 1' ) || die "Can't find assembly item part '19000 1'";
|
|
23 | 23 |
|
24 | 24 |
is($assembly_part->part_type, 'assembly', 'assembly has correct type'); |
25 | 25 |
is( scalar @{$assembly_part->assemblies}, 3, 'assembly consists of three parts' ); |
... | ... | |
27 | 27 |
# fetch assembly item corresponding to partnumber 19000 |
28 | 28 |
my $assembly_items = $assembly_part->find_assemblies( { parts_id => $assembly_item_part->id } ) || die "can't find assembly_item"; |
29 | 29 |
my $assembly_item = $assembly_items->[0]; |
30 |
is($assembly_item->part->partnumber, 'ap1', 'assembly part part relation works');
|
|
30 |
is($assembly_item->part->partnumber, '19000 1', 'assembly part part relation works');
|
|
31 | 31 |
is($assembly_item->assembly_part->partnumber, '19000', 'assembly part assembly part relation works'); |
32 | 32 |
|
33 | 33 |
|
34 | 34 |
|
35 |
my $assembly2_part = SL::Dev::Part::create_assembly( partnumber => '20000', part1number => 'ap2', assnumber => 'as2' )->save;
|
|
35 |
my $assembly2_part = SL::Dev::Part::create_assembly( partnumber => '20000', assnumber => 'as2' )->save; |
|
36 | 36 |
my $retval = validate_assembly($assembly_part,$assembly2_part); |
37 | 37 |
ok(!defined $retval, 'assembly 19000 can be child of assembly 20000' ); |
38 | 38 |
$assembly2_part->add_assemblies(SL::DB::Assembly->new(parts_id => $assembly_part->id, qty => 3, bom => 1)); |
39 | 39 |
$assembly2_part->save; |
40 | 40 |
|
41 |
my $assembly3_part = SL::Dev::Part::create_assembly( partnumber => '30000', part1number => 'ap3', assnumber => 'as3' )->save;
|
|
41 |
my $assembly3_part = SL::Dev::Part::create_assembly( partnumber => '30000', assnumber => 'as3' )->save; |
|
42 | 42 |
$retval = validate_assembly($assembly3_part,$assembly_part); |
43 | 43 |
ok(!defined $retval, 'assembly 30000 can be child of assembly 19000' ); |
44 | 44 |
|
... | ... | |
56 | 56 |
|
57 | 57 |
# fetch assembly item corresponding to partnumber 20000 |
58 | 58 |
my $assembly2_items = $assembly2_part->find_assemblies() || die "can't find assembly_item"; |
59 |
is( scalar @{$assembly2_items}, 5, 'assembly2 consists of four parts' );
|
|
59 |
is( scalar @{$assembly2_items}, 5, 'assembly2 consists of ive parts' );
|
|
60 | 60 |
my $assembly2_item = $assembly2_items->[3]; |
61 |
is($assembly2_item->qty, 3, 'count of 3.th assembly is 3' );
|
|
62 |
is($assembly2_item->part->part_type, 'assembly', '3.th assembly \''.$assembly2_item->part->partnumber. '\' is also an assembly');
|
|
61 |
is($assembly2_item->qty, 3, 'qty of 3rd assembly item is 3' );
|
|
62 |
is($assembly2_item->part->part_type, 'assembly', '3rd assembly item \'' . $assembly2_item->part->partnumber . '\' is also an assembly');
|
|
63 | 63 |
my $assembly3_items = $assembly2_item->part->find_assemblies() || die "can't find assembly_item"; |
64 |
is( scalar @{$assembly3_items}, 4, 'assembly3 consists of three parts' );
|
|
64 |
is( scalar @{$assembly3_items}, 4, 'assembly3 consists of four parts' );
|
|
65 | 65 |
|
66 | 66 |
|
67 | 67 |
|
... | ... | |
71 | 71 |
if (!$retval && $assembly_part->add_assemblies( SL::DB::Assembly->new(parts_id => $assembly_part->id, qty => 8, bom => 1))) { |
72 | 72 |
$assembly_part->save; |
73 | 73 |
} |
74 |
is( scalar @{$assembly_part->assemblies}, 4, 'assembly consists of three parts' );
|
|
74 |
is( scalar @{$assembly_part->assemblies}, 4, 'assembly consists of four parts' );
|
|
75 | 75 |
|
76 | 76 |
# check indirekt loop |
77 | 77 |
$retval = validate_assembly($assembly2_part,$assembly_part); |
... | ... | |
79 | 79 |
if (!$retval && $assembly_part->add_assemblies( SL::DB::Assembly->new(parts_id => $assembly2_part->id, qty => 9, bom => 1))) { |
80 | 80 |
$assembly_part->save; |
81 | 81 |
} |
82 |
is( scalar @{$assembly_part->assemblies}, 4, 'assembly consists of three parts' );
|
|
82 |
is( scalar @{$assembly_part->assemblies}, 4, 'assembly consists of four parts' );
|
|
83 | 83 |
|
84 | 84 |
clear_up(); |
85 | 85 |
done_testing; |
... | ... | |
92 | 92 |
sub reset_state { |
93 | 93 |
my %params = @_; |
94 | 94 |
|
95 |
my $assembly = SL::Dev::Part::create_assembly( partnumber => '19000' )->save; |
|
95 |
my $assembly = SL::Dev::Part::create_assembly( assnumber => '19000', partnumber => '19000' )->save;
|
|
96 | 96 |
}; |
97 | 97 |
|
98 | 98 |
1; |
t/part/assortment.t | ||
---|---|---|
12 | 12 |
|
13 | 13 |
clear_up(); |
14 | 14 |
|
15 |
my $assortment = SL::Dev::Part::create_assortment( partnumber => 'aso1',
|
|
16 |
description => "Assortment 1",
|
|
17 |
number_of_parts => 10,
|
|
15 |
my $assortment = SL::Dev::Part::create_assortment( assnumber => 'aso1',
|
|
16 |
description => "Assortment 1", |
|
17 |
number_of_parts => 10, |
|
18 | 18 |
)->save; |
19 | 19 |
|
20 | 20 |
is( SL::DB::Manager::Part->get_all_count(), 11, "total number of parts created is 11"); |
Auch abrufbar als: Unified diff
SL::Dev::Part - create_assembly und create_assortment überarbeitet
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.