mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-21 15:13:50 +00:00
FEATURE [centerHistory] attempt to complete test for centerHistory
This commit is contained in:
@@ -15,7 +15,6 @@ class PersonMoveAccompanyingPeriodParticipationHandler implements PersonMoveSqlH
|
||||
|
||||
public function getSqls(string $className, string $field, Person $from, Person $to): array
|
||||
{
|
||||
var_dump(__METHOD__);
|
||||
$insertSql = sprintf(<<<SQL
|
||||
INSERT INTO chill_person_accompanying_period_participation (person_id, accompanyingperiod_id, id, startdate, enddate)
|
||||
SELECT %d, accompanyingperiod_id, nextval('chill_person_person_center_history_id_seq'), startdate, enddate
|
||||
|
@@ -4,9 +4,14 @@ namespace Chill\PersonBundle\Actions\Remove\Handler;
|
||||
|
||||
use Chill\PersonBundle\Actions\Remove\PersonMoveSqlHandlerInterface;
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
|
||||
class PersonMoveCenterHistoryHandler implements PersonMoveSqlHandlerInterface
|
||||
{
|
||||
public function __construct(private EntityManagerInterface $em)
|
||||
{
|
||||
}
|
||||
|
||||
public function supports(string $className, string $field): bool
|
||||
{
|
||||
return $className === Person\PersonCenterHistory::class;
|
||||
@@ -14,32 +19,40 @@ class PersonMoveCenterHistoryHandler implements PersonMoveSqlHandlerInterface
|
||||
|
||||
public function getSqls(string $className, string $field, Person $from, Person $to): array
|
||||
{
|
||||
$insertSql = sprintf(<<<SQL
|
||||
INSERT INTO chill_person_person_center_history (id, person_id, center_id, startdate, enddate, createdat, updatedat, createdby_id, updatedby_id)
|
||||
SELECT nextval('chill_person_person_center_history_id_seq'), %d, center_id, startdate, enddate, createdat, updatedat, createdby_id, updatedby_id
|
||||
FROM chill_person_person_center_history cppch
|
||||
WHERE person_id = %d
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM chill_person_person_center_history cppch2
|
||||
WHERE
|
||||
person_id = %d
|
||||
AND daterange(cppch.startdate, cppch.enddate) && daterange(cppch2.startdate, cppch2.enddate)
|
||||
)
|
||||
SQL, $to->getId(), $from->getId(), $to->getId());
|
||||
$sqlStatements = [];
|
||||
|
||||
$updateSql = sprintf(<<<SQL
|
||||
UPDATE chill_person_person_center_history cppch
|
||||
SET startdate =
|
||||
WHERE person_id = %d
|
||||
AND EXISTS (
|
||||
SELECT 1 FROM chill_person_person_center_history cppch2
|
||||
WHERE
|
||||
person_id = %d
|
||||
AND daterange(cppch.startdate, cppch.enddate) && daterange(cppch2.startdate, cppch2.enddate)
|
||||
)
|
||||
$centerHistoriesA = $this->em->getRepository(Person\PersonCenterHistory::class)->findBy(['person' => $from->getId()]);
|
||||
// $centerHistoriesA = $from->getCenterHistory()->toArray();
|
||||
// var_dump($centerHistoriesA);
|
||||
$datesArrayA = array_map(fn($centerHistory) => $centerHistory->getStartDate(), $centerHistoriesA);
|
||||
if (!count($datesArrayA) === 0) {
|
||||
$oldestDateA = min($datesArrayA);
|
||||
}
|
||||
|
||||
$centerHistoriesB = $this->em->getRepository(Person\PersonCenterHistory::class)->findBy(['person' => $to->getId()]);
|
||||
// $centerHistoriesB = $to->getCenterHistory()->toArray();
|
||||
// var_dump($centerHistoriesB);
|
||||
$datesArrayB = array_map(fn($centerHistory) => $centerHistory->getStartDate(), $centerHistoriesB);
|
||||
|
||||
if (!count($datesArrayB) === 0) {
|
||||
$oldestDateB = min($datesArrayB);
|
||||
|
||||
$indexOldestCenter = array_search(min($centerHistoriesB), $centerHistoriesB);
|
||||
$oldestCenterHistoryB = $centerHistoriesB[$indexOldestCenter];
|
||||
}
|
||||
|
||||
$sqlDelete = sprintf("delete FROM chill_person_person_center_history WHERE person_id = %d", $from->getId());
|
||||
|
||||
$sqlStatements = [$sqlDelete];
|
||||
|
||||
if ($oldestDateA <= $oldestDateB) {
|
||||
$sqlInsert = sprintf("update chill_person_person_center_history set startDate = '%s'::date WHERE id = %d", $oldestDateA->format('Y-m-d'), $oldestCenterHistoryB->getId());
|
||||
|
||||
$sqlStatements = [$sqlInsert, $sqlDelete];
|
||||
}
|
||||
|
||||
return $sqlStatements;
|
||||
|
||||
SQL, $to->getId());
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user