Bug 16141: Making Transfers to Receive action a button
[koha.git] / Koha / Patron / Discharge.pm
blob4b775dcee9b24522f79d74b8d8f54b152cebcc07
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::Members qw( GetPendingIssues );
10 use C4::Reserves qw( GetReservesFromBorrowernumber CancelReserve );
12 use Koha::Database;
13 use Koha::DateUtils qw( dt_from_string output_pref );
15 my $rs = Koha::Database->new->schema->resultset('Discharge');
17 sub count {
18 my ($params) = @_;
19 my $values = {};
21 if( $params->{borrowernumber} ) {
22 $values->{borrower} = $params->{borrowernumber};
24 if( $params->{pending} ) {
25 $values->{needed} = { '!=', undef };
26 $values->{validated} = undef;
28 elsif( $params->{validated} ) {
29 $values->{validated} = { '!=', undef };
32 return $rs->search( $values )->count;
35 sub can_be_discharged {
36 my ($params) = @_;
37 return unless $params->{borrowernumber};
39 my $issues = GetPendingIssues( $params->{borrowernumber} );
40 if( @$issues ) {
41 return 0;
43 else {
44 return 1;
48 sub is_discharged {
49 my ($params) = @_;
50 return unless $params->{borrowernumber};
51 my $borrowernumber = $params->{borrowernumber};
54 my $restricted = Koha::Patron::Debarments::IsDebarred($borrowernumber);
55 my $validated = get_validated({borrowernumber => $borrowernumber});
57 if ($restricted && $validated) {
58 return 1;
59 } else {
60 return 0;
64 sub request {
65 my ($params) = @_;
66 my $borrowernumber = $params->{borrowernumber};
67 return unless $borrowernumber;
68 return unless can_be_discharged({ borrowernumber => $borrowernumber });
70 return $rs->create({
71 borrower => $borrowernumber,
72 needed => dt_from_string,
73 });
76 sub discharge {
77 my ($params) = @_;
78 my $borrowernumber = $params->{borrowernumber};
79 return unless $borrowernumber and can_be_discharged( { borrowernumber => $borrowernumber } );
81 # Cancel reserves
82 my @reserves = GetReservesFromBorrowernumber($borrowernumber);
83 for my $reserve (@reserves) {
84 CancelReserve( { reserve_id => $reserve->{reserve_id} } );
87 # Debar the member
88 Koha::Patron::Debarments::AddDebarment({
89 borrowernumber => $borrowernumber,
90 type => 'DISCHARGE',
91 });
93 # Generate the discharge
94 my $discharge = $rs->search({ borrower => $borrowernumber }, { order_by => { -desc => 'needed' }, rows => 1 });
95 if( $discharge->count > 0 ) {
96 $discharge->update({ validated => dt_from_string });
98 else {
99 $rs->create({
100 borrower => $borrowernumber,
101 validated => dt_from_string,
106 sub generate_as_pdf {
107 my ($params) = @_;
108 return unless $params->{borrowernumber};
110 my $letter = C4::Letters::GetPreparedLetter(
111 module => 'members',
112 letter_code => 'DISCHARGE',
113 tables => { borrowers => $params->{borrowernumber}, branches => $params->{'branchcode'}, },
116 my $today = output_pref( dt_from_string() );
117 $letter->{'title'} =~ s/<<today>>/$today/g;
118 $letter->{'content'} =~ s/<<today>>/$today/g;
120 my $tmpl = C4::Templates::gettemplate('batch/print-notices.tt', 'intranet', new CGI);
121 $tmpl->param(
122 stylesheet => C4::Context->preference("NoticeCSS"),
123 today => $today,
124 messages => [$letter],
127 my $html_path = tmpnam() . '.html';
128 my $pdf_path = tmpnam() . '.pdf';
129 my $html_content = $tmpl->output;
130 open my $html_fh, '>:encoding(utf8)', $html_path;
131 say $html_fh $html_content;
132 close $html_fh;
133 my $output = eval { require PDF::FromHTML; return; } || $@;
134 if ($output && $params->{testing}) {
135 carp $output;
136 $pdf_path = undef;
138 elsif ($output) {
139 die $output;
141 else {
142 my $pdf = PDF::FromHTML->new( encoding => 'utf-8' );
143 $pdf->load_file( $html_path );
144 $pdf->convert;
145 $pdf->write_file( $pdf_path );
148 return $pdf_path;
151 sub get_pendings {
152 my ($params) = @_;
153 my $branchcode = $params->{branchcode};
154 my $borrowernumber = $params->{borrowernumber};
156 my $cond = {
157 'me.needed' => { '!=', undef },
158 'me.validated' => undef,
159 ( defined $borrowernumber ? ( 'me.borrower' => $borrowernumber ) : () ),
160 ( defined $branchcode ? ( 'borrower.branchcode' => $branchcode ) : () ),
163 my @rs = $rs->search( $cond, { join => 'borrower' } );
164 return \@rs;
167 sub get_validated {
168 my ($params) = @_;
169 my $branchcode = $params->{branchcode};
170 my $borrowernumber = $params->{borrowernumber};
172 my $cond = {
173 'me.validated' => { '!=', undef },
174 ( defined $borrowernumber ? ( 'me.borrower' => $borrowernumber ) : () ),
175 ( defined $branchcode ? ( 'borrower.branchcode' => $branchcode ) : () ),
178 my @rs = $rs->search( $cond, { join => 'borrower' } );
179 return \@rs;