From 5d8ae2e6c8fd6f0de86f13ed444aec6fe69d9147 Mon Sep 17 00:00:00 2001
From: Tomas Cohen Arazi <tomascohen@theke.io>
Date: Mon, 30 Dec 2019 15:36:57 -0300
Subject: [PATCH] Bug 18731: Add API mappings to K::A::{Basket,Invoice}

This patch adds to_api_mapping definitions to the following classes:

- Koha::Acquisition::Basket
- Koha::Acquisition::Invoice

They are implemented following the proposed RFCs:
https://wiki.koha-community.org/wiki/Acquisitions_baskets_endpoint_RFC
https://wiki.koha-community.org/wiki/Acquisitions_invoices_endpoint_RFC

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
---
 Koha/Acquisition/Basket.pm     | 47 ++++++++++++++++++++++++++++++++++
 Koha/Acquisition/Invoice.pm    | 43 +++++++++++++++++++++++++++++++
 Koha/Schema/Result/Aqbasket.pm |  4 +++
 3 files changed, 94 insertions(+)

diff --git a/Koha/Acquisition/Basket.pm b/Koha/Acquisition/Basket.pm
index 5bda3ab303..cdb885b04f 100644
--- a/Koha/Acquisition/Basket.pm
+++ b/Koha/Acquisition/Basket.pm
@@ -71,6 +71,53 @@ sub effective_create_items {
     return $self->create_items || C4::Context->preference('AcqCreateItem');
 }
 
+=head3 to_api
+
+    my $json = $basket->to_api;
+
+Overloaded method that returns a JSON representation of the Koha::Acquisition::Basket object,
+suitable for API output.
+
+=cut
+
+sub to_api {
+    my ( $self ) = @_;
+
+    my $json = $self->SUPER::to_api;
+
+    $json->{closed} = ( $self->closedate )
+                                    ? Mojo::JSON->true
+                                    : Mojo::JSON->false;
+
+    return $json;
+}
+
+=head3 to_api_mapping
+
+This method returns the mapping for representing a Koha::Acquisition::Basket object
+on the API.
+
+=cut
+
+sub to_api_mapping {
+    return {
+        basketno                => 'basket_id',
+        basketname              => 'name',
+        booksellernote          => 'vendor_note',
+        contractnumber          => 'contract_id',
+        creationdate            => 'creation_date',
+        closedate               => 'close_date',
+        booksellerid            => 'vendor_id',
+        authorisedby            => 'authorised_by',
+        booksellerinvoicenumber => undef,
+        basketgroupid           => 'basket_group_id',
+        deliveryplace           => 'delivery_place',
+        billingplace            => 'billing_place',
+        branch                  => 'library_id',
+        is_standing             => 'standing'
+    };
+}
+
 =head2 Internal methods
 
 =head3 _type
diff --git a/Koha/Acquisition/Invoice.pm b/Koha/Acquisition/Invoice.pm
index 6117322226..2d35d92d33 100644
--- a/Koha/Acquisition/Invoice.pm
+++ b/Koha/Acquisition/Invoice.pm
@@ -27,6 +27,49 @@ Koha::Acquisition::Invoice object class
 
 =head1 API
 
+
+=head3 to_api
+
+    my $json = $invoice->to_api;
+
+Overloaded method that returns a JSON representation of the Koha::Acquisition::Invoice object,
+suitable for API output.
+
+=cut
+
+sub to_api {
+    my ( $self ) = @_;
+
+    my $json = $self->SUPER::to_api;
+
+    $json->{closed} = ( $self->closedate )
+                                    ? Mojo::JSON->true
+                                    : Mojo::JSON->false;
+
+    return $json;
+}
+
+=head3 to_api_mapping
+
+This method returns the mapping for representing a Koha::Acquisition::Invoice object
+on the API.
+
+=cut
+
+sub to_api_mapping {
+    return {
+        invoiceid             => 'invoice_id',
+        invoicenumber         => 'invoice_number',
+        booksellerid          => 'vendor_id',
+        shipmentdate          => 'shipping_date',
+        billingdate           => 'invoice_date',
+        closedate             => 'close_date',
+        shipmentcost          => 'shipping_cost',
+        shipmentcost_budgetid => 'shipping_cost_budget_id',
+        message_id            => undef
+    };
+}
+
 =head2 Internal methods
 
 =head3 _type
diff --git a/Koha/Schema/Result/Aqbasket.pm b/Koha/Schema/Result/Aqbasket.pm
index 220d91eeef..efbd6a1d36 100644
--- a/Koha/Schema/Result/Aqbasket.pm
+++ b/Koha/Schema/Result/Aqbasket.pm
@@ -335,4 +335,8 @@ sub koha_objects_class {
     'Koha::Acquisition::Baskets';
 }
 
+__PACKAGE__->add_columns(
+    '+is_standing' => { is_boolean => 1 }
+);
+
 1;
-- 
2.20.1