10 use List::MoreUtils qw(uniq);
11 use Support::TestSetup;
14 use SL::DB::Buchungsgruppe;
17 use SL::DB::DeliveryOrder;
26 my ($customer, $currency_id, @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::Invoice->delete_all(all => 1);
32 SL::DB::Manager::Part->delete_all(all => 1);
33 SL::DB::Manager::Customer->delete_all(all => 1);
39 $params{$_} ||= {} for qw(buchungsgruppe unit customer part tax);
43 $default_manager = $::lx_office_conf{system}->{default_manager};
44 if ($default_manager eq "swiss") {
45 $buchungsgruppe = SL::DB::Manager::Buchungsgruppe->find_by(description => 'Standard 8%', %{ $params{buchungsgruppe} }) || croak "No accounting group for 8\%";
46 $buchungsgruppe7 = SL::DB::Manager::Buchungsgruppe->find_by(description => 'Standard 2.5%', %{ $params{buchungsgruppe} })|| croak "No accounting group for 2.5\%";
47 $unit = SL::DB::Manager::Unit->find_by(name => 'kg', %{ $params{unit} }) || croak "No unit";
48 $employee = SL::DB::Manager::Employee->current || croak "No employee";
49 $tax = SL::DB::Manager::Tax->find_by(taxkey => 2, rate => 0.08, %{ $params{tax} }) || croak "No tax for 8\%";
50 $tax7 = SL::DB::Manager::Tax->find_by(taxkey => 3, rate => 0.025) || croak "No tax for 2.5\%";
51 $taxzone = SL::DB::Manager::TaxZone->find_by( description => 'Schweiz') || croak "No taxzone";
53 $buchungsgruppe = SL::DB::Manager::Buchungsgruppe->find_by(description => 'Standard 19%', %{ $params{buchungsgruppe} }) || croak "No accounting group";
54 $buchungsgruppe7 = SL::DB::Manager::Buchungsgruppe->find_by(description => 'Standard 7%') || croak "No accounting group for 7\%";
55 $unit = SL::DB::Manager::Unit->find_by(name => 'kg', %{ $params{unit} }) || croak "No unit";
56 $employee = SL::DB::Manager::Employee->current || croak "No employee";
57 $tax = SL::DB::Manager::Tax->find_by(taxkey => 3, rate => 0.19, %{ $params{tax} }) || croak "No tax";
58 $tax7 = SL::DB::Manager::Tax->find_by(taxkey => 2, rate => 0.07) || croak "No tax for 7\%";
59 $taxzone = SL::DB::Manager::TaxZone->find_by( description => 'Inland') || croak "No taxzone";
62 $currency_id = $::instance_conf->get_currency_id;
64 $customer = SL::DB::Customer->new(
65 name => 'Test Customer',
66 currency_id => $currency_id,
67 taxzone_id => $taxzone->id,
68 %{ $params{customer} }
72 push @parts, SL::DB::Part->new(
73 partnumber => 'T4254',
74 description => 'Fourty-two fifty-four',
77 buchungsgruppen_id => $buchungsgruppe->id,
82 push @parts, SL::DB::Part->new(
83 partnumber => 'T0815',
84 description => 'Zero EIGHT fifteeN @ 7%',
87 buchungsgruppen_id => $buchungsgruppe7->id,
96 return SL::DB::Invoice->new(
97 customer_id => $customer->id,
98 currency_id => $currency_id,
99 employee_id => $employee->id,
100 salesman_id => $employee->id,
101 gldate => DateTime->today_local->to_kivitendo,
102 taxzone_id => $taxzone->id,
103 transdate => DateTime->today_local->to_kivitendo,
113 my $part = delete($params{part}) || $parts[0];
115 return SL::DB::InvoiceItem->new(
116 parts_id => $part->id,
117 lastcost => $part->lastcost,
118 sellprice => $part->sellprice,
119 description => $part->description,
125 sub test_default_invoice_one_item_19_tax_not_included() {
128 my $item = new_item(qty => 2.5);
129 my $invoice = new_invoice(
131 invoiceitems => [ $item ],
134 my $taxkey = $item->part->get_taxkey(date => DateTime->today_local, is_sales => 1, taxzone => $invoice->taxzone_id);
136 # sellprice 2.34 * qty 2.5 = 5.85
137 # 19%(5.85) = 1.1115; rounded = 1.11 # 8%(5.85) = 0.468; rounded = 0.47
138 # total rounded = 6.96 # total rounded = 6.32
140 # lastcost 1.93 * qty 2.5 = 4.825; rounded 4.83
141 # line marge_total = 1.02
142 # line marge_percent = 17.4358974358974
144 my $title = $default_manager eq "swiss" ? 'default invoice, one item, 8% tax not included' : 'default invoice, one item, 19% tax not included';
145 my %data = $invoice->calculate_prices_and_taxes;
147 is($item->marge_total, 1.02, "${title}: item marge_total");
148 is($item->marge_percent, 17.4358974358974, "${title}: item marge_percent");
149 is($item->marge_price_factor, 1, "${title}: item marge_price_factor");
151 is($invoice->netamount, 5.85, "${title}: netamount");
152 if ($default_manager eq "swiss") {
153 is($invoice->amount, 6.32, "${title}: amount");
155 is($invoice->amount, 6.96, "${title}: amount");
157 is($invoice->marge_total, 1.02, "${title}: marge_total");
158 is($invoice->marge_percent, 17.4358974358974, "${title}: marge_percent");
163 $buchungsgruppe->income_accno_id($taxzone) => {
166 taxkey => $default_manager eq "swiss" ? 2 : 3,
175 $tax->chart_id => $default_manager eq "swiss" ? 0.47 : 1.11,
179 linetotal_cost => 4.83,
181 tax_amount => $default_manager eq "swiss" ? 0.468 : 1.1115,
182 taxkey_id => $taxkey->id,
185 }, "${title}: calculated data");
188 sub test_default_invoice_two_items_19_7_tax_not_included() {
191 my $item1 = new_item(qty => 2.5);
192 my $item2 = new_item(qty => 1.2, part => $parts[1]);
193 my $invoice = new_invoice(
195 invoiceitems => [ $item1, $item2 ],
198 my $taxkey1 = $item1->part->get_taxkey(date => DateTime->today_local, is_sales => 1, taxzone => $invoice->taxzone_id);
199 my $taxkey2 = $item2->part->get_taxkey(date => DateTime->today_local, is_sales => 1, taxzone => $invoice->taxzone_id);
202 # sellprice 2.34 * qty 2.5 = 5.85
203 # 19%(5.85) = 1.1115; rounded = 1.11 # 8%(5.85) = 0.468; rounded = 0.47
204 # total rounded = 6.96 # total rounded = 6.32
206 # lastcost 1.93 * qty 2.5 = 4.825; rounded 4.83
207 # line marge_total = 1.02
208 # line marge_percent = 17.4358974358974
211 # sellprice 9.714 * qty 1.2 = 11.6568 rounded 11.66
212 # 7%(11.6568) = 0.815976; rounded = 0.82 # 2.5%(11.6568) = 0.29142; rounded = 0.29
213 # total rounded = 12.48 # total rounded = 11.95
215 # lastcost 5.473 * qty 1.2 = 6.5676; rounded 6.57
216 # line marge_total = 5.09
217 # line marge_percent = 43.6535162950257
219 my $title = $default_manager eq "swiss" ? 'default invoice, two item, 8/2.5% tax not included' : 'default invoice, two item, 19/7% tax not included';
220 my %data = $invoice->calculate_prices_and_taxes;
222 is($item1->marge_total, 1.02, "${title}: item1 marge_total");
223 is($item1->marge_percent, 17.4358974358974, "${title}: item1 marge_percent");
224 is($item1->marge_price_factor, 1, "${title}: item1 marge_price_factor");
226 is($item2->marge_total, 5.09, "${title}: item2 marge_total");
227 is($item2->marge_percent, 43.6535162950257, "${title}: item2 marge_percent");
228 is($item2->marge_price_factor, 1, "${title}: item2 marge_price_factor");
230 is($invoice->netamount, 5.85 + 11.66, "${title}: netamount");
231 if ($default_manager eq "swiss") {
232 is($invoice->amount, 6.32 + 11.95, "${title}: amount");
234 is($invoice->amount, 6.96 + 12.48, "${title}: amount");
236 is($invoice->marge_total, 1.02 + 5.09, "${title}: marge_total");
237 is($invoice->marge_percent, 34.8943460879497, "${title}: marge_percent");
242 $buchungsgruppe->income_accno_id($taxzone) => {
245 taxkey => $default_manager eq "swiss" ? 2 : 3,
247 $buchungsgruppe7->income_accno_id($taxzone) => {
250 taxkey => $default_manager eq "swiss" ? 3 : 2,
259 $tax->chart_id => $default_manager eq "swiss" ? 0.47 : 1.11,
260 $tax7->chart_id => $default_manager eq "swiss" ? 0.29 : 0.82,
264 linetotal_cost => 4.83,
266 tax_amount => $default_manager eq "swiss" ? 0.468 : 1.1115,
267 taxkey_id => $taxkey1->id,
269 { linetotal => 11.66,
270 linetotal_cost => 6.57,
272 tax_amount => $default_manager eq "swiss" ? 0.2915 : 0.8162,
273 taxkey_id => $taxkey2->id,
276 }, "${title}: calculated data");
279 sub test_default_invoice_three_items_sellprice_rounding_discount() {
282 my $item1 = new_item(qty => 1, sellprice => 5.55, discount => .05);
283 my $item2 = new_item(qty => 1, sellprice => 5.50, discount => .05);
284 my $item3 = new_item(qty => 1, sellprice => 5.00, discount => .05);
285 my $invoice = new_invoice(
287 invoiceitems => [ $item1, $item2, $item3 ],
290 my %taxkeys = map { ($_->id => $_->get_taxkey(date => DateTime->today_local, is_sales => 1, taxzone => $invoice->taxzone_id)) } uniq map { $_->part } ($item1, $item2, $item3);
292 # this is how price_tax_calculator is implemented. It differs from
293 # the way sales_order / invoice - forms are calculating:
294 # linetotal = sellprice 5.55 * qty 1 * (1 - 0.05) = 5.2725; rounded 5.27
295 # linetotal = sellprice 5.50 * qty 1 * (1 - 0.05) = 5.225 rounded 5.23
296 # linetotal = sellprice 5.00 * qty 1 * (1 - 0.05) = 4.75; rounded 4.75
300 # discount = sellprice 5.55 * discount (0.05) = 0.2775; rounded 0.28
301 # sellprice = sellprice 5.55 - discount 0.28 = 5.27; rounded 5.27
302 # linetotal = sellprice 5.27 * qty 1 = 5.27; rounded 5.27
303 # 19%(5.27) = 1.0013; rounded = 1.00 # 8%(5.27) = 0.4216; rounded = 0.42
304 # total rounded = 6.27 # total rounded = 5.69
306 # lastcost 1.93 * qty 1 = 1.93; rounded 1.93
307 # line marge_total = 3.34
308 # line marge_percent = 63.3776091081594
311 # discount = sellprice 5.50 * discount 0.05 = 0.275; rounded 0.28
312 # sellprice = sellprice 5.50 - discount 0.28 = 5.22; rounded 5.22
313 # linetotal = sellprice 5.22 * qty 1 = 5.22; rounded 5.22
314 # 19%(5.22) = 0.9918; rounded = 0.99 # 8%(5.22) = 0.4176; rounded = 0.42
315 # total rounded = 6.21 # total rounded = 5.64
317 # lastcost 1.93 * qty 1 = 1.93; rounded 1.93
318 # line marge_total = 5.22 - 1.93 = 3.29
319 # line marge_percent = 3.29/5.22 = 0.630268199233716
322 # discount = sellprice 5.00 * discount 0.25 = 0.25; rounded 0.25
323 # sellprice = sellprice 5.00 - discount 0.25 = 4.75; rounded 4.75
324 # linetotal = sellprice 4.75 * qty 1 = 4.75; rounded 4.75
325 # 19%(4.75) = 0.9025; rounded = 0.90 # 8%(4.75) = 0.38; rounded = 0.38
326 # total rounded = 5.65 # total rounded = 5.13
328 # lastcost 1.93 * qty 1 = 1.93; rounded 1.93
329 # line marge_total = 2.82
330 # line marge_percent = 59.3684210526316
332 my $title = 'default invoice, three items, sellprice, rounding, discount';
333 my %data = $invoice->calculate_prices_and_taxes;
335 is($item1->marge_total, 3.34, "${title}: item1 marge_total");
336 is($item1->marge_percent, 63.3776091081594, "${title}: item1 marge_percent");
337 is($item1->marge_price_factor, 1, "${title}: item1 marge_price_factor");
339 is($item2->marge_total, 3.29, "${title}: item2 marge_total");
340 is($item2->marge_percent, 63.0268199233716, "${title}: item2 marge_percent");
341 is($item2->marge_price_factor, 1, "${title}: item2 marge_price_factor");
343 is($item3->marge_total, 2.82, "${title}: item3 marge_total");
344 is($item3->marge_percent, 59.3684210526316, "${title}: item3 marge_percent");
345 is($item3->marge_price_factor, 1, "${title}: item3 marge_price_factor");
347 is($invoice->netamount, 5.27 + 5.22 + 4.75, "${title}: netamount");
349 # 6.27 + 6.21 + 5.65 = 18.13 # 5.69 + 5.64 + 5.13 = 16.46
350 # 1.19*(5.27 + 5.22 + 4.75) = 18.1356; rounded 18.14 # 0.08*(15.24) = 1.2192; rounded 1.22
351 #is($invoice->amount, 6.27 + 6.21 + 5.65, "${title}: amount");
352 if ($default_manager eq "swiss") {
353 is($invoice->amount, 16.46, "${title}: amount");
355 is($invoice->amount, 18.14, "${title}: amount");
358 is($invoice->marge_total, 3.34 + 3.29 + 2.82, "${title}: marge_total");
359 is($invoice->marge_percent, 62.007874015748, "${title}: marge_percent");
364 $buchungsgruppe->income_accno_id($taxzone) => {
367 taxkey => $default_manager eq "swiss" ? 2 : 3,
376 $tax->chart_id => $default_manager eq "swiss" ? 1.22 : 2.9,
380 linetotal_cost => 1.93,
382 tax_amount => $default_manager eq "swiss" ? 0.4216 : 1.0013,
383 taxkey_id => $taxkeys{$item1->parts_id}->id,
386 linetotal_cost => 1.93,
388 tax_amount => $default_manager eq "swiss" ? 0.4176 : 0.9918,
389 taxkey_id => $taxkeys{$item2->parts_id}->id,
392 linetotal_cost => 1.93,
394 tax_amount => $default_manager eq "swiss" ? 0.38 : 0.9025,
395 taxkey_id => $taxkeys{$item3->parts_id}->id,
398 }, "${title}: calculated data");
401 Support::TestSetup::login();
403 test_default_invoice_one_item_19_tax_not_included();
404 test_default_invoice_two_items_19_7_tax_not_included();
405 test_default_invoice_three_items_sellprice_rounding_discount();