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