Bug 15208: Followup to reorder words
[koha.git] / opac / sco / sco-main.pl
blobeff549d57b47dabe69046f840e2e110efd50acdb
1 #!/usr/bin/perl
3 # This code has been modified by Trendsetters (originally from opac-user.pl)
4 # This code has been modified by rch
5 # Parts Copyright 2010-2011, ByWater Solutions (those related to username/password auth)
7 # This file is part of Koha.
9 # Koha is free software; you can redistribute it and/or modify it
10 # under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
14 # Koha is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22 # We're going to authenticate a self-check user. we'll add a flag to borrowers 'selfcheck'
24 # We're in a controlled environment; we trust the user.
25 # So the selfcheck station will accept a patronid and issue items to that borrower.
26 # FIXME: NOT really a controlled environment... We're on the internet!
28 # The checkout permission comes form the CGI cookie/session of a staff user.
29 # The patron is not really logging in here in the same way as they do on the
30 # rest of the OPAC. So don't confuse loggedinuser with the patron user.
32 # FIXME: inputfocus not really used in TMPL
34 use strict;
35 use warnings;
37 use CGI qw ( -utf8 );
38 use Digest::MD5 qw(md5_base64);
40 use C4::Auth qw(get_template_and_user checkpw);
41 use C4::Koha;
42 use C4::Circulation;
43 use C4::Reserves;
44 use C4::Output;
45 use C4::Members;
46 use C4::Biblio;
47 use C4::Items;
49 my $query = new CGI;
51 unless (C4::Context->preference('WebBasedSelfCheck')) {
52 # redirect to OPAC home if self-check is not enabled
53 print $query->redirect("/cgi-bin/koha/opac-main.pl");
54 exit;
57 if (C4::Context->preference('AutoSelfCheckAllowed'))
59 my $AutoSelfCheckID = C4::Context->preference('AutoSelfCheckID');
60 my $AutoSelfCheckPass = C4::Context->preference('AutoSelfCheckPass');
61 $query->param(-name=>'userid',-values=>[$AutoSelfCheckID]);
62 $query->param(-name=>'password',-values=>[$AutoSelfCheckPass]);
63 $query->param(-name=>'koha_login_context',-values=>['sco']);
65 my ($template, $loggedinuser, $cookie) = get_template_and_user({
66 template_name => "sco/sco-main.tt",
67 authnotrequired => 0,
68 flagsrequired => { circulate => "self_checkout" },
69 query => $query,
70 type => "opac",
71 debug => 1,
72 });
74 if (C4::Context->preference('SelfCheckoutByLogin'))
76 $template->param(authbylogin => 1);
79 # Get the self checkout timeout preference, or use 120 seconds as a default
80 my $selfchecktimeout = 120000;
81 if (C4::Context->preference('SelfCheckTimeout')) {
82 $selfchecktimeout = C4::Context->preference('SelfCheckTimeout') * 1000;
84 $template->param(SelfCheckTimeout => $selfchecktimeout);
86 # Checks policy laid out by AllowSelfCheckReturns, defaults to 'on' if preference is undefined
87 my $allowselfcheckreturns = 1;
88 if (defined C4::Context->preference('AllowSelfCheckReturns')) {
89 $allowselfcheckreturns = C4::Context->preference('AllowSelfCheckReturns');
91 $template->param(AllowSelfCheckReturns => $allowselfcheckreturns);
94 my $issuerid = $loggedinuser;
95 my ($op, $patronid, $patronlogin, $patronpw, $barcode, $confirmed) = (
96 $query->param("op") || '',
97 $query->param("patronid") || '',
98 $query->param("patronlogin")|| '',
99 $query->param("patronpw") || '',
100 $query->param("barcode") || '',
101 $query->param("confirmed") || '',
104 my $issuenoconfirm = 1; #don't need to confirm on issue.
105 #warn "issuerid: " . $issuerid;
106 my $issuer = GetMemberDetails($issuerid);
107 my $item = GetItem(undef,$barcode);
108 if (C4::Context->preference('SelfCheckoutByLogin') && !$patronid) {
109 my $dbh = C4::Context->dbh;
110 my $resval;
111 ($resval, $patronid) = checkpw($dbh, $patronlogin, $patronpw);
113 my $borrower = GetMemberDetails(undef,$patronid);
115 my $currencySymbol = "";
116 if ( defined C4::Budgets->GetCurrency() ) {
117 $currencySymbol = C4::Budgets->GetCurrency()->{symbol};
120 my $branch = $issuer->{branchcode};
121 my $confirm_required = 0;
122 my $return_only = 0;
123 #warn "issuer cardnumber: " . $issuer->{cardnumber};
124 #warn "patron cardnumber: " . $borrower->{cardnumber};
125 if ($op eq "logout") {
126 $query->param( patronid => undef, patronlogin => undef, patronpw => undef );
128 elsif ( $op eq "returnbook" && $allowselfcheckreturns ) {
129 my ($doreturn) = AddReturn( $barcode, $branch );
130 #warn "returnbook: " . $doreturn;
131 $borrower = GetMemberDetails(undef,$patronid);
133 elsif ( $op eq "checkout" ) {
134 my $impossible = {};
135 my $needconfirm = {};
136 if ( !$confirmed ) {
137 ( $impossible, $needconfirm ) = CanBookBeIssued(
138 $borrower,
139 $barcode,
140 undef,
142 C4::Context->preference("AllowItemsOnHoldCheckout")
145 $confirm_required = scalar keys %$needconfirm;
147 #warn "confirm_required: " . $confirm_required ;
148 if (scalar keys %$impossible) {
150 # warn "impossible: numkeys: " . scalar (keys(%$impossible));
151 #warn join " ", keys %$impossible;
152 my $issue_error = (keys %$impossible)[0];
154 # FIXME we assume only one error.
155 $template->param(
156 impossible => $issue_error,
157 "circ_error_$issue_error" => 1,
158 title => $item->{title},
159 hide_main => 1,
161 if ($issue_error eq 'DEBT') {
162 $template->param(amount => $currencySymbol.$impossible->{DEBT});
164 #warn "issue_error: " . $issue_error ;
165 if ( $issue_error eq "NO_MORE_RENEWALS" ) {
166 $return_only = 1;
167 $template->param(
168 returnitem => 1,
169 barcode => $barcode,
172 } elsif ( $needconfirm->{RENEW_ISSUE} ) {
173 if ($confirmed) {
174 #warn "renewing";
175 AddRenewal( $borrower, $item->{itemnumber} );
176 } else {
177 #warn "renew confirmation";
178 $template->param(
179 renew => 1,
180 barcode => $barcode,
181 confirm => 1,
182 confirm_renew_issue => 1,
183 hide_main => 1,
186 } elsif ( $confirm_required && !$confirmed ) {
187 #warn "failed confirmation";
188 my $issue_error = (keys %$needconfirm)[0];
189 $template->param(
190 impossible => (keys %$needconfirm)[0],
191 "circ_error_$issue_error" => 1,
192 hide_main => 1,
194 if ($issue_error eq 'DEBT') {
195 $template->param(amount => $currencySymbol.$needconfirm->{DEBT});
197 } else {
198 if ( $confirmed || $issuenoconfirm ) { # we'll want to call getpatroninfo again to get updated issues.
199 # warn "issuing book?";
200 AddIssue( $borrower, $barcode );
201 # ($borrower, $flags) = getpatroninformation(undef,undef, $patronid);
202 # $template->param(
203 # patronid => $patronid,
204 # validuser => 1,
205 # );
206 } else {
207 $confirm_required = 1;
208 #warn "issue confirmation";
209 $template->param(
210 confirm => "Issuing title: " . $item->{title},
211 barcode => $barcode,
212 hide_main => 1,
213 inputfocus => 'confirm',
217 } # $op
219 if ($borrower->{cardnumber}) {
220 # warn "issuer's branchcode: " . $issuer->{branchcode};
221 # warn "user's branchcode: " . $borrower->{branchcode};
222 my $borrowername = sprintf "%s %s", ($borrower->{firstname} || ''), ($borrower->{surname} || '');
223 my @issues;
224 my ($issueslist) = GetPendingIssues( $borrower->{'borrowernumber'} );
225 foreach my $it (@$issueslist) {
226 my ($renewokay, $renewerror) = CanBookBeIssued(
227 $borrower,
228 $it->{'barcode'},
229 undef,
231 C4::Context->preference("AllowItemsOnHoldCheckout")
233 $it->{'norenew'} = 1 if $renewokay->{'NO_MORE_RENEWALS'};
234 push @issues, $it;
237 $template->param(
238 validuser => 1,
239 borrowername => $borrowername,
240 issues_count => scalar(@issues),
241 ISSUES => \@issues,
242 patronid => $patronid,
243 patronlogin => $patronlogin,
244 patronpw => $patronpw,
245 noitemlinks => 1 ,
246 borrowernumber => $borrower->{'borrowernumber'},
248 my $inputfocus = ($return_only == 1) ? 'returnbook' :
249 ($confirm_required == 1) ? 'confirm' : 'barcode' ;
250 $template->param(
251 inputfocus => $inputfocus,
252 nofines => 1,
255 if (C4::Context->preference('ShowPatronImageInWebBasedSelfCheck')) {
256 my ($image, $dberror) = GetPatronImage($borrower->{borrowernumber});
257 if ($image) {
258 $template->param(
259 display_patron_image => 1,
260 cardnumber => $borrower->{cardnumber},
264 } else {
265 $template->param(
266 patronid => $patronid,
267 nouser => $patronid,
271 $template->param(
272 SCOUserJS => C4::Context->preference('SCOUserJS'),
273 SCOUserCSS => C4::Context->preference('SCOUserCSS'),
276 output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };