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