Bug 18001 - Unit Test
[koha.git] / members / moremember.pl
blob7bea08da0abf970071cde9e02ae1a9dd69006dd0
1 #!/usr/bin/perl
3 # Copyright 2000-2002 Katipo Communications
4 # Copyright 2010 BibLibre
5 # Copyright 2014 ByWater Solutions
7 # This file is part of Koha.
9 # Koha is free software; you can redistribute it and/or modify it
10 # under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
14 # Koha is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with Koha; if not, see <http://www.gnu.org/licenses>.
23 =head1 moremember.pl
25 script to do a borrower enquiry/bring up borrower details etc
26 Displays all the details about a borrower
27 written 20/12/99 by chris@katipo.co.nz
28 last modified 21/1/2000 by chris@katipo.co.nz
29 modified 31/1/2001 by chris@katipo.co.nz
30 to not allow items on request to be renewed
32 needs html removed and to use the C4::Output more, but its tricky
34 =cut
36 use strict;
37 #use warnings; FIXME - Bug 2505
38 use CGI qw ( -utf8 );
39 use C4::Context;
40 use C4::Auth;
41 use C4::Output;
42 use C4::Members;
43 use C4::Members::Attributes;
44 use C4::Members::AttributeTypes;
45 use C4::Reserves;
46 use C4::Circulation;
47 use C4::Koha;
48 use C4::Letters;
49 use C4::Biblio;
50 use C4::Form::MessagingPreferences;
51 use List::MoreUtils qw/uniq/;
52 use C4::Members::Attributes qw(GetBorrowerAttributes);
53 use Koha::AuthorisedValues;
54 use Koha::CsvProfiles;
55 use Koha::Patron::Debarments qw(GetDebarments);
56 use Koha::Patron::Images;
57 use Module::Load;
58 if ( C4::Context->preference('NorwegianPatronDBEnable') && C4::Context->preference('NorwegianPatronDBEnable') == 1 ) {
59 load Koha::NorwegianPatronDB, qw( NLGetSyncDataFromBorrowernumber );
61 #use Smart::Comments;
62 #use Data::Dumper;
63 use DateTime;
64 use Koha::DateUtils;
65 use Koha::Database;
66 use Koha::Patron::Categories;
67 use Koha::Token;
69 use vars qw($debug);
71 BEGIN {
72 $debug = $ENV{DEBUG} || 0;
75 my $dbh = C4::Context->dbh;
77 my $input = CGI->new;
78 $debug or $debug = $input->param('debug') || 0;
79 my $print = $input->param('print');
81 my $template_name;
82 my $quickslip = 0;
84 my $flagsrequired;
85 if (defined $print and $print eq "page") {
86 $template_name = "members/moremember-print.tt";
87 # circ staff who process checkouts but can't edit
88 # patrons still need to be able to access print view
89 $flagsrequired = { circulate => "circulate_remaining_permissions" };
90 } elsif (defined $print and $print eq "slip") {
91 $template_name = "members/moremember-receipt.tt";
92 # circ staff who process checkouts but can't edit
93 # patrons still need to be able to print receipts
94 $flagsrequired = { circulate => "circulate_remaining_permissions" };
95 } elsif (defined $print and $print eq "qslip") {
96 $template_name = "members/moremember-receipt.tt";
97 $quickslip = 1;
98 $flagsrequired = { circulate => "circulate_remaining_permissions" };
99 } elsif (defined $print and $print eq "brief") {
100 $template_name = "members/moremember-brief.tt";
101 $flagsrequired = { borrowers => 1 };
102 } else {
103 $template_name = "members/moremember.tt";
104 $flagsrequired = { borrowers => 1 };
107 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
109 template_name => $template_name,
110 query => $input,
111 type => "intranet",
112 authnotrequired => 0,
113 flagsrequired => $flagsrequired,
114 debug => 1,
117 my $borrowernumber = $input->param('borrowernumber');
118 my $error = $input->param('error');
119 $template->param( error => $error ) if ( $error );
121 my $patron = Koha::Patrons->find($borrowernumber);
122 my $issues = $patron->checkouts;
123 my $balance = $patron->account->balance;
124 $template->param(
125 issuecount => $issues->count,
126 fines => $balance,
130 my $data = GetMember( 'borrowernumber' => $borrowernumber );
132 if ( not defined $data ) {
133 $template->param (unknowuser => 1);
134 output_html_with_http_headers $input, $cookie, $template->output;
135 exit;
138 my $category_type = $data->{'category_type'};
140 $debug and printf STDERR "dates (enrolled,expiry,birthdate) raw: (%s, %s, %s)\n", map {$data->{$_}} qw(dateenrolled dateexpiry dateofbirth);
141 foreach (qw(dateenrolled dateexpiry dateofbirth)) {
142 my $userdate = $data->{$_};
143 unless ($userdate) {
144 $debug and warn sprintf "Empty \$data{%12s}", $_;
145 $data->{$_} = '';
146 next;
148 $data->{$_} = dt_from_string( $userdate );
150 $data->{'IS_ADULT'} = ( $data->{'categorycode'} ne 'I' );
152 for (qw(gonenoaddress lost borrowernotes)) {
153 $data->{$_} and $template->param(flagged => 1) and last;
156 if ( $patron->is_debarred ) {
157 $template->param( 'userdebarred' => 1, 'flagged' => 1 );
158 my $debar = $data->{'debarred'};
159 if ( $debar ne "9999-12-31" ) {
160 $template->param( 'userdebarreddate' => output_pref( { dt => dt_from_string( $debar ), dateonly => 1 } ) );
161 $template->param( 'debarredcomment' => $data->{debarredcomment} );
165 $data->{ "sex_".$data->{'sex'}."_p" } = 1 if defined $data->{sex};
167 if ( $category_type eq 'C') {
168 my $patron_categories = Koha::Patron::Categories->search_limited({ category_type => 'A' }, {order_by => ['categorycode']});
169 $template->param( 'CATCODE_MULTI' => 1) if $patron_categories->count > 1;
170 $template->param( 'catcode' => $patron_categories->next ) if $patron_categories->count == 1;
173 my @relatives;
174 if ( my $guarantor = $patron->guarantor ) {
175 $template->param( guarantor => $guarantor );
176 push @relatives, $guarantor->borrowernumber;
177 push @relatives, $_->borrowernumber for $patron->siblings;
178 } elsif ( $patron->contactname || $patron->contactfirstname ) {
179 $template->param(
180 guarantor => {
181 firstname => $patron->contactfirstname,
182 surname => $patron->contactname,
185 } else {
186 my @guarantees = $patron->guarantees;
187 $template->param( guarantees => \@guarantees );
188 push @relatives, $_->borrowernumber for @guarantees;
191 my $relatives_issues_count =
192 Koha::Database->new()->schema()->resultset('Issue')
193 ->count( { borrowernumber => \@relatives } );
195 $template->param( adultborrower => 1 ) if ( $category_type eq 'A' || $category_type eq 'I' );
197 my %bor;
198 $bor{'borrowernumber'} = $borrowernumber;
200 # Converts the branchcode to the branch name
201 my $samebranch;
202 if ( C4::Context->preference("IndependentBranches") ) {
203 my $userenv = C4::Context->userenv;
204 if ( C4::Context->IsSuperLibrarian() ) {
205 $samebranch = 1;
207 else {
208 $samebranch = ( $data->{'branchcode'} eq $userenv->{branch} );
211 else {
212 $samebranch = 1;
214 my $library = Koha::Libraries->find( $data->{branchcode})->unblessed;
215 @{$data}{keys %$library} = values %$library; # merge in all branch columns
217 my ( $total, $accts, $numaccts) = GetMemberAccountRecords( $borrowernumber );
219 # If printing a page, send the account informations to the template
220 if ($print eq "page") {
221 foreach my $accountline (@$accts) {
222 $accountline->{amount} = sprintf '%.2f', $accountline->{amount};
223 $accountline->{amountoutstanding} = sprintf '%.2f', $accountline->{amountoutstanding};
225 if ($accountline->{accounttype} ne 'F' && $accountline->{accounttype} ne 'FU'){
226 $accountline->{printtitle} = 1;
229 $template->param( accounts => $accts );
232 # Show OPAC privacy preference is system preference is set
233 if ( C4::Context->preference('OPACPrivacy') ) {
234 $template->param( OPACPrivacy => 1);
235 $template->param( "privacy".$data->{'privacy'} => 1);
238 my $today = DateTime->now( time_zone => C4::Context->tz);
239 $today->truncate(to => 'day');
240 my $overdues_exist = 0;
241 my $totalprice = 0;
243 # Calculate and display patron's age
244 if ( $data->{dateofbirth} ) {
245 $template->param( age => Koha::Patron->new({ dateofbirth => $data->{dateofbirth} })->get_age );
248 ### ###############################################################################
249 # BUILD HTML
250 # show all reserves of this borrower, and the position of the reservation ....
251 if ($borrowernumber) {
252 $template->param(
253 holds_count => Koha::Database->new()->schema()->resultset('Reserve')
254 ->count( { borrowernumber => $borrowernumber } ) );
257 # current alert subscriptions
258 my $alerts = getalert($borrowernumber);
259 foreach (@$alerts) {
260 $_->{ $_->{type} } = 1;
261 $_->{relatedto} = findrelatedto( $_->{type}, $_->{externalid} );
264 # Add sync data to the user data
265 if ( C4::Context->preference('NorwegianPatronDBEnable') && C4::Context->preference('NorwegianPatronDBEnable') == 1 ) {
266 my $sync = NLGetSyncDataFromBorrowernumber( $borrowernumber );
267 if ( $sync ) {
268 $data->{'sync'} = $sync->sync;
269 $data->{'syncstatus'} = $sync->syncstatus;
270 $data->{'lastsync'} = $sync->lastsync;
274 # check to see if patron's image exists in the database
275 # basically this gives us a template var to condition the display of
276 # patronimage related interface on
277 my $patron_image = Koha::Patron::Images->find($data->{borrowernumber});
278 $template->param( picture => 1 ) if $patron_image;
279 # Generate CSRF token for upload and delete image buttons
280 $template->param(
281 csrf_token => Koha::Token->new->generate_csrf({ session_id => $input->cookie('CGISESSID'),}),
285 $template->param(%$data);
287 if (C4::Context->preference('ExtendedPatronAttributes')) {
288 my $attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
289 my @classes = uniq( map {$_->{class}} @$attributes );
290 @classes = sort @classes;
292 my @attributes_loop;
293 for my $class (@classes) {
294 my @items;
295 for my $attr (@$attributes) {
296 push @items, $attr if $attr->{class} eq $class
298 my $av = Koha::AuthorisedValues->search({ category => 'PA_CLASS', authorised_value => $class });
299 my $lib = $av->count ? $av->next->lib : $class;
301 push @attributes_loop, {
302 class => $class,
303 items => \@items,
304 lib => $lib,
308 $template->param(
309 ExtendedPatronAttributes => 1,
310 attributes_loop => \@attributes_loop
313 my @types = C4::Members::AttributeTypes::GetAttributeTypes();
314 if (scalar(@types) == 0) {
315 $template->param(no_patron_attribute_types => 1);
319 if (C4::Context->preference('EnhancedMessagingPreferences')) {
320 C4::Form::MessagingPreferences::set_form_values({ borrowernumber => $borrowernumber }, $template);
321 $template->param(messaging_form_inactive => 1);
322 $template->param(SMSSendDriver => C4::Context->preference("SMSSendDriver"));
323 $template->param(SMSnumber => $data->{'smsalertnumber'});
324 $template->param(TalkingTechItivaPhone => C4::Context->preference("TalkingTechItivaPhoneNotification"));
327 if ( C4::Context->preference("ExportCircHistory") ) {
328 $template->param(csv_profiles => [ Koha::CsvProfiles->search({ type => 'marc' }) ]);
331 # in template <TMPL_IF name="I"> => instutitional (A for Adult, C for children)
332 $template->param( $data->{'categorycode'} => 1 );
333 $template->param(
334 patron => $patron,
335 detailview => 1,
336 borrowernumber => $borrowernumber,
337 othernames => $data->{'othernames'},
338 categoryname => $data->{'description'},
339 was_renewed => scalar $input->param('was_renewed') ? 1 : 0,
340 todaysdate => output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 }),
341 totalprice => sprintf("%.2f", $totalprice),
342 totaldue => sprintf("%.2f", $total),
343 totaldue_raw => $total,
344 overdues_exist => $overdues_exist,
345 StaffMember => ($category_type eq 'S'),
346 is_child => ($category_type eq 'C'),
347 samebranch => $samebranch,
348 quickslip => $quickslip,
349 housebound_role => $patron->housebound_role,
350 privacy_guarantor_checkouts => $data->{'privacy_guarantor_checkouts'},
351 activeBorrowerRelationship => (C4::Context->preference('borrowerRelationship') ne ''),
352 AutoResumeSuspendedHolds => C4::Context->preference('AutoResumeSuspendedHolds'),
353 SuspendHoldsIntranet => C4::Context->preference('SuspendHoldsIntranet'),
354 RoutingSerials => C4::Context->preference('RoutingSerials'),
355 debarments => GetDebarments({ borrowernumber => $borrowernumber }),
356 PatronsPerPage => C4::Context->preference("PatronsPerPage") || 20,
357 relatives_issues_count => $relatives_issues_count,
358 relatives_borrowernumbers => \@relatives,
361 output_html_with_http_headers $input, $cookie, $template->output;