1 package OpenILS::Application::Acq::Claims;
2 use base qw/OpenILS::Application/;
3 use strict; use warnings;
5 use OpenSRF::Utils::Logger qw(:logger);
6 use OpenILS::Utils::Fieldmapper;
7 use OpenILS::Utils::CStoreEditor q/:funcs/;
8 use OpenILS::Application::AppUtils;
10 my $U = 'OpenILS::Application::AppUtils';
13 __PACKAGE__->register_method(
14 method => 'claim_ready_items',
15 api_name => 'open-ils.acq.claim.eligible.lineitem_detail',
18 desc => q/Locates lineitem_details that are eligible for claiming/,
20 {desc => 'Authentication token', type => 'string'},
22 Filter object. Filter keys include
32 Flesh fields. Which fields to flesh on the response object.
33 For valid options, see the filter object
38 return => {desc => 'Claim ready data', type => 'object', class => 'acrlid'}
42 sub claim_ready_items {
43 my($self, $conn, $auth, $filters, $flesh_fields, $limit, $offset) = @_;
45 my $e = new_editor(authtoken=>$auth);
46 return $e->event unless $e->checkauth;
53 if(defined $filters->{ordering_agency}) {
54 return $e->event unless $e->allowed('VIEW_PURCHASE_ORDER', $filters->{ordering_agency});
56 $filters->{ordering_agency} = $U->user_has_work_perm_at($e, 'VIEW_PURCHASE_ORDER', {descendants => 1});
59 my $items = $e->search_acq_claim_ready_lineitem_detail([$filters, {limit => $limit, offset => $offset}]);
62 for my $item (@$items) {
64 # flesh from the flesh fields, using the cache when we can
65 foreach (@$flesh_fields) {
66 my $retrieve = "retrieve_acq_${_}";
67 $cache{$_} = {} unless $cache{$_};
69 $cache{$_}{$item->$_} ||
70 ($cache{$_}{$item->$_} = $e->$retrieve($item->$_))
74 $conn->respond($item);
80 __PACKAGE__->register_method(
81 method => "claim_item",
82 api_name => "open-ils.acq.claim.lineitem",
85 desc => q/Initiates a claim for a lineitem/,
87 {desc => "Authentication token", type => "string"},
88 {desc => "Lineitem ID", type => "number"},
89 {desc => q/Claim (acqcl) ID. If defined, attach new claim
90 events to this existing claim object/, type => "number"},
91 {desc => q/Claim Type (acqclt) ID. If defined (and no claim is
92 defined), create a new claim with this type/, type => "number"},
93 {desc => "Note for the claim event", type => "string"},
94 {desc => q/Optional: Claim Policy Actions. If not present,
95 claim events for all eligible claim policy actions will be
96 created. This is an array of acqclpa IDs./,
100 desc => "The claim voucher events on success, Event on error",
101 type => "object", class => "acrlid"
106 __PACKAGE__->register_method(
107 method => 'claim_item',
108 api_name => 'open-ils.acq.claim.lineitem_detail',
111 desc => q/Initiates a claim for an individual lineitem_detail/,
113 {desc => 'Authentication token', type => 'string'},
114 {desc => 'Lineitem Detail ID', type => 'number'},
115 {desc => 'Claim (acqcl) ID. If defined, attach new claim events to this existing claim object', type => 'number'},
116 {desc => 'Claim Type (acqclt) ID. If defined (and no claim is defined), create a new claim with this type', type => 'number'},
117 {desc => "Note for the claim event", type => "string"},
120 Optional: Claim Policy Actions. If not present, claim events
121 for all eligible claim policy actions will be created. This is
122 an array of acqclpa ID's.
127 Optional: Claim Event Types. If present, we bypass any policy configuration
128 and use the specified event types. This is useful for manual claiming against
129 items that have no claim policy.
135 desc => "The claim voucher events on success, Event on error",
136 type => "object", class => "acrlid"
145 my $object_id = shift;
146 my $claim_id = shift;
147 my $claim_type_id = shift;
149 my $policy_actions = shift;
151 # if this claim occurs outside of a policy, allow the caller to specificy the event type
152 my $claim_event_types = shift;
154 my $e = new_editor(xact => 1, authtoken=>$auth);
155 return $e->die_event unless $e->checkauth;
168 "acqlid" => ["lineitem"], "jub" => ["purchase_order"],
173 $claim = $e->retrieve_acq_claim($claim_id) or return $e->die_event;
174 } elsif($claim_type_id) {
175 $claim_type = $e->retrieve_acq_claim_type($claim_type_id) or return $e->die_event;
178 return OpenILS::Event->new('BAD_PARAMS');
183 if($self->api_name =~ /claim.lineitem_detail/) {
185 $lids = $e->search_acq_lineitem_detail([
186 {"id" => $object_id, "cancel_reason" => undef},
188 ]) or return $e->die_event;
190 } elsif($self->api_name =~ /claim.lineitem/) {
191 $lids = $e->search_acq_lineitem_detail([
192 {"lineitem" => $object_id, "cancel_reason" => undef},
194 ]) or return $e->die_event;
197 foreach my $lid (@$lids) {
199 $evt = claim_lineitem_detail(
200 $e, $lid, $claim, $claim_type, $policy_actions,
201 $note, $claim_events, $claim_event_types
207 # create related A/T events
208 $U->create_events_for_hook('claim_event.created', $_->[0], $_->[1]) for @{$claim_events->{trigger_stuff}};
210 # do voucher rendering and return result
211 $conn->respond($U->fire_object_event(
212 undef, "format.acqcle.html", $_->[0], $_->[1], "print-on-demand"
213 )) foreach @{$claim_events->{trigger_stuff}};
217 sub claim_lineitem_detail {
218 my($e, $lid, $claim, $claim_type, $policy_actions, $note, $claim_events, $claim_event_types) = @_;
220 # Create the claim object
222 $claim = Fieldmapper::acq::claim->new;
223 $claim->lineitem_detail($lid->id);
224 $claim->type($claim_type->id);
225 $e->create_acq_claim($claim) or return $e->die_event;
228 unless($claim_event_types) {
229 # user did not specify explicit event types
231 unless($policy_actions) {
232 # user did not specifcy policy actions. find all eligible.
234 my $list = $e->json_query({
235 select => {acrlid => ['claim_policy_action']},
237 where => {lineitem_detail => $lid->id}
240 $policy_actions = [map { $_->{claim_policy_action} } @$list];
243 # from the set of policy_action's, locate the related event types
244 # IOW, the policy action's action
245 $claim_event_types = [];
246 for my $act_id (@$policy_actions) {
247 my $action = $e->retrieve_acq_claim_policy_action($act_id) or return $e->die_event;
248 push(@$claim_event_types, $action->action);
252 # for each eligible (or chosen) policy actions, create a claim_event
253 for my $event_type (@$claim_event_types) {
254 my $event = Fieldmapper::acq::claim_event->new;
255 $event->claim($claim->id);
256 $event->type($event_type);
257 $event->creator($e->requestor->id);
259 $e->create_acq_claim_event($event) or return $e->die_event;
260 push(@{$claim_events->{events}}, $event);
261 push(@{$claim_events->{trigger_stuff}}, [$event, $lid->lineitem->purchase_order->ordering_agency]);
268 __PACKAGE__->register_method(
269 method => "get_claim_voucher_by_lid",
270 api_name => "open-ils.acq.claim.voucher.by_lineitem_detail",
273 desc => q/Retrieve existing claim vouchers by lineitem detail ID/,
275 {desc => "Authentication token", type => "string"},
276 {desc => "Lineitem detail ID", type => "number"}
279 desc => "Claim ready data", type => "object", class => "atev"
284 sub get_claim_voucher_by_lid {
285 my ($self, $conn, $auth, $lid_id) = @_;
287 my $e = new_editor("authtoken" => $auth);
288 return $e->die_event unless $e->checkauth;
290 my $lid = $e->retrieve_acq_lineitem_detail([
294 "acqlid" => ["lineitem"], "jub" => ["purchase_order"]
299 return $e->die_event unless $e->allowed(
300 "VIEW_PURCHASE_ORDER", $lid->lineitem->purchase_order->ordering_agency
303 my $id_list = $e->json_query({
304 "select" => {"atev" => ["id"]},
307 "atevdef" => {"field" => "id", "fkey" => "event_def"},
309 "field" => "id", "fkey" => "target",
312 "field" => "id", "fkey" => "claim",
315 "fkey" => "lineitem_detail",
326 "+atevdef" => {"hook" => "format.acqcle.html"},
327 "+acqlid" => {"id" => $lid_id}
330 }) or return $e->die_event;
332 if ($id_list && @$id_list) {
333 foreach (@$id_list) {
335 $e->retrieve_action_trigger_event([
338 "flesh_fields" => {"atev" => ["template_output"]}