From dbafff6575da307343bca1a54cb71b1bb0da6c04 Mon Sep 17 00:00:00 2001
From: jeremy breuillard <jeremy.breuillard@biblibre.com>
Date: Mon, 4 Apr 2022 11:00:09 +0200
Subject: [PATCH] Bug 23775: Claiming a serial issue doesn't create the next
 one

Whenever we need to generate manually a new serial we go to page 'serials-edit.pl'. With this patch it is possible to generate a new serial on page 'serials.pl'.

Test Plan:
-- Previously we need a serial which is in EXPECTED status & the Date received should not be later than today --
1)On the intra. Make sure to have at least 1 subscription for a bibliographic record & 1 vendor linked
2)Then Home > Serials > Claims > Claims for <your_vendor_name>
3)Check the row of the serial with the EXPECTED status
4)Click 'Send notification'
5)Notice the status of the row : it is now CLAIMED
6)To verify: Home > Serials > Serial collection information for <your_record_name>
7)Here the status is CLAIMED too but nothing happened around
8)Apply patch
9)Repeat from 2) to 6)
10)The status is still CLAIMED & the new serial with status EXPECTED is freshly generated
---
 C4/Serials.pm                                 | 22 +++++++++-
 .../prog/en/modules/serials/claims.tt         |  7 +++-
 .../en/modules/serials/serials-collection.tt  |  2 +
 .../prog/en/modules/serials/serials-edit.tt   |  6 +++
 serials/claims.pl                             |  9 +++-
 serials/serials-collection.pl                 | 10 +++++
 t/db_dependent/Serials/Claims.t               |  3 +-
 t/db_dependent/Serials/updateClaim.t          | 42 +++++++++++++++++++
 8 files changed, 96 insertions(+), 5 deletions(-)
 create mode 100755 t/db_dependent/Serials/updateClaim.t

