1 package OpenILS::Application::AppUtils;
2 use strict; use warnings;
3 use base qw/OpenSRF::Application/;
4 use OpenSRF::Utils::Cache;
5 use OpenSRF::Utils::Logger qw/$logger/;
6 use OpenILS::Utils::ModsParser;
7 use OpenSRF::EX qw(:try);
10 use OpenILS::Utils::CStoreEditor;
13 my $cache_client = "OpenSRF::Utils::Cache";
15 my $storage_session = undef;
17 # ---------------------------------------------------------------------------
18 # Pile of utilty methods used accross applications.
19 # ---------------------------------------------------------------------------
22 # ---------------------------------------------------------------------------
23 # on sucess, returns the created session, on failure throws ERROR exception
24 # ---------------------------------------------------------------------------
25 sub start_db_session {
28 my $session = OpenSRF::AppSession->connect( "open-ils.storage" );
29 my $trans_req = $session->request( "open-ils.storage.transaction.begin" );
31 my $trans_resp = $trans_req->recv();
32 if(ref($trans_resp) and UNIVERSAL::isa($trans_resp,"Error")) { throw $trans_resp; }
33 if( ! $trans_resp->content() ) {
35 ("Unable to Begin Transaction with database" );
39 $logger->debug("Setting global storage session to ".
40 "session: " . $session->session_id . " : " . $session->app );
42 $storage_session = $session;
47 # returns undef if user has all of the perms provided
48 # returns the first failed perm on failure
49 sub check_user_perms {
50 my($self, $user_id, $org_id, @perm_types ) = @_;
51 $logger->debug("Checking perms with user : $user_id , org: $org_id, @perm_types");
52 for my $type (@perm_types) {
53 return $type unless ($self->storagereq(
54 "open-ils.storage.permission.user_has_perm",
55 $user_id, $type, $org_id ));
60 # checks the list of user perms. The first one that fails returns a new
62 my( $self, $user_id, $org_id, @perm_types ) = @_;
63 my $t = $self->check_user_perms( $user_id, $org_id, @perm_types );
64 return OpenILS::Event->new('PERM_FAILURE', ilsperm => $t, ilspermloc => $org_id ) if $t;
70 # ---------------------------------------------------------------------------
71 # commits and destroys the session
72 # ---------------------------------------------------------------------------
73 sub commit_db_session {
74 my( $self, $session ) = @_;
76 my $req = $session->request( "open-ils.storage.transaction.commit" );
77 my $resp = $req->recv();
80 throw OpenSRF::EX::ERROR ("Unable to commit db session");
83 if(UNIVERSAL::isa($resp,"Error")) {
84 throw $resp ($resp->stringify);
88 throw OpenSRF::EX::ERROR ("Unable to commit db session");
92 $session->disconnect();
94 $storage_session = undef;
97 sub rollback_db_session {
98 my( $self, $session ) = @_;
100 my $req = $session->request("open-ils.storage.transaction.rollback");
101 my $resp = $req->recv();
102 if(UNIVERSAL::isa($resp,"Error")) { throw $resp; }
105 $session->disconnect();
107 $storage_session = undef;
111 # returns undef it the event is not an ILS event
112 # returns the event code otherwise
114 my( $self, $evt ) = @_;
115 return $evt->{ilsevent} if( ref($evt) eq 'HASH' and defined($evt->{ilsevent})) ;
119 # ---------------------------------------------------------------------------
120 # Checks to see if a user is logged in. Returns the user record on success,
121 # throws an exception on error.
122 # ---------------------------------------------------------------------------
123 sub check_user_session {
125 my( $self, $user_session ) = @_;
127 my $content = $self->simplereq(
129 'open-ils.auth.session.retrieve', $user_session );
131 if(! $content or $self->event_code($content)) {
132 throw OpenSRF::EX::ERROR
133 ("Session [$user_session] cannot be authenticated" );
136 $logger->debug("Fetch user session $user_session found user " . $content->id );
141 # generic simple request returning a scalar value
143 my($self, $service, $method, @params) = @_;
144 return $self->simple_scalar_request($service, $method, @params);
147 sub get_storage_session {
149 return undef; # XXX testing
151 if( $storage_session and
152 $storage_session->connected and
153 $storage_session->transport_connected and
154 $storage_session->app eq 'open-ils.storage' ) {
156 $logger->debug("get_storage_session(): returning existing session");
157 return $storage_session;
159 $logger->debug("get_storage_session(): returning undef");
160 $storage_session = undef;
165 sub simple_scalar_request {
166 my($self, $service, $method, @params) = @_;
170 if( $service eq 'open-ils.storage' ) {
171 if( $session = get_storage_session() ) {
172 $logger->debug("simple request using existing storage session ".$session->session_id);
173 } else { $session = undef; }
176 $session = OpenSRF::AppSession->create( $service ) unless $session;
178 my $request = $session->request( $method, @params );
184 $val = $request->gather(1);
191 warn "received error : service=$service : method=$method : params=".Dumper(\@params) . "\n $err";
192 throw $err ("Call to $service for method $method \n failed with exception: $err : " );
204 my $org_typelist = undef;
205 my $org_typelist_hash = {};
210 if($tree) { return $tree; }
212 # see if it's in the cache
213 $tree = $cache_client->new()->get_cache('_orgtree');
214 if($tree) { return $tree; }
217 warn "Retrieving Org Tree\n";
218 $orglist = $self->simple_scalar_request(
220 "open-ils.storage.direct.actor.org_unit.retrieve.all.atomic" );
223 if( ! $org_typelist ) {
224 warn "Retrieving org types\n";
225 $org_typelist = $self->simple_scalar_request(
227 "open-ils.storage.direct.actor.org_unit_type.retrieve.all.atomic" );
228 $self->build_org_type($org_typelist);
231 $tree = $self->build_org_tree($orglist,1);
232 $cache_client->new()->put_cache('_orgtree', $tree);
237 my $slimtree = undef;
238 sub get_slim_org_tree {
241 if($slimtree) { return $slimtree; }
243 # see if it's in the cache
244 $slimtree = $cache_client->new()->get_cache('slimorgtree');
245 if($slimtree) { return $slimtree; }
248 warn "Retrieving Org Tree\n";
249 $orglist = $self->simple_scalar_request(
251 "open-ils.storage.direct.actor.org_unit.retrieve.all.atomic" );
254 $slimtree = $self->build_org_tree($orglist);
255 $cache_client->new->put_cache('slimorgtree', $slimtree);
262 my($self, $org_typelist) = @_;
263 for my $type (@$org_typelist) {
264 $org_typelist_hash->{$type->id()} = $type;
272 my( $self, $orglist, $add_types ) = @_;
274 return $orglist unless (
275 ref($orglist) and @$orglist > 1 );
278 $a->ou_type <=> $b->ou_type ||
279 $a->name cmp $b->name } @$orglist;
281 for my $org (@list) {
285 if(!ref($org->ou_type()) and $add_types) {
286 $org->ou_type( $org_typelist_hash->{$org->ou_type()});
289 next unless (defined($org->parent_ou));
291 my ($parent) = grep { $_->id == $org->parent_ou } @list;
293 $parent->children([]) unless defined($parent->children);
294 push( @{$parent->children}, $org );
301 sub fetch_closed_date {
302 my( $self, $cd ) = @_;
305 $logger->debug("Fetching closed_date $cd from storage");
307 my $cd_obj = $self->simplereq(
309 'open-ils.storage.direct.actor.org_unit.closed_date.retrieve', $cd );
312 $logger->info("closed_date $cd not found in the db");
313 $evt = OpenILS::Event->new('ACTOR_USER_NOT_FOUND');
316 return ($cd_obj, $evt);
320 my( $self, $userid ) = @_;
323 $logger->debug("Fetching user $userid from storage");
325 $user = $self->simplereq(
327 'open-ils.storage.direct.actor.user.retrieve', $userid );
330 $logger->info("User $userid not found in the db");
331 $evt = OpenILS::Event->new('ACTOR_USER_NOT_FOUND');
334 return ($user, $evt);
338 my( $self, $session ) = @_;
339 my $user; my $evt; my $e;
341 $logger->debug("Checking user session $session");
344 $user = $self->check_user_session($session);
345 } catch Error with { $e = 1; };
347 $logger->debug("Done checking user session $session " . (($e) ? "error = $e" : "") );
349 if( $e or !$user ) { $evt = OpenILS::Event->new('NO_SESSION'); }
350 return ( $user, $evt );
354 # verifiese the session and checks the permissions agains the
355 # session user and the user's home_ou as the org id
357 my( $self, $session, @perms ) = @_;
358 my $user; my $evt; my $e;
359 $logger->debug("Checking user session $session and perms @perms");
360 ($user, $evt) = $self->checkses($session);
361 return (undef, $evt) if $evt;
362 $evt = $self->check_perms($user->id, $user->home_ou, @perms);
363 return ($user, $evt);
368 my( $self, $staffobj, $userid, @perms ) = @_;
370 $userid = $staffobj->id unless defined $userid;
372 $logger->debug("checkrequestor(): requestor => " . $staffobj->id . ", target => $userid");
374 if( $userid ne $staffobj->id ) {
375 ($user, $evt) = $self->fetch_user($userid);
376 return (undef, $evt) if $evt;
377 $evt = $self->check_perms( $staffobj->id, $user->home_ou, @perms );
383 return ($user, $evt);
386 sub checkses_requestor {
387 my( $self, $authtoken, $targetid, @perms ) = @_;
388 my( $requestor, $target, $evt );
390 ($requestor, $evt) = $self->checkses($authtoken);
391 return (undef, undef, $evt) if $evt;
393 ($target, $evt) = $self->checkrequestor( $requestor, $targetid, @perms );
394 return( $requestor, $target, $evt);
398 my( $self, $copyid ) = @_;
401 $logger->debug("Fetching copy $copyid from storage");
403 $copy = $self->simplereq(
405 'open-ils.storage.direct.asset.copy.retrieve', $copyid );
407 if(!$copy) { $evt = OpenILS::Event->new('ASSET_COPY_NOT_FOUND'); }
409 return( $copy, $evt );
413 # retrieves a circ object by id
414 sub fetch_circulation {
415 my( $self, $circid ) = @_;
418 $logger->debug("Fetching circ $circid from cstore");
420 $circ = $self->simplereq(
422 "open-ils.cstore.direct.action.circulation.retrieve", $circid );
425 $evt = OpenILS::Event->new('ACTION_CIRCULATION_NOT_FOUND', circid => $circid );
428 return ( $circ, $evt );
431 sub fetch_record_by_copy {
432 my( $self, $copyid ) = @_;
435 $logger->debug("Fetching record by copy $copyid from cstore");
437 $record = $self->simplereq(
439 'open-ils.cstore.direct.asset.copy.retrieve', $copyid,
441 flesh_fields => { bre => [ 'fixed_fields' ],
443 acp => [ 'call_number' ],
449 $evt = OpenILS::Event->new('BIBLIO_RECORD_ENTRY_NOT_FOUND');
451 $record = $record->call_number->record;
454 return ($record, $evt);
457 # turns a record object into an mvr (mods) object
459 my( $self, $record ) = @_;
460 my $u = OpenILS::Utils::ModsParser->new();
461 $u->start_mods_batch( $record->marc );
462 my $mods = $u->finish_mods_batch();
463 $mods->doc_id($record->id);
468 my( $self, $holdid ) = @_;
471 $logger->debug("Fetching hold $holdid from cstore");
473 $hold = $self->simplereq(
475 'open-ils.cstore.direct.action.hold_request.retrieve', $holdid);
477 $evt = OpenILS::Event->new('ACTION_HOLD_REQUEST_NOT_FOUND', holdid => $holdid) unless $hold;
479 return ($hold, $evt);
483 sub fetch_hold_transit_by_hold {
484 my( $self, $holdid ) = @_;
485 my( $transit, $evt );
487 $logger->debug("Fetching transit by hold $holdid from cstore");
489 $transit = $self->simplereq(
491 'open-ils.cstore.direct.action.hold_transit_copy.search.hold', $holdid );
493 $evt = OpenILS::Event->new('ACTION_HOLD_TRANSIT_COPY_NOT_FOUND', holdid => $holdid) unless $transit;
495 return ($transit, $evt );
498 # fetches the captured, but not fulfilled hold attached to a given copy
499 sub fetch_open_hold_by_copy {
500 my( $self, $copyid ) = @_;
501 $logger->debug("Searching for active hold for copy $copyid");
504 $hold = $self->cstorereq(
505 'open-ils.cstore.direct.action.hold_request.search',
507 current_copy => $copyid ,
508 capture_time => { "!=" => undef },
509 fulfillment_time => undef
512 $evt = OpenILS::Event->new('ACTION_HOLD_REQUEST_NOT_FOUND', copyid => $copyid) unless $hold;
513 return ($hold, $evt);
516 sub fetch_hold_transit {
517 my( $self, $transid ) = @_;
518 my( $htransit, $evt );
519 $logger->debug("Fetching hold transit with hold id $transid");
520 $htransit = $self->cstorereq(
521 'open-ils.cstore.direct.action.hold_transit_copy.retrieve', $transid );
522 $evt = OpenILS::Event->new('ACTION_HOLD_TRANSIT_COPY_NOT_FOUND', id => $transid) unless $htransit;
523 return ($htransit, $evt);
526 sub fetch_copy_by_barcode {
527 my( $self, $barcode ) = @_;
530 $logger->debug("Fetching copy by barcode $barcode from cstore");
532 $copy = $self->simplereq( 'open-ils.cstore',
533 'open-ils.cstore.direct.asset.copy.search', { barcode => $barcode, deleted => 'f'} );
534 #'open-ils.storage.direct.asset.copy.search.barcode', $barcode );
536 $evt = OpenILS::Event->new('ASSET_COPY_NOT_FOUND', barcode => $barcode) unless $copy;
538 return ($copy, $evt);
541 sub fetch_open_billable_transaction {
542 my( $self, $transid ) = @_;
543 my( $transaction, $evt );
545 $logger->debug("Fetching open billable transaction $transid from cstore");
547 $transaction = $self->simplereq(
549 'open-ils.cstore.direct.money.open_billable_transaction_summary.retrieve', $transid);
551 $evt = OpenILS::Event->new(
552 'MONEY_OPEN_BILLABLE_TRANSACTION_SUMMARY_NOT_FOUND', transid => $transid ) unless $transaction;
554 return ($transaction, $evt);
560 $buckets{'biblio'} = 'biblio_record_entry_bucket';
561 $buckets{'callnumber'} = 'call_number_bucket';
562 $buckets{'copy'} = 'copy_bucket';
563 $buckets{'user'} = 'user_bucket';
565 sub fetch_container {
566 my( $self, $id, $type ) = @_;
569 $logger->debug("Fetching container $id with type $type");
571 my $e = 'CONTAINER_CALL_NUMBER_BUCKET_NOT_FOUND';
572 $e = 'CONTAINER_BIBLIO_RECORD_ENTRY_BUCKET_NOT_FOUND' if $type eq 'biblio';
573 $e = 'CONTAINER_USER_BUCKET_NOT_FOUND' if $type eq 'user';
574 $e = 'CONTAINER_COPY_BUCKET_NOT_FOUND' if $type eq 'copy';
576 my $meth = $buckets{$type};
577 $bucket = $self->simplereq(
579 "open-ils.cstore.direct.container.$meth.retrieve", $id );
581 $evt = OpenILS::Event->new(
582 $e, container => $id, container_type => $type ) unless $bucket;
584 return ($bucket, $evt);
588 sub fetch_container_item {
589 my( $self, $id, $type ) = @_;
592 $logger->debug("Fetching container item $id with type $type");
594 my $meth = $buckets{$type} . "_item";
596 $bucket = $self->simplereq(
598 "open-ils.cstore.direct.container.$meth.retrieve", $id );
601 my $e = 'CONTAINER_CALL_NUMBER_BUCKET_ITEM_NOT_FOUND';
602 $e = 'CONTAINER_BIBLIO_RECORD_ENTRY_BUCKET_ITEM_NOT_FOUND' if $type eq 'biblio';
603 $e = 'CONTAINER_USER_BUCKET_ITEM_NOT_FOUND' if $type eq 'user';
604 $e = 'CONTAINER_COPY_BUCKET_ITEM_NOT_FOUND' if $type eq 'copy';
607 $evt = OpenILS::Event->new(
608 $e, itemid => $id, container_type => $type ) unless $bucket;
610 return ($bucket, $evt);
614 sub fetch_patron_standings {
616 $logger->debug("Fetching patron standings");
617 return $self->simplereq(
619 'open-ils.cstore.direct.config.standing.search.atomic', { id => { '!=' => undef } });
623 sub fetch_permission_group_tree {
625 $logger->debug("Fetching patron profiles");
626 return $self->simplereq(
628 'open-ils.actor.groups.tree.retrieve' );
632 sub fetch_patron_circ_summary {
633 my( $self, $userid ) = @_;
634 $logger->debug("Fetching patron summary for $userid");
635 my $summary = $self->simplereq(
637 "open-ils.storage.action.circulation.patron_summary", $userid );
641 $summary->[1] ||= 0.0;
648 sub fetch_copy_statuses {
650 $logger->debug("Fetching copy statuses");
651 return $self->simplereq(
653 'open-ils.cstore.direct.config.copy_status.search.atomic', { id => { '!=' => undef } });
656 sub fetch_copy_location {
657 my( $self, $id ) = @_;
659 my $cl = $self->cstorereq(
660 'open-ils.cstore.direct.asset.copy_location.retrieve', $id );
661 $evt = OpenILS::Event->new('ASSET_COPY_LOCATION_NOT_FOUND') unless $cl;
665 sub fetch_copy_locations {
667 return $self->simplereq(
669 'open-ils.cstore.direct.asset.copy_location.search.atomic', { id => { '!=' => undef } });
672 sub fetch_copy_location_by_name {
673 my( $self, $name, $org ) = @_;
675 my $cl = $self->cstorereq(
676 'open-ils.cstore.direct.asset.copy_location.search',
677 { name => $name, owning_lib => $org } );
678 $evt = OpenILS::Event->new('ASSET_COPY_LOCATION_NOT_FOUND') unless $cl;
682 sub fetch_callnumber {
683 my( $self, $id ) = @_;
685 $logger->debug("Fetching callnumber $id");
687 my $cn = $self->simplereq(
689 'open-ils.cstore.direct.asset.call_number.retrieve', $id );
690 $evt = OpenILS::Event->new( 'ASSET_CALL_NUMBER_NOT_FOUND', id => $id ) unless $cn;
692 return ( $cn, $evt );
695 my %ORG_CACHE; # - these rarely change, so cache them..
697 my( $self, $id ) = @_;
698 return $id if( ref($id) eq 'Fieldmapper::actor::org_unit' );
699 return $ORG_CACHE{$id} if $ORG_CACHE{$id};
700 $logger->debug("Fetching org unit $id");
703 my $org = $self->simplereq(
705 'open-ils.cstore.direct.actor.org_unit.retrieve', $id );
706 $evt = OpenILS::Event->new( 'ACTOR_ORG_UNIT_NOT_FOUND', id => $id ) unless $org;
707 $ORG_CACHE{$id} = $org;
713 my( $self, $type, $id ) = @_;
715 $logger->debug("Fetching $type stat cat: $id");
716 $cat = $self->simplereq(
718 "open-ils.cstore.direct.$type.stat_cat.retrieve", $id );
720 my $e = 'ASSET_STAT_CAT_NOT_FOUND';
721 $e = 'ACTOR_STAT_CAT_NOT_FOUND' if $type eq 'actor';
723 $evt = OpenILS::Event->new( $e, id => $id ) unless $cat;
724 return ( $cat, $evt );
727 sub fetch_stat_cat_entry {
728 my( $self, $type, $id ) = @_;
730 $logger->debug("Fetching $type stat cat entry: $id");
731 $entry = $self->simplereq(
733 "open-ils.cstore.direct.$type.stat_cat_entry.retrieve", $id );
735 my $e = 'ASSET_STAT_CAT_ENTRY_NOT_FOUND';
736 $e = 'ACTOR_STAT_CAT_ENTRY_NOT_FOUND' if $type eq 'actor';
738 $evt = OpenILS::Event->new( $e, id => $id ) unless $entry;
739 return ( $entry, $evt );
744 my( $self, $org_tree, $orgid ) = @_;
745 return $org_tree if ( $org_tree->id eq $orgid );
746 return undef unless ref($org_tree->children);
747 for my $c (@{$org_tree->children}) {
748 my $o = $self->find_org($c, $orgid);
754 sub fetch_non_cat_type_by_name_and_org {
755 my( $self, $name, $orgId ) = @_;
756 $logger->debug("Fetching non cat type $name at org $orgId");
757 my $types = $self->simplereq(
759 'open-ils.cstore.direct.config.non_cataloged_type.search.atomic',
760 { name => $name, owning_lib => $orgId } );
761 return ($types->[0], undef) if($types and @$types);
762 return (undef, OpenILS::Event->new('CONFIG_NON_CATALOGED_TYPE_NOT_FOUND') );
765 sub fetch_non_cat_type {
766 my( $self, $id ) = @_;
767 $logger->debug("Fetching non cat type $id");
769 $type = $self->simplereq(
771 'open-ils.cstore.direct.config.non_cataloged_type.retrieve', $id );
772 $evt = OpenILS::Event->new('CONFIG_NON_CATALOGED_TYPE_NOT_FOUND') unless $type;
773 return ($type, $evt);
776 sub DB_UPDATE_FAILED {
777 my( $self, $payload ) = @_;
778 return OpenILS::Event->new('DATABASE_UPDATE_FAILED',
779 payload => ($payload) ? $payload : undef );
782 sub fetch_circ_duration_by_name {
783 my( $self, $name ) = @_;
785 $dur = $self->simplereq(
787 'open-ils.cstore.direct.config.rules.circ_duration.search.atomic', { name => $name } );
789 $evt = OpenILS::Event->new('CONFIG_RULES_CIRC_DURATION_NOT_FOUND') unless $dur;
793 sub fetch_recurring_fine_by_name {
794 my( $self, $name ) = @_;
796 $obj = $self->simplereq(
798 'open-ils.cstore.direct.config.rules.recuring_fine.search.atomic', { name => $name } );
800 $evt = OpenILS::Event->new('CONFIG_RULES_RECURING_FINE_NOT_FOUND') unless $obj;
804 sub fetch_max_fine_by_name {
805 my( $self, $name ) = @_;
807 $obj = $self->simplereq(
809 'open-ils.cstore.direct.config.rules.max_fine.search.atomic', { name => $name } );
811 $evt = OpenILS::Event->new('CONFIG_RULES_MAX_FINE_NOT_FOUND') unless $obj;
816 my( $self, $method, @params ) = @_;
817 return $self->simplereq(
818 'open-ils.storage', $method, @params );
822 my( $self, $method, @params ) = @_;
823 return $self->simplereq(
824 'open-ils.cstore', $method, @params );
828 my( $self, $e, $name ) = @_;
829 if( $e and ref($e) eq 'HASH' and
830 defined($e->{textcode}) and $e->{textcode} eq $name ) {
837 my( undef, $f, $l ) = caller(0);
838 my( undef, undef, undef, $s ) = caller(1);
841 $logger->debug("LOGMARK: $f:$l:$s");
845 sub fetch_open_circulation {
846 my( $self, $cid ) = @_;
849 my $circ = $self->storagereq(
850 'open-ils.storage.direct.action.open_circulation.search_where',
851 { target_copy => $cid, stop_fines_time => undef } );
852 $evt = OpenILS::Event->new('ACTION_CIRCULATION_NOT_FOUND') unless $circ;
853 return ($circ, $evt);
856 sub fetch_all_open_circulation {
857 my( $self, $cid ) = @_;
860 my $circ = $self->cstorereq(
861 'open-ils.cstore.direct.action.open_circulation.search',
862 { target_copy => $cid, xact_finish => undef } );
863 $evt = OpenILS::Event->new('ACTION_CIRCULATION_NOT_FOUND') unless $circ;
864 return ($circ, $evt);
868 sub copy_status_from_name {
869 my( $self, $name ) = @_;
870 $copy_statuses = $self->fetch_copy_statuses unless $copy_statuses;
871 for my $status (@$copy_statuses) {
872 return $status if( $status->name =~ /$name/i );
877 sub copy_status_to_name {
878 my( $self, $sid ) = @_;
879 $copy_statuses = $self->fetch_copy_statuses unless $copy_statuses;
880 for my $status (@$copy_statuses) {
881 return $status->name if( $status->id == $sid );
886 sub fetch_open_transit_by_copy {
887 my( $self, $copyid ) = @_;
889 $transit = $self->cstorereq(
890 'open-ils.cstore.direct.action.transit_copy.search_where',
891 { target_copy => $copyid, dest_recv_time => undef });
892 $evt = OpenILS::Event->new('ACTION_TRANSIT_COPY_NOT_FOUND') unless $transit;
893 return ($transit, $evt);
897 my( $self, $copy ) = @_;
898 return undef unless $copy;
899 $copy->status( $copy->status->id ) if ref($copy->status);
900 $copy->location( $copy->location->id ) if ref($copy->location);
901 $copy->circ_lib( $copy->circ_lib->id ) if ref($copy->circ_lib);
905 # un-fleshes a copy and updates it in the DB
906 # returns a DB_UPDATE_FAILED event on error
907 # returns undef on success
909 my( $self, %params ) = @_;
911 my $copy = $params{copy} || die "update_copy(): copy required";
912 my $editor = $params{editor} || die "update_copy(): copy editor required";
913 my $session = $params{session};
915 $logger->debug("Updating copy in the database: " . $copy->id);
917 $self->unflesh_copy($copy);
918 $copy->editor( $editor );
919 $copy->edit_date( 'now' );
922 my $meth = 'open-ils.storage.direct.asset.copy.update';
924 $s = $session->request( $meth, $copy )->gather(1) if $session;
925 $s = $self->storagereq( $meth, $copy ) unless $session;
927 $logger->debug("Update of copy ".$copy->id." returned: $s");
929 return $self->DB_UPDATE_FAILED($copy) unless $s;
933 sub fetch_billable_xact {
934 my( $self, $id ) = @_;
936 $logger->debug("Fetching billable transaction %id");
937 $xact = $self->cstorereq(
938 'open-ils.cstore.direct.money.billable_transaction.retrieve', $id );
939 $evt = OpenILS::Event->new('MONEY_BILLABLE_TRANSACTION_NOT_FOUND') unless $xact;
940 return ($xact, $evt);
944 sub fetch_fleshed_copy {
945 my( $self, $id ) = @_;
947 $logger->info("Fetching fleshed copy $id");
948 $copy = $self->cstorereq(
949 "open-ils.cstore.fleshed.asset.copy.retrieve", $id );
950 $evt = OpenILS::Event->new('ASSET_COPY_NOT_FOUND', id => $id) unless $copy;
951 return ($copy, $evt);
955 # returns the org that owns the callnumber that the copy
957 sub fetch_copy_owner {
958 my( $self, $copyid ) = @_;
959 my( $copy, $cn, $evt );
960 $logger->debug("Fetching copy owner $copyid");
961 ($copy, $evt) = $self->fetch_copy($copyid);
962 return (undef,$evt) if $evt;
963 ($cn, $evt) = $self->fetch_callnumber($copy->call_number);
964 return (undef,$evt) if $evt;
965 return ($cn->owning_lib);
968 sub fetch_copy_note {
969 my( $self, $id ) = @_;
971 $logger->debug("Fetching copy note $id");
972 $note = $self->cstorereq(
973 'open-ils.cstore.direct.asset.copy_note.retrieve', $id );
974 $evt = OpenILS::Event->new('ASSET_COPY_NOTE_NOT_FOUND', id => $id ) unless $note;
975 return ($note, $evt);
978 sub fetch_call_numbers_by_title {
979 my( $self, $titleid ) = @_;
980 $logger->info("Fetching call numbers by title $titleid");
981 return $self->cstorereq(
982 'open-ils.cstore.direct.asset.call_number.search.atomic',
983 { record => $titleid, deleted => 'f' });
984 #'open-ils.storage.direct.asset.call_number.search.record.atomic', $titleid);
987 sub fetch_copies_by_call_number {
988 my( $self, $cnid ) = @_;
989 $logger->info("Fetching copies by call number $cnid");
990 return $self->cstorereq(
991 'open-ils.cstore.direct.asset.copy.search.atomic', { call_number => $cnid, deleted => 'f' } );
992 #'open-ils.storage.direct.asset.copy.search.call_number.atomic', $cnid );
995 sub fetch_user_by_barcode {
996 my( $self, $bc ) = @_;
997 my $cardid = $self->cstorereq(
998 'open-ils.cstore.id_list.actor.card.search', { barcode => $bc } );
999 return (undef, OpenILS::Event->new('ACTOR_CARD_NOT_FOUND', barcode => $bc)) unless $cardid;
1000 my $user = $self->cstorereq(
1001 'open-ils.cstore.direct.actor.user.search', { card => $cardid } );
1002 return (undef, OpenILS::Event->new('ACTOR_USER_NOT_FOUND', card => $cardid)) unless $user;
1008 # ---------------------------------------------------------------------
1009 # Updates and returns the patron penalties
1010 # ---------------------------------------------------------------------
1011 sub update_patron_penalties {
1012 my( $self, %args ) = @_;
1013 return $self->simplereq(
1015 'open-ils.penalty.patron_penalty.calculate',
1016 { update => 1, %args }
1021 my( $self, $billid ) = @_;
1022 $logger->debug("Fetching billing $billid");
1023 my $bill = $self->cstorereq(
1024 'open-ils.cstore.direct.money.billing.retrieve', $billid );
1025 my $evt = OpenILS::Event->new('MONEY_BILLING_NOT_FOUND') unless $bill;
1026 return($bill, $evt);
1032 sub fetch_org_tree {
1034 return $ORG_TREE if $ORG_TREE;
1035 return $ORG_TREE = OpenILS::Utils::CStoreEditor->new->search_actor_org_unit(
1037 {"parent_ou" => undef },
1040 flesh_fields => { aou => ['children'] },
1041 order_by => { aou => 'name'}
1048 my( $self, $node, $callback ) = @_;
1049 return unless $node;
1051 if( $node->children ) {
1052 $self->walk_org_tree($_, $callback) for @{$node->children};