Bug 15084: Replace C4::Budgets::GetCurrencies with Koha::Acquisition::Currencies...
[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 # Parts Copyright 2010-2011, ByWater Solutions (those related to username/password auth)
6 #
7 # This file is part of Koha.
8 #
9 # Koha is free software; you can redistribute it and/or modify it
10 # under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # Koha is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21
22 # We're going to authenticate a self-check user.  we'll add a flag to borrowers 'selfcheck'
23 #
24 # We're in a controlled environment; we trust the user.
25 # So the selfcheck station will accept a patronid and issue items to that borrower.
26 # FIXME: NOT really a controlled environment...  We're on the internet!
27 #
28 # The checkout permission comes form the CGI cookie/session of a staff user.
29 # The patron is not really logging in here in the same way as they do on the
30 # rest of the OPAC.  So don't confuse loggedinuser with the patron user.
31 #
32 # FIXME: inputfocus not really used in TMPL
33
34 use strict;
35 use warnings;
36
37 use CGI qw ( -utf8 );
38 use Digest::MD5 qw(md5_base64);
39
40 use C4::Auth qw(get_template_and_user checkpw);
41 use C4::Koha;
42 use C4::Circulation;
43 use C4::Reserves;
44 use C4::Output;
45 use C4::Members;
46 use C4::Biblio;
47 use C4::Items;
48 use Koha::Acquisition::Currencies;
49
50 my $query = new CGI;
51
52 unless (C4::Context->preference('WebBasedSelfCheck')) {
53     # redirect to OPAC home if self-check is not enabled
54     print $query->redirect("/cgi-bin/koha/opac-main.pl");
55     exit;
56 }
57
58 if (C4::Context->preference('AutoSelfCheckAllowed')) 
59 {
60     my $AutoSelfCheckID = C4::Context->preference('AutoSelfCheckID');
61     my $AutoSelfCheckPass = C4::Context->preference('AutoSelfCheckPass');
62     $query->param(-name=>'userid',-values=>[$AutoSelfCheckID]);
63     $query->param(-name=>'password',-values=>[$AutoSelfCheckPass]);
64     $query->param(-name=>'koha_login_context',-values=>['sco']);
65 }
66 my ($template, $loggedinuser, $cookie) = get_template_and_user({
67     template_name   => "sco/sco-main.tt",
68     authnotrequired => 0,
69     flagsrequired => { circulate => "self_checkout" },
70     query => $query,
71     type  => "opac",
72     debug => 1,
73 });
74
75 if (C4::Context->preference('SelfCheckoutByLogin'))
76 {
77     $template->param(authbylogin  => 1);
78 }
79
80 # Get the self checkout timeout preference, or use 120 seconds as a default
81 my $selfchecktimeout = 120000;
82 if (C4::Context->preference('SelfCheckTimeout')) { 
83     $selfchecktimeout = C4::Context->preference('SelfCheckTimeout') * 1000;
84 }
85 $template->param(SelfCheckTimeout => $selfchecktimeout);
86
87 # Checks policy laid out by AllowSelfCheckReturns, defaults to 'on' if preference is undefined
88 my $allowselfcheckreturns = 1;
89 if (defined C4::Context->preference('AllowSelfCheckReturns')) {
90     $allowselfcheckreturns = C4::Context->preference('AllowSelfCheckReturns');
91 }
92 $template->param(AllowSelfCheckReturns => $allowselfcheckreturns);
93
94
95 my $issuerid = $loggedinuser;
96 my ($op, $patronid, $patronlogin, $patronpw, $barcode, $confirmed) = (
97     $query->param("op")         || '',
98     $query->param("patronid")   || '',
99     $query->param("patronlogin")|| '',
100     $query->param("patronpw")   || '',
101     $query->param("barcode")    || '',
102     $query->param("confirmed")  || '',
103 );
104
105 my $issuenoconfirm = 1; #don't need to confirm on issue.
106 #warn "issuerid: " . $issuerid;
107 my $issuer   = GetMemberDetails($issuerid);
108 my $item     = GetItem(undef,$barcode);
109 if (C4::Context->preference('SelfCheckoutByLogin') && !$patronid) {
110     my $dbh = C4::Context->dbh;
111     my $resval;
112     ($resval, $patronid) = checkpw($dbh, $patronlogin, $patronpw);
113 }
114 my $borrower = GetMemberDetails(undef,$patronid);
115
116 my $currencySymbol = "";
117 if ( my $active_currency = Koha::Acquisition::Currencies->get_active ) {
118     $currencySymbol = $active_currency->symbol;
119 }
120
121 my $branch = $issuer->{branchcode};
122 my $confirm_required = 0;
123 my $return_only = 0;
124 #warn "issuer cardnumber: " .   $issuer->{cardnumber};
125 #warn "patron cardnumber: " . $borrower->{cardnumber};
126 if ($op eq "logout") {
127     $query->param( patronid => undef, patronlogin => undef, patronpw => undef );
128 }
129 elsif ( $op eq "returnbook" && $allowselfcheckreturns ) {
130     my ($doreturn) = AddReturn( $barcode, $branch );
131     #warn "returnbook: " . $doreturn;
132     $borrower = GetMemberDetails(undef,$patronid);
133 }
134 elsif ( $op eq "checkout" ) {
135     my $impossible  = {};
136     my $needconfirm = {};
137     if ( !$confirmed ) {
138         ( $impossible, $needconfirm ) = CanBookBeIssued(
139             $borrower,
140             $barcode,
141             undef,
142             0,
143             C4::Context->preference("AllowItemsOnHoldCheckout")
144         );
145     }
146     $confirm_required = scalar keys %$needconfirm;
147
148     #warn "confirm_required: " . $confirm_required ;
149     if (scalar keys %$impossible) {
150
151         #  warn "impossible: numkeys: " . scalar (keys(%$impossible));
152         #warn join " ", keys %$impossible;
153         my $issue_error = (keys %$impossible)[0];
154
155         # FIXME  we assume only one error.
156         $template->param(
157             impossible                => $issue_error,
158             "circ_error_$issue_error" => 1,
159             title                     => $item->{title},
160             hide_main                 => 1,
161         );
162         if ($issue_error eq 'DEBT') {
163             $template->param(amount => $currencySymbol.$impossible->{DEBT});
164         }
165         #warn "issue_error: " . $issue_error ;
166         if ( $issue_error eq "NO_MORE_RENEWALS" ) {
167             $return_only = 1;
168             $template->param(
169                 returnitem => 1,
170                 barcode    => $barcode,
171             );
172         }
173     } elsif ( $needconfirm->{RENEW_ISSUE} ) {
174         if ($confirmed) {
175             #warn "renewing";
176             AddRenewal( $borrower, $item->{itemnumber} );
177         } else {
178             #warn "renew confirmation";
179             $template->param(
180                 renew               => 1,
181                 barcode             => $barcode,
182                 confirm             => 1,
183                 confirm_renew_issue => 1,
184                 hide_main           => 1,
185             );
186         }
187     } elsif ( $confirm_required && !$confirmed ) {
188         #warn "failed confirmation";
189         my $issue_error = (keys %$needconfirm)[0];
190         $template->param(
191             impossible                => (keys %$needconfirm)[0],
192             "circ_error_$issue_error" => 1,
193             hide_main                 => 1,
194         );
195         if ($issue_error eq 'DEBT') {
196             $template->param(amount => $currencySymbol.$needconfirm->{DEBT});
197         }
198     } else {
199         if ( $confirmed || $issuenoconfirm ) {    # we'll want to call getpatroninfo again to get updated issues.
200             # warn "issuing book?";
201             AddIssue( $borrower, $barcode );
202             # ($borrower, $flags) = getpatroninformation(undef,undef, $patronid);
203             # $template->param(
204             #   patronid => $patronid,
205             #   validuser => 1,
206             # );
207         } else {
208             $confirm_required = 1;
209             #warn "issue confirmation";
210             $template->param(
211                 confirm    => "Issuing title: " . $item->{title},
212                 barcode    => $barcode,
213                 hide_main  => 1,
214                 inputfocus => 'confirm',
215             );
216         }
217     }
218 } # $op
219
220 if ($borrower->{cardnumber}) {
221 #   warn "issuer's  branchcode: " .   $issuer->{branchcode};
222 #   warn   "user's  branchcode: " . $borrower->{branchcode};
223     my $borrowername = sprintf "%s %s", ($borrower->{firstname} || ''), ($borrower->{surname} || '');
224     my @issues;
225     my ($issueslist) = GetPendingIssues( $borrower->{'borrowernumber'} );
226     foreach my $it (@$issueslist) {
227         my ($renewokay, $renewerror) = CanBookBeIssued(
228             $borrower,
229             $it->{'barcode'},
230             undef,
231             0,
232             C4::Context->preference("AllowItemsOnHoldCheckout")
233         );
234         $it->{'norenew'} = 1 if $renewokay->{'NO_MORE_RENEWALS'};
235         push @issues, $it;
236     }
237
238     $template->param(
239         validuser => 1,
240         borrowername => $borrowername,
241         issues_count => scalar(@issues),
242         ISSUES => \@issues,
243         patronid => $patronid,
244         patronlogin => $patronlogin,
245         patronpw => $patronpw,
246         noitemlinks => 1 ,
247         borrowernumber => $borrower->{'borrowernumber'},
248     );
249     my $inputfocus = ($return_only      == 1) ? 'returnbook' :
250                      ($confirm_required == 1) ? 'confirm'    : 'barcode' ;
251     $template->param(
252         inputfocus => $inputfocus,
253         nofines => 1,
254
255     );
256     if (C4::Context->preference('ShowPatronImageInWebBasedSelfCheck')) {
257         my ($image, $dberror) = GetPatronImage($borrower->{borrowernumber});
258         if ($image) {
259             $template->param(
260                 display_patron_image => 1,
261                 cardnumber           => $borrower->{cardnumber},
262             );
263         }
264     }
265 } else {
266     $template->param(
267         patronid   => $patronid,
268         nouser     => $patronid,
269     );
270 }
271
272 $template->param(
273     SCOUserJS  => C4::Context->preference('SCOUserJS'),
274     SCOUserCSS => C4::Context->preference('SCOUserCSS'),
275 );
276
277 output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };