]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/Application/Cat/AssetCommon.pm
ff750991fef458d1cc83769edcc4b1945dd5bce6
[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         my $evt;
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) );
201
202         $logger->info("vol-update: updating copy ".$copy->id);
203         my $orig_copy = $editor->retrieve_asset_copy($copy->id);
204
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});
208
209         $copy->editor($editor->requestor->id);
210         $copy->edit_date('now');
211
212         $copy->age_protect( $copy->age_protect->id )
213                 if ref $copy->age_protect;
214
215         $class->fix_copy_price($copy);
216     $class->check_hold_retarget($editor, $copy, $orig_copy, $retarget_holds);
217
218         return $editor->event unless $editor->update_asset_copy($copy);
219         return $class->remove_empty_objects($editor, $override, $orig_vol, $force_delete_empty_bib);
220 }
221
222 sub check_hold_retarget {
223     my($class, $editor, $copy, $orig_copy, $retarget_holds) = @_;
224     return unless $retarget_holds;
225
226     if( !($copy->isdeleted or $U->is_true($copy->deleted)) ) {
227         # see if a status change warrants a retarget
228
229         $orig_copy = $editor->retrieve_asset_copy($copy->id) unless $orig_copy;
230
231         if($orig_copy->status == $copy->status) {
232             # no status change, no retarget
233             return;
234         }
235
236         my $stat = $editor->retrieve_config_copy_status($copy->status);
237
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); 
241     }
242
243     my $hold_ids = $editor->search_action_hold_request(
244         {   current_copy        => $copy->id, 
245             cancel_time         => undef, 
246             fulfillment_time    => undef 
247         }, {idlist => 1}
248     );
249
250     push(@$retarget_holds, @$hold_ids);
251 }
252
253
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) = @_;
257
258         my $evt;
259         my $fetchvol = ($vol) ? 0 : 1;
260
261         my %cache;
262         $cache{$vol->id} = $vol if $vol;
263
264         for my $copy (@$copies) {
265
266                 my $copyid = $copy->id;
267                 $logger->info("vol-update: inspecting copy $copyid");
268
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;
273                 }
274
275                 return $editor->event unless 
276                         $editor->allowed('UPDATE_COPY', $class->copy_perm_org($vol, $copy));
277
278                 $copy->editor($editor->requestor->id);
279                 $copy->edit_date('now');
280
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);
284                 
285                 my $sc_entries = $copy->stat_cat_entries;
286                 $copy->clear_stat_cat_entries;
287
288         my $parts = $copy->parts;
289                 $copy->clear_parts;
290
291                 if( $copy->isdeleted ) {
292                         $evt = $class->delete_copy($editor, $override, $vol, $copy, $retarget_holds, $force_delete_empty_bib);
293                         return $evt if $evt;
294
295                 } elsif( $copy->isnew ) {
296                         $evt = $class->create_copy( $editor, $vol, $copy );
297                         return $evt if $evt;
298
299                 } elsif( $copy->ischanged ) {
300
301                         $evt = $class->update_copy( $editor, $override, $vol, $copy, $retarget_holds, $force_delete_empty_bib);
302                         return $evt if $evt;
303                 }
304
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);
310                 return $evt if $evt;
311         }
312
313         $logger->debug("vol-update: done updating copy batch");
314
315         return undef;
316 }
317
318
319 sub delete_copy {
320         my($class, $editor, $override, $vol, $copy, $retarget_holds, $force_delete_empty_bib, $skip_empty_cleanup) = @_;
321
322         return $editor->event unless
323                 $editor->allowed('DELETE_COPY', $class->copy_perm_org($vol, $copy));
324
325         my $stat = $U->copy_status($copy->status);
326         if ($U->is_true($stat->restrict_copy_delete)) {
327                 if ($override) {
328                         return $editor->event unless $editor->allowed('COPY_DELETE_WARNING.override', $class->copy_perm_org($vol, $copy))
329                 } else {
330                         return OpenILS::Event->new('COPY_DELETE_WARNING', payload => $copy->id )
331                 }
332         }
333
334         $logger->info("vol-update: deleting copy ".$copy->id);
335         $copy->deleted('t');
336
337         $copy->editor($editor->requestor->id);
338         $copy->edit_date('now');
339         $editor->update_asset_copy($copy) or return $editor->event;
340
341         # Delete any open transits for this copy
342         my $transits = $editor->search_action_transit_copy(
343                 { target_copy=>$copy->id, dest_recv_time => undef } );
344
345         for my $t (@$transits) {
346                 $editor->delete_action_transit_copy($t)
347                         or return $editor->event;
348         }
349
350     my $evt = $class->cancel_copy_holds($editor, $copy);
351     return $evt if $evt;
352
353     $class->check_hold_retarget($editor, $copy, undef, $retarget_holds);
354
355     return undef if $skip_empty_cleanup;
356
357         return $class->remove_empty_objects($editor, $override, $vol, $force_delete_empty_bib);
358 }
359
360
361 # deletes all holds that specifically target the deleted copy
362 sub cancel_copy_holds {
363     my($class, $editor, $copy) = @_;
364
365     my $holds = $editor->search_action_hold_request({   
366         target              => $copy->id, 
367         hold_type           => [qw/C R F/],
368         cancel_time         => undef, 
369         fulfillment_time    => undef 
370     });
371
372     return $class->cancel_hold_list($editor, $holds);
373 }
374
375 # deletes all holds that specifically target the deleted volume
376 sub cancel_volume_holds {
377     my($class, $editor, $volume) = @_;
378
379     my $holds = $editor->search_action_hold_request({   
380         target              => $volume->id, 
381         hold_type           => 'V',
382         cancel_time         => undef, 
383         fulfillment_time    => undef 
384     });
385
386     return $class->cancel_hold_list($editor, $holds);
387 }
388
389 sub cancel_hold_list {
390     my($class, $editor, $holds) = @_;
391
392     for my $hold (@$holds) {
393
394         $hold->cancel_time('now');
395         $hold->cancel_cause(1); # un-targeted expiration.  Do we need an alternate "target deleted" cause?
396         $editor->update_action_hold_request($hold) or return $editor->die_event;
397
398         # delete the copy maps.  
399         my $maps = $editor->search_action_hold_copy_map({hold => $hold->id});
400         for(@$maps) {
401             $editor->delete_action_hold_copy_map($_) 
402                 or return $editor->die_event;
403         }
404
405         # tell A/T the hold was cancelled.  Don't wait for a response..
406         my $at_ses = OpenSRF::AppSession->create('open-ils.trigger');
407         $at_ses->request(
408             'open-ils.trigger.event.autocreate',
409             'hold_request.cancel.expire_no_target', 
410             $hold, $hold->pickup_lib);
411     }
412
413     return undef;
414 }
415
416
417
418 sub create_volume {
419         my($class, $override, $editor, $vol) = @_;
420         my $evt;
421
422         return $evt if ( $evt = $class->org_cannot_have_vols($editor, $vol->owning_lib) );
423
424    # see if the record this volume references is marked as deleted
425    my $rec = $editor->retrieve_biblio_record_entry($vol->record)
426       or return $editor->die_event;
427    return OpenILS::Event->new('BIB_RECORD_DELETED', rec => $rec->id) 
428       if $U->is_true($rec->deleted);
429
430         # first lets see if there are any collisions
431         my $vols = $editor->search_asset_call_number( { 
432                         owning_lib      => $vol->owning_lib,
433                         record          => $vol->record,
434                         label                   => $vol->label,
435                         prefix                  => $vol->prefix,
436                         suffix                  => $vol->suffix,
437                         deleted         => 'f'
438                 }
439         );
440
441         my $label = undef;
442         if(@$vols) {
443       # we've found an exising volume
444                 if($override) { 
445                         $label = $vol->label;
446                 } else {
447                         return OpenILS::Event->new(
448                                 'VOLUME_LABEL_EXISTS', payload => $vol->id);
449                 }
450         }
451
452         # create a temp label so we can create the new volume, 
453     # then de-dup it with the existing volume
454         $vol->label( "__SYSTEM_TMP_$$".time) if $label;
455
456         $vol->creator($editor->requestor->id);
457         $vol->create_date('now');
458         $vol->editor($editor->requestor->id);
459         $vol->edit_date('now');
460         $vol->clear_id;
461
462         $editor->create_asset_call_number($vol) or return $editor->die_event;
463
464         if($label) {
465                 # now restore the label and merge into the existing record
466                 $vol->label($label);
467                 (undef, $evt) = 
468                         OpenILS::Application::Cat::Merge::merge_volumes($editor, [$vol], $$vols[0]);
469                 return $evt if $evt;
470         }
471
472         return undef;
473 }
474
475 # returns the volume if it exists
476 sub volume_exists {
477     my($class, $e, $rec_id, $label, $owning_lib, $prefix, $suffix) = @_;
478     return $e->search_asset_call_number(
479         {label => $label, record => $rec_id, owning_lib => $owning_lib, deleted => 'f', prefix => $prefix, suffix => $suffix})->[0];
480 }
481
482 sub find_or_create_volume {
483         my($class, $e, $label, $record_id, $org_id, $prefix, $suffix, $label_class) = @_;
484
485     $prefix ||= '-1';
486     $suffix ||= '-1';
487
488     my $vol;
489
490     if($record_id == OILS_PRECAT_RECORD) {
491         $vol = $e->retrieve_asset_call_number(OILS_PRECAT_CALL_NUMBER)
492             or return (undef, $e->die_event);
493
494     } else {
495         $vol = $class->volume_exists($e, $record_id, $label, $org_id, $prefix, $suffix);
496     }
497
498         # If the volume exists, return the ID
499     return ($vol, undef, 1) if $vol;
500
501         # -----------------------------------------------------------------
502         # Otherwise, create a new volume with the given attributes
503         # -----------------------------------------------------------------
504         return (undef, $e->die_event) unless $e->allowed('UPDATE_VOLUME', $org_id);
505
506         $vol = Fieldmapper::asset::call_number->new;
507         $vol->owning_lib($org_id);
508         $vol->label_class($label_class) if ($label_class);
509         $vol->label($label);
510         $vol->prefix($prefix);
511         $vol->suffix($suffix);
512         $vol->record($record_id);
513
514     my $evt = $class->create_volume(0, $e, $vol);
515     return (undef, $evt) if $evt;
516
517         return ($vol);
518 }
519
520
521 sub create_copy_note {
522     my($class, $e, $copy, $title, $value, $pub) = @_;
523     my $note = Fieldmapper::asset::copy_note->new;
524     $note->owning_copy($copy->id);
525     $note->creator($e->requestor->id);
526     $note->pub($pub ? 't' : 'f');
527     $note->value($value);
528     $note->title($title);
529     $e->create_asset_copy_note($note) or return $e->die_event;
530     return undef;
531 }
532
533
534 sub remove_empty_objects {
535         my($class, $editor, $override, $vol, $force_delete_empty_bib) = @_; 
536
537     my $koe = $U->ou_ancestor_setting_value(
538         $editor->requestor->ws_ou, 'cat.bib.keep_on_empty', $editor);
539     my $aoe =  $U->ou_ancestor_setting_value(
540         $editor->requestor->ws_ou, 'cat.bib.alert_on_empty', $editor);
541
542         if( OpenILS::Application::Cat::BibCommon->title_is_empty($editor, $vol->record, $vol->id) ) {
543
544         # delete this volume if it's not already marked as deleted
545         unless( $U->is_true($vol->deleted) || $vol->isdeleted ) {
546             my $evt = $class->delete_volume($editor, $vol, $override, 0, 1);
547             return $evt if $evt;
548         }
549
550         return OpenILS::Event->new('TITLE_LAST_COPY', payload => $vol->record ) 
551             if $aoe and not $override and not $force_delete_empty_bib;
552
553         unless($koe and not $force_delete_empty_bib) {
554             # delete the bib record if the keep-on-empty setting is not set (and we're not otherwise forcing things, say through acq settings)
555             my $evt = OpenILS::Application::Cat::BibCommon->delete_rec($editor, $vol->record);
556             return $evt if $evt;
557         }
558
559         } else {
560
561         # this may be the last copy attached to the volume.  
562
563         if($U->ou_ancestor_setting_value(
564                 $editor->requestor->ws_ou, 'cat.volume.delete_on_empty', $editor)) {
565
566             # if this volume is "empty" and not mid-delete, delete it.
567             unless($U->is_true($vol->deleted) || $vol->isdeleted) {
568
569                 my $copies = $editor->search_asset_copy(
570                     [{call_number => $vol->id, deleted => 'f'}, {limit => 1}], {idlist => 1});
571
572                 if(!@$copies) {
573                     my $evt = $class->delete_volume($editor, $vol, $override, 0, 1);
574                     return $evt if $evt;
575                 }
576             }
577         }
578     }
579
580         return undef;
581 }
582
583 # Deletes a volume.  Returns undef on success, event on error
584 # force : deletes all attached copies
585 # skip_copy_check : assumes caller has verified no copies need deleting first
586 sub delete_volume {
587     my($class, $editor, $vol, $override, $delete_copies, $skip_copy_checks) = @_;
588     my $evt;
589
590     unless($skip_copy_checks) {
591         my $cs = $editor->search_asset_copy(
592             [{call_number => $vol->id, deleted => 'f'}, {limit => 1}], {idlist => 1});
593
594         return OpenILS::Event->new('VOLUME_NOT_EMPTY', payload => $vol->id) 
595             if @$cs and !$delete_copies;
596
597         my $copies = $editor->search_asset_copy({call_number => $vol->id, deleted => 'f'});
598
599         for my $copy (@$copies) {
600             $evt = $class->delete_copy($editor, $override, $vol, $copy, 0, 0, 1);
601             return $evt if $evt;
602         }
603     }
604
605     $vol->deleted('t');
606     $vol->edit_date('now');
607     $vol->editor($editor->requestor->id);
608     $editor->update_asset_call_number($vol) or return $editor->die_event;
609
610     $evt = $class->cancel_volume_holds($editor, $vol);
611     return $evt if $evt;
612
613     # handle the case where this is the last volume on the record
614         return $class->remove_empty_objects($editor, $override, $vol);
615 }
616
617
618 sub copy_perm_org {
619         my($class, $vol, $copy) = @_;
620         my $org = $vol->owning_lib;
621         if( $vol->id == OILS_PRECAT_CALL_NUMBER ) {
622                 $org = ref($copy->circ_lib) ? $copy->circ_lib->id : $copy->circ_lib;
623         }
624         $logger->debug("using copy perm org $org");
625         return $org;
626 }
627
628
629 sub set_item_lost {
630     my($class, $e, $copy_id) = @_;
631
632     my $copy = $e->retrieve_asset_copy([
633         $copy_id, 
634         {flesh => 1, flesh_fields => {'acp' => ['call_number']}}])
635             or return $e->die_event;
636
637     my $owning_lib = 
638         ($copy->call_number->id == OILS_PRECAT_CALL_NUMBER) ? 
639             $copy->circ_lib : $copy->call_number->owning_lib;
640
641     my $circ = $e->search_action_circulation(
642         {checkin_time => undef, target_copy => $copy->id} )->[0]
643             or return $e->die_event;
644
645     $e->allowed('SET_CIRC_LOST', $circ->circ_lib) or return $e->die_event;
646
647     return $e->die_event(OpenILS::Event->new('COPY_MARKED_LOST'))
648             if $copy->status == OILS_COPY_STATUS_LOST;
649
650     # ---------------------------------------------------------------------
651     # fetch the related org settings
652     my $proc_fee = $U->ou_ancestor_setting_value(
653         $owning_lib, OILS_SETTING_LOST_PROCESSING_FEE, $e) || 0;
654     my $void_overdue = $U->ou_ancestor_setting_value(
655         $owning_lib, OILS_SETTING_VOID_OVERDUE_ON_LOST, $e) || 0;
656
657     # ---------------------------------------------------------------------
658     # move the copy into LOST status
659     $copy->status(OILS_COPY_STATUS_LOST);
660     $copy->editor($e->requestor->id);
661     $copy->edit_date('now');
662     $e->update_asset_copy($copy) or return $e->die_event;
663
664     my $price = $U->get_copy_price($e, $copy, $copy->call_number);
665
666     if( $price > 0 ) {
667         my $evt = OpenILS::Application::Circ::CircCommon->create_bill(
668             $e, $price, 3, 'Lost Materials', $circ->id);
669         return $evt if $evt;
670     }
671
672     # ---------------------------------------------------------------------
673     # if there is a processing fee, charge that too
674     if( $proc_fee > 0 ) {
675         my $evt = OpenILS::Application::Circ::CircCommon->create_bill(
676             $e, $proc_fee, 4, 'Lost Materials Processing Fee', $circ->id);
677         return $evt if $evt;
678     }
679
680     # ---------------------------------------------------------------------
681     # mark the circ as lost and stop the fines
682     $circ->stop_fines(OILS_STOP_FINES_LOST);
683     $circ->stop_fines_time('now') unless $circ->stop_fines_time;
684     $e->update_action_circulation($circ) or return $e->die_event;
685
686     # ---------------------------------------------------------------------
687     # void all overdue fines on this circ if configured
688     if( $void_overdue ) {
689         my $evt = OpenILS::Application::Circ::CircCommon->void_overdues($e, $circ);
690         return $evt if $evt;
691     }
692
693     my $evt = OpenILS::Application::Circ::CircCommon->reopen_xact($e, $circ->id);
694     return $evt if $evt;
695
696     my $ses = OpenSRF::AppSession->create('open-ils.trigger');
697     $ses->request('open-ils.trigger.event.autocreate', 'lost', $circ, $circ->circ_lib);
698
699     my $evt2 = OpenILS::Utils::Penalty->calculate_penalties($e, $circ->usr, $U->xact_org($circ->id,$e));
700     return $evt2 if $evt2;
701
702     return undef;
703 }