From 32447ff0a7ac38f732a96e0c7e99cd86beee78d8 Mon Sep 17 00:00:00 2001 From: Alex Arnaud Date: Wed, 18 Oct 2017 14:52:34 +0000 Subject: [PATCH] Bug 17656: Keep irregularities during subscription renewals When renewing a subscription, irregularities are lost. This patch allows to keep them for daily, weekly and monthly serials (1 unit per issue only). Test plan: - Apply this patch, - Launch installer/data/mysql/updatedatabase.pl - Launch misc/devel/update_dbix_class_files.pl - Create a daily subscription. i.e: - From: 2017-10-09, To: 2017-10-15, - Frequency: 1/day, - 7 issues, - test prediction pattern - check Wednesday and Thursday as irregularity, - save - Edit subscription, - Check that Wednesday and Thursday are still checked - Renew subscription from 2017-10-16 - Edit subscription, - Make sure that Wednesday and Thursday are still checked with corresponding serials - Do the same for a monthly and weekly subscription Rebased - 2017-12-15 - Alex Arnaud Signed-off-by: David Nind Rebased - 2021-03-04 - Alex Arnaud Signed-off-by: Victor Grousset/tuxayo --- C4/Serials.pm | 143 +++++++------- Koha/Subscription.pm | 132 ++++++++++++- ...ug_17656-add-permanent-irregularities.perl | 9 + installer/data/mysql/kohastructure.sql | 1 + .../prog/css/src/staff-global.scss | 10 +- .../serials/subscription_daily_choice.inc | 33 ++++ .../serials/subscription_monthly_choice.inc | 38 ++++ .../serials/subscription_weekly_choice.inc | 28 +++ .../modules/serials/showpredictionpattern.tt | 34 ++-- .../en/modules/serials/subscription-add.tt | 1 + .../en/modules/serials/subscription-renew.tt | 41 ++++ .../prog/js/showpredictionpattern.js | 21 ++- .../intranet-tmpl/prog/js/subscription-add.js | 77 +------- .../intranet-tmpl/prog/js/subscription.js | 78 ++++++++ serials/showpredictionpattern.pl | 15 +- serials/subscription-add.pl | 63 +++---- serials/subscription-renew.pl | 53 +++++- t/Serials/IssueNumber.t | 47 +++++ t/db_dependent/Koha/Subscription.t | 39 +++- .../Koha/Subscription/GuessEnddate.t | 148 +++++++++++++++ .../Koha/Subscription/Irregularities.t | 175 ++++++++++++++++++ .../Serials/SubscriptionIrregularity.t | 35 ++++ 22 files changed, 1007 insertions(+), 214 deletions(-) create mode 100644 installer/data/mysql/atomicupdate/Bug_17656-add-permanent-irregularities.perl create mode 100644 koha-tmpl/intranet-tmpl/prog/en/includes/serials/subscription_daily_choice.inc create mode 100644 koha-tmpl/intranet-tmpl/prog/en/includes/serials/subscription_monthly_choice.inc create mode 100644 koha-tmpl/intranet-tmpl/prog/en/includes/serials/subscription_weekly_choice.inc create mode 100644 koha-tmpl/intranet-tmpl/prog/js/subscription.js create mode 100644 t/Serials/IssueNumber.t create mode 100644 t/db_dependent/Koha/Subscription/GuessEnddate.t create mode 100644 t/db_dependent/Koha/Subscription/Irregularities.t create mode 100644 t/db_dependent/Serials/SubscriptionIrregularity.t diff --git a/C4/Serials.pm b/C4/Serials.pm index d840bf24b7..cc5d9cd4f9 100644 --- a/C4/Serials.pm +++ b/C4/Serials.pm @@ -99,6 +99,8 @@ BEGIN { HasItems findSerialsByStatus + &subscriptionCurrentlyOnOrder + &issue_number ); } @@ -1271,37 +1273,6 @@ sub ModNextExpected { } -=head2 GetSubscriptionIrregularities - -=over 4 - -=item @irreg = &GetSubscriptionIrregularities($subscriptionid); -get the list of irregularities for a subscription - -=back - -=cut - -sub GetSubscriptionIrregularities { - my $subscriptionid = shift; - - return unless $subscriptionid; - - my $dbh = C4::Context->dbh; - my $query = qq{ - SELECT irregularity - FROM subscription - WHERE subscriptionid = ? - }; - my $sth = $dbh->prepare($query); - $sth->execute($subscriptionid); - - my ($result) = $sth->fetchrow_array; - my @irreg = split /;/, $result; - - return @irreg; -} - =head2 ModSubscription this function modifies a subscription. Put all new values on input args. @@ -1312,7 +1283,7 @@ returns the number of rows affected sub ModSubscription { my ( $auser, $branchcode, $aqbooksellerid, $cost, $aqbudgetid, $startdate, - $periodicity, $firstacquidate, $irregularity, $numberpattern, $locale, + $periodicity, $firstacquidate, $irregularity, $permanent_irregularity, $numberpattern, $locale, $numberlength, $weeklength, $monthlength, $lastvalue1, $innerloop1, $lastvalue2, $innerloop2, $lastvalue3, $innerloop3, $status, $biblionumber, $callnumber, $notes, $letter, $manualhistory, @@ -1346,6 +1317,7 @@ sub ModSubscription { letter => $letter, firstacquidate => $firstacquidate, irregularity => $irregularity, + permanent_irregularity => $permanent_irregularity, numberpattern => $numberpattern, locale => $locale, callnumber => $callnumber, @@ -1396,51 +1368,52 @@ sub NewSubscription { $startdate, $periodicity, $numberlength, $weeklength, $monthlength, $lastvalue1, $innerloop1, $lastvalue2, $innerloop2, $lastvalue3, $innerloop3, $status, $notes, $letter, $firstacquidate, $irregularity, - $numberpattern, $locale, $callnumber, $manualhistory, $internalnotes, - $serialsadditems, $staffdisplaycount, $opacdisplaycount, $graceperiod, + $permanent_irregularity, $numberpattern, $locale, $callnumber, $manualhistory, + $internalnotes, $serialsadditems, $staffdisplaycount, $opacdisplaycount, $graceperiod, $location, $enddate, $skip_serialseq, $itemtype, $previousitemtype, $mana_id ) = @_; my $dbh = C4::Context->dbh; my $subscription = Koha::Subscription->new( { - librarian => $auser, - branchcode => $branchcode, - aqbooksellerid => $aqbooksellerid, - cost => $cost, - aqbudgetid => $aqbudgetid, - biblionumber => $biblionumber, - startdate => $startdate, - periodicity => $periodicity, - numberlength => $numberlength, - weeklength => $weeklength, - monthlength => $monthlength, - lastvalue1 => $lastvalue1, - innerloop1 => $innerloop1, - lastvalue2 => $lastvalue2, - innerloop2 => $innerloop2, - lastvalue3 => $lastvalue3, - innerloop3 => $innerloop3, - status => $status, - notes => $notes, - letter => $letter, - firstacquidate => $firstacquidate, - irregularity => $irregularity, - numberpattern => $numberpattern, - locale => $locale, - callnumber => $callnumber, - manualhistory => $manualhistory, - internalnotes => $internalnotes, - serialsadditems => $serialsadditems, - staffdisplaycount => $staffdisplaycount, - opacdisplaycount => $opacdisplaycount, - graceperiod => $graceperiod, - location => $location, - enddate => $enddate, - skip_serialseq => $skip_serialseq, - itemtype => $itemtype, - previousitemtype => $previousitemtype, - mana_id => $mana_id, + librarian => $auser, + branchcode => $branchcode, + aqbooksellerid => $aqbooksellerid, + cost => $cost, + aqbudgetid => $aqbudgetid, + biblionumber => $biblionumber, + startdate => $startdate, + periodicity => $periodicity, + numberlength => $numberlength, + weeklength => $weeklength, + monthlength => $monthlength, + lastvalue1 => $lastvalue1, + innerloop1 => $innerloop1, + lastvalue2 => $lastvalue2, + innerloop2 => $innerloop2, + lastvalue3 => $lastvalue3, + innerloop3 => $innerloop3, + status => $status, + notes => $notes, + letter => $letter, + firstacquidate => $firstacquidate, + irregularity => $irregularity, + permanent_irregularity => $permanent_irregularity, + numberpattern => $numberpattern, + locale => $locale, + callnumber => $callnumber, + manualhistory => $manualhistory, + internalnotes => $internalnotes, + serialsadditems => $serialsadditems, + staffdisplaycount => $staffdisplaycount, + opacdisplaycount => $opacdisplaycount, + graceperiod => $graceperiod, + location => $location, + enddate => $enddate, + skip_serialseq => $skip_serialseq, + itemtype => $itemtype, + previousitemtype => $previousitemtype, + mana_id => $mana_id, } )->store; $subscription->discard_changes; @@ -2694,6 +2667,34 @@ sub findSerialsByStatus { return @$serials; } +=head2 issue_number + + my $week_number = issue_number($dt, 'week'); + +=cut + +sub issue_number { + my ($date, $issue_type) = @_; + + unless ( ref($date) eq 'DateTime' ) { + Koha::Exceptions::WrongParameter->throw("Date passed to issue_number is not a valid DateTime object" ); + } + + if ($issue_type eq 'day') { + return $date->dow; + } + + if ($issue_type eq 'week') { + return $date->week_number; + } + + if ($issue_type eq 'month') { + return $date->month; + } + + return 0; +} + 1; __END__ diff --git a/Koha/Subscription.pm b/Koha/Subscription.pm index 45d1c09fa0..f042510c71 100644 --- a/Koha/Subscription.pm +++ b/Koha/Subscription.pm @@ -20,6 +20,9 @@ package Koha::Subscription; use Modern::Perl; +use C4::Serials::Frequency; +use C4::Serials qw(issue_number); + use Koha::Database; use Koha::Biblios; use Koha::Acquisition::Booksellers; @@ -27,6 +30,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 Add_Delta_Days Add_Delta_YM ); use base qw(Koha::Object Koha::Object::Mixin::AdditionalFields); @@ -195,7 +201,131 @@ sub get_sharable_info { } -=head3 _type +=head2 irregularities + +Returns an array containing irregularities + +=cut + +sub irregularities { + my ($self) = @_; + + return split(';', $self->irregularity); +} + +=head2 permanent_irregularities + +Returns an array containing permanent irregularities + +=cut + +sub permanent_irregularities { + my ($self) = @_; + + return split(';', $self->permanent_irregularity); +} + +=head3 lengthtype + +Return the length type (issues, weeks, months) + +=cut + +sub lengthtype { + my ($self) = @_; + + if ($self->numberlength) { + return 'issues'; + } + + if ($self->weeklength) { + return 'weeks'; + } + + if ($self->monthlength) { + return 'months'; + } +} + +=head3 guess_irregularities + +my @irregularities = $subscription->guess_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; +} + +=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/installer/data/mysql/atomicupdate/Bug_17656-add-permanent-irregularities.perl b/installer/data/mysql/atomicupdate/Bug_17656-add-permanent-irregularities.perl new file mode 100644 index 0000000000..fb6314dc82 --- /dev/null +++ b/installer/data/mysql/atomicupdate/Bug_17656-add-permanent-irregularities.perl @@ -0,0 +1,9 @@ +$DBversion = 'XXX'; +if( CheckVersion( $DBversion ) ) { + if( !column_exists( 'subscription', 'permanent_irregularity' ) ) { + $dbh->do( "ALTER TABLE `subscription` ADD permanent_irregularity MEDIUMTEXT AFTER irregularity" ); + } + + SetVersion( $DBversion ); + print "Upgrade to $DBversion done (Bug 17656 - Irregularities in serial prediction pattern are planned only for current subscription)\n"; +} diff --git a/installer/data/mysql/kohastructure.sql b/installer/data/mysql/kohastructure.sql index a309cea736..59bc319095 100644 --- a/installer/data/mysql/kohastructure.sql +++ b/installer/data/mysql/kohastructure.sql @@ -4804,6 +4804,7 @@ CREATE TABLE `subscription` ( `firstacquidate` date DEFAULT NULL COMMENT 'first issue received date', `manualhistory` tinyint(1) NOT NULL DEFAULT 0 COMMENT 'yes or no to managing the history manually', `irregularity` mediumtext COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT 'any irregularities in the subscription', + `permanent_irregularity` mediumtext COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT 'permanent irregularities for renewal', `skip_serialseq` tinyint(1) NOT NULL DEFAULT 0, `letter` varchar(20) COLLATE utf8mb4_unicode_ci DEFAULT NULL, `numberpattern` int(11) DEFAULT NULL COMMENT 'the numbering pattern used links to subscription_numberpatterns.id', diff --git a/koha-tmpl/intranet-tmpl/prog/css/src/staff-global.scss b/koha-tmpl/intranet-tmpl/prog/css/src/staff-global.scss index fa3421dee1..ab346604f4 100644 --- a/koha-tmpl/intranet-tmpl/prog/css/src/staff-global.scss +++ b/koha-tmpl/intranet-tmpl/prog/css/src/staff-global.scss @@ -4560,6 +4560,14 @@ div .suggestion_note { display:inline-block; white-space:nowrap } +.week-columns{ + -moz-column-count: 2; /* Firefox */ + -webkit-column-count: 2; /* Safari and Chrome */ + column-count: 3; +} + +fieldset.rows label.inline { + float: none; } @media (min-width: 200px) { @@ -4782,4 +4790,4 @@ div .suggestion_note { body.modalprinter .modal-dialog.focused .modal-content .modal-header .modal-title { margin-bottom: 20px; } -} +} \ No newline at end of file diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/serials/subscription_daily_choice.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/serials/subscription_daily_choice.inc new file mode 100644 index 0000000000..6a7fc42da0 --- /dev/null +++ b/koha-tmpl/intranet-tmpl/prog/en/includes/serials/subscription_daily_choice.inc @@ -0,0 +1,33 @@ +

+ If there is a day (or more) in the week where issues are never + published, you can check corresponding boxes below. +

+[% daily_choice = [ {name => 'monday', value => 1}, {name => 'tuesday', value => 2}, {name => 'wednesday', value => 3}, {name => 'thursday', value => 4}, {name => 'friday', value => 5}, {name => 'saturday', value => 6}, {name => 'sunday', value => 7} ] %] + +[% BLOCK days %] + [% SWITCH d %] + [% CASE 'monday' %]Monday + [% CASE 'tuesday' %]Tuesday + [% CASE 'wednesday' %]Wednesday + [% CASE 'thursday' %]Thursday + [% CASE 'friday' %]Friday + [% CASE 'saturday' %]Saturday + [% CASE 'sunday' %]Sunday + [% END %] +[% END %] + +[% FOREACH day IN daily_choice %] + [% IF permanent_irreg.grep( day.value ).0 == day.value %] + + [% ELSE %] + + [% END %] + +[% END %] diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/serials/subscription_monthly_choice.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/serials/subscription_monthly_choice.inc new file mode 100644 index 0000000000..6ba98e33a3 --- /dev/null +++ b/koha-tmpl/intranet-tmpl/prog/en/includes/serials/subscription_monthly_choice.inc @@ -0,0 +1,38 @@ +

+ If there is a month (or more) in the year where issues are never + published, you can check corresponding boxes below. +

+[% monthly_choice = [ {name => 'january', value => 1}, {name => 'february', value => 2}, {name => 'march', value => 3}, {name => 'april', value => 4}, {name => 'may', value => 5}, {name => 'june', value => 6}, {name => 'july', value => 7}, {name => 'august', value => 8}, {name => 'september', value => 9}, {name => 'october', value => 10}, {name => 'november', value => 11}, {name => 'december', value =>12} ] %] + +[% BLOCK months %] + [% SWITCH m %] + [% CASE 'january' %]January + [% CASE 'february' %]February + [% CASE 'march' %]March + [% CASE 'april' %]April + [% CASE 'may' %]May + [% CASE 'june' %]June + [% CASE 'july' %]July + [% CASE 'august' %]August + [% CASE 'september' %]September + [% CASE 'october' %]October + [% CASE 'november' %]November + [% CASE 'december' %]December + [% END %] +[% END %] + +[% FOREACH month IN monthly_choice %] + [% IF permanent_irreg.grep( month.value ).0 == month.value %] + + [% ELSE %] + + [% END %] + +[% END %] diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/serials/subscription_weekly_choice.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/serials/subscription_weekly_choice.inc new file mode 100644 index 0000000000..594243e336 --- /dev/null +++ b/koha-tmpl/intranet-tmpl/prog/en/includes/serials/subscription_weekly_choice.inc @@ -0,0 +1,28 @@ +
+ +
+

+ Choice the week(s) in the year where issues are never + published. +

diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/serials/showpredictionpattern.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/serials/showpredictionpattern.tt index c684348deb..f9373b3446 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/serials/showpredictionpattern.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/serials/showpredictionpattern.tt @@ -5,25 +5,21 @@ [% END %] [% IF (ask_for_irregularities) %]

Please check issues that are NOT published (irregularities)

- [% IF (daily_options) %] -

- If there is a day (or more) in the week where issues are never - published, you can check corresponding boxes below. -

- - - - - - - - - - - - - - + [% IF (show_irregularity_options) %] + + [% IF (show_irregularity_options == 'day') %] + [% INCLUDE 'serials/subscription_daily_choice.inc' %] + [% ELSIF (show_irregularity_options == 'month') %] + [% INCLUDE 'serials/subscription_monthly_choice.inc' %] + [% ELSIF (show_irregularity_options == 'week') %] + [% INCLUDE 'serials/subscription_weekly_choice.inc' %] + [% END %] [% END %] [% END %] [% IF (predictions_loop) %] diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/serials/subscription-add.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/serials/subscription-add.tt index 6c22b7cdd1..84f1a5c992 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/serials/subscription-add.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/serials/subscription-add.tt @@ -587,6 +587,7 @@ fieldset.rows table { clear: none; margin: 0; } var BOOKSELLER_IDS = [% To.json( bookseller_ids ) || '[]' | $raw %]; + [% Asset.js("js/subscription.js") | $raw %] [% Asset.js("js/subscription-add.js") | $raw %] [% Asset.js("js/showpredictionpattern.js") | $raw %] [% END %] 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 3cf27e27a5..dec0835152 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 @@ -1,5 +1,6 @@ [% USE Koha %] [% USE Branches %] +[% USE Asset %] [% SET footerjs = 1 %] [% INCLUDE 'doc-head-open.inc' %] [% IF subscription %] @@ -30,6 +31,36 @@
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Subscription renewal for [% subscription.bibliotitle | html %]
  1. @@ -78,6 +109,8 @@
  2. [% END %] + +
@@ -87,7 +120,13 @@ [% MACRO jsinclude BLOCK %] [% INCLUDE 'calendar.inc' %] + [% Asset.js("js/subscription.js") | $raw %] + [% Asset.js("js/showpredictionpattern.js") | $raw %] [% END %] [% INCLUDE 'intranet-bottom.inc' popup_window=1 %] diff --git a/koha-tmpl/intranet-tmpl/prog/js/showpredictionpattern.js b/koha-tmpl/intranet-tmpl/prog/js/showpredictionpattern.js index f5c38a09d3..6ab01c0b0a 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/showpredictionpattern.js +++ b/koha-tmpl/intranet-tmpl/prog/js/showpredictionpattern.js @@ -8,9 +8,28 @@ function Check_boxes(dow) { $(this).prop('checked', false); }); } + return false; +} +function Check_permanent_irregularities() { + nothing_checked = 1; + $("input[name='irregularity']").each(function() { + if ($(this).is(':checked')) { + nothing_checked = 0; + } + }); + + // No day/month/week are checked, could be after a renewal. + // So check them according to permanent irregularity + if (nothing_checked) { + $(".skip").each(function() { + if ($(this).is(':checked')) { + Check_boxes( $(this).data("dow")); + } + }); + } } $(document).ready(function(){ - $("#displayexample").on("change",".skipday",function(){ + $("#displayexample").on("change",".skip",function(){ Check_boxes( $(this).data("dow")); }); }); diff --git a/koha-tmpl/intranet-tmpl/prog/js/subscription-add.js b/koha-tmpl/intranet-tmpl/prog/js/subscription-add.js index 297756b59e..bfc84b29b8 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/subscription-add.js +++ b/koha-tmpl/intranet-tmpl/prog/js/subscription-add.js @@ -266,76 +266,6 @@ function restoreAdvancedPattern() { advancedpatternlocked = 1; } -function testPredictionPattern() { - var frequencyid = $("#frequency").val(); - var acquidate; - var error = 0; - var error_msg = ""; - if(frequencyid == undefined || frequencyid == ""){ - error_msg += "- " + MSG_FREQUENCY_UNDEFINED + "\n"; - error ++; - } - acquidate = $("#acqui_date").val(); - if(acquidate == undefined || acquidate == ""){ - error_msg += "-" + MSG_PUB_DATE_UNDEFINED + "\n"; - error ++; - } - if( more_than_one_serial !== "" ){ - var nextacquidate = $("#nextacquidate").val(); - if(nextacquidate == undefined || nextacquidate == ""){ - error_msg += "-" + MSG_NEXT_ISSUE_UNDEFINED + "\n"; - error ++; - } - } - - if(error){ - alert( MSG_PATTERN_TEST_FAILED.format(error_msg) ); - return false; - } - - var custompattern = 0; - if(advancedpatternlocked == 0) { - custompattern = 1; - } - - var ajaxData = { - 'custompattern': custompattern, - 'firstacquidate': acquidate - }; - - if( subscriptionid !== "" ){ - ajaxData.subscriptionid = subscriptionid; - } - if( more_than_one_serial !== "" ){ - ajaxData.nextacquidate = nextacquidate; - } - - - var ajaxParams = [ - 'to', 'subtype', 'sublength', 'frequency', 'numberingmethod', - 'lastvalue1', 'lastvalue2', 'lastvalue3', 'add1', 'add2', 'add3', - 'every1', 'every2', 'every3', 'innerloop1', 'innerloop2', 'innerloop3', - 'setto1', 'setto2', 'setto3', 'numbering1', 'numbering2', 'numbering3', - 'whenmorethan1', 'whenmorethan2', 'whenmorethan3', 'locale', - 'sfdescription', 'unitsperissue', 'issuesperunit', 'unit' - ]; - for(i in ajaxParams) { - var param = ajaxParams[i]; - var value = $("#"+param).val(); - if(value.length > 0) - ajaxData[param] = value; - } - - $.ajax({ - url:"/cgi-bin/koha/serials/showpredictionpattern.pl", - data: ajaxData, - success: function(data) { - showPredictionPatternTest( data ); - patternneedtobetested = 0; - } - }); -} - function saveAdvancedPattern() { if ($("#patternname").val().length == 0) { alert( MSG_PATTERN_NAME ); @@ -530,18 +460,13 @@ function mana_comment_close(){ $("#new_mana_comment").hide(); } -function showPredictionPatternTest( data ){ - $("#displayexample").html(data).show(); - $("#page_2 > div").attr("class","col-xs-6"); -} - function hidePredcitionPatternTest(){ $("#displayexample").hide(); $("#page_2 > div").attr("class","col-md-10 col-md-offset-1 col-lg-8 col-lg-offset-2"); } $(document).ready(function() { - if ( mana_enabled == 1 ) { + if ( typeof mana_enabled !== 'undefined'&& mana_enabled == 1 ) { mana_search(); } $("#displayexample").hide(); diff --git a/koha-tmpl/intranet-tmpl/prog/js/subscription.js b/koha-tmpl/intranet-tmpl/prog/js/subscription.js new file mode 100644 index 0000000000..3638bcf90f --- /dev/null +++ b/koha-tmpl/intranet-tmpl/prog/js/subscription.js @@ -0,0 +1,78 @@ + +var advancedpatternlocked; + +function testPredictionPattern() { + var frequencyid = $("#frequency").val(); + var acquidate; + var error = 0; + var error_msg = ""; + if(frequencyid == undefined || frequencyid == ""){ + error_msg += "- " + MSG_FREQUENCY_UNDEFINED + "\n"; + error ++; + } + acquidate = $("#acqui_date").val(); + if(acquidate == undefined || acquidate == ""){ + error_msg += "-" + MSG_PUB_DATE_UNDEFINED + "\n"; + error ++; + } + if( more_than_one_serial !== "" ){ + var nextacquidate = $("#nextacquidate").val(); + if(nextacquidate == undefined || nextacquidate == ""){ + error_msg += "-" + MSG_NEXT_ISSUE_UNDEFINED + "\n"; + error ++; + } + } + + if(error){ + alert( MSG_PATTERN_TEST_FAILED.format(error_msg) ); + return false; + } + + var custompattern = 0; + if(advancedpatternlocked == 0) { + custompattern = 1; + } + + var ajaxData = { + 'custompattern': custompattern, + 'firstacquidate': acquidate + }; + + if( subscriptionid !== "" ){ + ajaxData.subscriptionid = subscriptionid; + } + if( more_than_one_serial !== "" ){ + ajaxData.nextacquidate = nextacquidate; + } + + + var ajaxParams = [ + 'to', 'subtype', 'sublength', 'frequency', 'numberingmethod', + 'lastvalue1', 'lastvalue2', 'lastvalue3', 'add1', 'add2', 'add3', + 'every1', 'every2', 'every3', 'innerloop1', 'innerloop2', 'innerloop3', + 'setto1', 'setto2', 'setto3', 'numbering1', 'numbering2', 'numbering3', + 'whenmorethan1', 'whenmorethan2', 'whenmorethan3', 'locale', + 'sfdescription', 'unitsperissue', 'issuesperunit', 'unit' + ]; + for(i in ajaxParams) { + var param = ajaxParams[i]; + var value = $("#"+param).val(); + if(value && value.length > 0) + ajaxData[param] = value; + } + + $.ajax({ + url:"/cgi-bin/koha/serials/showpredictionpattern.pl", + data: ajaxData, + success: function(data) { +console.log(data); + showPredictionPatternTest( data ); + patternneedtobetested = 0; + } + }); +} + +function showPredictionPatternTest( data ){ + $("#displayexample").html(data).show(); + $("#page_2 > div").attr("class","col-xs-6"); +} diff --git a/serials/showpredictionpattern.pl b/serials/showpredictionpattern.pl index 6420e50ce1..24aef3f7f0 100755 --- a/serials/showpredictionpattern.pl +++ b/serials/showpredictionpattern.pl @@ -124,15 +124,19 @@ if(defined $subscriptionid) { my @predictions_loop; my ($calculated) = GetSeq(\%subscription, \%pattern); +my $dow = issue_number( dt_from_string($date), $frequency->{unit} ); push @predictions_loop, { number => $calculated, publicationdate => $date, issuenumber => $issuenumber, - dow => Day_of_Week(split /-/, $date), + dow => $dow, }; my @irreg = (); +my @permanent_irreg = (); if(defined $subscriptionid) { - @irreg = C4::Serials::GetSubscriptionIrregularities($subscriptionid); + my $subscription_o = Koha::Subscriptions->find($subscriptionid); + @irreg = $subscription_o->irregularities; + @permanent_irreg = $subscription_o->permanent_irregularities; while(@irreg && $issuenumber > $irreg[0]) { shift @irreg; } @@ -151,7 +155,7 @@ while( $i < 1000 ) { } if(defined $date){ $line{'publicationdate'} = $date; - $line{'dow'} = Day_of_Week(split /-/, $date); + $line{dow} = issue_number( dt_from_string($date), $frequency->{unit} ); } # Check if we don't have exceed end date @@ -187,8 +191,9 @@ $template->param( if ( $frequency->{unit} and not $custompattern ) { $template->param( ask_for_irregularities => 1 ); - if ( $frequency->{unit} eq 'day' and $frequency->{unitsperissue} == 1 ) { - $template->param( daily_options => 1 ); + if ( $frequency->{unit} =~ /^(day|month|week)$/ and $frequency->{unitsperissue} == 1 ) { + $template->param( show_irregularity_options => $frequency->{unit} ); + $template->param( permanent_irreg => \@permanent_irreg ); } } diff --git a/serials/subscription-add.pl b/serials/subscription-add.pl index 39426a6253..37db21ac54 100755 --- a/serials/subscription-add.pl +++ b/serials/subscription-add.pl @@ -251,34 +251,6 @@ sub get_letter_loop { ]; } -sub _guess_enddate { - 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; -} - sub redirect_add_subscription { my $periodicity = $query->param('frequency'); if ($periodicity eq 'mana') { @@ -300,6 +272,7 @@ sub redirect_add_subscription { my $cost = $query->param('cost'); my $aqbudgetid = $query->param('aqbudgetid'); my @irregularity = $query->multi_param('irregularity'); + my @permanent_irregularity = $query->multi_param('permanent_irregularity'); my $locale = $query->param('locale'); my $graceperiod = $query->param('graceperiod') || 0; @@ -340,19 +313,26 @@ 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 = _guess_enddate($firstacquidate, $periodicity, $numberlength, $weeklength, $monthlength) - } else { - $enddate = _guess_enddate($startdate, $periodicity, $numberlength, $weeklength, $monthlength) + $subscription->startdate($firstacquidate); } + $enddate = $subscription->guess_enddate; } my $subscriptionid = NewSubscription( $auser, $branchcode, $aqbooksellerid, $cost, $aqbudgetid, $biblionumber, $startdate, $periodicity, $numberlength, $weeklength, $monthlength, $lastvalue1, $innerloop1, $lastvalue2, $innerloop2, $lastvalue3, $innerloop3, $status, $notes, $letter, $firstacquidate, - join(";",@irregularity), $numberpattern, $locale, $callnumber, - $manualhistory, $internalnotes, $serialsadditems, + join(";",@irregularity), join(';', @permanent_irregularity), $numberpattern, $locale, + $callnumber, $manualhistory, $internalnotes, $serialsadditems, $staffdisplaycount, $opacdisplaycount, $graceperiod, $location, $enddate, $skip_serialseq, $itemtype, $previousitemtype, $mana_id ); @@ -386,6 +366,7 @@ sub redirect_add_subscription { sub redirect_mod_subscription { my $subscriptionid = $query->param('subscriptionid'); my @irregularity = $query->multi_param('irregularity'); + my @permanent_irregularity = $query->multi_param('permanent_irregularity'); my $auser = $query->param('user'); my $librarian => scalar $query->param('librarian'), my $branchcode = $query->param('branchcode'); @@ -453,11 +434,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 = _guess_enddate($nextacquidate, $periodicity, $numberlength, $weeklength, $monthlength); - } else { - $enddate = _guess_enddate($startdate, $periodicity, $numberlength, $weeklength, $monthlength); + $subscription->startdate($nextacquidate); } + $enddate = $subscription->guess_enddate; } my $nextexpected = GetNextExpected($subscriptionid); @@ -471,7 +459,8 @@ sub redirect_mod_subscription { ModSubscription( $auser, $branchcode, $aqbooksellerid, $cost, $aqbudgetid, $startdate, $periodicity, $firstacquidate, join(";",@irregularity), - $numberpattern, $locale, $numberlength, $weeklength, $monthlength, $lastvalue1, + join(';', @permanent_irregularity), $numberpattern, $locale, + $numberlength, $weeklength, $monthlength, $lastvalue1, $innerloop1, $lastvalue2, $innerloop2, $lastvalue3, $innerloop3, $status, $biblionumber, $callnumber, $notes, $letter, $manualhistory, $internalnotes, $serialsadditems, $staffdisplaycount, diff --git a/serials/subscription-renew.pl b/serials/subscription-renew.pl index 5da15363c1..ea495a8909 100755 --- a/serials/subscription-renew.pl +++ b/serials/subscription-renew.pl @@ -53,7 +53,10 @@ use C4::Context; use C4::Auth qw( get_template_and_user ); use C4::Output qw( output_and_exit output_html_with_http_headers ); use C4::Serials qw( GetSubscription GetSubscriptionLength NewSubscription ReNewSubscription ); +use C4::Serials::Frequency; +use C4::Serials::Numberpattern; use Koha::DateUtils qw( dt_from_string output_pref ); +use Date::Calc qw(Today Day_of_Year Week_of_Year Add_Delta_Days Add_Delta_YM); my $query = CGI->new; my $dbh = C4::Context->dbh; @@ -81,6 +84,9 @@ if ( $op eq "renew" ) { # Make sure the subscription exists my $subscription = GetSubscription( $subscriptionid ); output_and_exit( $query, $cookie, $template, 'unknown_subscription') unless $subscription; + + my @permanent_irregularities = $query->param('permanent_irregularity'); + my @irregularities = $query->param('irregularity'); my $startdate = output_pref( { str => scalar $query->param('startdate'), dateonly => 1, dateformat => 'iso' } ); ($numberlength, $weeklength, $monthlength) = GetSubscriptionLength( $subtype, $sublength ); ReNewSubscription( @@ -95,6 +101,11 @@ if ( $op eq "renew" ) { branchcode => $branchcode } ); + + $subscription = Koha::Subscriptions->find($subscriptionid); + $subscription->permanent_irregularity(join(';', @permanent_irregularities)); + $subscription->irregularity(join(';', @irregularities)); + $subscription->store; } elsif ( $op eq 'multi_renew' ) { for my $subscriptionid ( @subscriptionids ) { my $subscription = GetSubscription( $subscriptionid ); @@ -109,6 +120,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]; @@ -122,14 +137,48 @@ if ( $op eq "renew" ) { my $newstartdate = output_pref( { str => $subscription->{enddate}, dateonly => 1 } ) or output_pref( { dt => dt_from_string, dateonly => 1 } ); + my ($serials_number) = GetSerials($subscriptionid); + if ($serials_number > 1) { + $template->param(more_than_one_serial => 1); + } + + my $subscription_o = Koha::Subscriptions->find($subscription->{subscriptionid}); + my $lengthtype = $subscription_o->lengthtype; + my $nextexpected = GetNextExpected($subscriptionid); + $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)) { + if ($subscription->{$length_unit}) { + $sub_length=$subscription->{$length_unit}; + last; + } + } + + my $numberpattern = GetSubscriptionNumberpattern($subscription->{numberpattern}); + $template->param( - startdate => $newstartdate, - subscription => $subscription, + startdate => $newstartdate, + subscription => $subscription, + numberpattern => $numberpattern, + startdate => $newstartdate, + nextacquidate => $nextexpected->{planneddate}, + enddate => $enddate, + lengthtype => $lengthtype, + sublength => $sub_length, ); } $template->param( op => $op, + popup => ($mode eq 'popup'), ); output_html_with_http_headers $query, $cookie, $template->output; diff --git a/t/Serials/IssueNumber.t b/t/Serials/IssueNumber.t new file mode 100644 index 0000000000..da07f7a755 --- /dev/null +++ b/t/Serials/IssueNumber.t @@ -0,0 +1,47 @@ +#!/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 +# WITHOUT 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 => 10; +use Test::Warn; + +BEGIN { + use_ok('C4::Serials'); +} + +my $dt = DateTime->new(year => 2017, month => 1, day => 1); + +eval { issue_number('2017', 'day'); }; +is(ref($@), 'Koha::Exceptions::WrongParameter'); +is($@, 'Date passed to issue_number is not a valid DateTime object'); + +is(issue_number($dt, 'day'), 7, '2017-01-01 is the seventh day of week'); + +is(issue_number($dt, 'week'), 52, '2017-01-01 is the week #52 of the year'); + +is(issue_number($dt, 'month'), 1, '2017-01-01 is the first month of the year'); + +$dt = DateTime->new(year => 2022, month => 9, day => 17); + +is(issue_number($dt, 'day'), 6, '2022-09-17 is the sixth day of week'); + +is(issue_number($dt, 'week'), 37, '2022-09-17 is the week #37 of the year'); + +is(issue_number($dt, 'month'), 9, '2022-09-17 is the ninth month of the year'); + +is(issue_number($dt, 'foo'), 0, 'issue_number return 0 for others issue type'); diff --git a/t/db_dependent/Koha/Subscription.t b/t/db_dependent/Koha/Subscription.t index 6765640d38..a55db6a331 100755 --- a/t/db_dependent/Koha/Subscription.t +++ b/t/db_dependent/Koha/Subscription.t @@ -19,7 +19,7 @@ use Modern::Perl; -use Test::More tests => 10; +use Test::More tests => 11; use Koha::Database; use Koha::Subscription; @@ -106,6 +106,43 @@ subtest 'Koha::Subscription->frequency' => sub { is( $subscription->frequency->id, $frequency->id, 'Koha::Subscription->frequency should return the correct frequency' ); }; +subtest 'Koha::Subscription::lengthtype' => sub { + plan tests => 3; + + my $subs = $builder->build({ + source => 'Subscription', + value => { + numberlength => 7, + weeklength => 0, + monthlength => 0, + } + }); + my $subscription = Koha::Subscriptions->find($subs->{subscriptionid}); + is($subscription->lengthtype, 'issues', 'Subscription with numberlength has issues type'); + + my $subs2 = $builder->build({ + source => 'Subscription', + value => { + numberlength => 0, + weeklength => 52, + monthlength => 0, + } + }); + my $subscription2 = Koha::Subscriptions->find($subs2->{subscriptionid}); + is($subscription2->lengthtype, 'weeks', 'Subscription with weeklength has weeks type'); + + my $subs3 = $builder->build({ + source => 'Subscription', + value => { + numberlength => 0, + weeklength => 0, + monthlength => 12, + } + }); + my $subscription3 = Koha::Subscriptions->find($subs3->{subscriptionid}); + is($subscription3->lengthtype, 'months', 'Subscription with monthlength has months type'); +}; + my $nb_of_subs = Koha::Subscriptions->search->count; my $biblio_1 = $builder->build_sample_biblio; my $bi_1 = $biblio_1->biblioitem; diff --git a/t/db_dependent/Koha/Subscription/GuessEnddate.t b/t/db_dependent/Koha/Subscription/GuessEnddate.t new file mode 100644 index 0000000000..c16fb322ee --- /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; diff --git a/t/db_dependent/Koha/Subscription/Irregularities.t b/t/db_dependent/Koha/Subscription/Irregularities.t new file mode 100644 index 0000000000..a5b34c377b --- /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 diff --git a/t/db_dependent/Serials/SubscriptionIrregularity.t b/t/db_dependent/Serials/SubscriptionIrregularity.t new file mode 100644 index 0000000000..2e81d0fc0d --- /dev/null +++ b/t/db_dependent/Serials/SubscriptionIrregularity.t @@ -0,0 +1,35 @@ +#!/usr/bin/perl + +use Modern::Perl; + +use t::lib::TestBuilder; +use Test::More tests => 6; + +use Koha::Database; + +use_ok('Koha::Subscription'); + +my $schema = Koha::Database->new->schema; +$schema->storage->txn_begin; + +my $builder = t::lib::TestBuilder->new(); + +my $subscription = $builder->build({ + source => 'Subscription', + value => { + irregularity => '2;4;7', + permanent_irregularity => '1;2' + }, +}); + +my $subscription_o = Koha::Subscriptions->find($subscription->{subscriptionid}); +my @irreg = $subscription_o->irregularities; +is($irreg[0], 2, 'First element of irregularities is 2'); +is($irreg[1], 4, 'Second element of irregularities is 4'); +is($irreg[2], 7, 'Third element of irregularities is 7'); + +my @permanent_irreg = $subscription_o->permanent_irregularities; +is($permanent_irreg[0], 1, 'First element of permanent_irregularities is 1'); +is($permanent_irreg[1], 2, 'Second element of permanent_irregularities is 2'); + +$schema->storage->txn_rollback; -- 2.25.1