Zeiterfassung: Ein Recht dafür
[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) ],
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   $self->setup_list_action_bar;
47   $self->make_filter_summary;
48   $self->prepare_report;
49
50   $self->report_generator_list_objects(report => $self->{report}, objects => $self->models->get);
51 }
52
53 sub action_edit {
54   my ($self) = @_;
55
56   $::request->{layout}->use_javascript("${_}.js") for qw(kivi.TimeRecording ckeditor/ckeditor ckeditor/adapters/jquery kivi.Validator);
57
58   if ($self->time_recording->start_time) {
59     $self->{start_date} = $self->time_recording->start_time->to_kivitendo;
60     $self->{start_time} = $self->time_recording->start_time->to_kivitendo_time;
61   }
62   if ($self->time_recording->end_time) {
63     $self->{end_date}   = $self->time_recording->end_time->to_kivitendo;
64     $self->{end_time}   = $self->time_recording->end_time->to_kivitendo_time;
65   }
66
67   $self->setup_edit_action_bar;
68
69   $self->render('time_recording/form',
70                 title  => t8('Time Recording'),
71   );
72 }
73
74 sub action_save {
75   my ($self) = @_;
76
77   my @errors = $self->time_recording->validate;
78   if (@errors) {
79     $::form->error(t8('Saving the time recording entry failed: #1', join '<br>', @errors));
80     return;
81   }
82
83   if ( !eval { $self->time_recording->save; 1; } ) {
84     $::form->error(t8('Saving the time recording entry failed: #1', $EVAL_ERROR));
85     return;
86   }
87
88   $self->redirect_to(safe_callback());
89 }
90
91 sub action_delete {
92   my ($self) = @_;
93
94   $self->time_recording->delete;
95
96   $self->redirect_to(safe_callback());
97 }
98
99 sub init_time_recording {
100   my $time_recording = ($::form->{id}) ? SL::DB::TimeRecording->new(id => $::form->{id})->load
101                                        : SL::DB::TimeRecording->new(start_time => DateTime->now_local);
102
103   my %attributes = %{ $::form->{time_recording} || {} };
104
105   foreach my $type (qw(start end)) {
106     if ($::form->{$type . '_date'}) {
107       my $date = DateTime->from_kivitendo($::form->{$type . '_date'});
108       $attributes{$type . '_time'} = $date->clone;
109       if ($::form->{$type . '_time'}) {
110         my ($hour, $min) = split ':', $::form->{$type . '_time'};
111         $attributes{$type . '_time'}->set_hour($hour)  if $hour;
112         $attributes{$type . '_time'}->set_minute($min) if $min;
113       }
114     }
115   }
116
117   $attributes{staff_member_id} = $attributes{employee_id} = SL::DB::Manager::Employee->current->id;
118
119   $time_recording->assign_attributes(%attributes);
120
121   return $time_recording;
122 }
123
124 sub init_models {
125   SL::Controller::Helper::GetModels->new(
126     controller     => $_[0],
127     sorted         => \%sort_columns,
128     disable_plugin => 'paginated',
129     with_objects   => [ 'customer', 'type', 'project', 'staff_member', 'employee' ],
130   );
131 }
132
133 sub init_all_time_recording_types {
134   SL::DB::Manager::TimeRecordingType->get_all_sorted(query => [obsolete => 0]);
135 }
136
137 sub init_all_employees {
138   SL::DB::Manager::Employee->get_all_sorted;
139 }
140
141 sub check_auth {
142   $::auth->assert('time_recording');
143 }
144
145 sub prepare_report {
146   my ($self) = @_;
147
148   my $report      = SL::ReportGenerator->new(\%::myconfig, $::form);
149   $self->{report} = $report;
150
151   my @columns  = qw(start_time end_time customer type project description staff_member duration);
152
153   my %column_defs = (
154     start_time   => { text => t8('Start'),        sub => sub { $_[0]->start_time_as_timestamp },
155                       obj_link => sub { $self->url_for(action => 'edit', 'id' => $_[0]->id, callback => $self->models->get_callback) }  },
156     end_time     => { text => t8('End'),          sub => sub { $_[0]->end_time_as_timestamp },
157                       obj_link => sub { $self->url_for(action => 'edit', 'id' => $_[0]->id, callback => $self->models->get_callback) }  },
158     customer     => { text => t8('Customer'),     sub => sub { $_[0]->customer->displayable_name } },
159     type         => { text => t8('Type'),         sub => sub { $_[0]->type && $_[0]->type->abbreviation } },
160     project      => { text => t8('Project'),      sub => sub { $_[0]->project && $_[0]->project->displayable_name } },
161     description  => { text => t8('Description'),  sub => sub { $_[0]->description_as_stripped_html },
162                       raw_data => sub { $_[0]->description_as_restricted_html }, # raw_data only used for html(?)
163                       obj_link => sub { $self->url_for(action => 'edit', 'id' => $_[0]->id, callback => $self->models->get_callback) }  },
164     staff_member => { text => t8('Mitarbeiter'),  sub => sub { $_[0]->staff_member->safe_name } },
165     duration     => { text => t8('Duration'),     sub => sub { $_[0]->duration_as_duration_string },
166                       align => 'right'},
167   );
168
169   $report->set_options(
170     controller_class      => 'TimeRecording',
171     std_column_visibility => 1,
172     output_format         => 'HTML',
173     title                 => t8('Time Recordings'),
174     allow_pdf_export      => 1,
175     allow_csv_export      => 1,
176   );
177
178   $report->set_columns(%column_defs);
179   $report->set_column_order(@columns);
180   $report->set_export_options(qw(list filter));
181   $report->set_options_from_form;
182
183   $self->models->disable_plugin('paginated') if $report->{options}{output_format} =~ /^(pdf|csv)$/i;
184   #$self->models->add_additional_url_params();
185   $self->models->finalize;
186   $self->models->set_report_generator_sort_options(report => $report, sortable_columns => [keys %sort_columns]);
187
188   $report->set_options(
189     raw_top_info_text    => $self->render('time_recording/report_top',    { output => 0 }),
190     raw_bottom_info_text => $self->render('time_recording/report_bottom', { output => 0 }, models => $self->models),
191     attachment_basename  => t8('time_recordings') . strftime('_%Y%m%d', localtime time),
192   );
193 }
194
195 sub make_filter_summary {
196   my ($self) = @_;
197
198   my $filter = $::form->{filter} || {};
199   my @filter_strings;
200
201   my $staff_member = $filter->{staff_member_id} ? SL::DB::Employee->new(id => $filter->{staff_member_id})->load->safe_name : '';
202
203   my @filters = (
204     [ $filter->{"start_time:date::ge"},                        t8('From Start')      ],
205     [ $filter->{"start_time:date::le"},                        t8('To Start')        ],
206     [ $filter->{"customer"}->{"name:substr::ilike"},           t8('Customer')        ],
207     [ $filter->{"customer"}->{"customernumber:substr::ilike"}, t8('Customer Number') ],
208     [ $staff_member,                                           t8('Mitarbeiter')     ],
209   );
210
211   for (@filters) {
212     push @filter_strings, "$_->[1]: $_->[0]" if $_->[0];
213   }
214
215   $self->{filter_summary} = join ', ', @filter_strings;
216 }
217
218 sub setup_list_action_bar {
219   my ($self) = @_;
220
221   for my $bar ($::request->layout->get('actionbar')) {
222     $bar->add(
223       action => [
224         t8('Update'),
225         submit    => [ '#filter_form', { action => 'TimeRecording/list' } ],
226         accesskey => 'enter',
227       ],
228       action => [
229         t8('Add'),
230         link => $self->url_for(action => 'edit', callback => $self->models->get_callback),
231       ],
232     );
233   }
234 }
235
236 sub setup_edit_action_bar {
237   my ($self) = @_;
238
239   for my $bar ($::request->layout->get('actionbar')) {
240     $bar->add(
241       action => [
242         t8('Save'),
243         submit => [ '#form', { action => 'TimeRecording/save' } ],
244         checks => [ 'kivi.validate_form' ],
245       ],
246       action => [
247         t8('Delete'),
248         submit  => [ '#form', { action => 'TimeRecording/delete' } ],
249         only_if => $self->time_recording->id,
250       ],
251       action => [
252         t8('Cancel'),
253         link  => $self->url_for(safe_callback()),
254       ],
255     );
256   }
257 }
258
259 sub safe_callback {
260   $::form->{callback} || (action => 'list')
261 }
262
263 1;