1 package OpenILS::Application::Acq::BatchManager;
2 use OpenSRF::AppSession;
3 use OpenSRF::EX qw/:try/;
4 use strict; use warnings;
7 my($class, %args) = @_;
8 my $self = bless(\%args, $class);
16 purchase_order => undef,
22 $self->{ingest_queue} = [];
24 $self->throttle(5) unless $self->throttle;
30 $self->{conn} = $val if $val;
35 $self->{throttle} = $val if $val;
36 return $self->{throttle};
39 my($self, %other_args) = @_;
40 if($self->throttle and not %other_args) {
42 ($self->{args}->{progress} - $self->{last_respond_progress}) >= $self->throttle
45 $self->conn->respond({ %{$self->{args}}, %other_args });
46 $self->{last_respond_progress} = $self->{args}->{progress};
48 sub respond_complete {
49 my($self, %other_args) = @_;
51 $self->conn->respond_complete({ %{$self->{args}}, %other_args });
56 $self->{args}->{total} = $val if defined $val;
57 $self->{args}->{maximum} = $self->{args}->{total};
58 return $self->{args}->{total};
62 $self->{args}->{purchase_order} = $val if $val;
67 $self->{args}->{picklist} = $val if $val;
72 $self->{args}->{lid} += 1;
73 $self->{args}->{progress} += 1;
78 $self->{args}->{li} += 1;
79 $self->{args}->{progress} += 1;
84 $self->{args}->{copies} += 1;
85 $self->{args}->{progress} += 1;
90 $self->{args}->{bibs} += 1;
91 $self->{args}->{progress} += 1;
95 my($self, $amount) = @_;
96 $self->{args}->{debits_accrued} += $amount;
97 $self->{args}->{progress} += 1;
101 my($self, $editor) = @_;
102 $self->{editor} = $editor if defined $editor;
103 return $self->{editor};
107 $self->{args}->{complete} = 1;
112 my($self, $val) = @_;
113 $self->{ingest_ses} = $val if $val;
114 return $self->{ingest_ses};
117 sub push_ingest_queue {
118 my($self, $rec_id) = @_;
120 $self->ingest_ses(OpenSRF::AppSession->connect('open-ils.ingest'))
121 unless $self->ingest_ses;
123 my $req = $self->ingest_ses->request('open-ils.ingest.full.biblio.record', $rec_id);
125 push(@{$self->{ingest_queue}}, $req);
128 sub process_ingest_records {
130 return unless @{$self->{ingest_queue}};
132 for my $req (@{$self->{ingest_queue}}) {
136 $self->{args}->{indexed} += 1;
137 $self->{args}->{progress} += 1;
142 $self->ingest_ses->disconnect;
147 my($self, $org, $key, $val) = @_;
148 $self->{cache}->{$org} = {} unless $self->{cache}->{org};
149 $self->{cache}->{$org}->{$key} = $val if defined $val;
150 return $self->{cache}->{$org}->{$key};
154 package OpenILS::Application::Acq::Order;
155 use base qw/OpenILS::Application/;
156 use strict; use warnings;
157 # ----------------------------------------------------------------------------
158 # Break up each component of the order process and pieces into managable
159 # actions that can be shared across different workflows
160 # ----------------------------------------------------------------------------
162 use OpenSRF::Utils::Logger qw(:logger);
163 use OpenSRF::Utils::JSON;
164 use OpenILS::Utils::Fieldmapper;
165 use OpenILS::Utils::CStoreEditor q/:funcs/;
166 use OpenILS::Const qw/:const/;
167 use OpenSRF::EX q/:try/;
168 use OpenILS::Application::AppUtils;
169 use OpenILS::Application::Cat::BibCommon;
170 use OpenILS::Application::Cat::AssetCommon;
174 my $U = 'OpenILS::Application::AppUtils';
177 # ----------------------------------------------------------------------------
179 # ----------------------------------------------------------------------------
180 sub create_lineitem {
181 my($mgr, %args) = @_;
182 my $li = Fieldmapper::acq::lineitem->new;
183 $li->creator($mgr->editor->requestor->id);
184 $li->selector($li->creator);
185 $li->editor($li->creator);
186 $li->create_time('now');
187 $li->edit_time('now');
189 $li->$_($args{$_}) for keys %args;
192 return $mgr->editor->create_acq_lineitem($li);
195 sub update_lineitem {
197 $li->edit_time('now');
198 $li->editor($mgr->editor->requestor->id);
200 return $li if $mgr->editor->update_acq_lineitem($li);
204 sub delete_lineitem {
206 $li = $mgr->editor->retrieve_acq_lineitem($li) unless ref $li;
208 # delete the attached lineitem_details
209 my $lid_ids = $mgr->editor->search_acq_lineitem_detail({lineitem => $li->id}, {idlist=>1});
210 for my $lid_id (@$lid_ids) {
211 return 0 unless delete_lineitem_detail($mgr, $lid_id);
215 return $mgr->editor->delete_acq_lineitem($li);
218 # begins and commit transactions as it goes
219 sub create_lineitem_list_assets {
220 my($mgr, $li_ids) = @_;
221 return undef if check_import_li_marc_perms($mgr, $li_ids);
223 # create the bibs/volumes/copies and ingest the records
224 for my $li_id (@$li_ids) {
225 $mgr->editor->xact_begin;
226 my $data = create_lineitem_assets($mgr, $li_id) or return undef;
227 $mgr->editor->xact_commit;
228 # XXX ingest is in-db now
229 #$mgr->push_ingest_queue($data->{li}->eg_bib_id) if $data->{new_bib};
232 $mgr->process_ingest_records;
236 # returns event on error, undef on success
237 sub check_import_li_marc_perms {
238 my($mgr, $li_ids) = @_;
240 # if there are any order records that are not linked to
241 # in-db bib records, verify staff has perms to import order records
242 my $order_li = $mgr->editor->search_acq_lineitem(
243 [{id => $li_ids, eg_bib_id => undef}, {limit => 1}], {idlist => 1})->[0];
246 return $mgr->editor->die_event unless
247 $mgr->editor->allowed('IMPORT_ACQ_LINEITEM_BIB_RECORD');
254 # ----------------------------------------------------------------------------
255 # if all of the lineitem details for this lineitem have
256 # been received, mark the lineitem as received
257 # returns 1 on non-received, li on received, 0 on error
258 # ----------------------------------------------------------------------------
259 sub check_lineitem_received {
260 my($mgr, $li_id) = @_;
262 my $non_recv = $mgr->editor->search_acq_lineitem_detail(
263 {recv_time => undef, lineitem => $li_id}, {idlist=>1});
265 return 1 if @$non_recv;
267 my $li = $mgr->editor->retrieve_acq_lineitem($li_id);
268 $li->state('received');
269 return update_lineitem($mgr, $li);
272 sub receive_lineitem {
273 my($mgr, $li_id, $skip_complete_check) = @_;
274 my $li = $mgr->editor->retrieve_acq_lineitem($li_id) or return 0;
276 my $lid_ids = $mgr->editor->search_acq_lineitem_detail(
277 {lineitem => $li_id, recv_time => undef}, {idlist => 1});
279 for my $lid_id (@$lid_ids) {
280 receive_lineitem_detail($mgr, $lid_id, 1) or return 0;
284 $li->state('received');
285 update_lineitem($mgr, $li) or return 0;
286 return 1 if $skip_complete_check;
288 return check_purchase_order_received($mgr, $li->purchase_order);
291 sub rollback_receive_lineitem {
292 my($mgr, $li_id) = @_;
293 my $li = $mgr->editor->retrieve_acq_lineitem($li_id) or return 0;
295 my $lid_ids = $mgr->editor->search_acq_lineitem_detail(
296 {lineitem => $li_id, recv_time => {'!=' => undef}}, {idlist => 1});
298 for my $lid_id (@$lid_ids) {
299 rollback_receive_lineitem_detail($mgr, $lid_id, 1) or return 0;
303 $li->state('on-order');
304 return update_lineitem($mgr, $li);
307 # ----------------------------------------------------------------------------
309 # ----------------------------------------------------------------------------
310 sub create_lineitem_detail {
311 my($mgr, %args) = @_;
312 my $lid = Fieldmapper::acq::lineitem_detail->new;
313 $lid->$_($args{$_}) for keys %args;
316 return $mgr->editor->create_acq_lineitem_detail($lid);
320 # flesh out any required data with default values where appropriate
321 sub complete_lineitem_detail {
323 unless($lid->barcode) {
324 my $pfx = $U->ou_ancestor_setting_value($lid->owning_lib, 'acq.tmp_barcode_prefix') || 'ACQ';
325 $lid->barcode($pfx.$lid->id);
328 unless($lid->cn_label) {
329 my $pfx = $U->ou_ancestor_setting_value($lid->owning_lib, 'acq.tmp_callnumber_prefix') || 'ACQ';
330 $lid->cn_label($pfx.$lid->id);
333 if(!$lid->location and my $loc = $U->ou_ancestor_setting_value($lid->owning_lib, 'acq.default_copy_location')) {
334 $lid->location($loc);
337 if(!$lid->circ_modifier and my $mod = get_default_circ_modifier($mgr, $lid->owning_lib)) {
338 $lid->circ_modifier($mod);
341 $mgr->editor->update_acq_lineitem_detail($lid) or return 0;
345 sub get_default_circ_modifier {
347 my $mod = $mgr->cache($org, 'def_circ_mod');
349 $mod = $U->ou_ancestor_setting_value($org, 'acq.default_circ_modifier');
350 return $mgr->cache($org, 'def_circ_mod', $mod) if $mod;
354 sub delete_lineitem_detail {
356 $lid = $mgr->editor->retrieve_acq_lineitem_detail($lid) unless ref $lid;
357 return $mgr->editor->delete_acq_lineitem_detail($lid);
361 sub receive_lineitem_detail {
362 my($mgr, $lid_id, $skip_complete_check) = @_;
363 my $e = $mgr->editor;
365 my $lid = $e->retrieve_acq_lineitem_detail([
369 acqlid => ['fund_debit']
374 return 1 if $lid->recv_time;
376 $lid->recv_time('now');
377 $e->update_acq_lineitem_detail($lid) or return 0;
379 my $copy = $e->retrieve_asset_copy($lid->eg_copy_id) or return 0;
380 $copy->status(OILS_COPY_STATUS_IN_PROCESS);
381 $copy->edit_date('now');
382 $copy->editor($e->requestor->id);
383 $e->update_asset_copy($copy) or return 0;
385 if($lid->fund_debit) {
386 $lid->fund_debit->encumbrance('f');
387 $e->update_acq_fund_debit($lid->fund_debit) or return 0;
392 return 1 if $skip_complete_check;
394 my $li = check_lineitem_received($mgr, $lid->lineitem) or return 0;
395 return 1 if $li == 1; # li not received
397 return check_purchase_order_received($mgr, $li->purchase_order);
401 sub rollback_receive_lineitem_detail {
402 my($mgr, $lid_id) = @_;
403 my $e = $mgr->editor;
405 my $lid = $e->retrieve_acq_lineitem_detail([
409 acqlid => ['fund_debit']
414 return 1 unless $lid->recv_time;
416 $lid->clear_recv_time;
417 $e->update_acq_lineitem_detail($lid) or return 0;
419 my $copy = $e->retrieve_asset_copy($lid->eg_copy_id) or return 0;
420 $copy->status(OILS_COPY_STATUS_ON_ORDER);
421 $copy->edit_date('now');
422 $copy->editor($e->requestor->id);
423 $e->update_asset_copy($copy) or return 0;
425 if($lid->fund_debit) {
426 $lid->fund_debit->encumbrance('t');
427 $e->update_acq_fund_debit($lid->fund_debit) or return 0;
434 # ----------------------------------------------------------------------------
436 # ----------------------------------------------------------------------------
437 sub set_lineitem_attr {
438 my($mgr, %args) = @_;
439 my $attr_type = $args{attr_type};
441 # first, see if it's already set. May just need to overwrite it
442 my $attr = $mgr->editor->search_acq_lineitem_attr({
443 lineitem => $args{lineitem},
444 attr_type => $args{attr_type},
445 attr_name => $args{attr_name}
449 $attr->attr_value($args{attr_value});
450 return $attr if $mgr->editor->update_acq_lineitem_attr($attr);
455 $attr = Fieldmapper::acq::lineitem_attr->new;
456 $attr->$_($args{$_}) for keys %args;
458 unless($attr->definition) {
459 my $find = "search_acq_$attr_type";
460 my $attr_def_id = $mgr->editor->$find({code => $attr->attr_name}, {idlist=>1})->[0] or return 0;
461 $attr->definition($attr_def_id);
463 return $mgr->editor->create_acq_lineitem_attr($attr);
469 my $attrs = $li->attributes;
470 my ($marc_estimated, $local_estimated, $local_actual, $prov_estimated, $prov_actual);
472 for my $attr (@$attrs) {
473 if($attr->attr_name eq 'estimated_price') {
474 $local_estimated = $attr->attr_value
475 if $attr->attr_type eq 'lineitem_local_attr_definition';
476 $prov_estimated = $attr->attr_value
477 if $attr->attr_type eq 'lineitem_prov_attr_definition';
478 $marc_estimated = $attr->attr_value
479 if $attr->attr_type eq 'lineitem_marc_attr_definition';
481 } elsif($attr->attr_name eq 'actual_price') {
482 $local_actual = $attr->attr_value
483 if $attr->attr_type eq 'lineitem_local_attr_definition';
484 $prov_actual = $attr->attr_value
485 if $attr->attr_type eq 'lineitem_prov_attr_definition';
489 return ($local_actual, 1) if $local_actual;
490 return ($prov_actual, 2) if $prov_actual;
491 return ($local_estimated, 1) if $local_estimated;
492 return ($prov_estimated, 2) if $prov_estimated;
493 return ($marc_estimated, 3);
497 # ----------------------------------------------------------------------------
499 # ----------------------------------------------------------------------------
500 sub create_lineitem_debits {
501 my($mgr, $li, $price, $ptype) = @_;
503 ($price, $ptype) = get_li_price($li) unless $price;
506 $mgr->editor->event(OpenILS::Event->new('ACQ_LINEITEM_NO_PRICE', payload => $li->id));
507 $mgr->editor->rollback;
511 unless($li->provider) {
512 $mgr->editor->event(OpenILS::Event->new('ACQ_LINEITEM_NO_PROVIDER', payload => $li->id));
513 $mgr->editor->rollback;
517 my $lid_ids = $mgr->editor->search_acq_lineitem_detail(
518 {lineitem => $li->id},
522 for my $lid_id (@$lid_ids) {
524 my $lid = $mgr->editor->retrieve_acq_lineitem_detail([
527 flesh_fields => {acqlid => ['fund']}
531 create_lineitem_detail_debit($mgr, $li, $lid, $price, $ptype) or return 0;
540 # ptype 1=local, 2=provider, 3=marc
541 sub create_lineitem_detail_debit {
542 my($mgr, $li, $lid, $price, $ptype) = @_;
544 unless(ref $li and ref $li->provider) {
545 $li = $mgr->editor->retrieve_acq_lineitem([
548 flesh_fields => {jub => ['provider']},
553 unless(ref $lid and ref $lid->fund) {
554 $lid = $mgr->editor->retrieve_acq_lineitem_detail([
557 flesh_fields => {acqlid => ['fund']}
562 my $ctype = $lid->fund->currency_type;
565 if($ptype == 2) { # price from vendor
566 $ctype = $li->provider->currency_type;
567 $amount = currency_conversion($mgr, $ctype, $lid->fund->currency_type, $price);
570 my $debit = create_fund_debit(
572 fund => $lid->fund->id,
573 origin_amount => $price,
574 origin_currency_type => $ctype,
578 $lid->fund_debit($debit->id);
579 $lid->fund($lid->fund->id);
580 $mgr->editor->update_acq_lineitem_detail($lid) or return 0;
585 # ----------------------------------------------------------------------------
587 # ----------------------------------------------------------------------------
588 sub create_fund_debit {
589 my($mgr, %args) = @_;
591 # Verify the fund is not being spent beyond the hard stop amount
592 my $fund = $mgr->editor->retrieve_acq_fund($args{fund}) or return 0;
594 if($fund->balance_stop_percent) {
596 my $balance = $mgr->editor->search_acq_fund_combined_balance({fund => $fund->id})->[0];
597 my $allocations = $mgr->editor->search_acq_fund_allocation_total({fund => $fund->id})->[0];
598 $balance = ($balance) ? $balance->amount : 0;
599 $allocations = ($allocations) ? $allocations->amount : 0;
602 $allocations == 0 || # if no allocations were ever made, assume we have hit the stop percent
603 ( ( ( ($balance - $args{amount}) / $allocations ) * 100 ) < $fund->balance_stop_percent))
605 $mgr->editor->event(OpenILS::Event->new(
606 'FUND_EXCEEDS_STOP_PERCENT',
607 payload => {fund => $fund->id, debit_amount => $args{amount}}
613 my $debit = Fieldmapper::acq::fund_debit->new;
614 $debit->debit_type('purchase');
615 $debit->encumbrance('t');
616 $debit->$_($args{$_}) for keys %args;
618 $mgr->add_debit($debit->amount);
619 return $mgr->editor->create_acq_fund_debit($debit);
622 sub currency_conversion {
623 my($mgr, $src_currency, $dest_currency, $amount) = @_;
624 my $result = $mgr->editor->json_query(
625 {from => ['acq.exchange_ratio', $src_currency, $dest_currency, $amount]});
626 return $result->[0]->{'acq.exchange_ratio'};
630 # ----------------------------------------------------------------------------
632 # ----------------------------------------------------------------------------
633 sub create_picklist {
634 my($mgr, %args) = @_;
635 my $picklist = Fieldmapper::acq::picklist->new;
636 $picklist->creator($mgr->editor->requestor->id);
637 $picklist->owner($picklist->creator);
638 $picklist->editor($picklist->creator);
639 $picklist->create_time('now');
640 $picklist->edit_time('now');
641 $picklist->org_unit($mgr->editor->requestor->ws_ou);
642 $picklist->owner($mgr->editor->requestor->id);
643 $picklist->$_($args{$_}) for keys %args;
645 $mgr->picklist($picklist);
646 return $mgr->editor->create_acq_picklist($picklist);
649 sub update_picklist {
650 my($mgr, $picklist) = @_;
651 $picklist = $mgr->editor->retrieve_acq_picklist($picklist) unless ref $picklist;
652 $picklist->edit_time('now');
653 $picklist->editor($mgr->editor->requestor->id);
654 $mgr->picklist($picklist);
655 return $picklist if $mgr->editor->update_acq_picklist($picklist);
659 sub delete_picklist {
660 my($mgr, $picklist) = @_;
661 $picklist = $mgr->editor->retrieve_acq_picklist($picklist) unless ref $picklist;
663 # delete all 'new' lineitems
664 my $li_ids = $mgr->editor->search_acq_lineitem({picklist => $picklist->id, state => 'new'}, {idlist => 1});
665 for my $li_id (@$li_ids) {
666 my $li = $mgr->editor->retrieve_acq_lineitem($li_id);
667 return 0 unless delete_lineitem($mgr, $li);
671 # detach all non-'new' lineitems
672 $li_ids = $mgr->editor->search_acq_lineitem({picklist => $picklist->id, state => {'!=' => 'new'}}, {idlist => 1});
673 for my $li_id (@$li_ids) {
674 my $li = $mgr->editor->retrieve_acq_lineitem($li_id);
676 return 0 unless update_lineitem($mgr, $li);
680 # remove any picklist-specific object perms
681 my $ops = $mgr->editor->search_permission_usr_object_perm_map({object_type => 'acqpl', object_id => ''.$picklist->id});
683 return 0 unless $mgr->editor->delete_usr_object_perm_map($op);
686 return $mgr->editor->delete_acq_picklist($picklist);
689 # ----------------------------------------------------------------------------
691 # ----------------------------------------------------------------------------
692 sub update_purchase_order {
694 $po = $mgr->editor->retrieve_acq_purchase_order($po) unless ref $po;
695 $po->editor($mgr->editor->requestor->id);
696 $po->edit_time('now');
697 $mgr->purchase_order($po);
698 return $po if $mgr->editor->update_acq_purchase_order($po);
702 sub create_purchase_order {
703 my($mgr, %args) = @_;
705 # verify the chosen provider is still active
706 my $provider = $mgr->editor->retrieve_acq_provider($args{provider}) or return 0;
707 unless($U->is_true($provider->active)) {
708 $logger->error("provider is not active. cannot create PO");
709 $mgr->editor->event(OpenILS::Event->new('ACQ_PROVIDER_INACTIVE'));
713 my $po = Fieldmapper::acq::purchase_order->new;
714 $po->creator($mgr->editor->requestor->id);
715 $po->editor($mgr->editor->requestor->id);
716 $po->owner($mgr->editor->requestor->id);
717 $po->edit_time('now');
718 $po->create_time('now');
719 $po->state('pending');
720 $po->ordering_agency($mgr->editor->requestor->ws_ou);
721 $po->$_($args{$_}) for keys %args;
723 $mgr->purchase_order($po);
724 return $mgr->editor->create_acq_purchase_order($po);
727 # ----------------------------------------------------------------------------
728 # if all of the lineitems for this PO are received,
729 # mark the PO as received
730 # ----------------------------------------------------------------------------
731 sub check_purchase_order_received {
732 my($mgr, $po_id) = @_;
734 my $non_recv_li = $mgr->editor->search_acq_lineitem(
735 { purchase_order => $po_id,
736 state => {'!=' => 'received'}
739 return 1 if @$non_recv_li;
741 my $po = $mgr->editor->retrieve_acq_purchase_order($po_id);
742 $po->state('received');
743 return update_purchase_order($mgr, $po);
747 # ----------------------------------------------------------------------------
748 # Bib, Callnumber, and Copy data
749 # ----------------------------------------------------------------------------
751 sub create_lineitem_assets {
752 my($mgr, $li_id) = @_;
755 my $li = $mgr->editor->retrieve_acq_lineitem([
758 flesh_fields => {jub => ['purchase_order', 'attributes']}
762 # -----------------------------------------------------------------
763 # first, create the bib record if necessary
764 # -----------------------------------------------------------------
766 unless($li->eg_bib_id) {
767 create_bib($mgr, $li) or return 0;
771 my $li_details = $mgr->editor->search_acq_lineitem_detail({lineitem => $li_id}, {idlist=>1});
773 # -----------------------------------------------------------------
774 # for each lineitem_detail, create the volume if necessary, create
775 # a copy, and link them all together.
776 # -----------------------------------------------------------------
778 for my $lid_id (@{$li_details}) {
780 my $lid = $mgr->editor->retrieve_acq_lineitem_detail($lid_id) or return 0;
781 next if $lid->eg_copy_id;
783 # use the same callnumber label for all items within this lineitem
784 $lid->cn_label($first_cn) if $first_cn and not $lid->cn_label;
786 # apply defaults if necessary
787 return 0 unless complete_lineitem_detail($mgr, $lid);
789 $first_cn = $lid->cn_label unless $first_cn;
791 my $org = $lid->owning_lib;
792 my $label = $lid->cn_label;
793 my $bibid = $li->eg_bib_id;
795 my $volume = $mgr->cache($org, "cn.$bibid.$label");
797 $volume = create_volume($mgr, $li, $lid) or return 0;
798 $mgr->cache($org, "cn.$bibid.$label", $volume);
800 create_copy($mgr, $volume, $lid) or return 0;
803 return { li => $li, new_bib => $new_bib };
809 my $record = OpenILS::Application::Cat::BibCommon->biblio_record_xml_import(
814 1, # override tcn collisions
817 if($U->event_code($record)) {
818 $mgr->editor->event($record);
819 $mgr->editor->rollback;
823 $li->eg_bib_id($record->id);
825 return update_lineitem($mgr, $li);
829 my($mgr, $li, $lid) = @_;
832 OpenILS::Application::Cat::AssetCommon->find_or_create_volume(
840 $mgr->editor->event($evt);
848 my($mgr, $volume, $lid) = @_;
849 my $copy = Fieldmapper::asset::copy->new;
851 $copy->loan_duration(2);
852 $copy->fine_level(2);
853 $copy->status(OILS_COPY_STATUS_ON_ORDER);
854 $copy->barcode($lid->barcode);
855 $copy->location($lid->location);
856 $copy->call_number($volume->id);
857 $copy->circ_lib($volume->owning_lib);
858 $copy->circ_modifier($lid->circ_modifier);
860 my $evt = OpenILS::Application::Cat::AssetCommon->create_copy($mgr->editor, $volume, $copy);
862 $mgr->editor->event($evt);
867 $lid->eg_copy_id($copy->id);
868 $mgr->editor->update_acq_lineitem_detail($lid) or return 0;
876 # ----------------------------------------------------------------------------
877 # Workflow: Build a selection list from a Z39.50 search
878 # ----------------------------------------------------------------------------
880 __PACKAGE__->register_method(
882 api_name => 'open-ils.acq.picklist.search.z3950',
885 desc => 'Performs a z3950 federated search and creates a picklist and associated lineitems',
887 {desc => 'Authentication token', type => 'string'},
888 {desc => 'Search definition', type => 'object'},
889 {desc => 'Picklist name, optional', type => 'string'},
895 my($self, $conn, $auth, $search, $name, $options) = @_;
896 my $e = new_editor(authtoken=>$auth);
897 return $e->event unless $e->checkauth;
898 return $e->event unless $e->allowed('CREATE_PICKLIST');
900 $search->{limit} ||= 10;
903 my $ses = OpenSRF::AppSession->create('open-ils.search');
904 my $req = $ses->request('open-ils.search.z3950.search_class', $auth, $search);
909 while(my $resp = $req->recv(timeout=>60)) {
912 my $e = new_editor(requestor=>$e->requestor, xact=>1);
913 $mgr = OpenILS::Application::Acq::BatchManager->new(editor => $e, conn => $conn);
914 $picklist = zsearch_build_pl($mgr, $name);
918 my $result = $resp->content;
919 my $count = $result->{count};
920 $mgr->total( (($count < $search->{limit}) ? $count : $search->{limit})+1 );
922 for my $rec (@{$result->{records}}) {
924 my $li = create_lineitem($mgr,
925 picklist => $picklist->id,
926 source_label => $result->{service},
927 marc => $rec->{marcxml},
928 eg_bib_id => $rec->{bibid}
931 if($$options{respond_li}) {
932 $li->attributes($mgr->editor->search_acq_lineitem_attr({lineitem => $li->id}))
933 if $$options{flesh_attrs};
934 $li->clear_marc if $$options{clear_marc};
935 $mgr->respond(lineitem => $li);
942 $mgr->editor->commit;
943 return $mgr->respond_complete;
946 sub zsearch_build_pl {
947 my($mgr, $name) = @_;
950 my $picklist = $mgr->editor->search_acq_picklist({
951 owner => $mgr->editor->requestor->id,
955 if($name eq '' and $picklist) {
956 return 0 unless delete_picklist($mgr, $picklist);
960 return update_picklist($mgr, $picklist) if $picklist;
961 return create_picklist($mgr, name => $name);
965 # ----------------------------------------------------------------------------
966 # Workflow: Build a selection list / PO by importing a batch of MARC records
967 # ----------------------------------------------------------------------------
969 __PACKAGE__->register_method(
970 method => 'upload_records',
971 api_name => 'open-ils.acq.process_upload_records',
976 my($self, $conn, $auth, $key) = @_;
978 my $e = new_editor(authtoken => $auth, xact => 1);
979 return $e->die_event unless $e->checkauth;
980 my $mgr = OpenILS::Application::Acq::BatchManager->new(editor => $e, conn => $conn);
982 my $cache = OpenSRF::Utils::Cache->new;
984 my $data = $cache->get_cache("vandelay_import_spool_$key");
985 my $purpose = $data->{purpose};
986 my $filename = $data->{path};
987 my $provider = $data->{provider};
988 my $picklist = $data->{picklist};
989 my $create_po = $data->{create_po};
990 my $ordering_agency = $data->{ordering_agency};
991 my $create_assets = $data->{create_assets};
995 unless(-r $filename) {
996 $logger->error("unable to read MARC file $filename");
998 return OpenILS::Event->new('FILE_UPLOAD_ERROR', payload => {filename => $filename});
1001 $provider = $e->retrieve_acq_provider($provider) or return $e->die_event;
1004 $picklist = $e->retrieve_acq_picklist($picklist) or return $e->die_event;
1005 if($picklist->owner != $e->requestor->id) {
1006 return $e->die_event unless
1007 $e->allowed('CREATE_PICKLIST', $picklist->org_unit, $picklist);
1009 $mgr->picklist($picklist);
1014 $po = create_purchase_order($mgr,
1015 ordering_agency => $ordering_agency,
1016 provider => $provider->id,
1018 ) or return $mgr->editor->die_event;
1021 $logger->info("acq processing MARC file=$filename");
1023 my $marctype = 'USMARC'; # ?
1024 my $batch = new MARC::Batch ($marctype, $filename);
1039 } catch Error with {
1041 $logger->warn("Proccessing of record $count in set $key failed with error $err. Skipping this record");
1048 ($xml = $r->as_xml_record()) =~ s/\n//sog;
1049 $xml =~ s/^<\?xml.+\?\s*>//go;
1050 $xml =~ s/>\s+</></go;
1051 $xml =~ s/\p{Cc}//go;
1052 $xml = $U->entityize($xml);
1053 $xml =~ s/[\x00-\x1f]//go;
1055 } catch Error with {
1057 $logger->warn("Proccessing XML of record $count in set $key failed with error $err. Skipping this record");
1060 next if $err or not $xml;
1063 source_label => $provider->code,
1064 provider => $provider->id,
1068 $args{picklist} = $picklist->id if $picklist;
1070 $args{purchase_order} = $po->id;
1071 $args{state} = 'on-order';
1074 my $li = create_lineitem($mgr, %args) or return $mgr->editor->die_event;
1076 $li->provider($provider); # flesh it, we'll need it later
1078 import_lineitem_details($mgr, $ordering_agency, $li) or return $mgr->editor->die_event;
1081 push(@li_list, $li->id);
1087 $cache->delete_cache('vandelay_import_spool_' . $key);
1089 if($create_assets) {
1090 create_lineitem_list_assets($mgr, \@li_list) or return $e->die_event;
1093 return $mgr->respond_complete;
1096 sub import_lineitem_details {
1097 my($mgr, $ordering_agency, $li) = @_;
1099 my $holdings = $mgr->editor->json_query({from => ['acq.extract_provider_holding_data', $li->id]});
1100 return 1 unless @$holdings;
1101 my $org_path = $U->get_org_ancestors($ordering_agency);
1102 $org_path = [ reverse (@$org_path) ];
1107 # create a lineitem detail for each copy in the data
1109 my $compiled = extract_lineitem_detail_data($mgr, $org_path, $holdings, $idx);
1110 last unless defined $compiled;
1111 return 0 unless $compiled;
1113 # this takes the price of the last copy and uses it as the lineitem price
1114 # need to determine if a given record would include different prices for the same item
1115 $price = $$compiled{price};
1117 for(1..$$compiled{quantity}) {
1118 my $lid = create_lineitem_detail($mgr,
1119 lineitem => $li->id,
1120 owning_lib => $$compiled{owning_lib},
1121 cn_label => $$compiled{call_number},
1122 fund => $$compiled{fund},
1123 circ_modifier => $$compiled{circ_modifier},
1124 note => $$compiled{note},
1125 location => $$compiled{copy_location},
1126 collection_code => $$compiled{collection_code}
1134 # set the price attr so we'll know the source of the price
1137 attr_name => 'estimated_price',
1138 attr_type => 'lineitem_local_attr_definition',
1139 attr_value => $price,
1143 # if we're creating a purchase order, create the debits
1144 if($li->purchase_order) {
1145 create_lineitem_debits($mgr, $li, $price, 2) or return 0;
1152 # return hash on success, 0 on error, undef on no more holdings
1153 sub extract_lineitem_detail_data {
1154 my($mgr, $org_path, $holdings, $index) = @_;
1156 my @data_list = grep { $_->{holding} eq $index } @$holdings;
1157 return undef unless @data_list;
1159 my %compiled = map { $_->{attr} => $_->{data} } @data_list;
1160 my $base_org = $$org_path[0];
1164 $logger->error("Item import extraction error: $msg");
1165 $logger->error('Holdings Data: ' . OpenSRF::Utils::JSON->perl2JSON(\%compiled));
1166 $mgr->editor->rollback;
1167 $mgr->editor->event(OpenILS::Event->new('ACQ_IMPORT_ERROR', payload => $msg));
1171 $compiled{quantity} ||= 1;
1173 # ---------------------------------------------------------------------
1175 my $code = $compiled{fund_code};
1176 return $killme->('no fund code provided') unless $code;
1178 my $fund = $mgr->cache($base_org, "fund.$code");
1180 # search up the org tree for the most appropriate fund
1181 for my $org (@$org_path) {
1182 $fund = $mgr->editor->search_acq_fund(
1183 {org => $org, code => $code, year => DateTime->now->year}, {idlist => 1})->[0];
1187 return $killme->("no fund with code $code at orgs [@$org_path]") unless $fund;
1188 $compiled{fund} = $fund;
1189 $mgr->cache($base_org, "fund.$code", $fund);
1192 # ---------------------------------------------------------------------
1194 my $sn = $compiled{owning_lib};
1195 return $killme->('no owning_lib defined') unless $sn;
1197 $mgr->cache($base_org, "orgsn.$sn") ||
1198 $mgr->editor->search_actor_org_unit({shortname => $sn}, {idlist => 1})->[0];
1199 return $killme->("invalid owning_lib defined: $sn") unless $org_id;
1200 $compiled{owning_lib} = $org_id;
1201 $mgr->cache($$org_path[0], "orgsn.$sn", $org_id);
1204 # ---------------------------------------------------------------------
1207 $code = $compiled{circ_modifier};
1211 $mod = $mgr->cache($base_org, "mod.$code") ||
1212 $mgr->editor->retrieve_config_circ_modifier($code);
1213 return $killme->("invlalid circ_modifier $code") unless $mod;
1214 $mgr->cache($base_org, "mod.$code", $mod);
1218 $mod = get_default_circ_modifier($mgr, $base_org)
1219 or return $killme->('no circ_modifier defined');
1222 $compiled{circ_modifier} = $mod;
1225 # ---------------------------------------------------------------------
1227 my $name = $compiled{copy_location};
1229 my $loc = $mgr->cache($base_org, "copy_loc.$name");
1231 for my $org (@$org_path) {
1232 $loc = $mgr->editor->search_asset_copy_location(
1233 {owning_lib => $org, name => $name}, {idlist => 1})->[0];
1237 return $killme->("Invalid copy location $name") unless $loc;
1238 $compiled{copy_location} = $loc;
1239 $mgr->cache($base_org, "copy_loc.$name", $loc);
1247 # ----------------------------------------------------------------------------
1248 # Workflow: Given an existing purchase order, import/create the bibs,
1249 # callnumber and copy objects
1250 # ----------------------------------------------------------------------------
1252 __PACKAGE__->register_method(
1253 method => 'create_po_assets',
1254 api_name => 'open-ils.acq.purchase_order.assets.create',
1256 desc => q/Creates assets for each lineitem in the purchase order/,
1258 {desc => 'Authentication token', type => 'string'},
1259 {desc => 'The purchase order id', type => 'number'},
1261 return => {desc => 'Streams a total versus completed counts object, event on error'}
1265 sub create_po_assets {
1266 my($self, $conn, $auth, $po_id) = @_;
1268 my $e = new_editor(authtoken=>$auth, xact=>1);
1269 return $e->die_event unless $e->checkauth;
1270 my $mgr = OpenILS::Application::Acq::BatchManager->new(editor => $e, conn => $conn);
1272 my $po = $e->retrieve_acq_purchase_order($po_id) or return $e->die_event;
1274 my $li_ids = $e->search_acq_lineitem({purchase_order => $po_id}, {idlist => 1});
1276 # it's ugly, but it's fast. Get the total count of lineitem detail objects to process
1277 my $lid_total = $e->json_query({
1278 select => { acqlid => [{aggregate => 1, transform => 'count', column => 'id'}] },
1284 join => {acqpo => {fkey => 'purchase_order', field => 'id'}}
1288 where => {'+acqpo' => {id => $po_id}}
1291 $mgr->total(scalar(@$li_ids) + $lid_total);
1293 create_lineitem_list_assets($mgr, $li_ids) or return $e->die_event;
1296 update_purchase_order($mgr, $po) or return $e->die_event;
1299 return $mgr->respond_complete;
1304 __PACKAGE__->register_method(
1305 method => 'create_purchase_order_api',
1306 api_name => 'open-ils.acq.purchase_order.create',
1308 desc => 'Creates a new purchase order',
1310 {desc => 'Authentication token', type => 'string'},
1311 {desc => 'purchase_order to create', type => 'object'}
1313 return => {desc => 'The purchase order id, Event on failure'}
1317 sub create_purchase_order_api {
1318 my($self, $conn, $auth, $po, $args) = @_;
1321 my $e = new_editor(xact=>1, authtoken=>$auth);
1322 return $e->die_event unless $e->checkauth;
1323 return $e->die_event unless $e->allowed('CREATE_PURCHASE_ORDER', $po->ordering_agency);
1324 my $mgr = OpenILS::Application::Acq::BatchManager->new(editor => $e, conn => $conn);
1327 my %pargs = (ordering_agency => $e->requestor->ws_ou); # default
1328 $pargs{provider} = $po->provider if $po->provider;
1329 $pargs{ordering_agency} = $po->ordering_agency if $po->ordering_agency;
1330 $po = create_purchase_order($mgr, %pargs) or return $e->die_event;
1332 my $li_ids = $$args{lineitems};
1336 for my $li_id (@$li_ids) {
1338 my $li = $e->retrieve_acq_lineitem([
1340 {flesh => 1, flesh_fields => {jub => ['attributes']}}
1341 ]) or return $e->die_event;
1343 $li->provider($po->provider);
1344 $li->purchase_order($po->id);
1345 $li->state('pending-order');
1346 update_lineitem($mgr, $li) or return $e->die_event;
1349 create_lineitem_debits($mgr, $li) or return $e->die_event;
1353 # commit before starting the asset creation
1356 if($li_ids and $$args{create_assets}) {
1357 create_lineitem_list_assets($mgr, $li_ids) or return $e->die_event;
1360 return $mgr->respond_complete;
1364 __PACKAGE__->register_method(
1365 method => 'lineitem_detail_CUD_batch',
1366 api_name => 'open-ils.acq.lineitem_detail.cud.batch',
1369 desc => q/Creates a new purchase order line item detail.
1370 Additionally creates the associated fund_debit/,
1372 {desc => 'Authentication token', type => 'string'},
1373 {desc => 'List of lineitem_details to create', type => 'array'},
1375 return => {desc => 'Streaming response of current position in the array'}
1379 sub lineitem_detail_CUD_batch {
1380 my($self, $conn, $auth, $li_details) = @_;
1382 my $e = new_editor(xact=>1, authtoken=>$auth);
1383 return $e->die_event unless $e->checkauth;
1384 my $mgr = OpenILS::Application::Acq::BatchManager->new(editor => $e, conn => $conn);
1388 $mgr->total(scalar(@$li_details));
1392 for my $lid (@$li_details) {
1394 my $li = $li_cache{$lid->lineitem} || $e->retrieve_acq_lineitem($lid->lineitem);
1397 create_lineitem_detail($mgr, %{$lid->to_bare_hash}) or return $e->die_event;
1399 } elsif($lid->ischanged) {
1400 $e->update_acq_lineitem_detail($lid) or return $e->die_event;
1402 } elsif($lid->isdeleted) {
1403 delete_lineitem_detail($mgr, $lid) or return $e->die_event;
1406 $mgr->respond(li => $li);
1407 $li_cache{$lid->lineitem} = $li;
1411 return $mgr->respond_complete;
1415 __PACKAGE__->register_method(
1416 method => 'receive_po_api',
1417 api_name => 'open-ils.acq.purchase_order.receive'
1420 sub receive_po_api {
1421 my($self, $conn, $auth, $po_id) = @_;
1422 my $e = new_editor(xact => 1, authtoken => $auth);
1423 return $e->die_event unless $e->checkauth;
1424 my $mgr = OpenILS::Application::Acq::BatchManager->new(editor => $e, conn => $conn);
1426 my $po = $e->retrieve_acq_purchase_order($po_id) or return $e->die_event;
1427 return $e->die_event unless $e->allowed('RECEIVE_PURCHASE_ORDER', $po->ordering_agency);
1429 my $li_ids = $e->search_acq_lineitem({purchase_order => $po_id}, {idlist => 1});
1431 for my $li_id (@$li_ids) {
1432 receive_lineitem($mgr, $li_id) or return $e->die_event;
1436 $po->state('received');
1437 update_purchase_order($mgr, $po) or return $e->die_event;
1440 return $mgr->respond_complete;
1444 __PACKAGE__->register_method(
1445 method => 'receive_lineitem_detail_api',
1446 api_name => 'open-ils.acq.lineitem_detail.receive',
1448 desc => 'Mark a lineitem_detail as received',
1450 {desc => 'Authentication token', type => 'string'},
1451 {desc => 'lineitem detail ID', type => 'number'}
1453 return => {desc => '1 on success, Event on error'}
1457 sub receive_lineitem_detail_api {
1458 my($self, $conn, $auth, $lid_id) = @_;
1460 my $e = new_editor(xact=>1, authtoken=>$auth);
1461 return $e->die_event unless $e->checkauth;
1462 my $mgr = OpenILS::Application::Acq::BatchManager->new(editor => $e, conn => $conn);
1464 my $lid = $e->retrieve_acq_lineitem_detail([
1468 acqlid => ['lineitem'],
1469 jub => ['purchase_order']
1474 return $e->die_event unless $e->allowed(
1475 'RECEIVE_PURCHASE_ORDER', $lid->lineitem->purchase_order->ordering_agency);
1477 receive_lineitem_detail($mgr, $lid_id) or return $e->die_event;
1482 __PACKAGE__->register_method(
1483 method => 'receive_lineitem_api',
1484 api_name => 'open-ils.acq.lineitem.receive',
1486 desc => 'Mark a lineitem as received',
1488 {desc => 'Authentication token', type => 'string'},
1489 {desc => 'lineitem detail ID', type => 'number'}
1491 return => {desc => '1 on success, Event on error'}
1495 sub receive_lineitem_api {
1496 my($self, $conn, $auth, $li_id) = @_;
1498 my $e = new_editor(xact=>1, authtoken=>$auth);
1499 return $e->die_event unless $e->checkauth;
1500 my $mgr = OpenILS::Application::Acq::BatchManager->new(editor => $e, conn => $conn);
1502 my $li = $e->retrieve_acq_lineitem([
1506 jub => ['purchase_order']
1509 ]) or return $e->die_event;
1511 return $e->die_event unless $e->allowed(
1512 'RECEIVE_PURCHASE_ORDER', $li->purchase_order->ordering_agency);
1514 receive_lineitem($mgr, $li_id) or return $e->die_event;
1520 __PACKAGE__->register_method(
1521 method => 'rollback_receive_po_api',
1522 api_name => 'open-ils.acq.purchase_order.receive.rollback'
1525 sub rollback_receive_po_api {
1526 my($self, $conn, $auth, $po_id) = @_;
1527 my $e = new_editor(xact => 1, authtoken => $auth);
1528 return $e->die_event unless $e->checkauth;
1529 my $mgr = OpenILS::Application::Acq::BatchManager->new(editor => $e, conn => $conn);
1531 my $po = $e->retrieve_acq_purchase_order($po_id) or return $e->die_event;
1532 return $e->die_event unless $e->allowed('RECEIVE_PURCHASE_ORDER', $po->ordering_agency);
1534 my $li_ids = $e->search_acq_lineitem({purchase_order => $po_id}, {idlist => 1});
1536 for my $li_id (@$li_ids) {
1537 rollback_receive_lineitem($mgr, $li_id) or return $e->die_event;
1541 $po->state('on-order');
1542 update_purchase_order($mgr, $po) or return $e->die_event;
1545 return $mgr->respond_complete;
1549 __PACKAGE__->register_method(
1550 method => 'rollback_receive_lineitem_detail_api',
1551 api_name => 'open-ils.acq.lineitem_detail.receive.rollback',
1553 desc => 'Mark a lineitem_detail as received',
1555 {desc => 'Authentication token', type => 'string'},
1556 {desc => 'lineitem detail ID', type => 'number'}
1558 return => {desc => '1 on success, Event on error'}
1562 sub rollback_receive_lineitem_detail_api {
1563 my($self, $conn, $auth, $lid_id) = @_;
1565 my $e = new_editor(xact=>1, authtoken=>$auth);
1566 return $e->die_event unless $e->checkauth;
1567 my $mgr = OpenILS::Application::Acq::BatchManager->new(editor => $e, conn => $conn);
1569 my $lid = $e->retrieve_acq_lineitem_detail([
1573 acqlid => ['lineitem'],
1574 jub => ['purchase_order']
1578 my $li = $lid->lineitem;
1579 my $po = $li->purchase_order;
1581 return $e->die_event unless $e->allowed('RECEIVE_PURCHASE_ORDER', $po->ordering_agency);
1582 rollback_receive_lineitem_detail($mgr, $lid_id) or return $e->die_event;
1584 $li->state('on-order');
1585 $po->state('on-order');
1586 udpate_lineitem($mgr, $li) or return $e->die_event;
1587 udpate_purchase_order($mgr, $po) or return $e->die_event;
1593 __PACKAGE__->register_method(
1594 method => 'rollback_receive_lineitem_api',
1595 api_name => 'open-ils.acq.lineitem.receive.rollback',
1597 desc => 'Mark a lineitem as received',
1599 {desc => 'Authentication token', type => 'string'},
1600 {desc => 'lineitem detail ID', type => 'number'}
1602 return => {desc => '1 on success, Event on error'}
1606 sub rollback_receive_lineitem_api {
1607 my($self, $conn, $auth, $li_id) = @_;
1609 my $e = new_editor(xact=>1, authtoken=>$auth);
1610 return $e->die_event unless $e->checkauth;
1611 my $mgr = OpenILS::Application::Acq::BatchManager->new(editor => $e, conn => $conn);
1613 my $li = $e->retrieve_acq_lineitem_detail([
1617 jub => ['purchase_order']
1621 my $po = $li->purchase_order;
1623 return $e->die_event unless $e->allowed('RECEIVE_PURCHASE_ORDER', $po->ordering_agency);
1625 rollback_receive_lineitem($mgr, $li_id) or return $e->die_event;
1627 $po->state('on-order');
1628 update_purchase_order($mgr, $po) or return $e->die_event;
1635 __PACKAGE__->register_method(
1636 method => 'set_lineitem_price_api',
1637 api_name => 'open-ils.acq.lineitem.price.set',
1639 desc => 'Set lineitem price. If debits already exist, update them as well',
1641 {desc => 'Authentication token', type => 'string'},
1642 {desc => 'lineitem ID', type => 'number'}
1644 return => {desc => 'status blob, Event on error'}
1648 sub set_lineitem_price_api {
1649 my($self, $conn, $auth, $li_id, $price, $currency) = @_;
1651 my $e = new_editor(xact=>1, authtoken=>$auth);
1652 return $e->die_event unless $e->checkauth;
1653 my $mgr = OpenILS::Application::Acq::BatchManager->new(editor => $e, conn => $conn);
1657 my $li = $e->retrieve_acq_lineitem($li_id) or return $e->die_event;
1659 # update the local attr for estimated price
1662 attr_name => 'estimated_price',
1663 attr_type => 'lineitem_local_attr_definition',
1664 attr_value => $price,
1666 ) or return $e->die_event;
1668 my $lid_ids = $e->search_acq_lineitem_detail(
1669 {lineitem => $li_id, fund_debit => {'!=' => undef}},
1673 for my $lid_id (@$lid_ids) {
1675 my $lid = $e->retrieve_acq_lineitem_detail([
1677 flesh => 1, flesh_fields => {acqlid => ['fund', 'fund_debit']}}
1680 # onless otherwise specified, assume currency of new price is same as currency type of the fund
1681 $currency ||= $lid->fund->currency_type;
1682 my $amount = $price;
1684 if($lid->fund->currency_type ne $currency) {
1685 $amount = currency_conversion($mgr, $currency, $lid->fund->currency_type, $price);
1688 $lid->fund_debit->origin_currency_type($currency);
1689 $lid->fund_debit->origin_amount($price);
1690 $lid->fund_debit->amount($amount);
1692 $e->update_acq_fund_debit($lid->fund_debit) or return $e->die_event;
1698 return $mgr->respond_complete;
1702 __PACKAGE__->register_method(
1703 method => 'clone_picklist_api',
1704 api_name => 'open-ils.acq.picklist.clone',
1706 desc => 'Clones a picklist, including lineitem and lineitem details',
1708 {desc => 'Authentication token', type => 'string'},
1709 {desc => 'Picklist ID', type => 'number'},
1710 {desc => 'New Picklist Name', type => 'string'}
1712 return => {desc => 'status blob, Event on error'}
1716 sub clone_picklist_api {
1717 my($self, $conn, $auth, $pl_id, $name) = @_;
1719 my $e = new_editor(xact=>1, authtoken=>$auth);
1720 return $e->die_event unless $e->checkauth;
1721 my $mgr = OpenILS::Application::Acq::BatchManager->new(editor => $e, conn => $conn);
1723 my $old_pl = $e->retrieve_acq_picklist($pl_id);
1724 my $new_pl = create_picklist($mgr, %{$old_pl->to_bare_hash}, name => $name) or return $e->die_event;
1726 my $li_ids = $e->search_acq_lineitem({picklist => $pl_id}, {idlist => 1});
1728 for my $li_id (@$li_ids) {
1730 # copy the lineitems
1731 my $li = $e->retrieve_acq_lineitem($li_id);
1732 my $new_li = create_lineitem($mgr, %{$li->to_bare_hash}, picklist => $new_pl->id) or return $e->die_event;
1734 my $lid_ids = $e->search_acq_lineitem_detail({lineitem => $li_id}, {idlist => 1});
1735 for my $lid_id (@$lid_ids) {
1737 # copy the lineitem details
1738 my $lid = $e->retrieve_acq_lineitem_detail($lid_id);
1739 create_lineitem_detail($mgr, %{$lid->to_bare_hash}, lineitem => $new_li->id) or return $e->die_event;
1746 return $mgr->respond_complete;
1750 __PACKAGE__->register_method(
1751 method => 'merge_picklist_api',
1752 api_name => 'open-ils.acq.picklist.merge',
1754 desc => 'Merges 2 or more picklists into a single list',
1756 {desc => 'Authentication token', type => 'string'},
1757 {desc => 'Lead Picklist ID', type => 'number'},
1758 {desc => 'List of subordinate picklist IDs', type => 'array'}
1760 return => {desc => 'status blob, Event on error'}
1764 sub merge_picklist_api {
1765 my($self, $conn, $auth, $lead_pl, $pl_list) = @_;
1767 my $e = new_editor(xact=>1, authtoken=>$auth);
1768 return $e->die_event unless $e->checkauth;
1769 my $mgr = OpenILS::Application::Acq::BatchManager->new(editor => $e, conn => $conn);
1771 # XXX perms on each picklist modified
1773 # point all of the lineitems at the lead picklist
1774 my $li_ids = $e->search_acq_lineitem({picklist => $pl_list}, {idlist => 1});
1776 for my $li_id (@$li_ids) {
1777 my $li = $e->retrieve_acq_lineitem($li_id);
1778 $li->picklist($lead_pl);
1779 update_lineitem($mgr, $li) or return $e->die_event;
1783 # now delete the subordinate lists
1784 for my $pl_id (@$pl_list) {
1785 my $pl = $e->retrieve_acq_picklist($pl_id);
1786 $e->delete_acq_picklist($pl) or return $e->die_event;
1790 return $mgr->respond_complete;
1794 __PACKAGE__->register_method(
1795 method => 'delete_picklist_api',
1796 api_name => 'open-ils.acq.picklist.delete',
1798 desc => q/Deletes a picklist. It also deletes any lineitems in the "new" state.
1799 Other attached lineitems are detached'/,
1801 {desc => 'Authentication token', type => 'string'},
1802 {desc => 'Picklist ID to delete', type => 'number'}
1804 return => {desc => '1 on success, Event on error'}
1808 sub delete_picklist_api {
1809 my($self, $conn, $auth, $picklist_id) = @_;
1810 my $e = new_editor(xact=>1, authtoken=>$auth);
1811 return $e->die_event unless $e->checkauth;
1812 my $mgr = OpenILS::Application::Acq::BatchManager->new(editor => $e, conn => $conn);
1813 my $pl = $e->retrieve_acq_picklist($picklist_id) or return $e->die_event;
1814 delete_picklist($mgr, $pl) or return $e->die_event;
1816 return $mgr->respond_complete;
1821 __PACKAGE__->register_method(
1822 method => 'activate_purchase_order',
1823 api_name => 'open-ils.acq.purchase_order.activate',
1825 desc => q/Activates a purchase order. This updates the status of the PO
1826 and Lineitems to 'on-order'. Activated PO's are ready for EDI delivery
1829 {desc => 'Authentication token', type => 'string'},
1830 {desc => 'Purchase ID', type => 'number'}
1832 return => {desc => '1 on success, Event on error'}
1836 sub activate_purchase_order {
1837 my($self, $conn, $auth, $po_id) = @_;
1838 my $e = new_editor(xact=>1, authtoken=>$auth);
1839 return $e->die_event unless $e->checkauth;
1840 my $mgr = OpenILS::Application::Acq::BatchManager->new(editor => $e, conn => $conn);
1842 my $po = $e->retrieve_acq_purchase_order($po_id) or return $e->die_event;
1843 return $e->die_event unless $e->allowed('CREATE_PURCHASE_ORDER', $po->ordering_agency);
1845 $po->state('on-order');
1846 update_purchase_order($mgr, $po) or return $e->die_event;
1849 {purchase_order => $po_id, state => 'pending-order'},
1853 while( my $li = $e->search_acq_lineitem($query)->[0] ) {
1854 $li->state('on-order');
1855 update_lineitem($mgr, $li) or return $e->die_event;
1864 __PACKAGE__->register_method(
1865 method => 'split_purchase_order_by_lineitems',
1866 api_name => 'open-ils.acq.purchase_order.split_by_lineitems',
1868 desc => q/Splits a PO into many POs, 1 per lineitem. Only works for
1869 POs a) with more than one lineitems, and b) in the "pending" state./,
1871 {desc => 'Authentication token', type => 'string'},
1872 {desc => 'Purchase order ID', type => 'number'}
1874 return => {desc => 'list of new PO IDs on success, Event on error'}
1878 sub split_purchase_order_by_lineitems {
1879 my ($self, $conn, $auth, $po_id) = @_;
1881 my $e = new_editor("xact" => 1, "authtoken" => $auth);
1882 return $e->die_event unless $e->checkauth;
1884 my $po = $e->retrieve_acq_purchase_order([
1887 "flesh_fields" => {"acqpo" => [qw/lineitems notes/]}
1889 ]) or return $e->die_event;
1891 return $e->die_event
1892 unless $e->allowed("CREATE_PURCHASE_ORDER", $po->ordering_agency);
1894 unless ($po->state eq "pending") {
1896 return new OpenILS::Event("ACQ_PURCHASE_ORDER_TOO_LATE");
1899 unless (@{$po->lineitems} > 1) {
1901 return new OpenILS::Event("ACQ_PURCHASE_ORDER_TOO_SHORT");
1904 # To split an existing PO into many, it seems unwise to just delete the
1905 # original PO, so we'll instead detach all of the original POs' lineitems
1906 # but the first, then create new POs for each of the remaining LIs, and
1907 # then attach the LIs to their new POs.
1909 my @po_ids = ($po->id);
1910 my @moving_li = @{$po->lineitems};
1911 shift @moving_li; # discard first LI
1913 foreach my $li (@moving_li) {
1914 my $new_po = $po->clone;
1916 $new_po->clear_name;
1917 $new_po->creator($e->requestor->id);
1918 $new_po->editor($e->requestor->id);
1919 $new_po->owner($e->requestor->id);
1920 $new_po->edit_time("now");
1921 $new_po->create_time("now");
1923 $new_po = $e->create_acq_purchase_order($new_po);
1925 # Clone any notes attached to the old PO and attach to the new one.
1926 foreach my $note (@{$po->notes}) {
1927 my $new_note = $note->clone;
1928 $new_note->clear_id;
1929 $new_note->edit_time("now");
1930 $new_note->purchase_order($new_po->id);
1931 $e->create_acq_po_note($new_note);
1934 $li->edit_time("now");
1935 $li->purchase_order($new_po->id);
1936 $e->update_acq_lineitem($li);
1938 push @po_ids, $new_po->id;
1941 $po->edit_time("now");
1942 $e->update_acq_purchase_order($po);
1944 return \@po_ids if $e->commit;
1945 return $e->die_event;