Zeiterfassung: Recht berücksichtigen, Einträge von anderen (nicht) zu sehen
[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_time_recording_types all_employees can_view_all) ],
22 );
23
24
25 # safety
26 __PACKAGE__->run_before('check_auth');
27
28 #
29 # actions
30 #
31
32 my %sort_columns = (
33   start_time   => t8('Start'),
34   end_time     => t8('End'),
35   customer     => t8('Customer'),
36   type         => t8('Type'),
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 $time_recording = ($::form->{id}) ? SL::DB::TimeRecording->new(id => $::form->{id})->load
106                                        : SL::DB::TimeRecording->new(start_time => DateTime->now_local);
107
108   my %attributes = %{ $::form->{time_recording} || {} };
109
110   foreach my $type (qw(start end)) {
111     if ($::form->{$type . '_date'}) {
112       my $date = DateTime->from_kivitendo($::form->{$type . '_date'});
113       $attributes{$type . '_time'} = $date->clone;
114       if ($::form->{$type . '_time'}) {
115         my ($hour, $min) = split ':', $::form->{$type . '_time'};
116         $attributes{$type . '_time'}->set_hour($hour)  if $hour;
117         $attributes{$type . '_time'}->set_minute($min) if $min;
118       }
119     }
120   }
121
122   $attributes{staff_member_id} = $attributes{employee_id} = SL::DB::Manager::Employee->current->id;
123
124   $time_recording->assign_attributes(%attributes);
125
126   return $time_recording;
127 }
128
129 sub init_can_view_all {
130   $::auth->assert('time_recording_show_all', 1) || $::auth->assert('time_recording_edit_all', 1)
131 }
132
133 sub init_models {
134   my ($self) = @_;
135
136   my @where;
137   push @where, (staff_member_id => SL::DB::Manager::Employee->current->id) if !$self->can_view_all;
138
139   SL::Controller::Helper::GetModels->new(
140     controller     => $_[0],
141     sorted         => \%sort_columns,
142     disable_plugin => 'paginated',
143     query          => \@where,
144     with_objects   => [ 'customer', 'type', 'project', 'staff_member', 'employee' ],
145   );
146 }
147
148 sub init_all_time_recording_types {
149   SL::DB::Manager::TimeRecordingType->get_all_sorted(query => [obsolete => 0]);
150 }
151
152 sub init_all_employees {
153   SL::DB::Manager::Employee->get_all_sorted(query => [ deleted => 0 ]);
154 }
155
156 sub check_auth {
157   $::auth->assert('time_recording');
158 }
159
160 sub prepare_report {
161   my ($self) = @_;
162
163   my $report      = SL::ReportGenerator->new(\%::myconfig, $::form);
164   $self->{report} = $report;
165
166   my @columns  = qw(start_time end_time customer type project description staff_member duration);
167
168   my %column_defs = (
169     start_time   => { text => t8('Start'),        sub => sub { $_[0]->start_time_as_timestamp },
170                       obj_link => sub { $self->url_for(action => 'edit', 'id' => $_[0]->id, callback => $self->models->get_callback) }  },
171     end_time     => { text => t8('End'),          sub => sub { $_[0]->end_time_as_timestamp },
172                       obj_link => sub { $self->url_for(action => 'edit', 'id' => $_[0]->id, callback => $self->models->get_callback) }  },
173     customer     => { text => t8('Customer'),     sub => sub { $_[0]->customer->displayable_name } },
174     type         => { text => t8('Type'),         sub => sub { $_[0]->type && $_[0]->type->abbreviation } },
175     project      => { text => t8('Project'),      sub => sub { $_[0]->project && $_[0]->project->displayable_name } },
176     description  => { text => t8('Description'),  sub => sub { $_[0]->description_as_stripped_html },
177                       raw_data => sub { $_[0]->description_as_restricted_html }, # raw_data only used for html(?)
178                       obj_link => sub { $self->url_for(action => 'edit', 'id' => $_[0]->id, callback => $self->models->get_callback) }  },
179     staff_member => { text => t8('Mitarbeiter'),  sub => sub { $_[0]->staff_member->safe_name } },
180     duration     => { text => t8('Duration'),     sub => sub { $_[0]->duration_as_duration_string },
181                       align => 'right'},
182   );
183
184   $report->set_options(
185     controller_class      => 'TimeRecording',
186     std_column_visibility => 1,
187     output_format         => 'HTML',
188     title                 => t8('Time Recordings'),
189     allow_pdf_export      => 1,
190     allow_csv_export      => 1,
191   );
192
193   $report->set_columns(%column_defs);
194   $report->set_column_order(@columns);
195   $report->set_export_options(qw(list filter));
196   $report->set_options_from_form;
197
198   $self->models->disable_plugin('paginated') if $report->{options}{output_format} =~ /^(pdf|csv)$/i;
199   #$self->models->add_additional_url_params();
200   $self->models->finalize;
201   $self->models->set_report_generator_sort_options(report => $report, sortable_columns => [keys %sort_columns]);
202
203   $report->set_options(
204     raw_top_info_text    => $self->render('time_recording/report_top',    { output => 0 }),
205     raw_bottom_info_text => $self->render('time_recording/report_bottom', { output => 0 }, models => $self->models),
206     attachment_basename  => t8('time_recordings') . strftime('_%Y%m%d', localtime time),
207   );
208 }
209
210 sub make_filter_summary {
211   my ($self) = @_;
212
213   my $filter = $::form->{filter} || {};
214   my @filter_strings;
215
216   my $staff_member = $filter->{staff_member_id} ? SL::DB::Employee->new(id => $filter->{staff_member_id})->load->safe_name : '';
217
218   my @filters = (
219     [ $filter->{"start_time:date::ge"},                        t8('From Start')      ],
220     [ $filter->{"start_time:date::le"},                        t8('To Start')        ],
221     [ $filter->{"customer"}->{"name:substr::ilike"},           t8('Customer')        ],
222     [ $filter->{"customer"}->{"customernumber:substr::ilike"}, t8('Customer Number') ],
223     [ $staff_member,                                           t8('Mitarbeiter')     ],
224   );
225
226   for (@filters) {
227     push @filter_strings, "$_->[1]: $_->[0]" if $_->[0];
228   }
229
230   $self->{filter_summary} = join ', ', @filter_strings;
231 }
232
233 sub setup_list_action_bar {
234   my ($self) = @_;
235
236   for my $bar ($::request->layout->get('actionbar')) {
237     $bar->add(
238       action => [
239         t8('Update'),
240         submit    => [ '#filter_form', { action => 'TimeRecording/list' } ],
241         accesskey => 'enter',
242       ],
243       action => [
244         t8('Add'),
245         link => $self->url_for(action => 'edit', callback => $self->models->get_callback),
246       ],
247     );
248   }
249 }
250
251 sub setup_edit_action_bar {
252   my ($self) = @_;
253
254   for my $bar ($::request->layout->get('actionbar')) {
255     $bar->add(
256       action => [
257         t8('Save'),
258         submit => [ '#form', { action => 'TimeRecording/save' } ],
259         checks => [ 'kivi.validate_form' ],
260       ],
261       action => [
262         t8('Delete'),
263         submit  => [ '#form', { action => 'TimeRecording/delete' } ],
264         only_if => $self->time_recording->id,
265       ],
266       action => [
267         t8('Cancel'),
268         link  => $self->url_for(safe_callback()),
269       ],
270     );
271   }
272 }
273
274 sub safe_callback {
275   $::form->{callback} || (action => 'list')
276 }
277
278 1;