5 use List::Util qw(first);
15 my ($self, %params) = @_;
16 $main::lxdebug->enter_sub();
18 my $rc = SL::DB->client->with_transaction(\&_save, $self, %params);
20 $::lxdebug->leave_sub;
28 my $myconfig = \%main::myconfig;
29 my $form = $main::form;
31 my $dbh = $params{dbh} || SL::DB->client->dbh;
35 ($params{id}) = selectrow_query($form, $dbh, qq|SELECT nextval('follow_up_id')|);
37 $query = qq|INSERT INTO follow_ups (created_by, done, note_id, follow_up_date, created_for_user, id)
38 VALUES ((SELECT id FROM employee WHERE login = ?), ?, ?, ?, ?, ?)|;
40 push @values, $::myconfig{login};
43 $query = qq|UPDATE follow_ups SET done = ?, note_id = ?, follow_up_date = ?, created_for_user = ? WHERE id = ?|;
46 $params{note_id} = Notes->save('id' => $params{note_id},
47 'trans_id' => $params{id},
48 'trans_module' => 'fu',
49 'subject' => $params{subject},
50 'body' => $params{body},
53 $params{done} = 1 if (!defined $params{done});
55 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}));
57 do_query($form, $dbh, qq|DELETE FROM follow_up_links WHERE follow_up_id = ?|, conv_i($params{id}));
59 $query = qq|INSERT INTO follow_up_links (follow_up_id, trans_id, trans_type, trans_info) VALUES (?, ?, ?, ?)|;
60 my $sth = prepare_query($form, $dbh, $query);
62 foreach my $link (@{ $params{LINKS} }) {
63 do_statement($form, $sth, $query, conv_i($params{id}), conv_i($link->{trans_id}), $link->{trans_type}, $link->{trans_info});
72 $main::lxdebug->enter_sub();
77 Common::check_params(\%params, 'id');
79 my $myconfig = \%main::myconfig;
80 my $form = $main::form;
82 SL::DB->client->with_transaction(sub {
83 do_query($form, SL::DB->client->dbh, qq|UPDATE follow_ups SET done = TRUE WHERE id = ?|, conv_i($params{id}));
85 }) or do { die SL::DB->client->error };
87 $main::lxdebug->leave_sub();
91 $main::lxdebug->enter_sub();
96 Common::check_params(\%params, 'id');
98 my $myconfig = \%main::myconfig;
99 my $form = $main::form;
101 SL::DB->client->with_transaction(sub {
102 my $dbh = SL::DB->client->dbh;
104 my $id = conv_i($params{id});
106 do_query($form, $dbh, qq|DELETE FROM follow_up_links WHERE follow_up_id = ?|, $id);
107 do_query($form, $dbh, qq|DELETE FROM follow_ups WHERE id = ?|, $id);
108 do_query($form, $dbh, qq|DELETE FROM notes WHERE (trans_id = ?) AND (trans_module = 'fu')|, $id);
110 }) or do { die SL::DB->client->error };
112 $main::lxdebug->leave_sub();
116 $main::lxdebug->enter_sub();
121 Common::check_params(\%params, 'id');
123 my $myconfig = \%main::myconfig;
124 my $form = $main::form;
126 my $dbh = $form->get_standard_dbh($myconfig);
127 my ($query, @values);
129 my ($employee_id) = selectrow_query($form, $dbh, qq|SELECT id FROM employee WHERE login = ?|, $::myconfig{login});
130 $query = qq|SELECT fu.*, n.subject, n.body, n.created_by
132 LEFT JOIN notes n ON (fu.note_id = n.id)
134 AND ( (fu.created_by = ?) OR (fu.created_for_user = ?)
135 OR (fu.created_by IN (SELECT DISTINCT what FROM follow_up_access WHERE who = ?)))|;
136 my $ref = selectfirst_hashref_query($form, $dbh, $query, conv_i($params{id}), $employee_id, $employee_id, $employee_id);
139 $main::lxdebug->leave_sub();
143 $ref->{LINKS} = $self->retrieve_links(%{ $ref });
145 $main::lxdebug->leave_sub();
151 $main::lxdebug->enter_sub();
156 Common::check_params(\%params, qw(id));
158 my $myconfig = \%main::myconfig;
159 my $form = $main::form;
161 my $dbh = $form->get_standard_dbh($myconfig);
163 my $query = qq|SELECT ful.trans_id, ful.trans_type, ful.trans_info, fu.note_id
164 FROM follow_up_links ful
165 LEFT JOIN follow_ups fu ON (ful.follow_up_id = fu.id)
166 WHERE ful.follow_up_id = ?
169 my $links = selectall_hashref_query($form, $dbh, $query, conv_i($params{id}));
171 foreach my $link_ref (@{ $links }) {
172 my $link_details = FU->link_details(%{ $link_ref });
173 map { $link_ref->{$_} = $link_details->{$_} } keys %{ $link_details} if ($link_details);
176 $main::lxdebug->leave_sub();
182 $main::lxdebug->enter_sub();
187 my $myconfig = \%main::myconfig;
188 my $form = $main::form;
190 my $dbh = $form->get_standard_dbh($myconfig);
191 my ($query, $where, $where_user);
193 my ($employee_id) = selectrow_query($form, $dbh, qq|SELECT id FROM employee WHERE login = ?|, $::myconfig{login});
195 my @values_user = ();
197 if ($params{trans_id}) {
198 $where .= qq| AND EXISTS (SELECT * FROM follow_up_links ful
199 WHERE (ful.follow_up_id = fu.id) AND (ful.trans_id = ?))|;
200 push @values, conv_i($params{trans_id});
203 if ($params{due_only}) {
204 $where .= qq| AND (fu.follow_up_date <= current_date)|;
207 if ($params{done} ne $params{not_done}) {
208 my $not = $params{not_done} ? 'NOT' : '';
209 $where .= qq| AND $not COALESCE(fu.done, FALSE)|;
212 if ($params{not_id}) {
213 $where .= qq| AND (fu.id <> ?)|;
214 push @values, conv_i($params{not_id});
217 foreach my $item (qw(subject body)) {
218 next unless ($params{$item});
219 $where .= qq| AND (n.${item} ILIKE ?)|;
220 push @values, like($params{$item});
223 if ($params{reference}) {
224 $where .= qq| AND EXISTS (SELECT ful.follow_up_id
225 FROM follow_up_links ful
226 WHERE (ful.follow_up_id = fu.id)
227 AND (ful.trans_info ILIKE ?)
229 push @values, like($params{reference});
232 if ($params{follow_up_date_from}) {
233 $where .= qq| AND (fu.follow_up_date >= ?)|;
234 push @values, conv_date($params{follow_up_date_from});
236 if ($params{follow_up_date_to}) {
237 $where .= qq| AND (fu.follow_up_date <= ?)|;
238 push @values, conv_date($params{follow_up_date_to});
241 if ($params{itime_from}) {
242 $where .= qq| AND (date_trunc('DAY', fu.itime) >= ?)|;
243 push @values, conv_date($params{itime_from});
245 if ($params{itime_to}) {
246 $where .= qq| AND (date_trunc('DAY', fu.itime) <= ?)|;
247 push @values, conv_date($params{itime_to});
249 if ($params{created_for}) {
250 $where .= qq| AND fu.created_for_user = ?|;
251 push @values, conv_i($params{created_for});
254 if ($params{all_users}) {
255 $where_user = qq|OR (fu.created_by IN (SELECT DISTINCT what FROM follow_up_access WHERE who = ?))|;
256 push @values_user, $employee_id;
261 if ($form->{sort} ne 'title') {
263 'follow_up_date' => [ qw(fu.follow_up_date fu.id) ],
264 'created_on' => [ qw(created_on fu.id) ],
265 'subject' => [ qw(lower(n.subject)) ],
268 my $sortdir = !defined $form->{sortdir} ? 'ASC' : $form->{sortdir} ? 'ASC' : 'DESC';
269 my $sortkey = $sort_columns{$form->{sort}} ? $form->{sort} : 'follow_up_date';
270 $order_by = 'ORDER BY ' . join(', ', map { "$_ $sortdir" } @{ $sort_columns{$sortkey} });
273 $query = qq|SELECT fu.*, n.subject, n.body, n.created_by,
274 fu.follow_up_date <= current_date AS due,
275 fu.itime::DATE AS created_on,
276 COALESCE(eby.name, eby.login) AS created_by_name,
277 COALESCE(efor.name, efor.login) AS created_for_user_name
279 LEFT JOIN notes n ON (fu.note_id = n.id)
280 LEFT JOIN employee eby ON (n.created_by = eby.id)
281 LEFT JOIN employee efor ON (fu.created_for_user = efor.id)
282 WHERE ((fu.created_by = ?) OR (fu.created_for_user = ?)
287 my $follow_ups = selectall_hashref_query($form, $dbh, $query, $employee_id, $employee_id, @values_user, @values);
289 if (!scalar @{ $follow_ups }) {
290 $main::lxdebug->leave_sub();
294 foreach my $fu (@{ $follow_ups }) {
295 $fu->{LINKS} = $self->retrieve_links(%{ $fu });
298 if ($form->{sort} eq 'title') {
299 my $dir_factor = !defined $form->{sortdir} ? 1 : $form->{sortdir} ? 1 : -1;
300 $follow_ups = [ map { $_->[1] }
301 sort { ($a->[0] cmp $b->[0]) * $dir_factor }
302 map { my $fu = $follow_ups->[$_]; [ @{ $fu->{LINKS} } ? lc($fu->{LINKS}->[0]->{title}) : '', $fu ] }
303 (0 .. scalar(@{ $follow_ups }) - 1) ];
306 $main::lxdebug->leave_sub();
312 $main::lxdebug->enter_sub();
317 Common::check_params(\%params, qw(trans_id trans_type));
319 my $myconfig = \%main::myconfig;
320 my $form = $main::form;
321 my $locale = $main::locale;
323 my $q_id = $form->quote($params{trans_id});
326 if ($params{trans_type} eq 'customer') {
328 'url' => 'controller.pl?action=CustomerVendor/edit&db=customer&id=' . $form->quote($params{trans_id}) . '¬e_id=' . $form->quote($params{note_id}),
329 'title' => $locale->text('Customer') . " '$params{trans_info}'",
332 } elsif ($params{trans_type} eq 'vendor') {
334 'url' => 'controller.pl?action=CustomerVendor/edit&db=vendor&id=' . $params{trans_id} . '¬e_id=' . $form->quote($params{note_id}),
335 'title' => $locale->text('Vendor') . " '$params{trans_info}'",
338 } elsif ($params{trans_type} eq 'sales_quotation') {
340 'url' => 'oe.pl?action=edit&type=sales_quotation&id=' . $params{trans_id},
341 'title' => $locale->text('Sales quotation') . " $params{trans_info}",
344 } elsif ($params{trans_type} eq 'sales_delivery_order') {
347 'url' => 'do.pl?action=edit&type=sales_delivery_order&id=' . $params{trans_id} . '&edit_note_id=' . $form->quote($params{note_id}),
348 'title' => $locale->text('Sales delivery order') .' '. $params{trans_info},
351 } elsif ($params{trans_type} eq 'purchase_delivery_order') {
354 'url' => 'do.pl?action=edit&type=purchase_delivery_order&id=' . $params{trans_id} . '&edit_note_id=' . $form->quote($params{note_id}),
355 'title' => $locale->text('Purchase delivery order') .' '. $params{trans_info},
358 } elsif ($params{trans_type} eq 'sales_order') {
360 'url' => 'oe.pl?action=edit&type=sales_order&id=' . $params{trans_id},
361 'title' => $locale->text('Sales Order') . " $params{trans_info}",
364 } elsif ($params{trans_type} eq 'sales_invoice') {
366 'url' => 'is.pl?action=edit&type=invoice&id=' . $params{trans_id},
367 'title' => $locale->text('Sales Invoice') . " $params{trans_info}",
370 } elsif ($params{trans_type} eq 'purchase_invoice') {
372 'url' => 'ir.pl?action=edit&type=purchase_invoice&id=' . $params{trans_id},
373 'title' => $locale->text('Purchase Invoice') . " $params{trans_info}",
376 } elsif ($params{trans_type} eq 'credit_note') {
378 'url' => 'is.pl?action=edit&type=credit_note&id=' . $params{trans_id},
379 'title' => $locale->text('Credit Note') . " $params{trans_info}",
382 } elsif ($params{trans_type} eq 'dunning') {
384 'url' => 'dn.pl?action=print_dunning&format=pdf&media=screen&dunning_id=' . $params{trans_id},
385 'title' => $locale->text('Dunning') . " $params{trans_info}",
388 } elsif ($params{trans_type} eq 'request_quotation') {
390 'url' => 'oe.pl?action=edit&type=request_quotation&id=' . $params{trans_id},
391 'title' => $locale->text('Request quotation') . " $params{trans_info}",
394 } elsif ($params{trans_type} eq 'purchase_order') {
396 'url' => 'oe.pl?action=edit&type=purchase_order&id=' . $params{trans_id},
397 'title' => $locale->text('Purchase Order') . " $params{trans_info}",
400 } elsif ($params{trans_type} eq 'vendor_invoice') {
402 'url' => 'ir.pl?action=edit&type=invoice&id=' . $params{trans_id},
403 'title' => $locale->text('Vendor Invoice') . " $params{trans_info}",
406 } elsif ($params{trans_type} eq 'ar_transaction') {
408 'url' => 'ar.pl?action=edit&id=' . $params{trans_id},
409 'title' => $locale->text('AR Transaction') . " $params{trans_info}",
412 } elsif ($params{trans_type} eq 'ap_transaction') {
414 'url' => 'ap.pl?action=edit&id=' . $params{trans_id},
415 'title' => $locale->text('AP Transaction') . " $params{trans_info}",
418 } elsif ($params{trans_type} eq 'gl_transaction') {
420 'url' => 'gl.pl?action=edit&id=' . $params{trans_id},
421 'title' => $locale->text('GL Transaction') . " $params{trans_info}",
426 $main::lxdebug->leave_sub();
431 sub save_access_rights {
432 $main::lxdebug->enter_sub();
437 Common::check_params(\%params, 'access');
439 my $myconfig = \%main::myconfig;
440 my $form = $main::form;
442 SL::DB->client->with_transaction(sub {
443 my $dbh = SL::DB->client->dbh;
445 my ($id) = selectrow_query($form, $dbh, qq|SELECT id FROM employee WHERE login = ?|, $::myconfig{login});
447 do_query($form, $dbh, qq|DELETE FROM follow_up_access WHERE what = ?|, $id);
449 my $query = qq|INSERT INTO follow_up_access (who, what) VALUES (?, ?)|;
450 my $sth = prepare_query($form, $dbh, $query);
452 while (my ($who, $access_allowed) = each %{ $params{access} }) {
453 next unless ($access_allowed);
455 do_statement($form, $sth, $query, conv_i($who), $id);
460 }) or do { die SL::DB->client->error };
462 $main::lxdebug->leave_sub();
465 sub retrieve_access_rights {
466 $main::lxdebug->enter_sub();
471 my $myconfig = \%main::myconfig;
472 my $form = $main::form;
474 my $dbh = $form->get_standard_dbh($myconfig);
476 my $sth = prepare_execute_query($form, $dbh, qq|SELECT who FROM follow_up_access WHERE what = (SELECT id FROM employee WHERE login = ?)|, $::myconfig{login});
479 while (my $ref = $sth->fetchrow_hashref()) {
480 $access->{$ref->{who}} = 1;
485 $main::lxdebug->leave_sub();