10 use List::MoreUtils qw(uniq);
11 use Support::TestSetup;
15 use SL::DB::Buchungsgruppe;
18 use SL::DB::DeliveryOrder;
26 my ($customer, @parts, $buchungsgruppe, $buchungsgruppe7, $unit, $employee, $tax, $tax7, $taxzone);
29 SL::DB::Manager::Order->delete_all(all => 1);
30 SL::DB::Manager::DeliveryOrder->delete_all(all => 1);
31 SL::DB::Manager::InvoiceItem->delete_all(all => 1);
32 SL::DB::Manager::Invoice->delete_all(all => 1);
33 SL::DB::Manager::Part->delete_all(all => 1);
34 SL::DB::Manager::Customer->delete_all(all => 1);
40 $params{$_} ||= {} for qw(buchungsgruppe unit customer part tax);
44 $buchungsgruppe = SL::DB::Manager::Buchungsgruppe->find_by(description => 'Standard 19%', %{ $params{buchungsgruppe} }) || croak "No accounting group";
45 $buchungsgruppe7 = SL::DB::Manager::Buchungsgruppe->find_by(description => 'Standard 7%') || croak "No accounting group for 7\%";
46 $unit = SL::DB::Manager::Unit->find_by(name => 'kg', %{ $params{unit} }) || croak "No unit";
47 $employee = SL::DB::Manager::Employee->current || croak "No employee";
48 $tax = SL::DB::Manager::Tax->find_by(taxkey => 3, rate => 0.19, %{ $params{tax} }) || croak "No tax";
49 $tax7 = SL::DB::Manager::Tax->find_by(taxkey => 2, rate => 0.07) || croak "No tax for 7\%";
50 $taxzone = SL::DB::Manager::TaxZone->find_by( description => 'Inland') || croak "No taxzone";
52 $customer = SL::Dev::CustomerVendor::create_customer(
53 name => 'Test Customer',
54 taxzone_id => $taxzone->id,
55 %{ $params{customer} }
59 push @parts, SL::Dev::Part::create_part(
60 partnumber => 'T4254',
61 description => 'Fourty-two fifty-four',
64 buchungsgruppen_id => $buchungsgruppe->id,
69 push @parts, SL::Dev::Part::create_part(
70 partnumber => 'T0815',
71 description => 'Zero EIGHT fifteeN @ 7%',
74 buchungsgruppen_id => $buchungsgruppe7->id,
79 push @parts, SL::Dev::Part::create_part(
81 description => 'Triple 8',
84 buchungsgruppen_id => $buchungsgruppe->id,
94 return SL::Dev::Record::create_sales_invoice(
95 taxzone_id => $taxzone->id,
103 my $part = delete($params{part}) || $parts[0];
105 return SL::Dev::Record::create_invoice_item(
111 sub test_default_invoice_one_item_19_tax_not_included() {
114 my $item = new_item(qty => 2.5);
115 my $invoice = new_invoice(
117 invoiceitems => [ $item ],
120 my $taxkey = $item->part->get_taxkey(date => DateTime->today_local, is_sales => 1, taxzone => $invoice->taxzone_id);
122 # sellprice 2.34 * qty 2.5 = 5.85
123 # 19%(5.85) = 1.1115; rounded = 1.11
124 # total rounded = 6.96
126 # lastcost 1.93 * qty 2.5 = 4.825; rounded 4.83
127 # line marge_total = 1.02
128 # line marge_percent = 17.4358974358974
130 my $title = 'default invoice, one item, 19% tax not included';
131 my %data = $invoice->calculate_prices_and_taxes;
133 is($item->marge_total, 1.02, "${title}: item marge_total");
134 is($item->marge_percent, 17.4358974358974, "${title}: item marge_percent");
135 is($item->marge_price_factor, 1, "${title}: item marge_price_factor");
137 is($invoice->netamount, 5.85, "${title}: netamount");
138 is($invoice->amount, 6.96, "${title}: amount");
139 is($invoice->marge_total, 1.02, "${title}: marge_total");
140 is($invoice->marge_percent, 17.4358974358974, "${title}: marge_percent");
145 $buchungsgruppe->income_accno_id($taxzone) => {
157 $tax->chart_id => 1.11,
161 linetotal_cost => 4.83,
163 tax_amount => 1.1115,
164 taxkey_id => $taxkey->id,
168 }, "${title}: calculated data");
171 sub test_default_invoice_two_items_19_7_tax_not_included() {
174 my $item1 = new_item(qty => 2.5);
175 my $item2 = new_item(qty => 1.2, part => $parts[1]);
176 my $invoice = new_invoice(
178 invoiceitems => [ $item1, $item2 ],
181 my $taxkey1 = $item1->part->get_taxkey(date => DateTime->today_local, is_sales => 1, taxzone => $invoice->taxzone_id);
182 my $taxkey2 = $item2->part->get_taxkey(date => DateTime->today_local, is_sales => 1, taxzone => $invoice->taxzone_id);
185 # sellprice 2.34 * qty 2.5 = 5.85
186 # 19%(5.85) = 1.1115; rounded = 1.11
187 # total rounded = 6.96
189 # lastcost 1.93 * qty 2.5 = 4.825; rounded 4.83
190 # line marge_total = 1.02
191 # line marge_percent = 17.4358974358974
194 # sellprice 9.714 * qty 1.2 = 11.6568 rounded 11.66
195 # 7%(11.6568) = 0.815976; rounded = 0.82
196 # total rounded = 12.48
198 # lastcost 5.473 * qty 1.2 = 6.5676; rounded 6.57
199 # line marge_total = 5.09
200 # line marge_percent = 43.6535162950257
202 my $title = 'default invoice, two item, 19/7% tax not included';
203 my %data = $invoice->calculate_prices_and_taxes;
205 is($item1->marge_total, 1.02, "${title}: item1 marge_total");
206 is($item1->marge_percent, 17.4358974358974, "${title}: item1 marge_percent");
207 is($item1->marge_price_factor, 1, "${title}: item1 marge_price_factor");
209 is($item2->marge_total, 5.09, "${title}: item2 marge_total");
210 is($item2->marge_percent, 43.6535162950257, "${title}: item2 marge_percent");
211 is($item2->marge_price_factor, 1, "${title}: item2 marge_price_factor");
213 is($invoice->netamount, 5.85 + 11.66, "${title}: netamount");
214 is($invoice->amount, 6.96 + 12.48, "${title}: amount");
215 is($invoice->marge_total, 1.02 + 5.09, "${title}: marge_total");
216 is($invoice->marge_percent, 34.8943460879497, "${title}: marge_percent");
221 $buchungsgruppe->income_accno_id($taxzone) => {
226 $buchungsgruppe7->income_accno_id($taxzone) => {
238 $tax->chart_id => 1.11,
239 $tax7->chart_id => 0.82,
243 linetotal_cost => 4.83,
245 tax_amount => 1.1115,
246 taxkey_id => $taxkey1->id,
248 { linetotal => 11.66,
249 linetotal_cost => 6.57,
251 tax_amount => 0.8162,
252 taxkey_id => $taxkey2->id,
256 }, "${title}: calculated data");
259 sub test_default_invoice_three_items_sellprice_rounding_discount() {
262 my $item1 = new_item(qty => 1, sellprice => 5.55, discount => .05);
263 my $item2 = new_item(qty => 1, sellprice => 5.50, discount => .05);
264 my $item3 = new_item(qty => 1, sellprice => 5.00, discount => .05);
265 my $invoice = new_invoice(
267 invoiceitems => [ $item1, $item2, $item3 ],
270 my %taxkeys = map { ($_->id => $_->get_taxkey(date => DateTime->today_local, is_sales => 1, taxzone => $invoice->taxzone_id)) } uniq map { $_->part } ($item1, $item2, $item3);
272 # this is how price_tax_calculator is implemented. It differs from
273 # the way sales_order / invoice - forms are calculating:
274 # linetotal = sellprice 5.55 * qty 1 * (1 - 0.05) = 5.2725; rounded 5.27
275 # linetotal = sellprice 5.50 * qty 1 * (1 - 0.05) = 5.225 rounded 5.23
276 # linetotal = sellprice 5.00 * qty 1 * (1 - 0.05) = 4.75; rounded 4.75
280 # discount = sellprice 5.55 * discount (0.05) = 0.2775; rounded 0.28
281 # sellprice = sellprice 5.55 - discount 0.28 = 5.27; rounded 5.27
282 # linetotal = sellprice 5.27 * qty 1 = 5.27; rounded 5.27
283 # 19%(5.27) = 1.0013; rounded = 1.00
284 # total rounded = 6.27
286 # lastcost 1.93 * qty 1 = 1.93; rounded 1.93
287 # line marge_total = 3.34
288 # line marge_percent = 63.3776091081594
291 # discount = sellprice 5.50 * discount 0.05 = 0.275; rounded 0.28
292 # sellprice = sellprice 5.50 - discount 0.28 = 5.22; rounded 5.22
293 # linetotal = sellprice 5.22 * qty 1 = 5.22; rounded 5.22
294 # 19%(5.22) = 0.9918; rounded = 0.99
295 # total rounded = 6.21
297 # lastcost 1.93 * qty 1 = 1.93; rounded 1.93
298 # line marge_total = 5.22 - 1.93 = 3.29
299 # line marge_percent = 3.29/5.22 = 0.630268199233716
302 # discount = sellprice 5.00 * discount 0.25 = 0.25; rounded 0.25
303 # sellprice = sellprice 5.00 - discount 0.25 = 4.75; rounded 4.75
304 # linetotal = sellprice 4.75 * qty 1 = 4.75; rounded 4.75
305 # 19%(4.75) = 0.9025; rounded = 0.90
306 # total rounded = 5.65
308 # lastcost 1.93 * qty 1 = 1.93; rounded 1.93
309 # line marge_total = 2.82
310 # line marge_percent = 59.3684210526316
312 my $title = 'default invoice, three items, sellprice, rounding, discount';
313 my %data = $invoice->calculate_prices_and_taxes;
315 is($item1->marge_total, 3.34, "${title}: item1 marge_total");
316 is($item1->marge_percent, 63.3776091081594, "${title}: item1 marge_percent");
317 is($item1->marge_price_factor, 1, "${title}: item1 marge_price_factor");
319 is($item2->marge_total, 3.29, "${title}: item2 marge_total");
320 is($item2->marge_percent, 63.0268199233716, "${title}: item2 marge_percent");
321 is($item2->marge_price_factor, 1, "${title}: item2 marge_price_factor");
323 is($item3->marge_total, 2.82, "${title}: item3 marge_total");
324 is($item3->marge_percent, 59.3684210526316, "${title}: item3 marge_percent");
325 is($item3->marge_price_factor, 1, "${title}: item3 marge_price_factor");
327 is($invoice->netamount, 5.27 + 5.22 + 4.75, "${title}: netamount");
329 # 6.27 + 6.21 + 5.65 = 18.13
330 # 1.19*(5.27 + 5.22 + 4.75) = 18.1356; rounded 18.14
331 #is($invoice->amount, 6.27 + 6.21 + 5.65, "${title}: amount");
332 is($invoice->amount, 18.14, "${title}: amount");
334 is($invoice->marge_total, 3.34 + 3.29 + 2.82, "${title}: marge_total");
335 is($invoice->marge_percent, 62.007874015748, "${title}: marge_percent");
340 $buchungsgruppe->income_accno_id($taxzone) => {
352 $tax->chart_id => 2.9,
356 linetotal_cost => 1.93,
358 tax_amount => 1.0013,
359 taxkey_id => $taxkeys{$item1->parts_id}->id,
362 linetotal_cost => 1.93,
364 tax_amount => 0.9918,
365 taxkey_id => $taxkeys{$item2->parts_id}->id,
368 linetotal_cost => 1.93,
370 tax_amount => 0.9025,
371 taxkey_id => $taxkeys{$item3->parts_id}->id,
375 }, "${title}: calculated data");
378 sub test_default_invoice_one_item_19_tax_not_included_rounding_discount() {
381 my $item = new_item(qty => 6, part => $parts[2], discount => 0.03);
382 my $invoice = new_invoice(
384 invoiceitems => [ $item ],
387 my %taxkeys = map { ($_->id => $_->get_taxkey(date => DateTime->today_local, is_sales => 1, taxzone => $invoice->taxzone_id)) } uniq map { $_->part } ($item);
389 # PTC and ar form calculate linetotal differently:
390 # 6 parts for 0.60 with 3% discount
393 # linetotal = sellprice 0.60 * qty 6 * discount (1 - 0.03) = 3.492 rounded 3.49
394 # total = 3.49 + 0.66 = 4.15
397 # discount = sellprice 0.60 * discount (0.03) = 0.018; rounded 0.02
398 # sellprice = sellprice 0.60 - discount 0.02 = 0.58
399 # linetotal = sellprice 0.58 * qty 6 = 3.48
400 # 19%(3.48) = 0.6612; rounded = 0.66
401 # total rounded = 3.48 + 0.66 = 4.14
403 my $title = 'default invoice, one item, sellprice, rounding, discount';
404 my %data = $invoice->calculate_prices_and_taxes;
406 is($invoice->netamount, 3.48, "${title}: netamount");
408 is($invoice->amount, 4.14, "${title}: amount");
410 is($invoice->marge_total, 3.48, "${title}: marge_total");
411 is($invoice->marge_percent, 100, "${title}: marge_percent");
416 $buchungsgruppe->income_accno_id($taxzone) => {
428 $tax->chart_id => 0.66,
434 tax_amount => 0.6612,
435 taxkey_id => $taxkeys{$item->parts_id}->id,
439 }, "${title}: calculated data");
442 Support::TestSetup::login();
444 test_default_invoice_one_item_19_tax_not_included();
445 test_default_invoice_two_items_19_7_tax_not_included();
446 test_default_invoice_three_items_sellprice_rounding_discount();
447 test_default_invoice_one_item_19_tax_not_included_rounding_discount();