Line 0
Link Here
|
|
|
1 |
package C4::ClubsAndServices; |
2 |
|
3 |
# $Id: ClubsAndServices.pm,v 0.1 2007/04/10 kylemhall |
4 |
|
5 |
# This package is intended for dealing with clubs and services |
6 |
# and enrollments in such, such as summer reading clubs, and |
7 |
# library newsletters |
8 |
|
9 |
# Copyright 2007 Kyle Hall |
10 |
# |
11 |
# This file is part of Koha. |
12 |
# |
13 |
# Koha is free software; you can redistribute it and/or modify it under the |
14 |
# terms of the GNU General Public License as published by the Free Software |
15 |
# Foundation; either version 2 of the License, or (at your option) any later |
16 |
# version. |
17 |
# |
18 |
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
19 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
20 |
# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
21 |
# |
22 |
# You should have received a copy of the GNU General Public License along with |
23 |
# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place, |
24 |
# Suite 330, Boston, MA 02111-1307 USA |
25 |
|
26 |
use strict; |
27 |
|
28 |
require Exporter; |
29 |
|
30 |
use C4::Context; |
31 |
|
32 |
use DBI; |
33 |
|
34 |
use vars qw($VERSION @ISA @EXPORT); |
35 |
|
36 |
# set the version for version checking |
37 |
$VERSION = 0.01; |
38 |
|
39 |
=head1 NAME |
40 |
|
41 |
C4::ClubsAndServices - Functions for managing clubs and services |
42 |
|
43 |
=head1 FUNCTIONS |
44 |
|
45 |
=over 2 |
46 |
|
47 |
=cut |
48 |
|
49 |
@ISA = qw( Exporter ); |
50 |
@EXPORT = qw( |
51 |
AddClubOrServiceArchetype |
52 |
UpdateClubOrServiceArchetype |
53 |
DeleteClubOrServiceArchetype |
54 |
|
55 |
AddClubOrService |
56 |
UpdateClubOrService |
57 |
DeleteClubOrService |
58 |
|
59 |
EnrollInClubOrService |
60 |
GetEnrollments |
61 |
GetClubsAndServices |
62 |
GetClubOrService |
63 |
GetClubsAndServicesArchetypes |
64 |
GetClubOrServiceArchetype |
65 |
DoesEnrollmentRequireData |
66 |
CancelClubOrServiceEnrollment |
67 |
GetEnrolledClubsAndServices |
68 |
GetPubliclyEnrollableClubsAndServices |
69 |
GetAllEnrollableClubsAndServices |
70 |
GetCasEnrollments |
71 |
|
72 |
ReserveForBestSellersClub |
73 |
|
74 |
getTodayMysqlDateFormat |
75 |
); |
76 |
|
77 |
=head2 AddClubOrServiceArchetype |
78 |
|
79 |
Creates a new archetype for a club or service |
80 |
An archetype is something after which other things a patterned, |
81 |
For example, you could create a 'Summer Reading Club' club archtype |
82 |
which is then used to create an individual 'Summer Reading Club' |
83 |
*for each library* in your system. |
84 |
|
85 |
Input: |
86 |
$type : 'club' or 'service', could be extended to add more types |
87 |
$title: short description of the club or service |
88 |
$description: long description of the club or service |
89 |
$publicEnrollment: If true, any borrower should be able |
90 |
to enroll in club or service from opac. If false, |
91 |
Only a librarian should be able to enroll a borrower |
92 |
in the club or service. |
93 |
$casData1Title: explanation of what is stored in |
94 |
clubsAndServices.casData1Title |
95 |
$casData2Title: same but for casData2Title |
96 |
$casData3Title: same but for casData3Title |
97 |
$caseData1Title: explanation of what is stored in |
98 |
clubsAndServicesEnrollment.data1 |
99 |
$caseData2Title: Same but for data2 |
100 |
$caseData3Title: Same but for data3 |
101 |
$casData1Desc: Long explanation of what is stored in |
102 |
clubsAndServices.casData1Title |
103 |
$casData2Desc: same but for casData2Title |
104 |
$casData3Desc: same but for casData3Title |
105 |
$caseData1Desc: Long explanation of what is stored in |
106 |
clubsAndServicesEnrollment.data1 |
107 |
$caseData2Desc: Same but for data2 |
108 |
$caseData3Desc: Same but for data3 |
109 |
$caseRequireEmail: If 1, enrollment in clubs or services based on this archetype will require a valid e-mail address field in the borrower |
110 |
record as specified in the syspref AutoEmailPrimaryAddress |
111 |
$branchcode: The branchcode for the branch where this Archetype was created |
112 |
|
113 |
Output: |
114 |
$success: 1 if all database operations were successful, 0 otherwise |
115 |
$errorCode: Code for reason of failure, good for translating errors in templates |
116 |
$errorMessage: English description of error |
117 |
|
118 |
=cut |
119 |
|
120 |
sub AddClubOrServiceArchetype { |
121 |
my ( $type, $title, $description, $publicEnrollment, |
122 |
$casData1Title, $casData2Title, $casData3Title, |
123 |
$caseData1Title, $caseData2Title, $caseData3Title, |
124 |
$casData1Desc, $casData2Desc, $casData3Desc, |
125 |
$caseData1Desc, $caseData2Desc, $caseData3Desc, |
126 |
$caseRequireEmail, $branchcode ) = @_; |
127 |
|
128 |
## Check for all neccessary parameters |
129 |
if ( ! $type ) { |
130 |
return ( 0, 1, "No Type Given" ); |
131 |
} |
132 |
if ( ! $title ) { |
133 |
return ( 0, 2, "No Title Given" ); |
134 |
} |
135 |
if ( ! $description ) { |
136 |
return ( 0, 3, "No Description Given" ); |
137 |
} |
138 |
|
139 |
my $success = 1; |
140 |
|
141 |
my $dbh = C4::Context->dbh; |
142 |
|
143 |
my $sth; |
144 |
$sth = $dbh->prepare("INSERT INTO clubsAndServicesArchetypes ( casaId, type, title, description, publicEnrollment, caseRequireEmail, branchcode, last_updated ) |
145 |
VALUES ( NULL, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP)"); |
146 |
$sth->execute( $type, $title, $description, $publicEnrollment, $caseRequireEmail, $branchcode ) or $success = 0; |
147 |
my $casaId = $dbh->{'mysql_insertid'}; |
148 |
$sth->finish; |
149 |
|
150 |
if ( $casData1Title ) { |
151 |
$sth = $dbh->prepare("UPDATE clubsAndServicesArchetypes SET casData1Title = ? WHERE casaId = ?"); |
152 |
$sth->execute( $casData1Title, $casaId ) or $success = 0; |
153 |
$sth->finish; |
154 |
} |
155 |
if ( $casData2Title ) { |
156 |
$sth = $dbh->prepare("UPDATE clubsAndServicesArchetypes SET casData2Title = ? WHERE casaId = ?"); |
157 |
$sth->execute( $casData2Title, $casaId ) or $success = 0; |
158 |
$sth->finish; |
159 |
} |
160 |
if ( $casData3Title ) { |
161 |
$sth = $dbh->prepare("UPDATE clubsAndServicesArchetypes SET casData3Title = ? WHERE casaId = ?"); |
162 |
$sth->execute( $casData3Title, $casaId ) or $success = 0; |
163 |
$sth->finish; |
164 |
} |
165 |
|
166 |
|
167 |
if ( $caseData1Title ) { |
168 |
$sth = $dbh->prepare("UPDATE clubsAndServicesArchetypes SET caseData1Title = ? WHERE casaId = ?"); |
169 |
$sth->execute( $caseData1Title, $casaId ) or $success = 0; |
170 |
$sth->finish; |
171 |
} |
172 |
if ( $caseData2Title ) { |
173 |
$sth = $dbh->prepare("UPDATE clubsAndServicesArchetypes SET caseData2Title = ? WHERE casaId = ?"); |
174 |
$sth->execute( $caseData2Title, $casaId ) or $success = 0; |
175 |
$sth->finish; |
176 |
} |
177 |
if ( $caseData3Title ) { |
178 |
$sth = $dbh->prepare("UPDATE clubsAndServicesArchetypes SET caseData3Title = ? WHERE casaId = ?"); |
179 |
$sth->execute( $caseData3Title, $casaId ) or $success = 0; |
180 |
$sth->finish; |
181 |
} |
182 |
|
183 |
if ( $casData1Desc ) { |
184 |
$sth = $dbh->prepare("UPDATE clubsAndServicesArchetypes SET casData1Desc = ? WHERE casaId = ?"); |
185 |
$sth->execute( $casData1Desc, $casaId ) or $success = 0; |
186 |
$sth->finish; |
187 |
} |
188 |
if ( $casData2Desc ) { |
189 |
$sth = $dbh->prepare("UPDATE clubsAndServicesArchetypes SET casData2Desc = ? WHERE casaId = ?"); |
190 |
$sth->execute( $casData2Desc, $casaId ) or $success = 0; |
191 |
$sth->finish; |
192 |
} |
193 |
if ( $casData3Desc ) { |
194 |
$sth = $dbh->prepare("UPDATE clubsAndServicesArchetypes SET casData3Desc = ? WHERE casaId = ?"); |
195 |
$sth->execute( $casData3Desc, $casaId ) or $success = 0; |
196 |
$sth->finish; |
197 |
} |
198 |
|
199 |
if ( $caseData1Desc ) { |
200 |
$sth = $dbh->prepare("UPDATE clubsAndServicesArchetypes SET caseData1Desc = ? WHERE casaId = ?"); |
201 |
$sth->execute( $caseData1Desc, $casaId ) or $success = 0; |
202 |
$sth->finish; |
203 |
} |
204 |
if ( $caseData2Desc ) { |
205 |
$sth = $dbh->prepare("UPDATE clubsAndServicesArchetypes SET caseData2Desc = ? WHERE casaId = ?"); |
206 |
$sth->execute( $caseData2Desc, $casaId ) or $success = 0; |
207 |
$sth->finish; |
208 |
} |
209 |
if ( $caseData3Desc ) { |
210 |
$sth = $dbh->prepare("UPDATE clubsAndServicesArchetypes SET caseData3Desc = ? WHERE casaId = ?"); |
211 |
$sth->execute( $caseData3Desc, $casaId ) or $success = 0; |
212 |
$sth->finish; |
213 |
} |
214 |
|
215 |
my ( $errorCode, $errorMessage ); |
216 |
if ( ! $success ) { |
217 |
$errorMessage = "Database Failure"; |
218 |
$errorCode = 4; |
219 |
} |
220 |
|
221 |
return( $success, $errorCode, $errorMessage ); |
222 |
|
223 |
} |
224 |
|
225 |
=head2 UpdateClubOrServiceArchetype |
226 |
|
227 |
Updates an archetype for a club or service |
228 |
|
229 |
Input: |
230 |
$casaId: id of the archetype to be updated |
231 |
$type : 'club' or 'service', could be extended to add more types |
232 |
$title: short description of the club or service |
233 |
$description: long description of the club or service |
234 |
$publicEnrollment: If true, any borrower should be able |
235 |
to enroll in club or service from opac. If false, |
236 |
Only a librarian should be able to enroll a borrower |
237 |
in the club or service. |
238 |
$casData1Title: explanation of what is stored in |
239 |
clubsAndServices.casData1Title |
240 |
$casData2Title: same but for casData2Title |
241 |
$casData3Title: same but for casData3Title |
242 |
$caseData1Title: explanation of what is stored in |
243 |
clubsAndServicesEnrollment.data1 |
244 |
$caseData2Title: Same but for data2 |
245 |
$caseData3Title: Same but for data3 |
246 |
$casData1Desc: Long explanation of what is stored in |
247 |
clubsAndServices.casData1Title |
248 |
$casData2Desc: same but for casData2Title |
249 |
$casData3Desc: same but for casData3Title |
250 |
$caseData1Desc: Long explanation of what is stored in |
251 |
clubsAndServicesEnrollment.data1 |
252 |
$caseData2Desc: Same but for data2 |
253 |
$caseData3Desc: Same but for data3 |
254 |
$caseRequireEmail: If 1, enrollment in clubs or services based on this archetype will require a valid e-mail address field in the borrower |
255 |
record as specified in the syspref AutoEmailPrimaryAddress |
256 |
|
257 |
Output: |
258 |
$success: 1 if all database operations were successful, 0 otherwise |
259 |
$errorCode: Code for reason of failure, good for translating errors in templates |
260 |
$errorMessage: English description of error |
261 |
|
262 |
=cut |
263 |
|
264 |
sub UpdateClubOrServiceArchetype { |
265 |
my ( $casaId, $type, $title, $description, $publicEnrollment, |
266 |
$casData1Title, $casData2Title, $casData3Title, |
267 |
$caseData1Title, $caseData2Title, $caseData3Title, |
268 |
$casData1Desc, $casData2Desc, $casData3Desc, |
269 |
$caseData1Desc, $caseData2Desc, $caseData3Desc, |
270 |
$caseRequireEmail, |
271 |
) = @_; |
272 |
|
273 |
## Check for all neccessary parameters |
274 |
if ( ! $casaId ) { |
275 |
return ( 0, 1, "No Id Given" ); |
276 |
} |
277 |
if ( ! $type ) { |
278 |
return ( 0, 2, "No Type Given" ); |
279 |
} |
280 |
if ( ! $title ) { |
281 |
return ( 0, 3, "No Title Given" ); |
282 |
} |
283 |
if ( ! $description ) { |
284 |
return ( 0, 4, "No Description Given" ); |
285 |
} |
286 |
|
287 |
my $success = 1; |
288 |
|
289 |
my $dbh = C4::Context->dbh; |
290 |
|
291 |
my $sth; |
292 |
$sth = $dbh->prepare("UPDATE clubsAndServicesArchetypes |
293 |
SET |
294 |
type = ?, title = ?, description = ?, publicEnrollment = ?, |
295 |
casData1Title = ?, casData2Title = ?, casData3Title = ?, |
296 |
caseData1Title = ?, caseData2Title = ?, caseData3Title = ?, |
297 |
casData1Desc = ?, casData2Desc = ?, casData3Desc = ?, |
298 |
caseData1Desc = ?, caseData2Desc = ?, caseData3Desc = ?, caseRequireEmail = ?, |
299 |
last_updated = NOW() WHERE casaId = ?"); |
300 |
|
301 |
$sth->execute( $type, $title, $description, $publicEnrollment, |
302 |
$casData1Title, $casData2Title, $casData3Title, |
303 |
$caseData1Title, $caseData2Title, $caseData3Title, |
304 |
$casData1Desc, $casData2Desc, $casData3Desc, |
305 |
$caseData1Desc, $caseData2Desc, $caseData3Desc, |
306 |
$caseRequireEmail, $casaId ) |
307 |
or return ( $success = 0, my $errorCode = 6, my $errorMessage = $sth->errstr() ); |
308 |
$sth->finish; |
309 |
|
310 |
return $success; |
311 |
|
312 |
} |
313 |
|
314 |
=head2 DeleteClubOrServiceArchetype |
315 |
|
316 |
Deletes an Archetype of the given id |
317 |
and all Clubs or Services based on it, |
318 |
and all Enrollments based on those clubs |
319 |
or services. |
320 |
|
321 |
Input: |
322 |
$casaId : id of the Archtype to be deleted |
323 |
|
324 |
Output: |
325 |
$success : 1 on successful deletion, 0 otherwise |
326 |
|
327 |
=cut |
328 |
|
329 |
sub DeleteClubOrServiceArchetype { |
330 |
my ( $casaId ) = @_; |
331 |
|
332 |
## Paramter check |
333 |
if ( ! $casaId ) { |
334 |
return 0; |
335 |
} |
336 |
|
337 |
my $success = 1; |
338 |
|
339 |
my $dbh = C4::Context->dbh; |
340 |
|
341 |
my $sth; |
342 |
|
343 |
$sth = $dbh->prepare("DELETE FROM clubsAndServicesEnrollments WHERE casaId = ?"); |
344 |
$sth->execute( $casaId ) or $success = 0; |
345 |
$sth->finish; |
346 |
|
347 |
$sth = $dbh->prepare("DELETE FROM clubsAndServices WHERE casaId = ?"); |
348 |
$sth->execute( $casaId ) or $success = 0; |
349 |
$sth->finish; |
350 |
|
351 |
$sth = $dbh->prepare("DELETE FROM clubsAndServicesArchetypes WHERE casaId = ?"); |
352 |
$sth->execute( $casaId ) or $success = 0; |
353 |
$sth->finish; |
354 |
|
355 |
return 1; |
356 |
} |
357 |
|
358 |
=head2 AddClubOrService |
359 |
|
360 |
Creates a new club or service in the database |
361 |
|
362 |
Input: |
363 |
$type: 'club' or 'service', other types may be added as necessary. |
364 |
$title: Short description of the club or service |
365 |
$description: Long description of the club or service |
366 |
$casData1: The data described in case.casData1Title |
367 |
$casData2: The data described in case.casData2Title |
368 |
$casData3: The data described in case.casData3Title |
369 |
$startDate: The date the club or service begins ( Optional: Defaults to TODAY() ) |
370 |
$endDate: The date the club or service ends ( Optional ) |
371 |
$branchcode: Branch that created this club or service ( Optional: NULL is system-wide ) |
372 |
|
373 |
Output: |
374 |
$success: 1 on successful add, 0 on failure |
375 |
$errorCode: Code for reason of failure, good for translating errors in templates |
376 |
$errorMessage: English description of error |
377 |
|
378 |
=cut |
379 |
|
380 |
sub AddClubOrService { |
381 |
my ( $casaId, $title, $description, $casData1, $casData2, $casData3, $startDate, $endDate, $branchcode ) = @_; |
382 |
|
383 |
## Check for all neccessary parameters |
384 |
if ( ! $casaId ) { |
385 |
return ( 0, 1, "No Archetype Given" ); |
386 |
} |
387 |
if ( ! $title ) { |
388 |
return ( 0, 2, "No Title Given" ); |
389 |
} |
390 |
if ( ! $description ) { |
391 |
return ( 0, 3, "No Description Given" ); |
392 |
} |
393 |
|
394 |
my $success = 1; |
395 |
|
396 |
if ( ! $startDate ) { |
397 |
$startDate = getTodayMysqlDateFormat(); |
398 |
} |
399 |
|
400 |
my $dbh = C4::Context->dbh; |
401 |
|
402 |
my $sth; |
403 |
if ( $endDate ) { |
404 |
$sth = $dbh->prepare("INSERT INTO clubsAndServices ( casId, casaId, title, description, casData1, casData2, casData3, startDate, endDate, branchcode, last_updated ) |
405 |
VALUES ( NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP)"); |
406 |
$sth->execute( $casaId, $title, $description, $casData1, $casData2, $casData3, $startDate, $endDate, $branchcode ) or $success = 0; |
407 |
} else { |
408 |
$sth = $dbh->prepare("INSERT INTO clubsAndServices ( casId, casaId, title, description, casData1, casData2, casData3, startDate, branchcode, last_updated ) |
409 |
VALUES ( NULL, ?, ?, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP)"); |
410 |
$sth->execute( $casaId, $title, $description, $casData1, $casData2, $casData3, $startDate, $branchcode ) or $success = 0; |
411 |
} |
412 |
$sth->finish; |
413 |
|
414 |
my ( $errorCode, $errorMessage ); |
415 |
if ( ! $success ) { |
416 |
$errorMessage = "Database Failure"; |
417 |
$errorCode = 5; |
418 |
} |
419 |
|
420 |
return( $success, $errorCode, $errorMessage ); |
421 |
} |
422 |
|
423 |
=head UpdateClubOrService |
424 |
|
425 |
Updates club or service in the database |
426 |
|
427 |
Input: |
428 |
$casId: id of the club or service to be updated |
429 |
$type: 'club' or 'service', other types may be added as necessary. |
430 |
$title: Short description of the club or service |
431 |
$description: Long description of the club or service |
432 |
$casData1: The data described in case.casData1Title |
433 |
$casData2: The data described in case.casData2Title |
434 |
$casData3: The data described in case.casData3Title |
435 |
$startDate: The date the club or service begins ( Optional: Defaults to TODAY() ) |
436 |
$endDate: The date the club or service ends ( Optional ) |
437 |
|
438 |
Output: |
439 |
$success: 1 on successful add, 0 on failure |
440 |
$errorCode: Code for reason of failure, good for translating errors in templates |
441 |
$errorMessage: English description of error |
442 |
|
443 |
=cut |
444 |
|
445 |
sub UpdateClubOrService { |
446 |
my ( $casId, $casaId, $title, $description, $casData1, $casData2, $casData3, $startDate, $endDate ) = @_; |
447 |
|
448 |
## Check for all neccessary parameters |
449 |
if ( ! $casId ) { |
450 |
return ( 0, 1, "No casId Given" ); |
451 |
} |
452 |
if ( ! $casaId ) { |
453 |
return ( 0, 2, "No Archetype Given" ); |
454 |
} |
455 |
if ( ! $title ) { |
456 |
return ( 0, 3, "No Title Given" ); |
457 |
} |
458 |
if ( ! $description ) { |
459 |
return ( 0, 4, "No Description Given" ); |
460 |
} |
461 |
|
462 |
my $success = 1; |
463 |
|
464 |
if ( ! $startDate ) { |
465 |
$startDate = getTodayMysqlDateFormat(); |
466 |
} |
467 |
|
468 |
my $dbh = C4::Context->dbh; |
469 |
|
470 |
my $sth; |
471 |
if ( $endDate ) { |
472 |
$sth = $dbh->prepare("UPDATE clubsAndServices SET casaId = ?, title = ?, description = ?, casData1 = ?, casData2 = ?, casData3 = ?, startDate = ?, endDate = ?, last_updated = NOW() WHERE casId = ?"); |
473 |
$sth->execute( $casaId, $title, $description, $casData1, $casData2, $casData3, $startDate, $endDate, $casId ) or return( my $success = 0, my $errorCode = 5, my $errorMessage = $sth->errstr() ); |
474 |
} else { |
475 |
$sth = $dbh->prepare("UPDATE clubsAndServices SET casaId = ?, title = ?, description = ?, casData1 = ?, casData2 = ?, casData3 = ?, startDate = ?, endDate = NULL, last_updated = NOW() WHERE casId = ?"); |
476 |
$sth->execute( $casaId, $title, $description, $casData1, $casData2, $casData3, $startDate, $casId ) or return( my $success = 0, my $errorCode = 5, my $errorMessage = $sth->errstr() ); |
477 |
} |
478 |
$sth->finish; |
479 |
|
480 |
my ( $errorCode, $errorMessage ); |
481 |
if ( ! $success ) { |
482 |
$errorMessage = "Database Failure"; |
483 |
$errorCode = 5; |
484 |
} |
485 |
|
486 |
return( $success, $errorCode, $errorMessage ); |
487 |
} |
488 |
|
489 |
=head2 DeleteClubOrService |
490 |
|
491 |
Deletes a club or service of the given id and all enrollments based on it. |
492 |
|
493 |
Input: |
494 |
$casId : id of the club or service to be deleted |
495 |
|
496 |
Output: |
497 |
$success : 1 on successful deletion, 0 otherwise |
498 |
|
499 |
=cut |
500 |
|
501 |
sub DeleteClubOrService { |
502 |
my ( $casId ) = @_; |
503 |
|
504 |
if ( ! $casId ) { |
505 |
return 0; |
506 |
} |
507 |
|
508 |
my $success = 1; |
509 |
|
510 |
my $dbh = C4::Context->dbh; |
511 |
|
512 |
my $sth; |
513 |
$sth = $dbh->prepare("DELETE FROM clubsAndServicesEnrollments WHERE casId = ?"); |
514 |
$sth->execute( $casId ) or $success = 0; |
515 |
$sth->finish; |
516 |
|
517 |
$sth = $dbh->prepare("DELETE FROM clubsAndServices WHERE casId = ?"); |
518 |
$sth->execute( $casId ) or $success = 0; |
519 |
$sth->finish; |
520 |
|
521 |
return 1; |
522 |
} |
523 |
|
524 |
=head EnrollInClubOrService |
525 |
|
526 |
Enrolls a borrower in a given club or service |
527 |
|
528 |
Input: |
529 |
$casId: The unique id of the club or service being enrolled in |
530 |
$borrowerCardnumber: The card number of the enrolling borrower |
531 |
$dateEnrolled: Date the enrollment begins ( Optional: Defauls to TODAY() ) |
532 |
$data1: The data described in ClubsAndServicesArchetypes.caseData1Title |
533 |
$data2: The data described in ClubsAndServicesArchetypes.caseData2Title |
534 |
$data3: The data described in ClubsAndServicesArchetypes.caseData3Title |
535 |
$branchcode: The branch where this club or service enrollment is, |
536 |
$borrowernumber: ( Optional: Alternative to using $borrowerCardnumber ) |
537 |
|
538 |
Output: |
539 |
$success: 1 on successful enrollment, 0 on failure |
540 |
$errorCode: Code for reason of failure, good for translating errors in templates |
541 |
$errorMessage: English description of error |
542 |
|
543 |
=cut |
544 |
|
545 |
sub EnrollInClubOrService { |
546 |
my ( $casaId, $casId, $borrowerCardnumber, $dateEnrolled, $data1, $data2, $data3, $branchcode, $borrowernumber ) = @_; |
547 |
|
548 |
## Check for all neccessary parameters |
549 |
unless ( $casaId ) { |
550 |
return ( 0, 1, "No casaId Given" ); |
551 |
} |
552 |
unless ( $casId ) { |
553 |
return ( 0, 2, "No casId Given" ); |
554 |
} |
555 |
unless ( ( $borrowerCardnumber || $borrowernumber ) ) { |
556 |
return ( 0, 3, "No Borrower Given" ); |
557 |
} |
558 |
|
559 |
my $member; |
560 |
if ( $borrowerCardnumber ) { |
561 |
$member = C4::Members::GetMember( cardnumber => $borrowerCardnumber ); |
562 |
} elsif ( $borrowernumber ) { |
563 |
$member = C4::Members::GetMember( borrowernumber => $borrowernumber ); |
564 |
} else { |
565 |
return ( 0, 3, "No Borrower Given" ); |
566 |
} |
567 |
|
568 |
unless ( $member ) { |
569 |
return ( 0, 4, "No Matching Borrower Found" ); |
570 |
} |
571 |
|
572 |
my $casa = GetClubOrServiceArchetype( $casaId, 1 ); |
573 |
if ( $casa->{'caseRequireEmail'} ) { |
574 |
my $AutoEmailPrimaryAddress = C4::Context->preference('AutoEmailPrimaryAddress'); |
575 |
unless( $member->{ $AutoEmailPrimaryAddress } ) { |
576 |
return( 0, 4, "Email Address Required: No Valid Email Address In Borrower Record" ); |
577 |
} |
578 |
} |
579 |
|
580 |
$borrowernumber = $member->{'borrowernumber'}; |
581 |
|
582 |
if ( isEnrolled( $casId, $borrowernumber ) ) { return ( 0, 5, "Member is already enrolled!" ); } |
583 |
|
584 |
if ( ! $dateEnrolled ) { |
585 |
$dateEnrolled = getTodayMysqlDateFormat(); |
586 |
} |
587 |
|
588 |
my $dbh = C4::Context->dbh; |
589 |
my $sth = $dbh->prepare("INSERT INTO clubsAndServicesEnrollments ( caseId, casaId, casId, borrowernumber, data1, data2, data3, dateEnrolled, dateCanceled, last_updated, branchcode) |
590 |
VALUES ( NULL, ?, ?, ?, ?, ?, ?, ?, NULL, NOW(), ? )"); |
591 |
$sth->execute( $casaId, $casId, $borrowernumber, $data1, $data2, $data3, $dateEnrolled, $branchcode ) or return( my $success = 0, my $errorCode = 4, my $errorMessage = $sth->errstr() ); |
592 |
$sth->finish; |
593 |
|
594 |
return $success = 1; |
595 |
} |
596 |
|
597 |
=head2 GetEnrollments |
598 |
|
599 |
Returns information about the clubs and services the given borrower is enrolled in. |
600 |
|
601 |
Input: |
602 |
$borrowernumber: The borrowernumber of the borrower |
603 |
|
604 |
Output: |
605 |
$results: Reference to an array of associated arrays |
606 |
|
607 |
=cut |
608 |
|
609 |
sub GetEnrollments { |
610 |
my ( $borrowernumber ) = @_; |
611 |
|
612 |
my $dbh = C4::Context->dbh; |
613 |
|
614 |
my $sth = $dbh->prepare("SELECT * FROM clubsAndServices, clubsAndServicesEnrollments |
615 |
WHERE clubsAndServices.casId = clubsAndServicesEnrollments.casId |
616 |
AND clubsAndServicesEnrollments.borrowernumber = ?"); |
617 |
$sth->execute( $borrowernumber ) or return 0; |
618 |
|
619 |
my @results; |
620 |
while ( my $row = $sth->fetchrow_hashref ) { |
621 |
push( @results , $row ); |
622 |
} |
623 |
|
624 |
$sth->finish; |
625 |
|
626 |
return \@results; |
627 |
} |
628 |
|
629 |
=head2 GetCasEnrollments |
630 |
|
631 |
Returns information about the clubs and services borrowers that are enrolled |
632 |
|
633 |
Input: |
634 |
$casId: The id of the club or service to look up enrollments for |
635 |
|
636 |
Output: |
637 |
$results: Reference to an array of associated arrays |
638 |
|
639 |
=cut |
640 |
|
641 |
sub GetCasEnrollments { |
642 |
my ( $casId ) = @_; |
643 |
|
644 |
my $dbh = C4::Context->dbh; |
645 |
|
646 |
my $sth = $dbh->prepare("SELECT * FROM clubsAndServicesEnrollments, borrowers |
647 |
WHERE clubsAndServicesEnrollments.borrowernumber = borrowers.borrowernumber |
648 |
AND clubsAndServicesEnrollments.casId = ? AND dateCanceled IS NULL |
649 |
ORDER BY surname, firstname"); |
650 |
$sth->execute( $casId ) or return 0; |
651 |
|
652 |
my @results; |
653 |
while ( my $row = $sth->fetchrow_hashref ) { |
654 |
push( @results , $row ); |
655 |
} |
656 |
|
657 |
$sth->finish; |
658 |
|
659 |
return \@results; |
660 |
} |
661 |
|
662 |
=head2 GetClubsAndServices |
663 |
|
664 |
Returns information about clubs and services |
665 |
|
666 |
Input: |
667 |
$type: ( Optional: 'club' or 'service' ) |
668 |
$branchcode: ( Optional: Get clubs and services only created by this branch ) |
669 |
$orderby: ( Optional: name of column to sort by ) |
670 |
|
671 |
Output: |
672 |
$results: Reference to an array of associated arrays |
673 |
|
674 |
=cut |
675 |
|
676 |
sub GetClubsAndServices { |
677 |
my ( $type, $branchcode, $orderby ) = @_; |
678 |
$orderby = 'startDate DESC' unless ( $orderby ); |
679 |
|
680 |
my $dbh = C4::Context->dbh; |
681 |
|
682 |
my ( $sth, @results ); |
683 |
if ( $type && $branchcode ) { |
684 |
$sth = $dbh->prepare("SELECT clubsAndServices.casId, |
685 |
clubsAndServices.casaId, |
686 |
clubsAndServices.title, |
687 |
clubsAndServices.description, |
688 |
clubsAndServices.casData1, |
689 |
clubsAndServices.casData2, |
690 |
clubsAndServices.casData3, |
691 |
clubsAndServices.startDate, |
692 |
clubsAndServices.endDate, |
693 |
clubsAndServices.last_updated, |
694 |
clubsAndServices.branchcode |
695 |
FROM clubsAndServices, clubsAndServicesArchetypes |
696 |
WHERE |
697 |
clubsAndServices.casaId = clubsAndServicesArchetypes.casaId |
698 |
AND clubsAndServices.branchcode = ? |
699 |
AND clubsAndServicesArchetypes.type = ? |
700 |
ORDER BY $orderby |
701 |
"); |
702 |
$sth->execute( $branchcode, $type ) or return 0; |
703 |
|
704 |
} elsif ( $type ) { |
705 |
$sth = $dbh->prepare("SELECT clubsAndServices.casId, |
706 |
clubsAndServices.casaId, |
707 |
clubsAndServices.title, |
708 |
clubsAndServices.description, |
709 |
clubsAndServices.casData1, |
710 |
clubsAndServices.casData2, |
711 |
clubsAndServices.casData3, |
712 |
clubsAndServices.startDate, |
713 |
clubsAndServices.endDate, |
714 |
clubsAndServices.last_updated, |
715 |
clubsAndServices.branchcode |
716 |
FROM clubsAndServices, clubsAndServicesArchetypes |
717 |
WHERE |
718 |
clubsAndServices.casaId = clubsAndServicesArchetypes.casaId |
719 |
AND clubsAndServicesArchetypes.type = ? |
720 |
ORDER BY $orderby |
721 |
"); |
722 |
$sth->execute( $type ) or return 0; |
723 |
|
724 |
} elsif ( $branchcode ) { |
725 |
$sth = $dbh->prepare("SELECT clubsAndServices.casId, |
726 |
clubsAndServices.casaId, |
727 |
clubsAndServices.title, |
728 |
clubsAndServices.description, |
729 |
clubsAndServices.casData1, |
730 |
clubsAndServices.casData2, |
731 |
clubsAndServices.casData3, |
732 |
clubsAndServices.startDate, |
733 |
clubsAndServices.endDate, |
734 |
clubsAndServices.last_updated, |
735 |
clubsAndServices.branchcode |
736 |
FROM clubsAndServices, clubsAndServicesArchetypes |
737 |
WHERE |
738 |
clubsAndServices.casaId = clubsAndServicesArchetypes.casaId |
739 |
AND clubsAndServices.branchcode = ? |
740 |
ORDER BY $orderby |
741 |
"); |
742 |
$sth->execute( $branchcode ) or return 0; |
743 |
|
744 |
} else { ## Get all clubs and services |
745 |
$sth = $dbh->prepare("SELECT * FROM clubsAndServices ORDER BY $orderby"); |
746 |
$sth->execute() or return 0; |
747 |
} |
748 |
|
749 |
while ( my $row = $sth->fetchrow_hashref ) { |
750 |
push( @results , $row ); |
751 |
} |
752 |
|
753 |
$sth->finish; |
754 |
|
755 |
return \@results; |
756 |
|
757 |
} |
758 |
|
759 |
=head2 GetClubOrService |
760 |
|
761 |
Returns information about a club or service |
762 |
|
763 |
Input: |
764 |
$casId: Id of club or service to get |
765 |
|
766 |
Output: $casId, $casaId, $title, $description, $casData1, $casData2, $casData3, $startDate, $endDate, $last_updated, $branchcode |
767 |
|
768 |
=cut |
769 |
|
770 |
sub GetClubOrService { |
771 |
my ( $casId ) = @_; |
772 |
|
773 |
my $dbh = C4::Context->dbh; |
774 |
|
775 |
my ( $sth, @results ); |
776 |
$sth = $dbh->prepare("SELECT * FROM clubsAndServices WHERE casId = ?"); |
777 |
$sth->execute( $casId ) or return 0; |
778 |
|
779 |
my $row = $sth->fetchrow_hashref; |
780 |
|
781 |
$sth->finish; |
782 |
|
783 |
return ( |
784 |
$$row{'casId'}, |
785 |
$$row{'casaId'}, |
786 |
$$row{'title'}, |
787 |
$$row{'description'}, |
788 |
$$row{'casData1'}, |
789 |
$$row{'casData2'}, |
790 |
$$row{'casData3'}, |
791 |
$$row{'startDate'}, |
792 |
$$row{'endDate'}, |
793 |
$$row{'last_updated'}, |
794 |
$$row{'branchcode'} |
795 |
); |
796 |
|
797 |
} |
798 |
|
799 |
=head2 GetClubsAndServicesArchetypes |
800 |
|
801 |
Returns information about clubs and services archetypes |
802 |
|
803 |
Input: |
804 |
$type: 'club' or 'service' ( Optional: Defaults to all types ) |
805 |
$branchcode: Get clubs or services created by this branch ( Optional ) |
806 |
|
807 |
Output: |
808 |
$results: |
809 |
Otherwise: Reference to an array of associated arrays |
810 |
Except: 0 on failure |
811 |
|
812 |
=cut |
813 |
|
814 |
sub GetClubsAndServicesArchetypes { |
815 |
my ( $type, $branchcode ) = @_; |
816 |
my $dbh = C4::Context->dbh; |
817 |
|
818 |
my $sth; |
819 |
if ( $type && $branchcode) { |
820 |
$sth = $dbh->prepare("SELECT * FROM clubsAndServicesArchetypes WHERE type = ? AND branchcode = ?"); |
821 |
$sth->execute( $type, $branchcode ) or return 0; |
822 |
} elsif ( $type ) { |
823 |
$sth = $dbh->prepare("SELECT * FROM clubsAndServicesArchetypes WHERE type = ?"); |
824 |
$sth->execute( $type ) or return 0; |
825 |
} elsif ( $branchcode ) { |
826 |
$sth = $dbh->prepare("SELECT * FROM clubsAndServicesArchetypes WHERE branchcode = ?"); |
827 |
$sth->execute( $branchcode ) or return 0; |
828 |
} else { |
829 |
$sth = $dbh->prepare("SELECT * FROM clubsAndServicesArchetypes"); |
830 |
$sth->execute() or return 0; |
831 |
} |
832 |
|
833 |
my @results; |
834 |
while ( my $row = $sth->fetchrow_hashref ) { |
835 |
push( @results , $row ); |
836 |
} |
837 |
|
838 |
$sth->finish; |
839 |
|
840 |
return \@results; |
841 |
} |
842 |
|
843 |
=head2 GetClubOrServiceArchetype |
844 |
|
845 |
Returns information about a club or services archetype |
846 |
|
847 |
Input: |
848 |
$casaId: Id of Archetype to get |
849 |
$asHashref: Optional, if true, will return hashref instead of array |
850 |
|
851 |
Output: |
852 |
( $casaId, $type, $title, $description, $publicEnrollment, |
853 |
$casData1Title, $casData2Title, $casData3Title, |
854 |
$caseData1Title, $caseData2Title, $caseData3Title, |
855 |
$casData1Desc, $casData2Desc, $casData3Desc, |
856 |
$caseData1Desc, $caseData2Desc, $caseData3Desc, |
857 |
$caseRequireEmail, $last_updated, $branchcode ) |
858 |
Except: 0 on failure |
859 |
|
860 |
=cut |
861 |
|
862 |
sub GetClubOrServiceArchetype { |
863 |
my ( $casaId, $asHashref ) = @_; |
864 |
|
865 |
my $dbh = C4::Context->dbh; |
866 |
|
867 |
my $sth; |
868 |
$sth = $dbh->prepare("SELECT * FROM clubsAndServicesArchetypes WHERE casaId = ?"); |
869 |
$sth->execute( $casaId ) or return 0; |
870 |
|
871 |
my $row = $sth->fetchrow_hashref; |
872 |
|
873 |
$sth->finish; |
874 |
|
875 |
if ( $asHashref ) { return $row; } |
876 |
|
877 |
return ( |
878 |
$$row{'casaId'}, |
879 |
$$row{'type'}, |
880 |
$$row{'title'}, |
881 |
$$row{'description'}, |
882 |
$$row{'publicEnrollment'}, |
883 |
$$row{'casData1Title'}, |
884 |
$$row{'casData2Title'}, |
885 |
$$row{'casData3Title'}, |
886 |
$$row{'caseData1Title'}, |
887 |
$$row{'caseData2Title'}, |
888 |
$$row{'caseData3Title'}, |
889 |
$$row{'casData1Desc'}, |
890 |
$$row{'casData2Desc'}, |
891 |
$$row{'casData3Desc'}, |
892 |
$$row{'caseData1Desc'}, |
893 |
$$row{'caseData2Desc'}, |
894 |
$$row{'caseData3Desc'}, |
895 |
$$row{'caseRequireEmail'}, |
896 |
$$row{'last_updated'}, |
897 |
$$row{'branchcode'} |
898 |
); |
899 |
} |
900 |
|
901 |
=head2 DoesEnrollmentRequireData |
902 |
|
903 |
Returns 1 if the given Archetype has |
904 |
data fields that need to be filled in |
905 |
at the time of enrollment. |
906 |
|
907 |
Input: |
908 |
$casaId: Id of Archetype to get |
909 |
|
910 |
Output: |
911 |
1: Enrollment will require extra data |
912 |
0: Enrollment will not require extra data |
913 |
|
914 |
=cut |
915 |
|
916 |
sub DoesEnrollmentRequireData { |
917 |
my ( $casaId ) = @_; |
918 |
|
919 |
my $dbh = C4::Context->dbh; |
920 |
|
921 |
my $sth; |
922 |
$sth = $dbh->prepare("SELECT caseData1Title FROM clubsAndServicesArchetypes WHERE casaId = ?"); |
923 |
$sth->execute( $casaId ) or return 0; |
924 |
|
925 |
my $row = $sth->fetchrow_hashref; |
926 |
|
927 |
$sth->finish; |
928 |
|
929 |
if ( $$row{'caseData1Title'} ) { |
930 |
return 1; |
931 |
} else { |
932 |
return 0; |
933 |
} |
934 |
} |
935 |
|
936 |
|
937 |
=head2 CancelClubOrServiceEnrollment |
938 |
|
939 |
Cancels the given enrollment in a club or service |
940 |
|
941 |
Input: |
942 |
$caseId: The id of the enrollment to be canceled |
943 |
|
944 |
Output: |
945 |
$success: 1 on successful cancelation, 0 otherwise |
946 |
|
947 |
=cut |
948 |
|
949 |
sub CancelClubOrServiceEnrollment { |
950 |
my ( $caseId ) = @_; |
951 |
|
952 |
my $success = 1; |
953 |
|
954 |
my $dbh = C4::Context->dbh; |
955 |
|
956 |
my $sth = $dbh->prepare("UPDATE clubsAndServicesEnrollments SET dateCanceled = CURDATE(), last_updated = NOW() WHERE caseId = ?"); |
957 |
$sth->execute( $caseId ) or $success = 0; |
958 |
$sth->finish; |
959 |
|
960 |
return $success; |
961 |
} |
962 |
|
963 |
=head2 GetEnrolledClubsAndServices |
964 |
|
965 |
Returns information about clubs and services |
966 |
the given borrower is enrolled in. |
967 |
|
968 |
Input: |
969 |
$borrowernumber |
970 |
|
971 |
Output: |
972 |
$results: Reference to an array of associated arrays |
973 |
|
974 |
=cut |
975 |
|
976 |
sub GetEnrolledClubsAndServices { |
977 |
my ( $borrowernumber ) = @_; |
978 |
my $dbh = C4::Context->dbh; |
979 |
|
980 |
my ( $sth, @results ); |
981 |
$sth = $dbh->prepare("SELECT |
982 |
clubsAndServicesEnrollments.caseId, |
983 |
clubsAndServices.casId, |
984 |
clubsAndServices.casaId, |
985 |
clubsAndServices.title, |
986 |
clubsAndServices.description, |
987 |
clubsAndServices.branchcode, |
988 |
clubsAndServicesArchetypes.type, |
989 |
clubsAndServicesArchetypes.publicEnrollment |
990 |
FROM clubsAndServices, clubsAndServicesArchetypes, clubsAndServicesEnrollments |
991 |
WHERE ( |
992 |
clubsAndServices.casaId = clubsAndServicesArchetypes.casaId |
993 |
AND clubsAndServices.casId = clubsAndServicesEnrollments.casId |
994 |
AND ( clubsAndServices.endDate >= CURRENT_DATE() OR clubsAndServices.endDate IS NULL ) |
995 |
AND clubsAndServicesEnrollments.dateCanceled IS NULL |
996 |
AND clubsAndServicesEnrollments.borrowernumber = ? |
997 |
) |
998 |
ORDER BY type, title |
999 |
"); |
1000 |
$sth->execute( $borrowernumber ) or return 0; |
1001 |
|
1002 |
while ( my $row = $sth->fetchrow_hashref ) { |
1003 |
push( @results , $row ); |
1004 |
} |
1005 |
|
1006 |
$sth->finish; |
1007 |
|
1008 |
return \@results; |
1009 |
|
1010 |
} |
1011 |
|
1012 |
=head2 GetPubliclyEnrollableClubsAndServices |
1013 |
|
1014 |
Returns information about clubs and services |
1015 |
the given borrower can enroll in. |
1016 |
|
1017 |
Input: |
1018 |
$borrowernumber |
1019 |
|
1020 |
Output: |
1021 |
$results: Reference to an array of associated arrays |
1022 |
|
1023 |
=cut |
1024 |
|
1025 |
sub GetPubliclyEnrollableClubsAndServices { |
1026 |
my ( $borrowernumber ) = @_; |
1027 |
|
1028 |
my $dbh = C4::Context->dbh; |
1029 |
|
1030 |
my ( $sth, @results ); |
1031 |
$sth = $dbh->prepare(" |
1032 |
SELECT |
1033 |
DISTINCT ( clubsAndServices.casId ), |
1034 |
clubsAndServices.title, |
1035 |
clubsAndServices.description, |
1036 |
clubsAndServices.branchcode, |
1037 |
clubsAndServicesArchetypes.type, |
1038 |
clubsAndServices.casaId |
1039 |
FROM clubsAndServices, clubsAndServicesArchetypes |
1040 |
WHERE clubsAndServicesArchetypes.casaId = clubsAndServices.casaId |
1041 |
AND clubsAndServicesArchetypes.publicEnrollment =1 |
1042 |
AND clubsAndServices.casId NOT |
1043 |
IN ( |
1044 |
SELECT clubsAndServices.casId |
1045 |
FROM clubsAndServices, clubsAndServicesEnrollments |
1046 |
WHERE clubsAndServicesEnrollments.casId = clubsAndServices.casId |
1047 |
AND clubsAndServicesEnrollments.dateCanceled IS NULL |
1048 |
AND clubsAndServicesEnrollments.borrowernumber = ? |
1049 |
) |
1050 |
ORDER BY type, title"); |
1051 |
$sth->execute( $borrowernumber ) or return 0; |
1052 |
|
1053 |
while ( my $row = $sth->fetchrow_hashref ) { |
1054 |
push( @results , $row ); |
1055 |
} |
1056 |
|
1057 |
$sth->finish; |
1058 |
|
1059 |
return \@results; |
1060 |
|
1061 |
} |
1062 |
|
1063 |
=head2 GetAllEnrollableClubsAndServices |
1064 |
|
1065 |
Returns information about clubs and services |
1066 |
the given borrower can enroll in. |
1067 |
|
1068 |
Input: |
1069 |
$borrowernumber |
1070 |
|
1071 |
Output: |
1072 |
$results: Reference to an array of associated arrays |
1073 |
|
1074 |
=cut |
1075 |
|
1076 |
sub GetAllEnrollableClubsAndServices { |
1077 |
my ( $borrowernumber, $branchcode ) = @_; |
1078 |
|
1079 |
if ( $branchcode eq '' ) { |
1080 |
$branchcode = '%'; |
1081 |
} |
1082 |
|
1083 |
my $dbh = C4::Context->dbh; |
1084 |
|
1085 |
my ( $sth, @results ); |
1086 |
$sth = $dbh->prepare(" |
1087 |
SELECT |
1088 |
DISTINCT ( clubsAndServices.casId ), |
1089 |
clubsAndServices.title, |
1090 |
clubsAndServices.description, |
1091 |
clubsAndServices.branchcode, |
1092 |
clubsAndServicesArchetypes.type, |
1093 |
clubsAndServices.casaId |
1094 |
FROM clubsAndServices, clubsAndServicesArchetypes |
1095 |
WHERE clubsAndServicesArchetypes.casaId = clubsAndServices.casaId |
1096 |
AND ( |
1097 |
DATE(clubsAndServices.endDate) >= CURDATE() |
1098 |
OR |
1099 |
clubsAndServices.endDate IS NULL |
1100 |
) |
1101 |
AND clubsAndServices.branchcode LIKE ? |
1102 |
AND clubsAndServices.casId NOT |
1103 |
IN ( |
1104 |
SELECT clubsAndServices.casId |
1105 |
FROM clubsAndServices, clubsAndServicesEnrollments |
1106 |
WHERE clubsAndServicesEnrollments.casId = clubsAndServices.casId |
1107 |
AND clubsAndServicesEnrollments.dateCanceled IS NULL |
1108 |
AND clubsAndServicesEnrollments.borrowernumber = ? |
1109 |
) |
1110 |
ORDER BY type, title"); |
1111 |
$sth->execute( $branchcode, $borrowernumber ) or return 0; |
1112 |
|
1113 |
while ( my $row = $sth->fetchrow_hashref ) { |
1114 |
push( @results , $row ); |
1115 |
} |
1116 |
|
1117 |
$sth->finish; |
1118 |
|
1119 |
return \@results; |
1120 |
|
1121 |
} |
1122 |
|
1123 |
|
1124 |
sub getBorrowernumberByCardnumber { |
1125 |
my $dbh = C4::Context->dbh; |
1126 |
|
1127 |
my $sth = $dbh->prepare("SELECT borrowernumber FROM borrowers WHERE cardnumber = ?"); |
1128 |
$sth->execute( @_ ) or return( 0 ); |
1129 |
|
1130 |
my $row = $sth->fetchrow_hashref; |
1131 |
|
1132 |
my $borrowernumber = $$row{'borrowernumber'}; |
1133 |
$sth->finish; |
1134 |
|
1135 |
return( $borrowernumber ); |
1136 |
} |
1137 |
|
1138 |
sub isEnrolled { |
1139 |
my ( $casId, $borrowernumber ) = @_; |
1140 |
|
1141 |
my $dbh = C4::Context->dbh; |
1142 |
|
1143 |
my $sth = $dbh->prepare("SELECT COUNT(*) as isEnrolled FROM clubsAndServicesEnrollments WHERE casId = ? AND borrowernumber = ? AND dateCanceled IS NULL"); |
1144 |
$sth->execute( $casId, $borrowernumber ) or return( 0 ); |
1145 |
|
1146 |
my $row = $sth->fetchrow_hashref; |
1147 |
|
1148 |
my $isEnrolled = $$row{'isEnrolled'}; |
1149 |
$sth->finish; |
1150 |
|
1151 |
return( $isEnrolled ); |
1152 |
} |
1153 |
|
1154 |
sub getTodayMysqlDateFormat { |
1155 |
my ($day,$month,$year) = (localtime)[3,4,5]; |
1156 |
my $today = sprintf("%04d-%02d-%02d", $year + 1900, $month + 1, $day); |
1157 |
return $today; |
1158 |
} |
1159 |
|
1160 |
sub ReserveForBestSellersClub { |
1161 |
my ( $biblionumber ) = @_; |
1162 |
|
1163 |
unless( $biblionumber ) { return; } |
1164 |
|
1165 |
my $dbh = C4::Context->dbh; |
1166 |
my $sth; |
1167 |
|
1168 |
## Grab the bib for this biblionumber, we will need the author and title to find the relevent clubs |
1169 |
my $biblio_data = C4::Biblio::GetBiblioData( $biblionumber ); |
1170 |
my $author = $biblio_data->{'author'}; |
1171 |
my $title = $biblio_data->{'title'}; |
1172 |
my $itemtype = $biblio_data->{'itemtype'}; |
1173 |
|
1174 |
## Find the casaId for the Bestsellers Club archetype |
1175 |
$sth = $dbh->prepare("SELECT * FROM clubsAndServicesArchetypes WHERE title LIKE 'Bestsellers Club' "); |
1176 |
$sth->execute(); |
1177 |
my $casa = $sth->fetchrow_hashref(); |
1178 |
my $casaId = $casa->{'casaId'}; |
1179 |
$sth->finish(); |
1180 |
|
1181 |
unless( $casaId ) { return; } |
1182 |
|
1183 |
## Find all the relevent bestsellers clubs |
1184 |
## casData1 is title, casData2 is author |
1185 |
$sth = $dbh->prepare("SELECT * FROM clubsAndServices WHERE casaId = ?"); |
1186 |
$sth->execute( $casaId ); |
1187 |
my @clubs; |
1188 |
while ( my $club = $sth->fetchrow_hashref() ) { |
1189 |
#warn "Author/casData2 : '$author'/ " . $club->{'casData2'} . "'"; |
1190 |
#warn "Title/casData1 : '$title'/" . $club->{'casData1'} . "'"; |
1191 |
|
1192 |
## If the author, title or both match, keep it. |
1193 |
if ( ($club->{'casData1'} eq $title) || ($club->{'casData2'} eq $author) ) { |
1194 |
push( @clubs, $club ); |
1195 |
#warn "casId" . $club->{'casId'}; |
1196 |
} elsif ( $club->{'casData1'} =~ m/%/ ) { # Title is using % as a wildcard |
1197 |
my @substrings = split(/%/, $club->{'casData1'} ); |
1198 |
my $all_match = 1; |
1199 |
foreach my $sub ( @substrings ) { |
1200 |
unless( $title =~ m/\Q$sub/) { |
1201 |
$all_match = 0; |
1202 |
} |
1203 |
} |
1204 |
if ( $all_match ) { push( @clubs, $club ); } |
1205 |
} elsif ( $club->{'casData2'} =~ m/%/ ) { # Author is using % as a wildcard |
1206 |
my @substrings = split(/%/, $club->{'casData2'} ); |
1207 |
my $all_match = 1; |
1208 |
foreach my $sub ( @substrings ) { |
1209 |
unless( $author =~ m/\Q$sub/) { |
1210 |
$all_match = 0; |
1211 |
} |
1212 |
} |
1213 |
|
1214 |
## Make sure the bib is in the list of itemtypes to use |
1215 |
my @itemtypes = split( / /, $club->{'casData3'} ); |
1216 |
my $found_itemtype_match = 0; |
1217 |
if ( @itemtypes ) { ## If no itemtypes are listed, all itemtypes are valid, skip test. |
1218 |
foreach my $it ( @itemtypes ) { |
1219 |
if ( $it eq $itemtype ) { |
1220 |
$found_itemtype_match = 1; |
1221 |
last; ## Short circuit for speed. |
1222 |
} |
1223 |
} |
1224 |
$all_match = 0 unless ( $found_itemtype_match ); |
1225 |
} |
1226 |
|
1227 |
if ( $all_match ) { push( @clubs, $club ); } |
1228 |
} |
1229 |
} |
1230 |
$sth->finish(); |
1231 |
|
1232 |
unless( scalar( @clubs ) ) { return; } |
1233 |
|
1234 |
## Get all the members of the relevant clubs, but only get each borrower once, even if they are in multiple relevant clubs |
1235 |
## Randomize the order of the borrowers |
1236 |
my @casIds; |
1237 |
my $sql = "SELECT DISTINCT(borrowers.borrowernumber) FROM borrowers, clubsAndServicesEnrollments |
1238 |
WHERE clubsAndServicesEnrollments.borrowernumber = borrowers.borrowernumber |
1239 |
AND ("; |
1240 |
my $clubsCount = scalar( @clubs ); |
1241 |
foreach my $club ( @clubs ) { |
1242 |
$sql .= " casId = ?"; |
1243 |
if ( --$clubsCount ) { |
1244 |
$sql .= " OR"; |
1245 |
} |
1246 |
push( @casIds, $club->{'casId'} ); |
1247 |
} |
1248 |
$sql .= " ) ORDER BY RAND()"; |
1249 |
|
1250 |
|
1251 |
$sth = $dbh->prepare( $sql ); |
1252 |
$sth->execute( @casIds ); |
1253 |
my @borrowers; |
1254 |
while ( my $borrower = $sth->fetchrow_hashref() ) { |
1255 |
push( @borrowers, $borrower ); |
1256 |
} |
1257 |
|
1258 |
unless( scalar( @borrowers ) ) { return; } |
1259 |
|
1260 |
my $priority = 1; |
1261 |
foreach my $borrower ( @borrowers ) { |
1262 |
C4::Reserves::AddReserve( |
1263 |
my $branch = $borrower->{'branchcode'}, |
1264 |
my $borrowernumber = $borrower->{'borrowernumber'}, |
1265 |
$biblionumber, |
1266 |
my $constraint = 'a', |
1267 |
my $bibitems, |
1268 |
$priority, |
1269 |
my $notes = "Automatic Reserve for Bestsellers Club", |
1270 |
$title, |
1271 |
my $checkitem, |
1272 |
my $found, |
1273 |
my $expire_date |
1274 |
); |
1275 |
$priority++; |
1276 |
} |
1277 |
} |
1278 |
|
1279 |
1; |
1280 |
|
1281 |
__END__ |
1282 |
|
1283 |
=back |
1284 |
|
1285 |
=head1 AUTHOR |
1286 |
|
1287 |
Kyle M Hall <kylemhall@gmail.com> |
1288 |
|
1289 |
=cut |