ActionBar: existierende Inputs namens »action« vor Submit entfernen
[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_bank_transaction {
43  my (%params) = @_;
44
45  my $record = delete $params{record};
46  die "bank_transactions can only be created for invoices" unless ref($record) eq 'SL::DB::Invoice' or ref($record) eq 'SL::DB::PurchaseInvoice';
47
48  my $multiplier = $record->is_sales ? 1 : -1;
49  my $amount = (delete $params{amount} || $record->amount) * $multiplier;
50
51  my $bank_chart;
52  if ( $params{bank_chart_id} ) {
53    $bank_chart = SL::DB::Manager::Chart->find_by(id => delete $params{bank_chart_id}) or die "Can't find bank chart";
54  } elsif ( $::instance_conf->get_ar_paid_accno_id ) {
55    $bank_chart   = SL::DB::Manager::Chart->find_by(id => $::instance_conf->get_ar_paid_accno_id);
56  } else {
57    $bank_chart = SL::DB::Manager::Chart->find_by(description => 'Bank') or die "Can't find bank chart";
58  }
59  my $bank_account = SL::DB::Manager::BankAccount->find_by( chart_id => $bank_chart->id );
60
61  my $bt = SL::DB::BankTransaction->new(
62    local_bank_account_id => $bank_account->id,
63    remote_bank_code      => $record->customervendor->bank_code,
64    remote_account_number => $record->customervendor->account_number,
65    transdate             => DateTime->today,
66    valutadate            => DateTime->today,
67    amount                => $amount,
68    currency              => $record->currency->id,
69    remote_name           => $record->customervendor->depositor,
70    purpose               => $record->invnumber
71  );
72  $bt->assign_attributes(%params) if %params;
73  $bt->save;
74 }
75
76 1;
77
78 __END__
79
80 =head1 NAME
81
82 SL::Dev::Payment - create objects for payment-related testing, with minimal defaults
83
84 =head1 FUNCTIONS
85
86 =head2 C<create_payment_terms %PARAMS>
87
88 Create payment terms.
89
90 Minimal example with default values (30days, 5% skonto within 5 days):
91   my $payment_terms = SL::Dev::Record::create_payment_terms;
92
93 =head2 C<create_bank_account %PARAMS>
94
95 Required params: chart_id
96
97 Example:
98   my $bank_account = SL::Dev::Record::create_bank_account(chart_id => SL::DB::Manager::Chart->find_by(description => 'Bank')->id);
99
100 =head2 C<create_bank_transaction %PARAMS>
101
102 Create a bank transaction that matches an existing invoice record, e.g. to be able to
103 test the point system.
104
105 Required params: record  (an SL::DB::Invoice or SL::DB::PurchaseInvoice object)
106
107 Optional params: bank_chart_id : the chart id of a configured bank account
108                  amount        : the amount of the bank transaction
109
110 If no bank_chart_id is given, it tries to find a chart via defaults
111 (ar_paid_accno_id) or by searching for the chart named "Bank". The chart must
112 be connected to an existing BankAccount.
113
114 Param amount should always be relative to the absolute amount of the invoice, i.e. use positive
115 values for sales and purchases.
116
117 Example:
118   my $payment_terms = SL::Dev::Record::create_payment_terms;
119   my $bank_chart    = SL::DB::Manager::Chart->find_by(description => 'Bank');
120   my $bank_account  = SL::Dev::Record::create_bank_account(chart_id => $bank_chart->id);
121   my $customer      = SL::Dev::CustomerVendor::create_customer(iban           => 'DE12500105170648489890',
122                                                                bank_code      => 'abc',
123                                                                account_number => '44444',
124                                                                bank           => 'Testbank',
125                                                                bic            => 'foobar',
126                                                                depositor      => 'Name')->save;
127   my $sales_invoice = SL::Dev::Record::create_sales_invoice(customer      => $customer,
128                                                             payment_terms => $payment_terms,
129                                                            );
130   my $bt            = SL::Dev::Payment::create_bank_transaction(record        => $sales_invoice,
131                                                                 amount        => $sales_invoice->amount_less_skonto,
132                                                                 transdate     => DateTime->today->add(days => 10),
133                                                                 bank_chart_id => $bank_chart->id
134                                                                );
135   my ($agreement, $rule_matches) = $bt->get_agreement_with_invoice($sales_invoice);
136   # 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)'
137
138 To create a payment for 3 invoices that were all paid together, all with skonto:
139   my $ar1 = SL::DB::Manager::Invoice->find_by(invnumber=>'20');
140   my $ar2 = SL::DB::Manager::Invoice->find_by(invnumber=>'21');
141   my $ar3 = SL::DB::Manager::Invoice->find_by(invnumber=>'22');
142   SL::Dev::Payment::create_bank_transaction(record  => $ar1
143                                             amount  => ($ar1->amount_less_skonto + $ar2->amount_less_skonto + $ar2->amount_less_skonto),
144                                             purpose => 'Rechnungen 20, 21, 22',
145                                            );
146
147 =head1 TODO
148
149 Nothing here yet.
150
151 =head1 BUGS
152
153 Nothing here yet.
154
155 =head1 AUTHOR
156
157 G. Richardson E<lt>grichardson@kivitendo-premium.deE<gt>
158
159 =cut