1 package OpenILS::Application::AppUtils;
3 use strict; use warnings;
4 use OpenILS::Application;
5 use base qw/OpenILS::Application/;
6 use OpenSRF::Utils::Cache;
7 use OpenSRF::Utils::Logger qw/$logger/;
8 use OpenILS::Utils::ModsParser;
9 use OpenSRF::EX qw(:try);
12 use OpenILS::Utils::CStoreEditor qw/:funcs/;
13 use OpenILS::Const qw/:const/;
15 # ---------------------------------------------------------------------------
16 # Pile of utilty methods used accross applications.
17 # ---------------------------------------------------------------------------
18 my $cache_client = "OpenSRF::Utils::Cache";
21 # ---------------------------------------------------------------------------
22 # on sucess, returns the created session, on failure throws ERROR exception
23 # ---------------------------------------------------------------------------
24 sub start_db_session {
27 my $session = OpenSRF::AppSession->connect( "open-ils.storage" );
28 my $trans_req = $session->request( "open-ils.storage.transaction.begin" );
30 my $trans_resp = $trans_req->recv();
31 if(ref($trans_resp) and UNIVERSAL::isa($trans_resp,"Error")) { throw $trans_resp; }
32 if( ! $trans_resp->content() ) {
34 ("Unable to Begin Transaction with database" );
38 $logger->debug("Setting global storage session to ".
39 "session: " . $session->session_id . " : " . $session->app );
47 transform => 'permission.usr_has_perm',
58 # returns undef if user has all of the perms provided
59 # returns the first failed perm on failure
60 sub check_user_perms {
61 my($self, $user_id, $org_id, @perm_types ) = @_;
62 $logger->debug("Checking perms with user : $user_id , org: $org_id, @perm_types");
64 for my $type (@perm_types) {
65 $PERM_QUERY->{select}->{au}->[0]->{params} = [$type, $org_id];
66 $PERM_QUERY->{where}->{id} = $user_id;
67 return $type unless $self->is_true(new_editor()->json_query($PERM_QUERY)->[0]->{has_perm});
72 # checks the list of user perms. The first one that fails returns a new
74 my( $self, $user_id, $org_id, @perm_types ) = @_;
75 my $t = $self->check_user_perms( $user_id, $org_id, @perm_types );
76 return OpenILS::Event->new('PERM_FAILURE', ilsperm => $t, ilspermloc => $org_id ) if $t;
82 # ---------------------------------------------------------------------------
83 # commits and destroys the session
84 # ---------------------------------------------------------------------------
85 sub commit_db_session {
86 my( $self, $session ) = @_;
88 my $req = $session->request( "open-ils.storage.transaction.commit" );
89 my $resp = $req->recv();
92 throw OpenSRF::EX::ERROR ("Unable to commit db session");
95 if(UNIVERSAL::isa($resp,"Error")) {
96 throw $resp ($resp->stringify);
100 throw OpenSRF::EX::ERROR ("Unable to commit db session");
104 $session->disconnect();
108 sub rollback_db_session {
109 my( $self, $session ) = @_;
111 my $req = $session->request("open-ils.storage.transaction.rollback");
112 my $resp = $req->recv();
113 if(UNIVERSAL::isa($resp,"Error")) { throw $resp; }
116 $session->disconnect();
121 # returns undef it the event is not an ILS event
122 # returns the event code otherwise
124 my( $self, $evt ) = @_;
125 return $evt->{ilsevent} if( ref($evt) eq 'HASH' and defined($evt->{ilsevent})) ;
129 # ---------------------------------------------------------------------------
130 # Checks to see if a user is logged in. Returns the user record on success,
131 # throws an exception on error.
132 # ---------------------------------------------------------------------------
133 sub check_user_session {
135 my( $self, $user_session ) = @_;
137 my $content = $self->simplereq(
139 'open-ils.auth.session.retrieve', $user_session );
141 if(! $content or $self->event_code($content)) {
142 throw OpenSRF::EX::ERROR
143 ("Session [$user_session] cannot be authenticated" );
146 $logger->debug("Fetch user session $user_session found user " . $content->id );
151 # generic simple request returning a scalar value
153 my($self, $service, $method, @params) = @_;
154 return $self->simple_scalar_request($service, $method, @params);
158 sub simple_scalar_request {
159 my($self, $service, $method, @params) = @_;
161 my $session = OpenSRF::AppSession->create( $service );
163 my $request = $session->request( $method, @params );
169 $val = $request->gather(1);
176 warn "received error : service=$service : method=$method : params=".Dumper(\@params) . "\n $err";
177 throw $err ("Call to $service for method $method \n failed with exception: $err : " );
189 my $org_typelist = undef;
190 my $org_typelist_hash = {};
195 if($tree) { return $tree; }
197 # see if it's in the cache
198 $tree = $cache_client->new()->get_cache('_orgtree');
199 if($tree) { return $tree; }
202 warn "Retrieving Org Tree\n";
203 $orglist = $self->simple_scalar_request(
205 "open-ils.cstore.direct.actor.org_unit.search.atomic",
206 { id => { '!=' => undef } }
210 if( ! $org_typelist ) {
211 warn "Retrieving org types\n";
212 $org_typelist = $self->simple_scalar_request(
214 "open-ils.cstore.direct.actor.org_unit_type.search.atomic",
215 { id => { '!=' => undef } }
217 $self->build_org_type($org_typelist);
220 $tree = $self->build_org_tree($orglist,1);
221 $cache_client->new()->put_cache('_orgtree', $tree);
226 my $slimtree = undef;
227 sub get_slim_org_tree {
230 if($slimtree) { return $slimtree; }
232 # see if it's in the cache
233 $slimtree = $cache_client->new()->get_cache('slimorgtree');
234 if($slimtree) { return $slimtree; }
237 warn "Retrieving Org Tree\n";
238 $orglist = $self->simple_scalar_request(
240 "open-ils.cstore.direct.actor.org_unit.search.atomic",
241 { id => { '!=' => undef } }
245 $slimtree = $self->build_org_tree($orglist);
246 $cache_client->new->put_cache('slimorgtree', $slimtree);
253 my($self, $org_typelist) = @_;
254 for my $type (@$org_typelist) {
255 $org_typelist_hash->{$type->id()} = $type;
263 my( $self, $orglist, $add_types ) = @_;
265 return $orglist unless ref $orglist;
266 return $$orglist[0] if @$orglist == 1;
269 $a->ou_type <=> $b->ou_type ||
270 $a->name cmp $b->name } @$orglist;
272 for my $org (@list) {
276 if(!ref($org->ou_type()) and $add_types) {
277 $org->ou_type( $org_typelist_hash->{$org->ou_type()});
280 next unless (defined($org->parent_ou));
282 my ($parent) = grep { $_->id == $org->parent_ou } @list;
284 $parent->children([]) unless defined($parent->children);
285 push( @{$parent->children}, $org );
291 sub fetch_closed_date {
292 my( $self, $cd ) = @_;
295 $logger->debug("Fetching closed_date $cd from cstore");
297 my $cd_obj = $self->simplereq(
299 'open-ils.cstore.direct.actor.org_unit.closed_date.retrieve', $cd );
302 $logger->info("closed_date $cd not found in the db");
303 $evt = OpenILS::Event->new('ACTOR_USER_NOT_FOUND');
306 return ($cd_obj, $evt);
310 my( $self, $userid ) = @_;
313 $logger->debug("Fetching user $userid from cstore");
315 $user = $self->simplereq(
317 'open-ils.cstore.direct.actor.user.retrieve', $userid );
320 $logger->info("User $userid not found in the db");
321 $evt = OpenILS::Event->new('ACTOR_USER_NOT_FOUND');
324 return ($user, $evt);
328 my( $self, $session ) = @_;
329 my $user; my $evt; my $e;
331 $logger->debug("Checking user session $session");
334 $user = $self->check_user_session($session);
335 } catch Error with { $e = 1; };
337 $logger->debug("Done checking user session $session " . (($e) ? "error = $e" : "") );
339 if( $e or !$user ) { $evt = OpenILS::Event->new('NO_SESSION'); }
340 return ( $user, $evt );
344 # verifiese the session and checks the permissions agains the
345 # session user and the user's home_ou as the org id
347 my( $self, $session, @perms ) = @_;
348 my $user; my $evt; my $e;
349 $logger->debug("Checking user session $session and perms @perms");
350 ($user, $evt) = $self->checkses($session);
351 return (undef, $evt) if $evt;
352 $evt = $self->check_perms($user->id, $user->home_ou, @perms);
353 return ($user, $evt);
358 my( $self, $staffobj, $userid, @perms ) = @_;
360 $userid = $staffobj->id unless defined $userid;
362 $logger->debug("checkrequestor(): requestor => " . $staffobj->id . ", target => $userid");
364 if( $userid ne $staffobj->id ) {
365 ($user, $evt) = $self->fetch_user($userid);
366 return (undef, $evt) if $evt;
367 $evt = $self->check_perms( $staffobj->id, $user->home_ou, @perms );
373 return ($user, $evt);
376 sub checkses_requestor {
377 my( $self, $authtoken, $targetid, @perms ) = @_;
378 my( $requestor, $target, $evt );
380 ($requestor, $evt) = $self->checkses($authtoken);
381 return (undef, undef, $evt) if $evt;
383 ($target, $evt) = $self->checkrequestor( $requestor, $targetid, @perms );
384 return( $requestor, $target, $evt);
388 my( $self, $copyid ) = @_;
391 $logger->debug("Fetching copy $copyid from cstore");
393 $copy = $self->simplereq(
395 'open-ils.cstore.direct.asset.copy.retrieve', $copyid );
397 if(!$copy) { $evt = OpenILS::Event->new('ASSET_COPY_NOT_FOUND'); }
399 return( $copy, $evt );
403 # retrieves a circ object by id
404 sub fetch_circulation {
405 my( $self, $circid ) = @_;
408 $logger->debug("Fetching circ $circid from cstore");
410 $circ = $self->simplereq(
412 "open-ils.cstore.direct.action.circulation.retrieve", $circid );
415 $evt = OpenILS::Event->new('ACTION_CIRCULATION_NOT_FOUND', circid => $circid );
418 return ( $circ, $evt );
421 sub fetch_record_by_copy {
422 my( $self, $copyid ) = @_;
425 $logger->debug("Fetching record by copy $copyid from cstore");
427 $record = $self->simplereq(
429 'open-ils.cstore.direct.asset.copy.retrieve', $copyid,
431 flesh_fields => { bre => [ 'fixed_fields' ],
433 acp => [ 'call_number' ],
439 $evt = OpenILS::Event->new('BIBLIO_RECORD_ENTRY_NOT_FOUND');
441 $record = $record->call_number->record;
444 return ($record, $evt);
447 # turns a record object into an mvr (mods) object
449 my( $self, $record ) = @_;
450 return undef unless $record and $record->marc;
451 my $u = OpenILS::Utils::ModsParser->new();
452 $u->start_mods_batch( $record->marc );
453 my $mods = $u->finish_mods_batch();
454 $mods->doc_id($record->id);
455 $mods->tcn($record->tcn_value);
460 my( $self, $holdid ) = @_;
463 $logger->debug("Fetching hold $holdid from cstore");
465 $hold = $self->simplereq(
467 'open-ils.cstore.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 cstore");
481 $transit = $self->simplereq(
483 'open-ils.cstore.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->cstorereq(
497 'open-ils.cstore.direct.action.hold_request.search',
499 current_copy => $copyid ,
500 capture_time => { "!=" => undef },
501 fulfillment_time => undef,
502 cancel_time => undef,
505 $evt = OpenILS::Event->new('ACTION_HOLD_REQUEST_NOT_FOUND', copyid => $copyid) unless $hold;
506 return ($hold, $evt);
509 sub fetch_hold_transit {
510 my( $self, $transid ) = @_;
511 my( $htransit, $evt );
512 $logger->debug("Fetching hold transit with hold id $transid");
513 $htransit = $self->cstorereq(
514 'open-ils.cstore.direct.action.hold_transit_copy.retrieve', $transid );
515 $evt = OpenILS::Event->new('ACTION_HOLD_TRANSIT_COPY_NOT_FOUND', id => $transid) unless $htransit;
516 return ($htransit, $evt);
519 sub fetch_copy_by_barcode {
520 my( $self, $barcode ) = @_;
523 $logger->debug("Fetching copy by barcode $barcode from cstore");
525 $copy = $self->simplereq( 'open-ils.cstore',
526 'open-ils.cstore.direct.asset.copy.search', { barcode => $barcode, deleted => 'f'} );
527 #'open-ils.storage.direct.asset.copy.search.barcode', $barcode );
529 $evt = OpenILS::Event->new('ASSET_COPY_NOT_FOUND', barcode => $barcode) unless $copy;
531 return ($copy, $evt);
534 sub fetch_open_billable_transaction {
535 my( $self, $transid ) = @_;
536 my( $transaction, $evt );
538 $logger->debug("Fetching open billable transaction $transid from cstore");
540 $transaction = $self->simplereq(
542 'open-ils.cstore.direct.money.open_billable_transaction_summary.retrieve', $transid);
544 $evt = OpenILS::Event->new(
545 'MONEY_OPEN_BILLABLE_TRANSACTION_SUMMARY_NOT_FOUND', transid => $transid ) unless $transaction;
547 return ($transaction, $evt);
553 $buckets{'biblio'} = 'biblio_record_entry_bucket';
554 $buckets{'callnumber'} = 'call_number_bucket';
555 $buckets{'copy'} = 'copy_bucket';
556 $buckets{'user'} = 'user_bucket';
558 sub fetch_container {
559 my( $self, $id, $type ) = @_;
562 $logger->debug("Fetching container $id with type $type");
564 my $e = 'CONTAINER_CALL_NUMBER_BUCKET_NOT_FOUND';
565 $e = 'CONTAINER_BIBLIO_RECORD_ENTRY_BUCKET_NOT_FOUND' if $type eq 'biblio';
566 $e = 'CONTAINER_USER_BUCKET_NOT_FOUND' if $type eq 'user';
567 $e = 'CONTAINER_COPY_BUCKET_NOT_FOUND' if $type eq 'copy';
569 my $meth = $buckets{$type};
570 $bucket = $self->simplereq(
572 "open-ils.cstore.direct.container.$meth.retrieve", $id );
574 $evt = OpenILS::Event->new(
575 $e, container => $id, container_type => $type ) unless $bucket;
577 return ($bucket, $evt);
581 sub fetch_container_e {
582 my( $self, $editor, $id, $type ) = @_;
585 $bucket = $editor->retrieve_container_copy_bucket($id) if $type eq 'copy';
586 $bucket = $editor->retrieve_container_call_number_bucket($id) if $type eq 'callnumber';
587 $bucket = $editor->retrieve_container_biblio_record_entry_bucket($id) if $type eq 'biblio';
588 $bucket = $editor->retrieve_container_user_bucket($id) if $type eq 'user';
590 $evt = $editor->event unless $bucket;
591 return ($bucket, $evt);
594 sub fetch_container_item_e {
595 my( $self, $editor, $id, $type ) = @_;
598 $bucket = $editor->retrieve_container_copy_bucket_item($id) if $type eq 'copy';
599 $bucket = $editor->retrieve_container_call_number_bucket_item($id) if $type eq 'callnumber';
600 $bucket = $editor->retrieve_container_biblio_record_entry_bucket_item($id) if $type eq 'biblio';
601 $bucket = $editor->retrieve_container_user_bucket_item($id) if $type eq 'user';
603 $evt = $editor->event unless $bucket;
604 return ($bucket, $evt);
611 sub fetch_container_item {
612 my( $self, $id, $type ) = @_;
615 $logger->debug("Fetching container item $id with type $type");
617 my $meth = $buckets{$type} . "_item";
619 $bucket = $self->simplereq(
621 "open-ils.cstore.direct.container.$meth.retrieve", $id );
624 my $e = 'CONTAINER_CALL_NUMBER_BUCKET_ITEM_NOT_FOUND';
625 $e = 'CONTAINER_BIBLIO_RECORD_ENTRY_BUCKET_ITEM_NOT_FOUND' if $type eq 'biblio';
626 $e = 'CONTAINER_USER_BUCKET_ITEM_NOT_FOUND' if $type eq 'user';
627 $e = 'CONTAINER_COPY_BUCKET_ITEM_NOT_FOUND' if $type eq 'copy';
629 $evt = OpenILS::Event->new(
630 $e, itemid => $id, container_type => $type ) unless $bucket;
632 return ($bucket, $evt);
636 sub fetch_patron_standings {
638 $logger->debug("Fetching patron standings");
639 return $self->simplereq(
641 'open-ils.cstore.direct.config.standing.search.atomic', { id => { '!=' => undef } });
645 sub fetch_permission_group_tree {
647 $logger->debug("Fetching patron profiles");
648 return $self->simplereq(
650 'open-ils.actor.groups.tree.retrieve' );
654 sub fetch_patron_circ_summary {
655 my( $self, $userid ) = @_;
656 $logger->debug("Fetching patron summary for $userid");
657 my $summary = $self->simplereq(
659 "open-ils.storage.action.circulation.patron_summary", $userid );
663 $summary->[1] ||= 0.0;
670 sub fetch_copy_statuses {
672 $logger->debug("Fetching copy statuses");
673 return $self->simplereq(
675 'open-ils.cstore.direct.config.copy_status.search.atomic', { id => { '!=' => undef } });
678 sub fetch_copy_location {
679 my( $self, $id ) = @_;
681 my $cl = $self->cstorereq(
682 'open-ils.cstore.direct.asset.copy_location.retrieve', $id );
683 $evt = OpenILS::Event->new('ASSET_COPY_LOCATION_NOT_FOUND') unless $cl;
687 sub fetch_copy_locations {
689 return $self->simplereq(
691 'open-ils.cstore.direct.asset.copy_location.search.atomic', { id => { '!=' => undef } });
694 sub fetch_copy_location_by_name {
695 my( $self, $name, $org ) = @_;
697 my $cl = $self->cstorereq(
698 'open-ils.cstore.direct.asset.copy_location.search',
699 { name => $name, owning_lib => $org } );
700 $evt = OpenILS::Event->new('ASSET_COPY_LOCATION_NOT_FOUND') unless $cl;
704 sub fetch_callnumber {
705 my( $self, $id ) = @_;
708 my $e = OpenILS::Event->new( 'ASSET_CALL_NUMBER_NOT_FOUND', id => $id );
709 return( undef, $e ) unless $id;
711 $logger->debug("Fetching callnumber $id");
713 my $cn = $self->simplereq(
715 'open-ils.cstore.direct.asset.call_number.retrieve', $id );
716 $evt = $e unless $cn;
718 return ( $cn, $evt );
721 my %ORG_CACHE; # - these rarely change, so cache them..
723 my( $self, $id ) = @_;
724 return undef unless $id;
725 return $id if( ref($id) eq 'Fieldmapper::actor::org_unit' );
726 return $ORG_CACHE{$id} if $ORG_CACHE{$id};
727 $logger->debug("Fetching org unit $id");
730 my $org = $self->simplereq(
732 'open-ils.cstore.direct.actor.org_unit.retrieve', $id );
733 $evt = OpenILS::Event->new( 'ACTOR_ORG_UNIT_NOT_FOUND', id => $id ) unless $org;
734 $ORG_CACHE{$id} = $org;
740 my( $self, $type, $id ) = @_;
742 $logger->debug("Fetching $type stat cat: $id");
743 $cat = $self->simplereq(
745 "open-ils.cstore.direct.$type.stat_cat.retrieve", $id );
747 my $e = 'ASSET_STAT_CAT_NOT_FOUND';
748 $e = 'ACTOR_STAT_CAT_NOT_FOUND' if $type eq 'actor';
750 $evt = OpenILS::Event->new( $e, id => $id ) unless $cat;
751 return ( $cat, $evt );
754 sub fetch_stat_cat_entry {
755 my( $self, $type, $id ) = @_;
757 $logger->debug("Fetching $type stat cat entry: $id");
758 $entry = $self->simplereq(
760 "open-ils.cstore.direct.$type.stat_cat_entry.retrieve", $id );
762 my $e = 'ASSET_STAT_CAT_ENTRY_NOT_FOUND';
763 $e = 'ACTOR_STAT_CAT_ENTRY_NOT_FOUND' if $type eq 'actor';
765 $evt = OpenILS::Event->new( $e, id => $id ) unless $entry;
766 return ( $entry, $evt );
771 my( $self, $org_tree, $orgid ) = @_;
773 $logger->warn("find_org() did not receive a value for \$org_tree");
776 $logger->warn("find_org() did not receive a value for \$orgid");
779 return $org_tree if ( $org_tree->id eq $orgid );
780 return undef unless ref($org_tree->children);
781 for my $c (@{$org_tree->children}) {
782 my $o = $self->find_org($c, $orgid);
788 sub fetch_non_cat_type_by_name_and_org {
789 my( $self, $name, $orgId ) = @_;
790 $logger->debug("Fetching non cat type $name at org $orgId");
791 my $types = $self->simplereq(
793 'open-ils.cstore.direct.config.non_cataloged_type.search.atomic',
794 { name => $name, owning_lib => $orgId } );
795 return ($types->[0], undef) if($types and @$types);
796 return (undef, OpenILS::Event->new('CONFIG_NON_CATALOGED_TYPE_NOT_FOUND') );
799 sub fetch_non_cat_type {
800 my( $self, $id ) = @_;
801 $logger->debug("Fetching non cat type $id");
803 $type = $self->simplereq(
805 'open-ils.cstore.direct.config.non_cataloged_type.retrieve', $id );
806 $evt = OpenILS::Event->new('CONFIG_NON_CATALOGED_TYPE_NOT_FOUND') unless $type;
807 return ($type, $evt);
810 sub DB_UPDATE_FAILED {
811 my( $self, $payload ) = @_;
812 return OpenILS::Event->new('DATABASE_UPDATE_FAILED',
813 payload => ($payload) ? $payload : undef );
816 sub fetch_circ_duration_by_name {
817 my( $self, $name ) = @_;
819 $dur = $self->simplereq(
821 'open-ils.cstore.direct.config.rules.circ_duration.search.atomic', { name => $name } );
823 $evt = OpenILS::Event->new('CONFIG_RULES_CIRC_DURATION_NOT_FOUND') unless $dur;
827 sub fetch_recurring_fine_by_name {
828 my( $self, $name ) = @_;
830 $obj = $self->simplereq(
832 'open-ils.cstore.direct.config.rules.recuring_fine.search.atomic', { name => $name } );
834 $evt = OpenILS::Event->new('CONFIG_RULES_RECURING_FINE_NOT_FOUND') unless $obj;
838 sub fetch_max_fine_by_name {
839 my( $self, $name ) = @_;
841 $obj = $self->simplereq(
843 'open-ils.cstore.direct.config.rules.max_fine.search.atomic', { name => $name } );
845 $evt = OpenILS::Event->new('CONFIG_RULES_MAX_FINE_NOT_FOUND') unless $obj;
850 my( $self, $method, @params ) = @_;
851 return $self->simplereq(
852 'open-ils.storage', $method, @params );
856 my( $self, $method, @params ) = @_;
857 return $self->simplereq(
858 'open-ils.cstore', $method, @params );
862 my( $self, $e, $name ) = @_;
863 if( $e and ref($e) eq 'HASH' and
864 defined($e->{textcode}) and $e->{textcode} eq $name ) {
871 my( undef, $f, $l ) = caller(0);
872 my( undef, undef, undef, $s ) = caller(1);
875 $logger->debug("LOGMARK: $f:$l:$s");
879 sub fetch_open_circulation {
880 my( $self, $cid ) = @_;
883 my $circ = $self->cstorereq(
884 'open-ils.cstore.direct.action.open_circulation.search',
885 { target_copy => $cid, stop_fines_time => undef } );
886 $evt = OpenILS::Event->new('ACTION_CIRCULATION_NOT_FOUND') unless $circ;
887 return ($circ, $evt);
890 sub fetch_all_open_circulation {
891 my( $self, $cid ) = @_;
894 my $circ = $self->cstorereq(
895 'open-ils.cstore.direct.action.open_circulation.search',
896 { target_copy => $cid, xact_finish => undef } );
897 $evt = OpenILS::Event->new('ACTION_CIRCULATION_NOT_FOUND') unless $circ;
898 return ($circ, $evt);
902 sub copy_status_from_name {
903 my( $self, $name ) = @_;
904 $copy_statuses = $self->fetch_copy_statuses unless $copy_statuses;
905 for my $status (@$copy_statuses) {
906 return $status if( $status->name =~ /$name/i );
911 sub copy_status_to_name {
912 my( $self, $sid ) = @_;
913 $copy_statuses = $self->fetch_copy_statuses unless $copy_statuses;
914 for my $status (@$copy_statuses) {
915 return $status->name if( $status->id == $sid );
922 my( $self, $arg ) = @_;
923 return $arg if ref $arg;
924 $copy_statuses = $self->fetch_copy_statuses unless $copy_statuses;
925 my ($stat) = grep { $_->id == $arg } @$copy_statuses;
929 sub fetch_open_transit_by_copy {
930 my( $self, $copyid ) = @_;
932 $transit = $self->cstorereq(
933 'open-ils.cstore.direct.action.transit_copy.search',
934 { target_copy => $copyid, dest_recv_time => undef });
935 $evt = OpenILS::Event->new('ACTION_TRANSIT_COPY_NOT_FOUND') unless $transit;
936 return ($transit, $evt);
940 my( $self, $copy ) = @_;
941 return undef unless $copy;
942 $copy->status( $copy->status->id ) if ref($copy->status);
943 $copy->location( $copy->location->id ) if ref($copy->location);
944 $copy->circ_lib( $copy->circ_lib->id ) if ref($copy->circ_lib);
948 # un-fleshes a copy and updates it in the DB
949 # returns a DB_UPDATE_FAILED event on error
950 # returns undef on success
952 my( $self, %params ) = @_;
954 my $copy = $params{copy} || die "update_copy(): copy required";
955 my $editor = $params{editor} || die "update_copy(): copy editor required";
956 my $session = $params{session};
958 $logger->debug("Updating copy in the database: " . $copy->id);
960 $self->unflesh_copy($copy);
961 $copy->editor( $editor );
962 $copy->edit_date( 'now' );
965 my $meth = 'open-ils.storage.direct.asset.copy.update';
967 $s = $session->request( $meth, $copy )->gather(1) if $session;
968 $s = $self->storagereq( $meth, $copy ) unless $session;
970 $logger->debug("Update of copy ".$copy->id." returned: $s");
972 return $self->DB_UPDATE_FAILED($copy) unless $s;
976 sub fetch_billable_xact {
977 my( $self, $id ) = @_;
979 $logger->debug("Fetching billable transaction %id");
980 $xact = $self->cstorereq(
981 'open-ils.cstore.direct.money.billable_transaction.retrieve', $id );
982 $evt = OpenILS::Event->new('MONEY_BILLABLE_TRANSACTION_NOT_FOUND') unless $xact;
983 return ($xact, $evt);
986 sub fetch_billable_xact_summary {
987 my( $self, $id ) = @_;
989 $logger->debug("Fetching billable transaction summary %id");
990 $xact = $self->cstorereq(
991 'open-ils.cstore.direct.money.billable_transaction_summary.retrieve', $id );
992 $evt = OpenILS::Event->new('MONEY_BILLABLE_TRANSACTION_NOT_FOUND') unless $xact;
993 return ($xact, $evt);
996 sub fetch_fleshed_copy {
997 my( $self, $id ) = @_;
999 $logger->info("Fetching fleshed copy $id");
1000 $copy = $self->cstorereq(
1001 "open-ils.cstore.direct.asset.copy.retrieve", $id,
1003 flesh_fields => { acp => [ qw/ circ_lib location status stat_cat_entries / ] }
1006 $evt = OpenILS::Event->new('ASSET_COPY_NOT_FOUND', id => $id) unless $copy;
1007 return ($copy, $evt);
1011 # returns the org that owns the callnumber that the copy
1013 sub fetch_copy_owner {
1014 my( $self, $copyid ) = @_;
1015 my( $copy, $cn, $evt );
1016 $logger->debug("Fetching copy owner $copyid");
1017 ($copy, $evt) = $self->fetch_copy($copyid);
1018 return (undef,$evt) if $evt;
1019 ($cn, $evt) = $self->fetch_callnumber($copy->call_number);
1020 return (undef,$evt) if $evt;
1021 return ($cn->owning_lib);
1024 sub fetch_copy_note {
1025 my( $self, $id ) = @_;
1027 $logger->debug("Fetching copy note $id");
1028 $note = $self->cstorereq(
1029 'open-ils.cstore.direct.asset.copy_note.retrieve', $id );
1030 $evt = OpenILS::Event->new('ASSET_COPY_NOTE_NOT_FOUND', id => $id ) unless $note;
1031 return ($note, $evt);
1034 sub fetch_call_numbers_by_title {
1035 my( $self, $titleid ) = @_;
1036 $logger->info("Fetching call numbers by title $titleid");
1037 return $self->cstorereq(
1038 'open-ils.cstore.direct.asset.call_number.search.atomic',
1039 { record => $titleid, deleted => 'f' });
1040 #'open-ils.storage.direct.asset.call_number.search.record.atomic', $titleid);
1043 sub fetch_copies_by_call_number {
1044 my( $self, $cnid ) = @_;
1045 $logger->info("Fetching copies by call number $cnid");
1046 return $self->cstorereq(
1047 'open-ils.cstore.direct.asset.copy.search.atomic', { call_number => $cnid, deleted => 'f' } );
1048 #'open-ils.storage.direct.asset.copy.search.call_number.atomic', $cnid );
1051 sub fetch_user_by_barcode {
1052 my( $self, $bc ) = @_;
1053 my $cardid = $self->cstorereq(
1054 'open-ils.cstore.direct.actor.card.id_list', { barcode => $bc } );
1055 return (undef, OpenILS::Event->new('ACTOR_CARD_NOT_FOUND', barcode => $bc)) unless $cardid;
1056 my $user = $self->cstorereq(
1057 'open-ils.cstore.direct.actor.user.search', { card => $cardid } );
1058 return (undef, OpenILS::Event->new('ACTOR_USER_NOT_FOUND', card => $cardid)) unless $user;
1064 # ---------------------------------------------------------------------
1065 # Updates and returns the patron penalties
1066 # ---------------------------------------------------------------------
1067 sub update_patron_penalties {
1068 my( $self, %args ) = @_;
1069 return $self->simplereq(
1071 'open-ils.penalty.patron_penalty.calculate',
1072 { update => 1, %args }
1077 my( $self, $billid ) = @_;
1078 $logger->debug("Fetching billing $billid");
1079 my $bill = $self->cstorereq(
1080 'open-ils.cstore.direct.money.billing.retrieve', $billid );
1081 my $evt = OpenILS::Event->new('MONEY_BILLING_NOT_FOUND') unless $bill;
1082 return($bill, $evt);
1088 sub fetch_org_tree {
1090 return $ORG_TREE if $ORG_TREE;
1091 return $ORG_TREE = OpenILS::Utils::CStoreEditor->new->search_actor_org_unit(
1093 {"parent_ou" => undef },
1096 flesh_fields => { aou => ['children'] },
1097 order_by => { aou => 'name'}
1104 my( $self, $node, $callback ) = @_;
1105 return unless $node;
1107 if( $node->children ) {
1108 $self->walk_org_tree($_, $callback) for @{$node->children};
1113 my( $self, $item ) = @_;
1114 return 1 if $item and $item !~ /^f$/i;
1119 # This logic now lives in storage
1120 sub __patron_money_owed {
1121 my( $self, $patronid ) = @_;
1122 my $ses = OpenSRF::AppSession->create('open-ils.storage');
1123 my $req = $ses->request(
1124 'open-ils.storage.money.billable_transaction.summary.search',
1125 { usr => $patronid, xact_finish => undef } );
1129 while( $data = $req->recv ) {
1130 $data = $data->content;
1131 $total += $data->balance_owed;
1136 sub patron_money_owed {
1137 my( $self, $userid ) = @_;
1138 return $self->storagereq(
1139 'open-ils.storage.actor.user.total_owed', $userid);
1142 sub patron_total_items_out {
1143 my( $self, $userid ) = @_;
1144 return $self->storagereq(
1145 'open-ils.storage.actor.user.total_out', $userid);
1151 #---------------------------------------------------------------------
1152 # Returns ($summary, $event)
1153 #---------------------------------------------------------------------
1157 my $editor = shift || OpenILS::Utils::CStoreEditor->new;
1159 $id = $id->id if (ref($id));
1161 my $xact = $editor->retrieve_money_billable_transaction(
1165 flesh_fields => { mbt => [ qw/billings payments grocery circulation/ ] }
1168 ) or return (undef, $editor->event);
1170 return $self->make_mbts($xact);
1174 #---------------------------------------------------------------------
1175 # Given a list of money.billable_transaction objects, this creates
1176 # transaction summary objects for each
1177 #--------------------------------------------------------------------
1183 for my $x (@xacts) {
1185 my $s = new Fieldmapper::money::billable_transaction_summary;
1189 $s->xact_start( $x->xact_start );
1190 $s->xact_finish( $x->xact_finish );
1194 for my $b (@{ $x->billings }) {
1195 next if ($self->is_true($b->voided));
1196 $to += ($b->amount * 100);
1197 $lb ||= $b->billing_ts;
1198 if ($b->billing_ts ge $lb) {
1199 $lb = $b->billing_ts;
1200 $s->last_billing_note($b->note);
1201 $s->last_billing_ts($b->billing_ts);
1202 $s->last_billing_type($b->billing_type);
1206 $s->total_owed( sprintf('%0.2f', $to / 100 ) );
1210 for my $p (@{ $x->payments }) {
1211 next if ($self->is_true($p->voided));
1212 $tp += ($p->amount * 100);
1213 $lp ||= $p->payment_ts;
1214 if ($p->payment_ts ge $lp) {
1215 $lp = $p->payment_ts;
1216 $s->last_payment_note($p->note);
1217 $s->last_payment_ts($p->payment_ts);
1218 $s->last_payment_type($p->payment_type);
1222 $s->total_paid( sprintf('%0.2f', $tp / 100 ) );
1223 $s->balance_owed( sprintf('%0.2f', ($to - $tp) / 100) );
1224 $s->xact_type('grocery') if ($x->grocery);
1225 $s->xact_type('circulation') if ($x->circulation);
1227 $logger->debug("Created mbts with balance_owed = ". $s->balance_owed);
1236 sub ou_ancestor_setting_value {
1237 my $obj = ou_ancestor_setting(@_);
1238 return ($obj) ? $obj->{value} : undef;
1241 sub ou_ancestor_setting {
1242 my( $self, $orgid, $name, $e ) = @_;
1243 $e = $e || OpenILS::Utils::CStoreEditor->new;
1246 my $setting = $e->search_actor_org_unit_setting({org_unit=>$orgid, name=>$name})->[0];
1249 $logger->info("found org_setting $name at org $orgid : " . $setting->value);
1250 return { org => $orgid, value => OpenSRF::Utils::JSON->JSON2perl($setting->value) };
1253 my $org = $e->retrieve_actor_org_unit($orgid) or return $e->event;
1254 $orgid = $org->parent_ou or return undef;
1262 # returns the ISO8601 string representation of the requested epoch in GMT
1264 my( $self, $epoch ) = @_;
1265 my ($sec,$min,$hour,$mday,$mon,$year) = gmtime($epoch);
1266 $year += 1900; $mon += 1;
1268 '%s-%0.2d-%0.2dT%0.2d:%0.2d:%0.2d-00',
1269 $year, $mon, $mday, $hour, $min, $sec);