Revision 691a1293
Von Kivitendo Admin vor mehr als 8 Jahren hinzugefügt
t/bank/bank_transactions.t | ||
---|---|---|
1 |
use Test::More; |
|
2 |
|
|
3 |
use strict; |
|
4 |
|
|
5 |
use lib 't'; |
|
6 |
use utf8; |
|
7 |
|
|
8 |
use Carp; |
|
9 |
use Support::TestSetup; |
|
10 |
use Test::Exception; |
|
11 |
use List::Util qw(sum); |
|
12 |
|
|
13 |
use SL::DB::Buchungsgruppe; |
|
14 |
use SL::DB::Currency; |
|
15 |
use SL::DB::Exchangerate; |
|
16 |
use SL::DB::Customer; |
|
17 |
use SL::DB::Vendor; |
|
18 |
use SL::DB::Employee; |
|
19 |
use SL::DB::Invoice; |
|
20 |
use SL::DB::Part; |
|
21 |
use SL::DB::Unit; |
|
22 |
use SL::DB::TaxZone; |
|
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; |
|
28 |
use Data::Dumper; |
|
29 |
|
|
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); |
|
34 |
|
|
35 |
sub clear_up { |
|
36 |
|
|
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' ]); |
|
48 |
}; |
|
49 |
|
|
50 |
|
|
51 |
# starting test: |
|
52 |
Support::TestSetup::login(); |
|
53 |
|
|
54 |
reset_state(); # initialise customers/vendors/bank/currency/... |
|
55 |
|
|
56 |
test1(); |
|
57 |
test_overpayment_with_partialpayment(); |
|
58 |
test_overpayment(); |
|
59 |
test_skonto_exact(); |
|
60 |
test_two_invoices(); |
|
61 |
test_partial_payment(); |
|
62 |
|
|
63 |
# remove all created data at end of test |
|
64 |
# clear_up(); |
|
65 |
|
|
66 |
done_testing(); |
|
67 |
|
|
68 |
###### functions for setting up data |
|
69 |
|
|
70 |
sub reset_state { |
|
71 |
my %params = @_; |
|
72 |
|
|
73 |
$params{$_} ||= {} for qw(unit customer part tax vendor); |
|
74 |
|
|
75 |
clear_up(); |
|
76 |
|
|
77 |
$transdate1 = DateTime->today; |
|
78 |
$transdate2 = DateTime->today->add(days => 5); |
|
79 |
|
|
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"; |
|
85 |
|
|
86 |
$currency_id = $::instance_conf->get_currency_id; |
|
87 |
|
|
88 |
$bank_account = SL::DB::BankAccount->new( |
|
89 |
account_number => '123', |
|
90 |
bank_code => '123', |
|
91 |
iban => '123', |
|
92 |
bic => '123', |
|
93 |
bank => '123', |
|
94 |
chart_id => SL::DB::Manager::Chart->find_by(description => 'Bank')->id, |
|
95 |
name => SL::DB::Manager::Chart->find_by(description => 'Bank')->description, |
|
96 |
)->save; |
|
97 |
|
|
98 |
$customer = SL::DB::Customer->new( |
|
99 |
name => 'Test Customer', |
|
100 |
currency_id => $currency_id, |
|
101 |
taxzone_id => $taxzone->id, |
|
102 |
iban => 'DE12500105170648489890', |
|
103 |
bic => 'TESTBIC', |
|
104 |
account_number => '648489890', |
|
105 |
mandate_date_of_signature => $transdate1, |
|
106 |
mandator_id => 'foobar', |
|
107 |
bank => 'Geizkasse', |
|
108 |
depositor => 'Test Customer', |
|
109 |
%{ $params{customer} } |
|
110 |
)->save; |
|
111 |
|
|
112 |
$payment_terms = SL::DB::PaymentTerm->new( |
|
113 |
description => 'payment', |
|
114 |
description_long => 'payment', |
|
115 |
terms_netto => '30', |
|
116 |
terms_skonto => '5', |
|
117 |
percent_skonto => '0.05', |
|
118 |
auto_calculation => 1, |
|
119 |
)->save; |
|
120 |
|
|
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', |
|
127 |
bic => 'TESTBIC', |
|
128 |
account_number => '648489890', |
|
129 |
bank => 'Geizkasse', |
|
130 |
depositor => 'Test Vendor', |
|
131 |
%{ $params{vendor} } |
|
132 |
)->save; |
|
133 |
|
|
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% |
|
139 |
|
|
140 |
} |
|
141 |
|
|
142 |
sub test_ar_transaction { |
|
143 |
my (%params) = @_; |
|
144 |
my $netamount = 100; |
|
145 |
my $amount = $params{amount} || $::form->round_amount(100 * 1.19,2); |
|
146 |
my $invoice = SL::DB::Invoice->new( |
|
147 |
invoice => 0, |
|
148 |
invnumber => $params{invnumber} || undef, # let it use its own invnumber |
|
149 |
amount => $amount, |
|
150 |
netamount => $netamount, |
|
151 |
transdate => $transdate1, |
|
152 |
taxincluded => 0, |
|
153 |
customer_id => $customer->id, |
|
154 |
taxzone_id => $customer->taxzone_id, |
|
155 |
currency_id => $currency_id, |
|
156 |
transactions => [], |
|
157 |
payment_id => $params{payment_id} || undef, |
|
158 |
notes => 'test_ar_transaction', |
|
159 |
); |
|
160 |
$invoice->add_ar_amount_row( |
|
161 |
amount => $invoice->netamount, |
|
162 |
chart => $ar_amount_chart, |
|
163 |
tax_id => $tax->id, |
|
164 |
); |
|
165 |
|
|
166 |
$invoice->create_ar_row(chart => $ar_chart); |
|
167 |
$invoice->save; |
|
168 |
|
|
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'); |
|
173 |
|
|
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'); |
|
176 |
|
|
177 |
return $invoice; |
|
178 |
}; |
|
179 |
|
|
180 |
sub test_ap_transaction { |
|
181 |
my (%params) = @_; |
|
182 |
my $netamount = 100; |
|
183 |
my $amount = $::form->round_amount($netamount * 1.19,2); |
|
184 |
my $invoice = SL::DB::PurchaseInvoice->new( |
|
185 |
invoice => 0, |
|
186 |
invnumber => $params{invnumber} || 'test_ap_transaction', |
|
187 |
amount => $amount, |
|
188 |
netamount => $netamount, |
|
189 |
transdate => $transdate1, |
|
190 |
taxincluded => 0, |
|
191 |
vendor_id => $vendor->id, |
|
192 |
taxzone_id => $vendor->taxzone_id, |
|
193 |
currency_id => $currency_id, |
|
194 |
transactions => [], |
|
195 |
notes => 'test_ap_transaction', |
|
196 |
); |
|
197 |
$invoice->add_ap_amount_row( |
|
198 |
amount => $invoice->netamount, |
|
199 |
chart => $ap_amount_chart, |
|
200 |
tax_id => $tax_9->id, |
|
201 |
); |
|
202 |
|
|
203 |
$invoice->create_ap_row(chart => $ap_chart); |
|
204 |
$invoice->save; |
|
205 |
|
|
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'); |
|
210 |
|
|
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'); |
|
213 |
|
|
214 |
return $invoice; |
|
215 |
}; |
|
216 |
|
|
217 |
###### test cases |
|
218 |
|
|
219 |
sub test1 { |
|
220 |
|
|
221 |
my $testname = 'test1'; |
|
222 |
|
|
223 |
$ar_transaction = test_ar_transaction(invnumber => 'salesinv1'); |
|
224 |
|
|
225 |
my $bt = $ar_transaction->create_bank_transaction or die "Couldn't create bank_transaction"; |
|
226 |
|
|
227 |
$::form->{invoice_ids} = { |
|
228 |
$bt->id => [ $ar_transaction->id ] |
|
229 |
}; |
|
230 |
|
|
231 |
my $bt_controller = SL::Controller::BankTransaction->new; |
|
232 |
$bt_controller->action_save_invoices; |
|
233 |
|
|
234 |
$ar_transaction->load; |
|
235 |
$bt->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"); |
|
239 |
|
|
240 |
}; |
|
241 |
|
|
242 |
sub test_skonto_exact { |
|
243 |
|
|
244 |
my $testname = 'test_skonto_exact'; |
|
245 |
|
|
246 |
$ar_transaction = test_ar_transaction(invnumber => 'salesinv skonto', |
|
247 |
payment_id => $payment_terms->id, |
|
248 |
); |
|
249 |
|
|
250 |
my $bt = $ar_transaction->create_bank_transaction(amount => $ar_transaction->amount_less_skonto) or die "Couldn't create bank_transaction"; |
|
251 |
|
|
252 |
$::form->{invoice_ids} = { |
|
253 |
$bt->id => [ $ar_transaction->id ] |
|
254 |
}; |
|
255 |
$::form->{invoice_skontos} = { |
|
256 |
$bt->id => [ 'with_skonto_pt' ] |
|
257 |
}; |
|
258 |
|
|
259 |
my $bt_controller = SL::Controller::BankTransaction->new; |
|
260 |
$bt_controller->action_save_invoices; |
|
261 |
|
|
262 |
$ar_transaction->load; |
|
263 |
$bt->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"); |
|
267 |
|
|
268 |
}; |
|
269 |
|
|
270 |
sub test_two_invoices { |
|
271 |
|
|
272 |
my $testname = 'test_two_invoices'; |
|
273 |
|
|
274 |
my $ar_transaction_1 = test_ar_transaction(invnumber => 'salesinv_1'); |
|
275 |
my $ar_transaction_2 = test_ar_transaction(invnumber => 'salesinv_2'); |
|
276 |
|
|
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"; |
|
280 |
|
|
281 |
$::form->{invoice_ids} = { |
|
282 |
$bt->id => [ $ar_transaction_1->id, $ar_transaction_2->id ] |
|
283 |
}; |
|
284 |
|
|
285 |
my $bt_controller = SL::Controller::BankTransaction->new; |
|
286 |
$bt_controller->action_save_invoices; |
|
287 |
|
|
288 |
$ar_transaction_1->load; |
|
289 |
$ar_transaction_2->load; |
|
290 |
$bt->load; |
|
291 |
|
|
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"); |
|
297 |
|
|
298 |
}; |
|
299 |
|
|
300 |
sub test_overpayment { |
|
301 |
|
|
302 |
my $testname = 'test_overpayment'; |
|
303 |
|
|
304 |
$ar_transaction = test_ar_transaction(invnumber => 'salesinv overpaid'); |
|
305 |
|
|
306 |
# amount 135 > 119 |
|
307 |
my $bt = $ar_transaction->create_bank_transaction(amount => 135) or die "Couldn't create bank_transaction"; |
|
308 |
|
|
309 |
$::form->{invoice_ids} = { |
|
310 |
$bt->id => [ $ar_transaction->id ] |
|
311 |
}; |
|
312 |
|
|
313 |
my $bt_controller = SL::Controller::BankTransaction->new; |
|
314 |
$bt_controller->action_save_invoices; |
|
315 |
|
|
316 |
$ar_transaction->load; |
|
317 |
$bt->load; |
|
318 |
|
|
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 |
is($ar_transaction->closed , 0 , "$testname: 'salesinv overpaid' is open (via 'closed' method')"); |
|
322 |
is($ar_transaction->open_amount == 0 ? 1 : 0 , 0 , "$testname: 'salesinv overpaid is open (via amount-paid)"); |
|
323 |
|
|
324 |
}; |
|
325 |
|
|
326 |
sub test_overpayment_with_partialpayment { |
|
327 |
|
|
328 |
# two payments on different days, 10 and 119. If there is only one invoice we want it be overpaid. |
|
329 |
my $testname = 'test_overpayment_with_partialpayment'; |
|
330 |
|
|
331 |
$ar_transaction = test_ar_transaction(invnumber => 'salesinv overpaid partial'); |
|
332 |
|
|
333 |
my $bt_1 = $ar_transaction->create_bank_transaction(amount => 10) or die "Couldn't create bank_transaction"; |
|
334 |
my $bt_2 = $ar_transaction->create_bank_transaction(amount => 119, |
|
335 |
transdate => DateTime->today->add(days => 5), |
|
336 |
) or die "Couldn't create bank_transaction"; |
|
337 |
|
|
338 |
$::form->{invoice_ids} = { |
|
339 |
$bt_1->id => [ $ar_transaction->id ] |
|
340 |
}; |
|
341 |
my $bt_controller_1 = SL::Controller::BankTransaction->new; |
|
342 |
$bt_controller_1->action_save_invoices; |
|
343 |
|
|
344 |
$::form->{invoice_ids} = { |
|
345 |
$bt_2->id => [ $ar_transaction->id ] |
|
346 |
}; |
|
347 |
my $bt_controller_2 = SL::Controller::BankTransaction->new; |
|
348 |
$bt_controller_2->action_save_invoices; |
|
349 |
|
|
350 |
$ar_transaction->load; |
|
351 |
$bt_1->load; |
|
352 |
$bt_2->load; |
|
353 |
|
|
354 |
is($ar_transaction->paid , '129.00000' , "$testname: 'salesinv overpaid partial' was overpaid"); |
|
355 |
is($bt_1->invoice_amount , '10.00000' , "$testname: bt_1 invoice amount was assigned overpaid amount"); |
|
356 |
is($bt_2->invoice_amount , '119.00000' , "$testname: bt_2 invoice amount was assigned overpaid amount"); |
|
357 |
|
|
358 |
}; |
|
359 |
|
|
360 |
sub test_partial_payment { |
|
361 |
|
|
362 |
my $testname = 'test_partial_payment'; |
|
363 |
|
|
364 |
$ar_transaction = test_ar_transaction(invnumber => 'salesinv partial payment'); |
|
365 |
|
|
366 |
# amount 100 < 119 |
|
367 |
my $bt = $ar_transaction->create_bank_transaction(amount => 100) or die "Couldn't create bank_transaction"; |
|
368 |
|
|
369 |
$::form->{invoice_ids} = { |
|
370 |
$bt->id => [ $ar_transaction->id ] |
|
371 |
}; |
|
372 |
|
|
373 |
my $bt_controller = SL::Controller::BankTransaction->new; |
|
374 |
$bt_controller->action_save_invoices; |
|
375 |
|
|
376 |
$ar_transaction->load; |
|
377 |
$bt->load; |
|
378 |
|
|
379 |
is($ar_transaction->paid , '100.00000' , "$testname: 'salesinv partial payment' was partially paid"); |
|
380 |
is($bt->invoice_amount , '100.00000' , "$testname: bt invoice amount was assigned partially paid amount"); |
|
381 |
|
|
382 |
}; |
|
383 |
|
|
384 |
1; |
Auch abrufbar als: Unified diff
Test für Rechnungen bezahlen per 'Kontoauszug verbuchen'