From 50660f2ad83d1d02003377fd92a41aac65e55b61 Mon Sep 17 00:00:00 2001
From: Ghislain Loas <ghislo@sandbox.pergame.net>
Date: Fri, 30 Oct 2015 13:02:54 +0100
Subject: [PATCH] dev #30936 fix find item

---
 library/Class/Exemplaire.php                  |  1 +
 .../WebService/SIGB/CdScript/Service.php      | 30 +++++++++++++++----
 .../NoticeAjaxControllerCdScriptTest.php      |  7 +++--
 .../Class/WebService/SIGB/CdScriptTest.php    |  9 ++++--
 4 files changed, 37 insertions(+), 10 deletions(-)

diff --git a/library/Class/Exemplaire.php b/library/Class/Exemplaire.php
index f21c0f981cc..4e861710b87 100644
--- a/library/Class/Exemplaire.php
+++ b/library/Class/Exemplaire.php
@@ -70,6 +70,7 @@ class Class_Exemplaire extends Storm_Model_Abstract {
     return $site->getLibelle();
   }
 
+
   public function getBibLibelle() {
     return $this->hasBib() ? $this->getBib()->getLibelle() : '';
 
diff --git a/library/Class/WebService/SIGB/CdScript/Service.php b/library/Class/WebService/SIGB/CdScript/Service.php
index e19f6c34af7..18227ec38cb 100644
--- a/library/Class/WebService/SIGB/CdScript/Service.php
+++ b/library/Class/WebService/SIGB/CdScript/Service.php
@@ -85,12 +85,16 @@ class Class_Webservice_SIGB_CdScript_Service {
 
   public function getNotice($id) {
     $sigb_record = new Class_WebService_SIGB_Notice($id);
-    $item = Class_Exemplaire::findFirstBy(['code_barres' => $id]);
     $sigb_item = new Class_WebService_SIGB_Exemplaire($id);
-    $sigb_item->setCodeBarre($id);
-    $sigb_item->setBibliotheque($item->getBib());
-    $item->setUrl($this->getRessourceUrl($item))->save();
-    return $sigb_record->addExemplaire($sigb_item);
+    $sigb_record->addExemplaire($sigb_item);
+
+    if($item = $this->_getItem($id)) {
+      $sigb_item->setCodeBarre($id);
+      $sigb_item->setBibliotheque($item->getBib());
+      $item->setUrl($this->getRessourceUrl($item))->save();
+    }
+
+    return $sigb_record;
   }
 
 
@@ -105,5 +109,21 @@ class Class_Webservice_SIGB_CdScript_Service {
     $item = $this->getNoticeCache()->getExemplaire($notice_id, $code_barre);
     return $item;
   }
+
+
+  protected function _getCdScriptLibId() {
+    if($lib = Class_IntBib::findFirstBy(['comm_sigb' => Class_IntBib::COM_CDSCRIPT]))
+      return $lib->getIdBib();
+    return '';
+  }
+
+
+  protected function _getItem($id) {
+    if(!$lib = $this->_getCdScriptLibId())
+      return null;
+
+    return  Class_Exemplaire::findFirstBy(['code_barres' => $id,
+                                           'id_int_bib' => $lib]);
+    }
 }
 ?>
\ No newline at end of file
diff --git a/tests/application/modules/opac/controllers/NoticeAjaxControllerCdScriptTest.php b/tests/application/modules/opac/controllers/NoticeAjaxControllerCdScriptTest.php
index 9f908af6c5d..4842d9606b6 100644
--- a/tests/application/modules/opac/controllers/NoticeAjaxControllerCdScriptTest.php
+++ b/tests/application/modules/opac/controllers/NoticeAjaxControllerCdScriptTest.php
@@ -50,7 +50,8 @@ class NoticeAjaxControllerCdScriptRecordTest extends AbstractControllerTestCase
     Class_Profil::getCurrentProfil()->setCfgNotice($config);
 
     $this->fixture('Class_IntBib',
-                   ['id' => 3,
+                   ['id' => 31,
+                    'libelle' => 'le kiosque library',
                     'comm_sigb' => Class_IntBib::COM_CDSCRIPT]);
 
     $this->fixture('Class_Bib',
@@ -62,8 +63,8 @@ class NoticeAjaxControllerCdScriptRecordTest extends AbstractControllerTestCase
                                          'code_barres' => '456789',
                                          'id_origine' => '456789',
                                          'id_notice' => 2,
-                                         'id_int_bib' => '3',
-                                         'id_bib' => '3',
+                                         'id_int_bib' => 31,
+                                         'id_bib' => 3,
                                          'cote' => '',
                                          'zone995' => 'a:3:{i:0;a:2:{s:4:"code";s:1:"a";s:6:"valeur";s:10:"Le Kiosque";}i:1;a:2:{s:4:"code";s:1:"b";s:6:"valeur";s:3:"kio";}i:2;a:2:{s:4:"code";s:1:"f";s:6:"valeur";s:3:"319";}}']);
 
diff --git a/tests/library/Class/WebService/SIGB/CdScriptTest.php b/tests/library/Class/WebService/SIGB/CdScriptTest.php
index 516b6be9373..85684e1f9a3 100644
--- a/tests/library/Class/WebService/SIGB/CdScriptTest.php
+++ b/tests/library/Class/WebService/SIGB/CdScriptTest.php
@@ -42,6 +42,11 @@ class CdScriptServiceTest extends CdScriptTestCase {
   public function setUp() {
     parent::setUp();
 
+    $this->fixture('Class_IntBib',
+                   ['id' => 3,
+                    'libelle' => 'le kiosque library',
+                    'comm_sigb' => Class_IntBib::COM_CDSCRIPT]);
+
     $this->fixture('Class_Bib',
                    ['id' => 3,
                     'libelle' => 'Le kiosque library']);
@@ -63,8 +68,8 @@ class CdScriptServiceTest extends CdScriptTestCase {
                                         ['id' => 1,
                                          'code_barres' => '456789',
                                          'id_origine' => '456789',
-                                         'id_int_bib' => '3',
-                                         'id_bib' => '3',
+                                         'id_int_bib' => 3,
+                                         'id_bib' => 3,
                                          'zone995' => 'a:3:{i:0;a:2:{s:4:"code";s:1:"a";s:6:"valeur";s:10:"Le Kiosque";}i:1;a:2:{s:4:"code";s:1:"b";s:6:"valeur";s:3:"kio";}i:2;a:2:{s:4:"code";s:1:"f";s:6:"valeur";s:3:"319";}}']);
 
     $this->_sigb_le_kiosque = $this->_service->getNotice('456789')->getExemplaireByCodeBarre('456789');
-- 
GitLab