3 # This code has been modified by Trendsetters (originally from opac-user.pl)
4 # This code has been modified by rch
5 # We're going to authenticate a self-check user. we'll add a flag to borrowers 'selfcheck'
7 # We're in a controlled environment; we trust the user.
8 # So the selfcheck station will accept a patronid and issue items to that borrower.
9 # FIXME: NOT really a controlled environment... We're on the internet!
11 # The checkout permission comes form the CGI cookie/session of a staff user.
12 # The patron is not really logging in here in the same way as they do on the
13 # rest of the OPAC. So don't confuse loggedinuser with the patron user.
15 # FIXME: inputfocus not really used in TMPL
21 use Digest::MD5 qw(md5_base64);
23 use C4::Auth qw(get_template_and_user checkpw);
25 use C4::Dates qw/format_date/;
36 unless (C4::Context->preference('WebBasedSelfCheck')) {
37 # redirect to OPAC home if self-check is not enabled
38 print $query->redirect("/cgi-bin/koha/opac-main.pl");
42 if (C4::Context->preference('AutoSelfCheckAllowed'))
44 my $AutoSelfCheckID = C4::Context->preference('AutoSelfCheckID');
45 my $AutoSelfCheckPass = C4::Context->preference('AutoSelfCheckPass');
46 $query->param(-name=>'userid',-values=>[$AutoSelfCheckID]);
47 $query->param(-name=>'password',-values=>[$AutoSelfCheckPass]);
48 $query->param(-name=>'koha_login_context',-values=>['sco']);
50 my ($template, $loggedinuser, $cookie) = get_template_and_user({
51 template_name => "sco/sco-main.tmpl",
53 flagsrequired => { circulate => "circulate_remaining_permissions" },
58 if (C4::Context->preference('SelfCheckoutByLogin'))
60 $template->param(authbylogin => 1);
63 # Get the self checkout timeout preference, or use 120 seconds as a default
64 my $selfchecktimeout = 120000;
65 if (C4::Context->preference('SelfCheckTimeout')) {
66 $selfchecktimeout = C4::Context->preference('SelfCheckTimeout') * 1000;
68 $template->param(SelfCheckTimeout => $selfchecktimeout);
70 # Checks policy laid out by AllowSelfCheckReturns, defaults to 'on' if preference is undefined
71 my $allowselfcheckreturns = 1;
72 if (defined C4::Context->preference('AllowSelfCheckReturns')) {
73 $allowselfcheckreturns = C4::Context->preference('AllowSelfCheckReturns');
75 $template->param(AllowSelfCheckReturns => $allowselfcheckreturns);
78 my $issuerid = $loggedinuser;
79 my ($op, $patronid, $patronlogin, $patronpw, $barcode, $confirmed, $timedout) = (
80 $query->param("op") || '',
81 $query->param("patronid") || '',
82 $query->param("patronlogin")|| '',
83 $query->param("patronpw") || '',
84 $query->param("barcode") || '',
85 $query->param("confirmed") || '',
86 $query->param("timedout") || '', #not actually using this...
89 my $issuenoconfirm = 1; #don't need to confirm on issue.
90 #warn "issuerid: " . $issuerid;
91 my $issuer = GetMemberDetails($issuerid);
92 my $item = GetItem(undef,$barcode);
93 if (C4::Context->preference('SelfCheckoutByLogin') && !$patronid) {
94 my $dbh = C4::Context->dbh;
95 my ($resval, $patronid) = checkpw($dbh, $patronlogin, $patronpw);
97 my $borrower = GetMemberDetails(undef,$patronid);
100 my $branch = $issuer->{branchcode};
101 my $confirm_required = 0;
103 #warn "issuer cardnumber: " . $issuer->{cardnumber};
104 #warn "patron cardnumber: " . $borrower->{cardnumber};
105 if ($op eq "logout") {
106 $query->param( patronid => undef, patronlogin => undef, patronpw => undef );
108 elsif ( $op eq "returnbook" && $allowselfcheckreturns ) {
109 my ($doreturn) = AddReturn( $barcode, $branch );
110 #warn "returnbook: " . $doreturn;
111 $borrower = GetMemberDetails(undef,$patronid);
113 elsif ( $op eq "checkout" ) {
115 my $needconfirm = {};
117 ( $impossible, $needconfirm ) = CanBookBeIssued( $borrower, $barcode );
119 $confirm_required = scalar keys %$needconfirm;
121 #warn "confirm_required: " . $confirm_required ;
122 if (scalar keys %$impossible) {
124 # warn "impossible: numkeys: " . scalar (keys(%$impossible));
125 #warn join " ", keys %$impossible;
126 my $issue_error = (keys %$impossible)[0];
128 # FIXME we assume only one error.
130 impossible => $issue_error,
131 "circ_error_$issue_error" => 1,
132 title => $item->{title},
135 if ($issue_error eq 'DEBT') {
136 $template->param(amount => $impossible->{DEBT});
138 #warn "issue_error: " . $issue_error ;
139 if ( $issue_error eq "NO_MORE_RENEWALS" ) {
146 } elsif ( $needconfirm->{RENEW_ISSUE} ) {
149 AddRenewal( $borrower, $item->{itemnumber} );
151 #warn "renew confirmation";
156 confirm_renew_issue => 1,
160 } elsif ( $confirm_required && !$confirmed ) {
161 #warn "failed confirmation";
162 my $issue_error = (keys %$needconfirm)[0];
164 impossible => (keys %$needconfirm)[0],
165 "circ_error_$issue_error" => 1,
169 if ( $confirmed || $issuenoconfirm ) { # we'll want to call getpatroninfo again to get updated issues.
170 # warn "issuing book?";
171 AddIssue( $borrower, $barcode );
172 # ($borrower, $flags) = getpatroninformation(undef,undef, $patronid);
174 # patronid => $patronid,
178 $confirm_required = 1;
179 #warn "issue confirmation";
181 confirm => "Issuing title: " . $item->{title},
184 inputfocus => 'confirm',
190 if ($borrower->{cardnumber}) {
191 # warn "issuer's branchcode: " . $issuer->{branchcode};
192 # warn "user's branchcode: " . $borrower->{branchcode};
193 my $borrowername = sprintf "%s %s", ($borrower->{firstname} || ''), ($borrower->{surname} || '');
195 my ($issueslist) = GetPendingIssues( $borrower->{'borrowernumber'} );
196 foreach my $it (@$issueslist) {
197 $it->{date_due_display} = format_date($it->{date_due});
198 my ($renewokay, $renewerror) = CanBookBeIssued($borrower, $it->{'barcode'},'','');
199 $it->{'norenew'} = 1 if $renewokay->{'NO_MORE_RENEWALS'};
205 borrowername => $borrowername,
206 issues_count => scalar(@issues),
208 patronid => $patronid,
209 patronlogin => $patronlogin,
210 patronpw => $patronpw,
213 my $inputfocus = ($return_only == 1) ? 'returnbook' :
214 ($confirm_required == 1) ? 'confirm' : 'barcode' ;
216 inputfocus => $inputfocus,
218 "dateformat_" . C4::Context->preference('dateformat') => 1,
220 if (C4::Context->preference('ShowPatronImageInWebBasedSelfCheck')) {
221 my ($image, $dberror) = GetPatronImage($borrower->{cardnumber});
224 display_patron_image => 1,
225 cardnumber => $borrower->{cardnumber},
231 patronid => $patronid,
236 output_html_with_http_headers $query, $cookie, $template->output;