Bug 17268: Set boolean for shared column in schema
[koha.git] / Koha / Patron / Discharge.pm
blob4cd596dfd10bf723843dae099c1d99d98e226499
1 package Koha::Patron::Discharge;
3 use Modern::Perl;
4 use CGI;
5 use File::Temp qw( :POSIX );
6 use Carp;
8 use C4::Templates qw ( gettemplate );
9 use C4::Letters qw ( GetPreparedLetter );
11 use Koha::Database;
12 use Koha::DateUtils qw( dt_from_string output_pref );
13 use Koha::Patrons;
14 use Koha::Patron::Debarments;
16 sub count {
17 my ($params) = @_;
18 my $values = {};
20 if( $params->{borrowernumber} ) {
21 $values->{borrower} = $params->{borrowernumber};
23 if( $params->{pending} ) {
24 $values->{needed} = { '!=', undef };
25 $values->{validated} = undef;
27 elsif( $params->{validated} ) {
28 $values->{validated} = { '!=', undef };
31 return search_limited( $values )->count;
34 sub can_be_discharged {
35 my ($params) = @_;
36 return unless $params->{borrowernumber};
38 my $patron = Koha::Patrons->find( $params->{borrowernumber} );
39 return unless $patron;
41 my $has_pending_checkouts = $patron->checkouts->count;
42 return $has_pending_checkouts ? 0 : 1;
45 sub is_discharged {
46 my ($params) = @_;
47 return unless $params->{borrowernumber};
48 my $borrowernumber = $params->{borrowernumber};
50 my $restricted = Koha::Patrons->find( $borrowernumber )->is_debarred;
51 my @validated = get_validated({borrowernumber => $borrowernumber});
53 if ($restricted && @validated) {
54 return 1;
55 } else {
56 return 0;
60 sub request {
61 my ($params) = @_;
62 my $borrowernumber = $params->{borrowernumber};
63 return unless $borrowernumber;
64 return unless can_be_discharged({ borrowernumber => $borrowernumber });
66 my $rs = Koha::Database->new->schema->resultset('Discharge');
67 return $rs->create({
68 borrower => $borrowernumber,
69 needed => dt_from_string,
70 });
73 sub discharge {
74 my ($params) = @_;
75 my $borrowernumber = $params->{borrowernumber};
76 return unless $borrowernumber and can_be_discharged( { borrowernumber => $borrowernumber } );
78 # Cancel reserves
79 my $patron = Koha::Patrons->find( $borrowernumber );
80 my $holds = $patron->holds;
81 while ( my $hold = $holds->next ) {
82 $hold->cancel;
85 # Debar the member
86 Koha::Patron::Debarments::AddDebarment({
87 borrowernumber => $borrowernumber,
88 type => 'DISCHARGE',
89 });
91 # Generate the discharge
92 my $rs = Koha::Database->new->schema->resultset('Discharge');
93 my $discharge = $rs->search({ borrower => $borrowernumber }, { order_by => { -desc => 'needed' }, rows => 1 });
94 if( $discharge->count > 0 ) {
95 $discharge->update({ validated => dt_from_string });
97 else {
98 $rs->create({
99 borrower => $borrowernumber,
100 validated => dt_from_string,
105 sub generate_as_pdf {
106 my ($params) = @_;
107 return unless $params->{borrowernumber};
109 my $patron = Koha::Patrons->find( $params->{borrowernumber} );
110 my $letter = C4::Letters::GetPreparedLetter(
111 module => 'members',
112 letter_code => 'DISCHARGE',
113 lang => $patron->lang,
114 tables => { borrowers => $params->{borrowernumber}, branches => $params->{'branchcode'}, },
117 my $today = output_pref( dt_from_string() );
118 $letter->{'title'} =~ s/<<today>>/$today/g;
119 $letter->{'content'} =~ s/<<today>>/$today/g;
121 my $tmpl = C4::Templates::gettemplate('batch/print-notices.tt', 'intranet', new CGI);
122 $tmpl->param(
123 stylesheet => C4::Context->preference("NoticeCSS"),
124 today => $today,
125 messages => [$letter],
128 my $html_path = tmpnam() . '.html';
129 my $pdf_path = tmpnam() . '.pdf';
130 my $html_content = $tmpl->output;
131 open my $html_fh, '>:encoding(utf8)', $html_path;
132 say $html_fh $html_content;
133 close $html_fh;
134 my $output = eval { require PDF::FromHTML; return; } || $@;
135 if ($output && $params->{testing}) {
136 carp $output;
137 $pdf_path = undef;
139 elsif ($output) {
140 die $output;
142 else {
143 my $pdf = PDF::FromHTML->new( encoding => 'utf-8' );
144 $pdf->load_file( $html_path );
146 my $ttf = C4::Context->config('ttf');
147 if ( $ttf && exists $ttf->{font} ) {
149 my $type2path;
150 foreach my $font ( @{ $ttf->{font} } ) {
151 $type2path->{ $font->{type} } = $font->{content};
154 $pdf->convert(
155 FontBold => $type2path->{'HB'} || 'HelveticaBold',
156 FontOblique => $type2path->{'HO'} || 'HelveticaOblique',
157 FontBoldOblique => $type2path->{'HBO'}|| 'HelveticaBoldOblique',
158 FontUnicode => $type2path->{'H'} || 'Helvetica',
159 Font => $type2path->{'H'} || 'Helvetica',
161 } else {
162 $pdf->convert();
164 $pdf->write_file( $pdf_path );
167 return $pdf_path;
170 sub get_pendings {
171 my ($params) = @_;
172 my $branchcode = $params->{branchcode};
173 my $borrowernumber = $params->{borrowernumber};
175 my $cond = {
176 'me.needed' => { '!=', undef },
177 'me.validated' => undef,
178 ( defined $borrowernumber ? ( 'me.borrower' => $borrowernumber ) : () ),
179 ( defined $branchcode ? ( 'borrower.branchcode' => $branchcode ) : () ),
182 return search_limited( $cond );
185 sub get_validated {
186 my ($params) = @_;
187 my $branchcode = $params->{branchcode};
188 my $borrowernumber = $params->{borrowernumber};
190 my $cond = {
191 'me.validated' => { '!=', undef },
192 ( defined $borrowernumber ? ( 'me.borrower' => $borrowernumber ) : () ),
193 ( defined $branchcode ? ( 'borrower.branchcode' => $branchcode ) : () ),
196 return search_limited( $cond );
199 # TODO This module should be based on Koha::Object[s]
200 sub search_limited {
201 my ( $params, $attributes ) = @_;
202 my $userenv = C4::Context->userenv;
203 my @restricted_branchcodes;
204 if ( $userenv and $userenv->{number} ) {
205 my $logged_in_user = Koha::Patrons->find( $userenv->{number} );
206 @restricted_branchcodes = $logged_in_user->libraries_where_can_see_patrons;
208 $params->{'borrower.branchcode'} = { -in => \@restricted_branchcodes } if @restricted_branchcodes;
209 $attributes->{join} = 'borrower';
211 my $rs = Koha::Database->new->schema->resultset('Discharge');
212 return $rs->search( $params, { join => 'borrower' } );