From 9ab467476db97d2051fec32c77df576e8284707e Mon Sep 17 00:00:00 2001
From: Patrick Barroca <pbarroca@afi-sa.fr>
Date: Fri, 27 Sep 2019 14:04:49 +0200
Subject: [PATCH] hotline #96923 : fix serial import with double mode none

---
 VERSIONS_HOTLINE/96923                        |  1 +
 .../php/classes/classe_notice_integration.php | 83 ++++++++++---------
 .../tests/php/classes/KohaPeriodiquesTest.php |  6 +-
 .../classes/NoticeIntegrationOrpheeTest.php   | 67 +++++++++++++--
 .../php/classes/unimarc_orphee_96923.txt      |  1 +
 library/Class/NoticeUnimarc/Writer.php        |  3 +
 6 files changed, 111 insertions(+), 50 deletions(-)
 create mode 100644 VERSIONS_HOTLINE/96923
 create mode 100644 cosmogramme/tests/php/classes/unimarc_orphee_96923.txt

diff --git a/VERSIONS_HOTLINE/96923 b/VERSIONS_HOTLINE/96923
new file mode 100644
index 00000000000..28365822cc2
--- /dev/null
+++ b/VERSIONS_HOTLINE/96923
@@ -0,0 +1 @@
+ - ticket #96923 : Intégration : SIGB Orphée et Koha : Prise en charge de l'import des périodique en mode de dédoublonnage "Aucun dédoublonnage"
\ No newline at end of file
diff --git a/cosmogramme/php/classes/classe_notice_integration.php b/cosmogramme/php/classes/classe_notice_integration.php
index 0d15b3b7c42..4f70654a321 100644
--- a/cosmogramme/php/classes/classe_notice_integration.php
+++ b/cosmogramme/php/classes/classe_notice_integration.php
@@ -416,60 +416,63 @@ class notice_integration {
     if (0 == count($this->notice['exemplaires']))
       return [];
 
-    $number_map = [3 => 'v', 4 => '6'];
+    $number_map = [Class_IntProfilDonnees::SERIAL_FORMAT_KOHA => 'v',
+                   Class_IntProfilDonnees::SERIAL_FORMAT_ORPHEE => '6'];
     if (!array_key_exists($this->id_article_periodique, $number_map))
       return [];
 
     $champ_numero = $number_map[$this->id_article_periodique];
     $unimarc = $this->notice['unimarc'];
+    $items = $this->notice['exemplaires'];
+    $id_origine = $this->notice['id_origine'];
+
+    foreach($items as $item)
+      $this->_traitePeriodiquesKohaItem($unimarc,
+                                        unserialize($item['zone995']),
+                                        $champ_numero,
+                                        $id_origine);
+  }
 
