1 package SL::Dev::Payment;
5 our @EXPORT_OK = qw(create_payment_terms create_bank_account create_bank_transaction create_sepa_export create_sepa_export_item);
6 our %EXPORT_TAGS = (ALL => \@EXPORT_OK);
8 use SL::DB::PaymentTerm;
9 use SL::DB::BankAccount;
13 sub create_payment_terms {
16 my $payment_terms = SL::DB::PaymentTerm->new(
17 description => 'payment',
18 description_long => 'payment',
21 percent_skonto => '0.05',
22 auto_calculation => 1,
24 $payment_terms->assign_attributes(%params) if %params;
28 sub create_bank_account {
30 my $bank_account = SL::DB::BankAccount->new(
31 iban => 'DE12500105170648489890',
32 account_number => '0648489890',
34 chart_id => delete $params{chart_id} // $::instance_conf->get_ar_paid_accno_id,
35 name => 'Test bank account',
37 bank_code => '50010517'
39 $bank_account->assign_attributes(%params) if %params;
43 sub create_sepa_export {
45 my $sepa_export = SL::DB::SepaExport->new(
47 employee_id => $params{employee_id} // SL::DB::Manager::Employee->current->id,
51 $sepa_export->assign_attributes(%params) if %params;
55 sub create_sepa_export_item {
57 my $sepa_exportitem = SL::DB::SepaExportItem->new(
58 chart_id => delete $params{chart_id} // $::instance_conf->get_ar_paid_accno_id,
59 payment_type => 'without_skonto',
60 our_bic => 'BANK1234',
61 our_iban => 'DE12500105170648489890',
63 $sepa_exportitem->assign_attributes(%params) if %params;
64 $sepa_exportitem->save;
67 sub create_bank_transaction {
70 my $record = delete $params{record};
71 die "bank_transactions can only be created for invoices" unless ref($record) eq 'SL::DB::Invoice' or ref($record) eq 'SL::DB::PurchaseInvoice';
73 my $multiplier = $record->is_sales ? 1 : -1;
74 my $amount = (delete $params{amount} || $record->amount) * $multiplier;
77 if ( $params{bank_chart_id} ) {
78 $bank_chart = SL::DB::Manager::Chart->find_by(id => delete $params{bank_chart_id}) or die "Can't find bank chart";
79 } elsif ( $::instance_conf->get_ar_paid_accno_id ) {
80 $bank_chart = SL::DB::Manager::Chart->find_by(id => $::instance_conf->get_ar_paid_accno_id);
82 $bank_chart = SL::DB::Manager::Chart->find_by(description => 'Bank') or die "Can't find bank chart";
84 my $bank_account = SL::DB::Manager::BankAccount->find_by( chart_id => $bank_chart->id );
85 die "bank account missing" unless $bank_account;
87 my $bt = SL::DB::BankTransaction->new(
88 local_bank_account_id => $bank_account->id,
89 remote_bank_code => $record->customervendor->bank_code,
90 remote_account_number => $record->customervendor->account_number,
91 transdate => DateTime->today,
92 valutadate => DateTime->today,
94 currency => $record->currency->id,
95 remote_name => $record->customervendor->depositor,
96 purpose => $record->invnumber
98 $bt->assign_attributes(%params) if %params;
108 SL::Dev::Payment - create objects for payment-related testing, with minimal defaults
112 =head2 C<create_payment_terms %PARAMS>
114 Create payment terms.
116 Minimal example with default values (30days, 5% skonto within 5 days):
117 my $payment_terms = SL::Dev::Payment::create_payment_terms;
119 =head2 C<create_bank_account %PARAMS>
121 Required params: chart_id
124 my $bank_account = SL::Dev::Payment::create_bank_account(chart_id => SL::DB::Manager::Chart->find_by(description => 'Bank')->id);
126 =head2 C<create_bank_transaction %PARAMS>
128 Create a bank transaction that matches an existing invoice record, e.g. to be able to
129 test the point system.
131 Required params: record (an SL::DB::Invoice or SL::DB::PurchaseInvoice object)
133 Optional params: bank_chart_id : the chart id of a configured bank account
134 amount : the amount of the bank transaction
136 If no bank_chart_id is given, it tries to find a chart via defaults
137 (ar_paid_accno_id) or by searching for the chart named "Bank". The chart must
138 be connected to an existing BankAccount.
140 Param amount should always be relative to the absolute amount of the invoice, i.e. use positive
141 values for sales and purchases.
144 my $payment_terms = SL::Dev::Payment::create_payment_terms;
145 my $bank_chart = SL::DB::Manager::Chart->find_by(description => 'Bank');
146 my $bank_account = SL::Dev::Payment::create_bank_account(chart_id => $bank_chart->id);
147 my $customer = SL::Dev::CustomerVendor::create_customer(iban => 'DE12500105170648489890',
149 account_number => '44444',
152 depositor => 'Name')->save;
153 my $sales_invoice = SL::Dev::Record::create_sales_invoice(customer => $customer,
154 payment_terms => $payment_terms,
156 my $bt = SL::Dev::Payment::create_bank_transaction(record => $sales_invoice,
157 amount => $sales_invoice->amount_less_skonto,
158 transdate => DateTime->today->add(days => 10),
159 bank_chart_id => $bank_chart->id
161 my ($agreement, $rule_matches) = $bt->get_agreement_with_invoice($sales_invoice);
162 # 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)'
164 To create a payment for 3 invoices that were all paid together, all with skonto:
165 my $ar1 = SL::DB::Manager::Invoice->find_by(invnumber=>'20');
166 my $ar2 = SL::DB::Manager::Invoice->find_by(invnumber=>'21');
167 my $ar3 = SL::DB::Manager::Invoice->find_by(invnumber=>'22');
168 SL::Dev::Payment::create_bank_transaction(record => $ar1
169 amount => ($ar1->amount_less_skonto + $ar2->amount_less_skonto + $ar2->amount_less_skonto),
170 purpose => 'Rechnungen 20, 21, 22',
183 G. Richardson E<lt>grichardson@kivitendo-premium.deE<gt>