Bug 5811: Add sysprefs to control overriding fines
[koha.git] / opac / sco / sco-main.pl
1 #!/usr/bin/perl
2 #
3 # This code has been modified by Trendsetters (originally from opac-user.pl)
4 # This code has been modified by rch
5 # We're going to authenticate a self-check user.  we'll add a flag to borrowers 'selfcheck'
6 #
7 # We're in a controlled environment; we trust the user.
8 # So the selfcheck station will accept a patronid and issue items to that borrower.
9 # FIXME: NOT really a controlled environment...  We're on the internet!
10 #
11 # The checkout permission comes form the CGI cookie/session of a staff user.
12 # The patron is not really logging in here in the same way as they do on the
13 # rest of the OPAC.  So don't confuse loggedinuser with the patron user.
14 #
15 # FIXME: inputfocus not really used in TMPL
16
17 use strict;
18 use warnings;
19
20 use CGI;
21
22 use C4::Auth;
23 use C4::Koha;
24 use C4::Dates qw/format_date/;
25 use C4::Circulation;
26 use C4::Reserves;
27 use C4::Output;
28 use C4::Members;
29 use C4::Dates;
30 use C4::Biblio;
31 use C4::Items;
32
33 my $query = new CGI;
34
35 unless (C4::Context->preference('WebBasedSelfCheck')) {
36     # redirect to OPAC home if self-check is not enabled
37     print $query->redirect("/cgi-bin/koha/opac-main.pl");
38     exit;
39 }
40
41 if (C4::Context->preference('AutoSelfCheckAllowed')) 
42 {
43         my $AutoSelfCheckID = C4::Context->preference('AutoSelfCheckID');
44         my $AutoSelfCheckPass = C4::Context->preference('AutoSelfCheckPass');
45         $query->param(-name=>'userid',-values=>[$AutoSelfCheckID]);
46         $query->param(-name=>'password',-values=>[$AutoSelfCheckPass]);
47     $query->param(-name=>'koha_login_context',-values=>['sco']);
48 }
49 my ($template, $loggedinuser, $cookie) = get_template_and_user({
50     template_name   => "sco/sco-main.tmpl",
51     authnotrequired => 0,
52       flagsrequired => { circulate => "circulate_remaining_permissions" },
53     query => $query,
54     type  => "opac",
55     debug => 1,
56 });
57
58 my $issuerid = $loggedinuser;
59 my ($op, $patronid, $barcode, $confirmed, $timedout) = (
60     $query->param("op")         || '',
61     $query->param("patronid")   || '',
62     $query->param("barcode")    || '',
63     $query->param("confirmed")  || '',
64     $query->param("timedout")   || '', #not actually using this...
65 );
66
67 my %confirmation_strings = ( RENEW_ISSUE => "This item is already checked out to you.  Return it?", );
68 my $issuenoconfirm = 1; #don't need to confirm on issue.
69 #warn "issuerid: " . $issuerid;
70 my $issuer   = GetMemberDetails($issuerid);
71 my $item     = GetItem(undef,$barcode);
72 my $borrower = GetMemberDetails(undef,$patronid);
73
74 my $branch = $issuer->{branchcode};
75 my $confirm_required = 0;
76 my $return_only = 0;
77 #warn "issuer cardnumber: " .   $issuer->{cardnumber};
78 #warn "patron cardnumber: " . $borrower->{cardnumber};
79 if ($op eq "logout") {
80     $query->param( patronid => undef );
81 }
82 elsif ( $op eq "returnbook" ) {
83     my ($doreturn) = AddReturn( $barcode, $branch );
84     #warn "returnbook: " . $doreturn;
85     $borrower = GetMemberDetails( undef, $patronid );   # update borrower
86 }
87 elsif ( $op eq "checkout" ) {
88     my $impossible  = {};
89     my $needconfirm = {};
90     if ( !$confirmed ) {
91         ( $impossible, $needconfirm ) = CanBookBeIssued( $borrower, $barcode );
92     }
93     $confirm_required = scalar keys %$needconfirm;
94
95     #warn "confirm_required: " . $confirm_required ;
96     if (scalar keys %$impossible) {
97
98         #  warn "impossible: numkeys: " . scalar (keys(%$impossible));
99         #warn join " ", keys %$impossible;
100         my $issue_error = (keys %$impossible)[0];
101
102         # FIXME  we assume only one error.
103         $template->param(
104             impossible                => $issue_error,
105             "circ_error_$issue_error" => 1,
106             title                     => $item->{title},
107             hide_main                 => 1,
108         );
109         if ($issue_error eq 'DEBT') {
110             $template->param(amount => $impossible->{DEBT});
111         }
112         #warn "issue_error: " . $issue_error ;
113         if ( $issue_error eq "NO_MORE_RENEWALS" ) {
114             $return_only = 1;
115             $template->param(
116                 returnitem => 1,
117                 barcode    => $barcode,
118             );
119         }
120     } elsif ( $needconfirm->{RENEW_ISSUE} ) {
121         if ($confirmed) {
122             #warn "renewing";
123             AddRenewal( $borrower, $item->{itemnumber} );
124         } else {
125             #warn "renew confirmation";
126             $template->param(
127                 renew               => 1,
128                 barcode             => $barcode,
129                 confirm             => 1,
130                 confirm_renew_issue => 1,
131                 hide_main           => 1,
132             );
133         }
134     } elsif ( $confirm_required && !$confirmed ) {
135         #warn "failed confirmation";
136         my $issue_error = (keys %$needconfirm)[0];
137         $template->param(
138             impossible                => (keys %$needconfirm)[0],
139             "circ_error_$issue_error" => 1,
140             hide_main                 => 1,
141         );
142     } else {
143         if ( $confirmed || $issuenoconfirm ) {    # we'll want to call getpatroninfo again to get updated issues.
144             # warn "issuing book?";
145             AddIssue( $borrower, $barcode );
146             # ($borrower, $flags) = getpatroninformation(undef,undef, $patronid);
147             # $template->param(
148             #   patronid => $patronid,
149             #   validuser => 1,
150             # );
151         } else {
152             $confirm_required = 1;
153             #warn "issue confirmation";
154             $template->param(
155                 confirm    => "Issuing title: " . $item->{title},
156                 barcode    => $barcode,
157                 hide_main  => 1,
158                 inputfocus => 'confirm',
159             );
160         }
161     }
162 } # $op
163
164 if ($borrower->{cardnumber}) {
165 #   warn "issuer's  branchcode: " .   $issuer->{branchcode};
166 #   warn   "user's  branchcode: " . $borrower->{branchcode};
167     my $borrowername = sprintf "%s %s", ($borrower->{firstname} || ''), ($borrower->{surname} || '');
168     my @issues;
169     my ($issueslist) = GetPendingIssues( $borrower->{'borrowernumber'} );
170     foreach my $it (@$issueslist) {
171         $it->{date_due_display} = format_date($it->{date_due});
172         my ($renewokay, $renewerror) = CanBookBeIssued($borrower, $it->{'barcode'},'','');
173         $it->{'norenew'} = 1 if $renewokay->{'NO_MORE_RENEWALS'};
174         push @issues, $it;
175     }
176
177     $template->param(
178         validuser => 1,
179         borrowername => $borrowername,
180         issues_count => scalar(@issues),
181         ISSUES => \@issues,
182         patronid => $patronid,
183         noitemlinks => 1 ,
184     );
185     my $inputfocus = ($return_only      == 1) ? 'returnbook' :
186                      ($confirm_required == 1) ? 'confirm'    : 'barcode' ;
187     $template->param(
188         inputfocus => $inputfocus,
189                 nofines => 1,
190         "dateformat_" . C4::Context->preference('dateformat') => 1,
191     );
192     if (C4::Context->preference('ShowPatronImageInWebBasedSelfCheck')) {
193         my ($image, $dberror) = GetPatronImage($borrower->{cardnumber});
194         if ($image) {
195             $template->param(
196                 display_patron_image => 1,
197                 cardnumber           => $borrower->{cardnumber},
198             );
199         }
200     }
201 } else {
202     $template->param(
203         patronid   => $patronid,
204         nouser     => $patronid,
205     );
206 }
207
208 output_html_with_http_headers $query, $cookie, $template->output;