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