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 # see if the volume this copy references is marked as deleted
59 return OpenILS::Event->new('VOLUME_DELETED', vol => $vol->id)
60 if $U->is_true($vol->deleted);
63 my $org = (ref $copy->circ_lib) ? $copy->circ_lib->id : $copy->circ_lib;
64 return $evt if ($evt = $class->org_cannot_have_vols($editor, $org));
67 $copy->editor($editor->requestor->id);
68 $copy->creator($editor->requestor->id);
69 $copy->create_date('now');
70 $copy->call_number($vol->id);
71 $class->fix_copy_price($copy);
73 $editor->create_asset_copy($copy) or return $editor->die_event;
78 # if 'delete_stats' is true, the copy->stat_cat_entries data is
79 # treated as the authoritative list for the copy. existing entries
80 # that are not in said list will be deleted from the DB
81 sub update_copy_stat_entries {
82 my($class, $editor, $copy, $delete_stats) = @_;
84 return undef if $copy->isdeleted;
85 return undef unless $copy->ischanged or $copy->isnew;
88 my $entries = $copy->stat_cat_entries;
91 $entries = ($entries and @$entries) ? $entries : [];
93 return undef unless ($entries and @$entries);
96 my $maps = $editor->search_asset_stat_cat_entry_copy_map({owning_copy=>$copy->id});
99 # if there is no stat cat entry on the copy who's id matches the
100 # current map's id, remove the map from the database
101 for my $map (@$maps) {
102 if(! grep { $_->id == $map->stat_cat_entry } @$entries ) {
104 $logger->info("copy update found stale ".
105 "stat cat entry map ".$map->id. " on copy ".$copy->id);
107 $editor->delete_asset_stat_cat_entry_copy_map($map)
108 or return $editor->event;
113 # go through the stat cat update/create process
114 for my $entry (@$entries) {
117 # if this link already exists in the DB, don't attempt to re-create it
118 next if( grep{$_->stat_cat_entry == $entry->id} @$maps );
120 my $new_map = Fieldmapper::asset::stat_cat_entry_copy_map->new();
122 my $sc = ref($entry->stat_cat) ? $entry->stat_cat->id : $entry->stat_cat;
124 $new_map->stat_cat( $sc );
125 $new_map->stat_cat_entry( $entry->id );
126 $new_map->owning_copy( $copy->id );
128 $editor->create_asset_stat_cat_entry_copy_map($new_map)
129 or return $editor->event;
131 $logger->info("copy update created new stat cat entry map ".$editor->data);
137 # if 'delete_maps' is true, the copy->parts data is treated as the
138 # authoritative list for the copy. existing part maps not targeting
139 # these parts will be deleted from the DB
140 sub update_copy_parts {
141 my($class, $editor, $copy, $delete_maps) = @_;
143 return undef if $copy->isdeleted;
144 return undef unless $copy->ischanged or $copy->isnew;
147 my $incoming_parts = $copy->parts;
150 $incoming_parts = ($incoming_parts and @$incoming_parts) ? $incoming_parts : [];
152 return undef unless ($incoming_parts and @$incoming_parts);
155 my $maps = $editor->search_asset_copy_part_map({target_copy=>$copy->id});
158 # if there is no part map on the copy who's id matches the
159 # current map's id, remove the map from the database
160 for my $map (@$maps) {
161 if(! grep { $_->id == $map->part } @$incoming_parts ) {
163 $logger->info("copy update found stale ".
164 "monographic part map ".$map->id. " on copy ".$copy->id);
166 $editor->delete_asset_copy_part_map($map)
167 or return $editor->event;
172 # go through the part map update/create process
173 for my $incoming_part (@$incoming_parts) {
174 next unless $incoming_part;
176 # if this link already exists in the DB, don't attempt to re-create it
177 next if( grep{$_->part == $incoming_part->id} @$maps );
179 my $new_map = Fieldmapper::asset::copy_part_map->new();
181 $new_map->part( $incoming_part->id );
182 $new_map->target_copy( $copy->id );
184 $editor->create_asset_copy_part_map($new_map)
185 or return $editor->event;
187 $logger->info("copy update created new monographic part copy map ".$editor->data);
196 my($class, $editor, $override, $vol, $copy, $retarget_holds, $force_delete_empty_bib) = @_;
198 $override = { all => 1 } if($override && !ref $override);
199 $override = { all => 0 } if(!ref $override);
202 my $org = (ref $copy->circ_lib) ? $copy->circ_lib->id : $copy->circ_lib;
203 return $evt if ( $evt = $class->org_cannot_have_vols($editor, $org) );
205 $logger->info("vol-update: updating copy ".$copy->id);
206 my $orig_copy = $editor->retrieve_asset_copy($copy->id);
208 # Call-number may have changed, find the original
209 my $orig_vol_id = $editor->json_query({select => {acp => ['call_number']}, from => 'acp', where => {id => $copy->id}});
210 my $orig_vol = $editor->retrieve_asset_call_number($orig_vol_id->[0]->{call_number});
212 $copy->editor($editor->requestor->id);
213 $copy->edit_date('now');
215 $copy->age_protect( $copy->age_protect->id )
216 if ref $copy->age_protect;
218 $class->fix_copy_price($copy);
219 $class->check_hold_retarget($editor, $copy, $orig_copy, $retarget_holds);
221 return $editor->event unless $editor->update_asset_copy($copy);
222 return $class->remove_empty_objects($editor, $override, $orig_vol, $force_delete_empty_bib);
225 sub check_hold_retarget {
226 my($class, $editor, $copy, $orig_copy, $retarget_holds) = @_;
227 return unless $retarget_holds;
229 if( !($copy->isdeleted or $U->is_true($copy->deleted)) ) {
230 # see if a status change warrants a retarget
232 $orig_copy = $editor->retrieve_asset_copy($copy->id) unless $orig_copy;
234 if($orig_copy->status == $copy->status) {
235 # no status change, no retarget
239 my $stat = $editor->retrieve_config_copy_status($copy->status);
241 # new status is holdable, no retarget. Later add logic to find potential
242 # holds and retarget those to pick up the newly available copy
243 return if $U->is_true($stat->holdable);
246 my $hold_ids = $editor->search_action_hold_request(
247 { current_copy => $copy->id,
248 cancel_time => undef,
249 fulfillment_time => undef
253 push(@$retarget_holds, @$hold_ids);
257 # this does the actual work
258 sub update_fleshed_copies {
259 my($class, $editor, $override, $vol, $copies, $delete_stats, $retarget_holds, $force_delete_empty_bib) = @_;
261 $override = { all => 1 } if($override && !ref $override);
262 $override = { all => 0 } if(!ref $override);
265 my $fetchvol = ($vol) ? 0 : 1;
268 $cache{$vol->id} = $vol if $vol;
270 for my $copy (@$copies) {
272 my $copyid = $copy->id;
273 $logger->info("vol-update: inspecting copy $copyid");
275 if( !($vol = $cache{$copy->call_number}) ) {
276 $vol = $cache{$copy->call_number} =
277 $editor->retrieve_asset_call_number($copy->call_number);
278 return $editor->event unless $vol;
281 return $editor->event unless
282 $editor->allowed('UPDATE_COPY', $class->copy_perm_org($vol, $copy));
284 $copy->editor($editor->requestor->id);
285 $copy->edit_date('now');
287 $copy->status( $copy->status->id ) if ref($copy->status);
288 $copy->location( $copy->location->id ) if ref($copy->location);
289 $copy->circ_lib( $copy->circ_lib->id ) if ref($copy->circ_lib);
291 my $sc_entries = $copy->stat_cat_entries;
292 $copy->clear_stat_cat_entries;
294 my $parts = $copy->parts;
297 if( $copy->isdeleted ) {
298 $evt = $class->delete_copy($editor, $override, $vol, $copy, $retarget_holds, $force_delete_empty_bib);
301 } elsif( $copy->isnew ) {
302 $evt = $class->create_copy( $editor, $vol, $copy );
305 } elsif( $copy->ischanged ) {
307 $evt = $class->update_copy( $editor, $override, $vol, $copy, $retarget_holds, $force_delete_empty_bib);
311 $copy->stat_cat_entries( $sc_entries );
312 $evt = $class->update_copy_stat_entries($editor, $copy, $delete_stats);
313 $copy->parts( $parts );
314 # probably okay to use $delete_stats here for simplicity
315 $evt = $class->update_copy_parts($editor, $copy, $delete_stats);
319 $logger->debug("vol-update: done updating copy batch");
326 my($class, $editor, $override, $vol, $copy, $retarget_holds, $force_delete_empty_bib, $skip_empty_cleanup) = @_;
328 return $editor->event unless
329 $editor->allowed('DELETE_COPY', $class->copy_perm_org($vol, $copy));
331 $override = { all => 1 } if($override && !ref $override);
332 $override = { all => 0 } if(!ref $override);
334 my $stat = $U->copy_status($copy->status);
335 if ($U->is_true($stat->restrict_copy_delete)) {
336 if ($override->{all} || grep { $_ eq 'COPY_DELETE_WARNING' } @{$override->{events}}) {
337 return $editor->event unless $editor->allowed('COPY_DELETE_WARNING.override', $class->copy_perm_org($vol, $copy))
339 return OpenILS::Event->new('COPY_DELETE_WARNING', payload => $copy->id )
343 $logger->info("vol-update: deleting copy ".$copy->id);
346 $copy->editor($editor->requestor->id);
347 $copy->edit_date('now');
348 $editor->update_asset_copy($copy) or return $editor->event;
350 # Delete any open transits for this copy
351 my $transits = $editor->search_action_transit_copy(
352 { target_copy=>$copy->id, dest_recv_time => undef } );
354 for my $t (@$transits) {
355 $editor->delete_action_transit_copy($t)
356 or return $editor->event;
359 my $evt = $class->cancel_copy_holds($editor, $copy);
362 $class->check_hold_retarget($editor, $copy, undef, $retarget_holds);
364 return undef if $skip_empty_cleanup;
366 return $class->remove_empty_objects($editor, $override, $vol, $force_delete_empty_bib);
370 # deletes all holds that specifically target the deleted copy
371 sub cancel_copy_holds {
372 my($class, $editor, $copy) = @_;
374 my $holds = $editor->search_action_hold_request({
376 hold_type => [qw/C R F/],
377 cancel_time => undef,
378 fulfillment_time => undef
381 return $class->cancel_hold_list($editor, $holds);
384 # deletes all holds that specifically target the deleted volume
385 sub cancel_volume_holds {
386 my($class, $editor, $volume) = @_;
388 my $holds = $editor->search_action_hold_request({
389 target => $volume->id,
391 cancel_time => undef,
392 fulfillment_time => undef
395 return $class->cancel_hold_list($editor, $holds);
398 sub cancel_hold_list {
399 my($class, $editor, $holds) = @_;
401 for my $hold (@$holds) {
403 $hold->cancel_time('now');
404 $hold->cancel_cause(1); # un-targeted expiration. Do we need an alternate "target deleted" cause?
405 $editor->update_action_hold_request($hold) or return $editor->die_event;
407 # delete the copy maps.
408 my $maps = $editor->search_action_hold_copy_map({hold => $hold->id});
410 $editor->delete_action_hold_copy_map($_)
411 or return $editor->die_event;
414 # tell A/T the hold was cancelled. Don't wait for a response..
415 my $at_ses = OpenSRF::AppSession->create('open-ils.trigger');
417 'open-ils.trigger.event.autocreate',
418 'hold_request.cancel.expire_no_target',
419 $hold, $hold->pickup_lib);
428 my($class, $override, $editor, $vol) = @_;
431 return $evt if ( $evt = $class->org_cannot_have_vols($editor, $vol->owning_lib) );
433 $override = { all => 1 } if($override && !ref $override);
434 $override = { all => 0 } if(!ref $override);
436 # see if the record this volume references is marked as deleted
437 my $rec = $editor->retrieve_biblio_record_entry($vol->record)
438 or return $editor->die_event;
439 return OpenILS::Event->new('BIB_RECORD_DELETED', rec => $rec->id)
440 if $U->is_true($rec->deleted);
442 # first lets see if there are any collisions
443 my $vols = $editor->search_asset_call_number( {
444 owning_lib => $vol->owning_lib,
445 record => $vol->record,
446 label => $vol->label,
447 prefix => $vol->prefix,
448 suffix => $vol->suffix,
455 # we've found an exising volume
456 if($override->{all} || grep { $_ eq 'VOLUME_LABEL_EXISTS' } @{$override->{events}}) {
457 $label = $vol->label;
459 return OpenILS::Event->new(
460 'VOLUME_LABEL_EXISTS', payload => $vol->id);
464 # create a temp label so we can create the new volume,
465 # then de-dup it with the existing volume
466 $vol->label( "__SYSTEM_TMP_$$".time) if $label;
468 $vol->creator($editor->requestor->id);
469 $vol->create_date('now');
470 $vol->editor($editor->requestor->id);
471 $vol->edit_date('now');
474 $editor->create_asset_call_number($vol) or return $editor->die_event;
477 # now restore the label and merge into the existing record
480 OpenILS::Application::Cat::Merge::merge_volumes($editor, [$vol], $$vols[0]);
487 # returns the volume if it exists
489 my($class, $e, $rec_id, $label, $owning_lib, $prefix, $suffix) = @_;
490 return $e->search_asset_call_number(
491 {label => $label, record => $rec_id, owning_lib => $owning_lib, deleted => 'f', prefix => $prefix, suffix => $suffix})->[0];
494 sub find_or_create_volume {
495 my($class, $e, $label, $record_id, $org_id, $prefix, $suffix, $label_class) = @_;
502 if($record_id == OILS_PRECAT_RECORD) {
503 $vol = $e->retrieve_asset_call_number(OILS_PRECAT_CALL_NUMBER)
504 or return (undef, $e->die_event);
507 $vol = $class->volume_exists($e, $record_id, $label, $org_id, $prefix, $suffix);
510 # If the volume exists, return the ID
511 return ($vol, undef, 1) if $vol;
513 # -----------------------------------------------------------------
514 # Otherwise, create a new volume with the given attributes
515 # -----------------------------------------------------------------
516 return (undef, $e->die_event) unless $e->allowed('UPDATE_VOLUME', $org_id);
518 $vol = Fieldmapper::asset::call_number->new;
519 $vol->owning_lib($org_id);
520 $vol->label_class($label_class) if ($label_class);
522 $vol->prefix($prefix);
523 $vol->suffix($suffix);
524 $vol->record($record_id);
526 my $evt = $class->create_volume(0, $e, $vol);
527 return (undef, $evt) if $evt;
533 sub create_copy_note {
534 my($class, $e, $copy, $title, $value, $pub) = @_;
535 my $note = Fieldmapper::asset::copy_note->new;
536 $note->owning_copy($copy->id);
537 $note->creator($e->requestor->id);
538 $note->pub($pub ? 't' : 'f');
539 $note->value($value);
540 $note->title($title);
541 $e->create_asset_copy_note($note) or return $e->die_event;
546 sub remove_empty_objects {
547 my($class, $editor, $override, $vol, $force_delete_empty_bib) = @_;
549 $override = { all => 1 } if($override && !ref $override);
550 $override = { all => 0 } if(!ref $override);
552 my $koe = $U->ou_ancestor_setting_value(
553 $editor->requestor->ws_ou, 'cat.bib.keep_on_empty', $editor);
554 my $aoe = $U->ou_ancestor_setting_value(
555 $editor->requestor->ws_ou, 'cat.bib.alert_on_empty', $editor);
557 if( OpenILS::Application::Cat::BibCommon->title_is_empty($editor, $vol->record, $vol->id) ) {
559 # delete this volume if it's not already marked as deleted
560 unless( $U->is_true($vol->deleted) || $vol->isdeleted ) {
561 my $evt = $class->delete_volume($editor, $vol, $override, 0, 1);
565 return OpenILS::Event->new('TITLE_LAST_COPY', payload => $vol->record )
566 if $aoe and not ($override->{all} || grep { $_ eq 'TITLE_LAST_COPY' } @{$override->{events}}) and not $force_delete_empty_bib;
568 unless($koe and not $force_delete_empty_bib) {
569 # delete the bib record if the keep-on-empty setting is not set (and we're not otherwise forcing things, say through acq settings)
570 my $evt = OpenILS::Application::Cat::BibCommon->delete_rec($editor, $vol->record);
576 # this may be the last copy attached to the volume.
578 if($U->ou_ancestor_setting_value(
579 $editor->requestor->ws_ou, 'cat.volume.delete_on_empty', $editor)) {
581 # if this volume is "empty" and not mid-delete, delete it.
582 unless($U->is_true($vol->deleted) || $vol->isdeleted) {
584 my $copies = $editor->search_asset_copy(
585 [{call_number => $vol->id, deleted => 'f'}, {limit => 1}], {idlist => 1});
588 my $evt = $class->delete_volume($editor, $vol, $override, 0, 1);
598 # Deletes a volume. Returns undef on success, event on error
599 # force : deletes all attached copies
600 # skip_copy_check : assumes caller has verified no copies need deleting first
602 my($class, $editor, $vol, $override, $delete_copies, $skip_copy_checks) = @_;
605 unless($skip_copy_checks) {
606 my $cs = $editor->search_asset_copy(
607 [{call_number => $vol->id, deleted => 'f'}, {limit => 1}], {idlist => 1});
609 return OpenILS::Event->new('VOLUME_NOT_EMPTY', payload => $vol->id)
610 if @$cs and !$delete_copies;
612 my $copies = $editor->search_asset_copy({call_number => $vol->id, deleted => 'f'});
614 for my $copy (@$copies) {
615 $evt = $class->delete_copy($editor, $override, $vol, $copy, 0, 0, 1);
621 $vol->edit_date('now');
622 $vol->editor($editor->requestor->id);
623 $editor->update_asset_call_number($vol) or return $editor->die_event;
625 $evt = $class->cancel_volume_holds($editor, $vol);
628 # handle the case where this is the last volume on the record
629 return $class->remove_empty_objects($editor, $override, $vol);
634 my($class, $vol, $copy) = @_;
635 my $org = $vol->owning_lib;
636 if( $vol->id == OILS_PRECAT_CALL_NUMBER ) {
637 $org = ref($copy->circ_lib) ? $copy->circ_lib->id : $copy->circ_lib;
639 $logger->debug("using copy perm org $org");
645 my($class, $e, $copy_id) = @_;
647 my $copy = $e->retrieve_asset_copy([
649 {flesh => 1, flesh_fields => {'acp' => ['call_number']}}])
650 or return $e->die_event;
653 ($copy->call_number->id == OILS_PRECAT_CALL_NUMBER) ?
654 $copy->circ_lib : $copy->call_number->owning_lib;
656 my $circ = $e->search_action_circulation(
657 {checkin_time => undef, target_copy => $copy->id} )->[0]
658 or return $e->die_event;
660 $e->allowed('SET_CIRC_LOST', $circ->circ_lib) or return $e->die_event;
662 return $e->die_event(OpenILS::Event->new('COPY_MARKED_LOST'))
663 if $copy->status == OILS_COPY_STATUS_LOST;
665 # ---------------------------------------------------------------------
666 # fetch the related org settings
667 my $proc_fee = $U->ou_ancestor_setting_value(
668 $owning_lib, OILS_SETTING_LOST_PROCESSING_FEE, $e) || 0;
669 my $void_overdue = $U->ou_ancestor_setting_value(
670 $owning_lib, OILS_SETTING_VOID_OVERDUE_ON_LOST, $e) || 0;
672 # ---------------------------------------------------------------------
673 # move the copy into LOST status
674 $copy->status(OILS_COPY_STATUS_LOST);
675 $copy->editor($e->requestor->id);
676 $copy->edit_date('now');
677 $e->update_asset_copy($copy) or return $e->die_event;
679 my $price = $U->get_copy_price($e, $copy, $copy->call_number);
682 my $evt = OpenILS::Application::Circ::CircCommon->create_bill(
683 $e, $price, 3, 'Lost Materials', $circ->id);
687 # ---------------------------------------------------------------------
688 # if there is a processing fee, charge that too
689 if( $proc_fee > 0 ) {
690 my $evt = OpenILS::Application::Circ::CircCommon->create_bill(
691 $e, $proc_fee, 4, 'Lost Materials Processing Fee', $circ->id);
695 # ---------------------------------------------------------------------
696 # mark the circ as lost and stop the fines
697 $circ->stop_fines(OILS_STOP_FINES_LOST);
698 $circ->stop_fines_time('now') unless $circ->stop_fines_time;
699 $e->update_action_circulation($circ) or return $e->die_event;
701 # ---------------------------------------------------------------------
702 # void all overdue fines on this circ if configured
703 if( $void_overdue ) {
704 my $evt = OpenILS::Application::Circ::CircCommon->void_overdues($e, $circ);
708 my $evt = OpenILS::Application::Circ::CircCommon->reopen_xact($e, $circ->id);
711 my $ses = OpenSRF::AppSession->create('open-ils.trigger');
712 $ses->request('open-ils.trigger.event.autocreate', 'lost', $circ, $circ->circ_lib);
714 my $evt2 = OpenILS::Utils::Penalty->calculate_penalties($e, $circ->usr, $U->xact_org($circ->id,$e));
715 return $evt2 if $evt2;