replacing links to bookfunds into budgets
[koha.git] / C4 / Branch.pm
blob18177ec6513b18c71173ce36676daccc3a584ae8
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
48 @EXPORT_OK = qw( &onlymine &mybranch get_branch_code_from_name );
51 =head1 NAME
53 C4::Branch - Koha branch module
55 =head1 SYNOPSIS
57 use C4::Branch;
59 =head1 DESCRIPTION
61 The functions in this module deal with branches.
63 =head1 FUNCTIONS
65 =head2 GetBranches
67 $branches = &GetBranches();
69 Returns informations about ALL branches, IndependantBranches Insensitive.
70 GetBranchInfo() returns the same information without the problems of this function
71 (namespace collision, mainly).
72 Create a branch selector with the following code.
74 =head3 in PERL SCRIPT
76 my $branches = GetBranches;
77 my @branchloop;
78 foreach my $thisbranch (sort keys %$branches) {
79 my $selected = 1 if $thisbranch eq $branch;
80 my %row =(value => $thisbranch,
81 selected => $selected,
82 branchname => $branches->{$thisbranch}->{branchname},
84 push @branchloop, \%row;
87 =head3 in TEMPLATE
89 <select name="branch">
90 <option value="">Default</option>
91 <!-- TMPL_LOOP name="branchloop" -->
92 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="branchname" --></option>
93 <!-- /TMPL_LOOP -->
94 </select>
96 =head4 Note that you often will want to just use GetBranchesLoop, for exactly the example above.
98 =cut
100 sub GetBranches {
101 my ($onlymine)=@_;
102 # returns a reference to a hash of references to ALL branches...
103 my %branches;
104 my $dbh = C4::Context->dbh;
105 my $sth;
106 my $query="SELECT * FROM branches";
107 my @bind_parameters;
108 if ($onlymine && C4::Context->userenv && C4::Context->userenv->{branch}){
109 $query .= ' WHERE branchcode = ? ';
110 push @bind_parameters, C4::Context->userenv->{branch};
112 $query.=" ORDER BY branchname";
113 $sth = $dbh->prepare($query);
114 $sth->execute( @bind_parameters );
116 my $nsth = $dbh->prepare(
117 "SELECT categorycode FROM branchrelations WHERE branchcode = ?"
118 ); # prepare once, outside while loop
120 while ( my $branch = $sth->fetchrow_hashref ) {
121 $nsth->execute( $branch->{'branchcode'} );
122 while ( my ($cat) = $nsth->fetchrow_array ) {
123 # FIXME - This seems wrong. It ought to be
124 # $branch->{categorycodes}{$cat} = 1;
125 # otherwise, there's a namespace collision if there's a
126 # category with the same name as a field in the 'branches'
127 # table (i.e., don't create a category called "issuing").
128 # In addition, the current structure doesn't really allow
129 # you to list the categories that a branch belongs to:
130 # you'd have to list keys %$branch, and remove those keys
131 # that aren't fields in the "branches" table.
132 # $branch->{$cat} = 1;
133 $branch->{category}{$cat} = 1;
135 $branches{ $branch->{'branchcode'} } = $branch;
137 return ( \%branches );
140 sub onlymine {
141 return
142 C4::Context->preference('IndependantBranches') &&
143 C4::Context->userenv &&
144 C4::Context->userenv->{flags} %2 != 1 &&
145 C4::Context->userenv->{branch} ;
148 # always returns a string for OK comparison via "eq" or "ne"
149 sub mybranch {
150 C4::Context->userenv or return '';
151 return C4::Context->userenv->{branch} || '';
154 sub GetBranchesLoop (;$$) { # since this is what most pages want anyway
155 my $branch = @_ ? shift : mybranch(); # optional first argument is branchcode of "my branch", if preselection is wanted.
156 my $onlymine = @_ ? shift : onlymine();
157 my $branches = GetBranches($onlymine);
158 my @loop;
159 foreach (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches) {
160 push @loop, {
161 value => $_,
162 selected => ($_ eq $branch) ? 1 : 0,
163 branchname => $branches->{$_}->{branchname},
166 return \@loop;
169 =head2 GetBranchName
171 =cut
173 sub GetBranchName {
174 my ($branchcode) = @_;
175 my $dbh = C4::Context->dbh;
176 my $sth;
177 $sth = $dbh->prepare("Select branchname from branches where branchcode=?");
178 $sth->execute($branchcode);
179 my $branchname = $sth->fetchrow_array;
180 $sth->finish;
181 return ($branchname);
184 =head2 ModBranch
186 $error = &ModBranch($newvalue);
188 This function modify an existing branch
190 C<$newvalue> is a ref to an array wich is containt all the column from branches table.
192 =cut
194 sub ModBranch {
195 my ($data) = @_;
197 my $dbh = C4::Context->dbh;
198 if ($data->{add}) {
199 my $query = "
200 INSERT INTO branches
201 (branchcode,branchname,branchaddress1,
202 branchaddress2,branchaddress3,branchzip,branchcity,
203 branchcountry,branchphone,branchfax,branchemail,
204 branchurl,branchip,branchprinter,branchnotes)
205 VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
207 my $sth = $dbh->prepare($query);
208 $sth->execute(
209 $data->{'branchcode'}, $data->{'branchname'},
210 $data->{'branchaddress1'}, $data->{'branchaddress2'},
211 $data->{'branchaddress3'}, $data->{'branchzip'},
212 $data->{'branchcity'}, $data->{'branchcountry'},
213 $data->{'branchphone'}, $data->{'branchfax'},
214 $data->{'branchemail'}, $data->{'branchurl'},
215 $data->{'branchip'}, $data->{'branchprinter'},
216 $data->{'branchnotes'},
218 return 1 if $dbh->err;
219 } else {
220 my $query = "
221 UPDATE branches
222 SET branchname=?,branchaddress1=?,
223 branchaddress2=?,branchaddress3=?,branchzip=?,
224 branchcity=?,branchcountry=?,branchphone=?,
225 branchfax=?,branchemail=?,branchurl=?,branchip=?,
226 branchprinter=?,branchnotes=?
227 WHERE branchcode=?
229 my $sth = $dbh->prepare($query);
230 $sth->execute(
231 $data->{'branchname'},
232 $data->{'branchaddress1'}, $data->{'branchaddress2'},
233 $data->{'branchaddress3'}, $data->{'branchzip'},
234 $data->{'branchcity'}, $data->{'branchcountry'},
235 $data->{'branchphone'}, $data->{'branchfax'},
236 $data->{'branchemail'}, $data->{'branchurl'},
237 $data->{'branchip'}, $data->{'branchprinter'},
238 $data->{'branchnotes'},
239 $data->{'branchcode'},
242 # sort out the categories....
243 my @checkedcats;
244 my $cats = GetBranchCategory();
245 foreach my $cat (@$cats) {
246 my $code = $cat->{'categorycode'};
247 if ( $data->{$code} ) {
248 push( @checkedcats, $code );
251 my $branchcode = uc( $data->{'branchcode'} );
252 my $branch = GetBranchInfo($branchcode);
253 $branch = $branch->[0];
254 my $branchcats = $branch->{'categories'};
255 my @addcats;
256 my @removecats;
257 foreach my $bcat (@$branchcats) {
259 unless ( grep { /^$bcat$/ } @checkedcats ) {
260 push( @removecats, $bcat );
263 foreach my $ccat (@checkedcats) {
264 unless ( grep { /^$ccat$/ } @$branchcats ) {
265 push( @addcats, $ccat );
268 foreach my $cat (@addcats) {
269 my $sth =
270 $dbh->prepare(
271 "insert into branchrelations (branchcode, categorycode) values(?, ?)"
273 $sth->execute( $branchcode, $cat );
274 $sth->finish;
276 foreach my $cat (@removecats) {
277 my $sth =
278 $dbh->prepare(
279 "delete from branchrelations where branchcode=? and categorycode=?"
281 $sth->execute( $branchcode, $cat );
282 $sth->finish;
286 =head2 GetBranchCategory
288 $results = GetBranchCategory($categorycode);
290 C<$results> is an ref to an array.
292 =cut
294 sub GetBranchCategory {
296 # returns a reference to an array of hashes containing branches,
297 my ($catcode) = @_;
298 my $dbh = C4::Context->dbh;
299 my $sth;
301 # print DEBUG "GetBranchCategory: entry: catcode=".cvs($catcode)."\n";
302 if ($catcode) {
303 $sth =
304 $dbh->prepare(
305 "select * from branchcategories where categorycode = ?");
306 $sth->execute($catcode);
308 else {
309 $sth = $dbh->prepare("Select * from branchcategories");
310 $sth->execute();
312 my @results;
313 while ( my $data = $sth->fetchrow_hashref ) {
314 push( @results, $data );
316 $sth->finish;
318 # print DEBUG "GetBranchCategory: exit: returning ".cvs(\@results)."\n";
319 return \@results;
322 =head2 GetBranchCategories
324 my $categories = GetBranchCategories($branchcode,$categorytype);
326 Returns a list ref of anon hashrefs with keys eq columns of branchcategories table,
327 i.e. categorycode, categorydescription, categorytype, categoryname.
328 if $branchcode and/or $categorytype are passed, limit set to categories that
329 $branchcode is a member of , and to $categorytype.
331 =cut
333 sub GetBranchCategories {
334 my ($branchcode,$categorytype) = @_;
335 my $dbh = C4::Context->dbh();
336 my $query = "SELECT c.* FROM branchcategories c";
337 my (@where, @bind);
338 if($branchcode) {
339 $query .= ",branchrelations r, branches b ";
340 push @where, "c.categorycode=r.categorycode and r.branchcode=? ";
341 push @bind , $branchcode;
343 if ($categorytype) {
344 push @where, " c.categorytype=? ";
345 push @bind, $categorytype;
347 $query .= " where " . join(" and ", @where) if(@where);
348 $query .= " order by categorytype,c.categorycode";
349 my $sth=$dbh->prepare( $query);
350 $sth->execute(@bind);
352 my $branchcats = $sth->fetchall_arrayref({});
353 $sth->finish();
354 return( $branchcats );
357 =head2 GetCategoryTypes
359 $categorytypes = GetCategoryTypes;
360 returns a list of category types.
361 Currently these types are HARDCODED.
362 type: 'searchdomain' defines a group of agencies that the calling library may search in.
363 Other usage of agency categories falls under type: 'properties'.
364 to allow for other uses of categories.
365 The searchdomain bit may be better implemented as a separate module, but
366 the categories were already here, and minimally used.
367 =cut
369 #TODO manage category types. rename possibly to 'agency domains' ? as borrowergroups are called categories.
370 sub GetCategoryTypes() {
371 return ( 'searchdomain','properties');
374 =head2 GetBranch
376 $branch = GetBranch( $query, $branches );
378 =cut
380 sub GetBranch ($$) {
381 my ( $query, $branches ) = @_; # get branch for this query from branches
382 my $branch = $query->param('branch');
383 my %cookie = $query->cookie('userenv');
384 ($branch) || ($branch = $cookie{'branchname'});
385 ( $branches->{$branch} ) || ( $branch = ( keys %$branches )[0] );
386 return $branch;
389 =head2 GetBranchDetail
391 $branch = &GetBranchDetail($branchcode);
393 Given the branch code, the function returns a
394 hashref for the corresponding row in the branches table.
396 =cut
398 sub GetBranchDetail {
399 my ($branchcode) = shift or return;
400 my $sth = C4::Context->dbh->prepare("SELECT * FROM branches WHERE branchcode = ?");
401 $sth->execute($branchcode);
402 return $sth->fetchrow_hashref();
405 =head2 get_branchinfos_of
407 my $branchinfos_of = get_branchinfos_of(@branchcodes);
409 Associates a list of branchcodes to the information of the branch, taken in
410 branches table.
412 Returns a href where keys are branchcodes and values are href where keys are
413 branch information key.
415 print 'branchname is ', $branchinfos_of->{$code}->{branchname};
417 =cut
419 sub get_branchinfos_of {
420 my @branchcodes = @_;
422 my $query = '
423 SELECT branchcode,
424 branchname
425 FROM branches
426 WHERE branchcode IN ('
427 . join( ',', map( { "'" . $_ . "'" } @branchcodes ) ) . ')
429 return C4::Koha::get_infos_of( $query, 'branchcode' );
433 =head2 GetBranchesInCategory
435 my $branches = GetBranchesInCategory($categorycode);
437 Returns a href: keys %$branches eq (branchcode,branchname) .
439 =cut
441 sub GetBranchesInCategory($) {
442 my ($categorycode) = @_;
443 my @branches;
444 my $dbh = C4::Context->dbh();
445 my $sth=$dbh->prepare( "SELECT b.branchcode FROM branchrelations r, branches b
446 where r.branchcode=b.branchcode and r.categorycode=?");
447 $sth->execute($categorycode);
448 while (my $branch = $sth->fetchrow) {
449 push @branches, $branch;
451 $sth->finish();
452 return( \@branches );
455 =head2 GetBranchInfo
457 $results = GetBranchInfo($branchcode);
459 returns C<$results>, a reference to an array of hashes containing branches.
460 if $branchcode, just this branch, with associated categories.
461 if ! $branchcode && $categorytype, all branches in the category.
462 =cut
464 sub GetBranchInfo {
465 my ($branchcode,$categorytype) = @_;
466 my $dbh = C4::Context->dbh;
467 my $sth;
470 if ($branchcode) {
471 $sth =
472 $dbh->prepare(
473 "Select * from branches where branchcode = ? order by branchcode");
474 $sth->execute($branchcode);
476 else {
477 $sth = $dbh->prepare("Select * from branches order by branchcode");
478 $sth->execute();
480 my @results;
481 while ( my $data = $sth->fetchrow_hashref ) {
482 my @bind = ($data->{'branchcode'});
483 my $query= "select r.categorycode from branchrelations r";
484 $query .= ", branchcategories c " if($categorytype);
485 $query .= " where branchcode=? ";
486 if($categorytype) {
487 $query .= " and c.categorytype=? and r.categorycode=c.categorycode";
488 push @bind, $categorytype;
490 my $nsth=$dbh->prepare($query);
491 $nsth->execute( @bind );
492 my @cats = ();
493 while ( my ($cat) = $nsth->fetchrow_array ) {
494 push( @cats, $cat );
496 $nsth->finish;
497 $data->{'categories'} = \@cats;
498 push( @results, $data );
500 $sth->finish;
501 return \@results;
504 =head2 DelBranch
506 &DelBranch($branchcode);
508 =cut
510 sub DelBranch {
511 my ($branchcode) = @_;
512 my $dbh = C4::Context->dbh;
513 my $sth = $dbh->prepare("delete from branches where branchcode = ?");
514 $sth->execute($branchcode);
515 $sth->finish;
518 =head2 ModBranchCategoryInfo
520 &ModBranchCategoryInfo($data);
521 sets the data from the editbranch form, and writes to the database...
523 =cut
525 sub ModBranchCategoryInfo {
526 my ($data) = @_;
527 my $dbh = C4::Context->dbh;
528 if ($data->{'add'}){
529 # we are doing an insert
530 my $sth = $dbh->prepare("INSERT INTO branchcategories (categorycode,categoryname,codedescription,categorytype) VALUES (?,?,?,?)");
531 $sth->execute(uc( $data->{'categorycode'} ),$data->{'categoryname'}, $data->{'codedescription'},$data->{'categorytype'} );
532 $sth->finish();
534 else {
535 # modifying
536 my $sth = $dbh->prepare("UPDATE branchcategories SET categoryname=?,codedescription=?,categorytype=? WHERE categorycode=?");
537 $sth->execute($data->{'categoryname'}, $data->{'codedescription'},$data->{'categorytype'},uc( $data->{'categorycode'} ) );
538 $sth->finish();
542 =head2 DeleteBranchCategory
544 DeleteBranchCategory($categorycode);
546 =cut
548 sub DelBranchCategory {
549 my ($categorycode) = @_;
550 my $dbh = C4::Context->dbh;
551 my $sth = $dbh->prepare("delete from branchcategories where categorycode = ?");
552 $sth->execute($categorycode);
553 $sth->finish;
556 =head2 CheckBranchCategorycode
558 $number_rows_affected = CheckBranchCategorycode($categorycode);
560 =cut
562 sub CheckBranchCategorycode {
564 # check to see if the branchcode is being used in the database somewhere....
565 my ($categorycode) = @_;
566 my $dbh = C4::Context->dbh;
567 my $sth =
568 $dbh->prepare(
569 "select count(*) from branchrelations where categorycode=?");
570 $sth->execute($categorycode);
571 my ($total) = $sth->fetchrow_array;
572 return $total;
575 sub get_branch_code_from_name {
576 my @branch_name = @_;
577 my $query = "SELECT branchcode FROM branches WHERE branchname=?;";
578 my $dbh = C4::Context->dbh();
579 my $sth = $dbh->prepare($query);
580 $sth->execute(@branch_name);
581 return $sth->fetchrow_array;
585 __END__
587 =head1 AUTHOR
589 Koha Developement team <info@koha.org>
591 =cut