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 # tell A/T the hold was cancelled. Don't wait for a response..
408 my $at_ses = OpenSRF::AppSession->create('open-ils.trigger');
410 'open-ils.trigger.event.autocreate',
411 'hold_request.cancel.expire_no_target',
412 $hold, $hold->pickup_lib);
421 my($class, $override, $editor, $vol) = @_;
424 return $evt if ( $evt = $class->org_cannot_have_vols($editor, $vol->owning_lib) );
426 $override = { all => 1 } if($override && !ref $override);
427 $override = { all => 0 } if(!ref $override);
429 # see if the record this volume references is marked as deleted
430 my $rec = $editor->retrieve_biblio_record_entry($vol->record)
431 or return $editor->die_event;
432 return OpenILS::Event->new('BIB_RECORD_DELETED', rec => $rec->id)
433 if $U->is_true($rec->deleted);
435 # first lets see if there are any collisions
436 my $vols = $editor->search_asset_call_number( {
437 owning_lib => $vol->owning_lib,
438 record => $vol->record,
439 label => $vol->label,
440 prefix => $vol->prefix,
441 suffix => $vol->suffix,
448 # we've found an exising volume
449 if($override->{all} || grep { $_ eq 'VOLUME_LABEL_EXISTS' } @{$override->{events}}) {
450 $label = $vol->label;
452 return OpenILS::Event->new(
453 'VOLUME_LABEL_EXISTS', payload => $vol->id);
457 # create a temp label so we can create the new volume,
458 # then de-dup it with the existing volume
459 $vol->label( "__SYSTEM_TMP_$$".time) if $label;
461 $vol->creator($editor->requestor->id);
462 $vol->create_date('now');
463 $vol->editor($editor->requestor->id);
464 $vol->edit_date('now');
467 $editor->create_asset_call_number($vol) or return $editor->die_event;
470 # now restore the label and merge into the existing record
473 OpenILS::Application::Cat::Merge::merge_volumes($editor, [$vol], $$vols[0]);
480 # returns the volume if it exists
482 my($class, $e, $rec_id, $label, $owning_lib, $prefix, $suffix) = @_;
483 return $e->search_asset_call_number(
484 {label => $label, record => $rec_id, owning_lib => $owning_lib, deleted => 'f', prefix => $prefix, suffix => $suffix})->[0];
487 sub find_or_create_volume {
488 my($class, $e, $label, $record_id, $org_id, $prefix, $suffix, $label_class) = @_;
495 if($record_id == OILS_PRECAT_RECORD) {
496 $vol = $e->retrieve_asset_call_number(OILS_PRECAT_CALL_NUMBER)
497 or return (undef, $e->die_event);
500 $vol = $class->volume_exists($e, $record_id, $label, $org_id, $prefix, $suffix);
503 # If the volume exists, return the ID
504 return ($vol, undef, 1) if $vol;
506 # -----------------------------------------------------------------
507 # Otherwise, create a new volume with the given attributes
508 # -----------------------------------------------------------------
509 return (undef, $e->die_event) unless $e->allowed('UPDATE_VOLUME', $org_id);
511 $vol = Fieldmapper::asset::call_number->new;
512 $vol->owning_lib($org_id);
513 $vol->label_class($label_class) if ($label_class);
515 $vol->prefix($prefix);
516 $vol->suffix($suffix);
517 $vol->record($record_id);
519 my $evt = $class->create_volume(0, $e, $vol);
520 return (undef, $evt) if $evt;
526 sub create_copy_note {
527 my($class, $e, $copy, $title, $value, $pub) = @_;
528 my $note = Fieldmapper::asset::copy_note->new;
529 $note->owning_copy($copy->id);
530 $note->creator($e->requestor->id);
531 $note->pub($pub ? 't' : 'f');
532 $note->value($value);
533 $note->title($title);
534 $e->create_asset_copy_note($note) or return $e->die_event;
539 sub remove_empty_objects {
540 my($class, $editor, $override, $vol, $force_delete_empty_bib) = @_;
542 $override = { all => 1 } if($override && !ref $override);
543 $override = { all => 0 } if(!ref $override);
545 my $koe = $U->ou_ancestor_setting_value(
546 $editor->requestor->ws_ou, 'cat.bib.keep_on_empty', $editor);
547 my $aoe = $U->ou_ancestor_setting_value(
548 $editor->requestor->ws_ou, 'cat.bib.alert_on_empty', $editor);
550 if( OpenILS::Application::Cat::BibCommon->title_is_empty($editor, $vol->record, $vol->id) ) {
552 # delete this volume if it's not already marked as deleted
553 unless( $U->is_true($vol->deleted) || $vol->isdeleted ) {
554 my $evt = $class->delete_volume($editor, $vol, $override, 0, 1);
558 return OpenILS::Event->new('TITLE_LAST_COPY', payload => $vol->record )
559 if $aoe and not ($override->{all} || grep { $_ eq 'TITLE_LAST_COPY' } @{$override->{events}}) and not $force_delete_empty_bib;
561 unless($koe and not $force_delete_empty_bib) {
562 # delete the bib record if the keep-on-empty setting is not set (and we're not otherwise forcing things, say through acq settings)
563 my $evt = OpenILS::Application::Cat::BibCommon->delete_rec($editor, $vol->record);
569 # this may be the last copy attached to the volume.
571 if($U->ou_ancestor_setting_value(
572 $editor->requestor->ws_ou, 'cat.volume.delete_on_empty', $editor)) {
574 # if this volume is "empty" and not mid-delete, delete it.
575 unless($U->is_true($vol->deleted) || $vol->isdeleted) {
577 my $copies = $editor->search_asset_copy(
578 [{call_number => $vol->id, deleted => 'f'}, {limit => 1}], {idlist => 1});
581 my $evt = $class->delete_volume($editor, $vol, $override, 0, 1);
591 # Deletes a volume. Returns undef on success, event on error
592 # force : deletes all attached copies
593 # skip_copy_check : assumes caller has verified no copies need deleting first
595 my($class, $editor, $vol, $override, $delete_copies, $skip_copy_checks) = @_;
598 unless($skip_copy_checks) {
599 my $cs = $editor->search_asset_copy(
600 [{call_number => $vol->id, deleted => 'f'}, {limit => 1}], {idlist => 1});
602 return OpenILS::Event->new('VOLUME_NOT_EMPTY', payload => $vol->id)
603 if @$cs and !$delete_copies;
605 my $copies = $editor->search_asset_copy({call_number => $vol->id, deleted => 'f'});
607 for my $copy (@$copies) {
608 $evt = $class->delete_copy($editor, $override, $vol, $copy, 0, 0, 1);
614 $vol->edit_date('now');
615 $vol->editor($editor->requestor->id);
616 $editor->update_asset_call_number($vol) or return $editor->die_event;
618 $evt = $class->cancel_volume_holds($editor, $vol);
621 # handle the case where this is the last volume on the record
622 return $class->remove_empty_objects($editor, $override, $vol);
627 my($class, $vol, $copy) = @_;
628 my $org = $vol->owning_lib;
629 if( $vol->id == OILS_PRECAT_CALL_NUMBER ) {
630 $org = ref($copy->circ_lib) ? $copy->circ_lib->id : $copy->circ_lib;
632 $logger->debug("using copy perm org $org");
638 my ($class, $e, $copy_id) = @_;
640 return $class->set_item_lost_or_lod(
642 perm => 'SET_CIRC_LOST',
643 status => OILS_COPY_STATUS_LOST,
644 ous_proc_fee => OILS_SETTING_LOST_PROCESSING_FEE,
645 ous_void_od => OILS_SETTING_VOID_OVERDUE_ON_LOST,
648 bill_note => 'Lost Materials',
649 bill_fee_note => 'Lost Materials Processing Fee',
650 event => 'COPY_MARKED_LOST',
651 stop_fines => OILS_STOP_FINES_LOST,
656 sub set_item_long_overdue {
657 my ($class, $e, $copy_id) = @_;
659 return $class->set_item_lost_or_lod(
661 perm => 'SET_CIRC_LONG_OVERDUE',
662 status => 16, # Long Overdue
663 ous_proc_fee => 'circ.longoverdue_materials_processing_fee',
664 ous_void_od => 'circ.void_overdue_on_longoverdue',
667 bill_note => 'Long Overdue Materials',
668 bill_fee_note => 'Long Overdue Materials Processing Fee',
669 event => 'COPY_MARKED_LONG_OVERDUE',
670 stop_fines => 'LONGOVERDUE',
671 at_hook => 'longoverdue'
675 # LOST or LONGOVERDUE
676 # basic process is the same. details change.
677 sub set_item_lost_or_lod {
678 my ($class, $e, $copy_id, %args) = @_;
680 my $copy = $e->retrieve_asset_copy([
682 {flesh => 1, flesh_fields => {'acp' => ['call_number']}}])
683 or return $e->die_event;
686 ($copy->call_number->id == OILS_PRECAT_CALL_NUMBER) ?
687 $copy->circ_lib : $copy->call_number->owning_lib;
689 my $circ = $e->search_action_circulation(
690 {checkin_time => undef, target_copy => $copy->id} )->[0]
691 or return $e->die_event;
693 $e->allowed($args{perm}, $circ->circ_lib) or return $e->die_event;
695 return $e->die_event(OpenILS::Event->new($args{event}))
696 if $copy->status == $args{status};
698 # ---------------------------------------------------------------------
699 # fetch the related org settings
700 my $proc_fee = $U->ou_ancestor_setting_value(
701 $owning_lib, $args{ous_proc_fee}, $e) || 0;
702 my $void_overdue = $U->ou_ancestor_setting_value(
703 $owning_lib, $args{ous_void_od}, $e) || 0;
705 # ---------------------------------------------------------------------
706 # move the copy into LOST status
707 $copy->status($args{status});
708 $copy->editor($e->requestor->id);
709 $copy->edit_date('now');
710 $e->update_asset_copy($copy) or return $e->die_event;
712 my $price = $U->get_copy_price($e, $copy, $copy->call_number);
715 my $evt = OpenILS::Application::Circ::CircCommon->create_bill($e,
716 $price, $args{bill_type}, $args{bill_note}, $circ->id);
720 # ---------------------------------------------------------------------
721 # if there is a processing fee, charge that too
722 if( $proc_fee > 0 ) {
723 my $evt = OpenILS::Application::Circ::CircCommon->create_bill($e,
724 $proc_fee, $args{bill_fee_type}, $args{bill_fee_note}, $circ->id);
728 # ---------------------------------------------------------------------
729 # mark the circ as lost and stop the fines
730 $circ->stop_fines($args{stop_fines});
731 $circ->stop_fines_time('now') unless $circ->stop_fines_time;
732 $e->update_action_circulation($circ) or return $e->die_event;
734 # ---------------------------------------------------------------------
735 # void all overdue fines on this circ if configured
736 if( $void_overdue ) {
737 my $evt = OpenILS::Application::Circ::CircCommon->void_overdues($e, $circ);
741 my $evt = OpenILS::Application::Circ::CircCommon->reopen_xact($e, $circ->id);
744 my $ses = OpenSRF::AppSession->create('open-ils.trigger');
746 'open-ils.trigger.event.autocreate',
747 $args{at_hook}, $circ, $circ->circ_lib
750 my $evt2 = OpenILS::Utils::Penalty->calculate_penalties(
751 $e, $circ->usr, $U->xact_org($circ->id, $e));
752 return $evt2 if $evt2;