SL::Dev::Payment eingeführt
[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{chart_id} ) {
53    $bank_chart = SL::DB::Manager::Chart->find_by(chart_id => $params{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 Required params: record  (an SL::DB::Invoice or SL::DB::PurchaseInvoice object)
103
104
105 $params{amount} should always be relative to the absolute amount of the invoice, i.e. use positive
106 values for sales and purchases.
107
108 Create a bank transaction that matches an existing invoice record, e.g. to be able to
109 test the point system.
110
111 Example:
112   my $payment_terms = SL::Dev::Record::create_payment_terms;
113   my $bank_chart    = SL::DB::Manager::Chart->find_by(description => 'Bank');
114   my $bank_account  = SL::Dev::Record::create_bank_account(chart_id => $bank_chart->id);
115   my $customer      = SL::Dev::CustomerVendor::create_customer(iban           => 'DE12500105170648489890',
116                                                                bank_code      => 'abc',
117                                                                account_number => '44444',
118                                                                bank           => 'Testbank',
119                                                                bic            => 'foobar',
120                                                                depositor      => 'Name')->save;
121   my $sales_invoice = SL::Dev::Record::create_sales_invoice(customer      => $customer,
122                                                             payment_terms => $payment_terms,
123                                                            );
124   my $bt            = SL::Dev::Payment::create_bank_transaction(record        => $sales_invoice,
125                                                                 amount        => $sales_invoice->amount_less_skonto,
126                                                                 transdate     => DateTime->today->add(days => 10),
127                                                                 bank_chart_id => $bank_chart->id
128                                                                );
129   my ($agreement, $rule_matches) = $bt->get_agreement_with_invoice($sales_invoice);
130   # 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)'
131
132 To create a payment for 3 invoices that were all paid together, all with skonto:
133   my $ar1 = SL::DB::Manager::Invoice->find_by(invnumber=>'20');
134   my $ar2 = SL::DB::Manager::Invoice->find_by(invnumber=>'21');
135   my $ar3 = SL::DB::Manager::Invoice->find_by(invnumber=>'22');
136   SL::Dev::Payment::create_bank_transaction(record  => $ar1
137                                             amount  => ($ar1->amount_less_skonto + $ar2->amount_less_skonto + $ar2->amount_less_skonto),
138                                             purpose => 'Rechnungen 20, 21, 22',
139                                            );
140 =head1 TODO
141
142 =head1 BUGS
143
144 Nothing here yet.
145
146 =head1 AUTHOR
147
148 G. Richardson E<lt>grichardson@kivitendo-premium.deE<gt>
149
150 =cut