File Coverage

File:C4/Branch.pm
Coverage:7.6%

linestmtbrancondsubtimecode
1package C4::Branch;
2
3# This file is part of Koha.
4#
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.
9#
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.
13#
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
17
18
19
15
15
15
451
98
509
use strict;
20#use warnings; FIXME - Bug 2505
21require Exporter;
22
15
15
15
370
43
148
use C4::Context;
23
24
15
15
15
81
72
2656
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
25
26BEGIN {
27        # set the version for version checking
28
15
76
        $VERSION = 3.02;
29
15
276
        @ISA = qw(Exporter);
30
15
246
        @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
48                &mybranch
49        );
50
15
39868
        @EXPORT_OK = qw( &onlymine &mybranch get_branch_code_from_name );
51}
52
53 - 101
=head1 NAME

C4::Branch - Koha branch module

=head1 SYNOPSIS

use C4::Branch;

=head1 DESCRIPTION

The functions in this module deal with branches.

=head1 FUNCTIONS

=head2 GetBranches

  $branches = &GetBranches();

Returns informations about ALL branches, IndependantBranches Insensitive.
GetBranchInfo() returns the same information without the problems of this function 
(namespace collision, mainly).

Create a branch selector with the following code.

=head3 in PERL SCRIPT

    my $branches = GetBranches;
    my @branchloop;
    foreach my $thisbranch (sort keys %$branches) {
        my $selected = 1 if $thisbranch eq $branch;
        my %row =(value => $thisbranch,
                    selected => $selected,
                    branchname => $branches->{$thisbranch}->{branchname},
                );
        push @branchloop, \%row;
    }

=head3 in TEMPLATE

    <select name="branch">
        <option value="">Default</option>
        <!-- TMPL_LOOP name="branchloop" -->
        <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="branchname" --></option>
        <!-- /TMPL_LOOP -->
    </select>

=head4 Note that you often will want to just use GetBranchesLoop, for exactly the example above.

=cut
102
103sub GetBranches {
104
0
0
    my ($onlymine)=@_;
105    # returns a reference to a hash of references to ALL branches...
106
0
0
    my %branches;
107
0
0
    my $dbh = C4::Context->dbh;
108
0
0
    my $sth;
109
0
0
    my $query="SELECT * FROM branches";
110
0
0
    my @bind_parameters;
111
0
0
    if ($onlymine && C4::Context->userenv && C4::Context->userenv->{branch}){
112
0
0
      $query .= ' WHERE branchcode = ? ';
113
0
0
      push @bind_parameters, C4::Context->userenv->{branch};
114    }
115
0
0
        $query.=" ORDER BY branchname";
116
0
0
    $sth = $dbh->prepare($query);
117
0
0
    $sth->execute( @bind_parameters );
118
119
0
0
    my $nsth = $dbh->prepare(
120        "SELECT categorycode FROM branchrelations WHERE branchcode = ?"
121    ); # prepare once, outside while loop
122
123
0
0
    while ( my $branch = $sth->fetchrow_hashref ) {
124
0
0
        $nsth->execute( $branch->{'branchcode'} );
125
0
0
        while ( my ($cat) = $nsth->fetchrow_array ) {
126            # FIXME - This seems wrong. It ought to be
127            # $branch->{categorycodes}{$cat} = 1;
128            # otherwise, there's a namespace collision if there's a
129            # category with the same name as a field in the 'branches'
130            # table (i.e., don't create a category called "issuing").
131            # In addition, the current structure doesn't really allow
132            # you to list the categories that a branch belongs to:
133            # you'd have to list keys %$branch, and remove those keys
134            # that aren't fields in the "branches" table.
135         # $branch->{$cat} = 1;
136
0
0
            $branch->{category}{$cat} = 1;
137        }
138
0
0
        $branches{ $branch->{'branchcode'} } = $branch;
139    }
140
0
0
    return ( \%branches );
141}
142
143sub onlymine {
144    return
145
0
0
    C4::Context->preference('IndependantBranches') &&
146    C4::Context->userenv &&
147    C4::Context->userenv->{flags} %2 != 1 &&
148    C4::Context->userenv->{branch} ;
149}
150
151# always returns a string for OK comparison via "eq" or "ne"
152sub mybranch {
153
23
155
    C4::Context->userenv or return '';
154
23
145
    return C4::Context->userenv->{branch} || '';
155}
156
157sub GetBranchesLoop (;$$) { # since this is what most pages want anyway
158
0
    my $branch = @_ ? shift : mybranch(); # optional first argument is branchcode of "my branch", if preselection is wanted.
159
0
    my $onlymine = @_ ? shift : onlymine();
160
0
    my $branches = GetBranches($onlymine);
161
0
    my @loop;
162
0
0
    foreach ( sort { uc($branches->{$a}->{branchname}) cmp uc($branches->{$b}->{branchname}) } keys %$branches ) {
163
0
        push @loop, {
164            value => $_,
165            selected => ($_ eq $branch) ? 1 : 0,
166            branchname => $branches->{$_}->{branchname},
167        };
168    }
169
0
    return \@loop;
170}
171
172 - 174
=head2 GetBranchName

