my $dbh=C4::Context->dbh;
my $error;
$item->{permanent_location} //= $item->{location};
+ _mod_item_dates( $item );
my $query =
"INSERT INTO items SET
biblionumber = ?,
my $query = "UPDATE items SET ";
my @bind;
+ _mod_item_dates( $item );
for my $key ( keys %$item ) {
next if ( $key eq 'itemnumber' );
$query.="$key=?,";
return ($item->{'itemnumber'},$error);
}
+sub _mod_item_dates { # date formatting for date fields in item hash
+ my ( $item ) = @_;
+ return if !$item || ref($item) ne 'HASH';
+
+ my @keys = grep
+ { $_ =~ /^onloan$|^date|date$|datetime$/ }
+ keys %$item;
+ # Incl. dateaccessioned,replacementpricedate,datelastborrowed,datelastseen
+ # NOTE: We do not (yet) have items fields ending with datetime
+ # Fields with _on$ have been handled already
+
+ foreach my $key ( @keys ) {
+ next if !defined $item->{$key}; # skip undefs
+ my $dt = eval { dt_from_string( $item->{$key} ) };
+ # eval: dt_from_string will die on us if we pass illegal dates
+
+ my $newstr;
+ if( defined $dt && ref($dt) eq 'DateTime' ) {
+ if( $key =~ /datetime/ ) {
+ $newstr = DateTime::Format::MySQL->format_datetime($dt);
+ } else {
+ $newstr = DateTime::Format::MySQL->format_date($dt);
+ }
+ }
+ $item->{$key} = $newstr; # might be undef to clear garbage
+ }
+}
+
=head2 _koha_delete_item
_koha_delete_item( $itemnum );
use t::lib::Mocks;
use t::lib::TestBuilder;
-use Test::More tests => 10;
+use Test::More tests => 11;
use Test::Warn;
$schema->storage->txn_rollback;
};
+subtest '_mod_item_dates' => sub {
+ plan tests => 11;
+
+ is( C4::Items::_mod_item_dates(), undef, 'Call without parameters' );
+ is( C4::Items::_mod_item_dates(1), undef, 'Call without hashref' );
+
+ my $orgitem;
+ my $item = {
+ itemcallnumber => 'V II 149 1963',
+ barcode => '109304',
+ };
+ $orgitem = { %$item };
+ C4::Items::_mod_item_dates($item);
+ is_deeply( $item, $orgitem, 'No dates passed to _mod_item_dates' );
+
+ # add two correct dates
+ t::lib::Mocks::mock_preference('dateformat', 'us');
+ $item->{dateaccessioned} = '01/31/2016';
+ $item->{onloan} = $item->{dateaccessioned};
+ $orgitem = { %$item };
+ C4::Items::_mod_item_dates($item);
+ is( $item->{dateaccessioned}, '2016-01-31', 'dateaccessioned is fine' );
+ is( $item->{onloan}, '2016-01-31', 'onloan is fine too' );
+
+
+ # add some invalid dates
+ $item->{notexistingcolumndate} = '13/1/2015'; # wrong format
+ $item->{anotherdate} = 'tralala'; # even worse
+ $item->{myzerodate} = '0000-00-00'; # wrong too
+ C4::Items::_mod_item_dates($item);
+ is( $item->{notexistingcolumndate}, undef, 'Invalid date became NULL' );
+ is( $item->{anotherdate}, undef, 'Second invalid date became NULL too' );
+ is( $item->{myzerodate}, undef, '0000-00-00 became NULL too' );
+
+ # check if itemlost_on was not touched
+ $item->{itemlost_on} = '12345678';
+ $item->{withdrawn_on} = '12/31/2015 23:59:00';
+ $orgitem = { %$item };
+ C4::Items::_mod_item_dates($item);
+ is_deeply( $item, $orgitem, 'Colums with _on are not touched' );
+
+ t::lib::Mocks::mock_preference('dateformat', 'metric');
+ $item->{dateaccessioned} = '01/31/2016'; #wrong
+ $item->{yetanotherdatetime} = '20/01/2016 13:58:00'; #okay
+ C4::Items::_mod_item_dates($item);
+ is( $item->{dateaccessioned}, undef, 'dateaccessioned wrong format' );
+ is( $item->{yetanotherdatetime}, '2016-01-20 13:58:00',
+ 'yetanotherdatetime is ok' );
+};
+
# Helper method to set up a Biblio.
sub get_biblio {
my ( $frameworkcode ) = @_;