small bugfix to make logout work
[koha.git] / opac / opac-user.pl
1 #!/usr/bin/perl
2 use strict;
3 require Exporter;
4 use CGI;
5
6 use C4::Auth;
7 use C4::Koha;
8 use C4::Circulation::Circ2;
9 use C4::Circulation::Renewals2;
10 use C4::Reserves2;
11 use C4::Search;
12 use HTML::Template;
13
14 my $query = new CGI;
15 my ($template, $borrowernumber, $cookie) 
16     = get_template_and_user({template_name => "opac-user.tmpl",
17                              query => $query,
18                              type => "opac",
19                              authnotrequired => 0,
20                              flagsrequired => {borrow => 1},
21                              debug => 1,
22                              });
23
24 # get borrower information ....
25 my ($borr, $flags) = getpatroninformation(undef, $borrowernumber);
26
27 $borr->{'dateenrolled'} = slashifyDate($borr->{'dateenrolled'});
28 $borr->{'expiry'}       = slashifyDate($borr->{'expiry'});
29 $borr->{'dateofbirth'}  = slashifyDate($borr->{'dateofbirth'});
30 $borr->{'ethnicity'}    = fixEthnicity($borr->{'ethnicity'});
31
32 if ($borr->{'amountoutstanding'} > 5) {
33     $borr->{'amountoverfive'} = 1;
34 }
35 if (5 >= $borr->{'amountoutstanding'} && $borr->{'amountoutstanding'} > 0 ) {
36     $borr->{'amountoverzero'} = 1;
37 }
38 if ($borr->{'amountoutstanding'} < 0) {
39     $borr->{'amountlessthanzero'} = 1;
40     $borr->{'amountoutstanding'} = -1*($borr->{'amountoutstanding'});
41 }
42
43 $borr->{'amountoutstanding'} = sprintf "\$%.02f", $borr->{'amountoutstanding'};
44
45 my @bordat;
46 $bordat[0] = $borr;
47
48 $template->param(BORROWER_INFO => \@bordat);
49 $template->param(borrowernumber => $borrowernumber);
50
51 #get issued items ....
52 my $issues = getissues($borr);
53
54 my $count = 0;
55 my $overdues_count = 0;
56 my @overdues;
57 my @issuedat;
58 foreach my $key (keys %$issues) {
59     my $issue = $issues->{$key};
60     $issue->{'date_due'}  = slashifyDate($issue->{'date_due'});
61
62     # check for reserves
63     my ($restype, $res) = CheckReserves($issue->{'itemnumber'});
64     if ($restype) {
65         $issue->{'reserved'} = 1;
66     }
67
68     my ($numaccts,$accts,$total) = getboracctrecord(undef,$borr);
69     my $charges = 0;
70     foreach my $ac (@$accts) {
71         if ($ac->{'itemnumber'} == $issue->{'itemnumber'}) {
72             $charges += $ac->{'amountoutstanding'} if $ac->{'accounttype'} eq 'F'; 
73             $charges += $ac->{'amountoutstanding'} if $ac->{'accounttype'} eq 'L';
74         } 
75     }
76     $issue->{'charges'} = $charges;
77
78     # get publictype for icon
79     
80     my $publictype = $issue->{'publictype'};
81     $issue->{$publictype} = 1;
82
83     # check if item is renewable
84     my %env;
85     my $status = renewstatus(\%env,$borrowernumber, $issue->{'itemnumber'});
86
87     $issue->{'renewable'} = $status;
88     
89     if ($issue->{'overdue'}) {
90         push @overdues, $issue;
91         $overdues_count++;
92         $issue->{'overdue'} = 1;
93     } else {
94         $issue->{'issued'} = 1;
95     }
96     push @issuedat, $issue;
97     $count++;
98 }
99
100 $template->param(ISSUES => \@issuedat);
101 $template->param(issues_count => $count);
102
103 $template->param(OVERDUES => \@overdues);
104 $template->param(overdues_count => $overdues_count);
105
106 my $branches = getbranches();
107
108 # now the reserved items....
109 my ($rcount, $reserves) = FindReserves(undef, $borrowernumber);
110 foreach my $res (@$reserves) {
111     $res->{'reservedate'}  = slashifyDate($res->{'reservedate'});
112     my $publictype = $res->{'publictype'};
113     $res->{$publictype} = 1;
114     $res->{'waiting'} = 1 if $res->{'found'} eq 'W';
115     $res->{'branch'} = $branches->{$res->{'branchcode'}}->{'branchname'};
116 }
117
118 $template->param(RESERVES => $reserves);
119 $template->param(reserves_count => $rcount);
120
121 my @waiting;
122 my $wcount = 0;
123 foreach my $res (@$reserves) {
124     if ($res->{'itemnumber'}) {
125         $res->{'branch'} = $branches->{$res->{'branchcode'}}->{'branchname'};
126         push @waiting, $res;
127         $wcount++;
128     }
129 }
130
131 # $template->param(WAITING => \@waiting);
132 $template->param(waiting_count => $wcount);
133
134 print $query->header(-cookie => $cookie), $template->output;
135