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.cstore.direct.actor.org_unit.search.atomic",
221 { id => { '!=' => undef } }
225 if( ! $org_typelist ) {
226 warn "Retrieving org types\n";
227 $org_typelist = $self->simple_scalar_request(
229 "open-ils.cstore.direct.actor.org_unit_type.search.atomic",
230 { id => { '!=' => undef } }
232 $self->build_org_type($org_typelist);
235 $tree = $self->build_org_tree($orglist,1);
236 $cache_client->new()->put_cache('_orgtree', $tree);
241 my $slimtree = undef;
242 sub get_slim_org_tree {
245 if($slimtree) { return $slimtree; }
247 # see if it's in the cache
248 $slimtree = $cache_client->new()->get_cache('slimorgtree');
249 if($slimtree) { return $slimtree; }
252 warn "Retrieving Org Tree\n";
253 $orglist = $self->simple_scalar_request(
255 "open-ils.cstore.direct.actor.org_unit.search.atomic",
256 { id => { '!=' => undef } }
260 $slimtree = $self->build_org_tree($orglist);
261 $cache_client->new->put_cache('slimorgtree', $slimtree);
268 my($self, $org_typelist) = @_;
269 for my $type (@$org_typelist) {
270 $org_typelist_hash->{$type->id()} = $type;
278 my( $self, $orglist, $add_types ) = @_;
280 return $orglist unless (
281 ref($orglist) and @$orglist > 1 );
284 $a->ou_type <=> $b->ou_type ||
285 $a->name cmp $b->name } @$orglist;
287 for my $org (@list) {
291 if(!ref($org->ou_type()) and $add_types) {
292 $org->ou_type( $org_typelist_hash->{$org->ou_type()});
295 next unless (defined($org->parent_ou));
297 my ($parent) = grep { $_->id == $org->parent_ou } @list;
299 $parent->children([]) unless defined($parent->children);
300 push( @{$parent->children}, $org );
307 sub fetch_closed_date {
308 my( $self, $cd ) = @_;
311 $logger->debug("Fetching closed_date $cd from cstore");
313 my $cd_obj = $self->simplereq(
315 'open-ils.cstore.direct.actor.org_unit.closed_date.retrieve', $cd );
318 $logger->info("closed_date $cd not found in the db");
319 $evt = OpenILS::Event->new('ACTOR_USER_NOT_FOUND');
322 return ($cd_obj, $evt);
326 my( $self, $userid ) = @_;
329 $logger->debug("Fetching user $userid from cstore");
331 $user = $self->simplereq(
333 'open-ils.cstore.direct.actor.user.retrieve', $userid );
336 $logger->info("User $userid not found in the db");
337 $evt = OpenILS::Event->new('ACTOR_USER_NOT_FOUND');
340 return ($user, $evt);
344 my( $self, $session ) = @_;
345 my $user; my $evt; my $e;
347 $logger->debug("Checking user session $session");
350 $user = $self->check_user_session($session);
351 } catch Error with { $e = 1; };
353 $logger->debug("Done checking user session $session " . (($e) ? "error = $e" : "") );
355 if( $e or !$user ) { $evt = OpenILS::Event->new('NO_SESSION'); }
356 return ( $user, $evt );
360 # verifiese the session and checks the permissions agains the
361 # session user and the user's home_ou as the org id
363 my( $self, $session, @perms ) = @_;
364 my $user; my $evt; my $e;
365 $logger->debug("Checking user session $session and perms @perms");
366 ($user, $evt) = $self->checkses($session);
367 return (undef, $evt) if $evt;
368 $evt = $self->check_perms($user->id, $user->home_ou, @perms);
369 return ($user, $evt);
374 my( $self, $staffobj, $userid, @perms ) = @_;
376 $userid = $staffobj->id unless defined $userid;
378 $logger->debug("checkrequestor(): requestor => " . $staffobj->id . ", target => $userid");
380 if( $userid ne $staffobj->id ) {
381 ($user, $evt) = $self->fetch_user($userid);
382 return (undef, $evt) if $evt;
383 $evt = $self->check_perms( $staffobj->id, $user->home_ou, @perms );
389 return ($user, $evt);
392 sub checkses_requestor {
393 my( $self, $authtoken, $targetid, @perms ) = @_;
394 my( $requestor, $target, $evt );
396 ($requestor, $evt) = $self->checkses($authtoken);
397 return (undef, undef, $evt) if $evt;
399 ($target, $evt) = $self->checkrequestor( $requestor, $targetid, @perms );
400 return( $requestor, $target, $evt);
404 my( $self, $copyid ) = @_;
407 $logger->debug("Fetching copy $copyid from cstore");
409 $copy = $self->simplereq(
411 'open-ils.cstore.direct.asset.copy.retrieve', $copyid );
413 if(!$copy) { $evt = OpenILS::Event->new('ASSET_COPY_NOT_FOUND'); }
415 return( $copy, $evt );
419 # retrieves a circ object by id
420 sub fetch_circulation {
421 my( $self, $circid ) = @_;
424 $logger->debug("Fetching circ $circid from cstore");
426 $circ = $self->simplereq(
428 "open-ils.cstore.direct.action.circulation.retrieve", $circid );
431 $evt = OpenILS::Event->new('ACTION_CIRCULATION_NOT_FOUND', circid => $circid );
434 return ( $circ, $evt );
437 sub fetch_record_by_copy {
438 my( $self, $copyid ) = @_;
441 $logger->debug("Fetching record by copy $copyid from cstore");
443 $record = $self->simplereq(
445 'open-ils.cstore.direct.asset.copy.retrieve', $copyid,
447 flesh_fields => { bre => [ 'fixed_fields' ],
449 acp => [ 'call_number' ],
455 $evt = OpenILS::Event->new('BIBLIO_RECORD_ENTRY_NOT_FOUND');
457 $record = $record->call_number->record;
460 return ($record, $evt);
463 # turns a record object into an mvr (mods) object
465 my( $self, $record ) = @_;
466 my $u = OpenILS::Utils::ModsParser->new();
467 $u->start_mods_batch( $record->marc );
468 my $mods = $u->finish_mods_batch();
469 $mods->doc_id($record->id);
474 my( $self, $holdid ) = @_;
477 $logger->debug("Fetching hold $holdid from cstore");
479 $hold = $self->simplereq(
481 'open-ils.cstore.direct.action.hold_request.retrieve', $holdid);
483 $evt = OpenILS::Event->new('ACTION_HOLD_REQUEST_NOT_FOUND', holdid => $holdid) unless $hold;
485 return ($hold, $evt);
489 sub fetch_hold_transit_by_hold {
490 my( $self, $holdid ) = @_;
491 my( $transit, $evt );
493 $logger->debug("Fetching transit by hold $holdid from cstore");
495 $transit = $self->simplereq(
497 'open-ils.cstore.direct.action.hold_transit_copy.search', { hold => $holdid } );
499 $evt = OpenILS::Event->new('ACTION_HOLD_TRANSIT_COPY_NOT_FOUND', holdid => $holdid) unless $transit;
501 return ($transit, $evt );
504 # fetches the captured, but not fulfilled hold attached to a given copy
505 sub fetch_open_hold_by_copy {
506 my( $self, $copyid ) = @_;
507 $logger->debug("Searching for active hold for copy $copyid");
510 $hold = $self->cstorereq(
511 'open-ils.cstore.direct.action.hold_request.search',
513 current_copy => $copyid ,
514 capture_time => { "!=" => undef },
515 fulfillment_time => undef
518 $evt = OpenILS::Event->new('ACTION_HOLD_REQUEST_NOT_FOUND', copyid => $copyid) unless $hold;
519 return ($hold, $evt);
522 sub fetch_hold_transit {
523 my( $self, $transid ) = @_;
524 my( $htransit, $evt );
525 $logger->debug("Fetching hold transit with hold id $transid");
526 $htransit = $self->cstorereq(
527 'open-ils.cstore.direct.action.hold_transit_copy.retrieve', $transid );
528 $evt = OpenILS::Event->new('ACTION_HOLD_TRANSIT_COPY_NOT_FOUND', id => $transid) unless $htransit;
529 return ($htransit, $evt);
532 sub fetch_copy_by_barcode {
533 my( $self, $barcode ) = @_;
536 $logger->debug("Fetching copy by barcode $barcode from cstore");
538 $copy = $self->simplereq( 'open-ils.cstore',
539 'open-ils.cstore.direct.asset.copy.search', { barcode => $barcode, deleted => 'f'} );
540 #'open-ils.storage.direct.asset.copy.search.barcode', $barcode );
542 $evt = OpenILS::Event->new('ASSET_COPY_NOT_FOUND', barcode => $barcode) unless $copy;
544 return ($copy, $evt);
547 sub fetch_open_billable_transaction {
548 my( $self, $transid ) = @_;
549 my( $transaction, $evt );
551 $logger->debug("Fetching open billable transaction $transid from cstore");
553 $transaction = $self->simplereq(
555 'open-ils.cstore.direct.money.open_billable_transaction_summary.retrieve', $transid);
557 $evt = OpenILS::Event->new(
558 'MONEY_OPEN_BILLABLE_TRANSACTION_SUMMARY_NOT_FOUND', transid => $transid ) unless $transaction;
560 return ($transaction, $evt);
566 $buckets{'biblio'} = 'biblio_record_entry_bucket';
567 $buckets{'callnumber'} = 'call_number_bucket';
568 $buckets{'copy'} = 'copy_bucket';
569 $buckets{'user'} = 'user_bucket';
571 sub fetch_container {
572 my( $self, $id, $type ) = @_;
575 $logger->debug("Fetching container $id with type $type");
577 my $e = 'CONTAINER_CALL_NUMBER_BUCKET_NOT_FOUND';
578 $e = 'CONTAINER_BIBLIO_RECORD_ENTRY_BUCKET_NOT_FOUND' if $type eq 'biblio';
579 $e = 'CONTAINER_USER_BUCKET_NOT_FOUND' if $type eq 'user';
580 $e = 'CONTAINER_COPY_BUCKET_NOT_FOUND' if $type eq 'copy';
582 my $meth = $buckets{$type};
583 $bucket = $self->simplereq(
585 "open-ils.cstore.direct.container.$meth.retrieve", $id );
587 $evt = OpenILS::Event->new(
588 $e, container => $id, container_type => $type ) unless $bucket;
590 return ($bucket, $evt);
594 sub fetch_container_item {
595 my( $self, $id, $type ) = @_;
598 $logger->debug("Fetching container item $id with type $type");
600 my $meth = $buckets{$type} . "_item";
602 $bucket = $self->simplereq(
604 "open-ils.cstore.direct.container.$meth.retrieve", $id );
607 my $e = 'CONTAINER_CALL_NUMBER_BUCKET_ITEM_NOT_FOUND';
608 $e = 'CONTAINER_BIBLIO_RECORD_ENTRY_BUCKET_ITEM_NOT_FOUND' if $type eq 'biblio';
609 $e = 'CONTAINER_USER_BUCKET_ITEM_NOT_FOUND' if $type eq 'user';
610 $e = 'CONTAINER_COPY_BUCKET_ITEM_NOT_FOUND' if $type eq 'copy';
613 $evt = OpenILS::Event->new(
614 $e, itemid => $id, container_type => $type ) unless $bucket;
616 return ($bucket, $evt);
620 sub fetch_patron_standings {
622 $logger->debug("Fetching patron standings");
623 return $self->simplereq(
625 'open-ils.cstore.direct.config.standing.search.atomic', { id => { '!=' => undef } });
629 sub fetch_permission_group_tree {
631 $logger->debug("Fetching patron profiles");
632 return $self->simplereq(
634 'open-ils.actor.groups.tree.retrieve' );
638 sub fetch_patron_circ_summary {
639 my( $self, $userid ) = @_;
640 $logger->debug("Fetching patron summary for $userid");
641 my $summary = $self->simplereq(
643 "open-ils.storage.action.circulation.patron_summary", $userid );
647 $summary->[1] ||= 0.0;
654 sub fetch_copy_statuses {
656 $logger->debug("Fetching copy statuses");
657 return $self->simplereq(
659 'open-ils.cstore.direct.config.copy_status.search.atomic', { id => { '!=' => undef } });
662 sub fetch_copy_location {
663 my( $self, $id ) = @_;
665 my $cl = $self->cstorereq(
666 'open-ils.cstore.direct.asset.copy_location.retrieve', $id );
667 $evt = OpenILS::Event->new('ASSET_COPY_LOCATION_NOT_FOUND') unless $cl;
671 sub fetch_copy_locations {
673 return $self->simplereq(
675 'open-ils.cstore.direct.asset.copy_location.search.atomic', { id => { '!=' => undef } });
678 sub fetch_copy_location_by_name {
679 my( $self, $name, $org ) = @_;
681 my $cl = $self->cstorereq(
682 'open-ils.cstore.direct.asset.copy_location.search',
683 { name => $name, owning_lib => $org } );
684 $evt = OpenILS::Event->new('ASSET_COPY_LOCATION_NOT_FOUND') unless $cl;
688 sub fetch_callnumber {
689 my( $self, $id ) = @_;
691 $logger->debug("Fetching callnumber $id");
693 my $cn = $self->simplereq(
695 'open-ils.cstore.direct.asset.call_number.retrieve', $id );
696 $evt = OpenILS::Event->new( 'ASSET_CALL_NUMBER_NOT_FOUND', id => $id ) unless $cn;
698 return ( $cn, $evt );
701 my %ORG_CACHE; # - these rarely change, so cache them..
703 my( $self, $id ) = @_;
704 return $id if( ref($id) eq 'Fieldmapper::actor::org_unit' );
705 return $ORG_CACHE{$id} if $ORG_CACHE{$id};
706 $logger->debug("Fetching org unit $id");
709 my $org = $self->simplereq(
711 'open-ils.cstore.direct.actor.org_unit.retrieve', $id );
712 $evt = OpenILS::Event->new( 'ACTOR_ORG_UNIT_NOT_FOUND', id => $id ) unless $org;
713 $ORG_CACHE{$id} = $org;
719 my( $self, $type, $id ) = @_;
721 $logger->debug("Fetching $type stat cat: $id");
722 $cat = $self->simplereq(
724 "open-ils.cstore.direct.$type.stat_cat.retrieve", $id );
726 my $e = 'ASSET_STAT_CAT_NOT_FOUND';
727 $e = 'ACTOR_STAT_CAT_NOT_FOUND' if $type eq 'actor';
729 $evt = OpenILS::Event->new( $e, id => $id ) unless $cat;
730 return ( $cat, $evt );
733 sub fetch_stat_cat_entry {
734 my( $self, $type, $id ) = @_;
736 $logger->debug("Fetching $type stat cat entry: $id");
737 $entry = $self->simplereq(
739 "open-ils.cstore.direct.$type.stat_cat_entry.retrieve", $id );
741 my $e = 'ASSET_STAT_CAT_ENTRY_NOT_FOUND';
742 $e = 'ACTOR_STAT_CAT_ENTRY_NOT_FOUND' if $type eq 'actor';
744 $evt = OpenILS::Event->new( $e, id => $id ) unless $entry;
745 return ( $entry, $evt );
750 my( $self, $org_tree, $orgid ) = @_;
751 return $org_tree if ( $org_tree->id eq $orgid );
752 return undef unless ref($org_tree->children);
753 for my $c (@{$org_tree->children}) {
754 my $o = $self->find_org($c, $orgid);
760 sub fetch_non_cat_type_by_name_and_org {
761 my( $self, $name, $orgId ) = @_;
762 $logger->debug("Fetching non cat type $name at org $orgId");
763 my $types = $self->simplereq(
765 'open-ils.cstore.direct.config.non_cataloged_type.search.atomic',
766 { name => $name, owning_lib => $orgId } );
767 return ($types->[0], undef) if($types and @$types);
768 return (undef, OpenILS::Event->new('CONFIG_NON_CATALOGED_TYPE_NOT_FOUND') );
771 sub fetch_non_cat_type {
772 my( $self, $id ) = @_;
773 $logger->debug("Fetching non cat type $id");
775 $type = $self->simplereq(
777 'open-ils.cstore.direct.config.non_cataloged_type.retrieve', $id );
778 $evt = OpenILS::Event->new('CONFIG_NON_CATALOGED_TYPE_NOT_FOUND') unless $type;
779 return ($type, $evt);
782 sub DB_UPDATE_FAILED {
783 my( $self, $payload ) = @_;
784 return OpenILS::Event->new('DATABASE_UPDATE_FAILED',
785 payload => ($payload) ? $payload : undef );
788 sub fetch_circ_duration_by_name {
789 my( $self, $name ) = @_;
791 $dur = $self->simplereq(
793 'open-ils.cstore.direct.config.rules.circ_duration.search.atomic', { name => $name } );
795 $evt = OpenILS::Event->new('CONFIG_RULES_CIRC_DURATION_NOT_FOUND') unless $dur;
799 sub fetch_recurring_fine_by_name {
800 my( $self, $name ) = @_;
802 $obj = $self->simplereq(
804 'open-ils.cstore.direct.config.rules.recuring_fine.search.atomic', { name => $name } );
806 $evt = OpenILS::Event->new('CONFIG_RULES_RECURING_FINE_NOT_FOUND') unless $obj;
810 sub fetch_max_fine_by_name {
811 my( $self, $name ) = @_;
813 $obj = $self->simplereq(
815 'open-ils.cstore.direct.config.rules.max_fine.search.atomic', { name => $name } );
817 $evt = OpenILS::Event->new('CONFIG_RULES_MAX_FINE_NOT_FOUND') unless $obj;
822 my( $self, $method, @params ) = @_;
823 return $self->simplereq(
824 'open-ils.storage', $method, @params );
828 my( $self, $method, @params ) = @_;
829 return $self->simplereq(
830 'open-ils.cstore', $method, @params );
834 my( $self, $e, $name ) = @_;
835 if( $e and ref($e) eq 'HASH' and
836 defined($e->{textcode}) and $e->{textcode} eq $name ) {
843 my( undef, $f, $l ) = caller(0);
844 my( undef, undef, undef, $s ) = caller(1);
847 $logger->debug("LOGMARK: $f:$l:$s");
851 sub fetch_open_circulation {
852 my( $self, $cid ) = @_;
855 my $circ = $self->cstorereq(
856 'open-ils.cstore.direct.action.open_circulation.search',
857 { target_copy => $cid, stop_fines_time => undef } );
858 $evt = OpenILS::Event->new('ACTION_CIRCULATION_NOT_FOUND') unless $circ;
859 return ($circ, $evt);
862 sub fetch_all_open_circulation {
863 my( $self, $cid ) = @_;
866 my $circ = $self->cstorereq(
867 'open-ils.cstore.direct.action.open_circulation.search',
868 { target_copy => $cid, xact_finish => undef } );
869 $evt = OpenILS::Event->new('ACTION_CIRCULATION_NOT_FOUND') unless $circ;
870 return ($circ, $evt);
874 sub copy_status_from_name {
875 my( $self, $name ) = @_;
876 $copy_statuses = $self->fetch_copy_statuses unless $copy_statuses;
877 for my $status (@$copy_statuses) {
878 return $status if( $status->name =~ /$name/i );
883 sub copy_status_to_name {
884 my( $self, $sid ) = @_;
885 $copy_statuses = $self->fetch_copy_statuses unless $copy_statuses;
886 for my $status (@$copy_statuses) {
887 return $status->name if( $status->id == $sid );
892 sub fetch_open_transit_by_copy {
893 my( $self, $copyid ) = @_;
895 $transit = $self->cstorereq(
896 'open-ils.cstore.direct.action.transit_copy.search',
897 { target_copy => $copyid, dest_recv_time => undef });
898 $evt = OpenILS::Event->new('ACTION_TRANSIT_COPY_NOT_FOUND') unless $transit;
899 return ($transit, $evt);
903 my( $self, $copy ) = @_;
904 return undef unless $copy;
905 $copy->status( $copy->status->id ) if ref($copy->status);
906 $copy->location( $copy->location->id ) if ref($copy->location);
907 $copy->circ_lib( $copy->circ_lib->id ) if ref($copy->circ_lib);
911 # un-fleshes a copy and updates it in the DB
912 # returns a DB_UPDATE_FAILED event on error
913 # returns undef on success
915 my( $self, %params ) = @_;
917 my $copy = $params{copy} || die "update_copy(): copy required";
918 my $editor = $params{editor} || die "update_copy(): copy editor required";
919 my $session = $params{session};
921 $logger->debug("Updating copy in the database: " . $copy->id);
923 $self->unflesh_copy($copy);
924 $copy->editor( $editor );
925 $copy->edit_date( 'now' );
928 my $meth = 'open-ils.storage.direct.asset.copy.update';
930 $s = $session->request( $meth, $copy )->gather(1) if $session;
931 $s = $self->storagereq( $meth, $copy ) unless $session;
933 $logger->debug("Update of copy ".$copy->id." returned: $s");
935 return $self->DB_UPDATE_FAILED($copy) unless $s;
939 sub fetch_billable_xact {
940 my( $self, $id ) = @_;
942 $logger->debug("Fetching billable transaction %id");
943 $xact = $self->cstorereq(
944 'open-ils.cstore.direct.money.billable_transaction.retrieve', $id );
945 $evt = OpenILS::Event->new('MONEY_BILLABLE_TRANSACTION_NOT_FOUND') unless $xact;
946 return ($xact, $evt);
950 sub fetch_fleshed_copy {
951 my( $self, $id ) = @_;
953 $logger->info("Fetching fleshed copy $id");
954 $copy = $self->cstorereq(
955 "open-ils.cstore.direct.asset.copy.retrieve", $id,
957 flesh_fields => { acp => [ qw/ circ_lib location status stat_cat_entries / ] }
960 $evt = OpenILS::Event->new('ASSET_COPY_NOT_FOUND', id => $id) unless $copy;
961 return ($copy, $evt);
965 # returns the org that owns the callnumber that the copy
967 sub fetch_copy_owner {
968 my( $self, $copyid ) = @_;
969 my( $copy, $cn, $evt );
970 $logger->debug("Fetching copy owner $copyid");
971 ($copy, $evt) = $self->fetch_copy($copyid);
972 return (undef,$evt) if $evt;
973 ($cn, $evt) = $self->fetch_callnumber($copy->call_number);
974 return (undef,$evt) if $evt;
975 return ($cn->owning_lib);
978 sub fetch_copy_note {
979 my( $self, $id ) = @_;
981 $logger->debug("Fetching copy note $id");
982 $note = $self->cstorereq(
983 'open-ils.cstore.direct.asset.copy_note.retrieve', $id );
984 $evt = OpenILS::Event->new('ASSET_COPY_NOTE_NOT_FOUND', id => $id ) unless $note;
985 return ($note, $evt);
988 sub fetch_call_numbers_by_title {
989 my( $self, $titleid ) = @_;
990 $logger->info("Fetching call numbers by title $titleid");
991 return $self->cstorereq(
992 'open-ils.cstore.direct.asset.call_number.search.atomic',
993 { record => $titleid, deleted => 'f' });
994 #'open-ils.storage.direct.asset.call_number.search.record.atomic', $titleid);
997 sub fetch_copies_by_call_number {
998 my( $self, $cnid ) = @_;
999 $logger->info("Fetching copies by call number $cnid");
1000 return $self->cstorereq(
1001 'open-ils.cstore.direct.asset.copy.search.atomic', { call_number => $cnid, deleted => 'f' } );
1002 #'open-ils.storage.direct.asset.copy.search.call_number.atomic', $cnid );
1005 sub fetch_user_by_barcode {
1006 my( $self, $bc ) = @_;
1007 my $cardid = $self->cstorereq(
1008 'open-ils.cstore.direct.actor.card.id_list', { barcode => $bc } );
1009 return (undef, OpenILS::Event->new('ACTOR_CARD_NOT_FOUND', barcode => $bc)) unless $cardid;
1010 my $user = $self->cstorereq(
1011 'open-ils.cstore.direct.actor.user.search', { card => $cardid } );
1012 return (undef, OpenILS::Event->new('ACTOR_USER_NOT_FOUND', card => $cardid)) unless $user;
1018 # ---------------------------------------------------------------------
1019 # Updates and returns the patron penalties
1020 # ---------------------------------------------------------------------
1021 sub update_patron_penalties {
1022 my( $self, %args ) = @_;
1023 return $self->simplereq(
1025 'open-ils.penalty.patron_penalty.calculate',
1026 { update => 1, %args }
1031 my( $self, $billid ) = @_;
1032 $logger->debug("Fetching billing $billid");
1033 my $bill = $self->cstorereq(
1034 'open-ils.cstore.direct.money.billing.retrieve', $billid );
1035 my $evt = OpenILS::Event->new('MONEY_BILLING_NOT_FOUND') unless $bill;
1036 return($bill, $evt);
1042 sub fetch_org_tree {
1044 return $ORG_TREE if $ORG_TREE;
1045 return $ORG_TREE = OpenILS::Utils::CStoreEditor->new->search_actor_org_unit(
1047 {"parent_ou" => undef },
1050 flesh_fields => { aou => ['children'] },
1051 order_by => { aou => 'name'}
1058 my( $self, $node, $callback ) = @_;
1059 return unless $node;
1061 if( $node->children ) {
1062 $self->walk_org_tree($_, $callback) for @{$node->children};