Revision 1d34002d
Von Moritz Bunkus vor etwa 10 Jahren hinzugefügt
SL/DB/Helper/PriceTaxCalculator.pm | ||
---|---|---|
37 | 37 |
exchangerate => undef, |
38 | 38 |
is_sales => $self->can('customer') && $self->customer, |
39 | 39 |
is_invoice => (ref($self) =~ /Invoice/) || $params{invoice}, |
40 |
items => { },
|
|
40 |
items => [ ],
|
|
41 | 41 |
); |
42 | 42 |
|
43 | 43 |
_get_exchangerate($self, \%data, %params); |
... | ... | |
149 | 149 |
|
150 | 150 |
$data->{last_incex_chart_id} = $chart->id if $data->{is_sales}; |
151 | 151 |
|
152 |
$data->{items}->{ $item->id } = {
|
|
152 |
push @{ $data->{items} }, {
|
|
153 | 153 |
linetotal => $linetotal, |
154 | 154 |
linetotal_cost => $linetotal_cost, |
155 | 155 |
sellprice => $sellprice, |
156 | 156 |
tax_amount => $tax_amount, |
157 |
taxkey => $taxkey,
|
|
157 |
taxkey_id => $taxkey->id,
|
|
158 | 158 |
}; |
159 | 159 |
|
160 | 160 |
_dbg("CALCULATE! ${idx} i.qty " . $item->qty . " i.sellprice " . $item->sellprice . " sellprice $sellprice num_dec $num_dec taxamount $tax_amount " . |
... | ... | |
351 | 351 |
|
352 | 352 |
=item C<items> |
353 | 353 |
|
354 |
A hashref. For each line item this hashref contains an entry with |
|
355 |
additional values that have been calculated for that item but that |
|
356 |
aren't stored in the item object itself. These include C<linetotal>, |
|
357 |
C<linetotal_cost>, C<sellprice>, C<tax_amount> and C<taxkey>. |
|
354 |
An array reference. For each line item this array contains a hash ref |
|
355 |
entry with additional values that have been calculated for that item |
|
356 |
but that aren't stored in the item object itself. These include |
|
357 |
C<linetotal>, C<linetotal_cost>, C<sellprice>, C<tax_amount> and |
|
358 |
C<taxkey_id>. |
|
358 | 359 |
|
359 |
The items are hashed by their IDs. |
|
360 |
The items are stored in the same order the items are stored in the |
|
361 |
object that L</calculate_taxes_and_prices> has been called on. |
|
360 | 362 |
|
361 | 363 |
=back |
362 | 364 |
|
t/db_helper/price_tax_calculator.t | ||
---|---|---|
7 | 7 |
|
8 | 8 |
use Carp; |
9 | 9 |
use Data::Dumper; |
10 |
use List::MoreUtils qw(uniq); |
|
10 | 11 |
use Support::TestSetup; |
11 | 12 |
use Test::Exception; |
12 | 13 |
|
... | ... | |
110 | 111 |
invoiceitems => [ $item ], |
111 | 112 |
); |
112 | 113 |
|
114 |
my $taxkey = $item->part->get_taxkey(date => DateTime->today_local, is_sales => 1, taxzone => $invoice->taxzone_id); |
|
115 |
|
|
113 | 116 |
# sellprice 2.34 * qty 2.5 = 5.85 |
114 | 117 |
# 19%(5.85) = 1.1115; rounded = 1.11 |
115 | 118 |
# total rounded = 6.96 |
... | ... | |
147 | 150 |
taxes => { |
148 | 151 |
$tax->chart_id => 1.11, |
149 | 152 |
}, |
153 |
items => [ |
|
154 |
{ linetotal => 5.85, |
|
155 |
linetotal_cost => 4.83, |
|
156 |
sellprice => 2.34, |
|
157 |
tax_amount => 1.1115, |
|
158 |
taxkey_id => $taxkey->id, |
|
159 |
}, |
|
160 |
], |
|
150 | 161 |
}, "${title}: calculated data"); |
151 | 162 |
} |
152 | 163 |
|
... | ... | |
160 | 171 |
invoiceitems => [ $item1, $item2 ], |
161 | 172 |
); |
162 | 173 |
|
174 |
my $taxkey1 = $item1->part->get_taxkey(date => DateTime->today_local, is_sales => 1, taxzone => $invoice->taxzone_id); |
|
175 |
my $taxkey2 = $item2->part->get_taxkey(date => DateTime->today_local, is_sales => 1, taxzone => $invoice->taxzone_id); |
|
176 |
|
|
163 | 177 |
# item 1: |
164 | 178 |
# sellprice 2.34 * qty 2.5 = 5.85 |
165 | 179 |
# 19%(5.85) = 1.1115; rounded = 1.11 |
... | ... | |
217 | 231 |
$tax->chart_id => 1.11, |
218 | 232 |
$tax7->chart_id => 0.82, |
219 | 233 |
}, |
234 |
items => [ |
|
235 |
{ linetotal => 5.85, |
|
236 |
linetotal_cost => 4.83, |
|
237 |
sellprice => 2.34, |
|
238 |
tax_amount => 1.1115, |
|
239 |
taxkey_id => $taxkey1->id, |
|
240 |
}, |
|
241 |
{ linetotal => 11.66, |
|
242 |
linetotal_cost => 6.57, |
|
243 |
sellprice => 9.714, |
|
244 |
tax_amount => 0.8162, |
|
245 |
taxkey_id => $taxkey2->id, |
|
246 |
}, |
|
247 |
], |
|
220 | 248 |
}, "${title}: calculated data"); |
221 | 249 |
} |
222 | 250 |
|
... | ... | |
231 | 259 |
invoiceitems => [ $item1, $item2, $item3 ], |
232 | 260 |
); |
233 | 261 |
|
262 |
my %taxkeys = map { ($_->id => $_->get_taxkey(date => DateTime->today_local, is_sales => 1, taxzone => $invoice->taxzone_id)) } uniq map { $_->part } ($item1, $item2, $item3); |
|
263 |
|
|
234 | 264 |
# this is how price_tax_calculator is implemented. It differs from |
235 | 265 |
# the way sales_order / invoice - forms are calculating: |
236 | 266 |
# linetotal = sellprice 5.55 * qty 1 * (1 - 0.05) = 5.2725; rounded 5.27 |
... | ... | |
313 | 343 |
taxes => { |
314 | 344 |
$tax->chart_id => 2.9, |
315 | 345 |
}, |
346 |
items => [ |
|
347 |
{ linetotal => 5.27, |
|
348 |
linetotal_cost => 1.93, |
|
349 |
sellprice => 5.27, |
|
350 |
tax_amount => 1.0013, |
|
351 |
taxkey_id => $taxkeys{$item1->parts_id}->id, |
|
352 |
}, |
|
353 |
{ linetotal => 5.22, |
|
354 |
linetotal_cost => 1.93, |
|
355 |
sellprice => 5.22, |
|
356 |
tax_amount => 0.9918, |
|
357 |
taxkey_id => $taxkeys{$item2->parts_id}->id, |
|
358 |
}, |
|
359 |
{ linetotal => 4.75, |
|
360 |
linetotal_cost => 1.93, |
|
361 |
sellprice => 4.75, |
|
362 |
tax_amount => 0.9025, |
|
363 |
taxkey_id => $taxkeys{$item3->parts_id}->id, |
|
364 |
} |
|
365 |
], |
|
316 | 366 |
}, "${title}: calculated data"); |
317 | 367 |
} |
318 | 368 |
|
Auch abrufbar als: Unified diff
PriceTaxCalculator: für Items berechnete flüchtige Werte zurückgeben Teil 3
Die items können nicht als Hash-Referenz gespeichert werden, weil das
kaputt geht, wenn die Items noch nicht gespeichert wurden und damit noch
keine ID besitzen. Daher Umstellung auf Array-Speicherung.
Außerdem Anpassung des Testcases.