set koha_login_context in order to get auto-login for SCO working
[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
22 use C4::Auth;
23 use C4::Koha;
24 use C4::Dates qw/format_date/;
25 use C4::Circulation;
26 use C4::Reserves;
27 use C4::Output;
28 use C4::Members;
29 use C4::Dates;
30 use C4::Biblio;
31 use C4::Items;
32
33 my $query = new CGI;
34 if (C4::Context->preference('AutoSelfCheckAllowed')) 
35 {
36         my $AutoSelfCheckID = C4::Context->preference('AutoSelfCheckID');
37         my $AutoSelfCheckPass = C4::Context->preference('AutoSelfCheckPass');
38         $query->param(-name=>'userid',-values=>[$AutoSelfCheckID]);
39         $query->param(-name=>'password',-values=>[$AutoSelfCheckPass]);
40     $query->param(-name=>'koha_login_context',-values=>['sco']);
41 }
42 my ($template, $loggedinuser, $cookie) = get_template_and_user({
43     template_name   => "sco/sco-main.tmpl",
44     authnotrequired => 0,
45       flagsrequired => { circulate => "circulate_remaining_permissions" },
46     query => $query,
47     type  => "opac",
48     debug => 1,
49 });
50
51 my $issuerid = $loggedinuser;
52 my ($op, $patronid, $barcode, $confirmed, $timedout) = (
53     $query->param("op")         || '',
54     $query->param("patronid")   || '',
55     $query->param("barcode")    || '',
56     $query->param("confirmed")  || '',
57     $query->param("timedout")   || '', #not actually using this...
58 );
59
60 my %confirmation_strings = ( RENEW_ISSUE => "This item is already checked out to you.  Return it?", );
61 my $issuenoconfirm = 1; #don't need to confirm on issue.
62 #warn "issuerid: " . $issuerid;
63 my $issuer   = GetMemberDetails($issuerid);
64 my $item     = GetItem(undef,$barcode);
65 my $borrower = GetMemberDetails(undef,$patronid);
66
67 my $branch = $issuer->{branchcode};
68 my $confirm_required = 0;
69 my $return_only = 0;
70 #warn "issuer cardnumber: " .   $issuer->{cardnumber};
71 #warn "patron cardnumber: " . $borrower->{cardnumber};
72 if ($op eq "logout") {
73     $query->param( patronid => undef );
74 }
75 elsif ( $op eq "returnbook" ) {
76     my ($doreturn) = AddReturn( $barcode, $branch );
77     #warn "returnbook: " . $doreturn;
78     $borrower = GetMemberDetails( undef, $patronid );   # update borrower
79 }
80 elsif ( $op eq "checkout" ) {
81     my $impossible  = {};
82     my $needconfirm = {};
83     if ( !$confirmed ) {
84         ( $impossible, $needconfirm ) = CanBookBeIssued( $borrower, $barcode );
85     }
86     $confirm_required = scalar keys %$needconfirm;
87
88     #warn "confirm_required: " . $confirm_required ;
89     if (scalar keys %$impossible) {
90
91         #  warn "impossible: numkeys: " . scalar (keys(%$impossible));
92         #warn join " ", keys %$impossible;
93         my $issue_error = (keys %$impossible)[0];
94
95         # FIXME  we assume only one error.
96         $template->param(
97             impossible                => $issue_error,
98             "circ_error_$issue_error" => 1,
99             title                     => $item->{title},
100             hide_main                 => 1,
101         );
102         if ($issue_error eq 'DEBT') {
103             $template->param(amount => $impossible->{DEBT});
104         }
105         #warn "issue_error: " . $issue_error ;
106         if ( $issue_error eq "NO_MORE_RENEWALS" ) {
107             $return_only = 1;
108             $template->param(
109                 returnitem => 1,
110                 barcode    => $barcode,
111             );
112         }
113     } elsif ( $needconfirm->{RENEW_ISSUE} ) {
114         if ($confirmed) {
115             #warn "renewing";
116             AddRenewal( $borrower, $item->{itemnumber} );
117         } else {
118             #warn "renew confirmation";
119             $template->param(
120                 renew               => 1,
121                 barcode             => $barcode,
122                 confirm             => 1,
123                 confirm_renew_issue => 1,
124                 hide_main           => 1,
125             );
126         }
127     } elsif ( $confirm_required && !$confirmed ) {
128         #warn "failed confirmation";
129         my $issue_error = (keys %$needconfirm)[0];
130         $template->param(
131             impossible                => (keys %$needconfirm)[0],
132             "circ_error_$issue_error" => 1,
133             hide_main                 => 1,
134         );
135     } else {
136         if ( $confirmed || $issuenoconfirm ) {    # we'll want to call getpatroninfo again to get updated issues.
137             # warn "issuing book?";
138             AddIssue( $borrower, $barcode );
139             # ($borrower, $flags) = getpatroninformation(undef,undef, $patronid);
140             # $template->param(
141             #   patronid => $patronid,
142             #   validuser => 1,
143             # );
144         } else {
145             $confirm_required = 1;
146             #warn "issue confirmation";
147             $template->param(
148                 confirm    => "Issuing title: " . $item->{title},
149                 barcode    => $barcode,
150                 hide_main  => 1,
151                 inputfocus => 'confirm',
152             );
153         }
154     }
155 } # $op
156
157 if ($borrower->{cardnumber}) {
158 #   warn "issuer's  branchcode: " .   $issuer->{branchcode};
159 #   warn   "user's  branchcode: " . $borrower->{branchcode};
160     my $borrowername = sprintf "%s %s", ($borrower->{firstname} || ''), ($borrower->{surname} || '');
161     my @issues;
162     my ($issueslist) = GetPendingIssues( $borrower->{'borrowernumber'} );
163     foreach my $it (@$issueslist) {
164         $it->{date_due_display} = format_date($it->{date_due});
165         my ($renewokay, $renewerror) = CanBookBeIssued($borrower, $it->{'barcode'},'','');
166         $it->{'norenew'} = 1 if $renewokay->{'NO_MORE_RENEWALS'};
167         push @issues, $it;
168     }
169
170     $template->param(
171         validuser => 1,
172         borrowername => $borrowername,
173         issues_count => scalar(@issues),
174         ISSUES => \@issues,
175         patronid => $patronid,
176         noitemlinks => 1 ,
177     );
178     my $inputfocus = ($return_only      == 1) ? 'returnbook' :
179                      ($confirm_required == 1) ? 'confirm'    : 'barcode' ;
180     $template->param(
181         inputfocus => $inputfocus,
182                 nofines => 1,
183         "dateformat_" . C4::Context->preference('dateformat') => 1,
184     );
185     if (C4::Context->preference('ShowPatronImageInWebBasedSelfCheck')) {
186         my ($image, $dberror) = GetPatronImage($borrower->{cardnumber});
187         if ($image) {
188             $template->param(
189                 display_patron_image => 1,
190                 cardnumber           => $borrower->{cardnumber},
191             );
192         }
193     }
194 } else {
195     $template->param(
196         patronid   => $patronid,
197         nouser     => $patronid,
198     );
199 }
200
201 output_html_with_http_headers $query, $cookie, $template->output;