Skip to content

Commit 3a8748d

Browse files
author
roger
committed
Last additions to complete a first version for testing.
1 parent fd56e2e commit 3a8748d

File tree

9 files changed

+184
-42
lines changed

9 files changed

+184
-42
lines changed

Koha/Plugin/Com/LMSCloud/LatePaymentClaiming.pm

Lines changed: 73 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use Data::Dumper;
2626

2727
use C4::Context;
2828
use C4::Koha qw( GetAuthorisedValues );
29-
use C4::Auth qw( get_template_and_user );
29+
use C4::Auth qw( get_template_and_user haspermission );
3030

3131
use Koha::Acquisition::Currencies qw( get_active );
3232
use Koha::DateUtils qw( dt_from_string output_pref );
@@ -40,7 +40,7 @@ use Koha::Plugin::Com::LMSCloud::LatePaymentClaiming::LatePaymentClaimingConfigu
4040
use Koha::Plugin::Com::LMSCloud::LatePaymentClaiming::LatePaymentClaiming;
4141
use Koha::Plugin::Com::LMSCloud::LatePaymentClaiming::CheckExecution;
4242

43-
our $VERSION = "0.8.0";
43+
our $VERSION = "0.9.0";
4444
our $MINIMUM_VERSION = "22.11";
4545

