1 package OpenILS::Application::AppUtils;
2 use strict; use warnings;
3 use base qw/OpenSRF::Application/;
4 use OpenSRF::Utils::Cache;
5 use OpenSRF::EX qw(:try);
7 use OpenSRF::Utils::Logger;
8 use OpenILS::Utils::ModsParser;
11 my $logger = "OpenSRF::Utils::Logger";
14 my $cache_client = "OpenSRF::Utils::Cache";
16 my $storage_session = undef;
18 # ---------------------------------------------------------------------------
19 # Pile of utilty methods used accross applications.
20 # ---------------------------------------------------------------------------
23 # ---------------------------------------------------------------------------
24 # on sucess, returns the created session, on failure throws ERROR exception
25 # ---------------------------------------------------------------------------
26 sub start_db_session {
29 my $session = OpenSRF::AppSession->connect( "open-ils.storage" );
30 my $trans_req = $session->request( "open-ils.storage.transaction.begin" );
32 my $trans_resp = $trans_req->recv();
33 if(ref($trans_resp) and UNIVERSAL::isa($trans_resp,"Error")) { throw $trans_resp; }
34 if( ! $trans_resp->content() ) {
36 ("Unable to Begin Transaction with database" );
40 $logger->debug("Setting global storage session to ".
41 "session: " . $session->session_id . " : " . $session->app );
43 $storage_session = $session;
48 # returns undef if user has all of the perms provided
49 # returns the first failed perm on failure
50 sub check_user_perms {
51 my($self, $user_id, $org_id, @perm_types ) = @_;
52 $logger->debug("Checking perms with user : $user_id , org: $org_id, @perm_types");
53 for my $type (@perm_types) {
54 return $type unless ($self->storagereq(
55 "open-ils.storage.permission.user_has_perm",
56 $user_id, $type, $org_id ));
61 # checks the list of user perms. The first one that fails returns a new
62 # OpenILS::Perm object of that type. Returns undef if all perms are allowed
64 my( $self, $user_id, $org_id, @perm_types ) = @_;
65 my $t = $self->check_user_perms( $user_id, $org_id, @perm_types );
66 return OpenILS::Event->new('PERM_FAILURE', ilsperm => $t, ilspermloc => $org_id ) if $t;
72 # ---------------------------------------------------------------------------
73 # commits and destroys the session
74 # ---------------------------------------------------------------------------
75 sub commit_db_session {
76 my( $self, $session ) = @_;
78 my $req = $session->request( "open-ils.storage.transaction.commit" );
79 my $resp = $req->recv();
82 throw OpenSRF::EX::ERROR ("Unable to commit db session");
85 if(UNIVERSAL::isa($resp,"Error")) {
86 throw $resp ($resp->stringify);
90 throw OpenSRF::EX::ERROR ("Unable to commit db session");
94 $session->disconnect();
96 $storage_session = undef;
99 sub rollback_db_session {
100 my( $self, $session ) = @_;
102 my $req = $session->request("open-ils.storage.transaction.rollback");
103 my $resp = $req->recv();
104 if(UNIVERSAL::isa($resp,"Error")) { throw $resp; }
107 $session->disconnect();
109 $storage_session = undef;
113 # returns undef it the event is not an ILS event
114 # returns the event code otherwise
116 my( $self, $evt ) = @_;
117 return $evt->{ilsevent} if( ref($evt) eq 'HASH' and defined($evt->{ilsevent})) ;
121 # ---------------------------------------------------------------------------
122 # Checks to see if a user is logged in. Returns the user record on success,
123 # throws an exception on error.
124 # ---------------------------------------------------------------------------
125 sub check_user_session {
127 my( $self, $user_session ) = @_;
129 my $content = $self->simplereq(
131 'open-ils.auth.session.retrieve', $user_session );
134 if(! $content or $self->event_code($content)) {
135 throw OpenSRF::EX::ERROR
136 ("Session [$user_session] cannot be authenticated" );
139 $logger->debug("Fetch user session $user_session found user " . $content->id );
144 # generic simple request returning a scalar value
146 my($self, $service, $method, @params) = @_;
147 return $self->simple_scalar_request($service, $method, @params);
150 sub get_storage_session {
152 return undef; # XXX testing
154 if( $storage_session and
155 $storage_session->connected and
156 $storage_session->transport_connected and
157 $storage_session->app eq 'open-ils.storage' ) {
159 $logger->debug("get_storage_session(): returning existing session");
160 return $storage_session;
162 $logger->debug("get_storage_session(): returning undef");
163 $storage_session = undef;
168 sub simple_scalar_request {
169 my($self, $service, $method, @params) = @_;
173 if( $service eq 'open-ils.storage' ) {
174 if( $session = get_storage_session() ) {
175 $logger->debug("simple request using existing storage session ".$session->session_id);
176 } else { $session = undef; }
179 $session = OpenSRF::AppSession->create( $service ) unless $session;
181 my $request = $session->request( $method, @params );
187 $val = $request->gather(1);
194 warn "received error : service=$service : method=$method : params=".Dumper(\@params) . "\n $err";
195 throw $err ("Call to $service for method $method \n failed with exception: $err : " );
207 my $org_typelist = undef;
208 my $org_typelist_hash = {};
213 if($tree) { return $tree; }
215 # see if it's in the cache
216 $tree = $cache_client->new()->get_cache('_orgtree');
217 if($tree) { return $tree; }
220 warn "Retrieving Org Tree\n";
221 $orglist = $self->simple_scalar_request(
223 "open-ils.storage.direct.actor.org_unit.retrieve.all.atomic" );
226 if( ! $org_typelist ) {
227 warn "Retrieving org types\n";
228 $org_typelist = $self->simple_scalar_request(
230 "open-ils.storage.direct.actor.org_unit_type.retrieve.all.atomic" );
231 $self->build_org_type($org_typelist);
234 $tree = $self->build_org_tree($orglist,1);
235 $cache_client->new()->put_cache('_orgtree', $tree);
240 my $slimtree = undef;
241 sub get_slim_org_tree {
244 if($slimtree) { return $slimtree; }
246 # see if it's in the cache
247 $slimtree = $cache_client->new()->get_cache('slimorgtree');
248 if($slimtree) { return $slimtree; }
251 warn "Retrieving Org Tree\n";
252 $orglist = $self->simple_scalar_request(
254 "open-ils.storage.direct.actor.org_unit.retrieve.all.atomic" );
257 $slimtree = $self->build_org_tree($orglist);
258 $cache_client->new->put_cache('slimorgtree', $slimtree);
265 my($self, $org_typelist) = @_;
266 for my $type (@$org_typelist) {
267 $org_typelist_hash->{$type->id()} = $type;
275 my( $self, $orglist, $add_types ) = @_;
277 return $orglist unless (
278 ref($orglist) and @$orglist > 1 );
281 $a->ou_type <=> $b->ou_type ||
282 $a->name cmp $b->name } @$orglist;
284 for my $org (@list) {
288 if(!ref($org->ou_type()) and $add_types) {
289 $org->ou_type( $org_typelist_hash->{$org->ou_type()});
292 next unless (defined($org->parent_ou));
294 my ($parent) = grep { $_->id == $org->parent_ou } @list;
296 $parent->children([]) unless defined($parent->children);
297 push( @{$parent->children}, $org );
304 sub fetch_closed_date {
305 my( $self, $cd ) = @_;
308 $logger->debug("Fetching closed_date $cd from storage");
310 my $cd_obj = $self->simplereq(
312 'open-ils.storage.direct.actor.org_unit.closed_date.retrieve', $cd );
315 $logger->info("closed_date $cd not found in the db");
316 $evt = OpenILS::Event->new('ACTOR_USER_NOT_FOUND');
319 return ($cd_obj, $evt);
323 my( $self, $userid ) = @_;
326 $logger->debug("Fetching user $userid from storage");
328 $user = $self->simplereq(
330 'open-ils.storage.direct.actor.user.retrieve', $userid );
333 $logger->info("User $userid not found in the db");
334 $evt = OpenILS::Event->new('ACTOR_USER_NOT_FOUND');
337 return ($user, $evt);
341 my( $self, $session ) = @_;
342 my $user; my $evt; my $e;
344 $logger->debug("Checking user session $session");
347 $user = $self->check_user_session($session);
348 } catch Error with { $e = 1; };
350 if( $e or !$user ) { $evt = OpenILS::Event->new('NO_SESSION'); }
351 return ( $user, $evt );
355 # verifiese the session and checks the permissions agains the
356 # session user and the user's home_ou as the org id
358 my( $self, $session, @perms ) = @_;
359 my $user; my $evt; my $e;
360 $logger->debug("Checking user session $session and perms @perms");
361 ($user, $evt) = $self->checkses($session);
362 return (undef, $evt) if $evt;
363 $evt = $self->check_perms($user->id, $user->home_ou, @perms);
364 return ($user, $evt);
369 my( $self, $staffobj, $userid, @perms ) = @_;
371 $userid = $staffobj->id unless defined $userid;
373 $logger->debug("checkrequestor(): requestor => " . $staffobj->id . ", target => $userid");
375 if( $userid ne $staffobj->id ) {
376 ($user, $evt) = $self->fetch_user($userid);
377 return (undef, $evt) if $evt;
378 $evt = $self->check_perms( $staffobj->id, $user->home_ou, @perms );
384 return ($user, $evt);
387 sub checkses_requestor {
388 my( $self, $authtoken, $targetid, @perms ) = @_;
389 my( $requestor, $target, $evt );
391 ($requestor, $evt) = $self->checkses($authtoken);
392 return (undef, undef, $evt) if $evt;
394 ($target, $evt) = $self->checkrequestor( $requestor, $targetid, @perms );
395 return( $requestor, $target, $evt);
399 my( $self, $copyid ) = @_;
402 $logger->debug("Fetching copy $copyid from storage");
404 $copy = $self->simplereq(
406 'open-ils.storage.direct.asset.copy.retrieve', $copyid );
408 if(!$copy) { $evt = OpenILS::Event->new('ASSET_COPY_NOT_FOUND'); }
410 return( $copy, $evt );
414 # retrieves a circ object by id
415 sub fetch_circulation {
416 my( $self, $circid ) = @_;
419 $logger->debug("Fetching circ $circid from storage");
421 $circ = $self->simplereq(
423 "open-ils.storage.direct.action.circulation.retrieve", $circid );
426 $evt = OpenILS::Event->new('ACTION_CIRCULATION_NOT_FOUND', circid => $circid );
429 return ( $circ, $evt );
432 sub fetch_record_by_copy {
433 my( $self, $copyid ) = @_;
436 $logger->debug("Fetching record by copy $copyid from storage");
438 $record = $self->simplereq(
440 'open-ils.storage.fleshed.biblio.record_entry.retrieve_by_copy', $copyid );
443 $evt = OpenILS::Event->new('BIBLIO_RECORD_ENTRY_NOT_FOUND');
446 return ($record, $evt);
449 # turns a record object into an mvr (mods) object
451 my( $self, $record ) = @_;
452 my $u = OpenILS::Utils::ModsParser->new();
453 $u->start_mods_batch( $record->marc );
454 my $mods = $u->finish_mods_batch();
455 $mods->doc_id($record->id);
460 my( $self, $holdid ) = @_;
463 $logger->debug("Fetching hold $holdid from storage");
465 $hold = $self->simplereq(
467 'open-ils.storage.direct.action.hold_request.retrieve', $holdid);
469 $evt = OpenILS::Event->new('ACTION_HOLD_REQUEST_NOT_FOUND', holdid => $holdid) unless $hold;
471 return ($hold, $evt);
475 sub fetch_hold_transit_by_hold {
476 my( $self, $holdid ) = @_;
477 my( $transit, $evt );
479 $logger->debug("Fetching transit by hold $holdid from storage");
481 $transit = $self->simplereq(
483 'open-ils.storage.direct.action.hold_transit_copy.search.hold', $holdid );
485 $evt = OpenILS::Event->new('ACTION_HOLD_TRANSIT_COPY_NOT_FOUND', holdid => $holdid) unless $transit;
487 return ($transit, $evt );
490 # fetches the captured, but not fulfilled hold attached to a given copy
491 sub fetch_open_hold_by_copy {
492 my( $self, $copyid ) = @_;
493 $logger->debug("Searching for active hold for copy $copyid");
496 $hold = $self->storagereq(
497 'open-ils.storage.direct.action.hold_request.search_where',
499 current_copy => $copyid ,
500 capture_time => { "!=" => undef },
501 fulfillment_time => undef
504 $evt = OpenILS::Event->new('ACTION_HOLD_REQUEST_NOT_FOUND', copyid => $copyid) unless $hold;
505 return ($hold, $evt);
508 sub fetch_hold_transit {
509 my( $self, $transid ) = @_;
510 my( $htransit, $evt );
511 $logger->debug("Fetching hold transit with hold id $transid");
512 $htransit = $self->storagereq(
513 'open-ils.storage.direct.action.hold_transit_copy.retrieve', $transid );
514 $evt = OpenILS::Event->new('ACTION_HOLD_TRANSIT_COPY_NOT_FOUND', id => $transid) unless $htransit;
515 return ($htransit, $evt);
518 sub fetch_copy_by_barcode {
519 my( $self, $barcode ) = @_;
522 $logger->debug("Fetching copy by barcode $barcode from storage");
524 $copy = $self->simplereq( 'open-ils.storage',
525 'open-ils.storage.direct.asset.copy.search_where', { barcode => $barcode, deleted => 'f'} );
526 #'open-ils.storage.direct.asset.copy.search.barcode', $barcode );
528 $evt = OpenILS::Event->new('ASSET_COPY_NOT_FOUND', barcode => $barcode) unless $copy;
530 return ($copy, $evt);
533 sub fetch_open_billable_transaction {
534 my( $self, $transid ) = @_;
535 my( $transaction, $evt );
537 $logger->debug("Fetching open billable transaction $transid from storage");
539 $transaction = $self->simplereq(
541 'open-ils.storage.direct.money.open_billable_transaction_summary.retrieve', $transid);
543 $evt = OpenILS::Event->new(
544 'MONEY_OPEN_BILLABLE_TRANSACTION_SUMMARY_NOT_FOUND', transid => $transid ) unless $transaction;
546 return ($transaction, $evt);
552 $buckets{'biblio'} = 'biblio_record_entry_bucket';
553 $buckets{'callnumber'} = 'call_number_bucket';
554 $buckets{'copy'} = 'copy_bucket';
555 $buckets{'user'} = 'user_bucket';
557 sub fetch_container {
558 my( $self, $id, $type ) = @_;
561 $logger->debug("Fetching container $id with type $type");
563 my $e = 'CONTAINER_CALL_NUMBER_BUCKET_NOT_FOUND';
564 $e = 'CONTAINER_BIBLIO_RECORD_ENTRY_BUCKET_NOT_FOUND' if $type eq 'biblio';
565 $e = 'CONTAINER_USER_BUCKET_NOT_FOUND' if $type eq 'user';
566 $e = 'CONTAINER_COPY_BUCKET_NOT_FOUND' if $type eq 'copy';
568 my $meth = $buckets{$type};
569 $bucket = $self->simplereq(
571 "open-ils.storage.direct.container.$meth.retrieve", $id );
573 $evt = OpenILS::Event->new(
574 $e, container => $id, container_type => $type ) unless $bucket;
576 return ($bucket, $evt);
580 sub fetch_container_item {
581 my( $self, $id, $type ) = @_;
584 $logger->debug("Fetching container item $id with type $type");
586 my $meth = $buckets{$type} . "_item";
588 $bucket = $self->simplereq(
590 "open-ils.storage.direct.container.$meth.retrieve", $id );
593 my $e = 'CONTAINER_CALL_NUMBER_BUCKET_ITEM_NOT_FOUND';
594 $e = 'CONTAINER_BIBLIO_RECORD_ENTRY_BUCKET_ITEM_NOT_FOUND' if $type eq 'biblio';
595 $e = 'CONTAINER_USER_BUCKET_ITEM_NOT_FOUND' if $type eq 'user';
596 $e = 'CONTAINER_COPY_BUCKET_ITEM_NOT_FOUND' if $type eq 'copy';
599 $evt = OpenILS::Event->new(
600 $e, itemid => $id, container_type => $type ) unless $bucket;
602 return ($bucket, $evt);
606 sub fetch_patron_standings {
608 $logger->debug("Fetching patron standings");
609 return $self->simplereq(
611 'open-ils.storage.direct.config.standing.retrieve.all.atomic');
615 sub fetch_permission_group_tree {
617 $logger->debug("Fetching patron profiles");
618 return $self->simplereq(
620 'open-ils.actor.groups.tree.retrieve' );
624 sub fetch_patron_circ_summary {
625 my( $self, $userid ) = @_;
626 $logger->debug("Fetching patron summary for $userid");
627 my $summary = $self->simplereq(
629 "open-ils.storage.action.circulation.patron_summary", $userid );
633 $summary->[1] ||= 0.0;
640 sub fetch_copy_statuses {
642 $logger->debug("Fetching copy statuses");
643 return $self->simplereq(
645 'open-ils.storage.direct.config.copy_status.retrieve.all.atomic' );
648 sub fetch_copy_location {
649 my( $self, $id ) = @_;
651 my $cl = $self->storagereq(
652 'open-ils.storage.direct.asset.copy_location.retrieve', $id );
653 $evt = OpenILS::Event->new('ASSET_COPY_LOCATION_NOT_FOUND') unless $cl;
657 sub fetch_copy_locations {
659 return $self->simplereq(
661 'open-ils.storage.direct.asset.copy_location.retrieve.all.atomic');
664 sub fetch_copy_location_by_name {
665 my( $self, $name, $org ) = @_;
667 my $cl = $self->storagereq(
668 'open-ils.storage.direct.asset.copy_location.search_where',
669 { name => $name, owning_lib => $org } );
670 $evt = OpenILS::Event->new('ASSET_COPY_LOCATION_NOT_FOUND') unless $cl;
674 sub fetch_callnumber {
675 my( $self, $id ) = @_;
677 $logger->debug("Fetching callnumber $id");
679 my $cn = $self->simplereq(
681 'open-ils.storage.direct.asset.call_number.retrieve', $id );
682 $evt = OpenILS::Event->new( 'ASSET_CALL_NUMBER_NOT_FOUND', id => $id ) unless $cn;
684 return ( $cn, $evt );
687 my %ORG_CACHE; # - these rarely change, so cache them..
689 my( $self, $id ) = @_;
690 return $id if( ref($id) eq 'Fieldmapper::actor::org_unit' );
691 return $ORG_CACHE{$id} if $ORG_CACHE{$id};
692 $logger->debug("Fetching org unit $id");
695 my $org = $self->simplereq(
697 'open-ils.storage.direct.actor.org_unit.retrieve', $id );
698 $evt = OpenILS::Event->new( 'ACTOR_ORG_UNIT_NOT_FOUND', id => $id ) unless $org;
699 $ORG_CACHE{$id} = $org;
705 my( $self, $type, $id ) = @_;
707 $logger->debug("Fetching $type stat cat: $id");
708 $cat = $self->simplereq(
710 "open-ils.storage.direct.$type.stat_cat.retrieve", $id );
712 my $e = 'ASSET_STAT_CAT_NOT_FOUND';
713 $e = 'ACTOR_STAT_CAT_NOT_FOUND' if $type eq 'actor';
715 $evt = OpenILS::Event->new( $e, id => $id ) unless $cat;
716 return ( $cat, $evt );
719 sub fetch_stat_cat_entry {
720 my( $self, $type, $id ) = @_;
722 $logger->debug("Fetching $type stat cat entry: $id");
723 $entry = $self->simplereq(
725 "open-ils.storage.direct.$type.stat_cat_entry.retrieve", $id );
727 my $e = 'ASSET_STAT_CAT_ENTRY_NOT_FOUND';
728 $e = 'ACTOR_STAT_CAT_ENTRY_NOT_FOUND' if $type eq 'actor';
730 $evt = OpenILS::Event->new( $e, id => $id ) unless $entry;
731 return ( $entry, $evt );
736 my( $self, $org_tree, $orgid ) = @_;
737 return $org_tree if ( $org_tree->id eq $orgid );
738 return undef unless ref($org_tree->children);
739 for my $c (@{$org_tree->children}) {
740 my $o = $self->find_org($c, $orgid);
746 sub fetch_non_cat_type_by_name_and_org {
747 my( $self, $name, $orgId ) = @_;
748 $logger->debug("Fetching non cat type $name at org $orgId");
749 my $types = $self->simplereq(
751 'open-ils.storage.direct.config.non_cataloged_type.search_where.atomic',
752 { name => $name, owning_lib => $orgId } );
753 return ($types->[0], undef) if($types and @$types);
754 return (undef, OpenILS::Event->new('CONFIG_NON_CATALOGED_TYPE_NOT_FOUND') );
757 sub fetch_non_cat_type {
758 my( $self, $id ) = @_;
759 $logger->debug("Fetching non cat type $id");
761 $type = $self->simplereq(
763 'open-ils.storage.direct.config.non_cataloged_type.retrieve', $id );
764 $evt = OpenILS::Event->new('CONFIG_NON_CATALOGED_TYPE_NOT_FOUND') unless $type;
765 return ($type, $evt);
768 sub DB_UPDATE_FAILED {
769 my( $self, $payload ) = @_;
770 return OpenILS::Event->new('DATABASE_UPDATE_FAILED',
771 payload => ($payload) ? $payload : undef );
774 sub fetch_circ_duration_by_name {
775 my( $self, $name ) = @_;
777 $dur = $self->simplereq(
779 'open-ils.storage.direct.config.rules.circ_duration.search.name.atomic', $name );
781 $evt = OpenILS::Event->new('CONFIG_RULES_CIRC_DURATION_NOT_FOUND') unless $dur;
785 sub fetch_recurring_fine_by_name {
786 my( $self, $name ) = @_;
788 $obj = $self->simplereq(
790 'open-ils.storage.direct.config.rules.recuring_fine.search.name.atomic', $name );
792 $evt = OpenILS::Event->new('CONFIG_RULES_RECURING_FINE_NOT_FOUND') unless $obj;
796 sub fetch_max_fine_by_name {
797 my( $self, $name ) = @_;
799 $obj = $self->simplereq(
801 'open-ils.storage.direct.config.rules.max_fine.search.name.atomic', $name );
803 $evt = OpenILS::Event->new('CONFIG_RULES_MAX_FINE_NOT_FOUND') unless $obj;
808 my( $self, $method, @params ) = @_;
809 return $self->simplereq(
810 'open-ils.storage', $method, @params );
814 my( $self, $e, $name ) = @_;
815 if( $e and ref($e) eq 'HASH' and
816 defined($e->{textcode}) and $e->{textcode} eq $name ) {
823 my( undef, $f, $l ) = caller(0);
824 my( undef, undef, undef, $s ) = caller(1);
827 $logger->debug("LOGMARK: $f:$l:$s");
831 sub fetch_open_circulation {
832 my( $self, $cid ) = @_;
835 my $circ = $self->storagereq(
836 'open-ils.storage.direct.action.open_circulation.search_where',
837 { target_copy => $cid, stop_fines_time => undef } );
838 $evt = OpenILS::Event->new('ACTION_CIRCULATION_NOT_FOUND') unless $circ;
839 return ($circ, $evt);
842 sub fetch_all_open_circulation {
843 my( $self, $cid ) = @_;
846 my $circ = $self->storagereq(
847 'open-ils.storage.direct.action.open_circulation.search_where',
848 { target_copy => $cid, xact_finish => undef } );
849 $evt = OpenILS::Event->new('ACTION_CIRCULATION_NOT_FOUND') unless $circ;
850 return ($circ, $evt);
854 sub copy_status_from_name {
855 my( $self, $name ) = @_;
856 $copy_statuses = $self->fetch_copy_statuses unless $copy_statuses;
857 for my $status (@$copy_statuses) {
858 return $status if( $status->name =~ /$name/i );
863 sub copy_status_to_name {
864 my( $self, $sid ) = @_;
865 $copy_statuses = $self->fetch_copy_statuses unless $copy_statuses;
866 for my $status (@$copy_statuses) {
867 return $status->name if( $status->id == $sid );
872 sub fetch_open_transit_by_copy {
873 my( $self, $copyid ) = @_;
875 $transit = $self->storagereq(
876 'open-ils.storage.direct.action.transit_copy.search_where',
877 { target_copy => $copyid, dest_recv_time => undef });
878 $evt = OpenILS::Event->new('ACTION_TRANSIT_COPY_NOT_FOUND') unless $transit;
879 return ($transit, $evt);
883 my( $self, $copy ) = @_;
884 return undef unless $copy;
885 $copy->status( $copy->status->id ) if ref($copy->status);
886 $copy->location( $copy->location->id ) if ref($copy->location);
887 $copy->circ_lib( $copy->circ_lib->id ) if ref($copy->circ_lib);
891 # un-fleshes a copy and updates it in the DB
892 # returns a DB_UPDATE_FAILED event on error
893 # returns undef on success
895 my( $self, %params ) = @_;
897 my $copy = $params{copy} || die "update_copy(): copy required";
898 my $editor = $params{editor} || die "update_copy(): copy editor required";
899 my $session = $params{session};
901 $logger->debug("Updating copy in the database: " . $copy->id);
903 $self->unflesh_copy($copy);
904 $copy->editor( $editor );
905 $copy->edit_date( 'now' );
908 my $meth = 'open-ils.storage.direct.asset.copy.update';
910 $s = $session->request( $meth, $copy )->gather(1) if $session;
911 $s = $self->storagereq( $meth, $copy ) unless $session;
913 $logger->debug("Update of copy ".$copy->id." returned: $s");
915 return $self->DB_UPDATE_FAILED($copy) unless $s;
919 sub fetch_billable_xact {
920 my( $self, $id ) = @_;
922 $logger->debug("Fetching billable transaction %id");
923 $xact = $self->storagereq(
924 'open-ils.storage.direct.money.billable_transaction.retrieve', $id );
925 $evt = OpenILS::Event->new('MONEY_BILLABLE_TRANSACTION_NOT_FOUND') unless $xact;
926 return ($xact, $evt);
930 sub fetch_fleshed_copy {
931 my( $self, $id ) = @_;
933 $logger->info("Fetching fleshed copy $id");
934 $copy = $self->storagereq(
935 "open-ils.storage.fleshed.asset.copy.retrieve", $id );
936 $evt = OpenILS::Event->new('ASSET_COPY_NOT_FOUND', id => $id) unless $copy;
937 return ($copy, $evt);
941 # returns the org that owns the callnumber that the copy
943 sub fetch_copy_owner {
944 my( $self, $copyid ) = @_;
945 my( $copy, $cn, $evt );
946 $logger->debug("Fetching copy owner $copyid");
947 ($copy, $evt) = $self->fetch_copy($copyid);
948 return (undef,$evt) if $evt;
949 ($cn, $evt) = $self->fetch_callnumber($copy->call_number);
950 return (undef,$evt) if $evt;
951 return ($cn->owning_lib);
954 sub fetch_copy_note {
955 my( $self, $id ) = @_;
957 $logger->debug("Fetching copy note $id");
958 $note = $self->storagereq(
959 'open-ils.storage.direct.asset.copy_note.retrieve', $id );
960 $evt = OpenILS::Event->new('ASSET_COPY_NOTE_NOT_FOUND', id => $id ) unless $note;
961 return ($note, $evt);
964 sub fetch_call_numbers_by_title {
965 my( $self, $titleid ) = @_;
966 $logger->info("Fetching call numbers by title $titleid");
967 return $self->storagereq(
968 'open-ils.storage.direct.asset.call_number.search_where.atomic',
969 { record => $titleid, deleted => 'f' });
970 #'open-ils.storage.direct.asset.call_number.search.record.atomic', $titleid);
973 sub fetch_copies_by_call_number {
974 my( $self, $cnid ) = @_;
975 $logger->info("Fetching copies by call number $cnid");
976 return $self->storagereq(
977 'open-ils.storage.direct.asset.copy.search_where.atomic', { call_number => $cnid, deleted => 'f' } );
978 #'open-ils.storage.direct.asset.copy.search.call_number.atomic', $cnid );
981 sub fetch_user_by_barcode {
982 my( $self, $bc ) = @_;
983 my $cardid = $self->storagereq(
984 'open-ils.storage.id_list.actor.card.search.barcode', $bc );
985 return (undef, OpenILS::Event->new('ACTOR_CARD_NOT_FOUND', barcode => $bc)) unless $cardid;
986 my $user = $self->storagereq(
987 'open-ils.storage.direct.actor.user.search.card', $cardid );
988 return (undef, OpenILS::Event->new('ACTOR_USER_NOT_FOUND', card => $cardid)) unless $user;
994 # ---------------------------------------------------------------------
995 # Updates and returns the patron penalties
996 # ---------------------------------------------------------------------
997 sub update_patron_penalties {
998 my( $self, %args ) = @_;
999 return $self->simplereq(
1001 'open-ils.penalty.patron_penalty.calculate',
1002 { update => 1, %args }
1007 my( $self, $billid ) = @_;
1008 $logger->debug("Fetching billing $billid");
1009 my $bill = $self->storagereq(
1010 'open-ils.storage.direct.money.billing.retrieve', $billid );
1011 my $evt = OpenILS::Event->new('MONEY_BILLING_NOT_FOUND') unless $bill;
1012 return($bill, $evt);