]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/Application/Acq/Common.pm
LP1929741 ACQ Selection List & PO Angluar Port
[Evergreen.git] / Open-ILS / src / perlmods / lib / OpenILS / Application / Acq / Common.pm
1 package OpenILS::Application::Acq::Common;
2 use strict; use warnings;
3 use OpenILS::Application::AppUtils;
4 my $U = 'OpenILS::Application::AppUtils';
5
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';
11
12     my $li = $e->retrieve_acq_lineitem([
13         $li_id,
14         {   flesh => 1,
15             flesh_fields => {jub => ['purchase_order', 'picklist']}
16         }
17     ]) or return (undef, $e->die_event);
18
19     my $org;
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);
24
25     } elsif(my $pl = $li->picklist) {
26         $org = $pl->org_unit;
27         my $perms = ($perm_mode eq 'read') ? 'VIEW_PICKLIST' : 'CREATE_PICKLIST';
28         return ($li, $e->die_event) unless $e->allowed($perms, $org);
29     }
30
31     return ($li, undef, $org);
32 }
33
34 sub li_existing_copies {
35     my ($class, $e, $li_id) = @_;
36
37     my ($li, $evt, $org) = $class->fetch_and_check_li($e, $li_id);
38     return 0 if $evt;
39
40     # No fuzzy matching here (e.g. on ISBN).  Only exact matches are supported.
41     return 0 unless $li->eg_bib_id;
42
43     my $counts = $e->json_query({
44         select => {acp => [{
45             column => 'id', 
46             transform => 'count', 
47             aggregate => 1
48         }]},
49         from => {
50             acp => {
51                 acqlid => {
52                     fkey => 'id',
53                     field => 'eg_copy_id',
54                     type => 'left'
55                 },
56                 acn => {join => {bre => {}}}
57             }
58         },
59         where => {
60             '+bre' => {id => $li->eg_bib_id},
61             # don't count copies linked to the lineitem in question
62             '+acqlid' => {
63                 '-or' => [
64                     {lineitem => undef},
65                     {lineitem => {'<>' => $li_id}}
66                 ]
67             },
68             '+acn' => {
69                 owning_lib => $U->get_org_descendants($org)
70             },
71             # NOTE: should the excluded copy statuses be an AOUS?
72             '+acp' => {status => {'not in' => [3, 4, 13, 17]}}
73         }
74     });
75
76     return $counts->[0]->{id};
77 }
78
79
80