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 <victor@tuxayo.net>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
This commit is contained in:
Jonathan Druart 2020-07-09 10:52:11 +02:00 committed by Tomas Cohen Arazi
parent 46d3ec264c
commit 189851fb9b
2 changed files with 20 additions and 3 deletions

View file

@ -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;

View file

@ -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;
};