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