Patch from Galen Charlton, removing $Id$ $Log$ and $Revision$ from files
[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 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 use strict;
20 require Exporter;
21 use C4::Context;
22 use C4::Koha;
23
24 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
25
26 # set the version for version checking
27 $VERSION = 3.00;
28
29 =head1 NAME
30
31 C4::Branch - Koha branch module
32
33 =head1 SYNOPSIS
34
35 use C4::Branch;
36
37 =head1 DESCRIPTION
38
39 The functions in this module deal with branches.
40
41 =head1 FUNCTIONS
42
43 =cut
44
45 @ISA    = qw(Exporter);
46 @EXPORT = qw(
47    &GetBranchCategory
48    &GetBranchName
49    &GetBranch
50    &GetBranches
51    &GetBranchDetail
52    &get_branchinfos_of
53    &ModBranch
54    &CheckBranchCategorycode
55    &GetBranchInfo
56    &ModBranchCategoryInfo
57    &DelBranch
58    &DelBranchCategory
59 );
60
61 =head2 GetBranches
62
63   $branches = &GetBranches();
64   returns informations about ALL branches.
65   Create a branch selector with the following code
66   IndependantBranches Insensitive...
67   
68 =head3 in PERL SCRIPT
69
70 my $branches = GetBranches;
71 my @branchloop;
72 foreach my $thisbranch (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
82 =head3 in TEMPLATE
83             <select name="branch">
84                 <option value="">Default</option>
85             <!-- TMPL_LOOP name="branchloop" -->
86                 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="branchname" --></option>
87             <!-- /TMPL_LOOP -->
88             </select>
89
90 =cut
91
92 sub GetBranches {
93     my ($onlymine)=@_;
94     # returns a reference to a hash of references to ALL branches...
95     my %branches;
96     my $dbh = C4::Context->dbh;
97     my $sth;
98     my $query="SELECT * from branches";
99     if ($onlymine && C4::Context->userenv && C4::Context->userenv->{branch}){
100       $query .= " WHERE branchcode =".$dbh->quote(C4::Context->userenv->{branch});
101     }
102     $query.=" order by branchname";
103     $sth = $dbh->prepare($query);
104     $sth->execute;
105     while ( my $branch = $sth->fetchrow_hashref ) {
106         my $nsth =
107           $dbh->prepare(
108             "select categorycode from branchrelations where branchcode = ?");
109         $nsth->execute( $branch->{'branchcode'} );
110         while ( my ($cat) = $nsth->fetchrow_array ) {
111
112             # FIXME - This seems wrong. It ought to be
113             # $branch->{categorycodes}{$cat} = 1;
114             # otherwise, there's a namespace collision if there's a
115             # category with the same name as a field in the 'branches'
116             # table (i.e., don't create a category called "issuing").
117             # In addition, the current structure doesn't really allow
118             # you to list the categories that a branch belongs to:
119             # you'd have to list keys %$branch, and remove those keys
120             # that aren't fields in the "branches" table.
121             $branch->{$cat} = 1;
122         }
123         $branches{ $branch->{'branchcode'} } = $branch;
124     }
125     return ( \%branches );
126 }
127
128 =head2 GetBranchName
129
130 =cut
131
132 sub GetBranchName {
133     my ($branchcode) = @_;
134     my $dbh = C4::Context->dbh;
135     my $sth;
136     $sth = $dbh->prepare("Select branchname from branches where branchcode=?");
137     $sth->execute($branchcode);
138     my $branchname = $sth->fetchrow_array;
139     $sth->finish;
140     return ($branchname);
141 }
142
143 =head2 ModBranch
144
145 &ModBranch($newvalue);
146
147 This function modify an existing branches.
148
149 C<$newvalue> is a ref to an array wich is containt all the column from branches table.
150
151 =cut
152
153 sub ModBranch {
154     my ($data) = @_;
155     
156     my $dbh    = C4::Context->dbh;
157     if ($data->{add}) {
158         my $query  = "
159             INSERT INTO branches
160             (branchcode,branchname,branchaddress1,
161             branchaddress2,branchaddress3,branchphone,
162             branchfax,branchemail,branchip,branchprinter)
163             VALUES (?,?,?,?,?,?,?,?,?,?)
164         ";
165         my $sth    = $dbh->prepare($query);
166         $sth->execute(
167             $data->{'branchcode'},       $data->{'branchname'},
168             $data->{'branchaddress1'},   $data->{'branchaddress2'},
169             $data->{'branchaddress3'},   $data->{'branchphone'},
170             $data->{'branchfax'},        $data->{'branchemail'},
171             $data->{'branchip'},         $data->{'branchprinter'},
172         );
173     } else {
174         my $query  = "
175             UPDATE branches
176             SET branchname=?,branchaddress1=?,
177                 branchaddress2=?,branchaddress3=?,branchphone=?,
178                 branchfax=?,branchemail=?,branchip=?,branchprinter=?
179             WHERE branchcode=?
180         ";
181         my $sth    = $dbh->prepare($query);
182         $sth->execute(
183             $data->{'branchname'},
184             $data->{'branchaddress1'},   $data->{'branchaddress2'},
185             $data->{'branchaddress3'},   $data->{'branchphone'},
186             $data->{'branchfax'},        $data->{'branchemail'},
187             $data->{'branchip'},         $data->{'branchprinter'},
188             $data->{'branchcode'},
189         );
190     }
191     # sort out the categories....
192     my @checkedcats;
193     my $cats = GetBranchCategory();
194     foreach my $cat (@$cats) {
195         my $code = $cat->{'categorycode'};
196         if ( $data->{$code} ) {
197             push( @checkedcats, $code );
198         }
199     }
200     my $branchcode = uc( $data->{'branchcode'} );
201     my $branch     = GetBranchInfo($branchcode);
202     $branch = $branch->[0];
203     my $branchcats = $branch->{'categories'};
204     my @addcats;
205     my @removecats;
206     foreach my $bcat (@$branchcats) {
207
208         unless ( grep { /^$bcat$/ } @checkedcats ) {
209             push( @removecats, $bcat );
210         }
211     }
212     foreach my $ccat (@checkedcats) {
213         unless ( grep { /^$ccat$/ } @$branchcats ) {
214             push( @addcats, $ccat );
215         }
216     }
217     foreach my $cat (@addcats) {
218         my $sth =
219           $dbh->prepare(
220 "insert into branchrelations (branchcode, categorycode) values(?, ?)"
221           );
222         $sth->execute( $branchcode, $cat );
223         $sth->finish;
224     }
225     foreach my $cat (@removecats) {
226         my $sth =
227           $dbh->prepare(
228             "delete from branchrelations where branchcode=? and categorycode=?"
229           );
230         $sth->execute( $branchcode, $cat );
231         $sth->finish;
232     }
233 }
234
235 =head2 GetBranchCategory
236
237 $results = GetBranchCategory($categorycode);
238
239 C<$results> is an ref to an array.
240
241 =cut
242
243 sub GetBranchCategory {
244
245     # returns a reference to an array of hashes containing branches,
246     my ($catcode) = @_;
247     my $dbh = C4::Context->dbh;
248     my $sth;
249
250     #    print DEBUG "GetBranchCategory: entry: catcode=".cvs($catcode)."\n";
251     if ($catcode) {
252         $sth =
253           $dbh->prepare(
254             "select * from branchcategories where categorycode = ?");
255         $sth->execute($catcode);
256     }
257     else {
258         $sth = $dbh->prepare("Select * from branchcategories");
259         $sth->execute();
260     }
261     my @results;
262     while ( my $data = $sth->fetchrow_hashref ) {
263         push( @results, $data );
264     }
265     $sth->finish;
266
267     #    print DEBUG "GetBranchCategory: exit: returning ".cvs(\@results)."\n";
268     return \@results;
269 }
270
271 =head2 GetBranch
272
273 $branch = GetBranch( $query, $branches );
274
275 =cut
276
277 sub GetBranch ($$) {
278     my ( $query, $branches ) = @_;    # get branch for this query from branches
279     my $branch = $query->param('branch');
280     my %cookie = $query->cookie('userenv');
281     ($branch)                || ($branch = $cookie{'branchname'});
282     ( $branches->{$branch} ) || ( $branch = ( keys %$branches )[0] );
283     return $branch;
284 }
285
286 =head2 GetBranchDetail
287
288   $branchname = &GetBranchDetail($branchcode);
289
290 Given the branch code, the function returns the corresponding
291 branch name for a comprehensive information display
292
293 =cut
294
295 sub GetBranchDetail {
296     my ($branchcode) = @_;
297     my $dbh = C4::Context->dbh;
298     my $sth = $dbh->prepare("SELECT * FROM branches WHERE branchcode = ?");
299     $sth->execute($branchcode);
300     my $branchname = $sth->fetchrow_hashref();
301     $sth->finish();
302     return $branchname;
303 }
304
305
306 =head2 get_branchinfos_of
307
308   my $branchinfos_of = get_branchinfos_of(@branchcodes);
309
310 Associates a list of branchcodes to the information of the branch, taken in
311 branches table.
312
313 Returns a href where keys are branchcodes and values are href where keys are
314 branch information key.
315
316   print 'branchname is ', $branchinfos_of->{$code}->{branchname};
317
318 =cut
319
320 sub get_branchinfos_of {
321     my @branchcodes = @_;
322
323     my $query = '
324     SELECT branchcode,
325        branchname
326     FROM branches
327     WHERE branchcode IN ('
328       . join( ',', map( { "'" . $_ . "'" } @branchcodes ) ) . ')
329 ';
330     return C4::Koha::get_infos_of( $query, 'branchcode' );
331 }
332
333 =head2 GetBranchInfo
334
335 $results = GetBranchInfo($branchcode);
336
337 returns C<$results>, a reference to an array of hashes containing branches.
338
339 =cut
340
341 sub GetBranchInfo {
342     my ($branchcode) = @_;
343     my $dbh = C4::Context->dbh;
344     my $sth;
345     if ($branchcode) {
346         $sth =
347           $dbh->prepare(
348             "Select * from branches where branchcode = ? order by branchcode");
349         $sth->execute($branchcode);
350     }
351     else {
352         $sth = $dbh->prepare("Select * from branches order by branchcode");
353         $sth->execute();
354     }
355     my @results;
356     while ( my $data = $sth->fetchrow_hashref ) {
357         my $nsth =
358           $dbh->prepare(
359             "select categorycode from branchrelations where branchcode = ?");
360         $nsth->execute( $data->{'branchcode'} );
361         my @cats = ();
362         while ( my ($cat) = $nsth->fetchrow_array ) {
363             push( @cats, $cat );
364         }
365         $nsth->finish;
366         $data->{'categories'} = \@cats;
367         push( @results, $data );
368     }
369     $sth->finish;
370     return \@results;
371 }
372
373 =head2 DelBranch
374
375 &DelBranch($branchcode);
376
377 =cut
378
379 sub DelBranch {
380     my ($branchcode) = @_;
381     my $dbh = C4::Context->dbh;
382     my $sth = $dbh->prepare("delete from branches where branchcode = ?");
383     $sth->execute($branchcode);
384     $sth->finish;
385 }
386
387 =head2 ModBranchCategoryInfo
388
389 &ModBranchCategoryInfo($data);
390 sets the data from the editbranch form, and writes to the database...
391
392 =cut
393
394 sub ModBranchCategoryInfo {
395
396     my ($data) = @_;
397     my $dbh    = C4::Context->dbh;
398     my $sth    = $dbh->prepare("replace branchcategories (categorycode,categoryname,codedescription) values (?,?,?)");
399     $sth->execute(uc( $data->{'categorycode'} ),$data->{'categoryname'}, $data->{'codedescription'} );
400     $sth->finish;
401 }
402
403 =head2 DeleteBranchCategory
404
405 DeleteBranchCategory($categorycode);
406
407 =cut
408
409 sub DelBranchCategory {
410     my ($categorycode) = @_;
411     my $dbh = C4::Context->dbh;
412     my $sth = $dbh->prepare("delete from branchcategories where categorycode = ?");
413     $sth->execute($categorycode);
414     $sth->finish;
415 }
416
417 =head2 CheckBranchCategorycode
418
419 $number_rows_affected = CheckBranchCategorycode($categorycode);
420
421 =cut
422
423 sub CheckBranchCategorycode {
424
425     # check to see if the branchcode is being used in the database somewhere....
426     my ($categorycode) = @_;
427     my $dbh            = C4::Context->dbh;
428     my $sth            =
429       $dbh->prepare(
430         "select count(*) from branchrelations where categorycode=?");
431     $sth->execute($categorycode);
432     my ($total) = $sth->fetchrow_array;
433     return $total;
434 }
435
436
437
438 =head1 AUTHOR
439
440 Koha Developement team <info@koha.org>
441
442 =cut