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) = @_;
199 my $org = (ref $copy->circ_lib) ? $copy->circ_lib->id : $copy->circ_lib;
200 return $evt if ( $evt = $class->org_cannot_have_vols($editor, $org) );
202 $logger->info("vol-update: updating copy ".$copy->id);
203 my $orig_copy = $editor->retrieve_asset_copy($copy->id);
205 # Call-number may have changed, find the original
206 my $orig_vol_id = $editor->json_query({select => {acp => ['call_number']}, from => 'acp', where => {id => $copy->id}});
207 my $orig_vol = $editor->retrieve_asset_call_number($orig_vol_id->[0]->{call_number});
209 $copy->editor($editor->requestor->id);
210 $copy->edit_date('now');
212 $copy->age_protect( $copy->age_protect->id )
213 if ref $copy->age_protect;
215 $class->fix_copy_price($copy);
216 $class->check_hold_retarget($editor, $copy, $orig_copy, $retarget_holds);
218 return $editor->event unless $editor->update_asset_copy($copy);
219 return $class->remove_empty_objects($editor, $override, $orig_vol, $force_delete_empty_bib);
222 sub check_hold_retarget {
223 my($class, $editor, $copy, $orig_copy, $retarget_holds) = @_;
224 return unless $retarget_holds;
226 if( !($copy->isdeleted or $U->is_true($copy->deleted)) ) {
227 # see if a status change warrants a retarget
229 $orig_copy = $editor->retrieve_asset_copy($copy->id) unless $orig_copy;
231 if($orig_copy->status == $copy->status) {
232 # no status change, no retarget
236 my $stat = $editor->retrieve_config_copy_status($copy->status);
238 # new status is holdable, no retarget. Later add logic to find potential
239 # holds and retarget those to pick up the newly available copy
240 return if $U->is_true($stat->holdable);
243 my $hold_ids = $editor->search_action_hold_request(
244 { current_copy => $copy->id,
245 cancel_time => undef,
246 fulfillment_time => undef
250 push(@$retarget_holds, @$hold_ids);
254 # this does the actual work
255 sub update_fleshed_copies {
256 my($class, $editor, $override, $vol, $copies, $delete_stats, $retarget_holds, $force_delete_empty_bib) = @_;
259 my $fetchvol = ($vol) ? 0 : 1;
262 $cache{$vol->id} = $vol if $vol;
264 for my $copy (@$copies) {
266 my $copyid = $copy->id;
267 $logger->info("vol-update: inspecting copy $copyid");
269 if( !($vol = $cache{$copy->call_number}) ) {
270 $vol = $cache{$copy->call_number} =
271 $editor->retrieve_asset_call_number($copy->call_number);
272 return $editor->event unless $vol;
275 return $editor->event unless
276 $editor->allowed('UPDATE_COPY', $class->copy_perm_org($vol, $copy));
278 $copy->editor($editor->requestor->id);
279 $copy->edit_date('now');
281 $copy->status( $copy->status->id ) if ref($copy->status);
282 $copy->location( $copy->location->id ) if ref($copy->location);
283 $copy->circ_lib( $copy->circ_lib->id ) if ref($copy->circ_lib);
285 my $sc_entries = $copy->stat_cat_entries;
286 $copy->clear_stat_cat_entries;
288 my $parts = $copy->parts;
291 if( $copy->isdeleted ) {
292 $evt = $class->delete_copy($editor, $override, $vol, $copy, $retarget_holds, $force_delete_empty_bib);
295 } elsif( $copy->isnew ) {
296 $evt = $class->create_copy( $editor, $vol, $copy );
299 } elsif( $copy->ischanged ) {
301 $evt = $class->update_copy( $editor, $override, $vol, $copy, $retarget_holds, $force_delete_empty_bib);
305 $copy->stat_cat_entries( $sc_entries );
306 $evt = $class->update_copy_stat_entries($editor, $copy, $delete_stats);
307 $copy->parts( $parts );
308 # probably okay to use $delete_stats here for simplicity
309 $evt = $class->update_copy_parts($editor, $copy, $delete_stats);
313 $logger->debug("vol-update: done updating copy batch");
320 my($class, $editor, $override, $vol, $copy, $retarget_holds, $force_delete_empty_bib, $skip_empty_cleanup) = @_;
322 return $editor->event unless
323 $editor->allowed('DELETE_COPY', $class->copy_perm_org($vol, $copy));
325 my $stat = $U->copy_status($copy->status)->id;
328 return OpenILS::Event->new('COPY_DELETE_WARNING', payload => $copy->id )
329 if $stat == OILS_COPY_STATUS_CHECKED_OUT or
330 $stat == OILS_COPY_STATUS_IN_TRANSIT or
331 $stat == OILS_COPY_STATUS_ON_HOLDS_SHELF or
332 $stat == OILS_COPY_STATUS_ILL;
335 $logger->info("vol-update: deleting copy ".$copy->id);
338 $copy->editor($editor->requestor->id);
339 $copy->edit_date('now');
340 $editor->update_asset_copy($copy) or return $editor->event;
342 # Delete any open transits for this copy
343 my $transits = $editor->search_action_transit_copy(
344 { target_copy=>$copy->id, dest_recv_time => undef } );
346 for my $t (@$transits) {
347 $editor->delete_action_transit_copy($t)
348 or return $editor->event;
351 my $evt = $class->cancel_copy_holds($editor, $copy);
354 $class->check_hold_retarget($editor, $copy, undef, $retarget_holds);
356 return undef if $skip_empty_cleanup;
358 return $class->remove_empty_objects($editor, $override, $vol, $force_delete_empty_bib);
362 # deletes all holds that specifically target the deleted copy
363 sub cancel_copy_holds {
364 my($class, $editor, $copy) = @_;
366 my $holds = $editor->search_action_hold_request({
368 hold_type => [qw/C R F/],
369 cancel_time => undef,
370 fulfillment_time => undef
373 return $class->cancel_hold_list($editor, $holds);
376 # deletes all holds that specifically target the deleted volume
377 sub cancel_volume_holds {
378 my($class, $editor, $volume) = @_;
380 my $holds = $editor->search_action_hold_request({
381 target => $volume->id,
383 cancel_time => undef,
384 fulfillment_time => undef
387 return $class->cancel_hold_list($editor, $holds);
390 sub cancel_hold_list {
391 my($class, $editor, $holds) = @_;
393 for my $hold (@$holds) {
395 $hold->cancel_time('now');
396 $hold->cancel_cause(1); # un-targeted expiration. Do we need an alternate "target deleted" cause?
397 $editor->update_action_hold_request($hold) or return $editor->die_event;
399 # delete the copy maps.
400 my $maps = $editor->search_action_hold_copy_map({hold => $hold->id});
402 $editor->delete_action_hold_copy_map($_)
403 or return $editor->die_event;
406 # tell A/T the hold was cancelled. Don't wait for a response..
407 my $at_ses = OpenSRF::AppSession->create('open-ils.trigger');
409 'open-ils.trigger.event.autocreate',
410 'hold_request.cancel.expire_no_target',
411 $hold, $hold->pickup_lib);
420 my($class, $override, $editor, $vol) = @_;
423 return $evt if ( $evt = $class->org_cannot_have_vols($editor, $vol->owning_lib) );
425 # see if the record this volume references is marked as deleted
426 my $rec = $editor->retrieve_biblio_record_entry($vol->record)
427 or return $editor->die_event;
428 return OpenILS::Event->new('BIB_RECORD_DELETED', rec => $rec->id)
429 if $U->is_true($rec->deleted);
431 # first lets see if there are any collisions
432 my $vols = $editor->search_asset_call_number( {
433 owning_lib => $vol->owning_lib,
434 record => $vol->record,
435 label => $vol->label,
436 prefix => $vol->prefix,
437 suffix => $vol->suffix,
444 # we've found an exising volume
446 $label = $vol->label;
448 return OpenILS::Event->new(
449 'VOLUME_LABEL_EXISTS', payload => $vol->id);
453 # create a temp label so we can create the new volume,
454 # then de-dup it with the existing volume
455 $vol->label( "__SYSTEM_TMP_$$".time) if $label;
457 $vol->creator($editor->requestor->id);
458 $vol->create_date('now');
459 $vol->editor($editor->requestor->id);
460 $vol->edit_date('now');
463 $editor->create_asset_call_number($vol) or return $editor->die_event;
466 # now restore the label and merge into the existing record
469 OpenILS::Application::Cat::Merge::merge_volumes($editor, [$vol], $$vols[0]);
476 # returns the volume if it exists
478 my($class, $e, $rec_id, $label, $owning_lib, $prefix, $suffix) = @_;
479 return $e->search_asset_call_number(
480 {label => $label, record => $rec_id, owning_lib => $owning_lib, deleted => 'f', prefix => $prefix, suffix => $suffix})->[0];
483 sub find_or_create_volume {
484 my($class, $e, $label, $record_id, $org_id, $prefix, $suffix, $label_class) = @_;
491 if($record_id == OILS_PRECAT_RECORD) {
492 $vol = $e->retrieve_asset_call_number(OILS_PRECAT_CALL_NUMBER)
493 or return (undef, $e->die_event);
496 $vol = $class->volume_exists($e, $record_id, $label, $org_id, $prefix, $suffix);
499 # If the volume exists, return the ID
500 return ($vol, undef, 1) if $vol;
502 # -----------------------------------------------------------------
503 # Otherwise, create a new volume with the given attributes
504 # -----------------------------------------------------------------
505 return (undef, $e->die_event) unless $e->allowed('UPDATE_VOLUME', $org_id);
507 $vol = Fieldmapper::asset::call_number->new;
508 $vol->owning_lib($org_id);
509 $vol->label_class($label_class) if ($label_class);
511 $vol->prefix($prefix);
512 $vol->suffix($suffix);
513 $vol->record($record_id);
515 my $evt = $class->create_volume(0, $e, $vol);
516 return (undef, $evt) if $evt;
522 sub create_copy_note {
523 my($class, $e, $copy, $title, $value, $pub) = @_;
524 my $note = Fieldmapper::asset::copy_note->new;
525 $note->owning_copy($copy->id);
526 $note->creator($e->requestor->id);
527 $note->pub($pub ? 't' : 'f');
528 $note->value($value);
529 $note->title($title);
530 $e->create_asset_copy_note($note) or return $e->die_event;
535 sub remove_empty_objects {
536 my($class, $editor, $override, $vol, $force_delete_empty_bib) = @_;
538 my $koe = $U->ou_ancestor_setting_value(
539 $editor->requestor->ws_ou, 'cat.bib.keep_on_empty', $editor);
540 my $aoe = $U->ou_ancestor_setting_value(
541 $editor->requestor->ws_ou, 'cat.bib.alert_on_empty', $editor);
543 if( OpenILS::Application::Cat::BibCommon->title_is_empty($editor, $vol->record, $vol->id) ) {
545 # delete this volume if it's not already marked as deleted
546 unless( $U->is_true($vol->deleted) || $vol->isdeleted ) {
547 my $evt = $class->delete_volume($editor, $vol, $override, 0, 1);
551 return OpenILS::Event->new('TITLE_LAST_COPY', payload => $vol->record )
552 if $aoe and not $override and not $force_delete_empty_bib;
554 unless($koe and not $force_delete_empty_bib) {
555 # delete the bib record if the keep-on-empty setting is not set (and we're not otherwise forcing things, say through acq settings)
556 my $evt = OpenILS::Application::Cat::BibCommon->delete_rec($editor, $vol->record);
562 # this may be the last copy attached to the volume.
564 if($U->ou_ancestor_setting_value(
565 $editor->requestor->ws_ou, 'cat.volume.delete_on_empty', $editor)) {
567 # if this volume is "empty" and not mid-delete, delete it.
568 unless($U->is_true($vol->deleted) || $vol->isdeleted) {
570 my $copies = $editor->search_asset_copy(
571 [{call_number => $vol->id, deleted => 'f'}, {limit => 1}], {idlist => 1});
574 my $evt = $class->delete_volume($editor, $vol, $override, 0, 1);
584 # Deletes a volume. Returns undef on success, event on error
585 # force : deletes all attached copies
586 # skip_copy_check : assumes caller has verified no copies need deleting first
588 my($class, $editor, $vol, $override, $delete_copies, $skip_copy_checks) = @_;
591 unless($skip_copy_checks) {
592 my $cs = $editor->search_asset_copy(
593 [{call_number => $vol->id, deleted => 'f'}, {limit => 1}], {idlist => 1});
595 return OpenILS::Event->new('VOLUME_NOT_EMPTY', payload => $vol->id)
596 if @$cs and !$delete_copies;
598 my $copies = $editor->search_asset_copy({call_number => $vol->id, deleted => 'f'});
600 for my $copy (@$copies) {
601 $evt = $class->delete_copy($editor, $override, $vol, $copy, 0, 0, 1);
607 $vol->edit_date('now');
608 $vol->editor($editor->requestor->id);
609 $editor->update_asset_call_number($vol) or return $editor->die_event;
611 $evt = $class->cancel_volume_holds($editor, $vol);
614 # handle the case where this is the last volume on the record
615 return $class->remove_empty_objects($editor, $override, $vol);
620 my($class, $vol, $copy) = @_;
621 my $org = $vol->owning_lib;
622 if( $vol->id == OILS_PRECAT_CALL_NUMBER ) {
623 $org = ref($copy->circ_lib) ? $copy->circ_lib->id : $copy->circ_lib;
625 $logger->debug("using copy perm org $org");
631 my($class, $e, $copy_id) = @_;
633 my $copy = $e->retrieve_asset_copy([
635 {flesh => 1, flesh_fields => {'acp' => ['call_number']}}])
636 or return $e->die_event;
639 ($copy->call_number->id == OILS_PRECAT_CALL_NUMBER) ?
640 $copy->circ_lib : $copy->call_number->owning_lib;
642 my $circ = $e->search_action_circulation(
643 {checkin_time => undef, target_copy => $copy->id} )->[0]
644 or return $e->die_event;
646 $e->allowed('SET_CIRC_LOST', $circ->circ_lib) or return $e->die_event;
648 return $e->die_event(OpenILS::Event->new('COPY_MARKED_LOST'))
649 if $copy->status == OILS_COPY_STATUS_LOST;
651 # ---------------------------------------------------------------------
652 # fetch the related org settings
653 my $proc_fee = $U->ou_ancestor_setting_value(
654 $owning_lib, OILS_SETTING_LOST_PROCESSING_FEE, $e) || 0;
655 my $void_overdue = $U->ou_ancestor_setting_value(
656 $owning_lib, OILS_SETTING_VOID_OVERDUE_ON_LOST, $e) || 0;
658 # ---------------------------------------------------------------------
659 # move the copy into LOST status
660 $copy->status(OILS_COPY_STATUS_LOST);
661 $copy->editor($e->requestor->id);
662 $copy->edit_date('now');
663 $e->update_asset_copy($copy) or return $e->die_event;
665 my $price = $U->get_copy_price($e, $copy, $copy->call_number);
668 my $evt = OpenILS::Application::Circ::CircCommon->create_bill(
669 $e, $price, 3, 'Lost Materials', $circ->id);
673 # ---------------------------------------------------------------------
674 # if there is a processing fee, charge that too
675 if( $proc_fee > 0 ) {
676 my $evt = OpenILS::Application::Circ::CircCommon->create_bill(
677 $e, $proc_fee, 4, 'Lost Materials Processing Fee', $circ->id);
681 # ---------------------------------------------------------------------
682 # mark the circ as lost and stop the fines
683 $circ->stop_fines(OILS_STOP_FINES_LOST);
684 $circ->stop_fines_time('now') unless $circ->stop_fines_time;
685 $e->update_action_circulation($circ) or return $e->die_event;
687 # ---------------------------------------------------------------------
688 # void all overdue fines on this circ if configured
689 if( $void_overdue ) {
690 my $evt = OpenILS::Application::Circ::CircCommon->void_overdues($e, $circ);
694 my $evt = OpenILS::Application::Circ::CircCommon->reopen_xact($e, $circ->id);
697 my $ses = OpenSRF::AppSession->create('open-ils.trigger');
698 $ses->request('open-ils.trigger.event.autocreate', 'lost', $circ, $circ->circ_lib);
700 my $evt2 = OpenILS::Utils::Penalty->calculate_penalties($e, $circ->usr, $U->xact_org($circ->id,$e));
701 return $evt2 if $evt2;