epic-ts
[kivitendo-erp.git] / SL / DB / PaymentTerm.pm
1 package SL::DB::PaymentTerm;
2
3 use strict;
4
5 use List::Util qw(max);
6
7 use SL::DB::MetaSetup::PaymentTerm;
8 use SL::DB::Manager::PaymentTerm;
9 use SL::DB::Helper::ActsAsList;
10 use SL::DB::Helper::TranslatedAttributes;
11
12 __PACKAGE__->meta->initialize;
13
14 sub validate {
15   my ($self) = @_;
16
17   my @errors;
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;
20
21   return @errors;
22 }
23
24 sub calc_date {
25   my ($self, %params) = @_;
26
27   my $reference_date  = $params{reference_date} || DateTime->today_local;
28   $reference_date     = DateTime->from_kivitendo($reference_date) unless ref($reference_date) eq 'DateTime';
29
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';
33
34     return max $due_date, $reference_date;
35   }
36
37   my $terms           = ($params{terms} // 'net') eq 'discount' ? 'terms_skonto' : 'terms_netto';
38   my $date            = $reference_date->add(days => $self->$terms);
39
40   my $dow             = $date->day_of_week;
41   $date               = $date->add(days => 8 - $dow) if $dow > 5;
42
43   return $date;
44 }
45
46 1;
47 __END__
48
49 =pod
50
51 =encoding utf8
52
53 =head1 NAME
54
55 SL::DB::PaymentTerm - Rose model for the payment_terms table
56
57 =head1 SYNOPSIS
58
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
62
63 =head1 FUNCTIONS
64
65 =over 4
66
67 =item C<calc_date [%params]>
68
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.
73
74 Note that for automatic calculation the resulting date will be the
75 following Monday if the result falls on a weekend.
76
77 C<%params> can contain the following parameters:
78
79 =over 4
80
81 =item C<reference_date>
82
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>.
86
87 Defaults to the current date if unset.
88
89 =item C<due_date>
90
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.
94
95 =item C<terms>
96
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.
100
101 Defaults to C<net> if unset.
102
103 =back
104
105 =item C<validate>
106
107 Validates before saving and returns an array of human-readable error
108 messages in case of an error.
109
110 =back
111
112 =head1 BUGS
113
114 Nothing here yet.
115
116 =head1 AUTHOR
117
118 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
119
120 =cut