Line 0
Link Here
|
|
|
1 |
#!/usr/bin/perl |
2 |
|
3 |
# Copyright 2013 BibLibre SARL |
4 |
# This file is part of Koha. |
5 |
# |
6 |
# Koha is free software; you can redistribute it and/or modify it under the |
7 |
# terms of the GNU General Public License as published by the Free Software |
8 |
# Foundation; either version 2 of the License, or (at your option) any later |
9 |
# version. |
10 |
# |
11 |
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
12 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
13 |
# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
14 |
# |
15 |
# You should have received a copy of the GNU General Public License along |
16 |
# with Koha; if not, write to the Free Software Foundation, Inc., |
17 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
18 |
|
19 |
=head1 NAME |
20 |
|
21 |
discharge.pl |
22 |
|
23 |
=head1 DESCRIPTION |
24 |
|
25 |
Allows librarian to edit and/or manage borrowers' discharges |
26 |
|
27 |
=cut |
28 |
|
29 |
use Modern::Perl; |
30 |
|
31 |
use CGI; |
32 |
use C4::Auth; |
33 |
use C4::Output; |
34 |
use C4::Members; |
35 |
use C4::Reserves; |
36 |
use C4::Letters; |
37 |
use C4::Discharges; |
38 |
|
39 |
use Koha::DateUtils; |
40 |
|
41 |
my $input = new CGI; |
42 |
my ($template, $loggedinuser, $cookie, $flags) = get_template_and_user( { |
43 |
template_name => 'members/discharge.tmpl', |
44 |
query => $input, |
45 |
type => 'intranet', |
46 |
authnotrequired => 0, |
47 |
flagsrequired => { 'borrowers' => '*' }, |
48 |
debug => 1, |
49 |
} ); |
50 |
|
51 |
my $dischargePath = C4::Context->preference('dischargePath'); |
52 |
my $dischargeWebPath = C4::Context->preference('dischargeWebPath'); |
53 |
|
54 |
my $borrowernumber; |
55 |
my $data; |
56 |
if ( $input->param('borrowernumber') ) { |
57 |
$borrowernumber = $input->param('borrowernumber'); |
58 |
|
59 |
# Getting member data |
60 |
$data = GetMember( borrowernumber => $borrowernumber ); |
61 |
|
62 |
# Getting pending issues |
63 |
my $issues = GetPendingIssues($borrowernumber); |
64 |
my $hasissues = scalar(@$issues); |
65 |
|
66 |
# Getting reserves |
67 |
my @reserves = GetReservesFromBorrowernumber($borrowernumber); |
68 |
my $hasreserves = scalar(@reserves); |
69 |
|
70 |
# Generating discharge if needed |
71 |
if ($input->param('generatedischarge')) { |
72 |
# If borrower has pending reserves, we cancel them |
73 |
foreach (@reserves) { |
74 |
CancelReserve($_->{'reservenumber'}); |
75 |
} |
76 |
$hasreserves = 0; |
77 |
|
78 |
# Debarring member |
79 |
# Getting librarian's name |
80 |
my $librarian = GetMember('borrowernumber' => $loggedinuser); |
81 |
my $librarianname = $librarian->{'firstname'} . " " . $librarian->{'surname'}; |
82 |
|
83 |
# Getting today's date |
84 |
my $date = C4::Dates->new()->output(); |
85 |
# (this is quite ugly, but this is how borrowers seems to be permanently debarred) |
86 |
# FIXME: Perl strings are not translatable atm, so comment is written in english |
87 |
C4::Members::DebarMember($borrowernumber, '9999-12-31', "Discharge generated by $librarianname on $date"); |
88 |
|
89 |
# Creating message |
90 |
my $letter = C4::Letters::GetPreparedLetter( |
91 |
module => 'members', |
92 |
letter_code => 'DISCHARGE', |
93 |
tables => { |
94 |
borrowers => $borrowernumber, |
95 |
branches => $data->{branchcode}, |
96 |
}, |
97 |
); |
98 |
my $today = output_pref(dt_from_string()); |
99 |
$letter->{'title'} =~ s/<<today>>/$today/g; |
100 |
$letter->{'content'} =~ s/<<today>>/$today/g; |
101 |
|
102 |
C4::Letters::EnqueueLetter( |
103 |
{ letter => $letter, |
104 |
borrowernumber => $borrowernumber, |
105 |
message_transport_type => 'print', |
106 |
} |
107 |
); |
108 |
|
109 |
# Calling gather_print_notices.pl with borrowernumber and css to create html from letter |
110 |
# We place the generated html in a per-user directory |
111 |
# We create the per-user directory if it doesn't exist |
112 |
mkdir "$dischargePath/$borrowernumber" unless -d "$dischargePath/$borrowernumber"; |
113 |
qx{../misc/cronjobs/gather_print_notices.pl -f discharge -l DISCHARGE -b $borrowernumber $dischargePath/$borrowernumber}; |
114 |
|
115 |
# Calling printoverdues.pl in this same user directory |
116 |
qx{../misc/cronjobs/printoverdues.sh $dischargePath/$borrowernumber}; |
117 |
|
118 |
# Error handling: |
119 |
# Check if we really got our discharge as a pdf file |
120 |
my $todayiso = output_pref(dt_from_string(), 'iso', 1); |
121 |
# If we don't, then |
122 |
unless (-e "$dischargePath/$borrowernumber/discharge-$todayiso.pdf") { |
123 |
# Show an error message to the librarian |
124 |
$template->param(error => 1); |
125 |
|
126 |
# Remove the possibly generated html file |
127 |
unlink "$dischargePath/$borrowernumber/discharge-$todayiso.html"; |
128 |
|
129 |
# Remove the unprocessed message from message_queue |
130 |
removeUnprocessedDischarges($borrowernumber); |
131 |
|
132 |
} |
133 |
|
134 |
} |
135 |
|
136 |
# Getting already generated discharges |
137 |
my @list = GetDischarges($borrowernumber); |
138 |
my @loop = map {{ url => $dischargeWebPath . "/" . $borrowernumber . "/" . $_, filename => $_ }} @list; |
139 |
$template->param("dischargeloop" => \@loop) if (@list); |
140 |
|
141 |
|
142 |
$template->param( |
143 |
borrowernumber => $borrowernumber, |
144 |
biblionumber => $data->{'biblionumber'}, |
145 |
title => $data->{'title'}, |
146 |
initials => $data->{'initials'}, |
147 |
surname => $data->{'surname'}, |
148 |
borrowernumber => $borrowernumber, |
149 |
firstname => $data->{'firstname'}, |
150 |
cardnumber => $data->{'cardnumber'}, |
151 |
categorycode => $data->{'categorycode'}, |
152 |
category_type => $data->{'category_type'}, |
153 |
categoryname => $data->{'description'}, |
154 |
address => $data->{'address'}, |
155 |
address2 => $data->{'address2'}, |
156 |
city => $data->{'city'}, |
157 |
zipcode => $data->{'zipcode'}, |
158 |
country => $data->{'country'}, |
159 |
phone => $data->{'phone'}, |
160 |
email => $data->{'email'}, |
161 |
branchcode => $data->{'branchcode'}, |
162 |
hasissues => $hasissues, |
163 |
hasreserves => $hasreserves, |
164 |
); |
165 |
|
166 |
} |
167 |
# Send parameters to template |
168 |
$template->param( |
169 |
dischargeview => 1, |
170 |
path_missing => ( $dischargePath and $dischargeWebPath ) ? 0 : 1, |
171 |
); |
172 |
|
173 |
output_html_with_http_headers $input, $cookie, $template->output; |