Show enumchron, copynumber in opac detail iff present.
[koha.git] / C4 / Branch.pm
blob475516875c6e47958d844e40b4a9d1c3bb81f022
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.01;
29 @ISA = qw(Exporter);
30 @EXPORT = qw(
31 &GetBranchCategory
32 &GetBranchName
33 &GetBranch
34 &GetBranches
35 &GetBranchDetail
36 &get_branchinfos_of
37 &ModBranch
38 &CheckBranchCategorycode
39 &GetBranchInfo
40 &GetCategoryTypes
41 &GetBranchCategories
42 &GetBranchesInCategory
43 &ModBranchCategoryInfo
44 &DelBranch
45 &DelBranchCategory
49 =head1 NAME
51 C4::Branch - Koha branch module
53 =head1 SYNOPSIS
55 use C4::Branch;
57 =head1 DESCRIPTION
59 The functions in this module deal with branches.
61 =head1 FUNCTIONS
63 =head2 GetBranches
65 $branches = &GetBranches();
66 returns informations about ALL branches.
67 Create a branch selector with the following code
68 IndependantBranches Insensitive...
69 GetBranchInfo() returns the same information without the problems of this function
70 (namespace collision, mainly). You should probably use that, and replace GetBranches()
71 with GetBranchInfo() where you see it in the code.
73 =head3 in PERL SCRIPT
75 my $branches = GetBranches;
76 my @branchloop;
77 foreach my $thisbranch (keys %$branches) {
78 my $selected = 1 if $thisbranch eq $branch;
79 my %row =(value => $thisbranch,
80 selected => $selected,
81 branchname => $branches->{$thisbranch}->{'branchname'},
83 push @branchloop, \%row;
86 =head3 in TEMPLATE
87 <select name="branch">
88 <option value="">Default</option>
89 <!-- TMPL_LOOP name="branchloop" -->
90 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="branchname" --></option>
91 <!-- /TMPL_LOOP -->
92 </select>
94 =cut
96 sub GetBranches {
97 my ($onlymine)=@_;
98 # returns a reference to a hash of references to ALL branches...
99 my %branches;
100 my $dbh = C4::Context->dbh;
101 my $sth;
102 my $query="SELECT * FROM branches";
103 if ($onlymine && C4::Context->userenv && C4::Context->userenv->{branch}){
104 $query .= " WHERE branchcode =".$dbh->quote(C4::Context->userenv->{branch});
106 $query.=" ORDER BY branchname";
107 $sth = $dbh->prepare($query);
108 $sth->execute;
109 while ( my $branch = $sth->fetchrow_hashref ) {
110 my $nsth =
111 $dbh->prepare(
112 "SELECT categorycode FROM branchrelations WHERE branchcode = ?");
113 $nsth->execute( $branch->{'branchcode'} );
114 while ( my ($cat) = $nsth->fetchrow_array ) {
116 # FIXME - This seems wrong. It ought to be
117 # $branch->{categorycodes}{$cat} = 1;
118 # otherwise, there's a namespace collision if there's a
119 # category with the same name as a field in the 'branches'
120 # table (i.e., don't create a category called "issuing").
121 # In addition, the current structure doesn't really allow
122 # you to list the categories that a branch belongs to:
123 # you'd have to list keys %$branch, and remove those keys
124 # that aren't fields in the "branches" table.
125 # $branch->{$cat} = 1;
126 $branch->{category}{$cat} = 1;
128 $branches{ $branch->{'branchcode'} } = $branch;
130 return ( \%branches );
133 =head2 GetBranchName
135 =cut
137 sub GetBranchName {
138 my ($branchcode) = @_;
139 my $dbh = C4::Context->dbh;
140 my $sth;
141 $sth = $dbh->prepare("Select branchname from branches where branchcode=?");
142 $sth->execute($branchcode);
143 my $branchname = $sth->fetchrow_array;
144 $sth->finish;
145 return ($branchname);
148 =head2 ModBranch
150 &ModBranch($newvalue);
152 This function modify an existing branches.
154 C<$newvalue> is a ref to an array wich is containt all the column from branches table.
156 =cut
158 sub ModBranch {
159 my ($data) = @_;
161 my $dbh = C4::Context->dbh;
162 if ($data->{add}) {
163 my $query = "
164 INSERT INTO branches
165 (branchcode,branchname,branchaddress1,
166 branchaddress2,branchaddress3,branchphone,
167 branchfax,branchemail,branchip,branchprinter)
168 VALUES (?,?,?,?,?,?,?,?,?,?)
170 my $sth = $dbh->prepare($query);
171 $sth->execute(
172 $data->{'branchcode'}, $data->{'branchname'},
173 $data->{'branchaddress1'}, $data->{'branchaddress2'},
174 $data->{'branchaddress3'}, $data->{'branchphone'},
175 $data->{'branchfax'}, $data->{'branchemail'},
176 $data->{'branchip'}, $data->{'branchprinter'},
178 } else {
179 my $query = "
180 UPDATE branches
181 SET branchname=?,branchaddress1=?,
182 branchaddress2=?,branchaddress3=?,branchphone=?,
183 branchfax=?,branchemail=?,branchip=?,branchprinter=?
184 WHERE branchcode=?
186 my $sth = $dbh->prepare($query);
187 $sth->execute(
188 $data->{'branchname'},
189 $data->{'branchaddress1'}, $data->{'branchaddress2'},
190 $data->{'branchaddress3'}, $data->{'branchphone'},
191 $data->{'branchfax'}, $data->{'branchemail'},
192 $data->{'branchip'}, $data->{'branchprinter'},
193 $data->{'branchcode'},
196 # sort out the categories....
197 my @checkedcats;
198 my $cats = GetBranchCategory();
199 foreach my $cat (@$cats) {
200 my $code = $cat->{'categorycode'};
201 if ( $data->{$code} ) {
202 push( @checkedcats, $code );
205 my $branchcode = uc( $data->{'branchcode'} );
206 my $branch = GetBranchInfo($branchcode);
207 $branch = $branch->[0];
208 my $branchcats = $branch->{'categories'};
209 my @addcats;
210 my @removecats;
211 foreach my $bcat (@$branchcats) {
213 unless ( grep { /^$bcat$/ } @checkedcats ) {
214 push( @removecats, $bcat );
217 foreach my $ccat (@checkedcats) {
218 unless ( grep { /^$ccat$/ } @$branchcats ) {
219 push( @addcats, $ccat );
222 foreach my $cat (@addcats) {
223 my $sth =
224 $dbh->prepare(
225 "insert into branchrelations (branchcode, categorycode) values(?, ?)"
227 $sth->execute( $branchcode, $cat );
228 $sth->finish;
230 foreach my $cat (@removecats) {
231 my $sth =
232 $dbh->prepare(
233 "delete from branchrelations where branchcode=? and categorycode=?"
235 $sth->execute( $branchcode, $cat );
236 $sth->finish;
240 =head2 GetBranchCategory
242 $results = GetBranchCategory($categorycode);
244 C<$results> is an ref to an array.
246 =cut
248 sub GetBranchCategory {
250 # returns a reference to an array of hashes containing branches,
251 my ($catcode) = @_;
252 my $dbh = C4::Context->dbh;
253 my $sth;
255 # print DEBUG "GetBranchCategory: entry: catcode=".cvs($catcode)."\n";
256 if ($catcode) {
257 $sth =
258 $dbh->prepare(
259 "select * from branchcategories where categorycode = ?");
260 $sth->execute($catcode);
262 else {
263 $sth = $dbh->prepare("Select * from branchcategories");
264 $sth->execute();
266 my @results;
267 while ( my $data = $sth->fetchrow_hashref ) {
268 push( @results, $data );
270 $sth->finish;
272 # print DEBUG "GetBranchCategory: exit: returning ".cvs(\@results)."\n";
273 return \@results;
276 =head2 GetBranchCategories
278 my $categories = GetBranchCategories($branchcode,$categorytype);
280 Returns a list ref of anon hashrefs with keys eq columns of branchcategories table,
281 i.e. categorycode, categorydescription, categorytype, categoryname.
282 if $branchcode and/or $categorytype are passed, limit set to categories that
283 $branchcode is a member of , and to $categorytype.
285 =cut
287 sub GetBranchCategories {
288 my ($branchcode,$categorytype) = @_;
289 my $dbh = C4::Context->dbh();
290 my $query = "SELECT c.* FROM branchcategories c";
291 my (@where, @bind);
292 if($branchcode) {
293 $query .= ",branchrelations r, branches b ";
294 push @where, "c.categorycode=r.categorycode and r.branchcode=? ";
295 push @bind , $branchcode;
297 if ($categorytype) {
298 push @where, " c.categorytype=? ";
299 push @bind, $categorytype;
301 $query .= " where " . join(" and ", @where) if(@where);
302 $query .= " order by categorytype,c.categorycode";
303 my $sth=$dbh->prepare( $query);
304 $sth->execute(@bind);
306 my $branchcats = $sth->fetchall_arrayref({});
307 $sth->finish();
308 return( $branchcats );
311 =head2 GetCategoryTypes
313 $categorytypes = GetCategoryTypes;
314 returns a list of category types.
315 Currently these types are HARDCODED.
316 type: 'searchdomain' defines a group of agencies that the calling library may search in.
317 Other usage of agency categories falls under type: 'properties'.
318 to allow for other uses of categories.
319 The searchdomain bit may be better implemented as a separate module, but
320 the categories were already here, and minimally used.
321 =cut
323 #TODO manage category types. rename possibly to 'agency domains' ? as borrowergroups are called categories.
324 sub GetCategoryTypes() {
325 return ( 'searchdomain','properties');
328 =head2 GetBranch
330 $branch = GetBranch( $query, $branches );
332 =cut
334 sub GetBranch ($$) {
335 my ( $query, $branches ) = @_; # get branch for this query from branches
336 my $branch = $query->param('branch');
337 my %cookie = $query->cookie('userenv');
338 ($branch) || ($branch = $cookie{'branchname'});
339 ( $branches->{$branch} ) || ( $branch = ( keys %$branches )[0] );
340 return $branch;
343 =head2 GetBranchDetail
345 $branchname = &GetBranchDetail($branchcode);
347 Given the branch code, the function returns the corresponding
348 branch name for a comprehensive information display
350 =cut
352 sub GetBranchDetail {
353 my ($branchcode) = @_;
354 my $dbh = C4::Context->dbh;
355 my $sth = $dbh->prepare("SELECT * FROM branches WHERE branchcode = ?");
356 $sth->execute($branchcode);
357 my $branchname = $sth->fetchrow_hashref();
358 $sth->finish();
359 return $branchname;
362 =head2 get_branchinfos_of
364 my $branchinfos_of = get_branchinfos_of(@branchcodes);
366 Associates a list of branchcodes to the information of the branch, taken in
367 branches table.
369 Returns a href where keys are branchcodes and values are href where keys are
370 branch information key.
372 print 'branchname is ', $branchinfos_of->{$code}->{branchname};
374 =cut
376 sub get_branchinfos_of {
377 my @branchcodes = @_;
379 my $query = '
380 SELECT branchcode,
381 branchname
382 FROM branches
383 WHERE branchcode IN ('
384 . join( ',', map( { "'" . $_ . "'" } @branchcodes ) ) . ')
386 return C4::Koha::get_infos_of( $query, 'branchcode' );
390 =head2 GetBranchesInCategory
392 my $branches = GetBranchesInCategory($categorycode);
394 Returns a href: keys %$branches eq (branchcode,branchname) .
396 =cut
398 sub GetBranchesInCategory($) {
399 my ($categorycode) = @_;
400 my @branches;
401 my $dbh = C4::Context->dbh();
402 my $sth=$dbh->prepare( "SELECT b.branchcode FROM branchrelations r, branches b
403 where r.branchcode=b.branchcode and r.categorycode=?");
404 $sth->execute($categorycode);
405 while (my $branch = $sth->fetchrow) {
406 push @branches, $branch;
408 $sth->finish();
409 return( \@branches );
412 =head2 GetBranchInfo
414 $results = GetBranchInfo($branchcode);
416 returns C<$results>, a reference to an array of hashes containing branches.
417 if $branchcode, just this branch, with associated categories.
418 if ! $branchcode && $categorytype, all branches in the category.
419 =cut
421 sub GetBranchInfo {
422 my ($branchcode,$categorytype) = @_;
423 my $dbh = C4::Context->dbh;
424 my $sth;
427 if ($branchcode) {
428 $sth =
429 $dbh->prepare(
430 "Select * from branches where branchcode = ? order by branchcode");
431 $sth->execute($branchcode);
433 else {
434 $sth = $dbh->prepare("Select * from branches order by branchcode");
435 $sth->execute();
437 my @results;
438 while ( my $data = $sth->fetchrow_hashref ) {
439 my @bind = ($data->{'branchcode'});
440 my $query= "select r.categorycode from branchrelations r";
441 $query .= ", branchcategories c " if($categorytype);
442 $query .= " where branchcode=? ";
443 if($categorytype) {
444 $query .= " and c.categorytype=? and r.categorycode=c.categorycode";
445 push @bind, $categorytype;
447 my $nsth=$dbh->prepare($query);
448 $nsth->execute( @bind );
449 my @cats = ();
450 while ( my ($cat) = $nsth->fetchrow_array ) {
451 push( @cats, $cat );
453 $nsth->finish;
454 $data->{'categories'} = \@cats;
455 push( @results, $data );
457 $sth->finish;
458 return \@results;
461 =head2 DelBranch
463 &DelBranch($branchcode);
465 =cut
467 sub DelBranch {
468 my ($branchcode) = @_;
469 my $dbh = C4::Context->dbh;
470 my $sth = $dbh->prepare("delete from branches where branchcode = ?");
471 $sth->execute($branchcode);
472 $sth->finish;
475 =head2 ModBranchCategoryInfo
477 &ModBranchCategoryInfo($data);
478 sets the data from the editbranch form, and writes to the database...
480 =cut
482 sub ModBranchCategoryInfo {
483 my ($data) = @_;
484 my $dbh = C4::Context->dbh;
485 if ($data->{'add'}){
486 # we are doing an insert
487 my $sth = $dbh->prepare("INSERT INTO branchcategories (categorycode,categoryname,codedescription,categorytype) VALUES (?,?,?,?)");
488 $sth->execute(uc( $data->{'categorycode'} ),$data->{'categoryname'}, $data->{'codedescription'},$data->{'categorytype'} );
489 $sth->finish();
491 else {
492 # modifying
493 my $sth = $dbh->prepare("UPDATE branchcategories SET categoryname=?,codedescription=?,categorytype=? WHERE categorycode=?");
494 $sth->execute($data->{'categoryname'}, $data->{'codedescription'},$data->{'categorytype'},uc( $data->{'categorycode'} ) );
495 $sth->finish();
499 =head2 DeleteBranchCategory
501 DeleteBranchCategory($categorycode);
503 =cut
505 sub DelBranchCategory {
506 my ($categorycode) = @_;
507 my $dbh = C4::Context->dbh;
508 my $sth = $dbh->prepare("delete from branchcategories where categorycode = ?");
509 $sth->execute($categorycode);
510 $sth->finish;
513 =head2 CheckBranchCategorycode
515 $number_rows_affected = CheckBranchCategorycode($categorycode);
517 =cut
519 sub CheckBranchCategorycode {
521 # check to see if the branchcode is being used in the database somewhere....
522 my ($categorycode) = @_;
523 my $dbh = C4::Context->dbh;
524 my $sth =
525 $dbh->prepare(
526 "select count(*) from branchrelations where categorycode=?");
527 $sth->execute($categorycode);
528 my ($total) = $sth->fetchrow_array;
529 return $total;
533 __END__
535 =head1 AUTHOR
537 Koha Developement team <info@koha.org>
539 =cut