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

(-)a/C4/Items.pm (+30 lines)
Lines 2085-2090 sub _koha_new_item { Link Here
2085
    my $dbh=C4::Context->dbh;  
2085
    my $dbh=C4::Context->dbh;  
2086
    my $error;
2086
    my $error;
2087
    $item->{permanent_location} //= $item->{location};
2087
    $item->{permanent_location} //= $item->{location};
2088
    _mod_item_dates( $item );
2088
    my $query =
2089
    my $query =
2089
           "INSERT INTO items SET
2090
           "INSERT INTO items SET
2090
            biblionumber        = ?,
2091
            biblionumber        = ?,
Lines 2348-2353 sub _koha_modify_item { Link Here
2348
2349
2349
    my $query = "UPDATE items SET ";
2350
    my $query = "UPDATE items SET ";
2350
    my @bind;
2351
    my @bind;
2352
    _mod_item_dates( $item );
2351
    for my $key ( keys %$item ) {
2353
    for my $key ( keys %$item ) {
2352
        next if ( $key eq 'itemnumber' );
2354
        next if ( $key eq 'itemnumber' );
2353
        $query.="$key=?,";
2355
        $query.="$key=?,";
Lines 2365-2370 sub _koha_modify_item { Link Here
2365
    return ($item->{'itemnumber'},$error);
2367
    return ($item->{'itemnumber'},$error);
2366
}
2368
}
2367
2369
2370
sub _mod_item_dates { # date formatting for date fields in item hash
2371
    my ( $item ) = @_;
2372
    return if !$item || ref($item) ne 'HASH';
2373
2374
    my @keys = grep
2375
        { $_ =~ /^onloan$|^date|date$|datetime$/ }
2376
        keys %$item;
2377
    # Incl. dateaccessioned,replacementpricedate,datelastborrowed,datelastseen
2378
    # NOTE: We do not (yet) have items fields ending with datetime
2379
    # Fields with _on$ have been handled already
2380
2381
    foreach my $key ( @keys ) {
2382
        next if !defined $item->{$key}; # skip undefs
2383
        my $dt = eval { dt_from_string( $item->{$key} ) };
2384
            # eval: dt_from_string will die on us if we pass illegal dates
2385
2386
        my $newstr;
2387
        if( defined $dt  && ref($dt) eq 'DateTime' ) {
2388
            if( $key =~ /datetime/ ) {
2389
                $newstr = DateTime::Format::MySQL->format_datetime($dt);
2390
            } else {
2391
                $newstr = DateTime::Format::MySQL->format_date($dt);
2392
            }
2393
        }
2394
        $item->{$key} = $newstr; # might be undef to clear garbage
2395
    }
2396
}
2397
2368
=head2 _koha_delete_item
2398
=head2 _koha_delete_item
2369
2399
2370
  _koha_delete_item( $itemnum );
2400
  _koha_delete_item( $itemnum );
(-)a/t/db_dependent/Items.t (-2 / +51 lines)
Lines 26-32 use Koha::Library; Link Here
26
use t::lib::Mocks;
26
use t::lib::Mocks;
27
use t::lib::TestBuilder;
27
use t::lib::TestBuilder;
28
28
29
use Test::More tests => 10;
29
use Test::More tests => 11;
30
30
31
use Test::Warn;
31
use Test::Warn;
32
32
Lines 666-671 subtest 'C4::Items::_build_default_values_for_mod_marc' => sub { Link Here
666
    $schema->storage->txn_rollback;
666
    $schema->storage->txn_rollback;
667
};
667
};
668
668
669
subtest '_mod_item_dates' => sub {
670
    plan tests => 11;
671
672
    is( C4::Items::_mod_item_dates(), undef, 'Call without parameters' );
673
    is( C4::Items::_mod_item_dates(1), undef, 'Call without hashref' );
674
675
    my $orgitem;
676
    my $item = {
677
        itemcallnumber  => 'V II 149 1963',
678
        barcode         => '109304',    
679
    };
680
    $orgitem = { %$item };
681
    C4::Items::_mod_item_dates($item);
682
    is_deeply( $item, $orgitem, 'No dates passed to _mod_item_dates' );
683
684
    # add two correct dates
685
    t::lib::Mocks::mock_preference('dateformat', 'us');
686
    $item->{dateaccessioned} = '01/31/2016';
687
    $item->{onloan} =  $item->{dateaccessioned};
688
    $orgitem = { %$item };
689
    C4::Items::_mod_item_dates($item);
690
    is( $item->{dateaccessioned}, '2016-01-31', 'dateaccessioned is fine' );
691
    is( $item->{onloan}, '2016-01-31', 'onloan is fine too' );
692
693
694
    # add some invalid dates
695
    $item->{notexistingcolumndate} = '13/1/2015'; # wrong format
696
    $item->{anotherdate} = 'tralala'; # even worse
697
    $item->{myzerodate} = '0000-00-00'; # wrong too
698
    C4::Items::_mod_item_dates($item);
699
    is( $item->{notexistingcolumndate}, undef, 'Invalid date became NULL' );
700
    is( $item->{anotherdate}, undef, 'Second invalid date became NULL too' );
701
    is( $item->{myzerodate}, undef, '0000-00-00 became NULL too' );
702
703
    # check if itemlost_on was not touched
704
    $item->{itemlost_on} = '12345678';
705
    $item->{withdrawn_on} = '12/31/2015 23:59:00';
706
    $orgitem = { %$item };
707
    C4::Items::_mod_item_dates($item);
708
    is_deeply( $item, $orgitem, 'Colums with _on are not touched' );
709
710
    t::lib::Mocks::mock_preference('dateformat', 'metric');
711
    $item->{dateaccessioned} = '01/31/2016'; #wrong
712
    $item->{yetanotherdatetime} = '20/01/2016 13:58:00'; #okay
713
    C4::Items::_mod_item_dates($item);
714
    is( $item->{dateaccessioned}, undef, 'dateaccessioned wrong format' );
715
    is( $item->{yetanotherdatetime}, '2016-01-20 13:58:00',
716
        'yetanotherdatetime is ok' );
717
};
718
669
# Helper method to set up a Biblio.
719
# Helper method to set up a Biblio.
670
sub get_biblio {
720
sub get_biblio {
671
    my ( $frameworkcode ) = @_;
721
    my ( $frameworkcode ) = @_;
672
- 

Return to bug 17512