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