Bug 21613: (QA follow-up) Resolve warning on $ENV{_} - avoid autovivification
[koha.git] / Koha / Item.pm
index 0199f37..f3799ef 100644 (file)
@@ -22,9 +22,17 @@ use Modern::Perl;
 use Carp;
 
 use Koha::Database;
-
-use Koha::Borrowers;
+use Koha::DateUtils qw( dt_from_string );
+
+use C4::Context;
+use Koha::Checkouts;
+use Koha::IssuingRules;
+use Koha::Item::Transfer::Limits;
+use Koha::Item::Transfers;
+use Koha::Patrons;
 use Koha::Libraries;
+use Koha::StockRotationItem;
+use Koha::StockRotationRotas;
 
 use base qw(Koha::Object);
 
@@ -34,7 +42,7 @@ Koha::Item - Koha Item object class
 
 =head1 API
 
-=head2 Class Methods
+=head2 Class methods
 
 =cut
 
@@ -74,11 +82,69 @@ sub holding_branch {
     return $self->{_holding_branch};
 }
 
+=head3 biblio
+
+my $biblio = $item->biblio;
+
+Return the bibliographic record of this item
+
+=cut
+
+sub biblio {
+    my ( $self ) = @_;
+    my $biblio_rs = $self->_result->biblio;
+    return Koha::Biblio->_new_from_dbic( $biblio_rs );
+}
+
+=head3 biblioitem
+
+my $biblioitem = $item->biblioitem;
+
+Return the biblioitem record of this item
+
+=cut
+
+sub biblioitem {
+    my ( $self ) = @_;
+    my $biblioitem_rs = $self->_result->biblioitem;
+    return Koha::Biblioitem->_new_from_dbic( $biblioitem_rs );
+}
+
+=head3 checkout
+
+my $checkout = $item->checkout;
+
+Return the checkout for this item
+
+=cut
+
+sub checkout {
+    my ( $self ) = @_;
+    my $checkout_rs = $self->_result->issue;
+    return unless $checkout_rs;
+    return Koha::Checkout->_new_from_dbic( $checkout_rs );
+}
+
+=head3 get_transfer
+
+my $transfer = $item->get_transfer;
+
+Return the transfer if the item is in transit or undef
+
+=cut
+
+sub get_transfer {
+    my ( $self ) = @_;
+    my $transfer_rs = $self->_result->branchtransfers->search({ datearrived => undef })->first;
+    return unless $transfer_rs;
+    return Koha::Item::Transfer->_new_from_dbic( $transfer_rs );
+}
+
 =head3 last_returned_by
 
 Gets and sets the last borrower to return an item.
 
-Accepts and returns Koha::Borrower objects
+Accepts and returns Koha::Patron objects
 
 $item->last_returned_by( $borrowernumber );
 
@@ -99,7 +165,7 @@ sub last_returned_by {
         unless ( $self->{_last_returned_by} ) {
             my $result = $items_last_returned_by_rs->single( { itemnumber => $self->id } );
             if ($result) {
-                $self->{_last_returned_by} = Koha::Borrowers->find( $result->get_column('borrowernumber') );
+                $self->{_last_returned_by} = Koha::Patrons->find( $result->get_column('borrowernumber') );
             }
         }
 
@@ -107,7 +173,151 @@ sub last_returned_by {
     }
 }
 
