1 package OpenILS::Application::Acq::Lineitem;
2 use base qw/OpenILS::Application/;
3 use strict; use warnings;
6 use OpenSRF::Utils::Logger qw(:logger);
7 use OpenILS::Utils::Fieldmapper;
8 use OpenILS::Utils::CStoreEditor q/:funcs/;
9 use OpenILS::Const qw/:const/;
10 use OpenSRF::Utils::SettingsClient;
11 use OpenILS::Application::AppUtils;
12 use OpenILS::Application::Acq::Financials;
13 use OpenILS::Application::Cat::BibCommon;
14 use OpenILS::Application::Cat::AssetCommon;
15 my $U = 'OpenILS::Application::AppUtils';
18 __PACKAGE__->register_method(
19 method => 'create_lineitem',
20 api_name => 'open-ils.acq.lineitem.create',
22 desc => 'Creates a lineitem',
24 {desc => 'Authentication token', type => 'string'},
25 {desc => 'The lineitem object to create', type => 'object'},
27 return => {desc => 'ID of newly created lineitem on success, Event on error'}
32 my($self, $conn, $auth, $li) = @_;
33 my $e = new_editor(xact=>1, authtoken=>$auth);
34 return $e->die_event unless $e->checkauth;
38 my $picklist = $e->retrieve_acq_picklist($li->picklist)
39 or return $e->die_event;
41 if($picklist->owner != $e->requestor->id) {
42 return $e->die_event unless
43 $e->allowed('CREATE_PICKLIST', $picklist->org_unit, $picklist);
46 # indicate the picklist was updated
47 $picklist->edit_time('now');
48 $picklist->editor($e->requestor->id);
49 $e->update_acq_picklist($picklist) or return $e->die_event;
52 if($li->purchase_order) {
53 my $po = $e->retrieve_acq_purchase_order($li->purchase_order)
54 or return $e->die_event;
55 return $e->die_event unless
56 $e->allowed('MANAGE_PROVIDER', $po->ordering_agency, $po);
59 $li->selector($e->requestor->id);
60 $e->create_acq_lineitem($li) or return $e->die_event;
67 __PACKAGE__->register_method(
68 method => 'retrieve_lineitem',
69 api_name => 'open-ils.acq.lineitem.retrieve',
71 desc => 'Retrieves a lineitem',
73 {desc => 'Authentication token', type => 'string'},
74 {desc => 'lineitem ID to retrieve', type => 'number'},
75 {options => q/Hash of options, including
76 "flesh_attrs", which fleshes the attributes;
77 "flesh_li_details", which fleshes the order details objects/, type => 'hash'},
79 return => {desc => 'lineitem object on success, Event on error'}
84 sub retrieve_lineitem {
85 my($self, $conn, $auth, $li_id, $options) = @_;
86 my $e = new_editor(authtoken=>$auth);
87 return $e->die_event unless $e->checkauth;
88 return retrieve_lineitem_impl($e, $li_id, $options);
91 sub retrieve_lineitem_impl {
92 my ($e, $li_id, $options) = @_;
98 jub => ['purchase_order', 'picklist'], # needed for permission check
104 my $fields = $flesh->{flesh_fields};
106 push(@{$fields->{jub}}, 'attributes') if $$options{flesh_attrs};
107 push(@{$fields->{jub}}, 'lineitem_notes') if $$options{flesh_notes};
108 push(@{$fields->{acqlin}}, 'alert_text') if $$options{flesh_notes};
109 push(@{$fields->{jub}}, 'order_summary') if $$options{flesh_order_summary};
110 push(@{$fields->{acqlin}}, 'cancel_reason') if $$options{flesh_cancel_reason};
112 if($$options{flesh_li_details}) {
113 push(@{$fields->{jub}}, 'lineitem_details');
114 push(@{$fields->{acqlid}}, 'fund') if $$options{flesh_fund};
115 push(@{$fields->{acqlid}}, 'fund_debit') if $$options{flesh_fund_debit};
116 push(@{$fields->{acqlid}}, 'cancel_reason') if $$options{flesh_cancel_reason};
119 if($$options{clear_marc}) { # avoid fetching marc blob
120 my @fields = grep { $_ ne 'marc' } Fieldmapper::acq::lineitem->new->real_fields;
121 $flesh->{select} = {jub => [@fields]};
124 my $li = $e->retrieve_acq_lineitem([$li_id, $flesh]) or return $e->event;
126 # collect the # of lids
127 if($$options{flesh_li_details}) {
128 $li->item_count(scalar(@{$li->lineitem_details}));
130 my $details = $e->search_acq_lineitem_detail({lineitem => $li_id}, {idlist=>1});
131 $li->item_count(scalar(@$details));
134 # attach claims to LIDs
135 if($$options{flesh_li_details}) {
136 foreach (@{$li->lineitem_details}) {
138 $e->search_acq_claim([
139 {"lineitem_detail", $_->id}, {
140 "flesh" => 1, "flesh_fields" => {"acqcl" => ["type"]}
147 return $e->event unless (
148 $li->purchase_order and
149 $e->allowed(['VIEW_PURCHASE_ORDER', 'CREATE_PURCHASE_ORDER'],
150 $li->purchase_order->ordering_agency, $li->purchase_order)
152 $li->picklist and !$li->purchase_order and # user doesn't have view_po perms
153 $e->allowed(['VIEW_PICKLIST', 'CREATE_PICKLIST'],
154 $li->picklist->org_unit, $li->picklist)
157 unless ($$options{flesh_po}) {
159 $li->purchase_order ? $li->purchase_order->id : undef
162 unless ($$options{flesh_pl}) {
163 $li->picklist($li->picklist ? $li->picklist->id : undef);
170 __PACKAGE__->register_method(
171 method => 'delete_lineitem',
172 api_name => 'open-ils.acq.lineitem.delete',
174 desc => 'Deletes a lineitem',
176 {desc => 'Authentication token', type => 'string'},
177 {desc => 'lineitem ID to delete', type => 'number'},
179 return => {desc => '1 on success, Event on error'}
183 sub delete_lineitem {
184 my($self, $conn, $auth, $li_id) = @_;
185 my $e = new_editor(xact=>1, authtoken=>$auth);
186 return $e->die_event unless $e->checkauth;
188 my $li = $e->retrieve_acq_lineitem($li_id)
189 or return $e->die_event;
194 my $picklist = $e->retrieve_acq_picklist($li->picklist)
195 or return $e->die_event;
196 return OpenILS::Event->new('BAD_PARAMS')
197 if $picklist->owner != $e->requestor->id;
202 # delete the attached lineitem_details
203 my $lid_ids = $e->search_acq_lineitem_detail(
204 {lineitem => $li_id}, {idlist=>1});
206 for my $lid_id (@$lid_ids) {
207 $e->delete_acq_lineitem_detail(
208 $e->retrieve_acq_lineitem_detail($lid_id))
209 or return $e->die_event;
212 $e->delete_acq_lineitem($li) or return $e->die_event;
218 __PACKAGE__->register_method(
219 method => 'update_lineitem',
220 api_name => 'open-ils.acq.lineitem.update',
222 desc => 'Update one or many lineitems',
224 {desc => 'Authentication token', type => 'string'},
225 {desc => 'lineitem object update', type => 'object'}
227 return => {desc => '1 on success, Event on error'}
231 sub update_lineitem {
232 my($self, $conn, $auth, $li) = @_;
233 my $e = new_editor(xact=>1, authtoken=>$auth);
234 return $e->die_event unless $e->checkauth;
236 $li = [$li] unless ref $li eq "ARRAY";
238 my $evt = update_lineitem_impl($e, $_);
246 sub update_lineitem_impl {
249 my $orig_li = $e->retrieve_acq_lineitem([
251 { flesh => 1, # grab the lineitem with picklist attached
252 flesh_fields => {jub => ['picklist', 'purchase_order']}
254 ]) or return $e->die_event;
256 # the marc may have been cleared on retrieval...
257 $li->marc($orig_li->marc) unless $li->marc;
259 $li->editor($e->requestor->id);
260 $li->edit_time('now');
261 $e->update_acq_lineitem($li) or return $e->die_event;
265 __PACKAGE__->register_method(
266 method => 'lineitem_search',
267 api_name => 'open-ils.acq.lineitem.search',
270 desc => 'Searches lineitems',
272 {desc => 'Authentication token', type => 'string'},
273 {desc => 'Search definition', type => 'object'},
274 {desc => 'Options hash. idlist=true', type => 'object'},
275 {desc => 'List of lineitems', type => 'object/number'},
280 sub lineitem_search {
281 my($self, $conn, $auth, $search, $options) = @_;
282 my $e = new_editor(authtoken=>$auth, xact=>1);
283 return $e->event unless $e->checkauth;
284 return $e->event unless $e->allowed('CREATE_PICKLIST');
285 # XXX needs permissions consideration
286 my $lis = $e->search_acq_lineitem($search, {idlist=>1});
287 for my $li_id (@$lis) {
288 if($$options{idlist}) {
289 $conn->respond($li_id);
291 my $res = retrieve_lineitem($self, $conn, $auth, $li_id, $options);
292 $conn->respond($res) unless $U->event_code($res);
298 __PACKAGE__->register_method (
299 method => 'lineitems_related_by_bib',
300 api_name => 'open-ils.acq.lineitems_for_bib.by_bib_id',
303 Retrieves lineitems attached to same bib record, subject to the PO ordering agency. This variant takes the bib id.
304 @param authtoken Login session key
305 @param bib_id Id for the pertinent bib record.
306 @param options Object for tweaking the selection criteria and fleshing options.
310 __PACKAGE__->register_method (
311 method => 'lineitems_related_by_bib',
312 api_name => 'open-ils.acq.lineitems_for_bib.by_lineitem_id',
315 Retrieves lineitems attached to same bib record, subject to the PO ordering agency. This variant takes the id for any of the pertinent lineitems.
316 @param authtoken Login session key
317 @param bib_id Id for a pertinent lineitem.
318 @param options Object for tweaking the selection criteria and fleshing options.
322 __PACKAGE__->register_method (
323 method => 'lineitems_related_by_bib',
324 api_name => 'open-ils.acq.lineitems_for_bib.by_lineitem_id.count',
326 signature => q/See open-ils.acq.lineitems_for_bib.by_lineitem_id. This version returns numbers of lineitems only (XXX may count lineitems we don't actually have permission to retrieve)/
329 sub lineitems_related_by_bib {
330 my($self, $conn, $auth, $test_value, $options) = @_;
331 my $e = new_editor(authtoken => $auth);
332 return $e->event unless $e->checkauth;
334 my $perm_orgs = $U->user_has_work_perm_at($e, 'VIEW_PURCHASE_ORDER', {descendants =>1}, $e->requestor->id);
337 "select"=>{"jub"=>["id"]},
338 "from"=>{"jub" => {"acqpo" => {type => 'left'}, "acqpl" => {type => 'left'}}},
341 { "+acqpo"=>{ "ordering_agency" => $perm_orgs } },
342 { '+acqpl' => { org_unit => $perm_orgs } }
345 "order_by"=>[{"class"=>"jub", "field"=>"create_time", "direction"=>"desc"}]
348 # Be sure we just return the original LI if no related bibs
349 if ($self->api_name =~ /by_lineitem_id/) {
350 my $orig = retrieve_lineitem($self, $conn, $auth, $test_value) or
351 return $e->die_event;
352 if ($test_value = $orig->eg_bib_id) {
353 $query->{"where"}->{"eg_bib_id"} = $test_value;
355 $query->{"where"}->{"id"} = $orig->id;
357 } elsif ($test_value) {
358 $query->{"where"}->{"eg_bib_id"} = $test_value;
361 return new OpenILS::Event("BAD_PARAMS", "Null bib id");
364 if ($options && defined $options->{lineitem_state}) {
365 $query->{'where'}{'jub'}{'state'} = $options->{lineitem_state};
368 if ($options && defined $options->{po_state}) {
369 $query->{'where'}{'+acqpo'}{'state'} = $options->{po_state};
372 if ($options && defined $options->{order_by}) {
373 $query->{'order_by'} = $options->{order_by};
376 my $results = $e->json_query($query);
377 if ($self->api_name =~ /count$/) {
378 return scalar(@$results);
380 for my $result (@$results) {
381 # retrieve_lineitem takes care of POs and PLs and also handles
382 # options like flesh_notes and permissions checking.
384 retrieve_lineitem($self, $conn, $auth, $result->{"id"}, $options)
393 __PACKAGE__->register_method(
394 method => "lineitem_search_by_attributes",
395 api_name => "open-ils.acq.lineitem.search.by_attributes",
398 desc => "Performs a search against lineitem_attrs",
400 {desc => "Authentication token", type => "string"},
403 attr_value_pairs : list of pairs of (attr definition ID, attr value) where value can be scalar (fuzzy match) or array (exact match)
404 li_states : list of lineitem states
405 po_agencies : list of purchase order ordering agencies (org) ids
407 At least one of these search terms is required.
412 idlist : if set, only return lineitem IDs
413 clear_marc : if set, strip the MARC xml from the lineitem before delivery
414 flesh_attrs : flesh lineitem attributes;
421 __PACKAGE__->register_method(
422 method => "lineitem_search_by_attributes",
423 api_name => "open-ils.acq.lineitem.search.by_attributes.ident",
426 desc => "Performs a search against lineitem_attrs where ident is true.".
427 "See open-ils.acq.lineitem.search.by_attributes for params."
431 sub lineitem_search_by_attributes {
432 my ($self, $conn, $auth, $search, $options) = @_;
434 my $e = new_editor(authtoken => $auth, xact => 1);
435 return $e->die_event unless $e->checkauth;
436 # XXX needs permissions consideration
438 return [] unless $search;
439 my $attr_value_pairs = $search->{attr_value_pairs};
440 my $li_states = $search->{li_states};
441 my $po_agencies = $search->{po_agencies}; # XXX if none, base it on perms
444 "select" => {"acqlia" =>
445 [{"column" => "lineitem", "transform" => "distinct"}]
449 "acqliad" => {"field" => "id", "fkey" => "definition"},
452 "fkey" => "lineitem",
457 "fkey" => "purchase_order"
466 $where->{"+acqliad"} = {"ident" => "t"}
467 if $self->api_name =~ /\.ident/;
469 my $searched_for_something = 0;
471 if (ref $attr_value_pairs eq "ARRAY") {
472 $where->{"-or"} = [];
473 foreach (@$attr_value_pairs) {
475 my ($def, $value) = @$_;
476 push @{$where->{"-or"}}, {
478 "attr_value" => (ref $value) ?
479 $value : {"ilike" => "%" . $value . "%"},
484 $searched_for_something = 1;
487 if ($li_states and @$li_states) {
488 $where->{"+jub"} = {"state" => $li_states};
489 $searched_for_something = 1;
492 if ($po_agencies and @$po_agencies) {
493 $where->{"+acqpo"} = {"ordering_agency" => $po_agencies};
494 $searched_for_something = 1;
497 if (not $searched_for_something) {
499 return new OpenILS::Event(
500 "BAD_PARAMS", note => "You have provided no search terms."
504 $query->{"where"} = $where;
505 my $lis = $e->json_query($query);
507 for my $li_id_obj (@$lis) {
508 my $li_id = $li_id_obj->{"lineitem"};
509 if($options->{"idlist"}) {
510 $conn->respond($li_id);
513 retrieve_lineitem($self, $conn, $auth, $li_id, $options)
521 __PACKAGE__->register_method(
522 method => 'lineitem_search_ident',
523 api_name => 'open-ils.acq.lineitem.search.ident',
526 desc => 'Performs a search against lineitem_attrs where ident is true',
528 {desc => 'Authentication token', type => 'string'},
529 { desc => q/Search definition. Options are:
530 attr_values : list of attribute values (required)
531 li_states : list of lineitem states
532 po_agencies : list of purchase order ordering agencies (org) ids
537 Options hash. Options are:
538 idlist : if set, only return lineitem IDs
539 clear_marc : if set, strip the MARC xml from the lineitem before delivery
540 flesh_attrs : flesh lineitem attributes;
548 my $LI_ATTR_SEARCH = {
549 select => {acqlia => ['lineitem']},
562 fkey => 'purchase_order'
570 sub lineitem_search_ident {
571 my($self, $conn, $auth, $search, $options) = @_;
572 my $e = new_editor(authtoken=>$auth, xact=>1);
573 return $e->event unless $e->checkauth;
574 # XXX needs permissions consideration
576 return [] unless $search;
577 my $attr_values = $search->{attr_values};
578 my $li_states = $search->{li_states};
579 my $po_agencies = $search->{po_agencies}; # XXX if none, base it on perms
584 '+acqliad' => {ident => 't'},
588 push(@{$where_clause->{'-or'}}, {attr_value => {ilike => "%$_%"}}) for @$attr_values;
590 $where_clause->{'+jub'} = {state => {in => $li_states}}
591 if $li_states and @$li_states;
593 $where_clause->{'+acqpo'} = {ordering_agency => $po_agencies}
594 if $po_agencies and @$po_agencies;
596 $LI_ATTR_SEARCH->{where} = $where_clause;
598 my $lis = $e->json_query($LI_ATTR_SEARCH);
600 for my $li_id_obj (@$lis) {
601 my $li_id = $li_id_obj->{lineitem};
602 if($$options{idlist}) {
603 $conn->respond($li_id);
606 if($$options{flesh_attrs}) {
607 $li = $e->retrieve_acq_lineitem([
608 $li_id, {flesh => 1, flesh_fields => {jub => ['attributes']}}])
610 $li = $e->retrieve_acq_lineitem($li_id);
612 $li->clear_marc if $$options{clear_marc};
622 # this call duplicates a call in Order.pm and makes references to subs that don't exist.
623 # TODO: Verify then remove.
624 __PACKAGE__->register_method(
625 method => 'lineitem_detail_CUD_batch',
626 api_name => 'open-ils.acq.lineitem_detail.cud.batch_',
629 desc => q/Creates a new purchase order line item detail.
630 Additionally creates the associated fund_debit/,
632 {desc => 'Authentication token', type => 'string'},
633 {desc => 'List of lineitem_details to create', type => 'array'},
635 return => {desc => 'Streaming response of current position in the array'}
639 sub lineitem_detail_CUD_batch {
640 my($self, $conn, $auth, $li_details, $options) = @_;
641 my $e = new_editor(xact=>1, authtoken=>$auth);
642 return $e->die_event unless $e->checkauth;
644 my $total = scalar(@$li_details);
645 for my $li_detail (@$li_details) {
649 $logger->info(Dumper($li_detail));
650 $logger->info('lid id ' . $li_detail->id);
651 $logger->info('lineitem ' . $li_detail->lineitem);
653 if($li_detail->isnew) {
654 $res = create_lineitem_detail_impl($self, $conn, $e, $li_detail, $options);
655 } elsif($li_detail->ischanged) {
656 $res = update_lineitem_detail_impl($self, $conn, $e, $li_detail);
657 } elsif($li_detail->isdeleted) {
658 $res = delete_lineitem_detail_impl($self, $conn, $e, $li_detail->id);
660 return $e->event if $e->died;
661 $conn->respond({maximum => $total, progress => $pos++, li => $res});
664 return {complete => 1};
668 sub create_lineitem_detail_impl {
669 my($self, $conn, $e, $li_detail, $options) = @_;
672 my $li = $e->retrieve_acq_lineitem($li_detail->lineitem)
673 or return $e->die_event;
675 my $evt = update_li_edit_time($e, $li);
678 # XXX check lineitem provider perms
680 if($li_detail->fund) {
681 my $fund = $e->retrieve_acq_fund($li_detail->fund) or return $e->die_event;
682 return $e->die_event unless
683 $e->allowed('MANAGE_FUND', $fund->org, $fund);
686 $e->create_acq_lineitem_detail($li_detail) or return $e->die_event;
688 unless($li_detail->barcode) {
689 my $pfx = $U->ou_ancestor_setting_value($li_detail->owning_lib, 'acq.tmp_barcode_prefix') || 'ACQ';
690 $li_detail->barcode($pfx.$li_detail->id);
692 unless($li_detail->cn_label) {
693 my $pfx = $U->ou_ancestor_setting_value($li_detail->owning_lib, 'acq.tmp_callnumber_prefix') || 'ACQ';
694 $li_detail->cn_label($pfx.$li_detail->id);
697 if(my $loc = $U->ou_ancestor_setting_value($li_detail->owning_lib, 'acq.default_copy_location')) {
698 $li_detail->location($loc);
701 $e->update_acq_lineitem_detail($li_detail) or return $e->die_event;
703 return $li_detail if $$options{return_obj};
704 return $li_detail->id
708 sub update_li_edit_time {
710 # some lineitem edits are allowed after approval time...
711 # return OpenILS::Event->new('ACQ_LINEITEM_APPROVED', payload => $li->id)
712 # if $li->state eq 'approved';
713 $li->edit_time('now');
714 $li->editor($e->requestor->id);
715 $e->update_acq_lineitem($li) or return $e->die_event;
720 __PACKAGE__->register_method(
721 method => 'retrieve_lineitem_detail',
722 api_name => 'open-ils.acq.lineitem_detail.retrieve',
724 desc => q/Updates a lineitem detail/,
726 {desc => 'Authentication token', type => 'string'},
727 {desc => 'id of lineitem_detail to retrieve', type => 'number'},
729 return => {desc => 'object on success, Event on failure'}
732 sub retrieve_lineitem_detail {
733 my($self, $conn, $auth, $li_detail_id) = @_;
734 my $e = new_editor(authtoken=>$auth);
735 return $e->event unless $e->checkauth;
737 my $li_detail = $e->retrieve_acq_lineitem_detail($li_detail_id)
740 if($li_detail->fund) {
741 my $fund = $e->retrieve_acq_fund($li_detail->fund) or return $e->event;
742 return $e->event unless
743 $e->allowed('MANAGE_FUND', $fund->org, $fund);
746 # XXX check lineitem perms
752 __PACKAGE__->register_method(
753 method => 'approve_lineitem',
754 api_name => 'open-ils.acq.lineitem.approve',
756 desc => 'Mark a lineitem as approved',
758 {desc => 'Authentication token', type => 'string'},
759 {desc => 'lineitem ID', type => 'number'}
761 return => {desc => '1 on success, Event on error'}
764 sub approve_lineitem {
765 my($self, $conn, $auth, $li_id) = @_;
766 my $e = new_editor(xact=>1, authtoken=>$auth);
767 return $e->die_event unless $e->checkauth;
769 # XXX perm checks for each lineitem detail
771 my $li = $e->retrieve_acq_lineitem($li_id)
772 or return $e->die_event;
774 return OpenILS::Event->new('ACQ_LINEITEM_APPROVED', payload => $li_id)
775 if $li->state eq 'approved';
777 my $details = $e->search_acq_lineitem_detail({lineitem => $li_id});
778 return OpenILS::Event->new('ACQ_LINEITEM_NO_COPIES', payload => $li_id)
779 unless scalar(@$details) > 0;
781 for my $detail (@$details) {
782 return OpenILS::Event->new('ACQ_LINEITEM_DETAIL_NO_FUND', payload => $detail->id)
783 unless $detail->fund;
785 return OpenILS::Event->new('ACQ_LINEITEM_DETAIL_NO_ORG', payload => $detail->id)
786 unless $detail->owning_lib;
789 $li->state('approved');
790 $li->edit_time('now');
791 $e->update_acq_lineitem($li) or return $e->die_event;
799 __PACKAGE__->register_method(
800 method => 'set_lineitem_attr',
801 api_name => 'open-ils.acq.lineitem_usr_attr.set',
803 desc => 'Sets a lineitem_usr_attr value',
805 {desc => 'Authentication token', type => 'string'},
806 {desc => 'Lineitem ID', type => 'number'},
807 {desc => 'Attr name', type => 'string'},
808 {desc => 'Attr value', type => 'string'}
810 return => {desc => '1 on success, Event on error'}
814 __PACKAGE__->register_method(
815 method => 'set_lineitem_attr',
816 api_name => 'open-ils.acq.lineitem_local_attr.set',
818 desc => 'Sets a lineitem_local_attr value',
820 {desc => 'Authentication token', type => 'string'},
821 {desc => 'Lineitem ID', type => 'number'},
822 {desc => 'Attr name', type => 'string'},
823 {desc => 'Attr value', type => 'string'}
825 return => {desc => 'ID of the attr object on success, Event on error'}
830 sub set_lineitem_attr {
831 my($self, $conn, $auth, $li_id, $attr_name, $attr_value) = @_;
832 my $e = new_editor(xact=>1, authtoken=>$auth);
833 return $e->die_event unless $e->checkauth;
837 my $attr_type = $self->api_name =~ /local_attr/ ?
838 'lineitem_local_attr_definition' : 'lineitem_usr_attr_definition';
840 my $attr = $e->search_acq_lineitem_attr({
842 attr_type => $attr_type,
843 attr_name => $attr_name})->[0];
845 my $find = "search_acq_$attr_type";
848 $attr->attr_value($attr_value);
849 $e->update_acq_lineitem_attr($attr) or return $e->die_event;
851 $attr = Fieldmapper::acq::lineitem_attr->new;
852 $attr->lineitem($li_id);
853 $attr->attr_type($attr_type);
854 $attr->attr_name($attr_name);
855 $attr->attr_value($attr_value);
857 my $attr_def_id = $e->$find({code => $attr_name}, {idlist=>1})->[0]
858 or return $e->die_event;
859 $attr->definition($attr_def_id);
860 $e->create_acq_lineitem_attr($attr) or return $e->die_event;
867 __PACKAGE__->register_method(
868 method => 'get_lineitem_attr_defs',
869 api_name => 'open-ils.acq.lineitem_attr_definition.retrieve.all',
871 desc => 'Retrieve lineitem attr definitions',
873 {desc => 'Authentication token', type => 'string'},
875 return => {desc => 'List of attr definitions'}
879 sub get_lineitem_attr_defs {
880 my($self, $conn, $auth) = @_;
881 my $e = new_editor(authtoken=>$auth);
882 return $e->event unless $e->checkauth;
884 for my $type (qw/generated marc local usr provider/) {
885 my $call = "retrieve_all_acq_lineitem_${type}_attr_definition";
886 $results{$type} = $e->$call;
892 __PACKAGE__->register_method(
893 method => 'lineitem_note_CUD_batch',
894 api_name => 'open-ils.acq.lineitem_note.cud.batch',
897 desc => q/Manage lineitem notes/,
899 {desc => 'Authentication token', type => 'string'},
900 {desc => 'List of lineitem_notes to manage', type => 'array'},
902 return => {desc => 'Streaming response of current position in the array'}
906 sub lineitem_note_CUD_batch {
907 my($self, $conn, $auth, $li_notes) = @_;
909 my $e = new_editor(xact=>1, authtoken=>$auth);
910 return $e->die_event unless $e->checkauth;
913 my $total = @$li_notes;
916 for my $note (@$li_notes) {
918 $note->editor($e->requestor->id);
919 $note->edit_time('now');
922 $note->creator($e->requestor->id);
923 $note = $e->create_acq_lineitem_note($note) or return $e->die_event;
925 } elsif($note->isdeleted) {
926 $e->delete_acq_lineitem_note($note) or return $e->die_event;
928 } elsif($note->ischanged) {
929 $e->update_acq_lineitem_note($note) or return $e->die_event;
932 if(!$note->isdeleted) {
933 $note = $e->retrieve_acq_lineitem_note([
935 "flesh" => 1, "flesh_fields" => {"acqlin" => ["alert_text"]}
940 $conn->respond({maximum => $total, progress => ++$count, note => $note});
944 return {complete => 1};
947 __PACKAGE__->register_method(
948 method => 'ranged_line_item_alert_text',
949 api_name => 'open-ils.acq.line_item_alert_text.ranged.retrieve.all');
951 sub ranged_line_item_alert_text {
952 my($self, $conn, $auth, $org_id, $depth) = @_;
953 my $e = new_editor(authtoken => $auth);
954 return $e->event unless $e->checkauth;
955 return $e->event unless $e->allowed('ADMIN_ACQ_LINEITEM_ALERT_TEXT', $org_id);
956 return $e->search_acq_lineitem_alert_text(
957 {owning_lib => $U->get_org_full_path($org_id, $depth)});
961 __PACKAGE__->register_method(
962 method => "retrieve_lineitem_by_copy_id",
963 api_name => "open-ils.acq.lineitem.retrieve.by_copy_id",
965 desc => q/Manage lineitem notes/,
967 {desc => "Authentication token", type => "string"},
968 {desc => "Evergreen internal copy ID", type => "number"},
969 {desc => "Hash of options (see open-ils.acq.lineitem.retrieve",
973 desc => "Lineitem associated with given copy",
974 type => "object", class => "jub"
979 sub retrieve_lineitem_by_copy_id {
980 my ($self, $conn, $auth, $object_id, $options) = @_;
982 my $e = new_editor("authtoken" => $auth);
983 return $e->die_event unless $e->checkauth;
985 my $result = $e->json_query({
986 "select" => {"acqlid" => ["lineitem"]},
988 "where" => {"eg_copy_id" => $object_id}
991 return new OpenILS::Event("ACQ_LINEITEM_NOT_FOUND");
994 my $li = retrieve_lineitem_impl($e, $result->{"lineitem"}, $options) or
995 return $e->die_event;