X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FDB%2FPaymentTerm.pm;h=2f62bd9ac5790fb8b8b059918c6c2341bd31433f;hb=242119d5c072981ca16fdaef4546ce2e1b8cc314;hp=c087b7ffae4decb17da214c4352a95f708fe7d3c;hpb=c312836ce432677d4f7876f1e3a2b6d99f433cd6;p=kivitendo-erp.git diff --git a/SL/DB/PaymentTerm.pm b/SL/DB/PaymentTerm.pm index c087b7ffa..2f62bd9ac 100644 --- a/SL/DB/PaymentTerm.pm +++ b/SL/DB/PaymentTerm.pm @@ -2,6 +2,8 @@ package SL::DB::PaymentTerm; use strict; +use List::Util qw(max); + use SL::DB::MetaSetup::PaymentTerm; use SL::DB::Manager::PaymentTerm; use SL::DB::Helper::ActsAsList; @@ -25,8 +27,15 @@ sub calc_date { my $reference_date = $params{reference_date} || DateTime->today_local; $reference_date = DateTime->from_kivitendo($reference_date) unless ref($reference_date) eq 'DateTime'; + if (!$self->auto_calculation) { + my $due_date = $params{due_date} || $reference_date; + $due_date = DateTime->from_kivitendo($due_date) unless ref($due_date) eq 'DateTime'; + + return max $due_date, $reference_date; + } + my $terms = ($params{terms} // 'net') eq 'discount' ? 'terms_skonto' : 'terms_netto'; - my $date = $reference_date->add(days => $self->$terms); + my $date = $reference_date->clone->add(days => $self->$terms); my $dow = $date->day_of_week; $date = $date->add(days => 8 - $dow) if $dow > 5; @@ -48,8 +57,15 @@ SL::DB::PaymentTerm - Rose model for the payment_terms table =head1 SYNOPSIS my $terms = SL::DB::PaymentTerm->new(id => $::form->{payment_id})->load; - my $due_date_net = $erms->calc_date(terms => 'net'); # uses terms_netto - my $due_date_discount = $erms->calc_date(terms => 'discount'); # uses terms_skonto + my $due_date_net = $terms->calc_date(terms => 'net'); # uses terms_netto + my $due_date_discount = $terms->calc_date(terms => 'discount'); # uses terms_skonto + + # Calculate due date taking the existing invoice date and the due + # date entered by the user into account: + my $due_date = $terms->calc_date( + reference_date => $::form->{invdate}, + due_date => $::form->{duedate}, + ); =head1 FUNCTIONS @@ -58,8 +74,12 @@ SL::DB::PaymentTerm - Rose model for the payment_terms table =item C Calculates and returns a due date as an instance of L by -adding one of C<$self>'s terms fields. Note that the resulting date -will be the following Monday if the result falls on a weekend. +adding one of C<$self>'s terms fields if automatic calculation is on; +otherwise returns the currently-set due date (which must be provided) +or the reference date, whichever is later. + +Note that for automatic calculation the resulting date will be the +following Monday if the result falls on a weekend. C<%params> can contain the following parameters: @@ -73,6 +93,12 @@ is parsed via L. Defaults to the current date if unset. +=item C + +A currently set due date. If automatic calculation is off then this +date will be returned if it is provided and greater than or equal to +the C. Otherwise the reference date will be returned. + =item C Can be either C or C. For C the number of days to