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