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