From 9cba6a905179c4666d8d534c5d4349d319540cca Mon Sep 17 00:00:00 2001 From: Olli-Antti Kivilahti Date: Fri, 13 Feb 2015 17:02:22 +0200 Subject: [PATCH] Bug 13707 - Adding a new column datereceived for items and biblioitems. Value accessors. --- C4/Biblio.pm | 90 ++++++++++++++++++++++++++++++++++++++++++++++++ C4/Items.pm | 2 ++ acqui/finishreceive.pl | 2 ++ cataloguing/additem.pl | 17 +++++++++ 4 files changed, 111 insertions(+) diff --git a/C4/Biblio.pm b/C4/Biblio.pm index 8b75a59..e229546 100644 --- a/C4/Biblio.pm +++ b/C4/Biblio.pm @@ -97,6 +97,7 @@ BEGIN { &TransformKohaToMarc &PrepHostMarcField + &UpdateDatereceived &CountItemsIssued &CountBiblioInOrders &GetSubscriptionsId @@ -2210,6 +2211,95 @@ sub GetFrameworkCode { return $frameworkcode; } +=head UpdateDatereceived + + my $error = C4::Biblio::UpdateDatereceived($bibliodataOrBiblionumber, $datereceived, $record); + my $error = C4::Biblio::UpdateDatereceived({biblionumber => 1213, datereceived => '2014-12-10 10:12:33', ...}, DateTime, MARC::Record); + +Updates the biblioitems.datereceived and the corresponding MARC-Subfield if the datereceived +hasn't been set for this Biblio yet. Datereceived for the Biblio is the first moment +the Biblio became available for the library and shouldn't be updated for subsequent receivals. + +@PARAM1 MANDATORY + Integer, koha.biblio.biblionumber + OR + Hash, Like returned by C4::Biblio::GetBiblioData(). + biblionumber must be a hash key! + datereceived must be present as well, otherwise the koha.biblioitems.datereceived + will be overwritten, unless this is intended. +@PARAM2 OPTIONAL + DateTime-object, of the moment of receival. + Defaults to Now() if not given. +@PARAM3 OPTIONAL + MARC::Record, of the biblio to be updated. + By default is fetched using the supplied biblionumber. +@RETURNS String, error code: + 'NO_BIBLIODATA', couldn't find the koha.biblioitems-row + 'NO_BIBLIONUMBER', no biblionumber given as parameter or couldn't + find it from the Hash + 'NOT_DATETIME', $datereceived is not a DateTime-object + 'NO_RECORD', The given MARC::Record is invalid, or no MARC Record + could be found using the supplied biblionumber. + 'MODBIBLIO_ERROR', C4::Biblio::ModBiblio() failed. + +=cut +sub UpdateDatereceived { + my ($biblionumberOrBibliodata, $datereceived, $record) = @_; + + my $bibdata; + unless (ref $biblionumberOrBibliodata) { #We have a SCALAR, eg. a biblionumber. + my @biblioitems = C4::Biblio::GetBiblioItemByBiblioNumber($biblionumberOrBibliodata); + $bibdata = $biblioitems[0]; + } + else { + $bibdata = $biblionumberOrBibliodata; + } + return 'NO_BIBLIODATA' if (not($bibdata)); + return 'NO_BIBLIONUMBER' if (not($bibdata->{biblionumber})); + + if ($datereceived && ref $datereceived ne 'DateTime') { + return 'NOT_DATETIME'; + } + #Use the given DateTime or Use Now() + $datereceived = ($datereceived) ? $datereceived->iso8601() : DateTime->now( time_zone => C4::Context->tz() )->iso8601(); + + #Make sure to only update the datereceived for the Biblio if it hasn't been set yet. + if ($bibdata->{datereceived}) { + return undef; #All is OK and we will preserve the first moment of datereceived. + } + + if (not($record)) { + $record = C4::Biblio::GetMarcBiblio($bibdata->{biblionumber}); + } + elsif (ref($record) ne 'MARC::Record') { + return 'NO_RECORD'; + } + return 'NO_RECORD' unless $record; + + my $frameworkcode = ($bibdata->{frameworkcode}) ? $bibdata->{frameworkcode} : C4::Biblio::GetFrameworkCode($bibdata->{biblionumber}); + + #Get the mapped MARC-fields for items.datereceived + my ( $datereceivedFieldCode, $datereceivedSubfieldCode ) = + C4::Biblio::GetMarcFromKohaField( "items.datereceived", $frameworkcode ); + ( $datereceivedFieldCode, $datereceivedSubfieldCode ) = + C4::Biblio::GetMarcFromKohaField( "items.datereceived", '' ) unless ($datereceivedFieldCode); + + #UPSERT the datereceived DB column to MARC + my @existingFields = $record->field($datereceivedFieldCode); + if ($existingFields[0]) { + $existingFields[0]->update($datereceivedSubfieldCode => $datereceived); + } + else { + my $newField = MARC::Field->new($datereceivedFieldCode, '', '', $datereceivedSubfieldCode => $datereceived); + $record->insert_fields_ordered($newField); + } + + my $ok = C4::Biblio::ModBiblio($record, $bibdata->{biblionumber}, $frameworkcode); + return 'MODBIBLIO_ERROR' unless $ok; + return undef; #All is OK! +} + + =head2 TransformKohaToMarc $record = TransformKohaToMarc( $hash ) diff --git a/C4/Items.pm b/C4/Items.pm index 26f5200..76f0bd0 100644 --- a/C4/Items.pm +++ b/C4/Items.pm @@ -2133,6 +2133,7 @@ sub _koha_new_item { biblioitemnumber = ?, barcode = ?, dateaccessioned = ?, + datereceived = ?, booksellerid = ?, homebranch = ?, price = ?, @@ -2175,6 +2176,7 @@ sub _koha_new_item { $item->{'biblioitemnumber'}, $barcode, $item->{'dateaccessioned'}, + $item->{'datereceived'}, $item->{'booksellerid'}, $item->{'homebranch'}, $item->{'price'}, diff --git a/acqui/finishreceive.pl b/acqui/finishreceive.pl index 286474f..ab89bfa 100755 --- a/acqui/finishreceive.pl +++ b/acqui/finishreceive.pl @@ -165,9 +165,11 @@ ModItem( price => $unitprice, replacementprice => $rrp, replacementpricedate => $datereceived, + datereceived => DateTime->now( time_zone => C4::Context->tz() )->iso8601(), }, $biblionumber, $_ ) foreach GetItemnumbersFromOrder($new_ordernumber); +C4::Biblio::UpdateDatereceived($biblionumber); print $input->redirect("/cgi-bin/koha/acqui/parcel.pl?invoiceid=$invoiceid&sticky_filters=1"); diff --git a/cataloguing/additem.pl b/cataloguing/additem.pl index 9c7fb3f..14916a3 100755 --- a/cataloguing/additem.pl +++ b/cataloguing/additem.pl @@ -824,6 +824,13 @@ if($itemrecord){ } # and now we add fields that are empty +##Populate datereceived for new Items present in our library. +#Get the mapped MARC-fields for items.datereceived +my ( $datereceivedFieldCode, $datereceivedSubfieldCode ) = + C4::Biblio::GetMarcFromKohaField( "items.datereceived", $frameworkcode ); +( $datereceivedFieldCode, $datereceivedSubfieldCode ) = + C4::Biblio::GetMarcFromKohaField( "items.datereceived", '' ) unless ($datereceivedFieldCode); + # Using last created item if it exists $itemrecord = $cookieitemrecord if ($prefillitem and not $justaddeditem and $op ne "edititem"); @@ -838,6 +845,7 @@ foreach my $tag ( keys %{$tagslib}){ my @values = (undef); @values = $itemrecord->field($tag)->subfield($subtag) if ($itemrecord && defined($itemrecord->field($tag)) && defined($itemrecord->field($tag)->subfield($subtag))); for my $value (@values){ + $value = enforceDatereceived($tag, $subtag, $value); my $subfield_data = generate_subfield_form($tag, $subtag, $value, $tagslib, $tagslib->{$tag}->{$subtag}, $branches, $today_iso, $biblionumber, $temp, \@loop_data, $i, $restrictededition); push (@loop_data, $subfield_data); $i++; @@ -881,3 +889,12 @@ foreach my $error (@errors) { $template->param($error => 1); } output_html_with_http_headers $input, $cookie, $template->output; + +sub enforceDatereceived { + my ($tag, $subtag, $value) = @_; + #Set the datereceived as now() if it is not defined, or it's not a timestamp. + if ($tag eq $datereceivedFieldCode && $subtag eq $datereceivedSubfieldCode && (not($value) || not($value =~ /^\d\d\d\d-\d\d-\d\d/))) { + $value = DateTime->now( time_zone => C4::Context->tz() )->iso8601(); + } + return $value; +} \ No newline at end of file -- 1.7.9.5