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 $pdf->write_file( $pdf_path );
154 my $branchcode = $params->{branchcode
};
155 my $borrowernumber = $params->{borrowernumber
};
158 'me.needed' => { '!=', undef },
159 'me.validated' => undef,
160 ( defined $borrowernumber ?
( 'me.borrower' => $borrowernumber ) : () ),
161 ( defined $branchcode ?
( 'borrower.branchcode' => $branchcode ) : () ),
164 return search_limited
( $cond );
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 return search_limited
( $cond );
181 # TODO This module should be based on Koha::Object[s]
183 my ( $params, $attributes ) = @_;
184 my $userenv = C4
::Context
->userenv;
185 my @restricted_branchcodes;
186 if ( $userenv and $userenv->{number
} ) {
187 my $logged_in_user = Koha
::Patrons
->find( $userenv->{number
} );
188 @restricted_branchcodes = $logged_in_user->libraries_where_can_see_patrons;
190 $params->{'borrower.branchcode'} = { -in => \
@restricted_branchcodes } if @restricted_branchcodes;
191 $attributes->{join} = 'borrower';
193 my $rs = Koha
::Database
->new->schema->resultset('Discharge');
194 return $rs->search( $params, { join => 'borrower' } );