1 package Koha
::Patron
::Discharge
;
5 use File
::Temp
qw( :POSIX );
8 use C4
::Templates qw
( gettemplate
);
9 use C4
::Members
qw( GetPendingIssues );
12 use Koha
::DateUtils
qw( dt_from_string output_pref );
19 if( $params->{borrowernumber
} ) {
20 $values->{borrower
} = $params->{borrowernumber
};
22 if( $params->{pending
} ) {
23 $values->{needed
} = { '!=', undef };
24 $values->{validated
} = undef;
26 elsif( $params->{validated
} ) {
27 $values->{validated
} = { '!=', undef };
30 return search_limited
( $values )->count;
33 sub can_be_discharged
{
35 return unless $params->{borrowernumber
};
37 my $issues = GetPendingIssues
( $params->{borrowernumber
} );
48 return unless $params->{borrowernumber
};
49 my $borrowernumber = $params->{borrowernumber
};
51 my $restricted = Koha
::Patrons
->find( $borrowernumber )->is_debarred;
52 my @validated = get_validated
({borrowernumber
=> $borrowernumber});
54 if ($restricted && @validated) {
63 my $borrowernumber = $params->{borrowernumber
};
64 return unless $borrowernumber;
65 return unless can_be_discharged
({ borrowernumber
=> $borrowernumber });
67 my $rs = Koha
::Database
->new->schema->resultset('Discharge');
69 borrower
=> $borrowernumber,
70 needed
=> dt_from_string
,
76 my $borrowernumber = $params->{borrowernumber
};
77 return unless $borrowernumber and can_be_discharged
( { borrowernumber
=> $borrowernumber } );
80 my $patron = Koha
::Patrons
->find( $borrowernumber );
81 my $holds = $patron->holds;
82 while ( my $hold = $holds->next ) {
87 Koha
::Patron
::Debarments
::AddDebarment
({
88 borrowernumber
=> $borrowernumber,
92 # Generate the discharge
93 my $rs = Koha
::Database
->new->schema->resultset('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 $patron = Koha
::Patrons
->find( $params->{borrowernumber
} );
111 my $letter = C4
::Letters
::GetPreparedLetter
(
113 letter_code
=> 'DISCHARGE',
114 lang
=> $patron->lang,
115 tables
=> { borrowers
=> $params->{borrowernumber
}, branches
=> $params->{'branchcode'}, },
118 my $today = output_pref
( dt_from_string
() );
119 $letter->{'title'} =~ s/<<today>>/$today/g;
120 $letter->{'content'} =~ s/<<today>>/$today/g;
122 my $tmpl = C4
::Templates
::gettemplate
('batch/print-notices.tt', 'intranet', new CGI
);
124 stylesheet
=> C4
::Context
->preference("NoticeCSS"),
126 messages
=> [$letter],
129 my $html_path = tmpnam
() . '.html';
130 my $pdf_path = tmpnam
() . '.pdf';
131 my $html_content = $tmpl->output;
132 open my $html_fh, '>:encoding(utf8)', $html_path;
133 say $html_fh $html_content;
135 my $output = eval { require PDF
::FromHTML
; return; } || $@
;
136 if ($output && $params->{testing
}) {
144 my $pdf = PDF
::FromHTML
->new( encoding
=> 'utf-8' );
145 $pdf->load_file( $html_path );
147 $pdf->write_file( $pdf_path );
155 my $branchcode = $params->{branchcode
};
156 my $borrowernumber = $params->{borrowernumber
};
159 'me.needed' => { '!=', undef },
160 'me.validated' => undef,
161 ( defined $borrowernumber ?
( 'me.borrower' => $borrowernumber ) : () ),
162 ( defined $branchcode ?
( 'borrower.branchcode' => $branchcode ) : () ),
165 return search_limited
( $cond );
170 my $branchcode = $params->{branchcode
};
171 my $borrowernumber = $params->{borrowernumber
};
174 'me.validated' => { '!=', undef },
175 ( defined $borrowernumber ?
( 'me.borrower' => $borrowernumber ) : () ),
176 ( defined $branchcode ?
( 'borrower.branchcode' => $branchcode ) : () ),
179 return search_limited
( $cond );
182 # TODO This module should be based on Koha::Object[s]
184 my ( $params, $attributes ) = @_;
185 my $userenv = C4
::Context
->userenv;
186 my @restricted_branchcodes;
187 if ( $userenv and $userenv->{number
} ) {
188 my $logged_in_user = Koha
::Patrons
->find( $userenv->{number
} );
189 @restricted_branchcodes = $logged_in_user->libraries_where_can_see_patrons;
191 $params->{'borrower.branchcode'} = { -in => \
@restricted_branchcodes } if @restricted_branchcodes;
192 $attributes->{join} = 'borrower';
194 my $rs = Koha
::Database
->new->schema->resultset('Discharge');
195 return $rs->search( $params, { join => 'borrower' } );