]> wagnertech.de Git - mfinanz.git/blob - SL/Dev/Payment.pm
Merge branch 'master' of http://wagnertech.de/git/mfinanz
[mfinanz.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->invoice_type =~ m/^purchase_invoice$|^ap_transaction$|^credit_note$/   ? -1
74                 : $record->invoice_type =~ m/invoice|ar_transaction|^purchase_credit_note$/       ?  1
75                 : die "invalid state";
76  my $amount = (delete $params{amount} || $record->amount) * $multiplier;
77
78  my $bank_chart;
79  if ( $params{bank_chart_id} ) {
80    $bank_chart = SL::DB::Manager::Chart->find_by(id => delete $params{bank_chart_id}) or die "Can't find bank chart";
81  } elsif ( $::instance_conf->get_ar_paid_accno_id ) {
82    $bank_chart   = SL::DB::Manager::Chart->find_by(id => $::instance_conf->get_ar_paid_accno_id);
83  } else {
84    $bank_chart = SL::DB::Manager::Chart->find_by(description => 'Bank') or die "Can't find bank chart";
85  }
86  my $bank_account = SL::DB::Manager::BankAccount->find_by( chart_id => $bank_chart->id );
87  die "bank account missing" unless $bank_account;
88
89  my $bt = SL::DB::BankTransaction->new(
90    local_bank_account_id => $bank_account->id,
91    remote_bank_code      => $record->customervendor->bank_code,
92    remote_account_number => $record->customervendor->account_number,
93    transdate             => DateTime->today,
94    valutadate            => DateTime->today,
95    amount                => $amount,
96    currency              => $record->currency->id,
97    remote_name           => $record->customervendor->depositor,
98    purpose               => $record->invnumber
99  );
100  $bt->assign_attributes(%params) if %params;
101  $bt->save;
102 }
103
104 1;
105
106 __END__
107
108 =head1 NAME
109
110 SL::Dev::Payment - create objects for payment-related testing, with minimal defaults
111
112 =head1 FUNCTIONS
113
114 =head2 C<create_payment_terms %PARAMS>
115
116 Create payment terms.
117
118 Minimal example with default values (30days, 5% skonto within 5 days):
119   my $payment_terms = SL::Dev::Payment::create_payment_terms;
120
121 =head2 C<create_bank_account %PARAMS>
122
123 Required params: chart_id
124
125 Example:
126   my $bank_account = SL::Dev::Payment::create_bank_account(chart_id => SL::DB::Manager::Chart->find_by(description => 'Bank')->id);
127
128 =head2 C<create_bank_transaction %PARAMS>
129
130 Create a bank transaction that matches an existing invoice record, e.g. to be able to
131 test the point system.
132
133 Required params: record  (an SL::DB::Invoice or SL::DB::PurchaseInvoice object)
134
135 Optional params: bank_chart_id : the chart id of a configured bank account
136                  amount        : the amount of the bank transaction
137
138 If no bank_chart_id is given, it tries to find a chart via defaults
139 (ar_paid_accno_id) or by searching for the chart named "Bank". The chart must
140 be connected to an existing BankAccount.
141
142 Param amount should always be relative to the absolute amount of the invoice, i.e. use positive
143 values for sales and purchases.
144
145 Example:
146   my $payment_terms = SL::Dev::Payment::create_payment_terms;
147   my $bank_chart    = SL::DB::Manager::Chart->find_by(description => 'Bank');
148   my $bank_account  = SL::Dev::Payment::create_bank_account(chart_id => $bank_chart->id);
149   my $customer      = SL::Dev::CustomerVendor::create_customer(iban           => 'DE12500105170648489890',
150                                                                bank_code      => 'abc',
151                                                                account_number => '44444',
152                                                                bank           => 'Testbank',
153                                                                bic            => 'foobar',
154                                                                depositor      => 'Name')->save;
155   my $sales_invoice = SL::Dev::Record::create_sales_invoice(customer      => $customer,
156                                                             payment_terms => $payment_terms,
157                                                            );
158   my $bt            = SL::Dev::Payment::create_bank_transaction(record        => $sales_invoice,
159                                                                 amount        => $sales_invoice->amount_less_skonto,
160                                                                 transdate     => DateTime->today->add(days => 10),
161                                                                 bank_chart_id => $bank_chart->id
162                                                                );
163   my ($agreement, $rule_matches) = $bt->get_agreement_with_invoice($sales_invoice);
164   # 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)'
165
166 To create a payment for 3 invoices that were all paid together, all with skonto:
167   my $ar1 = SL::DB::Manager::Invoice->find_by(invnumber=>'20');
168   my $ar2 = SL::DB::Manager::Invoice->find_by(invnumber=>'21');
169   my $ar3 = SL::DB::Manager::Invoice->find_by(invnumber=>'22');
170   SL::Dev::Payment::create_bank_transaction(record  => $ar1
171                                             amount  => ($ar1->amount_less_skonto + $ar2->amount_less_skonto + $ar2->amount_less_skonto),
172                                             purpose => 'Rechnungen 20, 21, 22',
173                                            );
174
175 =head1 TODO
176
177 Nothing here yet.
178
179 =head1 BUGS
180
181 Nothing here yet.
182
183 =head1 AUTHOR
184
185 G. Richardson E<lt>grichardson@kivitendo-premium.deE<gt>
186
187 =cut