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