From 8e7693052696a6f88e159dbbabbf85542435b8b1 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 --- C4/Serials.pm | 142 ++++++++--------- Koha/Subscription.pm | 131 ++++++++++++++- .../Bug_17656-add-permanent-irregularities.perl | 9 ++ installer/data/mysql/kohastructure.sql | 1 + .../intranet-tmpl/prog/css/src/staff-global.scss | 10 ++ .../includes/serials/subscription_daily_choice.inc | 33 ++++ .../serials/subscription_monthly_choice.inc | 38 +++++ .../serials/subscription_weekly_choice.inc | 28 ++++ .../en/modules/serials/showpredictionpattern.tt | 34 ++-- .../prog/en/modules/serials/subscription-add.tt | 1 + .../prog/en/modules/serials/subscription-renew.tt | 41 +++++ .../intranet-tmpl/prog/js/showpredictionpattern.js | 21 ++- .../intranet-tmpl/prog/js/subscription-add.js | 77 +-------- koha-tmpl/intranet-tmpl/prog/js/subscription.js | 78 +++++++++ serials/showpredictionpattern.pl | 15 +- serials/subscription-add.pl | 69 ++++---- serials/subscription-renew.pl | 53 ++++++- t/Serials/IssueNumber.t | 47 ++++++ t/db_dependent/Koha/Subscription.t | 39 ++++- t/db_dependent/Koha/Subscription/GuessEnddate.t | 148 +++++++++++++++++ t/db_dependent/Koha/Subscription/Irregularities.t | 175 +++++++++++++++++++++ t/db_dependent/Serials/SubscriptionIrregularity.t | 35 +++++ 22 files changed, 1014 insertions(+), 211 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 b277871..ce4cc31 100644 --- a/C4/Serials.pm +++ b/C4/Serials.pm @@ -87,6 +87,7 @@ BEGIN { &CountIssues HasItems &subscriptionCurrentlyOnOrder + &issue_number ); } @@ -1263,37 +1264,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. @@ -1304,7 +1274,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, @@ -1338,6 +1308,7 @@ sub ModSubscription { letter => $letter, firstacquidate => $firstacquidate, irregularity => $irregularity, + permanent_irregularity => $permanent_irregularity, numberpattern => $numberpattern, locale => $locale, callnumber => $callnumber, @@ -1385,51 +1356,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; @@ -2684,6 +2656,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 07ab168..39a3da2 100644 --- a/Koha/Subscription.pm +++ b/Koha/Subscription.pm @@ -21,6 +21,9 @@ use Modern::Perl; use Carp; +use C4::Serials::Frequency; +use C4::Serials qw(issue_number); + use Koha::Database; use Koha::Biblios; use Koha::Acquisition::Booksellers; @@ -28,7 +31,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); @@ -197,7 +202,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 0000000..fb6314d --- /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 ae247e6..ab83560 100644 --- a/installer/data/mysql/kohastructure.sql +++ b/installer/data/mysql/kohastructure.sql @@ -4777,6 +4777,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 af5497a..f1157fa 100644 --- a/koha-tmpl/intranet-tmpl/prog/css/src/staff-global.scss +++ b/koha-tmpl/intranet-tmpl/prog/css/src/staff-global.scss @@ -4383,6 +4383,16 @@ div .suggestion_note { } } +.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) { } 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 0000000..6a7fc42 --- /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 0000000..6ba98e3 --- /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 0000000..594243e --- /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 c684348..f9373b3 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 bbeab8a..c857ebb 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 @@ -558,6 +558,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 f3f8c16..3fa0daa 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 f5c38a0..6ab01c0 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 ac656ea..10de62d 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 ); @@ -531,18 +461,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 0000000..3638bcf --- /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 d6bcfc7..0866b22 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 8e55ce0..2208542 100755 --- a/serials/subscription-add.pl +++ b/serials/subscription-add.pl @@ -255,32 +255,14 @@ 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 _get_sub_length { + my ($type, $length) = @_; + return + ( + $type eq 'issues' ? $length : 0, + $type eq 'weeks' ? $length : 0, + $type eq 'months' ? $length : 0, + ); } sub redirect_add_subscription { @@ -304,6 +286,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; @@ -344,19 +327,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 ); @@ -390,6 +380,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'); @@ -457,11 +448,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); @@ -475,7 +473,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 80727da..af32c90 100755 --- a/serials/subscription-renew.pl +++ b/serials/subscription-renew.pl @@ -53,6 +53,9 @@ use C4::Context; use C4::Auth; use C4::Output; use C4::Serials; +use C4::Serials::Frequency; +use C4::Serials::Numberpattern; +use Date::Calc qw(Today Day_of_Year Week_of_Year Add_Delta_Days Add_Delta_YM); use Koha::DateUtils; my $query = CGI->new; @@ -82,6 +85,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( @@ -96,6 +102,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 ); @@ -110,6 +121,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]; @@ -123,14 +138,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 0000000..da07f7a --- /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 6765640..a55db6a 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 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; 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 diff --git a/t/db_dependent/Serials/SubscriptionIrregularity.t b/t/db_dependent/Serials/SubscriptionIrregularity.t new file mode 100644 index 0000000..2e81d0f --- /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.7.4