5 use List::Util qw(first);
12 $main::lxdebug->enter_sub();
17 my $myconfig = \%main::myconfig;
18 my $form = $main::form;
20 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
24 ($params{id}) = selectrow_query($form, $dbh, qq|SELECT nextval('follow_up_id')|);
26 $query = qq|INSERT INTO follow_ups (created_by, done, note_id, follow_up_date, created_for_user, id)
27 VALUES ((SELECT id FROM employee WHERE login = ?), ?, ?, ?, ?, ?)|;
29 push @values, $form->{login};
32 $query = qq|UPDATE follow_ups SET done = ?, note_id = ?, follow_up_date = ?, created_for_user = ? WHERE id = ?|;
35 $params{note_id} = Notes->save('id' => $params{note_id},
36 'trans_id' => $params{id},
37 'trans_module' => 'fu',
38 'subject' => $params{subject},
39 'body' => $params{body},
42 $params{done} = 1 if (!defined $params{done});
44 do_query($form, $dbh, $query, @values, $params{done} ? 't' : 'f', conv_i($params{note_id}), $params{follow_up_date}, conv_i($params{created_for_user}), conv_i($params{id}));
46 do_query($form, $dbh, qq|DELETE FROM follow_up_links WHERE follow_up_id = ?|, conv_i($params{id}));
48 my $query = qq|INSERT INTO follow_up_links (follow_up_id, trans_id, trans_type, trans_info) VALUES (?, ?, ?, ?)|;
49 my $sth = prepare_query($form, $dbh, $query);
51 foreach my $link (@{ $params{LINKS} }) {
52 do_statement($form, $sth, $query, conv_i($params{id}), conv_i($link->{trans_id}), $link->{trans_type}, $link->{trans_info});
57 $dbh->commit() unless ($params{dbh});
59 $main::lxdebug->leave_sub();
63 $main::lxdebug->enter_sub();
68 Common::check_params(\%params, 'id');
70 my $myconfig = \%main::myconfig;
71 my $form = $main::form;
73 my $dbh = $form->get_standard_dbh($myconfig);
75 do_query($form, $dbh, qq|UPDATE follow_ups SET done = TRUE WHERE id = ?|, conv_i($params{id}));
79 $main::lxdebug->leave_sub();
83 $main::lxdebug->enter_sub();
88 Common::check_params(\%params, 'id');
90 my $myconfig = \%main::myconfig;
91 my $form = $main::form;
93 my $dbh = $form->get_standard_dbh($myconfig);
95 my $id = conv_i($params{id});
97 do_query($form, $dbh, qq|DELETE FROM follow_up_links WHERE follow_up_id = ?|, $id);
98 do_query($form, $dbh, qq|DELETE FROM follow_ups WHERE id = ?|, $id);
99 do_query($form, $dbh, qq|DELETE FROM notes WHERE (trans_id = ?) AND (trans_module = 'fu')|, $id);
103 $main::lxdebug->leave_sub();
107 $main::lxdebug->enter_sub();
112 Common::check_params(\%params, 'id');
114 my $myconfig = \%main::myconfig;
115 my $form = $main::form;
117 my $dbh = $form->get_standard_dbh($myconfig);
118 my ($query, @values);
120 my ($employee_id) = selectrow_query($form, $dbh, qq|SELECT id FROM employee WHERE login = ?|, $form->{login});
121 $query = qq|SELECT fu.*, n.subject, n.body, n.created_by
123 LEFT JOIN notes n ON (fu.note_id = n.id)
125 AND ( (fu.created_by = ?) OR (fu.created_for_user = ?)
126 OR (fu.created_by IN (SELECT DISTINCT what FROM follow_up_access WHERE who = ?)))|;
127 my $ref = selectfirst_hashref_query($form, $dbh, $query, conv_i($params{id}), $employee_id, $employee_id, $employee_id);
130 $main::lxdebug->leave_sub();
134 $ref->{LINKS} = $self->retrieve_links(%{ $ref });
136 $main::lxdebug->leave_sub();
142 $main::lxdebug->enter_sub();
147 Common::check_params(\%params, qw(id));
149 my $myconfig = \%main::myconfig;
150 my $form = $main::form;
152 my $dbh = $form->get_standard_dbh($myconfig);
154 my $query = qq|SELECT ful.trans_id, ful.trans_type, ful.trans_info, fu.note_id
155 FROM follow_up_links ful
156 LEFT JOIN follow_ups fu ON (ful.follow_up_id = fu.id)
157 WHERE ful.follow_up_id = ?
160 my $links = selectall_hashref_query($form, $dbh, $query, conv_i($params{id}));
162 foreach my $link_ref (@{ $links }) {
163 my $link_details = FU->link_details(%{ $link_ref });
164 map { $link_ref->{$_} = $link_details->{$_} } keys %{ $link_details} if ($link_details);
167 $main::lxdebug->leave_sub();
173 $main::lxdebug->enter_sub();
178 my $myconfig = \%main::myconfig;
179 my $form = $main::form;
181 my $dbh = $form->get_standard_dbh($myconfig);
182 my ($query, $where, $where_user);
184 my ($employee_id) = selectrow_query($form, $dbh, qq|SELECT id FROM employee WHERE login = ?|, $form->{login});
186 my @values_user = ();
188 if ($params{trans_id}) {
189 $where .= qq| AND EXISTS (SELECT * FROM follow_up_links ful
190 WHERE (ful.follow_up_id = fu.id) AND (ful.trans_id = ?))|;
191 push @values, conv_i($params{trans_id});
194 if ($params{due_only}) {
195 $where .= qq| AND (fu.follow_up_date <= current_date)|;
198 if ($params{done} ne $params{not_done}) {
199 my $not = $params{not_done} ? 'NOT' : '';
200 $where .= qq| AND $not COALESCE(fu.done, FALSE)|;
203 if ($params{not_id}) {
204 $where .= qq| AND (fu.id <> ?)|;
205 push @values, conv_i($params{not_id});
208 foreach my $item (qw(subject body)) {
209 next unless ($params{$item});
210 $where .= qq| AND (n.${item} ILIKE ?)|;
211 push @values, '%' . $params{$item} . '%';
214 if ($params{reference}) {
215 $where .= qq| AND EXISTS (SELECT ful.follow_up_id
216 FROM follow_up_links ful
217 WHERE (ful.follow_up_id = fu.id)
218 AND (ful.trans_info ILIKE ?)
220 push @values, '%' . $params{reference} . '%';
223 if ($params{follow_up_date_from}) {
224 $where .= qq| AND (fu.follow_up_date >= ?)|;
225 push @values, conv_date($params{follow_up_date_from});
227 if ($params{follow_up_date_to}) {
228 $where .= qq| AND (fu.follow_up_date <= ?)|;
229 push @values, conv_date($params{follow_up_date_to});
232 if ($params{itime_from}) {
233 $where .= qq| AND (date_trunc('DAY', fu.itime) >= ?)|;
234 push @values, conv_date($params{itime_from});
236 if ($params{itime_to}) {
237 $where .= qq| AND (date_trunc('DAY', fu.itime) <= ?)|;
238 push @values, conv_date($params{itime_to});
241 if ($params{all_users}) {
242 $where_user = qq|OR (fu.created_by IN (SELECT DISTINCT what FROM follow_up_access WHERE who = ?))|;
243 push @values_user, $employee_id;
248 if ($form->{sort} ne 'title') {
250 'follow_up_date' => [ qw(fu.follow_up_date fu.id) ],
251 'created_on' => [ qw(created_on fu.id) ],
252 'subject' => [ qw(lower(n.subject)) ],
255 my $sortdir = !defined $form->{sortdir} ? 'ASC' : $form->{sortdir} ? 'ASC' : 'DESC';
256 my $sortkey = $sort_columns{$form->{sort}} ? $form->{sort} : 'follow_up_date';
257 $order_by = 'ORDER BY ' . join(', ', map { "$_ $sortdir" } @{ $sort_columns{$sortkey} });
260 $query = qq|SELECT fu.*, n.subject, n.body, n.created_by,
261 fu.follow_up_date <= current_date AS due,
262 fu.itime::DATE AS created_on,
263 COALESCE(eby.name, eby.login) AS created_by_name,
264 COALESCE(efor.name, efor.login) AS created_for_user_name
266 LEFT JOIN notes n ON (fu.note_id = n.id)
267 LEFT JOIN employee eby ON (n.created_by = eby.id)
268 LEFT JOIN employee efor ON (fu.created_for_user = efor.id)
269 WHERE ((fu.created_by = ?) OR (fu.created_for_user = ?)
274 my $follow_ups = selectall_hashref_query($form, $dbh, $query, $employee_id, $employee_id, @values_user, @values);
276 if (!scalar @{ $follow_ups }) {
277 $main::lxdebug->leave_sub();
281 foreach my $fu (@{ $follow_ups }) {
282 $fu->{LINKS} = $self->retrieve_links(%{ $fu });
285 if ($form->{sort} eq 'title') {
286 my $dir_factor = !defined $form->{sortdir} ? 1 : $form->{sortdir} ? 1 : -1;
287 $follow_ups = [ map { $_->[1] }
288 sort { ($a->[0] cmp $b->[0]) * $dir_factor }
289 map { my $fu = $follow_ups->[$_]; [ @{ $fu->{LINKS} } ? lc($fu->{LINKS}->[0]->{title}) : '', $fu ] }
290 (0 .. scalar(@{ $follow_ups }) - 1) ];
293 $main::lxdebug->leave_sub();
299 $main::lxdebug->enter_sub();
304 Common::check_params(\%params, qw(trans_id trans_type));
306 my $myconfig = \%main::myconfig;
307 my $form = $main::form;
308 my $locale = $main::locale;
310 my $q_id = $form->quote($params{trans_id});
313 if ($params{trans_type} eq 'customer') {
315 'url' => 'ct.pl?action=edit&db=customer&id=' . $form->quote($params{trans_id}) . '&edit_note_id=' . $form->quote($params{note_id}),
316 'title' => $locale->text('Customer') . " '$params{trans_info}'",
319 } elsif ($params{trans_type} eq 'vendor') {
321 'url' => 'ct.pl?action=edit&type=sales_quotation&id=' . $params{trans_id} . '&edit_note_id=' . $form->quote($params{note_id}),
322 'title' => $locale->text('Vendor') . " '$params{trans_info}'",
325 } elsif ($params{trans_type} eq 'sales_quotation') {
327 'url' => 'oe.pl?action=edit&type=sales_quotation&id=' . $params{trans_id},
328 'title' => $locale->text('Sales quotation') . " $params{trans_info}",
331 } elsif ($params{trans_type} eq 'sales_order') {
333 'url' => 'oe.pl?action=edit&type=sales_order&id=' . $params{trans_id},
334 'title' => $locale->text('Sales Order') . " $params{trans_info}",
337 } elsif ($params{trans_type} eq 'sales_invoice') {
339 'url' => 'is.pl?action=edit&type=invoice&id=' . $params{trans_id},
340 'title' => $locale->text('Sales Invoice') . " $params{trans_info}",
343 } elsif ($params{trans_type} eq 'credit_note') {
345 'url' => 'is.pl?action=edit&type=credit_note&id=' . $params{trans_id},
346 'title' => $locale->text('Credit Note') . " $params{trans_info}",
349 } elsif ($params{trans_type} eq 'dunning') {
351 'url' => 'dn.pl?action=print_dunning&format=pdf&media=screen&dunning_id=' . $params{trans_id},
352 'title' => $locale->text('Dunning') . " $params{trans_info}",
355 } elsif ($params{trans_type} eq 'request_quotation') {
357 'url' => 'oe.pl?action=edit&type=request_quotation&id=' . $params{trans_id},
358 'title' => $locale->text('Request quotation') . " $params{trans_info}",
361 } elsif ($params{trans_type} eq 'purchase_order') {
363 'url' => 'oe.pl?action=edit&type=purchase_order&id=' . $params{trans_id},
364 'title' => $locale->text('Purchase Order') . " $params{trans_info}",
367 } elsif ($params{trans_type} eq 'vendor_invoice') {
369 'url' => 'ir.pl?action=edit&type=invoice&id=' . $params{trans_id},
370 'title' => $locale->text('Vendor Invoice') . " $params{trans_info}",
373 } elsif ($params{trans_type} eq 'ar_transaction') {
375 'url' => 'ar.pl?action=editid=' . $params{trans_id},
376 'title' => $locale->text('AR Transaction') . " $params{trans_info}",
379 } elsif ($params{trans_type} eq 'ap_transaction') {
381 'url' => 'ap.pl?action=editid=' . $params{trans_id},
382 'title' => $locale->text('AP Transaction') . " $params{trans_info}",
385 } elsif ($params{trans_type} eq 'gl_transaction') {
387 'url' => 'gl.pl?action=edit&id=' . $params{trans_id},
388 'title' => $locale->text('GL Transaction') . " $params{trans_info}",
393 $main::lxdebug->leave_sub();
398 sub save_access_rights {
399 $main::lxdebug->enter_sub();
404 Common::check_params(\%params, 'access');
406 my $myconfig = \%main::myconfig;
407 my $form = $main::form;
409 my $dbh = $form->get_standard_dbh($myconfig);
411 my ($id) = selectrow_query($form, $dbh, qq|SELECT id FROM employee WHERE login = ?|, $form->{login});
413 my $query = qq|INSERT INTO follow_up_access (who, what) VALUES (?, ?)|;
414 my $sth = prepare_query($form, $dbh, $query);
416 do_query($form, $dbh, qq|DELETE FROM follow_up_access WHERE what = ?|, $id);
418 while (my ($who, $access_allowed) = each %{ $params{access} }) {
419 next unless ($access_allowed);
421 do_statement($form, $sth, $query, conv_i($who), $id);
428 $main::lxdebug->leave_sub();
431 sub retrieve_access_rights {
432 $main::lxdebug->enter_sub();
437 my $myconfig = \%main::myconfig;
438 my $form = $main::form;
440 my $dbh = $form->get_standard_dbh($myconfig);
442 my $sth = prepare_execute_query($form, $dbh, qq|SELECT who FROM follow_up_access WHERE what = (SELECT id FROM employee WHERE login = ?)|, $form->{login});
445 while (my $ref = $sth->fetchrow_hashref()) {
446 $access->{$ref->{who}} = 1;
451 $main::lxdebug->leave_sub();