Line 0
Link Here
|
|
|
1 |
package C4::Overdues::OverdueRulesMap; |
2 |
|
3 |
# Copyright 2015 Vaara-kirjastot |
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 |
17 |
# with Koha; if not, write to the Free Software Foundation, Inc., |
18 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
19 |
|
20 |
use Modern::Perl; |
21 |
|
22 |
use C4::Context qw(dbh); |
23 |
use Koha::Cache; |
24 |
|
25 |
use vars qw($VERSION @ISA @EXPORT); |
26 |
|
27 |
BEGIN { |
28 |
# set the version for version checking |
29 |
$VERSION = 3.19.01; |
30 |
require Exporter; |
31 |
@ISA = qw(Exporter); |
32 |
push @EXPORT, qw( |
33 |
&new |
34 |
&getRulesForBranch |
35 |
); |
36 |
} |
37 |
|
38 |
=head new |
39 |
|
40 |
my $orm = C4::Overdues::OverdueRulesMap->new(); |
41 |
|
42 |
Finds a OverdueRulesMap from Koha::Cache or instantiates a new one. |
43 |
|
44 |
OverdueRulesMap (orm) is a map-representation of the overduerules- and |
45 |
overduerules_transport_types-tables. |
46 |
The cache entry is valid for 300 seconds. |
47 |
|
48 |
See getRulesForBranch() for structural representation. |
49 |
=cut |
50 |
|
51 |
sub new { |
52 |
my ($class) = @_; |
53 |
|
54 |
my $cache = Koha::Cache->get_instance(); |
55 |
my $orm = $cache->get_from_cache('overdueRulesMap'); |
56 |
unless ($orm) { |
57 |
$orm = _getOverdueActionsMap(); |
58 |
bless $orm, $class; |
59 |
$cache->set_in_cache('overdueRulesMap', $orm, {expiry => 300}); |
60 |
} |
61 |
return $orm if ref $orm eq 'C4::Overdues::OverdueRulesMap'; |
62 |
return undef; |
63 |
} |
64 |
|
65 |
=head getRulesForBranch |
66 |
|
67 |
my $overduerules = $orm->getRulesForBranch('CPL'); |
68 |
|
69 |
Finds the overduerules and message_transport_types for the given branch. |
70 |
If the branch has even one overduerule defined for a borrower category, |
71 |
will use that and skip checking for any defaults even for the missing |
72 |
borrower categories. |
73 |
If the branch has no overduerules defined, will use the default rules. |
74 |
|
75 |
@RETURNS Hash, of overduerules for the given branch. The HASH looks like this: |
76 |
{ |
77 |
STAFF => { #Borrower Categories |
78 |
1 => { #Letter numbers |
79 |
delay => 12, |
80 |
letter => 'ODUE1', |
81 |
debarred => undef || 1, |
82 |
message_transport_types => { print => 1, |
83 |
sms => 1, |
84 |
... |
85 |
} |
86 |
}, |
87 |
2 => {..}, |
88 |
... |
89 |
}, |
90 |
PATRON => {...}, |
91 |
... |
92 |
} |
93 |
=cut |
94 |
|
95 |
sub getRulesForBranch { |
96 |
my ($self, $branchCode) = @_; |
97 |
|
98 |
return $self->{$branchCode} if $self->{$branchCode}; |
99 |
return $self->{''}; |
100 |
} |
101 |
|
102 |
sub _getOverdueActionsMap { |
103 |
|
104 |
my $map = {}; |
105 |
my $overduerules = _getOverduerules(); |
106 |
my $overduerules_transport_types = _getOverduerules_transport_types(); |
107 |
|
108 |
#Make a map out of the overduerules-table. |
109 |
foreach my $o (@$overduerules) { |
110 |
$map->{ $o->{branchcode} }-> |
111 |
{ $o->{categorycode} }-> |
112 |
{ 1 } |
113 |
= |
114 |
{ delay => $o->{delay1}, |
115 |
letter => $o->{letter1}, |
116 |
debarred => $o->{debarred1}, |
117 |
}; |
118 |
|
119 |
$map->{ $o->{branchcode} }-> |
120 |
{ $o->{categorycode} }-> |
121 |
{ 2 } |
122 |
= |
123 |
{ delay => $o->{delay2}, |
124 |
letter => $o->{letter2}, |
125 |
debarred => $o->{debarred2}, |
126 |
}; |
127 |
|
128 |
$map->{ $o->{branchcode} }-> |
129 |
{ $o->{categorycode} }-> |
130 |
{ 3 } |
131 |
= |
132 |
{ delay => $o->{delay3}, |
133 |
letter => $o->{letter3}, |
134 |
debarred => $o->{debarred3}, |
135 |
}; |
136 |
} |
137 |
|
138 |
#Merge a map of the overduerules_transport_types |
139 |
foreach my $ott (@$overduerules_transport_types) { |
140 |
$map->{ $ott->{branchcode} }-> |
141 |
{ $ott->{categorycode} }-> |
142 |
{ $ott->{letternumber} }-> |
143 |
{ 'message_transport_types' }-> |
144 |
{ $ott->{message_transport_type} } = 1; |
145 |
} |
146 |
return $map; |
147 |
} |
148 |
|
149 |
sub _getOverduerules { |
150 |
my $dbh = C4::Context->dbh(); |
151 |
my $sth = $dbh->prepare("SELECT * FROM overduerules"); |
152 |
$sth->execute(); |
153 |
my $overduerules = $sth->fetchall_arrayref({}); |
154 |
return $overduerules; |
155 |
} |
156 |
sub _getOverduerules_transport_types { |
157 |
my $dbh = C4::Context->dbh(); |
158 |
my $sth = $dbh->prepare("SELECT * FROM overduerules_transport_types"); |
159 |
$sth->execute(); |
160 |
my $overduerules_transport_types = $sth->fetchall_arrayref({}); |
161 |
return $overduerules_transport_types; |
162 |
} |
163 |
1; #Satisfy the compiler |