]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/SIP/Transaction/Checkout.pm
d2ada4efb0449b3cb3199df1727a45cdb8b01a6a
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / SIP / Transaction / Checkout.pm
1 #
2 # An object to handle checkout status
3 #
4
5 package OpenILS::SIP::Transaction::Checkout;
6
7 use warnings;
8 use strict;
9
10 use POSIX qw(strftime);
11
12 use OpenILS::SIP;
13 use OpenILS::SIP::Transaction;
14 use OpenILS::SIP::Msg qw/:const/;
15 use Sys::Syslog qw(syslog);
16
17 use OpenILS::Application::AppUtils;
18 my $U = 'OpenILS::Application::AppUtils';
19
20
21 our @ISA = qw(OpenILS::SIP::Transaction);
22
23 # Most fields are handled by the Transaction superclass
24 my %fields = (
25               security_inhibit => 0,
26               due              => undef,
27               renew_ok         => 0,
28               );
29
30 sub new {
31          my $class = shift;
32
33     my $self = $class->SUPER::new(@_);
34
35     my $element;
36
37         foreach $element (keys %fields) {
38                 $self->{_permitted}->{$element} = $fields{$element};
39         }
40
41     @{$self}{keys %fields} = values %fields;
42          
43     return bless $self, $class;
44 }
45
46
47 # if this item is already checked out to the requested patron,
48 # renew the item and set $self->renew_ok to true.  
49 # XXX if it's a renewal and the renewal is not permitted, set 
50 # $self->screen_msg("Item on Hold for Another User"); (or somesuch)
51 # XXX Set $self->ok(0) on any errors
52 sub do_checkout {
53         my $self = shift;
54         syslog('LOG_DEBUG', "OILS: performing checkout...");
55
56         $self->ok(0); 
57
58         my $args = { 
59                 barcode => $self->{item}->id, 
60                 patron_barcode => $self->{patron}->id
61         };
62
63         my $resp = $U->simplereq(
64                 'open-ils.circ',
65                 'open-ils.circ.checkout.permit', 
66                 $self->{authtoken}, $args );
67
68         $resp = [$resp] unless ref $resp eq 'ARRAY';
69
70         my $key;
71
72         syslog('LOG_DEBUG', "OILS: Checkout permit returned event: " . JSON->perl2JSON($resp));
73
74         if( @$resp == 1 and ! $U->event_code($$resp[0]) ) {
75                 $key = $$resp[0]->{payload};
76                 syslog('LOG_INFO', "OILS: circ permit key => $key");
77
78         } else {
79
80                 # We got one or more non-success events
81                 $self->screen_msg('');
82                 for my $r (@$resp) {
83
84                         if( my $code = $U->event_code($resp) ) {
85                                 my $txt = $resp->{textcode};
86                                 syslog('LOG_INFO', "OILS: Checkout permit failed with event $code : $txt");
87
88                                 if( $txt eq 'OPEN_CIRCULATION_EXISTS' ) {
89                                         $self->screen_msg(OILS_SIP_MSG_CIRC_EXISTS);
90                                         return 0;
91                                 } else {
92                                         $self->screen_msg(OILS_SIP_MSG_CIRC_PERMIT_FAILED);
93                                 }
94                         }
95                 }
96                 return 0;
97         }
98
99         # --------------------------------------------------------------------
100         # Now do the actual checkout
101         # --------------------------------------------------------------------
102
103         $args = { 
104                 permit_key              => $key, 
105                 patron_barcode => $self->{patron}->id, 
106                 barcode                 => $self->{item}->id
107         };
108
109         $resp = $U->simplereq(
110                 'open-ils.circ',
111                 'open-ils.circ.checkout', $self->{authtoken}, $args );
112
113
114         syslog('LOG_DEBUG', "OILS: Checkout returned event: " . JSON->perl2JSON($resp));
115
116         # XXX Check for events
117         if( $resp ) {
118
119                 if( my $code = $U->event_code($resp) ) {
120                         my $txt = $resp->{textcode};
121                         syslog('LOG_INFO', "OILS: Checkout failed with event $code : $txt");
122                         $self->screen_msg('Checkout failed.  Please contact a librarian');
123                         return 0; 
124                 }
125
126                 syslog('LOG_INFO', "OILS: Checkout succeeded");
127
128                 my $circ = $resp->{payload}->{circ};
129                 $self->{'due'} = OpenILS::SIP->format_date($circ->due_date, 'due');
130                 $self->ok(1);
131
132                 return 1;
133         }
134
135         return 0;
136 }
137
138
139
140 1;