1 package SL::Controller::TimeRecording;
4 use parent qw(SL::Controller::Base);
7 use English qw(-no_match_vars);
8 use List::Util qw(sum0);
9 use POSIX qw(strftime);
11 use SL::Controller::Helper::GetModels;
12 use SL::Controller::Helper::ReportGenerator;
13 use SL::Controller::Helper::ReportGenerator::ControlRow qw(make_control_row);
19 use SL::DB::TimeRecording;
20 use SL::DB::TimeRecordingArticle;
21 use SL::Helper::Flash qw(flash);
22 use SL::Helper::Number qw(_round_number _parse_number _round_total);
23 use SL::Helper::UserPreferences::TimeRecording;
24 use SL::Locale::String qw(t8);
25 use SL::ReportGenerator;
27 use Rose::Object::MakeMethods::Generic
30 'scalar --get_set_init' => [ qw(time_recording models all_employees all_time_recording_articles all_orders can_view_all can_edit_all use_duration) ],
35 __PACKAGE__->run_before('check_auth');
36 __PACKAGE__->run_before('check_auth_edit', only => [ qw(edit save delete) ]);
40 start_time => t8('Start'),
41 end_time => t8('End'),
42 order => t8('Sales Order'),
43 customer => t8('Customer'),
44 part => t8('Article'),
45 project => t8('Project'),
46 description => t8('Description'),
47 staff_member => t8('Mitarbeiter'),
48 duration => t8('Duration'),
49 booked => t8('Booked'),
57 my ($self, %params) = @_;
59 $::form->{filter} //= {
60 staff_member_id => SL::DB::Manager::Employee->current->id,
61 "date:date::ge" => DateTime->today_local->add(weeks => -2)->to_kivitendo,
64 $self->setup_list_action_bar;
65 $self->make_filter_summary;
66 $self->prepare_report;
68 my $objects = $self->models->get;
70 my $total = sum0 map { _round_total($_->duration_in_hours) } @$objects;
71 my $total_h = int($total);
72 my $total_m = int($total * 60.0 + 0.5) % 60;
73 my $total_s = sprintf('%d:%02d', $total_h, $total_m);
75 push @$objects, make_control_row("separator");
76 push @$objects, make_control_row("data",
78 map( { $_ => {class => 'listtotal'} } keys %{$self->{report}->{columns}} ),
79 description => {data => t8('Total'), class => 'listtotal'},
80 duration => {data => $total_s, class => 'listtotal'}
83 $self->report_generator_list_objects(report => $self->{report}, objects => $objects);
89 $::request->{layout}->use_javascript("${_}.js") for qw(kivi.TimeRecording ckeditor/ckeditor ckeditor/adapters/jquery kivi.Validator);
91 if ($self->use_duration) {
92 flash('warning', t8('This entry is using start and end time. This information will be overwritten on saving.')) if !$self->time_recording->is_duration_used;
94 flash('warning', t8('This entry is using date and duration. This information will be overwritten on saving.')) if $self->time_recording->is_duration_used;
97 if ($self->time_recording->start_time) {
98 $self->{start_date} = $self->time_recording->start_time->to_kivitendo;
99 $self->{start_time} = $self->time_recording->start_time->to_kivitendo_time;
101 if ($self->time_recording->end_time) {
102 $self->{end_date} = $self->time_recording->end_time->to_kivitendo;
103 $self->{end_time} = $self->time_recording->end_time->to_kivitendo_time;
106 my $inputs_to_disable = $self->get_inputs_to_disable;
108 $self->setup_edit_action_bar;
110 $self->render('time_recording/form',
111 title => t8('Time Recording'),
112 inputs_to_disable => $inputs_to_disable,
119 if ($self->use_duration) {
120 $self->time_recording->start_time(undef);
121 $self->time_recording->end_time(undef);
124 my @errors = $self->time_recording->validate;
126 $::form->error(t8('Saving the time recording entry failed: #1', join '<br>', @errors));
130 if ( !eval { $self->time_recording->save; 1; } ) {
131 $::form->error(t8('Saving the time recording entry failed: #1', $EVAL_ERROR));
135 $self->redirect_to(safe_callback());
141 $self->time_recording->delete;
143 $self->redirect_to(safe_callback());
146 sub action_ajaj_get_order_info {
148 my $order = SL::DB::Order->new(id => $::form->{id})->load;
149 my $data = { customer => { id => $order->customer_id,
150 value => $order->customer->displayable_name,
153 project => { id => $order->globalproject_id,
154 value => ($order->globalproject_id ? $order->globalproject->displayable_name : undef),
158 $_[0]->render(\SL::JSON::to_json($data), { type => 'json', process => 0 });
161 sub action_ajaj_get_project_info {
163 my $project = SL::DB::Project->new(id => $::form->{id})->load;
166 if ($project->customer_id) {
167 $data = { customer => { id => $project->customer_id,
168 value => $project->customer->displayable_name,
174 $_[0]->render(\SL::JSON::to_json($data), { type => 'json', process => 0 });
177 sub init_time_recording {
180 my $is_new = !$::form->{id};
181 my $time_recording = !$is_new ? SL::DB::TimeRecording->new(id => $::form->{id})->load
182 : $self->use_duration ? SL::DB::TimeRecording->new(date => DateTime->today_local)
183 : SL::DB::TimeRecording->new(start_time => DateTime->now_local);
185 my %attributes = %{ $::form->{time_recording} || {} };
187 if ($self->use_duration) {
188 if (exists $::form->{duration_h} || exists $::form->{duration_m}) {
189 $attributes{duration} = _round_number(_parse_number($::form->{duration_h}) * 60 + _parse_number($::form->{duration_m}), 0);
193 foreach my $type (qw(start end)) {
194 if ($::form->{$type . '_date'}) {
195 my $date = DateTime->from_kivitendo($::form->{$type . '_date'});
196 $attributes{$type . '_time'} = $date->clone;
197 if ($::form->{$type . '_time'}) {
198 my ($hour, $min) = split ':', $::form->{$type . '_time'};
199 $attributes{$type . '_time'}->set_hour($hour) if $hour;
200 $attributes{$type . '_time'}->set_minute($min) if $min;
206 # do not overwrite staff member if you do not have the right
207 delete $attributes{staff_member_id} if !$_[0]->can_edit_all;
208 $attributes{staff_member_id} ||= SL::DB::Manager::Employee->current->id if $is_new;
210 $attributes{employee_id} = SL::DB::Manager::Employee->current->id;
212 $time_recording->assign_attributes(%attributes);
214 return $time_recording;
217 sub init_can_view_all {
218 $::auth->assert('time_recording_show_all', 1) || $::auth->assert('time_recording_edit_all', 1)
221 sub init_can_edit_all {
222 $::auth->assert('time_recording_edit_all', 1)
229 push @where, (staff_member_id => SL::DB::Manager::Employee->current->id) if !$self->can_view_all;
231 SL::Controller::Helper::GetModels->new(
233 sorted => \%sort_columns,
234 disable_plugin => 'paginated',
236 with_objects => [ 'customer', 'part', 'project', 'staff_member', 'employee', 'order' ],
240 sub init_all_employees {
241 SL::DB::Manager::Employee->get_all_sorted(query => [ deleted => 0 ]);
244 sub init_all_time_recording_articles {
245 my $selectable_parts = SL::DB::Manager::TimeRecordingArticle->get_all_sorted(
246 query => [or => [ 'part.obsolete' => 0, 'part.obsolete' => undef ]],
247 with_objects => ['part']);
249 my $res = [ map { {id => $_->part_id, description => $_->part->displayable_name} } @$selectable_parts];
250 my $curr_id = $_[0]->time_recording->part_id;
252 if ($curr_id && !grep { $curr_id == $_->{id} } @$res) {
253 unshift @$res, {id => $curr_id, description => $_[0]->time_recording->part->displayable_name};
259 sub init_all_orders {
260 my $orders = SL::DB::Manager::Order->get_all(query => [or => [ closed => 0, closed => undef ],
261 or => [ quotation => 0, quotation => undef ],
262 '!customer_id' => undef]);
263 return [ map { [$_->id, sprintf("%s %s", $_->number, $_->customervendor->name) ] } sort { $a->number <=> $b->number } @{$orders||[]} ];
266 sub init_use_duration {
267 return SL::Helper::UserPreferences::TimeRecording->new()->get_use_duration();
271 $::auth->assert('time_recording');
274 sub check_auth_edit {
277 if (!$self->can_edit_all && ($self->time_recording->staff_member_id != SL::DB::Manager::Employee->current->id)) {
278 $::form->error(t8('You do not have permission to access this entry.'));
285 my $report = SL::ReportGenerator->new(\%::myconfig, $::form);
286 $self->{report} = $report;
288 my @columns = qw(date start_time end_time order customer project part description staff_member duration booked);
291 date => { text => t8('Date'), sub => sub { $_[0]->date_as_date },
292 obj_link => sub { $self->url_for(action => 'edit', 'id' => $_[0]->id, callback => $self->models->get_callback) } },
293 start_time => { text => t8('Start'), sub => sub { $_[0]->start_time_as_timestamp },
294 obj_link => sub { $self->url_for(action => 'edit', 'id' => $_[0]->id, callback => $self->models->get_callback) } },
295 end_time => { text => t8('End'), sub => sub { $_[0]->end_time_as_timestamp },
296 obj_link => sub { $self->url_for(action => 'edit', 'id' => $_[0]->id, callback => $self->models->get_callback) } },
297 order => { text => t8('Sales Order'), sub => sub { $_[0]->order && $_[0]->order->number } },
298 customer => { text => t8('Customer'), sub => sub { $_[0]->customer->displayable_name } },
299 part => { text => t8('Article'), sub => sub { $_[0]->part && $_[0]->part->displayable_name } },
300 project => { text => t8('Project'), sub => sub { $_[0]->project && $_[0]->project->full_description(sytle => 'both') } },
301 description => { text => t8('Description'), sub => sub { $_[0]->description_as_stripped_html },
302 raw_data => sub { $_[0]->description_as_restricted_html }, # raw_data only used for html(?)
303 obj_link => sub { $self->url_for(action => 'edit', 'id' => $_[0]->id, callback => $self->models->get_callback) } },
304 staff_member => { text => t8('Mitarbeiter'), sub => sub { $_[0]->staff_member->safe_name } },
305 duration => { text => t8('Duration'), sub => sub { $_[0]->duration_as_duration_string },
307 booked => { text => t8('Booked'), sub => sub { $_[0]->booked ? t8('Yes') : t8('No') } },
310 my $title = t8('Time Recordings');
311 $report->{title} = $title; # for browser titlebar (title-tag)
313 $report->set_options(
314 controller_class => 'TimeRecording',
315 std_column_visibility => 1,
316 output_format => 'HTML',
317 title => $title, # for heading
318 allow_pdf_export => 1,
319 allow_csv_export => 1,
322 $report->set_columns(%column_defs);
323 $report->set_column_order(@columns);
324 $report->set_export_options(qw(list filter));
325 $report->set_options_from_form;
327 $self->models->disable_plugin('paginated') if $report->{options}{output_format} =~ /^(pdf|csv)$/i;
328 $self->models->add_additional_url_params(filter => $::form->{filter});
329 $self->models->finalize;
330 $self->models->set_report_generator_sort_options(report => $report, sortable_columns => [keys %sort_columns]);
332 $report->set_options(
333 raw_top_info_text => $self->render('time_recording/report_top', { output => 0 }),
334 raw_bottom_info_text => $self->render('time_recording/report_bottom', { output => 0 }, models => $self->models),
335 attachment_basename => t8('time_recordings') . strftime('_%Y%m%d', localtime time),
339 sub make_filter_summary {
342 my $filter = $::form->{filter} || {};
345 my $staff_member = $filter->{staff_member_id} ? SL::DB::Employee->new(id => $filter->{staff_member_id})->load->safe_name : '';
346 my $project = $filter->{project_id} ? SL::DB::Project->new (id => $filter->{project_id}) ->load->full_description(sytle => 'both') : '';
349 [ $filter->{"date:date::ge"}, t8('From Date') ],
350 [ $filter->{"date:date::le"}, t8('To Date') ],
351 [ $filter->{"customer"}->{"name:substr::ilike"}, t8('Customer') ],
352 [ $filter->{"customer"}->{"customernumber:substr::ilike"}, t8('Customer Number') ],
353 [ $filter->{"order"}->{"ordnumber:substr::ilike"}, t8('Order Number') ],
354 [ $project, t8('Project') ],
355 [ $filter->{"description:substr::ilike"}, t8('Description') ],
356 [ $staff_member, t8('Mitarbeiter') ],
360 push @filter_strings, "$_->[1]: $_->[0]" if $_->[0];
363 $self->{filter_summary} = join ', ', @filter_strings;
366 sub setup_list_action_bar {
369 for my $bar ($::request->layout->get('actionbar')) {
373 submit => [ '#filter_form', { action => 'TimeRecording/list' } ],
374 accesskey => 'enter',
378 link => $self->url_for(action => 'edit', callback => $self->models->get_callback),
384 sub setup_edit_action_bar {
387 for my $bar ($::request->layout->get('actionbar')) {
391 submit => [ '#form', { action => 'TimeRecording/save' } ],
392 checks => [ 'kivi.validate_form' ],
396 submit => [ '#form', { action => 'TimeRecording/delete' } ],
397 only_if => $self->time_recording->id,
401 link => $self->url_for(safe_callback()),
408 $::form->{callback} || (action => 'list')
411 sub get_inputs_to_disable {
414 return [qw(customer project)] if $self->time_recording->order_id;
415 return [qw(customer)] if $self->time_recording->project_id && $self->time_recording->project->customer_id;