1 package OpenILS::Application::Cat::AssetCommon;
2 use strict; use warnings;
3 use OpenILS::Application::Cat::BibCommon;
4 use OpenILS::Utils::CStoreEditor q/:funcs/;
5 use OpenSRF::Utils::Logger qw($logger);
6 use OpenILS::Application::Cat::Merge;
7 use OpenILS::Application::AppUtils;
8 use OpenILS::Utils::Fieldmapper;
9 use OpenILS::Const qw/:const/;
10 use OpenSRF::AppSession;
12 use OpenILS::Utils::Penalty;
13 use OpenILS::Application::Circ::CircCommon;
14 my $U = 'OpenILS::Application::AppUtils';
17 # ---------------------------------------------------------------------------
18 # Shared copy mangling code. Do not publish methods from here.
19 # ---------------------------------------------------------------------------
21 sub org_cannot_have_vols {
22 my($class, $e, $org_id) = @_;
23 my $org = $e->retrieve_actor_org_unit([
26 flesh_fields => {aou => ['ou_type']}
27 }]) or return $e->event;
29 return OpenILS::Event->new('ORG_CANNOT_HAVE_VOLS')
30 unless $U->is_true($org->ou_type->can_have_vols);
39 if(defined $copy->price) {
40 my $p = $copy->price || 0;
45 my $d = $copy->deposit_amount || 0;
47 $copy->deposit_amount($d);
51 my($class, $editor, $vol, $copy) = @_;
53 return $editor->event unless
54 $editor->allowed('CREATE_COPY', $class->copy_perm_org($vol, $copy));
56 my $existing = $editor->search_asset_copy(
57 { barcode => $copy->barcode, deleted => 'f' } );
59 return OpenILS::Event->new('ITEM_BARCODE_EXISTS') if @$existing;
61 my $copy_loc = $editor->search_asset_copy_location(
62 { id => $copy->location, deleted => 'f' } );
64 return OpenILS::Event->new('COPY_LOCATION_NOT_FOUND') unless @$copy_loc;
66 # see if the volume this copy references is marked as deleted
67 return OpenILS::Event->new('VOLUME_DELETED', vol => $vol->id)
68 if $U->is_true($vol->deleted);
71 my $org = (ref $copy->circ_lib) ? $copy->circ_lib->id : $copy->circ_lib;
72 return $evt if ($evt = $class->org_cannot_have_vols($editor, $org));
75 $copy->editor($editor->requestor->id);
76 $copy->creator($editor->requestor->id);
77 $copy->create_date('now');
78 $copy->call_number($vol->id);
79 $class->fix_copy_price($copy);
81 my $cp = $editor->create_asset_copy($copy) or return $editor->die_event;
87 # 'delete_stats' is somewhat of a misnomer. With no flags set, this method
88 # still deletes any existing maps not represented in $copy->stat_cat_entries,
89 # but aborts when $copy->stat_cat_entries is empty or undefined. If
90 # 'delete_stats' is true, this method will delete all the maps when
91 # $copy->stat_cat_entries is empty or undefined.
93 # The 'add_or_update_only' flag is more straightforward. It adds missing
94 # maps, updates present maps with any new values, and leaves the rest
96 sub update_copy_stat_entries {
97 my($class, $editor, $copy, $delete_stats, $add_or_update_only) = @_;
99 return undef if $copy->isdeleted;
100 return undef unless $copy->ischanged or $copy->isnew;
103 my $entries = $copy->stat_cat_entries;
105 if( $delete_stats ) {
106 $entries = ($entries and @$entries) ? $entries : [];
108 return undef unless ($entries and @$entries);
111 my $maps = $editor->search_asset_stat_cat_entry_copy_map({owning_copy=>$copy->id});
114 # if there is no stat cat entry on the copy who's id matches the
115 # current map's id, remove the map from the database
116 for my $map (@$maps) {
117 if (!$add_or_update_only) {
118 if(! grep { $_->id == $map->stat_cat_entry } @$entries ) {
120 $logger->info("copy update found stale ".
121 "stat cat entry map ".$map->id. " on copy ".$copy->id);
123 $editor->delete_asset_stat_cat_entry_copy_map($map)
124 or return $editor->event;
127 if( grep { $_->stat_cat == $map->stat_cat and $_->id != $map->stat_cat_entry } @$entries ) {
129 $logger->info("copy update found ".
130 "stat cat entry map ".$map->id. " needing update on copy ".$copy->id);
132 $editor->delete_asset_stat_cat_entry_copy_map($map)
133 or return $editor->event;
139 # go through the stat cat update/create process
140 for my $entry (@$entries) {
143 # if this link already exists in the DB, don't attempt to re-create it
144 next if( grep{$_->stat_cat_entry == $entry->id} @$maps );
146 my $new_map = Fieldmapper::asset::stat_cat_entry_copy_map->new();
148 my $sc = ref($entry->stat_cat) ? $entry->stat_cat->id : $entry->stat_cat;
150 $new_map->stat_cat( $sc );
151 $new_map->stat_cat_entry( $entry->id );
152 $new_map->owning_copy( $copy->id );
154 $editor->create_asset_stat_cat_entry_copy_map($new_map)
155 or return $editor->event;
157 $logger->info("copy update created new stat cat entry map ".$editor->data);
163 # if 'delete_maps' is true, the copy->parts data is treated as the
164 # authoritative list for the copy. existing part maps not targeting
165 # these parts will be deleted from the DB
166 sub update_copy_parts {
167 my($class, $editor, $copy, $delete_maps, $create_parts) = @_;
169 return undef if $copy->isdeleted;
170 return undef unless $copy->ischanged or $copy->isnew;
173 my $incoming_parts = $copy->parts;
176 $incoming_parts = ($incoming_parts and @$incoming_parts) ? $incoming_parts : [];
178 return undef unless ($incoming_parts and @$incoming_parts);
181 my $maps = $editor->search_asset_copy_part_map({target_copy=>$copy->id});
184 # if there is no part map on the copy who's id matches the
185 # current map's id, remove the map from the database
186 for my $map (@$maps) {
187 if(! grep { $_->id == $map->part } @$incoming_parts ) {
189 $logger->info("copy update found stale ".
190 "monographic part map ".$map->id. " on copy ".$copy->id);
192 $editor->delete_asset_copy_part_map($map)
193 or return $editor->event;
198 # go through the part map update/create process
199 for my $incoming_part (@$incoming_parts) {
200 next unless $incoming_part;
202 # if this link already exists in the DB, don't attempt to re-create it
203 next if( grep{$_->part == $incoming_part->id} @$maps );
205 if ($incoming_part->isnew) {
206 next unless $create_parts;
207 my $new_part = Fieldmapper::biblio::monograph_part->new();
208 $new_part->record( $incoming_part->record );
209 $new_part->label( $incoming_part->label );
210 $incoming_part = $editor->create_biblio_monograph_part($new_part)
211 or return $editor->event;
214 my $new_map = Fieldmapper::asset::copy_part_map->new();
216 $new_map->part( $incoming_part->id );
217 $new_map->target_copy( $copy->id );
219 $editor->create_asset_copy_part_map($new_map)
220 or return $editor->event;
222 $logger->info("copy update created new monographic part copy map ".$editor->data);
230 sub update_copy_notes {
231 my($class, $editor, $copy) = @_;
233 return undef if $copy->isdeleted;
236 my $incoming_notes = $copy->notes;
238 for my $incoming_note (@$incoming_notes) {
239 next unless $incoming_note;
241 if ($incoming_note->isnew) {
242 next if ($incoming_note->isdeleted); # if it was added and deleted in the same session
244 my $new_note = Fieldmapper::asset::copy_note->new();
245 $new_note->owning_copy( $copy->id );
246 $new_note->pub( $incoming_note->pub );
247 $new_note->title( $incoming_note->title );
248 $new_note->value( $incoming_note->value );
249 $new_note->creator( $incoming_note->creator || $editor->requestor->id );
250 $incoming_note = $editor->create_asset_copy_note($new_note)
251 or return $editor->event;
253 } elsif ($incoming_note->ischanged) {
254 $incoming_note = $editor->update_asset_copy_note($incoming_note)
255 } elsif ($incoming_note->isdeleted) {
256 $incoming_note = $editor->delete_asset_copy_note($incoming_note)
264 sub update_copy_alerts {
265 my($class, $editor, $copy) = @_;
267 return undef if $copy->isdeleted;
270 my $incoming_copy_alerts = $copy->copy_alerts;
272 for my $incoming_copy_alert (@$incoming_copy_alerts) {
273 next unless $incoming_copy_alert;
275 if ($incoming_copy_alert->isnew) {
276 next if ($incoming_copy_alert->isdeleted); # if it was added and deleted in the same session
278 my $new_copy_alert = Fieldmapper::asset::copy_alert->new();
279 $new_copy_alert->copy( $copy->id );
280 $new_copy_alert->temp( $incoming_copy_alert->temp );
281 $new_copy_alert->ack_time( $incoming_copy_alert->ack_time );
282 $new_copy_alert->note( $incoming_copy_alert->note );
283 $new_copy_alert->alert_type( $incoming_copy_alert->alert_type );
284 $new_copy_alert->create_staff( $incoming_copy_alert->create_staff || $editor->requestor->id );
285 $incoming_copy_alert = $editor->create_asset_copy_alert($new_copy_alert)
286 or return $editor->event;
287 } elsif ($incoming_copy_alert->ischanged) {
288 $incoming_copy_alert = $editor->update_asset_copy_alert($incoming_copy_alert)
289 } elsif ($incoming_copy_alert->isdeleted) {
290 $incoming_copy_alert = $editor->delete_asset_copy_alert($incoming_copy_alert->id)
298 sub update_copy_tags {
299 my($class, $editor, $copy) = @_;
301 return undef if $copy->isdeleted;
304 my $incoming_maps = $copy->tags;
306 for my $incoming_map (@$incoming_maps) {
307 next unless $incoming_map;
309 if ($incoming_map->isnew) {
310 next if ($incoming_map->isdeleted); # if it was added and deleted in the same session
313 if ($incoming_map->tag->isnew) {
314 my $new_tag = Fieldmapper::asset::copy_tag->new();
315 $new_tag->owner( $incoming_map->tag->owner );
316 $new_tag->label( $incoming_map->tag->label );
317 $new_tag->tag_type( $incoming_map->tag->tag_type );
318 $new_tag->pub( $incoming_map->tag->pub );
319 my $tag = $editor->create_asset_copy_tag($new_tag)
320 or return $editor->event;
323 $tag_id = $incoming_map->tag->id;
325 my $new_map = Fieldmapper::asset::copy_tag_copy_map->new();
326 $new_map->copy( $copy->id );
327 $new_map->tag( $tag_id );
328 $incoming_map = $editor->create_asset_copy_tag_copy_map($new_map)
329 or return $editor->event;
331 } elsif ($incoming_map->ischanged) {
332 $incoming_map = $editor->update_asset_copy_tag_copy_map($incoming_map)
333 } elsif ($incoming_map->isdeleted) {
334 $incoming_map = $editor->delete_asset_copy_tag_copy_map($incoming_map)
343 my($class, $editor, $override, $vol, $copy, $retarget_holds, $force_delete_empty_bib) = @_;
345 $override = { all => 1 } if($override && !ref $override);
346 $override = { all => 0 } if(!ref $override);
348 # Duplicated check from create_copy in case a copy template with a deleted location is applied later
349 my $copy_loc = $editor->search_asset_copy_location(
350 { id => $copy->location, deleted => 'f' } );
352 return OpenILS::Event->new('COPY_LOCATION_NOT_FOUND') unless @$copy_loc;
355 my $org = (ref $copy->circ_lib) ? $copy->circ_lib->id : $copy->circ_lib;
356 return $evt if ( $evt = $class->org_cannot_have_vols($editor, $org) );
358 $logger->info("vol-update: updating copy ".$copy->id);
359 my $orig_copy = $editor->retrieve_asset_copy($copy->id);
361 # Call-number may have changed, find the original
362 my $orig_vol_id = $editor->json_query({select => {acp => ['call_number']}, from => 'acp', where => {id => $copy->id}});
363 my $orig_vol = $editor->retrieve_asset_call_number($orig_vol_id->[0]->{call_number});
365 $copy->editor($editor->requestor->id);
366 $copy->edit_date('now');
368 $copy->age_protect( $copy->age_protect->id )
369 if ref $copy->age_protect;
371 $class->fix_copy_price($copy);
372 $class->check_hold_retarget($editor, $copy, $orig_copy, $retarget_holds);
374 return $editor->event unless $editor->update_asset_copy($copy);
375 return $class->remove_empty_objects($editor, $override, $orig_vol, $force_delete_empty_bib);
378 sub check_hold_retarget {
379 my($class, $editor, $copy, $orig_copy, $retarget_holds) = @_;
380 return unless $retarget_holds;
382 if( !($copy->isdeleted or $U->is_true($copy->deleted)) ) {
383 # see if a status change warrants a retarget
385 $orig_copy = $editor->retrieve_asset_copy($copy->id) unless $orig_copy;
387 if($orig_copy->status == $copy->status) {
388 # no status change, no retarget
392 my $stat = $editor->retrieve_config_copy_status($copy->status);
394 # new status is holdable, no retarget. Later add logic to find potential
395 # holds and retarget those to pick up the newly available copy
396 return if $U->is_true($stat->holdable);
399 my $hold_ids = $editor->search_action_hold_request(
400 { current_copy => $copy->id,
401 cancel_time => undef,
402 fulfillment_time => undef
406 push(@$retarget_holds, @$hold_ids);
409 # TODO: get Booking.pm to use this shared method
410 sub fetch_copies_by_ids {
411 my ($class, $e, $copy_ids) = @_;
412 my $results = $e->search_asset_copy([
414 {flesh => 1, flesh_fields => {acp => ['call_number']}}
416 return $results if ref($results) eq 'ARRAY';
420 # this does the actual work
421 sub update_fleshed_copies {
422 my($class, $editor, $override, $vol, $copies, $delete_stats, $retarget_holds, $force_delete_empty_bib, $create_parts) = @_;
424 $override = { all => 1 } if($override && !ref $override);
425 $override = { all => 0 } if(!ref $override);
428 my $fetchvol = ($vol) ? 0 : 1;
431 $cache{$vol->id} = $vol if $vol;
434 my ($original_copy, $cache_ref, $editor, $class, $logger) = @_;
436 my $copyid = $original_copy->id;
437 $logger->info("vol-update: inspecting copy $copyid");
440 if (!($vol = $cache_ref->{$original_copy->call_number})) {
441 $vol = $cache_ref->{$original_copy->call_number} =
442 $editor->retrieve_asset_call_number($original_copy->call_number);
443 return $editor->event unless $vol;
446 return $editor->event unless
447 $editor->allowed('UPDATE_COPY', $class->copy_perm_org($vol, $original_copy));
449 return; # return nothing if all checks pass
452 my $original_copies = $class->fetch_copies_by_ids( $editor, map { $_->id } @$copies );
453 for my $original_copy (@$original_copies) {
454 my $event = process_copy($original_copy, \%cache, $editor, $class, $logger);
455 return $event if $event;
458 for my $copy (@$copies) {
459 my $event = process_copy($copy, \%cache, $editor, $class, $logger);
460 return $event if $event;
462 $copy->editor($editor->requestor->id);
463 $copy->edit_date('now');
465 $copy->status( $copy->status->id ) if ref($copy->status);
466 $copy->location( $copy->location->id ) if ref($copy->location);
467 $copy->circ_lib( $copy->circ_lib->id ) if ref($copy->circ_lib);
469 my $parts = $copy->parts;
472 my $sc_entries = $copy->stat_cat_entries;
473 $copy->clear_stat_cat_entries;
475 my $notes = $copy->notes;
478 my $tags = $copy->tags;
481 my $copy_alerts = $copy->copy_alerts;
482 $copy->clear_copy_alerts;
484 if( $copy->isdeleted ) {
485 $evt = $class->delete_copy($editor, $override, $vol, $copy, $retarget_holds, $force_delete_empty_bib);
488 } elsif( $copy->isnew ) {
489 $evt = $class->create_copy( $editor, $vol, $copy );
492 } elsif( $copy->ischanged ) {
494 $evt = $class->update_copy( $editor, $override, $vol, $copy, $retarget_holds, $force_delete_empty_bib);
498 $copy->stat_cat_entries( $sc_entries );
499 $evt = $class->update_copy_stat_entries($editor, $copy, $delete_stats);
501 $copy->parts( $parts );
502 # probably okay to use $delete_stats here for simplicity
503 $evt = $class->update_copy_parts($editor, $copy, $delete_stats, $create_parts);
505 $copy->notes( $notes );
506 $evt = $class->update_copy_notes($editor, $copy);
508 $copy->tags( $tags );
509 $evt = $class->update_copy_tags($editor, $copy);
511 $copy->copy_alerts( $copy_alerts );
512 $evt = $class->update_copy_alerts($editor, $copy);
517 $logger->debug("vol-update: done updating copy batch");
524 my($class, $editor, $override, $vol, $copy, $retarget_holds, $force_delete_empty_bib, $skip_empty_cleanup) = @_;
526 return $editor->event unless
527 $editor->allowed('DELETE_COPY', $class->copy_perm_org($vol, $copy));
529 $override = { all => 1 } if($override && !ref $override);
530 $override = { all => 0 } if(!ref $override);
532 my $stat = $U->copy_status($copy->status);
533 if ($U->is_true($stat->restrict_copy_delete)) {
534 if ($override->{all} || grep { $_ eq 'COPY_DELETE_WARNING' } @{$override->{events}}) {
535 return $editor->event unless $editor->allowed('COPY_DELETE_WARNING.override', $class->copy_perm_org($vol, $copy))
537 return OpenILS::Event->new('COPY_DELETE_WARNING', payload => $copy->id )
541 $logger->info("vol-update: deleting copy ".$copy->id);
544 $copy->editor($editor->requestor->id);
545 $copy->edit_date('now');
546 $editor->update_asset_copy($copy) or return $editor->event;
548 # Cancel any open transits for this copy
549 my $transits = $editor->search_action_transit_copy(
550 { target_copy=>$copy->id, dest_recv_time => undef, cancel_time => undef } );
552 for my $t (@$transits) {
553 $t->cancel_time('now');
554 $editor->update_action_transit_copy($t)
555 or return $editor->event;
558 my $evt = $class->cancel_copy_holds($editor, $copy);
561 $class->check_hold_retarget($editor, $copy, undef, $retarget_holds);
563 return undef if $skip_empty_cleanup;
565 return $class->remove_empty_objects($editor, $override, $vol, $force_delete_empty_bib);
569 # deletes all holds that specifically target the deleted copy
570 sub cancel_copy_holds {
571 my($class, $editor, $copy) = @_;
573 my $holds = $editor->search_action_hold_request({
575 hold_type => [qw/C R F/],
576 cancel_time => undef,
577 fulfillment_time => undef
580 return $class->cancel_hold_list($editor, $holds);
583 # deletes all holds that specifically target the deleted volume
584 sub cancel_volume_holds {
585 my($class, $editor, $volume) = @_;
587 my $holds = $editor->search_action_hold_request({
588 target => $volume->id,
590 cancel_time => undef,
591 fulfillment_time => undef
594 return $class->cancel_hold_list($editor, $holds);
597 sub cancel_hold_list {
598 my($class, $editor, $holds) = @_;
600 for my $hold (@$holds) {
602 $hold->cancel_time('now');
603 $hold->cancel_cause(1); # un-targeted expiration. Do we need an alternate "target deleted" cause?
604 $editor->update_action_hold_request($hold) or return $editor->die_event;
606 # Update our copy of the hold to pick up the cancel_time
607 # before we pass it off to A/T.
608 $hold = $editor->retrieve_action_hold_request($hold->id);
610 # tell A/T the hold was cancelled. Don't wait for a response..
611 my $at_ses = OpenSRF::AppSession->create('open-ils.trigger');
613 'open-ils.trigger.event.autocreate',
614 'hold_request.cancel.expire_no_target',
615 $hold, $hold->pickup_lib);
621 sub test_perm_against_original_owning_lib {
622 my($class, $editor, $perm, $vid) = @_;
623 my $vol = $editor->retrieve_asset_call_number($vid) or return $editor->event;
624 return $editor->die_event unless $editor->allowed($perm, $vol->owning_lib);
629 my($class, $override, $editor, $vol) = @_;
632 return (undef, $evt) if ( $evt = $class->org_cannot_have_vols($editor, $vol->owning_lib) );
634 $override = { all => 1 } if($override && !ref $override);
635 $override = { all => 0 } if(!ref $override);
637 # see if the record this volume references is marked as deleted
638 my $rec = $editor->retrieve_biblio_record_entry($vol->record)
639 or return $editor->die_event;
643 OpenILS::Event->new('BIB_RECORD_DELETED', rec => $rec->id)
644 ) if $U->is_true($rec->deleted);
646 # first lets see if there are any collisions
647 my $vols = $editor->search_asset_call_number( {
648 owning_lib => $vol->owning_lib,
649 record => $vol->record,
650 label => $vol->label,
651 prefix => $vol->prefix,
652 suffix => $vol->suffix,
658 my $labelexists = undef;
660 # we've found an exising volume
661 if($override->{all} || grep { $_ eq 'VOLUME_LABEL_EXISTS' } @{$override->{events}}) {
662 $label = $vol->label;
667 OpenILS::Event->new('VOLUME_LABEL_EXISTS', payload => $vol->id)
672 # create a temp label so we can create the new volume,
673 # then de-dup it with the existing volume
674 $vol->label( "__SYSTEM_TMP_$$".time) if $labelexists;
676 $vol->creator($editor->requestor->id);
677 $vol->create_date('now');
678 $vol->editor($editor->requestor->id);
679 $vol->edit_date('now');
682 $editor->create_asset_call_number($vol) or return (undef, $editor->die_event);
685 # now restore the label and merge into the existing record
687 return OpenILS::Application::Cat::Merge::merge_volumes($editor, [$vol], $$vols[0]);
693 # returns the volume if it exists
695 my($class, $e, $rec_id, $label, $owning_lib, $prefix, $suffix) = @_;
696 return $e->search_asset_call_number(
697 {label => $label, record => $rec_id, owning_lib => $owning_lib, deleted => 'f', prefix => $prefix, suffix => $suffix})->[0];
700 sub find_or_create_volume {
701 my($class, $e, $label, $record_id, $org_id, $prefix, $suffix, $label_class) = @_;
708 if($record_id == OILS_PRECAT_RECORD) {
709 $vol = $e->retrieve_asset_call_number(OILS_PRECAT_CALL_NUMBER)
710 or return (undef, $e->die_event);
713 $vol = $class->volume_exists($e, $record_id, $label, $org_id, $prefix, $suffix);
716 # If the volume exists, return the ID
717 return ($vol, undef, 1) if $vol;
719 # -----------------------------------------------------------------
720 # Otherwise, create a new volume with the given attributes
721 # -----------------------------------------------------------------
722 return (undef, $e->die_event) unless $e->allowed('CREATE_VOLUME', $org_id);
724 $vol = Fieldmapper::asset::call_number->new;
725 $vol->owning_lib($org_id);
726 $vol->label_class($label_class) if ($label_class);
728 $vol->prefix($prefix);
729 $vol->suffix($suffix);
730 $vol->record($record_id);
732 return $class->create_volume(0, $e, $vol);
736 sub create_copy_note {
737 my($class, $e, $copy, $title, $value, $pub) = @_;
738 my $note = Fieldmapper::asset::copy_note->new;
739 $note->owning_copy($copy->id);
740 $note->creator($e->requestor->id);
741 $note->pub($pub ? 't' : 'f');
742 $note->value($value);
743 $note->title($title);
744 $e->create_asset_copy_note($note) or return $e->die_event;
749 sub remove_empty_objects {
750 my($class, $editor, $override, $vol, $force_delete_empty_bib) = @_;
752 $override = { all => 1 } if($override && !ref $override);
753 $override = { all => 0 } if(!ref $override);
755 my $koe = $U->ou_ancestor_setting_value(
756 $editor->requestor->ws_ou, 'cat.bib.keep_on_empty', $editor);
757 my $aoe = $U->ou_ancestor_setting_value(
758 $editor->requestor->ws_ou, 'cat.bib.alert_on_empty', $editor);
760 if( OpenILS::Application::Cat::BibCommon->title_is_empty($editor, $vol->record, $vol->id) ) {
762 # delete this volume if it's not already marked as deleted
763 unless( $U->is_true($vol->deleted) || $vol->isdeleted ) {
764 my $evt = $class->delete_volume($editor, $vol, $override, 0, 1);
768 return OpenILS::Event->new('TITLE_LAST_COPY', payload => $vol->record )
769 if $aoe and not ($override->{all} || grep { $_ eq 'TITLE_LAST_COPY' } @{$override->{events}}) and not $force_delete_empty_bib;
771 # check for any holds on the title and alert the user before plowing ahead
772 if( OpenILS::Application::Cat::BibCommon->title_has_holds($editor, $vol->record) ) {
773 return OpenILS::Event->new('TITLE_HAS_HOLDS', payload => $vol->record )
774 if not ($override->{all} || grep { $_ eq 'TITLE_HAS_HOLDS' } @{$override->{events}}) and not $force_delete_empty_bib;
777 unless($koe and not $force_delete_empty_bib) {
778 # delete the bib record if the keep-on-empty setting is not set (and we're not otherwise forcing things, say through acq settings)
779 my $evt = OpenILS::Application::Cat::BibCommon->delete_rec($editor, $vol->record);
785 # this may be the last copy attached to the volume.
787 if($U->ou_ancestor_setting_value(
788 $editor->requestor->ws_ou, 'cat.volume.delete_on_empty', $editor)) {
790 # if this volume is "empty" and not mid-delete, delete it.
791 unless($U->is_true($vol->deleted) || $vol->isdeleted) {
793 my $copies = $editor->search_asset_copy(
794 [{call_number => $vol->id, deleted => 'f'}, {limit => 1}], {idlist => 1});
797 my $evt = $class->delete_volume($editor, $vol, $override, 0, 1);
807 # Deletes a volume. Returns undef on success, event on error
808 # force : deletes all attached copies
809 # skip_copy_check : assumes caller has verified no copies need deleting first
811 my($class, $editor, $vol, $override, $delete_copies, $skip_copy_checks) = @_;
814 unless($skip_copy_checks) {
815 my $cs = $editor->search_asset_copy(
816 [{call_number => $vol->id, deleted => 'f'}, {limit => 1}], {idlist => 1});
818 return OpenILS::Event->new('VOLUME_NOT_EMPTY', payload => $vol->id)
819 if @$cs and !$delete_copies;
821 my $copies = $editor->search_asset_copy({call_number => $vol->id, deleted => 'f'});
823 for my $copy (@$copies) {
824 $evt = $class->delete_copy($editor, $override, $vol, $copy, 0, 0, 1);
830 $vol->edit_date('now');
831 $vol->editor($editor->requestor->id);
832 $editor->update_asset_call_number($vol) or return $editor->die_event;
834 $evt = $class->cancel_volume_holds($editor, $vol);
837 # handle the case where this is the last volume on the record
838 return $class->remove_empty_objects($editor, $override, $vol);
843 my($class, $vol, $copy) = @_;
844 my $org = $vol->owning_lib;
845 if( $vol->id == OILS_PRECAT_CALL_NUMBER ) {
846 $org = ref($copy->circ_lib) ? $copy->circ_lib->id : $copy->circ_lib;
848 $logger->debug("using copy perm org $org");
854 my ($class, $e, $copy_id) = @_;
856 return $class->set_item_lost_or_lod(
858 perm => 'SET_CIRC_LOST',
859 status => OILS_COPY_STATUS_LOST,
860 alt_status => 16, #Long Overdue,
861 ous_proc_fee => OILS_SETTING_LOST_PROCESSING_FEE,
862 ous_void_od => OILS_SETTING_VOID_OVERDUE_ON_LOST,
865 bill_note => 'Lost Materials',
866 bill_fee_note => 'Lost Materials Processing Fee',
867 event => 'COPY_MARKED_LOST',
868 stop_fines => OILS_STOP_FINES_LOST,
873 sub set_item_long_overdue {
874 my ($class, $e, $copy_id) = @_;
876 return $class->set_item_lost_or_lod(
878 perm => 'SET_CIRC_LONG_OVERDUE',
879 status => 16, # Long Overdue
880 alt_status => OILS_COPY_STATUS_LOST,
881 ous_proc_fee => 'circ.longoverdue_materials_processing_fee',
882 ous_void_od => 'circ.void_overdue_on_longoverdue',
885 bill_note => 'Long Overdue Materials',
886 bill_fee_note => 'Long Overdue Materials Processing Fee',
887 event => 'COPY_MARKED_LONG_OVERDUE',
888 stop_fines => 'LONGOVERDUE',
889 at_hook => 'longoverdue'
893 # LOST or LONGOVERDUE
894 # basic process is the same. details change.
895 sub set_item_lost_or_lod {
896 my ($class, $e, $copy_id, %args) = @_;
898 my $copy = $e->retrieve_asset_copy([
900 {flesh => 1, flesh_fields => {'acp' => ['call_number']}}])
901 or return $e->die_event;
904 ($copy->call_number->id == OILS_PRECAT_CALL_NUMBER) ?
905 $copy->circ_lib : $copy->call_number->owning_lib;
907 my $circ = $e->search_action_circulation(
908 {checkin_time => undef, target_copy => $copy->id} )->[0]
909 or return $e->die_event;
911 $e->allowed($args{perm}, $circ->circ_lib) or return $e->die_event;
913 return $e->die_event(OpenILS::Event->new($args{event}))
914 if ($copy->status == $args{status} || $copy->status == $args{alt_status});
916 # ---------------------------------------------------------------------
917 # fetch the related org settings
918 my $proc_fee = $U->ou_ancestor_setting_value(
919 $owning_lib, $args{ous_proc_fee}, $e) || 0;
920 my $void_overdue = $U->ou_ancestor_setting_value(
921 $owning_lib, $args{ous_void_od}, $e) || 0;
923 # ---------------------------------------------------------------------
924 # move the copy into LOST status
925 $copy->status($args{status});
926 $copy->editor($e->requestor->id);
927 $copy->edit_date('now');
928 $e->update_asset_copy($copy) or return $e->die_event;
930 my $price = $U->get_copy_price($e, $copy, $copy->call_number);
933 my $evt = OpenILS::Application::Circ::CircCommon->create_bill($e,
934 $price, $args{bill_type}, $args{bill_note}, $circ->id);
938 # ---------------------------------------------------------------------
939 # if there is a processing fee, charge that too
940 if( $proc_fee > 0 ) {
941 my $evt = OpenILS::Application::Circ::CircCommon->create_bill($e,
942 $proc_fee, $args{bill_fee_type}, $args{bill_fee_note}, $circ->id);
946 # ---------------------------------------------------------------------
947 # mark the circ as lost and stop the fines
948 $circ->stop_fines($args{stop_fines});
949 $circ->stop_fines_time('now') unless $circ->stop_fines_time;
950 $e->update_action_circulation($circ) or return $e->die_event;
952 # ---------------------------------------------------------------------
953 # zero out overdue fines on this circ if configured
954 if( $void_overdue ) {
955 my $evt = OpenILS::Application::Circ::CircCommon->void_or_zero_overdues($e, $circ, {force_zero => 1, note => "System: OVERDUE REVERSED for " . $args{bill_note} . " Processing"});
959 my $evt = OpenILS::Application::Circ::CircCommon->reopen_xact($e, $circ->id);
962 my $ses = OpenSRF::AppSession->create('open-ils.trigger');
964 'open-ils.trigger.event.autocreate',
965 $args{at_hook}, $circ, $circ->circ_lib
968 my $evt2 = OpenILS::Utils::Penalty->calculate_penalties(
969 $e, $circ->usr, $U->xact_org($circ->id, $e));
970 return $evt2 if $evt2;