]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/Application/Cat/AssetCommon.pm
LP#1386347 Remove more unneeded map deleters
[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         # tell A/T the hold was cancelled.  Don't wait for a response..
408         my $at_ses = OpenSRF::AppSession->create('open-ils.trigger');
409         $at_ses->request(
410             'open-ils.trigger.event.autocreate',
411             'hold_request.cancel.expire_no_target', 
412             $hold, $hold->pickup_lib);
413     }
414
415     return undef;
416 }
417
418
419
420 sub create_volume {
421     my($class, $override, $editor, $vol) = @_;
422     my $evt;
423
424     return $evt if ( $evt = $class->org_cannot_have_vols($editor, $vol->owning_lib) );
425
426     $override = { all => 1 } if($override && !ref $override);
427     $override = { all => 0 } if(!ref $override);
428
429    # see if the record this volume references is marked as deleted
430    my $rec = $editor->retrieve_biblio_record_entry($vol->record)
431       or return $editor->die_event;
432    return OpenILS::Event->new('BIB_RECORD_DELETED', rec => $rec->id) 
433       if $U->is_true($rec->deleted);
434
435     # first lets see if there are any collisions
436     my $vols = $editor->search_asset_call_number( { 
437             owning_lib  => $vol->owning_lib,
438             record      => $vol->record,
439             label           => $vol->label,
440             prefix          => $vol->prefix,
441             suffix          => $vol->suffix,
442             deleted     => 'f'
443         }
444     );
445
446     my $label = undef;
447     if(@$vols) {
448       # we've found an exising volume
449         if($override->{all} || grep { $_ eq 'VOLUME_LABEL_EXISTS' } @{$override->{events}}) {
450             $label = $vol->label;
451         } else {
452             return OpenILS::Event->new(
453                 'VOLUME_LABEL_EXISTS', payload => $vol->id);
454         }
455     }
456
457     # create a temp label so we can create the new volume, 
458     # then de-dup it with the existing volume
459     $vol->label( "__SYSTEM_TMP_$$".time) if $label;
460
461     $vol->creator($editor->requestor->id);
462     $vol->create_date('now');
463     $vol->editor($editor->requestor->id);
464     $vol->edit_date('now');
465     $vol->clear_id;
466
467     $editor->create_asset_call_number($vol) or return $editor->die_event;
468
469     if($label) {
470         # now restore the label and merge into the existing record
471         $vol->label($label);
472         (undef, $evt) = 
473             OpenILS::Application::Cat::Merge::merge_volumes($editor, [$vol], $$vols[0]);
474         return $evt if $evt;
475     }
476
477     return undef;
478 }
479
480 # returns the volume if it exists
481 sub volume_exists {
482     my($class, $e, $rec_id, $label, $owning_lib, $prefix, $suffix) = @_;
483     return $e->search_asset_call_number(
484         {label => $label, record => $rec_id, owning_lib => $owning_lib, deleted => 'f', prefix => $prefix, suffix => $suffix})->[0];
485 }
486
487 sub find_or_create_volume {
488     my($class, $e, $label, $record_id, $org_id, $prefix, $suffix, $label_class) = @_;
489
490     $prefix ||= '-1';
491     $suffix ||= '-1';
492
493     my $vol;
494
495     if($record_id == OILS_PRECAT_RECORD) {
496         $vol = $e->retrieve_asset_call_number(OILS_PRECAT_CALL_NUMBER)
497             or return (undef, $e->die_event);
498
499     } else {
500         $vol = $class->volume_exists($e, $record_id, $label, $org_id, $prefix, $suffix);
501     }
502
503     # If the volume exists, return the ID
504     return ($vol, undef, 1) if $vol;
505
506     # -----------------------------------------------------------------
507     # Otherwise, create a new volume with the given attributes
508     # -----------------------------------------------------------------
509     return (undef, $e->die_event) unless $e->allowed('UPDATE_VOLUME', $org_id);
510
511     $vol = Fieldmapper::asset::call_number->new;
512     $vol->owning_lib($org_id);
513     $vol->label_class($label_class) if ($label_class);
514     $vol->label($label);
515     $vol->prefix($prefix);
516     $vol->suffix($suffix);
517     $vol->record($record_id);
518
519     my $evt = $class->create_volume(0, $e, $vol);
520     return (undef, $evt) if $evt;
521
522     return ($vol);
523 }
524
525
526 sub create_copy_note {
527     my($class, $e, $copy, $title, $value, $pub) = @_;
528     my $note = Fieldmapper::asset::copy_note->new;
529     $note->owning_copy($copy->id);
530     $note->creator($e->requestor->id);
531     $note->pub($pub ? 't' : 'f');
532     $note->value($value);
533     $note->title($title);
534     $e->create_asset_copy_note($note) or return $e->die_event;
535     return undef;
536 }
537
538
539 sub remove_empty_objects {
540     my($class, $editor, $override, $vol, $force_delete_empty_bib) = @_; 
541
542     $override = { all => 1 } if($override && !ref $override);
543     $override = { all => 0 } if(!ref $override);
544
545     my $koe = $U->ou_ancestor_setting_value(
546         $editor->requestor->ws_ou, 'cat.bib.keep_on_empty', $editor);
547     my $aoe =  $U->ou_ancestor_setting_value(
548         $editor->requestor->ws_ou, 'cat.bib.alert_on_empty', $editor);
549
550     if( OpenILS::Application::Cat::BibCommon->title_is_empty($editor, $vol->record, $vol->id) ) {
551
552         # delete this volume if it's not already marked as deleted
553         unless( $U->is_true($vol->deleted) || $vol->isdeleted ) {
554             my $evt = $class->delete_volume($editor, $vol, $override, 0, 1);
555             return $evt if $evt;
556         }
557
558         return OpenILS::Event->new('TITLE_LAST_COPY', payload => $vol->record ) 
559             if $aoe and not ($override->{all} || grep { $_ eq 'TITLE_LAST_COPY' } @{$override->{events}}) and not $force_delete_empty_bib;
560
561         unless($koe and not $force_delete_empty_bib) {
562             # delete the bib record if the keep-on-empty setting is not set (and we're not otherwise forcing things, say through acq settings)
563             my $evt = OpenILS::Application::Cat::BibCommon->delete_rec($editor, $vol->record);
564             return $evt if $evt;
565         }
566
567     } else {
568
569         # this may be the last copy attached to the volume.  
570
571         if($U->ou_ancestor_setting_value(
572                 $editor->requestor->ws_ou, 'cat.volume.delete_on_empty', $editor)) {
573
574             # if this volume is "empty" and not mid-delete, delete it.
575             unless($U->is_true($vol->deleted) || $vol->isdeleted) {
576
577                 my $copies = $editor->search_asset_copy(
578                     [{call_number => $vol->id, deleted => 'f'}, {limit => 1}], {idlist => 1});
579
580                 if(!@$copies) {
581                     my $evt = $class->delete_volume($editor, $vol, $override, 0, 1);
582                     return $evt if $evt;
583                 }
584             }
585         }
586     }
587
588     return undef;
589 }
590
591 # Deletes a volume.  Returns undef on success, event on error
592 # force : deletes all attached copies
593 # skip_copy_check : assumes caller has verified no copies need deleting first
594 sub delete_volume {
595     my($class, $editor, $vol, $override, $delete_copies, $skip_copy_checks) = @_;
596     my $evt;
597
598     unless($skip_copy_checks) {
599         my $cs = $editor->search_asset_copy(
600             [{call_number => $vol->id, deleted => 'f'}, {limit => 1}], {idlist => 1});
601
602         return OpenILS::Event->new('VOLUME_NOT_EMPTY', payload => $vol->id) 
603             if @$cs and !$delete_copies;
604
605         my $copies = $editor->search_asset_copy({call_number => $vol->id, deleted => 'f'});
606
607         for my $copy (@$copies) {
608             $evt = $class->delete_copy($editor, $override, $vol, $copy, 0, 0, 1);
609             return $evt if $evt;
610         }
611     }
612
613     $vol->deleted('t');
614     $vol->edit_date('now');
615     $vol->editor($editor->requestor->id);
616     $editor->update_asset_call_number($vol) or return $editor->die_event;
617
618     $evt = $class->cancel_volume_holds($editor, $vol);
619     return $evt if $evt;
620
621     # handle the case where this is the last volume on the record
622     return $class->remove_empty_objects($editor, $override, $vol);
623 }
624
625
626 sub copy_perm_org {
627     my($class, $vol, $copy) = @_;
628     my $org = $vol->owning_lib;
629     if( $vol->id == OILS_PRECAT_CALL_NUMBER ) {
630         $org = ref($copy->circ_lib) ? $copy->circ_lib->id : $copy->circ_lib;
631     }
632     $logger->debug("using copy perm org $org");
633     return $org;
634 }
635
636
637 sub set_item_lost {
638     my ($class, $e, $copy_id) = @_;
639
640     return $class->set_item_lost_or_lod(
641         $e, $copy_id,
642         perm => 'SET_CIRC_LOST',
643         status => OILS_COPY_STATUS_LOST,
644         ous_proc_fee => OILS_SETTING_LOST_PROCESSING_FEE,
645         ous_void_od => OILS_SETTING_VOID_OVERDUE_ON_LOST,
646         bill_type => 3,
647         bill_fee_type => 4,
648         bill_note => 'Lost Materials',
649         bill_fee_note => 'Lost Materials Processing Fee',
650         event => 'COPY_MARKED_LOST',
651         stop_fines => OILS_STOP_FINES_LOST,
652         at_hook => 'lost'
653     );
654 }
655
656 sub set_item_long_overdue {
657     my ($class, $e, $copy_id) = @_;
658
659     return $class->set_item_lost_or_lod(
660         $e, $copy_id,
661         perm => 'SET_CIRC_LONG_OVERDUE',
662         status => 16, # Long Overdue
663         ous_proc_fee => 'circ.longoverdue_materials_processing_fee',
664         ous_void_od => 'circ.void_overdue_on_longoverdue',
665         bill_type => 10,
666         bill_fee_type => 11,
667         bill_note => 'Long Overdue Materials',
668         bill_fee_note => 'Long Overdue Materials Processing Fee',
669         event => 'COPY_MARKED_LONG_OVERDUE',
670         stop_fines => 'LONGOVERDUE',
671         at_hook => 'longoverdue'
672     );
673 }
674
675 # LOST or LONGOVERDUE
676 # basic process is the same.  details change.
677 sub set_item_lost_or_lod {
678     my ($class, $e, $copy_id, %args) = @_;
679
680     my $copy = $e->retrieve_asset_copy([
681         $copy_id, 
682         {flesh => 1, flesh_fields => {'acp' => ['call_number']}}])
683             or return $e->die_event;
684
685     my $owning_lib = 
686         ($copy->call_number->id == OILS_PRECAT_CALL_NUMBER) ? 
687             $copy->circ_lib : $copy->call_number->owning_lib;
688
689     my $circ = $e->search_action_circulation(
690         {checkin_time => undef, target_copy => $copy->id} )->[0]
691             or return $e->die_event;
692
693     $e->allowed($args{perm}, $circ->circ_lib) or return $e->die_event;
694
695     return $e->die_event(OpenILS::Event->new($args{event}))
696             if $copy->status == $args{status};
697
698     # ---------------------------------------------------------------------
699     # fetch the related org settings
700     my $proc_fee = $U->ou_ancestor_setting_value(
701         $owning_lib, $args{ous_proc_fee}, $e) || 0;
702     my $void_overdue = $U->ou_ancestor_setting_value(
703         $owning_lib, $args{ous_void_od}, $e) || 0;
704
705     # ---------------------------------------------------------------------
706     # move the copy into LOST status
707     $copy->status($args{status});
708     $copy->editor($e->requestor->id);
709     $copy->edit_date('now');
710     $e->update_asset_copy($copy) or return $e->die_event;
711
712     my $price = $U->get_copy_price($e, $copy, $copy->call_number);
713
714     if( $price > 0 ) {
715         my $evt = OpenILS::Application::Circ::CircCommon->create_bill($e, 
716             $price, $args{bill_type}, $args{bill_note}, $circ->id);
717         return $evt if $evt;
718     }
719
720     # ---------------------------------------------------------------------
721     # if there is a processing fee, charge that too
722     if( $proc_fee > 0 ) {
723         my $evt = OpenILS::Application::Circ::CircCommon->create_bill($e, 
724             $proc_fee, $args{bill_fee_type}, $args{bill_fee_note}, $circ->id);
725         return $evt if $evt;
726     }
727
728     # ---------------------------------------------------------------------
729     # mark the circ as lost and stop the fines
730     $circ->stop_fines($args{stop_fines});
731     $circ->stop_fines_time('now') unless $circ->stop_fines_time;
732     $e->update_action_circulation($circ) or return $e->die_event;
733
734     # ---------------------------------------------------------------------
735     # void all overdue fines on this circ if configured
736     if( $void_overdue ) {
737         my $evt = OpenILS::Application::Circ::CircCommon->void_overdues($e, $circ);
738         return $evt if $evt;
739     }
740
741     my $evt = OpenILS::Application::Circ::CircCommon->reopen_xact($e, $circ->id);
742     return $evt if $evt;
743
744     my $ses = OpenSRF::AppSession->create('open-ils.trigger');
745     $ses->request(
746         'open-ils.trigger.event.autocreate', 
747         $args{at_hook}, $circ, $circ->circ_lib
748     );
749
750     my $evt2 = OpenILS::Utils::Penalty->calculate_penalties(
751         $e, $circ->usr, $U->xact_org($circ->id, $e));
752     return $evt2 if $evt2;
753
754     return undef;
755 }