From db34a2ce74bc0244341cf4e5e099377f63b8d751 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 | 42 +++++ .../prog/en/modules/serials/subscription-renew.tt | 4 +- serials/subscription-renew.pl | 4 + t/db_dependent/Koha/Subscription.t | 1 - t/db_dependent/Koha/Subscription/Irregularities.t | 175 +++++++++++++++++++++ 5 files changed, 224 insertions(+), 2 deletions(-) create mode 100644 t/db_dependent/Koha/Subscription/Irregularities.t diff --git a/Koha/Subscription.pm b/Koha/Subscription.pm index f00e88c..a99c380 100644 --- a/Koha/Subscription.pm +++ b/Koha/Subscription.pm @@ -24,6 +24,9 @@ use Carp; use Koha::Database; use Koha::Biblios; use Koha::Acquisition::Booksellers; +use Koha::DateUtils; + +use Date::Calc qw( Delta_Days ); use base qw(Koha::Object); @@ -152,6 +155,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 723bde0..8b64ff6 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 @@ -84,7 +84,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 602461c..0c71cbb 100755 --- a/serials/subscription-renew.pl +++ b/serials/subscription-renew.pl @@ -101,6 +101,10 @@ if ( $op eq "renew" ) { $subscription->{enddate}, $subscription->{numberlength}, $subscription->{weeklength}, $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.t b/t/db_dependent/Koha/Subscription.t index 2d9e096..76f51b5 100644 --- a/t/db_dependent/Koha/Subscription.t +++ b/t/db_dependent/Koha/Subscription.t @@ -20,7 +20,6 @@ use Modern::Perl; use Test::More tests => 4; -use Test::More tests => 2; use t::lib::TestBuilder; use Koha::Subscriptions; 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