add link to household or household editor

This commit is contained in:
Julien Fastré 2021-06-10 15:17:15 +02:00
parent 3e39dd9a1e
commit 38c06977ca
8 changed files with 128 additions and 9 deletions

View File

@ -72,6 +72,15 @@ class HouseholdMemberController extends ApiController
} }
/** /**
* Route for showing an editor to leave a household.
*
* Possibles arguments are:
*
* * persons[]: an id of the person to add to the form
* * household: the id of the destination household
* * allow_leave_without_household: if present, the editor will allow
* to leave household without joining another
*
* @Route( * @Route(
* "/{_locale}/person/household/members/editor", * "/{_locale}/person/household/members/editor",
* name="chill_person_household_members_editor" * name="chill_person_household_members_editor"

View File

@ -102,8 +102,7 @@ class Household
{ {
$criteria = new Criteria(); $criteria = new Criteria();
$expr = Criteria::expr(); $expr = Criteria::expr();
$date = $now === null ? (new \DateTimeImmutable('now')) : $now; $date = $now === null ? (new \DateTimeImmutable('today')) : $now;
$criteria $criteria
->where($expr->orX( ->where($expr->orX(
@ -135,7 +134,7 @@ class Household
{ {
$criteria = new Criteria(); $criteria = new Criteria();
$expr = Criteria::expr(); $expr = Criteria::expr();
$date = $now === null ? (new \DateTimeImmutable('now')) : $now; $date = $now === null ? (new \DateTimeImmutable('today')) : $now;
$criteria $criteria
->where( ->where(

View File

@ -50,9 +50,9 @@ class HouseholdMember
private ?string $comment = NULL; private ?string $comment = NULL;
/** /**
* @ORM\Column(type="boolean") * @ORM\Column(type="boolean", name="sharedhousehold")
*/ */
private bool $sharedHousehold = false; private bool $shareHousehold = false;
/** /**
* @ORM\Column(type="boolean", options={"default": false}) * @ORM\Column(type="boolean", options={"default": false})
@ -98,7 +98,7 @@ class HouseholdMember
} }
$this->position = $position; $this->position = $position;
$this->sharedHousehold = $position->getShareHousehold(); $this->shareHousehold = $position->getShareHousehold();
return $this; return $this;
} }
@ -144,7 +144,7 @@ class HouseholdMember
*/ */
public function getShareHousehold(): ?bool public function getShareHousehold(): ?bool
{ {
return $this->sharedHousehold; return $this->shareHousehold;
} }

View File