=cut
175
176sub GetBranchName {
177
0
    my ($branchcode) = @_;
178
0
    my $dbh = C4::Context->dbh;
179
0
    my $sth;
180
0
    $sth = $dbh->prepare("Select branchname from branches where branchcode=?");
181
0
    $sth->execute($branchcode);
182
0
    my $branchname = $sth->fetchrow_array;
183
0
    $sth->finish;
184
0
    return ($branchname);
185}
186
187 - 195
=head2 ModBranch

$error = &ModBranch($newvalue);

This function modify an existing branch

C<$newvalue> is a ref to an array wich is containt all the column from branches table.

=cut
196
197sub ModBranch {
198
0
    my ($data) = @_;
199
200
0
    my $dbh = C4::Context->dbh;
201
0
    if ($data->{add}) {
202
0
        my $query = "
203            INSERT INTO branches
204            (branchcode,branchname,branchaddress1,
205            branchaddress2,branchaddress3,branchzip,branchcity,branchstate,
206            branchcountry,branchphone,branchfax,branchemail,
207            branchurl,branchip,branchprinter,branchnotes,opac_info)
208            VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
209        ";
210
0
        my $sth = $dbh->prepare($query);
211
0
        $sth->execute(
212            $data->{'branchcode'}, $data->{'branchname'},
213            $data->{'branchaddress1'}, $data->{'branchaddress2'},
214            $data->{'branchaddress3'}, $data->{'branchzip'},
215            $data->{'branchcity'}, $data->{'branchstate'},
216            $data->{'branchcountry'},
217            $data->{'branchphone'}, $data->{'branchfax'},
218            $data->{'branchemail'}, $data->{'branchurl'},
219            $data->{'branchip'}, $data->{'branchprinter'},
220            $data->{'branchnotes'}, $data->{opac_info},
221        );
222
0
        return 1 if $dbh->err;
223    } else {
224
0
        my $query = "
225            UPDATE branches
226            SET branchname=?,branchaddress1=?,
227                branchaddress2=?,branchaddress3=?,branchzip=?,
228                branchcity=?,branchstate=?,branchcountry=?,branchphone=?,
229                branchfax=?,branchemail=?,branchurl=?,branchip=?,
230                branchprinter=?,branchnotes=?,opac_info=?
231            WHERE branchcode=?
232        ";
233
0
        my $sth = $dbh->prepare($query);
234
0
        $sth->execute(
235            $data->{'branchname'},
236            $data->{'branchaddress1'}, $data->{'branchaddress2'},
237            $data->{'branchaddress3'}, $data->{'branchzip'},
238            $data->{'branchcity'}, $data->{'branchstate'},
239            $data->{'branchcountry'},
240            $data->{'branchphone'}, $data->{'branchfax'},
241            $data->{'branchemail'}, $data->{'branchurl'},
242            $data->{'branchip'}, $data->{'branchprinter'},
243            $data->{'branchnotes'}, $data->{opac_info},
244            $data->{'branchcode'},
245        );
246    }
247    # sort out the categories....
248
0
    my @checkedcats;
249
0
    my $cats = GetBranchCategory();
250
0
    foreach my $cat (@$cats) {
251
0
        my $code = $cat->{'categorycode'};
252
0
        if ( $data->{$code} ) {
253
0
            push( @checkedcats, $code );
254        }
255    }
256
0
    my $branchcode = uc( $data->{'branchcode'} );
257
0
    my $branch = GetBranchInfo($branchcode);
258
0
    $branch = $branch->[0];
259
0
    my $branchcats = $branch->{'categories'};
260
0
    my @addcats;
261
0
    my @removecats;
262
0
    foreach my $bcat (@$branchcats) {
263
264
0
0
        unless ( grep { /^$bcat$/ } @checkedcats ) {
265
0
            push( @removecats, $bcat );
266        }
267    }
268
0
    foreach my $ccat (@checkedcats) {
269
0
0
        unless ( grep { /^$ccat$/ } @$branchcats ) {
270
0
            push( @addcats, $ccat );
271        }
272    }
273
0
    foreach my $cat (@addcats) {
274
0
        my $sth =
275          $dbh->prepare(
276"insert into branchrelations (branchcode, categorycode) values(?, ?)"
277          );
278
0
        $sth->execute( $branchcode, $cat );
279
0
        $sth->finish;
280    }
281
0
    foreach my $cat (@removecats) {
282
0
        my $sth =
283          $dbh->prepare(
284            "delete from branchrelations where branchcode=? and categorycode=?"
285          );
286
0
        $sth->execute( $branchcode, $cat );
287
0
        $sth->finish;
288    }
289}
290
291 - 297
=head2 GetBranchCategory

