10 use List::MoreUtils qw(uniq);
11 use Support::TestSetup;
14 use SL::DB::Buchungsgruppe;
23 my ($customer, $currency_id, @parts, $buchungsgruppe, $buchungsgruppe7, $unit, $employee, $tax, $tax7, $taxzone);
28 $params{$_} ||= {} for qw(buchungsgruppe unit customer part tax);
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);
34 $buchungsgruppe = SL::DB::Manager::Buchungsgruppe->find_by(description => 'Standard 19%', %{ $params{buchungsgruppe} }) || croak "No accounting group";
35 $buchungsgruppe7 = SL::DB::Manager::Buchungsgruppe->find_by(description => 'Standard 7%') || croak "No accounting group for 7\%";
36 $unit = SL::DB::Manager::Unit->find_by(name => 'kg', %{ $params{unit} }) || croak "No unit";
37 $employee = SL::DB::Manager::Employee->current || croak "No employee";
38 $tax = SL::DB::Manager::Tax->find_by(taxkey => 3, rate => 0.19, %{ $params{tax} }) || croak "No tax";
39 $tax7 = SL::DB::Manager::Tax->find_by(taxkey => 2, rate => 0.07) || croak "No tax for 7\%";
40 $taxzone = SL::DB::Manager::TaxZone->find_by( description => 'Inland') || croak "No taxzone";
42 $currency_id = $::instance_conf->get_currency_id;
44 $customer = SL::DB::Customer->new(
45 name => 'Test Customer',
46 currency_id => $currency_id,
47 taxzone_id => $taxzone->id,
48 %{ $params{customer} }
52 push @parts, SL::DB::Part->new(
53 partnumber => 'T4254',
54 description => 'Fourty-two fifty-four',
57 buchungsgruppen_id => $buchungsgruppe->id,
62 push @parts, SL::DB::Part->new(
63 partnumber => 'T0815',
64 description => 'Zero EIGHT fifteeN @ 7%',
67 buchungsgruppen_id => $buchungsgruppe7->id,
76 return SL::DB::Invoice->new(
77 customer_id => $customer->id,
78 currency_id => $currency_id,
79 employee_id => $employee->id,
80 salesman_id => $employee->id,
81 gldate => DateTime->today_local->to_kivitendo,
82 taxzone_id => $taxzone->id,
83 transdate => DateTime->today_local->to_kivitendo,
93 my $part = delete($params{part}) || $parts[0];
95 return SL::DB::InvoiceItem->new(
96 parts_id => $part->id,
97 lastcost => $part->lastcost,
98 sellprice => $part->sellprice,
99 description => $part->description,
105 sub test_default_invoice_one_item_19_tax_not_included() {
108 my $item = new_item(qty => 2.5);
109 my $invoice = new_invoice(
111 invoiceitems => [ $item ],
114 my $taxkey = $item->part->get_taxkey(date => DateTime->today_local, is_sales => 1, taxzone => $invoice->taxzone_id);
116 # sellprice 2.34 * qty 2.5 = 5.85
117 # 19%(5.85) = 1.1115; rounded = 1.11
118 # total rounded = 6.96
120 # lastcost 1.93 * qty 2.5 = 4.825; rounded 4.83
121 # line marge_total = 1.02
122 # line marge_percent = 17.4358974358974
124 my $title = 'default invoice, one item, 19% tax not included';
125 my %data = $invoice->calculate_prices_and_taxes;
127 is($item->marge_total, 1.02, "${title}: item marge_total");
128 is($item->marge_percent, 17.4358974358974, "${title}: item marge_percent");
129 is($item->marge_price_factor, 1, "${title}: item marge_price_factor");
131 is($invoice->netamount, 5.85, "${title}: netamount");
132 is($invoice->amount, 6.96, "${title}: amount");
133 is($invoice->marge_total, 1.02, "${title}: marge_total");
134 is($invoice->marge_percent, 17.4358974358974, "${title}: marge_percent");
139 $buchungsgruppe->income_accno_id($taxzone) => {
151 $tax->chart_id => 1.11,
155 linetotal_cost => 4.83,
157 tax_amount => 1.1115,
158 taxkey_id => $taxkey->id,
161 }, "${title}: calculated data");
164 sub test_default_invoice_two_items_19_7_tax_not_included() {
167 my $item1 = new_item(qty => 2.5);
168 my $item2 = new_item(qty => 1.2, part => $parts[1]);
169 my $invoice = new_invoice(
171 invoiceitems => [ $item1, $item2 ],
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);
178 # sellprice 2.34 * qty 2.5 = 5.85
179 # 19%(5.85) = 1.1115; rounded = 1.11
180 # total rounded = 6.96
182 # lastcost 1.93 * qty 2.5 = 4.825; rounded 4.83
183 # line marge_total = 1.02
184 # line marge_percent = 17.4358974358974
187 # sellprice 9.714 * qty 1.2 = 11.6568 rounded 11.66
188 # 7%(11.6568) = 0.815976; rounded = 0.82
189 # total rounded = 12.48
191 # lastcost 5.473 * qty 1.2 = 6.5676; rounded 6.57
192 # line marge_total = 5.09
193 # line marge_percent = 43.6535162950257
195 my $title = 'default invoice, two item, 19/7% tax not included';
196 my %data = $invoice->calculate_prices_and_taxes;
198 is($item1->marge_total, 1.02, "${title}: item1 marge_total");
199 is($item1->marge_percent, 17.4358974358974, "${title}: item1 marge_percent");
200 is($item1->marge_price_factor, 1, "${title}: item1 marge_price_factor");
202 is($item2->marge_total, 5.09, "${title}: item2 marge_total");
203 is($item2->marge_percent, 43.6535162950257, "${title}: item2 marge_percent");
204 is($item2->marge_price_factor, 1, "${title}: item2 marge_price_factor");
206 is($invoice->netamount, 5.85 + 11.66, "${title}: netamount");
207 is($invoice->amount, 6.96 + 12.48, "${title}: amount");
208 is($invoice->marge_total, 1.02 + 5.09, "${title}: marge_total");
209 is($invoice->marge_percent, 34.8943460879497, "${title}: marge_percent");
214 $buchungsgruppe->income_accno_id($taxzone) => {
219 $buchungsgruppe7->income_accno_id($taxzone) => {
231 $tax->chart_id => 1.11,
232 $tax7->chart_id => 0.82,
236 linetotal_cost => 4.83,
238 tax_amount => 1.1115,
239 taxkey_id => $taxkey1->id,
241 { linetotal => 11.66,
242 linetotal_cost => 6.57,
244 tax_amount => 0.8162,
245 taxkey_id => $taxkey2->id,
248 }, "${title}: calculated data");
251 sub test_default_invoice_three_items_sellprice_rounding_discount() {
254 my $item1 = new_item(qty => 1, sellprice => 5.55, discount => .05);
255 my $item2 = new_item(qty => 1, sellprice => 5.50, discount => .05);
256 my $item3 = new_item(qty => 1, sellprice => 5.00, discount => .05);
257 my $invoice = new_invoice(
259 invoiceitems => [ $item1, $item2, $item3 ],
262 my %taxkeys = map { ($_->id => $_->get_taxkey(date => DateTime->today_local, is_sales => 1, taxzone => $invoice->taxzone_id)) } uniq map { $_->part } ($item1, $item2, $item3);
264 # this is how price_tax_calculator is implemented. It differs from
265 # the way sales_order / invoice - forms are calculating:
266 # linetotal = sellprice 5.55 * qty 1 * (1 - 0.05) = 5.2725; rounded 5.27
267 # linetotal = sellprice 5.50 * qty 1 * (1 - 0.05) = 5.225 rounded 5.23
268 # linetotal = sellprice 5.00 * qty 1 * (1 - 0.05) = 4.75; rounded 4.75
272 # discount = sellprice 5.55 * discount (0.05) = 0.2775; rounded 0.28
273 # sellprice = sellprice 5.55 - discount 0.28 = 5.27; rounded 5.27
274 # linetotal = sellprice 5.27 * qty 1 = 5.27; rounded 5.27
275 # 19%(5.27) = 1.0013; rounded = 1.00
276 # total rounded = 6.27
278 # lastcost 1.93 * qty 1 = 1.93; rounded 1.93
279 # line marge_total = 3.34
280 # line marge_percent = 63.3776091081594
283 # discount = sellprice 5.50 * discount 0.05 = 0.275; rounded 0.28
284 # sellprice = sellprice 5.50 - discount 0.28 = 5.22; rounded 5.22
285 # linetotal = sellprice 5.22 * qty 1 = 5.22; rounded 5.22
286 # 19%(5.22) = 0.9918; rounded = 0.99
287 # total rounded = 6.21
289 # lastcost 1.93 * qty 1 = 1.93; rounded 1.93
290 # line marge_total = 5.22 - 1.93 = 3.29
291 # line marge_percent = 3.29/5.22 = 0.630268199233716
294 # discount = sellprice 5.00 * discount 0.25 = 0.25; rounded 0.25
295 # sellprice = sellprice 5.00 - discount 0.25 = 4.75; rounded 4.75
296 # linetotal = sellprice 4.75 * qty 1 = 4.75; rounded 4.75
297 # 19%(4.75) = 0.9025; rounded = 0.90
298 # total rounded = 5.65
300 # lastcost 1.93 * qty 1 = 1.93; rounded 1.93
301 # line marge_total = 2.82
302 # line marge_percent = 59.3684210526316
304 my $title = 'default invoice, three items, sellprice, rounding, discount';
305 my %data = $invoice->calculate_prices_and_taxes;
307 is($item1->marge_total, 3.34, "${title}: item1 marge_total");
308 is($item1->marge_percent, 63.3776091081594, "${title}: item1 marge_percent");
309 is($item1->marge_price_factor, 1, "${title}: item1 marge_price_factor");
311 is($item2->marge_total, 3.29, "${title}: item2 marge_total");
312 is($item2->marge_percent, 63.0268199233716, "${title}: item2 marge_percent");
313 is($item2->marge_price_factor, 1, "${title}: item2 marge_price_factor");
315 is($item3->marge_total, 2.82, "${title}: item3 marge_total");
316 is($item3->marge_percent, 59.3684210526316, "${title}: item3 marge_percent");
317 is($item3->marge_price_factor, 1, "${title}: item3 marge_price_factor");
319 is($invoice->netamount, 5.27 + 5.22 + 4.75, "${title}: netamount");
321 # 6.27 + 6.21 + 5.65 = 18.13
322 # 1.19*(5.27 + 5.22 + 4.75) = 18.1356; rounded 18.14
323 #is($invoice->amount, 6.27 + 6.21 + 5.65, "${title}: amount");
324 is($invoice->amount, 18.14, "${title}: amount");
326 is($invoice->marge_total, 3.34 + 3.29 + 2.82, "${title}: marge_total");
327 is($invoice->marge_percent, 62.007874015748, "${title}: marge_percent");
332 $buchungsgruppe->income_accno_id($taxzone) => {
344 $tax->chart_id => 2.9,
348 linetotal_cost => 1.93,
350 tax_amount => 1.0013,
351 taxkey_id => $taxkeys{$item1->parts_id}->id,
354 linetotal_cost => 1.93,
356 tax_amount => 0.9918,
357 taxkey_id => $taxkeys{$item2->parts_id}->id,
360 linetotal_cost => 1.93,
362 tax_amount => 0.9025,
363 taxkey_id => $taxkeys{$item3->parts_id}->id,
366 }, "${title}: calculated data");
369 Support::TestSetup::login();
371 test_default_invoice_one_item_19_tax_not_included();
372 test_default_invoice_two_items_19_7_tax_not_included();
373 test_default_invoice_three_items_sellprice_rounding_discount();