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