Bug 19926: Add the Koha::Object->unblessed_all_relateds method
[koha-ffzg.git] / Koha / Biblio.pm
index d5ca370..d226304 100644 (file)
@@ -21,9 +21,10 @@ use Modern::Perl;
 
 use Carp;
 
-use C4::Biblio qw( GetRecordValue GetMarcBiblio GetFrameworkCode );
+use C4::Biblio qw();
 
 use Koha::Database;
+use Koha::DateUtils qw( dt_from_string );
 
 use base qw(Koha::Object);
 
@@ -32,6 +33,7 @@ use Koha::Biblioitems;
 use Koha::ArticleRequests;
 use Koha::ArticleRequest::Status;
 use Koha::IssuingRules;
+use Koha::Subscriptions;
 
 =head1 NAME
 
@@ -43,6 +45,20 @@ Koha::Biblio - Koha Biblio Object class
 
 =cut
 
+=head3 store
+
+Overloaded I<store> method to set default values
+
+=cut
+
+sub store {
+    my ( $self ) = @_;
+
+    $self->datecreated( dt_from_string ) unless $self->datecreated;
+
+    return $self->SUPER::store;
+}
+
 =head3 subtitles
 
 my @subtitles = $biblio->subtitles();
@@ -56,7 +72,11 @@ Keyword to MARC mapping for subtitle must be set for this method to return any p
 sub subtitles {
     my ( $self ) = @_;
 
-    return map { $_->{subfield} } @{ GetRecordValue( 'subtitle', GetMarcBiblio( $self->id ), $self->frameworkcode ) };
+    return map { $_->{subfield} } @{
+        C4::Biblio::GetRecordValue(
+            'subtitle',
+            C4::Biblio::GetMarcBiblio({ biblionumber => $self->id }),
+            $self->frameworkcode ) };
 }
 
 =head3 can_article_request
@@ -258,10 +278,26 @@ return the current holds placed on this record
 =cut
 
 sub holds {
-    my ( $self ) = @_;
+    my ( $self, $params, $attributes ) = @_;
+    $attributes->{order_by} = 'priority' unless exists $attributes->{order_by};
+    my $hold_rs = $self->_result->reserves->search( $params, $attributes );
+    return Koha::Holds->_new_from_dbic($hold_rs);
+}
+
+=head3 current_holds
+
+my $holds = $biblio->current_holds
+
+Return the holds placed on this bibliographic record.
+It does not include future holds.
+
+=cut
 
-    my $holds_rs = $self->_result->reserves;
-    return Koha::Holds->_new_from_dbic( $holds_rs );
+sub current_holds {
+    my ($self) = @_;
+    my $dtf = Koha::Database->new->schema->storage->datetime_parser;
+    return $self->holds(
+        { reservedate => { '<=' => $dtf->format_date(dt_from_string) } } );
 }
 
 =head3 biblioitem
@@ -280,6 +316,45 @@ sub biblioitem {
     return $self->{_biblioitem};
 }
 
+=head3 subscriptions
+
+my $subscriptions = $self->subscriptions
+
+Returns the related Koha::Subscriptions object for this Biblio object
+
+=cut
+
+sub subscriptions {
+    my ($self) = @_;
+
+    $self->{_subscriptions} ||= Koha::Subscriptions->search( { biblionumber => $self->biblionumber } );
+
+    return $self->{_subscriptions};
+}
+
+=head3 has_items_waiting_or_intransit
+
+my $itemsWaitingOrInTransit = $biblio->has_items_waiting_or_intransit
+
+Tells if this bibliographic record has items waiting or in transit.
+
+=cut
+
+sub has_items_waiting_or_intransit {
+    my ( $self ) = @_;
+
+    if ( Koha::Holds->search({ biblionumber => $self->id,
+                               found => ['W', 'T'] })->count ) {
+        return 1;
+    }
+
+    foreach my $item ( $self->items ) {
+        return 1 if $item->get_transfer;
+    }
+
+    return 0;
+}
+
 =head3 type
 
 =cut