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