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);
444 my( $self, $holdid ) = @_;
447 $logger->debug("Fetching hold $holdid from cstore");
449 $hold = $self->simplereq(
451 'open-ils.cstore.direct.action.hold_request.retrieve', $holdid);
453 $evt = OpenILS::Event->new('ACTION_HOLD_REQUEST_NOT_FOUND', holdid => $holdid) unless $hold;
455 return ($hold, $evt);
459 sub fetch_hold_transit_by_hold {
460 my( $self, $holdid ) = @_;
461 my( $transit, $evt );
463 $logger->debug("Fetching transit by hold $holdid from cstore");
465 $transit = $self->simplereq(
467 'open-ils.cstore.direct.action.hold_transit_copy.search', { hold => $holdid } );
469 $evt = OpenILS::Event->new('ACTION_HOLD_TRANSIT_COPY_NOT_FOUND', holdid => $holdid) unless $transit;
471 return ($transit, $evt );
474 # fetches the captured, but not fulfilled hold attached to a given copy
475 sub fetch_open_hold_by_copy {
476 my( $self, $copyid ) = @_;
477 $logger->debug("Searching for active hold for copy $copyid");
480 $hold = $self->cstorereq(
481 'open-ils.cstore.direct.action.hold_request.search',
483 current_copy => $copyid ,
484 capture_time => { "!=" => undef },
485 fulfillment_time => undef,
486 cancel_time => undef,
489 $evt = OpenILS::Event->new('ACTION_HOLD_REQUEST_NOT_FOUND', copyid => $copyid) unless $hold;
490 return ($hold, $evt);
493 sub fetch_hold_transit {
494 my( $self, $transid ) = @_;
495 my( $htransit, $evt );
496 $logger->debug("Fetching hold transit with hold id $transid");
497 $htransit = $self->cstorereq(
498 'open-ils.cstore.direct.action.hold_transit_copy.retrieve', $transid );
499 $evt = OpenILS::Event->new('ACTION_HOLD_TRANSIT_COPY_NOT_FOUND', id => $transid) unless $htransit;
500 return ($htransit, $evt);
503 sub fetch_copy_by_barcode {
504 my( $self, $barcode ) = @_;
507 $logger->debug("Fetching copy by barcode $barcode from cstore");
509 $copy = $self->simplereq( 'open-ils.cstore',
510 'open-ils.cstore.direct.asset.copy.search', { barcode => $barcode, deleted => 'f'} );
511 #'open-ils.storage.direct.asset.copy.search.barcode', $barcode );
513 $evt = OpenILS::Event->new('ASSET_COPY_NOT_FOUND', barcode => $barcode) unless $copy;
515 return ($copy, $evt);
518 sub fetch_open_billable_transaction {
519 my( $self, $transid ) = @_;
520 my( $transaction, $evt );
522 $logger->debug("Fetching open billable transaction $transid from cstore");
524 $transaction = $self->simplereq(
526 'open-ils.cstore.direct.money.open_billable_transaction_summary.retrieve', $transid);
528 $evt = OpenILS::Event->new(
529 'MONEY_OPEN_BILLABLE_TRANSACTION_SUMMARY_NOT_FOUND', transid => $transid ) unless $transaction;
531 return ($transaction, $evt);
537 $buckets{'biblio'} = 'biblio_record_entry_bucket';
538 $buckets{'callnumber'} = 'call_number_bucket';
539 $buckets{'copy'} = 'copy_bucket';
540 $buckets{'user'} = 'user_bucket';
542 sub fetch_container {
543 my( $self, $id, $type ) = @_;
546 $logger->debug("Fetching container $id with type $type");
548 my $e = 'CONTAINER_CALL_NUMBER_BUCKET_NOT_FOUND';
549 $e = 'CONTAINER_BIBLIO_RECORD_ENTRY_BUCKET_NOT_FOUND' if $type eq 'biblio';
550 $e = 'CONTAINER_USER_BUCKET_NOT_FOUND' if $type eq 'user';
551 $e = 'CONTAINER_COPY_BUCKET_NOT_FOUND' if $type eq 'copy';
553 my $meth = $buckets{$type};
554 $bucket = $self->simplereq(
556 "open-ils.cstore.direct.container.$meth.retrieve", $id );
558 $evt = OpenILS::Event->new(
559 $e, container => $id, container_type => $type ) unless $bucket;
561 return ($bucket, $evt);
565 sub fetch_container_e {
566 my( $self, $editor, $id, $type ) = @_;
569 $bucket = $editor->retrieve_container_copy_bucket($id) if $type eq 'copy';
570 $bucket = $editor->retrieve_container_call_number_bucket($id) if $type eq 'callnumber';
571 $bucket = $editor->retrieve_container_biblio_record_entry_bucket($id) if $type eq 'biblio';
572 $bucket = $editor->retrieve_container_user_bucket($id) if $type eq 'user';
574 $evt = $editor->event unless $bucket;
575 return ($bucket, $evt);
578 sub fetch_container_item_e {
579 my( $self, $editor, $id, $type ) = @_;
582 $bucket = $editor->retrieve_container_copy_bucket_item($id) if $type eq 'copy';
583 $bucket = $editor->retrieve_container_call_number_bucket_item($id) if $type eq 'callnumber';
584 $bucket = $editor->retrieve_container_biblio_record_entry_bucket_item($id) if $type eq 'biblio';
585 $bucket = $editor->retrieve_container_user_bucket_item($id) if $type eq 'user';
587 $evt = $editor->event unless $bucket;
588 return ($bucket, $evt);
595 sub fetch_container_item {
596 my( $self, $id, $type ) = @_;
599 $logger->debug("Fetching container item $id with type $type");
601 my $meth = $buckets{$type} . "_item";
603 $bucket = $self->simplereq(
605 "open-ils.cstore.direct.container.$meth.retrieve", $id );
608 my $e = 'CONTAINER_CALL_NUMBER_BUCKET_ITEM_NOT_FOUND';
609 $e = 'CONTAINER_BIBLIO_RECORD_ENTRY_BUCKET_ITEM_NOT_FOUND' if $type eq 'biblio';
610 $e = 'CONTAINER_USER_BUCKET_ITEM_NOT_FOUND' if $type eq 'user';
611 $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 ) = @_;
692 my $e = OpenILS::Event->new( 'ASSET_CALL_NUMBER_NOT_FOUND', id => $id );
693 return( undef, $e ) unless $id;
695 $logger->debug("Fetching callnumber $id");
697 my $cn = $self->simplereq(
699 'open-ils.cstore.direct.asset.call_number.retrieve', $id );
700 $evt = $e unless $cn;
702 return ( $cn, $evt );
705 my %ORG_CACHE; # - these rarely change, so cache them..
707 my( $self, $id ) = @_;
708 return undef unless $id;
709 return $id if( ref($id) eq 'Fieldmapper::actor::org_unit' );
710 return $ORG_CACHE{$id} if $ORG_CACHE{$id};
711 $logger->debug("Fetching org unit $id");
714 my $org = $self->simplereq(
716 'open-ils.cstore.direct.actor.org_unit.retrieve', $id );
717 $evt = OpenILS::Event->new( 'ACTOR_ORG_UNIT_NOT_FOUND', id => $id ) unless $org;
718 $ORG_CACHE{$id} = $org;
724 my( $self, $type, $id ) = @_;
726 $logger->debug("Fetching $type stat cat: $id");
727 $cat = $self->simplereq(
729 "open-ils.cstore.direct.$type.stat_cat.retrieve", $id );
731 my $e = 'ASSET_STAT_CAT_NOT_FOUND';
732 $e = 'ACTOR_STAT_CAT_NOT_FOUND' if $type eq 'actor';
734 $evt = OpenILS::Event->new( $e, id => $id ) unless $cat;
735 return ( $cat, $evt );
738 sub fetch_stat_cat_entry {
739 my( $self, $type, $id ) = @_;
741 $logger->debug("Fetching $type stat cat entry: $id");
742 $entry = $self->simplereq(
744 "open-ils.cstore.direct.$type.stat_cat_entry.retrieve", $id );
746 my $e = 'ASSET_STAT_CAT_ENTRY_NOT_FOUND';
747 $e = 'ACTOR_STAT_CAT_ENTRY_NOT_FOUND' if $type eq 'actor';
749 $evt = OpenILS::Event->new( $e, id => $id ) unless $entry;
750 return ( $entry, $evt );
755 my( $self, $org_tree, $orgid ) = @_;
756 return $org_tree if ( $org_tree->id eq $orgid );
757 return undef unless ref($org_tree->children);
758 for my $c (@{$org_tree->children}) {
759 my $o = $self->find_org($c, $orgid);
765 sub fetch_non_cat_type_by_name_and_org {
766 my( $self, $name, $orgId ) = @_;
767 $logger->debug("Fetching non cat type $name at org $orgId");
768 my $types = $self->simplereq(
770 'open-ils.cstore.direct.config.non_cataloged_type.search.atomic',
771 { name => $name, owning_lib => $orgId } );
772 return ($types->[0], undef) if($types and @$types);
773 return (undef, OpenILS::Event->new('CONFIG_NON_CATALOGED_TYPE_NOT_FOUND') );
776 sub fetch_non_cat_type {
777 my( $self, $id ) = @_;
778 $logger->debug("Fetching non cat type $id");
780 $type = $self->simplereq(
782 'open-ils.cstore.direct.config.non_cataloged_type.retrieve', $id );
783 $evt = OpenILS::Event->new('CONFIG_NON_CATALOGED_TYPE_NOT_FOUND') unless $type;
784 return ($type, $evt);
787 sub DB_UPDATE_FAILED {
788 my( $self, $payload ) = @_;
789 return OpenILS::Event->new('DATABASE_UPDATE_FAILED',
790 payload => ($payload) ? $payload : undef );
793 sub fetch_circ_duration_by_name {
794 my( $self, $name ) = @_;
796 $dur = $self->simplereq(
798 'open-ils.cstore.direct.config.rules.circ_duration.search.atomic', { name => $name } );
800 $evt = OpenILS::Event->new('CONFIG_RULES_CIRC_DURATION_NOT_FOUND') unless $dur;
804 sub fetch_recurring_fine_by_name {
805 my( $self, $name ) = @_;
807 $obj = $self->simplereq(
809 'open-ils.cstore.direct.config.rules.recuring_fine.search.atomic', { name => $name } );
811 $evt = OpenILS::Event->new('CONFIG_RULES_RECURING_FINE_NOT_FOUND') unless $obj;
815 sub fetch_max_fine_by_name {
816 my( $self, $name ) = @_;
818 $obj = $self->simplereq(
820 'open-ils.cstore.direct.config.rules.max_fine.search.atomic', { name => $name } );
822 $evt = OpenILS::Event->new('CONFIG_RULES_MAX_FINE_NOT_FOUND') unless $obj;
827 my( $self, $method, @params ) = @_;
828 return $self->simplereq(
829 'open-ils.storage', $method, @params );
833 my( $self, $method, @params ) = @_;
834 return $self->simplereq(
835 'open-ils.cstore', $method, @params );
839 my( $self, $e, $name ) = @_;
840 if( $e and ref($e) eq 'HASH' and
841 defined($e->{textcode}) and $e->{textcode} eq $name ) {
848 my( undef, $f, $l ) = caller(0);
849 my( undef, undef, undef, $s ) = caller(1);
852 $logger->debug("LOGMARK: $f:$l:$s");
856 sub fetch_open_circulation {
857 my( $self, $cid ) = @_;
860 my $circ = $self->cstorereq(
861 'open-ils.cstore.direct.action.open_circulation.search',
862 { target_copy => $cid, stop_fines_time => undef } );
863 $evt = OpenILS::Event->new('ACTION_CIRCULATION_NOT_FOUND') unless $circ;
864 return ($circ, $evt);
867 sub fetch_all_open_circulation {
868 my( $self, $cid ) = @_;
871 my $circ = $self->cstorereq(
872 'open-ils.cstore.direct.action.open_circulation.search',
873 { target_copy => $cid, xact_finish => undef } );
874 $evt = OpenILS::Event->new('ACTION_CIRCULATION_NOT_FOUND') unless $circ;
875 return ($circ, $evt);
879 sub copy_status_from_name {
880 my( $self, $name ) = @_;
881 $copy_statuses = $self->fetch_copy_statuses unless $copy_statuses;
882 for my $status (@$copy_statuses) {
883 return $status if( $status->name =~ /$name/i );
888 sub copy_status_to_name {
889 my( $self, $sid ) = @_;
890 $copy_statuses = $self->fetch_copy_statuses unless $copy_statuses;
891 for my $status (@$copy_statuses) {
892 return $status->name if( $status->id == $sid );
899 my( $self, $arg ) = @_;
900 return $arg if ref $arg;
901 $copy_statuses = $self->fetch_copy_statuses unless $copy_statuses;
902 my ($stat) = grep { $_->id == $arg } @$copy_statuses;
906 sub fetch_open_transit_by_copy {
907 my( $self, $copyid ) = @_;
909 $transit = $self->cstorereq(
910 'open-ils.cstore.direct.action.transit_copy.search',
911 { target_copy => $copyid, dest_recv_time => undef });
912 $evt = OpenILS::Event->new('ACTION_TRANSIT_COPY_NOT_FOUND') unless $transit;
913 return ($transit, $evt);
917 my( $self, $copy ) = @_;
918 return undef unless $copy;
919 $copy->status( $copy->status->id ) if ref($copy->status);
920 $copy->location( $copy->location->id ) if ref($copy->location);
921 $copy->circ_lib( $copy->circ_lib->id ) if ref($copy->circ_lib);
925 # un-fleshes a copy and updates it in the DB
926 # returns a DB_UPDATE_FAILED event on error
927 # returns undef on success
929 my( $self, %params ) = @_;
931 my $copy = $params{copy} || die "update_copy(): copy required";
932 my $editor = $params{editor} || die "update_copy(): copy editor required";
933 my $session = $params{session};
935 $logger->debug("Updating copy in the database: " . $copy->id);
937 $self->unflesh_copy($copy);
938 $copy->editor( $editor );
939 $copy->edit_date( 'now' );
942 my $meth = 'open-ils.storage.direct.asset.copy.update';
944 $s = $session->request( $meth, $copy )->gather(1) if $session;
945 $s = $self->storagereq( $meth, $copy ) unless $session;
947 $logger->debug("Update of copy ".$copy->id." returned: $s");
949 return $self->DB_UPDATE_FAILED($copy) unless $s;
953 sub fetch_billable_xact {
954 my( $self, $id ) = @_;
956 $logger->debug("Fetching billable transaction %id");
957 $xact = $self->cstorereq(
958 'open-ils.cstore.direct.money.billable_transaction.retrieve', $id );
959 $evt = OpenILS::Event->new('MONEY_BILLABLE_TRANSACTION_NOT_FOUND') unless $xact;
960 return ($xact, $evt);
963 sub fetch_billable_xact_summary {
964 my( $self, $id ) = @_;
966 $logger->debug("Fetching billable transaction summary %id");
967 $xact = $self->cstorereq(
968 'open-ils.cstore.direct.money.billable_transaction_summary.retrieve', $id );
969 $evt = OpenILS::Event->new('MONEY_BILLABLE_TRANSACTION_NOT_FOUND') unless $xact;
970 return ($xact, $evt);
973 sub fetch_fleshed_copy {
974 my( $self, $id ) = @_;
976 $logger->info("Fetching fleshed copy $id");
977 $copy = $self->cstorereq(
978 "open-ils.cstore.direct.asset.copy.retrieve", $id,
980 flesh_fields => { acp => [ qw/ circ_lib location status stat_cat_entries / ] }
983 $evt = OpenILS::Event->new('ASSET_COPY_NOT_FOUND', id => $id) unless $copy;
984 return ($copy, $evt);
988 # returns the org that owns the callnumber that the copy
990 sub fetch_copy_owner {
991 my( $self, $copyid ) = @_;
992 my( $copy, $cn, $evt );
993 $logger->debug("Fetching copy owner $copyid");
994 ($copy, $evt) = $self->fetch_copy($copyid);
995 return (undef,$evt) if $evt;
996 ($cn, $evt) = $self->fetch_callnumber($copy->call_number);
997 return (undef,$evt) if $evt;
998 return ($cn->owning_lib);
1001 sub fetch_copy_note {
1002 my( $self, $id ) = @_;
1004 $logger->debug("Fetching copy note $id");
1005 $note = $self->cstorereq(
1006 'open-ils.cstore.direct.asset.copy_note.retrieve', $id );
1007 $evt = OpenILS::Event->new('ASSET_COPY_NOTE_NOT_FOUND', id => $id ) unless $note;
1008 return ($note, $evt);
1011 sub fetch_call_numbers_by_title {
1012 my( $self, $titleid ) = @_;
1013 $logger->info("Fetching call numbers by title $titleid");
1014 return $self->cstorereq(
1015 'open-ils.cstore.direct.asset.call_number.search.atomic',
1016 { record => $titleid, deleted => 'f' });
1017 #'open-ils.storage.direct.asset.call_number.search.record.atomic', $titleid);
1020 sub fetch_copies_by_call_number {
1021 my( $self, $cnid ) = @_;
1022 $logger->info("Fetching copies by call number $cnid");
1023 return $self->cstorereq(
1024 'open-ils.cstore.direct.asset.copy.search.atomic', { call_number => $cnid, deleted => 'f' } );
1025 #'open-ils.storage.direct.asset.copy.search.call_number.atomic', $cnid );
1028 sub fetch_user_by_barcode {
1029 my( $self, $bc ) = @_;
1030 my $cardid = $self->cstorereq(
1031 'open-ils.cstore.direct.actor.card.id_list', { barcode => $bc } );
1032 return (undef, OpenILS::Event->new('ACTOR_CARD_NOT_FOUND', barcode => $bc)) unless $cardid;
1033 my $user = $self->cstorereq(
1034 'open-ils.cstore.direct.actor.user.search', { card => $cardid } );
1035 return (undef, OpenILS::Event->new('ACTOR_USER_NOT_FOUND', card => $cardid)) unless $user;
1041 # ---------------------------------------------------------------------
1042 # Updates and returns the patron penalties
1043 # ---------------------------------------------------------------------
1044 sub update_patron_penalties {
1045 my( $self, %args ) = @_;
1046 return $self->simplereq(
1048 'open-ils.penalty.patron_penalty.calculate',
1049 { update => 1, %args }
1054 my( $self, $billid ) = @_;
1055 $logger->debug("Fetching billing $billid");
1056 my $bill = $self->cstorereq(
1057 'open-ils.cstore.direct.money.billing.retrieve', $billid );
1058 my $evt = OpenILS::Event->new('MONEY_BILLING_NOT_FOUND') unless $bill;
1059 return($bill, $evt);
1065 sub fetch_org_tree {
1067 return $ORG_TREE if $ORG_TREE;
1068 return $ORG_TREE = OpenILS::Utils::CStoreEditor->new->search_actor_org_unit(
1070 {"parent_ou" => undef },
1073 flesh_fields => { aou => ['children'] },
1074 order_by => { aou => 'name'}
1081 my( $self, $node, $callback ) = @_;
1082 return unless $node;
1084 if( $node->children ) {
1085 $self->walk_org_tree($_, $callback) for @{$node->children};
1090 my( $self, $item ) = @_;
1091 return 1 if $item and $item !~ /^f$/i;
1096 # This logic now lives in storage
1097 sub __patron_money_owed {
1098 my( $self, $patronid ) = @_;
1099 my $ses = OpenSRF::AppSession->create('open-ils.storage');
1100 my $req = $ses->request(
1101 'open-ils.storage.money.billable_transaction.summary.search',
1102 { usr => $patronid, xact_finish => undef } );
1106 while( $data = $req->recv ) {
1107 $data = $data->content;
1108 $total += $data->balance_owed;
1113 sub patron_money_owed {
1114 my( $self, $userid ) = @_;
1115 return $self->storagereq(
1116 'open-ils.storage.actor.user.total_owed', $userid);
1119 sub patron_total_items_out {
1120 my( $self, $userid ) = @_;
1121 return $self->storagereq(
1122 'open-ils.storage.actor.user.total_out', $userid);
1128 #---------------------------------------------------------------------
1129 # Returns ($summary, $event)
1130 #---------------------------------------------------------------------
1134 my $editor = shift || new_editor();
1136 $id = $id->id if (ref($id));
1138 my $xact = $editor->retrieve_money_billable_transaction(
1142 flesh_fields => { mbt => [ qw/billings payments grocery circulation/ ] }
1145 ) or return (undef, $editor->event);
1147 return $self->make_mbts($xact);
1151 #---------------------------------------------------------------------
1152 # Given a list of money.billable_transaction objects, this creates
1153 # transaction summary objects for each
1154 #--------------------------------------------------------------------
1160 for my $x (@xacts) {
1162 my $s = new Fieldmapper::money::billable_transaction_summary;
1166 $s->xact_start( $x->xact_start );
1167 $s->xact_finish( $x->xact_finish );
1171 for my $b (@{ $x->billings }) {
1172 next if ($self->is_true($b->voided));
1173 $to += ($b->amount * 100);
1174 $lb ||= $b->billing_ts;
1175 if ($b->billing_ts ge $lb) {
1176 $lb = $b->billing_ts;
1177 $s->last_billing_note($b->note);
1178 $s->last_billing_ts($b->billing_ts);
1179 $s->last_billing_type($b->billing_type);
1183 $s->total_owed( sprintf('%0.2f', $to / 100 ) );
1187 for my $p (@{ $x->payments }) {
1188 next if ($self->is_true($p->voided));
1189 $tp += ($p->amount * 100);
1190 $lp ||= $p->payment_ts;
1191 if ($p->payment_ts ge $lp) {
1192 $lp = $p->payment_ts;
1193 $s->last_payment_note($p->note);
1194 $s->last_payment_ts($p->payment_ts);
1195 $s->last_payment_type($p->payment_type);
1199 $s->total_paid( sprintf('%0.2f', $tp / 100 ) );
1200 $s->balance_owed( sprintf('%0.2f', ($to - $tp) / 100) );
1201 $s->xact_type( 'grocery' ) if ($x->grocery);
1202 $s->xact_type( 'circulation' ) if ($x->circulation);
1204 $logger->debug("Created mbts with balance_owed = ". $s->balance_owed);