9 use Support::TestSetup;
11 use List::Util qw(sum);
13 use SL::DB::Buchungsgruppe;
15 use SL::DB::Exchangerate;
23 use SL::DB::BankAccount;
24 use SL::DB::PaymentTerm;
25 use SL::DB::PurchaseInvoice;
26 use SL::DB::BankTransaction;
27 use SL::Controller::BankTransaction;
30 my ($customer, $vendor, $currency_id, @parts, $unit, $employee, $tax, $tax7, $tax_9, $taxzone, $payment_terms, $bank_account);
31 my ($transdate1, $transdate2, $currency);
32 my ($ar_chart,$bank,$ar_amount_chart, $ap_chart, $ap_amount_chart);
33 my ($ar_transaction, $ap_transaction);
37 SL::DB::Manager::BankTransaction->delete_all(all => 1);
38 SL::DB::Manager::InvoiceItem->delete_all(all => 1);
39 SL::DB::Manager::InvoiceItem->delete_all(all => 1);
40 SL::DB::Manager::Invoice->delete_all(all => 1);
41 SL::DB::Manager::PurchaseInvoice->delete_all(all => 1);
42 SL::DB::Manager::Part->delete_all(all => 1);
43 SL::DB::Manager::Customer->delete_all(all => 1);
44 SL::DB::Manager::Vendor->delete_all(all => 1);
45 SL::DB::Manager::BankAccount->delete_all(all => 1);
46 SL::DB::Manager::PaymentTerm->delete_all(all => 1);
47 SL::DB::Manager::Currency->delete_all(where => [ name => 'CUR' ]);
52 Support::TestSetup::login();
54 reset_state(); # initialise customers/vendors/bank/currency/...
57 test_overpayment_with_partialpayment();
61 test_partial_payment();
63 # remove all created data at end of test
68 ###### functions for setting up data
73 $params{$_} ||= {} for qw(unit customer part tax vendor);
77 $transdate1 = DateTime->today;
78 $transdate2 = DateTime->today->add(days => 5);
80 $employee = SL::DB::Manager::Employee->current || croak "No employee";
81 $tax = SL::DB::Manager::Tax->find_by(taxkey => 3, rate => 0.19, %{ $params{tax} }) || croak "No tax";
82 $tax7 = SL::DB::Manager::Tax->find_by(taxkey => 2, rate => 0.07) || croak "No tax for 7\%";
83 $taxzone = SL::DB::Manager::TaxZone->find_by( description => 'Inland') || croak "No taxzone";
84 $tax_9 = SL::DB::Manager::Tax->find_by(taxkey => 9, rate => 0.19, %{ $params{tax} }) || croak "No tax";
86 $currency_id = $::instance_conf->get_currency_id;
88 $bank_account = SL::DB::BankAccount->new(
89 account_number => '123',
94 chart_id => SL::DB::Manager::Chart->find_by(description => 'Bank')->id,
95 name => SL::DB::Manager::Chart->find_by(description => 'Bank')->description,
98 $customer = SL::DB::Customer->new(
99 name => 'Test Customer',
100 currency_id => $currency_id,
101 taxzone_id => $taxzone->id,
102 iban => 'DE12500105170648489890',
104 account_number => '648489890',
105 mandate_date_of_signature => $transdate1,
106 mandator_id => 'foobar',
108 depositor => 'Test Customer',
109 %{ $params{customer} }
112 $payment_terms = SL::DB::PaymentTerm->new(
113 description => 'payment',
114 description_long => 'payment',
117 percent_skonto => '0.05',
118 auto_calculation => 1,
121 $vendor = SL::DB::Vendor->new(
122 name => 'Test Vendor',
123 currency_id => $currency_id,
124 taxzone_id => $taxzone->id,
125 payment_id => $payment_terms->id,
126 iban => 'DE12500105170648489890',
128 account_number => '648489890',
130 depositor => 'Test Vendor',
134 $ar_chart = SL::DB::Manager::Chart->find_by( accno => '1400' ); # Forderungen
135 $ap_chart = SL::DB::Manager::Chart->find_by( accno => '1600' ); # Verbindlichkeiten
136 $bank = SL::DB::Manager::Chart->find_by( accno => '1200' ); # Bank
137 $ar_amount_chart = SL::DB::Manager::Chart->find_by( accno => '8400' ); # Erlöse
138 $ap_amount_chart = SL::DB::Manager::Chart->find_by( accno => '3400' ); # Wareneingang 19%
142 sub test_ar_transaction {
145 my $amount = $params{amount} || $::form->round_amount(100 * 1.19,2);
146 my $invoice = SL::DB::Invoice->new(
148 invnumber => $params{invnumber} || undef, # let it use its own invnumber
150 netamount => $netamount,
151 transdate => $transdate1,
153 customer_id => $customer->id,
154 taxzone_id => $customer->taxzone_id,
155 currency_id => $currency_id,
157 payment_id => $params{payment_id} || undef,
158 notes => 'test_ar_transaction',
160 $invoice->add_ar_amount_row(
161 amount => $invoice->netamount,
162 chart => $ar_amount_chart,
166 $invoice->create_ar_row(chart => $ar_chart);
169 is($invoice->currency_id , $currency_id , 'currency_id has been saved');
170 is($invoice->netamount , 100 , 'ar amount has been converted');
171 is($invoice->amount , 119 , 'ar amount has been converted');
172 is($invoice->taxincluded , 0 , 'ar transaction doesn\'t have taxincluded');
174 is(SL::DB::Manager::AccTransaction->find_by(chart_id => $ar_amount_chart->id , trans_id => $invoice->id)->amount , '100.00000' , $ar_amount_chart->accno . ': has been converted for currency');
175 is(SL::DB::Manager::AccTransaction->find_by(chart_id => $ar_chart->id , trans_id => $invoice->id)->amount , '-119.00000' , $ar_chart->accno . ': has been converted for currency');
180 sub test_ap_transaction {
183 my $amount = $::form->round_amount($netamount * 1.19,2);
184 my $invoice = SL::DB::PurchaseInvoice->new(
186 invnumber => $params{invnumber} || 'test_ap_transaction',
188 netamount => $netamount,
189 transdate => $transdate1,
191 vendor_id => $vendor->id,
192 taxzone_id => $vendor->taxzone_id,
193 currency_id => $currency_id,
195 notes => 'test_ap_transaction',
197 $invoice->add_ap_amount_row(
198 amount => $invoice->netamount,
199 chart => $ap_amount_chart,
200 tax_id => $tax_9->id,
203 $invoice->create_ap_row(chart => $ap_chart);
206 is($invoice->currency_id , $currency_id , 'currency_id has been saved');
207 is($invoice->netamount , 100 , 'ap amount has been converted');
208 is($invoice->amount , 119 , 'ap amount has been converted');
209 is($invoice->taxincluded , 0 , 'ap transaction doesn\'t have taxincluded');
211 is(SL::DB::Manager::AccTransaction->find_by(chart_id => $ap_amount_chart->id , trans_id => $invoice->id)->amount , '-100.00000' , $ap_amount_chart->accno . ': has been converted for currency');
212 is(SL::DB::Manager::AccTransaction->find_by(chart_id => $ap_chart->id , trans_id => $invoice->id)->amount , '119.00000' , $ap_chart->accno . ': has been converted for currency');
221 my $testname = 'test1';
223 $ar_transaction = test_ar_transaction(invnumber => 'salesinv1');
225 my $bt = $ar_transaction->create_bank_transaction or die "Couldn't create bank_transaction";
227 $::form->{invoice_ids} = {
228 $bt->id => [ $ar_transaction->id ]
231 my $bt_controller = SL::Controller::BankTransaction->new;
232 $bt_controller->action_save_invoices;
234 $ar_transaction->load;
236 is($ar_transaction->paid , '119.00000' , "$testname: salesinv1 was paid");
237 is($ar_transaction->closed , 1 , "$testname: salesinv1 is closed");
238 is($bt->invoice_amount , '119.00000' , "$testname: bt invoice amount was assigned");
242 sub test_skonto_exact {
244 my $testname = 'test_skonto_exact';
246 $ar_transaction = test_ar_transaction(invnumber => 'salesinv skonto',
247 payment_id => $payment_terms->id,
250 my $bt = $ar_transaction->create_bank_transaction(amount => $ar_transaction->amount_less_skonto) or die "Couldn't create bank_transaction";
252 $::form->{invoice_ids} = {
253 $bt->id => [ $ar_transaction->id ]
255 $::form->{invoice_skontos} = {
256 $bt->id => [ 'with_skonto_pt' ]
259 my $bt_controller = SL::Controller::BankTransaction->new;
260 $bt_controller->action_save_invoices;
262 $ar_transaction->load;
264 is($ar_transaction->paid , '119.00000' , "$testname: salesinv skonto was paid");
265 is($ar_transaction->closed , 1 , "$testname: salesinv skonto is closed");
266 is($bt->invoice_amount , '113.05000' , "$testname: bt invoice amount was assigned");
270 sub test_two_invoices {
272 my $testname = 'test_two_invoices';
274 my $ar_transaction_1 = test_ar_transaction(invnumber => 'salesinv_1');
275 my $ar_transaction_2 = test_ar_transaction(invnumber => 'salesinv_2');
277 my $bt = $ar_transaction_1->create_bank_transaction(amount => ($ar_transaction_1->amount + $ar_transaction_2->amount),
278 purpose => "Rechnungen " . $ar_transaction_1->invnumber . " und " . $ar_transaction_2->invnumber,
279 ) or die "Couldn't create bank_transaction";
281 $::form->{invoice_ids} = {
282 $bt->id => [ $ar_transaction_1->id, $ar_transaction_2->id ]
285 my $bt_controller = SL::Controller::BankTransaction->new;
286 $bt_controller->action_save_invoices;
288 $ar_transaction_1->load;
289 $ar_transaction_2->load;
292 is($ar_transaction_1->paid , '119.00000' , "$testname: salesinv_1 was paid");
293 is($ar_transaction_1->closed , 1 , "$testname: salesinv_1 is closed");
294 is($ar_transaction_2->paid , '119.00000' , "$testname: salesinv_2 was paid");
295 is($ar_transaction_2->closed , 1 , "$testname: salesinv_2 is closed");
296 is($bt->invoice_amount , '238.00000' , "$testname: bt invoice amount was assigned");
300 sub test_overpayment {
302 my $testname = 'test_overpayment';
304 $ar_transaction = test_ar_transaction(invnumber => 'salesinv overpaid');
307 my $bt = $ar_transaction->create_bank_transaction(amount => 135) or die "Couldn't create bank_transaction";
309 $::form->{invoice_ids} = {
310 $bt->id => [ $ar_transaction->id ]
313 my $bt_controller = SL::Controller::BankTransaction->new;
314 $bt_controller->action_save_invoices;
316 $ar_transaction->load;
319 is($ar_transaction->paid , '135.00000' , "$testname: 'salesinv overpaid' was overpaid");
320 is($bt->invoice_amount , '135.00000' , "$testname: bt invoice amount was assigned overpaid amount");
321 { local $TODO = 'this currently fails because closed ignores over-payments, see commit d90966c7';
322 is($ar_transaction->closed , 0 , "$testname: 'salesinv overpaid' is open (via 'closed' method')");
324 is($ar_transaction->open_amount == 0 ? 1 : 0 , 0 , "$testname: 'salesinv overpaid is open (via amount-paid)");
328 sub test_overpayment_with_partialpayment {
330 # two payments on different days, 10 and 119. If there is only one invoice we want it be overpaid.
331 my $testname = 'test_overpayment_with_partialpayment';
333 $ar_transaction = test_ar_transaction(invnumber => 'salesinv overpaid partial');
335 my $bt_1 = $ar_transaction->create_bank_transaction(amount => 10) or die "Couldn't create bank_transaction";
336 my $bt_2 = $ar_transaction->create_bank_transaction(amount => 119,
337 transdate => DateTime->today->add(days => 5),
338 ) or die "Couldn't create bank_transaction";
340 $::form->{invoice_ids} = {
341 $bt_1->id => [ $ar_transaction->id ]
343 my $bt_controller_1 = SL::Controller::BankTransaction->new;
344 $bt_controller_1->action_save_invoices;
346 $::form->{invoice_ids} = {
347 $bt_2->id => [ $ar_transaction->id ]
349 my $bt_controller_2 = SL::Controller::BankTransaction->new;
350 $bt_controller_2->action_save_invoices;
352 $ar_transaction->load;
356 is($ar_transaction->paid , '129.00000' , "$testname: 'salesinv overpaid partial' was overpaid");
357 is($bt_1->invoice_amount , '10.00000' , "$testname: bt_1 invoice amount was assigned overpaid amount");
358 is($bt_2->invoice_amount , '119.00000' , "$testname: bt_2 invoice amount was assigned overpaid amount");
362 sub test_partial_payment {
364 my $testname = 'test_partial_payment';
366 $ar_transaction = test_ar_transaction(invnumber => 'salesinv partial payment');
369 my $bt = $ar_transaction->create_bank_transaction(amount => 100) or die "Couldn't create bank_transaction";
371 $::form->{invoice_ids} = {
372 $bt->id => [ $ar_transaction->id ]
375 my $bt_controller = SL::Controller::BankTransaction->new;
376 $bt_controller->action_save_invoices;
378 $ar_transaction->load;
381 is($ar_transaction->paid , '100.00000' , "$testname: 'salesinv partial payment' was partially paid");
382 is($bt->invoice_amount , '100.00000' , "$testname: bt invoice amount was assigned partially paid amount");