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',
65 buchungsgruppen_id => $buchungsgruppe->id,
70 push @parts, SL::DB::Part->new(
71 partnumber => 'T0815',
72 description => 'Zero EIGHT fifteeN @ 7%',
75 buchungsgruppen_id => $buchungsgruppe7->id,
80 push @parts, SL::DB::Part->new(
82 description => 'Triple 8',
85 buchungsgruppen_id => $buchungsgruppe->id,
95 return SL::DB::Invoice->new(
96 customer_id => $customer->id,
97 currency_id => $currency_id,
98 employee_id => $employee->id,
99 salesman_id => $employee->id,
100 gldate => DateTime->today_local->to_kivitendo,
101 taxzone_id => $taxzone->id,
102 transdate => DateTime->today_local->to_kivitendo,
112 my $part = delete($params{part}) || $parts[0];
114 return SL::DB::InvoiceItem->new(
115 parts_id => $part->id,
116 lastcost => $part->lastcost,
117 sellprice => $part->sellprice,
118 description => $part->description,
124 sub test_default_invoice_one_item_19_tax_not_included() {
127 my $item = new_item(qty => 2.5);
128 my $invoice = new_invoice(
130 invoiceitems => [ $item ],
133 my $taxkey = $item->part->get_taxkey(date => DateTime->today_local, is_sales => 1, taxzone => $invoice->taxzone_id);
135 # sellprice 2.34 * qty 2.5 = 5.85
136 # 19%(5.85) = 1.1115; rounded = 1.11
137 # total rounded = 6.96
139 # lastcost 1.93 * qty 2.5 = 4.825; rounded 4.83
140 # line marge_total = 1.02
141 # line marge_percent = 17.4358974358974
143 my $title = 'default invoice, one item, 19% tax not included';
144 my %data = $invoice->calculate_prices_and_taxes;
146 is($item->marge_total, 1.02, "${title}: item marge_total");
147 is($item->marge_percent, 17.4358974358974, "${title}: item marge_percent");
148 is($item->marge_price_factor, 1, "${title}: item marge_price_factor");
150 is($invoice->netamount, 5.85, "${title}: netamount");
151 is($invoice->amount, 6.96, "${title}: amount");
152 is($invoice->marge_total, 1.02, "${title}: marge_total");
153 is($invoice->marge_percent, 17.4358974358974, "${title}: marge_percent");
158 $buchungsgruppe->income_accno_id($taxzone) => {
170 $tax->chart_id => 1.11,
174 linetotal_cost => 4.83,
176 tax_amount => 1.1115,
177 taxkey_id => $taxkey->id,
181 }, "${title}: calculated data");
184 sub test_default_invoice_two_items_19_7_tax_not_included() {
187 my $item1 = new_item(qty => 2.5);
188 my $item2 = new_item(qty => 1.2, part => $parts[1]);
189 my $invoice = new_invoice(
191 invoiceitems => [ $item1, $item2 ],
194 my $taxkey1 = $item1->part->get_taxkey(date => DateTime->today_local, is_sales => 1, taxzone => $invoice->taxzone_id);
195 my $taxkey2 = $item2->part->get_taxkey(date => DateTime->today_local, is_sales => 1, taxzone => $invoice->taxzone_id);
198 # sellprice 2.34 * qty 2.5 = 5.85
199 # 19%(5.85) = 1.1115; rounded = 1.11
200 # total rounded = 6.96
202 # lastcost 1.93 * qty 2.5 = 4.825; rounded 4.83
203 # line marge_total = 1.02
204 # line marge_percent = 17.4358974358974
207 # sellprice 9.714 * qty 1.2 = 11.6568 rounded 11.66
208 # 7%(11.6568) = 0.815976; rounded = 0.82
209 # total rounded = 12.48
211 # lastcost 5.473 * qty 1.2 = 6.5676; rounded 6.57
212 # line marge_total = 5.09
213 # line marge_percent = 43.6535162950257
215 my $title = 'default invoice, two item, 19/7% tax not included';
216 my %data = $invoice->calculate_prices_and_taxes;
218 is($item1->marge_total, 1.02, "${title}: item1 marge_total");
219 is($item1->marge_percent, 17.4358974358974, "${title}: item1 marge_percent");
220 is($item1->marge_price_factor, 1, "${title}: item1 marge_price_factor");
222 is($item2->marge_total, 5.09, "${title}: item2 marge_total");
223 is($item2->marge_percent, 43.6535162950257, "${title}: item2 marge_percent");
224 is($item2->marge_price_factor, 1, "${title}: item2 marge_price_factor");
226 is($invoice->netamount, 5.85 + 11.66, "${title}: netamount");
227 is($invoice->amount, 6.96 + 12.48, "${title}: amount");
228 is($invoice->marge_total, 1.02 + 5.09, "${title}: marge_total");
229 is($invoice->marge_percent, 34.8943460879497, "${title}: marge_percent");
234 $buchungsgruppe->income_accno_id($taxzone) => {
239 $buchungsgruppe7->income_accno_id($taxzone) => {
251 $tax->chart_id => 1.11,
252 $tax7->chart_id => 0.82,
256 linetotal_cost => 4.83,
258 tax_amount => 1.1115,
259 taxkey_id => $taxkey1->id,
261 { linetotal => 11.66,
262 linetotal_cost => 6.57,
264 tax_amount => 0.8162,
265 taxkey_id => $taxkey2->id,
269 }, "${title}: calculated data");
272 sub test_default_invoice_three_items_sellprice_rounding_discount() {
275 my $item1 = new_item(qty => 1, sellprice => 5.55, discount => .05);
276 my $item2 = new_item(qty => 1, sellprice => 5.50, discount => .05);
277 my $item3 = new_item(qty => 1, sellprice => 5.00, discount => .05);
278 my $invoice = new_invoice(
280 invoiceitems => [ $item1, $item2, $item3 ],
283 my %taxkeys = map { ($_->id => $_->get_taxkey(date => DateTime->today_local, is_sales => 1, taxzone => $invoice->taxzone_id)) } uniq map { $_->part } ($item1, $item2, $item3);
285 # this is how price_tax_calculator is implemented. It differs from
286 # the way sales_order / invoice - forms are calculating:
287 # linetotal = sellprice 5.55 * qty 1 * (1 - 0.05) = 5.2725; rounded 5.27
288 # linetotal = sellprice 5.50 * qty 1 * (1 - 0.05) = 5.225 rounded 5.23
289 # linetotal = sellprice 5.00 * qty 1 * (1 - 0.05) = 4.75; rounded 4.75
293 # discount = sellprice 5.55 * discount (0.05) = 0.2775; rounded 0.28
294 # sellprice = sellprice 5.55 - discount 0.28 = 5.27; rounded 5.27
295 # linetotal = sellprice 5.27 * qty 1 = 5.27; rounded 5.27
296 # 19%(5.27) = 1.0013; rounded = 1.00
297 # total rounded = 6.27
299 # lastcost 1.93 * qty 1 = 1.93; rounded 1.93
300 # line marge_total = 3.34
301 # line marge_percent = 63.3776091081594
304 # discount = sellprice 5.50 * discount 0.05 = 0.275; rounded 0.28
305 # sellprice = sellprice 5.50 - discount 0.28 = 5.22; rounded 5.22
306 # linetotal = sellprice 5.22 * qty 1 = 5.22; rounded 5.22
307 # 19%(5.22) = 0.9918; rounded = 0.99
308 # total rounded = 6.21
310 # lastcost 1.93 * qty 1 = 1.93; rounded 1.93
311 # line marge_total = 5.22 - 1.93 = 3.29
312 # line marge_percent = 3.29/5.22 = 0.630268199233716
315 # discount = sellprice 5.00 * discount 0.25 = 0.25; rounded 0.25
316 # sellprice = sellprice 5.00 - discount 0.25 = 4.75; rounded 4.75
317 # linetotal = sellprice 4.75 * qty 1 = 4.75; rounded 4.75
318 # 19%(4.75) = 0.9025; rounded = 0.90
319 # total rounded = 5.65
321 # lastcost 1.93 * qty 1 = 1.93; rounded 1.93
322 # line marge_total = 2.82
323 # line marge_percent = 59.3684210526316
325 my $title = 'default invoice, three items, sellprice, rounding, discount';
326 my %data = $invoice->calculate_prices_and_taxes;
328 is($item1->marge_total, 3.34, "${title}: item1 marge_total");
329 is($item1->marge_percent, 63.3776091081594, "${title}: item1 marge_percent");
330 is($item1->marge_price_factor, 1, "${title}: item1 marge_price_factor");
332 is($item2->marge_total, 3.29, "${title}: item2 marge_total");
333 is($item2->marge_percent, 63.0268199233716, "${title}: item2 marge_percent");
334 is($item2->marge_price_factor, 1, "${title}: item2 marge_price_factor");
336 is($item3->marge_total, 2.82, "${title}: item3 marge_total");
337 is($item3->marge_percent, 59.3684210526316, "${title}: item3 marge_percent");
338 is($item3->marge_price_factor, 1, "${title}: item3 marge_price_factor");
340 is($invoice->netamount, 5.27 + 5.22 + 4.75, "${title}: netamount");
342 # 6.27 + 6.21 + 5.65 = 18.13
343 # 1.19*(5.27 + 5.22 + 4.75) = 18.1356; rounded 18.14
344 #is($invoice->amount, 6.27 + 6.21 + 5.65, "${title}: amount");
345 is($invoice->amount, 18.14, "${title}: amount");
347 is($invoice->marge_total, 3.34 + 3.29 + 2.82, "${title}: marge_total");
348 is($invoice->marge_percent, 62.007874015748, "${title}: marge_percent");
353 $buchungsgruppe->income_accno_id($taxzone) => {
365 $tax->chart_id => 2.9,
369 linetotal_cost => 1.93,
371 tax_amount => 1.0013,
372 taxkey_id => $taxkeys{$item1->parts_id}->id,
375 linetotal_cost => 1.93,
377 tax_amount => 0.9918,
378 taxkey_id => $taxkeys{$item2->parts_id}->id,
381 linetotal_cost => 1.93,
383 tax_amount => 0.9025,
384 taxkey_id => $taxkeys{$item3->parts_id}->id,
388 }, "${title}: calculated data");
391 sub test_default_invoice_one_item_19_tax_not_included_rounding_discount() {
394 my $item = new_item(qty => 6, part => $parts[2], discount => 0.03);
395 my $invoice = new_invoice(
397 invoiceitems => [ $item ],
400 my %taxkeys = map { ($_->id => $_->get_taxkey(date => DateTime->today_local, is_sales => 1, taxzone => $invoice->taxzone_id)) } uniq map { $_->part } ($item);
402 # PTC and ar form calculate linetotal differently:
403 # 6 parts for 0.60 with 3% discount
406 # linetotal = sellprice 0.60 * qty 6 * discount (1 - 0.03) = 3.492 rounded 3.49
407 # total = 3.49 + 0.66 = 4.15
410 # discount = sellprice 0.60 * discount (0.03) = 0.018; rounded 0.02
411 # sellprice = sellprice 0.60 - discount 0.02 = 0.58
412 # linetotal = sellprice 0.58 * qty 6 = 3.48
413 # 19%(3.48) = 0.6612; rounded = 0.66
414 # total rounded = 3.48 + 0.66 = 4.14
416 my $title = 'default invoice, one item, sellprice, rounding, discount';
417 my %data = $invoice->calculate_prices_and_taxes;
419 is($invoice->netamount, 3.48, "${title}: netamount");
421 is($invoice->amount, 4.14, "${title}: amount");
423 is($invoice->marge_total, 3.48, "${title}: marge_total");
424 is($invoice->marge_percent, 100, "${title}: marge_percent");
429 $buchungsgruppe->income_accno_id($taxzone) => {
441 $tax->chart_id => 0.66,
447 tax_amount => 0.6612,
448 taxkey_id => $taxkeys{$item->parts_id}->id,
452 }, "${title}: calculated data");
455 Support::TestSetup::login();
457 test_default_invoice_one_item_19_tax_not_included();
458 test_default_invoice_two_items_19_7_tax_not_included();
459 test_default_invoice_three_items_sellprice_rounding_discount();
460 test_default_invoice_one_item_19_tax_not_included_rounding_discount();