]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Acq/Claims.pm
6d4964c561be2ac55e48b79f147980621182c3a7
[working/Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Acq / Claims.pm
1 package OpenILS::Application::Acq::Claims;
2 use base qw/OpenILS::Application/;
3 use strict; use warnings;
4
5 use OpenSRF::Utils::Logger qw(:logger);
6 use OpenILS::Utils::Fieldmapper;
7 use OpenILS::Utils::CStoreEditor q/:funcs/;
8 use OpenILS::Application::AppUtils;
9 use OpenILS::Event;
10 my $U = 'OpenILS::Application::AppUtils';
11
12
13 __PACKAGE__->register_method(
14         method => 'claim_ready_items',
15         api_name        => 'open-ils.acq.claim.eligible.lineitem_detail',
16     stream => 1,
17         signature => {
18         desc => q/Locates lineitem_detail's that are eligible for claiming/,
19         params => [
20             {desc => 'Authentication token', type => 'string'},
21             {   desc => q/
22                     Filter object.  Filter keys include 
23                     purchase_order
24                     lineitem
25                     lineitem_detail
26                     claim_policy_action
27                 /, 
28                 type => 'object'
29             },
30             {   desc => q/
31                     Flesh fields.  Which fields to flesh on the response object.  
32                     For valid options, see the filter object
33                 q/, 
34                 type => 'array'
35             }
36         ],
37         return => {desc => 'Claim ready data', type => 'object', class => 'acrlid'}
38     }
39 );
40
41 sub claim_ready_items {
42     my($self, $conn, $auth, $filters, $flesh_fields, $limit, $offset) = @_;
43
44     my $e = new_editor(authtoken=>$auth);
45     return $e->event unless $e->checkauth;
46
47     $filters ||= {};
48     $flesh_fields ||= [];
49     $limit ||= 50;
50     $offset ||= 0;
51
52     if(defined $filters->{ordering_agency}) {
53         return $e->event unless $e->allowed('VIEW_PURCHASE_ORDER', $filters->{ordering_agency});
54     } else {
55         $filters->{ordering_agency} = $U->user_has_work_perm_at($e, 'VIEW_PURCHASE_ORDER', {descendants => 1});
56     }
57
58     my $items = $e->search_acq_claim_ready_lineitem_detail([$filters, {limit => $limit, offset => $offset}]);
59
60     my %cache;
61     for my $item (@$items) {
62
63         # flesh from the flesh fields, using the cache when we can
64         foreach (@$flesh_fields) {
65             my $retrieve = "retrieve_acq_${_}";
66             $cache{$_} = {} unless $cache{$_};
67             $item->$_( 
68                 $cache{$_}{$item->$_} || 
69                 ($cache{$_}{$item->$_} = $e->$retrieve($item->$_))
70             );
71         }
72
73         $conn->respond($item);
74     }
75
76     return undef;
77 }
78
79 __PACKAGE__->register_method(
80         method => 'claim_item',
81         api_name        => 'open-ils.acq.claim.lineitem_detail',
82     stream => 1,
83         signature => {
84         desc => q/Initiates a claim for an individual lineitem_detail/,
85         params => [
86             {desc => 'Authentication token', type => 'string'},
87             {desc => 'Lineitem Detail ID', type => 'number'},
88             {desc => 'Claim (acqcl) ID.  If defined, attach new claim events to this existing claim object', type => 'number'},
89             {desc => 'Claim Type (acqclt) ID.  If defined (and no claim is defined), create a new claim with this type', type => 'number'},
90             {   desc => q/
91                 
92                     Optional: Claim Policy Actions.  If not present, claim events 
93                     for all eligible claim policy actions will be created.  This is
94                     an array of acqclpa ID's.
95                 /, 
96                 type => 'array'
97             },
98         ],
99         return => {desc => 'The claim events on success, Event on error', type => 'object', class => 'acrlid'}
100     }
101 );
102
103 sub claim_item {
104     my $self = shift;
105     my $conn = shift;
106     my $auth = shift;
107     my $object_id = shift;
108     my $claim_id = shift;
109     my $claim_type_id = shift;
110     my $note = shift;
111     my $policy_actions = shift;
112     my $only_eligible = shift;
113
114     my $e = new_editor(xact => 1, authtoken=>$auth);
115     return $e->die_event unless $e->checkauth;
116
117     my $evt;
118     my $claim;
119     my $claim_type;
120     my $claim_events = {
121         events => [],
122         trigger_stuff => []
123     };
124
125     if($claim_id) {
126         $claim = $e->retrieve_acq_claim($claim_id) or return $e->die_event;
127     } elsif($claim_type_id) {
128         $claim_type = $e->retrieve_acq_claim_type($claim_type_id) or return $e->die_event;
129     } else {
130         $e->rollback;
131         return OpenILS::Event->new('BAD_PARAMS');
132     }
133
134     if($self->api_name =~ /claim.lineitem_detail/) {
135
136         my $lid = $e->retrieve_acq_lineitem_detail([
137             $object_id,
138             {
139                 flesh => 2,
140                 flesh_fields => {
141                     acqlid => ['lineitem'],
142                     jub => ['purchase_order'],
143                 }
144             }
145         ]) or return $e->die_event;
146         return $evt if 
147             $evt = claim_lineitem_detail(
148                 $e, $lid, $claim, $claim_type, $policy_actions, $note, $claim_events); 
149
150     } elsif($self->api_name =~ /claim.lineitem/) {
151
152         # TODO: add support for claiming from a lineitem
153     }
154
155     $e->commit;
156     $conn->respond_complete($claim_events->{events});
157
158     # create related A/T events
159     $U->create_events_for_hook('claim_event.created', $_->[0], $_->[1]) for @{$claim_events->{trigger_stuff}};
160     return undef;
161 }
162
163 sub claim_lineitem_detail {
164     my($e, $lid, $claim, $claim_type, $policy_actions, $note, $claim_events) = @_;
165
166     # Create the claim object
167     unless($claim) {
168         $claim = Fieldmapper::acq::claim->new;
169         $claim->lineitem_detail($lid->id);
170         $claim->type($claim_type->id);
171         $e->create_acq_claim($claim) or return $e->die_event;
172     }
173
174     # find all eligible policy actions if none are provided
175     unless($policy_actions) {
176         my $list = $e->json_query({
177             select => {acrlid => ['claim_policy_action']},
178             from => 'acrlid',
179             where => {lineitem_detail => $lid->id}
180         });
181
182         $policy_actions = [map { $_->{claim_policy_action} } @$list];
183     }
184
185     # for each eligible (or chosen) policy actions, create a claim_event
186     for my $act_id (@$policy_actions) {
187         my $action = $e->retrieve_acq_claim_policy_action($act_id) or return $e->die_event;
188         my $event = Fieldmapper::acq::claim_event->new;
189         $event->claim($claim->id);
190         $event->type($action->action);
191         $event->creator($e->requestor->id);
192         $event->note($note);
193         $e->create_acq_claim_event($event) or return $e->die_event;
194         push(@{$claim_events->{events}}, $event);
195         push(@{$claim_events->{trigger_stuff}}, [$event, $lid->lineitem->purchase_order->ordering_agency]);
196     }
197
198     return undef;
199 }
200
201
202
203 1;