5 use List::Util qw(first);
14 $main::lxdebug->enter_sub();
19 my $myconfig = \%main::myconfig;
20 my $form = $main::form;
22 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
26 ($params{id}) = selectrow_query($form, $dbh, qq|SELECT nextval('follow_up_id')|);
28 $query = qq|INSERT INTO follow_ups (created_by, done, note_id, follow_up_date, created_for_user, id)
29 VALUES ((SELECT id FROM employee WHERE login = ?), ?, ?, ?, ?, ?)|;
31 push @values, $form->{login};
34 $query = qq|UPDATE follow_ups SET done = ?, note_id = ?, follow_up_date = ?, created_for_user = ? WHERE id = ?|;
37 $params{note_id} = Notes->save('id' => $params{note_id},
38 'trans_id' => $params{id},
39 'trans_module' => 'fu',
40 'subject' => $params{subject},
41 'body' => $params{body},
44 $params{done} = 1 if (!defined $params{done});
46 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}));
48 do_query($form, $dbh, qq|DELETE FROM follow_up_links WHERE follow_up_id = ?|, conv_i($params{id}));
50 $query = qq|INSERT INTO follow_up_links (follow_up_id, trans_id, trans_type, trans_info) VALUES (?, ?, ?, ?)|;
51 my $sth = prepare_query($form, $dbh, $query);
53 foreach my $link (@{ $params{LINKS} }) {
54 do_statement($form, $sth, $query, conv_i($params{id}), conv_i($link->{trans_id}), $link->{trans_type}, $link->{trans_info});
59 $dbh->commit() unless ($params{dbh});
61 $main::lxdebug->leave_sub();
65 $main::lxdebug->enter_sub();
70 Common::check_params(\%params, 'id');
72 my $myconfig = \%main::myconfig;
73 my $form = $main::form;
75 my $dbh = $form->get_standard_dbh($myconfig);
77 do_query($form, $dbh, qq|UPDATE follow_ups SET done = TRUE WHERE id = ?|, conv_i($params{id}));
81 $main::lxdebug->leave_sub();
85 $main::lxdebug->enter_sub();
90 Common::check_params(\%params, 'id');
92 my $myconfig = \%main::myconfig;
93 my $form = $main::form;
95 my $dbh = $form->get_standard_dbh($myconfig);
97 my $id = conv_i($params{id});
99 do_query($form, $dbh, qq|DELETE FROM follow_up_links WHERE follow_up_id = ?|, $id);
100 do_query($form, $dbh, qq|DELETE FROM follow_ups WHERE id = ?|, $id);
101 do_query($form, $dbh, qq|DELETE FROM notes WHERE (trans_id = ?) AND (trans_module = 'fu')|, $id);
105 $main::lxdebug->leave_sub();
109 $main::lxdebug->enter_sub();
114 Common::check_params(\%params, 'id');
116 my $myconfig = \%main::myconfig;
117 my $form = $main::form;
119 my $dbh = $form->get_standard_dbh($myconfig);
120 my ($query, @values);
122 my ($employee_id) = selectrow_query($form, $dbh, qq|SELECT id FROM employee WHERE login = ?|, $form->{login});
123 $query = qq|SELECT fu.*, n.subject, n.body, n.created_by
125 LEFT JOIN notes n ON (fu.note_id = n.id)
127 AND ( (fu.created_by = ?) OR (fu.created_for_user = ?)
128 OR (fu.created_by IN (SELECT DISTINCT what FROM follow_up_access WHERE who = ?)))|;
129 my $ref = selectfirst_hashref_query($form, $dbh, $query, conv_i($params{id}), $employee_id, $employee_id, $employee_id);
132 $main::lxdebug->leave_sub();
136 $ref->{LINKS} = $self->retrieve_links(%{ $ref });
138 $main::lxdebug->leave_sub();
144 $main::lxdebug->enter_sub();
149 Common::check_params(\%params, qw(id));
151 my $myconfig = \%main::myconfig;
152 my $form = $main::form;
154 my $dbh = $form->get_standard_dbh($myconfig);
156 my $query = qq|SELECT ful.trans_id, ful.trans_type, ful.trans_info, fu.note_id
157 FROM follow_up_links ful
158 LEFT JOIN follow_ups fu ON (ful.follow_up_id = fu.id)
159 WHERE ful.follow_up_id = ?
162 my $links = selectall_hashref_query($form, $dbh, $query, conv_i($params{id}));
164 foreach my $link_ref (@{ $links }) {
165 my $link_details = FU->link_details(%{ $link_ref });
166 map { $link_ref->{$_} = $link_details->{$_} } keys %{ $link_details} if ($link_details);
169 $main::lxdebug->leave_sub();
175 $main::lxdebug->enter_sub();
180 my $myconfig = \%main::myconfig;
181 my $form = $main::form;
183 my $dbh = $form->get_standard_dbh($myconfig);
184 my ($query, $where, $where_user);
186 my ($employee_id) = selectrow_query($form, $dbh, qq|SELECT id FROM employee WHERE login = ?|, $form->{login});
188 my @values_user = ();
190 if ($params{trans_id}) {
191 $where .= qq| AND EXISTS (SELECT * FROM follow_up_links ful
192 WHERE (ful.follow_up_id = fu.id) AND (ful.trans_id = ?))|;
193 push @values, conv_i($params{trans_id});
196 if ($params{due_only}) {
197 $where .= qq| AND (fu.follow_up_date <= current_date)|;
200 if ($params{done} ne $params{not_done}) {
201 my $not = $params{not_done} ? 'NOT' : '';
202 $where .= qq| AND $not COALESCE(fu.done, FALSE)|;
205 if ($params{not_id}) {
206 $where .= qq| AND (fu.id <> ?)|;
207 push @values, conv_i($params{not_id});
210 foreach my $item (qw(subject body)) {
211 next unless ($params{$item});
212 $where .= qq| AND (n.${item} ILIKE ?)|;
213 push @values, '%' . $params{$item} . '%';
216 if ($params{reference}) {
217 $where .= qq| AND EXISTS (SELECT ful.follow_up_id
218 FROM follow_up_links ful
219 WHERE (ful.follow_up_id = fu.id)
220 AND (ful.trans_info ILIKE ?)
222 push @values, '%' . $params{reference} . '%';
225 if ($params{follow_up_date_from}) {
226 $where .= qq| AND (fu.follow_up_date >= ?)|;
227 push @values, conv_date($params{follow_up_date_from});
229 if ($params{follow_up_date_to}) {
230 $where .= qq| AND (fu.follow_up_date <= ?)|;
231 push @values, conv_date($params{follow_up_date_to});
234 if ($params{itime_from}) {
235 $where .= qq| AND (date_trunc('DAY', fu.itime) >= ?)|;
236 push @values, conv_date($params{itime_from});
238 if ($params{itime_to}) {
239 $where .= qq| AND (date_trunc('DAY', fu.itime) <= ?)|;
240 push @values, conv_date($params{itime_to});
243 if ($params{all_users}) {
244 $where_user = qq|OR (fu.created_by IN (SELECT DISTINCT what FROM follow_up_access WHERE who = ?))|;
245 push @values_user, $employee_id;
250 if ($form->{sort} ne 'title') {
252 'follow_up_date' => [ qw(fu.follow_up_date fu.id) ],
253 'created_on' => [ qw(created_on fu.id) ],
254 'subject' => [ qw(lower(n.subject)) ],
257 my $sortdir = !defined $form->{sortdir} ? 'ASC' : $form->{sortdir} ? 'ASC' : 'DESC';
258 my $sortkey = $sort_columns{$form->{sort}} ? $form->{sort} : 'follow_up_date';
259 $order_by = 'ORDER BY ' . join(', ', map { "$_ $sortdir" } @{ $sort_columns{$sortkey} });
262 $query = qq|SELECT fu.*, n.subject, n.body, n.created_by,
263 fu.follow_up_date <= current_date AS due,
264 fu.itime::DATE AS created_on,
265 COALESCE(eby.name, eby.login) AS created_by_name,
266 COALESCE(efor.name, efor.login) AS created_for_user_name
268 LEFT JOIN notes n ON (fu.note_id = n.id)
269 LEFT JOIN employee eby ON (n.created_by = eby.id)
270 LEFT JOIN employee efor ON (fu.created_for_user = efor.id)
271 WHERE ((fu.created_by = ?) OR (fu.created_for_user = ?)
276 my $follow_ups = selectall_hashref_query($form, $dbh, $query, $employee_id, $employee_id, @values_user, @values);
278 if (!scalar @{ $follow_ups }) {
279 $main::lxdebug->leave_sub();
283 foreach my $fu (@{ $follow_ups }) {
284 $fu->{LINKS} = $self->retrieve_links(%{ $fu });
287 if ($form->{sort} eq 'title') {
288 my $dir_factor = !defined $form->{sortdir} ? 1 : $form->{sortdir} ? 1 : -1;
289 $follow_ups = [ map { $_->[1] }
290 sort { ($a->[0] cmp $b->[0]) * $dir_factor }
291 map { my $fu = $follow_ups->[$_]; [ @{ $fu->{LINKS} } ? lc($fu->{LINKS}->[0]->{title}) : '', $fu ] }
292 (0 .. scalar(@{ $follow_ups }) - 1) ];
295 $main::lxdebug->leave_sub();
301 $main::lxdebug->enter_sub();
306 Common::check_params(\%params, qw(trans_id trans_type));
308 my $myconfig = \%main::myconfig;
309 my $form = $main::form;
310 my $locale = $main::locale;
312 my $q_id = $form->quote($params{trans_id});
315 if ($params{trans_type} eq 'customer') {
317 'url' => 'controller.pl?action=CustomerVendor/edit&db=customer&id=' . $form->quote($params{trans_id}) . '&edit_note_id=' . $form->quote($params{note_id}),
318 'title' => $locale->text('Customer') . " '$params{trans_info}'",
321 } elsif ($params{trans_type} eq 'vendor') {
323 'url' => 'controller.pl?action=CustomerVendor/edit&type=sales_quotation&id=' . $params{trans_id} . '&edit_note_id=' . $form->quote($params{note_id}),
324 'title' => $locale->text('Vendor') . " '$params{trans_info}'",
327 } elsif ($params{trans_type} eq 'sales_quotation') {
329 'url' => 'oe.pl?action=edit&type=sales_quotation&id=' . $params{trans_id},
330 'title' => $locale->text('Sales quotation') . " $params{trans_info}",
333 } elsif ($params{trans_type} eq 'sales_delivery_order') {
336 'url' => 'do.pl?action=edit&type=sales_delivery_order&id=' . $params{trans_id} . '&edit_note_id=' . $form->quote($params{note_id}),
337 'title' => $locale->text('Sales delivery order') .' '. $params{trans_info},
340 } elsif ($params{trans_type} eq 'purchase_delivery_order') {
343 'url' => 'do.pl?action=edit&type=purchase_delivery_order&id=' . $params{trans_id} . '&edit_note_id=' . $form->quote($params{note_id}),
344 'title' => $locale->text('Purchase delivery order') .' '. $params{trans_info},
347 } elsif ($params{trans_type} eq 'sales_order') {
349 'url' => 'oe.pl?action=edit&type=sales_order&id=' . $params{trans_id},
350 'title' => $locale->text('Sales Order') . " $params{trans_info}",
353 } elsif ($params{trans_type} eq 'sales_invoice') {
355 'url' => 'is.pl?action=edit&type=invoice&id=' . $params{trans_id},
356 'title' => $locale->text('Sales Invoice') . " $params{trans_info}",
359 } elsif ($params{trans_type} eq 'credit_note') {
361 'url' => 'is.pl?action=edit&type=credit_note&id=' . $params{trans_id},
362 'title' => $locale->text('Credit Note') . " $params{trans_info}",
365 } elsif ($params{trans_type} eq 'dunning') {
367 'url' => 'dn.pl?action=print_dunning&format=pdf&media=screen&dunning_id=' . $params{trans_id},
368 'title' => $locale->text('Dunning') . " $params{trans_info}",
371 } elsif ($params{trans_type} eq 'request_quotation') {
373 'url' => 'oe.pl?action=edit&type=request_quotation&id=' . $params{trans_id},
374 'title' => $locale->text('Request quotation') . " $params{trans_info}",
377 } elsif ($params{trans_type} eq 'purchase_order') {
379 'url' => 'oe.pl?action=edit&type=purchase_order&id=' . $params{trans_id},
380 'title' => $locale->text('Purchase Order') . " $params{trans_info}",
383 } elsif ($params{trans_type} eq 'vendor_invoice') {
385 'url' => 'ir.pl?action=edit&type=invoice&id=' . $params{trans_id},
386 'title' => $locale->text('Vendor Invoice') . " $params{trans_info}",
389 } elsif ($params{trans_type} eq 'ar_transaction') {
391 'url' => 'ar.pl?action=edit&id=' . $params{trans_id},
392 'title' => $locale->text('AR Transaction') . " $params{trans_info}",
395 } elsif ($params{trans_type} eq 'ap_transaction') {
397 'url' => 'ap.pl?action=edit&id=' . $params{trans_id},
398 'title' => $locale->text('AP Transaction') . " $params{trans_info}",
401 } elsif ($params{trans_type} eq 'gl_transaction') {
403 'url' => 'gl.pl?action=edit&id=' . $params{trans_id},
404 'title' => $locale->text('GL Transaction') . " $params{trans_info}",
409 $main::lxdebug->leave_sub();
414 sub save_access_rights {
415 $main::lxdebug->enter_sub();
420 Common::check_params(\%params, 'access');
422 my $myconfig = \%main::myconfig;
423 my $form = $main::form;
425 my $dbh = $form->get_standard_dbh($myconfig);
427 my ($id) = selectrow_query($form, $dbh, qq|SELECT id FROM employee WHERE login = ?|, $form->{login});
429 do_query($form, $dbh, qq|DELETE FROM follow_up_access WHERE what = ?|, $id);
431 my $query = qq|INSERT INTO follow_up_access (who, what) VALUES (?, ?)|;
432 my $sth = prepare_query($form, $dbh, $query);
434 while (my ($who, $access_allowed) = each %{ $params{access} }) {
435 next unless ($access_allowed);
437 do_statement($form, $sth, $query, conv_i($who), $id);
444 $main::lxdebug->leave_sub();
447 sub retrieve_access_rights {
448 $main::lxdebug->enter_sub();
453 my $myconfig = \%main::myconfig;
454 my $form = $main::form;
456 my $dbh = $form->get_standard_dbh($myconfig);
458 my $sth = prepare_execute_query($form, $dbh, qq|SELECT who FROM follow_up_access WHERE what = (SELECT id FROM employee WHERE login = ?)|, $form->{login});
461 while (my $ref = $sth->fetchrow_hashref()) {
462 $access->{$ref->{who}} = 1;
467 $main::lxdebug->leave_sub();