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::Application::Circ::CircCommon;
13 my $U = 'OpenILS::Application::AppUtils';
16 # ---------------------------------------------------------------------------
17 # Shared copy mangling code. Do not publish methods from here.
18 # ---------------------------------------------------------------------------
20 sub org_cannot_have_vols {
21 my($class, $e, $org_id) = @_;
22 my $org = $e->retrieve_actor_org_unit([
25 flesh_fields => {aou => ['ou_type']}
26 }]) or return $e->event;
28 return OpenILS::Event->new('ORG_CANNOT_HAVE_VOLS')
29 unless $U->is_true($org->ou_type->can_have_vols);
38 if(defined $copy->price) {
39 my $p = $copy->price || 0;
44 my $d = $copy->deposit_amount || 0;
46 $copy->deposit_amount($d);
50 my($class, $editor, $vol, $copy) = @_;
52 my $existing = $editor->search_asset_copy(
53 { barcode => $copy->barcode, deleted => 'f' } );
55 return OpenILS::Event->new('ITEM_BARCODE_EXISTS') if @$existing;
57 # see if the volume this copy references is marked as deleted
58 return OpenILS::Event->new('VOLUME_DELETED', vol => $vol->id)
59 if $U->is_true($vol->deleted);
62 my $org = (ref $copy->circ_lib) ? $copy->circ_lib->id : $copy->circ_lib;
63 return $evt if ($evt = $class->org_cannot_have_vols($editor, $org));
66 $copy->editor($editor->requestor->id);
67 $copy->creator($editor->requestor->id);
68 $copy->create_date('now');
69 $copy->call_number($vol->id);
70 $class->fix_copy_price($copy);
72 $editor->create_asset_copy($copy) or return $editor->die_event;
77 # if 'delete_stats' is true, the copy->stat_cat_entries data is
78 # treated as the authoritative list for the copy. existing entries
79 # that are not in said list will be deleted from the DB
80 sub update_copy_stat_entries {
81 my($class, $editor, $copy, $delete_stats) = @_;
83 return undef if $copy->isdeleted;
84 return undef unless $copy->ischanged or $copy->isnew;
87 my $entries = $copy->stat_cat_entries;
90 $entries = ($entries and @$entries) ? $entries : [];
92 return undef unless ($entries and @$entries);
95 my $maps = $editor->search_asset_stat_cat_entry_copy_map({owning_copy=>$copy->id});
98 # if there is no stat cat entry on the copy who's id matches the
99 # current map's id, remove the map from the database
100 for my $map (@$maps) {
101 if(! grep { $_->id == $map->stat_cat_entry } @$entries ) {
103 $logger->info("copy update found stale ".
104 "stat cat entry map ".$map->id. " on copy ".$copy->id);
106 $editor->delete_asset_stat_cat_entry_copy_map($map)
107 or return $editor->event;
112 # go through the stat cat update/create process
113 for my $entry (@$entries) {
116 # if this link already exists in the DB, don't attempt to re-create it
117 next if( grep{$_->stat_cat_entry == $entry->id} @$maps );
119 my $new_map = Fieldmapper::asset::stat_cat_entry_copy_map->new();
121 my $sc = ref($entry->stat_cat) ? $entry->stat_cat->id : $entry->stat_cat;
123 $new_map->stat_cat( $sc );
124 $new_map->stat_cat_entry( $entry->id );
125 $new_map->owning_copy( $copy->id );
127 $editor->create_asset_stat_cat_entry_copy_map($new_map)
128 or return $editor->event;
130 $logger->info("copy update created new stat cat entry map ".$editor->data);
136 # if 'delete_maps' is true, the copy->parts data is treated as the
137 # authoritative list for the copy. existing part maps not targeting
138 # these parts will be deleted from the DB
139 sub update_copy_parts {
140 my($class, $editor, $copy, $delete_maps) = @_;
142 return undef if $copy->isdeleted;
143 return undef unless $copy->ischanged or $copy->isnew;
146 my $incoming_parts = $copy->parts;
149 $incoming_parts = ($incoming_parts and @$incoming_parts) ? $incoming_parts : [];
151 return undef unless ($incoming_parts and @$incoming_parts);
154 my $maps = $editor->search_asset_copy_part_map({target_copy=>$copy->id});
157 # if there is no part map on the copy who's id matches the
158 # current map's id, remove the map from the database
159 for my $map (@$maps) {
160 if(! grep { $_->id == $map->part } @$incoming_parts ) {
162 $logger->info("copy update found stale ".
163 "monographic part map ".$map->id. " on copy ".$copy->id);
165 $editor->delete_asset_copy_part_map($map)
166 or return $editor->event;
171 # go through the part map update/create process
172 for my $incoming_part (@$incoming_parts) {
173 next unless $incoming_part;
175 # if this link already exists in the DB, don't attempt to re-create it
176 next if( grep{$_->part == $incoming_part->id} @$maps );
178 my $new_map = Fieldmapper::asset::copy_part_map->new();
180 $new_map->part( $incoming_part->id );
181 $new_map->target_copy( $copy->id );
183 $editor->create_asset_copy_part_map($new_map)
184 or return $editor->event;
186 $logger->info("copy update created new monographic part copy map ".$editor->data);
195 my($class, $editor, $override, $vol, $copy, $retarget_holds, $force_delete_empty_bib) = @_;
198 my $org = (ref $copy->circ_lib) ? $copy->circ_lib->id : $copy->circ_lib;
199 return $evt if ( $evt = $class->org_cannot_have_vols($editor, $org) );
201 $logger->info("vol-update: updating copy ".$copy->id);
202 my $orig_copy = $editor->retrieve_asset_copy($copy->id);
203 my $orig_vol = $editor->retrieve_asset_call_number($copy->call_number);
205 $copy->editor($editor->requestor->id);
206 $copy->edit_date('now');
208 $copy->age_protect( $copy->age_protect->id )
209 if ref $copy->age_protect;
211 $class->fix_copy_price($copy);
212 $class->check_hold_retarget($editor, $copy, $orig_copy, $retarget_holds);
214 return $editor->event unless $editor->update_asset_copy($copy);
215 return $class->remove_empty_objects($editor, $override, $orig_vol, $force_delete_empty_bib);
218 sub check_hold_retarget {
219 my($class, $editor, $copy, $orig_copy, $retarget_holds) = @_;
220 return unless $retarget_holds;
222 if( !($copy->isdeleted or $U->is_true($copy->deleted)) ) {
223 # see if a status change warrants a retarget
225 $orig_copy = $editor->retrieve_asset_copy($copy->id) unless $orig_copy;
227 if($orig_copy->status == $copy->status) {
228 # no status change, no retarget
232 my $stat = $editor->retrieve_config_copy_status($copy->status);
234 # new status is holdable, no retarget. Later add logic to find potential
235 # holds and retarget those to pick up the newly available copy
236 return if $U->is_true($stat->holdable);
239 my $hold_ids = $editor->search_action_hold_request(
240 { current_copy => $copy->id,
241 cancel_time => undef,
242 fulfillment_time => undef
246 push(@$retarget_holds, @$hold_ids);
250 # this does the actual work
251 sub update_fleshed_copies {
252 my($class, $editor, $override, $vol, $copies, $delete_stats, $retarget_holds, $force_delete_empty_bib) = @_;
255 my $fetchvol = ($vol) ? 0 : 1;
258 $cache{$vol->id} = $vol if $vol;
260 for my $copy (@$copies) {
262 my $copyid = $copy->id;
263 $logger->info("vol-update: inspecting copy $copyid");
265 if( !($vol = $cache{$copy->call_number}) ) {
266 $vol = $cache{$copy->call_number} =
267 $editor->retrieve_asset_call_number($copy->call_number);
268 return $editor->event unless $vol;
271 return $editor->event unless
272 $editor->allowed('UPDATE_COPY', $class->copy_perm_org($vol, $copy));
274 $copy->editor($editor->requestor->id);
275 $copy->edit_date('now');
277 $copy->status( $copy->status->id ) if ref($copy->status);
278 $copy->location( $copy->location->id ) if ref($copy->location);
279 $copy->circ_lib( $copy->circ_lib->id ) if ref($copy->circ_lib);
281 my $sc_entries = $copy->stat_cat_entries;
282 $copy->clear_stat_cat_entries;
284 my $parts = $copy->parts;
287 if( $copy->isdeleted ) {
288 $evt = $class->delete_copy($editor, $override, $vol, $copy, $retarget_holds, $force_delete_empty_bib);
291 } elsif( $copy->isnew ) {
292 $evt = $class->create_copy( $editor, $vol, $copy );
295 } elsif( $copy->ischanged ) {
297 $evt = $class->update_copy( $editor, $override, $vol, $copy, $retarget_holds, $force_delete_empty_bib);
301 $copy->stat_cat_entries( $sc_entries );
302 $evt = $class->update_copy_stat_entries($editor, $copy, $delete_stats);
303 $copy->parts( $parts );
304 # probably okay to use $delete_stats here for simplicity
305 $evt = $class->update_copy_parts($editor, $copy, $delete_stats);
309 $logger->debug("vol-update: done updating copy batch");
316 my($class, $editor, $override, $vol, $copy, $retarget_holds, $force_delete_empty_bib, $skip_empty_cleanup) = @_;
318 return $editor->event unless
319 $editor->allowed('DELETE_COPY', $class->copy_perm_org($vol, $copy));
321 my $stat = $U->copy_status($copy->status)->id;
324 return OpenILS::Event->new('COPY_DELETE_WARNING', payload => $copy->id )
325 if $stat == OILS_COPY_STATUS_CHECKED_OUT or
326 $stat == OILS_COPY_STATUS_IN_TRANSIT or
327 $stat == OILS_COPY_STATUS_ON_HOLDS_SHELF or
328 $stat == OILS_COPY_STATUS_ILL;
331 $logger->info("vol-update: deleting copy ".$copy->id);
334 $copy->editor($editor->requestor->id);
335 $copy->edit_date('now');
336 $editor->update_asset_copy($copy) or return $editor->event;
338 # Delete any open transits for this copy
339 my $transits = $editor->search_action_transit_copy(
340 { target_copy=>$copy->id, dest_recv_time => undef } );
342 for my $t (@$transits) {
343 $editor->delete_action_transit_copy($t)
344 or return $editor->event;
347 $class->check_hold_retarget($editor, $copy, undef, $retarget_holds);
349 return undef if $skip_empty_cleanup;
351 return $class->remove_empty_objects($editor, $override, $vol, $force_delete_empty_bib);
357 my($class, $override, $editor, $vol) = @_;
360 return $evt if ( $evt = $class->org_cannot_have_vols($editor, $vol->owning_lib) );
362 # see if the record this volume references is marked as deleted
363 my $rec = $editor->retrieve_biblio_record_entry($vol->record)
364 or return $editor->die_event;
365 return OpenILS::Event->new('BIB_RECORD_DELETED', rec => $rec->id)
366 if $U->is_true($rec->deleted);
368 # first lets see if there are any collisions
369 my $vols = $editor->search_asset_call_number( {
370 owning_lib => $vol->owning_lib,
371 record => $vol->record,
372 label => $vol->label,
373 prefix => $vol->prefix,
374 suffix => $vol->suffix,
381 # we've found an exising volume
383 $label = $vol->label;
385 return OpenILS::Event->new(
386 'VOLUME_LABEL_EXISTS', payload => $vol->id);
390 # create a temp label so we can create the new volume,
391 # then de-dup it with the existing volume
392 $vol->label( "__SYSTEM_TMP_$$".time) if $label;
394 $vol->creator($editor->requestor->id);
395 $vol->create_date('now');
396 $vol->editor($editor->requestor->id);
397 $vol->edit_date('now');
400 $editor->create_asset_call_number($vol) or return $editor->die_event;
403 # now restore the label and merge into the existing record
406 OpenILS::Application::Cat::Merge::merge_volumes($editor, [$vol], $$vols[0]);
413 # returns the volume if it exists
415 my($class, $e, $rec_id, $label, $owning_lib, $prefix, $suffix) = @_;
416 return $e->search_asset_call_number(
417 {label => $label, record => $rec_id, owning_lib => $owning_lib, deleted => 'f', prefix => $prefix, suffix => $suffix})->[0];
420 sub find_or_create_volume {
421 my($class, $e, $label, $record_id, $org_id, $prefix, $suffix, $label_class) = @_;
428 if($record_id == OILS_PRECAT_RECORD) {
429 $vol = $e->retrieve_asset_call_number(OILS_PRECAT_CALL_NUMBER)
430 or return (undef, $e->die_event);
433 $vol = $class->volume_exists($e, $record_id, $label, $org_id, $prefix, $suffix);
436 # If the volume exists, return the ID
437 return ($vol, undef, 1) if $vol;
439 # -----------------------------------------------------------------
440 # Otherwise, create a new volume with the given attributes
441 # -----------------------------------------------------------------
442 return (undef, $e->die_event) unless $e->allowed('UPDATE_VOLUME', $org_id);
444 $vol = Fieldmapper::asset::call_number->new;
445 $vol->owning_lib($org_id);
446 $vol->label_class($label_class) if ($label_class);
448 $vol->prefix($prefix);
449 $vol->suffix($suffix);
450 $vol->record($record_id);
452 my $evt = $class->create_volume(0, $e, $vol);
453 return (undef, $evt) if $evt;
459 sub create_copy_note {
460 my($class, $e, $copy, $title, $value, $pub) = @_;
461 my $note = Fieldmapper::asset::copy_note->new;
462 $note->owning_copy($copy->id);
463 $note->creator($e->requestor->id);
465 $note->value($value);
466 $note->title($title);
467 $e->create_asset_copy_note($note) or return $e->die_event;
472 sub remove_empty_objects {
473 my($class, $editor, $override, $vol, $force_delete_empty_bib) = @_;
475 my $koe = $U->ou_ancestor_setting_value(
476 $editor->requestor->ws_ou, 'cat.bib.keep_on_empty', $editor);
477 my $aoe = $U->ou_ancestor_setting_value(
478 $editor->requestor->ws_ou, 'cat.bib.alert_on_empty', $editor);
480 if( OpenILS::Application::Cat::BibCommon->title_is_empty($editor, $vol->record, $vol->id) ) {
482 # delete this volume if it's not already marked as deleted
483 unless( $U->is_true($vol->deleted) || $vol->isdeleted ) {
484 my $evt = $class->delete_volume($editor, $vol, $override, 0, 1);
488 return OpenILS::Event->new('TITLE_LAST_COPY', payload => $vol->record )
489 if $aoe and not $override and not $force_delete_empty_bib;
491 unless($koe and not $force_delete_empty_bib) {
492 # delete the bib record if the keep-on-empty setting is not set (and we're not otherwise forcing things, say through acq settings)
493 my $evt = OpenILS::Application::Cat::BibCommon->delete_rec($editor, $vol->record);
501 # Deletes a volume. Returns undef on success, event on error
502 # force : deletes all attached copies
503 # skip_copy_check : assumes caller has verified no copies need deleting first
505 my($class, $editor, $vol, $override, $delete_copies, $skip_copy_checks) = @_;
508 unless($skip_copy_checks) {
509 my $cs = $editor->search_asset_copy(
510 [{call_number => $vol->id, deleted => 'f'}, {limit => 1}], {idlist => 1});
512 return OpenILS::Event->new('VOLUME_NOT_EMPTY', payload => $vol->id)
513 if @$cs and !$delete_copies;
515 my $copies = $editor->search_asset_copy({call_number => $vol->id, deleted => 'f'});
517 for my $copy (@$copies) {
518 $evt = $class->delete_copy($editor, $override, $vol, $copy, 0, 0, 1);
524 $vol->edit_date('now');
525 $vol->editor($editor->requestor->id);
526 $editor->update_asset_call_number($vol) or return $editor->die_event;
528 # handle the case where this is the last volume on the record
529 return $class->remove_empty_objects($editor, $override, $vol);
534 my($class, $vol, $copy) = @_;
535 my $org = $vol->owning_lib;
536 if( $vol->id == OILS_PRECAT_CALL_NUMBER ) {
537 $org = ref($copy->circ_lib) ? $copy->circ_lib->id : $copy->circ_lib;
539 $logger->debug("using copy perm org $org");
545 my($class, $e, $copy_id) = @_;
547 my $copy = $e->retrieve_asset_copy([
549 {flesh => 1, flesh_fields => {'acp' => ['call_number']}}])
550 or return $e->die_event;
553 ($copy->call_number->id == OILS_PRECAT_CALL_NUMBER) ?
554 $copy->circ_lib : $copy->call_number->owning_lib;
556 my $circ = $e->search_action_circulation(
557 {checkin_time => undef, target_copy => $copy->id} )->[0]
558 or return $e->die_event;
560 $e->allowed('SET_CIRC_LOST', $circ->circ_lib) or return $e->die_event;
562 return $e->die_event(OpenILS::Event->new('COPY_MARKED_LOST'))
563 if $copy->status == OILS_COPY_STATUS_LOST;
565 # ---------------------------------------------------------------------
566 # fetch the related org settings
567 my $proc_fee = $U->ou_ancestor_setting_value(
568 $owning_lib, OILS_SETTING_LOST_PROCESSING_FEE, $e) || 0;
569 my $void_overdue = $U->ou_ancestor_setting_value(
570 $owning_lib, OILS_SETTING_VOID_OVERDUE_ON_LOST, $e) || 0;
572 # ---------------------------------------------------------------------
573 # move the copy into LOST status
574 $copy->status(OILS_COPY_STATUS_LOST);
575 $copy->editor($e->requestor->id);
576 $copy->edit_date('now');
577 $e->update_asset_copy($copy) or return $e->die_event;
579 my $price = $U->get_copy_price($e, $copy, $copy->call_number);
582 my $evt = OpenILS::Application::Circ::CircCommon->create_bill(
583 $e, $price, 3, 'Lost Materials', $circ->id);
587 # ---------------------------------------------------------------------
588 # if there is a processing fee, charge that too
589 if( $proc_fee > 0 ) {
590 my $evt = OpenILS::Application::Circ::CircCommon->create_bill(
591 $e, $proc_fee, 4, 'Lost Materials Processing Fee', $circ->id);
595 # ---------------------------------------------------------------------
596 # mark the circ as lost and stop the fines
597 $circ->stop_fines(OILS_STOP_FINES_LOST);
598 $circ->stop_fines_time('now') unless $circ->stop_fines_time;
599 $e->update_action_circulation($circ) or return $e->die_event;
601 # ---------------------------------------------------------------------
602 # void all overdue fines on this circ if configured
603 if( $void_overdue ) {
604 my $evt = OpenILS::Application::Circ::CircCommon->void_overdues($e, $circ);
608 my $evt = OpenILS::Application::Circ::CircCommon->reopen_xact($e, $circ->id);
611 my $ses = OpenSRF::AppSession->create('open-ils.trigger');
612 $ses->request('open-ils.trigger.event.autocreate', 'lost', $circ, $circ->circ_lib);