$results = GetBranchCategory($categorycode);

C<$results> is an ref to an array.

=cut
298
299sub GetBranchCategory {
300
301    # returns a reference to an array of hashes containing branches,
302
0
    my ($catcode) = @_;
303
0
    my $dbh = C4::Context->dbh;
304
0
    my $sth;
305
306    # print DEBUG "GetBranchCategory: entry: catcode=".cvs($catcode)."\n";
307
0
    if ($catcode) {
308
0
        $sth =
309          $dbh->prepare(
310            "select * from branchcategories where categorycode = ?");
311
0
        $sth->execute($catcode);
312    }
313    else {
314
0
        $sth = $dbh->prepare("Select * from branchcategories");
315
0
        $sth->execute();
316    }
317
0
    my @results;
318
0
    while ( my $data = $sth->fetchrow_hashref ) {
319
0
        push( @results, $data );
320    }
321
0
    $sth->finish;
322
323    # print DEBUG "GetBranchCategory: exit: returning ".cvs(\@results)."\n";
324
0
    return \@results;
325}
326
327 - 336
=head2 GetBranchCategories

  my $categories = GetBranchCategories($branchcode,$categorytype);

Returns a list ref of anon hashrefs with keys eq columns of branchcategories table,
i.e. categorycode, categorydescription, categorytype, categoryname.
if $branchcode and/or $categorytype are passed, limit set to categories that
$branchcode is a member of , and to $categorytype.

=cut
337
338sub GetBranchCategories {
339
0
    my ($branchcode,$categorytype) = @_;
340
0
        my $dbh = C4::Context->dbh();
341
0
        my $query = "SELECT c.* FROM branchcategories c";
342
0
        my (@where, @bind);
343
0
        if($branchcode) {
344
0
                $query .= ",branchrelations r, branches b ";
345
0
                push @where, "c.categorycode=r.categorycode and r.branchcode=? ";
346
0
                push @bind , $branchcode;
347        }
348
0
        if ($categorytype) {
349
0
                push @where, " c.categorytype=? ";
350
0
                push @bind, $categorytype;
351        }
352
0
        $query .= " where " . join(" and ", @where) if(@where);
353
0
        $query .= " order by categorytype,c.categorycode";
354
0
        my $sth=$dbh->prepare( $query);
355
0
        $sth->execute(@bind);
356
357
0
        my $branchcats = $sth->fetchall_arrayref({});
358
0
        $sth->finish();
359
0
        return( $branchcats );
360}
361
362 - 372
=head2 GetCategoryTypes

