View | Details | Raw Unified | Return to bug 5872
Collapse All | Expand All

(-)a/C4/IssuingRules.pm (-1 / +261 lines)
Line 0 Link Here
0
- 
1
package C4::IssuingRules;
2
3
# Copyright 2009 BibLibre SARL
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it under the
8
# terms of the GNU General Public License as published by the Free Software
9
# Foundation; either version 2 of the License, or (at your option) any later
10
# version.
11
#
12
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License along with
17
# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18
# Suite 330, Boston, MA  02111-1307 USA
19
20
use strict;
21
use warnings;
22
use C4::Context;
23
use C4::Koha;
24
use C4::SQLHelper qw( SearchInTable InsertInTable UpdateInTable DeleteInTable );
25
use Memoize;
26
27
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
28
29
BEGIN {
30
	# set the version for version checking
31
	$VERSION = 3.0.5;
32
	@ISA    = qw(Exporter);
33
	@EXPORT = qw(
34
	    &GetIssuingRule
35
		&GetIssuingRulesByBranchCode
36
		&GetIssuingRules
37
		&AddIssuingRule
38
		&ModIssuingRule
39
		&DelIssuingRule
40
	);
41
}
42
43
=head1 NAME
44
45
C4::IssuingRules - Koha issuing rules module
46
47
=head1 SYNOPSIS
48
49
use C4::IssuingRules;
50
51
=head1 DESCRIPTION
52
53
The functions in this module deal with issuing rules.
54
55
=head1 FUNCTIONS
56
57
=head2 GetIssuingRule
58
59
Compute the issuing rule for an itemtype, a borrower category and a branch.
60
Returns a hashref from the issuingrules table.
61
62
my $rule = &GetIssuingRule($categorycode, $itemtype, $branchcode);
63
64
The rules are applied from most specific to less specific, using the first found in this order:
65
    * same library, same patron type, same item type
66
    * same library, same patron type, default item type
67
    * same library, default patron type, same item type
68
    * same library, default patron type, default item type
69
    * default library, same patron type, same item type
70
    * default library, same patron type, default item type
71
    * default library, default patron type, same item type
72
    * default library, default patron type, default item type
73
74
The values in the returned hashref are inherited from a more generic rules if undef.
75
76
=cut
77
#Caching GetIssuingRule
78
memoize('GetIssuingRule');
79
80
sub GetIssuingRule {
81
    my ( $categorycode, $itemtype, $branchcode ) = @_;
82
    $categorycode||="*";
83
    $itemtype||="*";
84
    $branchcode||="*";
85
86
    # This configuration table defines the order of inheritance. We'll loop over it.
87
    my @attempts = (
88
        [ "*"          , "*"      , "*"         ],
89
        [ "*"          , $itemtype, "*"         ],
90
        [ $categorycode, "*"      , "*"         ],
91
        [ $categorycode, $itemtype, "*"         ],
92
        [ "*"          , "*"      , $branchcode ],
93
        [ "*"          , $itemtype, $branchcode ],
94
        [ $categorycode, "*"      , $branchcode ],
95
        [ $categorycode, $itemtype, $branchcode ],
96
    );
97
98
    # This complex query returns a nested hashref, so we can access a rule using :
99
    # my $rule = $$rules{$categorycode}{$itemtype}{$branchcode};
100
    # this will be usefull in the inheritance computation code
101
    my $dbh = C4::Context->dbh;
102
    my $rules = $dbh->selectall_hashref(
103
        "SELECT * FROM issuingrules where branchcode IN ('*',?) and itemtype IN ('*', ?) and categorycode IN ('*',?)",
104
        ["branchcode", "itemtype", "categorycode"],
105
        undef,
106
        ( $branchcode, $itemtype, $categorycode )
107
    );
108
109
    # This block is for inheritance. It loops over rules returned by the 
110
    # previous query. If a value is found in a more specific rule, it replaces 
111
    # the old value from the more generic rule.
112
    my $oldrule;
113
    for my $attempt ( @attempts ) {
114
        if ( my $rule = $$rules{@$attempt[2]}{@$attempt[1]}{@$attempt[0]} ) {
115
            if ( $oldrule ) {
116
                for ( keys %$oldrule ) {
117
                    if ( defined $rule->{$_} ) {
118
                        $oldrule->{$_} = $rule->{$_};
119
                    }
120
                }
121
            } else {
122
                $oldrule = $rule;
123
            }
124
        }
125
    }
126
    if($oldrule){
127
        return $oldrule;
128
    }else{
129
        return {
130
            'itemtype'          => $itemtype,
131
            'categorycode'      => $categorycode,
132
            'branchcode'        => $branchcode,
133
            'holdspickupdelay'  => 0,
134
       #     'maxissueqty'       => 0,
135
            'renewalsallowed'   => 0,
136
            'firstremind'       => 0,
137
            'accountsent'       => 0,
138
            'reservecharge'     => 0,
139
            'fine'              => 0,
140
            'restrictedtype'    => 0,
141
            'rentaldiscount'    => 0,
142
            'chargename'        => 0,
143
            'finedays'          => 0,
144
            'holdrestricted'    => 0,
145
            'allowonshelfholds' => 0,
146
            'reservesallowed'   => 0,
147
            'chargeperiod'      => 0,
148
       #     'issuelength'       => 0,
149
            'renewalperiod'     => 0,
150
        };
151
    }
152
}
153
154
=head2 GetIssuingRulesByBranchCode
155
  
