From 6fe5b24a1c6de413a9ac7efed50ad27858da1add Mon Sep 17 00:00:00 2001
From: Tomas Cohen Arazi <tomascohen@theke.io>
Date: Wed, 18 Dec 2024 09:58:14 -0300
Subject: [PATCH] Bug 38330: Make sure biblio.opac_suppressed is
 populated/updated on save

This patchset adds a new column to biblio/deletedbiblio for storing the
information about OPAC suppression for the record.

It works as any other framework-mapped attribute. It defaults to 0 (not
suppressed) and some sanitization is done on storing, because the DB
should only contain 0 or 1.

To test:
0. Have a couple records with 942$n set to suppress them.
1. Apply this patches
2. Run:
   $ ktd --shell
  k$ updatedatabase
=> SUCCESS: All good
3. Check the `opac_suppressed` column was added to both tables
4. Notice it tells you need to run `touch_all_biblios.pl`
5. Run:
  k$ koha-mysql kohadev
   > SELECT opac_suppressed,COUNT(opac_suppressed) FROM biblio GROUP BY opac_suppressed;
=> FAIL: There are no records with the opac_suppressed flag set to 1
6. Run:
  k$ perl misc/maintenance/touch_all_biblios.pl -v
=> SUCCESS: No failures
7. Repeat 5
=> SUCCESS: Your couple records have the flag set. This means ModBiblio
is doing the right thing
8. Play with adding new records (suppressed and not)
=> SUCCESS: Suppression status is set correctly
9. Change the status to select records
=> SUCCESS: The DB column and the MARC are in sync!
10. Sign off :-D

Signed-off-by: Andrew Fuerste Henry <andrew@bywatersolutions.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: David Cook <dcook@prosentient.com.au>
---
 C4/Biblio.pm | 36 ++++++++++++++++++++++--------------
 1 file changed, 22 insertions(+), 14 deletions(-)

diff --git a/C4/Biblio.pm b/C4/Biblio.pm
index 6a0449f05f..685c91e4c1 100644
--- a/C4/Biblio.pm
+++ b/C4/Biblio.pm
@@ -230,20 +230,21 @@ sub AddBiblio {
 
                 my $biblio = Koha::Biblio->new(
                     {
-                        frameworkcode => $frameworkcode,
-                        author        => $olddata->{author},
-                        title         => $olddata->{title},
-                        subtitle      => $olddata->{subtitle},
-                        medium        => $olddata->{medium},
-                        part_number   => $olddata->{part_number},
-                        part_name     => $olddata->{part_name},
-                        unititle      => $olddata->{unititle},
-                        notes         => $olddata->{notes},
-                        serial        => $olddata->{serial},
-                        seriestitle   => $olddata->{seriestitle},
-                        copyrightdate => $olddata->{copyrightdate},
-                        datecreated   => \'NOW()',
-                        abstract      => $olddata->{abstract},
+                        frameworkcode   => $frameworkcode,
+                        author          => $olddata->{author},
+                        title           => $olddata->{title},
+                        subtitle        => $olddata->{subtitle},
+                        medium          => $olddata->{medium},
+                        part_number     => $olddata->{part_number},
+                        part_name       => $olddata->{part_name},
+                        unititle        => $olddata->{unititle},
+                        notes           => $olddata->{notes},
+                        serial          => $olddata->{serial},
+                        seriestitle     => $olddata->{seriestitle},
+                        copyrightdate   => $olddata->{copyrightdate},
+                        datecreated     => \'NOW()',
+                        abstract        => $olddata->{abstract},
+                        opac_suppressed => $olddata->{opac_suppressed},
                     }
                 )->store;
                 $biblionumber = $biblio->biblionumber;
@@ -2417,6 +2418,10 @@ sub TransformMarcToKoha {
             # Additional polishing for individual kohafields
             if ( $kohafield =~ /copyrightdate|publicationyear/ ) {
                 $value = _adjust_pubyear($value);
+            } elsif ( $kohafield eq 'biblio.opac_suppressed' ) {
+
+                # this should always be a boolean
+                $value = $value ? 1 : 0;
             }
         }
 
@@ -2655,6 +2660,9 @@ sub _koha_modify_biblio {
         ;
     my $sth = $dbh->prepare($query);
 
+    # it always needs to be defined
+    $biblio->{opac_suppressed} //= 0;
+
     $sth->execute(
         $frameworkcode,      $biblio->{'author'},      $biblio->{'title'},     $biblio->{'subtitle'},
         $biblio->{'medium'}, $biblio->{'part_number'}, $biblio->{'part_name'}, $biblio->{'unititle'},
-- 
2.39.5