4646
our $metadata = {
@@ -75,6 +75,7 @@ sub configure {
7575
$self->store_data(
7676
{
7777
batch_active => scalar $cgi->param('batch_active'),
78+
last_run => scalar $cgi->param('last_run'),
7879
execution_month_days => scalar $cgi->param('execution_month_days') || '*',
7980
execution_monthes => scalar $cgi->param('execution_monthes') || '*',
8081
execution_weekdays => scalar $cgi->param('execution_weekdays') || '*',
@@ -171,6 +172,7 @@ sub configure {
171172

172173
$template->param(
173174
batch_active => $self->retrieve_data('batch_active') || 0,
175+
last_run => $self->retrieve_data('last_run'),
174176
execution_month_days => $self->retrieve_data('execution_month_days') || '*',
175177
execution_monthes => $self->retrieve_data('execution_monthes') || '*',
176178
execution_weekdays => $self->retrieve_data('execution_weekdays') || '*',
@@ -202,21 +204,86 @@ sub cronjob_nightly {
202204

203205
my $cron = Koha::Plugin::Com::LMSCloud::LatePaymentClaiming::CheckExecution->new();
204206

207+
my $startdate = DateTime->now()->subtract(days => 1);
208+
my $lastrun = $self->retrieve_data('last_run');
209+
if ( $lastrun ) {
210+
$startdate = dt_from_string($lastrun);
211+
}
212+
205213
my $result = $cron->getNextDay(
206214
$execution_month_days,
207215
$execution_monthes,
208216
$execution_weekdays,
209-
DateTime->now()->subtract(days => 1),
217+
$startdate,
210218
$execution_on_closing_days,
211219
$execution_on_closing_days_library);
212220
if ( $result->{ok} ) {
213221
if ( $result->{next}->ymd('-') eq DateTime->now()->ymd('-') ) {
214222
my $doClaim = Koha::Plugin::Com::LMSCloud::LatePaymentClaiming::LatePaymentClaiming->new();
215223
$doClaim->claimPatronsOfAllConfigurations();
224+
$self->store_data({ last_run => dt_from_string()->ymd('-') });
216225
}
217226
}
218227
}
219228

229+
sub intranet_js {
230+
my ( $self ) = @_;
231+
232+
my $user_id = C4::Context->userenv ? C4::Context->userenv->{'id'} : undef;
233+
my $canViewClaims = 0;
234+
if ( $user_id && haspermission($user_id, { plugins => 'plugins_tool' }) ) {
235+
$canViewClaims = 1;
236+
}
237+
238+
my $intranetJSadd = q^
239+
<script>
240+
$(document).ready( function() {
241+
if ( $('#patron_messages.circmessage,#circmessages.circmessage').length > 0 ) {
242+
var msgPanel = $('#patron_messages.circmessage,#circmessages.circmessage').first();
243+
if ( $('input[name="borrowernumber"]').length > 0 ) {
244+
var patron = $('input[name="borrowernumber"]').first().val();
245+
if ( patron && patron.length > 0 ) {
246+
$.ajax({
247+
'dataType': 'json',
248+
'type': 'GET',
249+
'url': '/api/v1/contrib/latepaymentclaiming/late_payment_claims',
250+
'data': { patron_id: patron, status: 'current', draw: 1 },
251+
'success': function(result) {
252+
// console.log(result);
253+
if ( result && result.data && result.recordsTotal == 1 ) {
254+
var claimDate = result.data[0].creationdate;
255+
var level = result.data[0].level;
256+
257+
if ( ! msgPanel.hasClass("attention") ) {
258+
msgPanel.addClass("attention");
259+
}
260+
261+
var listElem;
262+
var msgPanelHead = msgPanel.find("h3:contains('Attention'),h3:contains('Achtung')");
263+
if ( msgPanelHead.length > 0 ) {
264+
listElem = msgPanelHead.next('ul');
265+
}
266+
else {
267+
msgPanel.prepend('<h3>Achtung</h3><ul></ul>');
268+
msgPanelHead = msgPanel.find("h3:contains('Attention'),h3:contains('Achtung')");
269+
listElem = msgPanelHead.next('ul');
270+
}
271+
if ( listElem && listElem.length > 0 ) {
272+
listElem.prepend('<li class="charges blocker"><span class="circ-hlt">Gebührenmahnung:</span> ' + level + '. Gebührenmahnung (angemahnt seit ' + $date(claimDate) + ').^ .
273+
($canViewClaims ? q^ <a href="/cgi-bin/koha/plugins/run.pl?class=Koha%3A%3APlugin%3A%3ACom%3A%3ALMSCloud%3A%3ALatePaymentClaiming&method=tool&toolaction=currentClaims&status_filter=all&patron_id=' + patron + '">Zeige Mahnfall</a>^ : '') . q^</li>');
274+
}
275+
}
276+
}
277+
});
278+
}
279+
}
280+
}
281+
});
282+
</script>
283+
^;
284+
return $intranetJSadd;
285+
}
286+
220287
sub tool {
221288
my ( $self, $args ) = @_;
222289

@@ -346,6 +413,7 @@ sub claimInteractive {
346413
);
347414
}
348415
elsif ( exists($result->{actionIdList}) ) {
416+
$self->store_data({ last_run => dt_from_string()->ymd('-') });
349417
$self->claimHistory({ actionIdList => $result->{actionIdList}, actionIdCount => $result->{actionIdCount} });
350418
exit;
351419
}
@@ -368,6 +436,7 @@ sub currentClaims {
368436
my $cgi = $self->{cgi};
369437

370438
my $status_filter = $cgi->param('status_filter') || '';
439+
my $patron_id = $cgi->param('patron_id') || '';
371440

372441
$status_filter = 'open' if (! $status_filter );
373442
$status_filter = '' if ( $status_filter eq 'all' );
@@ -381,6 +450,7 @@ sub currentClaims {
381450

382451
$template->param(
383452
status_filter => $status_filter,
453+
patron_id => $patron_id,
384454
action => 'list',
385455
branches => \@branches,
386456
categorylist => \@categorylist
@@ -429,7 +499,6 @@ sub install {
429499
`amountoutstanding` DECIMAL(28,6) default NULL COMMENT 'Outstanding fee at the time of the history entry',
430500
`patron_selections` mediumtext DEFAULT NULL COMMENT 'Selection parameters applied to reach that level JSON format',
431501
`ban_actions` mediumtext NOT NULL COMMENT 'Ban actions performed at the level in JSON format',
432-
`unban_actions` mediumtext NOT NULL COMMENT 'Unban actions performed in JSON format',
433502
`timestamp` timestamp NOT NULL
434503
DEFAULT current_timestamp() COMMENT 'The timestamp the action was performed',
435504
`manager_id` int(11) DEFAULT NULL COMMENT 'Staff member who performed the action (NULL if it was an automated action)',
@@ -451,7 +520,6 @@ sub install {
451520
`outstanding_fee_limit` DECIMAL(28,6) default NULL COMMENT 'Fee limit that needs to be reached for this level',
452521
`patron_selections` mediumtext NOT NULL COMMENT 'Selection parameters defined in JSON format',
453522
`ban_actions` mediumtext NOT NULL COMMENT 'Ban actions to perform in JSON format',
454-
`unban_actions` mediumtext NOT NULL COMMENT 'Unban actions to perform in JSON format',
455523
PRIMARY KEY (`id`),
456524
UNIQUE KEY `lmsc_lpcr_uniq` (`branchcode`,`categorycode`,`level`),
457525
KEY `lmsc_lpcr_ibfk_1` (`branchcode`),

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

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ sub getNextDay {
5757
my ($self,$dayCron,$monthCron,$weekdayCron,$start,$execOnClosingDays,$execOnClosingDayLibrary,$checkRes) = @_;
5858

5959
my $startDate = DateTime->new(year => $start->year, month => $start->month, day => $start->day);
60+
my $today = DateTime->now();
61+
my $todayCompare = DateTime->new(year => $today->year, month => $today->month, day => $today->day);
6062

6163
my $checkResult;
6264
if ( $checkRes ) {
@@ -77,6 +79,7 @@ sub getNextDay {
7779
my $cronweekday = DateTime::Event::Cron->new("0 0 * " . $month . " " . $weekday);
7880

7981
my $nextDay;
82+
8083
if ( $day ne 'L' ) {
8184
$nextDay = $cronday->next($startDate);
8285
} else {
@@ -87,7 +90,7 @@ sub getNextDay {
8790
my $maxCycles = $self->{maxIterations};
8891

8992
my ($checkres,$useDate) = $self->compareDatesAndCheckClosingDay($nextDay,$nextWeekday,$execOnClosingDays,$execOnClosingDayLibrary);
90-
while ( $checkres != 0 && $maxCycles-- > 0 ) {
93+
while ( ($checkres != 0 || $useDate < $todayCompare) && $maxCycles-- > 0 ) {
9194
# print "$cmpres => ", $nextDay->ymd(), " ", $nextDay->hms(), " ", $nextWeekday->ymd(), " ", , $nextDay->hms(), "\n";
9295
if ( $checkres == 1 ) {
9396
$nextWeekday = $cronweekday->next($nextWeekday);
@@ -115,32 +118,37 @@ sub getNextDay {
115118

116119
my $cron;
117120
my $nextDay;
118-
if ( $day ne 'L' ) {
119-
$cron = DateTime::Event::Cron->new("0 0 " . $day . " " . $month . " " . $weekday);
120-
$nextDay = $cron->next($startDate);
121-
} else {
122-
$nextDay = $self->getNextLastDayOfMonth($startDate);
123-
}
124-
125121

126-
if ( $execOnClosingDays && $execOnClosingDayLibrary ) {
127-
my $calendar = Koha::Calendar->new( branchcode => $execOnClosingDayLibrary );
128-
if ( $execOnClosingDays eq 'no' ) {
129-
my $maxCycles = $self->{maxIterations};
130-
while ( $calendar->is_holiday($nextDay) && $maxCycles-- > 0) {
131-
if ( $day ne 'L' ) {
132-
$nextDay = $cron->next($nextDay);
133-
} else {
134-
$nextDay = $self->getNextLastDayOfMonth($nextDay);
122+
while ( !$nextDay || $nextDay < $todayCompare ) {
123+
if ( $day ne 'L' ) {
124+
$cron = DateTime::Event::Cron->new("0 0 " . $day . " " . $month . " " . $weekday);
125+
$nextDay = $cron->next($startDate);
126+
} else {
127+
$nextDay = $self->getNextLastDayOfMonth($startDate);
128+
}
129+
130+
$startDate = $nextDay;
131+
132+
if ( $execOnClosingDays && $execOnClosingDayLibrary ) {
133+
my $calendar = Koha::Calendar->new( branchcode => $execOnClosingDayLibrary );
134+
if ( $execOnClosingDays eq 'no' ) {
135+
my $maxCycles = $self->{maxIterations};
136+
while ( $calendar->is_holiday($nextDay) && $maxCycles-- > 0) {
137+
if ( $day ne 'L' ) {
138+
$nextDay = $cron->next($nextDay);
139+
} else {
140+
$nextDay = $self->getNextLastDayOfMonth($nextDay);
141+
}
135142
}
136143
}
137-
}
138-
elsif ( $execOnClosingDays eq 'delay' ) {
139-
my $maxCycles = $self->{maxIterations};
140-
while ( $calendar->is_holiday($nextDay) && $maxCycles-- >= 1 ) {
141-
$nextDay->add(days => 1);
144+
elsif ( $execOnClosingDays eq 'delay' ) {
145+
my $maxCycles = $self->{maxIterations};
146+
while ( $calendar->is_holiday($nextDay) && $maxCycles-- >= 1 ) {
147+
$nextDay->add(days => 1);
148+
}
142149
}
143150
}
151+
# print $nextDay->ymd('-'), " <=> ", $todayCompare->ymd('-'), "\n";
144152
}
145153

146154
$checkResult->{next} = $nextDay;
@@ -229,8 +237,9 @@ sub getNextLastDayOfMonth {
229237
1;
230238

231239
#my $cron = Koha::Plugin::Com::LMSCloud::LatePaymentClaiming::CheckExecution->new();
240+
#my $startDate = DateTime->new(year => 2025, month => 12, day => 31);
241+
#my $res = $cron->getNextDays('1-7','*','7',$startDate,'delay','Zentrale',10);
232242

233-
#my $res = $cron->getNextDays('*','*','7',DateTime->now(),'delay','Zentrale',10);
234243
#foreach my $d(@$res) {
235244
# print $d->dmy("."),"\n";
236245
#}

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,10 @@ sub getLatePaymentClaims {
224224
push @where, "b.cardnumber LIKE ?";
225225
push @params, '%' . $parameters->{cardnumber} . '%';
226226
}
227+
if ( $parameters->{patron_id} ) {
228+
push @where, "b.borrowernumber = ?";
229+
push @params, $parameters->{patron_id};
230+
}
227231
if ( $parameters->{patron} ) {
228232
my $sPatron = '%' . $parameters->{patron} . '%';
229233
push @where, "(b.surname LIKE ? OR b.firstname LIKE ? OR b.middle_name LIKE ? OR b.othernames LIKE ? OR b.address LIKE ? OR b.address2 LIKE ? OR b.city LIKE ? OR b.zipcode LIKE ? OR b.country LIKE ?)";
@@ -250,8 +254,13 @@ sub getLatePaymentClaims {
250254
push @params, $parameters->{level};
251255
}
252256
if ( $parameters->{status} ) {
253-
push @where, "lpc.state = ?";
254-
push @params, $parameters->{status};
257+
if ( $parameters->{status} eq 'current' ) {
258+
push @where, "lpc.state IN (?,?)";
259+
push @params, 'open', 'paused';
260+
} else {
261+
push @where, "lpc.state = ?";
262+
push @params, $parameters->{status};
263+
}
255264
}
256265
if ( $parameters->{comment} ) {
257266
push @where, "lpc.comment LIKE ?";

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ sub getLatePaymentClaims {
8888

8989
my $parameters = {
9090
claim_id => $param->{claim_id},
91+
patron_id => $param->{patron_id},
9192
cardnumber => $param->{cardnumber},
9293
patron => $param->{patron},
9394
library => $param->{library},
@@ -106,9 +107,12 @@ sub getLatePaymentClaims {
106107
push @$resultClaims, databaseClaim2Api($claim);
107108
}
108109

110+
my $returnData = { data => $resultClaims, recordsFiltered => $result->{full_count}, recordsTotal => $result->{full_count} };
111+
$returnData->{draw} = $param->{draw} if ( $param->{draw} );
112+
109113
return $c->render(
110114
status => 200,
111-
openapi => { data => $resultClaims, recordsFiltered => $result->{full_count}, recordsTotal => $result->{full_count}, draw => $param->{draw} }
115+
openapi => $returnData
112116
);
113117
}
114118

Koha/Plugin/Com/LMSCloud/LatePaymentClaiming/claimHistory.tt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919
</head>
2020
<body id="latePaymentClaimsTool">
2121
[% INCLUDE 'html_helpers.inc' %]
22-
[% INCLUDE 'cat-search.inc' %]
2322

23+
[% WRAPPER 'header.inc' %]
24+
[% INCLUDE 'patron-search-header.inc' %]
25+
[% END %]
2426

2527
[% WRAPPER 'sub-header.inc' %]
2628
[% WRAPPER breadcrumbs %]
@@ -399,7 +401,7 @@
399401
} );
400402
},
401403
"fnServerData": function(sSource, params, fnCallback) {
402-
console.log(params);
404+
// console.log(params);
403405
let readParams = {};
404406
for (let i=0;i<params.length;i++) {
405407
readParams[params[i].name] = params[i].value;

Koha/Plugin/Com/LMSCloud/LatePaymentClaiming/claimInteractive.tt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818
</head>
1919
<body id="latePaymentClaimsTool">
2020
[% INCLUDE 'html_helpers.inc' %]
21-
[% INCLUDE 'cat-search.inc' %]
2221

22+
[% WRAPPER 'header.inc' %]
23+
[% INCLUDE 'patron-search-header.inc' %]
24+
[% END %]
2325

2426
[% WRAPPER 'sub-header.inc' %]
2527
[% WRAPPER breadcrumbs %]

Koha/Plugin/Com/LMSCloud/LatePaymentClaiming/configure.tt

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@
2222
<body id="late_payment_claim_plugin_configuration">
2323
[% INCLUDE 'html_helpers.inc' %]
2424
[% PROCESS 'restriction-types.inc' %]
25-
[% INCLUDE 'cat-search.inc' %]
25+
26+
[% WRAPPER 'header.inc' %]
27+
[% INCLUDE 'patron-search-header.inc' %]
28+
[% END %]
2629

2730
[% WRAPPER 'sub-header.inc' %]
2831
[% WRAPPER breadcrumbs %]
@@ -155,6 +158,10 @@
155158
[% END %]
156159
</select>
157160
</li>
161+
<li>
162+
<label class="radio" for="last_run">Letzte Ausführung am:</label>
163+
<input type="hidden" name="last_run" id="last_run" value="[% last_run | html %]" />[% IF last_run %]<span id="resetStartDate">[% last_run | $KohaDates %]&nbsp;&nbsp;&nbsp;&nbsp;<a href="javascript:resetStartDate()">Zurücksetzen</a></span>[% END %]
164+
</li>
158165
<li>
159166
<label for="generate_next_execution_days">Nächste Ausführungstage:</label>
160167
<input class="btn btn-xs" onclick="javascript:getNextExecutionDays()" name="generate_next_execution_days" id="generate_next_execution_days" value="Anzeigen" />
@@ -2227,6 +2234,7 @@
22272234
}
22282235

22292236
function getNextExecutionDays() {
2237+
var lastrun = $('#general_settings_form input[name="last_run"]').val();
22302238
var day = $('#general_settings_form input[name="execution_month_days"]').val();
22312239
var month = $('#general_settings_form input[name="execution_monthes"]').val();
22322240
var weekdays = $('#general_settings_form input[name="execution_weekdays"]').val();
@@ -2244,7 +2252,7 @@
22442252
options = {
22452253
url: '/api/v1/contrib/latepaymentclaiming/get_next_execution_days',
22462254
accepts: { text: "application/json" },
2247-
data: { day: day, month: month, weekday: weekdays, execOnClosingDays: executionOnClosingDays, execOnClosingDayLibrary: executionOnClosingDaysLibrary }
2255+
data: { startDate: lastrun, day: day, month: month, weekday: weekdays, execOnClosingDays: executionOnClosingDays, execOnClosingDayLibrary: executionOnClosingDaysLibrary }
22482256
};
22492257
$.ajax(options)
22502258
.then(function(result) {
@@ -2286,6 +2294,11 @@
22862294
}
22872295
});
22882296
}
2297+
2298+
function resetStartDate() {
2299+
$('#general_settings_form input[name="last_run"]').val('');
2300+
$('#resetStartDate').text('');
2301+
}
22892302

22902303
</script>
22912304

0 commit comments

Comments
 (0)