use Modern::Perl;
use Carp;
+use List::MoreUtils qw(any);
-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);
-use C4::Circulation qw(GetIssuingRule);
-use Koha::Items;
-use Koha::Biblioitems;
-use Koha::ArticleRequests;
use Koha::ArticleRequest::Status;
+use Koha::ArticleRequests;
+use Koha::Biblio::Metadatas;
+use Koha::Biblioitems;
+use Koha::IssuingRules;
+use Koha::Item::Transfer::Limits;
+use Koha::Items;
+use Koha::Libraries;
+use Koha::Subscriptions;
=head1 NAME
=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 metadata
+
+my $metadata = $biblio->metadata();
+
+Returns a Koha::Biblio::Metadata object
+
+=cut
+
+sub metadata {
+ my ( $self ) = @_;
+
+ my $metadata = $self->_result->metadata;
+ return Koha::Biblio::Metadata->_new_from_dbic($metadata);
+}
+
=head3 subtitles
my @subtitles = $biblio->subtitles();
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
return q{};
}
+=head3 can_be_transferred
+
+$biblio->can_be_transferred({ to => $to_library, from => $from_library })
+
+Checks if at least one item of a biblio can be transferred to given library.
+
+This feature is controlled by two system preferences:
+UseBranchTransferLimits to enable / disable the feature
+BranchTransferLimitsType to use either an itemnumber or ccode as an identifier
+ for setting the limitations
+
+Performance-wise, it is recommended to use this method for a biblio instead of
+iterating each item of a biblio with Koha::Item->can_be_transferred().
+
+Takes HASHref that can have the following parameters:
+ MANDATORY PARAMETERS:
+ $to : Koha::Library
+ OPTIONAL PARAMETERS:
+ $from : Koha::Library # if given, only items from that
+ # holdingbranch are considered
+
+Returns 1 if at least one of the item of a biblio can be transferred
+to $to_library, otherwise 0.
+
+=cut
+
+sub can_be_transferred {
+ my ($self, $params) = @_;
+
+ my $to = $params->{to};
+ my $from = $params->{from};
+
+ return 1 unless C4::Context->preference('UseBranchTransferLimits');
+ my $limittype = C4::Context->preference('BranchTransferLimitsType');
+
+ my $items;
+ foreach my $item_of_bib ($self->items->as_list) {
+ next unless $item_of_bib->holdingbranch;
+ next if $from && $from->branchcode ne $item_of_bib->holdingbranch;
+ return 1 if $item_of_bib->holdingbranch eq $to->branchcode;
+ my $code = $limittype eq 'itemtype'
+ ? $item_of_bib->effective_itemtype
+ : $item_of_bib->ccode;
+ return 1 unless $code;
+ $items->{$code}->{$item_of_bib->holdingbranch} = 1;
+ }
+
+ # At this point we will have a HASHref containing each itemtype/ccode that
+ # this biblio has, inside which are all of the holdingbranches where those
+ # items are located at. Then, we will query Koha::Item::Transfer::Limits to
+ # find out whether a transfer limits for such $limittype from any of the
+ # listed holdingbranches to the given $to library exist. If at least one
+ # holdingbranch for that $limittype does not have a transfer limit to given
+ # $to library, then we know that the transfer is possible.
+ foreach my $code (keys %{$items}) {
+ my @holdingbranches = keys %{$items->{$code}};
+ return 1 if Koha::Item::Transfer::Limits->search({
+ toBranch => $to->branchcode,
+ fromBranch => { 'in' => \@holdingbranches },
+ $limittype => $code
+ }, {
+ group_by => [qw/fromBranch/]
+ })->count == scalar(@holdingbranches) ? 0 : 1;
+ }
+
+ return 0;
+}
+
+=head3 hidden_in_opac
+
+my $bool = $biblio->hidden_in_opac({ [ rules => $rules ] })
+
+Returns true if the biblio matches the hidding criteria defined in $rules.
+Returns false otherwise.
+
+Takes HASHref that can have the following parameters:
+ OPTIONAL PARAMETERS:
+ $rules : { <field> => [ value_1, ... ], ... }
+
+Note: $rules inherits its structure from the parsed YAML from reading
+the I<OpacHiddenItems> system preference.
+
+=cut
+
+sub hidden_in_opac {
+ my ( $self, $params ) = @_;
+
+ my $rules = $params->{rules} // {};
+
+ return !(any { !$_->hidden_in_opac({ rules => $rules }) } $self->items->as_list);
+}
+
=head3 article_request_type
my $type = $biblio->article_request_type( $borrower );
my $borrowertype = $borrower->categorycode;
my $itemtype = $self->itemtype();
- my $rules = C4::Circulation::GetIssuingRule( $borrowertype, $itemtype );
+ my $issuing_rule = Koha::IssuingRules->get_effective_issuing_rule({ categorycode => $borrowertype, itemtype => $itemtype });
- return $rules->{article_requests} || q{};
+ return q{} unless $issuing_rule;
+ return $issuing_rule->article_requests || q{}
}
=head3 article_request_type_for_items
=head3 items
-my @items = $biblio->items();
my $items = $biblio->items();
-Returns the related Koha::Items object for this biblio in scalar context,
-or list of Koha::Item objects in list context.
+Returns the related Koha::Items object for this biblio
=cut
sub items {
my ($self) = @_;
- $self->{_items} ||= Koha::Items->search( { biblionumber => $self->biblionumber() } );
+ my $items_rs = $self->_result->items;
- return wantarray ? $self->{_items}->as_list : $self->{_items};
+ return Koha::Items->_new_from_dbic( $items_rs );
}
=head3 itemtype
sub itemtype {
my ( $self ) = @_;
- return $self->_biblioitem()->itemtype();
+ return $self->biblioitem()->itemtype();
+}
+
+=head3 holds
+
+my $holds = $biblio->holds();
+
+return the current holds placed on this record
+
+=cut
+
+sub holds {
+ 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 _biblioitem
+=head3 current_holds
+
+my $holds = $biblio->current_holds
-my $field = $self->_biblioitem()->itemtype
+Return the holds placed on this bibliographic record.
+It does not include future holds.
+
+=cut
+
+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
+
+my $field = $self->biblioitem()->itemtype
Returns the related Koha::Biblioitem object for this Biblio object
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->as_list ) {
+ return 1 if $item->get_transfer;
+ }
+
+ return 0;
+}
+
=head3 type
=cut