added ttf fonts
[share-koha-fer] / lib / Koha / Patron / Discharge.pm
1 package Koha::Patron::Discharge;
2
3 use Modern::Perl;
4 use CGI;
5 use File::Temp qw( :POSIX );
6 use Carp;
7
8 use C4::Templates qw ( gettemplate );
9 use C4::Letters qw ( GetPreparedLetter );
10
11 use Koha::Database;
12 use Koha::DateUtils qw( dt_from_string output_pref );
13 use Koha::Patrons;
14 use Koha::Patron::Debarments;
15
16 sub count {
17     my ($params) = @_;
18     my $values = {};
19
20     if( $params->{borrowernumber} ) {
21         $values->{borrower} = $params->{borrowernumber};
22     }
23     if( $params->{pending} ) {
24         $values->{needed} = { '!=', undef };
25         $values->{validated} = undef;
26     }
27     elsif( $params->{validated} ) {
28         $values->{validated} = { '!=', undef };
29     }
30
31     return search_limited( $values )->count;
32 }
33
34 sub can_be_discharged {
35     my ($params) = @_;
36     return unless $params->{borrowernumber};
37
38     my $patron = Koha::Patrons->find( $params->{borrowernumber} );
39     return unless $patron;
40
41     my $has_pending_checkouts = $patron->checkouts->count;
42     return $has_pending_checkouts ? 0 : 1;
43 }
44
45 sub is_discharged {
46     my ($params) = @_;
47     return unless $params->{borrowernumber};
48     my $borrowernumber = $params->{borrowernumber};
49
50     my $restricted = Koha::Patrons->find( $borrowernumber )->is_debarred;
51     my @validated = get_validated({borrowernumber => $borrowernumber});
52
53     if ($restricted && @validated) {
54         return 1;
55     } else {
56         return 0;
57     }
58 }
59
60 sub request {
61     my ($params) = @_;
62     my $borrowernumber = $params->{borrowernumber};
63     return unless $borrowernumber;
64     return unless can_be_discharged({ borrowernumber => $borrowernumber });
65
66     my $rs = Koha::Database->new->schema->resultset('Discharge');
67     return $rs->create({
68         borrower => $borrowernumber,
69         needed   => dt_from_string,
70     });
71 }
72
73 sub discharge {
74     my ($params) = @_;
75     my $borrowernumber = $params->{borrowernumber};
76     return unless $borrowernumber and can_be_discharged( { borrowernumber => $borrowernumber } );
77
78     # Cancel reserves
79     my $patron = Koha::Patrons->find( $borrowernumber );
80     my $holds = $patron->holds;
81     while ( my $hold = $holds->next ) {
82         $hold->cancel;
83     }
84
85     # Debar the member
86     Koha::Patron::Debarments::AddDebarment({
87         borrowernumber => $borrowernumber,
88         type           => 'DISCHARGE',
89     });
90
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 });
96     }
97     else {
98         $rs->create({
99             borrower  => $borrowernumber,
100             validated => dt_from_string,
101         });
102     }
103 }
104
105 sub generate_as_pdf {
106     my ($params) = @_;
107     return unless $params->{borrowernumber};
108
109     my $patron = Koha::Patrons->find( $params->{borrowernumber} );
110     my $letter = C4::Letters::GetPreparedLetter(
111         module      => 'members',
112         letter_code => 'DISCHARGE',
113         lang        => $patron->lang,
114         tables      => { borrowers => $params->{borrowernumber}, branches => $params->{'branchcode'}, },
115     );
116
117     my $today = output_pref( dt_from_string() );
118     $letter->{'title'}   =~ s/<<today>>/$today/g;
119     $letter->{'content'} =~ s/<<today>>/$today/g;
120
121     my $tmpl = C4::Templates::gettemplate('batch/print-notices.tt', 'intranet', new CGI);
122     $tmpl->param(
123         stylesheet => C4::Context->preference("NoticeCSS"),
124         today      => $today,
125         messages   => [$letter],
126     );
127
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;
133     close $html_fh;
134     my $output = eval { require PDF::FromHTML; return; } || $@;
135     if ($output && $params->{testing}) {
136         carp $output;
137         $pdf_path = undef;
138     }
139     elsif ($output) {
140         die $output;
141     }
142     else {
143         my $pdf = PDF::FromHTML->new( encoding => 'utf-8' );
144         $pdf->load_file( $html_path );
145
146                 my $ttf = C4::Context->config('ttf');
147                 if ( $ttf  && exists $ttf->{font} ) {
148
149                         use Data::Dump qw(dump);
150 warn "XXX ttf = ",dump($ttf);
151
152                         my $type2path;
153                         foreach my $font ( @{ $ttf->{font} } ) {
154                                         $type2path->{ $font->{type} } = $font->{content};
155                         }
156
157                         warn "# type2path = ",dump($type2path);
158
159
160                 $pdf->convert(
161 #                               PageWidth         640
162 #                               PageResolution    540
163 #                               FontBold          'HelveticaBold'
164 #                               FontOblique       'HelveticaOblique'
165 #                               FontBoldOblique   'HelveticaBoldOblique'
166 #                               LineHeight        12
167 #                               FontUnicode       'Helvetica'
168 #                               Font              (same as FontUnicode)
169 #                               PageSize          'A4'
170 #                               Landscape         0
171
172                                 FontBold          => $type2path->{'HB'} || 'HelveticaBold',
173                                 FontOblique       => $type2path->{'HO'} || 'HelveticaOblique',
174                                 FontBoldOblique   => $type2path->{'HBO'}|| 'HelveticaBoldOblique',
175                                 FontUnicode       => $type2path->{'H'}  || 'Helvetica',
176                                 Font              => $type2path->{'H'}  || 'Helvetica',
177                         );
178                 }
179         $pdf->write_file( $pdf_path );
180     }
181
182     return $pdf_path;
183 }
184
185 sub get_pendings {
186     my ($params)       = @_;
187     my $branchcode     = $params->{branchcode};
188     my $borrowernumber = $params->{borrowernumber};
189
190     my $cond = {
191         'me.needed'    => { '!=', undef },
192         'me.validated' => undef,
193         ( defined $borrowernumber ? ( 'me.borrower' => $borrowernumber ) : () ),
194         ( defined $branchcode ? ( 'borrower.branchcode' => $branchcode ) : () ),
195     };
196
197     return search_limited( $cond );
198 }
199
200 sub get_validated {
201     my ($params)       = @_;
202     my $branchcode     = $params->{branchcode};
203     my $borrowernumber = $params->{borrowernumber};
204
205     my $cond = {
206         'me.validated' => { '!=', undef },
207         ( defined $borrowernumber ? ( 'me.borrower' => $borrowernumber ) : () ),
208         ( defined $branchcode ? ( 'borrower.branchcode' => $branchcode ) : () ),
209     };
210
211     return search_limited( $cond );
212 }
213
214 # TODO This module should be based on Koha::Object[s]
215 sub search_limited {
216     my ( $params, $attributes ) = @_;
217     my $userenv = C4::Context->userenv;
218     my @restricted_branchcodes;
219     if ( $userenv and $userenv->{number} ) {
220         my $logged_in_user = Koha::Patrons->find( $userenv->{number} );
221         @restricted_branchcodes = $logged_in_user->libraries_where_can_see_patrons;
222     }
223     $params->{'borrower.branchcode'} = { -in => \@restricted_branchcodes } if @restricted_branchcodes;
224     $attributes->{join} = 'borrower';
225
226     my $rs = Koha::Database->new->schema->resultset('Discharge');
227     return $rs->search( $params, { join => 'borrower' } );
228 }
229
230 1;