Bug 14224: Check if the columns do not exist before creating them
[koha.git] / Koha / Patron / Discharge.pm
blobd519d40940cfaf7d1a92a82eddd01db3937e336a
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 );
14 use Koha::Patrons;
16 my $rs = Koha::Database->new->schema->resultset('Discharge');
18 sub count {
19 my ($params) = @_;
20 my $values = {};
22 if( $params->{borrowernumber} ) {
23 $values->{borrower} = $params->{borrowernumber};
25 if( $params->{pending} ) {
26 $values->{needed} = { '!=', undef };
27 $values->{validated} = undef;
29 elsif( $params->{validated} ) {
30 $values->{validated} = { '!=', undef };
33 return $rs->search( $values )->count;
36 sub can_be_discharged {
37 my ($params) = @_;
38 return unless $params->{borrowernumber};
40 my $issues = GetPendingIssues( $params->{borrowernumber} );
41 if( @$issues ) {
42 return 0;
44 else {
45 return 1;
49 sub is_discharged {
50 my ($params) = @_;
51 return unless $params->{borrowernumber};
52 my $borrowernumber = $params->{borrowernumber};
54 my $restricted = Koha::Patrons->find( $borrowernumber )->is_debarred;
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;