From e606b779d67eb971825a7045ff0422fa7f04e6ec Mon Sep 17 00:00:00 2001
From: Patrick Barroca <pbarroca@afi-sa.fr>
Date: Tue, 26 Feb 2019 16:25:12 +0100
Subject: [PATCH] dev #86257 : PNB dilicom novelty date from order date

---
 VERSIONS_WIP/86257                            |  1 +
 cosmogramme/sql/patch/patch_368.php           |  2 +
 library/Class/Album.php                       |  5 ++
 library/Class/Indexation/PseudoNotice.php     | 64 ++++++++++++-------
 .../Class/Indexation/PseudoNoticeFactory.php  |  9 ++-
 .../Class/Migration/DilicomNoveltyIndex.php   | 44 +++++++++++++
 tests/db/UpgradeDBTest.php                    | 10 +++
 .../Migration/DilicomNoveltyIndexTest.php     | 59 +++++++++++++++++
 tests/scenarios/PnbDilicom/PnbDilicomTest.php |  7 ++
 9 files changed, 174 insertions(+), 27 deletions(-)
 create mode 100644 VERSIONS_WIP/86257
 create mode 100644 cosmogramme/sql/patch/patch_368.php
 create mode 100644 library/Class/Migration/DilicomNoveltyIndex.php
 create mode 100644 tests/library/Class/Migration/DilicomNoveltyIndexTest.php

diff --git a/VERSIONS_WIP/86257 b/VERSIONS_WIP/86257
new file mode 100644
index 00000000000..b5c845cb100
--- /dev/null
+++ b/VERSIONS_WIP/86257
@@ -0,0 +1 @@
+ - ticket #86257 : PNB Dilicom : La date de nouveauté des notices est calculée à partir de la date de commande la plus récente pour un document
\ No newline at end of file
diff --git a/cosmogramme/sql/patch/patch_368.php b/cosmogramme/sql/patch/patch_368.php
new file mode 100644
index 00000000000..72cd87de630
--- /dev/null
+++ b/cosmogramme/sql/patch/patch_368.php
@@ -0,0 +1,2 @@
+<?php
+(new Class_Migration_DilicomNoveltyIndex())->run();
diff --git a/library/Class/Album.php b/library/Class/Album.php
index 1a0b3683d32..32a3c9a1694 100644
--- a/library/Class/Album.php
+++ b/library/Class/Album.php
@@ -1460,6 +1460,11 @@ class Class_Album extends Storm_Model_Abstract {
   }
 
 
+  public function beValidated() {
+    return $this->setStatus(static::STATUS_VALIDATED);
+  }
+
+
   public function getIdEad() {
     return $this->hasIdOrigine() ? $this->getIdOrigine() : 'A' . $this->getId();
   }
diff --git a/library/Class/Indexation/PseudoNotice.php b/library/Class/Indexation/PseudoNotice.php
index 7d267031067..4ddd5acd446 100644
--- a/library/Class/Indexation/PseudoNotice.php
+++ b/library/Class/Indexation/PseudoNotice.php
@@ -154,26 +154,6 @@ class Class_Indexation_PseudoNotice {
   }
 
 
