Bug 15479 Add tests for ILS.pm
[koha.git] / C4 / Branch.pm
1 package C4::Branch;
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
19 use strict;
20 #use warnings; FIXME - Bug 2505
21 require Exporter;
22 use C4::Context;
23 use Koha::LibraryCategories;
24
25 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
26
27 BEGIN {
28         # set the version for version checking
29     $VERSION = 3.07.00.049;
30         @ISA    = qw(Exporter);
31         @EXPORT = qw(
32                 &GetBranchName
33                 &GetBranch
34                 &GetBranches
35                 &GetBranchesLoop
36                 &GetBranchDetail
37                 &ModBranch
38                 &GetBranchInfo
39                 &GetBranchesInCategory
40                 &mybranch
41         );
42     @EXPORT_OK = qw( &onlymine &mybranch );
43 }
44
45 =head1 NAME
46
47 C4::Branch - Koha branch module
48
49 =head1 SYNOPSIS
50
51 use C4::Branch;
52
53 =head1 DESCRIPTION
54
55 The functions in this module deal with branches.
56
57 =head1 FUNCTIONS
58
59 =head2 GetBranches
60
61   $branches = &GetBranches();
62
63 Returns informations about ALL branches, IndependentBranches Insensitive.
64 GetBranchInfo() returns the same information.
65
66 Create a branch selector with the following code.
67
68 =head3 in PERL SCRIPT
69
70     my $branches = GetBranches;
71     my @branchloop;
72     foreach my $thisbranch (sort keys %$branches) {
73         my $selected = 1 if $thisbranch eq $branch;
74         my %row =(value => $thisbranch,
75                     selected => $selected,
76                     branchname => $branches->{$thisbranch}->{branchname},
77                 );
78         push @branchloop, \%row;
79     }
80
81 =head3 in TEMPLATE
82
83     <select name="branch" id="branch">
84         <option value=""></option>
85             [% FOREACH branchloo IN branchloop %]
86                 [% IF ( branchloo.selected ) %]
87                     <option value="[% branchloo.value %]" selected="selected">[% branchloo.branchname %]</option>
88                 [% ELSE %]
89                     <option value="[% branchloo.value %]" >[% branchloo.branchname %]</option>
90                 [% END %]
91             [% END %]
92     </select>
93
94 =head4 Note that you often will want to just use GetBranchesLoop, for exactly the example above.
95
96 =cut
97
98 sub GetBranches {
99     my ($onlymine) = @_;
100
101     # returns a reference to a hash of references to ALL branches...
102     my %branches;
103     my $dbh = C4::Context->dbh;
104     my $sth;
105     my $query = "SELECT * FROM branches";
106     my @bind_parameters;
107     if ( $onlymine && C4::Context->userenv && C4::Context->userenv->{branch} ) {
108         $query .= ' WHERE branchcode = ? ';
109         push @bind_parameters, C4::Context->userenv->{branch};
110     }
111     $query .= " ORDER BY branchname";
112     $sth = $dbh->prepare($query);
113     $sth->execute(@bind_parameters);
114
115     my $relations_sth =
116       $dbh->prepare("SELECT branchcode,categorycode FROM branchrelations");
117     $relations_sth->execute();
118     my %relations;
119     while ( my $rel = $relations_sth->fetchrow_hashref ) {
120         push @{ $relations{ $rel->{branchcode} } }, $rel->{categorycode};
121     }
122
123     while ( my $branch = $sth->fetchrow_hashref ) {
124         foreach my $cat ( @{ $relations{ $branch->{branchcode} } } ) {
125             $branch->{category}{$cat} = 1;
126         }
127         $branches{ $branch->{'branchcode'} } = $branch;
128     }
129     return ( \%branches );
130 }
131
132 sub onlymine {
133     return
134          C4::Context->preference('IndependentBranches')
135       && C4::Context->userenv
136       && !C4::Context->IsSuperLibrarian()
137       && C4::Context->userenv->{branch};
138 }
139
140 # always returns a string for OK comparison via "eq" or "ne"
141 sub mybranch {
142     C4::Context->userenv           or return '';
143     return C4::Context->userenv->{branch} || '';
144 }
145
146 sub GetBranchesLoop {  # since this is what most pages want anyway
147     my $branch   = @_ ? shift : mybranch();     # optional first argument is branchcode of "my branch", if preselection is wanted.
148     my $onlymine = @_ ? shift : onlymine();
149     my $branches = GetBranches($onlymine);
150     my @loop;
151     foreach my $branchcode ( sort { uc($branches->{$a}->{branchname}) cmp uc($branches->{$b}->{branchname}) } keys %$branches ) {
152         push @loop, {
153             value      => $branchcode,
154             branchcode => $branchcode,
155             selected   => ($branchcode eq $branch) ? 1 : 0,
156             branchname => $branches->{$branchcode}->{branchname},
157         };
158     }
159     return \@loop;
160 }
161
162 =head2 GetBranchName
163
164 =cut
165
166 sub GetBranchName {
167     my ($branchcode) = @_;
168     my $dbh = C4::Context->dbh;
169     my $sth;
170     $sth = $dbh->prepare("Select branchname from branches where branchcode=?");
171     $sth->execute($branchcode);
172     my $branchname = $sth->fetchrow_array;
173     return ($branchname);
174 }
175
176 =head2 ModBranch
177
178 $error = &ModBranch($newvalue);
179
180 This function modifies an existing branch
181
182 C<$newvalue> is a ref to an array which contains all the columns from branches table.
183
184 =cut
185
186 sub ModBranch {
187     my ($data) = @_;
188     
189     my $dbh    = C4::Context->dbh;
190     if ($data->{add}) {
191         my $query  = "
192             INSERT INTO branches
193             (branchcode,branchname,branchaddress1,
194             branchaddress2,branchaddress3,branchzip,branchcity,branchstate,
195             branchcountry,branchphone,branchfax,branchemail,
196             branchurl,branchip,branchprinter,branchnotes,opac_info,
197             branchreplyto, branchreturnpath)
198             VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
199         ";
200         my $sth    = $dbh->prepare($query);
201         $sth->execute(
202             $data->{'branchcode'},       $data->{'branchname'},
203             $data->{'branchaddress1'},   $data->{'branchaddress2'},
204             $data->{'branchaddress3'},   $data->{'branchzip'},
205             $data->{'branchcity'},       $data->{'branchstate'},
206             $data->{'branchcountry'},
207             $data->{'branchphone'},      $data->{'branchfax'},
208             $data->{'branchemail'},      $data->{'branchurl'},
209             $data->{'branchip'},         $data->{'branchprinter'},
210             $data->{'branchnotes'},      $data->{opac_info},
211             $data->{'branchreplyto'},    $data->{'branchreturnpath'}
212         );
213         return 1 if $dbh->err;
214     } else {
215         my $query  = "
216             UPDATE branches
217             SET branchname=?,branchaddress1=?,
218                 branchaddress2=?,branchaddress3=?,branchzip=?,
219                 branchcity=?,branchstate=?,branchcountry=?,branchphone=?,
220                 branchfax=?,branchemail=?,branchurl=?,branchip=?,
221                 branchprinter=?,branchnotes=?,opac_info=?,
222                 branchreplyto=?, branchreturnpath=?
223             WHERE branchcode=?
224         ";
225         my $sth    = $dbh->prepare($query);
226         $sth->execute(
227             $data->{'branchname'},
228             $data->{'branchaddress1'},   $data->{'branchaddress2'},
229             $data->{'branchaddress3'},   $data->{'branchzip'},
230             $data->{'branchcity'},       $data->{'branchstate'},       
231             $data->{'branchcountry'},
232             $data->{'branchphone'},      $data->{'branchfax'},
233             $data->{'branchemail'},      $data->{'branchurl'},
234             $data->{'branchip'},         $data->{'branchprinter'},
235             $data->{'branchnotes'},      $data->{opac_info},
236             $data->{'branchreplyto'},    $data->{'branchreturnpath'},
237             $data->{'branchcode'},
238         );
239     }
240     # sort out the categories....
241     my @checkedcats;
242     my @cats = Koha::LibraryCategories->search;
243     foreach my $cat (@cats) {
244         my $code = $cat->categorycode;
245         if ( $data->{$code} ) {
246             push( @checkedcats, $code );
247         }
248     }
249     my $branchcode = uc( $data->{'branchcode'} );
250     my $branch     = GetBranchInfo($branchcode);
251     $branch = $branch->[0];
252     my $branchcats = $branch->{'categories'};
253     my @addcats;
254     my @removecats;
255     foreach my $bcat (@$branchcats) {
256
257         unless ( grep { /^$bcat$/ } @checkedcats ) {
258             push( @removecats, $bcat );
259         }
260     }
261     foreach my $ccat (@checkedcats) {
262         unless ( grep { /^$ccat$/ } @$branchcats ) {
263             push( @addcats, $ccat );
264         }
265     }
266     foreach my $cat (@addcats) {
267         my $sth =
268           $dbh->prepare(
269 "insert into branchrelations (branchcode, categorycode) values(?, ?)"
270           );
271         $sth->execute( $branchcode, $cat );
272     }
273     foreach my $cat (@removecats) {
274         my $sth =
275           $dbh->prepare(
276             "delete from branchrelations where branchcode=? and categorycode=?"
277           );
278         $sth->execute( $branchcode, $cat );
279     }
280 }
281
282 =head2 GetBranch
283
284 $branch = GetBranch( $query, $branches );
285
286 =cut
287
288 sub GetBranch {
289     my ( $query, $branches ) = @_;    # get branch for this query from branches
290     my $branch = $query->param('branch');
291     my %cookie = $query->cookie('userenv');
292     ($branch)                || ($branch = $cookie{'branchname'});
293     ( $branches->{$branch} ) || ( $branch = ( keys %$branches )[0] );
294     return $branch;
295 }
296
297 =head2 GetBranchDetail
298
299     $branch = &GetBranchDetail($branchcode);
300
301 Given the branch code, the function returns a
302 hashref for the corresponding row in the branches table.
303
304 =cut
305
306 sub GetBranchDetail {
307     my ($branchcode) = shift or return;
308     my $sth = C4::Context->dbh->prepare("SELECT * FROM branches WHERE branchcode = ?");
309     $sth->execute($branchcode);
310     return $sth->fetchrow_hashref();
311 }
312
313 =head2 GetBranchesInCategory
314
315   my $branches = GetBranchesInCategory($categorycode);
316
317 Returns a href:  keys %$branches eq (branchcode,branchname) .
318
319 =cut
320
321 sub GetBranchesInCategory {
322     my ($categorycode) = @_;
323         my @branches;
324         my $dbh = C4::Context->dbh();
325         my $sth=$dbh->prepare( "SELECT b.branchcode FROM branchrelations r, branches b 
326                                                         where r.branchcode=b.branchcode and r.categorycode=?");
327     $sth->execute($categorycode);
328         while (my $branch = $sth->fetchrow) {
329                 push @branches, $branch;
330         }
331         return( \@branches );
332 }
333
334 =head2 GetBranchInfo
335
336 $results = GetBranchInfo($branchcode);
337
338 returns C<$results>, a reference to an array of hashes containing branches.
339 if $branchcode, just this branch, with associated categories.
340 if ! $branchcode && $categorytype, all branches in the category.
341
342 =cut
343
344 sub GetBranchInfo {
345     my ($branchcode,$categorytype) = @_;
346     my $dbh = C4::Context->dbh;
347     my $sth;
348
349
350         if ($branchcode) {
351         $sth =
352           $dbh->prepare(
353             "Select * from branches where branchcode = ? order by branchcode");
354         $sth->execute($branchcode);
355     }
356     else {
357         $sth = $dbh->prepare("Select * from branches order by branchcode");
358         $sth->execute();
359     }
360     my @results;
361     while ( my $data = $sth->fetchrow_hashref ) {
362                 my @bind = ($data->{'branchcode'});
363         my $query= "select r.categorycode from branchrelations r";
364                 $query .= ", branchcategories c " if($categorytype);
365                 $query .= " where  branchcode=? ";
366                 if($categorytype) { 
367                         $query .= " and c.categorytype=? and r.categorycode=c.categorycode";
368                         push @bind, $categorytype;
369                 }
370         my $nsth=$dbh->prepare($query);
371                 $nsth->execute( @bind );
372         my @cats = ();
373         while ( my ($cat) = $nsth->fetchrow_array ) {
374             push( @cats, $cat );
375         }
376         $data->{'categories'} = \@cats;
377         push( @results, $data );
378     }
379     return \@results;
380 }
381
382 1;
383 __END__
384
385 =head1 AUTHOR
386
387 Koha Development Team <http://koha-community.org/>
388
389 =cut