From 3ab13e9d6daf3da9846df4be2df39c5d31c548b8 Mon Sep 17 00:00:00 2001
From: Laurent Laffont <llaffont@afi-sa.fr>
Date: Sat, 9 Dec 2017 15:24:14 +0100
Subject: [PATCH] dev #13615  loans history can fetch all loans from webservice

---
 .../WebService/SIGB/AbstractRESTService.php   |  1 -
 .../Class/WebService/SIGB/AbstractService.php |  2 +-
 library/Class/WebService/SIGB/Emprunteur.php  |  8 +++--
 .../Class/WebService/SIGB/Koha/Service.php    |  4 +--
 .../Class/WebService/SIGB/LoansHistory.php    | 31 ++++++++++++++++++-
 .../SIGB/Nanook/GetLoanHistoryReader.php      | 15 +++++++--
 .../Class/WebService/SIGB/Nanook/Service.php  |  7 +++--
 .../Class/WebService/SIGB/NanookTest.php      | 31 ++++++++++++++-----
 8 files changed, 80 insertions(+), 19 deletions(-)

diff --git a/library/Class/WebService/SIGB/AbstractRESTService.php b/library/Class/WebService/SIGB/AbstractRESTService.php
index 78a5612708a..a485b4b9bf0 100644
--- a/library/Class/WebService/SIGB/AbstractRESTService.php
+++ b/library/Class/WebService/SIGB/AbstractRESTService.php
@@ -61,7 +61,6 @@ abstract class Class_WebService_SIGB_AbstractRESTService extends Class_WebServic
    * @return Class_WebService_SimpleWebClient
    */
   public function getWebClient() {
-    xdebug_break();
     if (!isset($this->_web_client))
       $this->_web_client = new Class_WebService_SimpleWebClient();
     return $this->_web_client;
diff --git a/library/Class/WebService/SIGB/AbstractService.php b/library/Class/WebService/SIGB/AbstractService.php
index 84a94d24542..76713763028 100644
--- a/library/Class/WebService/SIGB/AbstractService.php
+++ b/library/Class/WebService/SIGB/AbstractService.php
@@ -129,7 +129,7 @@ abstract class Class_WebService_SIGB_AbstractService {
   }
 
 
-  public function loansHistory($emprunteur, $page) {
+  public function loansHistory($emprunteur, $page = 1) {
     return new Class_WebService_SIGB_LoansHistory();
   }
 
diff --git a/library/Class/WebService/SIGB/Emprunteur.php b/library/Class/WebService/SIGB/Emprunteur.php
index cefffc16286..799974e13bd 100644
--- a/library/Class/WebService/SIGB/Emprunteur.php
+++ b/library/Class/WebService/SIGB/Emprunteur.php
@@ -448,10 +448,12 @@ class Class_WebService_SIGB_Emprunteur {
   }
 
 
-  public function getLoansHistory($page) {
-    return $this->_service
-      ? $this->_service->loansHistory($this,$page)
+  public function getLoansHistory($page = 1) {
+    $history = $this->_service
+      ? $this->_service->loansHistory($this, $page)
       : new Class_WebService_SIGB_LoansHistory();
+
+    return $history->setLoaner($this);
   }
 
 
diff --git a/library/Class/WebService/SIGB/Koha/Service.php b/library/Class/WebService/SIGB/Koha/Service.php
index ce9be047508..a00fad26fb1 100644
--- a/library/Class/WebService/SIGB/Koha/Service.php
+++ b/library/Class/WebService/SIGB/Koha/Service.php
@@ -198,9 +198,9 @@ class Class_WebService_SIGB_Koha_Service extends Class_WebService_SIGB_AbstractR
   }
 
 
-  public function loansHistory($user) {
+  public function loansHistory($user, $page = 1) {
     if (!$this->providesLoansHistory())
-      return parent::loansHistory();
+      return parent::loansHistory($user, $page);
 
     return $this
       ->getRestfulService()
diff --git a/library/Class/WebService/SIGB/LoansHistory.php b/library/Class/WebService/SIGB/LoansHistory.php
index d4de744572d..1a6e73e3567 100644
--- a/library/Class/WebService/SIGB/LoansHistory.php
+++ b/library/Class/WebService/SIGB/LoansHistory.php
@@ -21,7 +21,9 @@
 
 
 class Class_WebService_SIGB_LoansHistory {
-  protected $_total_count=0,
+  protected
+    $_total_count = 0,
+    $_loaner,
     $_loans;
 
   public function __construct() {
@@ -29,6 +31,12 @@ class Class_WebService_SIGB_LoansHistory {
   }
 
 
+  public function setLoaner($loaner) {
+    $this->_loaner = $loaner;
+    return $this;
+  }
+
+
   public function totalCount() {
     return $this->_total_count;
   }
@@ -38,20 +46,41 @@ class Class_WebService_SIGB_LoansHistory {
     return $this->_loans;
   }
 
+
   public function setTotalCount($count) {
     $this->_total_count = $count;
     return $this;
   }
 
+
   public function addLoan($loan) {
     $this->_loans->append($loan);
     return $this;
   }
 
+
   public function setLoans($loans) {
     $this->_loans = new Storm_Collection($loans);
     return $this;
   }
+
+
+  public function fetchAll() {
+    if (!$this->_total_count)
+      return $this;
+
+    if (!$this->_loaner)
+      return $this;
+
+    $current_page = 1;
+    do {
+      $current_page = $current_page + 1;
+      $loans = $this->_loaner->getLoansHistory($current_page)->getLoans();
+      $this->_loans->addAll($loans);
+    } while(!$loans->isEmpty() and ($this->_loans->count() < $this->_total_count));
+
+    return $this;
+  }
 }
 
 ?>
\ No newline at end of file
diff --git a/library/Class/WebService/SIGB/Nanook/GetLoanHistoryReader.php b/library/Class/WebService/SIGB/Nanook/GetLoanHistoryReader.php
index 1a32b4a524d..38214968c26 100644
--- a/library/Class/WebService/SIGB/Nanook/GetLoanHistoryReader.php
+++ b/library/Class/WebService/SIGB/Nanook/GetLoanHistoryReader.php
@@ -81,10 +81,21 @@ class Class_WebService_SIGB_Nanook_GetLoanHistoryReader {
 
 
   public function endBackDate($data) {
-    $date = implode('/', array_reverse(explode('-', $data)));
-    $this->_current_loan->getExemplaire()->setDateRetour($date);
+    $this->_current_loan
+      ->getExemplaire()
+      ->setDateRetour($this->_formatDate($data));
+  }
+
 
+  public function endLoanDate($data) {
+    $this->_current_loan
+      ->getExemplaire()
+      ->setIssueDate($this->_formatDate($data));
   }
 
 
+  protected function _formatDate($data) {
+    return implode('/', array_reverse(explode('-', $data)));
+  }
+
 }
\ No newline at end of file
diff --git a/library/Class/WebService/SIGB/Nanook/Service.php b/library/Class/WebService/SIGB/Nanook/Service.php
index 4b1a915b9e2..b5c4932ff44 100644
--- a/library/Class/WebService/SIGB/Nanook/Service.php
+++ b/library/Class/WebService/SIGB/Nanook/Service.php
@@ -140,12 +140,15 @@ class Class_Webservice_SIGB_Nanook_Service extends Class_WebService_SIGB_Abstrac
   }
 
 
-  public function loansHistory($emprunteur, $page) {
+  public function loansHistory($emprunteur, $page = 1) {
     $url = $this->buildQueryURL(['service' => 'GetLoanHistory',
                                  'patronId' => $emprunteur->getId()])
       .'?pageNumber='.$page;
     $xml = $this->getWebClient()->open_url($url);
-    return (new Class_WebService_SIGB_Nanook_GetLoanHistoryReader())->parseXML($xml)->getHistory();
+    $history = (new Class_WebService_SIGB_Nanook_GetLoanHistoryReader())
+      ->parseXML($xml)
+      ->getHistory();
+    return $history->setLoaner($emprunteur);
   }
 
 
diff --git a/tests/library/Class/WebService/SIGB/NanookTest.php b/tests/library/Class/WebService/SIGB/NanookTest.php
index 30a020edf1d..7406e86d0a4 100644
--- a/tests/library/Class/WebService/SIGB/NanookTest.php
+++ b/tests/library/Class/WebService/SIGB/NanookTest.php
@@ -1282,11 +1282,13 @@ class NanookPickupLocationsActiveWithSiteIdTest extends NanookTestCase {
 }
 
 
+
+
 class NanookLoanHistoryTest extends NanookTestCase {
-  protected $loans_history,
+  protected
+    $loans_history,
     $emprunteur;
 
-
   public function setUp() {
     parent::setUp();
      $this->_mock_web_client
@@ -1297,9 +1299,10 @@ class NanookLoanHistoryTest extends NanookTestCase {
       ->with('http://localhost:8080/afi_Nanook/ilsdi/service/GetLoanHistory/patronId/34?pageNumber=2')
       ->answers(NanookFixtures::xmlGetLoanHistoryPageTwo())
       ->beStrict();
-     $this->emprunteur = new Class_WebService_SIGB_Emprunteur('34', 'harlock');
+     $this->emprunteur = (new Class_WebService_SIGB_Emprunteur('34', 'harlock'))
+       ->setService($this->_service);
 
-     $this->loans_history = $this->_service->loansHistory($this->emprunteur,1);
+     $this->loans_history = $this->emprunteur->getLoansHistory();
   }
 
 
@@ -1311,13 +1314,13 @@ class NanookLoanHistoryTest extends NanookTestCase {
 
   /** @test */
   public function oneLoanShouldContainsTitleLeCameleon() {
-    $this->assertEquals("Le caméléon", $this->loans_history->getLoans()[1]->getTitre());
+    $this->assertEquals('Le caméléon', $this->loans_history->getLoans()[1]->getTitre());
   }
 
 
   /** @test */
   public function firstLoanIdShouldBe231645() {
-    $this->assertEquals("231645", $this->loans_history->getLoans()[0]->getId());
+    $this->assertEquals(231645, $this->loans_history->getLoans()[0]->getId());
   }
 
   /** @test */
@@ -1343,9 +1346,23 @@ class NanookLoanHistoryTest extends NanookTestCase {
     $this->assertEquals('02/09/2017', $this->loans_history->getLoans()[0]->getDateRetour());
   }
 
+
+  /** @test */
+  public function secondLoanIssueDateShouldBe24_08_2017() {
+    $this->assertEquals('24/08/2017', $this->loans_history->getLoans()[0]->getIssueDate());
+  }
+
+
   /** @test */
   public function secondPageLoanTitleShouldBeJeteCroquerai() {
-    $this->assertEquals('Je te croquerai !', $this->_service->loansHistory($this->emprunteur,2)->getLoans()[0]->getTitre());
+    $this->assertEquals('Je te croquerai !', $this->_service->loansHistory($this->emprunteur, 2)->getLoans()[0]->getTitre());
+  }
+
+
+  /** @test */
+  public function loanHistorFetchAllShouldLoadAllPages() {
+    $history = $this->_service->loansHistory($this->emprunteur)->fetchAll();
+    $this->assertCount(3, $history->getLoans());
   }
 
 }
\ No newline at end of file
-- 
GitLab