1 package OpenILS::Application::Acq::Common;
2 use strict; use warnings;
3 use OpenILS::Application::AppUtils;
4 my $U = 'OpenILS::Application::AppUtils';
6 # retrieves a lineitem, fleshes its PO and PL, checks perms
7 # returns ($li, $evt, $org)
8 sub fetch_and_check_li {
9 my ($class, $e, $li_id, $perm_mode) = @_;
10 $perm_mode ||= 'read';
12 my $li = $e->retrieve_acq_lineitem([
15 flesh_fields => {jub => ['purchase_order', 'picklist']}
17 ]) or return (undef, $e->die_event);
20 if(my $po = $li->purchase_order) {
21 $org = $po->ordering_agency;
22 my $perms = ($perm_mode eq 'read') ? 'VIEW_PURCHASE_ORDER' : 'CREATE_PURCHASE_ORDER';
23 return ($li, $e->die_event) unless $e->allowed($perms, $org);
25 } elsif(my $pl = $li->picklist) {
27 my $perms = ($perm_mode eq 'read') ? 'VIEW_PICKLIST' : 'CREATE_PICKLIST';
28 return ($li, $e->die_event) unless $e->allowed($perms, $org);
31 return ($li, undef, $org);
34 sub li_existing_copies {
35 my ($class, $e, $li_id) = @_;
37 my ($li, $evt, $org) = $class->fetch_and_check_li($e, $li_id);
40 # No fuzzy matching here (e.g. on ISBN). Only exact matches are supported.
41 return 0 unless $li->eg_bib_id;
43 my $counts = $e->json_query({
53 field => 'eg_copy_id',
56 acn => {join => {bre => {}}}
60 '+bre' => {id => $li->eg_bib_id},
61 # don't count copies linked to the lineitem in question
65 {lineitem => {'<>' => $li_id}}
69 owning_lib => $U->get_org_descendants($org)
71 # NOTE: should the excluded copy statuses be an AOUS?
72 '+acp' => {status => {'not in' => [3, 4, 13, 17]}}
76 return $counts->[0]->{id};