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)
6 # We're going to authenticate a self-check user. we'll add a flag to borrowers 'selfcheck'
8 # We're in a controlled environment; we trust the user.
9 # So the selfcheck station will accept a patronid and issue items to that borrower.
10 # FIXME: NOT really a controlled environment... We're on the internet!
12 # The checkout permission comes form the CGI cookie/session of a staff user.
13 # The patron is not really logging in here in the same way as they do on the
14 # rest of the OPAC. So don't confuse loggedinuser with the patron user.
16 # FIXME: inputfocus not really used in TMPL
22 use Digest::MD5 qw(md5_base64);
24 use C4::Auth qw(get_template_and_user checkpw);
26 use C4::Dates qw/format_date/;
37 unless (C4::Context->preference('WebBasedSelfCheck')) {
38 # redirect to OPAC home if self-check is not enabled
39 print $query->redirect("/cgi-bin/koha/opac-main.pl");
43 if (C4::Context->preference('AutoSelfCheckAllowed'))
45 my $AutoSelfCheckID = C4::Context->preference('AutoSelfCheckID');
46 my $AutoSelfCheckPass = C4::Context->preference('AutoSelfCheckPass');
47 $query->param(-name=>'userid',-values=>[$AutoSelfCheckID]);
48 $query->param(-name=>'password',-values=>[$AutoSelfCheckPass]);
49 $query->param(-name=>'koha_login_context',-values=>['sco']);
51 my ($template, $loggedinuser, $cookie) = get_template_and_user({
52 template_name => "sco/sco-main.tmpl",
54 flagsrequired => { circulate => "circulate_remaining_permissions" },
59 if (C4::Context->preference('SelfCheckoutByLogin'))
61 $template->param(authbylogin => 1);
64 # Get the self checkout timeout preference, or use 120 seconds as a default
65 my $selfchecktimeout = 120000;
66 if (C4::Context->preference('SelfCheckTimeout')) {
67 $selfchecktimeout = C4::Context->preference('SelfCheckTimeout') * 1000;
69 $template->param(SelfCheckTimeout => $selfchecktimeout);
71 # Checks policy laid out by AllowSelfCheckReturns, defaults to 'on' if preference is undefined
72 my $allowselfcheckreturns = 1;
73 if (defined C4::Context->preference('AllowSelfCheckReturns')) {
74 $allowselfcheckreturns = C4::Context->preference('AllowSelfCheckReturns');
76 $template->param(AllowSelfCheckReturns => $allowselfcheckreturns);
79 my $issuerid = $loggedinuser;
80 my ($op, $patronid, $patronlogin, $patronpw, $barcode, $confirmed, $timedout) = (
81 $query->param("op") || '',
82 $query->param("patronid") || '',
83 $query->param("patronlogin")|| '',
84 $query->param("patronpw") || '',
85 $query->param("barcode") || '',
86 $query->param("confirmed") || '',
87 $query->param("timedout") || '', #not actually using this...
90 my $issuenoconfirm = 1; #don't need to confirm on issue.
91 #warn "issuerid: " . $issuerid;
92 my $issuer = GetMemberDetails($issuerid);
93 my $item = GetItem(undef,$barcode);
94 if (C4::Context->preference('SelfCheckoutByLogin') && !$patronid) {
95 my $dbh = C4::Context->dbh;
97 ($resval, $patronid) = checkpw($dbh, $patronlogin, $patronpw);
99 my $borrower = GetMemberDetails(undef,$patronid);
102 my $branch = $issuer->{branchcode};
103 my $confirm_required = 0;
105 #warn "issuer cardnumber: " . $issuer->{cardnumber};
106 #warn "patron cardnumber: " . $borrower->{cardnumber};
107 if ($op eq "logout") {
108 $query->param( patronid => undef, patronlogin => undef, patronpw => undef );
110 elsif ( $op eq "returnbook" && $allowselfcheckreturns ) {
111 my ($doreturn) = AddReturn( $barcode, $branch );
112 #warn "returnbook: " . $doreturn;
113 $borrower = GetMemberDetails(undef,$patronid);
115 elsif ( $op eq "checkout" ) {
117 my $needconfirm = {};
119 ( $impossible, $needconfirm ) = CanBookBeIssued( $borrower, $barcode );
121 $confirm_required = scalar keys %$needconfirm;
123 #warn "confirm_required: " . $confirm_required ;
124 if (scalar keys %$impossible) {
126 # warn "impossible: numkeys: " . scalar (keys(%$impossible));
127 #warn join " ", keys %$impossible;
128 my $issue_error = (keys %$impossible)[0];
130 # FIXME we assume only one error.
132 impossible => $issue_error,
133 "circ_error_$issue_error" => 1,
134 title => $item->{title},
137 if ($issue_error eq 'DEBT') {
138 $template->param(amount => $impossible->{DEBT});
140 #warn "issue_error: " . $issue_error ;
141 if ( $issue_error eq "NO_MORE_RENEWALS" ) {
148 } elsif ( $needconfirm->{RENEW_ISSUE} ) {
151 AddRenewal( $borrower, $item->{itemnumber} );
153 #warn "renew confirmation";
158 confirm_renew_issue => 1,
162 } elsif ( $confirm_required && !$confirmed ) {
163 #warn "failed confirmation";
164 my $issue_error = (keys %$needconfirm)[0];
166 impossible => (keys %$needconfirm)[0],
167 "circ_error_$issue_error" => 1,
171 if ( $confirmed || $issuenoconfirm ) { # we'll want to call getpatroninfo again to get updated issues.
172 # warn "issuing book?";
173 AddIssue( $borrower, $barcode );
174 # ($borrower, $flags) = getpatroninformation(undef,undef, $patronid);
176 # patronid => $patronid,
180 $confirm_required = 1;
181 #warn "issue confirmation";
183 confirm => "Issuing title: " . $item->{title},
186 inputfocus => 'confirm',
192 if ($borrower->{cardnumber}) {
193 # warn "issuer's branchcode: " . $issuer->{branchcode};
194 # warn "user's branchcode: " . $borrower->{branchcode};
195 my $borrowername = sprintf "%s %s", ($borrower->{firstname} || ''), ($borrower->{surname} || '');
197 my ($issueslist) = GetPendingIssues( $borrower->{'borrowernumber'} );
198 foreach my $it (@$issueslist) {
199 $it->{date_due_display} = format_date($it->{date_due});
200 my ($renewokay, $renewerror) = CanBookBeIssued($borrower, $it->{'barcode'},'','');
201 $it->{'norenew'} = 1 if $renewokay->{'NO_MORE_RENEWALS'};
207 borrowername => $borrowername,
208 issues_count => scalar(@issues),
210 patronid => $patronid,
211 patronlogin => $patronlogin,
212 patronpw => $patronpw,
215 my $inputfocus = ($return_only == 1) ? 'returnbook' :
216 ($confirm_required == 1) ? 'confirm' : 'barcode' ;
218 inputfocus => $inputfocus,
220 "dateformat_" . C4::Context->preference('dateformat') => 1,
222 if (C4::Context->preference('ShowPatronImageInWebBasedSelfCheck')) {
223 my ($image, $dberror) = GetPatronImage($borrower->{cardnumber});
226 display_patron_image => 1,
227 cardnumber => $borrower->{cardnumber},
233 patronid => $patronid,
238 output_html_with_http_headers $query, $cookie, $template->output;