epic-s6ts
[kivitendo-erp.git] / SL / DB / TimeRecording.pm
1 # This file has been auto-generated only because it didn't exist.
2 # Feel free to modify it at will; it will not be overwritten automatically.
3
4 package SL::DB::TimeRecording;
5
6 use strict;
7
8 use SL::Locale::String qw(t8);
9
10 use SL::DB::Helper::AttrDuration;
11 use SL::DB::Helper::AttrHTML;
12
13 use SL::DB::MetaSetup::TimeRecording;
14 use SL::DB::Manager::TimeRecording;
15
16 __PACKAGE__->meta->initialize;
17
18 __PACKAGE__->attr_duration_minutes(qw(duration));
19
20 __PACKAGE__->attr_html('description');
21
22 __PACKAGE__->before_save('_before_save_check_valid');
23
24 sub _before_save_check_valid {
25   my ($self) = @_;
26
27   my @errors = $self->validate;
28   return (scalar @errors == 0);
29 }
30
31 sub validate {
32   my ($self) = @_;
33
34   my @errors;
35
36   push @errors, t8('Customer must not be empty.')                              if !$self->customer_id;
37   push @errors, t8('Staff member must not be empty.')                          if !$self->staff_member_id;
38   push @errors, t8('Employee must not be empty.')                              if !$self->employee_id;
39   push @errors, t8('Description must not be empty.')                           if !$self->description;
40   push @errors, t8('Start time must be earlier than end time.')                if $self->is_time_in_wrong_order;
41   push @errors, t8('Assigned order must be a sales order.')                    if $self->order_id && 'sales_order' ne $self->order->type;
42   push @errors, t8('Customer of assigned order must match customer.')          if $self->order_id && $self->order->customer_id != $self->customer_id;
43   push @errors, t8('Customer of assigned project must match customer.')        if $self->project_id && $self->project->customer_id && $self->project->customer_id != $self->customer_id;
44   push @errors, t8('Project of assigned order must match assigned project.')
45     if $self->project_id && $self->order_id && $self->order->globalproject_id && $self->project_id != $self->order->globalproject_id;
46
47   my $conflict = $self->is_time_overlapping;
48   push @errors, t8('Entry overlaps with "#1".', $conflict->displayable_times)  if $conflict;
49
50   return @errors;
51 }
52
53 sub is_time_overlapping {
54   my ($self) = @_;
55
56   # Do not allow overlapping time periods.
57   # Start time can be equal to another end time
58   # (an end time can be equal to another start time)
59
60   # We cannot check if no staff member is given.
61   return if !$self->staff_member_id;
62
63   # If no start time and no end time are given, there is no overlapping.
64   return if !($self->start_time || $self->end_time);
65
66   my $conflicting;
67
68   # Start time or end time can be undefined.
69   if (!$self->start_time) {
70     $conflicting = SL::DB::Manager::TimeRecording->get_all(where  => [ and => [ '!id'           => $self->id,
71                                                                                 staff_member_id => $self->staff_member_id,
72                                                                                 start_time      => {lt => $self->end_time},
73                                                                                 end_time        => {ge => $self->end_time} ] ],
74                                                            sort_by => 'start_time DESC',
75                                                            limit   => 1);
76   } elsif (!$self->end_time) {
77     $conflicting = SL::DB::Manager::TimeRecording->get_all(where  => [ and => [ '!id'           => $self->id,
78                                                                                 staff_member_id => $self->staff_member_id,
79                                                                                 or              => [ and => [start_time => {le => $self->start_time},
80                                                                                                              end_time   => {gt => $self->start_time} ],
81                                                                                                      start_time => $self->start_time,
82                                                                                 ],
83                                                                        ],
84                                                            ],
85                                                            sort_by => 'start_time DESC',
86                                                            limit   => 1);
87   } else {
88     $conflicting = SL::DB::Manager::TimeRecording->get_all(where  => [ and => [ '!id'           => $self->id,
89                                                                                 staff_member_id => $self->staff_member_id,
90                                                                                 or              => [ and => [ start_time => {lt => $self->end_time},
91                                                                                                               end_time   => {gt => $self->start_time} ] ,
92                                                                                                      or  => [ start_time => $self->start_time,
93                                                                                                               end_time   => $self->end_time, ],
94                                                                                 ]
95                                                                        ]
96                                                            ],
97                                                            sort_by => 'start_time DESC',
98                                                            limit   => 1);
99   }
100
101   return $conflicting->[0] if @$conflicting;
102   return;
103 }
104
105 sub is_time_in_wrong_order {
106   my ($self) = @_;
107
108   if ($self->start_time && $self->end_time
109       && $self->start_time >= $self->end_time) {
110     return 1;
111   }
112
113   return;
114 }
115
116 sub is_duration_used {
117   return !$_[0]->start_time;
118 }
119
120 sub displayable_times {
121   my ($self) = @_;
122
123   my $text;
124
125   if ($self->is_duration_used) {
126     $text = $self->date_as_date . ': ' . ($self->duration_as_duration_string || '--:--');
127
128   } else {
129     # placeholder
130     my $ph =  $::locale->format_date_object(DateTime->new(year => 1111, month => 11, day => 11, hour => 11, minute => 11), precision => 'minute');
131     $ph    =~ s{1}{-}g;
132     $text  =  ($self->start_time_as_timestamp||$ph) . ' - ' . ($self->end_time_as_timestamp||$ph);
133   }
134
135   return $text;
136 }
137
138 1;