10 use List::MoreUtils qw(uniq);
11 use Support::TestSetup;
14 use SL::DB::Buchungsgruppe;
17 use SL::DB::DeliveryOrder;
25 my ($customer, $currency_id, @parts, $buchungsgruppe, $buchungsgruppe7, $unit, $employee, $tax, $tax7, $taxzone);
28 SL::DB::Manager::Order->delete_all(all => 1);
29 SL::DB::Manager::DeliveryOrder->delete_all(all => 1);
30 SL::DB::Manager::Invoice->delete_all(all => 1);
31 SL::DB::Manager::Part->delete_all(all => 1);
32 SL::DB::Manager::Customer->delete_all(all => 1);
38 $params{$_} ||= {} for qw(buchungsgruppe unit customer part tax);
42 $buchungsgruppe = SL::DB::Manager::Buchungsgruppe->find_by(description => 'Standard 19%', %{ $params{buchungsgruppe} }) || croak "No accounting group";
43 $buchungsgruppe7 = SL::DB::Manager::Buchungsgruppe->find_by(description => 'Standard 7%') || croak "No accounting group for 7\%";
44 $unit = SL::DB::Manager::Unit->find_by(name => 'kg', %{ $params{unit} }) || croak "No unit";
45 $employee = SL::DB::Manager::Employee->current || croak "No employee";
46 $tax = SL::DB::Manager::Tax->find_by(taxkey => 3, rate => 0.19, %{ $params{tax} }) || croak "No tax";
47 $tax7 = SL::DB::Manager::Tax->find_by(taxkey => 2, rate => 0.07) || croak "No tax for 7\%";
48 $taxzone = SL::DB::Manager::TaxZone->find_by( description => 'Inland') || croak "No taxzone";
50 $currency_id = $::instance_conf->get_currency_id;
52 $customer = SL::DB::Customer->new(
53 name => 'Test Customer',
54 currency_id => $currency_id,
55 taxzone_id => $taxzone->id,
56 %{ $params{customer} }
60 push @parts, SL::DB::Part->new(
61 partnumber => 'T4254',
62 description => 'Fourty-two fifty-four',
66 buchungsgruppen_id => $buchungsgruppe->id,
71 push @parts, SL::DB::Part->new(
72 partnumber => 'T0815',
73 description => 'Zero EIGHT fifteeN @ 7%',
77 buchungsgruppen_id => $buchungsgruppe7->id,
82 push @parts, SL::DB::Part->new(
84 description => 'Triple 8',
88 buchungsgruppen_id => $buchungsgruppe->id,
98 return SL::DB::Invoice->new(
99 customer_id => $customer->id,
100 currency_id => $currency_id,
101 employee_id => $employee->id,
102 salesman_id => $employee->id,
103 gldate => DateTime->today_local->to_kivitendo,
104 taxzone_id => $taxzone->id,
105 transdate => DateTime->today_local->to_kivitendo,
115 my $part = delete($params{part}) || $parts[0];
117 return SL::DB::InvoiceItem->new(
118 parts_id => $part->id,
119 lastcost => $part->lastcost,
120 sellprice => $part->sellprice,
121 description => $part->description,
127 sub test_default_invoice_one_item_19_tax_not_included() {
130 my $item = new_item(qty => 2.5);
131 my $invoice = new_invoice(
133 invoiceitems => [ $item ],
136 my $taxkey = $item->part->get_taxkey(date => DateTime->today_local, is_sales => 1, taxzone => $invoice->taxzone_id);
138 # sellprice 2.34 * qty 2.5 = 5.85
139 # 19%(5.85) = 1.1115; rounded = 1.11
140 # total rounded = 6.96
142 # lastcost 1.93 * qty 2.5 = 4.825; rounded 4.83
143 # line marge_total = 1.02
144 # line marge_percent = 17.4358974358974
146 my $title = 'default invoice, one item, 19% tax not included';
147 my %data = $invoice->calculate_prices_and_taxes;
149 is($item->marge_total, 1.02, "${title}: item marge_total");
150 is($item->marge_percent, 17.4358974358974, "${title}: item marge_percent");
151 is($item->marge_price_factor, 1, "${title}: item marge_price_factor");
153 is($invoice->netamount, 5.85, "${title}: netamount");
154 is($invoice->amount, 6.96, "${title}: amount");
155 is($invoice->marge_total, 1.02, "${title}: marge_total");
156 is($invoice->marge_percent, 17.4358974358974, "${title}: marge_percent");
161 $buchungsgruppe->income_accno_id($taxzone) => {
173 $tax->chart_id => 1.11,
177 linetotal_cost => 4.83,
179 tax_amount => 1.1115,
180 taxkey_id => $taxkey->id,
184 }, "${title}: calculated data");
187 sub test_default_invoice_two_items_19_7_tax_not_included() {
190 my $item1 = new_item(qty => 2.5);
191 my $item2 = new_item(qty => 1.2, part => $parts[1]);
192 my $invoice = new_invoice(
194 invoiceitems => [ $item1, $item2 ],
197 my $taxkey1 = $item1->part->get_taxkey(date => DateTime->today_local, is_sales => 1, taxzone => $invoice->taxzone_id);
198 my $taxkey2 = $item2->part->get_taxkey(date => DateTime->today_local, is_sales => 1, taxzone => $invoice->taxzone_id);
201 # sellprice 2.34 * qty 2.5 = 5.85
202 # 19%(5.85) = 1.1115; rounded = 1.11
203 # total rounded = 6.96
205 # lastcost 1.93 * qty 2.5 = 4.825; rounded 4.83
206 # line marge_total = 1.02
207 # line marge_percent = 17.4358974358974
210 # sellprice 9.714 * qty 1.2 = 11.6568 rounded 11.66
211 # 7%(11.6568) = 0.815976; rounded = 0.82
212 # total rounded = 12.48
214 # lastcost 5.473 * qty 1.2 = 6.5676; rounded 6.57
215 # line marge_total = 5.09
216 # line marge_percent = 43.6535162950257
218 my $title = 'default invoice, two item, 19/7% tax not included';
219 my %data = $invoice->calculate_prices_and_taxes;
221 is($item1->marge_total, 1.02, "${title}: item1 marge_total");
222 is($item1->marge_percent, 17.4358974358974, "${title}: item1 marge_percent");
223 is($item1->marge_price_factor, 1, "${title}: item1 marge_price_factor");
225 is($item2->marge_total, 5.09, "${title}: item2 marge_total");
226 is($item2->marge_percent, 43.6535162950257, "${title}: item2 marge_percent");
227 is($item2->marge_price_factor, 1, "${title}: item2 marge_price_factor");
229 is($invoice->netamount, 5.85 + 11.66, "${title}: netamount");
230 is($invoice->amount, 6.96 + 12.48, "${title}: amount");
231 is($invoice->marge_total, 1.02 + 5.09, "${title}: marge_total");
232 is($invoice->marge_percent, 34.8943460879497, "${title}: marge_percent");
237 $buchungsgruppe->income_accno_id($taxzone) => {
242 $buchungsgruppe7->income_accno_id($taxzone) => {
254 $tax->chart_id => 1.11,
255 $tax7->chart_id => 0.82,
259 linetotal_cost => 4.83,
261 tax_amount => 1.1115,
262 taxkey_id => $taxkey1->id,
264 { linetotal => 11.66,
265 linetotal_cost => 6.57,
267 tax_amount => 0.8162,
268 taxkey_id => $taxkey2->id,
272 }, "${title}: calculated data");
275 sub test_default_invoice_three_items_sellprice_rounding_discount() {
278 my $item1 = new_item(qty => 1, sellprice => 5.55, discount => .05);
279 my $item2 = new_item(qty => 1, sellprice => 5.50, discount => .05);
280 my $item3 = new_item(qty => 1, sellprice => 5.00, discount => .05);
281 my $invoice = new_invoice(
283 invoiceitems => [ $item1, $item2, $item3 ],
286 my %taxkeys = map { ($_->id => $_->get_taxkey(date => DateTime->today_local, is_sales => 1, taxzone => $invoice->taxzone_id)) } uniq map { $_->part } ($item1, $item2, $item3);
288 # this is how price_tax_calculator is implemented. It differs from
289 # the way sales_order / invoice - forms are calculating:
290 # linetotal = sellprice 5.55 * qty 1 * (1 - 0.05) = 5.2725; rounded 5.27
291 # linetotal = sellprice 5.50 * qty 1 * (1 - 0.05) = 5.225 rounded 5.23
292 # linetotal = sellprice 5.00 * qty 1 * (1 - 0.05) = 4.75; rounded 4.75
296 # discount = sellprice 5.55 * discount (0.05) = 0.2775; rounded 0.28
297 # sellprice = sellprice 5.55 - discount 0.28 = 5.27; rounded 5.27
298 # linetotal = sellprice 5.27 * qty 1 = 5.27; rounded 5.27
299 # 19%(5.27) = 1.0013; rounded = 1.00
300 # total rounded = 6.27
302 # lastcost 1.93 * qty 1 = 1.93; rounded 1.93
303 # line marge_total = 3.34
304 # line marge_percent = 63.3776091081594
307 # discount = sellprice 5.50 * discount 0.05 = 0.275; rounded 0.28
308 # sellprice = sellprice 5.50 - discount 0.28 = 5.22; rounded 5.22
309 # linetotal = sellprice 5.22 * qty 1 = 5.22; rounded 5.22
310 # 19%(5.22) = 0.9918; rounded = 0.99
311 # total rounded = 6.21
313 # lastcost 1.93 * qty 1 = 1.93; rounded 1.93
314 # line marge_total = 5.22 - 1.93 = 3.29
315 # line marge_percent = 3.29/5.22 = 0.630268199233716
318 # discount = sellprice 5.00 * discount 0.25 = 0.25; rounded 0.25
319 # sellprice = sellprice 5.00 - discount 0.25 = 4.75; rounded 4.75
320 # linetotal = sellprice 4.75 * qty 1 = 4.75; rounded 4.75
321 # 19%(4.75) = 0.9025; rounded = 0.90
322 # total rounded = 5.65
324 # lastcost 1.93 * qty 1 = 1.93; rounded 1.93
325 # line marge_total = 2.82
326 # line marge_percent = 59.3684210526316
328 my $title = 'default invoice, three items, sellprice, rounding, discount';
329 my %data = $invoice->calculate_prices_and_taxes;
331 is($item1->marge_total, 3.34, "${title}: item1 marge_total");
332 is($item1->marge_percent, 63.3776091081594, "${title}: item1 marge_percent");
333 is($item1->marge_price_factor, 1, "${title}: item1 marge_price_factor");
335 is($item2->marge_total, 3.29, "${title}: item2 marge_total");
336 is($item2->marge_percent, 63.0268199233716, "${title}: item2 marge_percent");
337 is($item2->marge_price_factor, 1, "${title}: item2 marge_price_factor");
339 is($item3->marge_total, 2.82, "${title}: item3 marge_total");
340 is($item3->marge_percent, 59.3684210526316, "${title}: item3 marge_percent");
341 is($item3->marge_price_factor, 1, "${title}: item3 marge_price_factor");
343 is($invoice->netamount, 5.27 + 5.22 + 4.75, "${title}: netamount");
345 # 6.27 + 6.21 + 5.65 = 18.13
346 # 1.19*(5.27 + 5.22 + 4.75) = 18.1356; rounded 18.14
347 #is($invoice->amount, 6.27 + 6.21 + 5.65, "${title}: amount");
348 is($invoice->amount, 18.14, "${title}: amount");
350 is($invoice->marge_total, 3.34 + 3.29 + 2.82, "${title}: marge_total");
351 is($invoice->marge_percent, 62.007874015748, "${title}: marge_percent");
356 $buchungsgruppe->income_accno_id($taxzone) => {
368 $tax->chart_id => 2.9,
372 linetotal_cost => 1.93,
374 tax_amount => 1.0013,
375 taxkey_id => $taxkeys{$item1->parts_id}->id,
378 linetotal_cost => 1.93,
380 tax_amount => 0.9918,
381 taxkey_id => $taxkeys{$item2->parts_id}->id,
384 linetotal_cost => 1.93,
386 tax_amount => 0.9025,
387 taxkey_id => $taxkeys{$item3->parts_id}->id,
391 }, "${title}: calculated data");
394 sub test_default_invoice_one_item_19_tax_not_included_rounding_discount() {
397 my $item = new_item(qty => 6, part => $parts[2], discount => 0.03);
398 my $invoice = new_invoice(
400 invoiceitems => [ $item ],
403 my %taxkeys = map { ($_->id => $_->get_taxkey(date => DateTime->today_local, is_sales => 1, taxzone => $invoice->taxzone_id)) } uniq map { $_->part } ($item);
405 # PTC and ar form calculate linetotal differently:
406 # 6 parts for 0.60 with 3% discount
409 # linetotal = sellprice 0.60 * qty 6 * discount (1 - 0.03) = 3.492 rounded 3.49
410 # total = 3.49 + 0.66 = 4.15
413 # discount = sellprice 0.60 * discount (0.03) = 0.018; rounded 0.02
414 # sellprice = sellprice 0.60 - discount 0.02 = 0.58
415 # linetotal = sellprice 0.58 * qty 6 = 3.48
416 # 19%(3.48) = 0.6612; rounded = 0.66
417 # total rounded = 3.48 + 0.66 = 4.14
419 my $title = 'default invoice, one item, sellprice, rounding, discount';
420 my %data = $invoice->calculate_prices_and_taxes;
422 is($invoice->netamount, 3.48, "${title}: netamount");
424 is($invoice->amount, 4.14, "${title}: amount");
426 is($invoice->marge_total, 3.48, "${title}: marge_total");
427 is($invoice->marge_percent, 100, "${title}: marge_percent");
432 $buchungsgruppe->income_accno_id($taxzone) => {
444 $tax->chart_id => 0.66,
450 tax_amount => 0.6612,
451 taxkey_id => $taxkeys{$item->parts_id}->id,
455 }, "${title}: calculated data");
458 Support::TestSetup::login();
460 test_default_invoice_one_item_19_tax_not_included();
461 test_default_invoice_two_items_19_7_tax_not_included();
462 test_default_invoice_three_items_sellprice_rounding_discount();
463 test_default_invoice_one_item_19_tax_not_included_rounding_discount();