- perltidy corrects broken indentation on 'if' block.
[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
64
65 if ( $op eq "returnbook" ) {
66     my ($doreturn) = AddReturn( $barcode, $branch );
67
68     #warn "returnbook: " . $doreturn;
69     ($borrower) = GetMemberDetails( undef, $patronid );
70 }
71
72 if ( $op eq "checkout" ) {
73     my $impossible  = {};
74     my $needconfirm = {};
75     if ( !$confirmed ) {
76         ( $impossible, $needconfirm ) = CanBookBeIssued( $borrower, $barcode );
77     }
78     $confirm_required = scalar( keys(%$needconfirm) );
79
80     #warn "confirm_required: " . $confirm_required ;
81     if ( scalar( keys(%$impossible) ) ) {
82
83         #  warn "impossible: numkeys: " . scalar (keys(%$impossible));
84         my ($issue_error) = keys %$impossible;
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
93         #warn "issue_error: " . $issue_error ;
94         if ( $issue_error eq "NO_MORE_RENEWALS" ) {
95             $return_only = 1;
96             $template->param(
97                 returnitem => 1,
98                 barcode    => $barcode,
99             );
100         }
101     } elsif ( $needconfirm->{RENEW_ISSUE} ) {
102         if ($confirmed) {
103
104             #warn "renewing";
105             AddRenewal( $borrower, $item->{itemnumber} );
106         } else {
107
108             #warn "renew confirmation";
109             $template->param(
110                 renew               => 1,
111                 barcode             => $barcode,
112                 confirm             => 1,
113                 confirm_renew_issue => 1,
114                 hide_main           => 1,
115             );
116         }
117     } elsif ( $confirm_required && !$confirmed ) {
118
119         #warn "failed confirmation";
120         my ($confirmation) = keys %$needconfirm;
121         $template->param(
122             impossible => $confirmation,
123             hide_main  => 1,
124         );
125     } else {
126         if ( $confirmed || $issuenoconfirm ) {    # we'll want to call getpatroninfo again to get updated issues.
127                                                   #warn "issuing book?";
128             AddIssue( $borrower, $barcode );
129
130             #    ($borrower, $flags) = getpatroninformation(undef,undef, $patronid);
131
132             #    $template->param( patronid => $patronid,
133             #                   validuser => 1,
134             #                   );
135         } else {
136             $confirm_required = 1;
137
138             #warn "issue confirmation";
139             $template->param(
140                 confirm    => "Issuing title: " . $item->{title},
141                 barcode    => $barcode,
142                 hide_main  => 1,
143                 inputfocus => 'confirm',
144             );
145         }
146     }
147 }    # op=checkout
148
149 if ($borrower->{cardnumber}) {
150
151 #   warn "here's the issuer's  branchcode: ".$issuer->{branchcode};
152 #   warn "here's the user's  branchcode: ".$borrower->{branchcode};
153         my $bornum = $borrower->{borrowernumber};
154         my $borrowername = $borrower->{firstname} . " " . $borrower->{surname};
155         my @issues;
156
157     my ($issueslist) = GetPendingIssues( $borrower->{'borrowernumber'} );
158     foreach my $it (@$issueslist) {
159
160         my ( $renewokay, $renewerror ) =
161                 CanBookBeIssued( $borrower, $it->{'barcode'}, '', '' );
162         $it->{'norenew'} = 1 if $renewokay->{'NO_MORE_RENEWALS'} == 1;
163         push @issues, $it;
164         $cnt++;
165     }
166
167    $template->param(  validuser => 1,
168                         borrowername => $borrowername,
169                         issues_count => $cnt,
170                         ISSUES => \@issues,,
171                         patronid => $patronid ,
172                         noitemlinks => 1 ,
173                 );
174    $cnt = 0;
175    my $inputfocus;
176    if ($return_only ==1) {
177       $inputfocus = 'returnbook' ;
178    }elsif ($confirm_required == 1) {
179       $inputfocus = 'confirm' ;
180    } else {
181       $inputfocus = 'barcode' ;
182    }
183
184 $template->param( inputfocus => $inputfocus,
185                 nofines => 1,
186                 );
187
188 } else {
189
190  $template->param( patronid => $patronid,  nouser => $patronid,
191                         inputfocus => 'patronid', );
192 }
193
194 output_html_with_http_headers $query, $cookie, $template->output;