1 package Koha
::Patron
::Discharge
;
5 use File
::Temp
qw( :POSIX );
8 use C4
::Templates qw
( gettemplate
);
9 use C4
::Letters qw
( GetPreparedLetter
);
12 use Koha
::DateUtils
qw( dt_from_string output_pref );
14 use Koha
::Patron
::Debarments
;
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
{
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;
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) {
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');
68 borrower
=> $borrowernumber,
69 needed
=> dt_from_string
,
75 my $borrowernumber = $params->{borrowernumber
};
76 return unless $borrowernumber and can_be_discharged
( { borrowernumber
=> $borrowernumber } );
79 my $patron = Koha
::Patrons
->find( $borrowernumber );
80 my $holds = $patron->holds;
81 while ( my $hold = $holds->next ) {
86 Koha
::Patron
::Debarments
::AddDebarment
({
87 borrowernumber
=> $borrowernumber,
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
});
99 borrower
=> $borrowernumber,
100 validated
=> dt_from_string
,
105 sub generate_as_pdf
{
107 return unless $params->{borrowernumber
};
109 my $patron = Koha
::Patrons
->find( $params->{borrowernumber
} );
110 my $letter = C4
::Letters
::GetPreparedLetter
(
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
);
123 stylesheet
=> C4
::Context
->preference("NoticeCSS"),
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;
134 my $output = eval { require PDF
::FromHTML
; return; } || $@
;
135 if ($output && $params->{testing
}) {
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
} ) {
150 foreach my $font ( @
{ $ttf->{font
} } ) {
151 $type2path->{ $font->{type
} } = $font->{content
};
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',
164 $pdf->write_file( $pdf_path );
172 my $branchcode = $params->{branchcode
};
173 my $borrowernumber = $params->{borrowernumber
};
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 );
187 my $branchcode = $params->{branchcode
};
188 my $borrowernumber = $params->{borrowernumber
};
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]
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' } );