BankTransaction: Überarbeitung von "Kontoauszug verbuchen" , SEPA-Export wieder integ...
[kivitendo-erp.git] / SL / Dev / Payment.pm
1 package SL::Dev::Payment;
2
3 use strict;
4 use base qw(Exporter);
5 our @EXPORT = qw(create_payment_terms create_bank_account create_bank_transaction);
6
7 use SL::DB::PaymentTerm;
8 use SL::DB::BankAccount;
9 use SL::DB::Chart;
10 use DateTime;
11
12 sub create_payment_terms {
13   my (%params) = @_;
14
15   my $payment_terms =  SL::DB::PaymentTerm->new(
16     description      => 'payment',
17     description_long => 'payment',
18     terms_netto      => '30',
19     terms_skonto     => '5',
20     percent_skonto   => '0.05',
21     auto_calculation => 1,
22   );
23   $payment_terms->assign_attributes(%params) if %params;
24   $payment_terms->save;
25 }
26
27 sub create_bank_account {
28   my (%params) = @_;
29   my $bank_account = SL::DB::BankAccount->new(
30     iban           => 'DE12500105170648489890',
31     account_number => '0648489890',
32     bank           => 'Testbank',
33     chart_id       => delete $params{chart_id} // $::instance_conf->get_ar_paid_accno_id,
34     name           => 'Test bank account',
35     bic            => 'BANK1234',
36     bank_code      => '50010517'
37   );
38   $bank_account->assign_attributes(%params) if %params;
39   $bank_account->save;
40 }
41
42 sub create_sepa_export {
43   my (%params) = @_;
44   my $sepa_export = SL::DB::SepaExport->new(
45     closed      => 0,
46     employee_id  => $params{employee_id} // SL::DB::Manager::Employee->current->id,
47     executed    => 0,
48     vc          => 'customer',
49   );
50   $sepa_export->assign_attributes(%params) if %params;
51   $sepa_export->save;
52 }
53
54 sub create_sepa_export_item {
55   my (%params) = @_;
56   my $sepa_exportitem = SL::DB::SepaExportItem->new(
57     chart_id       => delete $params{chart_id}       // $::instance_conf->get_ar_paid_accno_id,
58     payment_type   => 'without_skonto',
59     our_bic        => 'BANK1234',
60     our_iban       => 'DE12500105170648489890',
61   );
62   $sepa_exportitem->assign_attributes(%params) if %params;
63   $sepa_exportitem->save;
64 }
65
66 sub create_bank_transaction {
67  my (%params) = @_;
68
69  my $record = delete $params{record};
70  die "bank_transactions can only be created for invoices" unless ref($record) eq 'SL::DB::Invoice' or ref($record) eq 'SL::DB::PurchaseInvoice';
71
72  my $multiplier = $record->is_sales ? 1 : -1;
73  my $amount = (delete $params{amount} || $record->amount) * $multiplier;
74
75  my $bank_chart;
76  if ( $params{bank_chart_id} ) {
77    $bank_chart = SL::DB::Manager::Chart->find_by(id => delete $params{bank_chart_id}) or die "Can't find bank chart";
78  } elsif ( $::instance_conf->get_ar_paid_accno_id ) {
79    $bank_chart   = SL::DB::Manager::Chart->find_by(id => $::instance_conf->get_ar_paid_accno_id);
80  } else {
81    $bank_chart = SL::DB::Manager::Chart->find_by(description => 'Bank') or die "Can't find bank chart";
82  }
83  my $bank_account = SL::DB::Manager::BankAccount->find_by( chart_id => $bank_chart->id );
84
85  my $bt = SL::DB::BankTransaction->new(
86    local_bank_account_id => $bank_account->id,
87    remote_bank_code      => $record->customervendor->bank_code,
88    remote_account_number => $record->customervendor->account_number,
89    transdate             => DateTime->today,
90    valutadate            => DateTime->today,
91    amount                => $amount,
92    currency              => $record->currency->id,
93    remote_name           => $record->customervendor->depositor,
94    purpose               => $record->invnumber
95  );
96  $bt->assign_attributes(%params) if %params;
97  $bt->save;
98 }
99
100 1;
101
102 __END__
103
104 =head1 NAME
105
106 SL::Dev::Payment - create objects for payment-related testing, with minimal defaults
107
108 =head1 FUNCTIONS
109
110 =head2 C<create_payment_terms %PARAMS>
111
112 Create payment terms.
113
114 Minimal example with default values (30days, 5% skonto within 5 days):
115   my $payment_terms = SL::Dev::Payment::create_payment_terms;
116
117 =head2 C<create_bank_account %PARAMS>
118
119 Required params: chart_id
120
121 Example:
122   my $bank_account = SL::Dev::Payment::create_bank_account(chart_id => SL::DB::Manager::Chart->find_by(description => 'Bank')->id);
123
124 =head2 C<create_bank_transaction %PARAMS>
125
126 Create a bank transaction that matches an existing invoice record, e.g. to be able to
127 test the point system.
128
129 Required params: record  (an SL::DB::Invoice or SL::DB::PurchaseInvoice object)
130
131 Optional params: bank_chart_id : the chart id of a configured bank account
132                  amount        : the amount of the bank transaction
133
134 If no bank_chart_id is given, it tries to find a chart via defaults
135 (ar_paid_accno_id) or by searching for the chart named "Bank". The chart must
136 be connected to an existing BankAccount.
137
138 Param amount should always be relative to the absolute amount of the invoice, i.e. use positive
139 values for sales and purchases.
140
141 Example:
142   my $payment_terms = SL::Dev::Payment::create_payment_terms;
143   my $bank_chart    = SL::DB::Manager::Chart->find_by(description => 'Bank');
144   my $bank_account  = SL::Dev::Payment::create_bank_account(chart_id => $bank_chart->id);
145   my $customer      = SL::Dev::CustomerVendor::create_customer(iban           => 'DE12500105170648489890',
146                                                                bank_code      => 'abc',
147                                                                account_number => '44444',
148                                                                bank           => 'Testbank',
149                                                                bic            => 'foobar',
150                                                                depositor      => 'Name')->save;
151   my $sales_invoice = SL::Dev::Record::create_sales_invoice(customer      => $customer,
152                                                             payment_terms => $payment_terms,
153                                                            );
154   my $bt            = SL::Dev::Payment::create_bank_transaction(record        => $sales_invoice,
155                                                                 amount        => $sales_invoice->amount_less_skonto,
156                                                                 transdate     => DateTime->today->add(days => 10),
157                                                                 bank_chart_id => $bank_chart->id
158                                                                );
159   my ($agreement, $rule_matches) = $bt->get_agreement_with_invoice($sales_invoice);
160   # 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)'
161
162 To create a payment for 3 invoices that were all paid together, all with skonto:
163   my $ar1 = SL::DB::Manager::Invoice->find_by(invnumber=>'20');
164   my $ar2 = SL::DB::Manager::Invoice->find_by(invnumber=>'21');
165   my $ar3 = SL::DB::Manager::Invoice->find_by(invnumber=>'22');
166   SL::Dev::Payment::create_bank_transaction(record  => $ar1
167                                             amount  => ($ar1->amount_less_skonto + $ar2->amount_less_skonto + $ar2->amount_less_skonto),
168                                             purpose => 'Rechnungen 20, 21, 22',
169                                            );
170
171 =head1 TODO
172
173 Nothing here yet.
174
175 =head1 BUGS
176
177 Nothing here yet.
178
179 =head1 AUTHOR
180
181 G. Richardson E<lt>grichardson@kivitendo-premium.deE<gt>
182
183 =cut