]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Circ/Circulate.pm
removed test line
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Circ / Circulate.pm
1 package OpenILS::Application::Circ::Circulate;
2 use base 'OpenSRF::Application';
3 use strict; use warnings;
4 use Data::Dumper;
5 use OpenSRF::EX qw(:try);
6 use OpenSRF::Utils::Logger qw(:logger);
7 use OpenILS::Utils::ScriptRunner;
8 use OpenILS::Application::AppUtils;
9 my $apputils = "OpenILS::Application::AppUtils";
10
11 my %scripts;            # - circulation script filenames
12 my $standings;          # - cached patron standings
13 my $group_tree; # - cached permission group tree
14
15 # ------------------------------------------------------------------------------
16 # Load the circ script from the config
17 # ------------------------------------------------------------------------------
18 sub initialize {
19
20         my $self = shift;
21         my $conf = OpenSRF::Utils::SettingsClient->new;
22         my @pfx = ( "apps", "open-ils.circ","app_settings", "scripts" );
23
24         my $p           = $conf->config_value(  @pfx, 'permission' );
25         my $d           = $conf->config_value(  @pfx, 'duration' );
26         my $f           = $conf->config_value(  @pfx, 'recurring_fines' );
27         my $m           = $conf->config_value(  @pfx, 'max_fines' );
28         my $pr  = $conf->config_value(  @pfx, 'permit_renew' );
29         my $ph  = $conf->config_value(  @pfx, 'permit_hold' );
30
31         $logger->error( "Missing circ script(s)" ) 
32                 unless( $p and $d and $f and $m and $pr and $ph );
33
34         $scripts{circ_permit}                   = $p;
35         $scripts{circ_duration}                 = $d;
36         $scripts{circ_recurring_fines}= $f;
37         $scripts{circ_max_fines}                = $m;
38         $scripts{circ_renew_permit}     = $pr;
39         $scripts{hold_permit}                   = $ph;
40
41         $logger->debug("Loaded rules scripts for circ: " .
42                 "circ permit : $p, circ duration :$d , circ recurring fines : $f, " .
43                 "circ max fines : $m, circ renew permit : $pr, permit hold: $ph");
44 }
45
46
47 # ------------------------------------------------------------------------------
48 # Loads the necessary circ objects and pushes them into the script environment
49 # Returns ( $data, $evt ).  if $evt is defined, then an
50 # unexpedted event occurred and should be dealt with / returned to the caller
51 # ------------------------------------------------------------------------------
52 sub create_circ_env {
53         my %params = @_;
54
55         my $barcode = $params{barcode};
56         my $patron      = $params{patron};
57         my $summary = $params{fetch_patron_circ_summary};
58
59         my ( $copy, $title, $evt );
60
61         if(!$standings) {
62                 $standings = $apputils->fetch_patron_standings();
63                 $group_tree = $apputils->fetch_permission_group_tree();
64         }
65
66         ( $copy, $evt ) = $apputils->fetch_copy_by_barcode( $barcode );
67         return ( undef, $evt ) if $evt;
68
69         ( $title, $evt ) = $apputils->fetch_record_by_copy( $copy->id );
70         return ( undef, $evt ) if $evt;
71
72         $summary = $apputils->fetch_patron_circ_summary($patron->id) if $summary;
73
74         _doctor_circ_objects( $patron, $title, $copy, $summary );
75
76         my $runner = _build_circ_script_runner( $patron, $title, $copy, $summary );
77
78         return { 
79                 runner                  => $runner, 
80                 title                           => $title, 
81                 patron                  => $patron, 
82                 copy                            => $copy, 
83                 standings               => $standings, 
84                 group_tree              => $group_tree,
85                 circ_summary    => $summary, 
86         };
87 }
88
89
90 # ------------------------------------------------------------------------------
91 # Patches up circ objects to make them easier to use from within the script
92 # environment
93 # ------------------------------------------------------------------------------
94 sub _doctor_circ_objects {
95         my( $patron, $title, $copy, $summary ) = @_;
96         for my $s (@$standings) {
97                 $patron->standing( $s->value) if( $s->id eq $patron->standing);
98         }
99 }
100
101
102 # ------------------------------------------------------------------------------
103 # Constructs and shoves data into the script environment
104 # ------------------------------------------------------------------------------
105 sub _build_circ_script_runner {
106         my( $patron, $title, $copy, $summary ) = @_;
107
108         my $runner = OpenILS::Utils::ScriptRunner->new( type => 'js' );
109
110         $runner->insert( 'patron',              $patron );
111         $runner->insert( 'title',               $title );
112         $runner->insert( 'copy',                $copy );
113         $runner->insert( 'standings', $standings );
114         $runner->insert( 'group_tree', $group_tree );
115
116         # circ script result
117         $runner->insert( 'result', {} );
118         $runner->insert( 'result.event', 'SUCCESS' );
119
120         if($summary) {
121                 $runner->insert( 'patron_info', {} );
122                 $runner->insert( 'patron_info.copy_count', $summary->[0] );
123                 $runner->insert( 'patron_info.fines', $summary->[1] );
124         }
125
126         return $runner;
127 }
128
129
130 # ------------------------------------------------------------------------------
131
132 __PACKAGE__->register_method(
133         method  => "permit_circ",
134         api_name        => "open-ils.circ.permit_checkout_",
135         notes           => <<"  NOTES");
136                 Determines if the given checkout can occur
137                 PARAMS( authtoken, barcode => bc, patron => pid, renew => t/f )
138                 Returns an event
139         NOTES
140
141 sub permit_circ {
142         my( $self, $client, $authtoken, %params ) = @_;
143         my $barcode             = $params{barcode};
144         my $patronid    = $params{patron};
145         my $isrenew             = $params{renew};
146         my ( $requestor, $patron, $env, $evt );
147
148         # check permisson of the requestor
149         ( $requestor, $patron, $evt ) = $apputils->checkses_requestor( 
150                 $authtoken, $patronid, 'VIEW_PERMIT_CHECKOUT' );
151         return $evt if $evt;
152
153         # fetch and build the circulation environment
154         ( $env, $evt ) = create_circ_env( barcode => $barcode, 
155                 patron => $patron, fetch_patron_circ_summary => 1 );
156         return $evt if $evt;
157
158         my $runner = $env->{runner};
159         $runner->load($scripts{circ_permit});
160         $runner->run or throw OpenSRF::EX::ERROR ("Circ Permit Script Died");
161
162         return OpenILS::Event->new($runner->retrieve('result.event'));
163 }
164
165
166 # ------------------------------------------------------------------------------
167
168 __PACKAGE__->register_method(
169         method  => "circulate",
170         api_name        => "open-ils.circ.checkout.barcode_",
171         notes           => <<"  NOTES");
172                 Checks out an item based on barcode
173                 PARAMS( authtoken, barcode => bc, patron => pid )
174         NOTES
175
176 sub circulate {
177         my( $self, $client, $authtoken, %params ) = @_;
178         my $barcode             = $params{barcode};
179         my $patronid    = $params{patron};
180 }
181
182
183 # ------------------------------------------------------------------------------
184
185 __PACKAGE__->register_method(
186         method  => "checkin",
187         api_name        => "open-ils.circ.checkin.barcode_",
188         notes           => <<"  NOTES");
189         PARAMS( authtoken, barcode => bc )
190         Checks in based on barcode
191         Returns an event object whose payload contains the record, circ, and copy
192         If the item needs to be routed, the event is a ROUTE_COPY event
193         with an additional 'route_to' variable set on the event
194         NOTES
195
196 sub checkin {
197         my( $self, $client, $authtoken, %params ) = @_;
198         my $barcode             = $params{barcode};
199 }
200
201 # ------------------------------------------------------------------------------
202
203 __PACKAGE__->register_method(
204         method  => "renew",
205         api_name        => "open-ils.circ.renew_",
206         notes           => <<"  NOTES");
207         PARAMS( authtoken, circ => circ_id );
208         open-ils.circ.renew(login_session, circ_object);
209         Renews the provided circulation.  login_session is the requestor of the
210         renewal and if the logged in user is not the same as circ->usr, then
211         the logged in user must have RENEW_CIRC permissions.
212         NOTES
213
214 sub renew {
215         my( $self, $client, $authtoken, %params ) = @_;
216         my $circ        = $params{circ};
217 }
218
219         
220
221
222 1;