Zeiterfassung: Keine Zeiterfassungstypen mehr
[kivitendo-erp.git] / SL / Controller / TimeRecording.pm
1 package SL::Controller::TimeRecording;
2
3 use strict;
4 use parent qw(SL::Controller::Base);
5
6 use DateTime;
7 use English qw(-no_match_vars);
8 use POSIX qw(strftime);
9
10 use SL::Controller::Helper::GetModels;
11 use SL::Controller::Helper::ReportGenerator;
12 use SL::DB::Customer;
13 use SL::DB::Employee;
14 use SL::DB::TimeRecording;
15 use SL::Locale::String qw(t8);
16 use SL::ReportGenerator;
17
18 use Rose::Object::MakeMethods::Generic
19 (
20 # scalar                  => [ qw() ],
21  'scalar --get_set_init' => [ qw(time_recording models all_employees can_view_all can_edit_all) ],
22 );
23
24
25 # safety
26 __PACKAGE__->run_before('check_auth');
27 __PACKAGE__->run_before('check_auth_edit', only => [ qw(edit save delete) ]);
28
29 #
30 # actions
31 #
32
33 my %sort_columns = (
34   start_time   => t8('Start'),
35   end_time     => t8('End'),
36   customer     => t8('Customer'),
37   project      => t8('Project'),
38   description  => t8('Description'),
39   staff_member => t8('Mitarbeiter'),
40   duration     => t8('Duration'),
41 );
42
43 sub action_list {
44   my ($self, %params) = @_;
45
46   $::form->{filter} //=  {
47     staff_member_id       => SL::DB::Manager::Employee->current->id,
48     "start_time:date::ge" => DateTime->now_local->add(weeks => -2)->to_kivitendo,
49   };
50
51   $self->setup_list_action_bar;
52   $self->make_filter_summary;
53   $self->prepare_report;
54
55   $self->report_generator_list_objects(report => $self->{report}, objects => $self->models->get);
56 }
57
58 sub action_edit {
59   my ($self) = @_;
60
61   $::request->{layout}->use_javascript("${_}.js") for qw(kivi.TimeRecording ckeditor/ckeditor ckeditor/adapters/jquery kivi.Validator);
62
63   if ($self->time_recording->start_time) {
64     $self->{start_date} = $self->time_recording->start_time->to_kivitendo;
65     $self->{start_time} = $self->time_recording->start_time->to_kivitendo_time;
66   }
67   if ($self->time_recording->end_time) {
68     $self->{end_date}   = $self->time_recording->end_time->to_kivitendo;
69     $self->{end_time}   = $self->time_recording->end_time->to_kivitendo_time;
70   }
71
72   $self->setup_edit_action_bar;
73
74   $self->render('time_recording/form',
75                 title  => t8('Time Recording'),
76   );
77 }
78
79 sub action_save {
80   my ($self) = @_;
81
82   my @errors = $self->time_recording->validate;
83   if (@errors) {
84     $::form->error(t8('Saving the time recording entry failed: #1', join '<br>', @errors));
85     return;
86   }
87
88   if ( !eval { $self->time_recording->save; 1; } ) {
89     $::form->error(t8('Saving the time recording entry failed: #1', $EVAL_ERROR));
90     return;
91   }
92
93   $self->redirect_to(safe_callback());
94 }
95
96 sub action_delete {
97   my ($self) = @_;
98
99   $self->time_recording->delete;
100
101   $self->redirect_to(safe_callback());
102 }
103
104 sub init_time_recording {
105   my $is_new         = !$::form->{id};
106   my $time_recording = $is_new ? SL::DB::TimeRecording->new(start_time => DateTime->now_local)
107                                : SL::DB::TimeRecording->new(id => $::form->{id})->load;
108
109   my %attributes = %{ $::form->{time_recording} || {} };
110
111   foreach my $type (qw(start end)) {
112     if ($::form->{$type . '_date'}) {
113       my $date = DateTime->from_kivitendo($::form->{$type . '_date'});
114       $attributes{$type . '_time'} = $date->clone;
115       if ($::form->{$type . '_time'}) {
116         my ($hour, $min) = split ':', $::form->{$type . '_time'};
117         $attributes{$type . '_time'}->set_hour($hour)  if $hour;
118         $attributes{$type . '_time'}->set_minute($min) if $min;
119       }
120     }
121   }
122
123   # do not overwright staff member if you do not have the right
124   delete $attributes{staff_member_id} if !$_[0]->can_edit_all;
125   $attributes{staff_member_id} = SL::DB::Manager::Employee->current->id if $is_new;
126
127   $attributes{employee_id}     = SL::DB::Manager::Employee->current->id;
128
129   $time_recording->assign_attributes(%attributes);
130
131   return $time_recording;
132 }
133
134 sub init_can_view_all {
135   $::auth->assert('time_recording_show_all', 1) || $::auth->assert('time_recording_edit_all', 1)
136 }
137
138 sub init_can_edit_all {
139   $::auth->assert('time_recording_edit_all', 1)
140 }
141
142 sub init_models {
143   my ($self) = @_;
144
145   my @where;
146   push @where, (staff_member_id => SL::DB::Manager::Employee->current->id) if !$self->can_view_all;
147
148   SL::Controller::Helper::GetModels->new(
149     controller     => $_[0],
150     sorted         => \%sort_columns,
151     disable_plugin => 'paginated',
152     query          => \@where,
153     with_objects   => [ 'customer', 'project', 'staff_member', 'employee' ],
154   );
155 }
156
157 sub init_all_employees {
158   SL::DB::Manager::Employee->get_all_sorted(query => [ deleted => 0 ]);
159 }
160
161 sub check_auth {
162   $::auth->assert('time_recording');
163 }
164
165 sub check_auth_edit {
166   my ($self) = @_;
167
168   if (!$self->can_edit_all && ($self->time_recording->staff_member_id != SL::DB::Manager::Employee->current->id)) {
169     $::form->error(t8('You do not have permission to access this entry.'));
170   }
171 }
172
173 sub prepare_report {
174   my ($self) = @_;
175
176   my $report      = SL::ReportGenerator->new(\%::myconfig, $::form);
177   $self->{report} = $report;
178
179   my @columns  = qw(start_time end_time customer project description staff_member duration);
180
181   my %column_defs = (
182     start_time   => { text => t8('Start'),        sub => sub { $_[0]->start_time_as_timestamp },
183                       obj_link => sub { $self->url_for(action => 'edit', 'id' => $_[0]->id, callback => $self->models->get_callback) }  },
184     end_time     => { text => t8('End'),          sub => sub { $_[0]->end_time_as_timestamp },
185                       obj_link => sub { $self->url_for(action => 'edit', 'id' => $_[0]->id, callback => $self->models->get_callback) }  },
186     customer     => { text => t8('Customer'),     sub => sub { $_[0]->customer->displayable_name } },
187     project      => { text => t8('Project'),      sub => sub { $_[0]->project && $_[0]->project->displayable_name } },
188     description  => { text => t8('Description'),  sub => sub { $_[0]->description_as_stripped_html },
189                       raw_data => sub { $_[0]->description_as_restricted_html }, # raw_data only used for html(?)
190                       obj_link => sub { $self->url_for(action => 'edit', 'id' => $_[0]->id, callback => $self->models->get_callback) }  },
191     staff_member => { text => t8('Mitarbeiter'),  sub => sub { $_[0]->staff_member->safe_name } },
192     duration     => { text => t8('Duration'),     sub => sub { $_[0]->duration_as_duration_string },
193                       align => 'right'},
194   );
195
196   $report->set_options(
197     controller_class      => 'TimeRecording',
198     std_column_visibility => 1,
199     output_format         => 'HTML',
200     title                 => t8('Time Recordings'),
201     allow_pdf_export      => 1,
202     allow_csv_export      => 1,
203   );
204
205   $report->set_columns(%column_defs);
206   $report->set_column_order(@columns);
207   $report->set_export_options(qw(list filter));
208   $report->set_options_from_form;
209
210   $self->models->disable_plugin('paginated') if $report->{options}{output_format} =~ /^(pdf|csv)$/i;
211   $self->models->add_additional_url_params(filter => $::form->{filter});
212   $self->models->finalize;
213   $self->models->set_report_generator_sort_options(report => $report, sortable_columns => [keys %sort_columns]);
214
215   $report->set_options(
216     raw_top_info_text    => $self->render('time_recording/report_top',    { output => 0 }),
217     raw_bottom_info_text => $self->render('time_recording/report_bottom', { output => 0 }, models => $self->models),
218     attachment_basename  => t8('time_recordings') . strftime('_%Y%m%d', localtime time),
219   );
220 }
221
222 sub make_filter_summary {
223   my ($self) = @_;
224
225   my $filter = $::form->{filter} || {};
226   my @filter_strings;
227
228   my $staff_member = $filter->{staff_member_id} ? SL::DB::Employee->new(id => $filter->{staff_member_id})->load->safe_name : '';
229
230   my @filters = (
231     [ $filter->{"start_time:date::ge"},                        t8('From Start')      ],
232     [ $filter->{"start_time:date::le"},                        t8('To Start')        ],
233     [ $filter->{"customer"}->{"name:substr::ilike"},           t8('Customer')        ],
234     [ $filter->{"customer"}->{"customernumber:substr::ilike"}, t8('Customer Number') ],
235     [ $staff_member,                                           t8('Mitarbeiter')     ],
236   );
237
238   for (@filters) {
239     push @filter_strings, "$_->[1]: $_->[0]" if $_->[0];
240   }
241
242   $self->{filter_summary} = join ', ', @filter_strings;
243 }
244
245 sub setup_list_action_bar {
246   my ($self) = @_;
247
248   for my $bar ($::request->layout->get('actionbar')) {
249     $bar->add(
250       action => [
251         t8('Update'),
252         submit    => [ '#filter_form', { action => 'TimeRecording/list' } ],
253         accesskey => 'enter',
254       ],
255       action => [
256         t8('Add'),
257         link => $self->url_for(action => 'edit', callback => $self->models->get_callback),
258       ],
259     );
260   }
261 }
262
263 sub setup_edit_action_bar {
264   my ($self) = @_;
265
266   for my $bar ($::request->layout->get('actionbar')) {
267     $bar->add(
268       action => [
269         t8('Save'),
270         submit => [ '#form', { action => 'TimeRecording/save' } ],
271         checks => [ 'kivi.validate_form' ],
272       ],
273       action => [
274         t8('Delete'),
275         submit  => [ '#form', { action => 'TimeRecording/delete' } ],
276         only_if => $self->time_recording->id,
277       ],
278       action => [
279         t8('Cancel'),
280         link  => $self->url_for(safe_callback()),
281       ],
282     );
283   }
284 }
285
286 sub safe_callback {
287   $::form->{callback} || (action => 'list')
288 }
289
290 1;