10 use Support::TestSetup;
13 use SL::DB::Buchungsgruppe;
22 my ($customer, $currency_id, @parts, $buchungsgruppe, $buchungsgruppe7, $unit, $employee, $tax, $tax7, $taxzone);
27 $params{$_} ||= {} for qw(buchungsgruppe unit customer part tax);
29 SL::DB::Manager::Invoice->delete_all(all => 1);
30 SL::DB::Manager::Part->delete_all(all => 1);
31 SL::DB::Manager::Customer->delete_all(all => 1);
33 $buchungsgruppe = SL::DB::Manager::Buchungsgruppe->find_by(description => 'Standard 19%', %{ $params{buchungsgruppe} }) || croak "No accounting group";
34 $buchungsgruppe7 = SL::DB::Manager::Buchungsgruppe->find_by(description => 'Standard 7%') || croak "No accounting group for 7\%";
35 $unit = SL::DB::Manager::Unit->find_by(name => 'kg', %{ $params{unit} }) || croak "No unit";
36 $employee = SL::DB::Manager::Employee->current || croak "No employee";
37 $tax = SL::DB::Manager::Tax->find_by(taxkey => 3, rate => 0.19, %{ $params{tax} }) || croak "No tax";
38 $tax7 = SL::DB::Manager::Tax->find_by(taxkey => 2, rate => 0.07) || croak "No tax for 7\%";
39 $taxzone = SL::DB::Manager::TaxZone->find_by( description => 'Inland') || croak "No taxzone";
41 $currency_id = $::instance_conf->get_currency_id;
43 $customer = SL::DB::Customer->new(
44 name => 'Test Customer',
45 currency_id => $currency_id,
46 taxzone_id => $taxzone->id,
47 %{ $params{customer} }
51 push @parts, SL::DB::Part->new(
52 partnumber => 'T4254',
53 description => 'Fourty-two fifty-four',
56 buchungsgruppen_id => $buchungsgruppe->id,
61 push @parts, SL::DB::Part->new(
62 partnumber => 'T0815',
63 description => 'Zero EIGHT fifteeN @ 7%',
66 buchungsgruppen_id => $buchungsgruppe7->id,
75 return SL::DB::Invoice->new(
76 customer_id => $customer->id,
77 currency_id => $currency_id,
78 employee_id => $employee->id,
79 salesman_id => $employee->id,
80 gldate => DateTime->today_local->to_kivitendo,
81 taxzone_id => $taxzone->id,
82 transdate => DateTime->today_local->to_kivitendo,
92 my $part = delete($params{part}) || $parts[0];
94 return SL::DB::InvoiceItem->new(
95 parts_id => $part->id,
96 lastcost => $part->lastcost,
97 sellprice => $part->sellprice,
98 description => $part->description,
104 sub test_default_invoice_one_item_19_tax_not_included() {
107 my $item = new_item(qty => 2.5);
108 my $invoice = new_invoice(
110 invoiceitems => [ $item ],
113 # sellprice 2.34 * qty 2.5 = 5.85
114 # 19%(5.85) = 1.1115; rounded = 1.11
115 # total rounded = 6.96
117 # lastcost 1.93 * qty 2.5 = 4.825; rounded 4.83
118 # line marge_total = 1.02
119 # line marge_percent = 17.4358974358974
121 my $title = 'default invoice, one item, 19% tax not included';
122 my %data = $invoice->calculate_prices_and_taxes;
124 is($item->marge_total, 1.02, "${title}: item marge_total");
125 is($item->marge_percent, 17.4358974358974, "${title}: item marge_percent");
126 is($item->marge_price_factor, 1, "${title}: item marge_price_factor");
128 is($invoice->netamount, 5.85, "${title}: netamount");
129 is($invoice->amount, 6.96, "${title}: amount");
130 is($invoice->marge_total, 1.02, "${title}: marge_total");
131 is($invoice->marge_percent, 17.4358974358974, "${title}: marge_percent");
136 $buchungsgruppe->income_accno_id($taxzone) => {
148 $tax->chart_id => 1.11,
150 }, "${title}: calculated data");
153 sub test_default_invoice_two_items_19_7_tax_not_included() {
156 my $item1 = new_item(qty => 2.5);
157 my $item2 = new_item(qty => 1.2, part => $parts[1]);
158 my $invoice = new_invoice(
160 invoiceitems => [ $item1, $item2 ],
164 # sellprice 2.34 * qty 2.5 = 5.85
165 # 19%(5.85) = 1.1115; rounded = 1.11
166 # total rounded = 6.96
168 # lastcost 1.93 * qty 2.5 = 4.825; rounded 4.83
169 # line marge_total = 1.02
170 # line marge_percent = 17.4358974358974
173 # sellprice 9.714 * qty 1.2 = 11.6568 rounded 11.66
174 # 7%(11.6568) = 0.815976; rounded = 0.82
175 # total rounded = 12.48
177 # lastcost 5.473 * qty 1.2 = 6.5676; rounded 6.57
178 # line marge_total = 5.09
179 # line marge_percent = 43.6535162950257
181 my $title = 'default invoice, two item, 19/7% tax not included';
182 my %data = $invoice->calculate_prices_and_taxes;
184 is($item1->marge_total, 1.02, "${title}: item1 marge_total");
185 is($item1->marge_percent, 17.4358974358974, "${title}: item1 marge_percent");
186 is($item1->marge_price_factor, 1, "${title}: item1 marge_price_factor");
188 is($item2->marge_total, 5.09, "${title}: item2 marge_total");
189 is($item2->marge_percent, 43.6535162950257, "${title}: item2 marge_percent");
190 is($item2->marge_price_factor, 1, "${title}: item2 marge_price_factor");
192 is($invoice->netamount, 5.85 + 11.66, "${title}: netamount");
193 is($invoice->amount, 6.96 + 12.48, "${title}: amount");
194 is($invoice->marge_total, 1.02 + 5.09, "${title}: marge_total");
195 is($invoice->marge_percent, 34.8943460879497, "${title}: marge_percent");
200 $buchungsgruppe->income_accno_id($taxzone) => {
205 $buchungsgruppe7->income_accno_id($taxzone) => {
217 $tax->chart_id => 1.11,
218 $tax7->chart_id => 0.82,
220 }, "${title}: calculated data");
223 sub test_default_invoice_three_items_sellprice_rounding_discount() {
226 my $item1 = new_item(qty => 1, sellprice => 5.55, discount => .05);
227 my $item2 = new_item(qty => 1, sellprice => 5.50, discount => .05);
228 my $item3 = new_item(qty => 1, sellprice => 5.00, discount => .05);
229 my $invoice = new_invoice(
231 invoiceitems => [ $item1, $item2, $item3 ],
234 # this is how price_tax_calculator is implemented. It differs from
235 # the way sales_order / invoice - forms are calculating:
236 # linetotal = sellprice 5.55 * qty 1 * (1 - 0.05) = 5.2725; rounded 5.27
237 # linetotal = sellprice 5.50 * qty 1 * (1 - 0.05) = 5.225 rounded 5.23
238 # linetotal = sellprice 5.00 * qty 1 * (1 - 0.05) = 4.75; rounded 4.75
242 # discount = sellprice 5.55 * discount (0.05) = 0.2775; rounded 0.28
243 # sellprice = sellprice 5.55 - discount 0.28 = 5.27; rounded 5.27
244 # linetotal = sellprice 5.27 * qty 1 = 5.27; rounded 5.27
245 # 19%(5.27) = 1.0013; rounded = 1.00
246 # total rounded = 6.27
248 # lastcost 1.93 * qty 1 = 1.93; rounded 1.93
249 # line marge_total = 3.34
250 # line marge_percent = 63.3776091081594
253 # discount = sellprice 5.50 * discount 0.05 = 0.275; rounded 0.28
254 # sellprice = sellprice 5.50 - discount 0.28 = 5.22; rounded 5.22
255 # linetotal = sellprice 5.22 * qty 1 = 5.22; rounded 5.22
256 # 19%(5.22) = 0.9918; rounded = 0.99
257 # total rounded = 6.21
259 # lastcost 1.93 * qty 1 = 1.93; rounded 1.93
260 # line marge_total = 5.22 - 1.93 = 3.29
261 # line marge_percent = 3.29/5.22 = 0.630268199233716
264 # discount = sellprice 5.00 * discount 0.25 = 0.25; rounded 0.25
265 # sellprice = sellprice 5.00 - discount 0.25 = 4.75; rounded 4.75
266 # linetotal = sellprice 4.75 * qty 1 = 4.75; rounded 4.75
267 # 19%(4.75) = 0.9025; rounded = 0.90
268 # total rounded = 5.65
270 # lastcost 1.93 * qty 1 = 1.93; rounded 1.93
271 # line marge_total = 2.82
272 # line marge_percent = 59.3684210526316
274 my $title = 'default invoice, three items, sellprice, rounding, discount';
275 my %data = $invoice->calculate_prices_and_taxes;
277 is($item1->marge_total, 3.34, "${title}: item1 marge_total");
278 is($item1->marge_percent, 63.3776091081594, "${title}: item1 marge_percent");
279 is($item1->marge_price_factor, 1, "${title}: item1 marge_price_factor");
281 is($item2->marge_total, 3.29, "${title}: item2 marge_total");
282 is($item2->marge_percent, 63.0268199233716, "${title}: item2 marge_percent");
283 is($item2->marge_price_factor, 1, "${title}: item2 marge_price_factor");
285 is($item3->marge_total, 2.82, "${title}: item3 marge_total");
286 is($item3->marge_percent, 59.3684210526316, "${title}: item3 marge_percent");
287 is($item3->marge_price_factor, 1, "${title}: item3 marge_price_factor");
289 is($invoice->netamount, 5.27 + 5.22 + 4.75, "${title}: netamount");
291 # 6.27 + 6.21 + 5.65 = 18.13
292 # 1.19*(5.27 + 5.22 + 4.75) = 18.1356; rounded 18.14
293 #is($invoice->amount, 6.27 + 6.21 + 5.65, "${title}: amount");
294 is($invoice->amount, 18.14, "${title}: amount");
296 is($invoice->marge_total, 3.34 + 3.29 + 2.82, "${title}: marge_total");
297 is($invoice->marge_percent, 62.007874015748, "${title}: marge_percent");
302 $buchungsgruppe->income_accno_id($taxzone) => {
314 $tax->chart_id => 2.9,
316 }, "${title}: calculated data");
319 Support::TestSetup::login();
321 test_default_invoice_one_item_19_tax_not_included();
322 test_default_invoice_two_items_19_7_tax_not_included();
323 test_default_invoice_three_items_sellprice_rounding_discount();