1 package OpenILS::Application::Acq::BatchManager;
2 use OpenILS::Application::Acq::Financials;
3 use OpenSRF::AppSession;
4 use OpenSRF::EX qw/:try/;
5 use strict; use warnings;
8 my($class, %args) = @_;
9 my $self = bless(\%args, $class);
18 purchase_order => undef,
26 $self->throttle(4) unless $self->throttle;
27 $self->{post_proc_queue} = [];
28 $self->{last_respond_progress} = 0;
34 $self->{conn} = $val if $val;
39 $self->{throttle} = $val if $val;
40 return $self->{throttle};
43 my($self, %other_args) = @_;
44 if($self->throttle and not %other_args) {
46 ($self->{args}->{progress} - $self->{last_respond_progress}) >= $self->throttle
49 $self->conn->respond({ %{$self->{args}}, %other_args });
50 $self->{last_respond_progress} = $self->{args}->{progress};
51 $self->throttle($self->throttle * 2) unless $self->throttle >= 256;
53 sub respond_complete {
54 my($self, %other_args) = @_;
56 $self->conn->respond_complete({ %{$self->{args}}, %other_args });
57 $self->run_post_response_hooks;
61 # run the post response hook subs, shifting them off as we go
62 sub run_post_response_hooks {
64 (shift @{$self->{post_proc_queue}})->() while @{$self->{post_proc_queue}};
67 # any subs passed to this method will be run after the call to respond_complete
70 push(@{$self->{post_proc_queue}}, $sub);
75 $self->{args}->{total} = $val if defined $val;
76 $self->{args}->{maximum} = $self->{args}->{total};
77 return $self->{args}->{total};
81 $self->{args}->{purchase_order} = $val if $val;
86 $self->{args}->{picklist} = $val if $val;
91 $self->{args}->{lid} += 1;
92 $self->{args}->{progress} += 1;
97 $self->{args}->{li} += 1;
98 $self->{args}->{progress} += 1;
103 $self->{args}->{vqbr} += 1;
104 $self->{args}->{progress} += 1;
109 $self->{args}->{copies} += 1;
110 $self->{args}->{progress} += 1;
115 $self->{args}->{bibs} += 1;
116 $self->{args}->{progress} += 1;
120 my($self, $amount) = @_;
121 $self->{args}->{debits_accrued} += $amount;
122 $self->{args}->{progress} += 1;
126 my($self, $editor) = @_;
127 $self->{editor} = $editor if defined $editor;
128 return $self->{editor};
132 $self->{args}->{complete} = 1;
137 my($self, $org, $key, $val) = @_;
138 $self->{cache}->{$org} = {} unless $self->{cache}->{org};
139 $self->{cache}->{$org}->{$key} = $val if defined $val;
140 return $self->{cache}->{$org}->{$key};
144 package OpenILS::Application::Acq::Order;
145 use base qw/OpenILS::Application/;
146 use strict; use warnings;
147 # ----------------------------------------------------------------------------
148 # Break up each component of the order process and pieces into managable
149 # actions that can be shared across different workflows
150 # ----------------------------------------------------------------------------
152 use OpenSRF::Utils::Logger qw(:logger);
153 use OpenSRF::Utils::JSON;
154 use OpenSRF::AppSession;
155 use OpenILS::Utils::Fieldmapper;
156 use OpenILS::Utils::CStoreEditor q/:funcs/;
157 use OpenILS::Utils::Normalize qw/clean_marc/;
158 use OpenILS::Const qw/:const/;
159 use OpenSRF::EX q/:try/;
160 use OpenILS::Application::AppUtils;
161 use OpenILS::Application::Cat::BibCommon;
162 use OpenILS::Application::Cat::AssetCommon;
165 use MARC::File::XML (BinaryEncoding => 'UTF-8');
166 use Digest::MD5 qw(md5_hex);
168 $Data::Dumper::Indent = 0;
169 my $U = 'OpenILS::Application::AppUtils';
172 # ----------------------------------------------------------------------------
174 # ----------------------------------------------------------------------------
175 sub create_lineitem {
176 my($mgr, %args) = @_;
177 my $li = Fieldmapper::acq::lineitem->new;
178 $li->creator($mgr->editor->requestor->id);
179 $li->selector($li->creator);
180 $li->editor($li->creator);
181 $li->create_time('now');
182 $li->edit_time('now');
184 $li->$_($args{$_}) for keys %args;
187 $mgr->editor->create_acq_lineitem($li) or return 0;
189 unless($li->estimated_unit_price) {
190 # extract the price from the MARC data
191 my $price = get_li_price_from_attr($mgr->editor, $li) or return $li;
192 $li->estimated_unit_price($price);
193 return update_lineitem($mgr, $li);
199 sub get_li_price_from_attr {
201 my $attrs = $li->attributes || $e->search_acq_lineitem_attr({lineitem => $li->id});
203 for my $attr_type (qw/
204 lineitem_local_attr_definition
205 lineitem_prov_attr_definition
206 lineitem_marc_attr_definition/) {
209 $_->attr_name eq 'estimated_price' and
210 $_->attr_type eq $attr_type } @$attrs;
212 return $attr->attr_value if $attr;
219 sub update_lineitem {
221 $li->edit_time('now');
222 $li->editor($mgr->editor->requestor->id);
224 return $mgr->editor->retrieve_acq_lineitem($mgr->editor->data) if
225 $mgr->editor->update_acq_lineitem($li);
230 # ----------------------------------------------------------------------------
231 # Create real holds from patron requests for a given lineitem
232 # ----------------------------------------------------------------------------
233 sub promote_lineitem_holds {
236 my $requests = $mgr->editor->search_acq_user_request(
237 { lineitem => $li->id,
239 [ { need_before => {'>' => 'now'} },
240 { need_before => undef }
245 for my $request ( @$requests ) {
247 $request->eg_bib( $li->eg_bib_id );
248 $mgr->editor->update_acq_user_request( $request ) or return 0;
250 next unless ($U->is_true( $request->hold ));
252 my $hold = Fieldmapper::action::hold_request->new;
253 $hold->usr( $request->usr );
254 $hold->requestor( $request->usr );
255 $hold->request_time( $request->request_date );
256 $hold->pickup_lib( $request->pickup_lib );
257 $hold->request_lib( $request->pickup_lib );
258 $hold->selection_ou( $request->pickup_lib );
259 $hold->phone_notify( $request->phone_notify );
260 $hold->email_notify( $request->email_notify );
261 $hold->expire_time( $request->need_before );
263 if ($request->holdable_formats) {
264 my $mrm = $mgr->editor->search_metabib_metarecord_source_map( { source => $li->eg_bib_id } )->[0];
266 $hold->hold_type( 'M' );
267 $hold->holdable_formats( $request->holdable_formats );
268 $hold->target( $mrm->metarecord );
272 if (!$hold->target) {
273 $hold->hold_type( 'T' );
274 $hold->target( $li->eg_bib_id );
277 # if behind-the-desk holds are supported at the
278 # pickup library, apply the patron default
279 my $bdous = $U->ou_ancestor_setting_value(
281 'circ.holds.behind_desk_pickup_supported',
286 my $set = $mgr->editor->search_actor_user_setting(
287 {usr => $hold->usr, name => 'circ.holds_behind_desk'})->[0];
289 $hold->behind_desk('t') if $set and
290 OpenSRF::Utils::JSON->JSON2perl($set->value);
293 $mgr->editor->create_action_hold_request( $hold ) or return 0;
299 sub delete_lineitem {
301 $li = $mgr->editor->retrieve_acq_lineitem($li) unless ref $li;
303 # delete the attached lineitem_details
304 my $lid_ids = $mgr->editor->search_acq_lineitem_detail({lineitem => $li->id}, {idlist=>1});
305 for my $lid_id (@$lid_ids) {
306 return 0 unless delete_lineitem_detail($mgr, $lid_id);
310 return $mgr->editor->delete_acq_lineitem($li);
313 # begins and commit transactions as it goes
314 # bib_only exits before creation of copies and callnumbers
315 sub create_lineitem_list_assets {
316 my($mgr, $li_ids, $vandelay, $bib_only) = @_;
318 # Do not create line items if none are specified
319 return {} unless (scalar(@$li_ids));
321 if (check_import_li_marc_perms($mgr, $li_ids)) { # event on error
322 $logger->error("acq-vl: user does not have permission to import acq records");
326 my $res = import_li_bibs_via_vandelay($mgr, $li_ids, $vandelay);
327 return undef unless $res;
328 return $res if $bib_only;
330 # create the bibs/volumes/copies for the successfully imported records
331 for my $li_id (@{$res->{li_ids}}) {
332 $mgr->editor->xact_begin;
333 my $data = create_lineitem_assets($mgr, $li_id) or return undef;
334 $mgr->editor->xact_commit;
341 sub test_vandelay_import_args {
342 my $vandelay = shift;
343 my $q_needed = shift;
345 # we need valid args and (sometimes) a queue
346 return 0 unless $vandelay and (
348 $vandelay->{queue_name} or
349 $vandelay->{existing_queue}
352 # match-based merge/overlay import
353 return 2 if $vandelay->{merge_profile} and (
354 $vandelay->{auto_overlay_exact} or
355 $vandelay->{auto_overlay_1match} or
356 $vandelay->{auto_overlay_best_match}
360 return 2 if $vandelay->{import_no_match};
362 return 1; # queue only
365 sub find_or_create_vandelay_queue {
366 my ($e, $vandelay) = @_;
369 if (my $name = $vandelay->{queue_name}) {
371 # first, see if a queue w/ this name already exists
372 # for this user. If so, use that instead.
374 $queue = $e->search_vandelay_bib_queue(
375 {name => $name, owner => $e->requestor->id})->[0];
379 $logger->info("acq-vl: using existing queue $name");
383 $logger->info("acq-vl: creating new vandelay queue $name");
385 $queue = new Fieldmapper::vandelay::bib_queue;
387 $queue->queue_type('acq');
388 $queue->owner($e->requestor->id);
389 $queue->match_set($vandelay->{match_set} || undef); # avoid ''
390 $queue = $e->create_vandelay_bib_queue($queue) or return undef;
394 $queue = $e->retrieve_vandelay_bib_queue($vandelay->{existing_queue})
402 sub import_li_bibs_via_vandelay {
403 my ($mgr, $li_ids, $vandelay) = @_;
404 my $res = {li_ids => []};
405 my $e = $mgr->editor;
408 my $needs_importing = $e->search_acq_lineitem(
409 {id => $li_ids, eg_bib_id => undef},
413 if (!@$needs_importing) {
414 $logger->info("acq-vl: all records already imported. no Vandelay work to do");
415 return {li_ids => $li_ids};
418 # see if we have any records that are not yet linked to VL records (i.e.
419 # not in a queue). This will tell us if lack of a queue name is an error.
420 my $non_queued = $e->search_acq_lineitem(
421 {id => $needs_importing, queued_record => undef},
425 # add the already-imported records to the response list
426 push(@{$res->{li_ids}}, grep { $_ != @$needs_importing } @$li_ids);
428 $logger->info("acq-vl: processing recs via Vandelay with args: ".Dumper($vandelay));
430 my $vl_stat = test_vandelay_import_args($vandelay, scalar(@$non_queued));
432 $logger->error("acq-vl: invalid vandelay arguments for acq import (queue needed)");
438 # when any non-queued lineitems exist, their vandelay counterparts
439 # require a place to live.
440 $queue = find_or_create_vandelay_queue($e, $vandelay) or return $res;
443 # if all lineitems are already queued, the queue reported to the user
444 # is purely for information / convenience. pick a random queue.
445 $queue = $e->retrieve_acq_lineitem([
446 $needs_importing->[0], {
449 jub => ['queued_record'],
453 ])->queued_record->queue;
456 $mgr->{args}->{queue} = $queue;
458 # load the lineitems into the queue for merge processing
461 for my $li_id (@$needs_importing) {
463 my $li = $e->retrieve_acq_lineitem($li_id) or return $res;
465 if ($li->queued_record) {
466 $logger->info("acq-vl: $li_id already linked to a vandelay record");
467 push(@vqbr_ids, $li->queued_record);
470 $logger->info("acq-vl: creating new vandelay record for lineitem $li_id");
472 # create a new VL queued record and link it up
473 my $vqbr = Fieldmapper::vandelay::queued_bib_record->new;
474 $vqbr->marc($li->marc);
475 $vqbr->queue($queue->id);
476 $vqbr->bib_source($vandelay->{bib_source} || undef); # avoid ''
477 $vqbr = $e->create_vandelay_queued_bib_record($vqbr) or return $res;
478 push(@vqbr_ids, $vqbr->id);
480 # tell the acq record which vandelay record it's linked to
481 $li->queued_record($vqbr->id);
482 $e->update_acq_lineitem($li) or return $res;
490 $logger->info("acq-vl: created vandelay records [@vqbr_ids]");
492 # we have to commit the transaction now since
493 # vandelay uses its own transactions.
496 return $res if $vl_stat == 1; # queue only
498 # Import the bibs via vandelay. Note: Vandely will
499 # update acq.lineitem.eg_bib_id on successful import.
501 $vandelay->{report_all} = 1;
502 my $ses = OpenSRF::AppSession->create('open-ils.vandelay');
503 my $req = $ses->request(
504 'open-ils.vandelay.bib_record.list.import',
505 $e->authtoken, \@vqbr_ids, $vandelay);
507 # pull the responses, noting all that were successfully imported
509 while (my $resp = $req->recv(timeout => 600)) {
510 my $stat = $resp->content;
512 if(!$stat or $U->event_code($stat)) { # import failure
513 $logger->error("acq-vl: error importing vandelay record " . Dumper($stat));
517 # "imported" refers to the vqbr id, not the
518 # success/failure of the vqbr merge attempt
519 next unless $stat->{imported};
521 my ($imported) = grep {$_->queued_record eq $stat->{imported}} @lis;
522 my $li_id = $imported->id;
524 if ($stat->{no_import}) {
525 $logger->info("acq-vl: acq lineitem $li_id did not import");
527 } else { # successful import
529 push(@success_lis, $li_id);
532 $logger->info("acq-vl: acq lineitem $li_id successfully merged/imported");
537 $logger->info("acq-vl: successfully imported lineitems [@success_lis]");
539 # add the successfully imported lineitems to the already-imported lineitems
540 push (@{$res->{li_ids}}, @success_lis);
545 # returns event on error, undef on success
546 sub check_import_li_marc_perms {
547 my($mgr, $li_ids) = @_;
549 # if there are any order records that are not linked to
550 # in-db bib records, verify staff has perms to import order records
551 my $order_li = $mgr->editor->search_acq_lineitem(
552 [{id => $li_ids, eg_bib_id => undef}, {limit => 1}], {idlist => 1})->[0];
555 return $mgr->editor->die_event unless
556 $mgr->editor->allowed('IMPORT_ACQ_LINEITEM_BIB_RECORD');
563 # ----------------------------------------------------------------------------
564 # if all of the lineitem details for this lineitem have
565 # been received, mark the lineitem as received
566 # returns 1 on non-received, li on received, 0 on error
567 # ----------------------------------------------------------------------------
569 sub describe_affected_po {
573 OpenILS::Application::Acq::Financials::build_price_summary(
578 "state" => $po->state,
579 "amount_encumbered" => $enc,
580 "amount_spent" => $spent
585 sub check_lineitem_received {
586 my($mgr, $li_id) = @_;
588 my $non_recv = $mgr->editor->search_acq_lineitem_detail(
589 {recv_time => undef, lineitem => $li_id}, {idlist=>1});
591 return 1 if @$non_recv;
593 my $li = $mgr->editor->retrieve_acq_lineitem($li_id);
594 $li->state('received');
595 return update_lineitem($mgr, $li);
598 sub receive_lineitem {
599 my($mgr, $li_id, $skip_complete_check) = @_;
600 my $li = $mgr->editor->retrieve_acq_lineitem($li_id) or return 0;
602 return 0 unless $li->state eq 'on-order' or $li->state eq 'cancelled'; # sic
604 $li->clear_cancel_reason; # un-cancel on receive
606 my $lid_ids = $mgr->editor->search_acq_lineitem_detail(
607 {lineitem => $li_id, recv_time => undef}, {idlist => 1});
609 for my $lid_id (@$lid_ids) {
610 receive_lineitem_detail($mgr, $lid_id, 1) or return 0;
614 $li->state('received');
616 $li = update_lineitem($mgr, $li) or return 0;
617 $mgr->post_process( sub { create_lineitem_status_events($mgr, $li_id, 'aur.received'); });
621 $skip_complete_check or (
622 $po = check_purchase_order_received($mgr, $li->purchase_order)
625 my $result = {"li" => {$li->id => {"state" => $li->state}}};
626 $result->{"po"} = describe_affected_po($mgr->editor, $po) if ref $po;
630 sub rollback_receive_lineitem {
631 my($mgr, $li_id) = @_;
632 my $li = $mgr->editor->retrieve_acq_lineitem($li_id) or return 0;
634 my $lid_ids = $mgr->editor->search_acq_lineitem_detail(
635 {lineitem => $li_id, recv_time => {'!=' => undef}}, {idlist => 1});
637 for my $lid_id (@$lid_ids) {
638 rollback_receive_lineitem_detail($mgr, $lid_id, 1) or return 0;
642 $li->state('on-order');
643 return update_lineitem($mgr, $li);
647 sub create_lineitem_status_events {
648 my($mgr, $li_id, $hook) = @_;
650 my $ses = OpenSRF::AppSession->create('open-ils.trigger');
652 my $user_reqs = $mgr->editor->search_acq_user_request([
653 {lineitem => $li_id},
654 {flesh => 1, flesh_fields => {aur => ['usr']}}
657 for my $user_req (@$user_reqs) {
658 my $req = $ses->request('open-ils.trigger.event.autocreate', $hook, $user_req, $user_req->usr->home_ou);
666 # ----------------------------------------------------------------------------
668 # ----------------------------------------------------------------------------
669 sub create_lineitem_detail {
670 my($mgr, %args) = @_;
671 my $lid = Fieldmapper::acq::lineitem_detail->new;
672 $lid->$_($args{$_}) for keys %args;
675 return $mgr->editor->create_acq_lineitem_detail($lid);
679 # flesh out any required data with default values where appropriate
680 sub complete_lineitem_detail {
682 unless($lid->barcode) {
683 my $pfx = $U->ou_ancestor_setting_value($lid->owning_lib, 'acq.tmp_barcode_prefix') || 'ACQ';
684 $lid->barcode($pfx.$lid->id);
687 unless($lid->cn_label) {
688 my $pfx = $U->ou_ancestor_setting_value($lid->owning_lib, 'acq.tmp_callnumber_prefix') || 'ACQ';
689 $lid->cn_label($pfx.$lid->id);
692 if(!$lid->location and my $loc = $U->ou_ancestor_setting_value($lid->owning_lib, 'acq.default_copy_location')) {
693 $lid->location($loc);
696 $lid->circ_modifier(get_default_circ_modifier($mgr, $lid->owning_lib))
697 unless defined $lid->circ_modifier;
699 $mgr->editor->update_acq_lineitem_detail($lid) or return 0;
703 sub get_default_circ_modifier {
705 my $code = $mgr->cache($org, 'def_circ_mod');
706 $code = $U->ou_ancestor_setting_value($org, 'acq.default_circ_modifier') unless defined $code;
707 return $mgr->cache($org, 'def_circ_mod', $code) if defined $code;
711 sub delete_lineitem_detail {
713 $lid = $mgr->editor->retrieve_acq_lineitem_detail($lid) unless ref $lid;
714 return $mgr->editor->delete_acq_lineitem_detail($lid);
718 sub receive_lineitem_detail {
719 my($mgr, $lid_id, $skip_complete_check) = @_;
720 my $e = $mgr->editor;
722 my $lid = $e->retrieve_acq_lineitem_detail([
726 acqlid => ['fund_debit']
731 return 1 if $lid->recv_time;
733 # if the LID is marked as canceled, remove the cancel reason,
734 # and reinstate fund debits where deleted by cancelation.
735 if ($lid->cancel_reason) {
736 my $cr = $e->retrieve_acq_cancel_reason($lid->cancel_reason);
738 if (!$U->is_true($cr->keep_debits)) {
739 # debits were removed during cancelation.
740 create_lineitem_detail_debit(
741 $mgr, $lid->lineitem, $lid) or return 0;
743 $lid->clear_cancel_reason;
746 $lid->receiver($e->requestor->id);
747 $lid->recv_time('now');
748 $e->update_acq_lineitem_detail($lid) or return 0;
750 if ($lid->eg_copy_id) {
751 my $copy = $e->retrieve_asset_copy($lid->eg_copy_id) or return 0;
752 # only update status if it hasn't already been updated
753 $copy->status(OILS_COPY_STATUS_IN_PROCESS) if $copy->status == OILS_COPY_STATUS_ON_ORDER;
754 $copy->edit_date('now');
755 $copy->editor($e->requestor->id);
756 $copy->creator($e->requestor->id) if $U->ou_ancestor_setting_value(
757 $e->requestor->ws_ou, 'acq.copy_creator_uses_receiver', $e);
758 $e->update_asset_copy($copy) or return 0;
763 return 1 if $skip_complete_check;
765 my $li = check_lineitem_received($mgr, $lid->lineitem) or return 0;
766 return 1 if $li == 1; # li not received
768 return check_purchase_order_received($mgr, $li->purchase_order) or return 0;
772 sub rollback_receive_lineitem_detail {
773 my($mgr, $lid_id) = @_;
774 my $e = $mgr->editor;
776 my $lid = $e->retrieve_acq_lineitem_detail([
780 acqlid => ['fund_debit']
785 return 1 unless $lid->recv_time;
787 $lid->clear_receiver;
788 $lid->clear_recv_time;
789 $e->update_acq_lineitem_detail($lid) or return 0;
791 if ($lid->eg_copy_id) {
792 my $copy = $e->retrieve_asset_copy($lid->eg_copy_id) or return 0;
793 $copy->status(OILS_COPY_STATUS_ON_ORDER);
794 $copy->edit_date('now');
795 $copy->editor($e->requestor->id);
796 $e->update_asset_copy($copy) or return 0;
803 # ----------------------------------------------------------------------------
805 # ----------------------------------------------------------------------------
806 sub set_lineitem_attr {
807 my($mgr, %args) = @_;
808 my $attr_type = $args{attr_type};
810 # first, see if it's already set. May just need to overwrite it
811 my $attr = $mgr->editor->search_acq_lineitem_attr({
812 lineitem => $args{lineitem},
813 attr_type => $args{attr_type},
814 attr_name => $args{attr_name}
818 $attr->attr_value($args{attr_value});
819 return $attr if $mgr->editor->update_acq_lineitem_attr($attr);
824 $attr = Fieldmapper::acq::lineitem_attr->new;
825 $attr->$_($args{$_}) for keys %args;
827 unless($attr->definition) {
828 my $find = "search_acq_$attr_type";
829 my $attr_def_id = $mgr->editor->$find({code => $attr->attr_name}, {idlist=>1})->[0] or return 0;
830 $attr->definition($attr_def_id);
832 return $mgr->editor->create_acq_lineitem_attr($attr);
836 # ----------------------------------------------------------------------------
838 # ----------------------------------------------------------------------------
839 sub create_lineitem_debits {
840 my ($mgr, $li, $options) = @_;
842 my $dry_run = $options->{dry_run};
844 unless($li->estimated_unit_price) {
845 $mgr->editor->event(OpenILS::Event->new('ACQ_LINEITEM_NO_PRICE', payload => $li->id));
846 $mgr->editor->rollback;
850 unless($li->provider) {
851 $mgr->editor->event(OpenILS::Event->new('ACQ_LINEITEM_NO_PROVIDER', payload => $li->id));
852 $mgr->editor->rollback;
856 my $lid_ids = $mgr->editor->search_acq_lineitem_detail(
857 {lineitem => $li->id},
861 if (@$lid_ids == 0 and !$options->{zero_copy_activate}) {
862 $mgr->editor->event(OpenILS::Event->new('ACQ_LINEITEM_NO_COPIES', payload => $li->id));
863 $mgr->editor->rollback;
867 for my $lid_id (@$lid_ids) {
869 my $lid = $mgr->editor->retrieve_acq_lineitem_detail([
872 flesh_fields => {acqlid => ['fund']}
876 create_lineitem_detail_debit($mgr, $li, $lid, $dry_run) or return 0;
885 sub create_lineitem_detail_debit {
886 my ($mgr, $li, $lid, $dry_run, $no_translate) = @_;
888 # don't create the debit if one already exists
889 return $mgr->editor->retrieve_acq_fund_debit($lid->fund_debit) if $lid->fund_debit;
891 my $li_id = ref($li) ? $li->id : $li;
893 unless(ref $li and ref $li->provider) {
894 $li = $mgr->editor->retrieve_acq_lineitem([
897 flesh_fields => {jub => ['provider']},
903 $lid->fund($mgr->editor->retrieve_acq_fund($lid->fund)) unless(ref $lid->fund);
905 $lid = $mgr->editor->retrieve_acq_lineitem_detail([
908 flesh_fields => {acqlid => ['fund']}
913 unless ($lid->fund) {
915 new OpenILS::Event("ACQ_FUND_NOT_FOUND") # close enough
920 my $amount = $li->estimated_unit_price;
921 if($li->provider->currency_type ne $lid->fund->currency_type and !$no_translate) {
923 # At Fund debit creation time, translate into the currency of the fund
924 # TODO: org setting to disable automatic currency conversion at debit create time?
926 $amount = $mgr->editor->json_query({
928 'acq.exchange_ratio',
929 $li->provider->currency_type, # source currency
930 $lid->fund->currency_type, # destination currency
931 $li->estimated_unit_price # source amount
933 })->[0]->{'acq.exchange_ratio'};
936 my $debit = create_fund_debit(
939 fund => $lid->fund->id,
940 origin_amount => $li->estimated_unit_price,
941 origin_currency_type => $li->provider->currency_type,
945 $lid->fund_debit($debit->id);
946 $lid->fund($lid->fund->id);
947 $mgr->editor->update_acq_lineitem_detail($lid) or return 0;
952 __PACKAGE__->register_method(
953 "method" => "fund_exceeds_balance_percent_api",
954 "api_name" => "open-ils.acq.fund.check_balance_percentages",
956 "desc" => q/Determine whether a given fund exceeds its defined
957 "balance stop and warning percentages"/,
959 {"desc" => "Authentication token", "type" => "string"},
960 {"desc" => "Fund ID", "type" => "number"},
961 {"desc" => "Theoretical debit amount (optional)",
964 "return" => {"desc" => q/An array of two values, for stop and warning,
965 in that order: 1 if fund exceeds that balance percentage, else 0/}
969 sub fund_exceeds_balance_percent_api {
970 my ($self, $conn, $auth, $fund_id, $debit_amount) = @_;
974 my $e = new_editor("authtoken" => $auth);
975 return $e->die_event unless $e->checkauth;
977 my $fund = $e->retrieve_acq_fund($fund_id) or return $e->die_event;
978 return $e->die_event unless $e->allowed("VIEW_FUND", $fund->org);
981 fund_exceeds_balance_percent($fund, $debit_amount, $e, "stop"),
982 fund_exceeds_balance_percent($fund, $debit_amount, $e, "warning")
989 sub fund_exceeds_balance_percent {
990 my ($fund, $debit_amount, $e, $which) = @_;
992 my ($method_name, $event_name) = @{{
994 "balance_warning_percent", "ACQ_FUND_EXCEEDS_WARN_PERCENT"
997 "balance_stop_percent", "ACQ_FUND_EXCEEDS_STOP_PERCENT"
1001 if ($fund->$method_name) {
1003 $e->search_acq_fund_combined_balance({"fund" => $fund->id})->[0];
1005 $e->search_acq_fund_allocation_total({"fund" => $fund->id})->[0];
1007 $balance = ($balance) ? $balance->amount : 0;
1008 $allocations = ($allocations) ? $allocations->amount : 0;
1011 $allocations == 0 || # if no allocations were ever made, assume we have hit the stop percent
1012 ((($allocations - $balance + $debit_amount) / $allocations) * 100) > $fund->$method_name
1014 $logger->info("fund would hit a limit: " . $fund->id . ", $balance, $debit_amount, $allocations, $method_name");
1019 "fund" => $fund, "debit_amount" => $debit_amount
1029 # ----------------------------------------------------------------------------
1031 # ----------------------------------------------------------------------------
1032 sub create_fund_debit {
1033 my($mgr, $dry_run, %args) = @_;
1035 # Verify the fund is not being spent beyond the hard stop amount
1036 my $fund = $mgr->editor->retrieve_acq_fund($args{fund}) or return 0;
1039 fund_exceeds_balance_percent(
1040 $fund, $args{"amount"}, $mgr->editor, "stop"
1043 $dry_run and fund_exceeds_balance_percent(
1044 $fund, $args{"amount"}, $mgr->editor, "warning"
1047 my $debit = Fieldmapper::acq::fund_debit->new;
1048 $debit->debit_type('purchase');
1049 $debit->encumbrance('t');
1050 $debit->$_($args{$_}) for keys %args;
1052 $mgr->add_debit($debit->amount);
1053 return $mgr->editor->create_acq_fund_debit($debit);
1057 # ----------------------------------------------------------------------------
1059 # ----------------------------------------------------------------------------
1060 sub create_picklist {
1061 my($mgr, %args) = @_;
1062 my $picklist = Fieldmapper::acq::picklist->new;
1063 $picklist->creator($mgr->editor->requestor->id);
1064 $picklist->owner($picklist->creator);
1065 $picklist->editor($picklist->creator);
1066 $picklist->create_time('now');
1067 $picklist->edit_time('now');
1068 $picklist->org_unit($mgr->editor->requestor->ws_ou);
1069 $picklist->owner($mgr->editor->requestor->id);
1070 $picklist->$_($args{$_}) for keys %args;
1071 $picklist->clear_id;
1072 $mgr->picklist($picklist);
1073 return $mgr->editor->create_acq_picklist($picklist);
1076 sub update_picklist {
1077 my($mgr, $picklist) = @_;
1078 $picklist = $mgr->editor->retrieve_acq_picklist($picklist) unless ref $picklist;
1079 $picklist->edit_time('now');
1080 $picklist->editor($mgr->editor->requestor->id);
1081 if ($mgr->editor->update_acq_picklist($picklist)) {
1082 $picklist = $mgr->editor->retrieve_acq_picklist($mgr->editor->data);
1083 $mgr->picklist($picklist);
1090 sub delete_picklist {
1091 my($mgr, $picklist) = @_;
1092 $picklist = $mgr->editor->retrieve_acq_picklist($picklist) unless ref $picklist;
1094 # delete all 'new' lineitems
1095 my $li_ids = $mgr->editor->search_acq_lineitem(
1097 picklist => $picklist->id,
1098 "-or" => {state => "new", purchase_order => undef}
1102 for my $li_id (@$li_ids) {
1103 my $li = $mgr->editor->retrieve_acq_lineitem($li_id);
1104 return 0 unless delete_lineitem($mgr, $li);
1108 # detach all non-'new' lineitems
1109 $li_ids = $mgr->editor->search_acq_lineitem({picklist => $picklist->id, state => {'!=' => 'new'}}, {idlist => 1});
1110 for my $li_id (@$li_ids) {
1111 my $li = $mgr->editor->retrieve_acq_lineitem($li_id);
1112 $li->clear_picklist;
1113 return 0 unless update_lineitem($mgr, $li);
1117 # remove any picklist-specific object perms
1118 my $ops = $mgr->editor->search_permission_usr_object_perm_map({object_type => 'acqpl', object_id => ''.$picklist->id});
1119 for my $op (@$ops) {
1120 return 0 unless $mgr->editor->delete_usr_object_perm_map($op);
1123 return $mgr->editor->delete_acq_picklist($picklist);
1126 # ----------------------------------------------------------------------------
1128 # ----------------------------------------------------------------------------
1129 sub update_purchase_order {
1131 $po = $mgr->editor->retrieve_acq_purchase_order($po) unless ref $po;
1132 $po->editor($mgr->editor->requestor->id);
1133 $po->edit_time('now');
1134 $mgr->purchase_order($po);
1135 return $mgr->editor->retrieve_acq_purchase_order($mgr->editor->data)
1136 if $mgr->editor->update_acq_purchase_order($po);
1140 sub create_purchase_order {
1141 my($mgr, %args) = @_;
1143 # verify the chosen provider is still active
1144 my $provider = $mgr->editor->retrieve_acq_provider($args{provider}) or return 0;
1145 unless($U->is_true($provider->active)) {
1146 $logger->error("provider is not active. cannot create PO");
1147 $mgr->editor->event(OpenILS::Event->new('ACQ_PROVIDER_INACTIVE'));
1151 my $po = Fieldmapper::acq::purchase_order->new;
1152 $po->creator($mgr->editor->requestor->id);
1153 $po->editor($mgr->editor->requestor->id);
1154 $po->owner($mgr->editor->requestor->id);
1155 $po->edit_time('now');
1156 $po->create_time('now');
1157 $po->state('pending');
1158 $po->ordering_agency($mgr->editor->requestor->ws_ou);
1159 $po->$_($args{$_}) for keys %args;
1161 $mgr->purchase_order($po);
1162 return $mgr->editor->create_acq_purchase_order($po);
1165 # ----------------------------------------------------------------------------
1166 # if all of the lineitems for this PO are received,
1167 # mark the PO as received
1168 # ----------------------------------------------------------------------------
1169 sub check_purchase_order_received {
1170 my($mgr, $po_id) = @_;
1172 my $non_recv_li = $mgr->editor->search_acq_lineitem(
1173 { purchase_order => $po_id,
1174 state => {'!=' => 'received'}
1177 my $po = $mgr->editor->retrieve_acq_purchase_order($po_id);
1178 return $po if @$non_recv_li;
1180 $po->state('received');
1181 return update_purchase_order($mgr, $po);
1185 # ----------------------------------------------------------------------------
1186 # Bib, Callnumber, and Copy data
1187 # ----------------------------------------------------------------------------
1189 sub create_lineitem_assets {
1190 my($mgr, $li_id) = @_;
1193 my $li = $mgr->editor->retrieve_acq_lineitem([
1196 flesh_fields => {jub => ['purchase_order', 'attributes']}
1200 # note: at this point, the bib record this LI links to should already be created
1202 # -----------------------------------------------------------------
1203 # The lineitem is going live, promote user request holds to real holds
1204 # -----------------------------------------------------------------
1205 promote_lineitem_holds($mgr, $li) or return 0;
1207 my $li_details = $mgr->editor->search_acq_lineitem_detail({lineitem => $li_id}, {idlist=>1});
1209 # -----------------------------------------------------------------
1210 # for each lineitem_detail, create the volume if necessary, create
1211 # a copy, and link them all together.
1212 # -----------------------------------------------------------------
1214 for my $lid_id (@{$li_details}) {
1216 my $lid = $mgr->editor->retrieve_acq_lineitem_detail($lid_id) or return 0;
1217 next if $lid->eg_copy_id;
1219 # use the same callnumber label for all items within this lineitem
1220 $lid->cn_label($first_cn) if $first_cn and not $lid->cn_label;
1222 # apply defaults if necessary
1223 return 0 unless complete_lineitem_detail($mgr, $lid);
1225 $first_cn = $lid->cn_label unless $first_cn;
1227 my $org = $lid->owning_lib;
1228 my $label = $lid->cn_label;
1229 my $bibid = $li->eg_bib_id;
1231 my $volume = $mgr->cache($org, "cn.$bibid.$label");
1233 $volume = create_volume($mgr, $li, $lid) or return 0;
1234 $mgr->cache($org, "cn.$bibid.$label", $volume);
1236 create_copy($mgr, $volume, $lid, $li) or return 0;
1239 return { li => $li };
1243 my($mgr, $li, $lid) = @_;
1245 my ($volume, $evt) =
1246 OpenILS::Application::Cat::AssetCommon->find_or_create_volume(
1254 $mgr->editor->event($evt);
1262 my($mgr, $volume, $lid, $li) = @_;
1263 my $copy = Fieldmapper::asset::copy->new;
1265 $copy->loan_duration(2);
1266 $copy->fine_level(2);
1267 $copy->status(($lid->recv_time) ? OILS_COPY_STATUS_IN_PROCESS : OILS_COPY_STATUS_ON_ORDER);
1268 $copy->barcode($lid->barcode);
1269 $copy->location($lid->location);
1270 $copy->call_number($volume->id);
1271 $copy->circ_lib($volume->owning_lib);
1272 $copy->circ_modifier($lid->circ_modifier);
1274 # AKA list price. We might need a $li->list_price field since
1275 # estimated price is not necessarily the same as list price
1276 $copy->price($li->estimated_unit_price);
1278 my $evt = OpenILS::Application::Cat::AssetCommon->create_copy($mgr->editor, $volume, $copy);
1280 $mgr->editor->event($evt);
1285 $lid->eg_copy_id($copy->id);
1286 $mgr->editor->update_acq_lineitem_detail($lid) or return 0;
1294 # ----------------------------------------------------------------------------
1295 # Workflow: Build a selection list from a Z39.50 search
1296 # ----------------------------------------------------------------------------
1298 __PACKAGE__->register_method(
1299 method => 'zsearch',
1300 api_name => 'open-ils.acq.picklist.search.z3950',
1303 desc => 'Performs a z3950 federated search and creates a picklist and associated lineitems',
1305 {desc => 'Authentication token', type => 'string'},
1306 {desc => 'Search definition', type => 'object'},
1307 {desc => 'Picklist name, optional', type => 'string'},
1313 my($self, $conn, $auth, $search, $name, $options) = @_;
1314 my $e = new_editor(authtoken=>$auth);
1315 return $e->event unless $e->checkauth;
1316 return $e->event unless $e->allowed('CREATE_PICKLIST');
1318 $search->{limit} ||= 10;
1321 my $ses = OpenSRF::AppSession->create('open-ils.search');
1322 my $req = $ses->request('open-ils.search.z3950.search_class', $auth, $search);
1327 while(my $resp = $req->recv(timeout=>60)) {
1330 my $e = new_editor(requestor=>$e->requestor, xact=>1);
1331 $mgr = OpenILS::Application::Acq::BatchManager->new(editor => $e, conn => $conn);
1332 $picklist = zsearch_build_pl($mgr, $name);
1336 my $result = $resp->content;
1337 my $count = $result->{count} || 0;
1338 $mgr->total( (($count < $search->{limit}) ? $count : $search->{limit})+1 );
1340 for my $rec (@{$result->{records}}) {
1342 my $li = create_lineitem($mgr,
1343 picklist => $picklist->id,
1344 source_label => $result->{service},
1345 marc => $rec->{marcxml},
1346 eg_bib_id => $rec->{bibid}
1349 if($$options{respond_li}) {
1350 $li->attributes($mgr->editor->search_acq_lineitem_attr({lineitem => $li->id}))
1351 if $$options{flesh_attrs};
1352 $li->clear_marc if $$options{clear_marc};
1353 $mgr->respond(lineitem => $li);
1360 $mgr->editor->commit;
1361 return $mgr->respond_complete;
1364 sub zsearch_build_pl {
1365 my($mgr, $name) = @_;
1368 my $picklist = $mgr->editor->search_acq_picklist({
1369 owner => $mgr->editor->requestor->id,
1373 if($name eq '' and $picklist) {
1374 return 0 unless delete_picklist($mgr, $picklist);
1378 return update_picklist($mgr, $picklist) if $picklist;
1379 return create_picklist($mgr, name => $name);
1383 # ----------------------------------------------------------------------------
1384 # Workflow: Build a selection list / PO by importing a batch of MARC records
1385 # ----------------------------------------------------------------------------
1387 __PACKAGE__->register_method(
1388 method => 'upload_records',
1389 api_name => 'open-ils.acq.process_upload_records',
1391 max_chunk_count => 1
1394 sub upload_records {
1395 my($self, $conn, $auth, $key, $args) = @_;
1398 my $e = new_editor(authtoken => $auth, xact => 1);
1399 return $e->die_event unless $e->checkauth;
1400 my $mgr = OpenILS::Application::Acq::BatchManager->new(editor => $e, conn => $conn);
1402 my $cache = OpenSRF::Utils::Cache->new;
1404 my $data = $cache->get_cache("vandelay_import_spool_$key");
1405 my $filename = $data->{path};
1406 my $provider = $args->{provider};
1407 my $picklist = $args->{picklist};
1408 my $create_po = $args->{create_po};
1409 my $activate_po = $args->{activate_po};
1410 my $vandelay = $args->{vandelay};
1411 my $ordering_agency = $args->{ordering_agency} || $e->requestor->ws_ou;
1412 my $fiscal_year = $args->{fiscal_year};
1414 # if the user provides no fiscal year, find the
1415 # current fiscal year for the ordering agency.
1416 $fiscal_year ||= $U->simplereq(
1418 'open-ils.acq.org_unit.current_fiscal_year',
1426 unless(-r $filename) {
1427 $logger->error("unable to read MARC file $filename");
1429 return OpenILS::Event->new('FILE_UPLOAD_ERROR', payload => {filename => $filename});
1432 $provider = $e->retrieve_acq_provider($provider) or return $e->die_event;
1435 $picklist = $e->retrieve_acq_picklist($picklist) or return $e->die_event;
1436 if($picklist->owner != $e->requestor->id) {
1437 return $e->die_event unless
1438 $e->allowed('CREATE_PICKLIST', $picklist->org_unit, $picklist);
1440 $mgr->picklist($picklist);
1444 return $e->die_event unless
1445 $e->allowed('CREATE_PURCHASE_ORDER', $ordering_agency);
1447 $po = create_purchase_order($mgr,
1448 ordering_agency => $ordering_agency,
1449 provider => $provider->id,
1450 state => 'pending' # will be updated later if activated
1451 ) or return $mgr->editor->die_event;
1454 $logger->info("acq processing MARC file=$filename");
1456 my $batch = new MARC::Batch ('USMARC', $filename);
1464 my ($err, $xml, $r);
1469 } catch Error with {
1471 $logger->warn("Proccessing of record $count in set $key failed with error $err. Skipping this record");
1478 $xml = clean_marc($r);
1479 } catch Error with {
1481 $logger->warn("Proccessing XML of record $count in set $key failed with error $err. Skipping this record");
1484 next if $err or not $xml;
1487 source_label => $provider->code,
1488 provider => $provider->id,
1492 $args{picklist} = $picklist->id if $picklist;
1494 $args{purchase_order} = $po->id;
1495 $args{state} = 'pending-order';
1498 my $li = create_lineitem($mgr, %args) or return $mgr->editor->die_event;
1500 $li->provider($provider); # flesh it, we'll need it later
1502 import_lineitem_details($mgr, $ordering_agency, $li, $fiscal_year)
1503 or return $mgr->editor->die_event;
1506 push(@li_list, $li->id);
1511 $evt = extract_po_name($mgr, $po, \@li_list);
1512 return $evt if $evt;
1517 $cache->delete_cache('vandelay_import_spool_' . $key);
1519 if ($po and $activate_po) {
1520 my $die_event = activate_purchase_order_impl($mgr, $po->id, $vandelay);
1521 return $die_event if $die_event;
1523 } elsif ($vandelay) {
1524 $vandelay->{new_rec_perm} = 'IMPORT_ACQ_LINEITEM_BIB_RECORD_UPLOAD';
1525 create_lineitem_list_assets($mgr, \@li_list, $vandelay,
1526 !$vandelay->{create_assets}) or return $e->die_event;
1529 return $mgr->respond_complete;
1532 # see if the PO name is encoded in the newly imported records
1533 sub extract_po_name {
1534 my ($mgr, $po, $li_ids) = @_;
1535 my $e = $mgr->editor;
1537 # find the first instance of the name
1538 my $attr = $e->search_acq_lineitem_attr([
1539 { lineitem => $li_ids,
1540 attr_type => 'lineitem_provider_attr_definition',
1541 attr_name => 'purchase_order'
1543 order_by => {aqlia => 'id'},
1546 ])->[0] or return undef;
1548 my $name = $attr->attr_value;
1550 # see if another PO already has the name, provider, and org
1551 my $existing = $e->search_acq_purchase_order(
1553 ordering_agency => $po->ordering_agency,
1554 provider => $po->provider
1559 # if a PO exists with the same name (and provider/org)
1560 # tack the po ID into the name to differentiate
1561 $name = sprintf("$name (%s)", $po->id) if $existing;
1563 $logger->info("Extracted PO name: $name");
1566 update_purchase_order($mgr, $po) or return $e->die_event;
1570 sub import_lineitem_details {
1571 my($mgr, $ordering_agency, $li, $fiscal_year) = @_;
1573 my $holdings = $mgr->editor->json_query({from => ['acq.extract_provider_holding_data', $li->id]});
1574 return 1 unless @$holdings;
1575 my $org_path = $U->get_org_ancestors($ordering_agency);
1576 $org_path = [ reverse (@$org_path) ];
1582 # create a lineitem detail for each copy in the data
1584 my $compiled = extract_lineitem_detail_data($mgr, $org_path, $holdings, $idx, $fiscal_year);
1585 last unless defined $compiled;
1586 return 0 unless $compiled;
1588 # this takes the price of the last copy and uses it as the lineitem price
1589 # need to determine if a given record would include different prices for the same item
1590 $price = $$compiled{estimated_price};
1592 last unless $$compiled{quantity};
1594 for(1..$$compiled{quantity}) {
1595 my $lid = create_lineitem_detail(
1597 lineitem => $li->id,
1598 owning_lib => $$compiled{owning_lib},
1599 cn_label => $$compiled{call_number},
1600 fund => $$compiled{fund},
1601 circ_modifier => $$compiled{circ_modifier},
1602 note => $$compiled{note},
1603 location => $$compiled{copy_location},
1604 collection_code => $$compiled{collection_code},
1605 barcode => $$compiled{barcode}
1613 $li->estimated_unit_price($price);
1614 update_lineitem($mgr, $li) or return 0;
1618 # return hash on success, 0 on error, undef on no more holdings
1619 sub extract_lineitem_detail_data {
1620 my($mgr, $org_path, $holdings, $index, $fiscal_year) = @_;
1622 my @data_list = grep { $_->{holding} eq $index } @$holdings;
1623 return undef unless @data_list;
1625 my %compiled = map { $_->{attr} => $_->{data} } @data_list;
1626 my $base_org = $$org_path[0];
1630 $logger->error("Item import extraction error: $msg");
1631 $logger->error('Holdings Data: ' . OpenSRF::Utils::JSON->perl2JSON(\%compiled));
1632 $mgr->editor->rollback;
1633 $mgr->editor->event(OpenILS::Event->new('ACQ_IMPORT_ERROR', payload => $msg));
1637 # ---------------------------------------------------------------------
1639 if(my $code = $compiled{fund_code}) {
1641 my $fund = $mgr->cache($base_org, "fund.$code");
1643 # search up the org tree for the most appropriate fund
1644 for my $org (@$org_path) {
1645 $fund = $mgr->editor->search_acq_fund(
1646 {org => $org, code => $code, year => $fiscal_year}, {idlist => 1})->[0];
1650 return $killme->("no fund with code $code at orgs [@$org_path]") unless $fund;
1651 $compiled{fund} = $fund;
1652 $mgr->cache($base_org, "fund.$code", $fund);
1656 # ---------------------------------------------------------------------
1658 if(my $sn = $compiled{owning_lib}) {
1659 my $org_id = $mgr->cache($base_org, "orgsn.$sn") ||
1660 $mgr->editor->search_actor_org_unit({shortname => $sn}, {idlist => 1})->[0];
1661 return $killme->("invalid owning_lib defined: $sn") unless $org_id;
1662 $compiled{owning_lib} = $org_id;
1663 $mgr->cache($$org_path[0], "orgsn.$sn", $org_id);
1667 # ---------------------------------------------------------------------
1669 my $code = $compiled{circ_modifier};
1673 # verify this is a valid circ modifier
1674 return $killme->("invlalid circ_modifier $code") unless
1675 defined $mgr->cache($base_org, "mod.$code") or
1676 $mgr->editor->retrieve_config_circ_modifier($code);
1678 # if valid, cache for future tests
1679 $mgr->cache($base_org, "mod.$code", $code);
1682 $compiled{circ_modifier} = get_default_circ_modifier($mgr, $base_org);
1686 # ---------------------------------------------------------------------
1688 if( my $name = $compiled{copy_location}) {
1690 my $cp_base_org = $base_org;
1692 if ($compiled{owning_lib}) {
1693 # start looking for copy locations at the copy
1694 # owning lib instaed of the upload context org
1695 $cp_base_org = $compiled{owning_lib};
1698 my $loc = $mgr->cache($cp_base_org, "copy_loc.$name");
1700 my $org = $cp_base_org;
1702 $loc = $mgr->editor->search_asset_copy_location(
1703 {owning_lib => $org, name => $name}, {idlist => 1})->[0];
1705 $org = $mgr->editor->retrieve_actor_org_unit($org)->parent_ou;
1708 return $killme->("Invalid copy location $name") unless $loc;
1709 $compiled{copy_location} = $loc;
1710 $mgr->cache($cp_base_org, "copy_loc.$name", $loc);
1718 # ----------------------------------------------------------------------------
1719 # Workflow: Given an existing purchase order, import/create the bibs,
1720 # callnumber and copy objects
1721 # ----------------------------------------------------------------------------
1723 __PACKAGE__->register_method(
1724 method => 'create_po_assets',
1725 api_name => 'open-ils.acq.purchase_order.assets.create',
1727 desc => q/Creates assets for each lineitem in the purchase order/,
1729 {desc => 'Authentication token', type => 'string'},
1730 {desc => 'The purchase order id', type => 'number'},
1732 return => {desc => 'Streams a total versus completed counts object, event on error'}
1734 max_chunk_count => 1
1737 sub create_po_assets {
1738 my($self, $conn, $auth, $po_id, $args) = @_;
1741 my $e = new_editor(authtoken=>$auth, xact=>1);
1742 return $e->die_event unless $e->checkauth;
1743 my $mgr = OpenILS::Application::Acq::BatchManager->new(editor => $e, conn => $conn);
1745 my $po = $e->retrieve_acq_purchase_order($po_id) or return $e->die_event;
1747 my $li_ids = $e->search_acq_lineitem({purchase_order => $po_id}, {idlist => 1});
1749 # it's ugly, but it's fast. Get the total count of lineitem detail objects to process
1750 my $lid_total = $e->json_query({
1751 select => { acqlid => [{aggregate => 1, transform => 'count', column => 'id'}] },
1757 join => {acqpo => {fkey => 'purchase_order', field => 'id'}}
1761 where => {'+acqpo' => {id => $po_id}}
1764 $mgr->total(scalar(@$li_ids) + $lid_total);
1766 create_lineitem_list_assets($mgr, $li_ids, $args->{vandelay})
1767 or return $e->die_event;
1770 update_purchase_order($mgr, $po) or return $e->die_event;
1773 return $mgr->respond_complete;
1778 __PACKAGE__->register_method(
1779 method => 'create_purchase_order_api',
1780 api_name => 'open-ils.acq.purchase_order.create',
1782 desc => 'Creates a new purchase order',
1784 {desc => 'Authentication token', type => 'string'},
1785 {desc => 'purchase_order to create', type => 'object'}
1787 return => {desc => 'The purchase order id, Event on failure'}
1789 max_chunk_count => 1
1792 sub create_purchase_order_api {
1793 my($self, $conn, $auth, $po, $args) = @_;
1796 my $e = new_editor(xact=>1, authtoken=>$auth);
1797 return $e->die_event unless $e->checkauth;
1798 return $e->die_event unless $e->allowed('CREATE_PURCHASE_ORDER', $po->ordering_agency);
1799 my $mgr = OpenILS::Application::Acq::BatchManager->new(editor => $e, conn => $conn);
1802 my %pargs = (ordering_agency => $e->requestor->ws_ou); # default
1803 $pargs{provider} = $po->provider if $po->provider;
1804 $pargs{ordering_agency} = $po->ordering_agency if $po->ordering_agency;
1805 $pargs{prepayment_required} = $po->prepayment_required if $po->prepayment_required;
1806 my $vandelay = $args->{vandelay};
1808 $po = create_purchase_order($mgr, %pargs) or return $e->die_event;
1810 my $li_ids = $$args{lineitems};
1814 for my $li_id (@$li_ids) {
1816 my $li = $e->retrieve_acq_lineitem([
1818 {flesh => 1, flesh_fields => {jub => ['attributes']}}
1819 ]) or return $e->die_event;
1821 return $e->die_event(
1823 "BAD_PARAMS", payload => $li,
1824 note => "acq.lineitem #" . $li->id .
1825 ": purchase_order #" . $li->purchase_order
1827 ) if $li->purchase_order;
1829 $li->provider($po->provider);
1830 $li->purchase_order($po->id);
1831 $li->state('pending-order');
1832 update_lineitem($mgr, $li) or return $e->die_event;
1837 # see if we have a PO name encoded in any of our lineitems
1838 my $evt = extract_po_name($mgr, $po, $li_ids);
1839 return $evt if $evt;
1841 # commit before starting the asset creation
1847 create_lineitem_list_assets(
1848 $mgr, $li_ids, $vandelay, !$$args{create_assets})
1849 or return $e->die_event;
1853 apply_default_copies($mgr, $po) or return $e->die_event;
1857 return $mgr->respond_complete;
1860 # !transaction must be managed by the caller
1861 # creates the default number of copies for each lineitem on the PO.
1862 # when a LI already has copies attached, no default copies are added.
1863 # without li_id, all lineitems are checked/applied
1864 # returns 1 on success, 0 on error
1865 sub apply_default_copies {
1866 my ($mgr, $po, $li_id) = @_;
1868 my $e = $mgr->editor;
1870 my $provider = ref($po->provider) ? $po->provider :
1871 $e->retrieve_acq_provider($po->provider);
1873 my $copy_count = $provider->default_copy_count || return 1;
1875 $logger->info("Applying $copy_count default copies for PO ".$po->id);
1877 my $li_ids = $li_id ? [$li_id] :
1878 $e->search_acq_lineitem({
1879 purchase_order => $po->id,
1880 cancel_reason => undef
1885 for my $li_id (@$li_ids) {
1887 my $lid_ids = $e->search_acq_lineitem_detail(
1888 {lineitem => $li_id}, {idlist => 1});
1890 # do not apply default copies when copies already exist
1893 for (1 .. $copy_count) {
1894 create_lineitem_detail($mgr,
1896 owning_lib => $e->requestor->ws_ou
1906 __PACKAGE__->register_method(
1907 method => 'update_lineitem_fund_batch',
1908 api_name => 'open-ils.acq.lineitem.fund.update.batch',
1911 desc => q/Given a set of lineitem IDS, updates the fund for all attached lineitem details/
1915 sub update_lineitem_fund_batch {
1916 my($self, $conn, $auth, $li_ids, $fund_id) = @_;
1917 my $e = new_editor(xact=>1, authtoken=>$auth);
1918 return $e->die_event unless $e->checkauth;
1919 my $mgr = OpenILS::Application::Acq::BatchManager->new(editor => $e, conn => $conn);
1920 for my $li_id (@$li_ids) {
1921 my ($li, $evt) = fetch_and_check_li($e, $li_id, 'write');
1922 return $evt if $evt;
1923 my $li_details = $e->search_acq_lineitem_detail({lineitem => $li_id});
1924 $_->fund($fund_id) and $_->ischanged(1) for @$li_details;
1925 $evt = lineitem_detail_CUD_batch($mgr, $li_details);
1926 return $evt if $evt;
1931 return $mgr->respond_complete;
1936 __PACKAGE__->register_method(
1937 method => 'lineitem_detail_CUD_batch_api',
1938 api_name => 'open-ils.acq.lineitem_detail.cud.batch',
1941 desc => q/Creates a new purchase order line item detail. / .
1942 q/Additionally creates the associated fund_debit/,
1944 {desc => 'Authentication token', type => 'string'},
1945 {desc => 'List of lineitem_details to create', type => 'array'},
1946 {desc => 'Create Debits. Used for creating post-po-asset-creation debits', type => 'bool'},
1948 return => {desc => 'Streaming response of current position in the array'}
1952 __PACKAGE__->register_method(
1953 method => 'lineitem_detail_CUD_batch_api',
1954 api_name => 'open-ils.acq.lineitem_detail.cud.batch.dry_run',
1958 Dry run version of open-ils.acq.lineitem_detail.cud.batch.
1959 In dry_run mode, updated fund_debit's the exceed the warning
1960 percent return an event.
1966 sub lineitem_detail_CUD_batch_api {
1967 my($self, $conn, $auth, $li_details, $create_debits) = @_;
1968 my $e = new_editor(xact=>1, authtoken=>$auth);
1969 return $e->die_event unless $e->checkauth;
1970 my $mgr = OpenILS::Application::Acq::BatchManager->new(editor => $e, conn => $conn);
1971 my $dry_run = ($self->api_name =~ /dry_run/o);
1972 my $evt = lineitem_detail_CUD_batch($mgr, $li_details, $create_debits, $dry_run);
1973 return $evt if $evt;
1975 return $mgr->respond_complete;
1979 sub lineitem_detail_CUD_batch {
1980 my($mgr, $li_details, $create_debits, $dry_run) = @_;
1982 $mgr->total(scalar(@$li_details));
1983 my $e = $mgr->editor;
1987 my $fund_cache = {};
1990 for my $lid (@$li_details) {
1992 unless($li = $li_cache{$lid->lineitem}) {
1993 ($li, $evt) = fetch_and_check_li($e, $lid->lineitem, 'write');
1994 return $evt if $evt;
1998 $lid = create_lineitem_detail($mgr, %{$lid->to_bare_hash}) or return $e->die_event;
1999 if($create_debits) {
2000 $li->provider($e->retrieve_acq_provider($li->provider)) or return $e->die_event;
2001 $lid->fund($e->retrieve_acq_fund($lid->fund)) or return $e->die_event;
2002 create_lineitem_detail_debit($mgr, $li, $lid, 0, 1) or return $e->die_event;
2005 } elsif($lid->ischanged) {
2006 return $evt if $evt = handle_changed_lid($e, $lid, $dry_run, $fund_cache);
2008 } elsif($lid->isdeleted) {
2009 delete_lineitem_detail($mgr, $lid) or return $e->die_event;
2012 $mgr->respond(li => $li);
2013 $li_cache{$lid->lineitem} = $li;
2019 sub handle_changed_lid {
2020 my($e, $lid, $dry_run, $fund_cache) = @_;
2022 my $orig_lid = $e->retrieve_acq_lineitem_detail($lid->id) or return $e->die_event;
2024 # updating the fund, so update the debit
2025 if($orig_lid->fund_debit and $orig_lid->fund != $lid->fund) {
2027 my $debit = $e->retrieve_acq_fund_debit($orig_lid->fund_debit);
2028 my $new_fund = $$fund_cache{$lid->fund} =
2029 $$fund_cache{$lid->fund} || $e->retrieve_acq_fund($lid->fund);
2031 # check the thresholds
2032 return $e->die_event if
2033 fund_exceeds_balance_percent($new_fund, $debit->amount, $e, "stop");
2034 return $e->die_event if $dry_run and
2035 fund_exceeds_balance_percent($new_fund, $debit->amount, $e, "warning");
2037 $debit->fund($new_fund->id);
2038 $e->update_acq_fund_debit($debit) or return $e->die_event;
2041 $e->update_acq_lineitem_detail($lid) or return $e->die_event;
2046 __PACKAGE__->register_method(
2047 method => 'receive_po_api',
2048 api_name => 'open-ils.acq.purchase_order.receive'
2051 sub receive_po_api {
2052 my($self, $conn, $auth, $po_id) = @_;
2053 my $e = new_editor(xact => 1, authtoken => $auth);
2054 return $e->die_event unless $e->checkauth;
2055 my $mgr = OpenILS::Application::Acq::BatchManager->new(editor => $e, conn => $conn);
2057 my $po = $e->retrieve_acq_purchase_order($po_id) or return $e->die_event;
2058 return $e->die_event unless $e->allowed('RECEIVE_PURCHASE_ORDER', $po->ordering_agency);
2060 my $li_ids = $e->search_acq_lineitem({purchase_order => $po_id}, {idlist => 1});
2062 for my $li_id (@$li_ids) {
2063 receive_lineitem($mgr, $li_id) or return $e->die_event;
2067 $po->state('received');
2068 update_purchase_order($mgr, $po) or return $e->die_event;
2071 return $mgr->respond_complete;
2075 # At the moment there's a lack of parallelism between the receive and unreceive
2076 # API methods for POs and the API methods for LIs and LIDs. The methods for
2077 # POs stream back objects as they act, whereas the methods for LIs and LIDs
2078 # atomically return an object that describes only what changed (in LIs and LIDs
2079 # themselves or in the objects to which to LIs and LIDs belong).
2081 # The methods for LIs and LIDs work the way they do to faciliate the UI's
2082 # maintaining correct information about the state of these things when a user
2083 # wants to receive or unreceive these objects without refreshing their whole
2084 # display. The UI feature for receiving and un-receiving a whole PO just
2085 # refreshes the whole display, so this absence of parallelism in the UI is also
2086 # relected in this module.
2088 # This could be neatened in the future by making POs receive and unreceive in
2089 # the same way the LIs and LIDs do.
2091 __PACKAGE__->register_method(
2092 method => 'receive_lineitem_detail_api',
2093 api_name => 'open-ils.acq.lineitem_detail.receive',
2095 desc => 'Mark a lineitem_detail as received',
2097 {desc => 'Authentication token', type => 'string'},
2098 {desc => 'lineitem detail ID', type => 'number'}
2101 "on success, object describing changes to LID and possibly " .
2102 "to LI and PO; on error, Event"
2107 sub receive_lineitem_detail_api {
2108 my($self, $conn, $auth, $lid_id) = @_;
2110 my $e = new_editor(xact=>1, authtoken=>$auth);
2111 return $e->die_event unless $e->checkauth;
2112 my $mgr = OpenILS::Application::Acq::BatchManager->new(editor => $e, conn => $conn);
2115 "flesh" => 2, "flesh_fields" => {
2116 "acqlid" => ["lineitem"], "jub" => ["purchase_order"]
2120 my $lid = $e->retrieve_acq_lineitem_detail([$lid_id, $fleshing]);
2122 return $e->die_event unless $e->allowed(
2123 'RECEIVE_PURCHASE_ORDER', $lid->lineitem->purchase_order->ordering_agency);
2126 my $recvd = receive_lineitem_detail($mgr, $lid_id) or return $e->die_event;
2128 # .. and re-retrieve
2129 $lid = $e->retrieve_acq_lineitem_detail([$lid_id, $fleshing]);
2131 # Now build result data structure.
2132 my $result = {"lid" => {$lid->id => {"recv_time" => $lid->recv_time}}};
2135 if ($recvd->class_name =~ /::purchase_order/) {
2136 $result->{"po"} = describe_affected_po($e, $recvd);
2138 $lid->lineitem->id => {"state" => $lid->lineitem->state}
2140 } elsif ($recvd->class_name =~ /::lineitem/) {
2141 $result->{"li"} = {$recvd->id => {"state" => $recvd->state}};
2145 describe_affected_po($e, $lid->lineitem->purchase_order);
2151 __PACKAGE__->register_method(
2152 method => 'receive_lineitem_api',
2153 api_name => 'open-ils.acq.lineitem.receive',
2155 desc => 'Mark a lineitem as received',
2157 {desc => 'Authentication token', type => 'string'},
2158 {desc => 'lineitem ID', type => 'number'}
2161 "on success, object describing changes to LI and possibly PO; " .
2167 sub receive_lineitem_api {
2168 my($self, $conn, $auth, $li_id) = @_;
2170 my $e = new_editor(xact=>1, authtoken=>$auth);
2171 return $e->die_event unless $e->checkauth;
2172 my $mgr = OpenILS::Application::Acq::BatchManager->new(editor => $e, conn => $conn);
2174 my $li = $e->retrieve_acq_lineitem([
2178 jub => ['purchase_order']
2181 ]) or return $e->die_event;
2183 return $e->die_event unless $e->allowed(
2184 'RECEIVE_PURCHASE_ORDER', $li->purchase_order->ordering_agency);
2186 my $res = receive_lineitem($mgr, $li_id) or return $e->die_event;
2188 $conn->respond_complete($res);
2189 $mgr->run_post_response_hooks
2193 __PACKAGE__->register_method(
2194 method => 'receive_lineitem_batch_api',
2195 api_name => 'open-ils.acq.lineitem.receive.batch',
2198 desc => 'Mark lineitems as received',
2200 {desc => 'Authentication token', type => 'string'},
2201 {desc => 'lineitem ID list', type => 'array'}
2204 q/On success, stream of objects describing changes to LIs and
2205 possibly PO; onerror, Event. Any event, even after lots of other
2206 objects, should mean general failure of whole batch operation./
2211 sub receive_lineitem_batch_api {
2212 my ($self, $conn, $auth, $li_idlist) = @_;
2214 return unless ref $li_idlist eq 'ARRAY' and @$li_idlist;
2216 my $e = new_editor(xact => 1, authtoken => $auth);
2217 return $e->die_event unless $e->checkauth;
2219 my $mgr = new OpenILS::Application::Acq::BatchManager(
2220 editor => $e, conn => $conn
2223 for my $li_id (map { int $_ } @$li_idlist) {
2224 my $li = $e->retrieve_acq_lineitem([
2227 flesh_fields => { jub => ['purchase_order'] }
2229 ]) or return $e->die_event;
2231 return $e->die_event unless $e->allowed(
2232 'RECEIVE_PURCHASE_ORDER', $li->purchase_order->ordering_agency
2235 receive_lineitem($mgr, $li_id) or return $e->die_event;
2239 $e->commit or return $e->die_event;
2240 $mgr->respond_complete;
2241 $mgr->run_post_response_hooks;
2244 __PACKAGE__->register_method(
2245 method => 'rollback_receive_po_api',
2246 api_name => 'open-ils.acq.purchase_order.receive.rollback'
2249 sub rollback_receive_po_api {
2250 my($self, $conn, $auth, $po_id) = @_;
2251 my $e = new_editor(xact => 1, authtoken => $auth);
2252 return $e->die_event unless $e->checkauth;
2253 my $mgr = OpenILS::Application::Acq::BatchManager->new(editor => $e, conn => $conn);
2255 my $po = $e->retrieve_acq_purchase_order($po_id) or return $e->die_event;
2256 return $e->die_event unless $e->allowed('RECEIVE_PURCHASE_ORDER', $po->ordering_agency);
2258 my $li_ids = $e->search_acq_lineitem({purchase_order => $po_id}, {idlist => 1});
2260 for my $li_id (@$li_ids) {
2261 rollback_receive_lineitem($mgr, $li_id) or return $e->die_event;
2265 $po->state('on-order');
2266 update_purchase_order($mgr, $po) or return $e->die_event;
2269 return $mgr->respond_complete;
2273 __PACKAGE__->register_method(
2274 method => 'rollback_receive_lineitem_detail_api',
2275 api_name => 'open-ils.acq.lineitem_detail.receive.rollback',
2277 desc => 'Mark a lineitem_detail as Un-received',
2279 {desc => 'Authentication token', type => 'string'},
2280 {desc => 'lineitem detail ID', type => 'number'}
2283 "on success, object describing changes to LID and possibly " .
2284 "to LI and PO; on error, Event"
2289 sub rollback_receive_lineitem_detail_api {
2290 my($self, $conn, $auth, $lid_id) = @_;
2292 my $e = new_editor(xact=>1, authtoken=>$auth);
2293 return $e->die_event unless $e->checkauth;
2294 my $mgr = OpenILS::Application::Acq::BatchManager->new(editor => $e, conn => $conn);
2296 my $lid = $e->retrieve_acq_lineitem_detail([
2300 acqlid => ['lineitem'],
2301 jub => ['purchase_order']
2305 my $li = $lid->lineitem;
2306 my $po = $li->purchase_order;
2308 return $e->die_event unless $e->allowed('RECEIVE_PURCHASE_ORDER', $po->ordering_agency);
2312 my $recvd = rollback_receive_lineitem_detail($mgr, $lid_id)
2313 or return $e->die_event;
2316 $result->{"lid"} = {$recvd->id => {"recv_time" => $recvd->recv_time}};
2318 $result->{"lid"} = {$lid->id => {"recv_time" => $lid->recv_time}};
2321 if ($li->state eq "received") {
2322 $li->state("on-order");
2323 $li = update_lineitem($mgr, $li) or return $e->die_event;
2324 $result->{"li"} = {$li->id => {"state" => $li->state}};
2327 if ($po->state eq "received") {
2328 $po->state("on-order");
2329 $po = update_purchase_order($mgr, $po) or return $e->die_event;
2331 $result->{"po"} = describe_affected_po($e, $po);
2333 $e->commit and return $result or return $e->die_event;
2336 __PACKAGE__->register_method(
2337 method => 'rollback_receive_lineitem_api',
2338 api_name => 'open-ils.acq.lineitem.receive.rollback',
2340 desc => 'Mark a lineitem as Un-received',
2342 {desc => 'Authentication token', type => 'string'},
2343 {desc => 'lineitem ID', type => 'number'}
2346 "on success, object describing changes to LI and possibly PO; " .
2352 sub rollback_receive_lineitem_api {
2353 my($self, $conn, $auth, $li_id) = @_;
2355 my $e = new_editor(xact=>1, authtoken=>$auth);
2356 return $e->die_event unless $e->checkauth;
2357 my $mgr = OpenILS::Application::Acq::BatchManager->new(editor => $e, conn => $conn);
2359 my $li = $e->retrieve_acq_lineitem([
2361 "flesh" => 1, "flesh_fields" => {"jub" => ["purchase_order"]}
2364 my $po = $li->purchase_order;
2366 return $e->die_event unless $e->allowed('RECEIVE_PURCHASE_ORDER', $po->ordering_agency);
2368 $li = rollback_receive_lineitem($mgr, $li_id) or return $e->die_event;
2370 my $result = {"li" => {$li->id => {"state" => $li->state}}};
2371 if ($po->state eq "received") {
2372 $po->state("on-order");
2373 $po = update_purchase_order($mgr, $po) or return $e->die_event;
2375 $result->{"po"} = describe_affected_po($e, $po);
2377 $e->commit and return $result or return $e->die_event;
2380 __PACKAGE__->register_method(
2381 method => 'rollback_receive_lineitem_batch_api',
2382 api_name => 'open-ils.acq.lineitem.receive.rollback.batch',
2385 desc => 'Mark a list of lineitems as Un-received',
2387 {desc => 'Authentication token', type => 'string'},
2388 {desc => 'lineitem ID list', type => 'array'}
2391 q/on success, a stream of objects describing changes to LI and
2392 possibly PO; on error, Event. Any event means all previously
2393 returned objects indicate changes that didn't really happen./
2398 sub rollback_receive_lineitem_batch_api {
2399 my ($self, $conn, $auth, $li_idlist) = @_;
2401 return unless ref $li_idlist eq 'ARRAY' and @$li_idlist;
2403 my $e = new_editor(xact => 1, authtoken => $auth);
2404 return $e->die_event unless $e->checkauth;
2406 my $mgr = new OpenILS::Application::Acq::BatchManager(
2407 editor => $e, conn => $conn
2410 for my $li_id (map { int $_ } @$li_idlist) {
2411 my $li = $e->retrieve_acq_lineitem([
2414 "flesh_fields" => {"jub" => ["purchase_order"]}
2418 my $po = $li->purchase_order;
2420 return $e->die_event unless
2421 $e->allowed('RECEIVE_PURCHASE_ORDER', $po->ordering_agency);
2423 $li = rollback_receive_lineitem($mgr, $li_id) or return $e->die_event;
2425 my $result = {"li" => {$li->id => {"state" => $li->state}}};
2426 if ($po->state eq "received") { # should happen first time, not after
2427 $po->state("on-order");
2428 $po = update_purchase_order($mgr, $po) or return $e->die_event;
2430 $result->{"po"} = describe_affected_po($e, $po);
2432 $mgr->respond(%$result);
2435 $e->commit or return $e->die_event;
2436 $mgr->respond_complete;
2437 $mgr->run_post_response_hooks;
2441 __PACKAGE__->register_method(
2442 method => 'set_lineitem_price_api',
2443 api_name => 'open-ils.acq.lineitem.price.set',
2445 desc => 'Set lineitem price. If debits already exist, update them as well',
2447 {desc => 'Authentication token', type => 'string'},
2448 {desc => 'lineitem ID', type => 'number'}
2450 return => {desc => 'status blob, Event on error'}
2454 sub set_lineitem_price_api {
2455 my($self, $conn, $auth, $li_id, $price) = @_;
2457 my $e = new_editor(xact=>1, authtoken=>$auth);
2458 return $e->die_event unless $e->checkauth;
2459 my $mgr = OpenILS::Application::Acq::BatchManager->new(editor => $e, conn => $conn);
2461 my ($li, $evt) = fetch_and_check_li($e, $li_id, 'write');
2462 return $evt if $evt;
2464 $li->estimated_unit_price($price);
2465 update_lineitem($mgr, $li) or return $e->die_event;
2467 my $lid_ids = $e->search_acq_lineitem_detail(
2468 {lineitem => $li_id, fund_debit => {'!=' => undef}},
2472 for my $lid_id (@$lid_ids) {
2474 my $lid = $e->retrieve_acq_lineitem_detail([
2476 flesh => 1, flesh_fields => {acqlid => ['fund', 'fund_debit']}}
2479 $lid->fund_debit->amount($price);
2480 $e->update_acq_fund_debit($lid->fund_debit) or return $e->die_event;
2486 return $mgr->respond_complete;
2490 __PACKAGE__->register_method(
2491 method => 'clone_picklist_api',
2492 api_name => 'open-ils.acq.picklist.clone',
2494 desc => 'Clones a picklist, including lineitem and lineitem details',
2496 {desc => 'Authentication token', type => 'string'},
2497 {desc => 'Picklist ID', type => 'number'},
2498 {desc => 'New Picklist Name', type => 'string'}
2500 return => {desc => 'status blob, Event on error'}
2504 sub clone_picklist_api {
2505 my($self, $conn, $auth, $pl_id, $name) = @_;
2507 my $e = new_editor(xact=>1, authtoken=>$auth);
2508 return $e->die_event unless $e->checkauth;
2509 my $mgr = OpenILS::Application::Acq::BatchManager->new(editor => $e, conn => $conn);
2511 my $old_pl = $e->retrieve_acq_picklist($pl_id);
2512 my $new_pl = create_picklist($mgr, %{$old_pl->to_bare_hash}, name => $name) or return $e->die_event;
2514 my $li_ids = $e->search_acq_lineitem({picklist => $pl_id}, {idlist => 1});
2516 # get the current user
2517 my $cloner = $mgr->editor->requestor->id;
2519 for my $li_id (@$li_ids) {
2521 # copy the lineitems' MARC
2522 my $marc = ($e->retrieve_acq_lineitem($li_id))->marc;
2524 # create a skeletal clone of the item
2525 my $li = Fieldmapper::acq::lineitem->new;
2526 $li->creator($cloner);
2527 $li->selector($cloner);
2528 $li->editor($cloner);
2531 my $new_li = create_lineitem($mgr, %{$li->to_bare_hash}, picklist => $new_pl->id) or return $e->die_event;
2537 return $mgr->respond_complete;
2541 __PACKAGE__->register_method(
2542 method => 'merge_picklist_api',
2543 api_name => 'open-ils.acq.picklist.merge',
2545 desc => 'Merges 2 or more picklists into a single list',
2547 {desc => 'Authentication token', type => 'string'},
2548 {desc => 'Lead Picklist ID', type => 'number'},
2549 {desc => 'List of subordinate picklist IDs', type => 'array'}
2551 return => {desc => 'status blob, Event on error'}
2555 sub merge_picklist_api {
2556 my($self, $conn, $auth, $lead_pl, $pl_list) = @_;
2558 my $e = new_editor(xact=>1, authtoken=>$auth);
2559 return $e->die_event unless $e->checkauth;
2560 my $mgr = OpenILS::Application::Acq::BatchManager->new(editor => $e, conn => $conn);
2562 # XXX perms on each picklist modified
2564 $lead_pl = $e->retrieve_acq_picklist($lead_pl) or return $e->die_event;
2565 # point all of the lineitems at the lead picklist
2566 my $li_ids = $e->search_acq_lineitem({picklist => $pl_list}, {idlist => 1});
2568 for my $li_id (@$li_ids) {
2569 my $li = $e->retrieve_acq_lineitem($li_id);
2570 $li->picklist($lead_pl);
2571 update_lineitem($mgr, $li) or return $e->die_event;
2575 # now delete the subordinate lists
2576 for my $pl_id (@$pl_list) {
2577 my $pl = $e->retrieve_acq_picklist($pl_id);
2578 $e->delete_acq_picklist($pl) or return $e->die_event;
2581 update_picklist($mgr, $lead_pl) or return $e->die_event;
2584 return $mgr->respond_complete;
2588 __PACKAGE__->register_method(
2589 method => 'delete_picklist_api',
2590 api_name => 'open-ils.acq.picklist.delete',
2592 desc => q/Deletes a picklist. It also deletes any lineitems in the "new" state. / .
2593 q/Other attached lineitems are detached/,
2595 {desc => 'Authentication token', type => 'string'},
2596 {desc => 'Picklist ID to delete', type => 'number'}
2598 return => {desc => '1 on success, Event on error'}
2602 sub delete_picklist_api {
2603 my($self, $conn, $auth, $picklist_id) = @_;
2604 my $e = new_editor(xact=>1, authtoken=>$auth);
2605 return $e->die_event unless $e->checkauth;
2606 my $mgr = OpenILS::Application::Acq::BatchManager->new(editor => $e, conn => $conn);
2607 my $pl = $e->retrieve_acq_picklist($picklist_id) or return $e->die_event;
2608 delete_picklist($mgr, $pl) or return $e->die_event;
2610 return $mgr->respond_complete;
2615 __PACKAGE__->register_method(
2616 method => 'activate_purchase_order',
2617 api_name => 'open-ils.acq.purchase_order.activate.dry_run'
2620 __PACKAGE__->register_method(
2621 method => 'activate_purchase_order',
2622 api_name => 'open-ils.acq.purchase_order.activate',
2624 desc => q/Activates a purchase order. This updates the status of the PO / .
2625 q/and Lineitems to 'on-order'. Activated PO's are ready for EDI delivery if appropriate./,
2627 {desc => 'Authentication token', type => 'string'},
2628 {desc => 'Purchase ID', type => 'number'}
2630 return => {desc => '1 on success, Event on error'}
2634 sub activate_purchase_order {
2635 my($self, $conn, $auth, $po_id, $vandelay, $options) = @_;
2637 $$options{dry_run} = ($self->api_name =~ /\.dry_run/) ? 1 : 0;
2639 my $e = new_editor(authtoken=>$auth);
2640 return $e->die_event unless $e->checkauth;
2641 my $mgr = OpenILS::Application::Acq::BatchManager->new(editor => $e, conn => $conn);
2642 my $die_event = activate_purchase_order_impl($mgr, $po_id, $vandelay, $options);
2643 return $e->die_event if $die_event;
2644 $conn->respond_complete(1);
2645 $mgr->run_post_response_hooks unless $$options{dry_run};
2649 # xacts managed within
2650 sub activate_purchase_order_impl {
2651 my ($mgr, $po_id, $vandelay, $options) = @_;
2653 my $dry_run = $$options{dry_run};
2654 my $no_assets = $$options{no_assets};
2656 # read-only until lineitem asset creation
2657 my $e = $mgr->editor;
2660 my $po = $e->retrieve_acq_purchase_order($po_id) or return $e->die_event;
2661 return $e->die_event unless $e->allowed('CREATE_PURCHASE_ORDER', $po->ordering_agency);
2663 return $e->die_event(OpenILS::Event->new('PO_ALREADY_ACTIVATED'))
2664 if $po->order_date; # PO cannot be re-activated
2666 my $provider = $e->retrieve_acq_provider($po->provider);
2668 # find lineitems and create assets for all
2671 purchase_order => $po_id,
2672 state => [qw/pending-order new order-ready/]
2675 my $li_ids = $e->search_acq_lineitem($query, {idlist => 1});
2677 my $vl_resp; # imported li's and the managing queue
2678 unless ($dry_run or $no_assets) {
2679 $e->rollback; # read-only thus far
2681 # list_assets manages its own transactions
2682 $vl_resp = create_lineitem_list_assets($mgr, $li_ids, $vandelay)
2683 or return OpenILS::Event->new('ACQ_LI_IMPORT_FAILED');
2687 # create fund debits for lineitems
2689 for my $li_id (@$li_ids) {
2690 my $li = $e->retrieve_acq_lineitem($li_id);
2692 unless ($li->eg_bib_id or $dry_run or $no_assets) {
2693 # we encountered a lineitem that was not successfully imported.
2694 # we cannot continue. rollback and report.
2696 return OpenILS::Event->new('ACQ_LI_IMPORT_FAILED', {queue => $vl_resp->{queue}});
2699 $li->state('on-order');
2700 $li->claim_policy($provider->default_claim_policy)
2701 if $provider->default_claim_policy and !$li->claim_policy;
2702 create_lineitem_debits($mgr, $li, $options) or return $e->die_event;
2703 update_lineitem($mgr, $li) or return $e->die_event;
2704 $mgr->post_process( sub { create_lineitem_status_events($mgr, $li->id, 'aur.ordered'); });
2708 # create po-item debits
2710 for my $po_item (@{$e->search_acq_po_item({purchase_order => $po_id})}) {
2712 my $debit = create_fund_debit(
2715 debit_type => 'direct_charge', # to match invoicing
2716 origin_amount => $po_item->estimated_cost,
2717 origin_currency_type => $e->retrieve_acq_fund($po_item->fund)->currency_type,
2718 amount => $po_item->estimated_cost,
2719 fund => $po_item->fund
2720 ) or return $e->die_event;
2721 $po_item->fund_debit($debit->id);
2722 $e->update_acq_po_item($po_item) or return $e->die_event;
2726 # mark PO as ordered
2728 $po->state('on-order');
2729 $po->order_date('now');
2730 update_purchase_order($mgr, $po) or return $e->die_event;
2733 $dry_run and $e->rollback or $e->commit;
2735 # tell the world we activated a PO
2736 $U->create_events_for_hook('acqpo.activated', $po, $po->ordering_agency) unless $dry_run;
2742 __PACKAGE__->register_method(
2743 method => 'split_purchase_order_by_lineitems',
2744 api_name => 'open-ils.acq.purchase_order.split_by_lineitems',
2746 desc => q/Splits a PO into many POs, 1 per lineitem. Only works for / .
2747 q/POs a) with more than one lineitems, and b) in the "pending" state./,
2749 {desc => 'Authentication token', type => 'string'},
2750 {desc => 'Purchase order ID', type => 'number'}
2752 return => {desc => 'list of new PO IDs on success, Event on error'}
2756 sub split_purchase_order_by_lineitems {
2757 my ($self, $conn, $auth, $po_id) = @_;
2759 my $e = new_editor("xact" => 1, "authtoken" => $auth);
2760 return $e->die_event unless $e->checkauth;
2762 my $po = $e->retrieve_acq_purchase_order([
2765 "flesh_fields" => {"acqpo" => [qw/lineitems notes/]}
2767 ]) or return $e->die_event;
2769 return $e->die_event
2770 unless $e->allowed("CREATE_PURCHASE_ORDER", $po->ordering_agency);
2772 unless ($po->state eq "pending") {
2774 return new OpenILS::Event("ACQ_PURCHASE_ORDER_TOO_LATE");
2777 unless (@{$po->lineitems} > 1) {
2779 return new OpenILS::Event("ACQ_PURCHASE_ORDER_TOO_SHORT");
2782 # To split an existing PO into many, it seems unwise to just delete the
2783 # original PO, so we'll instead detach all of the original POs' lineitems
2784 # but the first, then create new POs for each of the remaining LIs, and
2785 # then attach the LIs to their new POs.
2787 my @po_ids = ($po->id);
2788 my @moving_li = @{$po->lineitems};
2789 shift @moving_li; # discard first LI
2791 foreach my $li (@moving_li) {
2792 my $new_po = $po->clone;
2794 $new_po->clear_name;
2795 $new_po->creator($e->requestor->id);
2796 $new_po->editor($e->requestor->id);
2797 $new_po->owner($e->requestor->id);
2798 $new_po->edit_time("now");
2799 $new_po->create_time("now");
2801 $new_po = $e->create_acq_purchase_order($new_po);
2803 # Clone any notes attached to the old PO and attach to the new one.
2804 foreach my $note (@{$po->notes}) {
2805 my $new_note = $note->clone;
2806 $new_note->clear_id;
2807 $new_note->edit_time("now");
2808 $new_note->purchase_order($new_po->id);
2809 $e->create_acq_po_note($new_note);
2812 $li->edit_time("now");
2813 $li->purchase_order($new_po->id);
2814 $e->update_acq_lineitem($li);
2816 push @po_ids, $new_po->id;
2819 $po->edit_time("now");
2820 $e->update_acq_purchase_order($po);
2822 return \@po_ids if $e->commit;
2823 return $e->die_event;
2827 sub not_cancelable {
2829 (ref $o eq "HASH" and $o->{"textcode"} eq "ACQ_NOT_CANCELABLE");
2832 __PACKAGE__->register_method(
2833 method => "cancel_purchase_order_api",
2834 api_name => "open-ils.acq.purchase_order.cancel",
2836 desc => q/Cancels an on-order purchase order/,
2838 {desc => "Authentication token", type => "string"},
2839 {desc => "PO ID to cancel", type => "number"},
2840 {desc => "Cancel reason ID", type => "number"}
2842 return => {desc => q/Object describing changed POs, LIs and LIDs
2843 on success; Event on error./}
2847 sub cancel_purchase_order_api {
2848 my ($self, $conn, $auth, $po_id, $cancel_reason) = @_;
2850 my $e = new_editor("xact" => 1, "authtoken" => $auth);
2851 return $e->die_event unless $e->checkauth;
2852 my $mgr = new OpenILS::Application::Acq::BatchManager(
2853 "editor" => $e, "conn" => $conn
2856 $cancel_reason = $mgr->editor->retrieve_acq_cancel_reason($cancel_reason) or
2857 return new OpenILS::Event(
2858 "BAD_PARAMS", "note" => "Provide cancel reason ID"
2861 my $result = cancel_purchase_order($mgr, $po_id, $cancel_reason) or
2862 return $e->die_event;
2863 if (not_cancelable($result)) { # event not from CStoreEditor
2866 } elsif ($result == -1) {
2868 return new OpenILS::Event("ACQ_ALREADY_CANCELED");
2871 $e->commit or return $e->die_event;
2873 # XXX create purchase order status events?
2875 if ($mgr->{post_commit}) {
2876 foreach my $func (@{$mgr->{post_commit}}) {
2884 sub cancel_purchase_order {
2885 my ($mgr, $po_id, $cancel_reason) = @_;
2887 my $po = $mgr->editor->retrieve_acq_purchase_order($po_id) or return 0;
2889 # XXX is "cancelled" a typo? It's not correct US spelling, anyway.
2890 # Depending on context, this may not warrant an event.
2891 return -1 if $po->state eq "cancelled";
2893 # But this always does.
2894 return new OpenILS::Event(
2895 "ACQ_NOT_CANCELABLE", "note" => "purchase_order $po_id"
2896 ) unless ($po->state eq "on-order" or $po->state eq "pending");
2899 $mgr->editor->allowed("CREATE_PURCHASE_ORDER", $po->ordering_agency);
2901 $po->state("cancelled");
2902 $po->cancel_reason($cancel_reason->id);
2904 my $li_ids = $mgr->editor->search_acq_lineitem(
2905 {"purchase_order" => $po_id}, {"idlist" => 1}
2908 my $result = {"li" => {}, "lid" => {}};
2909 foreach my $li_id (@$li_ids) {
2910 my $li_result = cancel_lineitem($mgr, $li_id, $cancel_reason)
2913 next if $li_result == -1; # already canceled:skip.
2914 return $li_result if not_cancelable($li_result); # not cancelable:stop.
2916 # Merge in each LI result (there's only going to be
2917 # one per call to cancel_lineitem).
2918 my ($k, $v) = each %{$li_result->{"li"}};
2919 $result->{"li"}->{$k} = $v;
2921 # Merge in each LID result (there may be many per call to
2923 while (($k, $v) = each %{$li_result->{"lid"}}) {
2924 $result->{"lid"}->{$k} = $v;
2928 # TODO who/what/where/how do we indicate this change for electronic orders?
2929 # TODO return changes to encumbered/spent
2930 # TODO maybe cascade up from smaller object to container object if last
2931 # smaller object in the container has been canceled?
2933 update_purchase_order($mgr, $po) or return 0;
2935 $po_id => {"state" => $po->state, "cancel_reason" => $cancel_reason}
2941 __PACKAGE__->register_method(
2942 method => "cancel_lineitem_api",
2943 api_name => "open-ils.acq.lineitem.cancel",
2945 desc => q/Cancels an on-order lineitem/,
2947 {desc => "Authentication token", type => "string"},
2948 {desc => "Lineitem ID to cancel", type => "number"},
2949 {desc => "Cancel reason ID", type => "number"}
2951 return => {desc => q/Object describing changed LIs and LIDs on success;
2956 __PACKAGE__->register_method(
2957 method => "cancel_lineitem_api",
2958 api_name => "open-ils.acq.lineitem.cancel.batch",
2960 desc => q/Batched version of open-ils.acq.lineitem.cancel/,
2961 return => {desc => q/Object describing changed LIs and LIDs on success;
2966 sub cancel_lineitem_api {
2967 my ($self, $conn, $auth, $li_id, $cancel_reason) = @_;
2969 my $batched = $self->api_name =~ /\.batch/;
2971 my $e = new_editor("xact" => 1, "authtoken" => $auth);
2972 return $e->die_event unless $e->checkauth;
2973 my $mgr = new OpenILS::Application::Acq::BatchManager(
2974 "editor" => $e, "conn" => $conn
2977 $cancel_reason = $mgr->editor->retrieve_acq_cancel_reason($cancel_reason) or
2978 return new OpenILS::Event(
2979 "BAD_PARAMS", "note" => "Provide cancel reason ID"
2982 my ($result, $maybe_event);
2985 $result = {"li" => {}, "lid" => {}};
2986 foreach my $one_li_id (@$li_id) {
2987 my $one = cancel_lineitem($mgr, $one_li_id, $cancel_reason) or
2988 return $e->die_event;
2989 if (not_cancelable($one)) {
2990 $maybe_event = $one;
2991 } elsif ($result == -1) {
2992 $maybe_event = new OpenILS::Event("ACQ_ALREADY_CANCELED");
2996 while (($k, $v) = each %{$one->{"li"}}) {
2997 $result->{"li"}->{$k} = $v;
3000 if ($one->{"lid"}) {
3001 while (($k, $v) = each %{$one->{"lid"}}) {
3002 $result->{"lid"}->{$k} = $v;
3008 $result = cancel_lineitem($mgr, $li_id, $cancel_reason) or
3009 return $e->die_event;
3011 if (not_cancelable($result)) {
3014 } elsif ($result == -1) {
3016 return new OpenILS::Event("ACQ_ALREADY_CANCELED");
3020 if ($batched and not scalar keys %{$result->{"li"}}) {
3022 return $maybe_event;
3024 $e->commit or return $e->die_event;
3025 # create_lineitem_status_events should handle array li_id ok
3026 create_lineitem_status_events($mgr, $li_id, "aur.cancelled");
3028 if ($mgr->{post_commit}) {
3029 foreach my $func (@{$mgr->{post_commit}}) {
3038 sub cancel_lineitem {
3039 my ($mgr, $li_id, $cancel_reason) = @_;
3041 my $li = $mgr->editor->retrieve_acq_lineitem([
3042 $li_id, {flesh => 1,
3043 flesh_fields => {jub => ['purchase_order','cancel_reason']}}
3046 return 0 unless $mgr->editor->allowed(
3047 "CREATE_PURCHASE_ORDER", $li->purchase_order->ordering_agency
3050 # Depending on context, this may not warrant an event.
3051 return -1 if $li->state eq "cancelled"
3052 and $li->cancel_reason->keep_debits eq 'f';
3054 # But this always does. Note that this used to be looser, but you can
3055 # no longer cancel lineitems that lack a PO or that are in "pending-order"
3056 # state (you could in the past).
3057 return new OpenILS::Event(
3058 "ACQ_NOT_CANCELABLE", "note" => "lineitem $li_id"
3059 ) unless $li->purchase_order and
3060 ($li->state eq "on-order" or $li->state eq "cancelled");
3062 $li->state("cancelled");
3063 $li->cancel_reason($cancel_reason->id);
3065 my $lids = $mgr->editor->search_acq_lineitem_detail([{
3066 "lineitem" => $li_id
3069 flesh_fields => { acqlid => ['eg_copy_id'] }
3072 my $result = {"lid" => {}};
3074 foreach my $lid (@$lids) {
3075 my $lid_result = cancel_lineitem_detail($mgr, $lid->id, $cancel_reason)
3078 # gathering any real copies for deletion
3079 if ($lid->eg_copy_id) {
3080 $lid->eg_copy_id->isdeleted('t');
3081 push @$copies, $lid->eg_copy_id;
3084 next if $lid_result == -1; # already canceled: just skip it.
3085 return $lid_result if not_cancelable($lid_result); # not cxlable: stop.
3087 # Merge in each LID result (there's only going to be one per call to
3088 # cancel_lineitem_detail).
3089 my ($k, $v) = each %{$lid_result->{"lid"}};
3090 $result->{"lid"}->{$k} = $v;
3093 # Attempt to delete the gathered copies (this will also handle volume deletion and bib deletion)
3094 # Delete empty bibs according org unit setting
3095 my $force_delete_empty_bib = $U->ou_ancestor_setting_value(
3096 $mgr->editor->requestor->ws_ou, 'cat.bib.delete_on_no_copy_via_acq_lineitem_cancel', $mgr->editor);
3097 if (scalar(@$copies)>0) {
3099 my $delete_stats = undef;
3100 my $retarget_holds = [];
3101 my $cat_evt = OpenILS::Application::Cat::AssetCommon->update_fleshed_copies(
3102 $mgr->editor, $override, undef, $copies, $delete_stats, $retarget_holds,$force_delete_empty_bib);
3105 $logger->info("fleshed copy update failed with event: ".OpenSRF::Utils::JSON->perl2JSON($cat_evt));
3106 return new OpenILS::Event(
3107 "ACQ_NOT_CANCELABLE", "note" => "lineitem $li_id", "payload" => $cat_evt
3111 # We can't do the following and stay within the same transaction, but that's okay, the hold targeter will pick these up later.
3112 #my $ses = OpenSRF::AppSession->create('open-ils.circ');
3113 #$ses->request('open-ils.circ.hold.reset.batch', $auth, $retarget_holds);
3116 # if we have a bib, check to see whether it has been deleted. if so, cancel any active holds targeting that bib
3117 if ($li->eg_bib_id) {
3118 my $bib = $mgr->editor->retrieve_biblio_record_entry($li->eg_bib_id) or return new OpenILS::Event(
3119 "ACQ_NOT_CANCELABLE", "note" => "Could not retrieve bib " . $li->eg_bib_id . " for lineitem $li_id"
3121 if ($U->is_true($bib->deleted)) {
3122 my $holds = $mgr->editor->search_action_hold_request(
3123 { cancel_time => undef,
3124 fulfillment_time => undef,
3125 target => $li->eg_bib_id
3129 my %cached_usr_home_ou = ();
3131 for my $hold (@$holds) {
3133 $logger->info("Cancelling hold ".$hold->id.
3134 " due to acq lineitem cancellation.");
3136 $hold->cancel_time('now');
3137 $hold->cancel_cause(5); # 'Staff forced'--we may want a new hold cancel cause reason for this
3138 $hold->cancel_note('Corresponding Acquistion Lineitem/Purchase Order was cancelled.');
3139 unless($mgr->editor->update_action_hold_request($hold)) {
3140 my $evt = $mgr->editor->event;
3141 $logger->error("Error updating hold ". $evt->textcode .":". $evt->desc .":". $evt->stacktrace);
3142 return new OpenILS::Event(
3143 "ACQ_NOT_CANCELABLE", "note" => "Could not cancel hold " . $hold->id . " for lineitem $li_id", "payload" => $evt
3146 if (! defined $mgr->{post_commit}) { # we need a mechanism for creating trigger events, but only if the transaction gets committed
3147 $mgr->{post_commit} = [];
3149 push @{ $mgr->{post_commit} }, sub {
3150 my $home_ou = $cached_usr_home_ou{$hold->usr};
3152 my $user = $mgr->editor->retrieve_actor_user($hold->usr); # FIXME: how do we want to handle failures here?
3153 $home_ou = $user->home_ou;
3154 $cached_usr_home_ou{$hold->usr} = $home_ou;
3156 $U->create_events_for_hook('hold_request.cancel.cancelled_order', $hold, $home_ou);
3162 update_lineitem($mgr, $li) or return 0;
3165 "state" => $li->state,
3166 "cancel_reason" => $cancel_reason
3173 __PACKAGE__->register_method(
3174 method => "cancel_lineitem_detail_api",
3175 api_name => "open-ils.acq.lineitem_detail.cancel",
3177 desc => q/Cancels an on-order lineitem detail/,
3179 {desc => "Authentication token", type => "string"},
3180 {desc => "Lineitem detail ID to cancel", type => "number"},
3181 {desc => "Cancel reason ID", type => "number"}
3183 return => {desc => q/Object describing changed LIDs on success;
3188 sub cancel_lineitem_detail_api {
3189 my ($self, $conn, $auth, $lid_id, $cancel_reason) = @_;
3191 my $e = new_editor("xact" => 1, "authtoken" => $auth);
3192 return $e->die_event unless $e->checkauth;
3193 my $mgr = new OpenILS::Application::Acq::BatchManager(
3194 "editor" => $e, "conn" => $conn
3197 $cancel_reason = $mgr->editor->retrieve_acq_cancel_reason($cancel_reason) or
3198 return new OpenILS::Event(
3199 "BAD_PARAMS", "note" => "Provide cancel reason ID"
3202 my $result = cancel_lineitem_detail($mgr, $lid_id, $cancel_reason) or
3203 return $e->die_event;
3205 if (not_cancelable($result)) {
3208 } elsif ($result == -1) {
3210 return new OpenILS::Event("ACQ_ALREADY_CANCELED");
3213 $e->commit or return $e->die_event;
3215 # XXX create lineitem detail status events?
3219 sub cancel_lineitem_detail {
3220 my ($mgr, $lid_id, $cancel_reason) = @_;
3221 my $lid = $mgr->editor->retrieve_acq_lineitem_detail([
3225 "acqlid" => ["lineitem","cancel_reason"],
3226 "jub" => ["purchase_order"]
3231 # It's OK to cancel an already-canceled copy if the copy was
3232 # previously "delayed" -- keep_debits == true
3233 # Depending on context, this may not warrant an event.
3234 return -1 if $lid->cancel_reason
3235 and $lid->cancel_reason->keep_debits eq 'f';
3237 # But this always does.
3238 return new OpenILS::Event(
3239 "ACQ_NOT_CANCELABLE", "note" => "lineitem_detail $lid_id"
3241 (! $lid->lineitem->purchase_order) or
3243 (not $lid->recv_time) and
3245 $lid->lineitem->purchase_order and (
3246 $lid->lineitem->state eq "on-order" or
3247 $lid->lineitem->state eq "pending-order" or
3248 $lid->lineitem->state eq "cancelled"
3253 return 0 unless $mgr->editor->allowed(
3254 "CREATE_PURCHASE_ORDER",
3255 $lid->lineitem->purchase_order->ordering_agency
3256 ) or (! $lid->lineitem->purchase_order);
3258 $lid->cancel_reason($cancel_reason->id);
3260 unless($U->is_true($cancel_reason->keep_debits)) {
3261 my $debit_id = $lid->fund_debit;
3262 $lid->clear_fund_debit;
3265 # item is cancelled. Remove the fund debit.
3266 my $debit = $mgr->editor->retrieve_acq_fund_debit($debit_id);
3267 if (!$U->is_true($debit->encumbrance)) {
3268 $mgr->editor->rollback;
3269 return OpenILS::Event->new('ACQ_NOT_CANCELABLE',
3270 note => "Debit is marked as paid: $debit_id");
3272 $mgr->editor->delete_acq_fund_debit($debit) or return $mgr->editor->die_event;
3276 # XXX LIDs don't have either an editor or a edit_time field. Should we
3277 # update these on the LI when we alter an LID?
3278 $mgr->editor->update_acq_lineitem_detail($lid) or return 0;
3280 return {"lid" => {$lid_id => {"cancel_reason" => $cancel_reason}}};
3284 __PACKAGE__->register_method(
3285 method => 'user_requests',
3286 api_name => 'open-ils.acq.user_request.retrieve.by_user_id',
3289 desc => 'Retrieve fleshed user requests and related data for a given user.',
3291 { desc => 'Authentication token', type => 'string' },
3292 { desc => 'User ID of the owner, or array of IDs', },
3293 { desc => 'Options hash (optional) with any of the keys: order_by, limit, offset, state (of the lineitem)',
3298 desc => 'Fleshed user requests and related data',
3304 __PACKAGE__->register_method(
3305 method => 'user_requests',
3306 api_name => 'open-ils.acq.user_request.retrieve.by_home_ou',
3309 desc => 'Retrieve fleshed user requests and related data for a given org unit or units.',
3311 { desc => 'Authentication token', type => 'string' },
3312 { desc => 'Org unit ID, or array of IDs', },
3313 { desc => 'Options hash (optional) with any of the keys: order_by, limit, offset, state (of the lineitem)',
3318 desc => 'Fleshed user requests and related data',
3325 my($self, $conn, $auth, $search_value, $options) = @_;
3326 my $e = new_editor(authtoken => $auth);
3327 return $e->event unless $e->checkauth;
3328 my $rid = $e->requestor->id;
3332 "select"=>{"aur"=>["id"],"au"=>["home_ou", {column => 'id', alias => 'usr_id'} ]},
3333 "from"=>{ "aur" => { "au" => {}, "jub" => { "type" => "left" } } },
3337 {"id"=>undef}, # this with the left-join pulls in requests without lineitems
3338 {"state"=>["new","on-order","pending-order"]} # FIXME - probably needs softcoding
3342 "order_by"=>[{"class"=>"aur", "field"=>"request_date", "direction"=>"desc"}]
3345 foreach (qw/ order_by limit offset /) {
3346 $query->{$_} = $options->{$_} if defined $options->{$_};
3348 if (defined $options->{'state'}) {
3349 $query->{'where'}->{'+jub'}->{'-or'}->[1]->{'state'} = $options->{'state'};
3352 if ($self->api_name =~ /by_user_id/) {
3353 $query->{'where'}->{'usr'} = $search_value;
3355 $query->{'where'}->{'+au'} = { 'home_ou' => $search_value };
3358 my $pertinent_ids = $e->json_query($query);
3361 for my $id_blob (@$pertinent_ids) {
3362 if ($rid != $id_blob->{usr_id}) {
3363 if (!defined $perm_test{ $id_blob->{home_ou} }) {
3364 $perm_test{ $id_blob->{home_ou} } = $e->allowed( ['user_request.view'], $id_blob->{home_ou} );
3366 if (!$perm_test{ $id_blob->{home_ou} }) {
3370 my $aur_obj = $e->retrieve_acq_user_request([
3372 {flesh => 1, flesh_fields => { "aur" => [ 'lineitem' ] } }
3374 if (! $aur_obj) { next; }
3376 if ($aur_obj->lineitem()) {
3377 $aur_obj->lineitem()->clear_marc();
3379 $conn->respond($aur_obj);
3385 __PACKAGE__->register_method (
3386 method => 'update_user_request',
3387 api_name => 'open-ils.acq.user_request.cancel.batch',
3390 desc => 'If given a cancel reason, will update the request with that reason, otherwise, this will delete the request altogether. The ' .
3391 'intention is for staff interfaces or processes to provide cancel reasons, and for patron interfaces to just delete the requests.' ,
3393 { desc => 'Authentication token', type => 'string' },
3394 { desc => 'ID or array of IDs for the user requests to cancel' },
3395 { desc => 'Cancel Reason ID (optional)', type => 'string' }
3398 desc => 'progress object, event on error',
3402 __PACKAGE__->register_method (
3403 method => 'update_user_request',
3404 api_name => 'open-ils.acq.user_request.set_no_hold.batch',
3407 desc => 'Remove the hold from a user request or set of requests',
3409 { desc => 'Authentication token', type => 'string' },
3410 { desc => 'ID or array of IDs for the user requests to modify' }
3413 desc => 'progress object, event on error',
3418 sub update_user_request {
3419 my($self, $conn, $auth, $aur_ids, $cancel_reason) = @_;
3420 my $e = new_editor(xact => 1, authtoken => $auth);
3421 return $e->die_event unless $e->checkauth;
3422 my $rid = $e->requestor->id;
3426 for my $id (@$aur_ids) {
3428 my $aur_obj = $e->retrieve_acq_user_request([
3431 flesh_fields => { "aur" => ['lineitem', 'usr'] }
3433 ]) or return $e->die_event;
3435 my $context_org = $aur_obj->usr()->home_ou();
3436 $aur_obj->usr( $aur_obj->usr()->id() );
3438 if ($rid != $aur_obj->usr) {
3439 if (!defined $perm_test{ $context_org }) {
3440 $perm_test{ $context_org } = $e->allowed( ['user_request.update'], $context_org );
3442 if (!$perm_test{ $context_org }) {
3447 if($self->api_name =~ /set_no_hold/) {
3448 if ($U->is_true($aur_obj->hold)) {
3450 $e->update_acq_user_request($aur_obj) or return $e->die_event;
3454 if($self->api_name =~ /cancel/) {
3455 if ( $cancel_reason ) {
3456 $aur_obj->cancel_reason( $cancel_reason );
3457 $e->update_acq_user_request($aur_obj) or return $e->die_event;
3458 create_user_request_events( $e, [ $aur_obj ], 'aur.rejected' );
3460 $e->delete_acq_user_request($aur_obj);
3464 $conn->respond({maximum => scalar(@$aur_ids), progress => $x++});
3468 return {complete => 1};
3471 __PACKAGE__->register_method (
3472 method => 'new_user_request',
3473 api_name => 'open-ils.acq.user_request.create',
3475 desc => 'Create a new user request object in the DB',
3477 { desc => 'Authentication token', type => 'string' },
3478 { desc => 'User request data hash. Hash keys match the fields for the "aur" object', type => 'object' }
3481 desc => 'The created user request object, or event on error'
3486 sub new_user_request {
3487 my($self, $conn, $auth, $form_data) = @_;
3488 my $e = new_editor(xact => 1, authtoken => $auth);
3489 return $e->die_event unless $e->checkauth;
3490 my $rid = $e->requestor->id;
3491 my $target_user_fleshed;
3492 if (! defined $$form_data{'usr'}) {
3493 $$form_data{'usr'} = $rid;
3495 if ($$form_data{'usr'} != $rid) {
3496 # See if the requestor can place the request on behalf of a different user.
3497 $target_user_fleshed = $e->retrieve_actor_user($$form_data{'usr'}) or return $e->die_event;
3498 $e->allowed('user_request.create', $target_user_fleshed->home_ou) or return $e->die_event;
3500 $target_user_fleshed = $e->requestor;
3501 $e->allowed('CREATE_PURCHASE_REQUEST') or return $e->die_event;
3503 if (! defined $$form_data{'pickup_lib'}) {
3504 if ($target_user_fleshed->ws_ou) {
3505 $$form_data{'pickup_lib'} = $target_user_fleshed->ws_ou;
3507 $$form_data{'pickup_lib'} = $target_user_fleshed->home_ou;
3510 if (! defined $$form_data{'request_type'}) {
3511 $$form_data{'request_type'} = 1; # Books
3513 my $aur_obj = new Fieldmapper::acq::user_request;
3515 $aur_obj->usr( $$form_data{'usr'} );
3516 $aur_obj->request_date( 'now' );
3517 for my $field ( keys %$form_data ) {
3518 if (defined $$form_data{$field} and $field !~ /^(id|lineitem|eg_bib|request_date|cancel_reason)$/) {
3519 $aur_obj->$field( $$form_data{$field} );
3523 $aur_obj = $e->create_acq_user_request($aur_obj) or return $e->die_event;
3525 $e->commit and create_user_request_events( $e, [ $aur_obj ], 'aur.created' );
3530 sub create_user_request_events {
3531 my($e, $user_reqs, $hook) = @_;
3533 my $ses = OpenSRF::AppSession->create('open-ils.trigger');
3536 my %cached_usr_home_ou = ();
3537 for my $user_req (@$user_reqs) {
3538 my $home_ou = $cached_usr_home_ou{$user_req->usr};
3540 my $user = $e->retrieve_actor_user($user_req->usr) or return $e->die_event;
3541 $home_ou = $user->home_ou;
3542 $cached_usr_home_ou{$user_req->usr} = $home_ou;
3544 my $req = $ses->request('open-ils.trigger.event.autocreate', $hook, $user_req, $home_ou);
3553 __PACKAGE__->register_method(
3554 method => "po_note_CUD_batch",
3555 api_name => "open-ils.acq.po_note.cud.batch",
3558 desc => q/Manage purchase order notes/,
3560 {desc => "Authentication token", type => "string"},
3561 {desc => "List of po_notes to manage", type => "array"},
3563 return => {desc => "Stream of successfully managed objects"}
3567 sub po_note_CUD_batch {
3568 my ($self, $conn, $auth, $notes) = @_;
3570 my $e = new_editor("xact"=> 1, "authtoken" => $auth);
3571 return $e->die_event unless $e->checkauth;
3574 my $total = @$notes;
3577 foreach my $note (@$notes) {
3579 $note->editor($e->requestor->id);
3580 $note->edit_time("now");
3583 $note->creator($e->requestor->id);
3584 $note = $e->create_acq_po_note($note) or return $e->die_event;
3585 } elsif ($note->isdeleted) {
3586 $e->delete_acq_po_note($note) or return $e->die_event;
3587 } elsif ($note->ischanged) {
3588 $e->update_acq_po_note($note) or return $e->die_event;
3591 unless ($note->isdeleted) {
3592 $note = $e->retrieve_acq_po_note($note->id) or
3593 return $e->die_event;
3597 {"maximum" => $total, "progress" => ++$count, "note" => $note}
3601 $e->commit and $conn->respond_complete or return $e->die_event;
3605 # retrieves a lineitem, fleshes its PO and PL, checks perms
3606 # returns ($li, $evt, $org)
3607 sub fetch_and_check_li {
3610 my $perm_mode = shift || 'read';
3612 my $li = $e->retrieve_acq_lineitem([
3615 flesh_fields => {jub => ['purchase_order', 'picklist']}
3617 ]) or return (undef, $e->die_event);
3620 if(my $po = $li->purchase_order) {
3621 $org = $po->ordering_agency;
3622 my $perms = ($perm_mode eq 'read') ? 'VIEW_PURCHASE_ORDER' : 'CREATE_PURCHASE_ORDER';
3623 return ($li, $e->die_event) unless $e->allowed($perms, $org);
3625 } elsif(my $pl = $li->picklist) {
3626 $org = $pl->org_unit;
3627 my $perms = ($perm_mode eq 'read') ? 'VIEW_PICKLIST' : 'CREATE_PICKLIST';
3628 return ($li, $e->die_event) unless $e->allowed($perms, $org);
3631 return ($li, undef, $org);
3635 __PACKAGE__->register_method(
3636 method => "clone_distrib_form",
3637 api_name => "open-ils.acq.distribution_formula.clone",
3640 desc => q/Clone a distribution formula/,
3642 {desc => "Authentication token", type => "string"},
3643 {desc => "Original formula ID", type => 'integer'},
3644 {desc => "Name of new formula", type => 'string'},
3646 return => {desc => "ID of newly created formula"}
3650 sub clone_distrib_form {
3651 my($self, $client, $auth, $form_id, $new_name) = @_;
3653 my $e = new_editor("xact"=> 1, "authtoken" => $auth);
3654 return $e->die_event unless $e->checkauth;
3656 my $old_form = $e->retrieve_acq_distribution_formula($form_id) or return $e->die_event;
3657 return $e->die_event unless $e->allowed('ADMIN_ACQ_DISTRIB_FORMULA', $old_form->owner);
3659 my $new_form = Fieldmapper::acq::distribution_formula->new;
3661 $new_form->owner($old_form->owner);
3662 $new_form->name($new_name);
3663 $e->create_acq_distribution_formula($new_form) or return $e->die_event;
3665 my $entries = $e->search_acq_distribution_formula_entry({formula => $form_id});
3666 for my $entry (@$entries) {
3667 my $new_entry = Fieldmapper::acq::distribution_formula_entry->new;
3668 $new_entry->$_($entry->$_()) for $entry->real_fields;
3669 $new_entry->formula($new_form->id);
3670 $new_entry->clear_id;
3671 $e->create_acq_distribution_formula_entry($new_entry) or return $e->die_event;
3675 return $new_form->id;
3678 __PACKAGE__->register_method(
3679 method => 'add_li_to_po',
3680 api_name => 'open-ils.acq.purchase_order.add_lineitem',
3682 desc => q/Adds a lineitem to an existing purchase order/,
3684 {desc => 'Authentication token', type => 'string'},
3685 {desc => 'The purchase order id', type => 'number'},
3686 {desc => 'The lineitem ID (or an array of them)', type => 'mixed'},
3688 return => {desc => 'Streams a total versus completed counts object, event on error'}
3693 my($self, $conn, $auth, $po_id, $li_id) = @_;
3695 my $e = new_editor(authtoken => $auth, xact => 1);
3696 return $e->die_event unless $e->checkauth;
3698 my $mgr = OpenILS::Application::Acq::BatchManager->new(editor => $e, conn => $conn);
3700 my $po = $e->retrieve_acq_purchase_order($po_id)
3701 or return $e->die_event;
3703 return $e->die_event unless
3704 $e->allowed('CREATE_PURCHASE_ORDER', $po->ordering_agency);
3706 unless ($po->state =~ /new|pending/) {
3708 return {success => 0, po => $po, error => 'bad-po-state'};
3713 if (ref $li_id eq "ARRAY") {
3714 $li_id = [ map { int($_) } @$li_id ];
3715 return $e->die_event(new OpenILS::Event("BAD_PARAMS")) unless @$li_id;
3717 $lis = $e->search_acq_lineitem({id => $li_id})
3718 or return $e->die_event;
3720 my $li = $e->retrieve_acq_lineitem(int($li_id))
3721 or return $e->die_event;
3725 foreach my $li (@$lis) {
3726 if ($li->state !~ /new|order-ready|pending-order/ or
3727 $li->purchase_order) {
3729 return {success => 0, li => $li, error => 'bad-li-state'};
3732 $li->provider($po->provider);
3733 $li->purchase_order($po_id);
3734 $li->state('pending-order');
3735 apply_default_copies($mgr, $po, $li->id) or return $e->die_event;
3736 update_lineitem($mgr, $li) or return $e->die_event;
3740 return {success => 1};
3743 __PACKAGE__->register_method(
3744 method => 'po_lineitems_no_copies',
3745 api_name => 'open-ils.acq.purchase_order.no_copy_lineitems.id_list',
3749 desc => q/Returns the set of lineitem IDs for a given PO that have no copies attached/,
3751 {desc => 'Authentication token', type => 'string'},
3752 {desc => 'The purchase order id', type => 'number'},
3754 return => {desc => 'Stream of lineitem IDs on success, event on error'}
3758 sub po_lineitems_no_copies {
3759 my ($self, $conn, $auth, $po_id) = @_;
3761 my $e = new_editor(authtoken => $auth);
3762 return $e->event unless $e->checkauth;
3764 # first check the view perms for LI's attached to this PO
3765 my $po = $e->retrieve_acq_purchase_order($po_id) or return $e->event;
3766 return $e->event unless $e->allowed('VIEW_PURCHASE_ORDER', $po->ordering_agency);
3768 my $ids = $e->json_query({
3769 select => {jub => ['id']},
3770 from => {jub => {acqlid => {type => 'left'}}},
3772 '+jub' => {purchase_order => $po_id},
3773 '+acqlid' => {lineitem => undef}
3777 $conn->respond($_->{id}) for @$ids;
3781 __PACKAGE__->register_method(
3782 method => 'set_li_order_ident',
3783 api_name => 'open-ils.acq.lineitem.order_identifier.set',
3786 Given an existing lineitem_attr (typically a marc_attr), this will
3787 create a matching local_attr to store the name and value and mark
3788 the attr as the order_ident. Any existing local_attr marked as
3789 order_ident is removed.
3792 {desc => 'Authentication token', type => 'string'},
3793 {desc => q/Args object:
3794 source_attr_id : ID of the existing lineitem_attr to use as
3796 lineitem_id : lineitem id
3797 attr_name : name ('isbn', etc.) of a new marc_attr to add to
3798 the lineitem to use for the order ident
3799 attr_value : value for the new marc_attr
3800 no_apply_bre : if set, newly added attrs will not be applied
3801 to the lineitems' linked bib record/,
3804 return => {desc => q/Returns the attribute
3805 responsible for tracking the order identifier/}
3809 sub set_li_order_ident {
3810 my ($self, $conn, $auth, $args) = @_;
3814 my $source_attr_id = $args->{source_attr_id};
3816 my $e = new_editor(authtoken => $auth, xact => 1);
3817 return $e->die_event unless $e->checkauth;
3819 # fetch attr, LI, and check update permissions
3821 my $li_id = $args->{lineitem_id};
3823 if ($source_attr_id) {
3824 $source_attr = $e->retrieve_acq_lineitem_attr($source_attr_id)
3825 or return $e->die_event;
3826 $li_id = $source_attr->lineitem;
3829 my ($li, $evt, $perm_org) = fetch_and_check_li($e, $li_id, 'write');
3830 return $evt if $evt;
3832 return $e->die_event unless
3833 $e->allowed('ACQ_SET_LINEITEM_IDENTIFIER', $perm_org);
3835 # if needed, create a new marc attr for
3836 # the lineitem to represent the ident value
3838 ($source_attr, $evt) = apply_new_li_ident_attr(
3839 $e, $li, $perm_org, $args->{attr_name}, $args->{attr_value})
3840 unless $source_attr;
3842 return $evt if $evt;
3844 # remove the existing order_ident attribute if present
3846 my $old_attr = $e->search_acq_lineitem_attr({
3847 attr_type => 'lineitem_local_attr_definition',
3848 lineitem => $li->id,
3854 # if we already have an order_ident that matches the
3855 # source attr, there's nothing left to do.
3857 if ($old_attr->attr_name eq $source_attr->attr_name and
3858 $old_attr->attr_value eq $source_attr->attr_value) {
3864 # remove the old order_ident attribute
3865 $e->delete_acq_lineitem_attr($old_attr) or return $e->die_event;
3869 # make sure we have a local_attr_def to match the source attr def
3871 my $local_def = $e->search_acq_lineitem_local_attr_definition({
3872 code => $source_attr->attr_name
3877 $e->retrieve_acq_lineitem_attr_definition($source_attr->definition);
3878 $local_def = Fieldmapper::acq::lineitem_local_attr_definition->new;
3879 $local_def->code($source_def->code);
3880 $local_def->description($source_def->description);
3881 $local_def = $e->create_acq_lineitem_local_attr_definition($local_def)
3882 or return $e->die_event;
3885 # create the new order_ident local attr
3887 my $new_attr = Fieldmapper::acq::lineitem_attr->new;
3888 $new_attr->definition($local_def->id);
3889 $new_attr->attr_type('lineitem_local_attr_definition');
3890 $new_attr->lineitem($li->id);
3891 $new_attr->attr_name($source_attr->attr_name);
3892 $new_attr->attr_value($source_attr->attr_value);
3893 $new_attr->order_ident('t');
3895 $new_attr = $e->create_acq_lineitem_attr($new_attr)
3896 or return $e->die_event;
3903 # Given an isbn, issn, or upc, add the value to the lineitem marc.
3904 # Upon update, the value will be auto-magically represented as
3905 # a lineitem marc attr.
3906 # If the li is linked to a bib record and the user has the correct
3907 # permissions, update the bib record to match.
3908 sub apply_new_li_ident_attr {
3909 my ($e, $li, $perm_org, $attr_name, $attr_value) = @_;
3917 my $marc_field = MARC::Field->new(
3918 $tags{$attr_name}, '', '','a' => $attr_value);
3920 my $li_rec = MARC::Record->new_from_xml($li->marc, 'UTF-8', 'USMARC');
3921 $li_rec->insert_fields_ordered($marc_field);
3923 $li->marc(clean_marc($li_rec));
3924 $li->editor($e->requestor->id);
3925 $li->edit_time('now');
3927 $e->update_acq_lineitem($li) or return (undef, $e->die_event);
3929 my $source_attr = $e->search_acq_lineitem_attr({
3930 attr_name => $attr_name,
3931 attr_value => $attr_value,
3932 attr_type => 'lineitem_marc_attr_definition'
3935 if (!$source_attr) {
3936 $logger->error("ACQ lineitem update failed to produce a matching ".
3937 " marc attribute for $attr_name => $attr_value");
3938 return (undef, OpenILS::Event->new('INTERNAL_SERVER_ERROR'));
3941 return ($source_attr) unless
3943 $e->allowed('ACQ_ADD_LINEITEM_IDENTIFIER', $perm_org);
3945 # li is linked to a bib record and user has the update perms
3947 my $bre = $e->retrieve_biblio_record_entry($li->eg_bib_id);
3948 my $bre_marc = MARC::Record->new_from_xml($bre->marc, 'UTF-8', 'USMARC');
3949 $bre_marc->insert_fields_ordered($marc_field);
3951 $bre->marc(clean_marc($bre_marc));
3952 $bre->editor($e->requestor->id);
3953 $bre->edit_date('now');
3955 $e->update_biblio_record_entry($bre) or return (undef, $e->die_event);
3957 return ($source_attr);