+
+=head2 checkuniquemember (OUEST-PROVENCE)
+
+ $result = &checkuniquemember($collectivity,$surname,$categorycode,$firstname,$dateofbirth);
+
+Checks that a member exists or not in the database.
+
+C<&result> is 1 (=exist) or 0 (=does not exist)
+C<&collectivity> is 1 (= we add a collectivity) or 0 (= we add a physical member)
+C<&surname> is the surname
+C<&categorycode> is from categorycode table
+C<&firstname> is the firstname (only if collectivity=0)
+C<&dateofbirth> is the date of birth (only if collectivity=0)
+
+=cut
+
+sub checkuniquemember{
+ my ($collectivity,$surname,$firstname,$dateofbirth)=@_;
+ my $dbh = C4::Context->dbh;
+ my $request;
+ if ($collectivity ) {
+# $request="select count(*) from borrowers where surname=? and categorycode=?";
+ $request="select borrowernumber,categorycode from borrowers where surname=? ";
+ } else {
+# $request="select count(*) from borrowers where surname=? and categorycode=? and firstname=? and dateofbirth=?";
+ $request="select borrowernumber,categorycode from borrowers where surname=? and firstname=? and dateofbirth=?";
+ }
+ my $sth=$dbh->prepare($request);
+ if ($collectivity) {
+ $sth->execute(uc($surname));
+ } else {
+ $sth->execute(uc($surname),ucfirst($firstname),$dateofbirth);
+ }
+ my @data= $sth->fetchrow;
+ if ($data[0]){
+ $sth->finish;
+ return $data[0],$data[1];
+#
+ }else{
+ $sth->finish;
+ return 0;
+ }
+}
+
+=head2 getzipnamecity (OUEST-PROVENCE)
+
+take all info from table city for the fields city and zip
+check for the name and the zip code of the city selected
+
+=cut
+sub getzipnamecity {
+ my ($cityid)=@_;
+ my $dbh = C4::Context->dbh;
+ my $sth=$dbh->prepare("select city_name,city_zipcode from cities where cityid=? ");
+ $sth->execute($cityid);
+ my @data = $sth->fetchrow;
+ return $data[0],$data[1];
+}
+
+=head2 updatechildguarantor (OUEST-PROVENCE)
+
+check for title,firstname,surname,adress,zip code and city from guarantor to
+guarantorchild
+
+=cut
+#'
+
+sub getguarantordata{
+ my ($borrowerid)=@_;
+ my $dbh = C4::Context->dbh;
+ my $sth=$dbh->prepare("Select title,firstname,surname,streetnumber,address,streettype,address2,zipcode,city,phone,phonepro,mobile,email,emailpro from borrowers where borrowernumber =? ");
+ $sth->execute($borrowerid);
+ my $guarantor_data=$sth->fetchrow_hashref;
+ $sth->finish;
+ return $guarantor_data;
+}
+
+=head2 getdcity (OUEST-PROVENCE)
+recover cityid with city_name condition
+=cut
+
+sub getidcity {
+ my ($city_name)=@_;
+ my $dbh = C4::Context->dbh;
+ my $sth=$dbh->prepare("select cityid from cities where city_name=? ");
+ $sth->execute($city_name);
+ my $data = $sth->fetchrow;
+ return $data;
+}
+
+
+=head2 getcategorytype (OUEST-PROVENCE)
+
+check for the category_type with categorycode
+and return the category_type
+
+=cut
+sub getcategorytype {
+ my ($categorycode)=@_;
+ my $dbh = C4::Context->dbh;
+ my $sth=$dbh->prepare("Select category_type,description from categories where categorycode=? ");
+ $sth->execute($categorycode);
+ my ($category_type,$description) = $sth->fetchrow;
+ return $category_type,$description;
+}
+
+sub calcexpirydate {
+ my ($categorycode,$dateenrolled)=@_;
+ my $dbh=C4::Context->dbh;
+ my $sth = $dbh->prepare("select enrolmentperiod from categories where categorycode=?");
+ $sth->execute($categorycode);
+ my ($enrolmentperiod) = $sth->fetchrow;
+ $enrolmentperiod = 12 unless ($enrolmentperiod);
+ return format_date_in_iso(&DateCalc($dateenrolled,"$enrolmentperiod months"));
+}
+
+=head2 checkuserpassword (OUEST-PROVENCE)
+
+check for the password and login are not used
+return the number of record
+0=> NOT USED 1=> USED
+
+=cut
+sub checkuserpassword{
+ my ($borrowerid,$userid,$password)=@_;
+ $password=md5_base64($password);
+ my $dbh = C4::Context->dbh;
+ my $sth=$dbh->prepare("Select count(*) from borrowers where borrowernumber !=? and userid =? and password=? ");
+ $sth->execute($borrowerid,$userid,$password);
+ my $number_rows=$sth->fetchrow;
+ return $number_rows;
+
+}
+
+=head2 borrowercategories
+
+ ($codes_arrayref, $labels_hashref) = &borrowercategories();
+
+Looks up the different types of borrowers in the database. Returns two
+elements: a reference-to-array, which lists the borrower category
+codes, and a reference-to-hash, which maps the borrower category codes
+to category descriptions.
+
+=cut
+#'
+sub borrowercategories {
+ my ($category_type,$action)=@_;
+ my $dbh = C4::Context->dbh;
+ my $request;
+ $request="Select categorycode,description from categories where category_type=? order by categorycode";
+ my $sth=$dbh->prepare($request);
+ $sth->execute($category_type);
+ my %labels;
+ my @codes;
+ while (my $data=$sth->fetchrow_hashref){
+ push @codes,$data->{'categorycode'};
+ $labels{$data->{'categorycode'}}=$data->{'description'};
+ }
+ $sth->finish;
+ return(\@codes,\%labels);
+}
+
+=head2 getborrowercategory
+
+ $description = &getborrowercategory($categorycode);
+
+Given the borrower's category code, the function returns the corresponding
+description for a comprehensive information display.
+
+=cut
+
+sub getborrowercategory
+{
+ my ($catcode) = @_;
+ my $dbh = C4::Context->dbh;
+ my $sth = $dbh->prepare("SELECT description FROM categories WHERE categorycode = ?");
+ $sth->execute($catcode);
+ my $description = $sth->fetchrow();
+ $sth->finish();
+ return $description;
+} # sub getborrowercategory
+
+
+=head2 ethnicitycategories
+
+ ($codes_arrayref, $labels_hashref) = ðnicitycategories();
+
+Looks up the different ethnic types in the database. Returns two
+elements: a reference-to-array, which lists the ethnicity codes, and a
+reference-to-hash, which maps the ethnicity codes to ethnicity
+descriptions.
+
+=cut
+#'
+
+sub ethnicitycategories {
+ my $dbh = C4::Context->dbh;
+ my $sth=$dbh->prepare("Select code,name from ethnicity order by name");
+ $sth->execute;
+ my %labels;
+ my @codes;
+ while (my $data=$sth->fetchrow_hashref){
+ push @codes,$data->{'code'};
+ $labels{$data->{'code'}}=$data->{'name'};
+ }
+ $sth->finish;
+ return(\@codes,\%labels);
+}
+
+=head2 fixEthnicity
+
+ $ethn_name = &fixEthnicity($ethn_code);
+
+Takes an ethnicity code (e.g., "european" or "pi") and returns the
+corresponding descriptive name from the C<ethnicity> table in the
+Koha database ("European" or "Pacific Islander").
+
+=cut
+#'
+
+sub fixEthnicity($) {
+
+ my $ethnicity = shift;
+ my $dbh = C4::Context->dbh;
+ my $sth=$dbh->prepare("Select name from ethnicity where code = ?");
+ $sth->execute($ethnicity);
+ my $data=$sth->fetchrow_hashref;
+ $sth->finish;
+ return $data->{'name'};
+} # sub fixEthnicity
+
+=head2 get_institutions
+
+ $insitutions = get_institutions();
+
+Just returns a list of all the borrowers of type I, borrownumber and name
+
+=cut
+#'
+
+sub get_institutions {
+ my $dbh = C4::Context->dbh();
+ my $sth = $dbh->prepare("SELECT borrowernumber,surname FROM borrowers WHERE categorycode=? ORDER BY surname");
+ $sth->execute('I');
+ my %orgs;
+ while (my $data = $sth->fetchrow_hashref()){
+ $orgs{$data->{'borrowernumber'}}=$data;
+ }
+ $sth->finish();
+ return(\%orgs);
+
+} # sub get_institutions
+
+=head2 add_member_orgs
+
+ add_member_orgs($borrowernumber,$borrowernumbers);
+
+Takes a borrowernumber and a list of other borrowernumbers and inserts them into the borrowers_to_borrowers table
+
+=cut
+#'
+sub add_member_orgs {
+ my ($borrowernumber,$otherborrowers) = @_;
+ my $dbh = C4::Context->dbh();
+ my $query = "INSERT INTO borrowers_to_borrowers (borrower1,borrower2) VALUES (?,?)";
+ my $sth = $dbh->prepare($query);
+ foreach my $bornum (@$otherborrowers){
+ $sth->execute($borrowernumber,$bornum);
+ }
+ $sth->finish();
+
+} # sub add_member_orgs