-    foreach($this->notice['exemplaires'] as $exemplaire) {
-      $this->notice_sgbd->ouvrirNotice($unimarc,
-                                       $this->id_profil,
-                                       $this->sigb,
-                                       $this->type_doc_force);
 
-      $champs = unserialize($exemplaire['zone995']);
-      $table_champs = [];
-      $numero = '';
-      foreach($champs as $champ) {
-        $table_champs[] = [$champ['code'], $champ['valeur']];
+  protected function _traitePeriodiquesKohaItem($unimarc, $champs, $champ_numero, $id_origine) {
+    $this->notice_sgbd->ouvrirNotice($unimarc,
+                                     $this->id_profil,
+                                     $this->sigb,
+                                     $this->type_doc_force);
 
-        if ($champ['code'] != $champ_numero)
-          continue;
+    $table_champs = [];
+    $numero_value = '';
+    $complement_titre = '';
 
+    foreach($champs as $champ) {
+      $table_champs[] = [$champ['code'], $champ['valeur']];
+      if ($champ['code'] == $champ_numero)
+        $numero_value = $champ['valeur'];
+    }
 
-        if (preg_match('/(\d+)/', $champ['valeur'], $numbers)) {
-          $numero = $numbers[0];
-          $complement_titre = trim(substr($champ['valeur'], strpos($champ['valeur'], $numero) + strlen($numero)));
-        } else {
-          $numero = $champ['valeur'];
-        }
+    $numero = $this->_periodiqueKohaNumeroFrom($numero_value, $complement_titre);
 
-      }
+    $this->notice_sgbd->delete_field('001');
+    $this->notice_sgbd->add_zone('001', $id_origine . '-' . $numero);
 
-      $this->notice_sgbd->add_field('461', '11', 't' . $this->notice['titre_princ']);
-      $this->notice_sgbd->add_field('461', '11', 'v' . $numero);
-      if ($complement_titre)
-        $this->notice_sgbd->add_field('461', '11', 'o' . $complement_titre);
-      $this->notice_sgbd->add_field('995', '  ', $table_champs);
-      $this->notice_sgbd->update();
-      $data = $this->notice_sgbd->getFullRecord();
-      $this->traiteNotice($data);
-    }
+    $this->notice_sgbd->add_field('461', '11', 't' . $this->notice['titre_princ']);
+    $this->notice_sgbd->add_field('461', '11', 'v' . $numero);
+    if ($complement_titre)
+      $this->notice_sgbd->add_field('461', '11', 'o' . $complement_titre);
+
+    $this->notice_sgbd->add_field('995', '  ', $table_champs);
+    $this->notice_sgbd->update();
+
+    $this->traiteNotice($this->notice_sgbd->getFullRecord());
   }
 
-// ----------------------------------------------------------------
-// Retourne l'url d'origine du site (ex: http://opac3.pergame.net/)
-// ----------------------------------------------------------------
-  private function getUrlSite() {
-    if (isset($this->url_site))
-      return $this->url_site;
-
-    $adresse=getVariable("url_site");
-    if(strtolower(substr($adresse,0,7)) !="http://") $adresse="http://".$adresse;
-    if(substr($adresse,-1,1)!="/") $adresse.="/";
-    return $this->url_site = $adresse;
+
+  protected function _periodiqueKohaNumeroFrom($value, &$complement) {
+    if (1 !== preg_match('/(\d+)/', $value, $numbers))
+      return $value;
+
+    $complement = trim(substr($value, strpos($value, $numbers[0]) + strlen($numbers[0])));
+    return $numbers[0];
   }
 
 
diff --git a/cosmogramme/tests/php/classes/KohaPeriodiquesTest.php b/cosmogramme/tests/php/classes/KohaPeriodiquesTest.php
index 9438a6ead8e..e38cfb06231 100644
--- a/cosmogramme/tests/php/classes/KohaPeriodiquesTest.php
+++ b/cosmogramme/tests/php/classes/KohaPeriodiquesTest.php
@@ -240,11 +240,13 @@ class KohaPeriodiquesMarieClaireWithoutDoubleSearchTest extends KohaPeriodiquesT
     $this->loadNotice('unimarc_kohaperiodique');
   }
 
+
   /** @test */
