]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/Application/Cat/AssetCommon.pm
Allow more granular overrides
[working/Evergreen.git] / Open-ILS / src / perlmods / lib / OpenILS / Application / Cat / AssetCommon.pm
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;
11 use OpenILS::Event;
12 use OpenILS::Utils::Penalty;
13 use OpenILS::Application::Circ::CircCommon;
14 my $U = 'OpenILS::Application::AppUtils';
15
16
17 # ---------------------------------------------------------------------------
18 # Shared copy mangling code.  Do not publish methods from here.
19 # ---------------------------------------------------------------------------
20
21 sub org_cannot_have_vols {
22     my($class, $e, $org_id) = @_;
23         my $org = $e->retrieve_actor_org_unit([
24         $org_id,
25         {   flesh => 1,
26             flesh_fields => {aou => ['ou_type']}
27         }]) or return $e->event;
28
29         return OpenILS::Event->new('ORG_CANNOT_HAVE_VOLS')
30                 unless $U->is_true($org->ou_type->can_have_vols);
31
32         return 0;
33 }
34
35 sub fix_copy_price {
36     my $class = shift;
37         my $copy = shift;
38
39     if(defined $copy->price) {
40             my $p = $copy->price || 0;
41             $p =~ s/\$//og;
42             $copy->price($p);
43     }
44
45         my $d = $copy->deposit_amount || 0;
46         $d =~ s/\$//og;
47         $copy->deposit_amount($d);
48 }
49
50 sub create_copy {
51         my($class, $editor, $vol, $copy) = @_;
52
53         my $existing = $editor->search_asset_copy(
54                 { barcode => $copy->barcode, deleted => 'f' } );
55         
56         return OpenILS::Event->new('ITEM_BARCODE_EXISTS') if @$existing;
57
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);
61
62         my $evt;
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));
65
66         $copy->clear_id;
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);
72
73         $editor->create_asset_copy($copy) or return $editor->die_event;
74         return undef;
75 }
76
77
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) = @_;
83
84         return undef if $copy->isdeleted;
85         return undef unless $copy->ischanged or $copy->isnew;
86
87         my $evt;
88         my $entries = $copy->stat_cat_entries;
89
90         if( $delete_stats ) {
91                 $entries = ($entries and @$entries) ? $entries : [];
92         } else {
93                 return undef unless ($entries and @$entries);
94         }
95
96         my $maps = $editor->search_asset_stat_cat_entry_copy_map({owning_copy=>$copy->id});
97
98         if(!$copy->isnew) {
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 ) {
103
104                                 $logger->info("copy update found stale ".
105                                         "stat cat entry map ".$map->id. " on copy ".$copy->id);
106
107                                 $editor->delete_asset_stat_cat_entry_copy_map($map)
108                                         or return $editor->event;
109                         }
110                 }
111         }
112
113         # go through the stat cat update/create process
114         for my $entry (@$entries) { 
115                 next unless $entry;
116
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 );
119         
120                 my $new_map = Fieldmapper::asset::stat_cat_entry_copy_map->new();
121
122                 my $sc = ref($entry->stat_cat) ? $entry->stat_cat->id : $entry->stat_cat;
123                 
124                 $new_map->stat_cat( $sc );
125                 $new_map->stat_cat_entry( $entry->id );
126                 $new_map->owning_copy( $copy->id );
127
128                 $editor->create_asset_stat_cat_entry_copy_map($new_map)
129                         or return $editor->event;
130
131                 $logger->info("copy update created new stat cat entry map ".$editor->data);
132         }
133
134         return undef;
135 }
136
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) = @_;
142
143         return undef if $copy->isdeleted;
144         return undef unless $copy->ischanged or $copy->isnew;
145
146         my $evt;
147         my $incoming_parts = $copy->parts;
148
149         if( $delete_maps ) {
150                 $incoming_parts = ($incoming_parts and @$incoming_parts) ? $incoming_parts : [];
151         } else {
152                 return undef unless ($incoming_parts and @$incoming_parts);
153         }
154
155         my $maps = $editor->search_asset_copy_part_map({target_copy=>$copy->id});
156
157         if(!$copy->isnew) {
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 ) {
162
163                                 $logger->info("copy update found stale ".
164                                         "monographic part map ".$map->id. " on copy ".$copy->id);
165
166                                 $editor->delete_asset_copy_part_map($map)
167                                         or return $editor->event;
168                         }
169                 }
170         }
171
172         # go through the part map update/create process
173         for my $incoming_part (@$incoming_parts) { 
174                 next unless $incoming_part;
175
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 );
178         
179                 my $new_map = Fieldmapper::asset::copy_part_map->new();
180
181                 $new_map->part( $incoming_part->id );
182                 $new_map->target_copy( $copy->id );
183
184                 $editor->create_asset_copy_part_map($new_map)
185                         or return $editor->event;
186
187                 $logger->info("copy update created new monographic part copy map ".$editor->data);
188         }
189
190         return undef;
191 }
192
193
194
195 sub update_copy {
196         my($class, $editor, $override, $vol, $copy, $retarget_holds, $force_delete_empty_bib) = @_;
197
198     $override = { all => 1 } if($override && !ref $override);
199     $override = { all => 0 } if(!ref $override);
200
201         my $evt;
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) );
204
205         $logger->info("vol-update: updating copy ".$copy->id);
206         my $orig_copy = $editor->retrieve_asset_copy($copy->id);
207
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});
211
212         $copy->editor($editor->requestor->id);
213         $copy->edit_date('now');
214
215         $copy->age_protect( $copy->age_protect->id )
216                 if ref $copy->age_protect;
217
218         $class->fix_copy_price($copy);
219     $class->check_hold_retarget($editor, $copy, $orig_copy, $retarget_holds);
220
221         return $editor->event unless $editor->update_asset_copy($copy);
222         return $class->remove_empty_objects($editor, $override, $orig_vol, $force_delete_empty_bib);
223 }
224
225 sub check_hold_retarget {
226     my($class, $editor, $copy, $orig_copy, $retarget_holds) = @_;
227     return unless $retarget_holds;
228
229     if( !($copy->isdeleted or $U->is_true($copy->deleted)) ) {
230         # see if a status change warrants a retarget
231
232         $orig_copy = $editor->retrieve_asset_copy($copy->id) unless $orig_copy;
233
234         if($orig_copy->status == $copy->status) {
235             # no status change, no retarget
236             return;
237         }
238
239         my $stat = $editor->retrieve_config_copy_status($copy->status);
240
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); 
244     }
245
246     my $hold_ids = $editor->search_action_hold_request(
247         {   current_copy        => $copy->id, 
248             cancel_time         => undef, 
249             fulfillment_time    => undef 
250         }, {idlist => 1}
251     );
252
253     push(@$retarget_holds, @$hold_ids);
254 }
255
256
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) = @_;
260
261     $override = { all => 1 } if($override && !ref $override);
262     $override = { all => 0 } if(!ref $override);
263
264         my $evt;
265         my $fetchvol = ($vol) ? 0 : 1;
266
267         my %cache;
268         $cache{$vol->id} = $vol if $vol;
269
270         for my $copy (@$copies) {
271
272                 my $copyid = $copy->id;
273                 $logger->info("vol-update: inspecting copy $copyid");
274
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;
279                 }
280
281                 return $editor->event unless 
282                         $editor->allowed('UPDATE_COPY', $class->copy_perm_org($vol, $copy));
283
284                 $copy->editor($editor->requestor->id);
285                 $copy->edit_date('now');
286
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);
290                 
291                 my $sc_entries = $copy->stat_cat_entries;
292                 $copy->clear_stat_cat_entries;
293
294         my $parts = $copy->parts;
295                 $copy->clear_parts;
296
297                 if( $copy->isdeleted ) {
298                         $evt = $class->delete_copy($editor, $override, $vol, $copy, $retarget_holds, $force_delete_empty_bib);
299                         return $evt if $evt;
300
301                 } elsif( $copy->isnew ) {
302                         $evt = $class->create_copy( $editor, $vol, $copy );
303                         return $evt if $evt;
304
305                 } elsif( $copy->ischanged ) {
306
307                         $evt = $class->update_copy( $editor, $override, $vol, $copy, $retarget_holds, $force_delete_empty_bib);
308                         return $evt if $evt;
309                 }
310
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);
316                 return $evt if $evt;
317         }
318
319         $logger->debug("vol-update: done updating copy batch");
320
321         return undef;
322 }
323
324
325 sub delete_copy {
326         my($class, $editor, $override, $vol, $copy, $retarget_holds, $force_delete_empty_bib, $skip_empty_cleanup) = @_;
327
328         return $editor->event unless
329                 $editor->allowed('DELETE_COPY', $class->copy_perm_org($vol, $copy));
330
331     $override = { all => 1 } if($override && !ref $override);
332     $override = { all => 0 } if(!ref $override);
333
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))
338                 } else {
339                         return OpenILS::Event->new('COPY_DELETE_WARNING', payload => $copy->id )
340                 }
341         }
342
343         $logger->info("vol-update: deleting copy ".$copy->id);
344         $copy->deleted('t');
345
346         $copy->editor($editor->requestor->id);
347         $copy->edit_date('now');
348         $editor->update_asset_copy($copy) or return $editor->event;
349
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 } );
353
354         for my $t (@$transits) {
355                 $editor->delete_action_transit_copy($t)
356                         or return $editor->event;
357         }
358
359     my $evt = $class->cancel_copy_holds($editor, $copy);
360     return $evt if $evt;
361
362     $class->check_hold_retarget($editor, $copy, undef, $retarget_holds);
363
364     return undef if $skip_empty_cleanup;
365
366         return $class->remove_empty_objects($editor, $override, $vol, $force_delete_empty_bib);
367 }
368
369
370 # deletes all holds that specifically target the deleted copy
371 sub cancel_copy_holds {
372     my($class, $editor, $copy) = @_;
373
374     my $holds = $editor->search_action_hold_request({   
375         target              => $copy->id, 
376         hold_type           => [qw/C R F/],
377         cancel_time         => undef, 
378         fulfillment_time    => undef 
379     });
380
381     return $class->cancel_hold_list($editor, $holds);
382 }
383
384 # deletes all holds that specifically target the deleted volume
385 sub cancel_volume_holds {
386     my($class, $editor, $volume) = @_;
387
388     my $holds = $editor->search_action_hold_request({   
389         target              => $volume->id, 
390         hold_type           => 'V',
391         cancel_time         => undef, 
392         fulfillment_time    => undef 
393     });
394
395     return $class->cancel_hold_list($editor, $holds);
396 }
397
398 sub cancel_hold_list {
399     my($class, $editor, $holds) = @_;
400
401     for my $hold (@$holds) {
402
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;
406
407         # delete the copy maps.  
408         my $maps = $editor->search_action_hold_copy_map({hold => $hold->id});
409         for(@$maps) {
410             $editor->delete_action_hold_copy_map($_) 
411                 or return $editor->die_event;
412         }
413
414         # tell A/T the hold was cancelled.  Don't wait for a response..
415         my $at_ses = OpenSRF::AppSession->create('open-ils.trigger');
416         $at_ses->request(
417             'open-ils.trigger.event.autocreate',
418             'hold_request.cancel.expire_no_target', 
419             $hold, $hold->pickup_lib);
420     }
421
422     return undef;
423 }
424
425
426
427 sub create_volume {
428         my($class, $override, $editor, $vol) = @_;
429         my $evt;
430
431         return $evt if ( $evt = $class->org_cannot_have_vols($editor, $vol->owning_lib) );
432
433     $override = { all => 1 } if($override && !ref $override);
434     $override = { all => 0 } if(!ref $override);
435
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);
441
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,
449                         deleted         => 'f'
450                 }
451         );
452
453         my $label = undef;
454         if(@$vols) {
455       # we've found an exising volume
456                 if($override->{all} || grep { $_ eq 'VOLUME_LABEL_EXISTS' } @{$override->{events}}) {
457                         $label = $vol->label;
458                 } else {
459                         return OpenILS::Event->new(
460                                 'VOLUME_LABEL_EXISTS', payload => $vol->id);
461                 }
462         }
463
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;
467
468         $vol->creator($editor->requestor->id);
469         $vol->create_date('now');
470         $vol->editor($editor->requestor->id);
471         $vol->edit_date('now');
472         $vol->clear_id;
473
474         $editor->create_asset_call_number($vol) or return $editor->die_event;
475
476         if($label) {
477                 # now restore the label and merge into the existing record
478                 $vol->label($label);
479                 (undef, $evt) = 
480                         OpenILS::Application::Cat::Merge::merge_volumes($editor, [$vol], $$vols[0]);
481                 return $evt if $evt;
482         }
483
484         return undef;
485 }
486
487 # returns the volume if it exists
488 sub volume_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];
492 }
493
494 sub find_or_create_volume {
495         my($class, $e, $label, $record_id, $org_id, $prefix, $suffix, $label_class) = @_;
496
497     $prefix ||= '-1';
498     $suffix ||= '-1';
499
500     my $vol;
501
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);
505
506     } else {
507         $vol = $class->volume_exists($e, $record_id, $label, $org_id, $prefix, $suffix);
508     }
509
510         # If the volume exists, return the ID
511     return ($vol, undef, 1) if $vol;
512
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);
517
518         $vol = Fieldmapper::asset::call_number->new;
519         $vol->owning_lib($org_id);
520         $vol->label_class($label_class) if ($label_class);
521         $vol->label($label);
522         $vol->prefix($prefix);
523         $vol->suffix($suffix);
524         $vol->record($record_id);
525
526     my $evt = $class->create_volume(0, $e, $vol);
527     return (undef, $evt) if $evt;
528
529         return ($vol);
530 }
531
532
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;
542     return undef;
543 }
544
545
546 sub remove_empty_objects {
547         my($class, $editor, $override, $vol, $force_delete_empty_bib) = @_; 
548
549     $override = { all => 1 } if($override && !ref $override);
550     $override = { all => 0 } if(!ref $override);
551
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);
556
557         if( OpenILS::Application::Cat::BibCommon->title_is_empty($editor, $vol->record, $vol->id) ) {
558
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);
562             return $evt if $evt;
563         }
564
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;
567
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);
571             return $evt if $evt;
572         }
573
574         } else {
575
576         # this may be the last copy attached to the volume.  
577
578         if($U->ou_ancestor_setting_value(
579                 $editor->requestor->ws_ou, 'cat.volume.delete_on_empty', $editor)) {
580
581             # if this volume is "empty" and not mid-delete, delete it.
582             unless($U->is_true($vol->deleted) || $vol->isdeleted) {
583
584                 my $copies = $editor->search_asset_copy(
585                     [{call_number => $vol->id, deleted => 'f'}, {limit => 1}], {idlist => 1});
586
587                 if(!@$copies) {
588                     my $evt = $class->delete_volume($editor, $vol, $override, 0, 1);
589                     return $evt if $evt;
590                 }
591             }
592         }
593     }
594
595         return undef;
596 }
597
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
601 sub delete_volume {
602     my($class, $editor, $vol, $override, $delete_copies, $skip_copy_checks) = @_;
603     my $evt;
604
605     unless($skip_copy_checks) {
606         my $cs = $editor->search_asset_copy(
607             [{call_number => $vol->id, deleted => 'f'}, {limit => 1}], {idlist => 1});
608
609         return OpenILS::Event->new('VOLUME_NOT_EMPTY', payload => $vol->id) 
610             if @$cs and !$delete_copies;
611
612         my $copies = $editor->search_asset_copy({call_number => $vol->id, deleted => 'f'});
613
614         for my $copy (@$copies) {
615             $evt = $class->delete_copy($editor, $override, $vol, $copy, 0, 0, 1);
616             return $evt if $evt;
617         }
618     }
619
620     $vol->deleted('t');
621     $vol->edit_date('now');
622     $vol->editor($editor->requestor->id);
623     $editor->update_asset_call_number($vol) or return $editor->die_event;
624
625     $evt = $class->cancel_volume_holds($editor, $vol);
626     return $evt if $evt;
627
628     # handle the case where this is the last volume on the record
629         return $class->remove_empty_objects($editor, $override, $vol);
630 }
631
632
633 sub copy_perm_org {
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;
638         }
639         $logger->debug("using copy perm org $org");
640         return $org;
641 }
642
643
644 sub set_item_lost {
645     my($class, $e, $copy_id) = @_;
646
647     my $copy = $e->retrieve_asset_copy([
648         $copy_id, 
649         {flesh => 1, flesh_fields => {'acp' => ['call_number']}}])
650             or return $e->die_event;
651
652     my $owning_lib = 
653         ($copy->call_number->id == OILS_PRECAT_CALL_NUMBER) ? 
654             $copy->circ_lib : $copy->call_number->owning_lib;
655
656     my $circ = $e->search_action_circulation(
657         {checkin_time => undef, target_copy => $copy->id} )->[0]
658             or return $e->die_event;
659
660     $e->allowed('SET_CIRC_LOST', $circ->circ_lib) or return $e->die_event;
661
662     return $e->die_event(OpenILS::Event->new('COPY_MARKED_LOST'))
663             if $copy->status == OILS_COPY_STATUS_LOST;
664
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;
671
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;
678
679     my $price = $U->get_copy_price($e, $copy, $copy->call_number);
680
681     if( $price > 0 ) {
682         my $evt = OpenILS::Application::Circ::CircCommon->create_bill(
683             $e, $price, 3, 'Lost Materials', $circ->id);
684         return $evt if $evt;
685     }
686
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);
692         return $evt if $evt;
693     }
694
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;
700
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);
705         return $evt if $evt;
706     }
707
708     my $evt = OpenILS::Application::Circ::CircCommon->reopen_xact($e, $circ->id);
709     return $evt if $evt;
710
711     my $ses = OpenSRF::AppSession->create('open-ils.trigger');
712     $ses->request('open-ils.trigger.event.autocreate', 'lost', $circ, $circ->circ_lib);
713
714     my $evt2 = OpenILS::Utils::Penalty->calculate_penalties($e, $circ->usr, $U->xact_org($circ->id,$e));
715     return $evt2 if $evt2;
716
717     return undef;
718 }