From b79196013d619aefc521f8886744c4d3057b9e07 Mon Sep 17 00:00:00 2001 From: Marcel de Rooy Date: Mon, 5 Feb 2024 13:09:54 +0000 Subject: [PATCH] Bug 35993: Add SimpleMarc::update_last_transaction_time Note: This routine does not check field existence like ModBiblioMarc does/did (see next patch). So it inserts a field 005 if it is not present. Test plan: Run t/SimpleMARC.t Signed-off-by: Marcel de Rooy Signed-off-by: Phil Ringnalda --- Koha/SimpleMARC.pm | 49 +++++++++++++++++++++++++++++++++++++--------- t/SimpleMARC.t | 20 +++++++++++++++++-- 2 files changed, 58 insertions(+), 11 deletions(-) diff --git a/Koha/SimpleMARC.pm b/Koha/SimpleMARC.pm index a77ce9ea9e..be7f025ea2 100644 --- a/Koha/SimpleMARC.pm +++ b/Koha/SimpleMARC.pm @@ -18,21 +18,24 @@ package Koha::SimpleMARC; use Modern::Perl; +use constant LAST_TRANSACTION_FIELD => q/005/; # MARC21/UNIMARC + our (@ISA, @EXPORT_OK); BEGIN { require Exporter; our @ISA = qw(Exporter); @EXPORT_OK = qw( - read_field - add_field - update_field - copy_field - copy_and_replace_field - move_field - delete_field - field_exists - field_equals + read_field + add_field + update_field + copy_field + copy_and_replace_field + move_field + delete_field + field_exists + field_equals + update_last_transaction_time ); } @@ -525,6 +528,33 @@ sub delete_field { } } +=head3 update_last_transaction_time + + update_last_transaction_time( { record => $record } ); + + Inserts or updates field for last transaction (005) + +=cut + +sub update_last_transaction_time { + my ($params) = @_; + my $record = $params->{record}; + + my @localtime = (localtime)[ 5, 4, 3, 2, 1, 0 ]; + $localtime[0] += 1900; # add century + $localtime[1]++; # month 1-based + + my $value = sprintf( "%4d%02d%02d%02d%02d%04.1f", @localtime ); + my $field; + if ( $field = $record->field(LAST_TRANSACTION_FIELD) ) { + $field->update($value); + } else { + $record->insert_fields_ordered( + MARC::Field->new( LAST_TRANSACTION_FIELD, $value ), + ); + } +} + sub _delete_field { my ( $params ) = @_; my $record = $params->{record}; @@ -663,5 +693,6 @@ sub _modify_values { } return @$values; } + 1; __END__ diff --git a/t/SimpleMARC.t b/t/SimpleMARC.t index cb53df77e5..3e8e840ff8 100755 --- a/t/SimpleMARC.t +++ b/t/SimpleMARC.t @@ -2,11 +2,14 @@ use Modern::Perl; -use Test::More tests => 11; +use Test::More tests => 12; use_ok("MARC::Field"); use_ok("MARC::Record"); -use_ok("Koha::SimpleMARC", qw( field_exists read_field update_field copy_field copy_and_replace_field move_field delete_field field_equals )); +use_ok( + "Koha::SimpleMARC", + qw( field_exists read_field update_field copy_field copy_and_replace_field move_field delete_field field_equals update_last_transaction_time ) +); sub new_record { my $record = MARC::Record->new; @@ -1833,3 +1836,16 @@ subtest 'field_equals' => sub { is_deeply( $match, [1], 'first 008 control field matches "eng"' ); }; }; + +subtest 'update_last_transaction_time' => sub { + plan tests => 3; + my $record = MARC::Record->new; + update_last_transaction_time( { record => $record } ); + my $value1 = $record->field('005')->data; + like( $value1, qr/^\d{14}\.0$/, 'Looks like a 005' ); + sleep 1; + update_last_transaction_time( { record => $record } ); + my $value2 = $record->field('005')->data; + like( $value2, qr/^\d{14}\.0$/, 'Still looks like a 005' ); + isnt( $value1, $value2, 'Should not be the same a second later' ); +}; -- 2.40.0