item rework: moved GetItem
[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 require Exporter;
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 => { managesco => 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, $flags) = GetMemberDetails($issuerid);
48 my $item = GetItem(undef,$barcode);
49 my ($borrower, $flags) = GetMemberDetails(undef,$userid);
50
51 my $branch = $issuer->{branchcode};
52 my $confirm_required = 0;
53 my $return_only = 0;
54 #warn "issuer cardnum: " . $issuer->{cardnumber};
55 #warn "cardnumber= ".$borrower->{cardnumber};
56 if ($op eq "logout") {
57         $query->param( userid => undef );
58 }
59   if ($op eq "returnbook") {
60       my ($doreturn ) = AddReturn($barcode, $branch);
61      #warn "returnbook: " . $doreturn;
62     ($borrower, $flags) = GetMemberDetails(undef, $userid);
63   }
64   
65   if ($op eq "checkout" ) {
66         my $impossible = {};
67         my $needconfirm = {};
68       if ( !$confirmed ) {
69          ($impossible,$needconfirm) = CanBookBeIssued($borrower,$barcode);
70       }
71         $confirm_required = scalar(keys(%$needconfirm));
72         #warn "confirm_required: " . $confirm_required ;
73         if (scalar(keys(%$impossible))) {
74     #  warn "impossible: numkeys: " . scalar (keys(%$impossible));
75          my ($issue_error) = keys %$impossible ;
76          # FIXME  we assume only one error.
77          $template->param( impossible => $issue_error,
78                         title => $item->{title} ,
79                         hide_main => 1,
80                         );
81         #warn "issue_error: " . $issue_error ;
82         if ($issue_error eq "NO_MORE_RENEWALS") {
83                 $return_only = 1;
84                 $template->param ( returnitem => 1,
85                                 barcode => $barcode ,
86                                 );
87          }
88       } elsif ($needconfirm->{RENEW_ISSUE} ) {
89           if ( $confirmed ) {
90           #warn "renewing";
91             AddRenewal($borrower,$item->{itemnumber});
92           } else {
93           #warn "renew confirmation";
94            $template->param( renew => 1,
95                         barcode => $barcode,
96                         confirm => $confirmation_strings{RENEW_ISSUE},
97                         hide_main => 1,
98                         );
99           }
100       } elsif ( $confirm_required && !$confirmed ) {
101       #warn "failed confirmation";
102          my ($confirmation) = keys %$needconfirm ;
103          $template->param( impossible => $confirmation,
104                         hide_main => 1,
105                         );
106       } else {
107          if ( $confirmed || $issuenoconfirm ) {  # we'll want to call getpatroninfo again to get updated issues.
108             #warn "issuing book?";
109             AddIssue($borrower,$barcode);
110         #    ($borrower, $flags) = getpatroninformation(undef,undef, $userid);
111                 
112        #    $template->param( userid => $userid,
113 #                       validuser => 1,
114 #                       );
115          } else {
116            $confirm_required = 1;
117            #warn "issue confirmation";
118            $template->param( confirm => "Issuing title: " . $item->{title} ,
119                         barcode => $barcode,
120                         hide_main => 1,
121                         inputfocus => 'confirm',
122                         );
123         }
124       }
125    } # op=checkout
126
127 if ($borrower->{cardnumber}) {
128
129 #   warn "here's the issuer's  branchcode: ".$issuer->{branchcode};
130 #   warn "here's the user's  branchcode: ".$borrower->{branchcode};
131         my $bornum = $borrower->{borrowernumber};
132         my $borrowername = $borrower->{firstname} . " " . $borrower->{surname};
133         my @issues;
134         my ($countissues,$issueslist) = GetPendingIssues($borrower->{'borrowernumber'});
135         foreach my $it ( @$issueslist ) {
136                 push @issues, $it;
137                 $cnt++;
138         }
139    $template->param(  validuser => 1,  
140                         borrowername => $borrowername,
141                         issues_count => $cnt, 
142                         ISSUES => \@issues,,
143                         userid => $userid ,
144                         noitemlinks => 1 ,
145                 );
146    $cnt = 0;
147    my $inputfocus;
148    if ($return_only ==1) {
149       $inputfocus = 'returnbook' ;
150    }elsif ($confirm_required == 1) {
151       $inputfocus = 'confirm' ;
152    } else {
153       $inputfocus = 'barcode' ;
154     }
155
156 $template->param( inputfocus => $inputfocus,
157                 nofines => 1,
158                 );
159
160 } else {
161
162  $template->param( userid => $userid,  nouser => $userid,
163                         inputfocus => 'userid', );
164 }
165
166 output_html_with_http_headers $query, $cookie, $template->output;