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