SL::Dev::Payment eingeführt
authorG. Richardson <information@kivitendo-premium.de>
Wed, 1 Feb 2017 08:16:29 +0000 (09:16 +0100)
committerG. Richardson <information@kivitendo-premium.de>
Wed, 1 Feb 2017 12:12:54 +0000 (13:12 +0100)
Neue Funktionen zur Datenerstellung für Tests rund um Zahlungen.

create_bank_transaction aus Payment Helper nach SL::Dev verschoben.
neue Funktionen create_payment_terms und create_bank_account

SL/DB/Helper/Payment.pm
SL/Dev/ALL.pm
SL/Dev/Payment.pm [new file with mode: 0644]

index 4403b02..d371e11 100644 (file)
@@ -4,7 +4,7 @@ use strict;
 
 use parent qw(Exporter);
 our @EXPORT = qw(pay_invoice);
-our @EXPORT_OK = qw(skonto_date skonto_charts amount_less_skonto within_skonto_period percent_skonto reference_account reference_amount open_amount open_percent remaining_skonto_days skonto_amount check_skonto_configuration valid_skonto_amount get_payment_suggestions validate_payment_type open_sepa_transfer_amount get_payment_select_options_for_bank_transaction create_bank_transaction exchangerate forex);
+our @EXPORT_OK = qw(skonto_date skonto_charts amount_less_skonto within_skonto_period percent_skonto reference_account reference_amount open_amount open_percent remaining_skonto_days skonto_amount check_skonto_configuration valid_skonto_amount get_payment_suggestions validate_payment_type open_sepa_transfer_amount get_payment_select_options_for_bank_transaction exchangerate forex);
 our %EXPORT_TAGS = (
   "ALL" => [@EXPORT, @EXPORT_OK],
 );
@@ -723,41 +723,6 @@ sub validate_payment_type {
   return 1;
 }
 