diff --git a/C4/Serials.pm b/C4/Serials.pm
index ad772fecac..767114e8e0 100644
--- a/C4/Serials.pm
+++ b/C4/Serials.pm
@@ -1150,7 +1150,6 @@ sub ModSerialStatus {
         my $subscription = GetSubscription($subscriptionid);
         my $pattern = C4::Serials::Numberpattern::GetSubscriptionNumberpattern($subscription->{numberpattern});
         my $frequency = C4::Serials::Frequency::GetSubscriptionFrequency($subscription->{periodicity});
-
         # next issue number
         my (
             $newserialseq,  $newlastvalue1, $newlastvalue2, $newlastvalue3,
@@ -1931,6 +1930,27 @@ sub updateClaim {
     unless ( ref $serialids ) {
         $serialids = [ $serialids ];
     }
+    foreach my $serialid(@$serialids) {
+
+        #warn Dumper("dans la boucle", $serialid, $serialids);
+        my $serial = Koha::Serials->find($serialid);
+        my $serialseq = $serial->serialseq;
+        my $publisheddate = $serial->publisheddate;
+        my $publisheddatetext = $serial->publisheddatetext;
+        my $planneddate = $serial->planneddate;
+        my $status = C4::Serials->CLAIMED;
+        my $notes = $serial->notes;
+
+        C4::Serials::ModSerialStatus(
+            $serialid,
+            $serialseq,
+            $planneddate,
+            $publisheddate,
+            $publisheddatetext,
+            $status,
+            $notes
+        );
+    }
     my $dbh = C4::Context->dbh;
     return $dbh->do(q|
         UPDATE serial
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/serials/claims.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/serials/claims.tt
index 9c9278a7f2..cfe0646686 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/serials/claims.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/serials/claims.tt
@@ -45,7 +45,6 @@
             <main>
 
     <h1>Claims [% IF ( suppliername ) %] for [% suppliername | html %] [% END %]</h1>
-
     [% IF error_claim %]
         [% IF error_claim == 'no_vendor_email' %]
             <div class="dialog alert">This vendor has no email defined for late issues.</div>
@@ -70,6 +69,7 @@
 	    <label for="supplierid">Vendor: </label>
 	    <select id="supplierid" name="supplierid">
 	        [% FOREACH suploo IN suploop %]
+            [% USE Dumper %] [% Dumper.dump(suploo) %]
 	            [% IF ( suploo.selected ) %]
 	            <option value="[% suploo.id | html %]" selected="selected" >
 	            [% ELSE %]
@@ -122,6 +122,9 @@
                         [% FOR field IN additional_fields_for_subscription %]
                           <th>[% field.name | html %]</th>
                         [% END %]
+                        
+                            [% Dumper.dump('claimletter', claimletter) %]
+
                     </tr>
                 </thead>
                 <tfoot>
@@ -212,7 +215,7 @@
         </form>
     </fieldset>
 [% END %]
-
+[% USE Dumper %] [% Dumper.dump('missingissues', missingissues) %]
             </main>
         </div> <!-- /.col-sm-10.col-sm-push-2 -->
 
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/serials/serials-collection.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/serials/serials-collection.tt
index 69575baacc..20d9fcc1e2 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/serials/serials-collection.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/serials/serials-collection.tt
@@ -197,6 +197,8 @@
           </thead>
           <tbody>
       [% FOREACH serial IN year.serials %]
+      [% USE Dumper %] [% Dumper.dump(" voici les serial id visibles sur serial collection :", serial.serialid) %]
+
         <tr>
             [% IF ( CAN_user_serials_receive_serials ) %]
                 <td>
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/serials/serials-edit.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/serials/serials-edit.tt
index 642dd2cc5e..47c71630a9 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/serials/serials-edit.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/serials/serials-edit.tt
@@ -132,6 +132,8 @@ $(document).ready(function() {
 		<th>Notes</th>
 	</tr>
 [% FOREACH serialslis IN serialslist %]
+[% USE Dumper %]
+
     <tr>
         <td>
             <input type="hidden" name="serialid" value="[% serialslis.serialid | html %]" />
@@ -295,6 +297,8 @@ $(document).ready(function() {
 [% END %]
 [% FOREACH newserialloo IN newserialloop %]
     [% UNLESS ( newserialloo.subscriptionexpired ) %]
+[% USE Dumper %] [% Dumper.dump('newserialloo :', newserialloo) %]
+
     <tr>
         <td>
             <input type="hidden" name="serialid" value="NEW" />
@@ -380,6 +384,7 @@ $(document).ready(function() {
         </td>
     </tr>
   [% IF ( newserialloo.serialsadditems ) %]
+  [% Dumper.dump('serialsadditems:', serialsadditems) %]
           <tr>
           <td colspan="6">
       <a id="label[% newserialloo.subscriptionid | html %][% newserialloo.serialid | html %]" style="color: grey; font-size: 80%; cursor: pointer;"  onclick="unHideItems('items[% newserialloo.subscriptionid | html %][% newserialloo.serialid | html %]','label[% newserialloo.subscriptionid | html %][% newserialloo.serialid | html %]', '[% newserialloo.serialid | html %]')">
@@ -449,6 +454,7 @@ $(document).ready(function() {
     <fieldset class="action">
         <div class="btn-group"><input type="submit" value="Save" class="btn btn-default"  accesskey="w" /></div>
         [% UNLESS ( serialsadditems ) %]&nbsp;&nbsp;
+        [% Dumper.dump('serialsadditems', serialsadditems) %]
             <div class="btn-group"><a href="#multi_receiving" role="button" class="btn btn-default" data-toggle="modal"><i class="fa fa-plus"></i> Multi receiving</a></div>
         [% END %]
 
diff --git a/serials/claims.pl b/serials/claims.pl
index a16d840efe..53bd923b83 100755
--- a/serials/claims.pl
+++ b/serials/claims.pl
@@ -25,15 +25,21 @@ use C4::Output qw( output_html_with_http_headers );
 use C4::Context;
 use C4::Letters qw( GetLetters SendAlerts );
 
+
 use Koha::AdditionalFields;
 use Koha::CsvProfiles;
+use Koha::Serial;
 
-my $input = CGI->new;
+use Data::Dumper;
 
+my $input = CGI->new;
+#warn Dumper('CGI new :', $input);
 my $serialid = $input->param('serialid');
 my $op = $input->param('op');
+#warn Dumper('op :', $op);
 my $claimletter = $input->param('claimletter');
 my $supplierid = $input->param('supplierid');
+#warn Dumper('claimletter :', $claimletter);
 my $suppliername = $input->param('suppliername');
 
 # open template first (security & userenv set here)
@@ -64,6 +70,7 @@ if (@serialnums) { # i.e. they have been flagged to generate claims
             C4::Serials::updateClaim( \@serialnums );
         }
     };
+
     if ( $@ ) {
         $template->param(error_claim => $@);
     } elsif ( ref $err and exists $err->{error} ) {
diff --git a/serials/serials-collection.pl b/serials/serials-collection.pl
index 2179b83069..4d1da13448 100755
--- a/serials/serials-collection.pl
+++ b/serials/serials-collection.pl
@@ -31,6 +31,8 @@ use Koha::DateUtils qw( dt_from_string );
 
 use List::MoreUtils qw( uniq );
 
+use Data::Dumper;
+
 
 my $query = CGI->new;
 my $op = $query->param('op') || q{};
@@ -108,17 +110,24 @@ if($op eq 'gennext' && @subscriptionid){
 
 my $countitems = 0;
 my @serialsid = $query->multi_param('serialid');
+warn Dumper('serial', \@serialsid);
+
 my $subscriptionid = $subscriptionid[0];
+    warn Dumper('subscription', $subscriptionid);
+
 
 if($op eq 'delete_confirm'){
     foreach my $serialid (@serialsid){
         $countitems += Koha::Serial::Items->search({serialid => $serialid})->count();
+        warn Dumper('serial', \@serialsid);
     }
 }elsif($op eq 'delete_confirmed'){
     if($query->param('delitems') eq "Yes"){
         my @itemnumbers;
         foreach my $serialid (@serialsid){
+            warn Dumper('serial', \@serialsid);
             my @ids = Koha::Serial::Items->search({serialid => $serialid})->get_column('itemnumber');
+            warn Dumper('serial', \@serialsid);
             push(@itemnumbers, @ids);
         }
         my $items = Koha::Items->search({ itemnumber => \@itemnumbers });
@@ -131,6 +140,7 @@ if($op eq 'delete_confirm'){
     for my $serialid (@serialsid){
         my $serial = Koha::Serials->find($serialid);
         ModSerialStatus($serialid, $serial->serialseq, $serial->planneddate, $serial->publisheddate, $serial->publisheddatetext, 6, "");
+        warn Dumper('serial', \@serialsid);
     }
 }
 
diff --git a/t/db_dependent/Serials/Claims.t b/t/db_dependent/Serials/Claims.t
index afc3a18e66..b2651ebc4a 100755
--- a/t/db_dependent/Serials/Claims.t
+++ b/t/db_dependent/Serials/Claims.t
@@ -76,7 +76,9 @@ my $sample_supplier2 = {
 };
 
 my $supplier1 = Koha::Acquisition::Bookseller->new($sample_supplier1)->store;
+
 my $supplier2 = Koha::Acquisition::Bookseller->new($sample_supplier2)->store;
+
 my $supplier_id1 = $supplier1->id;
 my $supplier_id2 = $supplier2->id;
 
@@ -130,7 +132,6 @@ is( GetLateOrMissingIssues(), undef, 'GetLateOrMissingIssues should return undef
 
 my @late_or_missing_issues = GetLateOrMissingIssues( $supplier_id1 );
 is( scalar(@late_or_missing_issues), 1, 'supplier 1 should have 1 issue in late' );
-
 @late_or_missing_issues = GetLateOrMissingIssues( $supplier_id2);
 is( scalar(@late_or_missing_issues), 2, 'supplier 2 should have 2 issues in late' );
 
diff --git a/t/db_dependent/Serials/updateClaim.t b/t/db_dependent/Serials/updateClaim.t
new file mode 100755
index 0000000000..1eed7db318
--- /dev/null
+++ b/t/db_dependent/Serials/updateClaim.t
@@ -0,0 +1,42 @@
+use Modern::Perl;
+use Test::More tests => 3;
+
+use C4::Serials;
+use Koha::Database;
+use Koha::DateUtils qw( dt_from_string output_pref );
+
+my $schema = Koha::Database->new->schema;
+$schema->storage->txn_begin;
+my $dbh = C4::Context->dbh;
+
+$dbh->do(q|DELETE FROM issues|);
+$dbh->do(q|DELETE FROM subscription|);
+
+my $branchcode = 'CPL';
+my $record = MARC::Record->new();
+my ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio($record, '');
+
+# Create a new subscription
+my $subscriptionid_claims = C4::Serials::NewSubscription(
+undef,      $branchcode,     undef, undef, undef, $biblionumber,
+'2013-01-01', undef, undef, undef,  undef,
+undef,      undef,  undef, undef, undef, undef,
+1,          "notes",undef, '9999-01-01', undef, undef,
+undef,       undef,  0,    "intnotes",  0,
+undef, undef, 0,          undef,         '2013-12-31', 0
+);
+
+# Verify and get the serial ID of the subscription
+my ( $totalissues, @serials ) = C4::Serials::GetSerials($subscriptionid_claims, 1);
+
+C4::Serials::updateClaim( $serials[0]->{serialid} ); # Updating the claim
+# sort the result to separate the CLAIMED and EXPECTED status
+@serials = sort { $a->{serialid} <=> $b->{serialid} } @serials;
+
+# Verify if serial IDs are correctly generated
+( $totalissues, @serials ) = C4::Serials::GetSerials($subscriptionid_claims);
+
+is ( scalar(@serials), 2, "le test est terminé" );  # Gives the length of the @serials
+
+is ( ($serials[0]->{status}), C4::Serials::CLAIMED, "test CLAIMED" );
+is ( ($serials[1]->{status}), C4::Serials::EXPECTED, "test EXPECTED" );
-- 
2.17.1