$categorytypes = GetCategoryTypes;
returns a list of category types.
Currently these types are HARDCODED.
type: 'searchdomain' defines a group of agencies that the calling library may search in.
Other usage of agency categories falls under type: 'properties'.
	to allow for other uses of categories.
The searchdomain bit may be better implemented as a separate module, but
the categories were already here, and minimally used.
=cut
373
374        #TODO manage category types. rename possibly to 'agency domains' ? as borrowergroups are called categories.
375sub GetCategoryTypes() {
376
0
        return ( 'searchdomain','properties');
377}
378
379 - 383
=head2 GetBranch

$branch = GetBranch( $query, $branches );

=cut
384
385sub GetBranch ($$) {
386
0
    my ( $query, $branches ) = @_; # get branch for this query from branches
387
0
    my $branch = $query->param('branch');
388
0
    my %cookie = $query->cookie('userenv');
389
0
    ($branch) || ($branch = $cookie{'branchname'});
390
0
    ( $branches->{$branch} ) || ( $branch = ( keys %$branches )[0] );
391
0
    return $branch;
392}
393
394 - 401
=head2 GetBranchDetail

    $branch = &GetBranchDetail($branchcode);

Given the branch code, the function returns a
hashref for the corresponding row in the branches table.

=cut
402
403sub GetBranchDetail {
404
0
    my ($branchcode) = shift or return;
405
0
    my $sth = C4::Context->dbh->prepare("SELECT * FROM branches WHERE branchcode = ?");
406
0
    $sth->execute($branchcode);
407
0
    return $sth->fetchrow_hashref();
408}
409
410 - 416
=head2 GetBranchesInCategory

  my $branches = GetBranchesInCategory($categorycode);

Returns a href:  keys %$branches eq (branchcode,branchname) .

