Merge remote-tracking branch 'origin/new/bug_6390'
[koha.git] / opac / sco / sco-main.pl
1 #!/usr/bin/perl
2 #
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'
7 #
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!
11 #
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.
15 #
16 # FIXME: inputfocus not really used in TMPL
17
18 use strict;
19 use warnings;
20
21 use CGI;
22 use Digest::MD5 qw(md5_base64);
23
24 use C4::Auth qw(get_template_and_user checkpw);
25 use C4::Koha;
26 use C4::Dates qw/format_date/;
27 use C4::Circulation;
28 use C4::Reserves;
29 use C4::Output;
30 use C4::Members;
31 use C4::Dates;
32 use C4::Biblio;
33 use C4::Items;
34
35 my $query = new CGI;
36
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");
40     exit;
41 }
42
43 if (C4::Context->preference('AutoSelfCheckAllowed')) 
44 {
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']);
50 }
51 my ($template, $loggedinuser, $cookie) = get_template_and_user({
52     template_name   => "sco/sco-main.tmpl",
53     authnotrequired => 0,
54       flagsrequired => { circulate => "circulate_remaining_permissions" },
55     query => $query,
56     type  => "opac",
57     debug => 1,
58 });
59 if (C4::Context->preference('SelfCheckoutByLogin'))
60 {
61     $template->param(authbylogin  => 1);
62 }
63
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;
68 }
69 $template->param(SelfCheckTimeout => $selfchecktimeout);
70
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');
75 }
76 $template->param(AllowSelfCheckReturns => $allowselfcheckreturns);
77
78
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...
88 );
89
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;
96     my $resval;
97     ($resval, $patronid) = checkpw($dbh, $patronlogin, $patronpw);
98 }
99 my $borrower = GetMemberDetails(undef,$patronid);
100
101
102 my $branch = $issuer->{branchcode};
103 my $confirm_required = 0;
104 my $return_only = 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 );
109 }
110 elsif ( $op eq "returnbook" && $allowselfcheckreturns ) {
111     my ($doreturn) = AddReturn( $barcode, $branch );
112     #warn "returnbook: " . $doreturn;
113     $borrower = GetMemberDetails(undef,$patronid);
114 }
115 elsif ( $op eq "checkout" ) {
116     my $impossible  = {};
117     my $needconfirm = {};
118     if ( !$confirmed ) {
119         ( $impossible, $needconfirm ) = CanBookBeIssued( $borrower, $barcode );
120     }
121     $confirm_required = scalar keys %$needconfirm;
122
123     #warn "confirm_required: " . $confirm_required ;
124     if (scalar keys %$impossible) {
125
126         #  warn "impossible: numkeys: " . scalar (keys(%$impossible));
127         #warn join " ", keys %$impossible;
128         my $issue_error = (keys %$impossible)[0];
129
130         # FIXME  we assume only one error.
131         $template->param(
132             impossible                => $issue_error,
133             "circ_error_$issue_error" => 1,
134             title                     => $item->{title},
135             hide_main                 => 1,
136         );
137         if ($issue_error eq 'DEBT') {
138             $template->param(amount => $impossible->{DEBT});
139         }
140         #warn "issue_error: " . $issue_error ;
141         if ( $issue_error eq "NO_MORE_RENEWALS" ) {
142             $return_only = 1;
143             $template->param(
144                 returnitem => 1,
145                 barcode    => $barcode,
146             );
147         }
148     } elsif ( $needconfirm->{RENEW_ISSUE} ) {
149         if ($confirmed) {
150             #warn "renewing";
151             AddRenewal( $borrower, $item->{itemnumber} );
152         } else {
153             #warn "renew confirmation";
154             $template->param(
155                 renew               => 1,
156                 barcode             => $barcode,
157                 confirm             => 1,
158                 confirm_renew_issue => 1,
159                 hide_main           => 1,
160             );
161         }
162     } elsif ( $confirm_required && !$confirmed ) {
163         #warn "failed confirmation";
164         my $issue_error = (keys %$needconfirm)[0];
165         $template->param(
166             impossible                => (keys %$needconfirm)[0],
167             "circ_error_$issue_error" => 1,
168             hide_main                 => 1,
169         );
170     } else {
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);
175             # $template->param(
176             #   patronid => $patronid,
177             #   validuser => 1,
178             # );
179         } else {
180             $confirm_required = 1;
181             #warn "issue confirmation";
182             $template->param(
183                 confirm    => "Issuing title: " . $item->{title},
184                 barcode    => $barcode,
185                 hide_main  => 1,
186                 inputfocus => 'confirm',
187             );
188         }
189     }
190 } # $op
191
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} || '');
196     my @issues;
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'};
202         push @issues, $it;
203     }
204
205     $template->param(
206         validuser => 1,
207         borrowername => $borrowername,
208         issues_count => scalar(@issues),
209         ISSUES => \@issues,
210         patronid => $patronid,
211         patronlogin => $patronlogin,
212         patronpw => $patronpw,
213         noitemlinks => 1 ,
214     );
215     my $inputfocus = ($return_only      == 1) ? 'returnbook' :
216                      ($confirm_required == 1) ? 'confirm'    : 'barcode' ;
217     $template->param(
218         inputfocus => $inputfocus,
219                 nofines => 1,
220         "dateformat_" . C4::Context->preference('dateformat') => 1,
221     );
222     if (C4::Context->preference('ShowPatronImageInWebBasedSelfCheck')) {
223         my ($image, $dberror) = GetPatronImage($borrower->{cardnumber});
224         if ($image) {
225             $template->param(
226                 display_patron_image => 1,
227                 cardnumber           => $borrower->{cardnumber},
228             );
229         }
230     }
231 } else {
232     $template->param(
233         patronid   => $patronid,
234         nouser     => $patronid,
235     );
236 }
237
238 output_html_with_http_headers $query, $cookie, $template->output;