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 Support::TestSetup::login();
222 test_default_invoice_one_item_19_tax_not_included();
223 test_default_invoice_two_items_19_7_tax_not_included();