From 4d3425295b1b5c6b0c0fcd9f5e073eb1b5077e50 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Thu, 9 Jul 2020 10:52:11 +0200 Subject: [PATCH] Bug 25964: Prevent data loss when editing items from a MARC record Coming from: Bug 23463: Use new method Koha::Object->set_or_blank We have DB fields that are not mapped with MARC fields, for instance paidfor. They are not handled correctly. In ModItemFromMarc, we get a MARC record in parameter and update the item in DB. But we are loosing the fields that are not in the MARC record Signed-off-by: Victor Grousset/tuxayo --- C4/Items.pm | 13 +++++++++++++ t/db_dependent/Items.t | 10 +++++++--- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/C4/Items.pm b/C4/Items.pm index 53bf3edb65..f2db7a4894 100644 --- a/C4/Items.pm +++ b/C4/Items.pm @@ -293,6 +293,19 @@ sub ModItemFromMarc { $localitemmarc->append_fields( $item_marc->field($itemtag) ); my $item_object = Koha::Items->find($itemnumber); my $item = TransformMarcToKoha( $localitemmarc, $frameworkcode, 'items' ); + + # Retrieving the values for the fields that are not linked + my @mapped_fields = Koha::MarcSubfieldStructures->search( + { + frameworkcode => $frameworkcode, + kohafield => { -like => "items.%" } + } + )->get_column('kohafield'); + for my $c ( $item_object->_result->result_source->columns ) { + next if grep { "items.$c" eq $_ } @mapped_fields; + $item->{$c} = $item_object->$c; + } + $item->{cn_source} = delete $item->{'items.cn_source'}; # Because of C4::Biblio::_disambiguate $item->{cn_sort} = delete $item->{'items.cn_sort'}; # Because of C4::Biblio::_disambiguate $item->{itemnumber} = $itemnumber; diff --git a/t/db_dependent/Items.t b/t/db_dependent/Items.t index 61a691b458..010b41e97a 100755 --- a/t/db_dependent/Items.t +++ b/t/db_dependent/Items.t @@ -601,12 +601,12 @@ subtest 'SearchItems test' => sub { # Make sure the link is used my $item3 = Koha::Items->find($item3_itemnumber); - ok($item3->itemnotes eq 'foobar', 'itemnotes eq "foobar"'); + is($item3->itemnotes, 'foobar', 'itemnotes eq "foobar"'); # Do the same search again. # This time it will search in items.itemnotes ($items, $total_results) = SearchItems($filter); - ok(scalar @$items == 1, 'found 1 item with itemnotes = "foobar"'); + is(scalar(@$items), 1, 'found 1 item with itemnotes = "foobar"'); my ($cpl_items_after) = SearchItems( { field => 'homebranch', query => $library1->{branchcode} } ); is( ( scalar( @$cpl_items_after ) - scalar ( @$cpl_items_before ) ), 1, 'SearchItems should return something' ); @@ -984,7 +984,7 @@ subtest 'Split subfields in Item2Marc (Bug 21774)' => sub { }; subtest 'ModItemFromMarc' => sub { - plan tests => 2; + plan tests => 4; $schema->storage->txn_begin; my $builder = t::lib::TestBuilder->new; @@ -1009,6 +1009,8 @@ subtest 'ModItemFromMarc' => sub { my $item = Koha::Items->find($itemnumber); is( $item->itemlost, 1, 'itemlost picked from the item marc'); + $item->paidfor("this is something")->store; + my $updated_item_record = new MARC::Record; $updated_item_record->append_fields( MARC::Field->new( @@ -1019,6 +1021,8 @@ subtest 'ModItemFromMarc' => sub { my $updated_item = ModItemFromMarc($updated_item_record, $biblio->biblionumber, $itemnumber); is( $updated_item->{itemlost}, 0, 'itemlost should have been reset to the default value in DB' ); + is( $updated_item->{paidfor}, "this is something", "Non mapped field has not been reset" ); + is( Koha::Items->find($itemnumber)->paidfor, "this is something" ); $schema->storage->txn_rollback; }; -- 2.27.0