1 package SL::DB::PaymentTerm;
5 use List::Util qw(max);
7 use SL::DB::MetaSetup::PaymentTerm;
8 use SL::DB::Manager::PaymentTerm;
9 use SL::DB::Helper::ActsAsList;
10 use SL::DB::Helper::TranslatedAttributes;
12 __PACKAGE__->meta->initialize;
18 push @errors, $::locale->text('The description is missing.') if !$self->description;
19 push @errors, $::locale->text('The long description is missing.') if !$self->description_long;
25 my ($self, %params) = @_;
27 my $reference_date = $params{reference_date} || DateTime->today_local;
28 $reference_date = DateTime->from_kivitendo($reference_date) unless ref($reference_date) eq 'DateTime';
30 if (!$self->auto_calculation) {
31 my $due_date = $params{due_date} || $reference_date;
32 $due_date = DateTime->from_kivitendo($due_date) unless ref($due_date) eq 'DateTime';
34 return max $due_date, $reference_date;
37 my $terms = ($params{terms} // 'net') eq 'discount' ? 'terms_skonto' : 'terms_netto';
38 my $date = $reference_date->add(days => $self->$terms);
40 my $dow = $date->day_of_week;
41 $date = $date->add(days => 8 - $dow) if $dow > 5;
55 SL::DB::PaymentTerm - Rose model for the payment_terms table
59 my $terms = SL::DB::PaymentTerm->new(id => $::form->{payment_id})->load;
60 my $due_date_net = $erms->calc_date(terms => 'net'); # uses terms_netto
61 my $due_date_discount = $erms->calc_date(terms => 'discount'); # uses terms_skonto
67 =item C<calc_date [%params]>
69 Calculates and returns a due date as an instance of L<DateTime> by
70 adding one of C<$self>'s terms fields if automatic calculation is on;
71 otherwise returns the currently-set due date (which must be provided)
72 or the reference date, whichever is later.
74 Note that for automatic calculation the resulting date will be the
75 following Monday if the result falls on a weekend.
77 C<%params> can contain the following parameters:
81 =item C<reference_date>
83 The reference date from which the due date will be calculated. Can be
84 either an instance of L<DateTime> or a scalar in which case the scalar
85 is parsed via L<DateTime/from_kivitendo>.
87 Defaults to the current date if unset.
91 A currently set due date. If automatic calculation is off then this
92 date will be returned if it is provided and greater than or equal to
93 the C<reference_date>. Otherwise the reference date will be returned.
97 Can be either C<net> or C<discount>. For C<net> the number of days to
98 add to the reference date are C<$self-E<gt>terms_netto>. For
99 C<discount> C<$self-E<gt>terms_skonto> is used.
101 Defaults to C<net> if unset.
107 Validates before saving and returns an array of human-readable error
108 messages in case of an error.
118 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>