]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Circ/ScriptBuilder.pm
cleaning up, testing
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Circ / ScriptBuilder.pm
1 package OpenILS::Application::Circ::ScriptBuilder;
2 use strict; use warnings;
3 use OpenILS::Utils::ScriptRunner;
4 use OpenILS::Utils::CStoreEditor qw/:funcs/;
5 use OpenILS::Application::AppUtils;
6 use OpenILS::Application::Actor;
7 use OpenSRF::Utils::Logger qw/$logger/;
8 my $U = "OpenILS::Application::AppUtils";
9 use Data::Dumper;
10
11 my $evt = "environment";
12 my @COPY_STATUSES;
13 my @COPY_LOCATIONS;
14 my @GROUP_LIST;
15
16
17 # -----------------------------------------------------------------------
18 # Possible Args:
19 #  copy
20 #  copy_id
21 #  copy_barcode
22 #
23 #  patron
24 #  patron_id
25 #  patron_barcode
26 #
27 #  fetch_patron_circ_info - load info on items out, overdues, and fines.
28 #
29 #  _direct - this is a hash of key/value pairs to shove directly into the 
30 #  script runner.  Use this to cover data not covered by this module
31 # -----------------------------------------------------------------------
32 sub build {
33         my( $class, $args ) = @_;
34
35         my $evt;
36         my @evts;
37
38         my $editor = $$args{editor} || new_editor();
39
40         $args->{_direct} = {} unless $args->{_direct};
41         
42         $evt = fetch_bib_data($editor, $args);
43         push(@evts, $evt) if $evt;
44         $evt = fetch_user_data($editor, $args);
45         push(@evts, $evt) if $evt;
46         $args->{_event} = \@evts;
47         return build_runner($editor, $args);
48 }
49
50
51 sub build_runner {
52         my $editor      = shift;
53         my $ctx         = shift;
54         my $runner      = OpenILS::Utils::ScriptRunner->new;
55
56         $runner->insert( "$evt.patron",         $ctx->{patron}, 1);
57         $runner->insert( "$evt.copy",                   $ctx->{copy}, 1);
58         $runner->insert( "$evt.volume",         $ctx->{volume}, 1);
59         $runner->insert( "$evt.title",          $ctx->{title}, 1);
60         $runner->insert( "$evt.requestor",      $ctx->{requestor}, 1);
61         $runner->insert( "$evt.titleDescriptor", $ctx->{titleDescriptor}, 1);
62
63         $runner->insert( "$evt.patronItemsOut", $ctx->{patronItemsOut}, 1 );
64         $runner->insert( "$evt.patronOverdueCount", $ctx->{patronOverdue}, 1 );
65         $runner->insert( "$evt.patronFines", $ctx->{patronFines}, 1 );
66
67         # circ script result
68         $runner->insert("result", {});
69         $runner->insert("result.event", 'SUCCESS');
70         $runner->insert("result.events", []);
71         $runner->insert('result.fatalEvents', []);
72         $runner->insert('result.infoEvents', []);
73
74         $runner->insert("$evt.$_", $ctx->{_direct}->{$_}) for keys %{$ctx->{_direct}};
75
76         $ctx->{runner} = $runner;
77         return $runner;
78 }
79
80 sub fetch_bib_data {
81         my $e = shift;
82         my $ctx = shift;
83
84         if(!$ctx->{copy}) {
85
86                 if($ctx->{copy_id}) {
87                         $ctx->{copy} = $e->retrieve_asset_copy($ctx->{copy_id})
88                                 or return $e->event;
89
90                 } elsif( $ctx->{copy_barcode} ) {
91
92                         $ctx->{copy} = $e->search_asset_copy(
93                                 {barcode => $ctx->{copy_barcode}}) or return $e->event;
94                         $ctx->{copy} = $ctx->{copy}->[0];
95                 }
96         }
97
98         return undef unless my $copy = $ctx->{copy};
99
100         # --------------------------------------------------------------------
101         # Fetch/Cache the copy status and location objects
102         # --------------------------------------------------------------------
103         if(!@COPY_STATUSES) {
104                 my $s = $e->retrieve_all_config_copy_status();
105                 @COPY_STATUSES = @$s;
106                 $s = $e->retrieve_all_asset_copy_location();
107                 @COPY_LOCATIONS = @$s;
108         }
109
110         # Flesh the status and location
111         $copy->status( 
112                 grep { $_->id == $copy->status } @COPY_STATUSES ) 
113                 unless ref $copy->status;
114
115         $copy->location( 
116                 grep { $_->id == $copy->location } @COPY_LOCATIONS ) 
117                 unless ref $copy->location;
118
119         $copy->circ_lib( 
120                 $e->retrieve_actor_org_unit($copy->circ_lib)) 
121                 unless ref $copy->circ_lib;
122
123         $ctx->{volume} = $e->retrieve_asset_call_number(
124                 $ctx->{copy}->call_number) or return $e->event;
125
126         $ctx->{title} = $e->retrieve_biblio_record_entry(
127                 $ctx->{volume}->record) or return $e->event;
128
129         if(!$ctx->{titleDescriptor}) {
130                 $ctx->{titleDescriptor} = $e->search_metabib_record_descriptor( 
131                         { record => $ctx->{title}->id }) or return $e->event;
132
133                 $ctx->{titleDescriptor} = $ctx->{titleDescriptor}->[0];
134         }
135
136         return undef;
137 }
138
139
140
141 sub fetch_user_data {
142         my( $e, $ctx ) = @_;
143         
144         if(!$ctx->{patron}) {
145
146                 if( $ctx->{patron_id} ) {
147                         $ctx->{patron} = $e->retrieve_actor_user($ctx->{patron_id});
148
149                 } elsif( $ctx->{patron_barcode} ) {
150
151                         my $card = $e->search_actor_card( 
152                                 { barcode => $ctx->{patron_barcode} } ) or return $e->event;
153
154                         $ctx->{patron} = $e->search_actor_user( 
155                                 { card => $card->[0]->id }) or return $e->event;
156                         $ctx->{patron} = $ctx->{patron}->[0];
157                 }
158         }
159
160         return undef unless my $patron = $ctx->{patron};
161
162         $patron->home_ou( 
163                 $e->retrieve_actor_org_unit($patron->home_ou) ) 
164                 unless ref $patron->home_ou;
165
166
167         if(!@GROUP_LIST) {
168                 my $s = $e->retrieve_all_permission_grp_tree();
169                 @GROUP_LIST = @$s;
170         }
171
172         $patron->profile( 
173                 grep { $_->id == $patron->profile } @GROUP_LIST ) 
174                 unless ref $patron->profile;
175
176         $patron->card($e->retrieve_actor_card($patron->card));
177
178         $ctx->{requestor} = $ctx->{requestor} || $e->requestor;
179
180         # this could alter the requestor object within the editor..
181         #if( my $req = $ctx->{requestor} ) {
182         #       $req->home_ou( $e->retrieve_actor_org_unit($requestor->home_ou) );      
183         #       $req->ws_ou( $e->retrieve_actor_org_unit($requestor->ws_ou) );  
184         #}
185
186         if( $ctx->{fetch_patron_circ_info} ) {
187
188                 my $circ_counts = 
189                         OpenILS::Application::Actor::_checked_out(1, $e, $patron->id);
190
191                 $ctx->{patronOverdue} = $circ_counts->{overdue} || 0;
192                 $ctx->{patronItemsOut} = $ctx->{patronOverdue} + $circ_counts->{out};
193
194                 # Grab the fines
195                 my $fxacts = $e->search_money_open_billable_transaction_summary(
196                         { usr => $patron->id, balance_owed => { ">" => 0 } });
197
198                 my $fines = 0;
199                 $fines += $_->balance_owed for @$fxacts;
200                 $ctx->{patronFines} = $fines;
201
202                 $logger->debug("script_builder: patron fines determined to be $fines");
203                 $logger->debug("script_builder: patron overdue count is " . $ctx->{patronOverdue});
204         }
205
206         return undef;
207 }
208
209 1;
210