1 package Koha
::Patron
::Discharge
;
5 use File
::Temp
qw( :POSIX );
8 use C4
::Templates qw
( gettemplate
);
9 use C4
::Members
qw( GetPendingIssues );
10 use C4
::Reserves
qw( GetReservesFromBorrowernumber CancelReserve );
13 use Koha
::DateUtils
qw( dt_from_string output_pref );
15 my $rs = Koha
::Database
->new->schema->resultset('Discharge');
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
{
37 return unless $params->{borrowernumber
};
39 my $issues = GetPendingIssues
( $params->{borrowernumber
} );
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) {
66 my $borrowernumber = $params->{borrowernumber
};
67 return unless $borrowernumber;
68 return unless can_be_discharged
({ borrowernumber
=> $borrowernumber });
71 borrower
=> $borrowernumber,
72 needed
=> dt_from_string
,
78 my $borrowernumber = $params->{borrowernumber
};
79 return unless $borrowernumber and can_be_discharged
( { borrowernumber
=> $borrowernumber } );
82 my @reserves = GetReservesFromBorrowernumber
($borrowernumber);
83 for my $reserve (@reserves) {
84 CancelReserve
( { reserve_id
=> $reserve->{reserve_id
} } );
88 Koha
::Patron
::Debarments
::AddDebarment
({
89 borrowernumber
=> $borrowernumber,
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
});
100 borrower
=> $borrowernumber,
101 validated
=> dt_from_string
,
106 sub generate_as_pdf
{
108 return unless $params->{borrowernumber
};
110 my $letter = C4
::Letters
::GetPreparedLetter
(
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
);
122 stylesheet
=> C4
::Context
->preference("NoticeCSS"),
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;
133 my $output = eval { require PDF
::FromHTML
; return; } || $@
;
134 if ($output && $params->{testing
}) {
142 my $pdf = PDF
::FromHTML
->new( encoding
=> 'utf-8' );
143 $pdf->load_file( $html_path );
145 $pdf->write_file( $pdf_path );
153 my $branchcode = $params->{branchcode
};
154 my $borrowernumber = $params->{borrowernumber
};
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' } );
169 my $branchcode = $params->{branchcode
};
170 my $borrowernumber = $params->{borrowernumber
};
173 'me.validated' => { '!=', undef },
174 ( defined $borrowernumber ?
( 'me.borrower' => $borrowernumber ) : () ),
175 ( defined $branchcode ?
( 'borrower.branchcode' => $branchcode ) : () ),
178 my @rs = $rs->search( $cond, { join => 'borrower' } );