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