1 package Koha
::Patron
::Discharge
;
5 use File
::Temp
qw( :POSIX );
8 use C4
::Templates qw
( gettemplate
);
11 use Koha
::DateUtils
qw( dt_from_string output_pref );
18 if( $params->{borrowernumber
} ) {
19 $values->{borrower
} = $params->{borrowernumber
};
21 if( $params->{pending
} ) {
22 $values->{needed
} = { '!=', undef };
23 $values->{validated
} = undef;
25 elsif( $params->{validated
} ) {
26 $values->{validated
} = { '!=', undef };
29 return search_limited
( $values )->count;
32 sub can_be_discharged
{
34 return unless $params->{borrowernumber
};
36 my $patron = Koha
::Patrons
->find( $params->{borrowernumber
} );
37 return unless $patron;
39 my $has_pending_checkouts = $patron->pending_checkouts->count;
40 return $has_pending_checkouts ?
0 : 1;
45 return unless $params->{borrowernumber
};
46 my $borrowernumber = $params->{borrowernumber
};
48 my $restricted = Koha
::Patrons
->find( $borrowernumber )->is_debarred;
49 my @validated = get_validated
({borrowernumber
=> $borrowernumber});
51 if ($restricted && @validated) {
60 my $borrowernumber = $params->{borrowernumber
};
61 return unless $borrowernumber;
62 return unless can_be_discharged
({ borrowernumber
=> $borrowernumber });
64 my $rs = Koha
::Database
->new->schema->resultset('Discharge');
66 borrower
=> $borrowernumber,
67 needed
=> dt_from_string
,
73 my $borrowernumber = $params->{borrowernumber
};
74 return unless $borrowernumber and can_be_discharged
( { borrowernumber
=> $borrowernumber } );
77 my $patron = Koha
::Patrons
->find( $borrowernumber );
78 my $holds = $patron->holds;
79 while ( my $hold = $holds->next ) {
84 Koha
::Patron
::Debarments
::AddDebarment
({
85 borrowernumber
=> $borrowernumber,
89 # Generate the discharge
90 my $rs = Koha
::Database
->new->schema->resultset('Discharge');
91 my $discharge = $rs->search({ borrower
=> $borrowernumber }, { order_by
=> { -desc
=> 'needed' }, rows
=> 1 });
92 if( $discharge->count > 0 ) {
93 $discharge->update({ validated
=> dt_from_string
});
97 borrower
=> $borrowernumber,
98 validated
=> dt_from_string
,
103 sub generate_as_pdf
{
105 return unless $params->{borrowernumber
};
107 my $patron = Koha
::Patrons
->find( $params->{borrowernumber
} );
108 my $letter = C4
::Letters
::GetPreparedLetter
(
110 letter_code
=> 'DISCHARGE',
111 lang
=> $patron->lang,
112 tables
=> { borrowers
=> $params->{borrowernumber
}, branches
=> $params->{'branchcode'}, },
115 my $today = output_pref
( dt_from_string
() );
116 $letter->{'title'} =~ s/<<today>>/$today/g;
117 $letter->{'content'} =~ s/<<today>>/$today/g;
119 my $tmpl = C4
::Templates
::gettemplate
('batch/print-notices.tt', 'intranet', new CGI
);
121 stylesheet
=> C4
::Context
->preference("NoticeCSS"),
123 messages
=> [$letter],
126 my $html_path = tmpnam
() . '.html';
127 my $pdf_path = tmpnam
() . '.pdf';
128 my $html_content = $tmpl->output;
129 open my $html_fh, '>:encoding(utf8)', $html_path;
130 say $html_fh $html_content;
132 my $output = eval { require PDF
::FromHTML
; return; } || $@
;
133 if ($output && $params->{testing
}) {
141 my $pdf = PDF
::FromHTML
->new( encoding
=> 'utf-8' );
142 $pdf->load_file( $html_path );
144 $pdf->write_file( $pdf_path );
152 my $branchcode = $params->{branchcode
};
153 my $borrowernumber = $params->{borrowernumber
};
156 'me.needed' => { '!=', undef },
157 'me.validated' => undef,
158 ( defined $borrowernumber ?
( 'me.borrower' => $borrowernumber ) : () ),
159 ( defined $branchcode ?
( 'borrower.branchcode' => $branchcode ) : () ),
162 return search_limited
( $cond );
167 my $branchcode = $params->{branchcode
};
168 my $borrowernumber = $params->{borrowernumber
};
171 'me.validated' => { '!=', undef },
172 ( defined $borrowernumber ?
( 'me.borrower' => $borrowernumber ) : () ),
173 ( defined $branchcode ?
( 'borrower.branchcode' => $branchcode ) : () ),
176 return search_limited
( $cond );
179 # TODO This module should be based on Koha::Object[s]
181 my ( $params, $attributes ) = @_;
182 my $userenv = C4
::Context
->userenv;
183 my @restricted_branchcodes;
184 if ( $userenv and $userenv->{number
} ) {
185 my $logged_in_user = Koha
::Patrons
->find( $userenv->{number
} );
186 @restricted_branchcodes = $logged_in_user->libraries_where_can_see_patrons;
188 $params->{'borrower.branchcode'} = { -in => \
@restricted_branchcodes } if @restricted_branchcodes;
189 $attributes->{join} = 'borrower';
191 my $rs = Koha
::Database
->new->schema->resultset('Discharge');
192 return $rs->search( $params, { join => 'borrower' } );