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);
17 purchase_order => undef,
23 $self->{ingest_queue} = [];
25 $self->throttle(5) unless $self->throttle;
26 $self->{post_proc_queue} = [];
27 $self->{last_respond_progress} = 0;
33 $self->{conn} = $val if $val;
38 $self->{throttle} = $val if $val;
39 return $self->{throttle};
42 my($self, %other_args) = @_;
43 if($self->throttle and not %other_args) {
45 ($self->{args}->{progress} - $self->{last_respond_progress}) >= $self->throttle
48 $self->conn->respond({ %{$self->{args}}, %other_args });
49 $self->{last_respond_progress} = $self->{args}->{progress};
51 sub respond_complete {
52 my($self, %other_args) = @_;
54 $self->conn->respond_complete({ %{$self->{args}}, %other_args });
55 $self->run_post_response_hooks;
59 # run the post response hook subs, shifting them off as we go
60 sub run_post_response_hooks {
62 (shift @{$self->{post_proc_queue}})->() while @{$self->{post_proc_queue}};
65 # any subs passed to this method will be run after the call to respond_complete
68 push(@{$self->{post_proc_queue}}, $sub);
73 $self->{args}->{total} = $val if defined $val;
74 $self->{args}->{maximum} = $self->{args}->{total};
75 return $self->{args}->{total};
79 $self->{args}->{purchase_order} = $val if $val;
84 $self->{args}->{picklist} = $val if $val;
89 $self->{args}->{lid} += 1;
90 $self->{args}->{progress} += 1;
95 $self->{args}->{li} += 1;
96 $self->{args}->{progress} += 1;
101 $self->{args}->{copies} += 1;
102 $self->{args}->{progress} += 1;
107 $self->{args}->{bibs} += 1;
108 $self->{args}->{progress} += 1;
112 my($self, $amount) = @_;
113 $self->{args}->{debits_accrued} += $amount;
114 $self->{args}->{progress} += 1;
118 my($self, $editor) = @_;
119 $self->{editor} = $editor if defined $editor;
120 return $self->{editor};
124 $self->{args}->{complete} = 1;
129 my($self, $val) = @_;
130 $self->{ingest_ses} = $val if $val;
131 return $self->{ingest_ses};
134 sub push_ingest_queue {
135 my($self, $rec_id) = @_;
137 $self->ingest_ses(OpenSRF::AppSession->connect('open-ils.ingest'))
138 unless $self->ingest_ses;
140 my $req = $self->ingest_ses->request('open-ils.ingest.full.biblio.record', $rec_id);
142 push(@{$self->{ingest_queue}}, $req);
145 sub process_ingest_records {
147 return unless @{$self->{ingest_queue}};
149 for my $req (@{$self->{ingest_queue}}) {
153 $self->{args}->{indexed} += 1;
154 $self->{args}->{progress} += 1;
159 $self->ingest_ses->disconnect;
164 my($self, $org, $key, $val) = @_;
165 $self->{cache}->{$org} = {} unless $self->{cache}->{org};
166 $self->{cache}->{$org}->{$key} = $val if defined $val;
167 return $self->{cache}->{$org}->{$key};
171 package OpenILS::Application::Acq::Order;
172 use base qw/OpenILS::Application/;
173 use strict; use warnings;
174 # ----------------------------------------------------------------------------
175 # Break up each component of the order process and pieces into managable
176 # actions that can be shared across different workflows
177 # ----------------------------------------------------------------------------
179 use OpenSRF::Utils::Logger qw(:logger);
180 use OpenSRF::Utils::JSON;
181 use OpenSRF::AppSession;
182 use OpenILS::Utils::Fieldmapper;
183 use OpenILS::Utils::CStoreEditor q/:funcs/;
184 use OpenILS::Utils::Normalize qw/clean_marc/;
185 use OpenILS::Const qw/:const/;
186 use OpenSRF::EX q/:try/;
187 use OpenILS::Application::AppUtils;
188 use OpenILS::Application::Cat::BibCommon;
189 use OpenILS::Application::Cat::AssetCommon;
192 use MARC::File::XML (BinaryEncoding => 'UTF-8');
193 my $U = 'OpenILS::Application::AppUtils';
196 # ----------------------------------------------------------------------------
198 # ----------------------------------------------------------------------------
199 sub create_lineitem {
200 my($mgr, %args) = @_;
201 my $li = Fieldmapper::acq::lineitem->new;
202 $li->creator($mgr->editor->requestor->id);
203 $li->selector($li->creator);
204 $li->editor($li->creator);
205 $li->create_time('now');
206 $li->edit_time('now');
208 $li->$_($args{$_}) for keys %args;
211 $mgr->editor->create_acq_lineitem($li) or return 0;
213 unless($li->estimated_unit_price) {
214 # extract the price from the MARC data
215 my $price = get_li_price_from_attr($mgr->editor, $li) or return $li;
216 $li->estimated_unit_price($price);
217 return update_lineitem($mgr, $li);
223 sub get_li_price_from_attr {
225 my $attrs = $li->attributes || $e->search_acq_lineitem_attr({lineitem => $li->id});
227 for my $attr_type (qw/
228 lineitem_local_attr_definition
229 lineitem_prov_attr_definition
230 lineitem_marc_attr_definition/) {
233 $_->attr_name eq 'estimated_price' and
234 $_->attr_type eq $attr_type } @$attrs;
236 return $attr->attr_value if $attr;
243 sub update_lineitem {
245 $li->edit_time('now');
246 $li->editor($mgr->editor->requestor->id);
248 return $mgr->editor->retrieve_acq_lineitem($mgr->editor->data) if
249 $mgr->editor->update_acq_lineitem($li);
254 # ----------------------------------------------------------------------------
255 # Create real holds from patron requests for a given lineitem
256 # ----------------------------------------------------------------------------
257 sub promote_lineitem_holds {
260 my $requests = $mgr->editor->search_acq_user_request(
261 { lineitem => $li->id,
263 [ { need_before => {'>' => 'now'} },
264 { need_before => undef }
269 for my $request ( @$requests ) {
271 $request->eg_bib( $li->eg_bib_id );
272 $mgr->editor->update_acq_user_request( $request ) or return 0;
274 next unless ($U->is_true( $request->hold ));
276 my $hold = Fieldmapper::action::hold_request->new;
277 $hold->usr( $request->usr );
278 $hold->requestor( $request->usr );
279 $hold->request_time( $request->request_date );
280 $hold->pickup_lib( $request->pickup_lib );
281 $hold->request_lib( $request->pickup_lib );
282 $hold->selection_ou( $request->pickup_lib );
283 $hold->phone_notify( $request->phone_notify );
284 $hold->email_notify( $request->email_notify );
285 $hold->expire_time( $request->need_before );
287 if ($request->holdable_formats) {
288 my $mrm = $mgr->editor->search_metabib_metarecord_source_map( { source => $li->eg_bib_id } )->[0];
290 $hold->hold_type( 'M' );
291 $hold->holdable_formats( $request->holdable_formats );
292 $hold->target( $mrm->metarecord );
296 if (!$hold->target) {
297 $hold->hold_type( 'T' );
298 $hold->target( $li->eg_bib_id );
301 $mgr->editor->create_actor_hold_request( $hold ) or return 0;
307 sub delete_lineitem {
309 $li = $mgr->editor->retrieve_acq_lineitem($li) unless ref $li;
311 # delete the attached lineitem_details
312 my $lid_ids = $mgr->editor->search_acq_lineitem_detail({lineitem => $li->id}, {idlist=>1});
313 for my $lid_id (@$lid_ids) {
314 return 0 unless delete_lineitem_detail($mgr, $lid_id);
318 return $mgr->editor->delete_acq_lineitem($li);
321 # begins and commit transactions as it goes
322 sub create_lineitem_list_assets {
323 my($mgr, $li_ids) = @_;
324 return undef if check_import_li_marc_perms($mgr, $li_ids);
326 # create the bibs/volumes/copies and ingest the records
327 for my $li_id (@$li_ids) {
328 $mgr->editor->xact_begin;
329 my $data = create_lineitem_assets($mgr, $li_id) or return undef;
330 $mgr->editor->xact_commit;
331 # XXX ingest is in-db now
332 #$mgr->push_ingest_queue($data->{li}->eg_bib_id) if $data->{new_bib};
335 $mgr->process_ingest_records;
339 # returns event on error, undef on success
340 sub check_import_li_marc_perms {
341 my($mgr, $li_ids) = @_;
343 # if there are any order records that are not linked to
344 # in-db bib records, verify staff has perms to import order records
345 my $order_li = $mgr->editor->search_acq_lineitem(
346 [{id => $li_ids, eg_bib_id => undef}, {limit => 1}], {idlist => 1})->[0];
349 return $mgr->editor->die_event unless
350 $mgr->editor->allowed('IMPORT_ACQ_LINEITEM_BIB_RECORD');
357 # ----------------------------------------------------------------------------
358 # if all of the lineitem details for this lineitem have
359 # been received, mark the lineitem as received
360 # returns 1 on non-received, li on received, 0 on error
361 # ----------------------------------------------------------------------------
363 sub describe_affected_po {
367 OpenILS::Application::Acq::Financials::build_price_summary(
372 "state" => $po->state,
373 "amount_encumbered" => $enc,
374 "amount_spent" => $spent
379 sub check_lineitem_received {
380 my($mgr, $li_id) = @_;
382 my $non_recv = $mgr->editor->search_acq_lineitem_detail(
383 {recv_time => undef, lineitem => $li_id}, {idlist=>1});
385 return 1 if @$non_recv;
387 my $li = $mgr->editor->retrieve_acq_lineitem($li_id);
388 $li->state('received');
389 return update_lineitem($mgr, $li);
392 sub receive_lineitem {
393 my($mgr, $li_id, $skip_complete_check) = @_;
394 my $li = $mgr->editor->retrieve_acq_lineitem($li_id) or return 0;
396 my $lid_ids = $mgr->editor->search_acq_lineitem_detail(
397 {lineitem => $li_id, recv_time => undef}, {idlist => 1});
399 for my $lid_id (@$lid_ids) {
400 receive_lineitem_detail($mgr, $lid_id, 1) or return 0;
404 $li->state('received');
406 $li = update_lineitem($mgr, $li) or return 0;
407 $mgr->post_process( sub { create_lineitem_status_events($mgr, $li_id, 'aur.received'); });
411 $skip_complete_check or (
412 $po = check_purchase_order_received($mgr, $li->purchase_order)
415 my $result = {"li" => {$li->id => {"state" => $li->state}}};
416 $result->{"po"} = describe_affected_po($mgr->editor, $po) if ref $po;
420 sub rollback_receive_lineitem {
421 my($mgr, $li_id) = @_;
422 my $li = $mgr->editor->retrieve_acq_lineitem($li_id) or return 0;
424 my $lid_ids = $mgr->editor->search_acq_lineitem_detail(
425 {lineitem => $li_id, recv_time => {'!=' => undef}}, {idlist => 1});
427 for my $lid_id (@$lid_ids) {
428 rollback_receive_lineitem_detail($mgr, $lid_id, 1) or return 0;
432 $li->state('on-order');
433 return update_lineitem($mgr, $li);
437 sub create_lineitem_status_events {
438 my($mgr, $li_id, $hook) = @_;
440 my $ses = OpenSRF::AppSession->create('open-ils.trigger');
442 my $user_reqs = $mgr->editor->search_acq_user_request([
443 {lineitem => $li_id},
444 {flesh => 1, flesh_fields => {aur => ['usr']}}
447 for my $user_req (@$user_reqs) {
448 my $req = $ses->request('open-ils.trigger.event.autocreate', $hook, $user_req, $user_req->usr->home_ou);
456 # ----------------------------------------------------------------------------
458 # ----------------------------------------------------------------------------
459 sub create_lineitem_detail {
460 my($mgr, %args) = @_;
461 my $lid = Fieldmapper::acq::lineitem_detail->new;
462 $lid->$_($args{$_}) for keys %args;
465 return $mgr->editor->create_acq_lineitem_detail($lid);
469 # flesh out any required data with default values where appropriate
470 sub complete_lineitem_detail {
472 unless($lid->barcode) {
473 my $pfx = $U->ou_ancestor_setting_value($lid->owning_lib, 'acq.tmp_barcode_prefix') || 'ACQ';
474 $lid->barcode($pfx.$lid->id);
477 unless($lid->cn_label) {
478 my $pfx = $U->ou_ancestor_setting_value($lid->owning_lib, 'acq.tmp_callnumber_prefix') || 'ACQ';
479 $lid->cn_label($pfx.$lid->id);
482 if(!$lid->location and my $loc = $U->ou_ancestor_setting_value($lid->owning_lib, 'acq.default_copy_location')) {
483 $lid->location($loc);
486 $lid->circ_modifier(get_default_circ_modifier($mgr, $lid->owning_lib))
487 unless defined $lid->circ_modifier;
489 $mgr->editor->update_acq_lineitem_detail($lid) or return 0;
493 sub get_default_circ_modifier {
495 my $code = $mgr->cache($org, 'def_circ_mod');
496 $code = $U->ou_ancestor_setting_value($org, 'acq.default_circ_modifier') unless defined $code;
497 return $mgr->cache($org, 'def_circ_mod', $code) if defined $code;
501 sub delete_lineitem_detail {
503 $lid = $mgr->editor->retrieve_acq_lineitem_detail($lid) unless ref $lid;
504 return $mgr->editor->delete_acq_lineitem_detail($lid);
508 sub receive_lineitem_detail {
509 my($mgr, $lid_id, $skip_complete_check) = @_;
510 my $e = $mgr->editor;
512 my $lid = $e->retrieve_acq_lineitem_detail([
516 acqlid => ['fund_debit']
521 return 1 if $lid->recv_time;
523 $lid->receiver($e->requestor->id);
524 $lid->recv_time('now');
525 $e->update_acq_lineitem_detail($lid) or return 0;
527 if ($lid->eg_copy_id) {
528 my $copy = $e->retrieve_asset_copy($lid->eg_copy_id) or return 0;
529 $copy->status(OILS_COPY_STATUS_IN_PROCESS);
530 $copy->edit_date('now');
531 $copy->editor($e->requestor->id);
532 $copy->creator($e->requestor->id) if $U->ou_ancestor_setting_value(
533 $e->requestor->ws_ou, 'acq.copy_creator_uses_receiver', $e);
534 $e->update_asset_copy($copy) or return 0;
539 return 1 if $skip_complete_check;
541 my $li = check_lineitem_received($mgr, $lid->lineitem) or return 0;
542 return 1 if $li == 1; # li not received
544 return check_purchase_order_received($mgr, $li->purchase_order) or return 0;
548 sub rollback_receive_lineitem_detail {
549 my($mgr, $lid_id) = @_;
550 my $e = $mgr->editor;
552 my $lid = $e->retrieve_acq_lineitem_detail([
556 acqlid => ['fund_debit']
561 return 1 unless $lid->recv_time;
563 $lid->clear_receiver;
564 $lid->clear_recv_time;
565 $e->update_acq_lineitem_detail($lid) or return 0;
567 if ($lid->eg_copy_id) {
568 my $copy = $e->retrieve_asset_copy($lid->eg_copy_id) or return 0;
569 $copy->status(OILS_COPY_STATUS_ON_ORDER);
570 $copy->edit_date('now');
571 $copy->editor($e->requestor->id);
572 $e->update_asset_copy($copy) or return 0;
579 # ----------------------------------------------------------------------------
581 # ----------------------------------------------------------------------------
582 sub set_lineitem_attr {
583 my($mgr, %args) = @_;
584 my $attr_type = $args{attr_type};
586 # first, see if it's already set. May just need to overwrite it
587 my $attr = $mgr->editor->search_acq_lineitem_attr({
588 lineitem => $args{lineitem},
589 attr_type => $args{attr_type},
590 attr_name => $args{attr_name}
594 $attr->attr_value($args{attr_value});
595 return $attr if $mgr->editor->update_acq_lineitem_attr($attr);
600 $attr = Fieldmapper::acq::lineitem_attr->new;
601 $attr->$_($args{$_}) for keys %args;
603 unless($attr->definition) {
604 my $find = "search_acq_$attr_type";
605 my $attr_def_id = $mgr->editor->$find({code => $attr->attr_name}, {idlist=>1})->[0] or return 0;
606 $attr->definition($attr_def_id);
608 return $mgr->editor->create_acq_lineitem_attr($attr);
612 # ----------------------------------------------------------------------------
614 # ----------------------------------------------------------------------------
615 sub create_lineitem_debits {
616 my ($mgr, $li, $dry_run) = @_;
618 unless($li->estimated_unit_price) {
619 $mgr->editor->event(OpenILS::Event->new('ACQ_LINEITEM_NO_PRICE', payload => $li->id));
620 $mgr->editor->rollback;
624 unless($li->provider) {
625 $mgr->editor->event(OpenILS::Event->new('ACQ_LINEITEM_NO_PROVIDER', payload => $li->id));
626 $mgr->editor->rollback;
630 my $lid_ids = $mgr->editor->search_acq_lineitem_detail(
631 {lineitem => $li->id},
635 for my $lid_id (@$lid_ids) {
637 my $lid = $mgr->editor->retrieve_acq_lineitem_detail([
640 flesh_fields => {acqlid => ['fund']}
644 create_lineitem_detail_debit($mgr, $li, $lid, $dry_run) or return 0;
653 sub create_lineitem_detail_debit {
654 my ($mgr, $li, $lid, $dry_run, $no_translate) = @_;
656 # don't create the debit if one already exists
657 return $mgr->editor->retrieve_acq_fund_debit($lid->fund_debit) if $lid->fund_debit;
659 my $li_id = ref($li) ? $li->id : $li;
661 unless(ref $li and ref $li->provider) {
662 $li = $mgr->editor->retrieve_acq_lineitem([
665 flesh_fields => {jub => ['provider']},
671 $lid->fund($mgr->editor->retrieve_acq_fund($lid->fund)) unless(ref $lid->fund);
673 $lid = $mgr->editor->retrieve_acq_lineitem_detail([
676 flesh_fields => {acqlid => ['fund']}
681 unless ($lid->fund) {
683 new OpenILS::Event("ACQ_FUND_NOT_FOUND") # close enough
688 my $amount = $li->estimated_unit_price;
689 if($li->provider->currency_type ne $lid->fund->currency_type and !$no_translate) {
691 # At Fund debit creation time, translate into the currency of the fund
692 # TODO: org setting to disable automatic currency conversion at debit create time?
694 $amount = $mgr->editor->json_query({
696 'acq.exchange_ratio',
697 $li->provider->currency_type, # source currency
698 $lid->fund->currency_type, # destination currency
699 $li->estimated_unit_price # source amount
701 })->[0]->{'acq.exchange_ratio'};
704 my $debit = create_fund_debit(
707 fund => $lid->fund->id,
708 origin_amount => $li->estimated_unit_price,
709 origin_currency_type => $li->provider->currency_type,
713 $lid->fund_debit($debit->id);
714 $lid->fund($lid->fund->id);
715 $mgr->editor->update_acq_lineitem_detail($lid) or return 0;
720 __PACKAGE__->register_method(
721 "method" => "fund_exceeds_balance_percent_api",
722 "api_name" => "open-ils.acq.fund.check_balance_percentages",
724 "desc" => q/Determine whether a given fund exceeds its defined
725 "balance stop and warning percentages"/,
727 {"desc" => "Authentication token", "type" => "string"},
728 {"desc" => "Fund ID", "type" => "number"},
729 {"desc" => "Theoretical debit amount (optional)",
732 "return" => {"desc" => q/An array of two values, for stop and warning,
733 in that order: 1 if fund exceeds that balance percentage, else 0/}
737 sub fund_exceeds_balance_percent_api {
738 my ($self, $conn, $auth, $fund_id, $debit_amount) = @_;
742 my $e = new_editor("authtoken" => $auth);
743 return $e->die_event unless $e->checkauth;
745 my $fund = $e->retrieve_acq_fund($fund_id) or return $e->die_event;
746 return $e->die_event unless $e->allowed("VIEW_FUND", $fund->org);
749 fund_exceeds_balance_percent($fund, $debit_amount, $e, "stop"),
750 fund_exceeds_balance_percent($fund, $debit_amount, $e, "warning")
757 sub fund_exceeds_balance_percent {
758 my ($fund, $debit_amount, $e, $which) = @_;
760 my ($method_name, $event_name) = @{{
762 "balance_warning_percent", "ACQ_FUND_EXCEEDS_WARN_PERCENT"
765 "balance_stop_percent", "ACQ_FUND_EXCEEDS_STOP_PERCENT"
769 if ($fund->$method_name) {
771 $e->search_acq_fund_combined_balance({"fund" => $fund->id})->[0];
773 $e->search_acq_fund_allocation_total({"fund" => $fund->id})->[0];
775 $balance = ($balance) ? $balance->amount : 0;
776 $allocations = ($allocations) ? $allocations->amount : 0;
779 $allocations == 0 || # if no allocations were ever made, assume we have hit the stop percent
780 ((($allocations - $balance + $debit_amount) / $allocations) * 100) > $fund->$method_name
782 $logger->info("fund would hit a limit: " . $fund->id . ", $balance, $debit_amount, $allocations, $method_name");
787 "fund" => $fund, "debit_amount" => $debit_amount
797 # ----------------------------------------------------------------------------
799 # ----------------------------------------------------------------------------
800 sub create_fund_debit {
801 my($mgr, $dry_run, %args) = @_;
803 # Verify the fund is not being spent beyond the hard stop amount
804 my $fund = $mgr->editor->retrieve_acq_fund($args{fund}) or return 0;
807 fund_exceeds_balance_percent(
808 $fund, $args{"amount"}, $mgr->editor, "stop"
811 $dry_run and fund_exceeds_balance_percent(
812 $fund, $args{"amount"}, $mgr->editor, "warning"
815 my $debit = Fieldmapper::acq::fund_debit->new;
816 $debit->debit_type('purchase');
817 $debit->encumbrance('t');
818 $debit->$_($args{$_}) for keys %args;
820 $mgr->add_debit($debit->amount);
821 return $mgr->editor->create_acq_fund_debit($debit);
825 # ----------------------------------------------------------------------------
827 # ----------------------------------------------------------------------------
828 sub create_picklist {
829 my($mgr, %args) = @_;
830 my $picklist = Fieldmapper::acq::picklist->new;
831 $picklist->creator($mgr->editor->requestor->id);
832 $picklist->owner($picklist->creator);
833 $picklist->editor($picklist->creator);
834 $picklist->create_time('now');
835 $picklist->edit_time('now');
836 $picklist->org_unit($mgr->editor->requestor->ws_ou);
837 $picklist->owner($mgr->editor->requestor->id);
838 $picklist->$_($args{$_}) for keys %args;
840 $mgr->picklist($picklist);
841 return $mgr->editor->create_acq_picklist($picklist);
844 sub update_picklist {
845 my($mgr, $picklist) = @_;
846 $picklist = $mgr->editor->retrieve_acq_picklist($picklist) unless ref $picklist;
847 $picklist->edit_time('now');
848 $picklist->editor($mgr->editor->requestor->id);
849 if ($mgr->editor->update_acq_picklist($picklist)) {
850 $picklist = $mgr->editor->retrieve_acq_picklist($mgr->editor->data);
851 $mgr->picklist($picklist);
858 sub delete_picklist {
859 my($mgr, $picklist) = @_;
860 $picklist = $mgr->editor->retrieve_acq_picklist($picklist) unless ref $picklist;
862 # delete all 'new' lineitems
863 my $li_ids = $mgr->editor->search_acq_lineitem(
865 picklist => $picklist->id,
866 "-or" => {state => "new", purchase_order => undef}
870 for my $li_id (@$li_ids) {
871 my $li = $mgr->editor->retrieve_acq_lineitem($li_id);
872 return 0 unless delete_lineitem($mgr, $li);
876 # detach all non-'new' lineitems
877 $li_ids = $mgr->editor->search_acq_lineitem({picklist => $picklist->id, state => {'!=' => 'new'}}, {idlist => 1});
878 for my $li_id (@$li_ids) {
879 my $li = $mgr->editor->retrieve_acq_lineitem($li_id);
881 return 0 unless update_lineitem($mgr, $li);
885 # remove any picklist-specific object perms
886 my $ops = $mgr->editor->search_permission_usr_object_perm_map({object_type => 'acqpl', object_id => ''.$picklist->id});
888 return 0 unless $mgr->editor->delete_usr_object_perm_map($op);
891 return $mgr->editor->delete_acq_picklist($picklist);
894 # ----------------------------------------------------------------------------
896 # ----------------------------------------------------------------------------
897 sub update_purchase_order {
899 $po = $mgr->editor->retrieve_acq_purchase_order($po) unless ref $po;
900 $po->editor($mgr->editor->requestor->id);
901 $po->edit_time('now');
902 $mgr->purchase_order($po);
903 return $mgr->editor->retrieve_acq_purchase_order($mgr->editor->data)
904 if $mgr->editor->update_acq_purchase_order($po);
908 sub create_purchase_order {
909 my($mgr, %args) = @_;
911 # verify the chosen provider is still active
912 my $provider = $mgr->editor->retrieve_acq_provider($args{provider}) or return 0;
913 unless($U->is_true($provider->active)) {
914 $logger->error("provider is not active. cannot create PO");
915 $mgr->editor->event(OpenILS::Event->new('ACQ_PROVIDER_INACTIVE'));
919 my $po = Fieldmapper::acq::purchase_order->new;
920 $po->creator($mgr->editor->requestor->id);
921 $po->editor($mgr->editor->requestor->id);
922 $po->owner($mgr->editor->requestor->id);
923 $po->edit_time('now');
924 $po->create_time('now');
925 $po->state('pending');
926 $po->ordering_agency($mgr->editor->requestor->ws_ou);
927 $po->$_($args{$_}) for keys %args;
929 $mgr->purchase_order($po);
930 return $mgr->editor->create_acq_purchase_order($po);
933 # ----------------------------------------------------------------------------
934 # if all of the lineitems for this PO are received,
935 # mark the PO as received
936 # ----------------------------------------------------------------------------
937 sub check_purchase_order_received {
938 my($mgr, $po_id) = @_;
940 my $non_recv_li = $mgr->editor->search_acq_lineitem(
941 { purchase_order => $po_id,
942 state => {'!=' => 'received'}
945 my $po = $mgr->editor->retrieve_acq_purchase_order($po_id);
946 return $po if @$non_recv_li;
948 $po->state('received');
949 return update_purchase_order($mgr, $po);
953 # ----------------------------------------------------------------------------
954 # Bib, Callnumber, and Copy data
955 # ----------------------------------------------------------------------------
957 sub create_lineitem_assets {
958 my($mgr, $li_id) = @_;
961 my $li = $mgr->editor->retrieve_acq_lineitem([
964 flesh_fields => {jub => ['purchase_order', 'attributes']}
968 # -----------------------------------------------------------------
969 # first, create the bib record if necessary
970 # -----------------------------------------------------------------
972 unless($li->eg_bib_id) {
973 create_bib($mgr, $li) or return 0;
978 # -----------------------------------------------------------------
979 # The lineitem is going live, promote user request holds to real holds
980 # -----------------------------------------------------------------
981 promote_lineitem_holds($mgr, $li) or return 0;
983 my $li_details = $mgr->editor->search_acq_lineitem_detail({lineitem => $li_id}, {idlist=>1});
985 # -----------------------------------------------------------------
986 # for each lineitem_detail, create the volume if necessary, create
987 # a copy, and link them all together.
988 # -----------------------------------------------------------------
990 for my $lid_id (@{$li_details}) {
992 my $lid = $mgr->editor->retrieve_acq_lineitem_detail($lid_id) or return 0;
993 next if $lid->eg_copy_id;
995 # use the same callnumber label for all items within this lineitem
996 $lid->cn_label($first_cn) if $first_cn and not $lid->cn_label;
998 # apply defaults if necessary
999 return 0 unless complete_lineitem_detail($mgr, $lid);
1001 $first_cn = $lid->cn_label unless $first_cn;
1003 my $org = $lid->owning_lib;
1004 my $label = $lid->cn_label;
1005 my $bibid = $li->eg_bib_id;
1007 my $volume = $mgr->cache($org, "cn.$bibid.$label");
1009 $volume = create_volume($mgr, $li, $lid) or return 0;
1010 $mgr->cache($org, "cn.$bibid.$label", $volume);
1012 create_copy($mgr, $volume, $lid, $li) or return 0;
1015 return { li => $li, new_bib => $new_bib };
1021 my $record = OpenILS::Application::Cat::BibCommon->biblio_record_xml_import(
1026 1, # override tcn collisions
1029 if($U->event_code($record)) {
1030 $mgr->editor->event($record);
1031 $mgr->editor->rollback;
1035 $li->eg_bib_id($record->id);
1037 return update_lineitem($mgr, $li);
1041 my($mgr, $li, $lid) = @_;
1043 my ($volume, $evt) =
1044 OpenILS::Application::Cat::AssetCommon->find_or_create_volume(
1052 $mgr->editor->event($evt);
1060 my($mgr, $volume, $lid, $li) = @_;
1061 my $copy = Fieldmapper::asset::copy->new;
1063 $copy->loan_duration(2);
1064 $copy->fine_level(2);
1065 $copy->status(($lid->recv_time) ? OILS_COPY_STATUS_IN_PROCESS : OILS_COPY_STATUS_ON_ORDER);
1066 $copy->barcode($lid->barcode);
1067 $copy->location($lid->location);
1068 $copy->call_number($volume->id);
1069 $copy->circ_lib($volume->owning_lib);
1070 $copy->circ_modifier($lid->circ_modifier);
1072 # AKA list price. We might need a $li->list_price field since
1073 # estimated price is not necessarily the same as list price
1074 $copy->price($li->estimated_unit_price);
1076 my $evt = OpenILS::Application::Cat::AssetCommon->create_copy($mgr->editor, $volume, $copy);
1078 $mgr->editor->event($evt);
1083 $lid->eg_copy_id($copy->id);
1084 $mgr->editor->update_acq_lineitem_detail($lid) or return 0;
1092 # ----------------------------------------------------------------------------
1093 # Workflow: Build a selection list from a Z39.50 search
1094 # ----------------------------------------------------------------------------
1096 __PACKAGE__->register_method(
1097 method => 'zsearch',
1098 api_name => 'open-ils.acq.picklist.search.z3950',
1101 desc => 'Performs a z3950 federated search and creates a picklist and associated lineitems',
1103 {desc => 'Authentication token', type => 'string'},
1104 {desc => 'Search definition', type => 'object'},
1105 {desc => 'Picklist name, optional', type => 'string'},
1111 my($self, $conn, $auth, $search, $name, $options) = @_;
1112 my $e = new_editor(authtoken=>$auth);
1113 return $e->event unless $e->checkauth;
1114 return $e->event unless $e->allowed('CREATE_PICKLIST');
1116 $search->{limit} ||= 10;
1119 my $ses = OpenSRF::AppSession->create('open-ils.search');
1120 my $req = $ses->request('open-ils.search.z3950.search_class', $auth, $search);
1125 while(my $resp = $req->recv(timeout=>60)) {
1128 my $e = new_editor(requestor=>$e->requestor, xact=>1);
1129 $mgr = OpenILS::Application::Acq::BatchManager->new(editor => $e, conn => $conn);
1130 $picklist = zsearch_build_pl($mgr, $name);
1134 my $result = $resp->content;
1135 my $count = $result->{count} || 0;
1136 $mgr->total( (($count < $search->{limit}) ? $count : $search->{limit})+1 );
1138 for my $rec (@{$result->{records}}) {
1140 my $li = create_lineitem($mgr,
1141 picklist => $picklist->id,
1142 source_label => $result->{service},
1143 marc => $rec->{marcxml},
1144 eg_bib_id => $rec->{bibid}
1147 if($$options{respond_li}) {
1148 $li->attributes($mgr->editor->search_acq_lineitem_attr({lineitem => $li->id}))
1149 if $$options{flesh_attrs};
1150 $li->clear_marc if $$options{clear_marc};
1151 $mgr->respond(lineitem => $li);
1158 $mgr->editor->commit;
1159 return $mgr->respond_complete;
1162 sub zsearch_build_pl {
1163 my($mgr, $name) = @_;
1166 my $picklist = $mgr->editor->search_acq_picklist({
1167 owner => $mgr->editor->requestor->id,
1171 if($name eq '' and $picklist) {
1172 return 0 unless delete_picklist($mgr, $picklist);
1176 return update_picklist($mgr, $picklist) if $picklist;
1177 return create_picklist($mgr, name => $name);
1181 # ----------------------------------------------------------------------------
1182 # Workflow: Build a selection list / PO by importing a batch of MARC records
1183 # ----------------------------------------------------------------------------
1185 __PACKAGE__->register_method(
1186 method => 'upload_records',
1187 api_name => 'open-ils.acq.process_upload_records',
1191 sub upload_records {
1192 my($self, $conn, $auth, $key) = @_;
1194 my $e = new_editor(authtoken => $auth, xact => 1);
1195 return $e->die_event unless $e->checkauth;
1196 my $mgr = OpenILS::Application::Acq::BatchManager->new(editor => $e, conn => $conn);
1198 my $cache = OpenSRF::Utils::Cache->new;
1200 my $data = $cache->get_cache("vandelay_import_spool_$key");
1201 my $purpose = $data->{purpose};
1202 my $filename = $data->{path};
1203 my $provider = $data->{provider};
1204 my $picklist = $data->{picklist};
1205 my $create_po = $data->{create_po};
1206 my $activate_po = $data->{activate_po};
1207 my $ordering_agency = $data->{ordering_agency};
1208 my $create_assets = $data->{create_assets};
1212 unless(-r $filename) {
1213 $logger->error("unable to read MARC file $filename");
1215 return OpenILS::Event->new('FILE_UPLOAD_ERROR', payload => {filename => $filename});
1218 $provider = $e->retrieve_acq_provider($provider) or return $e->die_event;
1221 $picklist = $e->retrieve_acq_picklist($picklist) or return $e->die_event;
1222 if($picklist->owner != $e->requestor->id) {
1223 return $e->die_event unless
1224 $e->allowed('CREATE_PICKLIST', $picklist->org_unit, $picklist);
1226 $mgr->picklist($picklist);
1231 $po = create_purchase_order($mgr,
1232 ordering_agency => $ordering_agency,
1233 provider => $provider->id,
1234 state => 'pending' # will be updated later if activated
1235 ) or return $mgr->editor->die_event;
1238 $logger->info("acq processing MARC file=$filename");
1240 my $batch = new MARC::Batch ('USMARC', $filename);
1248 my ($err, $xml, $r);
1253 } catch Error with {
1255 $logger->warn("Proccessing of record $count in set $key failed with error $err. Skipping this record");
1262 $xml = clean_marc($r);
1263 } catch Error with {
1265 $logger->warn("Proccessing XML of record $count in set $key failed with error $err. Skipping this record");
1268 next if $err or not $xml;
1271 source_label => $provider->code,
1272 provider => $provider->id,
1276 $args{picklist} = $picklist->id if $picklist;
1278 $args{purchase_order} = $po->id;
1279 $args{state} = 'pending-order';
1282 my $li = create_lineitem($mgr, %args) or return $mgr->editor->die_event;
1284 $li->provider($provider); # flesh it, we'll need it later
1286 import_lineitem_details($mgr, $ordering_agency, $li) or return $mgr->editor->die_event;
1289 push(@li_list, $li->id);
1293 my $die_event = activate_purchase_order_impl($mgr, $po->id) if $po and $activate_po;
1294 return $die_event if $die_event;
1298 $cache->delete_cache('vandelay_import_spool_' . $key);
1300 if ($create_assets) {
1301 create_lineitem_list_assets($mgr, \@li_list) or return $e->die_event;
1304 return $mgr->respond_complete;
1307 sub import_lineitem_details {
1308 my($mgr, $ordering_agency, $li) = @_;
1310 my $holdings = $mgr->editor->json_query({from => ['acq.extract_provider_holding_data', $li->id]});
1311 return 1 unless @$holdings;
1312 my $org_path = $U->get_org_ancestors($ordering_agency);
1313 $org_path = [ reverse (@$org_path) ];
1319 # create a lineitem detail for each copy in the data
1321 my $compiled = extract_lineitem_detail_data($mgr, $org_path, $holdings, $idx);
1322 last unless defined $compiled;
1323 return 0 unless $compiled;
1325 # this takes the price of the last copy and uses it as the lineitem price
1326 # need to determine if a given record would include different prices for the same item
1327 $price = $$compiled{estimated_price};
1329 last unless $$compiled{quantity};
1331 for(1..$$compiled{quantity}) {
1332 my $lid = create_lineitem_detail(
1334 lineitem => $li->id,
1335 owning_lib => $$compiled{owning_lib},
1336 cn_label => $$compiled{call_number},
1337 fund => $$compiled{fund},
1338 circ_modifier => $$compiled{circ_modifier},
1339 note => $$compiled{note},
1340 location => $$compiled{copy_location},
1341 collection_code => $$compiled{collection_code},
1342 barcode => $$compiled{barcode}
1350 $li->estimated_unit_price($price);
1351 update_lineitem($mgr, $li) or return 0;
1355 # return hash on success, 0 on error, undef on no more holdings
1356 sub extract_lineitem_detail_data {
1357 my($mgr, $org_path, $holdings, $index) = @_;
1359 my @data_list = grep { $_->{holding} eq $index } @$holdings;
1360 return undef unless @data_list;
1362 my %compiled = map { $_->{attr} => $_->{data} } @data_list;
1363 my $base_org = $$org_path[0];
1367 $logger->error("Item import extraction error: $msg");
1368 $logger->error('Holdings Data: ' . OpenSRF::Utils::JSON->perl2JSON(\%compiled));
1369 $mgr->editor->rollback;
1370 $mgr->editor->event(OpenILS::Event->new('ACQ_IMPORT_ERROR', payload => $msg));
1374 # ---------------------------------------------------------------------
1376 if(my $code = $compiled{fund_code}) {
1378 my $fund = $mgr->cache($base_org, "fund.$code");
1380 # search up the org tree for the most appropriate fund
1381 for my $org (@$org_path) {
1382 $fund = $mgr->editor->search_acq_fund(
1383 {org => $org, code => $code, year => DateTime->now->year}, {idlist => 1})->[0];
1387 return $killme->("no fund with code $code at orgs [@$org_path]") unless $fund;
1388 $compiled{fund} = $fund;
1389 $mgr->cache($base_org, "fund.$code", $fund);
1393 # ---------------------------------------------------------------------
1395 if(my $sn = $compiled{owning_lib}) {
1396 my $org_id = $mgr->cache($base_org, "orgsn.$sn") ||
1397 $mgr->editor->search_actor_org_unit({shortname => $sn}, {idlist => 1})->[0];
1398 return $killme->("invalid owning_lib defined: $sn") unless $org_id;
1399 $compiled{owning_lib} = $org_id;
1400 $mgr->cache($$org_path[0], "orgsn.$sn", $org_id);
1404 # ---------------------------------------------------------------------
1406 my $code = $compiled{circ_modifier};
1410 # verify this is a valid circ modifier
1411 return $killme->("invlalid circ_modifier $code") unless
1412 defined $mgr->cache($base_org, "mod.$code") or
1413 $mgr->editor->retrieve_config_circ_modifier($code);
1415 # if valid, cache for future tests
1416 $mgr->cache($base_org, "mod.$code", $code);
1419 $compiled{circ_modifier} = get_default_circ_modifier($mgr, $base_org);
1423 # ---------------------------------------------------------------------
1425 if( my $name = $compiled{copy_location}) {
1426 my $loc = $mgr->cache($base_org, "copy_loc.$name");
1428 for my $org (@$org_path) {
1429 $loc = $mgr->editor->search_asset_copy_location(
1430 {owning_lib => $org, name => $name}, {idlist => 1})->[0];
1434 return $killme->("Invalid copy location $name") unless $loc;
1435 $compiled{copy_location} = $loc;
1436 $mgr->cache($base_org, "copy_loc.$name", $loc);
1444 # ----------------------------------------------------------------------------
1445 # Workflow: Given an existing purchase order, import/create the bibs,
1446 # callnumber and copy objects
1447 # ----------------------------------------------------------------------------
1449 __PACKAGE__->register_method(
1450 method => 'create_po_assets',
1451 api_name => 'open-ils.acq.purchase_order.assets.create',
1453 desc => q/Creates assets for each lineitem in the purchase order/,
1455 {desc => 'Authentication token', type => 'string'},
1456 {desc => 'The purchase order id', type => 'number'},
1458 return => {desc => 'Streams a total versus completed counts object, event on error'}
1462 sub create_po_assets {
1463 my($self, $conn, $auth, $po_id) = @_;
1465 my $e = new_editor(authtoken=>$auth, xact=>1);
1466 return $e->die_event unless $e->checkauth;
1467 my $mgr = OpenILS::Application::Acq::BatchManager->new(editor => $e, conn => $conn);
1469 my $po = $e->retrieve_acq_purchase_order($po_id) or return $e->die_event;
1471 my $li_ids = $e->search_acq_lineitem({purchase_order => $po_id}, {idlist => 1});
1473 # it's ugly, but it's fast. Get the total count of lineitem detail objects to process
1474 my $lid_total = $e->json_query({
1475 select => { acqlid => [{aggregate => 1, transform => 'count', column => 'id'}] },
1481 join => {acqpo => {fkey => 'purchase_order', field => 'id'}}
1485 where => {'+acqpo' => {id => $po_id}}
1488 $mgr->total(scalar(@$li_ids) + $lid_total);
1490 create_lineitem_list_assets($mgr, $li_ids) or return $e->die_event;
1493 update_purchase_order($mgr, $po) or return $e->die_event;
1496 return $mgr->respond_complete;
1501 __PACKAGE__->register_method(
1502 method => 'create_purchase_order_api',
1503 api_name => 'open-ils.acq.purchase_order.create',
1505 desc => 'Creates a new purchase order',
1507 {desc => 'Authentication token', type => 'string'},
1508 {desc => 'purchase_order to create', type => 'object'}
1510 return => {desc => 'The purchase order id, Event on failure'}
1514 sub create_purchase_order_api {
1515 my($self, $conn, $auth, $po, $args) = @_;
1518 my $e = new_editor(xact=>1, authtoken=>$auth);
1519 return $e->die_event unless $e->checkauth;
1520 return $e->die_event unless $e->allowed('CREATE_PURCHASE_ORDER', $po->ordering_agency);
1521 my $mgr = OpenILS::Application::Acq::BatchManager->new(editor => $e, conn => $conn);
1524 my %pargs = (ordering_agency => $e->requestor->ws_ou); # default
1525 $pargs{provider} = $po->provider if $po->provider;
1526 $pargs{ordering_agency} = $po->ordering_agency if $po->ordering_agency;
1527 $pargs{prepayment_required} = $po->prepayment_required if $po->prepayment_required;
1529 $po = create_purchase_order($mgr, %pargs) or return $e->die_event;
1531 my $li_ids = $$args{lineitems};
1535 for my $li_id (@$li_ids) {
1537 my $li = $e->retrieve_acq_lineitem([
1539 {flesh => 1, flesh_fields => {jub => ['attributes']}}
1540 ]) or return $e->die_event;
1542 $li->provider($po->provider);
1543 $li->purchase_order($po->id);
1544 $li->state('pending-order');
1545 update_lineitem($mgr, $li) or return $e->die_event;
1550 # commit before starting the asset creation
1553 if($li_ids and $$args{create_assets}) {
1554 create_lineitem_list_assets($mgr, $li_ids) or return $e->die_event;
1557 return $mgr->respond_complete;
1562 __PACKAGE__->register_method(
1563 method => 'update_lineitem_fund_batch',
1564 api_name => 'open-ils.acq.lineitem.fund.update.batch',
1567 desc => q/Given a set of lineitem IDS, updates the fund for all attached lineitem details/
1571 sub update_lineitem_fund_batch {
1572 my($self, $conn, $auth, $li_ids, $fund_id) = @_;
1573 my $e = new_editor(xact=>1, authtoken=>$auth);
1574 return $e->die_event unless $e->checkauth;
1575 my $mgr = OpenILS::Application::Acq::BatchManager->new(editor => $e, conn => $conn);
1576 for my $li_id (@$li_ids) {
1577 my ($li, $evt) = fetch_and_check_li($e, $li_id, 'write');
1578 return $evt if $evt;
1579 my $li_details = $e->search_acq_lineitem_detail({lineitem => $li_id});
1580 $_->fund($fund_id) and $_->ischanged(1) for @$li_details;
1581 $evt = lineitem_detail_CUD_batch($mgr, $li_details);
1582 return $evt if $evt;
1587 return $mgr->respond_complete;
1592 __PACKAGE__->register_method(
1593 method => 'lineitem_detail_CUD_batch_api',
1594 api_name => 'open-ils.acq.lineitem_detail.cud.batch',
1597 desc => q/Creates a new purchase order line item detail. / .
1598 q/Additionally creates the associated fund_debit/,
1600 {desc => 'Authentication token', type => 'string'},
1601 {desc => 'List of lineitem_details to create', type => 'array'},
1602 {desc => 'Create Debits. Used for creating post-po-asset-creation debits', type => 'bool'},
1604 return => {desc => 'Streaming response of current position in the array'}
1608 __PACKAGE__->register_method(
1609 method => 'lineitem_detail_CUD_batch_api',
1610 api_name => 'open-ils.acq.lineitem_detail.cud.batch.dry_run',
1614 Dry run version of open-ils.acq.lineitem_detail.cud.batch.
1615 In dry_run mode, updated fund_debit's the exceed the warning
1616 percent return an event.
1622 sub lineitem_detail_CUD_batch_api {
1623 my($self, $conn, $auth, $li_details, $create_debits) = @_;
1624 my $e = new_editor(xact=>1, authtoken=>$auth);
1625 return $e->die_event unless $e->checkauth;
1626 my $mgr = OpenILS::Application::Acq::BatchManager->new(editor => $e, conn => $conn);
1627 my $dry_run = ($self->api_name =~ /dry_run/o);
1628 my $evt = lineitem_detail_CUD_batch($mgr, $li_details, $create_debits, $dry_run);
1629 return $evt if $evt;
1631 return $mgr->respond_complete;
1635 sub lineitem_detail_CUD_batch {
1636 my($mgr, $li_details, $create_debits, $dry_run) = @_;
1638 $mgr->total(scalar(@$li_details));
1639 my $e = $mgr->editor;
1643 my $fund_cache = {};
1646 for my $lid (@$li_details) {
1648 unless($li = $li_cache{$lid->lineitem}) {
1649 ($li, $evt) = fetch_and_check_li($e, $lid->lineitem, 'write');
1650 return $evt if $evt;
1654 $lid = create_lineitem_detail($mgr, %{$lid->to_bare_hash}) or return $e->die_event;
1655 if($create_debits) {
1656 $li->provider($e->retrieve_acq_provider($li->provider)) or return $e->die_event;
1657 $lid->fund($e->retrieve_acq_fund($lid->fund)) or return $e->die_event;
1658 create_lineitem_detail_debit($mgr, $li, $lid, 0, 1) or return $e->die_event;
1661 } elsif($lid->ischanged) {
1662 return $evt if $evt = handle_changed_lid($e, $lid, $dry_run, $fund_cache);
1664 } elsif($lid->isdeleted) {
1665 delete_lineitem_detail($mgr, $lid) or return $e->die_event;
1668 $mgr->respond(li => $li);
1669 $li_cache{$lid->lineitem} = $li;
1675 sub handle_changed_lid {
1676 my($e, $lid, $dry_run, $fund_cache) = @_;
1678 my $orig_lid = $e->retrieve_acq_lineitem_detail($lid->id) or return $e->die_event;
1680 # updating the fund, so update the debit
1681 if($orig_lid->fund_debit and $orig_lid->fund != $lid->fund) {
1683 my $debit = $e->retrieve_acq_fund_debit($orig_lid->fund_debit);
1684 my $new_fund = $$fund_cache{$lid->fund} =
1685 $$fund_cache{$lid->fund} || $e->retrieve_acq_fund($lid->fund);
1687 # check the thresholds
1688 return $e->die_event if
1689 fund_exceeds_balance_percent($new_fund, $debit->amount, $e, "stop");
1690 return $e->die_event if $dry_run and
1691 fund_exceeds_balance_percent($new_fund, $debit->amount, $e, "warning");
1693 $debit->fund($new_fund->id);
1694 $e->update_acq_fund_debit($debit) or return $e->die_event;
1697 $e->update_acq_lineitem_detail($lid) or return $e->die_event;
1702 __PACKAGE__->register_method(
1703 method => 'receive_po_api',
1704 api_name => 'open-ils.acq.purchase_order.receive'
1707 sub receive_po_api {
1708 my($self, $conn, $auth, $po_id) = @_;
1709 my $e = new_editor(xact => 1, authtoken => $auth);
1710 return $e->die_event unless $e->checkauth;
1711 my $mgr = OpenILS::Application::Acq::BatchManager->new(editor => $e, conn => $conn);
1713 my $po = $e->retrieve_acq_purchase_order($po_id) or return $e->die_event;
1714 return $e->die_event unless $e->allowed('RECEIVE_PURCHASE_ORDER', $po->ordering_agency);
1716 my $li_ids = $e->search_acq_lineitem({purchase_order => $po_id}, {idlist => 1});
1718 for my $li_id (@$li_ids) {
1719 receive_lineitem($mgr, $li_id) or return $e->die_event;
1723 $po->state('received');
1724 update_purchase_order($mgr, $po) or return $e->die_event;
1727 return $mgr->respond_complete;
1731 # At the moment there's a lack of parallelism between the receive and unreceive
1732 # API methods for POs and the API methods for LIs and LIDs. The methods for
1733 # POs stream back objects as they act, whereas the methods for LIs and LIDs
1734 # atomically return an object that describes only what changed (in LIs and LIDs
1735 # themselves or in the objects to which to LIs and LIDs belong).
1737 # The methods for LIs and LIDs work the way they do to faciliate the UI's
1738 # maintaining correct information about the state of these things when a user
1739 # wants to receive or unreceive these objects without refreshing their whole
1740 # display. The UI feature for receiving and un-receiving a whole PO just
1741 # refreshes the whole display, so this absence of parallelism in the UI is also
1742 # relected in this module.
1744 # This could be neatened in the future by making POs receive and unreceive in
1745 # the same way the LIs and LIDs do.
1747 __PACKAGE__->register_method(
1748 method => 'receive_lineitem_detail_api',
1749 api_name => 'open-ils.acq.lineitem_detail.receive',
1751 desc => 'Mark a lineitem_detail as received',
1753 {desc => 'Authentication token', type => 'string'},
1754 {desc => 'lineitem detail ID', type => 'number'}
1757 "on success, object describing changes to LID and possibly " .
1758 "to LI and PO; on error, Event"
1763 sub receive_lineitem_detail_api {
1764 my($self, $conn, $auth, $lid_id) = @_;
1766 my $e = new_editor(xact=>1, authtoken=>$auth);
1767 return $e->die_event unless $e->checkauth;
1768 my $mgr = OpenILS::Application::Acq::BatchManager->new(editor => $e, conn => $conn);
1771 "flesh" => 2, "flesh_fields" => {
1772 "acqlid" => ["lineitem"], "jub" => ["purchase_order"]
1776 my $lid = $e->retrieve_acq_lineitem_detail([$lid_id, $fleshing]);
1778 return $e->die_event unless $e->allowed(
1779 'RECEIVE_PURCHASE_ORDER', $lid->lineitem->purchase_order->ordering_agency);
1782 my $recvd = receive_lineitem_detail($mgr, $lid_id) or return $e->die_event;
1784 # .. and re-retrieve
1785 $lid = $e->retrieve_acq_lineitem_detail([$lid_id, $fleshing]);
1787 # Now build result data structure.
1788 my $result = {"lid" => {$lid->id => {"recv_time" => $lid->recv_time}}};
1791 if ($recvd->class_name =~ /::purchase_order/) {
1792 $result->{"po"} = describe_affected_po($e, $recvd);
1794 $lid->lineitem->id => {"state" => $lid->lineitem->state}
1796 } elsif ($recvd->class_name =~ /::lineitem/) {
1797 $result->{"li"} = {$recvd->id => {"state" => $recvd->state}};
1801 describe_affected_po($e, $lid->lineitem->purchase_order);
1807 __PACKAGE__->register_method(
1808 method => 'receive_lineitem_api',
1809 api_name => 'open-ils.acq.lineitem.receive',
1811 desc => 'Mark a lineitem as received',
1813 {desc => 'Authentication token', type => 'string'},
1814 {desc => 'lineitem ID', type => 'number'}
1817 "on success, object describing changes to LI and possibly PO; " .
1823 sub receive_lineitem_api {
1824 my($self, $conn, $auth, $li_id) = @_;
1826 my $e = new_editor(xact=>1, authtoken=>$auth);
1827 return $e->die_event unless $e->checkauth;
1828 my $mgr = OpenILS::Application::Acq::BatchManager->new(editor => $e, conn => $conn);
1830 my $li = $e->retrieve_acq_lineitem([
1834 jub => ['purchase_order']
1837 ]) or return $e->die_event;
1839 return $e->die_event unless $e->allowed(
1840 'RECEIVE_PURCHASE_ORDER', $li->purchase_order->ordering_agency);
1842 my $res = receive_lineitem($mgr, $li_id) or return $e->die_event;
1844 $conn->respond_complete($res);
1845 $mgr->run_post_response_hooks;
1849 __PACKAGE__->register_method(
1850 method => 'rollback_receive_po_api',
1851 api_name => 'open-ils.acq.purchase_order.receive.rollback'
1854 sub rollback_receive_po_api {
1855 my($self, $conn, $auth, $po_id) = @_;
1856 my $e = new_editor(xact => 1, authtoken => $auth);
1857 return $e->die_event unless $e->checkauth;
1858 my $mgr = OpenILS::Application::Acq::BatchManager->new(editor => $e, conn => $conn);
1860 my $po = $e->retrieve_acq_purchase_order($po_id) or return $e->die_event;
1861 return $e->die_event unless $e->allowed('RECEIVE_PURCHASE_ORDER', $po->ordering_agency);
1863 my $li_ids = $e->search_acq_lineitem({purchase_order => $po_id}, {idlist => 1});
1865 for my $li_id (@$li_ids) {
1866 rollback_receive_lineitem($mgr, $li_id) or return $e->die_event;
1870 $po->state('on-order');
1871 update_purchase_order($mgr, $po) or return $e->die_event;
1874 return $mgr->respond_complete;
1878 __PACKAGE__->register_method(
1879 method => 'rollback_receive_lineitem_detail_api',
1880 api_name => 'open-ils.acq.lineitem_detail.receive.rollback',
1882 desc => 'Mark a lineitem_detail as Un-received',
1884 {desc => 'Authentication token', type => 'string'},
1885 {desc => 'lineitem detail ID', type => 'number'}
1888 "on success, object describing changes to LID and possibly " .
1889 "to LI and PO; on error, Event"
1894 sub rollback_receive_lineitem_detail_api {
1895 my($self, $conn, $auth, $lid_id) = @_;
1897 my $e = new_editor(xact=>1, authtoken=>$auth);
1898 return $e->die_event unless $e->checkauth;
1899 my $mgr = OpenILS::Application::Acq::BatchManager->new(editor => $e, conn => $conn);
1901 my $lid = $e->retrieve_acq_lineitem_detail([
1905 acqlid => ['lineitem'],
1906 jub => ['purchase_order']
1910 my $li = $lid->lineitem;
1911 my $po = $li->purchase_order;
1913 return $e->die_event unless $e->allowed('RECEIVE_PURCHASE_ORDER', $po->ordering_agency);
1917 my $recvd = rollback_receive_lineitem_detail($mgr, $lid_id)
1918 or return $e->die_event;
1921 $result->{"lid"} = {$recvd->id => {"recv_time" => $recvd->recv_time}};
1923 $result->{"lid"} = {$lid->id => {"recv_time" => $lid->recv_time}};
1926 if ($li->state eq "received") {
1927 $li->state("on-order");
1928 $li = update_lineitem($mgr, $li) or return $e->die_event;
1929 $result->{"li"} = {$li->id => {"state" => $li->state}};
1932 if ($po->state eq "received") {
1933 $po->state("on-order");
1934 $po = update_purchase_order($mgr, $po) or return $e->die_event;
1936 $result->{"po"} = describe_affected_po($e, $po);
1938 $e->commit and return $result or return $e->die_event;
1941 __PACKAGE__->register_method(
1942 method => 'rollback_receive_lineitem_api',
1943 api_name => 'open-ils.acq.lineitem.receive.rollback',
1945 desc => 'Mark a lineitem as Un-received',
1947 {desc => 'Authentication token', type => 'string'},
1948 {desc => 'lineitem ID', type => 'number'}
1951 "on success, object describing changes to LI and possibly PO; " .
1957 sub rollback_receive_lineitem_api {
1958 my($self, $conn, $auth, $li_id) = @_;
1960 my $e = new_editor(xact=>1, authtoken=>$auth);
1961 return $e->die_event unless $e->checkauth;
1962 my $mgr = OpenILS::Application::Acq::BatchManager->new(editor => $e, conn => $conn);
1964 my $li = $e->retrieve_acq_lineitem([
1966 "flesh" => 1, "flesh_fields" => {"jub" => ["purchase_order"]}
1969 my $po = $li->purchase_order;
1971 return $e->die_event unless $e->allowed('RECEIVE_PURCHASE_ORDER', $po->ordering_agency);
1973 $li = rollback_receive_lineitem($mgr, $li_id) or return $e->die_event;
1975 my $result = {"li" => {$li->id => {"state" => $li->state}}};
1976 if ($po->state eq "received") {
1977 $po->state("on-order");
1978 $po = update_purchase_order($mgr, $po) or return $e->die_event;
1980 $result->{"po"} = describe_affected_po($e, $po);
1982 $e->commit and return $result or return $e->die_event;
1986 __PACKAGE__->register_method(
1987 method => 'set_lineitem_price_api',
1988 api_name => 'open-ils.acq.lineitem.price.set',
1990 desc => 'Set lineitem price. If debits already exist, update them as well',
1992 {desc => 'Authentication token', type => 'string'},
1993 {desc => 'lineitem ID', type => 'number'}
1995 return => {desc => 'status blob, Event on error'}
1999 sub set_lineitem_price_api {
2000 my($self, $conn, $auth, $li_id, $price) = @_;
2002 my $e = new_editor(xact=>1, authtoken=>$auth);
2003 return $e->die_event unless $e->checkauth;
2004 my $mgr = OpenILS::Application::Acq::BatchManager->new(editor => $e, conn => $conn);
2006 my ($li, $evt) = fetch_and_check_li($e, $li_id, 'write');
2007 return $evt if $evt;
2009 $li->estimated_unit_price($price);
2010 update_lineitem($mgr, $li) or return $e->die_event;
2012 my $lid_ids = $e->search_acq_lineitem_detail(
2013 {lineitem => $li_id, fund_debit => {'!=' => undef}},
2017 for my $lid_id (@$lid_ids) {
2019 my $lid = $e->retrieve_acq_lineitem_detail([
2021 flesh => 1, flesh_fields => {acqlid => ['fund', 'fund_debit']}}
2024 $lid->fund_debit->amount($price);
2025 $e->update_acq_fund_debit($lid->fund_debit) or return $e->die_event;
2031 return $mgr->respond_complete;
2035 __PACKAGE__->register_method(
2036 method => 'clone_picklist_api',
2037 api_name => 'open-ils.acq.picklist.clone',
2039 desc => 'Clones a picklist, including lineitem and lineitem details',
2041 {desc => 'Authentication token', type => 'string'},
2042 {desc => 'Picklist ID', type => 'number'},
2043 {desc => 'New Picklist Name', type => 'string'}
2045 return => {desc => 'status blob, Event on error'}
2049 sub clone_picklist_api {
2050 my($self, $conn, $auth, $pl_id, $name) = @_;
2052 my $e = new_editor(xact=>1, authtoken=>$auth);
2053 return $e->die_event unless $e->checkauth;
2054 my $mgr = OpenILS::Application::Acq::BatchManager->new(editor => $e, conn => $conn);
2056 my $old_pl = $e->retrieve_acq_picklist($pl_id);
2057 my $new_pl = create_picklist($mgr, %{$old_pl->to_bare_hash}, name => $name) or return $e->die_event;
2059 my $li_ids = $e->search_acq_lineitem({picklist => $pl_id}, {idlist => 1});
2061 for my $li_id (@$li_ids) {
2063 # copy the lineitems
2064 my $li = $e->retrieve_acq_lineitem($li_id);
2065 my $new_li = create_lineitem($mgr, %{$li->to_bare_hash}, picklist => $new_pl->id) or return $e->die_event;
2067 my $lid_ids = $e->search_acq_lineitem_detail({lineitem => $li_id}, {idlist => 1});
2068 for my $lid_id (@$lid_ids) {
2070 # copy the lineitem details
2071 my $lid = $e->retrieve_acq_lineitem_detail($lid_id);
2072 create_lineitem_detail($mgr, %{$lid->to_bare_hash}, lineitem => $new_li->id) or return $e->die_event;
2079 return $mgr->respond_complete;
2083 __PACKAGE__->register_method(
2084 method => 'merge_picklist_api',
2085 api_name => 'open-ils.acq.picklist.merge',
2087 desc => 'Merges 2 or more picklists into a single list',
2089 {desc => 'Authentication token', type => 'string'},
2090 {desc => 'Lead Picklist ID', type => 'number'},
2091 {desc => 'List of subordinate picklist IDs', type => 'array'}
2093 return => {desc => 'status blob, Event on error'}
2097 sub merge_picklist_api {
2098 my($self, $conn, $auth, $lead_pl, $pl_list) = @_;
2100 my $e = new_editor(xact=>1, authtoken=>$auth);
2101 return $e->die_event unless $e->checkauth;
2102 my $mgr = OpenILS::Application::Acq::BatchManager->new(editor => $e, conn => $conn);
2104 # XXX perms on each picklist modified
2106 $lead_pl = $e->retrieve_acq_picklist($lead_pl) or return $e->die_event;
2107 # point all of the lineitems at the lead picklist
2108 my $li_ids = $e->search_acq_lineitem({picklist => $pl_list}, {idlist => 1});
2110 for my $li_id (@$li_ids) {
2111 my $li = $e->retrieve_acq_lineitem($li_id);
2112 $li->picklist($lead_pl);
2113 update_lineitem($mgr, $li) or return $e->die_event;
2117 # now delete the subordinate lists
2118 for my $pl_id (@$pl_list) {
2119 my $pl = $e->retrieve_acq_picklist($pl_id);
2120 $e->delete_acq_picklist($pl) or return $e->die_event;
2123 update_picklist($mgr, $lead_pl) or return $e->die_event;
2126 return $mgr->respond_complete;
2130 __PACKAGE__->register_method(
2131 method => 'delete_picklist_api',
2132 api_name => 'open-ils.acq.picklist.delete',
2134 desc => q/Deletes a picklist. It also deletes any lineitems in the "new" state. / .
2135 q/Other attached lineitems are detached/,
2137 {desc => 'Authentication token', type => 'string'},
2138 {desc => 'Picklist ID to delete', type => 'number'}
2140 return => {desc => '1 on success, Event on error'}
2144 sub delete_picklist_api {
2145 my($self, $conn, $auth, $picklist_id) = @_;
2146 my $e = new_editor(xact=>1, authtoken=>$auth);
2147 return $e->die_event unless $e->checkauth;
2148 my $mgr = OpenILS::Application::Acq::BatchManager->new(editor => $e, conn => $conn);
2149 my $pl = $e->retrieve_acq_picklist($picklist_id) or return $e->die_event;
2150 delete_picklist($mgr, $pl) or return $e->die_event;
2152 return $mgr->respond_complete;
2157 __PACKAGE__->register_method(
2158 method => 'activate_purchase_order',
2159 api_name => 'open-ils.acq.purchase_order.activate.dry_run'
2162 __PACKAGE__->register_method(
2163 method => 'activate_purchase_order',
2164 api_name => 'open-ils.acq.purchase_order.activate',
2166 desc => q/Activates a purchase order. This updates the status of the PO / .
2167 q/and Lineitems to 'on-order'. Activated PO's are ready for EDI delivery if appropriate./,
2169 {desc => 'Authentication token', type => 'string'},
2170 {desc => 'Purchase ID', type => 'number'}
2172 return => {desc => '1 on success, Event on error'}
2176 sub activate_purchase_order {
2177 my($self, $conn, $auth, $po_id) = @_;
2179 my $dry_run = ($self->api_name =~ /\.dry_run/) ? 1 : 0;
2180 my $e = new_editor(xact=>1, authtoken=>$auth);
2181 return $e->die_event unless $e->checkauth;
2182 my $mgr = OpenILS::Application::Acq::BatchManager->new(editor => $e, conn => $conn);
2183 my $die_event = activate_purchase_order_impl($mgr, $po_id, $dry_run);
2184 return $e->die_event if $die_event;
2190 $conn->respond_complete(1);
2191 $mgr->run_post_response_hooks;
2195 sub activate_purchase_order_impl {
2196 my ($mgr, $po_id, $dry_run) = @_;
2197 my $e = $mgr->editor;
2199 my $po = $e->retrieve_acq_purchase_order($po_id) or return $e->die_event;
2200 return $e->die_event unless $e->allowed('CREATE_PURCHASE_ORDER', $po->ordering_agency);
2202 my $provider = $e->retrieve_acq_provider($po->provider);
2204 $po->state('on-order');
2205 $po->order_date('now');
2206 update_purchase_order($mgr, $po) or return $e->die_event;
2210 purchase_order => $po_id,
2211 state => [qw/pending-order new order-ready/]
2216 while( my $li_id = $e->search_acq_lineitem($query, {idlist => 1})->[0] ) {
2220 $li = $e->retrieve_acq_lineitem($li_id);
2222 # can't activate a PO w/o assets. Create lineitem assets as necessary
2223 my $data = create_lineitem_assets($mgr, $li_id) or return $e->die_event;
2227 $li->state('on-order');
2228 $li->claim_policy($provider->default_claim_policy)
2229 if $provider->default_claim_policy and !$li->claim_policy;
2230 create_lineitem_debits($mgr, $li, $dry_run) or return $e->die_event;
2231 update_lineitem($mgr, $li) or return $e->die_event;
2232 $mgr->post_process( sub { create_lineitem_status_events($mgr, $li->id, 'aur.ordered'); });
2236 for my $po_item (@{$e->search_acq_po_item({purchase_order => $po_id})}) {
2238 my $debit = create_fund_debit(
2241 debit_type => 'direct_charge', # to match invoicing
2242 origin_amount => $po_item->estimated_cost,
2243 origin_currency_type => $e->retrieve_acq_fund($po_item->fund)->currency_type,
2244 amount => $po_item->estimated_cost,
2245 fund => $po_item->fund
2246 ) or return $e->die_event;
2247 $po_item->fund_debit($debit->id);
2248 $e->update_acq_po_item($po_item) or return $e->die_event;
2252 # tell the world we activated a PO
2253 $U->create_events_for_hook('acqpo.activated', $po, $po->ordering_agency) unless $dry_run;
2259 __PACKAGE__->register_method(
2260 method => 'split_purchase_order_by_lineitems',
2261 api_name => 'open-ils.acq.purchase_order.split_by_lineitems',
2263 desc => q/Splits a PO into many POs, 1 per lineitem. Only works for / .
2264 q/POs a) with more than one lineitems, and b) in the "pending" state./,
2266 {desc => 'Authentication token', type => 'string'},
2267 {desc => 'Purchase order ID', type => 'number'}
2269 return => {desc => 'list of new PO IDs on success, Event on error'}
2273 sub split_purchase_order_by_lineitems {
2274 my ($self, $conn, $auth, $po_id) = @_;
2276 my $e = new_editor("xact" => 1, "authtoken" => $auth);
2277 return $e->die_event unless $e->checkauth;
2279 my $po = $e->retrieve_acq_purchase_order([
2282 "flesh_fields" => {"acqpo" => [qw/lineitems notes/]}
2284 ]) or return $e->die_event;
2286 return $e->die_event
2287 unless $e->allowed("CREATE_PURCHASE_ORDER", $po->ordering_agency);
2289 unless ($po->state eq "pending") {
2291 return new OpenILS::Event("ACQ_PURCHASE_ORDER_TOO_LATE");
2294 unless (@{$po->lineitems} > 1) {
2296 return new OpenILS::Event("ACQ_PURCHASE_ORDER_TOO_SHORT");
2299 # To split an existing PO into many, it seems unwise to just delete the
2300 # original PO, so we'll instead detach all of the original POs' lineitems
2301 # but the first, then create new POs for each of the remaining LIs, and
2302 # then attach the LIs to their new POs.
2304 my @po_ids = ($po->id);
2305 my @moving_li = @{$po->lineitems};
2306 shift @moving_li; # discard first LI
2308 foreach my $li (@moving_li) {
2309 my $new_po = $po->clone;
2311 $new_po->clear_name;
2312 $new_po->creator($e->requestor->id);
2313 $new_po->editor($e->requestor->id);
2314 $new_po->owner($e->requestor->id);
2315 $new_po->edit_time("now");
2316 $new_po->create_time("now");
2318 $new_po = $e->create_acq_purchase_order($new_po);
2320 # Clone any notes attached to the old PO and attach to the new one.
2321 foreach my $note (@{$po->notes}) {
2322 my $new_note = $note->clone;
2323 $new_note->clear_id;
2324 $new_note->edit_time("now");
2325 $new_note->purchase_order($new_po->id);
2326 $e->create_acq_po_note($new_note);
2329 $li->edit_time("now");
2330 $li->purchase_order($new_po->id);
2331 $e->update_acq_lineitem($li);
2333 push @po_ids, $new_po->id;
2336 $po->edit_time("now");
2337 $e->update_acq_purchase_order($po);
2339 return \@po_ids if $e->commit;
2340 return $e->die_event;
2344 sub not_cancelable {
2346 (ref $o eq "HASH" and $o->{"textcode"} eq "ACQ_NOT_CANCELABLE");
2349 __PACKAGE__->register_method(
2350 method => "cancel_purchase_order_api",
2351 api_name => "open-ils.acq.purchase_order.cancel",
2353 desc => q/Cancels an on-order purchase order/,
2355 {desc => "Authentication token", type => "string"},
2356 {desc => "PO ID to cancel", type => "number"},
2357 {desc => "Cancel reason ID", type => "number"}
2359 return => {desc => q/Object describing changed POs, LIs and LIDs
2360 on success; Event on error./}
2364 sub cancel_purchase_order_api {
2365 my ($self, $conn, $auth, $po_id, $cancel_reason) = @_;
2367 my $e = new_editor("xact" => 1, "authtoken" => $auth);
2368 return $e->die_event unless $e->checkauth;
2369 my $mgr = new OpenILS::Application::Acq::BatchManager(
2370 "editor" => $e, "conn" => $conn
2373 $cancel_reason = $mgr->editor->retrieve_acq_cancel_reason($cancel_reason) or
2374 return new OpenILS::Event(
2375 "BAD_PARAMS", "note" => "Provide cancel reason ID"
2378 my $result = cancel_purchase_order($mgr, $po_id, $cancel_reason) or
2379 return $e->die_event;
2380 if (not_cancelable($result)) { # event not from CStoreEditor
2383 } elsif ($result == -1) {
2385 return new OpenILS::Event("ACQ_ALREADY_CANCELED");
2388 $e->commit or return $e->die_event;
2390 # XXX create purchase order status events?
2392 if ($mgr->{post_commit}) {
2393 foreach my $func (@{$mgr->{post_commit}}) {
2401 sub cancel_purchase_order {
2402 my ($mgr, $po_id, $cancel_reason) = @_;
2404 my $po = $mgr->editor->retrieve_acq_purchase_order($po_id) or return 0;
2406 # XXX is "cancelled" a typo? It's not correct US spelling, anyway.
2407 # Depending on context, this may not warrant an event.
2408 return -1 if $po->state eq "cancelled";
2410 # But this always does.
2411 return new OpenILS::Event(
2412 "ACQ_NOT_CANCELABLE", "note" => "purchase_order $po_id"
2413 ) unless ($po->state eq "on-order" or $po->state eq "pending");
2416 $mgr->editor->allowed("CREATE_PURCHASE_ORDER", $po->ordering_agency);
2418 $po->state("cancelled");
2419 $po->cancel_reason($cancel_reason->id);
2421 my $li_ids = $mgr->editor->search_acq_lineitem(
2422 {"purchase_order" => $po_id}, {"idlist" => 1}
2425 my $result = {"li" => {}, "lid" => {}};
2426 foreach my $li_id (@$li_ids) {
2427 my $li_result = cancel_lineitem($mgr, $li_id, $cancel_reason)
2430 next if $li_result == -1; # already canceled:skip.
2431 return $li_result if not_cancelable($li_result); # not cancelable:stop.
2433 # Merge in each LI result (there's only going to be
2434 # one per call to cancel_lineitem).
2435 my ($k, $v) = each %{$li_result->{"li"}};
2436 $result->{"li"}->{$k} = $v;
2438 # Merge in each LID result (there may be many per call to
2440 while (($k, $v) = each %{$li_result->{"lid"}}) {
2441 $result->{"lid"}->{$k} = $v;
2445 # TODO who/what/where/how do we indicate this change for electronic orders?
2446 # TODO return changes to encumbered/spent
2447 # TODO maybe cascade up from smaller object to container object if last
2448 # smaller object in the container has been canceled?
2450 update_purchase_order($mgr, $po) or return 0;
2452 $po_id => {"state" => $po->state, "cancel_reason" => $cancel_reason}
2458 __PACKAGE__->register_method(
2459 method => "cancel_lineitem_api",
2460 api_name => "open-ils.acq.lineitem.cancel",
2462 desc => q/Cancels an on-order lineitem/,
2464 {desc => "Authentication token", type => "string"},
2465 {desc => "Lineitem ID to cancel", type => "number"},
2466 {desc => "Cancel reason ID", type => "number"}
2468 return => {desc => q/Object describing changed LIs and LIDs on success;
2473 __PACKAGE__->register_method(
2474 method => "cancel_lineitem_api",
2475 api_name => "open-ils.acq.lineitem.cancel.batch",
2477 desc => q/Batched version of open-ils.acq.lineitem.cancel/,
2478 return => {desc => q/Object describing changed LIs and LIDs on success;
2483 sub cancel_lineitem_api {
2484 my ($self, $conn, $auth, $li_id, $cancel_reason) = @_;
2486 my $batched = $self->api_name =~ /\.batch/;
2488 my $e = new_editor("xact" => 1, "authtoken" => $auth);
2489 return $e->die_event unless $e->checkauth;
2490 my $mgr = new OpenILS::Application::Acq::BatchManager(
2491 "editor" => $e, "conn" => $conn
2494 $cancel_reason = $mgr->editor->retrieve_acq_cancel_reason($cancel_reason) or
2495 return new OpenILS::Event(
2496 "BAD_PARAMS", "note" => "Provide cancel reason ID"
2499 my ($result, $maybe_event);
2502 $result = {"li" => {}, "lid" => {}};
2503 foreach my $one_li_id (@$li_id) {
2504 my $one = cancel_lineitem($mgr, $one_li_id, $cancel_reason) or
2505 return $e->die_event;
2506 if (not_cancelable($one)) {
2507 $maybe_event = $one;
2508 } elsif ($result == -1) {
2509 $maybe_event = new OpenILS::Event("ACQ_ALREADY_CANCELED");
2513 while (($k, $v) = each %{$one->{"li"}}) {
2514 $result->{"li"}->{$k} = $v;
2517 if ($one->{"lid"}) {
2518 while (($k, $v) = each %{$one->{"lid"}}) {
2519 $result->{"lid"}->{$k} = $v;
2525 $result = cancel_lineitem($mgr, $li_id, $cancel_reason) or
2526 return $e->die_event;
2528 if (not_cancelable($result)) {
2531 } elsif ($result == -1) {
2533 return new OpenILS::Event("ACQ_ALREADY_CANCELED");
2537 if ($batched and not scalar keys %{$result->{"li"}}) {
2539 return $maybe_event;
2541 $e->commit or return $e->die_event;
2542 # create_lineitem_status_events should handle array li_id ok
2543 create_lineitem_status_events($mgr, $li_id, "aur.cancelled");
2545 if ($mgr->{post_commit}) {
2546 foreach my $func (@{$mgr->{post_commit}}) {
2555 sub cancel_lineitem {
2556 my ($mgr, $li_id, $cancel_reason) = @_;
2557 my $li = $mgr->editor->retrieve_acq_lineitem([
2558 $li_id, {flesh => 1, flesh_fields => {jub => ['purchase_order']}}
2561 return 0 unless $mgr->editor->allowed(
2562 "CREATE_PURCHASE_ORDER", $li->purchase_order->ordering_agency
2565 # Depending on context, this may not warrant an event.
2566 return -1 if $li->state eq "cancelled";
2568 # But this always does.
2569 return new OpenILS::Event(
2570 "ACQ_NOT_CANCELABLE", "note" => "lineitem $li_id"
2572 (! $li->purchase_order) or (
2573 $li->purchase_order and (
2574 $li->state eq "on-order" or $li->state eq "pending-order"
2579 $li->state("cancelled");
2580 $li->cancel_reason($cancel_reason->id);
2582 my $lids = $mgr->editor->search_acq_lineitem_detail([{
2583 "lineitem" => $li_id
2586 flesh_fields => { acqlid => ['eg_copy_id'] }
2589 my $result = {"lid" => {}};
2591 foreach my $lid (@$lids) {
2592 my $lid_result = cancel_lineitem_detail($mgr, $lid->id, $cancel_reason)
2595 # gathering any real copies for deletion
2596 if ($lid->eg_copy_id) {
2597 $lid->eg_copy_id->isdeleted('t');
2598 push @$copies, $lid->eg_copy_id;
2601 next if $lid_result == -1; # already canceled: just skip it.
2602 return $lid_result if not_cancelable($lid_result); # not cxlable: stop.
2604 # Merge in each LID result (there's only going to be one per call to
2605 # cancel_lineitem_detail).
2606 my ($k, $v) = each %{$lid_result->{"lid"}};
2607 $result->{"lid"}->{$k} = $v;
2610 # Attempt to delete the gathered copies (this will also handle volume deletion and bib deletion)
2611 # Delete empty bibs according org unit setting
2612 my $force_delete_empty_bib = $U->ou_ancestor_setting_value(
2613 $mgr->editor->requestor->ws_ou, 'cat.bib.delete_on_no_copy_via_acq_lineitem_cancel', $mgr->editor);
2614 if (scalar(@$copies)>0) {
2616 my $delete_stats = undef;
2617 my $retarget_holds = [];
2618 my $cat_evt = OpenILS::Application::Cat::AssetCommon->update_fleshed_copies(
2619 $mgr->editor, $override, undef, $copies, $delete_stats, $retarget_holds,$force_delete_empty_bib);
2622 $logger->info("fleshed copy update failed with event: ".OpenSRF::Utils::JSON->perl2JSON($cat_evt));
2623 return new OpenILS::Event(
2624 "ACQ_NOT_CANCELABLE", "note" => "lineitem $li_id", "payload" => $cat_evt
2628 # We can't do the following and stay within the same transaction, but that's okay, the hold targeter will pick these up later.
2629 #my $ses = OpenSRF::AppSession->create('open-ils.circ');
2630 #$ses->request('open-ils.circ.hold.reset.batch', $auth, $retarget_holds);
2633 # if we have a bib, check to see whether it has been deleted. if so, cancel any active holds targeting that bib
2634 if ($li->eg_bib_id) {
2635 my $bib = $mgr->editor->retrieve_biblio_record_entry($li->eg_bib_id) or return new OpenILS::Event(
2636 "ACQ_NOT_CANCELABLE", "note" => "Could not retrieve bib " . $li->eg_bib_id . " for lineitem $li_id"
2638 if ($U->is_true($bib->deleted)) {
2639 my $holds = $mgr->editor->search_action_hold_request(
2640 { cancel_time => undef,
2641 fulfillment_time => undef,
2642 target => $li->eg_bib_id
2646 my %cached_usr_home_ou = ();
2648 for my $hold (@$holds) {
2650 $logger->info("Cancelling hold ".$hold->id.
2651 " due to acq lineitem cancellation.");
2653 $hold->cancel_time('now');
2654 $hold->cancel_cause(5); # 'Staff forced'--we may want a new hold cancel cause reason for this
2655 $hold->cancel_note('Corresponding Acquistion Lineitem/Purchase Order was cancelled.');
2656 unless($mgr->editor->update_action_hold_request($hold)) {
2657 my $evt = $mgr->editor->event;
2658 $logger->error("Error updating hold ". $evt->textcode .":". $evt->desc .":". $evt->stacktrace);
2659 return new OpenILS::Event(
2660 "ACQ_NOT_CANCELABLE", "note" => "Could not cancel hold " . $hold->id . " for lineitem $li_id", "payload" => $evt
2663 if (! defined $mgr->{post_commit}) { # we need a mechanism for creating trigger events, but only if the transaction gets committed
2664 $mgr->{post_commit} = [];
2666 push @{ $mgr->{post_commit} }, sub {
2667 my $home_ou = $cached_usr_home_ou{$hold->usr};
2669 my $user = $mgr->editor->retrieve_actor_user($hold->usr); # FIXME: how do we want to handle failures here?
2670 $home_ou = $user->home_ou;
2671 $cached_usr_home_ou{$hold->usr} = $home_ou;
2673 $U->create_events_for_hook('hold_request.cancel.cancelled_order', $hold, $home_ou);
2679 update_lineitem($mgr, $li) or return 0;
2682 "state" => $li->state,
2683 "cancel_reason" => $cancel_reason
2690 __PACKAGE__->register_method(
2691 method => "cancel_lineitem_detail_api",
2692 api_name => "open-ils.acq.lineitem_detail.cancel",
2694 desc => q/Cancels an on-order lineitem detail/,
2696 {desc => "Authentication token", type => "string"},
2697 {desc => "Lineitem detail ID to cancel", type => "number"},
2698 {desc => "Cancel reason ID", type => "number"}
2700 return => {desc => q/Object describing changed LIDs on success;
2705 sub cancel_lineitem_detail_api {
2706 my ($self, $conn, $auth, $lid_id, $cancel_reason) = @_;
2708 my $e = new_editor("xact" => 1, "authtoken" => $auth);
2709 return $e->die_event unless $e->checkauth;
2710 my $mgr = new OpenILS::Application::Acq::BatchManager(
2711 "editor" => $e, "conn" => $conn
2714 $cancel_reason = $mgr->editor->retrieve_acq_cancel_reason($cancel_reason) or
2715 return new OpenILS::Event(
2716 "BAD_PARAMS", "note" => "Provide cancel reason ID"
2719 my $result = cancel_lineitem_detail($mgr, $lid_id, $cancel_reason) or
2720 return $e->die_event;
2722 if (not_cancelable($result)) {
2725 } elsif ($result == -1) {
2727 return new OpenILS::Event("ACQ_ALREADY_CANCELED");
2730 $e->commit or return $e->die_event;
2732 # XXX create lineitem detail status events?
2736 sub cancel_lineitem_detail {
2737 my ($mgr, $lid_id, $cancel_reason) = @_;
2738 my $lid = $mgr->editor->retrieve_acq_lineitem_detail([
2742 "acqlid" => ["lineitem"], "jub" => ["purchase_order"]
2747 # Depending on context, this may not warrant an event.
2748 return -1 if $lid->cancel_reason;
2750 # But this always does.
2751 return new OpenILS::Event(
2752 "ACQ_NOT_CANCELABLE", "note" => "lineitem_detail $lid_id"
2754 (! $lid->lineitem->purchase_order) or
2756 (not $lid->recv_time) and
2758 $lid->lineitem->purchase_order and (
2759 $lid->lineitem->state eq "on-order" or
2760 $lid->lineitem->state eq "pending-order"
2765 return 0 unless $mgr->editor->allowed(
2766 "CREATE_PURCHASE_ORDER",
2767 $lid->lineitem->purchase_order->ordering_agency
2768 ) or (! $lid->lineitem->purchase_order);
2770 $lid->cancel_reason($cancel_reason->id);
2772 unless($U->is_true($cancel_reason->keep_debits)) {
2773 my $debit_id = $lid->fund_debit;
2774 $lid->clear_fund_debit;
2777 # item is cancelled. Remove the fund debit.
2778 my $debit = $mgr->editor->retrieve_acq_fund_debit($debit_id);
2779 if (!$U->is_true($debit->encumbrance)) {
2780 $mgr->editor->rollback;
2781 return OpenILS::Event->new('ACQ_NOT_CANCELABLE',
2782 note => "Debit is marked as paid: $debit_id");
2784 $mgr->editor->delete_acq_fund_debit($debit) or return $mgr->editor->die_event;
2788 # XXX LIDs don't have either an editor or a edit_time field. Should we
2789 # update these on the LI when we alter an LID?
2790 $mgr->editor->update_acq_lineitem_detail($lid) or return 0;
2792 return {"lid" => {$lid_id => {"cancel_reason" => $cancel_reason}}};
2796 __PACKAGE__->register_method(
2797 method => 'user_requests',
2798 api_name => 'open-ils.acq.user_request.retrieve.by_user_id',
2801 desc => 'Retrieve fleshed user requests and related data for a given user.',
2803 { desc => 'Authentication token', type => 'string' },
2804 { desc => 'User ID of the owner, or array of IDs', },
2805 { desc => 'Options hash (optional) with any of the keys: order_by, limit, offset, state (of the lineitem)',
2810 desc => 'Fleshed user requests and related data',
2816 __PACKAGE__->register_method(
2817 method => 'user_requests',
2818 api_name => 'open-ils.acq.user_request.retrieve.by_home_ou',
2821 desc => 'Retrieve fleshed user requests and related data for a given org unit or units.',
2823 { desc => 'Authentication token', type => 'string' },
2824 { desc => 'Org unit ID, or array of IDs', },
2825 { desc => 'Options hash (optional) with any of the keys: order_by, limit, offset, state (of the lineitem)',
2830 desc => 'Fleshed user requests and related data',
2837 my($self, $conn, $auth, $search_value, $options) = @_;
2838 my $e = new_editor(authtoken => $auth);
2839 return $e->event unless $e->checkauth;
2840 my $rid = $e->requestor->id;
2844 "select"=>{"aur"=>["id"],"au"=>["home_ou", {column => 'id', alias => 'usr_id'} ]},
2845 "from"=>{ "aur" => { "au" => {}, "jub" => { "type" => "left" } } },
2849 {"id"=>undef}, # this with the left-join pulls in requests without lineitems
2850 {"state"=>["new","on-order","pending-order"]} # FIXME - probably needs softcoding
2854 "order_by"=>[{"class"=>"aur", "field"=>"request_date", "direction"=>"desc"}]
2857 foreach (qw/ order_by limit offset /) {
2858 $query->{$_} = $options->{$_} if defined $options->{$_};
2860 if (defined $options->{'state'}) {
2861 $query->{'where'}->{'+jub'}->{'-or'}->[1]->{'state'} = $options->{'state'};
2864 if ($self->api_name =~ /by_user_id/) {
2865 $query->{'where'}->{'usr'} = $search_value;
2867 $query->{'where'}->{'+au'} = { 'home_ou' => $search_value };
2870 my $pertinent_ids = $e->json_query($query);
2873 for my $id_blob (@$pertinent_ids) {
2874 if ($rid != $id_blob->{usr_id}) {
2875 if (!defined $perm_test{ $id_blob->{home_ou} }) {
2876 $perm_test{ $id_blob->{home_ou} } = $e->allowed( ['user_request.view'], $id_blob->{home_ou} );
2878 if (!$perm_test{ $id_blob->{home_ou} }) {
2882 my $aur_obj = $e->retrieve_acq_user_request([
2884 {flesh => 1, flesh_fields => { "aur" => [ 'lineitem' ] } }
2886 if (! $aur_obj) { next; }
2888 if ($aur_obj->lineitem()) {
2889 $aur_obj->lineitem()->clear_marc();
2891 $conn->respond($aur_obj);
2897 __PACKAGE__->register_method (
2898 method => 'update_user_request',
2899 api_name => 'open-ils.acq.user_request.cancel.batch',
2902 desc => 'If given a cancel reason, will update the request with that reason, otherwise, this will delete the request altogether. The ' .
2903 'intention is for staff interfaces or processes to provide cancel reasons, and for patron interfaces to just delete the requests.' ,
2905 { desc => 'Authentication token', type => 'string' },
2906 { desc => 'ID or array of IDs for the user requests to cancel' },
2907 { desc => 'Cancel Reason ID (optional)', type => 'string' }
2910 desc => 'progress object, event on error',
2914 __PACKAGE__->register_method (
2915 method => 'update_user_request',
2916 api_name => 'open-ils.acq.user_request.set_no_hold.batch',
2919 desc => 'Remove the hold from a user request or set of requests',
2921 { desc => 'Authentication token', type => 'string' },
2922 { desc => 'ID or array of IDs for the user requests to modify' }
2925 desc => 'progress object, event on error',
2930 sub update_user_request {
2931 my($self, $conn, $auth, $aur_ids, $cancel_reason) = @_;
2932 my $e = new_editor(xact => 1, authtoken => $auth);
2933 return $e->die_event unless $e->checkauth;
2934 my $rid = $e->requestor->id;
2938 for my $id (@$aur_ids) {
2940 my $aur_obj = $e->retrieve_acq_user_request([
2943 flesh_fields => { "aur" => ['lineitem', 'usr'] }
2945 ]) or return $e->die_event;
2947 my $context_org = $aur_obj->usr()->home_ou();
2948 $aur_obj->usr( $aur_obj->usr()->id() );
2950 if ($rid != $aur_obj->usr) {
2951 if (!defined $perm_test{ $context_org }) {
2952 $perm_test{ $context_org } = $e->allowed( ['user_request.update'], $context_org );
2954 if (!$perm_test{ $context_org }) {
2959 if($self->api_name =~ /set_no_hold/) {
2960 if ($U->is_true($aur_obj->hold)) {
2962 $e->update_acq_user_request($aur_obj) or return $e->die_event;
2966 if($self->api_name =~ /cancel/) {
2967 if ( $cancel_reason ) {
2968 $aur_obj->cancel_reason( $cancel_reason );
2969 $e->update_acq_user_request($aur_obj) or return $e->die_event;
2970 create_user_request_events( $e, [ $aur_obj ], 'aur.rejected' );
2972 $e->delete_acq_user_request($aur_obj);
2976 $conn->respond({maximum => scalar(@$aur_ids), progress => $x++});
2980 return {complete => 1};
2983 __PACKAGE__->register_method (
2984 method => 'new_user_request',
2985 api_name => 'open-ils.acq.user_request.create',
2987 desc => 'Create a new user request object in the DB',
2989 { desc => 'Authentication token', type => 'string' },
2990 { desc => 'User request data hash. Hash keys match the fields for the "aur" object', type => 'object' }
2993 desc => 'The created user request object, or event on error'
2998 sub new_user_request {
2999 my($self, $conn, $auth, $form_data) = @_;
3000 my $e = new_editor(xact => 1, authtoken => $auth);
3001 return $e->die_event unless $e->checkauth;
3002 my $rid = $e->requestor->id;
3003 my $target_user_fleshed;
3004 if (! defined $$form_data{'usr'}) {
3005 $$form_data{'usr'} = $rid;
3007 if ($$form_data{'usr'} != $rid) {
3008 # See if the requestor can place the request on behalf of a different user.
3009 $target_user_fleshed = $e->retrieve_actor_user($$form_data{'usr'}) or return $e->die_event;
3010 $e->allowed('user_request.create', $target_user_fleshed->home_ou) or return $e->die_event;
3012 $target_user_fleshed = $e->requestor;
3013 $e->allowed('CREATE_PURCHASE_REQUEST') or return $e->die_event;
3015 if (! defined $$form_data{'pickup_lib'}) {
3016 if ($target_user_fleshed->ws_ou) {
3017 $$form_data{'pickup_lib'} = $target_user_fleshed->ws_ou;
3019 $$form_data{'pickup_lib'} = $target_user_fleshed->home_ou;
3022 if (! defined $$form_data{'request_type'}) {
3023 $$form_data{'request_type'} = 1; # Books
3025 my $aur_obj = new Fieldmapper::acq::user_request;
3027 $aur_obj->usr( $$form_data{'usr'} );
3028 $aur_obj->request_date( 'now' );
3029 for my $field ( keys %$form_data ) {
3030 if (defined $$form_data{$field} and $field !~ /^(id|lineitem|eg_bib|request_date|cancel_reason)$/) {
3031 $aur_obj->$field( $$form_data{$field} );
3035 $aur_obj = $e->create_acq_user_request($aur_obj) or return $e->die_event;
3037 $e->commit and create_user_request_events( $e, [ $aur_obj ], 'aur.created' );
3042 sub create_user_request_events {
3043 my($e, $user_reqs, $hook) = @_;
3045 my $ses = OpenSRF::AppSession->create('open-ils.trigger');
3048 my %cached_usr_home_ou = ();
3049 for my $user_req (@$user_reqs) {
3050 my $home_ou = $cached_usr_home_ou{$user_req->usr};
3052 my $user = $e->retrieve_actor_user($user_req->usr) or return $e->die_event;
3053 $home_ou = $user->home_ou;
3054 $cached_usr_home_ou{$user_req->usr} = $home_ou;
3056 my $req = $ses->request('open-ils.trigger.event.autocreate', $hook, $user_req, $home_ou);
3065 __PACKAGE__->register_method(
3066 method => "po_note_CUD_batch",
3067 api_name => "open-ils.acq.po_note.cud.batch",
3070 desc => q/Manage purchase order notes/,
3072 {desc => "Authentication token", type => "string"},
3073 {desc => "List of po_notes to manage", type => "array"},
3075 return => {desc => "Stream of successfully managed objects"}
3079 sub po_note_CUD_batch {
3080 my ($self, $conn, $auth, $notes) = @_;
3082 my $e = new_editor("xact"=> 1, "authtoken" => $auth);
3083 return $e->die_event unless $e->checkauth;
3086 my $total = @$notes;
3089 foreach my $note (@$notes) {
3091 $note->editor($e->requestor->id);
3092 $note->edit_time("now");
3095 $note->creator($e->requestor->id);
3096 $note = $e->create_acq_po_note($note) or return $e->die_event;
3097 } elsif ($note->isdeleted) {
3098 $e->delete_acq_po_note($note) or return $e->die_event;
3099 } elsif ($note->ischanged) {
3100 $e->update_acq_po_note($note) or return $e->die_event;
3103 unless ($note->isdeleted) {
3104 $note = $e->retrieve_acq_po_note($note->id) or
3105 return $e->die_event;
3109 {"maximum" => $total, "progress" => ++$count, "note" => $note}
3113 $e->commit and $conn->respond_complete or return $e->die_event;
3117 # retrieves a lineitem, fleshes its PO and PL, checks perms
3118 sub fetch_and_check_li {
3121 my $perm_mode = shift || 'read';
3123 my $li = $e->retrieve_acq_lineitem([
3126 flesh_fields => {jub => ['purchase_order', 'picklist']}
3128 ]) or return $e->die_event;
3130 if(my $po = $li->purchase_order) {
3131 my $perms = ($perm_mode eq 'read') ? 'VIEW_PURCHASE_ORDER' : 'CREATE_PURCHASE_ORDER';
3132 return ($li, $e->die_event) unless $e->allowed($perms, $po->ordering_agency);
3134 } elsif(my $pl = $li->picklist) {
3135 my $perms = ($perm_mode eq 'read') ? 'VIEW_PICKLIST' : 'CREATE_PICKLIST';
3136 return ($li, $e->die_event) unless $e->allowed($perms, $pl->org_unit);
3143 __PACKAGE__->register_method(
3144 method => "clone_distrib_form",
3145 api_name => "open-ils.acq.distribution_formula.clone",
3148 desc => q/Clone a distribution formula/,
3150 {desc => "Authentication token", type => "string"},
3151 {desc => "Original formula ID", type => 'integer'},
3152 {desc => "Name of new formula", type => 'string'},
3154 return => {desc => "ID of newly created formula"}
3158 sub clone_distrib_form {
3159 my($self, $client, $auth, $form_id, $new_name) = @_;
3161 my $e = new_editor("xact"=> 1, "authtoken" => $auth);
3162 return $e->die_event unless $e->checkauth;
3164 my $old_form = $e->retrieve_acq_distribution_formula($form_id) or return $e->die_event;
3165 return $e->die_event unless $e->allowed('ADMIN_ACQ_DISTRIB_FORMULA', $old_form->owner);
3167 my $new_form = Fieldmapper::acq::distribution_formula->new;
3169 $new_form->owner($old_form->owner);
3170 $new_form->name($new_name);
3171 $e->create_acq_distribution_formula($new_form) or return $e->die_event;
3173 my $entries = $e->search_acq_distribution_formula_entry({formula => $form_id});
3174 for my $entry (@$entries) {
3175 my $new_entry = Fieldmapper::acq::distribution_formula_entry->new;
3176 $new_entry->$_($entry->$_()) for $entry->real_fields;
3177 $new_entry->formula($new_form->id);
3178 $new_entry->clear_id;
3179 $e->create_acq_distribution_formula_entry($new_entry) or return $e->die_event;
3183 return $new_form->id;
3186 __PACKAGE__->register_method(
3187 method => 'add_li_to_po',
3188 api_name => 'open-ils.acq.purchase_order.add_lineitem',
3190 desc => q/Adds a lineitem to an existing purchase order/,
3192 {desc => 'Authentication token', type => 'string'},
3193 {desc => 'The purchase order id', type => 'number'},
3194 {desc => 'The lineitem ID', type => 'number'},
3196 return => {desc => 'Streams a total versus completed counts object, event on error'}
3201 my($self, $conn, $auth, $po_id, $li_id) = @_;
3203 my $e = new_editor(authtoken => $auth, xact => 1);
3204 return $e->die_event unless $e->checkauth;
3206 my $mgr = OpenILS::Application::Acq::BatchManager->new(editor => $e, conn => $conn);
3208 my $po = $e->retrieve_acq_purchase_order($po_id)
3209 or return $e->die_event;
3211 my $li = $e->retrieve_acq_lineitem($li_id)
3212 or return $e->die_event;
3214 return $e->die_event unless
3215 $e->allowed('CREATE_PURCHASE_ORDER', $po->ordering_agency);
3217 unless ($po->state =~ /new|pending/) {
3219 return {success => 0, po => $po, error => 'bad-po-state'};
3222 unless ($li->state =~ /new|order-ready|pending-order/) {
3224 return {success => 0, li => $li, error => 'bad-li-state'};
3227 $li->purchase_order($po_id);
3228 $li->state('pending-order');
3229 update_lineitem($mgr, $li) or return $e->die_event;
3232 return {success => 1};