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