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
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
25 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
27 # set the version for version checking
28 $VERSION = do { my @v = '$Revision$' =~ /\d+/g; shift(@v).".".join( "_", map { sprintf "%03d", $_ } @v ); };
32 C4::Branch - Koha branch module
40 The functions in this module deal with branches.
55 &CheckBranchCategorycode
57 &ModBranchCategoryInfo
64 $branches = &GetBranches();
65 returns informations about ALL branches.
66 Create a branch selector with the following code
67 IndependantBranches Insensitive...
71 my $branches = GetBranches;
73 foreach my $thisbranch (keys %$branches) {
74 my $selected = 1 if $thisbranch eq $branch;
75 my %row =(value => $thisbranch,
76 selected => $selected,
77 branchname => $branches->{$thisbranch}->{'branchname'},
79 push @branchloop, \%row;
84 <select name="branch">
85 <option value="">Default</option>
86 <!-- TMPL_LOOP name="branchloop" -->
87 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="branchname" --></option>
95 # returns a reference to a hash of references to ALL branches...
97 my $dbh = C4::Context->dbh;
99 my $query="SELECT * from branches";
100 if ($onlymine && C4::Context->userenv && C4::Context->userenv->{branch}){
101 $query .= " WHERE branchcode =".$dbh->quote(C4::Context->userenv->{branch});
103 $query.=" order by branchname";
104 $sth = $dbh->prepare($query);
106 while ( my $branch = $sth->fetchrow_hashref ) {
109 "select categorycode from branchrelations where branchcode = ?");
110 $nsth->execute( $branch->{'branchcode'} );
111 while ( my ($cat) = $nsth->fetchrow_array ) {
113 # FIXME - This seems wrong. It ought to be
114 # $branch->{categorycodes}{$cat} = 1;
115 # otherwise, there's a namespace collision if there's a
116 # category with the same name as a field in the 'branches'
117 # table (i.e., don't create a category called "issuing").
118 # In addition, the current structure doesn't really allow
119 # you to list the categories that a branch belongs to:
120 # you'd have to list keys %$branch, and remove those keys
121 # that aren't fields in the "branches" table.
124 $branches{ $branch->{'branchcode'} } = $branch;
126 return ( \%branches );
134 my ($branchcode) = @_;
135 my $dbh = C4::Context->dbh;
137 $sth = $dbh->prepare("Select branchname from branches where branchcode=?");
138 $sth->execute($branchcode);
139 my $branchname = $sth->fetchrow_array;
141 return ($branchname);
146 &ModBranch($newvalue);
148 This function modify an existing branches.
150 C<$newvalue> is a ref to an array wich is containt all the column from branches table.
157 my $dbh = C4::Context->dbh;
161 (branchcode,branchname,branchaddress1,
162 branchaddress2,branchaddress3,branchphone,
163 branchfax,branchemail,branchip,branchprinter)
164 VALUES (?,?,?,?,?,?,?,?,?,?)
166 my $sth = $dbh->prepare($query);
168 $data->{'branchcode'}, $data->{'branchname'},
169 $data->{'branchaddress1'}, $data->{'branchaddress2'},
170 $data->{'branchaddress3'}, $data->{'branchphone'},
171 $data->{'branchfax'}, $data->{'branchemail'},
172 $data->{'branchip'}, $data->{'branchprinter'},
177 SET branchname=?,branchaddress1=?,
178 branchaddress2=?,branchaddress3=?,branchphone=?,
179 branchfax=?,branchemail=?,branchip=?,branchprinter=?
182 my $sth = $dbh->prepare($query);
184 $data->{'branchname'},
185 $data->{'branchaddress1'}, $data->{'branchaddress2'},
186 $data->{'branchaddress3'}, $data->{'branchphone'},
187 $data->{'branchfax'}, $data->{'branchemail'},
188 $data->{'branchip'}, $data->{'branchprinter'},
189 $data->{'branchcode'},
192 # sort out the categories....
194 my $cats = GetBranchCategory();
195 foreach my $cat (@$cats) {
196 my $code = $cat->{'categorycode'};
197 if ( $data->{$code} ) {
198 push( @checkedcats, $code );
201 my $branchcode = uc( $data->{'branchcode'} );
202 my $branch = GetBranchInfo($branchcode);
203 $branch = $branch->[0];
204 my $branchcats = $branch->{'categories'};
207 foreach my $bcat (@$branchcats) {
209 unless ( grep { /^$bcat$/ } @checkedcats ) {
210 push( @removecats, $bcat );
213 foreach my $ccat (@checkedcats) {
214 unless ( grep { /^$ccat$/ } @$branchcats ) {
215 push( @addcats, $ccat );
218 foreach my $cat (@addcats) {
221 "insert into branchrelations (branchcode, categorycode) values(?, ?)"
223 $sth->execute( $branchcode, $cat );
226 foreach my $cat (@removecats) {
229 "delete from branchrelations where branchcode=? and categorycode=?"
231 $sth->execute( $branchcode, $cat );
236 =head2 GetBranchCategory
238 $results = GetBranchCategory($categorycode);
240 C<$results> is an ref to an array.
244 sub GetBranchCategory {
246 # returns a reference to an array of hashes containing branches,
248 my $dbh = C4::Context->dbh;
251 # print DEBUG "GetBranchCategory: entry: catcode=".cvs($catcode)."\n";
255 "select * from branchcategories where categorycode = ?");
256 $sth->execute($catcode);
259 $sth = $dbh->prepare("Select * from branchcategories");
263 while ( my $data = $sth->fetchrow_hashref ) {
264 push( @results, $data );
268 # print DEBUG "GetBranchCategory: exit: returning ".cvs(\@results)."\n";
274 $branch = GetBranch( $query, $branches );
279 my ( $query, $branches ) = @_; # get branch for this query from branches
280 my $branch = $query->param('branch');
281 my %cookie = $query->cookie('userenv');
282 ($branch) || ($branch = $cookie{'branchname'});
283 ( $branches->{$branch} ) || ( $branch = ( keys %$branches )[0] );
287 =head2 GetBranchDetail
289 $branchname = &GetBranchDetail($branchcode);
291 Given the branch code, the function returns the corresponding
292 branch name for a comprehensive information display
296 sub GetBranchDetail {
297 my ($branchcode) = @_;
298 my $dbh = C4::Context->dbh;
299 my $sth = $dbh->prepare("SELECT * FROM branches WHERE branchcode = ?");
300 $sth->execute($branchcode);
301 my $branchname = $sth->fetchrow_hashref();
307 =head2 get_branchinfos_of
309 my $branchinfos_of = get_branchinfos_of(@branchcodes);
311 Associates a list of branchcodes to the information of the branch, taken in
314 Returns a href where keys are branchcodes and values are href where keys are
315 branch information key.
317 print 'branchname is ', $branchinfos_of->{$code}->{branchname};
321 sub get_branchinfos_of {
322 my @branchcodes = @_;
328 WHERE branchcode IN ('
329 . join( ',', map( { "'" . $_ . "'" } @branchcodes ) ) . ')
331 return C4::Koha::get_infos_of( $query, 'branchcode' );
336 $results = GetBranchInfo($branchcode);
338 returns C<$results>, a reference to an array of hashes containing branches.
343 my ($branchcode) = @_;
344 my $dbh = C4::Context->dbh;
349 "Select * from branches where branchcode = ? order by branchcode");
350 $sth->execute($branchcode);
353 $sth = $dbh->prepare("Select * from branches order by branchcode");
357 while ( my $data = $sth->fetchrow_hashref ) {
360 "select categorycode from branchrelations where branchcode = ?");
361 $nsth->execute( $data->{'branchcode'} );
363 while ( my ($cat) = $nsth->fetchrow_array ) {
367 $data->{'categories'} = \@cats;
368 push( @results, $data );
376 &DelBranch($branchcode);
381 my ($branchcode) = @_;
382 my $dbh = C4::Context->dbh;
383 my $sth = $dbh->prepare("delete from branches where branchcode = ?");
384 $sth->execute($branchcode);
388 =head2 ModBranchCategoryInfo
390 &ModBranchCategoryInfo($data);
391 sets the data from the editbranch form, and writes to the database...
395 sub ModBranchCategoryInfo {
398 my $dbh = C4::Context->dbh;
399 my $sth = $dbh->prepare("replace branchcategories (categorycode,categoryname,codedescription) values (?,?,?)");
400 $sth->execute(uc( $data->{'categorycode'} ),$data->{'categoryname'}, $data->{'codedescription'} );
404 =head2 DeleteBranchCategory
406 DeleteBranchCategory($categorycode);
410 sub DelBranchCategory {
411 my ($categorycode) = @_;
412 my $dbh = C4::Context->dbh;
413 my $sth = $dbh->prepare("delete from branchcategories where categorycode = ?");
414 $sth->execute($categorycode);
418 =head2 CheckBranchCategorycode
420 $number_rows_affected = CheckBranchCategorycode($categorycode);
424 sub CheckBranchCategorycode {
426 # check to see if the branchcode is being used in the database somewhere....
427 my ($categorycode) = @_;
428 my $dbh = C4::Context->dbh;
431 "select count(*) from branchrelations where categorycode=?");
432 $sth->execute($categorycode);
433 my ($total) = $sth->fetchrow_array;
441 Koha Developement team <info@koha.org>