Merge remote-tracking branch 'origin/master' into features/create-social-action

This commit is contained in:
2021-06-21 14:38:09 +02:00
22 changed files with 628 additions and 198 deletions

View File

@@ -30,7 +30,7 @@ class Household
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
* @Serializer\Groups({"write"})
* @Serializer\Groups({"read"})
*/
private ?int $id = null;
@@ -196,6 +196,11 @@ class Household
}
public function getCurrentMembers(?\DateTimeImmutable $now = null): Collection
{
return $this->getMembers()->matching($this->buildCriteriaCurrentMembers($now));
}
private function buildCriteriaCurrentMembers(?\DateTimeImmutable $now = null): Criteria
{
$criteria = new Criteria();
$expr = Criteria::expr();
@@ -211,7 +216,53 @@ class Household
$expr->gte('endDate', $date)
));
return $this->getMembers()->matching($criteria);
return $criteria;
}
/**
* @return HouseholdMember[]
*/
public function getCurrentMembersOrdered(?\DateTimeImmutable $now = null): Collection
{
$members = $this->getCurrentMembers($now);
$members->getIterator()
->uasort(
function (HouseholdMember $a, HouseholdMember $b) {
if ($a->getPosition()->getOrdering() < $b->getPosition()->getOrdering()) {
return -1;
}
if ($a->getPosition()->getOrdering() > $b->getPosition()->getOrdering()) {
return 1;
}
if ($a->isHolder() && !$b->isHolder()) {
return 1;
}
if (!$a->isHolder() && $b->isHolder()) {
return -1;
}
return 0;
}
);
return $members;
}
/**
* get current members ids
*
* Used in serialization
*
* @Serializer\Groups({"read"})
* @Serializer\SerializedName("current_members_id")
*
*/
public function getCurrentMembersIds(?\DateTimeImmutable $now = null): Collection
{
return $this->getCurrentMembers($now)->map(
fn (HouseholdMember $m) => $m->getId()
);
}
/**

View File

@@ -1264,15 +1264,24 @@ class Person implements HasCenterInterface
}
public function getCurrentHousehold(?\DateTimeImmutable $at = null): ?Household
{
$participation = $this->getCurrentHouseholdParticipationShareHousehold($at);
return $participation instanceof HouseholdMember ?
$participation->getHousehold()
: null;
}
public function getCurrentHouseholdParticipationShareHousehold(?\DateTimeImmutable $at = null): ?HouseholdMember
{
$criteria = new Criteria();
$expr = Criteria::expr();
$date = NULL === $at ? new \DateTimeImmutable('now') : $at;
$date = NULL === $at ? new \DateTimeImmutable('today') : $at;
$datef = $date->format('Y-m-d');
if (
NULL !== ($this->currentHouseholdAt[$datef] ?? NULL)) {
return $this->currentHouseholdAt[$datef];
NULL !== ($this->currentHouseholdParticipationAt[$datef] ?? NULL)) {
return $this->currentHouseholdParticipationAt[$datef];
}
$criteria
@@ -1281,7 +1290,7 @@ class Person implements HasCenterInterface
$expr->lte('startDate', $date),
$expr->orX(
$expr->isNull('endDate'),
$expr->gte('endDate', $date)
$expr->gt('endDate', $date)
),
$expr->eq('shareHousehold', true)
)
@@ -1292,8 +1301,7 @@ class Person implements HasCenterInterface
;
return $participations->count() > 0 ?
$this->currentHouseholdAt[$datef] = $participations->first()
->getHousehold()
$this->currentHouseholdParticipationAt[$datef] = $participations->first()
: null;
}