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