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;
11 use OpenILS::Const qw/:const/;
13 # ---------------------------------------------------------------------------
14 # Pile of utilty methods used accross applications.
15 # ---------------------------------------------------------------------------
16 my $cache_client = "OpenSRF::Utils::Cache";
19 # ---------------------------------------------------------------------------
20 # on sucess, returns the created session, on failure throws ERROR exception
21 # ---------------------------------------------------------------------------
22 sub start_db_session {
25 my $session = OpenSRF::AppSession->connect( "open-ils.storage" );
26 my $trans_req = $session->request( "open-ils.storage.transaction.begin" );
28 my $trans_resp = $trans_req->recv();
29 if(ref($trans_resp) and UNIVERSAL::isa($trans_resp,"Error")) { throw $trans_resp; }
30 if( ! $trans_resp->content() ) {
32 ("Unable to Begin Transaction with database" );
36 $logger->debug("Setting global storage session to ".
37 "session: " . $session->session_id . " : " . $session->app );
43 # returns undef if user has all of the perms provided
44 # returns the first failed perm on failure
45 sub check_user_perms {
46 my($self, $user_id, $org_id, @perm_types ) = @_;
47 $logger->debug("Checking perms with user : $user_id , org: $org_id, @perm_types");
48 for my $type (@perm_types) {
49 return $type unless ($self->storagereq(
50 "open-ils.storage.permission.user_has_perm",
51 $user_id, $type, $org_id ));
56 # checks the list of user perms. The first one that fails returns a new
58 my( $self, $user_id, $org_id, @perm_types ) = @_;
59 my $t = $self->check_user_perms( $user_id, $org_id, @perm_types );
60 return OpenILS::Event->new('PERM_FAILURE', ilsperm => $t, ilspermloc => $org_id ) if $t;
66 # ---------------------------------------------------------------------------
67 # commits and destroys the session
68 # ---------------------------------------------------------------------------
69 sub commit_db_session {
70 my( $self, $session ) = @_;
72 my $req = $session->request( "open-ils.storage.transaction.commit" );
73 my $resp = $req->recv();
76 throw OpenSRF::EX::ERROR ("Unable to commit db session");
79 if(UNIVERSAL::isa($resp,"Error")) {
80 throw $resp ($resp->stringify);
84 throw OpenSRF::EX::ERROR ("Unable to commit db session");
88 $session->disconnect();
92 sub rollback_db_session {
93 my( $self, $session ) = @_;
95 my $req = $session->request("open-ils.storage.transaction.rollback");
96 my $resp = $req->recv();
97 if(UNIVERSAL::isa($resp,"Error")) { throw $resp; }
100 $session->disconnect();
105 # returns undef it the event is not an ILS event
106 # returns the event code otherwise
108 my( $self, $evt ) = @_;
109 return $evt->{ilsevent} if( ref($evt) eq 'HASH' and defined($evt->{ilsevent})) ;
113 # ---------------------------------------------------------------------------
114 # Checks to see if a user is logged in. Returns the user record on success,
115 # throws an exception on error.
116 # ---------------------------------------------------------------------------
117 sub check_user_session {
119 my( $self, $user_session ) = @_;
121 my $content = $self->simplereq(
123 'open-ils.auth.session.retrieve', $user_session );
125 if(! $content or $self->event_code($content)) {
126 throw OpenSRF::EX::ERROR
127 ("Session [$user_session] cannot be authenticated" );
130 $logger->debug("Fetch user session $user_session found user " . $content->id );
135 # generic simple request returning a scalar value
137 my($self, $service, $method, @params) = @_;
138 return $self->simple_scalar_request($service, $method, @params);
142 sub simple_scalar_request {
143 my($self, $service, $method, @params) = @_;
145 my $session = OpenSRF::AppSession->create( $service );
147 my $request = $session->request( $method, @params );
153 $val = $request->gather(1);
160 warn "received error : service=$service : method=$method : params=".Dumper(\@params) . "\n $err";
161 throw $err ("Call to $service for method $method \n failed with exception: $err : " );
173 my $org_typelist = undef;
174 my $org_typelist_hash = {};
179 if($tree) { return $tree; }
181 # see if it's in the cache
182 $tree = $cache_client->new()->get_cache('_orgtree');
183 if($tree) { return $tree; }
186 warn "Retrieving Org Tree\n";
187 $orglist = $self->simple_scalar_request(
189 "open-ils.cstore.direct.actor.org_unit.search.atomic",
190 { id => { '!=' => undef } }
194 if( ! $org_typelist ) {
195 warn "Retrieving org types\n";
196 $org_typelist = $self->simple_scalar_request(
198 "open-ils.cstore.direct.actor.org_unit_type.search.atomic",
199 { id => { '!=' => undef } }
201 $self->build_org_type($org_typelist);
204 $tree = $self->build_org_tree($orglist,1);
205 $cache_client->new()->put_cache('_orgtree', $tree);
210 my $slimtree = undef;
211 sub get_slim_org_tree {
214 if($slimtree) { return $slimtree; }
216 # see if it's in the cache
217 $slimtree = $cache_client->new()->get_cache('slimorgtree');
218 if($slimtree) { return $slimtree; }
221 warn "Retrieving Org Tree\n";
222 $orglist = $self->simple_scalar_request(
224 "open-ils.cstore.direct.actor.org_unit.search.atomic",
225 { id => { '!=' => undef } }
229 $slimtree = $self->build_org_tree($orglist);
230 $cache_client->new->put_cache('slimorgtree', $slimtree);
237 my($self, $org_typelist) = @_;
238 for my $type (@$org_typelist) {
239 $org_typelist_hash->{$type->id()} = $type;
247 my( $self, $orglist, $add_types ) = @_;
249 return $orglist unless (
250 ref($orglist) and @$orglist > 1 );
253 $a->ou_type <=> $b->ou_type ||
254 $a->name cmp $b->name } @$orglist;
256 for my $org (@list) {
260 if(!ref($org->ou_type()) and $add_types) {
261 $org->ou_type( $org_typelist_hash->{$org->ou_type()});
264 next unless (defined($org->parent_ou));
266 my ($parent) = grep { $_->id == $org->parent_ou } @list;
268 $parent->children([]) unless defined($parent->children);
269 push( @{$parent->children}, $org );
276 sub fetch_closed_date {
277 my( $self, $cd ) = @_;
280 $logger->debug("Fetching closed_date $cd from cstore");
282 my $cd_obj = $self->simplereq(
284 'open-ils.cstore.direct.actor.org_unit.closed_date.retrieve', $cd );
287 $logger->info("closed_date $cd not found in the db");
288 $evt = OpenILS::Event->new('ACTOR_USER_NOT_FOUND');
291 return ($cd_obj, $evt);
295 my( $self, $userid ) = @_;
298 $logger->debug("Fetching user $userid from cstore");
300 $user = $self->simplereq(
302 'open-ils.cstore.direct.actor.user.retrieve', $userid );
305 $logger->info("User $userid not found in the db");
306 $evt = OpenILS::Event->new('ACTOR_USER_NOT_FOUND');
309 return ($user, $evt);
313 my( $self, $session ) = @_;
314 my $user; my $evt; my $e;
316 $logger->debug("Checking user session $session");
319 $user = $self->check_user_session($session);
320 } catch Error with { $e = 1; };
322 $logger->debug("Done checking user session $session " . (($e) ? "error = $e" : "") );
324 if( $e or !$user ) { $evt = OpenILS::Event->new('NO_SESSION'); }
325 return ( $user, $evt );
329 # verifiese the session and checks the permissions agains the
330 # session user and the user's home_ou as the org id
332 my( $self, $session, @perms ) = @_;
333 my $user; my $evt; my $e;
334 $logger->debug("Checking user session $session and perms @perms");
335 ($user, $evt) = $self->checkses($session);
336 return (undef, $evt) if $evt;
337 $evt = $self->check_perms($user->id, $user->home_ou, @perms);
338 return ($user, $evt);
343 my( $self, $staffobj, $userid, @perms ) = @_;
345 $userid = $staffobj->id unless defined $userid;
347 $logger->debug("checkrequestor(): requestor => " . $staffobj->id . ", target => $userid");
349 if( $userid ne $staffobj->id ) {
350 ($user, $evt) = $self->fetch_user($userid);
351 return (undef, $evt) if $evt;
352 $evt = $self->check_perms( $staffobj->id, $user->home_ou, @perms );
358 return ($user, $evt);
361 sub checkses_requestor {
362 my( $self, $authtoken, $targetid, @perms ) = @_;
363 my( $requestor, $target, $evt );
365 ($requestor, $evt) = $self->checkses($authtoken);
366 return (undef, undef, $evt) if $evt;
368 ($target, $evt) = $self->checkrequestor( $requestor, $targetid, @perms );
369 return( $requestor, $target, $evt);
373 my( $self, $copyid ) = @_;
376 $logger->debug("Fetching copy $copyid from cstore");
378 $copy = $self->simplereq(
380 'open-ils.cstore.direct.asset.copy.retrieve', $copyid );
382 if(!$copy) { $evt = OpenILS::Event->new('ASSET_COPY_NOT_FOUND'); }
384 return( $copy, $evt );
388 # retrieves a circ object by id
389 sub fetch_circulation {
390 my( $self, $circid ) = @_;
393 $logger->debug("Fetching circ $circid from cstore");
395 $circ = $self->simplereq(
397 "open-ils.cstore.direct.action.circulation.retrieve", $circid );
400 $evt = OpenILS::Event->new('ACTION_CIRCULATION_NOT_FOUND', circid => $circid );
403 return ( $circ, $evt );
406 sub fetch_record_by_copy {
407 my( $self, $copyid ) = @_;
410 $logger->debug("Fetching record by copy $copyid from cstore");
412 $record = $self->simplereq(
414 'open-ils.cstore.direct.asset.copy.retrieve', $copyid,
416 flesh_fields => { bre => [ 'fixed_fields' ],
418 acp => [ 'call_number' ],
424 $evt = OpenILS::Event->new('BIBLIO_RECORD_ENTRY_NOT_FOUND');
426 $record = $record->call_number->record;
429 return ($record, $evt);
432 # turns a record object into an mvr (mods) object
434 my( $self, $record ) = @_;
435 return undef unless $record and $record->marc;
436 my $u = OpenILS::Utils::ModsParser->new();
437 $u->start_mods_batch( $record->marc );
438 my $mods = $u->finish_mods_batch();
439 $mods->doc_id($record->id);
440 $mods->tcn($record->tcn_value);
445 my( $self, $holdid ) = @_;
448 $logger->debug("Fetching hold $holdid from cstore");
450 $hold = $self->simplereq(
452 'open-ils.cstore.direct.action.hold_request.retrieve', $holdid);
454 $evt = OpenILS::Event->new('ACTION_HOLD_REQUEST_NOT_FOUND', holdid => $holdid) unless $hold;
456 return ($hold, $evt);
460 sub fetch_hold_transit_by_hold {
461 my( $self, $holdid ) = @_;
462 my( $transit, $evt );
464 $logger->debug("Fetching transit by hold $holdid from cstore");
466 $transit = $self->simplereq(
468 'open-ils.cstore.direct.action.hold_transit_copy.search', { hold => $holdid } );
470 $evt = OpenILS::Event->new('ACTION_HOLD_TRANSIT_COPY_NOT_FOUND', holdid => $holdid) unless $transit;
472 return ($transit, $evt );
475 # fetches the captured, but not fulfilled hold attached to a given copy
476 sub fetch_open_hold_by_copy {
477 my( $self, $copyid ) = @_;
478 $logger->debug("Searching for active hold for copy $copyid");
481 $hold = $self->cstorereq(
482 'open-ils.cstore.direct.action.hold_request.search',
484 current_copy => $copyid ,
485 capture_time => { "!=" => undef },
486 fulfillment_time => undef,
487 cancel_time => undef,
490 $evt = OpenILS::Event->new('ACTION_HOLD_REQUEST_NOT_FOUND', copyid => $copyid) unless $hold;
491 return ($hold, $evt);
494 sub fetch_hold_transit {
495 my( $self, $transid ) = @_;
496 my( $htransit, $evt );
497 $logger->debug("Fetching hold transit with hold id $transid");
498 $htransit = $self->cstorereq(
499 'open-ils.cstore.direct.action.hold_transit_copy.retrieve', $transid );
500 $evt = OpenILS::Event->new('ACTION_HOLD_TRANSIT_COPY_NOT_FOUND', id => $transid) unless $htransit;
501 return ($htransit, $evt);
504 sub fetch_copy_by_barcode {
505 my( $self, $barcode ) = @_;
508 $logger->debug("Fetching copy by barcode $barcode from cstore");
510 $copy = $self->simplereq( 'open-ils.cstore',
511 'open-ils.cstore.direct.asset.copy.search', { barcode => $barcode, deleted => 'f'} );
512 #'open-ils.storage.direct.asset.copy.search.barcode', $barcode );
514 $evt = OpenILS::Event->new('ASSET_COPY_NOT_FOUND', barcode => $barcode) unless $copy;
516 return ($copy, $evt);
519 sub fetch_open_billable_transaction {
520 my( $self, $transid ) = @_;
521 my( $transaction, $evt );
523 $logger->debug("Fetching open billable transaction $transid from cstore");
525 $transaction = $self->simplereq(
527 'open-ils.cstore.direct.money.open_billable_transaction_summary.retrieve', $transid);
529 $evt = OpenILS::Event->new(
530 'MONEY_OPEN_BILLABLE_TRANSACTION_SUMMARY_NOT_FOUND', transid => $transid ) unless $transaction;
532 return ($transaction, $evt);
538 $buckets{'biblio'} = 'biblio_record_entry_bucket';
539 $buckets{'callnumber'} = 'call_number_bucket';
540 $buckets{'copy'} = 'copy_bucket';
541 $buckets{'user'} = 'user_bucket';
543 sub fetch_container {
544 my( $self, $id, $type ) = @_;
547 $logger->debug("Fetching container $id with type $type");
549 my $e = 'CONTAINER_CALL_NUMBER_BUCKET_NOT_FOUND';
550 $e = 'CONTAINER_BIBLIO_RECORD_ENTRY_BUCKET_NOT_FOUND' if $type eq 'biblio';
551 $e = 'CONTAINER_USER_BUCKET_NOT_FOUND' if $type eq 'user';
552 $e = 'CONTAINER_COPY_BUCKET_NOT_FOUND' if $type eq 'copy';
554 my $meth = $buckets{$type};
555 $bucket = $self->simplereq(
557 "open-ils.cstore.direct.container.$meth.retrieve", $id );
559 $evt = OpenILS::Event->new(
560 $e, container => $id, container_type => $type ) unless $bucket;
562 return ($bucket, $evt);
566 sub fetch_container_e {
567 my( $self, $editor, $id, $type ) = @_;
570 $bucket = $editor->retrieve_container_copy_bucket($id) if $type eq 'copy';
571 $bucket = $editor->retrieve_container_call_number_bucket($id) if $type eq 'callnumber';
572 $bucket = $editor->retrieve_container_biblio_record_entry_bucket($id) if $type eq 'biblio';
573 $bucket = $editor->retrieve_container_user_bucket($id) if $type eq 'user';
575 $evt = $editor->event unless $bucket;
576 return ($bucket, $evt);
579 sub fetch_container_item_e {
580 my( $self, $editor, $id, $type ) = @_;
583 $bucket = $editor->retrieve_container_copy_bucket_item($id) if $type eq 'copy';
584 $bucket = $editor->retrieve_container_call_number_bucket_item($id) if $type eq 'callnumber';
585 $bucket = $editor->retrieve_container_biblio_record_entry_bucket_item($id) if $type eq 'biblio';
586 $bucket = $editor->retrieve_container_user_bucket_item($id) if $type eq 'user';
588 $evt = $editor->event unless $bucket;
589 return ($bucket, $evt);
596 sub fetch_container_item {
597 my( $self, $id, $type ) = @_;
600 $logger->debug("Fetching container item $id with type $type");
602 my $meth = $buckets{$type} . "_item";
604 $bucket = $self->simplereq(
606 "open-ils.cstore.direct.container.$meth.retrieve", $id );
609 my $e = 'CONTAINER_CALL_NUMBER_BUCKET_ITEM_NOT_FOUND';
610 $e = 'CONTAINER_BIBLIO_RECORD_ENTRY_BUCKET_ITEM_NOT_FOUND' if $type eq 'biblio';
611 $e = 'CONTAINER_USER_BUCKET_ITEM_NOT_FOUND' if $type eq 'user';
612 $e = 'CONTAINER_COPY_BUCKET_ITEM_NOT_FOUND' if $type eq 'copy';
614 $evt = OpenILS::Event->new(
615 $e, itemid => $id, container_type => $type ) unless $bucket;
617 return ($bucket, $evt);
621 sub fetch_patron_standings {
623 $logger->debug("Fetching patron standings");
624 return $self->simplereq(
626 'open-ils.cstore.direct.config.standing.search.atomic', { id => { '!=' => undef } });
630 sub fetch_permission_group_tree {
632 $logger->debug("Fetching patron profiles");
633 return $self->simplereq(
635 'open-ils.actor.groups.tree.retrieve' );
639 sub fetch_patron_circ_summary {
640 my( $self, $userid ) = @_;
641 $logger->debug("Fetching patron summary for $userid");
642 my $summary = $self->simplereq(
644 "open-ils.storage.action.circulation.patron_summary", $userid );
648 $summary->[1] ||= 0.0;
655 sub fetch_copy_statuses {
657 $logger->debug("Fetching copy statuses");
658 return $self->simplereq(
660 'open-ils.cstore.direct.config.copy_status.search.atomic', { id => { '!=' => undef } });
663 sub fetch_copy_location {
664 my( $self, $id ) = @_;
666 my $cl = $self->cstorereq(
667 'open-ils.cstore.direct.asset.copy_location.retrieve', $id );
668 $evt = OpenILS::Event->new('ASSET_COPY_LOCATION_NOT_FOUND') unless $cl;
672 sub fetch_copy_locations {
674 return $self->simplereq(
676 'open-ils.cstore.direct.asset.copy_location.search.atomic', { id => { '!=' => undef } });
679 sub fetch_copy_location_by_name {
680 my( $self, $name, $org ) = @_;
682 my $cl = $self->cstorereq(
683 'open-ils.cstore.direct.asset.copy_location.search',
684 { name => $name, owning_lib => $org } );
685 $evt = OpenILS::Event->new('ASSET_COPY_LOCATION_NOT_FOUND') unless $cl;
689 sub fetch_callnumber {
690 my( $self, $id ) = @_;
693 my $e = OpenILS::Event->new( 'ASSET_CALL_NUMBER_NOT_FOUND', id => $id );
694 return( undef, $e ) unless $id;
696 $logger->debug("Fetching callnumber $id");
698 my $cn = $self->simplereq(
700 'open-ils.cstore.direct.asset.call_number.retrieve', $id );
701 $evt = $e unless $cn;
703 return ( $cn, $evt );
706 my %ORG_CACHE; # - these rarely change, so cache them..
708 my( $self, $id ) = @_;
709 return undef unless $id;
710 return $id if( ref($id) eq 'Fieldmapper::actor::org_unit' );
711 return $ORG_CACHE{$id} if $ORG_CACHE{$id};
712 $logger->debug("Fetching org unit $id");
715 my $org = $self->simplereq(
717 'open-ils.cstore.direct.actor.org_unit.retrieve', $id );
718 $evt = OpenILS::Event->new( 'ACTOR_ORG_UNIT_NOT_FOUND', id => $id ) unless $org;
719 $ORG_CACHE{$id} = $org;
725 my( $self, $type, $id ) = @_;
727 $logger->debug("Fetching $type stat cat: $id");
728 $cat = $self->simplereq(
730 "open-ils.cstore.direct.$type.stat_cat.retrieve", $id );
732 my $e = 'ASSET_STAT_CAT_NOT_FOUND';
733 $e = 'ACTOR_STAT_CAT_NOT_FOUND' if $type eq 'actor';
735 $evt = OpenILS::Event->new( $e, id => $id ) unless $cat;
736 return ( $cat, $evt );
739 sub fetch_stat_cat_entry {
740 my( $self, $type, $id ) = @_;
742 $logger->debug("Fetching $type stat cat entry: $id");
743 $entry = $self->simplereq(
745 "open-ils.cstore.direct.$type.stat_cat_entry.retrieve", $id );
747 my $e = 'ASSET_STAT_CAT_ENTRY_NOT_FOUND';
748 $e = 'ACTOR_STAT_CAT_ENTRY_NOT_FOUND' if $type eq 'actor';
750 $evt = OpenILS::Event->new( $e, id => $id ) unless $entry;
751 return ( $entry, $evt );
756 my( $self, $org_tree, $orgid ) = @_;
757 return $org_tree if ( $org_tree->id eq $orgid );
758 return undef unless ref($org_tree->children);
759 for my $c (@{$org_tree->children}) {
760 my $o = $self->find_org($c, $orgid);
766 sub fetch_non_cat_type_by_name_and_org {
767 my( $self, $name, $orgId ) = @_;
768 $logger->debug("Fetching non cat type $name at org $orgId");
769 my $types = $self->simplereq(
771 'open-ils.cstore.direct.config.non_cataloged_type.search.atomic',
772 { name => $name, owning_lib => $orgId } );
773 return ($types->[0], undef) if($types and @$types);
774 return (undef, OpenILS::Event->new('CONFIG_NON_CATALOGED_TYPE_NOT_FOUND') );
777 sub fetch_non_cat_type {
778 my( $self, $id ) = @_;
779 $logger->debug("Fetching non cat type $id");
781 $type = $self->simplereq(
783 'open-ils.cstore.direct.config.non_cataloged_type.retrieve', $id );
784 $evt = OpenILS::Event->new('CONFIG_NON_CATALOGED_TYPE_NOT_FOUND') unless $type;
785 return ($type, $evt);
788 sub DB_UPDATE_FAILED {
789 my( $self, $payload ) = @_;
790 return OpenILS::Event->new('DATABASE_UPDATE_FAILED',
791 payload => ($payload) ? $payload : undef );
794 sub fetch_circ_duration_by_name {
795 my( $self, $name ) = @_;
797 $dur = $self->simplereq(
799 'open-ils.cstore.direct.config.rules.circ_duration.search.atomic', { name => $name } );
801 $evt = OpenILS::Event->new('CONFIG_RULES_CIRC_DURATION_NOT_FOUND') unless $dur;
805 sub fetch_recurring_fine_by_name {
806 my( $self, $name ) = @_;
808 $obj = $self->simplereq(
810 'open-ils.cstore.direct.config.rules.recuring_fine.search.atomic', { name => $name } );
812 $evt = OpenILS::Event->new('CONFIG_RULES_RECURING_FINE_NOT_FOUND') unless $obj;
816 sub fetch_max_fine_by_name {
817 my( $self, $name ) = @_;
819 $obj = $self->simplereq(
821 'open-ils.cstore.direct.config.rules.max_fine.search.atomic', { name => $name } );
823 $evt = OpenILS::Event->new('CONFIG_RULES_MAX_FINE_NOT_FOUND') unless $obj;
828 my( $self, $method, @params ) = @_;
829 return $self->simplereq(
830 'open-ils.storage', $method, @params );
834 my( $self, $method, @params ) = @_;
835 return $self->simplereq(
836 'open-ils.cstore', $method, @params );
840 my( $self, $e, $name ) = @_;
841 if( $e and ref($e) eq 'HASH' and
842 defined($e->{textcode}) and $e->{textcode} eq $name ) {
849 my( undef, $f, $l ) = caller(0);
850 my( undef, undef, undef, $s ) = caller(1);
853 $logger->debug("LOGMARK: $f:$l:$s");
857 sub fetch_open_circulation {
858 my( $self, $cid ) = @_;
861 my $circ = $self->cstorereq(
862 'open-ils.cstore.direct.action.open_circulation.search',
863 { target_copy => $cid, stop_fines_time => undef } );
864 $evt = OpenILS::Event->new('ACTION_CIRCULATION_NOT_FOUND') unless $circ;
865 return ($circ, $evt);
868 sub fetch_all_open_circulation {
869 my( $self, $cid ) = @_;
872 my $circ = $self->cstorereq(
873 'open-ils.cstore.direct.action.open_circulation.search',
874 { target_copy => $cid, xact_finish => undef } );
875 $evt = OpenILS::Event->new('ACTION_CIRCULATION_NOT_FOUND') unless $circ;
876 return ($circ, $evt);
880 sub copy_status_from_name {
881 my( $self, $name ) = @_;
882 $copy_statuses = $self->fetch_copy_statuses unless $copy_statuses;
883 for my $status (@$copy_statuses) {
884 return $status if( $status->name =~ /$name/i );
889 sub copy_status_to_name {
890 my( $self, $sid ) = @_;
891 $copy_statuses = $self->fetch_copy_statuses unless $copy_statuses;
892 for my $status (@$copy_statuses) {
893 return $status->name if( $status->id == $sid );
900 my( $self, $arg ) = @_;
901 return $arg if ref $arg;
902 $copy_statuses = $self->fetch_copy_statuses unless $copy_statuses;
903 my ($stat) = grep { $_->id == $arg } @$copy_statuses;
907 sub fetch_open_transit_by_copy {
908 my( $self, $copyid ) = @_;
910 $transit = $self->cstorereq(
911 'open-ils.cstore.direct.action.transit_copy.search',
912 { target_copy => $copyid, dest_recv_time => undef });
913 $evt = OpenILS::Event->new('ACTION_TRANSIT_COPY_NOT_FOUND') unless $transit;
914 return ($transit, $evt);
918 my( $self, $copy ) = @_;
919 return undef unless $copy;
920 $copy->status( $copy->status->id ) if ref($copy->status);
921 $copy->location( $copy->location->id ) if ref($copy->location);
922 $copy->circ_lib( $copy->circ_lib->id ) if ref($copy->circ_lib);
926 # un-fleshes a copy and updates it in the DB
927 # returns a DB_UPDATE_FAILED event on error
928 # returns undef on success
930 my( $self, %params ) = @_;
932 my $copy = $params{copy} || die "update_copy(): copy required";
933 my $editor = $params{editor} || die "update_copy(): copy editor required";
934 my $session = $params{session};
936 $logger->debug("Updating copy in the database: " . $copy->id);
938 $self->unflesh_copy($copy);
939 $copy->editor( $editor );
940 $copy->edit_date( 'now' );
943 my $meth = 'open-ils.storage.direct.asset.copy.update';
945 $s = $session->request( $meth, $copy )->gather(1) if $session;
946 $s = $self->storagereq( $meth, $copy ) unless $session;
948 $logger->debug("Update of copy ".$copy->id." returned: $s");
950 return $self->DB_UPDATE_FAILED($copy) unless $s;
954 sub fetch_billable_xact {
955 my( $self, $id ) = @_;
957 $logger->debug("Fetching billable transaction %id");
958 $xact = $self->cstorereq(
959 'open-ils.cstore.direct.money.billable_transaction.retrieve', $id );
960 $evt = OpenILS::Event->new('MONEY_BILLABLE_TRANSACTION_NOT_FOUND') unless $xact;
961 return ($xact, $evt);
964 sub fetch_billable_xact_summary {
965 my( $self, $id ) = @_;
967 $logger->debug("Fetching billable transaction summary %id");
968 $xact = $self->cstorereq(
969 'open-ils.cstore.direct.money.billable_transaction_summary.retrieve', $id );
970 $evt = OpenILS::Event->new('MONEY_BILLABLE_TRANSACTION_NOT_FOUND') unless $xact;
971 return ($xact, $evt);
974 sub fetch_fleshed_copy {
975 my( $self, $id ) = @_;
977 $logger->info("Fetching fleshed copy $id");
978 $copy = $self->cstorereq(
979 "open-ils.cstore.direct.asset.copy.retrieve", $id,
981 flesh_fields => { acp => [ qw/ circ_lib location status stat_cat_entries / ] }
984 $evt = OpenILS::Event->new('ASSET_COPY_NOT_FOUND', id => $id) unless $copy;
985 return ($copy, $evt);
989 # returns the org that owns the callnumber that the copy
991 sub fetch_copy_owner {
992 my( $self, $copyid ) = @_;
993 my( $copy, $cn, $evt );
994 $logger->debug("Fetching copy owner $copyid");
995 ($copy, $evt) = $self->fetch_copy($copyid);
996 return (undef,$evt) if $evt;
997 ($cn, $evt) = $self->fetch_callnumber($copy->call_number);
998 return (undef,$evt) if $evt;
999 return ($cn->owning_lib);
1002 sub fetch_copy_note {
1003 my( $self, $id ) = @_;
1005 $logger->debug("Fetching copy note $id");
1006 $note = $self->cstorereq(
1007 'open-ils.cstore.direct.asset.copy_note.retrieve', $id );
1008 $evt = OpenILS::Event->new('ASSET_COPY_NOTE_NOT_FOUND', id => $id ) unless $note;
1009 return ($note, $evt);
1012 sub fetch_call_numbers_by_title {
1013 my( $self, $titleid ) = @_;
1014 $logger->info("Fetching call numbers by title $titleid");
1015 return $self->cstorereq(
1016 'open-ils.cstore.direct.asset.call_number.search.atomic',
1017 { record => $titleid, deleted => 'f' });
1018 #'open-ils.storage.direct.asset.call_number.search.record.atomic', $titleid);
1021 sub fetch_copies_by_call_number {
1022 my( $self, $cnid ) = @_;
1023 $logger->info("Fetching copies by call number $cnid");
1024 return $self->cstorereq(
1025 'open-ils.cstore.direct.asset.copy.search.atomic', { call_number => $cnid, deleted => 'f' } );
1026 #'open-ils.storage.direct.asset.copy.search.call_number.atomic', $cnid );
1029 sub fetch_user_by_barcode {
1030 my( $self, $bc ) = @_;
1031 my $cardid = $self->cstorereq(
1032 'open-ils.cstore.direct.actor.card.id_list', { barcode => $bc } );
1033 return (undef, OpenILS::Event->new('ACTOR_CARD_NOT_FOUND', barcode => $bc)) unless $cardid;
1034 my $user = $self->cstorereq(
1035 'open-ils.cstore.direct.actor.user.search', { card => $cardid } );
1036 return (undef, OpenILS::Event->new('ACTOR_USER_NOT_FOUND', card => $cardid)) unless $user;
1042 # ---------------------------------------------------------------------
1043 # Updates and returns the patron penalties
1044 # ---------------------------------------------------------------------
1045 sub update_patron_penalties {
1046 my( $self, %args ) = @_;
1047 return $self->simplereq(
1049 'open-ils.penalty.patron_penalty.calculate',
1050 { update => 1, %args }
1055 my( $self, $billid ) = @_;
1056 $logger->debug("Fetching billing $billid");
1057 my $bill = $self->cstorereq(
1058 'open-ils.cstore.direct.money.billing.retrieve', $billid );
1059 my $evt = OpenILS::Event->new('MONEY_BILLING_NOT_FOUND') unless $bill;
1060 return($bill, $evt);
1066 sub fetch_org_tree {
1068 return $ORG_TREE if $ORG_TREE;
1069 return $ORG_TREE = OpenILS::Utils::CStoreEditor->new->search_actor_org_unit(
1071 {"parent_ou" => undef },
1074 flesh_fields => { aou => ['children'] },
1075 order_by => { aou => 'name'}
1082 my( $self, $node, $callback ) = @_;
1083 return unless $node;
1085 if( $node->children ) {
1086 $self->walk_org_tree($_, $callback) for @{$node->children};
1091 my( $self, $item ) = @_;
1092 return 1 if $item and $item !~ /^f$/i;
1097 # This logic now lives in storage
1098 sub __patron_money_owed {
1099 my( $self, $patronid ) = @_;
1100 my $ses = OpenSRF::AppSession->create('open-ils.storage');
1101 my $req = $ses->request(
1102 'open-ils.storage.money.billable_transaction.summary.search',
1103 { usr => $patronid, xact_finish => undef } );
1107 while( $data = $req->recv ) {
1108 $data = $data->content;
1109 $total += $data->balance_owed;
1114 sub patron_money_owed {
1115 my( $self, $userid ) = @_;
1116 return $self->storagereq(
1117 'open-ils.storage.actor.user.total_owed', $userid);
1120 sub patron_total_items_out {
1121 my( $self, $userid ) = @_;
1122 return $self->storagereq(
1123 'open-ils.storage.actor.user.total_out', $userid);
1129 #---------------------------------------------------------------------
1130 # Returns ($summary, $event)
1131 #---------------------------------------------------------------------
1135 my $editor = shift || OpenILS::Utils::CStoreEditor->new;
1137 $id = $id->id if (ref($id));
1139 my $xact = $editor->retrieve_money_billable_transaction(
1143 flesh_fields => { mbt => [ qw/billings payments grocery circulation/ ] }
1146 ) or return (undef, $editor->event);
1148 return $self->make_mbts($xact);
1152 #---------------------------------------------------------------------
1153 # Given a list of money.billable_transaction objects, this creates
1154 # transaction summary objects for each
1155 #--------------------------------------------------------------------
1161 for my $x (@xacts) {
1163 my $s = new Fieldmapper::money::billable_transaction_summary;
1167 $s->xact_start( $x->xact_start );
1168 $s->xact_finish( $x->xact_finish );
1172 for my $b (@{ $x->billings }) {
1173 next if ($self->is_true($b->voided));
1174 $to += ($b->amount * 100);
1175 $lb ||= $b->billing_ts;
1176 if ($b->billing_ts ge $lb) {
1177 $lb = $b->billing_ts;
1178 $s->last_billing_note($b->note);
1179 $s->last_billing_ts($b->billing_ts);
1180 $s->last_billing_type($b->billing_type);
1184 $s->total_owed( sprintf('%0.2f', $to / 100 ) );
1188 for my $p (@{ $x->payments }) {
1189 next if ($self->is_true($p->voided));
1190 $tp += ($p->amount * 100);
1191 $lp ||= $p->payment_ts;
1192 if ($p->payment_ts ge $lp) {
1193 $lp = $p->payment_ts;
1194 $s->last_payment_note($p->note);
1195 $s->last_payment_ts($p->payment_ts);
1196 $s->last_payment_type($p->payment_type);
1200 $s->total_paid( sprintf('%0.2f', $tp / 100 ) );
1201 $s->balance_owed( sprintf('%0.2f', ($to - $tp) / 100) );
1202 $s->xact_type('grocery') if ($x->grocery);
1203 $s->xact_type('circulation') if ($x->circulation);
1205 $logger->debug("Created mbts with balance_owed = ". $s->balance_owed);
1214 sub ou_ancestor_setting_value {
1215 my $obj = ou_ancestor_setting(@_);
1216 return ($obj) ? $obj->{value} : undef;
1219 sub ou_ancestor_setting {
1220 my( $self, $orgid, $name, $e ) = @_;
1221 $e = $e || OpenILS::Utils::CStoreEditor->new;
1224 my $setting = $e->search_actor_org_unit_setting({org_unit=>$orgid, name=>$name})->[0];
1227 $logger->info("found org_setting $name at org $orgid : " . $setting->value);
1228 return { org => $orgid, value => JSON->JSON2perl($setting->value) };
1231 my $org = $e->retrieve_actor_org_unit($orgid) or return $e->event;
1232 $orgid = $org->parent_ou or return undef;
1240 # returns the ISO8601 string representation of the requested epoch in GMT
1242 my( $self, $epoch ) = @_;
1243 my ($sec,$min,$hour,$mday,$mon,$year) = gmtime($epoch);
1244 $year += 1900; $mon += 1;
1246 '%s-%0.2d-%0.2dT%0.2d:%0.2d:%0.2d-00',
1247 $year, $mon, $mday, $hour, $min, $sec);