bug 2615: remove unneeded 'require Exporter'
[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 userid 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, $userid, $barcode, $confirmed, $timedout )= ($query->param("op"), 
38                                          $query->param("userid"), 
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,$userid);
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( userid => undef );
59 }
60   if ($op eq "returnbook") {
61       my ($doreturn ) = AddReturn($barcode, $branch);
62      #warn "returnbook: " . $doreturn;
63     ($borrower) = GetMemberDetails(undef, $userid);
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 => $confirmation_strings{RENEW_ISSUE},
98                         hide_main => 1,
99                         );
100           }
101       } elsif ( $confirm_required && !$confirmed ) {
102       #warn "failed confirmation";
103          my ($confirmation) = keys %$needconfirm ;
104          $template->param( impossible => $confirmation,
105                         hide_main => 1,
106                         );
107       } else {
108          if ( $confirmed || $issuenoconfirm ) {  # we'll want to call getpatroninfo again to get updated issues.
109             #warn "issuing book?";
110             AddIssue($borrower,$barcode);
111         #    ($borrower, $flags) = getpatroninformation(undef,undef, $userid);
112                 
113        #    $template->param( userid => $userid,
114 #                       validuser => 1,
115 #                       );
116          } else {
117            $confirm_required = 1;
118            #warn "issue confirmation";
119            $template->param( confirm => "Issuing title: " . $item->{title} ,
120                         barcode => $barcode,
121                         hide_main => 1,
122                         inputfocus => 'confirm',
123                         );
124         }
125       }
126    } # op=checkout
127
128 if ($borrower->{cardnumber}) {
129
130 #   warn "here's the issuer's  branchcode: ".$issuer->{branchcode};
131 #   warn "here's the user's  branchcode: ".$borrower->{branchcode};
132         my $bornum = $borrower->{borrowernumber};
133         my $borrowername = $borrower->{firstname} . " " . $borrower->{surname};
134         my @issues;
135         my ($countissues,$issueslist) = GetPendingIssues($borrower->{'borrowernumber'});
136         foreach my $it ( @$issueslist ) {
137                 push @issues, $it;
138                 $cnt++;
139         }
140    $template->param(  validuser => 1,  
141                         borrowername => $borrowername,
142                         issues_count => $cnt, 
143                         ISSUES => \@issues,,
144                         userid => $userid ,
145                         noitemlinks => 1 ,
146                 );
147    $cnt = 0;
148    my $inputfocus;
149    if ($return_only ==1) {
150       $inputfocus = 'returnbook' ;
151    }elsif ($confirm_required == 1) {
152       $inputfocus = 'confirm' ;
153    } else {
154       $inputfocus = 'barcode' ;
155     }
156
157 $template->param( inputfocus => $inputfocus,
158                 nofines => 1,
159                 );
160
161 } else {
162
163  $template->param( userid => $userid,  nouser => $userid,
164                         inputfocus => 'userid', );
165 }
166
167 output_html_with_http_headers $query, $cookie, $template->output;