return $counts->[0]->{id};
}
+__PACKAGE__->register_method(
+ method => 'transfer_lineitem',
+ api_name => 'open-ils.acq.lineitem.transfer_to_bib',
+ signature => {
+ desc => q/Transfers a lineitem and all associated
+ assets to a different target bib record /,
+ params => [
+ {desc => 'Authentication token', type => 'string'},
+ {desc => 'The lineitem id', type => 'number'},
+ {desc => 'The target bib record id', type => 'number'},
+ ],
+ return => {desc => q/1 on success, event on error/}
+ }
+);
+
+sub transfer_lineitem {
+ my($self, $conn, $auth, $li_id, $bib_id, $ops) = @_;
+ $ops ||= {}; # reserved for later use (e.g. holds transfer)
+
+ my $e = new_editor(authtoken=>$auth, xact=>1);
+ return $e->die_event unless $e->checkauth;
+
+ my ($li, $evt, $perm_org) = fetch_and_check_li($e, $li_id, 'write');
+ return $evt if $evt;
+
+ # Sanity check the target bib.
+
+ my $bre = $e->retrieve_biblio_record_entry($bib_id)
+ or return $e->die_event;
+
+ if ($U->is_true($bre->deleted)) {
+ $e->rollback;
+ return OpenILS::Event->new(
+ 'BAD_PARAMS', {note => 'Target bib is deleted'});
+ }
+
+ # Point the lineitem at the selected bib record. Note this will
+ # work even if the lineitem is not currently pointing at any bib
+ # record. IOW, this could be used to manually link LI's to bibs.
+
+ my $orig_bib_id = $li->eg_bib_id; # capture for later.
+ $li->eg_bib_id($bib_id);
+ $li->edit_time('now');
+ $li->editor($e->requestor->id);
+ $e->update_acq_lineitem($li) or return $e->die_event;
+
+
+ # Transfer any asset.call_number's (with their linked asset.copy's)
+ # that were created for this lineitem to the new bib record.
+ my $lid_ids = $e->json_query({
+ select => {acqlid => ['id']},
+ from => 'acqlid',
+ where => {lineitem => $li_id}
+ });
+
+ # TODO: Create or transfer volumes to the new bib record. Create if
+ # some copies will remain linked to the old volume, merge if all
+ # copies are moving.
+ # TODO: Transfer monograph parts.
+
+ for my $lid_id (map { $_->{id} } @$lid_ids) {
+
+ my $lid = $e->retrieve_acq_lineitem_detail($lid_id);
+
+ }
+
+ $e->commit;
+
+ return 1;
+}
+
1;