-  protected function _dilicomAlbumToZone995() {
-    $dilicom_item = ($items = $this->_model->getItems())
-      ? array_pop($items)
-      : new Class_Album_Item();
-
-    $this->_exemplaire->setIdOrigine($this->_model->getIdOrigine());
-    $this->_exemplaire->setZone995(serialize([
-                                              ['code' => 'a',
-                                               'valeur' => $this->_exemplaire->getBibLibelle()],
-                                              ['code' => 'c',
-                                               'valeur' => $dilicom_item->getOrderDate()],
-                                              ['code' => 'f',
-                                               'valeur' => $this->_model->getIdOrigine()],
-                                              ['code' => 'n',
-                                               'valeur' => $dilicom_item->getOrderLineId()],
-                                              ['code' => 'v',
-                                               'valeur' => $this->_exemplaire->getActivite()]]));
-  }
-
-
   protected function _ensureRecord() {
     if ($this->_notice = $this->_model->getNotice())
       return $this->_notice->setTypeDoc($this->_type_doc)->save();
@@ -243,9 +223,6 @@ class Class_Indexation_PseudoNotice {
 
 
   protected function _indexItem() {
-    if($this->_notice->isDilicom())
-      $this->_dilicomAlbumToZone995();
-
     $genre = null;
     if ($this->dataExist('genre', $this->_datas)) {
       $genres = explode(';', $this->_datas['genre']);
@@ -477,7 +454,6 @@ class Class_Indexation_PseudoNotice_Album extends Class_Indexation_PseudoNotice{
   }
 
 
-
   public function extractEditors() {
     $editors = parent::extractEditors();
     if(empty($editors))
@@ -543,6 +519,46 @@ class Class_Indexation_PseudoNotice_Album extends Class_Indexation_PseudoNotice{
 
 
 
+class Class_Indexation_PseudoNotice_AlbumDilicom extends Class_Indexation_PseudoNotice_Album {
+  protected function _index() {
+    if (!$items = $this->_model->getItems())
+      return parent::_index();
+
+    $last_time = 0;
+    foreach($items as $item) {
+      if ($last_time < ($time = strtotime($item->getOrderDate())))
+        $last_time = $time;
+    }
+
+    $this->_notice->setDateCreation(date('Y-m-d H:i:s', $last_time));
+    parent::_index();
+  }
+
+
+  protected function _indexItem() {
+    $dilicom_item = ($items = $this->_model->getItems())
+      ? array_pop($items)
+      : new Class_Album_Item();
+
+    $this->_exemplaire->setIdOrigine($this->_model->getIdOrigine());
+    $this->_exemplaire->setZone995(serialize([
+                                              ['code' => 'a',
+                                               'valeur' => $this->_exemplaire->getBibLibelle()],
+                                              ['code' => 'c',
+                                               'valeur' => $dilicom_item->getOrderDate()],
+                                              ['code' => 'f',
+                                               'valeur' => $this->_model->getIdOrigine()],
+                                              ['code' => 'n',
+                                               'valeur' => $dilicom_item->getOrderLineId()],
+                                              ['code' => 'v',
+                                               'valeur' => $this->_exemplaire->getActivite()]]));
+
+    parent::_indexItem();
+  }
+}
+
+
+
 class Class_Indexation_PseudoNotice_Cms extends Class_Indexation_PseudoNotice{
   protected $_model_name = 'Class_Article';
   protected $_id = 'id_article';
diff --git a/library/Class/Indexation/PseudoNoticeFactory.php b/library/Class/Indexation/PseudoNoticeFactory.php
index 7eaf9de780d..ecb48933fae 100644
--- a/library/Class/Indexation/PseudoNoticeFactory.php
+++ b/library/Class/Indexation/PseudoNoticeFactory.php
@@ -34,9 +34,12 @@ class Class_Indexation_PseudoNoticeFactory {
       return new $class_name($model);
     }
 
+    if (!Class_TypeDoc::isDigital($type_doc))
+      return new Class_Indexation_PseudoNotice_Null();
+
     // bibnum
-    return Class_TypeDoc::isDigital($type_doc)
-      ? new Class_Indexation_PseudoNotice_Album($model)
-      : new Class_Indexation_PseudoNotice_Null();
+    return $model->isDilicom()
+      ? new Class_Indexation_PseudoNotice_AlbumDilicom($model)
+      : new Class_Indexation_PseudoNotice_Album($model);
   }
 }
\ No newline at end of file
diff --git a/library/Class/Migration/DilicomNoveltyIndex.php b/library/Class/Migration/DilicomNoveltyIndex.php
new file mode 100644
index 00000000000..97752629be9
--- /dev/null
+++ b/library/Class/Migration/DilicomNoveltyIndex.php
@@ -0,0 +1,44 @@
+<?php
+/**
+ * Copyright (c) 2012-2019, Agence Française Informatique (AFI). All rights reserved.
+ *
+ * BOKEH is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
+ * the Free Software Foundation.
+ *
+ * There are special exceptions to the terms and conditions of the AGPL as it
+ * is applied to this software (see README file).
+ *
+ * BOKEH is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
+ *
+ * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
+ * along with BOKEH; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
+ */
+
+
+class Class_Migration_DilicomNoveltyIndex {
+  use Trait_MemoryCleaner;
+
+  public function run() {
+    if (!Class_AdminVar::isDilicomPNBEnabled())
+      return;
+
+    $page = 0;
+    while($albums = Class_Album::findAllBy(['type_doc_id' => Class_TypeDoc::DILICOM,
+                                            'limitPage' => [$page, 100]])) {
+      $this->_runPage($albums);
+      $page++;
+      $this->_cleanMemory();
+    }
+  }
+
+
+  protected function _runPage($albums) {
+    foreach($albums as $album)
+      $album->index();
+  }
+}
diff --git a/tests/db/UpgradeDBTest.php b/tests/db/UpgradeDBTest.php
index 5b79da63bbf..f61ad0b46ea 100644
--- a/tests/db/UpgradeDBTest.php
+++ b/tests/db/UpgradeDBTest.php
@@ -2528,3 +2528,13 @@ class UpgradeDB_367_Test extends UpgradeDBTestCase {
     $this->assertIndex('bib_admin_users_non_valid', 'id_site');
   }
 }
+
+
+
+
+class UpgradeDB_368_Test extends UpgradeDBTestCase {
+  public function prepare() {}
+
+  /** @test */
+  public function placeholderDilicomNoveltyIndexMigration() {}
+}
diff --git a/tests/library/Class/Migration/DilicomNoveltyIndexTest.php b/tests/library/Class/Migration/DilicomNoveltyIndexTest.php
new file mode 100644
index 00000000000..0fbde0e8ba9
--- /dev/null
+++ b/tests/library/Class/Migration/DilicomNoveltyIndexTest.php
@@ -0,0 +1,59 @@
+<?php
+/**
+ * Copyright (c) 2012-2019, Agence Française Informatique (AFI). All rights reserved.
+ *
+ * BOKEH is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
+ * the Free Software Foundation.
+ *
+ * There are special exceptions to the terms and conditions of the AGPL as it
+ * is applied to this software (see README file).
+ *
+ * BOKEH is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
+ *
+ * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
+ * along with BOKEH; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
+ */
+
+require_once __DIR__.'/../../../fixtures/DilicomFixtures.php';
+
+
+class Class_Migration_DilicomNoveltyIndexTest extends ModelTestCase {
+  public function setUp() {
+    parent::setUp();
+    RessourcesNumeriquesFixtures::activateDilicom();
+    $this->fixture('Class_Album',
+                   ['id' => 1,
+                    'titre' => 'Obligatoire',
+                    'type_doc_id' => 'Assimil'])
+         ->beValidated()
+         ->index();
+
+
+    $album = (new DilicomFixtures)->albumTotemThora();
+    $album->beValidated()->index();
+    $album->getNotice()->setDateCreation('0000-00-00 00:00:00')
+          ->save();
+
+    (new Class_Migration_DilicomNoveltyIndex())
+      ->setMemoryCleaner(function() {})
+      ->run();
+  }
+
+  /** @test */
+  public function dilicomRecordDateCreationShouldBe2015_04_01() {
+    $this->assertEquals('2015-04-01 00:00:00',
+                        Class_Album::find(3)->getNotice()->getDateCreation());
+  }
+
+
+  /** @test */
+  public function assimilRecordDateCreationShouldBeEmpty() {
+    $this->assertEquals('', Class_Album::find(1)->getNotice()->getDateCreation());
+
+  }
+}
diff --git a/tests/scenarios/PnbDilicom/PnbDilicomTest.php b/tests/scenarios/PnbDilicom/PnbDilicomTest.php
index 3e19387ef41..53133e5baf5 100644
--- a/tests/scenarios/PnbDilicom/PnbDilicomTest.php
+++ b/tests/scenarios/PnbDilicom/PnbDilicomTest.php
@@ -1128,6 +1128,13 @@ class PnbDilicomBatchJobOnixWithFullTest extends PnbDilicomJobOnixTestCase {
     $this->assertNotNull(Class_Album::find(1)->getNotice());
     $this->assertEquals('Dilicom-9782290123409', Class_Notice::find(1)->getAlbum()->getIdOrigine());
   }
+
+
+  /** @test */
+  public function dilicomRecordNoveltyDateShouldBeOrderDate() {
+    Class_Album::find(1)->index();
+    $this->assertEquals('2015-07-10 13:12:43', Class_Album::find(1)->getNotice()->getDateCreation());
+  }
 }
 
 
-- 
GitLab