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