/
Discharge.pm
203 lines (166 loc) · 5.77 KB
/
Discharge.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package Koha::Patron::Discharge;
use Modern::Perl;
use CGI;
use File::Temp qw( tmpnam );
use IPC::Cmd;
use Carp qw( carp );
use C4::Templates qw ( gettemplate );
use C4::Letters qw( GetPreparedLetter );
use Koha::Database;
use Koha::DateUtils qw( dt_from_string output_pref );
use Koha::Patrons;
use Koha::Patron::Debarments qw( AddDebarment );
sub count {
my ($params) = @_;
my $values = {};
if( $params->{borrowernumber} ) {
$values->{borrower} = $params->{borrowernumber};
}
if( $params->{pending} ) {
$values->{needed} = { '!=', undef };
$values->{validated} = undef;
}
elsif( $params->{validated} ) {
$values->{validated} = { '!=', undef };
}
return search_limited( $values )->count;
}
sub can_be_discharged {
my ($params) = @_;
return unless $params->{borrowernumber};
my $patron = Koha::Patrons->find( $params->{borrowernumber} );
return unless $patron;
my $has_pending_checkouts = $patron->checkouts->count;
return $has_pending_checkouts ? 0 : 1;
}
sub is_discharged {
my ($params) = @_;
return unless $params->{borrowernumber};
my $borrowernumber = $params->{borrowernumber};
my $restricted = Koha::Patrons->find( $borrowernumber )->is_debarred;
my @validated = get_validated({borrowernumber => $borrowernumber});
if ($restricted && @validated) {
return 1;
} else {
return 0;
}
}
sub request {
my ($params) = @_;
my $borrowernumber = $params->{borrowernumber};
return unless $borrowernumber;
return unless can_be_discharged({ borrowernumber => $borrowernumber });
my $rs = Koha::Database->new->schema->resultset('Discharge');
return $rs->create({
borrower => $borrowernumber,
needed => dt_from_string,
});
}
sub discharge {
my ($params) = @_;
my $borrowernumber = $params->{borrowernumber};
return unless $borrowernumber and can_be_discharged( { borrowernumber => $borrowernumber } );
# Cancel reserves
my $patron = Koha::Patrons->find( $borrowernumber );
my $holds = $patron->holds;
while ( my $hold = $holds->next ) {
$hold->cancel;
}
# Debar the member
Koha::Patron::Debarments::AddDebarment({
borrowernumber => $borrowernumber,
type => 'DISCHARGE',
});
# Generate the discharge
my $rs = Koha::Database->new->schema->resultset('Discharge');
my $discharge = $rs->search({ borrower => $borrowernumber }, { order_by => { -desc => 'needed' }, rows => 1 });
if( $discharge->count > 0 ) {
$discharge->update({ validated => dt_from_string });
}
else {
$rs->create({
borrower => $borrowernumber,
validated => dt_from_string,
});
}
}
sub generate_as_pdf {
my ($params) = @_;
return unless $params->{borrowernumber};
my $patron = Koha::Patrons->find( $params->{borrowernumber} );
my $letter = C4::Letters::GetPreparedLetter(
module => 'members',
letter_code => 'DISCHARGE',
lang => $patron->lang,
tables => { borrowers => $params->{borrowernumber}, branches => $params->{'branchcode'}, },
);
my $today = output_pref( dt_from_string() );
$letter->{'title'} =~ s/<<today>>/$today/g;
$letter->{'content'} =~ s/<<today>>/$today/g;
my $tmpl = C4::Templates::gettemplate('batch/print-notices.tt', 'intranet', CGI->new);
$tmpl->param(
stylesheet => C4::Context->preference("NoticeCSS"),
today => $today,
messages => [$letter],
);
my $html_path = tmpnam() . '.html';
my $pdf_path = tmpnam() . '.pdf';
my $html_content = $tmpl->output;
# export to HTML
open my $html_fh, '>:encoding(utf8)', $html_path;
say $html_fh $html_content;
close $html_fh;
if ( IPC::Cmd::can_run('weasyprint') ) {
my( $success, $error_message, $full_buf, $stdout_buf, $stderr_buf ) =
IPC::Cmd::run( command => "weasyprint $html_path $pdf_path", verbose => 0 );
map {warn $_} @$stderr_buf
if $stderr_buf and scalar @$stderr_buf;
unless ( $success ) {
warn $error_message;
$pdf_path = undef;
}
}
else {
warn "weasyprint not found!";
$pdf_path = undef;
}
return $pdf_path;
}
sub get_pendings {
my ($params) = @_;
my $branchcode = $params->{branchcode};
my $borrowernumber = $params->{borrowernumber};
my $cond = {
'me.needed' => { '!=', undef },
'me.validated' => undef,
( defined $borrowernumber ? ( 'me.borrower' => $borrowernumber ) : () ),
( defined $branchcode ? ( 'borrower.branchcode' => $branchcode ) : () ),
};
return search_limited( $cond );
}
sub get_validated {
my ($params) = @_;
my $branchcode = $params->{branchcode};
my $borrowernumber = $params->{borrowernumber};
my $cond = {
'me.validated' => { '!=', undef },
( defined $borrowernumber ? ( 'me.borrower' => $borrowernumber ) : () ),
( defined $branchcode ? ( 'borrower.branchcode' => $branchcode ) : () ),
};
return search_limited( $cond );
}
# TODO This module should be based on Koha::Object[s]
sub search_limited {
my ( $params, $attributes ) = @_;
my $userenv = C4::Context->userenv;
my @restricted_branchcodes;
if ( $userenv and $userenv->{number} ) {
my $logged_in_user = Koha::Patrons->find( $userenv->{number} );
@restricted_branchcodes = $logged_in_user->libraries_where_can_see_patrons;
}
$params->{'borrower.branchcode'} = { -in => \@restricted_branchcodes } if @restricted_branchcodes;
$attributes->{join} = 'borrower';
my $rs = Koha::Database->new->schema->resultset('Discharge');
return $rs->search( $params, { join => 'borrower' } );
}
1;