1 package SL::Dev::Payment;
5 our @EXPORT = qw(create_payment_terms create_bank_account create_bank_transaction);
7 use SL::DB::PaymentTerm;
8 use SL::DB::BankAccount;
12 sub create_payment_terms {
15 my $payment_terms = SL::DB::PaymentTerm->new(
16 description => 'payment',
17 description_long => 'payment',
20 percent_skonto => '0.05',
21 auto_calculation => 1,
23 $payment_terms->assign_attributes(%params) if %params;
27 sub create_bank_account {
29 my $bank_account = SL::DB::BankAccount->new(
30 iban => 'DE12500105170648489890',
31 account_number => '0648489890',
33 chart_id => delete $params{chart_id} // $::instance_conf->get_ar_paid_accno_id,
34 name => 'Test bank account',
36 bank_code => '50010517'
38 $bank_account->assign_attributes(%params) if %params;
42 sub create_bank_transaction {
45 my $record = delete $params{record};
46 die "bank_transactions can only be created for invoices" unless ref($record) eq 'SL::DB::Invoice' or ref($record) eq 'SL::DB::PurchaseInvoice';
48 my $multiplier = $record->is_sales ? 1 : -1;
49 my $amount = (delete $params{amount} || $record->amount) * $multiplier;
52 if ( $params{bank_chart_id} ) {
53 $bank_chart = SL::DB::Manager::Chart->find_by(id => delete $params{bank_chart_id}) or die "Can't find bank chart";
54 } elsif ( $::instance_conf->get_ar_paid_accno_id ) {
55 $bank_chart = SL::DB::Manager::Chart->find_by(id => $::instance_conf->get_ar_paid_accno_id);
57 $bank_chart = SL::DB::Manager::Chart->find_by(description => 'Bank') or die "Can't find bank chart";
59 my $bank_account = SL::DB::Manager::BankAccount->find_by( chart_id => $bank_chart->id );
61 my $bt = SL::DB::BankTransaction->new(
62 local_bank_account_id => $bank_account->id,
63 remote_bank_code => $record->customervendor->bank_code,
64 remote_account_number => $record->customervendor->account_number,
65 transdate => DateTime->today,
66 valutadate => DateTime->today,
68 currency => $record->currency->id,
69 remote_name => $record->customervendor->depositor,
70 purpose => $record->invnumber
72 $bt->assign_attributes(%params) if %params;
82 SL::Dev::Payment - create objects for payment-related testing, with minimal defaults
86 =head2 C<create_payment_terms %PARAMS>
90 Minimal example with default values (30days, 5% skonto within 5 days):
91 my $payment_terms = SL::Dev::Record::create_payment_terms;
93 =head2 C<create_bank_account %PARAMS>
95 Required params: chart_id
98 my $bank_account = SL::Dev::Record::create_bank_account(chart_id => SL::DB::Manager::Chart->find_by(description => 'Bank')->id);
100 =head2 C<create_bank_transaction %PARAMS>
102 Create a bank transaction that matches an existing invoice record, e.g. to be able to
103 test the point system.
105 Required params: record (an SL::DB::Invoice or SL::DB::PurchaseInvoice object)
107 Optional params: bank_chart_id : the chart id of a configured bank account
108 amount : the amount of the bank transaction
110 If no bank_chart_id is given, it tries to find a chart via defaults
111 (ar_paid_accno_id) or by searching for the chart named "Bank". The chart must
112 be connected to an existing BankAccount.
114 Param amount should always be relative to the absolute amount of the invoice, i.e. use positive
115 values for sales and purchases.
118 my $payment_terms = SL::Dev::Record::create_payment_terms;
119 my $bank_chart = SL::DB::Manager::Chart->find_by(description => 'Bank');
120 my $bank_account = SL::Dev::Record::create_bank_account(chart_id => $bank_chart->id);
121 my $customer = SL::Dev::CustomerVendor::create_customer(iban => 'DE12500105170648489890',
123 account_number => '44444',
126 depositor => 'Name')->save;
127 my $sales_invoice = SL::Dev::Record::create_sales_invoice(customer => $customer,
128 payment_terms => $payment_terms,
130 my $bt = SL::Dev::Payment::create_bank_transaction(record => $sales_invoice,
131 amount => $sales_invoice->amount_less_skonto,
132 transdate => DateTime->today->add(days => 10),
133 bank_chart_id => $bank_chart->id
135 my ($agreement, $rule_matches) = $bt->get_agreement_with_invoice($sales_invoice);
136 # 14, 'remote_account_number(3) skonto_exact_amount(5) cust_vend_number_in_purpose(1) depositor_matches(2) payment_within_30_days(1) datebonus14(2)'
138 To create a payment for 3 invoices that were all paid together, all with skonto:
139 my $ar1 = SL::DB::Manager::Invoice->find_by(invnumber=>'20');
140 my $ar2 = SL::DB::Manager::Invoice->find_by(invnumber=>'21');
141 my $ar3 = SL::DB::Manager::Invoice->find_by(invnumber=>'22');
142 SL::Dev::Payment::create_bank_transaction(record => $ar1
143 amount => ($ar1->amount_less_skonto + $ar2->amount_less_skonto + $ar2->amount_less_skonto),
144 purpose => 'Rechnungen 20, 21, 22',
157 G. Richardson E<lt>grichardson@kivitendo-premium.deE<gt>