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