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,
84 return SL::DB::Invoice->new(
85 customer_id => $customer->id,
86 currency_id => $currency_id,
87 employee_id => $employee->id,
88 salesman_id => $employee->id,
89 gldate => DateTime->today_local->to_kivitendo,
90 taxzone_id => $taxzone->id,
91 transdate => DateTime->today_local->to_kivitendo,
101 my $part = delete($params{part}) || $parts[0];
103 return SL::DB::InvoiceItem->new(
104 parts_id => $part->id,
105 lastcost => $part->lastcost,
106 sellprice => $part->sellprice,
107 description => $part->description,
113 sub test_default_invoice_one_item_19_tax_not_included() {
116 my $item = new_item(qty => 2.5);
117 my $invoice = new_invoice(
119 invoiceitems => [ $item ],
122 my $taxkey = $item->part->get_taxkey(date => DateTime->today_local, is_sales => 1, taxzone => $invoice->taxzone_id);
124 # sellprice 2.34 * qty 2.5 = 5.85
125 # 19%(5.85) = 1.1115; rounded = 1.11
126 # total rounded = 6.96
128 # lastcost 1.93 * qty 2.5 = 4.825; rounded 4.83
129 # line marge_total = 1.02
130 # line marge_percent = 17.4358974358974
132 my $title = 'default invoice, one item, 19% tax not included';
133 my %data = $invoice->calculate_prices_and_taxes;
135 is($item->marge_total, 1.02, "${title}: item marge_total");
136 is($item->marge_percent, 17.4358974358974, "${title}: item marge_percent");
137 is($item->marge_price_factor, 1, "${title}: item marge_price_factor");
139 is($invoice->netamount, 5.85, "${title}: netamount");
140 is($invoice->amount, 6.96, "${title}: amount");
141 is($invoice->marge_total, 1.02, "${title}: marge_total");
142 is($invoice->marge_percent, 17.4358974358974, "${title}: marge_percent");
147 $buchungsgruppe->income_accno_id($taxzone) => {
159 $tax->chart_id => 1.11,
163 linetotal_cost => 4.83,
165 tax_amount => 1.1115,
166 taxkey_id => $taxkey->id,
169 }, "${title}: calculated data");
172 sub test_default_invoice_two_items_19_7_tax_not_included() {
175 my $item1 = new_item(qty => 2.5);
176 my $item2 = new_item(qty => 1.2, part => $parts[1]);
177 my $invoice = new_invoice(
179 invoiceitems => [ $item1, $item2 ],
182 my $taxkey1 = $item1->part->get_taxkey(date => DateTime->today_local, is_sales => 1, taxzone => $invoice->taxzone_id);
183 my $taxkey2 = $item2->part->get_taxkey(date => DateTime->today_local, is_sales => 1, taxzone => $invoice->taxzone_id);
186 # sellprice 2.34 * qty 2.5 = 5.85
187 # 19%(5.85) = 1.1115; rounded = 1.11
188 # total rounded = 6.96
190 # lastcost 1.93 * qty 2.5 = 4.825; rounded 4.83
191 # line marge_total = 1.02
192 # line marge_percent = 17.4358974358974
195 # sellprice 9.714 * qty 1.2 = 11.6568 rounded 11.66
196 # 7%(11.6568) = 0.815976; rounded = 0.82
197 # total rounded = 12.48
199 # lastcost 5.473 * qty 1.2 = 6.5676; rounded 6.57
200 # line marge_total = 5.09
201 # line marge_percent = 43.6535162950257
203 my $title = 'default invoice, two item, 19/7% tax not included';
204 my %data = $invoice->calculate_prices_and_taxes;
206 is($item1->marge_total, 1.02, "${title}: item1 marge_total");
207 is($item1->marge_percent, 17.4358974358974, "${title}: item1 marge_percent");
208 is($item1->marge_price_factor, 1, "${title}: item1 marge_price_factor");
210 is($item2->marge_total, 5.09, "${title}: item2 marge_total");
211 is($item2->marge_percent, 43.6535162950257, "${title}: item2 marge_percent");
212 is($item2->marge_price_factor, 1, "${title}: item2 marge_price_factor");
214 is($invoice->netamount, 5.85 + 11.66, "${title}: netamount");
215 is($invoice->amount, 6.96 + 12.48, "${title}: amount");
216 is($invoice->marge_total, 1.02 + 5.09, "${title}: marge_total");
217 is($invoice->marge_percent, 34.8943460879497, "${title}: marge_percent");
222 $buchungsgruppe->income_accno_id($taxzone) => {
227 $buchungsgruppe7->income_accno_id($taxzone) => {
239 $tax->chart_id => 1.11,
240 $tax7->chart_id => 0.82,
244 linetotal_cost => 4.83,
246 tax_amount => 1.1115,
247 taxkey_id => $taxkey1->id,
249 { linetotal => 11.66,
250 linetotal_cost => 6.57,
252 tax_amount => 0.8162,
253 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,
374 }, "${title}: calculated data");
377 Support::TestSetup::login();
379 test_default_invoice_one_item_19_tax_not_included();
380 test_default_invoice_two_items_19_7_tax_not_included();
381 test_default_invoice_three_items_sellprice_rounding_discount();