Type in Übersetzung
[kivitendo-erp.git] / SL / Dev / Payment.pm
1 package SL::Dev::Payment;
2
3 use strict;
4 use base qw(Exporter);
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);
7
8 use SL::DB::PaymentTerm;
9 use SL::DB::BankAccount;
10 use SL::DB::Chart;
11 use DateTime;
12
13 sub create_payment_terms {
14   my (%params) = @_;
15
16   my $payment_terms =  SL::DB::PaymentTerm->new(
17     description      => 'payment',
18     description_long => 'payment',
19     terms_netto      => '30',
20     terms_skonto     => '5',
21     percent_skonto   => '0.05',
22     auto_calculation => 1,
23   );
24   $payment_terms->assign_attributes(%params) if %params;
25   $payment_terms->save;
26 }
27
28 sub create_bank_account {
29   my (%params) = @_;
30   my $bank_account = SL::DB::BankAccount->new(
31     iban           => 'DE12500105170648489890',
32     account_number => '0648489890',
33     bank           => 'Testbank',
34     chart_id       => delete $params{chart_id} // $::instance_conf->get_ar_paid_accno_id,
35     name           => 'Test bank account',
36     bic            => 'BANK1234',
37     bank_code      => '50010517'
38   );
39   $bank_account->assign_attributes(%params) if %params;
40   $bank_account->save;
41 }
42
43 sub create_sepa_export {
44   my (%params) = @_;
45   my $sepa_export = SL::DB::SepaExport->new(
46     closed       => 0,
47     employee_id  => $params{employee_id} // SL::DB::Manager::Employee->current->id,
48     executed     => 0,
49     vc           => 'customer',
50   );
51   $sepa_export->assign_attributes(%params) if %params;
52   $sepa_export->save;
53 }
54
55 sub create_sepa_export_item {
56   my (%params) = @_;
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',
62   );
63   $sepa_exportitem->assign_attributes(%params) if %params;
64   $sepa_exportitem->save;
65 }
66
67 sub create_bank_transaction {
68  my (%params) = @_;
69
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';
72
73  my $multiplier = $record->is_sales ? 1 : -1;
74  my $amount = (delete $params{amount} || $record->amount) * $multiplier;
75
76  my $bank_chart;
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);
81  } else {
82    $bank_chart = SL::DB::Manager::Chart->find_by(description => 'Bank') or die "Can't find bank chart";
83  }
84  my $bank_account = SL::DB::Manager::BankAccount->find_by( chart_id => $bank_chart->id );
85
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,
92    amount                => $amount,
93    currency              => $record->currency->id,
94    remote_name           => $record->customervendor->depositor,
95    purpose               => $record->invnumber
96  );
97  $bt->assign_attributes(%params) if %params;
98  $bt->save;
99 }
100
101 1;
102
103 __END__
104
105 =head1 NAME
106
107 SL::Dev::Payment - create objects for payment-related testing, with minimal defaults
108
109 =head1 FUNCTIONS
110
111 =head2 C<create_payment_terms %PARAMS>
112
113 Create payment terms.
114
115 Minimal example with default values (30days, 5% skonto within 5 days):
116   my $payment_terms = SL::Dev::Payment::create_payment_terms;
117
118 =head2 C<create_bank_account %PARAMS>
119
120 Required params: chart_id
121
122 Example:
123   my $bank_account = SL::Dev::Payment::create_bank_account(chart_id => SL::DB::Manager::Chart->find_by(description => 'Bank')->id);
124
125 =head2 C<create_bank_transaction %PARAMS>
126
127 Create a bank transaction that matches an existing invoice record, e.g. to be able to
128 test the point system.
129
130 Required params: record  (an SL::DB::Invoice or SL::DB::PurchaseInvoice object)
131
132 Optional params: bank_chart_id : the chart id of a configured bank account
133                  amount        : the amount of the bank transaction
134
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.
138
139 Param amount should always be relative to the absolute amount of the invoice, i.e. use positive
140 values for sales and purchases.
141
142 Example:
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',
147                                                                bank_code      => 'abc',
148                                                                account_number => '44444',
149                                                                bank           => 'Testbank',
150                                                                bic            => 'foobar',
151                                                                depositor      => 'Name')->save;
152   my $sales_invoice = SL::Dev::Record::create_sales_invoice(customer      => $customer,
153                                                             payment_terms => $payment_terms,
154                                                            );
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
159                                                                );
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)'
162
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',
170                                            );
171
172 =head1 TODO
173
174 Nothing here yet.
175
176 =head1 BUGS
177
178 Nothing here yet.
179
180 =head1 AUTHOR
181
182 G. Richardson E<lt>grichardson@kivitendo-premium.deE<gt>
183
184 =cut