10 use Support::TestSetup;
13 use SL::DB::Buchungsgruppe;
21 my ($customer, $currency_id, @parts, $buchungsgruppe, $buchungsgruppe7, $unit, $employee, $tax, $tax7);
26 $params{$_} ||= {} for qw(buchungsgruppe unit customer part tax);
28 SL::DB::Manager::Invoice->delete_all(all => 1);
29 SL::DB::Manager::Part->delete_all(all => 1);
30 SL::DB::Manager::Customer->delete_all(all => 1);
32 $buchungsgruppe = SL::DB::Manager::Buchungsgruppe->find_by(description => 'Standard 19%', %{ $params{buchungsgruppe} }) || croak "No accounting group";
33 $buchungsgruppe7 = SL::DB::Manager::Buchungsgruppe->find_by(description => 'Standard 7%') || croak "No accounting group for 7\%";
34 $unit = SL::DB::Manager::Unit->find_by(name => 'kg', %{ $params{unit} }) || croak "No unit";
35 $employee = SL::DB::Manager::Employee->current || croak "No employee";
36 $tax = SL::DB::Manager::Tax->find_by(taxkey => 3, rate => 0.19, %{ $params{tax} }) || croak "No tax";
37 $tax7 = SL::DB::Manager::Tax->find_by(taxkey => 2, rate => 0.07) || croak "No tax for 7\%";
39 $currency_id = $::instance_conf->get_currency_id;
41 $customer = SL::DB::Customer->new(
42 name => 'Test Customer',
43 currency_id => $currency_id,
44 %{ $params{customer} }
48 push @parts, SL::DB::Part->new(
49 partnumber => 'T4254',
50 description => 'Fourty-two fifty-four',
53 buchungsgruppen_id => $buchungsgruppe->id,
58 push @parts, SL::DB::Part->new(
59 partnumber => 'T0815',
60 description => 'Zero EIGHT fifteeN @ 7%',
63 buchungsgruppen_id => $buchungsgruppe7->id,
72 return SL::DB::Invoice->new(
73 customer_id => $customer->id,
74 currency_id => $currency_id,
75 employee_id => $employee->id,
76 salesman_id => $employee->id,
77 gldate => DateTime->today_local->to_kivitendo,
79 transdate => DateTime->today_local->to_kivitendo,
89 my $part = delete($params{part}) || $parts[0];
91 return SL::DB::InvoiceItem->new(
92 parts_id => $part->id,
93 lastcost => $part->lastcost,
94 sellprice => $part->sellprice,
95 description => $part->description,
101 sub test_default_invoice_one_item_19_tax_not_included() {
104 my $item = new_item(qty => 2.5);
105 my $invoice = new_invoice(
107 invoiceitems => [ $item ],
110 # sellprice 2.34 * qty 2.5 = 5.85
111 # 19%(5.85) = 1.1115; rounded = 1.11
112 # total rounded = 6.96
114 # lastcost 1.93 * qty 2.5 = 4.825; rounded 4.83
115 # line marge_total = 1.02
116 # line marge_percent = 17.4358974358974
118 my $title = 'default invoice, one item, 19% tax not included';
119 my %data = $invoice->calculate_prices_and_taxes;
121 is($item->marge_total, 1.02, "${title}: item marge_total");
122 is($item->marge_percent, 17.4358974358974, "${title}: item marge_percent");
123 is($item->marge_price_factor, 1, "${title}: item marge_price_factor");
125 is($invoice->netamount, 5.85, "${title}: netamount");
126 is($invoice->amount, 6.96, "${title}: amount");
127 is($invoice->marge_total, 1.02, "${title}: marge_total");
128 is($invoice->marge_percent, 17.4358974358974, "${title}: marge_percent");
133 $buchungsgruppe->income_accno_id_0 => {
145 $tax->chart_id => 1.11,
147 }, "${title}: calculated data");
150 sub test_default_invoice_two_items_19_7_tax_not_included() {
153 my $item1 = new_item(qty => 2.5);
154 my $item2 = new_item(qty => 1.2, part => $parts[1]);
155 my $invoice = new_invoice(
157 invoiceitems => [ $item1, $item2 ],
161 # sellprice 2.34 * qty 2.5 = 5.85
162 # 19%(5.85) = 1.1115; rounded = 1.11
163 # total rounded = 6.96
165 # lastcost 1.93 * qty 2.5 = 4.825; rounded 4.83
166 # line marge_total = 1.02
167 # line marge_percent = 17.4358974358974
170 # sellprice 9.714 * qty 1.2 = 11.6568 rounded 11.66
171 # 7%(11.6568) = 0.815976; rounded = 0.82
172 # total rounded = 12.48
174 # lastcost 5.473 * qty 1.2 = 6.5676; rounded 6.57
175 # line marge_total = 5.09
176 # line marge_percent = 43.6535162950257
178 my $title = 'default invoice, two item, 19/7% tax not included';
179 my %data = $invoice->calculate_prices_and_taxes;
181 is($item1->marge_total, 1.02, "${title}: item1 marge_total");
182 is($item1->marge_percent, 17.4358974358974, "${title}: item1 marge_percent");
183 is($item1->marge_price_factor, 1, "${title}: item1 marge_price_factor");
185 is($item2->marge_total, 5.09, "${title}: item2 marge_total");
186 is($item2->marge_percent, 43.6535162950257, "${title}: item2 marge_percent");
187 is($item2->marge_price_factor, 1, "${title}: item2 marge_price_factor");
189 is($invoice->netamount, 5.85 + 11.66, "${title}: netamount");
190 is($invoice->amount, 6.96 + 12.48, "${title}: amount");
191 is($invoice->marge_total, 1.02 + 5.09, "${title}: marge_total");
192 is($invoice->marge_percent, 34.8943460879497, "${title}: marge_percent");
197 $buchungsgruppe->income_accno_id_0 => {
202 $buchungsgruppe7->income_accno_id_0 => {
214 $tax->chart_id => 1.11,
215 $tax7->chart_id => 0.82,
217 }, "${title}: calculated data");
220 sub test_default_invoice_three_items_sellprice_rounding_discount() {
223 my $item1 = new_item(qty => 1, sellprice => 5.55, discount => .05);
224 my $item2 = new_item(qty => 1, sellprice => 5.50, discount => .05);
225 my $item3 = new_item(qty => 1, sellprice => 5.00, discount => .05);
226 my $invoice = new_invoice(
228 invoiceitems => [ $item1, $item2, $item3 ],
231 # this is how price_tax_calculator is implemented. It differs from
232 # the way sales_order / invoice - forms are calculating:
233 # linetotal = sellprice 5.55 * qty 1 * (1 - 0.05) = 5.2725; rounded 5.27
234 # linetotal = sellprice 5.50 * qty 1 * (1 - 0.05) = 5.225 rounded 5.23
235 # linetotal = sellprice 5.00 * qty 1 * (1 - 0.05) = 4.75; rounded 4.75
239 # discount = sellprice 5.55 * discount (0.05) = 0.2775; rounded 0.28
240 # sellprice = sellprice 5.55 - discount 0.28 = 5.27; rounded 5.27
241 # linetotal = sellprice 5.27 * qty 1 = 5.27; rounded 5.27
242 # 19%(5.27) = 1.0013; rounded = 1.00
243 # total rounded = 6.27
245 # lastcost 1.93 * qty 1 = 1.93; rounded 1.93
246 # line marge_total = 3.34
247 # line marge_percent = 63.3776091081594
250 # discount = sellprice 5.50 * discount 0.05 = 0.275; rounded 0.28
251 # sellprice = sellprice 5.50 - discount 0.28 = 5.22; rounded 5.22
252 # linetotal = sellprice 5.22 * qty 1 = 5.22; rounded 5.22
253 # 19%(5.22) = 0.9918; rounded = 0.99
254 # total rounded = 6.21
256 # lastcost 1.93 * qty 1 = 1.93; rounded 1.93
257 # line marge_total = 5.22 - 1.93 = 3.29
258 # line marge_percent = 3.29/5.22 = 0.630268199233716
261 # discount = sellprice 5.00 * discount 0.25 = 0.25; rounded 0.25
262 # sellprice = sellprice 5.00 - discount 0.25 = 4.75; rounded 4.75
263 # linetotal = sellprice 4.75 * qty 1 = 4.75; rounded 4.75
264 # 19%(4.75) = 0.9025; rounded = 0.90
265 # total rounded = 5.65
267 # lastcost 1.93 * qty 1 = 1.93; rounded 1.93
268 # line marge_total = 2.82
269 # line marge_percent = 59.3684210526316
271 my $title = 'default invoice, three items, sellprice, rounding, discount';
272 my %data = $invoice->calculate_prices_and_taxes;
274 is($item1->marge_total, 3.34, "${title}: item1 marge_total");
275 is($item1->marge_percent, 63.3776091081594, "${title}: item1 marge_percent");
276 is($item1->marge_price_factor, 1, "${title}: item1 marge_price_factor");
278 is($item2->marge_total, 3.29, "${title}: item2 marge_total");
279 is($item2->marge_percent, 63.0268199233716, "${title}: item2 marge_percent");
280 is($item2->marge_price_factor, 1, "${title}: item2 marge_price_factor");
282 is($item3->marge_total, 2.82, "${title}: item3 marge_total");
283 is($item3->marge_percent, 59.3684210526316, "${title}: item3 marge_percent");
284 is($item3->marge_price_factor, 1, "${title}: item3 marge_price_factor");
286 is($invoice->netamount, 5.27 + 5.22 + 4.75, "${title}: netamount");
288 # 6.27 + 6.21 + 5.65 = 18.13
289 # 1.19*(5.27 + 5.22 + 4.75) = 18.1356; rounded 18.14
290 #is($invoice->amount, 6.27 + 6.21 + 5.65, "${title}: amount");
291 is($invoice->amount, 18.14, "${title}: amount");
293 is($invoice->marge_total, 3.34 + 3.29 + 2.82, "${title}: marge_total");
294 is($invoice->marge_percent, 62.007874015748, "${title}: marge_percent");
299 $buchungsgruppe->income_accno_id_0 => {
311 $tax->chart_id => 2.9,
313 }, "${title}: calculated data");
316 Support::TestSetup::login();
318 test_default_invoice_one_item_19_tax_not_included();
319 test_default_invoice_two_items_19_7_tax_not_included();
320 test_default_invoice_three_items_sellprice_rounding_discount();