bug 2613: allow web self-check to accept patron ID
[koha.git] / opac / sco / sco-main.pl
1 #!/usr/bin/perl
2 # This code has been modified by Trendsetters (originally from opac-user.pl)
3 # This code has been modified by rch
4 # We're going to authenticate a self-check user.  we'll add a flag to borrowers 'selfcheck'
5 # We're in a controlled environment; we trust the user. so the selfcheck station will accept a patronid and 
6 # issue items to that borrower.
7 #
8 use strict;
9
10 use CGI;
11
12 #use C4::Authsco;
13 use C4::Auth;
14 use C4::Koha;
15 use C4::Circulation;
16 use C4::Reserves;
17 use C4::Search;
18 use C4::Output;
19 use C4::Members;
20 use HTML::Template::Pro;
21 use C4::Dates;
22 use C4::Biblio;
23 use C4::Items;
24
25 my $query = new CGI;
26 my ($template, $loggedinuser, $cookie)
27     = get_template_and_user({template_name => "sco/sco-main.tmpl",
28                              query => $query,
29                              type => "opac",
30                              authnotrequired => 0,
31                              flagsrequired => { circulate => 1 },
32                              debug => 1,
33                              });
34 my $dbh = C4::Context->dbh;
35
36 my $issuerid = $loggedinuser;
37 my ($op, $patronid, $barcode, $confirmed, $timedout )= ($query->param("op"), 
38                                          $query->param("patronid"), 
39                                         $query->param("barcode"),
40                                         $query->param( "confirmed"),
41                                         $query->param( "timedout"), #not actually using this...
42                                          );
43 my %confirmation_strings = ( RENEW_ISSUE => "This item is already checked out to you.  Return it?", );
44 my $issuenoconfirm = 1; #don't need to confirm on issue.
45 my $cnt = 0;
46 #warn "issuerid: " . $issuerid;
47 my ($issuer) = GetMemberDetails($issuerid);
48 my $item = GetItem(undef,$barcode);
49 my $borrower;
50 ($borrower) = GetMemberDetails(undef,$patronid);
51
52 my $branch = $issuer->{branchcode};
53 my $confirm_required = 0;
54 my $return_only = 0;
55 #warn "issuer cardnum: " . $issuer->{cardnumber};
56 #warn "cardnumber= ".$borrower->{cardnumber};
57 if ($op eq "logout") {
58         $query->param( patronid => undef );
59 }
60   if ($op eq "returnbook") {
61       my ($doreturn ) = AddReturn($barcode, $branch);
62      #warn "returnbook: " . $doreturn;
63     ($borrower) = GetMemberDetails(undef, $patronid);
64   }
65   
66   if ($op eq "checkout" ) {
67         my $impossible = {};
68         my $needconfirm = {};
69       if ( !$confirmed ) {
70          ($impossible,$needconfirm) = CanBookBeIssued($borrower,$barcode);
71       }
72         $confirm_required = scalar(keys(%$needconfirm));
73         #warn "confirm_required: " . $confirm_required ;
74         if (scalar(keys(%$impossible))) {
75     #  warn "impossible: numkeys: " . scalar (keys(%$impossible));
76          my ($issue_error) = keys %$impossible ;
77          # FIXME  we assume only one error.
78          $template->param( impossible => $issue_error,
79                         title => $item->{title} ,
80                         hide_main => 1,
81                         );
82         #warn "issue_error: " . $issue_error ;
83         if ($issue_error eq "NO_MORE_RENEWALS") {
84                 $return_only = 1;
85                 $template->param ( returnitem => 1,
86                                 barcode => $barcode ,
87                                 );
88          }
89       } elsif ($needconfirm->{RENEW_ISSUE} ) {
90           if ( $confirmed ) {
91           #warn "renewing";
92             AddRenewal($borrower,$item->{itemnumber});
93           } else {
94           #warn "renew confirmation";
95            $template->param( renew => 1,
96                         barcode => $barcode,
97             confirm => 1,
98             confirm_renew_issue => 1,
99                         hide_main => 1,
100                         );
101           }
102       } elsif ( $confirm_required && !$confirmed ) {
103       #warn "failed confirmation";
104          my ($confirmation) = keys %$needconfirm ;
105          $template->param( impossible => $confirmation,
106                         hide_main => 1,
107                         );
108       } else {
109          if ( $confirmed || $issuenoconfirm ) {  # we'll want to call getpatroninfo again to get updated issues.
110             #warn "issuing book?";
111             AddIssue($borrower,$barcode);
112         #    ($borrower, $flags) = getpatroninformation(undef,undef, $patronid);
113                 
114        #    $template->param( patronid => $patronid,
115 #                       validuser => 1,
116 #                       );
117          } else {
118            $confirm_required = 1;
119            #warn "issue confirmation";
120            $template->param( confirm => "Issuing title: " . $item->{title} ,
121                         barcode => $barcode,
122                         hide_main => 1,
123                         inputfocus => 'confirm',
124                         );
125         }
126       }
127    } # op=checkout
128
129 if ($borrower->{cardnumber}) {
130
131 #   warn "here's the issuer's  branchcode: ".$issuer->{branchcode};
132 #   warn "here's the user's  branchcode: ".$borrower->{branchcode};
133         my $bornum = $borrower->{borrowernumber};
134         my $borrowername = $borrower->{firstname} . " " . $borrower->{surname};
135         my @issues;
136         my ($countissues,$issueslist) = GetPendingIssues($borrower->{'borrowernumber'});
137         foreach my $it ( @$issueslist ) {
138                 push @issues, $it;
139                 $cnt++;
140         }
141    $template->param(  validuser => 1,  
142                         borrowername => $borrowername,
143                         issues_count => $cnt, 
144                         ISSUES => \@issues,,
145                         patronid => $patronid ,
146                         noitemlinks => 1 ,
147                 );
148    $cnt = 0;
149    my $inputfocus;
150    if ($return_only ==1) {
151       $inputfocus = 'returnbook' ;
152    }elsif ($confirm_required == 1) {
153       $inputfocus = 'confirm' ;
154    } else {
155       $inputfocus = 'barcode' ;
156     }
157
158 $template->param( inputfocus => $inputfocus,
159                 nofines => 1,
160                 );
161
162 } else {
163
164  $template->param( patronid => $patronid,  nouser => $patronid,
165                         inputfocus => 'patronid', );
166 }
167
168 output_html_with_http_headers $query, $cookie, $template->output;