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 = OpenILS::Application::Cat::AssetCommon->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);
138 my($class, $editor, $override, $vol, $copy, $retarget_holds, $force_delete_empty_bib) = @_;
141 my $org = (ref $copy->circ_lib) ? $copy->circ_lib->id : $copy->circ_lib;
142 return $evt if ( $evt = OpenILS::Application::Cat::AssetCommon->org_cannot_have_vols($editor, $org) );
144 $logger->info("vol-update: updating copy ".$copy->id);
145 my $orig_copy = $editor->retrieve_asset_copy($copy->id);
146 my $orig_vol = $editor->retrieve_asset_call_number($copy->call_number);
148 $copy->editor($editor->requestor->id);
149 $copy->edit_date('now');
151 $copy->age_protect( $copy->age_protect->id )
152 if ref $copy->age_protect;
154 $class->fix_copy_price($copy);
155 $class->check_hold_retarget($editor, $copy, $orig_copy, $retarget_holds);
157 return $editor->event unless $editor->update_asset_copy($copy);
158 return $class->remove_empty_objects($editor, $override, $orig_vol, $force_delete_empty_bib);
161 sub check_hold_retarget {
162 my($class, $editor, $copy, $orig_copy, $retarget_holds) = @_;
163 return unless $retarget_holds;
165 if( !($copy->isdeleted or $U->is_true($copy->deleted)) ) {
166 # see if a status change warrants a retarget
168 $orig_copy = $editor->retrieve_asset_copy($copy->id) unless $orig_copy;
170 if($orig_copy->status == $copy->status) {
171 # no status change, no retarget
175 my $stat = $editor->retrieve_config_copy_status($copy->status);
177 # new status is holdable, no retarget. Later add logic to find potential
178 # holds and retarget those to pick up the newly available copy
179 return if $U->is_true($stat->holdable);
182 my $hold_ids = $editor->search_action_hold_request(
183 { current_copy => $copy->id,
184 cancel_time => undef,
185 fulfillment_time => undef
189 push(@$retarget_holds, @$hold_ids);
193 # this does the actual work
194 sub update_fleshed_copies {
195 my($class, $editor, $override, $vol, $copies, $delete_stats, $retarget_holds, $force_delete_empty_bib) = @_;
198 my $fetchvol = ($vol) ? 0 : 1;
201 $cache{$vol->id} = $vol if $vol;
203 for my $copy (@$copies) {
205 my $copyid = $copy->id;
206 $logger->info("vol-update: inspecting copy $copyid");
208 if( !($vol = $cache{$copy->call_number}) ) {
209 $vol = $cache{$copy->call_number} =
210 $editor->retrieve_asset_call_number($copy->call_number);
211 return $editor->event unless $vol;
214 return $editor->event unless
215 $editor->allowed('UPDATE_COPY', $class->copy_perm_org($vol, $copy));
217 $copy->editor($editor->requestor->id);
218 $copy->edit_date('now');
220 $copy->status( $copy->status->id ) if ref($copy->status);
221 $copy->location( $copy->location->id ) if ref($copy->location);
222 $copy->circ_lib( $copy->circ_lib->id ) if ref($copy->circ_lib);
224 my $sc_entries = $copy->stat_cat_entries;
225 $copy->clear_stat_cat_entries;
227 if( $copy->isdeleted ) {
228 $evt = $class->delete_copy($editor, $override, $vol, $copy, $retarget_holds, $force_delete_empty_bib);
231 } elsif( $copy->isnew ) {
232 $evt = $class->create_copy( $editor, $vol, $copy );
235 } elsif( $copy->ischanged ) {
237 $evt = $class->update_copy( $editor, $override, $vol, $copy, $retarget_holds, $force_delete_empty_bib);
241 $copy->stat_cat_entries( $sc_entries );
242 $evt = $class->update_copy_stat_entries($editor, $copy, $delete_stats);
246 $logger->debug("vol-update: done updating copy batch");
253 my($class, $editor, $override, $vol, $copy, $retarget_holds, $force_delete_empty_bib) = @_;
255 return $editor->event unless
256 $editor->allowed('DELETE_COPY', $class->copy_perm_org($vol, $copy));
258 my $stat = $U->copy_status($copy->status)->id;
261 return OpenILS::Event->new('COPY_DELETE_WARNING', payload => $copy->id )
262 if $stat == OILS_COPY_STATUS_CHECKED_OUT or
263 $stat == OILS_COPY_STATUS_IN_TRANSIT or
264 $stat == OILS_COPY_STATUS_ON_HOLDS_SHELF or
265 $stat == OILS_COPY_STATUS_ILL;
268 $logger->info("vol-update: deleting copy ".$copy->id);
271 $copy->editor($editor->requestor->id);
272 $copy->edit_date('now');
273 $editor->update_asset_copy($copy) or return $editor->event;
275 # Delete any open transits for this copy
276 my $transits = $editor->search_action_transit_copy(
277 { target_copy=>$copy->id, dest_recv_time => undef } );
279 for my $t (@$transits) {
280 $editor->delete_action_transit_copy($t)
281 or return $editor->event;
284 $class->check_hold_retarget($editor, $copy, undef, $retarget_holds);
286 return $class->remove_empty_objects($editor, $override, $vol, $force_delete_empty_bib);
292 my($class, $override, $editor, $vol) = @_;
295 return $evt if ( $evt = $class->org_cannot_have_vols($editor, $vol->owning_lib) );
297 # see if the record this volume references is marked as deleted
298 my $rec = $editor->retrieve_biblio_record_entry($vol->record)
299 or return $editor->die_event;
300 return OpenILS::Event->new('BIB_RECORD_DELETED', rec => $rec->id)
301 if $U->is_true($rec->deleted);
303 # first lets see if there are any collisions
304 my $vols = $editor->search_asset_call_number( {
305 owning_lib => $vol->owning_lib,
306 record => $vol->record,
307 label => $vol->label,
314 # we've found an exising volume
316 $label = $vol->label;
318 return OpenILS::Event->new(
319 'VOLUME_LABEL_EXISTS', payload => $vol->id);
323 # create a temp label so we can create the new volume,
324 # then de-dup it with the existing volume
325 $vol->label( "__SYSTEM_TMP_$$".time) if $label;
327 $vol->creator($editor->requestor->id);
328 $vol->create_date('now');
329 $vol->editor($editor->requestor->id);
330 $vol->edit_date('now');
333 $editor->create_asset_call_number($vol) or return $editor->die_event;
336 # now restore the label and merge into the existing record
339 OpenILS::Application::Cat::Merge::merge_volumes($editor, [$vol], $$vols[0]);
346 # returns the volume if it exists
348 my($class, $e, $rec_id, $label, $owning_lib) = @_;
349 return $e->search_asset_call_number(
350 {label => $label, record => $rec_id, owning_lib => $owning_lib, deleted => 'f'})->[0];
353 sub find_or_create_volume {
354 my($class, $e, $label, $record_id, $org_id) = @_;
358 if($record_id == OILS_PRECAT_RECORD) {
359 $vol = $e->retrieve_asset_call_number(OILS_PRECAT_CALL_NUMBER)
360 or return (undef, $e->die_event);
363 $vol = $class->volume_exists($e, $record_id, $label, $org_id);
366 # If the volume exists, return the ID
367 return ($vol, undef, 1) if $vol;
369 # -----------------------------------------------------------------
370 # Otherwise, create a new volume with the given attributes
371 # -----------------------------------------------------------------
372 return (undef, $e->die_event) unless $e->allowed('UPDATE_VOLUME', $org_id);
374 $vol = Fieldmapper::asset::call_number->new;
375 $vol->owning_lib($org_id);
377 $vol->record($record_id);
379 my $evt = OpenILS::Application::Cat::AssetCommon->create_volume(0, $e, $vol);
380 return (undef, $evt) if $evt;
386 sub create_copy_note {
387 my($class, $e, $copy, $title, $value, $pub) = @_;
388 my $note = Fieldmapper::asset::copy_note->new;
389 $note->owning_copy($copy->id);
390 $note->creator($e->requestor->id);
392 $note->value($value);
393 $note->title($title);
394 $e->create_asset_copy_note($note) or return $e->die_event;
399 sub remove_empty_objects {
400 my($class, $editor, $override, $vol, $force_delete_empty_bib) = @_;
402 my $koe = $U->ou_ancestor_setting_value(
403 $editor->requestor->ws_ou, 'cat.bib.keep_on_empty', $editor);
404 my $aoe = $U->ou_ancestor_setting_value(
405 $editor->requestor->ws_ou, 'cat.bib.alert_on_empty', $editor);
407 if( OpenILS::Application::Cat::BibCommon->title_is_empty($editor, $vol->record, $vol->id) ) {
409 # delete this volume if it's not already marked as deleted
410 unless( $U->is_true($vol->deleted) || $vol->isdeleted ) {
412 $vol->editor($editor->requestor->id);
413 $vol->edit_date('now');
414 $editor->update_asset_call_number($vol) or return $editor->event;
417 return OpenILS::Event->new('TITLE_LAST_COPY', payload => $vol->record )
418 if $aoe and not $override and not $force_delete_empty_bib;
420 unless($koe and not $force_delete_empty_bib) {
421 # delete the bib record if the keep-on-empty setting is not set (and we're not otherwise forcing things, say through acq settings)
422 my $evt = OpenILS::Application::Cat::BibCommon->delete_rec($editor, $vol->record);
432 my($class, $vol, $copy) = @_;
433 my $org = $vol->owning_lib;
434 if( $vol->id == OILS_PRECAT_CALL_NUMBER ) {
435 $org = ref($copy->circ_lib) ? $copy->circ_lib->id : $copy->circ_lib;
437 $logger->debug("using copy perm org $org");
443 my($class, $e, $copy_id) = @_;
445 my $copy = $e->retrieve_asset_copy([
447 {flesh => 1, flesh_fields => {'acp' => ['call_number']}}])
448 or return $e->die_event;
451 ($copy->call_number->id == OILS_PRECAT_CALL_NUMBER) ?
452 $copy->circ_lib : $copy->call_number->owning_lib;
454 my $circ = $e->search_action_circulation(
455 {checkin_time => undef, target_copy => $copy->id} )->[0]
456 or return $e->die_event;
458 $e->allowed('SET_CIRC_LOST', $circ->circ_lib) or return $e->die_event;
460 return $e->die_event(OpenILS::Event->new('COPY_MARKED_LOST'))
461 if $copy->status == OILS_COPY_STATUS_LOST;
463 # ---------------------------------------------------------------------
464 # fetch the related org settings
465 my $proc_fee = $U->ou_ancestor_setting_value(
466 $owning_lib, OILS_SETTING_LOST_PROCESSING_FEE, $e) || 0;
467 my $void_overdue = $U->ou_ancestor_setting_value(
468 $owning_lib, OILS_SETTING_VOID_OVERDUE_ON_LOST, $e) || 0;
470 # ---------------------------------------------------------------------
471 # move the copy into LOST status
472 $copy->status(OILS_COPY_STATUS_LOST);
473 $copy->editor($e->requestor->id);
474 $copy->edit_date('now');
475 $e->update_asset_copy($copy) or return $e->die_event;
477 my $price = $U->get_copy_price($e, $copy, $copy->call_number);
480 my $evt = OpenILS::Application::Circ::CircCommon->create_bill(
481 $e, $price, 3, 'Lost Materials', $circ->id);
485 # ---------------------------------------------------------------------
486 # if there is a processing fee, charge that too
487 if( $proc_fee > 0 ) {
488 my $evt = OpenILS::Application::Circ::CircCommon->create_bill(
489 $e, $proc_fee, 4, 'Lost Materials Processing Fee', $circ->id);
493 # ---------------------------------------------------------------------
494 # mark the circ as lost and stop the fines
495 $circ->stop_fines(OILS_STOP_FINES_LOST);
496 $circ->stop_fines_time('now') unless $circ->stop_fines_time;
497 $e->update_action_circulation($circ) or return $e->die_event;
499 # ---------------------------------------------------------------------
500 # void all overdue fines on this circ if configured
501 if( $void_overdue ) {
502 my $evt = OpenILS::Application::Circ::CircCommon->void_overdues($e, $circ);
506 my $evt = OpenILS::Application::Circ::CircCommon->reopen_xact($e, $circ->id);
509 my $ses = OpenSRF::AppSession->create('open-ils.trigger');
510 $ses->request('open-ils.trigger.event.autocreate', 'lost', $circ, $circ->circ_lib);