From a690aa6ccae2766dc142138346b03c2e413534c0 Mon Sep 17 00:00:00 2001
From: Tomas Cohen Arazi <tomascohen@theke.io>
Date: Fri, 9 Nov 2018 16:29:43 -0300
Subject: [PATCH] Bug 21786: Make balance and credits endpoints handle
 library_id

This patch:
- Adds the branchcode <-> library_id mapping in the controller
- Adds 'library_id' to the credit and account_line definitions
- Makes add_credit handle the library_id param, passing it to the
  underlying $account->add_credit method.

To test:
- Run:
  $ kshell
 k$ prove t/db_dependent/api/v1/patrons_accounts.t
=> FAIL: Tests fail, because library_id is not mapped
(balance) and because it is not handled when adding a credit.
- Apply this patch
- Run:
  k$ prove t/db_dependent/api/v1/patrons_accounts.t
=> SUCCESS: Tests pass!
- Sign off :-D

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
---
 Koha/REST/V1/Patrons/Account.pm | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/Koha/REST/V1/Patrons/Account.pm b/Koha/REST/V1/Patrons/Account.pm
index 68f0788fdc..deb4d3d58c 100644
--- a/Koha/REST/V1/Patrons/Account.pm
+++ b/Koha/REST/V1/Patrons/Account.pm
@@ -105,6 +105,7 @@ sub add_credit {
         my $payment_type = $body->{payment_type};
         my $description  = $body->{description};
         my $note         = $body->{note};
+        my $library_id   = $body->{library_id};
 
         my $credit = $account->add_credit(
             {   amount       => $amount,
@@ -112,7 +113,8 @@ sub add_credit {
                 payment_type => $payment_type,
                 description  => $description,
                 note         => $note,
-                user_id      => $user->id
+                user_id      => $user->id,
+                library_id   => $library_id
             }
         );
         $credit->discard_changes;
@@ -228,6 +230,7 @@ our $to_api_mapping = {
     accounttype       => 'account_type',
     amountoutstanding => 'amount_outstanding',
     borrowernumber    => 'patron_id',
+    branchcode        => 'library_id',
     issue_id          => 'checkout_id',
     itemnumber        => 'item_id',
     manager_id        => 'user_id',
@@ -245,6 +248,7 @@ our $to_model_mapping = {
     checkout_id        => 'issue_id',
     internal_note      => 'note',
     item_id            => 'itemnumber',
+    library_id         => 'branchcode',
     patron_id          => 'borrowernumber',
     user_id            => 'manager_id'
 };
-- 
2.17.2 (Apple Git-113)