-sub create_bank_transaction {
-  my ($self, %params) = @_;
-
-  require SL::DB::Chart;
-  require SL::DB::BankAccount;
-
-  my $bank_chart;
-  if ( $params{chart_id} ) {
-    $bank_chart = SL::DB::Manager::Chart->find_by(chart_id => $params{chart_id}) or die "Can't find bank chart";
-  } elsif ( $::instance_conf->get_ar_paid_accno_id ) {
-    $bank_chart   = SL::DB::Manager::Chart->find_by(id => $::instance_conf->get_ar_paid_accno_id);
-  } else {
-    $bank_chart = SL::DB::Manager::Chart->find_by(description => 'Bank') or die "Can't find bank chart";
-  };
-  my $bank_account = SL::DB::Manager::BankAccount->find_by(chart_id => $bank_chart->id) or die "Can't find bank account for chart";
-
-  my $multiplier = $self->is_sales ? 1 : -1;
-  my $amount = ($params{amount} || $self->amount) * $multiplier;
-
-  my $transdate = $params{transdate} || DateTime->today;
-
-  my $bt = SL::DB::BankTransaction->new(
-    local_bank_account_id => $bank_account->id,
-    remote_bank_code      => $self->customervendor->bank_code,
-    remote_account_number => $self->customervendor->account_number,
-    transdate             => $transdate,
-    valutadate            => $transdate,
-    amount                => $::form->round_amount($amount, 2),
-    currency              => $self->currency->id,
-    remote_name           => $self->customervendor->depositor,
-    purpose               => $params{purpose} || $self->invnumber
-  )->save;
-};
-
-
 sub forex {
   my ($self) = @_;
   $self->currency_id == $::instance_conf->get_currency_id ? return 0 : return 1;
@@ -1110,39 +1075,6 @@ If skonto is possible (skonto_date exists), add two possibilities:
 without_skonto and with_skonto_pt if payment date is within skonto_date,
 preselect with_skonto_pt, otherwise preselect without skonto.
 
-=item C<create_bank_transaction %params>
-
-Method used for testing purposes, allows you to quickly create bank
-transactions from invoices to have something to test payments against.
-
- my $ap = SL::DB::Manager::Invoice->find_by(id => 41);
- $ap->create_bank_transaction(amount => $ap->amount/2, transdate => DateTime->today->add(days => 5));
-
-To create a payment for 3 invoices that were all paid together, all with skonto:
- my $ar1 = SL::DB::Manager::Invoice->find_by(invnumber=>'20');
- my $ar2 = SL::DB::Manager::Invoice->find_by(invnumber=>'21');
- my $ar3 = SL::DB::Manager::Invoice->find_by(invnumber=>'22');
- $ar1->create_bank_transaction(amount  => ($ar1->amount_less_skonto + $ar2->amount_less_skonto + $ar2->amount_less_skonto),
-                               purpose => 'Rechnungen 20, 21, 22',
-                              );
-
-Amount is always relative to the absolute amount of the invoice, use positive
-values for sales and purchases.
-
-The following params can be passed to override the defaults:
-
-=over 2
-
-=item * amount
-
-=item * purpose
-
-=item * chart_id (the chart the amount is to be paid to)
-
-=item * transdate
-
-=back
-
 =item C<exchangerate>
 
 Returns 1 immediately if the record uses the default currency.
index 54666b7..988ab81 100644 (file)
@@ -6,6 +6,7 @@ use SL::Dev::Part;
 use SL::Dev::CustomerVendor;
 use SL::Dev::Inventory;
 use SL::Dev::Record;
+use SL::Dev::Payment;
 
 1;
 
diff --git a/SL/Dev/Payment.pm b/SL/Dev/Payment.pm
new file mode 100644 (file)
index 0000000..bce5c0f
--- /dev/null
@@ -0,0 +1,150 @@
+package SL::Dev::Payment;
+
+use strict;
+use base qw(Exporter);
+our @EXPORT = qw(create_payment_terms create_bank_account create_bank_transaction);
+
+use SL::DB::PaymentTerm;
+use SL::DB::BankAccount;
+use SL::DB::Chart;
+use DateTime;
+
+sub create_payment_terms {
+  my (%params) = @_;
+
+  my $payment_terms =  SL::DB::PaymentTerm->new(
+    description      => 'payment',
+    description_long => 'payment',
+    terms_netto      => '30',
+    terms_skonto     => '5',
+    percent_skonto   => '0.05',
+    auto_calculation => 1,
+  );
+  $payment_terms->assign_attributes(%params) if %params;
+  $payment_terms->save;
+}
+
+sub create_bank_account {
+  my (%params) = @_;
+  my $bank_account = SL::DB::BankAccount->new(
+    iban           => 'DE12500105170648489890',
+    account_number => '0648489890',
+    bank           => 'Testbank',
+    chart_id       => delete $params{chart_id} // $::instance_conf->get_ar_paid_accno_id,
+    name           => 'Test bank account',
+    bic            => 'BANK1234',
+    bank_code      => '50010517'
+  );
+  $bank_account->assign_attributes(%params) if %params;
+  $bank_account->save;
+}
+
+sub create_bank_transaction {
+ my (%params) = @_;
+
+ my $record = delete $params{record};
+ die "bank_transactions can only be created for invoices" unless ref($record) eq 'SL::DB::Invoice' or ref($record) eq 'SL::DB::PurchaseInvoice';
+
+ my $multiplier = $record->is_sales ? 1 : -1;
+ my $amount = (delete $params{amount} || $record->amount) * $multiplier;
+
+ my $bank_chart;
+ if ( $params{chart_id} ) {
+   $bank_chart = SL::DB::Manager::Chart->find_by(chart_id => $params{chart_id}) or die "Can't find bank chart";
+ } elsif ( $::instance_conf->get_ar_paid_accno_id ) {
+   $bank_chart   = SL::DB::Manager::Chart->find_by(id => $::instance_conf->get_ar_paid_accno_id);
+ } else {
+   $bank_chart = SL::DB::Manager::Chart->find_by(description => 'Bank') or die "Can't find bank chart";
+ }
+ my $bank_account = SL::DB::Manager::BankAccount->find_by( chart_id => $bank_chart->id );
+
+ my $bt = SL::DB::BankTransaction->new(
+   local_bank_account_id => $bank_account->id,
+   remote_bank_code      => $record->customervendor->bank_code,
+   remote_account_number => $record->customervendor->account_number,
+   transdate             => DateTime->today,
+   valutadate            => DateTime->today,
+   amount                => $amount,
+   currency              => $record->currency->id,
+   remote_name           => $record->customervendor->depositor,
+   purpose               => $record->invnumber
+ );
+ $bt->assign_attributes(%params) if %params;
+ $bt->save;
+}
+
+1;
+
+__END__
+
+=head1 NAME
+
+SL::Dev::Payment - create objects for payment-related testing, with minimal defaults
+
+=head1 FUNCTIONS
+
+=head2 C<create_payment_terms %PARAMS>
+
+Create payment terms.
+
+Minimal example with default values (30days, 5% skonto within 5 days):
+  my $payment_terms = SL::Dev::Record::create_payment_terms;
+
+=head2 C<create_bank_account %PARAMS>
+
+Required params: chart_id
+
+Example:
+  my $bank_account = SL::Dev::Record::create_bank_account(chart_id => SL::DB::Manager::Chart->find_by(description => 'Bank')->id);
+
+=head2 C<create_bank_transaction %PARAMS>
+
+Required params: record  (an SL::DB::Invoice or SL::DB::PurchaseInvoice object)
+
+
+$params{amount} should always be relative to the absolute amount of the invoice, i.e. use positive
+values for sales and purchases.
+
+Create a bank transaction that matches an existing invoice record, e.g. to be able to
+test the point system.
+
+Example:
+  my $payment_terms = SL::Dev::Record::create_payment_terms;
+  my $bank_chart    = SL::DB::Manager::Chart->find_by(description => 'Bank');
+  my $bank_account  = SL::Dev::Record::create_bank_account(chart_id => $bank_chart->id);
+  my $customer      = SL::Dev::CustomerVendor::create_customer(iban           => 'DE12500105170648489890',
+                                                               bank_code      => 'abc',
+                                                               account_number => '44444',
+                                                               bank           => 'Testbank',
+                                                               bic            => 'foobar',
+                                                               depositor      => 'Name')->save;
+  my $sales_invoice = SL::Dev::Record::create_sales_invoice(customer      => $customer,
+                                                            payment_terms => $payment_terms,
+                                                           );
+  my $bt            = SL::Dev::Payment::create_bank_transaction(record        => $sales_invoice,
+                                                                amount        => $sales_invoice->amount_less_skonto,
+                                                                transdate     => DateTime->today->add(days => 10),
+                                                                bank_chart_id => $bank_chart->id
+                                                               );
+  my ($agreement, $rule_matches) = $bt->get_agreement_with_invoice($sales_invoice);
+  # 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)'
+
+To create a payment for 3 invoices that were all paid together, all with skonto:
+  my $ar1 = SL::DB::Manager::Invoice->find_by(invnumber=>'20');
+  my $ar2 = SL::DB::Manager::Invoice->find_by(invnumber=>'21');
+  my $ar3 = SL::DB::Manager::Invoice->find_by(invnumber=>'22');
+  SL::Dev::Payment::create_bank_transaction(record  => $ar1
+                                            amount  => ($ar1->amount_less_skonto + $ar2->amount_less_skonto + $ar2->amount_less_skonto),
+                                            purpose => 'Rechnungen 20, 21, 22',
+                                           );
+=head1 TODO
+
+=head1 BUGS
+
+Nothing here yet.
+
+=head1 AUTHOR
+
+G. Richardson E<lt>grichardson@kivitendo-premium.deE<gt>
+
+=cut