-=head3 type
+=head3 can_article_request
+
+my $bool = $item->can_article_request( $borrower )
+
+Returns true if item can be specifically requested
+
+$borrower must be a Koha::Patron object
+
+=cut
+
+sub can_article_request {
+    my ( $self, $borrower ) = @_;
+
+    my $rule = $self->article_request_type($borrower);
+
+    return 1 if $rule && $rule ne 'no' && $rule ne 'bib_only';
+    return q{};
+}
+
+=head3 can_be_transferred
+
+$item->can_be_transferred({ to => $to_library, from => $from_library })
+Checks if an item 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
+
+Takes HASHref that can have the following parameters:
+    MANDATORY PARAMETERS:
+    $to   : Koha::Library
+    OPTIONAL PARAMETERS:
+    $from : Koha::Library  # if not given, item holdingbranch
+                           # will be used instead
+
+Returns 1 if item can be transferred to $to_library, otherwise 0.
+
+To find out whether at least one item of a Koha::Biblio can be transferred, please
+see Koha::Biblio->can_be_transferred() instead of using this method for
+multiple items of the same biblio.
+
+=cut
+
+sub can_be_transferred {
+    my ($self, $params) = @_;
+
+    my $to   = $params->{to};
+    my $from = $params->{from};
+
+    $to   = $to->branchcode;
+    $from = defined $from ? $from->branchcode : $self->holdingbranch;
+
+    return 1 if $from eq $to; # Transfer to current branch is allowed
+    return 1 unless C4::Context->preference('UseBranchTransferLimits');
+
+    my $limittype = C4::Context->preference('BranchTransferLimitsType');
+    return Koha::Item::Transfer::Limits->search({
+        toBranch => $to,
+        fromBranch => $from,
+        $limittype => $limittype eq 'itemtype'
+                        ? $self->effective_itemtype : $self->ccode
+    })->count ? 0 : 1;
+}
+
+=head3 article_request_type
+
+my $type = $item->article_request_type( $borrower )
+
+returns 'yes', 'no', 'bib_only', or 'item_only'
+
+$borrower must be a Koha::Patron object
+
+=cut
+
+sub article_request_type {
+    my ( $self, $borrower ) = @_;
+
+    my $branch_control = C4::Context->preference('HomeOrHoldingBranch');
+    my $branchcode =
+        $branch_control eq 'homebranch'    ? $self->homebranch
+      : $branch_control eq 'holdingbranch' ? $self->holdingbranch
+      :                                      undef;
+    my $borrowertype = $borrower->categorycode;
+    my $itemtype = $self->effective_itemtype();
+    my $issuing_rule = Koha::IssuingRules->get_effective_issuing_rule({ categorycode => $borrowertype, itemtype => $itemtype, branchcode => $branchcode });
+
+    return q{} unless $issuing_rule;
+    return $issuing_rule->article_requests || q{}
+}
+
+=head3 current_holds
+
+=cut
+
+sub current_holds {
+    my ( $self ) = @_;
+    my $attributes = { order_by => 'priority' };
+    my $dtf = Koha::Database->new->schema->storage->datetime_parser;
+    my $params = {
+        itemnumber => $self->itemnumber,
+        suspend => 0,
+        -or => [
+            reservedate => { '<=' => $dtf->format_date(dt_from_string) },
+            waitingdate => { '!=' => undef },
+        ],
+    };
+    my $hold_rs = $self->_result->reserves->search( $params, $attributes );
+    return Koha::Holds->_new_from_dbic($hold_rs);
+}
+
+=head3 stockrotationitem
+
+  my $sritem = Koha::Item->stockrotationitem;
+
+Returns the stock rotation item associated with the current item.
+
+=cut
+
+sub stockrotationitem {
+    my ( $self ) = @_;
+    my $rs = $self->_result->stockrotationitem;
+    return 0 if !$rs;
+    return Koha::StockRotationItem->_new_from_dbic( $rs );
+}
+
+=head3 add_to_rota
+
+  my $item = $item->add_to_rota($rota_id);
+
+Add this item to the rota identified by $ROTA_ID, which means associating it
+with the first stage of that rota.  Should this item already be associated
+with a rota, then we will move it to the new rota.
+
+=cut
+
+sub add_to_rota {
+    my ( $self, $rota_id ) = @_;
+    Koha::StockRotationRotas->find($rota_id)->add_item($self->itemnumber);
+    return $self;
+}
+
+=head2 Internal methods
+
+=head3 _type
 
 =cut