From 112e74019399eb797e0bbe68a2c68c5cb3018692 Mon Sep 17 00:00:00 2001 From: Alex Arnaud Date: Tue, 19 Dec 2017 15:40:06 +0000 Subject: [PATCH] Bug 17656 - Update irregularities on subscription renewals 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 - Renew subscription from 2017-10-16 - Check that you have the prediction pattern table on renwal page - Submit renewal - Check that irregularity and permanent_irregularity column have been updated. - Do the same for a monthly and weekly subscription --- C4/Serials.pm | 35 ++++++++++ Koha/Subscription.pm | 22 ++++++ .../prog/en/modules/serials/subscription-add.tt | 1 + .../prog/en/modules/serials/subscription-renew.tt | 42 ++++++++++++ .../intranet-tmpl/prog/js/showpredictionpattern.js | 1 - .../intranet-tmpl/prog/js/subscription-add.js | 69 ------------------- koha-tmpl/intranet-tmpl/prog/js/subscription.js | 71 +++++++++++++++++++ serials/subscription-add.pl | 36 ++-------- serials/subscription-renew.pl | 80 +++++++++++++++++++--- t/db_dependent/Koha/Subscription.t | 42 +++++++++++- 10 files changed, 288 insertions(+), 111 deletions(-) create mode 100644 koha-tmpl/intranet-tmpl/prog/js/subscription.js diff --git a/C4/Serials.pm b/C4/Serials.pm index 57e3eb6..52bf2bb 100644 --- a/C4/Serials.pm +++ b/C4/Serials.pm @@ -87,6 +87,7 @@ BEGIN { HasItems &GetSubscriptionsFromBorrower &subscriptionCurrentlyOnOrder + &GuessEnddate ); } @@ -2712,6 +2713,40 @@ sub findSerialsByStatus { return @$serials; } +=head2 GessEnddate + +my $enddate = GessEnddate($startdate_iso $frequencyid, $numberlength, $weeklength, $monthlength); + +=cut + +sub GuessEnddate { + my ($startdate_iso, $frequencyid, $numberlength, $weeklength, $monthlength) = @_; + my ($year, $month, $day); + my $enddate; + if($numberlength != 0) { + my $frequency = GetSubscriptionFrequency($frequencyid); + if($frequency->{'unit'} eq 'day') { + ($year, $month, $day) = Add_Delta_Days(split(/-/, $startdate_iso), $numberlength * $frequency->{'unitsperissue'} / $frequency->{'issuesperunit'}); + } elsif($frequency->{'unit'} eq 'week') { + ($year, $month, $day) = Add_Delta_Days(split(/-/, $startdate_iso), $numberlength * 7 * $frequency->{'unitsperissue'} / $frequency->{'issuesperunit'}); + } elsif($frequency->{'unit'} eq 'month') { + ($year, $month, $day) = Add_Delta_YM(split(/-/, $startdate_iso), 0, $numberlength * $frequency->{'unitsperissue'} / $frequency->{'issuesperunit'}); + } elsif($frequency->{'unit'} eq 'year') { + ($year, $month, $day) = Add_Delta_YM(split(/-/, $startdate_iso), $numberlength * $frequency->{'unitsperissue'} / $frequency->{'issuesperunit'}, 0); + } + } elsif($weeklength != 0) { + ($year, $month, $day) = Add_Delta_Days(split(/-/, $startdate_iso), $weeklength * 7); + } elsif($monthlength != 0) { + ($year, $month, $day) = Add_Delta_YM(split(/-/, $startdate_iso), 0, $monthlength); + } + if(defined $year) { + $enddate = sprintf("%04d-%02d-%02d", $year, $month, $day); + } else { + undef $enddate; + } + return $enddate; +} + 1; __END__ diff --git a/Koha/Subscription.pm b/Koha/Subscription.pm index 839f17a..8d5aeca 100644 --- a/Koha/Subscription.pm +++ b/Koha/Subscription.pm @@ -72,6 +72,28 @@ sub permanent_irregularities { return split(';', $self->permanent_irregularity); } +=head3 subtype + +Return the length type (issues, weeks, months) + +=cut + +sub subtype { + my ($self) = @_; + + if ($self->numberlength) { + return 'issues'; + } + + if ($self->weeklength) { + return 'weeks'; + } + + if ($self->monthlength) { + return 'months'; + } +} + =head3 type =cut 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 f9c0cae..7d3fbae 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 @@ -510,6 +510,7 @@ fieldset.rows li.radio { width: 100%; } /* override staff-global.css */ var MSG_PUB_DATE_UNDEFINED = _("First publication date is not defined"); var MSG_NEXT_ISSUE_UNDEFINED = _("Next issue publication date is not defined"); + [% 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 64a264e..05674ee 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 @@ -24,6 +24,36 @@
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Subscription renewal for [% bibliotitle %]
  1. @@ -38,13 +68,25 @@
  • +
  • +
    [% END %] [% MACRO jsinclude BLOCK %] [% INCLUDE 'calendar.inc' %] + + + [% END %] [% INCLUDE 'intranet-bottom.inc' %] diff --git a/koha-tmpl/intranet-tmpl/prog/js/showpredictionpattern.js b/koha-tmpl/intranet-tmpl/prog/js/showpredictionpattern.js index ffc7013..4e2418d 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/showpredictionpattern.js +++ b/koha-tmpl/intranet-tmpl/prog/js/showpredictionpattern.js @@ -21,7 +21,6 @@ function Check_permanent_irregularities() { // So check them according to permanent irregularity if (nothing_checked) { $(".skip").each(function() { - console.log('iciiiiiiiiiiiiii'); if ($(this).is(':checked')) { 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 c995751..bf45472 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/subscription-add.js +++ b/koha-tmpl/intranet-tmpl/prog/js/subscription-add.js @@ -225,75 +225,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 += _("- Frequency is not defined") + "\n"; - error ++; - } - acquidate = $("#acqui_date").val(); - if(acquidate == undefined || acquidate == ""){ - error_msg += _("- First publication date is not defined") + "\n"; - error ++; - } - if( more_than_one_serial !== "" ){ - var nextacquidate = $("#nextacquidate").val(); - if(nextacquidate == undefined || nextacquidate == ""){ - error_msg += _("- Next issue publication date is not defined") + "\n"; - error ++; - } - } - - if(error){ - alert(_("Cannot test prediction pattern for the following reason(s): %s").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' - ]; - 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) { - $("#displayexample").html(data); - patternneedtobetested = 0; - } - }); -} - function saveAdvancedPattern() { if ($("#patternname").val().length == 0) { alert(_("Please enter a name for this pattern")); 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..f8dea82 --- /dev/null +++ b/koha-tmpl/intranet-tmpl/prog/js/subscription.js @@ -0,0 +1,71 @@ + +var advancedpatternlocked; + +function testPredictionPattern() { + var frequencyid = $("#frequency").val(); + var acquidate; + var error = 0; + var error_msg = ""; + if(frequencyid == undefined || frequencyid == ""){ + error_msg += _("- Frequency is not defined") + "\n"; + error ++; + } + acquidate = $("#acqui_date").val(); + if(acquidate == undefined || acquidate == ""){ + error_msg += _("- First publication date is not defined") + "\n"; + error ++; + } + if( more_than_one_serial !== "" ){ + var nextacquidate = $("#nextacquidate").val(); + if(nextacquidate == undefined || nextacquidate == ""){ + error_msg += _("- Next issue publication date is not defined") + "\n"; + error ++; + } + } + + if(error){ + alert(_("Cannot test prediction pattern for the following reason(s): %s").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' + ]; + 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) { + $("#displayexample").html(data); + patternneedtobetested = 0; + } + }); +} diff --git a/serials/subscription-add.pl b/serials/subscription-add.pl index bb04ecd..13413cb 100755 --- a/serials/subscription-add.pl +++ b/serials/subscription-add.pl @@ -258,34 +258,6 @@ sub _get_sub_length { ); } -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 $auser = $query->param('user'); my $branchcode = $query->param('branchcode'); @@ -331,9 +303,9 @@ sub redirect_add_subscription { if(!defined $enddate || $enddate eq '') { if($subtype eq "issues") { - $enddate = _guess_enddate($firstacquidate, $periodicity, $numberlength, $weeklength, $monthlength) + $enddate = GuessEnddate($firstacquidate, $periodicity, $numberlength, $weeklength, $monthlength) } else { - $enddate = _guess_enddate($startdate, $periodicity, $numberlength, $weeklength, $monthlength) + $enddate = GuessEnddate($startdate, $periodicity, $numberlength, $weeklength, $monthlength) } } @@ -408,9 +380,9 @@ sub redirect_mod_subscription { # Guess end date if(!defined $enddate || $enddate eq '') { if($subtype eq "issues") { - $enddate = _guess_enddate($nextacquidate, $periodicity, $numberlength, $weeklength, $monthlength); + $enddate = GuessEnddate($nextacquidate, $periodicity, $numberlength, $weeklength, $monthlength); } else { - $enddate = _guess_enddate($startdate, $periodicity, $numberlength, $weeklength, $monthlength); + $enddate = GuessEnddate($startdate, $periodicity, $numberlength, $weeklength, $monthlength); } } diff --git a/serials/subscription-renew.pl b/serials/subscription-renew.pl index bbbc8e5..a997c64 100755 --- a/serials/subscription-renew.pl +++ b/serials/subscription-renew.pl @@ -54,6 +54,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 = new CGI; @@ -74,6 +77,8 @@ my ( $template, $loggedinuser, $cookie ) = get_template_and_user( } ); if ( $op eq "renew" ) { + 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' } ); ReNewSubscription( $subscriptionid, $loggedinuser, @@ -81,6 +86,11 @@ if ( $op eq "renew" ) { $query->param('weeklength'), $query->param('monthlength'), $query->param('note') ); + + my $subscription = Koha::Subscriptions->find($subscriptionid); + $subscription->permanent_irregularity(join(';', @permanent_irregularities)); + $subscription->irregularity(join(';', @irregularities)); + $subscription->store; } my $subscription = GetSubscription($subscriptionid); @@ -92,15 +102,69 @@ if ($subscription->{'cannotedit'}){ 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 $subtype = $subscription_o->subtype; +my $nextexpected = GetNextExpected($subscriptionid); +my $enddate = GuessEnddate($subscription->{enddate} || dt_from_string, + $subscription->{periodicity}, + $subscription->{numberlength}, + $subscription->{weeklength}, + $subscription->{monthlength}); + +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, - numberlength => $subscription->{numberlength}, - weeklength => $subscription->{weeklength}, - monthlength => $subscription->{monthlength}, - subscriptionid => $subscriptionid, - bibliotitle => $subscription->{bibliotitle}, - $op => 1, - popup => ($mode eq 'popup'), + startdate => $newstartdate, + numberlength => $subscription->{numberlength}, + weeklength => $subscription->{weeklength}, + monthlength => $subscription->{monthlength}, + subscriptionid => $subscriptionid, + irregularity => $subscription->{irregularity}, + periodicity => $subscription->{periodicity}, + firstacquidate => $subscription->{firstacquidate}, + nextacquidate => $nextexpected->{planneddate}, + enddate => $enddate, + subtype => $subtype, + sublength => $sub_length, + numberingmethod => $numberpattern->{numberingmethod}, + lastvalue1 => $subscription->{lastvalue1}, + lastvalue2 => $subscription->{lastvalue2}, + lastvalue3 => $subscription->{lastvalue3}, + add1 => $numberpattern->{add1}, + add2 => $numberpattern->{add2}, + add3 => $numberpattern->{add3}, + every1 => $numberpattern->{every1}, + every2 => $numberpattern->{every2}, + every3 => $numberpattern->{every3}, + innerloop1 => $subscription->{innerloop1}, + innerloop2 => $subscription->{innerloop2}, + innerloop3 => $subscription->{innerloop3}, + setto1 => $numberpattern->{setto1}, + setto2 => $numberpattern->{setto2}, + setto3 => $numberpattern->{setto3}, + numbering1 => $numberpattern->{numbering1}, + numbering2 => $numberpattern->{numbering2}, + numbering3 => $numberpattern->{numbering3}, + whenmorethan1 => $numberpattern->{whenmorethan1}, + whenmorethan2 => $numberpattern->{whenmorethan2}, + whenmorethan3 => $numberpattern->{whenmorethan3}, + locale => $subscription->{locale}, + bibliotitle => $subscription->{bibliotitle}, + $op => 1, + popup => ($mode eq 'popup'), ); # Print the page diff --git a/t/db_dependent/Koha/Subscription.t b/t/db_dependent/Koha/Subscription.t index 46256d3..e47ca75 100644 --- a/t/db_dependent/Koha/Subscription.t +++ b/t/db_dependent/Koha/Subscription.t @@ -19,7 +19,8 @@ use Modern::Perl; -use Test::More tests => 1; +use Test::More tests => 2; +use t::lib::TestBuilder; use Koha::Subscription; use Koha::Biblio; @@ -27,6 +28,8 @@ use Koha::Biblio; my $schema = Koha::Database->new->schema; $schema->storage->txn_begin; +my $builder = t::lib::TestBuilder->new(); + subtest 'Koha::Subscription::biblio' => sub { plan tests => 1; @@ -39,6 +42,43 @@ subtest 'Koha::Subscription::biblio' => sub { is($b->biblionumber, $biblio->biblionumber, 'Koha::Subscription::biblio returns the correct biblio'); }; +subtest 'Koha::Subscription::subtype' => 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->subtype, '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->subtype, '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->subtype, 'months', 'Subscription with monthlength has months type'); +}; + $schema->storage->txn_rollback; 1; -- 2.7.4