Skip to content

Commit 6517b04

Browse files
author
roger
committed
First version with basic functionality added.
1 parent ea39a7e commit 6517b04

25 files changed

+15095
-89
lines changed

Koha/Plugin/Com/LMSCloud/LatePaymentClaiming.pm

Lines changed: 297 additions & 26 deletions
Large diffs are not rendered by default.

Koha/Plugin/Com/LMSCloud/LatePaymentClaiming/BanActions.pm

Lines changed: 521 additions & 0 deletions
Large diffs are not rendered by default.

Koha/Plugin/Com/LMSCloud/LatePaymentClaiming/LatePaymentClaiming.pm

Lines changed: 563 additions & 0 deletions
Large diffs are not rendered by default.

Koha/Plugin/Com/LMSCloud/LatePaymentClaiming/LatePaymentClaiming/BanActions.pm

Lines changed: 521 additions & 0 deletions
Large diffs are not rendered by default.

Koha/Plugin/Com/LMSCloud/LatePaymentClaiming/LatePaymentClaiming/LatePaymentClaiming.pm

Lines changed: 563 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 288 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,288 @@
1+
package Koha::Plugin::Com::LMSCloud::LatePaymentClaiming::LatePaymentClaimingConfiguration;
2+
3+
# This program is free software: you can redistribute it and/or modify
4+
# it under the terms of the GNU General Public License as published by
5+
# the Free Software Foundation, either version 3 of the License, or
6+
# (at your option) any later version.
7+
#
8+
# This program is distributed in the hope that it will be useful,
9+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
# GNU General Public License for more details.
12+
#
13+
# You should have received a copy of the GNU General Public License
14+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
15+
#
16+
# This program comes with ABSOLUTELY NO WARRANTY;
17+
18+
use Modern::Perl;
19+
use utf8;
20+
21+
use Data::Dumper;
22+
23+
use C4::Context;
24+
use JSON;
25+
26+
use Koha::Libraries;
27+
use Koha::Patron::Categories;
28+
29+
sub new {
30+
my ( $class, $args ) = @_;
31+
32+
my $self = {};
33+
bless $self, $class;
34+
35+
return $self;
36+
}
37+
38+
sub getConfigurationList {
39+
my $self = shift;
40+
41+
my $dbh = C4::Context->dbh;
42+
43+
my $sth = $dbh->prepare(q{
44+
SELECT lpcr.branchcode AS branchcode,
45+
lpcr.categorycode AS categorycode,
46+
MAX(lpcr.level) AS levels_defined
47+
FROM lmsc_late_payment_claim_rules lpcr
48+
GROUP BY lpcr.branchcode, lpcr.categorycode
49+
ORDER BY lpcr.branchcode, lpcr.categorycode
50+
});
51+
$sth->execute();
52+
my $configurations = [];
53+
while ( my $config = $sth->fetchrow_hashref ) {
54+
push @$configurations, {
55+
library_id => ($config->{branchcode} ? $config->{branchcode} : '*'),
56+
category_id => ($config->{categorycode} ? $config->{categorycode} : '*'),
57+
levels_defined => $config->{levels_defined}
58+
};
59+
}
60+
61+
my $branchcodes = {};
62+
foreach my $library( Koha::Libraries->search_filtered({ -or => [ mobilebranch => undef, mobilebranch => '' ] }, { order_by => 'branchname' })->as_list ) {
63+
$branchcodes->{$library->branchcode} = $library->branchname;
64+
}
65+
my $categories = {};
66+
foreach my $categorie( Koha::Patron::Categories->search({}, {order_by => ['description']})->as_list ) {
67+
$categories->{$categorie->categorycode} = $categorie->description;
68+
}
69+
70+
@$configurations = reverse sort {
71+
my $cmplib = compareLibraryId($a->{library_id},$b->{library_id},$branchcodes);
72+
return compareCategoryId($a->{category_id},$b->{category_id},$categories) if ( $cmplib == 0 );
73+
return $cmplib;
74+
} @$configurations;
75+
76+
return $configurations;
77+
}
78+
79+
sub compareLibraryId {
80+
my ($alib_id,$blib_id,$libraries) = @_;
81+
82+
if ( $alib_id eq '*' && $blib_id eq '*' ) {
83+
return 0;
84+
}
85+
elsif ( $alib_id eq '*' ) {
86+
return -1;
87+
}
88+
elsif ( $blib_id eq '*' ) {
89+
return 1;
90+
}
91+
else {
92+
my $alibname = ( exists($libraries->{$alib_id}) ? $libraries->{$alib_id} : $alib_id );
93+
my $blibname = ( exists($libraries->{$blib_id}) ? $libraries->{$blib_id} : $blib_id );
94+
return $alibname cmp $blibname;
95+
}
96+
}
97+
98+
sub compareCategoryId {
99+
my ($acateg_id,$bcateg_id,$categories) = @_;
100+
101+
if ( $acateg_id eq '*' && $bcateg_id eq '*' ) {
102+
return 0;
103+
}
104+
elsif ( $acateg_id eq '*' ) {
105+
return -1;
106+
}
107+
elsif ( $bcateg_id eq '*' ) {
108+
return 1;
109+
}
110+
else {
111+
my $acategname = ( exists($categories->{$acateg_id}) ? $categories->{$acateg_id} : $acateg_id );
112+
my $bcategname = ( exists($categories->{$bcateg_id}) ? $categories->{$bcateg_id} : $bcateg_id );
113+
return $acategname cmp $bcategname;
114+
}
115+
}
116+
117+
sub getConfiguration {
118+
my $self = shift;
119+
my $branchcode = shift;
120+
my $categorycode = shift;
121+
122+
my $dbh = C4::Context->dbh;
123+
124+
my @params = (0);
125+
my $sql = q{
126+
SELECT *
127+
FROM lmsc_late_payment_claim_rules lpcr
128+
WHERE level >= ?
129+
};
130+
131+
if ($branchcode && $branchcode ne '*') {
132+
$sql .= ' AND branchcode = ? ';
133+
push @params, $branchcode;
134+
} else {
135+
$sql .= ' AND branchcode IS NULL ';
136+
}
137+
if ($categorycode && $categorycode ne '*') {
138+
$sql .= ' AND categorycode = ? ';
139+
push @params, $categorycode;
140+
} else {
141+
$sql .= ' AND categorycode IS NULL ';
142+
}
143+
$sql .= ' ORDER BY level';
144+
145+
my $sth = $dbh->prepare($sql);
146+
147+
$sth->execute(@params);
148+
my $configuration = { library_id => ($branchcode ? $branchcode : '*'),
149+
category_id => ($categorycode ? $categorycode : '*'),
150+
level_configuration => [] };
151+
my $json = JSON->new->allow_nonref;
152+
153+
while ( my $config = $sth->fetchrow_hashref ) {
154+
push @{$configuration->{level_configuration}},
155+
{
156+
level => $config->{level},
157+
outstanding_fee_limit => $config->{outstanding_fee_limit},
158+
patron_selections => $json->decode( $config->{patron_selections} ),
159+
ban_actions => $json->decode( $config->{ban_actions} ),
160+
unban_actions => $json->decode( $config->{unban_actions} )
161+
};
162+
}
163+
164+
return $configuration;
165+
}
166+
167+
sub removeConfiguration {
168+
my $self = shift;
169+
my $branchcode = shift;
170+
my $categorycode = shift;
171+
172+
my $dbh = C4::Context->dbh;
173+
174+
my @params = (0);
175+
my $sql = q{DELETE FROM lmsc_late_payment_claim_rules WHERE level >= ?};
176+
if ($branchcode && $branchcode ne '*') {
177+
$sql .= ' AND branchcode = ? ';
178+
push @params, $branchcode;
179+
} else {
180+
$sql .= ' AND branchcode IS NULL ';
181+
}
182+
if ($categorycode && $categorycode ne '*') {
183+
$sql .= ' AND categorycode = ? ';
184+
push @params, $categorycode;
185+
} else {
186+
$sql .= ' AND categorycode IS NULL ';
187+
}
188+
189+
return $dbh->do($sql,undef,@params);
190+
}
191+
192+
sub saveConfiguration {
193+
my $self = shift;
194+
my $branchcode = shift;
195+
my $categorycode = shift;
196+
my $config = shift;
197+
198+
my $json = JSON->new->allow_nonref;
199+
200+
my $dbh = C4::Context->dbh;
201+
202+
my $checkLevel = 0;
203+
foreach my $levelConfig( sort { $a->{level} <=> $b->{level} } @{$config->{level_configuration}} ) {
204+
return if ( $checkLevel != $levelConfig->{level} );
205+
$checkLevel++;
206+
}
207+
208+
foreach my $levelConfig( sort { $a->{level} <=> $b->{level} } @{$config->{level_configuration}} ) {
209+
210+
# Build the values we are going to save
211+
my $savedConf = {
212+
branchcode => ((!$branchcode || $branchcode eq '*') ? undef : $branchcode),
213+
categorycode => ((!$categorycode || $categorycode eq '*') ? undef : $categorycode),
214+
level => $levelConfig->{level},
215+
outstanding_fee_limit => $levelConfig->{outstanding_fee_limit},
216+
patron_selections => $json->encode( $levelConfig->{patron_selections} ),
217+
ban_actions => $json->encode( $levelConfig->{ban_actions} ),
218+
unban_actions => $json->encode( $levelConfig->{unban_actions} )
219+
};
220+
221+
# In order to update existing level entries initialize the where params
222+
my $where = '';
223+
my $params = [];
224+
if ( $savedConf->{branchcode} ) {
225+
$where .= "branchcode = ? ";
226+
push @$params, $savedConf->{branchcode};
227+
} else {
228+
$where .= "branchcode IS NULL ";
229+
}
230+
if ( $savedConf->{categorycode} ) {
231+
$where .= "AND categorycode = ? ";
232+
push @$params, $savedConf->{categorycode};
233+
} else {
234+
$where .= "AND categorycode IS NULL ";
235+
}
236+
237+
# Check whether the level config exists and get the id of the record
238+
my $select = "SELECT id FROM lmsc_late_payment_claim_rules lpcr WHERE $where AND level = ?";
239+
my $sth = $dbh->prepare($select);
240+
$sth->execute(@$params,$savedConf->{level});
241+
242+
if ( my ($id) = $sth->fetchrow_array ) {
243+
$savedConf->{id} = $id;
244+
}
245+
246+
print STDERR Dumper($savedConf);
247+
248+
# if we can do an update ...
249+
if ( exists($savedConf->{id}) ) {
250+
$dbh->do(q{
251+
UPDATE lmsc_late_payment_claim_rules
252+
SET outstanding_fee_limit = ?,
253+
patron_selections = ?,
254+
ban_actions = ?,
255+
unban_actions = ?
256+
WHERE
257+
id = ?
258+
}, undef,
259+
$savedConf->{outstanding_fee_limit},
260+
$savedConf->{patron_selections},
261+
$savedConf->{ban_actions},
262+
$savedConf->{unban_actions},
263+
$savedConf->{id}
264+
);
265+
}
266+
# or do an insert
267+
else {
268+
$dbh->do(q{
269+
INSERT lmsc_late_payment_claim_rules (branchcode,categorycode,level,outstanding_fee_limit,patron_selections,ban_actions,unban_actions)
270+
VALUES (?,?,?,?,?,?,?)
271+
}, undef,
272+
$savedConf->{branchcode},
273+
$savedConf->{categorycode},
274+
$savedConf->{level},
275+
$savedConf->{outstanding_fee_limit},
276+
$savedConf->{patron_selections},
277+
$savedConf->{ban_actions},
278+
$savedConf->{unban_actions}
279+
);
280+
}
281+
# delete any existing higher level entry
282+
$dbh->do("DELETE FROM lmsc_late_payment_claim_rules WHERE $where AND level >= ?",undef,@$params,$checkLevel);
283+
}
284+
return $self->getConfiguration($branchcode,$categorycode);
285+
}
286+
287+
1;
288+
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package Koha::Plugin::Com::LMSCloud::LatePaymentClaiming::LatePaymentClaimingConfigurationController;
2+
3+
# This program is free software: you can redistribute it and/or modify
4+
# it under the terms of the GNU General Public License as published by
5+
# the Free Software Foundation, either version 3 of the License, or
6+
# (at your option) any later version.
7+
#
8+
# This program is distributed in the hope that it will be useful,
9+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
# GNU General Public License for more details.
12+
#
13+
# You should have received a copy of the GNU General Public License
14+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
15+
#
16+
# This program comes with ABSOLUTELY NO WARRANTY;
17+
18+
use Modern::Perl;
19+
use utf8;
20+
21+
use JSON;
22+
use Data::Dumper;
23+
use Try::Tiny qw( catch try );
24+
25+
use Mojo::Base 'Mojolicious::Controller';
26+
use Koha::Patrons;
27+
use Koha::DateUtils qw( dt_from_string );
28+
29+
use Koha::Plugin::Com::LMSCloud::LatePaymentClaiming::LatePaymentClaimingConfiguration;
30+
31+
32+
sub listConfigurations {
33+
my $c = shift->openapi->valid_input or return;
34+
35+
my $config = Koha::Plugin::Com::LMSCloud::LatePaymentClaiming::LatePaymentClaimingConfiguration->new();
36+
my $response = $config->getConfigurationList();
37+
38+
return $c->render(status => 200, openapi => $response );
39+
}
40+
41+
sub getConfiguration {
42+
my $c = shift->openapi->valid_input or return;
43+
44+
my $library_id = $c->validation->output->{'library_id'};
45+
my $category_id = $c->validation->output->{'category_id'};
46+
47+
my $config = Koha::Plugin::Com::LMSCloud::LatePaymentClaiming::LatePaymentClaimingConfiguration->new();
48+
my $response = $config->getConfiguration($library_id,$category_id);
49+
50+
return $c->render(status => 200, openapi => $response );
51+
}
52+
53+
sub removeConfiguration {
54+
my $c = shift->openapi->valid_input or return;
55+
56+
my $library_id = $c->validation->output->{'library_id'};
57+
my $category_id = $c->validation->output->{'category_id'};
58+
59+
my $config = Koha::Plugin::Com::LMSCloud::LatePaymentClaiming::LatePaymentClaimingConfiguration->new();
60+
my $result = $config->removeConfiguration($library_id,$category_id);
61+
62+
return $c->render(status => 200, openapi => { detail => "Configuration succesful removed." } );
63+
}
64+
65+
sub saveConfiguration {
66+
my $c = shift->openapi->valid_input or return;
67+
68+
my $library_id = $c->validation->output->{'library_id'};
69+
my $category_id = $c->validation->output->{'category_id'};
70+
71+
my $body = $c->validation->param('body');
72+
73+
my $config = Koha::Plugin::Com::LMSCloud::LatePaymentClaiming::LatePaymentClaimingConfiguration->new();
74+
75+
return try {
76+
if ( my $saved_config = $config->saveConfiguration($library_id,$category_id,$body) ) {
77+
return $c->render(status => 200, openapi => $saved_config );
78+
}
79+
}
80+
catch {
81+
$c->unhandled_exception($_);
82+
}
83+
}
84+
85+
86+
1;

0 commit comments

Comments
 (0)