156
  my @issuingrules = &GetIssuingRulesByBranchCode($branchcode);
157
158
  Retruns a list of hashref from the issuingrules Koha table for a given 
159
  branchcode.
160
  Each hashref will contain data from issuingrules plus human readable names of 
161
  patron and item categories.
162
163
=cut
164
165
sub GetIssuingRulesByBranchCode {
166
    my $dbh = C4::Context->dbh;
167
    my $sth = $dbh->prepare("
168
        SELECT issuingrules.*, itemtypes.description AS humanitemtype, categories.description AS humancategorycode
169
        FROM issuingrules
170
        LEFT JOIN itemtypes
171
            ON (itemtypes.itemtype = issuingrules.itemtype)
172
        LEFT JOIN categories
173
            ON (categories.categorycode = issuingrules.categorycode)
174
        WHERE issuingrules.branchcode = ?
175
        ORDER BY humancategorycode, humanitemtype
176
    ");
177
    $sth->execute(shift);
178
    
179
    my $res = $sth->fetchall_arrayref({});
180
    
181
    return @$res;
182
}
183
184
=head2 GetIssuingRules
185
  
186
  my @issuingrules = &GetIssuingRules({
187
      branchcode   => $branch, 
188
      categorycode => $input->param('categorycode'), 
189
      itemtype     => $input->param('itemtype'),
190
  });
191
192
  Get an issuing rule from Koha database.
193
  An alias for SearchInTable, see C4::SQLHelper for more help.
194
195
=cut
196
197
sub GetIssuingRules {
198
    my $res = SearchInTable('issuingrules', shift);
199
    return @$res;
200
}
201
202
=head2 AddIssuingRule
203
204
  my $issuingrule = {
205
      branchcode      => $branch,
206
      categorycode    => $input->param('categorycode'),
207
      itemtype        => $input->param('itemtype'),
208
      maxissueqty     => $maxissueqty,
209
      renewalsallowed => $input->param('renewalsallowed'),
210
      reservesallowed => $input->param('reservesallowed'),
211
      issuelength     => $input->param('issuelength'),
212
      fine            => $input->param('fine'),
213
      finedays        => $input->param('finedays'),
214
      firstremind     => $input->param('firstremind'),
215
      chargeperiod    => $input->param('chargeperiod'),
216
  };
217
  
218
  &AddIssuingRule( $issuingrule );
219
220
  Adds an issuing rule to Koha database.
221
  An alias for InsertInTable, see C4::SQLHelper for more help.
222
223
=cut
224
225
sub AddIssuingRule { InsertInTable('issuingrules',shift); }
226
227
=head2 ModIssuingRule
228
  
229
  &ModIssuingRule( $issuingrule );
230
231
  Update an issuing rule of the Koha database.
232
  An alias for UpdateInTable, see C4::SQLHelper for more help.
233
234
=cut
235
236
sub ModIssuingRule { UpdateInTable('issuingrules',shift); }
237
238
=head2 DelIssuingRule
239
  
240
  DelIssuingRule({
241
      branchcode   => $branch, 
242
      categorycode => $input->param('categorycode'), 
243
      itemtype     => $input->param('itemtype'),
244
  });
245
246
  Delete an issuing rule from Koha database.
247
  An alias for DeleteInTable, see C4::SQLHelper for more help.
248
249
=cut
250
251
sub DelIssuingRule { DeleteInTable('issuingrules',shift); }
252
253
1;
254
255
=head1 AUTHOR
256
257
Koha Developement team <info@koha.org>
258
259
Jean-André Santoni <jeanandre.santoni@biblibre.com>
260
261
=cut

Return to bug 5872