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