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 );
86 my $bt = SL::DB::BankTransaction->new(
87 local_bank_account_id => $bank_account->id,
88 remote_bank_code => $record->customervendor->bank_code,
89 remote_account_number => $record->customervendor->account_number,
90 transdate => DateTime->today,
91 valutadate => DateTime->today,
93 currency => $record->currency->id,
94 remote_name => $record->customervendor->depositor,
95 purpose => $record->invnumber
97 $bt->assign_attributes(%params) if %params;
107 SL::Dev::Payment - create objects for payment-related testing, with minimal defaults
111 =head2 C<create_payment_terms %PARAMS>
113 Create payment terms.
115 Minimal example with default values (30days, 5% skonto within 5 days):
116 my $payment_terms = SL::Dev::Payment::create_payment_terms;
118 =head2 C<create_bank_account %PARAMS>
120 Required params: chart_id
123 my $bank_account = SL::Dev::Payment::create_bank_account(chart_id => SL::DB::Manager::Chart->find_by(description => 'Bank')->id);
125 =head2 C<create_bank_transaction %PARAMS>
127 Create a bank transaction that matches an existing invoice record, e.g. to be able to
128 test the point system.
130 Required params: record (an SL::DB::Invoice or SL::DB::PurchaseInvoice object)
132 Optional params: bank_chart_id : the chart id of a configured bank account
133 amount : the amount of the bank transaction
135 If no bank_chart_id is given, it tries to find a chart via defaults
136 (ar_paid_accno_id) or by searching for the chart named "Bank". The chart must
137 be connected to an existing BankAccount.
139 Param amount should always be relative to the absolute amount of the invoice, i.e. use positive
140 values for sales and purchases.
143 my $payment_terms = SL::Dev::Payment::create_payment_terms;
144 my $bank_chart = SL::DB::Manager::Chart->find_by(description => 'Bank');
145 my $bank_account = SL::Dev::Payment::create_bank_account(chart_id => $bank_chart->id);
146 my $customer = SL::Dev::CustomerVendor::create_customer(iban => 'DE12500105170648489890',
148 account_number => '44444',
151 depositor => 'Name')->save;
152 my $sales_invoice = SL::Dev::Record::create_sales_invoice(customer => $customer,
153 payment_terms => $payment_terms,
155 my $bt = SL::Dev::Payment::create_bank_transaction(record => $sales_invoice,
156 amount => $sales_invoice->amount_less_skonto,
157 transdate => DateTime->today->add(days => 10),
158 bank_chart_id => $bank_chart->id
160 my ($agreement, $rule_matches) = $bt->get_agreement_with_invoice($sales_invoice);
161 # 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)'
163 To create a payment for 3 invoices that were all paid together, all with skonto:
164 my $ar1 = SL::DB::Manager::Invoice->find_by(invnumber=>'20');
165 my $ar2 = SL::DB::Manager::Invoice->find_by(invnumber=>'21');
166 my $ar3 = SL::DB::Manager::Invoice->find_by(invnumber=>'22');
167 SL::Dev::Payment::create_bank_transaction(record => $ar1
168 amount => ($ar1->amount_less_skonto + $ar2->amount_less_skonto + $ar2->amount_less_skonto),
169 purpose => 'Rechnungen 20, 21, 22',
182 G. Richardson E<lt>grichardson@kivitendo-premium.deE<gt>