From 1ff7577a883b7590f677d90cff5469d383a688d2 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
---
 C4/Biblio.pm | 36 ++++++++++++++++++++++--------------
 1 file changed, 22 insertions(+), 14 deletions(-)

diff --git a/C4/Biblio.pm b/C4/Biblio.pm
index 647aee75c2c..a3de492576a 100644
--- a/C4/Biblio.pm
+++ b/C4/Biblio.pm
@@ -229,20 +229,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;
@@ -2364,6 +2365,10 @@ sub TransformMarcToKoha {
             if( $kohafield =~ /copyrightdate|publicationyear/ ) {
                 $value = _adjust_pubyear( $value );
             }
+            elsif ( $kohafield eq 'biblio.opac_suppressed' ) {
+                # this should always be a boolean
+                $value = $value ? 1 : 0;
+            }
         }
 
         next if !defined $value;
@@ -2595,6 +2600,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.47.1