From 29c63e70360777fadfc3cf26ed7360f7f5770bae Mon Sep 17 00:00:00 2001
From: Tomas Cohen Arazi <tomascohen@theke.io>
Date: Mon, 3 Feb 2020 15:27:25 -0300
Subject: [PATCH] Bug 24857: Koha::Biblio::Volume methods

This patch introduces the following methods:
- add_item
- del_item
- items
- to_api_mapping
- store (overloaded)

Signed-off-by: Andrew Fuerste-Henry <andrew@bywatersolutions.com>

Signed-off-by: Rebecca  Coert <rcoert@arlingtonva.us>
---
 Koha/Biblio/Volume.pm | 94 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 94 insertions(+)

diff --git a/Koha/Biblio/Volume.pm b/Koha/Biblio/Volume.pm
index fa71112a55..3f3d3efc4e 100644
--- a/Koha/Biblio/Volume.pm
+++ b/Koha/Biblio/Volume.pm
@@ -19,6 +19,10 @@ use Modern::Perl;
 
 use base qw(Koha::Object);
 
+use Koha::Biblio::Volume::Items;
+use Koha::Exceptions::Object;
+use Koha::Items;
+
 =head1 NAME
 
 Koha::Volume - Koha Volume Object class
@@ -27,8 +31,98 @@ Koha::Volume - Koha Volume Object class
 
 =head2 Class methods
 
+=head3 store
+
+    $volume->store;
+
+Overloaded I<store> method that takes care of creation date handling.
+
+=cut
+
+sub store {
+    my ($self) = @_;
+
+    unless ( $self->in_storage ) {
+        # new entry
+        $self->set(
+            {
+                created_on => \'NOW()'
+            }
+        );
+    }
+
+    return $self->SUPER::store();
+}
+
+=head3 items
+
+    my $items = $volume->items;
+
+Returns all the items linked to the volume.
+
+=cut
+
+sub items {
+    my ($self) = @_;
+
+    my $items_rs = $self->_result->volume_items;
+    my @item_numbers = $items_rs->get_column('itemnumber')->all;
+
+    return unless @item_numbers;
+
+    return Koha::Items->search(
+        {
+            itemnumber => {
+                -in => \@item_numbers
+            }
+        }
+    );
+}
+
+=head3 add_item
+
+    $volume->add_item({ item_id => $item_id });
+
 =cut
 
+sub add_item {
+    my ($self, $params) = @_;
+
+    my $item_id = $params->{item_id};
+
+    my $item = Koha::Items->find( $item_id );
+    unless ( $item->biblionumber == $self->biblionumber ) {
+        Koha::Exceptions::Object::FKConstraint->throw(
+            broken_fk => 'biblionumber'
+        );
+    }
+
+    Koha::Biblio::Volume::Item->new(
+        {
+            itemnumber => $item_id,
+            volume_id  => $self->id
+        }
+    )->store;
+
+    return $self;
+}
+
+=head3 to_api_mapping
+
+This method returns the mapping for representing a Koha::Biblio::Volume object
+on the API.
+
+=cut
+
+sub to_api_mapping {
+    return {
+        id           => 'volume_id',
+        biblionumber => 'biblio_id',
+        created_on   => 'creation_date',
+        updated_on   => 'modification_date'
+    };
+}
+
 =head2 Internal methods
 
 =head3 _type
-- 
2.24.1 (Apple Git-126)