From 65b27edfcca351ea79a16385f158b98362f6e1f4 Mon Sep 17 00:00:00 2001 From: Alex Arnaud Date: Tue, 15 May 2018 09:09:38 +0000 Subject: [PATCH] Bug 17656: Update irregularities on multi-renewals --- Koha/Subscription.pm | 41 +++++ .../prog/en/modules/serials/subscription-renew.tt | 4 +- serials/subscription-renew.pl | 4 + t/db_dependent/Koha/Subscription/Irregularities.t | 175 +++++++++++++++++++++ 4 files changed, 223 insertions(+), 1 deletion(-) create mode 100644 t/db_dependent/Koha/Subscription/Irregularities.t diff --git a/Koha/Subscription.pm b/Koha/Subscription.pm index b5f8bb2..c395517 100644 --- a/Koha/Subscription.pm +++ b/Koha/Subscription.pm @@ -28,7 +28,9 @@ use Koha::Biblioitems; use Koha::Subscriptions; use Koha::Subscription::Frequencies; use Koha::Subscription::Numberpatterns; +use Koha::DateUtils; use JSON; +use Date::Calc qw( Delta_Days ); use base qw(Koha::Object Koha::Object::Mixin::AdditionalFields); @@ -243,6 +245,45 @@ sub lengthtype { } } +=head3 update_irregularities + +$subscription->update_irregularities + +=cut + +sub guess_irregularities { + my ($self) = @_; + + my @irregularities; + my %permanent_irregularities = map { $_ => 1 } $self->permanent_irregularities; + + my $schema = Koha::Database->new->schema; + my $frequency = $schema->resultset('SubscriptionFrequency')->find($self->periodicity); + + my $enddate = $self->enddate; + my $nextexpected = C4::Serials::GetNextExpected($self->id); + my $date = $nextexpected->{planneddate}; + + my ($issuenumber) = C4::Serials::GetFictiveIssueNumber($self->unblessed, $date); + my $dow = issue_number( dt_from_string($date), $frequency->unit ); + + if ( defined( $permanent_irregularities{$dow} ) ) { + push @irregularities, $issuenumber; + } + + while ( Delta_Days( split(/-/, $date), split(/-/, $enddate) ) >= 0 ) { + $issuenumber++; + $date = C4::Serials::GetNextDate($self->unblessed, $date); + $dow = issue_number( dt_from_string($date), $frequency->unit ); + + if ( defined( $permanent_irregularities{$dow} ) ) { + push @irregularities, $issuenumber; + } + } + + return @irregularities; +} + =head3 type =cut diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/serials/subscription-renew.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/serials/subscription-renew.tt index f2f3b59..b202c1f 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/serials/subscription-renew.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/serials/subscription-renew.tt @@ -117,7 +117,9 @@ var irregularity = "[% subscription.irregularity %]"; var more_than_one_serial = "[% more_than_one_serial %]"; $(document).ready(function(){ - testPredictionPattern(); + if (subscriptionid) { + testPredictionPattern(); + } $(".close").on("click", function(e){ e.preventDefault(); window.opener.location.reload(false); diff --git a/serials/subscription-renew.pl b/serials/subscription-renew.pl index 2c4f959..d3edaf3 100755 --- a/serials/subscription-renew.pl +++ b/serials/subscription-renew.pl @@ -122,6 +122,10 @@ if ( $op eq "renew" ) { monthlength => $subscription->{monthlength}, } ); + + $subscription = Koha::Subscriptions->find( $subscriptionid ); + my @irregularities = $subscription->guess_irregularities; + $subscription->irregularity( join(';', @irregularities) )->store; } } else { my $subscriptionid = $subscriptionids[0]; diff --git a/t/db_dependent/Koha/Subscription/Irregularities.t b/t/db_dependent/Koha/Subscription/Irregularities.t new file mode 100644 index 0000000..a5b34c3 --- /dev/null +++ b/t/db_dependent/Koha/Subscription/Irregularities.t @@ -0,0 +1,175 @@ +#!/usr/bin/perl + +# This file is part of Koha +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WIT HOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + +use Test::More tests => 2; +use t::lib::TestBuilder; + +use C4::Serials; + +use Koha::Database; +use Koha::Subscriptions; + +BEGIN { + use_ok('Koha::Subscriptions'); +} + +my $schema = Koha::Database->new->schema; +my $builder = t::lib::TestBuilder->new; + +$schema->storage->txn_begin; + +my $biblio = $builder->build({ + source => 'Biblio', +}); + +my $frequency = $builder->build({ + source => 'SubscriptionFrequency', + value => { + unit => 'day', + unitsperissue => 1, + issuesperunit => 1 + } +}); + +my $number_pattern = $builder->build({ + source => 'SubscriptionNumberpattern', + value => { + numberingmethod => 'No.{X}', + add1 => 1, + every1 => 1, + whenmorethan1 => 99999, + setto1 => 1 + } +}); + +my $subscription = $builder->build({ + source => 'Subscription', + value => { + biblionumber => $biblio->{biblionumber}, + startdate => '2018-05-07', + weeklength => 0, + monthlength => 0, + numberlength => 7, + periodicity => $frequency->{id}, + countissuesperunit => 1, + status => 1, + lastvalue1 => 5, + lastvalue2 => 0, + innerloop1 => 0, + innerloop2 => 0, + innerloop3 => 0, + lastvalue3 => 0, + firstacquidate => '2018-05-07', + irregularity => '3;4', + permanent_irregularity => '3;4', + numberpattern => 7, + enddate => '2018-05-13', + closed => 0 + } +}); + +$builder->build({ + source => 'Serial', + value => { + biblionumber => $biblio->{biblionumber}, + subscriptionid => $subscription->{subscriptionid}, + serialseq => 'No.1', + serialseq_x => 1, + status => 2, + planneddate => '2018-05-07', + publisheddate => '2018-05-07', + } +}); + +$builder->build({ + source => 'Serial', + value => { + biblionumber => $biblio->{biblionumber}, + subscriptionid => $subscription->{subscriptionid}, + serialseq => 'No.2', + serialseq_x => 2, + status => 2, + planneddate => '2018-05-08', + publisheddate => '2018-05-08', + } +}); + +$builder->build({ + source => 'Serial', + value => { + biblionumber => $biblio->{biblionumber}, + subscriptionid => $subscription->{subscriptionid}, + serialseq => 'No.3', + serialseq_x => 3, + status => 2, + planneddate => '2018-05-11', + publisheddate => '2018-05-11', + } +}); + +$builder->build({ + source => 'Serial', + value => { + biblionumber => $biblio->{biblionumber}, + subscriptionid => $subscription->{subscriptionid}, + serialseq => 'No.4', + serialseq_x => 4, + status => 2, + planneddate => '2018-05-12', + publisheddate => '2018-05-12', + } +}); + +$builder->build({ + source => 'Serial', + value => { + biblionumber => $biblio->{biblionumber}, + subscriptionid => $subscription->{subscriptionid}, + serialseq => 'No.5', + serialseq_x => 5, + status => 2, + planneddate => '2018-05-13', + publisheddate => '2018-05-13', + } +}); + +$builder->build({ + source => 'Serial', + value => { + biblionumber => $biblio->{biblionumber}, + subscriptionid => $subscription->{subscriptionid}, + serialseq => 'No.6', + serialseq_x => 6, + status => 1, + planneddate => '2018-05-14', + publisheddate => '2018-05-14', + } +}); + +ReNewSubscription( + $subscription->{subscriptionid}, undef, + $subscription->{enddate}, $subscription->{numberlength}, + $subscription->{weeklength}, $subscription->{monthlength}, +); + +my $s = Koha::Subscriptions->find($subscription->{subscriptionid}); +my $irregularities = join(';', $s->guess_irregularities); +is($irregularities, '10;11', 'Irregularities have been updated'); + +$schema->storage->txn_rollback; \ No newline at end of file -- 2.7.4