From a43e7f8179f264b97c16b9fe7feb25c07ce09e83 Mon Sep 17 00:00:00 2001 From: Alex Arnaud Date: Fri, 18 May 2018 07:58:52 +0000 Subject: [PATCH] Bug 17656: move GuessEnddate in Koha::Subscription::guess_enddate --- C4/Serials.pm | 35 ------ Koha/Subscription.pm | 47 +++++++- serials/subscription-add.pl | 26 ++++- serials/subscription-renew.pl | 13 ++- t/db_dependent/Koha/Subscription/GuessEnddate.t | 148 ++++++++++++++++++++++++ 5 files changed, 220 insertions(+), 49 deletions(-) create mode 100644 t/db_dependent/Koha/Subscription/GuessEnddate.t diff --git a/C4/Serials.pm b/C4/Serials.pm index b828f4b..8abeaf0 100644 --- a/C4/Serials.pm +++ b/C4/Serials.pm @@ -87,7 +87,6 @@ BEGIN { HasItems &GetSubscriptionsFromBorrower &subscriptionCurrentlyOnOrder - &GuessEnddate ); } @@ -2716,40 +2715,6 @@ sub findSerialsByStatus { return @$serials; } -=head2 GuessEnddate - -my $enddate = GuessEnddate($startdate_iso $frequencyid, $numberlength, $weeklength, $monthlength); - -=cut - -sub GuessEnddate { - my ($startdate_iso, $frequencyid, $numberlength, $weeklength, $monthlength) = @_; - my ($year, $month, $day); - my $enddate; - if($numberlength != 0) { - my $frequency = GetSubscriptionFrequency($frequencyid); - if($frequency->{'unit'} eq 'day') { - ($year, $month, $day) = Add_Delta_Days(split(/-/, $startdate_iso), $numberlength * $frequency->{'unitsperissue'} / $frequency->{'issuesperunit'}); - } elsif($frequency->{'unit'} eq 'week') { - ($year, $month, $day) = Add_Delta_Days(split(/-/, $startdate_iso), $numberlength * 7 * $frequency->{'unitsperissue'} / $frequency->{'issuesperunit'}); - } elsif($frequency->{'unit'} eq 'month') { - ($year, $month, $day) = Add_Delta_YM(split(/-/, $startdate_iso), 0, $numberlength * $frequency->{'unitsperissue'} / $frequency->{'issuesperunit'}); - } elsif($frequency->{'unit'} eq 'year') { - ($year, $month, $day) = Add_Delta_YM(split(/-/, $startdate_iso), $numberlength * $frequency->{'unitsperissue'} / $frequency->{'issuesperunit'}, 0); - } - } elsif($weeklength != 0) { - ($year, $month, $day) = Add_Delta_Days(split(/-/, $startdate_iso), $weeklength * 7); - } elsif($monthlength != 0) { - ($year, $month, $day) = Add_Delta_YM(split(/-/, $startdate_iso), 0, $monthlength); - } - if(defined $year) { - $enddate = sprintf("%04d-%02d-%02d", $year, $month, $day); - } else { - undef $enddate; - } - return $enddate; -} - 1; __END__ diff --git a/Koha/Subscription.pm b/Koha/Subscription.pm index a99c380..2dcfcd4 100644 --- a/Koha/Subscription.pm +++ b/Koha/Subscription.pm @@ -21,12 +21,14 @@ use Modern::Perl; use Carp; +use C4::Serials::Frequency; + use Koha::Database; use Koha::Biblios; use Koha::Acquisition::Booksellers; use Koha::DateUtils; -use Date::Calc qw( Delta_Days ); +use Date::Calc qw( Delta_Days Add_Delta_Days Add_Delta_YM ); use base qw(Koha::Object); @@ -155,9 +157,9 @@ sub lengthtype { } } -=head3 update_irregularities +=head3 guess_irregularities -$subscription->update_irregularities +my @irregularities = $subscription->guess_irregularities =cut @@ -194,6 +196,45 @@ sub guess_irregularities { return @irregularities; } +=head2 guess_enddate + +my $enddate = $subscription->guess_enddate; + +=cut + +sub guess_enddate { + my ($self) = @_; + + my ($year, $month, $day, $enddate); + my $startdate = $self->startdate; + my $numberlength = $self->numberlength; + my $weeklength = $self->weeklength; + my $monthlength = $self->monthlength; + + if($numberlength != 0) { + my $frequency = GetSubscriptionFrequency($self->periodicity); + if($frequency->{'unit'} eq 'day') { + ($year, $month, $day) = Add_Delta_Days(split(/-/, $startdate), $numberlength * $frequency->{'unitsperissue'} / $frequency->{'issuesperunit'}); + } elsif($frequency->{'unit'} eq 'week') { + ($year, $month, $day) = Add_Delta_Days(split(/-/, $startdate), $numberlength * 7 * $frequency->{'unitsperissue'} / $frequency->{'issuesperunit'}); + } elsif($frequency->{'unit'} eq 'month') { + ($year, $month, $day) = Add_Delta_YM(split(/-/, $startdate), 0, $numberlength * $frequency->{'unitsperissue'} / $frequency->{'issuesperunit'}); + } elsif($frequency->{'unit'} eq 'year') { + ($year, $month, $day) = Add_Delta_YM(split(/-/, $startdate), $numberlength * $frequency->{'unitsperissue'} / $frequency->{'issuesperunit'}, 0); + } + } elsif($weeklength != 0) { + ($year, $month, $day) = Add_Delta_Days(split(/-/, $startdate), $weeklength * 7); + } elsif($monthlength != 0) { + ($year, $month, $day) = Add_Delta_YM(split(/-/, $startdate), 0, $monthlength); + } + if(defined $year) { + $enddate = sprintf("%04d-%02d-%02d", $year, $month, $day); + } else { + undef $enddate; + } + return $enddate; +} + =head3 type =cut diff --git a/serials/subscription-add.pl b/serials/subscription-add.pl index 84d874d..af246ab 100755 --- a/serials/subscription-add.pl +++ b/serials/subscription-add.pl @@ -301,11 +301,18 @@ sub redirect_add_subscription { my $firstacquidate = output_pref( { str => scalar $query->param('firstacquidate'), dateonly => 1, dateformat => 'iso' } ); if(!defined $enddate || $enddate eq '') { + my $subscription = Koha::Subscriptions->new({ + startdate => $startdate, + periodicity => $periodicity, + weeklength => $weeklength, + monthlength =>$monthlength, + numberlength => $numberlength + }); + if($subtype eq "issues") { - $enddate = GuessEnddate($firstacquidate, $periodicity, $numberlength, $weeklength, $monthlength) - } else { - $enddate = GuessEnddate($startdate, $periodicity, $numberlength, $weeklength, $monthlength) + $subscription->startdate($firstacquidate); } + $enddate = $subscription->guess_enddate; } my $subscriptionid = NewSubscription( @@ -378,11 +385,18 @@ sub redirect_mod_subscription { # Guess end date if(!defined $enddate || $enddate eq '') { + my $subscription = Koha::Subscriptions->new({ + startdate => $startdate, + periodicity => $periodicity, + weeklength => $weeklength, + monthlength =>$monthlength, + numberlength => $numberlength + }); + if($subtype eq "issues") { - $enddate = GuessEnddate($nextacquidate, $periodicity, $numberlength, $weeklength, $monthlength); - } else { - $enddate = GuessEnddate($startdate, $periodicity, $numberlength, $weeklength, $monthlength); + $subscription->startdate($nextacquidate); } + $enddate = $subscription->guess_enddate; } my $nextexpected = GetNextExpected($subscriptionid); diff --git a/serials/subscription-renew.pl b/serials/subscription-renew.pl index 0c71cbb..500132d 100755 --- a/serials/subscription-renew.pl +++ b/serials/subscription-renew.pl @@ -125,11 +125,14 @@ if ( $op eq "renew" ) { my $subscription_o = Koha::Subscriptions->find($subscription->{subscriptionid}); my $lengthtype = $subscription_o->lengthtype; my $nextexpected = GetNextExpected($subscriptionid); - my $enddate = GuessEnddate($subscription->{enddate} || dt_from_string, - $subscription->{periodicity}, - $subscription->{numberlength}, - $subscription->{weeklength}, - $subscription->{monthlength}); + $subscription_o->update({ + startdate => $subscription->{enddate}, + periodicity => $subscription->{periodicity}, + weeklength => $subscription->{weeklength}, + monthlength => $subscription->{monthlength}, + numberlength => $subscription->{numberlength} + }); + my $enddate = $subscription_o->guess_enddate; my $sub_length; foreach my $length_unit (qw(numberlength weeklength monthlength)) { diff --git a/t/db_dependent/Koha/Subscription/GuessEnddate.t b/t/db_dependent/Koha/Subscription/GuessEnddate.t new file mode 100644 index 0000000..c16fb32 --- /dev/null +++ b/t/db_dependent/Koha/Subscription/GuessEnddate.t @@ -0,0 +1,148 @@ +#!/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 => 15; +use t::lib::TestBuilder; + +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 $frequency = $builder->build({ + source => 'SubscriptionFrequency', + value => { + unit => 'day', + unitsperissue => 1, + issuesperunit => 1 + } +}); + +my $subscription = $builder->build({ + source => 'Subscription', + value => { + startdate => '2018-01-01', + weeklength => 0, + monthlength => 0, + numberlength => 7, + periodicity => $frequency->{id}, + } +}); + +$subscription = Koha::Subscriptions->find($subscription->{subscriptionid}); +$frequency = $schema->resultset('SubscriptionFrequency')->find($frequency->{id}); +is($subscription->guess_enddate, '2018-01-08'); + +$frequency->update({ + unit => 'day', + unitsperissue => 2, + issuesperunit => 1 +}); +is($subscription->guess_enddate, '2018-01-15'); + +$frequency->update({ + unit => 'day', + unitsperissue => 1, + issuesperunit => 2 +}); +is($subscription->guess_enddate, '2018-01-04'); + +$frequency->update({ + unit => 'week', + unitsperissue => 1, + issuesperunit => 1 +}); +is($subscription->guess_enddate, '2018-02-19'); + +$frequency->update({ + unit => 'week', + unitsperissue => 2, + issuesperunit => 1 +}); +is($subscription->guess_enddate, '2018-04-09'); + +$frequency->update({ + unit => 'week', + unitsperissue => 1, + issuesperunit => 2 +}); +is($subscription->guess_enddate, '2018-01-25'); + +$frequency->update({ + unit => 'month', + unitsperissue => 1, + issuesperunit => 1 +}); +is($subscription->guess_enddate, '2018-08-01'); + +$frequency->update({ + unit => 'month', + unitsperissue => 2, + issuesperunit => 1 +}); +is($subscription->guess_enddate, '2019-03-01'); + +$frequency->update({ + unit => 'month', + unitsperissue => 1, + issuesperunit => 2 +}); +is($subscription->guess_enddate, '2018-04-01'); + +$frequency->update({ + unit => 'year', + unitsperissue => 1, + issuesperunit => 1 +}); +is($subscription->guess_enddate, '2025-01-01'); + +$frequency->update({ + unit => 'year', + unitsperissue => 2, + issuesperunit => 1 +}); +is($subscription->guess_enddate, '2032-01-01'); + +$frequency->update({ + unit => 'year', + unitsperissue => 1, + issuesperunit => 2 +}); +is($subscription->guess_enddate, '2021-01-01'); + + +$subscription->numberlength(0); +$subscription->weeklength(2); +$subscription->store; +is($subscription->guess_enddate, '2018-01-15'); + +$subscription->weeklength(0); +$subscription->monthlength(1); +$subscription->store; +is($subscription->guess_enddate, '2018-02-01'); + +$schema->storage->txn_rollback; -- 2.7.4