@ -25,6 +25,7 @@ namespace Chill\PersonBundle\Entity;
use ArrayIterator; use ArrayIterator;
use Chill\MainBundle\Entity\Center; use Chill\MainBundle\Entity\Center;
use Chill\MainBundle\Entity\Country; use Chill\MainBundle\Entity\Country;
use Chill\PersonBundle\Entity\Household\Household;
use Chill\PersonBundle\Entity\MaritalStatus; use Chill\PersonBundle\Entity\MaritalStatus;
use Chill\PersonBundle\Entity\Household\HouseholdMember; use Chill\PersonBundle\Entity\Household\HouseholdMember;
use Chill\MainBundle\Entity\HasCenterInterface; use Chill\MainBundle\Entity\HasCenterInterface;
@ -281,6 +282,11 @@ class Person implements HasCenterInterface
*/ */
private Collection $householdParticipations; private Collection $householdParticipations;
/**
* Cache the computation of household
*/
private array $currentHouseholdAt = [];
/** /**
* Person constructor. * Person constructor.
* *
@ -1202,4 +1208,43 @@ class Person implements HasCenterInterface
{ {
return $this->householdParticipations; return $this->householdParticipations;
} }
public function getCurrentHousehold(?\DateTimeImmutable $at = null): ?Household
{
$criteria = new Criteria();
$expr = Criteria::expr();
$date = NULL === $at ? new \DateTimeImmutable('now') : $at;
$datef = $date->format('Y-m-d');
if (
NULL !== ($this->currentHouseholdAt[$datef] ?? NULL)) {
return $this->currentHouseholdAt[$datef];
}
$criteria
->where(
$expr->andX(
$expr->lte('startDate', $date),
$expr->orX(
$expr->isNull('endDate'),
$expr->gte('endDate', $date)
),
$expr->eq('shareHousehold', true)
)
);
$participations = $this->getHouseholdParticipations()
->matching($criteria)
;
return $participations->count() > 0 ?
$this->currentHouseholdAt[$datef] = $participations->first()
->getHousehold()
: null;
}
public function isSharingHousehold(?\DateTimeImmutable $at = null): bool
{
return NULL !== $this->getCurrentHousehold($at);
}
} }

View File

@ -62,6 +62,19 @@
<li> <li>
<a href="{{ path('chill_person_view', { person_id: p.person.id }) }}" class="sc-button bt-show" target="_blank" title="Voir"></a> <a href="{{ path('chill_person_view', { person_id: p.person.id }) }}" class="sc-button bt-show" target="_blank" title="Voir"></a>
</li> </li>
{% if p.person.isSharingHousehold %}
<li>
<a
href="{{ chill_path_add_return_path(
'chill_person_household_summary',
{ 'household_id': p.person.getCurrentHousehold.id }
) }}"
class="sc-button">
<i class="fa fa-home"></i>
{{ 'household.Household file'|trans }}
</a>
</li>
{% endif %}
</ul> </ul>
</div> </div>
</div> </div>

View File

@ -48,7 +48,18 @@
</a> </a>
</li> </li>
<li> <li>
<a href="#" class="sc-button" /><i class="fa fa-sign-out"></i>{{ 'household.Leave'|trans }}</a> <a
href="{{ chill_path_add_return_path(
'chill_person_household_members_editor',
{
'persons': [ m.person.id ],
'allow_leave_without_household': true
} ) }}"
class="sc-button"
/>
<i class="fa fa-sign-out"></i>
{{ 'household.Leave'|trans }}
</a>
</li> </li>
</ul> </ul>
</div> </div>
@ -129,7 +140,9 @@
<ul class="record_actions sticky-form-buttons"> <ul class="record_actions sticky-form-buttons">
<li> <li>
<a class="sc-button bt-create"> <a
href="{{ chill_path_add_return_path('chill_person_household_members_editor', {'household': household.id }) }}"
class="sc-button bt-create">
{{ 'household.Add a member'|trans }} {{ 'household.Add a member'|trans }}
</a> </a>
</li> </li>

View File

@ -22,6 +22,9 @@
namespace Chill\PersonBundle\Tests\Entity; namespace Chill\PersonBundle\Tests\Entity;
use Chill\PersonBundle\Entity\Household\HouseholdMember;
use Chill\PersonBundle\Entity\Household\Position;
use Chill\PersonBundle\Entity\Household\Household;
use Chill\PersonBundle\Entity\Person; use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Entity\AccompanyingPeriod; use Chill\PersonBundle\Entity\AccompanyingPeriod;
use Chill\MainBundle\Entity\Address; use Chill\MainBundle\Entity\Address;
@ -205,4 +208,39 @@ class PersonTest extends \PHPUnit\Framework\TestCase
$this::assertEquals($address3, $p->getLastAddress($addressDate3)); $this::assertEquals($address3, $p->getLastAddress($addressDate3));
} }
public function testIsSharingHousehold()
{
$person = new Person();
$household = new Household();
$positionShare = (new Position())
->setShareHousehold(true);
$positionNotShare = (new Position())
->setShareHousehold(false);
$membership1 = (new HouseholdMember())
->setStartDate(new \DateTimeImmutable('10 years ago'))
->setEndDate(new \DateTimeImmutable('5 years ago'))
->setPerson($person)
->setPosition($positionShare)
;
$household->addMember($membership1);
$membership2 = (new HouseholdMember())
->setStartDate(new \DateTimeImmutable('4 years ago'))
->setEndDate(new \DateTimeImmutable('2 years ago'))
->setPerson($person)
->setPosition($positionNotShare)
;
$household->addMember($membership2);
$this->assertEquals(2, $person->getHouseholdParticipations()
->count());
$this->assertFalse($person->isSharingHousehold());
$this->assertTrue($person->isSharingHousehold(
new \DateTimeImmutable('6 years ago')));
$this->assertFalse($person->isSharingHousehold(
new \DateTimeImmutable('3 years ago')));
}
} }

View File

@ -17,6 +17,8 @@ household:
Those members does not share address: Ces usagers ne partagent pas l'adresse du ménage. Those members does not share address: Ces usagers ne partagent pas l'adresse du ménage.
Any persons into this position: Aucune personne n'appartient au ménage à cette position. Any persons into this position: Aucune personne n'appartient au ménage à cette position.
Leave: Quitter le ménage Leave: Quitter le ménage
Household file: Dossier ménage
Add a member: Ajouter un membre
Update membership: Modifier Update membership: Modifier
successfully saved member: Membre enregistré avec succès successfully saved member: Membre enregistré avec succès
Start date: Date de début de l'appartenance au ménage Start date: Date de début de l'appartenance au ménage