Letters / alert system, continuing...
[wip/koha-chris_n.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::Search;
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->{'amountoutstanding'} > 5) {
35     $borr->{'amountoverfive'} = 1;
36 }
37 if (5 >= $borr->{'amountoutstanding'} && $borr->{'amountoutstanding'} > 0 ) {
38     $borr->{'amountoverzero'} = 1;
39 }
40 if ($borr->{'amountoutstanding'} < 0) {
41     $borr->{'amountlessthanzero'} = 1;
42     $borr->{'amountoutstanding'} = -1*($borr->{'amountoutstanding'});
43 }
44
45 $borr->{'amountoutstanding'} = sprintf "%.02f", $borr->{'amountoutstanding'};
46
47 my @bordat;
48 $bordat[0] = $borr;
49
50 $template->param(BORROWER_INFO => \@bordat);
51 $template->param(borrowernumber => $borrowernumber);
52
53 #get issued items ....
54 my $issues = getissues($borr);
55
56 my $count = 0;
57 my $overdues_count = 0;
58 my @overdues;
59 my @issuedat;
60 foreach my $key (keys %$issues) {
61     my $issue = $issues->{$key};
62     $issue->{'date_due'}  = format_date($issue->{'date_due'});
63
64     # check for reserves
65     my ($restype, $res) = CheckReserves($issue->{'itemnumber'});
66     if ($restype) {
67         $issue->{'reserved'} = 1;
68     }
69
70     my ($numaccts,$accts,$total) = getboracctrecord(undef,$borr);
71     my $charges = 0;
72     foreach my $ac (@$accts) {
73         if ($ac->{'itemnumber'} == $issue->{'itemnumber'}) {
74             $charges += $ac->{'amountoutstanding'} if $ac->{'accounttype'} eq 'F'; 
75             $charges += $ac->{'amountoutstanding'} if $ac->{'accounttype'} eq 'L';
76         } 
77     }
78     $issue->{'charges'} = $charges;
79
80     # get publictype for icon
81     
82     my $publictype = $issue->{'publictype'};
83     $issue->{$publictype} = 1;
84
85     # check if item is renewable
86     my %env;
87     my $status = renewstatus(\%env,$borrowernumber, $issue->{'itemnumber'});
88
89     $issue->{'status'} = $status;
90
91     if ($issue->{'overdue'}) {
92         push @overdues, $issue;
93         $overdues_count++;
94         $issue->{'overdue'} = 1;
95     } else {
96         $issue->{'issued'} = 1;
97     }
98     push @issuedat, $issue;
99     $count++;
100 }
101
102 $template->param(ISSUES => \@issuedat);
103 $template->param(issues_count => $count);
104
105 $template->param(OVERDUES => \@overdues);
106 $template->param(overdues_count => $overdues_count);
107
108 my $branches = getbranches();
109
110 # now the reserved items....
111 my ($rcount, $reserves) = FindReserves(undef, $borrowernumber);
112 foreach my $res (@$reserves) {
113     $res->{'reservedate'}  = format_date($res->{'reservedate'});
114     my $publictype = $res->{'publictype'};
115     $res->{$publictype} = 1;
116     $res->{'waiting'} = 1 if $res->{'found'} eq 'W';
117     $res->{'branch'} = $branches->{$res->{'branchcode'}}->{'branchname'};
118 }
119
120 $template->param(RESERVES => $reserves);
121 $template->param(reserves_count => $rcount);
122
123 my @waiting;
124 my $wcount = 0;
125 foreach my $res (@$reserves) {
126     if ($res->{'itemnumber'}) {
127         my $item = getiteminformation('',$res->{'itemnumber'},'');
128         $res->{'holdingbranch'} = $branches->{$item->{'holdingbranch'}}->{'branchname'};
129         $res->{'branch'} = $branches->{$res->{'branchcode'}}->{'branchname'};
130         if($res->{'holdingbranch'} eq $res->{'branch'}){
131                         $res->{'atdestination'} = 1;
132                 }
133         push @waiting, $res;
134         $wcount++;
135     }
136 }
137 $template->param(WAITING => \@waiting);
138
139 # current alert subscriptions
140 warn " B : $borrowernumber";
141 my $alerts = getalert($borrowernumber);
142 foreach (@$alerts) {
143         $_->{$_->{type}}=1;
144         $_->{relatedto} = findrelatedto($_->{type},$_->{externalid});
145 }
146
147 $template->param(waiting_count => $wcount,
148                                 LibraryName => C4::Context->preference("LibraryName"),
149                                 suggestion => C4::Context->preference("suggestion"),
150                                 virtualshelves => C4::Context->preference("virtualshelves"),
151                                 textmessaging => $borr->{textmessaging},
152                                 alertloop => $alerts,
153 );
154
155 output_html_with_http_headers $query, $cookie, $template->output;
156