-  public function shouldHaveInsertedOnlyOneRecord() {
-    $this->assertEquals(1, count(Class_Notice::findAll()));
+  public function shouldHaveInserted21RecordsToo() {
+    $this->assertEquals(21, count(Class_Notice::findAll()));
   }
 
+
   /** @test */
   public function shouldHaveInserted51Items() {
     $this->assertCount(51, Class_Exemplaire::findAll());
diff --git a/cosmogramme/tests/php/classes/NoticeIntegrationOrpheeTest.php b/cosmogramme/tests/php/classes/NoticeIntegrationOrpheeTest.php
index 7bec6a94ac5..198907651ad 100644
--- a/cosmogramme/tests/php/classes/NoticeIntegrationOrpheeTest.php
+++ b/cosmogramme/tests/php/classes/NoticeIntegrationOrpheeTest.php
@@ -27,18 +27,10 @@ require_once 'NoticeIntegrationTest.php';
 class NoticeIntegrationOrpheeDegreeTest extends NoticeIntegrationTestCase {
 	public function setUp() {
 		parent::setUp();
-		Storm_Model_Loader::defaultToVolatile();
-
 		$this->loadNotice('unimarc_orphee_24365');
 	}
 
 
-	public function tearDown() {
-		Storm_Model_Loader::defaultToDb();
-		parent::tearDown();
-	}
-
-
 	public function getProfilDonnees() {
 		return Class_IntProfilDonnees::forOrphee()
 			->setIdProfil(110)
@@ -56,4 +48,63 @@ class NoticeIntegrationOrpheeDegreeTest extends NoticeIntegrationTestCase {
 	public function recordShouldContainsValidUtf8Degree() {
 		$this->assertContains('N°', Class_Notice::find(1)->get_subfield(315, 'a')[0]);
 	}
+}
+
+
+
+
+/** @see http://forge.afi-sa.fr/issues/96923 */
+class NoticeIntegrationOrpheeSerialWithDoubleSearchNoneTest extends NoticeIntegrationTestCase {
+	public function setUp() {
+		parent::setUp();
+
+    Class_CosmoVar::setValueOf(Class_Notice_DoubleFinder::VAR_DOUBLE_MODE,
+                               Class_CosmoVar::DOUBLE_SEARCH_NONE);
+
+    $this->loadNotice('unimarc_orphee_96923');
+	}
+
+
+	public function getProfilDonnees() {
+		return Class_IntProfilDonnees::forOrphee()
+			->setIdProfil(110)
+			->getRawAttributes();
+	}
+
+
+  /** @test */
+  public function shouldCreate29Records() {
+    $this->assertEquals(29, Class_Notice::count());
+  }
+
+
+  public function numbers() {
+    return array_filter(array_map(function($number)
+                                  {
+                                    return (459 != $number)
+                                      ? [$number]
+                                      : null;
+                                  },
+                                  range(458, 487)));
+  }
+
+
+  /**
+   * @test
+   * @dataProvider numbers
+   */
+  public function numberShouldHaveItsRecord($number) {
+    $key = 'GEO-UNNOUVEAUMONDELATER--' . $number . '-GEO-1979-2';
+    $this->assertNotNull(Class_Notice::findFirstBy(['clef_alpha' => $key]));
+  }
+
+
+  /**
+   * @test
+   * @dataProvider numbers
+   */
+  public function numberShouldHaveItsItemWithSpecificIdOrigine($number) {
+    $id = 'frOr0939524523-' . $number;
+    $this->assertNotNull(Class_Exemplaire::findFirstBy(['id_origine' => $id]));
+  }
 }
\ No newline at end of file
diff --git a/cosmogramme/tests/php/classes/unimarc_orphee_96923.txt b/cosmogramme/tests/php/classes/unimarc_orphee_96923.txt
new file mode 100644
index 00000000000..0c976843d77
--- /dev/null
+++ b/cosmogramme/tests/php/classes/unimarc_orphee_96923.txt
@@ -0,0 +1 @@
+06689nas  2200493   450 001001500000011001400015100004100029101001300070110001600083200006700099210002500166300004200191326001300233801002000246995019600266995023200462995022900694995023200923995019101155995019501346995019701541995019901738995022801937995019702165995019502362995023302557995019602790995022702986995023303213995022703446995019003673995022503863995022004088995021804308995022204526995022104748995019104969995021805160995019105378995016105569995019105730995015505921995011906076frOr0939524523  a0220-8243  a19901002                  0103        afrecfre  aa          1 aGÂeoeUn nouveau monde, la Terrefsous la dir. de Robert Fiess  aPariscGeod1979---> 1aRevue conservÂee 2 ans en salle verte  aMensuel. 3aFRbBM St-Cloud00a200024850170b939524523cpd20g1h1i1l3061445o19/07/2019p30/07/2019q15/06/2019r13/07/2019t1303000835u7328w2x12y2z1C01/04/2017D16F1G17H17NbOgS20T1U12V20W12Z2645808100a200024945483b939524523cpd20g1h1i1k1300202098l3151093m03/09/2019n01/10/2019o04/07/2019p31/08/2019q30/04/2019r22/06/2019t1303001424u7360w2x12y2z2C01/06/2017D16F1G17H17NbOgS20T1U12V20W12Z2646008100a200025015658b939524523cpd20g1h1i1k1303700096l3061445m07/09/2019n05/10/2019o20/03/2019p06/04/2019q13/03/2019r20/03/2019t2981921u7376w2x12y2z2C01/07/2017D16F1G13H13NbOgS20T1U12V20W12Z2646108100a200025076874b939524523cpd20g1h1i1k1300202098l3151093m03/09/2019n01/10/2019o04/07/2019p31/08/2019q10/05/2019r22/06/2019t1300200775u7392w2x12y2z2C01/08/2017D16F1G14H14NbOgS20T1U12V20W12Z2646208100a200025177102b939524523cpd20g1h1i1l1303000003o12/03/2019p10/04/2019q22/01/2019r06/03/2019t1513u7408w2x12y2z1C01/09/2017D16F1G8H8NbOgS20T1U12V20W12Z2646308100a200025215894b939524523cpd20g1h1i1l28311557o06/07/2019p13/07/2019q10/04/2018r16/05/2018t1301200653u7424w2x12y2z1C01/10/2017D16F1G3H3NbOgS20T1U12V20W12Z2646408100a200025237187b939524523cpd20g1h1i1l1303000891o09/06/2018p07/07/2018q10/03/2018r11/04/2018t1301200701u7440w2x12y2z1C01/11/2017D16F1G5H5NbOgS20T1U12V20W12Z1646508100a200025686300b939524523cpd20g1h1i1l1301200937o22/06/2019p11/09/2019q20/04/2019r18/05/2019t1301201559u7456w2x12y2z1C01/12/2017D16F1G12H12NbOgS20T1U12V20W12Z2646608100a200025265709b939524523cpd20g1h1i1k1301900227l1303001424m07/09/2019n05/10/2019o30/04/2019p22/06/2019q09/03/2019r24/04/2019t10218u7472w2x12y2z2C01/01/2018D16F1G9H9NbOgS20T1U12V20W12Z2646708200a200025867645b939524523cpd20g1h1i1l1301602009o20/07/2019p14/09/2019q30/04/2019r22/06/2019t1303001424u7488w2x12y2z1C01/02/2018D16F1G6H6NbOgS20T1U12V20W12Z2646808200a200025812948b939524523cpd20g1h1i1l28311557o06/07/2019p13/07/2019q13/06/2019r05/07/2019t1300201650u7504w2x12y2z1C01/03/2018D16F1G6H6NbOgS20T1U12V20W12Z2646908200a200025953858b939524523cpd20g1h1i1k1301201559l1303001424m27/06/2019n12/09/2019o30/04/2019p22/06/2019q09/01/2019r26/01/2019t1301201559u7520w2x12y2z2C01/04/2018D16F1G9H9NbOgS20T1U12V20W12Z2647008200a200025286267b939524523cpd20g1h1i1l3061618o15/06/2019p29/06/2019q11/05/2019r15/06/2019t1301602009u7536w2x12y2z1C01/05/2018D16F1G10H10NbOgS20T1U12V20W12Z2647108200a200025974078b939524523cpd20g1h1i1k1300202098l1301600890m03/09/2019n01/10/2019o20/10/2018p01/12/2018q30/06/2018r26/09/2018t1513u7552w2x12y2z2C01/06/2018D16F1G3H3NbOgS20T1U12V20W12Z2647208200a200025968542b939524523cpd20g1h1i1k1300202098l1301202218m03/09/2019n01/10/2019o13/07/2019p27/08/2019q18/05/2019r07/06/2019t1301201559u7568w2x12y2z2C01/07/2018D16F1G6H6NbOgS20T1U12V20W12Z2647308200a200025479649b939524523cpd20g1h1i1k1301203206l3151047m03/09/2019n01/10/2019o09/02/2019p06/04/2019q02/02/2019r09/02/2019t3061618u7584w2x12y2z2C01/08/2018D16F1G6H6NbOgS20T1U12V20W12Z2647408200a200025500832b939524523cpd20g1h1i1l7012o23/01/2019p26/03/2019q08/12/2018r14/01/2019t1301200701u7600w2x12y2z1C01/09/2018D3F1G5H5NbOgS20T1U12V20W12Z2647508200a200025584257b939524523cpd20g1h1i1k1300202098l3070278m03/09/2019n01/10/2019o27/07/2019p12/08/2019q31/05/2019r06/06/2019t12883u7616w2x12y2z2C01/10/2018D16F1G8H8NbOgS20T1U12V20W12Z2647608200a200026034864b939524523cpd20g1h1i1k1301203206l344m03/09/2019n01/10/2019o28/02/2019p27/03/2019q28/11/2018r12/01/2019t1513u7632w2x12y2z2C01/11/2018D16F1G3H3NbOgS20T1U12V20W12Z2647708200a200026104089b939524523cpd20g1h1i1k7012l9974m06/09/2019n04/10/2019o30/08/2019p03/09/2019q27/07/2019r31/07/2019t3070278u7648w2x12y2z2C01/12/2018D16F1G3H3NbOgS20T1U12V20W12Z2647808200a200025470481b939524523cpd20g1h1i1k7012l1301202218m06/09/2019n04/10/2019o13/07/2019p27/08/2019q31/05/2019r06/06/2019t12883u7664w2x12y2z2C01/01/2019D16F1G7H7NbOgS20T1U12V20W12Z2647908300a200026181871b939524523cpd20g1h1i1k7012l3005010m06/09/2019n04/10/2019o18/05/2019p18/06/2019q20/04/2019r04/05/2019t3061618u7680w2x12y2z2C01/02/2019D16F1G4H4NbOgS20T1U12V20W12Z2648008300a200026257119b939524523cpd20g1h1i1l9974o18/06/2019p09/07/2019q11/05/2019r15/06/2019t1301602009u7696w2x12y2z1C01/03/2019D16F1G3H3NbOgS20T1U12V20W12Z2648108300a200026332516b939524523cpd20g1h1i1k7012l3061618m06/09/2019n04/10/2019o15/06/2019p29/06/2019q27/04/2019r08/06/2019t1513u7712w2x12y2z2C01/04/2019D16F1G3H3NbOgS20T1U12V20W12Z2648208300a200026352985b939524523cpd20g1h1i1k1303001103l1513m14/09/2019n12/10/2019o28/05/2019p10/09/2019u7728w2x12y2z2C01/05/2019D16F1G2H2NbOgS20T1U12V20W12Z2648308300a200026423364b939524523cpd20g1h1i1k1301201559m27/06/2019n12/09/2019u7744w2x12y2z2C01/06/2019D16F1G1H1NbOgS20T1U12V20W12Z2648408300a200026555447b939524523cpd20g1h1i1k1513l1301202297m10/09/2019n08/10/2019o31/07/2019p04/09/2019u7760w2x12y2z2C01/07/2019D16F1G2H2NbOgS20T1U12V20W12Z2648508300a200026584983b939524523cpd20g1h1i1k1513m28/08/2019n25/09/2019u7776w2x12y2z2C01/08/2019D16F1G1H1NbOgS20T1U12V20W12Z2648608300a200026646600b939524523cpd20g1h1i1u7792w2x12y1z1C01/09/2019D16F1NbOgS20T1U12V20W12Z16487083
\ No newline at end of file
diff --git a/library/Class/NoticeUnimarc/Writer.php b/library/Class/NoticeUnimarc/Writer.php
index 895e29a2662..cb7469dde29 100644
--- a/library/Class/NoticeUnimarc/Writer.php
+++ b/library/Class/NoticeUnimarc/Writer.php
@@ -128,6 +128,9 @@ class Class_NoticeUnimarc_Writer extends Class_NoticeUnimarc {
     if (!isset($this->inner_data[$zone]))
       $this->inner_data[$zone] = [];
 
+    if ($this->field_end != substr($valeur, -(strlen($this->field_end))))
+      $valeur .= $this->field_end;
+
     $this->inner_data[$zone][] = $valeur;
     ksort($this->inner_data);
   }
-- 
GitLab