Bug 19893: Increase test coverage
[srvgit] / Koha / ItemType.pm
index 48df808..d2a23b5 100644 (file)
@@ -22,6 +22,7 @@ use Carp;
 use C4::Koha;
 use C4::Languages;
 use Koha::Database;
+use Koha::IssuingRules;
 use Koha::Localizations;
 
 use base qw(Koha::Object);
@@ -36,6 +37,18 @@ Koha::ItemType - Koha Item type Object class
 
 =cut
 
+sub store {
+    my ($self) = @_;
+
+    $self->rentalcharge(undef)       if $self->rentalcharge eq '';
+    $self->defaultreplacecost(undef) if $self->defaultreplacecost eq '';
+    $self->processfee(undef)         if $self->processfee eq '';
+    $self->notforloan(0) unless $self->notforloan;
+    $self->hideinopac(0) unless $self->hideinopac;
+
+    return $self->SUPER::store;
+}
+
 =head3 image_location
 
 =cut
@@ -51,6 +64,14 @@ sub image_location {
 
 sub translated_description {
     my ( $self, $lang ) = @_;
+    if ( my $translated_description = eval { $self->get_column('translated_description') } ) {
+        # If the value has already been fetched (eg. from sarch_with_localization),
+        # do not search for it again
+        # Note: This is a bit hacky but should be fast
+        return $translated_description
+             ? $translated_description
+             : $self->description;
+    }
     $lang ||= C4::Languages::getlanguage;
     my $translated_description = Koha::Localizations->search({
         code => $self->itemtype,
@@ -81,6 +102,43 @@ sub translated_descriptions {
     } @translated_descriptions ];
 }
 
+
+
+=head3 can_be_deleted
+
+my $can_be_deleted = Koha::ItemType->can_be_deleted();
+
+Counts up the number of biblioitems and items with itemtype (code) and hands back the combined number of biblioitems and items with the itemtype
+
+=cut
+
+sub can_be_deleted {
+    my ($self) = @_;
+    my $nb_items = Koha::Items->search( { itype => $self->itemtype } )->count;
+    my $nb_biblioitems = Koha::Biblioitems->search( { itemtype => $self->itemtype } )->count;
+    return $nb_items + $nb_biblioitems == 0 ? 1 : 0;
+}
+
+=head3 may_article_request
+
+    Returns true if it is likely possible to make an article request for
+    this item type.
+    Optional parameter: categorycode (for patron).
+
+=cut
+
+sub may_article_request {
+    my ( $self, $params ) = @_;
+    return q{} if !C4::Context->preference('ArticleRequests');
+    my $itemtype = $self->itemtype;
+    my $category = $params->{categorycode};
+
+    my $guess = Koha::IssuingRules->guess_article_requestable_itemtypes({
+        $category ? ( categorycode => $category ) : (),
+    });
+    return ( $guess->{ $itemtype // q{} } || $guess->{ '*' } ) ? 1 : q{};
+}
+
 =head3 type
 
 =cut