move ReservesMaxPickUpDelay to Holds Policy on Circ Prefs
[koha.git] / C4 / Branch.pm
bloba6b1a3aef00ce07213b149f0ad1b9f254b1f8afc
1 package C4::Branch;
3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 2 of the License, or (at your option) any later
8 # version.
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License along with
15 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
16 # Suite 330, Boston, MA 02111-1307 USA
19 use strict;
20 require Exporter;
21 use C4::Context;
22 use C4::Koha;
24 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
26 BEGIN {
27 # set the version for version checking
28 $VERSION = 3.02;
29 @ISA = qw(Exporter);
30 @EXPORT = qw(
31 &GetBranchCategory
32 &GetBranchName
33 &GetBranch
34 &GetBranches
35 &GetBranchesLoop
36 &GetBranchDetail
37 &get_branchinfos_of
38 &ModBranch
39 &CheckBranchCategorycode
40 &GetBranchInfo
41 &GetCategoryTypes
42 &GetBranchCategories
43 &GetBranchesInCategory
44 &ModBranchCategoryInfo
45 &DelBranch
46 &DelBranchCategory
47 &CheckCategoryUnique
49 @EXPORT_OK = qw( &onlymine &mybranch get_branch_code_from_name );
52 =head1 NAME
54 C4::Branch - Koha branch module
56 =head1 SYNOPSIS
58 use C4::Branch;
60 =head1 DESCRIPTION
62 The functions in this module deal with branches.
64 =head1 FUNCTIONS
66 =head2 GetBranches
68 $branches = &GetBranches();
70 Returns informations about ALL branches, IndependantBranches Insensitive.
71 GetBranchInfo() returns the same information without the problems of this function
72 (namespace collision, mainly).
73 Create a branch selector with the following code.
75 =head3 in PERL SCRIPT
77 my $branches = GetBranches;
78 my @branchloop;
79 foreach my $thisbranch (sort keys %$branches) {
80 my $selected = 1 if $thisbranch eq $branch;
81 my %row =(value => $thisbranch,
82 selected => $selected,
83 branchname => $branches->{$thisbranch}->{branchname},
85 push @branchloop, \%row;
88 =head3 in TEMPLATE
90 <select name="branch">
91 <option value="">Default</option>
92 <!-- TMPL_LOOP name="branchloop" -->
93 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="branchname" --></option>
94 <!-- /TMPL_LOOP -->
95 </select>
97 =head4 Note that you often will want to just use GetBranchesLoop, for exactly the example above.
99 =cut
101 sub GetBranches {
102 my ($onlymine)=@_;
103 # returns a reference to a hash of references to ALL branches...
104 my %branches;
105 my $dbh = C4::Context->dbh;
106 my $sth;
107 my $query="SELECT * FROM branches";
108 my @bind_parameters;
109 if ($onlymine && C4::Context->userenv && C4::Context->userenv->{branch}){
110 $query .= ' WHERE branchcode = ? ';
111 push @bind_parameters, C4::Context->userenv->{branch};
113 $query.=" ORDER BY branchname";
114 $sth = $dbh->prepare($query);
115 $sth->execute( @bind_parameters );
117 my $nsth = $dbh->prepare(
118 "SELECT categorycode FROM branchrelations WHERE branchcode = ?"
119 ); # prepare once, outside while loop
121 while ( my $branch = $sth->fetchrow_hashref ) {
122 $nsth->execute( $branch->{'branchcode'} );
123 while ( my ($cat) = $nsth->fetchrow_array ) {
124 # FIXME - This seems wrong. It ought to be
125 # $branch->{categorycodes}{$cat} = 1;
126 # otherwise, there's a namespace collision if there's a
127 # category with the same name as a field in the 'branches'
128 # table (i.e., don't create a category called "issuing").
129 # In addition, the current structure doesn't really allow
130 # you to list the categories that a branch belongs to:
131 # you'd have to list keys %$branch, and remove those keys
132 # that aren't fields in the "branches" table.
133 # $branch->{$cat} = 1;
134 $branch->{category}{$cat} = 1;
136 $branches{ $branch->{'branchcode'} } = $branch;
138 return ( \%branches );
141 sub onlymine {
142 return
143 C4::Context->preference('IndependantBranches') &&
144 C4::Context->userenv &&
145 C4::Context->userenv->{flags} %2 != 1 &&
146 C4::Context->userenv->{branch} ;
149 # always returns a string for OK comparison via "eq" or "ne"
150 sub mybranch {
151 C4::Context->userenv or return '';
152 return C4::Context->userenv->{branch} || '';
155 sub GetBranchesLoop (;$$) { # since this is what most pages want anyway
156 my $branch = @_ ? shift : mybranch(); # optional first argument is branchcode of "my branch", if preselection is wanted.
157 my $onlymine = @_ ? shift : onlymine();
158 my $branches = GetBranches($onlymine);
159 my @loop;
160 my $searchMyLibraryFirst = C4::Context->preference("SearchMyLibraryFirst");;
161 foreach (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches) {
162 push @loop, {
163 value => $_,
164 selected => (($_ eq $branch) && $searchMyLibraryFirst ) ? 1 : 0,
165 branchname => $branches->{$_}->{branchname},
168 return \@loop;
171 =head2 GetBranchName
173 =cut
175 sub GetBranchName {
176 my ($branchcode) = @_;
177 my $dbh = C4::Context->dbh;
178 my $sth;
179 $sth = $dbh->prepare("Select branchname from branches where branchcode=?");
180 $sth->execute($branchcode);
181 my $branchname = $sth->fetchrow_array;
182 $sth->finish;
183 return ($branchname);
186 =head2 ModBranch
188 $error = &ModBranch($newvalue);
190 This function modify an existing branch
192 C<$newvalue> is a ref to an array wich is containt all the column from branches table.
194 =cut
196 sub ModBranch {
197 my ($data) = @_;
199 my $dbh = C4::Context->dbh;
200 if ($data->{add}) {
201 my $query = "
202 INSERT INTO branches
203 (branchcode,branchname,branchaddress1,
204 branchaddress2,branchaddress3,branchzip,branchcity,
205 branchcountry,branchphone,branchfax,branchemail,
206 branchurl,branchip,branchprinter,branchnotes)
207 VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
209 my $sth = $dbh->prepare($query);
210 $sth->execute(
211 $data->{'branchcode'}, $data->{'branchname'},
212 $data->{'branchaddress1'}, $data->{'branchaddress2'},
213 $data->{'branchaddress3'}, $data->{'branchzip'},
214 $data->{'branchcity'}, $data->{'branchcountry'},
215 $data->{'branchphone'}, $data->{'branchfax'},
216 $data->{'branchemail'}, $data->{'branchurl'},
217 $data->{'branchip'}, $data->{'branchprinter'},
218 $data->{'branchnotes'},
220 return 1 if $dbh->err;
221 } else {
222 my $query = "
223 UPDATE branches
224 SET branchname=?,branchaddress1=?,
225 branchaddress2=?,branchaddress3=?,branchzip=?,
226 branchcity=?,branchcountry=?,branchphone=?,
227 branchfax=?,branchemail=?,branchurl=?,branchip=?,
228 branchprinter=?,branchnotes=?
229 WHERE branchcode=?
231 my $sth = $dbh->prepare($query);
232 $sth->execute(
233 $data->{'branchname'},
234 $data->{'branchaddress1'}, $data->{'branchaddress2'},
235 $data->{'branchaddress3'}, $data->{'branchzip'},
236 $data->{'branchcity'}, $data->{'branchcountry'},
237 $data->{'branchphone'}, $data->{'branchfax'},
238 $data->{'branchemail'}, $data->{'branchurl'},
239 $data->{'branchip'}, $data->{'branchprinter'},
240 $data->{'branchnotes'},
241 $data->{'branchcode'},
244 # sort out the categories....
245 my @checkedcats;
246 my $cats = GetBranchCategory();
247 foreach my $cat (@$cats) {
248 my $code = $cat->{'categorycode'};
249 if ( $data->{$code} ) {
250 push( @checkedcats, $code );
253 my $branchcode = uc( $data->{'branchcode'} );
254 my $branch = GetBranchInfo($branchcode);
255 $branch = $branch->[0];
256 my $branchcats = $branch->{'categories'};
257 my @addcats;
258 my @removecats;
259 foreach my $bcat (@$branchcats) {
261 unless ( grep { /^$bcat$/ } @checkedcats ) {
262 push( @removecats, $bcat );
265 foreach my $ccat (@checkedcats) {
266 unless ( grep { /^$ccat$/ } @$branchcats ) {
267 push( @addcats, $ccat );
270 foreach my $cat (@addcats) {
271 my $sth =
272 $dbh->prepare(
273 "insert into branchrelations (branchcode, categorycode) values(?, ?)"
275 $sth->execute( $branchcode, $cat );
276 $sth->finish;
278 foreach my $cat (@removecats) {
279 my $sth =
280 $dbh->prepare(
281 "delete from branchrelations where branchcode=? and categorycode=?"
283 $sth->execute( $branchcode, $cat );
284 $sth->finish;
288 =head2 GetBranchCategory
290 $results = GetBranchCategory($categorycode);
292 C<$results> is an ref to an array.
294 =cut
296 sub GetBranchCategory {
298 # returns a reference to an array of hashes containing branches,
299 my ($catcode) = @_;
300 my $dbh = C4::Context->dbh;
301 my $sth;
303 # print DEBUG "GetBranchCategory: entry: catcode=".cvs($catcode)."\n";
304 if ($catcode) {
305 $sth =
306 $dbh->prepare(
307 "select * from branchcategories where categorycode = ?");
308 $sth->execute($catcode);
310 else {
311 $sth = $dbh->prepare("Select * from branchcategories");
312 $sth->execute();
314 my @results;
315 while ( my $data = $sth->fetchrow_hashref ) {
316 push( @results, $data );
318 $sth->finish;
320 # print DEBUG "GetBranchCategory: exit: returning ".cvs(\@results)."\n";
321 return \@results;
324 =head2 GetBranchCategories
326 my $categories = GetBranchCategories($branchcode,$categorytype);
328 Returns a list ref of anon hashrefs with keys eq columns of branchcategories table,
329 i.e. categorycode, categorydescription, categorytype, categoryname.
330 if $branchcode and/or $categorytype are passed, limit set to categories that
331 $branchcode is a member of , and to $categorytype.
333 =cut
335 sub GetBranchCategories {
336 my ($branchcode,$categorytype) = @_;
337 my $dbh = C4::Context->dbh();
338 my $query = "SELECT c.* FROM branchcategories c";
339 my (@where, @bind);
340 if($branchcode) {
341 $query .= ",branchrelations r, branches b ";
342 push @where, "c.categorycode=r.categorycode and r.branchcode=? ";
343 push @bind , $branchcode;
345 if ($categorytype) {
346 push @where, " c.categorytype=? ";
347 push @bind, $categorytype;
349 $query .= " where " . join(" and ", @where) if(@where);
350 $query .= " order by categorytype,c.categorycode";
351 my $sth=$dbh->prepare( $query);
352 $sth->execute(@bind);
354 my $branchcats = $sth->fetchall_arrayref({});
355 $sth->finish();
356 return( $branchcats );
359 =head2 GetCategoryTypes
361 $categorytypes = GetCategoryTypes;
362 returns a list of category types.
363 Currently these types are HARDCODED.
364 type: 'searchdomain' defines a group of agencies that the calling library may search in.
365 Other usage of agency categories falls under type: 'properties'.
366 to allow for other uses of categories.
367 The searchdomain bit may be better implemented as a separate module, but
368 the categories were already here, and minimally used.
369 =cut
371 #TODO manage category types. rename possibly to 'agency domains' ? as borrowergroups are called categories.
372 sub GetCategoryTypes() {
373 return ( 'searchdomain','properties');
376 =head2 GetBranch
378 $branch = GetBranch( $query, $branches );
380 =cut
382 sub GetBranch ($$) {
383 my ( $query, $branches ) = @_; # get branch for this query from branches
384 my $branch = $query->param('branch');
385 my %cookie = $query->cookie('userenv');
386 ($branch) || ($branch = $cookie{'branchname'});
387 ( $branches->{$branch} ) || ( $branch = ( keys %$branches )[0] );
388 return $branch;
391 =head2 GetBranchDetail
393 $branch = &GetBranchDetail($branchcode);
395 Given the branch code, the function returns a
396 hashref for the corresponding row in the branches table.
398 =cut
400 sub GetBranchDetail {
401 my ($branchcode) = shift or return;
402 my $sth = C4::Context->dbh->prepare("SELECT * FROM branches WHERE branchcode = ?");
403 $sth->execute($branchcode);
404 return $sth->fetchrow_hashref();
407 =head2 get_branchinfos_of
409 my $branchinfos_of = get_branchinfos_of(@branchcodes);
411 Associates a list of branchcodes to the information of the branch, taken in
412 branches table.
414 Returns a href where keys are branchcodes and values are href where keys are
415 branch information key.
417 print 'branchname is ', $branchinfos_of->{$code}->{branchname};
419 =cut
421 sub get_branchinfos_of {
422 my @branchcodes = @_;
424 my $query = '
425 SELECT branchcode,
426 branchname
427 FROM branches
428 WHERE branchcode IN ('
429 . join( ',', map( { "'" . $_ . "'" } @branchcodes ) ) . ')
431 return C4::Koha::get_infos_of( $query, 'branchcode' );
435 =head2 GetBranchesInCategory
437 my $branches = GetBranchesInCategory($categorycode);
439 Returns a href: keys %$branches eq (branchcode,branchname) .
441 =cut
443 sub GetBranchesInCategory($) {
444 my ($categorycode) = @_;
445 my @branches;
446 my $dbh = C4::Context->dbh();
447 my $sth=$dbh->prepare( "SELECT b.branchcode FROM branchrelations r, branches b
448 where r.branchcode=b.branchcode and r.categorycode=?");
449 $sth->execute($categorycode);
450 while (my $branch = $sth->fetchrow) {
451 push @branches, $branch;
453 $sth->finish();
454 return( \@branches );
457 =head2 GetBranchInfo
459 $results = GetBranchInfo($branchcode);
461 returns C<$results>, a reference to an array of hashes containing branches.
462 if $branchcode, just this branch, with associated categories.
463 if ! $branchcode && $categorytype, all branches in the category.
464 =cut
466 sub GetBranchInfo {
467 my ($branchcode,$categorytype) = @_;
468 my $dbh = C4::Context->dbh;
469 my $sth;
472 if ($branchcode) {
473 $sth =
474 $dbh->prepare(
475 "Select * from branches where branchcode = ? order by branchcode");
476 $sth->execute($branchcode);
478 else {
479 $sth = $dbh->prepare("Select * from branches order by branchcode");
480 $sth->execute();
482 my @results;
483 while ( my $data = $sth->fetchrow_hashref ) {
484 my @bind = ($data->{'branchcode'});
485 my $query= "select r.categorycode from branchrelations r";
486 $query .= ", branchcategories c " if($categorytype);
487 $query .= " where branchcode=? ";
488 if($categorytype) {
489 $query .= " and c.categorytype=? and r.categorycode=c.categorycode";
490 push @bind, $categorytype;
492 my $nsth=$dbh->prepare($query);
493 $nsth->execute( @bind );
494 my @cats = ();
495 while ( my ($cat) = $nsth->fetchrow_array ) {
496 push( @cats, $cat );
498 $nsth->finish;
499 $data->{'categories'} = \@cats;
500 push( @results, $data );
502 $sth->finish;
503 return \@results;
506 =head2 DelBranch
508 &DelBranch($branchcode);
510 =cut
512 sub DelBranch {
513 my ($branchcode) = @_;
514 my $dbh = C4::Context->dbh;
515 my $sth = $dbh->prepare("delete from branches where branchcode = ?");
516 $sth->execute($branchcode);
517 $sth->finish;
520 =head2 ModBranchCategoryInfo
522 &ModBranchCategoryInfo($data);
523 sets the data from the editbranch form, and writes to the database...
525 =cut
527 sub ModBranchCategoryInfo {
528 my ($data) = @_;
529 my $dbh = C4::Context->dbh;
530 if ($data->{'add'}){
531 # we are doing an insert
532 my $sth = $dbh->prepare("INSERT INTO branchcategories (categorycode,categoryname,codedescription,categorytype) VALUES (?,?,?,?)");
533 $sth->execute(uc( $data->{'categorycode'} ),$data->{'categoryname'}, $data->{'codedescription'},$data->{'categorytype'} );
534 $sth->finish();
536 else {
537 # modifying
538 my $sth = $dbh->prepare("UPDATE branchcategories SET categoryname=?,codedescription=?,categorytype=? WHERE categorycode=?");
539 $sth->execute($data->{'categoryname'}, $data->{'codedescription'},$data->{'categorytype'},uc( $data->{'categorycode'} ) );
540 $sth->finish();
544 =head2 CheckCategoryUnique
546 if (CheckCategoryUnique($categorycode)){
547 # do something
550 =cut
552 sub CheckCategoryUnique {
553 my $categorycode = shift;
554 my $dbh = C4::Context->dbh;
555 my $sth = $dbh->prepare("SELECT categorycode FROM branchcategories WHERE categorycode = ?");
556 $sth->execute(uc( $categorycode) );
557 if (my $data = $sth->fetchrow_hashref){
558 return 0;
560 else {
561 return 1;
566 =head2 DeleteBranchCategory
568 DeleteBranchCategory($categorycode);
570 =cut
572 sub DelBranchCategory {
573 my ($categorycode) = @_;
574 my $dbh = C4::Context->dbh;
575 my $sth = $dbh->prepare("delete from branchcategories where categorycode = ?");
576 $sth->execute($categorycode);
577 $sth->finish;
580 =head2 CheckBranchCategorycode
582 $number_rows_affected = CheckBranchCategorycode($categorycode);
584 =cut
586 sub CheckBranchCategorycode {
588 # check to see if the branchcode is being used in the database somewhere....
589 my ($categorycode) = @_;
590 my $dbh = C4::Context->dbh;
591 my $sth =
592 $dbh->prepare(
593 "select count(*) from branchrelations where categorycode=?");
594 $sth->execute($categorycode);
595 my ($total) = $sth->fetchrow_array;
596 return $total;
599 sub get_branch_code_from_name {
600 my @branch_name = @_;
601 my $query = "SELECT branchcode FROM branches WHERE branchname=?;";
602 my $dbh = C4::Context->dbh();
603 my $sth = $dbh->prepare($query);
604 $sth->execute(@branch_name);
605 return $sth->fetchrow_array;
609 __END__
611 =head1 AUTHOR
613 Koha Developement team <info@koha.org>
615 =cut