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 my $existing = $editor->search_asset_copy(
54 { barcode => $copy->barcode, deleted => 'f' } );
56 return OpenILS::Event->new('ITEM_BARCODE_EXISTS') if @$existing;
58 my $copy_loc = $editor->search_asset_copy_location(
59 { id => $copy->location, deleted => 'f' } );
61 return OpenILS::Event->new('COPY_LOCATION_NOT_FOUND') unless @$copy_loc;
63 # see if the volume this copy references is marked as deleted
64 return OpenILS::Event->new('VOLUME_DELETED', vol => $vol->id)
65 if $U->is_true($vol->deleted);
68 my $org = (ref $copy->circ_lib) ? $copy->circ_lib->id : $copy->circ_lib;
69 return $evt if ($evt = $class->org_cannot_have_vols($editor, $org));
72 $copy->editor($editor->requestor->id);
73 $copy->creator($editor->requestor->id);
74 $copy->create_date('now');
75 $copy->call_number($vol->id);
76 $class->fix_copy_price($copy);
78 $editor->create_asset_copy($copy) or return $editor->die_event;
83 # if 'delete_stats' is true, the copy->stat_cat_entries data is
84 # treated as the authoritative list for the copy. existing entries
85 # that are not in said list will be deleted from the DB
86 sub update_copy_stat_entries {
87 my($class, $editor, $copy, $delete_stats) = @_;
89 return undef if $copy->isdeleted;
90 return undef unless $copy->ischanged or $copy->isnew;
93 my $entries = $copy->stat_cat_entries;
96 $entries = ($entries and @$entries) ? $entries : [];
98 return undef unless ($entries and @$entries);
101 my $maps = $editor->search_asset_stat_cat_entry_copy_map({owning_copy=>$copy->id});
104 # if there is no stat cat entry on the copy who's id matches the
105 # current map's id, remove the map from the database
106 for my $map (@$maps) {
107 if(! grep { $_->id == $map->stat_cat_entry } @$entries ) {
109 $logger->info("copy update found stale ".
110 "stat cat entry map ".$map->id. " on copy ".$copy->id);
112 $editor->delete_asset_stat_cat_entry_copy_map($map)
113 or return $editor->event;
118 # go through the stat cat update/create process
119 for my $entry (@$entries) {
122 # if this link already exists in the DB, don't attempt to re-create it
123 next if( grep{$_->stat_cat_entry == $entry->id} @$maps );
125 my $new_map = Fieldmapper::asset::stat_cat_entry_copy_map->new();
127 my $sc = ref($entry->stat_cat) ? $entry->stat_cat->id : $entry->stat_cat;
129 $new_map->stat_cat( $sc );
130 $new_map->stat_cat_entry( $entry->id );
131 $new_map->owning_copy( $copy->id );
133 $editor->create_asset_stat_cat_entry_copy_map($new_map)
134 or return $editor->event;
136 $logger->info("copy update created new stat cat entry map ".$editor->data);
142 # if 'delete_maps' is true, the copy->parts data is treated as the
143 # authoritative list for the copy. existing part maps not targeting
144 # these parts will be deleted from the DB
145 sub update_copy_parts {
146 my($class, $editor, $copy, $delete_maps) = @_;
148 return undef if $copy->isdeleted;
149 return undef unless $copy->ischanged or $copy->isnew;
152 my $incoming_parts = $copy->parts;
155 $incoming_parts = ($incoming_parts and @$incoming_parts) ? $incoming_parts : [];
157 return undef unless ($incoming_parts and @$incoming_parts);
160 my $maps = $editor->search_asset_copy_part_map({target_copy=>$copy->id});
163 # if there is no part map on the copy who's id matches the
164 # current map's id, remove the map from the database
165 for my $map (@$maps) {
166 if(! grep { $_->id == $map->part } @$incoming_parts ) {
168 $logger->info("copy update found stale ".
169 "monographic part map ".$map->id. " on copy ".$copy->id);
171 $editor->delete_asset_copy_part_map($map)
172 or return $editor->event;
177 # go through the part map update/create process
178 for my $incoming_part (@$incoming_parts) {
179 next unless $incoming_part;
181 # if this link already exists in the DB, don't attempt to re-create it
182 next if( grep{$_->part == $incoming_part->id} @$maps );
184 my $new_map = Fieldmapper::asset::copy_part_map->new();
186 $new_map->part( $incoming_part->id );
187 $new_map->target_copy( $copy->id );
189 $editor->create_asset_copy_part_map($new_map)
190 or return $editor->event;
192 $logger->info("copy update created new monographic part copy map ".$editor->data);
201 my($class, $editor, $override, $vol, $copy, $retarget_holds, $force_delete_empty_bib) = @_;
203 $override = { all => 1 } if($override && !ref $override);
204 $override = { all => 0 } if(!ref $override);
206 # Duplicated check from create_copy in case a copy template with a deleted location is applied later
207 my $copy_loc = $editor->search_asset_copy_location(
208 { id => $copy->location, deleted => 'f' } );
210 return OpenILS::Event->new('COPY_LOCATION_NOT_FOUND') unless @$copy_loc;
213 my $org = (ref $copy->circ_lib) ? $copy->circ_lib->id : $copy->circ_lib;
214 return $evt if ( $evt = $class->org_cannot_have_vols($editor, $org) );
216 $logger->info("vol-update: updating copy ".$copy->id);
217 my $orig_copy = $editor->retrieve_asset_copy($copy->id);
219 # Call-number may have changed, find the original
220 my $orig_vol_id = $editor->json_query({select => {acp => ['call_number']}, from => 'acp', where => {id => $copy->id}});
221 my $orig_vol = $editor->retrieve_asset_call_number($orig_vol_id->[0]->{call_number});
223 $copy->editor($editor->requestor->id);
224 $copy->edit_date('now');
226 $copy->age_protect( $copy->age_protect->id )
227 if ref $copy->age_protect;
229 $class->fix_copy_price($copy);
230 $class->check_hold_retarget($editor, $copy, $orig_copy, $retarget_holds);
232 return $editor->event unless $editor->update_asset_copy($copy);
233 return $class->remove_empty_objects($editor, $override, $orig_vol, $force_delete_empty_bib);
236 sub check_hold_retarget {
237 my($class, $editor, $copy, $orig_copy, $retarget_holds) = @_;
238 return unless $retarget_holds;
240 if( !($copy->isdeleted or $U->is_true($copy->deleted)) ) {
241 # see if a status change warrants a retarget
243 $orig_copy = $editor->retrieve_asset_copy($copy->id) unless $orig_copy;
245 if($orig_copy->status == $copy->status) {
246 # no status change, no retarget
250 my $stat = $editor->retrieve_config_copy_status($copy->status);
252 # new status is holdable, no retarget. Later add logic to find potential
253 # holds and retarget those to pick up the newly available copy
254 return if $U->is_true($stat->holdable);
257 my $hold_ids = $editor->search_action_hold_request(
258 { current_copy => $copy->id,
259 cancel_time => undef,
260 fulfillment_time => undef
264 push(@$retarget_holds, @$hold_ids);
268 # this does the actual work
269 sub update_fleshed_copies {
270 my($class, $editor, $override, $vol, $copies, $delete_stats, $retarget_holds, $force_delete_empty_bib) = @_;
272 $override = { all => 1 } if($override && !ref $override);
273 $override = { all => 0 } if(!ref $override);
276 my $fetchvol = ($vol) ? 0 : 1;
279 $cache{$vol->id} = $vol if $vol;
281 for my $copy (@$copies) {
283 my $copyid = $copy->id;
284 $logger->info("vol-update: inspecting copy $copyid");
286 if( !($vol = $cache{$copy->call_number}) ) {
287 $vol = $cache{$copy->call_number} =
288 $editor->retrieve_asset_call_number($copy->call_number);
289 return $editor->event unless $vol;
292 return $editor->event unless
293 $editor->allowed('UPDATE_COPY', $class->copy_perm_org($vol, $copy));
295 $copy->editor($editor->requestor->id);
296 $copy->edit_date('now');
298 $copy->status( $copy->status->id ) if ref($copy->status);
299 $copy->location( $copy->location->id ) if ref($copy->location);
300 $copy->circ_lib( $copy->circ_lib->id ) if ref($copy->circ_lib);
302 my $sc_entries = $copy->stat_cat_entries;
303 $copy->clear_stat_cat_entries;
305 my $parts = $copy->parts;
308 if( $copy->isdeleted ) {
309 $evt = $class->delete_copy($editor, $override, $vol, $copy, $retarget_holds, $force_delete_empty_bib);
312 } elsif( $copy->isnew ) {
313 $evt = $class->create_copy( $editor, $vol, $copy );
316 } elsif( $copy->ischanged ) {
318 $evt = $class->update_copy( $editor, $override, $vol, $copy, $retarget_holds, $force_delete_empty_bib);
322 $copy->stat_cat_entries( $sc_entries );
323 $evt = $class->update_copy_stat_entries($editor, $copy, $delete_stats);
324 $copy->parts( $parts );
325 # probably okay to use $delete_stats here for simplicity
326 $evt = $class->update_copy_parts($editor, $copy, $delete_stats);
330 $logger->debug("vol-update: done updating copy batch");
337 my($class, $editor, $override, $vol, $copy, $retarget_holds, $force_delete_empty_bib, $skip_empty_cleanup) = @_;
339 return $editor->event unless
340 $editor->allowed('DELETE_COPY', $class->copy_perm_org($vol, $copy));
342 $override = { all => 1 } if($override && !ref $override);
343 $override = { all => 0 } if(!ref $override);
345 my $stat = $U->copy_status($copy->status);
346 if ($U->is_true($stat->restrict_copy_delete)) {
347 if ($override->{all} || grep { $_ eq 'COPY_DELETE_WARNING' } @{$override->{events}}) {
348 return $editor->event unless $editor->allowed('COPY_DELETE_WARNING.override', $class->copy_perm_org($vol, $copy))
350 return OpenILS::Event->new('COPY_DELETE_WARNING', payload => $copy->id )
354 $logger->info("vol-update: deleting copy ".$copy->id);
357 $copy->editor($editor->requestor->id);
358 $copy->edit_date('now');
359 $editor->update_asset_copy($copy) or return $editor->event;
361 # Delete any open transits for this copy
362 my $transits = $editor->search_action_transit_copy(
363 { target_copy=>$copy->id, dest_recv_time => undef } );
365 for my $t (@$transits) {
366 $editor->delete_action_transit_copy($t)
367 or return $editor->event;
370 my $evt = $class->cancel_copy_holds($editor, $copy);
373 $class->check_hold_retarget($editor, $copy, undef, $retarget_holds);
375 return undef if $skip_empty_cleanup;
377 return $class->remove_empty_objects($editor, $override, $vol, $force_delete_empty_bib);
381 # deletes all holds that specifically target the deleted copy
382 sub cancel_copy_holds {
383 my($class, $editor, $copy) = @_;
385 my $holds = $editor->search_action_hold_request({
387 hold_type => [qw/C R F/],
388 cancel_time => undef,
389 fulfillment_time => undef
392 return $class->cancel_hold_list($editor, $holds);
395 # deletes all holds that specifically target the deleted volume
396 sub cancel_volume_holds {
397 my($class, $editor, $volume) = @_;
399 my $holds = $editor->search_action_hold_request({
400 target => $volume->id,
402 cancel_time => undef,
403 fulfillment_time => undef
406 return $class->cancel_hold_list($editor, $holds);
409 sub cancel_hold_list {
410 my($class, $editor, $holds) = @_;
412 for my $hold (@$holds) {
414 $hold->cancel_time('now');
415 $hold->cancel_cause(1); # un-targeted expiration. Do we need an alternate "target deleted" cause?
416 $editor->update_action_hold_request($hold) or return $editor->die_event;
418 # tell A/T the hold was cancelled. Don't wait for a response..
419 my $at_ses = OpenSRF::AppSession->create('open-ils.trigger');
421 'open-ils.trigger.event.autocreate',
422 'hold_request.cancel.expire_no_target',
423 $hold, $hold->pickup_lib);
432 my($class, $override, $editor, $vol) = @_;
435 return $evt if ( $evt = $class->org_cannot_have_vols($editor, $vol->owning_lib) );
437 $override = { all => 1 } if($override && !ref $override);
438 $override = { all => 0 } if(!ref $override);
440 # see if the record this volume references is marked as deleted
441 my $rec = $editor->retrieve_biblio_record_entry($vol->record)
442 or return $editor->die_event;
443 return OpenILS::Event->new('BIB_RECORD_DELETED', rec => $rec->id)
444 if $U->is_true($rec->deleted);
446 # first lets see if there are any collisions
447 my $vols = $editor->search_asset_call_number( {
448 owning_lib => $vol->owning_lib,
449 record => $vol->record,
450 label => $vol->label,
451 prefix => $vol->prefix,
452 suffix => $vol->suffix,
459 # we've found an exising volume
460 if($override->{all} || grep { $_ eq 'VOLUME_LABEL_EXISTS' } @{$override->{events}}) {
461 $label = $vol->label;
463 return OpenILS::Event->new(
464 'VOLUME_LABEL_EXISTS', payload => $vol->id);
468 # create a temp label so we can create the new volume,
469 # then de-dup it with the existing volume
470 $vol->label( "__SYSTEM_TMP_$$".time) if $label;
472 $vol->creator($editor->requestor->id);
473 $vol->create_date('now');
474 $vol->editor($editor->requestor->id);
475 $vol->edit_date('now');
478 $editor->create_asset_call_number($vol) or return $editor->die_event;
481 # now restore the label and merge into the existing record
484 OpenILS::Application::Cat::Merge::merge_volumes($editor, [$vol], $$vols[0]);
491 # returns the volume if it exists
493 my($class, $e, $rec_id, $label, $owning_lib, $prefix, $suffix) = @_;
494 return $e->search_asset_call_number(
495 {label => $label, record => $rec_id, owning_lib => $owning_lib, deleted => 'f', prefix => $prefix, suffix => $suffix})->[0];
498 sub find_or_create_volume {
499 my($class, $e, $label, $record_id, $org_id, $prefix, $suffix, $label_class) = @_;
506 if($record_id == OILS_PRECAT_RECORD) {
507 $vol = $e->retrieve_asset_call_number(OILS_PRECAT_CALL_NUMBER)
508 or return (undef, $e->die_event);
511 $vol = $class->volume_exists($e, $record_id, $label, $org_id, $prefix, $suffix);
514 # If the volume exists, return the ID
515 return ($vol, undef, 1) if $vol;
517 # -----------------------------------------------------------------
518 # Otherwise, create a new volume with the given attributes
519 # -----------------------------------------------------------------
520 return (undef, $e->die_event) unless $e->allowed('UPDATE_VOLUME', $org_id);
522 $vol = Fieldmapper::asset::call_number->new;
523 $vol->owning_lib($org_id);
524 $vol->label_class($label_class) if ($label_class);
526 $vol->prefix($prefix);
527 $vol->suffix($suffix);
528 $vol->record($record_id);
530 my $evt = $class->create_volume(0, $e, $vol);
531 return (undef, $evt) if $evt;
537 sub create_copy_note {
538 my($class, $e, $copy, $title, $value, $pub) = @_;
539 my $note = Fieldmapper::asset::copy_note->new;
540 $note->owning_copy($copy->id);
541 $note->creator($e->requestor->id);
542 $note->pub($pub ? 't' : 'f');
543 $note->value($value);
544 $note->title($title);
545 $e->create_asset_copy_note($note) or return $e->die_event;
550 sub remove_empty_objects {
551 my($class, $editor, $override, $vol, $force_delete_empty_bib) = @_;
553 $override = { all => 1 } if($override && !ref $override);
554 $override = { all => 0 } if(!ref $override);
556 my $koe = $U->ou_ancestor_setting_value(
557 $editor->requestor->ws_ou, 'cat.bib.keep_on_empty', $editor);
558 my $aoe = $U->ou_ancestor_setting_value(
559 $editor->requestor->ws_ou, 'cat.bib.alert_on_empty', $editor);
561 if( OpenILS::Application::Cat::BibCommon->title_is_empty($editor, $vol->record, $vol->id) ) {
563 # delete this volume if it's not already marked as deleted
564 unless( $U->is_true($vol->deleted) || $vol->isdeleted ) {
565 my $evt = $class->delete_volume($editor, $vol, $override, 0, 1);
569 return OpenILS::Event->new('TITLE_LAST_COPY', payload => $vol->record )
570 if $aoe and not ($override->{all} || grep { $_ eq 'TITLE_LAST_COPY' } @{$override->{events}}) and not $force_delete_empty_bib;
572 unless($koe and not $force_delete_empty_bib) {
573 # delete the bib record if the keep-on-empty setting is not set (and we're not otherwise forcing things, say through acq settings)
574 my $evt = OpenILS::Application::Cat::BibCommon->delete_rec($editor, $vol->record);
580 # this may be the last copy attached to the volume.
582 if($U->ou_ancestor_setting_value(
583 $editor->requestor->ws_ou, 'cat.volume.delete_on_empty', $editor)) {
585 # if this volume is "empty" and not mid-delete, delete it.
586 unless($U->is_true($vol->deleted) || $vol->isdeleted) {
588 my $copies = $editor->search_asset_copy(
589 [{call_number => $vol->id, deleted => 'f'}, {limit => 1}], {idlist => 1});
592 my $evt = $class->delete_volume($editor, $vol, $override, 0, 1);
602 # Deletes a volume. Returns undef on success, event on error
603 # force : deletes all attached copies
604 # skip_copy_check : assumes caller has verified no copies need deleting first
606 my($class, $editor, $vol, $override, $delete_copies, $skip_copy_checks) = @_;
609 unless($skip_copy_checks) {
610 my $cs = $editor->search_asset_copy(
611 [{call_number => $vol->id, deleted => 'f'}, {limit => 1}], {idlist => 1});
613 return OpenILS::Event->new('VOLUME_NOT_EMPTY', payload => $vol->id)
614 if @$cs and !$delete_copies;
616 my $copies = $editor->search_asset_copy({call_number => $vol->id, deleted => 'f'});
618 for my $copy (@$copies) {
619 $evt = $class->delete_copy($editor, $override, $vol, $copy, 0, 0, 1);
625 $vol->edit_date('now');
626 $vol->editor($editor->requestor->id);
627 $editor->update_asset_call_number($vol) or return $editor->die_event;
629 $evt = $class->cancel_volume_holds($editor, $vol);
632 # handle the case where this is the last volume on the record
633 return $class->remove_empty_objects($editor, $override, $vol);
638 my($class, $vol, $copy) = @_;
639 my $org = $vol->owning_lib;
640 if( $vol->id == OILS_PRECAT_CALL_NUMBER ) {
641 $org = ref($copy->circ_lib) ? $copy->circ_lib->id : $copy->circ_lib;
643 $logger->debug("using copy perm org $org");
649 my ($class, $e, $copy_id) = @_;
651 return $class->set_item_lost_or_lod(
653 perm => 'SET_CIRC_LOST',
654 status => OILS_COPY_STATUS_LOST,
655 ous_proc_fee => OILS_SETTING_LOST_PROCESSING_FEE,
656 ous_void_od => OILS_SETTING_VOID_OVERDUE_ON_LOST,
659 bill_note => 'Lost Materials',
660 bill_fee_note => 'Lost Materials Processing Fee',
661 event => 'COPY_MARKED_LOST',
662 stop_fines => OILS_STOP_FINES_LOST,
667 sub set_item_long_overdue {
668 my ($class, $e, $copy_id) = @_;
670 return $class->set_item_lost_or_lod(
672 perm => 'SET_CIRC_LONG_OVERDUE',
673 status => 16, # Long Overdue
674 ous_proc_fee => 'circ.longoverdue_materials_processing_fee',
675 ous_void_od => 'circ.void_overdue_on_longoverdue',
678 bill_note => 'Long Overdue Materials',
679 bill_fee_note => 'Long Overdue Materials Processing Fee',
680 event => 'COPY_MARKED_LONG_OVERDUE',
681 stop_fines => 'LONGOVERDUE',
682 at_hook => 'longoverdue'
686 # LOST or LONGOVERDUE
687 # basic process is the same. details change.
688 sub set_item_lost_or_lod {
689 my ($class, $e, $copy_id, %args) = @_;
691 my $copy = $e->retrieve_asset_copy([
693 {flesh => 1, flesh_fields => {'acp' => ['call_number']}}])
694 or return $e->die_event;
697 ($copy->call_number->id == OILS_PRECAT_CALL_NUMBER) ?
698 $copy->circ_lib : $copy->call_number->owning_lib;
700 my $circ = $e->search_action_circulation(
701 {checkin_time => undef, target_copy => $copy->id} )->[0]
702 or return $e->die_event;
704 $e->allowed($args{perm}, $circ->circ_lib) or return $e->die_event;
706 return $e->die_event(OpenILS::Event->new($args{event}))
707 if $copy->status == $args{status};
709 # ---------------------------------------------------------------------
710 # fetch the related org settings
711 my $proc_fee = $U->ou_ancestor_setting_value(
712 $owning_lib, $args{ous_proc_fee}, $e) || 0;
713 my $void_overdue = $U->ou_ancestor_setting_value(
714 $owning_lib, $args{ous_void_od}, $e) || 0;
716 # ---------------------------------------------------------------------
717 # move the copy into LOST status
718 $copy->status($args{status});
719 $copy->editor($e->requestor->id);
720 $copy->edit_date('now');
721 $e->update_asset_copy($copy) or return $e->die_event;
723 my $price = $U->get_copy_price($e, $copy, $copy->call_number);
726 my $evt = OpenILS::Application::Circ::CircCommon->create_bill($e,
727 $price, $args{bill_type}, $args{bill_note}, $circ->id);
731 # ---------------------------------------------------------------------
732 # if there is a processing fee, charge that too
733 if( $proc_fee > 0 ) {
734 my $evt = OpenILS::Application::Circ::CircCommon->create_bill($e,
735 $proc_fee, $args{bill_fee_type}, $args{bill_fee_note}, $circ->id);
739 # ---------------------------------------------------------------------
740 # mark the circ as lost and stop the fines
741 $circ->stop_fines($args{stop_fines});
742 $circ->stop_fines_time('now') unless $circ->stop_fines_time;
743 $e->update_action_circulation($circ) or return $e->die_event;
745 # ---------------------------------------------------------------------
746 # void all overdue fines on this circ if configured
747 if( $void_overdue ) {
748 my $evt = OpenILS::Application::Circ::CircCommon->void_or_zero_overdues($e, $circ);
752 my $evt = OpenILS::Application::Circ::CircCommon->reopen_xact($e, $circ->id);
755 my $ses = OpenSRF::AppSession->create('open-ils.trigger');
757 'open-ils.trigger.event.autocreate',
758 $args{at_hook}, $circ, $circ->circ_lib
761 my $evt2 = OpenILS::Utils::Penalty->calculate_penalties(
762 $e, $circ->usr, $U->xact_org($circ->id, $e));
763 return $evt2 if $evt2;