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