Tamil/Overdue.pm untested

filter: Statementstmt 0.0% 0 / 167 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 271 CC 27 SCAR 41.7
CC
27
Cov
0%
claim
132.0
print
110.0
process
30.0
CRAP 64.5
1 package Koha::Contrib::Tamil::Overdue;
2 $Koha::Contrib::Tamil::Overdue::VERSION = '0.074';
3 use Moose;
4 use Modern::Perl;
5 use YAML qw/ Dump LoadFile /;
6 use DateTime;
7 use List::Util qw/ first /;
8 use Path::Tiny;
9 use Text::Xslate;
10 use C4::Context;
11 use C4::Letters;
12 use C4::Letters;
13  
14  
15 # Is the process effective. If not, output the result.
16 has doit => ( is => 'rw', isa => 'Bool' );
17  
18 # Is the process effective. If not, output the result.
19 has verbose => ( is => 'rw', isa => 'Bool' );
20  
21  
22 has c => (
23 is => 'rw',
24 isa => 'HashRef',
25 default => sub {
26 my $file = $ENV{KOHA_CONF};
27 $file =~ s/koha-conf\.xml/overdue\/config.yaml/;
28 my $c = LoadFile($file);
29 return $c;
30 },
31 );
32  
33 has tx => ( is => 'rw', isa => 'Text::Xslate' );
34  
35 has now => (
36 is => 'rw',
37 isa => 'Str',
38 );
39  
40  
41 sub BUILD {
42 my $self = shift;
43  
44 DateTime->DefaultLocale( $self->c->{date}->{locale} );
45 my $d = DateTime->now()->strftime( $self->c->{date}->{now} );
46 $d =~ s/ / /g;
47 $self->now( $d );
48  
49 $self->tx( Text::Xslate->new(
50 path => $self->c->{dirs}->{template},
51 suffix => '.tx',
52 type => 'text',
53 ) );
54 }
55  
56
57 #
58 # Claim a specific group of issues
59 #
60 sub claim {
61 my ($self, $borrowernumber, $cycle, $items) = @_;
62
63 # On the claim, branch info come from borrower home branch
64 my $dbh = C4::Context->dbh();
65 my $sth = $dbh->prepare("
66 SELECT *
67 FROM borrowers
68 LEFT JOIN branches USING(branchcode)
69 LEFT JOIN categories USING(categorycode)
70 WHERE borrowernumber=?" );
71 $sth->execute($borrowernumber);
72 my $borr = $sth->fetchrow_hashref;
73 $borr->{$_} ||= '' for qw/ firstname surname /;
74 my $has_email = $borr->{email} ? 1 : 0;
75  
76 # Skip issue from borrower of specific category
77 return unless $borr->{overduenoticerequired};
78  
79 my $context = {
80 now => $self->now,
81 borrower => $borr,
82 };
83 if ( my $id = $borr->{guarantorid} ) {
84 $sth = $dbh->prepare("
85 SELECT *
86 FROM borrowers
87 LEFT JOIN branches USING(branchcode)
88 LEFT JOIN categories USING(categorycode)
89 WHERE borrowernumber=?" );
90 $sth->execute($id);
91 if ( my $g = $sth->fetchrow_hashref ) {
92 $g->{$_} ||= '' for qw/ firstname surname /;
93 $context->{guarantor} = $g;
94 }
95 }
96 my $sql = "
97 SELECT items.*,
98 itemtypes.description AS 'type.description',
99 holding.branchname AS 'holding.name',
100 holding.branchaddress1 AS 'holding.address1',
101 holding.branchaddress2 AS 'holding.address2',
102 holding.branchaddress3 AS 'holding.address3',
103 holding.branchzip AS 'holding.zip',
104 holding.branchcity AS 'holding.city',
105 holding.branchstate AS 'holding.state',
106 holding.branchcountry AS 'holding.country',
107 holding.branchphone AS 'holding.phone',
108 holding.branchemail AS 'holding.email',
109 holding.branchurl AS 'holding.url',
110 home.branchname AS 'home.name',
111 home.branchaddress1 AS 'home.address1',
112 home.branchaddress2 AS 'home.address2',
113 home.branchaddress3 AS 'home.address3',
114 home.branchzip AS 'home.zip',
115 home.branchcity AS 'home.city',
116 home.branchstate AS 'home.state',
117 home.branchcountry AS 'home.country',
118 home.branchphone AS 'home.phone',
119 home.branchemail AS 'home.email',
120 home.branchurl AS 'home.url',
121 biblio.author AS 'biblio.author',
122 biblio.title AS 'biblio.title',
123 biblio.unititle AS 'biblio.unititle',
124 biblio.notes AS 'biblio.notes',
125 biblioitems.volume AS 'biblio.volume',
126 biblioitems.number AS 'biblio.number',
127 biblioitems.itemtype AS 'biblio.type',
128 biblioitems.isbn AS 'biblio.isbn',
129 biblioitems.lccn AS 'biblio.lccn',
130 issues.date_due AS 'issue.date_due',
131 issues.issuedate AS 'issue.issuedate',
132 TO_DAYS(NOW())-TO_DAYS(date_due) AS 'issue.overdue_days'
133 FROM issues
134 LEFT JOIN items USING(itemnumber)
135 LEFT JOIN itemtypes ON itemtypes.itemtype = items.itype
136 LEFT JOIN branches AS holding ON holding.branchcode = items.holdingbranch
137 LEFT JOIN branches AS home ON home.branchcode = items.homebranch
138 LEFT JOIN biblio USING(biblionumber)
139 LEFT JOIN biblioitems USING(biblionumber)
140 WHERE issues.itemnumber IN (" . join(',', @$items) . ")";
141 $sth = $dbh->prepare($sql);
142 $sth->execute;
143 my $i = $context->{items} = [];
144 my $strftime = $self->c->{date}->{due};
145 while ( my $item = $sth->fetchrow_hashref ) {
146 for my $name ( ('issue.date_due', 'issue.issuedate') ) {
147 my $d = $item->{$name};
148 $d = DateTime->new(
149 year => substr($d, 0, 4),
150 month => substr($d, 5, 2),
151 day => substr($d, 8, 2) );
152 $item->{$name} = $d->strftime($strftime);
153 }
154 push @$i, $item;
155 }
156  
157 for my $claim ( @{$cycle->{claims}} ) {
158 next if $claim->{type} eq 'email' && !$has_email;
159 $context->{title} = $cycle->{title};
160 my $template = $claim->{template};
161 #say "CONTEXT", Dump($context);
162 $self->tx->{type} = $has_email ? 'text' : 'html';
163 my $content = $self->tx->render($template , $context);
164 $content =~ s/'/'/g; #FIXME: why?
165 my $letter = {
166 title => $cycle->{title},
167 content => $content,
168 'content-type' => $has_email ? 'text/plain; charset="UTF-8"' : 'text/html; charset="UTF-8"',
169 };
170 if ( $self->verbose ) {
171 say $letter->{title}, ": borrower #", $borr->{borrowernumber}, " ",
172 $borr->{surname}, " ", $borr->{firstname};
173 }
174 if ( $self->doit ) {
175 C4::Letters::EnqueueLetter( {
176 letter => $letter,
177 borrowernumber => $borrowernumber,
178 message_transport_type => $claim->{type},
179 to_address => $borr->{email} || '',
180 from_address => $borr->{branchemail},
181 } );
182 }
183 elsif ( $self->verbose ) {
184 say '-' x 72, "\n", $content, "\n";
185 }
186 last;
187 }
188
189 }
190  
191  
192 sub handle_borrower {
193 my ($self, $borrower) = @_;
194 return unless $borrower->{borrowernumber};
195  
196 while ( my ($icycle, $items) = each %{$borrower->{cycles}} ) {
197 $self->claim(
198 $borrower->{borrowernumber},
199 $self->c->{cycles}->[$icycle],
200 $items
201 );
202 }
203 }
204
205  
206 my @fnames = qw/
207 day
208 borrower.category
209 borrower.branch
210 item.home
211 item.holding
212 item.type
213 item.ccode
214 biblio.type
215 /;
216  
217  
218 sub process {
219 my $self = shift;
220 my $dbh = C4::Context->dbh();
221 my $sth = $dbh->prepare("
222 SELECT borrowers.borrowernumber,
223 issues.itemnumber,
224 TO_DAYS(NOW())-TO_DAYS(date_due) AS day,
225 borrowers.branchcode AS 'borrower.branch',
226 borrowers.categorycode AS 'borrower.category',
227 homebranch AS 'item.home',
228 holdingbranch AS 'item.holding',
229 itype AS 'item.type',
230 items.ccode AS 'item.ccode',
231 itemtype AS 'biblio.type'
232 FROM issues
233 LEFT JOIN borrowers USING(borrowernumber)
234 LEFT JOIN items USING(itemnumber)
235 LEFT JOIN biblioitems ON biblioitems.biblionumber = items.biblionumber
236 WHERE date_due < NOW()
237 ORDER BY surname, firstname
238 ");
239 $sth->execute;
240 my $borrower = { borrowernumber => 0 };
241 my @cycles = @{$self->c->{cycles}};
242 while ( my $issue = $sth->fetchrow_hashref ) {
243 my $icycle = 0;
244 my $match;
245 while ( $icycle < @cycles ) {
246 my $cycle = $cycles[$icycle];
247 my $criteria = $cycle->{criteria};
248 my $code = $criteria;
249 $code =~ s/$_/\$issue->{'$_'}/g for @fnames;
250 $code = "\$match = $code";
251 eval($code);
252 if ( $@ ) {
253 say "Wrong citeria:\n $criteria\n $code";
254 warn();
255 exit;
256 }
257 last if $match;
258 $icycle++;
259 }
260 next unless $match;
261 if ( $issue->{borrowernumber} != $borrower->{borrowernumber} ) {
262 $self->handle_borrower($borrower);
263 $borrower = { borrowernumber => $issue->{borrowernumber}, cycles => {} };
264 }
265 my $cycles = $borrower->{cycles};
266 my $items = $cycles->{$icycle} ||= [];
267 push @$items, $issue->{itemnumber};
268 }
269 $self->handle_borrower($borrower);
270 }
271  
272  
273 sub clear {
274 my $sql =
275 "DELETE FROM message_queue
276 WHERE status = 'pending'
277 AND message_transport_type IN ('email','print')
278 AND time_queued >= DATE_SUB(now(), interval 1 HOUR) ";
279 C4::Context->dbh->do($sql)
280 }
281  
282  
283 sub print {
284 my $self = shift;
285  
286 my $messages = C4::Letters::_get_unsent_messages( { message_transport_type => 'print' } );
287 my %msg_per_branch;
288 for my $message ( @$messages ) {
289 my $m = $msg_per_branch{$message->{branchcode}} ||= [];
290 push @$m, $message;
291 }
292 $messages = undef;
293  
294 my $dir = $self->c->{dirs}->{print};
295 say "Print in directory $dir" if $self->verbose;
296 chdir $dir;
297 my $now = DateTime->now();
298 mkdir $now->year unless -d $now->year;
299 chdir $now->year;
300 $now = $now->ymd;
301 while ( my ($branch, $messages) = each %msg_per_branch ) {
302 if ($self->doit) {
303 mkdir $branch unless -d $branch;
304 chdir $branch;
305 }
306 my $file = "$now-$branch.html";
307 say "Create file $branch/$file" if $self->verbose;
308 if ( $self->doit ) {
309 my $fh = IO::File->new($file, ">:encoding(utf8)")
310 or die "Enable to create $file";
311 print $fh $_->{content} for @$messages;
312 }
313 chdir ".." if $self->doit;
314 }
315  
316 return unless $self->doit;
317 say "Set all 'print' messages from 'pending' to 'sent'" if $self->doit && $self->verbose;
318 C4::Context->dbh->do(
319 "UPDATE message_queue SET status='sent' WHERE message_transport_type='print'");
320 }
321  
322  
323 sub email {
324 my $self = shift;
325 return unless $self->doit;
326 say "Send 'email' messages" if $self->verbose;
327 C4::Letters::SendQueuedMessages({ verbose => $self->verbose });
328 }
329  
330  
331 1;
332  
333 __END__