From b8e8bc08774f46903ca5a118afba3906a6d2c2e6 Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Thu, 11 Jul 2013 15:06:00 +0200 Subject: [PATCH] =?utf8?q?SL::DB::PaymentTerm::calc=5Fdate:=20Berechnung?= =?utf8?q?=20von=20F=C3=A4lligkeitsdatum=20anhand=20von=20terms=5Fnetto/te?= =?utf8?q?rms=5Fskonto?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- SL/DB/PaymentTerm.pm | 79 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/SL/DB/PaymentTerm.pm b/SL/DB/PaymentTerm.pm index 6a40a40ab..c087b7ffa 100644 --- a/SL/DB/PaymentTerm.pm +++ b/SL/DB/PaymentTerm.pm @@ -19,4 +19,83 @@ sub validate { return @errors; } +sub calc_date { + my ($self, %params) = @_; + + my $reference_date = $params{reference_date} || DateTime->today_local; + $reference_date = DateTime->from_kivitendo($reference_date) unless ref($reference_date) eq 'DateTime'; + + my $terms = ($params{terms} // 'net') eq 'discount' ? 'terms_skonto' : 'terms_netto'; + my $date = $reference_date->add(days => $self->$terms); + + my $dow = $date->day_of_week; + $date = $date->add(days => 8 - $dow) if $dow > 5; + + return $date; +} + 1; +__END__ + +=pod + +=encoding utf8 + +=head1 NAME + +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 + +=head1 FUNCTIONS + +=over 4 + +=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. + +C<%params> can contain the following parameters: + +=over 4 + +=item C + +The reference date from which the due date will be calculated. Can be +either an instance of L or a scalar in which case the scalar +is parsed via L. + +Defaults to the current date if unset. + +=item C + +Can be either C or C. For C the number of days to +add to the reference date are C<$self-Eterms_netto>. For +C C<$self-Eterms_skonto> is used. + +Defaults to C if unset. + +=back + +=item C + +Validates before saving and returns an array of human-readable error +messages in case of an error. + +=back + +=head1 BUGS + +Nothing here yet. + +=head1 AUTHOR + +Moritz Bunkus Em.bunkus@linet-services.deE + +=cut -- 2.20.1