5 # Copyright 2000-2003 Katipo Communications
7 # This file is part of Koha.
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License along with
19 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
20 # Suite 330, Boston, MA 02111-1307 USA
29 use Digest::MD5 qw(md5_base64);
31 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
33 $VERSION = do { my @v = '$Revision$' =~ /\d+/g; shift(@v) . "." . join("_", map {sprintf "%03d", $_ } @v); };
37 C4::Members - Perl Module containing convenience functions for member handling
55 &BornameSearch &getmember &borrdata &borrdata2 &fixup_cardnumber &findguarantees &findguarantor &GuarantornameSearch &NewBorrowerNumber &modmember &newmember &changepassword &borrissues &allissues
56 &checkuniquemember &getzipnamecity &getidcity &getguarantordata &getcategorytype
57 &calcexpirydate &checkuserpassword
59 &borrowercategories &getborrowercategory
61 ðnicitycategories get_institutions
67 ($count, $borrowers) = &BornameSearch($env, $searchstring, $type);
69 Looks up patrons (borrowers) by name.
73 BUGFIX 499: C<$type> is now used to determine type of search.
74 if $type is "simple", search is performed on the first letter of the
77 C<$searchstring> is a space-separated list of search terms. Each term
78 must match the beginning a borrower's surname, first name, or other
81 C<&BornameSearch> returns a two-element list. C<$borrowers> is a
82 reference-to-array; each element is a reference-to-hash, whose keys
83 are the fields of the C<borrowers> table in the Koha database.
84 C<$count> is the number of elements in C<$borrowers>.
88 #used by member enquiries from the intranet
91 my ($env,$searchstring,$orderby,$type)=@_;
92 my $dbh = C4::Context->dbh;
93 my $query = ""; my $count; my @data;
96 if($type eq "simple") # simple search for one letter only
98 $query="Select * from borrowers where surname like ? order by $orderby";
99 @bind=("$searchstring%");
101 else # advanced search looking in surname, firstname and othernames
103 @data=split(' ',$searchstring);
105 $query="Select * from borrowers
106 where ((surname like ? or surname like ?
107 or firstname like ? or firstname like ?
108 or othernames like ? or othernames like ?)
110 @bind=("$data[0]%","% $data[0]%","$data[0]%","% $data[0]%","$data[0]%","% $data[0]%");
111 for (my $i=1;$i<$count;$i++){
112 $query=$query." and (".
113 " surname like ? or surname like ?
114 or firstname like ? or firstname like ?
115 or othernames like ? or othernames like ?)";
116 push(@bind,"$data[$i]%","% $data[$i]%","$data[$i]%","% $data[$i]%","$data[$i]%","% $data[$i]%");
119 $query=$query.") or cardnumber like ?
121 push(@bind,$searchstring);
125 my $sth=$dbh->prepare($query);
126 # warn "Q $orderby : $query";
127 $sth->execute(@bind);
130 while (my $data=$sth->fetchrow_hashref){
131 push(@results,$data);
135 return ($cnt,\@results);
140 $borrower = &getmember($cardnumber, $borrowernumber);
142 Looks up information about a patron (borrower) by either card number
143 or borrower number. If $borrowernumber is specified, C<&borrdata>
144 searches by borrower number; otherwise, it searches by card number.
146 C<&getmember> returns a reference-to-hash whose keys are the fields of
147 the C<borrowers> table in the Koha database.
152 my ($cardnumber,$bornum)=@_;
153 $cardnumber = uc $cardnumber;
154 my $dbh = C4::Context->dbh;
157 $sth=$dbh->prepare("Select * from borrowers where cardnumber=?");
158 $sth->execute($cardnumber);
160 $sth=$dbh->prepare("Select * from borrowers where borrowernumber=?");
161 $sth->execute($bornum);
163 my $data=$sth->fetchrow_hashref;
167 } else { # try with firstname
169 my $sth=$dbh->prepare("select * from borrowers where firstname=?");
170 $sth->execute($cardnumber);
171 my $data=$sth->fetchrow_hashref;
181 $borrower = &borrdata($cardnumber, $borrowernumber);
183 Looks up information about a patron (borrower) by either card number
184 or borrower number. If $borrowernumber is specified, C<&borrdata>
185 searches by borrower number; otherwise, it searches by card number.
187 C<&borrdata> returns a reference-to-hash whose keys are the fields of
188 the C<borrowers> table in the Koha database.
193 my ($cardnumber,$bornum)=@_;
194 $cardnumber = uc $cardnumber;
195 my $dbh = C4::Context->dbh;
198 $sth=$dbh->prepare("Select borrowers.*,categories.category_type from borrowers left join categories on borrowers.categorycode=categories.categorycode where cardnumber=?");
199 $sth->execute($cardnumber);
201 $sth=$dbh->prepare("Select borrowers.*,categories.category_type from borrowers left join categories on borrowers.categorycode=categories.categorycode where borrowernumber=?");
202 $sth->execute($bornum);
204 my $data=$sth->fetchrow_hashref;
205 warn "DATA".$data->{category_type};
209 } else { # try with firstname
211 my $sth=$dbh->prepare("Select borrowers.*,categories.category_type from borrowers left join categories on borrowers.categorycode=categories.categorycode where firstname=?");
212 $sth->execute($cardnumber);
213 my $data=$sth->fetchrow_hashref;
224 ($borrowed, $due, $fine) = &borrdata2($env, $borrowernumber);
226 Returns aggregate data about items borrowed by the patron with the
227 given borrowernumber.
231 C<&borrdata2> returns a three-element array. C<$borrowed> is the
232 number of books the patron currently has borrowed. C<$due> is the
233 number of overdue items the patron currently has borrowed. C<$fine> is
234 the total fine currently due by the borrower.
239 my ($env,$bornum)=@_;
240 my $dbh = C4::Context->dbh;
241 my $query="Select count(*) from issues where borrowernumber='$bornum' and
244 my $sth=$dbh->prepare($query);
246 my $data=$sth->fetchrow_hashref;
248 $sth=$dbh->prepare("Select count(*) from issues where
249 borrowernumber='$bornum' and date_due < now() and returndate is NULL");
251 my $data2=$sth->fetchrow_hashref;
253 $sth=$dbh->prepare("Select sum(amountoutstanding) from accountlines where
254 borrowernumber='$bornum'");
256 my $data3=$sth->fetchrow_hashref;
259 return($data2->{'count(*)'},$data->{'count(*)'},$data3->{'sum(amountoutstanding)'});
264 my $dbh = C4::Context->dbh;
265 $data{'dateofbirth'}=format_date_in_iso($data{'dateofbirth'});
266 $data{'expiry'}=format_date_in_iso($data{'expiry'});
267 my $query="update borrowers set title='$data{'title'}',expiry='$data{'expiry'}',
268 cardnumber='$data{'cardnumber'}',sex='$data{'sex'}',ethnotes='$data{'ethnicnotes'}',
269 streetaddress='$data{'streetaddress'}',faxnumber='$data{'faxnumber'}',firstname='$data{'firstname'}',
270 altnotes='$data{'altnotes'}',dateofbirth='$data{'dateofbirth'}',contactname='$data{'contactname'}',
271 emailaddress='$data{'emailaddress'}',streetcity='$data{'streetcity'}',
272 altrelationship='$data{'altrelationship'}',othernames='$data{'othernames'}',phoneday='$data{'phoneday'}',
273 categorycode='$data{'categorycode'}',city='$data{'city'}',area='$data{'area'}',phone='$data{'phone'}',
274 borrowernotes='$data{'borrowernotes'}',altphone='$data{'altphone'}',surname='$data{'surname'}',
275 initials='$data{'initials'}',physstreet='$data{'physstreet'}',ethnicity='$data{'ethnicity'}',
276 gonenoaddress='$data{'gna'}',lost='$data{'lost'}',debarred='$data{'debarred'}',
277 textmessaging='$data{'textmessaging'}', branchcode = '$data{'branchcode'}',
278 zipcode = '$data{'zipcode'}',homezipcode='$data{'homezipcode'}', sort1='$data{'sort1'}', sort2='$data{'sort2'}'
279 where borrowernumber=$data{'borrowernumber'}";
280 my $sth=$dbh->prepare($query);
283 # ok if its an adult (type) it may have borrowers that depend on it as a guarantor
284 # so when we update information for an adult we should check for guarantees and update the relevant part
285 # of their records, ie addresses and phone numbers
286 if ($data{'categorycode'} eq 'A' || $data{'categorycode'} eq 'W'){
287 # is adult check guarantees;
288 updateguarantees(%data);
294 my $dbh = C4::Context->dbh;
295 $data{'userid'}='' unless $data{'password'};
296 $data{'password'}=md5_base64($data{'password'}) if $data{'password'};
297 $data{'dateofbirth'}=format_date_in_iso($data{'dateofbirth'});
298 $data{'dateenrolled'}=format_date_in_iso($data{'dateenrolled'});
299 $data{expiry}=format_date_in_iso($data{expiry});
300 my $query="insert into borrowers set cardnumber=".$dbh->quote($data{'cardnumber'}).
301 ",surname=".$dbh->quote($data{'surname'}).
302 ",firstname=".$dbh->quote($data{'firstname'}).
303 ",title=".$dbh->quote($data{'title'}).
304 ",othernames=".$dbh->quote($data{'othernames'}).
305 ",initials=".$dbh->quote($data{'initials'}).
306 ",streetnumber=".$dbh->quote($data{'streetnumber'}).
307 ",streettype=".$dbh->quote($data{'streettype'}).
308 ",address=".$dbh->quote($data{'address'}).
309 ",address2=".$dbh->quote($data{'address2'}).
310 ",zipcode=".$dbh->quote($data{'zipcode'}).
311 ",city=".$dbh->quote($data{'city'}).
312 ",phone=".$dbh->quote($data{'phone'}).
313 ",email=".$dbh->quote($data{'email'}).
314 ",mobile=".$dbh->quote($data{'mobile'}).
315 ",phonepro=".$dbh->quote($data{'phonepro'}).
316 ",opacnote=".$dbh->quote($data{'opacnote'}).
317 ",guarantorid=".$dbh->quote($data{'guarantorid'}).
318 ",dateofbirth=".$dbh->quote($data{'dateofbirth'}).
319 ",branchcode=".$dbh->quote($data{'branchcode'}).
320 ",categorycode=".$dbh->quote($data{'categorycode'}).
321 ",dateenrolled=".$dbh->quote($data{'dateenrolled'}).
322 ",contactname=".$dbh->quote($data{'contactname'}).
323 ",borrowernotes=".$dbh->quote($data{'borrowernotes'}).
324 ",dateexpiry=".$dbh->quote($data{'dateexpiry'}).
325 ",contactnote=".$dbh->quote($data{'contactnote'}).
326 ",b_address=".$dbh->quote($data{'b_address'}).
327 ",b_zipcode=".$dbh->quote($data{'b_zipcode'}).
328 ",b_city=".$dbh->quote($data{'b_city'}).
329 ",b_phone=".$dbh->quote($data{'b_phone'}).
330 ",b_email=".$dbh->quote($data{'b_email'},).
331 ",password=".$dbh->quote($data{'password'}).
332 ",userid=".$dbh->quote($data{'userid'}).
333 ",sort1=".$dbh->quote($data{'sort1'}).
334 ",sort2=".$dbh->quote($data{'sort2'}).
335 ",contacttitle=".$dbh->quote($data{'contacttitle'}).
336 ",emailpro=".$dbh->quote($data{'emailpro'}).
337 ",contactfirstname=".$dbh->quote($data{'contactfirstname'}).
338 ",sex=".$dbh->quote($data{'sex'}).
339 ",fax=".$dbh->quote($data{'fax'}).
340 ",flags=".$dbh->quote($data{'flags'}).
341 ",relationship=".$dbh->quote($data{'relationship'})
343 my $sth=$dbh->prepare($query);
346 $data{'borrowerid'} =$dbh->{'mysql_insertid'};
347 return $data{'borrowerid'};
351 my ($uid,$member,$digest) = @_;
352 my $dbh = C4::Context->dbh;
353 #Make sure the userid chosen is unique and not theirs if non-empty. If it is not,
354 #Then we need to tell the user and have them create a new one.
355 my $sth=$dbh->prepare("select * from borrowers where userid=? and borrowernumber != ?");
356 $sth->execute($uid,$member);
357 if ( ($uid ne '') && ($sth->fetchrow) ) {
360 #Everything is good so we can update the information.
361 $sth=$dbh->prepare("update borrowers set userid=?, password=? where borrowernumber=?");
362 $sth->execute($uid, $digest, $member);
367 sub getmemberfromuserid {
369 my $dbh = C4::Context->dbh;
370 my $sth = $dbh->prepare("select * from borrowers where userid=?");
371 $sth->execute($userid);
372 return $sth->fetchrow_hashref;
374 sub updateguarantees {
376 my $dbh = C4::Context->dbh;
377 my ($count,$guarantees)=findguarantees($data{'borrowernumber'});
378 for (my $i=0;$i<$count;$i++){
380 # It looks like the $i is only being returned to handle walking through
381 # the array, which is probably better done as a foreach loop.
383 my $guaquery="update borrowers set streetaddress='$data{'address'}',faxnumber='$data{'faxnumber'}',
384 streetcity='$data{'streetcity'}',phoneday='$data{'phoneday'}',city='$data{'city'}',area='$data{'area'}',phone='$data{'phone'}'
385 ,streetaddress='$data{'address'}'
386 where borrowernumber='$guarantees->[$i]->{'borrowernumber'}'";
387 my $sth3=$dbh->prepare($guaquery);
392 ################################################################################
394 =item fixup_cardnumber
396 Warning: The caller is responsible for locking the members table in write
397 mode, to avoid database corruption.
401 use vars qw( @weightings );
402 my @weightings = (8,4,6,3,5,2,1);
404 sub fixup_cardnumber ($) {
405 my($cardnumber) = @_;
406 my $autonumber_members = C4::Context->boolean_preference('autoMemberNum');
407 $autonumber_members = 0 unless defined $autonumber_members;
408 # Find out whether member numbers should be generated
409 # automatically. Should be either "1" or something else.
410 # Defaults to "0", which is interpreted as "no".
412 # if ($cardnumber !~ /\S/ && $autonumber_members) {
413 if ($autonumber_members) {
414 my $dbh = C4::Context->dbh;
415 if (C4::Context->preference('checkdigit') eq 'katipo') {
416 # if checkdigit is selected, calculate katipo-style cardnumber.
417 # otherwise, just use the max()
418 # purpose: generate checksum'd member numbers.
419 # We'll assume we just got the max value of digits 2-8 of member #'s
420 # from the database and our job is to increment that by one,
421 # determine the 1st and 9th digits and return the full string.
422 my $sth=$dbh->prepare("select max(substring(borrowers.cardnumber,2,7)) from borrowers");
425 my $data=$sth->fetchrow_hashref;
426 $cardnumber=$data->{'max(substring(borrowers.cardnumber,2,7))'};
428 if (! $cardnumber) { # If DB has no values,
429 $cardnumber = 1000000; # start at 1000000
435 for (my $i = 0; $i < 8; $i += 1) {
436 # read weightings, left to right, 1 char at a time
437 my $temp1 = $weightings[$i];
439 # sequence left to right, 1 char at a time
440 my $temp2 = substr($cardnumber,$i,1);
442 # mult each char 1-7 by its corresponding weighting
443 $sum += $temp1 * $temp2;
447 $rem = 'X' if $rem == 10;
449 $cardnumber="V$cardnumber$rem";
451 # MODIFIED BY JF: mysql4.1 allows casting as an integer, which is probably
452 # better. I'll leave the original in in case it needs to be changed for you
453 my $sth=$dbh->prepare("select max(cast(cardnumber as signed)) from borrowers");
454 #my $sth=$dbh->prepare("select max(borrowers.cardnumber) from borrowers");
458 my ($result)=$sth->fetchrow;
460 $cardnumber=$result+1;
468 my $dbh = C4::Context->dbh;
469 my $sth=$dbh->prepare("select cardnumber,borrowernumber from borrowers where
471 $sth->execute($bornum);
474 while (my $data=$sth->fetchrow_hashref){
484 $guarantor = &findguarantor($borrower_no);
485 $guarantor_cardno = $guarantor->{"cardnumber"};
486 $guarantor_surname = $guarantor->{"surname"};
489 C<&findguarantor> takes a borrower number (presumably that of a child
490 patron), finds the guarantor for C<$borrower_no> (the child's parent),
491 and returns the record for the guarantor.
493 C<&findguarantor> returns a reference-to-hash. Its keys are the fields
494 from the C<borrowers> database table;
500 my $dbh = C4::Context->dbh;
501 my $sth=$dbh->prepare("Select * from borrowers where borrowernumber=?");
502 $sth->execute($bornum);
503 my $data=$sth->fetchrow_hashref;
508 =item GuarantornameSearch
510 ($count, $borrowers) = &GuarantornameSearch($env, $searchstring, $type);
512 Looks up guarantor by name.
516 BUGFIX 499: C<$type> is now used to determine type of search.
517 if $type is "simple", search is performed on the first letter of the
520 C<$searchstring> is a space-separated list of search terms. Each term
521 must match the beginning a borrower's surname, first name, or other
524 C<&GuarantornameSearch> returns a two-element list. C<$borrowers> is a
525 reference-to-array; each element is a reference-to-hash, whose keys
526 are the fields of the C<borrowers> table in the Koha database.
527 C<$count> is the number of elements in C<$borrowers>.
529 return all info from guarantor =>only category_type A
533 #used by member enquiries from the intranet
534 #called by guarantor_search.pl
535 sub GuarantornameSearch {
536 my ($env,$searchstring,$orderby,$type)=@_;
537 my $dbh = C4::Context->dbh;
538 my $query = ""; my $count; my @data;
541 if($type eq "simple") # simple search for one letter only
543 $query="Select * from borrowers,categories where borrowers.categorycode=categories.categorycode and category_type='A' and surname like ? order by $orderby";
544 @bind=("$searchstring%");
546 else # advanced search looking in surname, firstname and othernames
548 @data=split(' ',$searchstring);
550 $query="Select * from borrowers,categories
551 where ((surname like ? or surname like ?
552 or firstname like ? or firstname like ?
553 or othernames like ? or othernames like ?) and borrowers.categorycode=categories.categorycode and category_type='A'
555 @bind=("$data[0]%","% $data[0]%","$data[0]%","% $data[0]%","$data[0]%","% $data[0]%");
556 for (my $i=1;$i<$count;$i++){
557 $query=$query." and (".
558 " surname like ? or surname like ?
559 or firstname like ? or firstname like ?
560 or othernames like ? or othernames like ?)";
561 push(@bind,"$data[$i]%","% $data[$i]%","$data[$i]%","% $data[$i]%","$data[$i]%","% $data[$i]%");
564 $query=$query.") or cardnumber like ?
566 push(@bind,$searchstring);
570 my $sth=$dbh->prepare($query);
571 $sth->execute(@bind);
574 while (my $data=$sth->fetchrow_hashref){
575 push(@results,$data);
579 return ($cnt,\@results);
582 =item NewBorrowerNumber
584 $num = &NewBorrowerNumber();
586 Allocates a new, unused borrower number, and returns it.
590 # FIXME - This is identical to C4::Circulation::Borrower::NewBorrowerNumber.
591 # Pick one and stick with it. Preferably use the other one. This function
592 # doesn't belong in C4::Search.
593 sub NewBorrowerNumber {
594 my $dbh = C4::Context->dbh;
595 my $sth=$dbh->prepare("Select max(borrowernumber) from borrowers");
597 my $data=$sth->fetchrow_hashref;
599 $data->{'max(borrowernumber)'}++;
600 return($data->{'max(borrowernumber)'});
605 ($count, $issues) = &borrissues($borrowernumber);
607 Looks up what the patron with the given borrowernumber has borrowed.
609 C<&borrissues> returns a two-element array. C<$issues> is a
610 reference-to-array, where each element is a reference-to-hash; the
611 keys are the fields from the C<issues>, C<biblio>, and C<items> tables
612 in the Koha database. C<$count> is the number of elements in
619 my $dbh = C4::Context->dbh;
620 my $sth=$dbh->prepare("Select * from issues,biblio,items where borrowernumber=?
621 and items.itemnumber=issues.itemnumber
622 and items.biblionumber=biblio.biblionumber
623 and issues.returndate is NULL order by date_due");
624 $sth->execute($bornum);
626 while (my $data = $sth->fetchrow_hashref) {
630 return(scalar(@result), \@result);
635 ($count, $issues) = &allissues($borrowernumber, $sortkey, $limit);
637 Looks up what the patron with the given borrowernumber has borrowed,
638 and sorts the results.
640 C<$sortkey> is the name of a field on which to sort the results. This
641 should be the name of a field in the C<issues>, C<biblio>,
642 C<biblioitems>, or C<items> table in the Koha database.
644 C<$limit> is the maximum number of results to return.
646 C<&allissues> returns a two-element array. C<$issues> is a
647 reference-to-array, where each element is a reference-to-hash; the
648 keys are the fields from the C<issues>, C<biblio>, C<biblioitems>, and
649 C<items> tables of the Koha database. C<$count> is the number of
650 elements in C<$issues>
655 my ($bornum,$order,$limit)=@_;
656 #FIXME: sanity-check order and limit
657 my $dbh = C4::Context->dbh;
658 my $query="Select * from issues,biblio,items,biblioitems
659 where borrowernumber=? and
660 items.biblioitemnumber=biblioitems.biblioitemnumber and
661 items.itemnumber=issues.itemnumber and
662 items.biblionumber=biblio.biblionumber order by $order";
664 $query.=" limit $limit";
667 my $sth=$dbh->prepare($query);
668 $sth->execute($bornum);
671 while (my $data=$sth->fetchrow_hashref){
679 =item getboracctrecord
681 ($count, $acctlines, $total) = &getboracctrecord($env, $borrowernumber);
683 Looks up accounting data for the patron with the given borrowernumber.
687 (FIXME - I'm not at all sure what this is about.)
689 C<&getboracctrecord> returns a three-element array. C<$acctlines> is a
690 reference-to-array, where each element is a reference-to-hash; the
691 keys are the fields of the C<accountlines> table in the Koha database.
692 C<$count> is the number of elements in C<$acctlines>. C<$total> is the
693 total amount outstanding for all of the account lines.
697 sub getboracctrecord {
698 my ($env,$params) = @_;
699 my $dbh = C4::Context->dbh;
702 my $sth=$dbh->prepare("Select * from accountlines where
703 borrowernumber=? order by date desc,timestamp desc");
705 $sth->execute($params->{'borrowernumber'});
707 while (my $data=$sth->fetchrow_hashref){
708 #FIXME before reinstating: insecure?
709 # if ($data->{'itemnumber'} ne ''){
710 # $query="Select * from items,biblio where items.itemnumber=
711 # '$data->{'itemnumber'}' and biblio.biblionumber=items.biblionumber";
712 # my $sth2=$dbh->prepare($query);
714 # my $data2=$sth2->fetchrow_hashref;
718 $acctlines[$numlines] = $data;
720 $total += $data->{'amountoutstanding'};
723 return ($numlines,\@acctlines,$total);
727 =head2 checkuniquemember (OUEST-PROVENCE)
729 $result = &checkuniquemember($collectivity,$surname,$categorycode,$firstname,$dateofbirth);
731 Checks that a member exists or not in the database.
733 C<&result> is 1 (=exist) or 0 (=does not exist)
734 C<&collectivity> is 1 (= we add a collectivity) or 0 (= we add a physical member)
735 C<&surname> is the surname
736 C<&categorycode> is from categorycode table
737 C<&firstname> is the firstname (only if collectivity=0)
738 C<&dateofbirth> is the date of birth (only if collectivity=0)
742 sub checkuniquemember{
743 my ($collectivity,$surname,$firstname,$dateofbirth)=@_;
744 my $dbh = C4::Context->dbh;
746 if ($collectivity ) {
747 # $request="select count(*) from borrowers where surname=? and categorycode=?";
748 $request="select borrowernumber,categorycode from borrowers where surname=? ";
750 # $request="select count(*) from borrowers where surname=? and categorycode=? and firstname=? and dateofbirth=?";
751 $request="select borrowernumber,categorycode from borrowers where surname=? and firstname=? and dateofbirth=?";
753 my $sth=$dbh->prepare($request);
755 $sth->execute(uc($surname));
757 $sth->execute(uc($surname),ucfirst($firstname),$dateofbirth);
759 my @data= $sth->fetchrow;
762 return $data[0],$data[1];
770 =head2 getzipnamecity (OUEST-PROVENCE)
772 take all info from table city for the fields city and zip
773 check for the name and the zip code of the city selected
778 my $dbh = C4::Context->dbh;
779 my $sth=$dbh->prepare("select city_name,city_zipcode from cities where cityid=? ");
780 $sth->execute($cityid);
781 my @data = $sth->fetchrow;
782 return $data[0],$data[1];
785 =head2 updatechildguarantor (OUEST-PROVENCE)
787 check for title,firstname,surname,adress,zip code and city from guarantor to
791 sub getguarantordata{
793 my $dbh = C4::Context->dbh;
794 my $sth=$dbh->prepare("Select title,firstname,surname,streetnumber,address,streettype,address2,zipcode,city,phone,phonepro,mobile,email,emailpro from borrowers where borrowernumber =? ");
795 $sth->execute($borrowerid);
796 my $guarantor_data=$sth->fetchrow_hashref;
798 return $guarantor_data;
801 =head2 getdcity (OUEST-PROVENCE)
802 recover cityid with city_name condition
806 my $dbh = C4::Context->dbh;
807 my $sth=$dbh->prepare("select cityid from cities where city_name=? ");
808 $sth->execute($city_name);
809 my $data = $sth->fetchrow;
814 =head2 getcategorytype (OUEST-PROVENCE)
816 check for the category_type with categorycode
817 and return the category_type
820 sub getcategorytype {
821 my ($categorycode)=@_;
822 my $dbh = C4::Context->dbh;
823 my $sth=$dbh->prepare("Select category_type,description from categories where categorycode=? ");
824 $sth->execute($categorycode);
825 my ($category_type,$description) = $sth->fetchrow;
826 return $category_type,$description;
830 my ($categorycode,$dateenrolled)=@_;
831 my $dbh=C4::Context->dbh;
832 my $sth = $dbh->prepare("select enrolmentperiod from categories where categorycode=?");
833 $sth->execute($categorycode);
834 my ($enrolmentperiod) = $sth->fetchrow;
835 $enrolmentperiod = 12 unless ($enrolmentperiod);
836 return format_date_in_iso(&DateCalc($dateenrolled,"$enrolmentperiod months"));
839 =head2 checkuserpassword (OUEST-PROVENCE)
841 check for the password and login are not used
842 return the number of record
843 0=> NOT USED 1=> USED
846 sub checkuserpassword{
847 my ($borrowerid,$userid,$password)=@_;
848 $password=md5_base64($password);
849 my $dbh = C4::Context->dbh;
850 my $sth=$dbh->prepare("Select count(*) from borrowers where borrowernumber !=? and userid =? and password=? ");
851 $sth->execute($borrowerid,$userid,$password);
852 my $number_rows=$sth->fetchrow;
857 =head2 borrowercategories
859 ($codes_arrayref, $labels_hashref) = &borrowercategories();
861 Looks up the different types of borrowers in the database. Returns two
862 elements: a reference-to-array, which lists the borrower category
863 codes, and a reference-to-hash, which maps the borrower category codes
864 to category descriptions.
868 sub borrowercategories {
869 my ($category_type,$action)=@_;
870 my $dbh = C4::Context->dbh;
872 $request="Select categorycode,description from categories where category_type=? order by categorycode";
873 my $sth=$dbh->prepare($request);
874 $sth->execute($category_type);
877 while (my $data=$sth->fetchrow_hashref){
878 push @codes,$data->{'categorycode'};
879 $labels{$data->{'categorycode'}}=$data->{'description'};
882 return(\@codes,\%labels);
885 =item getborrowercategory
887 $description = &getborrowercategory($categorycode);
889 Given the borrower's category code, the function returns the corresponding
890 description for a comprehensive information display.
894 sub getborrowercategory
897 my $dbh = C4::Context->dbh;
898 my $sth = $dbh->prepare("SELECT description FROM categories WHERE categorycode = ?");
899 $sth->execute($catcode);
900 my $description = $sth->fetchrow();
903 } # sub getborrowercategory
906 =head2 ethnicitycategories
908 ($codes_arrayref, $labels_hashref) = ðnicitycategories();
910 Looks up the different ethnic types in the database. Returns two
911 elements: a reference-to-array, which lists the ethnicity codes, and a
912 reference-to-hash, which maps the ethnicity codes to ethnicity
918 sub ethnicitycategories {
919 my $dbh = C4::Context->dbh;
920 my $sth=$dbh->prepare("Select code,name from ethnicity order by name");
924 while (my $data=$sth->fetchrow_hashref){
925 push @codes,$data->{'code'};
926 $labels{$data->{'code'}}=$data->{'name'};
929 return(\@codes,\%labels);
934 $ethn_name = &fixEthnicity($ethn_code);
936 Takes an ethnicity code (e.g., "european" or "pi") and returns the
937 corresponding descriptive name from the C<ethnicity> table in the
938 Koha database ("European" or "Pacific Islander").
943 sub fixEthnicity($) {
945 my $ethnicity = shift;
946 my $dbh = C4::Context->dbh;
947 my $sth=$dbh->prepare("Select name from ethnicity where code = ?");
948 $sth->execute($ethnicity);
949 my $data=$sth->fetchrow_hashref;
951 return $data->{'name'};
954 =head2 get_institutions
956 $insitutions = get_institutions();
958 Just returns a list of all the borrowers of type I, borrownumber and name
963 sub get_institutions {
964 my $dbh = C4::Context->dbh();
965 my $sth = $dbh->prepare("SELECT borrowernumber,surname FROM borrowers WHERE categorycode=? ORDER BY surname");
968 while (my $data = $sth->fetchrow_hashref()){
969 $orgs{$data->{'borrowernumber'}}=$data;
974 } # sub get_institutions