1 package SL::DB::PaymentTerm;
5 use SL::DB::MetaSetup::PaymentTerm;
6 use SL::DB::Manager::PaymentTerm;
7 use SL::DB::Helper::ActsAsList;
8 use SL::DB::Helper::TranslatedAttributes;
10 __PACKAGE__->meta->initialize;
16 push @errors, $::locale->text('The description is missing.') if !$self->description;
17 push @errors, $::locale->text('The long description is missing.') if !$self->description_long;
23 my ($self, %params) = @_;
25 my $reference_date = $params{reference_date} || DateTime->today_local;
26 $reference_date = DateTime->from_kivitendo($reference_date) unless ref($reference_date) eq 'DateTime';
28 my $terms = ($params{terms} // 'net') eq 'discount' ? 'terms_skonto' : 'terms_netto';
29 my $date = $reference_date->add(days => $self->$terms);
31 my $dow = $date->day_of_week;
32 $date = $date->add(days => 8 - $dow) if $dow > 5;
46 SL::DB::PaymentTerm - Rose model for the payment_terms table
50 my $terms = SL::DB::PaymentTerm->new(id => $::form->{payment_id})->load;
51 my $due_date_net = $erms->calc_date(terms => 'net'); # uses terms_netto
52 my $due_date_discount = $erms->calc_date(terms => 'discount'); # uses terms_skonto
58 =item C<calc_date [%params]>
60 Calculates and returns a due date as an instance of L<DateTime> by
61 adding one of C<$self>'s terms fields. Note that the resulting date
62 will be the following Monday if the result falls on a weekend.
64 C<%params> can contain the following parameters:
68 =item C<reference_date>
70 The reference date from which the due date will be calculated. Can be
71 either an instance of L<DateTime> or a scalar in which case the scalar
72 is parsed via L<DateTime/from_kivitendo>.
74 Defaults to the current date if unset.
78 Can be either C<net> or C<discount>. For C<net> the number of days to
79 add to the reference date are C<$self-E<gt>terms_netto>. For
80 C<discount> C<$self-E<gt>terms_skonto> is used.
82 Defaults to C<net> if unset.
88 Validates before saving and returns an array of human-readable error
89 messages in case of an error.
99 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>