=cut
417
418sub GetBranchesInCategory($) {
419
0
    my ($categorycode) = @_;
420
0
        my @branches;
421
0
        my $dbh = C4::Context->dbh();
422
0
        my $sth=$dbh->prepare( "SELECT b.branchcode FROM branchrelations r, branches b
423                                                        where r.branchcode=b.branchcode and r.categorycode=?");
424
0
    $sth->execute($categorycode);
425
0
        while (my $branch = $sth->fetchrow) {
426
0
                push @branches, $branch;
427        }
428
0
        $sth->finish();
429
0
        return( \@branches );
430}
431
432 - 439
=head2 GetBranchInfo

$results = GetBranchInfo($branchcode);

returns C<$results>, a reference to an array of hashes containing branches.
if $branchcode, just this branch, with associated categories.
if ! $branchcode && $categorytype, all branches in the category.
=cut
440
441sub GetBranchInfo {
442
0
    my ($branchcode,$categorytype) = @_;
443
0
    my $dbh = C4::Context->dbh;
444
0
    my $sth;
445
446
447
0
        if ($branchcode) {
448
0
        $sth =
449          $dbh->prepare(
450            "Select * from branches where branchcode = ? order by branchcode");
451
0
        $sth->execute($branchcode);
452    }
453    else {
454
0
        $sth = $dbh->prepare("Select * from branches order by branchcode");
455
0
        $sth->execute();
456    }
457
0
    my @results;
458
0
    while ( my $data = $sth->fetchrow_hashref ) {
459
0
                my @bind = ($data->{'branchcode'});
460
0
        my $query= "select r.categorycode from branchrelations r";
461
0
                $query .= ", branchcategories c " if($categorytype);
462
0
                $query .= " where branchcode=? ";
463
0
                if($categorytype) {
464
0
                        $query .= " and c.categorytype=? and r.categorycode=c.categorycode";
465
0
                        push @bind, $categorytype;
466                }
467
0
        my $nsth=$dbh->prepare($query);
468
0
                $nsth->execute( @bind );
469
0
        my @cats = ();
470
0
        while ( my ($cat) = $nsth->fetchrow_array ) {
471
0
            push( @cats, $cat );
472        }
473
0
        $nsth->finish;
474
0
        $data->{'categories'} = \@cats;
475
0
        push( @results, $data );
476    }
477
0
    $sth->finish;
478
0
    return \@results;
479}
480
481 - 485
=head2 DelBranch

&DelBranch($branchcode);

=cut
486
487sub DelBranch {
488
0
    my ($branchcode) = @_;
489
0
    my $dbh = C4::Context->dbh;
490
0
    my $sth = $dbh->prepare("delete from branches where branchcode = ?");
491
0
    $sth->execute($branchcode);
492
0
    $sth->finish;
493}
494
495 - 500
=head2 ModBranchCategoryInfo

&ModBranchCategoryInfo($data);
sets the data from the editbranch form, and writes to the database...

=cut
501
502sub ModBranchCategoryInfo {
503
0
    my ($data) = @_;
504
0
    my $dbh = C4::Context->dbh;
505
0
    if ($data->{'add'}){
506        # we are doing an insert
507
0
        my $sth = $dbh->prepare("INSERT INTO branchcategories (categorycode,categoryname,codedescription,categorytype) VALUES (?,?,?,?)");
508
0
        $sth->execute(uc( $data->{'categorycode'} ),$data->{'categoryname'}, $data->{'codedescription'},$data->{'categorytype'} );
509
0
        $sth->finish();
510    }
511    else {
512        # modifying
513
0
        my $sth = $dbh->prepare("UPDATE branchcategories SET categoryname=?,codedescription=?,categorytype=? WHERE categorycode=?");
514
0
        $sth->execute($data->{'categoryname'}, $data->{'codedescription'},$data->{'categorytype'},uc( $data->{'categorycode'} ) );
515
0
        $sth->finish();
516    }
517}
518
519 - 525
=head2 CheckCategoryUnique

if (CheckCategoryUnique($categorycode)){
  # do something
}

=cut
526
527sub CheckCategoryUnique {
528
0
    my $categorycode = shift;
529
0
    my $dbh = C4::Context->dbh;
530
0
    my $sth = $dbh->prepare("SELECT categorycode FROM branchcategories WHERE categorycode = ?");
531
0
    $sth->execute(uc( $categorycode) );
532
0
    if (my $data = $sth->fetchrow_hashref){
533
0
        return 0;
534    }
535    else {
536
0
        return 1;
537    }
538}
539
540
541 - 545
=head2 DeleteBranchCategory

DeleteBranchCategory($categorycode);

=cut
546
547sub DelBranchCategory {
548
0
    my ($categorycode) = @_;
549
0
    my $dbh = C4::Context->dbh;
550
0
    my $sth = $dbh->prepare("delete from branchcategories where categorycode = ?");
551
0
    $sth->execute($categorycode);
552
0
    $sth->finish;
553}
554
555 - 559
=head2 CheckBranchCategorycode

$number_rows_affected = CheckBranchCategorycode($categorycode);

=cut
560
561sub CheckBranchCategorycode {
562
563    # check to see if the branchcode is being used in the database somewhere....
564
0
    my ($categorycode) = @_;
565
0
    my $dbh = C4::Context->dbh;
566
0
    my $sth =
567      $dbh->prepare(
568        "select count(*) from branchrelations where categorycode=?");
569
0
    $sth->execute($categorycode);
570
0
    my ($total) = $sth->fetchrow_array;
571
0
    return $total;
572}
573
574sub get_branch_code_from_name {
575
0
   my @branch_name = @_;
576
0
   my $query = "SELECT branchcode FROM branches WHERE branchname=?;";
577
0
   my $dbh = C4::Context->dbh();
578
0
   my $sth = $dbh->prepare($query);
579
0
   $sth->execute(@branch_name);
580
0
   return $sth->fetchrow_array;
581}
582
5831;