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