d9001e35c9b99bcba26179dde43d4840094c4f29
[kivitendo-erp.git] / bin / mozilla / fu.pl
1 use POSIX qw(strftime);
2
3 use SL::FU;
4 use SL::Locale::String qw(t8);
5 use SL::ReportGenerator;
6
7 require "bin/mozilla/reportgenerator.pl";
8
9 use strict;
10
11 sub _collect_links {
12   $main::lxdebug->enter_sub();
13
14   $main::auth->assert('productivity');
15
16   my $dest = shift;
17
18   my $form     = $main::form;
19
20   $dest->{LINKS} = [];
21
22   foreach my $i (1 .. $form->{trans_rowcount}) {
23     next if (!$form->{"trans_id_$i"} || !$form->{"trans_type_$i"});
24
25     push @{ $dest->{LINKS} }, { map { +"trans_$_" => $form->{"trans_${_}_$i"} } qw(id type info) };
26   }
27
28   $main::lxdebug->leave_sub();
29 }
30
31 sub add {
32   $main::lxdebug->enter_sub();
33
34   $main::auth->assert('productivity');
35
36   my $form     = $main::form;
37   my %myconfig = %main::myconfig;
38   my $locale   = $main::locale;
39
40   _collect_links($form);
41
42   $form->get_employee($form->get_standard_dbh(\%myconfig));
43   $form->{created_for_user} = $form->{employee_id};
44
45   $form->{subject} = $form->{trans_subject_1} if $form->{trans_subject_1};
46
47   my $link_details;
48
49   if (0 < scalar @{ $form->{LINKS} }) {
50     $link_details = FU->link_details(%{ $form->{LINKS}->[0] });
51   }
52
53   if ($link_details && $link_details->{title}) {
54     $form->{title} = $locale->text('Add Follow-Up for #1', $link_details->{title});
55   } else {
56     $form->{title} = $locale->text('Add Follow-Up');
57   }
58
59   display_form();
60
61   $main::lxdebug->leave_sub();
62 }
63
64 sub edit {
65   $main::lxdebug->enter_sub();
66
67   $main::auth->assert('productivity');
68
69   my $form     = $main::form;
70   my $locale   = $main::locale;
71
72   my $ref = FU->retrieve('id' => $form->{id});
73
74   if (!$ref) {
75     $form->error($locale->text("Invalid follow-up ID."));
76   }
77
78   map { $form->{$_} = $ref->{$_} } keys %{ $ref };
79
80   if (@{ $form->{LINKS} } && $form->{LINKS}->[0]->{title}) {
81     $form->{title} = $locale->text('Edit Follow-Up for #1', $form->{LINKS}->[0]->{title});
82   } else {
83     $form->{title} = $locale->text('Edit Follow-Up');
84   }
85
86   display_form();
87
88   $main::lxdebug->leave_sub();
89 }
90
91 sub display_form {
92   $main::lxdebug->enter_sub();
93
94   $main::auth->assert('productivity');
95
96   my $form     = $main::form;
97
98   $form->get_lists("employees" => "EMPLOYEES");
99
100   my %params;
101   $params{not_id}     = $form->{id} if ($form->{id});
102   $params{trans_id}   = $form->{LINKS}->[0]->{trans_id} if (@{ $form->{LINKS} });
103   $form->{FOLLOW_UPS} = FU->follow_ups(%params);
104
105   setup_fu_display_form_action_bar() unless $::form->{POPUP_MODE};
106
107   $form->header(no_layout => $::form->{POPUP_MODE});
108   print $form->parse_html_template('fu/add_edit');
109
110   $main::lxdebug->leave_sub();
111 }
112
113 sub save_follow_up {
114   $main::lxdebug->enter_sub();
115
116   $main::auth->assert('productivity');
117
118   my $form     = $main::form;
119   my $locale   = $main::locale;
120
121   $form->isblank('created_for_user', $locale->text('You must chose a user.'));
122   $form->isblank('follow_up_date',   $locale->text('The follow-up date is missing.'));
123   $form->isblank('subject',          $locale->text('The subject is missing.'));
124
125   my %params = (map({ $_ => $form->{$_} } qw(id subject body note_id created_for_user follow_up_date)), 'done' => 0);
126
127   _collect_links(\%params);
128
129   FU->save(%params);
130
131   if ($form->{POPUP_MODE}) {
132     $form->header();
133     print $form->parse_html_template('fu/close_window');
134     $::dispatcher->end_request;
135   }
136
137   $form->{SAVED_MESSAGE} = $locale->text('Follow-Up saved.');
138
139   if ($form->{callback}) {
140     $form->redirect();
141   }
142
143   delete @{$form}{qw(id subject body created_for_user follow_up_date)};
144
145   map { $form->{$_} = 1 } qw(due_only all_users not_done);
146
147   report();
148
149   $main::lxdebug->leave_sub();
150 }
151
152 sub finish {
153   $main::lxdebug->enter_sub();
154
155   $main::auth->assert('productivity');
156
157   my $form     = $main::form;
158   my $locale   = $main::locale;
159
160   if ($form->{id}) {
161     my $ref = FU->retrieve('id' => $form->{id});
162
163     if (!$ref) {
164       $form->error($locale->text("Invalid follow-up ID."));
165     }
166
167     FU->finish('id' => $form->{id});
168
169   } else {
170     foreach my $i (1..$form->{rowcount}) {
171       next unless ($form->{"selected_$i"} && $form->{"follow_up_id_$i"});
172
173       FU->finish('id' => $form->{"follow_up_id_$i"});
174     }
175   }
176
177   if ($form->{POPUP_MODE}) {
178     $form->header();
179     print $form->parse_html_template('fu/close_window');
180     $::dispatcher->end_request;
181   }
182
183   $form->redirect() if ($form->{callback});
184
185   report();
186
187   $main::lxdebug->leave_sub();
188 }
189
190 sub delete {
191   $main::lxdebug->enter_sub();
192
193   $main::auth->assert('productivity');
194
195   my $form     = $main::form;
196   my $locale   = $main::locale;
197
198   if ($form->{id}) {
199     my $ref = FU->retrieve('id' => $form->{id});
200
201     if (!$ref) {
202       $form->error($locale->text("Invalid follow-up ID."));
203     }
204
205     FU->delete('id' => $form->{id});
206
207   } else {
208     foreach my $i (1..$form->{rowcount}) {
209       next unless ($form->{"selected_$i"} && $form->{"follow_up_id_$i"});
210
211       FU->delete('id' => $form->{"follow_up_id_$i"});
212     }
213   }
214
215   if ($form->{POPUP_MODE}) {
216     $form->header();
217     print $form->parse_html_template('fu/close_window');
218     $::dispatcher->end_request;
219   }
220
221   $form->redirect() if ($form->{callback});
222
223   report();
224
225   $main::lxdebug->leave_sub();
226 }
227
228 sub search {
229   $main::lxdebug->enter_sub();
230
231   $main::auth->assert('productivity');
232
233   my $form     = $main::form;
234   my $locale   = $main::locale;
235
236   $form->get_lists("employees" => "EMPLOYEES");
237
238   $form->{title}    = $locale->text('Follow-Ups');
239
240   setup_fu_search_action_bar();
241   $form->header();
242   print $form->parse_html_template('fu/search');
243
244   $main::lxdebug->leave_sub();
245 }
246
247 sub report {
248   $main::lxdebug->enter_sub();
249
250   $main::auth->assert('productivity');
251
252   my $form     = $main::form;
253   my %myconfig = %main::myconfig;
254   my $locale   = $main::locale;
255   my $cgi      = $::request->{cgi};
256
257   my @report_params = qw(created_for subject body reference follow_up_date_from follow_up_date_to itime_from itime_to due_only all_users done not_done);
258
259   report_generator_set_default_sort('follow_up_date', 1);
260
261   my $follow_ups    = FU->follow_ups(map { $_ => $form->{$_} } @report_params);
262   $form->{rowcount} = scalar @{ $follow_ups };
263
264   $form->{title}    = $locale->text('Follow-Ups');
265
266   my %column_defs = (
267     'selected'              => { 'text' => '', },
268     'follow_up_date'        => { 'text' => $locale->text('Follow-Up Date'), },
269     'created_on'            => { 'text' => $locale->text('Created on'), },
270     'title'                 => { 'text' => $locale->text('Reference'), },
271     'subject'               => { 'text' => $locale->text('Subject'), },
272     'created_by_name'       => { 'text' => $locale->text('Created by'), },
273     'created_for_user_name' => { 'text' => $locale->text('Follow-up for'), },
274     'done'                  => { 'text' => $locale->text('Done'), 'visible' => $form->{done} && $form->{not_done} ? 1 : 0 },
275   );
276
277   my @columns = qw(selected follow_up_date created_on subject title created_by_name created_for_user_name done);
278   my $href    = build_std_url('action=report', grep { $form->{$_} } @report_params);
279
280   foreach my $name (qw(follow_up_date created_on title subject)) {
281     my $sortdir                 = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
282     $column_defs{$name}->{link} = $href . "&sort=$name&sortdir=$sortdir";
283   }
284
285   my @options;
286
287   if ($form->{created_for}) {
288     $form->get_lists("employees" => "EMPLOYEES");
289
290     foreach my $employee (@{ $form->{EMPLOYEES} }) {
291       if ($employee->{id} == $form->{created_for}) {
292         push @options, $locale->text('Created for') . " : " . ($employee->{name} ? "$employee->{name} ($employee->{login})" : $employee->{login});
293         last;
294       }
295     }
296   }
297
298   push @options, $locale->text('Subject')                  . " : $form->{subject}"   if ($form->{subject});
299   push @options, $locale->text('Body')                     . " : $form->{body}"      if ($form->{body});
300   push @options, $locale->text('Reference')                . " : $form->{reference}" if ($form->{reference});
301   push @options, $locale->text('Done')                                               if ($form->{done});
302   push @options, $locale->text('Not done yet')                                       if ($form->{not_done});
303   push @options, $locale->text('Only due follow-ups')                                if ($form->{due_only});
304   push @options, $locale->text("Other users' follow-ups")                            if ($form->{all_users});
305
306   my @hidden_report_params = map { +{ 'key' => $_, 'value' => $form->{$_} } } @report_params;
307
308   my $report = SL::ReportGenerator->new(\%myconfig, $form, 'std_column_visibility' => 1);
309
310   $report->set_columns(%column_defs);
311   $report->set_column_order(@columns);
312
313   $report->set_export_options('report', @report_params, qw(sort sortdir));
314
315   $report->set_sort_indicator($form->{sort}, $form->{sortdir});
316
317   $report->set_options('raw_top_info_text'    => $form->parse_html_template('fu/report_top',    { 'OPTIONS' => \@options }),
318                        'raw_bottom_info_text' => $form->parse_html_template('fu/report_bottom', { 'HIDDEN'  => \@hidden_report_params }),
319                        'output_format'        => 'HTML',
320                        'title'                => $form->{title},
321                        'attachment_basename'  => $locale->text('follow_up_list') . strftime('_%Y%m%d', localtime time),
322     );
323   $report->set_options_from_form();
324   $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
325
326   my $idx      = 0;
327   my $callback = build_std_url('action=report', grep { $form->{$_} } @report_params);
328   my $edit_url = build_std_url('action=edit', 'callback=' . E($callback));
329
330   foreach my $fu (@{ $follow_ups }) {
331     $idx++;
332
333     $fu->{done} = $fu->{done} ? $locale->text('Yes') : $locale->text('No');
334
335     my $row = { map { $_ => { 'data' => $fu->{$_} } } keys %{ $fu } };
336
337     $row->{selected} = {
338       'raw_data' =>   $cgi->hidden('-name' => "follow_up_id_${idx}", '-value' => $fu->{id})
339                     . $cgi->checkbox('-name' => "selected_${idx}",   '-value' => 1, '-label' => ''),
340       'valign'   => 'center',
341       'align'    => 'center',
342     };
343
344     if (@{ $fu->{LINKS} }) {
345       my $link = $fu->{LINKS}->[0];
346
347       $row->{title}->{data} = $link->{title};
348       $row->{title}->{link} = $link->{url};
349     }
350
351     $row->{subject}->{link} = $edit_url . '&id=' . Q($fu->{id});
352
353     $report->add_data($row);
354   }
355
356   setup_fu_report_action_bar();
357   $report->generate_with_headers();
358
359   $main::lxdebug->leave_sub();
360 }
361
362 sub report_for_todo_list {
363   $main::lxdebug->enter_sub();
364
365   $main::auth->assert('productivity');
366
367   my $form     = $main::form;
368
369   my @report_params = qw(created_for subject body reference follow_up_date_from follow_up_date_to itime_from itime_to due_only all_users done not_done);
370
371   my %params   = (
372     'due_only'          => 1,
373     'not_done'          => 1,
374     'created_for_login' => $::myconfig{login},
375     );
376
377   my $follow_ups = FU->follow_ups(%params);
378   my $content;
379
380   if (@{ $follow_ups }) {
381     my $callback = build_std_url('action');
382     my $edit_url = build_std_url('script=fu.pl', 'action=edit', 'callback=' . E($callback)) . '&id=';
383
384     foreach my $fu (@{ $follow_ups }) {
385       if (@{ $fu->{LINKS} }) {
386         my $link = $fu->{LINKS}->[0];
387
388         $fu->{reference}      = $link->{title};
389         $fu->{reference_link} = $link->{url};
390       }
391     }
392
393     $content = $form->parse_html_template('fu/report_for_todo_list', { 'FOLLOW_UPS' => $follow_ups,
394                                                                        'callback'   => $callback,
395                                                                        'edit_url'   => $edit_url, });
396   }
397
398   $main::lxdebug->leave_sub();
399
400   return $content;
401 }
402
403 sub edit_access_rights {
404   $main::lxdebug->enter_sub();
405
406   $main::auth->assert('productivity');
407
408   my $form     = $main::form;
409   my $locale   = $main::locale;
410
411   my $access = FU->retrieve_access_rights();
412
413   $form->{EMPLOYEES} = SL::DB::Manager::Employee->get_all_sorted(query => [ deleted => 0 ]);
414
415   map { $_->{access} = $access->{$_->{id}} } @{ $form->{EMPLOYEES} };
416
417   $form->{title} = $locale->text('Edit Access Rights for Follow-Ups');
418
419   setup_fu_edit_access_rights_action_bar();
420
421   $form->header();
422   print $form->parse_html_template('fu/edit_access_rights');
423
424   $main::lxdebug->leave_sub();
425 }
426
427 sub save_access_rights {
428   $main::lxdebug->enter_sub();
429
430   $main::auth->assert('productivity');
431
432   my $form     = $main::form;
433   my $locale   = $main::locale;
434
435   my %access;
436
437   foreach my $i (1 .. $form->{rowcount}) {
438     my $id = $form->{"employee_id_$i"};
439
440     $access{$id} = 1 if ($id && $form->{"access_$id"});
441   }
442
443   FU->save_access_rights('access' => \%access);
444
445   $form->{SAVED_MESSAGE} = $locale->text('The access rights have been saved.');
446   edit_access_rights();
447
448   $main::lxdebug->leave_sub();
449 }
450
451 sub update {
452   call_sub($main::form->{nextsub});
453 }
454
455 sub continue {
456   call_sub($main::form->{nextsub});
457 }
458
459 sub save {
460   $main::auth->assert('productivity');
461
462   if ($main::form->{save_nextsub}) {
463     call_sub($main::form->{save_nextsub});
464   } else {
465     save_follow_up();
466   }
467 }
468
469 sub dispatcher {
470   $main::lxdebug->enter_sub();
471
472   my $form     = $main::form;
473   my $locale   = $main::locale;
474
475   foreach my $action (qw(finish save delete)) {
476     if ($form->{"action_${action}"}) {
477       call_sub($action);
478       return;
479     }
480   }
481
482   call_sub($form->{default_action}) if ($form->{default_action});
483
484   $form->error($locale->text('No action defined.'));
485 }
486
487 sub setup_fu_search_action_bar {
488   my %params = @_;
489
490   for my $bar ($::request->layout->get('actionbar')) {
491     $bar->add(
492       action => [
493         t8('Show'),
494         submit    => [ '#form', { action => "report" } ],
495         accesskey => 'enter',
496       ],
497     );
498   }
499 }
500
501 sub setup_fu_display_form_action_bar {
502   my %params = @_;
503
504   for my $bar ($::request->layout->get('actionbar')) {
505     $bar->add(
506       action => [
507         t8('Save'),
508         submit    => [ '#form', { action => "save" } ],
509         accesskey => 'enter',
510       ],
511       action => [
512         t8('Finish'),
513         submit   => [ '#form', { action => "finish" } ],
514         disabled => !$::form->{id} ? t8('The object has not been saved yet.') : undef,
515       ],
516       action => [
517         t8('Delete'),
518         submit   => [ '#form', { action => "delete" } ],
519         disabled => !$::form->{id} ? t8('The object has not been saved yet.') : undef,
520         confirm  => t8('Do you really want to delete this object?'),
521       ],
522     );
523   }
524 }
525
526 sub setup_fu_report_action_bar {
527   my %params = @_;
528
529   for my $bar ($::request->layout->get('actionbar')) {
530     $bar->add(
531       action => [
532         t8('Finish'),
533         submit => [ '#form', { action => "finish" } ],
534         checks => [ [ 'kivi.check_if_entries_selected', '[name^=selected_]' ] ],
535       ],
536       action => [
537         t8('Delete'),
538         submit  => [ '#form', { action => "delete" } ],
539         checks  => [ [ 'kivi.check_if_entries_selected', '[name^=selected_]' ] ],
540         confirm => t8('Do you really want to delete the selected objects?'),
541       ],
542     );
543   }
544 }
545
546 sub setup_fu_edit_access_rights_action_bar {
547   my %params = @_;
548
549   for my $bar ($::request->layout->get('actionbar')) {
550     $bar->add(
551       action => [
552         t8('Save'),
553         submit    => [ '#form', { action => "save_access_rights" } ],
554         accesskey => 'enter',
555       ],
556     );
557   }
558 }
559
560 1;