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 |
|
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.