bug 3436: improve error messages in self-check
[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 my ($template, $loggedinuser, $cookie) = get_template_and_user({
35     template_name   => "sco/sco-main.tmpl",
36     authnotrequired => 0,
37       flagsrequired => { circulate => "circulate_remaining_permissions" },
38     query => $query,
39     type  => "opac",
40     debug => 1,
41 });
42
43 my $issuerid = $loggedinuser;
44 my ($op, $patronid, $barcode, $confirmed, $timedout) = (
45     $query->param("op")         || '',
46     $query->param("patronid")   || '',
47     $query->param("barcode")    || '',
48     $query->param("confirmed")  || '',
49     $query->param("timedout")   || '', #not actually using this...
50 );
51
52 my %confirmation_strings = ( RENEW_ISSUE => "This item is already checked out to you.  Return it?", );
53 my $issuenoconfirm = 1; #don't need to confirm on issue.
54 #warn "issuerid: " . $issuerid;
55 my $issuer   = GetMemberDetails($issuerid);
56 my $item     = GetItem(undef,$barcode);
57 my $borrower = GetMemberDetails(undef,$patronid);
58
59 my $branch = $issuer->{branchcode};
60 my $confirm_required = 0;
61 my $return_only = 0;
62 #warn "issuer cardnumber: " .   $issuer->{cardnumber};
63 #warn "patron cardnumber: " . $borrower->{cardnumber};
64 if ($op eq "logout") {
65     $query->param( patronid => undef );
66 }
67 elsif ( $op eq "returnbook" ) {
68     my ($doreturn) = AddReturn( $barcode, $branch );
69     #warn "returnbook: " . $doreturn;
70     $borrower = GetMemberDetails( undef, $patronid );   # update borrower
71 }
72 elsif ( $op eq "checkout" ) {
73     my $impossible  = {};
74     my $needconfirm = {};
75     if ( !$confirmed ) {
76         ( $impossible, $needconfirm ) = CanBookBeIssued( $borrower, $barcode );
77     }
78     $confirm_required = scalar keys %$needconfirm;
79
80     #warn "confirm_required: " . $confirm_required ;
81     if (scalar keys %$impossible) {
82
83         #  warn "impossible: numkeys: " . scalar (keys(%$impossible));
84         #warn join " ", keys %$impossible;
85         my $issue_error = (keys %$impossible)[0];
86
87         # FIXME  we assume only one error.
88         $template->param(
89             impossible                => $issue_error,
90             "circ_error_$issue_error" => 1,
91             title                     => $item->{title},
92             hide_main                 => 1,
93         );
94         if ($issue_error eq 'DEBT') {
95             $template->param(amount => $impossible->{DEBT});
96         }
97         #warn "issue_error: " . $issue_error ;
98         if ( $issue_error eq "NO_MORE_RENEWALS" ) {
99             $return_only = 1;
100             $template->param(
101                 returnitem => 1,
102                 barcode    => $barcode,
103             );
104         }
105     } elsif ( $needconfirm->{RENEW_ISSUE} ) {
106         if ($confirmed) {
107             #warn "renewing";
108             AddRenewal( $borrower, $item->{itemnumber} );
109         } else {
110             #warn "renew confirmation";
111             $template->param(
112                 renew               => 1,
113                 barcode             => $barcode,
114                 confirm             => 1,
115                 confirm_renew_issue => 1,
116                 hide_main           => 1,
117             );
118         }
119     } elsif ( $confirm_required && !$confirmed ) {
120         #warn "failed confirmation";
121         my $issue_error = (keys %$needconfirm)[0];
122         $template->param(
123             impossible                => (keys %$needconfirm)[0],
124             "circ_error_$issue_error" => 1,
125             hide_main                 => 1,
126         );
127     } else {
128         if ( $confirmed || $issuenoconfirm ) {    # we'll want to call getpatroninfo again to get updated issues.
129             # warn "issuing book?";
130             AddIssue( $borrower, $barcode );
131             # ($borrower, $flags) = getpatroninformation(undef,undef, $patronid);
132             # $template->param(
133             #   patronid => $patronid,
134             #   validuser => 1,
135             # );
136         } else {
137             $confirm_required = 1;
138             #warn "issue confirmation";
139             $template->param(
140                 confirm    => "Issuing title: " . $item->{title},
141                 barcode    => $barcode,
142                 hide_main  => 1,
143                 inputfocus => 'confirm',
144             );
145         }
146     }
147 } # $op
148
149 if ($borrower->{cardnumber}) {
150 #   warn "issuer's  branchcode: " .   $issuer->{branchcode};
151 #   warn   "user's  branchcode: " . $borrower->{branchcode};
152     my $borrowername = sprintf "%s %s", ($borrower->{firstname} || ''), ($borrower->{surname} || '');
153     my @issues;
154     my ($issueslist) = GetPendingIssues( $borrower->{'borrowernumber'} );
155     foreach my $it (@$issueslist) {
156         $it->{date_due_display} = format_date($it->{date_due});
157         my ($renewokay, $renewerror) = CanBookBeIssued($borrower, $it->{'barcode'},'','');
158         $it->{'norenew'} = 1 if $renewokay->{'NO_MORE_RENEWALS'};
159         push @issues, $it;
160     }
161
162     $template->param(
163         validuser => 1,
164         borrowername => $borrowername,
165         issues_count => scalar(@issues),
166         ISSUES => \@issues,
167         patronid => $patronid,
168         noitemlinks => 1 ,
169     );
170     my $inputfocus = ($return_only      == 1) ? 'returnbook' :
171                      ($confirm_required == 1) ? 'confirm'    : 'barcode' ;
172     $template->param(
173         inputfocus => $inputfocus,
174                 nofines => 1,
175         "dateformat_" . C4::Context->preference('dateformat') => 1,
176     );
177     if (C4::Context->preference('ShowPatronImageInWebBasedSelfCheck')) {
178         my ($image, $dberror) = GetPatronImage($borrower->{cardnumber});
179         if ($image) {
180             $template->param(
181                 display_patron_image => 1,
182                 cardnumber           => $borrower->{cardnumber},
183             );
184         }
185     }
186 } else {
187     $template->param(
188         patronid   => $patronid,
189         nouser     => $patronid,
190     );
191 }
192
193 output_html_with_http_headers $query, $cookie, $template->output;