View | Details | Raw Unified | Return to bug 23463
Collapse All | Expand All

(-)a/C4/Items.pm (-1 / +3 lines)
Lines 281-287 sub ModItemFromMarc { Link Here
281
    my $item_object = Koha::Items->find($itemnumber);
281
    my $item_object = Koha::Items->find($itemnumber);
282
    my $item = TransformMarcToKoha( $localitemmarc, $frameworkcode, 'items' );
282
    my $item = TransformMarcToKoha( $localitemmarc, $frameworkcode, 'items' );
283
    $item->{cn_source} = delete $item->{'items.cn_source'}; # Because of C4::Biblio::_disambiguate
283
    $item->{cn_source} = delete $item->{'items.cn_source'}; # Because of C4::Biblio::_disambiguate
284
    $item_object->set($item);
284
    $item->{itemnumber} = $itemnumber;
285
    $item->{biblionumber} = $biblionumber;
286
    $item_object = $item_object->set_or_blank($item);
285
    my $unlinked_item_subfields = _get_unlinked_item_subfields( $localitemmarc, $frameworkcode );
287
    my $unlinked_item_subfields = _get_unlinked_item_subfields( $localitemmarc, $frameworkcode );
286
    $item_object->more_subfields_xml(_get_unlinked_subfields_xml($unlinked_item_subfields))->store;
288
    $item_object->more_subfields_xml(_get_unlinked_subfields_xml($unlinked_item_subfields))->store;
287
    $item_object->store;
289
    $item_object->store;
(-)a/Koha/Object.pm (+35 lines)
Lines 269-274 sub set { Link Here
269
    return $self->_result()->set_columns($properties) ? $self : undef;
269
    return $self->_result()->set_columns($properties) ? $self : undef;
270
}
270
}
271
271
272
=head3 $object->set_or_blank( $properties_hashref )
273
274
$object->set_or_blank(
275
    {
276
        property1 => $property1,
277
        property2 => $property2,
278
        property3 => $propery3,
279
    }
280
);
281
282
If not listed in $properties_hashref, the property will be set to the default
283
value defined at DB level, or nulled.
284
285
=cut
286
287
288
sub set_or_blank {
289
    my ( $self, $properties ) = @_;
290
291
    my $columns_info = $self->_result->result_source->columns_info;
292
293
    foreach my $col ( keys %{$columns_info} ) {
294
295
        next if exists $properties->{$col};
296
297
        if ( $columns_info->{$col}->{is_nullable} ) {
298
            $properties->{$col} = undef;
299
        } else {
300
            $properties->{$col} = $columns_info->{$col}->{default_value};
301
        }
302
    }
303
304
    return $self->set($properties);
305
}
306
272
=head3 $object->unblessed();
307
=head3 $object->unblessed();
273
308
274
Returns an unblessed representation of object.
309
Returns an unblessed representation of object.
(-)a/t/db_dependent/Items.t (-1 / +41 lines)
Lines 33-39 use Koha::AuthorisedValues; Link Here
33
use t::lib::Mocks;
33
use t::lib::Mocks;
34
use t::lib::TestBuilder;
34
use t::lib::TestBuilder;
35
35
36
use Test::More tests => 14;
36
use Test::More tests => 15;
37
37
38
use Test::Warn;
38
use Test::Warn;
39
39
Lines 975-977 subtest 'Split subfields in Item2Marc (Bug 21774)' => sub { Link Here
975
975
976
    $schema->storage->txn_rollback;
976
    $schema->storage->txn_rollback;
977
};
977
};
978
979
subtest 'ModItemFromMarc' => sub {
980
    plan tests => 2;
981
    $schema->storage->txn_begin;
982
983
    my $builder = t::lib::TestBuilder->new;
984
    my ($itemfield) = GetMarcFromKohaField( 'items.itemnumber' );
985
    my $itemtype = $builder->build_object({ class => 'Koha::ItemTypes' });
986
    my $biblio = $builder->build_sample_biblio;
987
    my ( $lost_tag, $lost_sf ) = GetMarcFromKohaField( 'items.itemlost' );
988
    my $item_record = new MARC::Record;
989
    $item_record->append_fields(
990
        MARC::Field->new(
991
            $itemfield, '', '',
992
            'y' => $itemtype->itemtype,
993
        ),
994
        MARC::Field->new(
995
            $itemfield, '', '',
996
            $lost_sf => '1',
997
        ),
998
    );
999
    my (undef, undef, $itemnumber) = AddItemFromMarc($item_record,
1000
        $biblio->biblionumber);
1001
1002
    my $item = Koha::Items->find($itemnumber);
1003
    is( $item->itemlost, 1, 'itemlost picked from the item marc');
1004
1005
    my $updated_item_record = new MARC::Record;
1006
    $updated_item_record->append_fields(
1007
        MARC::Field->new(
1008
            $itemfield, '', '',
1009
            'y' => $itemtype->itemtype,
1010
        )
1011
    );
1012
1013
    my $updated_item = ModItemFromMarc($updated_item_record, $biblio->biblionumber, $itemnumber);
1014
    is( $updated_item->{itemlost}, 0, 'itemlost should have been reset to the default value in DB' );
1015
1016
    $schema->storage->txn_rollback;
1017
};
(-)a/t/db_dependent/Koha/Object.t (-2 / +35 lines)
Lines 17-23 Link Here
17
17
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Test::More tests => 19;
20
use Test::More tests => 20;
21
use Test::Exception;
21
use Test::Exception;
22
use Test::Warn;
22
use Test::Warn;
23
use DateTime;
23
use DateTime;
Lines 818-820 subtest 'prefetch_whitelist() tests' => sub { Link Here
818
818
819
    $schema->storage->txn_rollback;
819
    $schema->storage->txn_rollback;
820
};
820
};
821
- 
821
822
subtest 'set_or_blank' => sub {
823
824
    plan tests => 5;
825
826
    $schema->storage->txn_begin;
827
828
    my $item = $builder->build_sample_item;
829
    my $item_info = $item->unblessed;
830
    $item = $item->set_or_blank($item_info);
831
    is_deeply($item->unblessed, $item_info, 'set_or_blank assign the correct value if unchanged');
832
833
    # int not null
834
    delete $item_info->{itemlost};
835
    $item = $item->set_or_blank($item_info);
836
    is($item->itemlost, 0, 'set_or_blank should have set itemlost to 0, default value defined in DB');
837
838
    # int nullable
839
    delete $item_info->{restricted};
840
    $item = $item->set_or_blank($item_info);
841
    is($item->restricted, undef, 'set_or_blank should have set restristed to null' );
842
843
    # datetime nullable
844
    delete $item_info->{dateaccessioned};
845
    $item = $item->set_or_blank($item_info);
846
    is($item->dateaccessioned, undef, 'set_or_blank should have set dateaccessioned to null');
847
848
    # timestamp not null
849
    delete $item_info->{timestamp};
850
    $item = $item->set_or_blank($item_info);
851
    isnt($item->timestamp, undef, 'set_or_blank should have set timestamp to a correct value');
852
853
    $schema->storage->txn_rollback;
854
};

Return to bug 23463