]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/Application/Cat/AssetCommon.pm
LP#121054: Add Deleted Flag to asset.copy_location
[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     my $copy_loc = $editor->search_asset_copy_location(
59         { id => $copy->location, deleted => 'f' } );
60         
61     return OpenILS::Event->new('COPY_LOCATION_NOT_FOUND') unless @$copy_loc;
62     
63    # see if the volume this copy references is marked as deleted
64     return OpenILS::Event->new('VOLUME_DELETED', vol => $vol->id) 
65         if $U->is_true($vol->deleted);
66
67     my $evt;
68     my $org = (ref $copy->circ_lib) ? $copy->circ_lib->id : $copy->circ_lib;
69     return $evt if ($evt = $class->org_cannot_have_vols($editor, $org));
70
71     $copy->clear_id;
72     $copy->editor($editor->requestor->id);
73     $copy->creator($editor->requestor->id);
74     $copy->create_date('now');
75     $copy->call_number($vol->id);
76     $class->fix_copy_price($copy);
77
78     $editor->create_asset_copy($copy) or return $editor->die_event;
79     return undef;
80 }
81
82
83 # if 'delete_stats' is true, the copy->stat_cat_entries data is 
84 # treated as the authoritative list for the copy. existing entries
85 # that are not in said list will be deleted from the DB
86 sub update_copy_stat_entries {
87     my($class, $editor, $copy, $delete_stats) = @_;
88
89     return undef if $copy->isdeleted;
90     return undef unless $copy->ischanged or $copy->isnew;
91
92     my $evt;
93     my $entries = $copy->stat_cat_entries;
94
95     if( $delete_stats ) {
96         $entries = ($entries and @$entries) ? $entries : [];
97     } else {
98         return undef unless ($entries and @$entries);
99     }
100
101     my $maps = $editor->search_asset_stat_cat_entry_copy_map({owning_copy=>$copy->id});
102
103     if(!$copy->isnew) {
104         # if there is no stat cat entry on the copy who's id matches the
105         # current map's id, remove the map from the database
106         for my $map (@$maps) {
107             if(! grep { $_->id == $map->stat_cat_entry } @$entries ) {
108
109                 $logger->info("copy update found stale ".
110                     "stat cat entry map ".$map->id. " on copy ".$copy->id);
111
112                 $editor->delete_asset_stat_cat_entry_copy_map($map)
113                     or return $editor->event;
114             }
115         }
116     }
117
118     # go through the stat cat update/create process
119     for my $entry (@$entries) { 
120         next unless $entry;
121
122         # if this link already exists in the DB, don't attempt to re-create it
123         next if( grep{$_->stat_cat_entry == $entry->id} @$maps );
124     
125         my $new_map = Fieldmapper::asset::stat_cat_entry_copy_map->new();
126
127         my $sc = ref($entry->stat_cat) ? $entry->stat_cat->id : $entry->stat_cat;
128         
129         $new_map->stat_cat( $sc );
130         $new_map->stat_cat_entry( $entry->id );
131         $new_map->owning_copy( $copy->id );
132
133         $editor->create_asset_stat_cat_entry_copy_map($new_map)
134             or return $editor->event;
135
136         $logger->info("copy update created new stat cat entry map ".$editor->data);
137     }
138
139     return undef;
140 }
141
142 # if 'delete_maps' is true, the copy->parts data is  treated as the
143 # authoritative list for the copy. existing part maps not targeting
144 # these parts will be deleted from the DB
145 sub update_copy_parts {
146     my($class, $editor, $copy, $delete_maps) = @_;
147
148     return undef if $copy->isdeleted;
149     return undef unless $copy->ischanged or $copy->isnew;
150
151     my $evt;
152     my $incoming_parts = $copy->parts;
153
154     if( $delete_maps ) {
155         $incoming_parts = ($incoming_parts and @$incoming_parts) ? $incoming_parts : [];
156     } else {
157         return undef unless ($incoming_parts and @$incoming_parts);
158     }
159
160     my $maps = $editor->search_asset_copy_part_map({target_copy=>$copy->id});
161
162     if(!$copy->isnew) {
163         # if there is no part map on the copy who's id matches the
164         # current map's id, remove the map from the database
165         for my $map (@$maps) {
166             if(! grep { $_->id == $map->part } @$incoming_parts ) {
167
168                 $logger->info("copy update found stale ".
169                     "monographic part map ".$map->id. " on copy ".$copy->id);
170
171                 $editor->delete_asset_copy_part_map($map)
172                     or return $editor->event;
173             }
174         }
175     }
176
177     # go through the part map update/create process
178     for my $incoming_part (@$incoming_parts) { 
179         next unless $incoming_part;
180
181         # if this link already exists in the DB, don't attempt to re-create it
182         next if( grep{$_->part == $incoming_part->id} @$maps );
183     
184         my $new_map = Fieldmapper::asset::copy_part_map->new();
185
186         $new_map->part( $incoming_part->id );
187         $new_map->target_copy( $copy->id );
188
189         $editor->create_asset_copy_part_map($new_map)
190             or return $editor->event;
191
192         $logger->info("copy update created new monographic part copy map ".$editor->data);
193     }
194
195     return undef;
196 }
197
198
199
200 sub update_copy {
201     my($class, $editor, $override, $vol, $copy, $retarget_holds, $force_delete_empty_bib) = @_;
202
203     $override = { all => 1 } if($override && !ref $override);
204     $override = { all => 0 } if(!ref $override);
205
206     # Duplicated check from create_copy in case a copy template with a deleted location is applied later
207     my $copy_loc = $editor->search_asset_copy_location(
208         { id => $copy->location, deleted => 'f' } );
209         
210     return OpenILS::Event->new('COPY_LOCATION_NOT_FOUND') unless @$copy_loc;
211     
212     my $evt;
213     my $org = (ref $copy->circ_lib) ? $copy->circ_lib->id : $copy->circ_lib;
214     return $evt if ( $evt = $class->org_cannot_have_vols($editor, $org) );
215
216     $logger->info("vol-update: updating copy ".$copy->id);
217     my $orig_copy = $editor->retrieve_asset_copy($copy->id);
218
219     # Call-number may have changed, find the original
220     my $orig_vol_id = $editor->json_query({select => {acp => ['call_number']}, from => 'acp', where => {id => $copy->id}});
221     my $orig_vol  = $editor->retrieve_asset_call_number($orig_vol_id->[0]->{call_number});
222
223     $copy->editor($editor->requestor->id);
224     $copy->edit_date('now');
225
226     $copy->age_protect( $copy->age_protect->id )
227         if ref $copy->age_protect;
228
229     $class->fix_copy_price($copy);
230     $class->check_hold_retarget($editor, $copy, $orig_copy, $retarget_holds);
231
232     return $editor->event unless $editor->update_asset_copy($copy);
233     return $class->remove_empty_objects($editor, $override, $orig_vol, $force_delete_empty_bib);
234 }
235
236 sub check_hold_retarget {
237     my($class, $editor, $copy, $orig_copy, $retarget_holds) = @_;
238     return unless $retarget_holds;
239
240     if( !($copy->isdeleted or $U->is_true($copy->deleted)) ) {
241         # see if a status change warrants a retarget
242
243         $orig_copy = $editor->retrieve_asset_copy($copy->id) unless $orig_copy;
244
245         if($orig_copy->status == $copy->status) {
246             # no status change, no retarget
247             return;
248         }
249
250         my $stat = $editor->retrieve_config_copy_status($copy->status);
251
252         # new status is holdable, no retarget. Later add logic to find potential 
253         # holds and retarget those to pick up the newly available copy
254         return if $U->is_true($stat->holdable); 
255     }
256
257     my $hold_ids = $editor->search_action_hold_request(
258         {   current_copy        => $copy->id, 
259             cancel_time         => undef, 
260             fulfillment_time    => undef 
261         }, {idlist => 1}
262     );
263
264     push(@$retarget_holds, @$hold_ids);
265 }
266
267
268 # this does the actual work
269 sub update_fleshed_copies {
270     my($class, $editor, $override, $vol, $copies, $delete_stats, $retarget_holds, $force_delete_empty_bib) = @_;
271
272     $override = { all => 1 } if($override && !ref $override);
273     $override = { all => 0 } if(!ref $override);
274
275     my $evt;
276     my $fetchvol = ($vol) ? 0 : 1;
277
278     my %cache;
279     $cache{$vol->id} = $vol if $vol;
280
281     for my $copy (@$copies) {
282
283         my $copyid = $copy->id;
284         $logger->info("vol-update: inspecting copy $copyid");
285
286         if( !($vol = $cache{$copy->call_number}) ) {
287             $vol = $cache{$copy->call_number} = 
288                 $editor->retrieve_asset_call_number($copy->call_number);
289             return $editor->event unless $vol;
290         }
291
292         return $editor->event unless 
293             $editor->allowed('UPDATE_COPY', $class->copy_perm_org($vol, $copy));
294
295         $copy->editor($editor->requestor->id);
296         $copy->edit_date('now');
297
298         $copy->status( $copy->status->id ) if ref($copy->status);
299         $copy->location( $copy->location->id ) if ref($copy->location);
300         $copy->circ_lib( $copy->circ_lib->id ) if ref($copy->circ_lib);
301         
302         my $sc_entries = $copy->stat_cat_entries;
303         $copy->clear_stat_cat_entries;
304
305         my $parts = $copy->parts;
306         $copy->clear_parts;
307
308         if( $copy->isdeleted ) {
309             $evt = $class->delete_copy($editor, $override, $vol, $copy, $retarget_holds, $force_delete_empty_bib);
310             return $evt if $evt;
311
312         } elsif( $copy->isnew ) {
313             $evt = $class->create_copy( $editor, $vol, $copy );
314             return $evt if $evt;
315
316         } elsif( $copy->ischanged ) {
317
318             $evt = $class->update_copy( $editor, $override, $vol, $copy, $retarget_holds, $force_delete_empty_bib);
319             return $evt if $evt;
320         }
321
322         $copy->stat_cat_entries( $sc_entries );
323         $evt = $class->update_copy_stat_entries($editor, $copy, $delete_stats);
324         $copy->parts( $parts );
325         # probably okay to use $delete_stats here for simplicity
326         $evt = $class->update_copy_parts($editor, $copy, $delete_stats);
327         return $evt if $evt;
328     }
329
330     $logger->debug("vol-update: done updating copy batch");
331
332     return undef;
333 }
334
335
336 sub delete_copy {
337     my($class, $editor, $override, $vol, $copy, $retarget_holds, $force_delete_empty_bib, $skip_empty_cleanup) = @_;
338
339     return $editor->event unless
340         $editor->allowed('DELETE_COPY', $class->copy_perm_org($vol, $copy));
341
342     $override = { all => 1 } if($override && !ref $override);
343     $override = { all => 0 } if(!ref $override);
344
345     my $stat = $U->copy_status($copy->status);
346     if ($U->is_true($stat->restrict_copy_delete)) {
347         if ($override->{all} || grep { $_ eq 'COPY_DELETE_WARNING' } @{$override->{events}}) {
348             return $editor->event unless $editor->allowed('COPY_DELETE_WARNING.override', $class->copy_perm_org($vol, $copy))
349         } else {
350             return OpenILS::Event->new('COPY_DELETE_WARNING', payload => $copy->id )
351         }
352     }
353
354     $logger->info("vol-update: deleting copy ".$copy->id);
355     $copy->deleted('t');
356
357     $copy->editor($editor->requestor->id);
358     $copy->edit_date('now');
359     $editor->update_asset_copy($copy) or return $editor->event;
360
361     # Delete any open transits for this copy
362     my $transits = $editor->search_action_transit_copy(
363         { target_copy=>$copy->id, dest_recv_time => undef } );
364
365     for my $t (@$transits) {
366         $editor->delete_action_transit_copy($t)
367             or return $editor->event;
368     }
369
370     my $evt = $class->cancel_copy_holds($editor, $copy);
371     return $evt if $evt;
372
373     $class->check_hold_retarget($editor, $copy, undef, $retarget_holds);
374
375     return undef if $skip_empty_cleanup;
376
377     return $class->remove_empty_objects($editor, $override, $vol, $force_delete_empty_bib);
378 }
379
380
381 # deletes all holds that specifically target the deleted copy
382 sub cancel_copy_holds {
383     my($class, $editor, $copy) = @_;
384
385     my $holds = $editor->search_action_hold_request({   
386         target              => $copy->id, 
387         hold_type           => [qw/C R F/],
388         cancel_time         => undef, 
389         fulfillment_time    => undef 
390     });
391
392     return $class->cancel_hold_list($editor, $holds);
393 }
394
395 # deletes all holds that specifically target the deleted volume
396 sub cancel_volume_holds {
397     my($class, $editor, $volume) = @_;
398
399     my $holds = $editor->search_action_hold_request({   
400         target              => $volume->id, 
401         hold_type           => 'V',
402         cancel_time         => undef, 
403         fulfillment_time    => undef 
404     });
405
406     return $class->cancel_hold_list($editor, $holds);
407 }
408
409 sub cancel_hold_list {
410     my($class, $editor, $holds) = @_;
411
412     for my $hold (@$holds) {
413
414         $hold->cancel_time('now');
415         $hold->cancel_cause(1); # un-targeted expiration.  Do we need an alternate "target deleted" cause?
416         $editor->update_action_hold_request($hold) or return $editor->die_event;
417
418         # tell A/T the hold was cancelled.  Don't wait for a response..
419         my $at_ses = OpenSRF::AppSession->create('open-ils.trigger');
420         $at_ses->request(
421             'open-ils.trigger.event.autocreate',
422             'hold_request.cancel.expire_no_target', 
423             $hold, $hold->pickup_lib);
424     }
425
426     return undef;
427 }
428
429
430
431 sub create_volume {
432     my($class, $override, $editor, $vol) = @_;
433     my $evt;
434
435     return $evt if ( $evt = $class->org_cannot_have_vols($editor, $vol->owning_lib) );
436
437     $override = { all => 1 } if($override && !ref $override);
438     $override = { all => 0 } if(!ref $override);
439
440    # see if the record this volume references is marked as deleted
441    my $rec = $editor->retrieve_biblio_record_entry($vol->record)
442       or return $editor->die_event;
443    return OpenILS::Event->new('BIB_RECORD_DELETED', rec => $rec->id) 
444       if $U->is_true($rec->deleted);
445
446     # first lets see if there are any collisions
447     my $vols = $editor->search_asset_call_number( { 
448             owning_lib  => $vol->owning_lib,
449             record      => $vol->record,
450             label           => $vol->label,
451             prefix          => $vol->prefix,
452             suffix          => $vol->suffix,
453             deleted     => 'f'
454         }
455     );
456
457     my $label = undef;
458     if(@$vols) {
459       # we've found an exising volume
460         if($override->{all} || grep { $_ eq 'VOLUME_LABEL_EXISTS' } @{$override->{events}}) {
461             $label = $vol->label;
462         } else {
463             return OpenILS::Event->new(
464                 'VOLUME_LABEL_EXISTS', payload => $vol->id);
465         }
466     }
467
468     # create a temp label so we can create the new volume, 
469     # then de-dup it with the existing volume
470     $vol->label( "__SYSTEM_TMP_$$".time) if $label;
471
472     $vol->creator($editor->requestor->id);
473     $vol->create_date('now');
474     $vol->editor($editor->requestor->id);
475     $vol->edit_date('now');
476     $vol->clear_id;
477
478     $editor->create_asset_call_number($vol) or return $editor->die_event;
479
480     if($label) {
481         # now restore the label and merge into the existing record
482         $vol->label($label);
483         (undef, $evt) = 
484             OpenILS::Application::Cat::Merge::merge_volumes($editor, [$vol], $$vols[0]);
485         return $evt if $evt;
486     }
487
488     return undef;
489 }
490
491 # returns the volume if it exists
492 sub volume_exists {
493     my($class, $e, $rec_id, $label, $owning_lib, $prefix, $suffix) = @_;
494     return $e->search_asset_call_number(
495         {label => $label, record => $rec_id, owning_lib => $owning_lib, deleted => 'f', prefix => $prefix, suffix => $suffix})->[0];
496 }
497
498 sub find_or_create_volume {
499     my($class, $e, $label, $record_id, $org_id, $prefix, $suffix, $label_class) = @_;
500
501     $prefix ||= '-1';
502     $suffix ||= '-1';
503
504     my $vol;
505
506     if($record_id == OILS_PRECAT_RECORD) {
507         $vol = $e->retrieve_asset_call_number(OILS_PRECAT_CALL_NUMBER)
508             or return (undef, $e->die_event);
509
510     } else {
511         $vol = $class->volume_exists($e, $record_id, $label, $org_id, $prefix, $suffix);
512     }
513
514     # If the volume exists, return the ID
515     return ($vol, undef, 1) if $vol;
516
517     # -----------------------------------------------------------------
518     # Otherwise, create a new volume with the given attributes
519     # -----------------------------------------------------------------
520     return (undef, $e->die_event) unless $e->allowed('UPDATE_VOLUME', $org_id);
521
522     $vol = Fieldmapper::asset::call_number->new;
523     $vol->owning_lib($org_id);
524     $vol->label_class($label_class) if ($label_class);
525     $vol->label($label);
526     $vol->prefix($prefix);
527     $vol->suffix($suffix);
528     $vol->record($record_id);
529
530     my $evt = $class->create_volume(0, $e, $vol);
531     return (undef, $evt) if $evt;
532
533     return ($vol);
534 }
535
536
537 sub create_copy_note {
538     my($class, $e, $copy, $title, $value, $pub) = @_;
539     my $note = Fieldmapper::asset::copy_note->new;
540     $note->owning_copy($copy->id);
541     $note->creator($e->requestor->id);
542     $note->pub($pub ? 't' : 'f');
543     $note->value($value);
544     $note->title($title);
545     $e->create_asset_copy_note($note) or return $e->die_event;
546     return undef;
547 }
548
549
550 sub remove_empty_objects {
551     my($class, $editor, $override, $vol, $force_delete_empty_bib) = @_; 
552
553     $override = { all => 1 } if($override && !ref $override);
554     $override = { all => 0 } if(!ref $override);
555
556     my $koe = $U->ou_ancestor_setting_value(
557         $editor->requestor->ws_ou, 'cat.bib.keep_on_empty', $editor);
558     my $aoe =  $U->ou_ancestor_setting_value(
559         $editor->requestor->ws_ou, 'cat.bib.alert_on_empty', $editor);
560
561     if( OpenILS::Application::Cat::BibCommon->title_is_empty($editor, $vol->record, $vol->id) ) {
562
563         # delete this volume if it's not already marked as deleted
564         unless( $U->is_true($vol->deleted) || $vol->isdeleted ) {
565             my $evt = $class->delete_volume($editor, $vol, $override, 0, 1);
566             return $evt if $evt;
567         }
568
569         return OpenILS::Event->new('TITLE_LAST_COPY', payload => $vol->record ) 
570             if $aoe and not ($override->{all} || grep { $_ eq 'TITLE_LAST_COPY' } @{$override->{events}}) and not $force_delete_empty_bib;
571
572         unless($koe and not $force_delete_empty_bib) {
573             # delete the bib record if the keep-on-empty setting is not set (and we're not otherwise forcing things, say through acq settings)
574             my $evt = OpenILS::Application::Cat::BibCommon->delete_rec($editor, $vol->record);
575             return $evt if $evt;
576         }
577
578     } else {
579
580         # this may be the last copy attached to the volume.  
581
582         if($U->ou_ancestor_setting_value(
583                 $editor->requestor->ws_ou, 'cat.volume.delete_on_empty', $editor)) {
584
585             # if this volume is "empty" and not mid-delete, delete it.
586             unless($U->is_true($vol->deleted) || $vol->isdeleted) {
587
588                 my $copies = $editor->search_asset_copy(
589                     [{call_number => $vol->id, deleted => 'f'}, {limit => 1}], {idlist => 1});
590
591                 if(!@$copies) {
592                     my $evt = $class->delete_volume($editor, $vol, $override, 0, 1);
593                     return $evt if $evt;
594                 }
595             }
596         }
597     }
598
599     return undef;
600 }
601
602 # Deletes a volume.  Returns undef on success, event on error
603 # force : deletes all attached copies
604 # skip_copy_check : assumes caller has verified no copies need deleting first
605 sub delete_volume {
606     my($class, $editor, $vol, $override, $delete_copies, $skip_copy_checks) = @_;
607     my $evt;
608
609     unless($skip_copy_checks) {
610         my $cs = $editor->search_asset_copy(
611             [{call_number => $vol->id, deleted => 'f'}, {limit => 1}], {idlist => 1});
612
613         return OpenILS::Event->new('VOLUME_NOT_EMPTY', payload => $vol->id) 
614             if @$cs and !$delete_copies;
615
616         my $copies = $editor->search_asset_copy({call_number => $vol->id, deleted => 'f'});
617
618         for my $copy (@$copies) {
619             $evt = $class->delete_copy($editor, $override, $vol, $copy, 0, 0, 1);
620             return $evt if $evt;
621         }
622     }
623
624     $vol->deleted('t');
625     $vol->edit_date('now');
626     $vol->editor($editor->requestor->id);
627     $editor->update_asset_call_number($vol) or return $editor->die_event;
628
629     $evt = $class->cancel_volume_holds($editor, $vol);
630     return $evt if $evt;
631
632     # handle the case where this is the last volume on the record
633     return $class->remove_empty_objects($editor, $override, $vol);
634 }
635
636
637 sub copy_perm_org {
638     my($class, $vol, $copy) = @_;
639     my $org = $vol->owning_lib;
640     if( $vol->id == OILS_PRECAT_CALL_NUMBER ) {
641         $org = ref($copy->circ_lib) ? $copy->circ_lib->id : $copy->circ_lib;
642     }
643     $logger->debug("using copy perm org $org");
644     return $org;
645 }
646
647
648 sub set_item_lost {
649     my ($class, $e, $copy_id) = @_;
650
651     return $class->set_item_lost_or_lod(
652         $e, $copy_id,
653         perm => 'SET_CIRC_LOST',
654         status => OILS_COPY_STATUS_LOST,
655         ous_proc_fee => OILS_SETTING_LOST_PROCESSING_FEE,
656         ous_void_od => OILS_SETTING_VOID_OVERDUE_ON_LOST,
657         bill_type => 3,
658         bill_fee_type => 4,
659         bill_note => 'Lost Materials',
660         bill_fee_note => 'Lost Materials Processing Fee',
661         event => 'COPY_MARKED_LOST',
662         stop_fines => OILS_STOP_FINES_LOST,
663         at_hook => 'lost'
664     );
665 }
666
667 sub set_item_long_overdue {
668     my ($class, $e, $copy_id) = @_;
669
670     return $class->set_item_lost_or_lod(
671         $e, $copy_id,
672         perm => 'SET_CIRC_LONG_OVERDUE',
673         status => 16, # Long Overdue
674         ous_proc_fee => 'circ.longoverdue_materials_processing_fee',
675         ous_void_od => 'circ.void_overdue_on_longoverdue',
676         bill_type => 10,
677         bill_fee_type => 11,
678         bill_note => 'Long Overdue Materials',
679         bill_fee_note => 'Long Overdue Materials Processing Fee',
680         event => 'COPY_MARKED_LONG_OVERDUE',
681         stop_fines => 'LONGOVERDUE',
682         at_hook => 'longoverdue'
683     );
684 }
685
686 # LOST or LONGOVERDUE
687 # basic process is the same.  details change.
688 sub set_item_lost_or_lod {
689     my ($class, $e, $copy_id, %args) = @_;
690
691     my $copy = $e->retrieve_asset_copy([
692         $copy_id, 
693         {flesh => 1, flesh_fields => {'acp' => ['call_number']}}])
694             or return $e->die_event;
695
696     my $owning_lib = 
697         ($copy->call_number->id == OILS_PRECAT_CALL_NUMBER) ? 
698             $copy->circ_lib : $copy->call_number->owning_lib;
699
700     my $circ = $e->search_action_circulation(
701         {checkin_time => undef, target_copy => $copy->id} )->[0]
702             or return $e->die_event;
703
704     $e->allowed($args{perm}, $circ->circ_lib) or return $e->die_event;
705
706     return $e->die_event(OpenILS::Event->new($args{event}))
707             if $copy->status == $args{status};
708
709     # ---------------------------------------------------------------------
710     # fetch the related org settings
711     my $proc_fee = $U->ou_ancestor_setting_value(
712         $owning_lib, $args{ous_proc_fee}, $e) || 0;
713     my $void_overdue = $U->ou_ancestor_setting_value(
714         $owning_lib, $args{ous_void_od}, $e) || 0;
715
716     # ---------------------------------------------------------------------
717     # move the copy into LOST status
718     $copy->status($args{status});
719     $copy->editor($e->requestor->id);
720     $copy->edit_date('now');
721     $e->update_asset_copy($copy) or return $e->die_event;
722
723     my $price = $U->get_copy_price($e, $copy, $copy->call_number);
724
725     if( $price > 0 ) {
726         my $evt = OpenILS::Application::Circ::CircCommon->create_bill($e, 
727             $price, $args{bill_type}, $args{bill_note}, $circ->id);
728         return $evt if $evt;
729     }
730
731     # ---------------------------------------------------------------------
732     # if there is a processing fee, charge that too
733     if( $proc_fee > 0 ) {
734         my $evt = OpenILS::Application::Circ::CircCommon->create_bill($e, 
735             $proc_fee, $args{bill_fee_type}, $args{bill_fee_note}, $circ->id);
736         return $evt if $evt;
737     }
738
739     # ---------------------------------------------------------------------
740     # mark the circ as lost and stop the fines
741     $circ->stop_fines($args{stop_fines});
742     $circ->stop_fines_time('now') unless $circ->stop_fines_time;
743     $e->update_action_circulation($circ) or return $e->die_event;
744
745     # ---------------------------------------------------------------------
746     # void all overdue fines on this circ if configured
747     if( $void_overdue ) {
748         my $evt = OpenILS::Application::Circ::CircCommon->void_overdues($e, $circ);
749         return $evt if $evt;
750     }
751
752     my $evt = OpenILS::Application::Circ::CircCommon->reopen_xact($e, $circ->id);
753     return $evt if $evt;
754
755     my $ses = OpenSRF::AppSession->create('open-ils.trigger');
756     $ses->request(
757         'open-ils.trigger.event.autocreate', 
758         $args{at_hook}, $circ, $circ->circ_lib
759     );
760
761     my $evt2 = OpenILS::Utils::Penalty->calculate_penalties(
762         $e, $circ->usr, $U->xact_org($circ->id, $e));
763     return $evt2 if $evt2;
764
765     return undef;
766 }