From bad5506b986fa96792862f0687a698c7bddc18f4 Mon Sep 17 00:00:00 2001 From: nobohan Date: Tue, 19 Apr 2022 18:01:25 +0200 Subject: [PATCH] person: create a person with address (and household without position (remove required position for household member) --- .../Entity/Household/HouseholdMember.php | 9 +- .../Household/MembersEditor.php | 111 +++++++++--------- .../public/vuejs/_components/AddPersons.vue | 6 +- .../vuejs/_components/OnTheFly/Person.vue | 1 + .../Resources/public/vuejs/_js/i18n.js | 3 +- .../Resources/views/Person/create.html.twig | 1 + .../Normalizer/MembersEditorNormalizer.php | 18 ++- .../translations/messages.fr.yml | 1 + 8 files changed, 80 insertions(+), 70 deletions(-) diff --git a/src/Bundle/ChillPersonBundle/Entity/Household/HouseholdMember.php b/src/Bundle/ChillPersonBundle/Entity/Household/HouseholdMember.php index 22c33f85f..4b71ad560 100644 --- a/src/Bundle/ChillPersonBundle/Entity/Household/HouseholdMember.php +++ b/src/Bundle/ChillPersonBundle/Entity/Household/HouseholdMember.php @@ -82,14 +82,13 @@ class HouseholdMember /** * @ORM\ManyToOne(targetEntity=Position::class) * @Serializer\Groups({"read", "docgen:read"}) - * @Assert\NotNull(groups={"household_memberships_created"}) */ private ?Position $position = null; /** * @ORM\Column(type="boolean", name="sharedhousehold") */ - private bool $shareHousehold = false; + private bool $shareHousehold = true; /** * @ORM\Column(type="date_immutable", nullable=true, options={"default": null}) @@ -201,7 +200,7 @@ class HouseholdMember return $this; } - public function setPosition(Position $position): self + public function setPosition(?Position $position): self { if ($this->position instanceof Position) { throw new LogicException('The position is already set. You cannot change ' . @@ -209,7 +208,9 @@ class HouseholdMember } $this->position = $position; - $this->shareHousehold = $position->getShareHousehold(); + if (null !== $position){ + $this->shareHousehold = $position->getShareHousehold(); + } return $this; } diff --git a/src/Bundle/ChillPersonBundle/Household/MembersEditor.php b/src/Bundle/ChillPersonBundle/Household/MembersEditor.php index 1c7cbb538..01d6d7e9c 100644 --- a/src/Bundle/ChillPersonBundle/Household/MembersEditor.php +++ b/src/Bundle/ChillPersonBundle/Household/MembersEditor.php @@ -55,7 +55,7 @@ class MembersEditor $this->eventDispatcher = $eventDispatcher; } - public function addMovement(DateTimeImmutable $date, Person $person, Position $position, ?bool $holder = false, ?string $comment = null): self + public function addMovement(DateTimeImmutable $date, Person $person, ?Position $position, ?bool $holder = false, ?string $comment = null): self { if (null === $this->household) { throw new LogicException('You must define a household first'); @@ -69,72 +69,75 @@ class MembersEditor ->setComment($comment); $this->household->addMember($membership); - if ($position->getShareHousehold()) { - // launch event only if moving to a "share household" position, - // and if the destination household is different than the previous one - $event = new PersonAddressMoveEvent($person); - $event->setNextMembership($membership); + if (null !== $position) { + if ($position->getShareHousehold()) { + // launch event only if moving to a "share household" position, + // and if the destination household is different than the previous one + $event = new PersonAddressMoveEvent($person); + $event->setNextMembership($membership); - $counter = 0; + $counter = 0; - foreach ($person->getHouseholdParticipationsShareHousehold() as $participation) { - if ($participation === $membership) { - continue; + foreach ($person->getHouseholdParticipationsShareHousehold() as $participation) { + if ($participation === $membership) { + continue; + } + + if ($participation->getStartDate() > $membership->getStartDate()) { + continue; + } + + ++$counter; + + if ($participation->getEndDate() === null || $participation->getEndDate() > $date) { + $participation->setEndDate($date); + $this->membershipsAffected[] = $participation; + $this->oldMembershipsHashes[] = spl_object_hash($participation); + + if ($participation->getHousehold() !== $this->household) { + $event->setPreviousMembership($participation); + $this->events[] = $event; + } + } } - if ($participation->getStartDate() > $membership->getStartDate()) { - continue; + // send also the event if there was no participation before + if (0 === $counter) { + $this->events[] = $event; } - ++$counter; + foreach ($person->getHouseholdParticipationsNotShareHousehold() as $participation) { + if ($participation->getHousehold() === $this->household + && $participation->getEndDate() === null || $participation->getEndDate() > $membership->getStartDate() + && $participation->getStartDate() <= $membership->getStartDate() + ) { + $participation->setEndDate($membership->getStartDate()); + } + } + } else { + // if a members is moved to the same household than the one he belongs to, + // we should make it leave the household + if ($person->getCurrentHousehold($date) === $this->household) { + $this->leaveMovement($date, $person); + } - if ($participation->getEndDate() === null || $participation->getEndDate() > $date) { - $participation->setEndDate($date); - $this->membershipsAffected[] = $participation; - $this->oldMembershipsHashes[] = spl_object_hash($participation); + // if there are multiple belongings not sharing household, close the others + foreach ($person->getHouseholdParticipationsNotShareHousehold() as $participation) { + if ($participation === $membership) { + continue; + } - if ($participation->getHousehold() !== $this->household) { - $event->setPreviousMembership($participation); - $this->events[] = $event; + if ($participation->getHousehold() === $this->household + && ($participation->getEndDate() === null || $participation->getEndDate() > $membership->getStartDate()) + && $participation->getStartDate() <= $membership->getStartDate() + ) { + $participation->setEndDate($membership->getStartDate()); } } } - - // send also the event if there was no participation before - if (0 === $counter) { - $this->events[] = $event; - } - - foreach ($person->getHouseholdParticipationsNotShareHousehold() as $participation) { - if ($participation->getHousehold() === $this->household - && $participation->getEndDate() === null || $participation->getEndDate() > $membership->getStartDate() - && $participation->getStartDate() <= $membership->getStartDate() - ) { - $participation->setEndDate($membership->getStartDate()); - } - } - } else { - // if a members is moved to the same household than the one he belongs to, - // we should make it leave the household - if ($person->getCurrentHousehold($date) === $this->household) { - $this->leaveMovement($date, $person); - } - - // if there are multiple belongings not sharing household, close the others - foreach ($person->getHouseholdParticipationsNotShareHousehold() as $participation) { - if ($participation === $membership) { - continue; - } - - if ($participation->getHousehold() === $this->household - && ($participation->getEndDate() === null || $participation->getEndDate() > $membership->getStartDate()) - && $participation->getStartDate() <= $membership->getStartDate() - ) { - $participation->setEndDate($membership->getStartDate()); - } - } } + $this->membershipsAffected[] = $membership; $this->persistables[] = $membership; diff --git a/src/Bundle/ChillPersonBundle/Resources/public/vuejs/_components/AddPersons.vue b/src/Bundle/ChillPersonBundle/Resources/public/vuejs/_components/AddPersons.vue index 3c1cd52b1..0d50b368c 100644 --- a/src/Bundle/ChillPersonBundle/Resources/public/vuejs/_components/AddPersons.vue +++ b/src/Bundle/ChillPersonBundle/Resources/public/vuejs/_components/AddPersons.vue @@ -302,14 +302,10 @@ export default { 'type': 'person', 'id': responsePerson.id }, - 'position': { - "type": "household_position", - "id": 4 //TODO, which position? - }, 'start_date': { 'datetime': `${new Date().toISOString().split('T')[0]}T00:00:00+02:00` }, - 'holder': false, //TODO true or false? + 'holder': false, 'comment': null } ], diff --git a/src/Bundle/ChillPersonBundle/Resources/public/vuejs/_components/OnTheFly/Person.vue b/src/Bundle/ChillPersonBundle/Resources/public/vuejs/_components/OnTheFly/Person.vue index 3f8fd1043..27657ff46 100644 --- a/src/Bundle/ChillPersonBundle/Resources/public/vuejs/_components/OnTheFly/Person.vue +++ b/src/Bundle/ChillPersonBundle/Resources/public/vuejs/_components/OnTheFly/Person.vue @@ -146,6 +146,7 @@
+

{{ $t('person.address.warning') }}

+

{{ 'A new household will be created. The person will be member of this household.'|trans }}

{{ form_row(form.address) }}
diff --git a/src/Bundle/ChillPersonBundle/Serializer/Normalizer/MembersEditorNormalizer.php b/src/Bundle/ChillPersonBundle/Serializer/Normalizer/MembersEditorNormalizer.php index 757a0bfcb..6e1f9da6c 100644 --- a/src/Bundle/ChillPersonBundle/Serializer/Normalizer/MembersEditorNormalizer.php +++ b/src/Bundle/ChillPersonBundle/Serializer/Normalizer/MembersEditorNormalizer.php @@ -116,12 +116,18 @@ class MembersEditorNormalizer implements DenormalizerAwareInterface, Denormalize $format, $context ); - $position = $this->denormalizer->denormalize( - $concerned['position'] ?? null, - Position::class, - $format, - $context - ); + + if (\array_key_exists('position', $concerned)) { + $position = $this->denormalizer->denormalize( + $concerned['position'] ?? null, + Position::class, + $format, + $context + ); + } else { + $position = null; + } + $startDate = $this->denormalizer->denormalize( $concerned['start_date'] ?? null, DateTimeImmutable::class, diff --git a/src/Bundle/ChillPersonBundle/translations/messages.fr.yml b/src/Bundle/ChillPersonBundle/translations/messages.fr.yml index 2059b11dd..b24a5e064 100644 --- a/src/Bundle/ChillPersonBundle/translations/messages.fr.yml +++ b/src/Bundle/ChillPersonBundle/translations/messages.fr.yml @@ -87,6 +87,7 @@ choose civility: -- All genders: tous les genres Any person selected: Aucune personne sélectionnée Create a household and add an address: Créer un ménage et ajouter une adresse +A new household will be created. The person will be member of this household.: Un nouveau ménage va être créé. L'usager sera membre de ce ménage. # dédoublonnage Old person: Doublon