Merge remote-tracking branch 'origin/master' into features/household-validation

This commit is contained in:
2021-06-18 12:02:34 +02:00
83 changed files with 3539 additions and 647 deletions

View File

@@ -7,6 +7,8 @@ use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\Criteria;
use Symfony\Component\Serializer\Annotation as Serializer;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Chill\MainBundle\Entity\Address;
use Chill\PersonBundle\Entity\Household\HouseholdMember;
use Chill\PersonBundle\Validator\Constraints\Household\MaxHolder;
@@ -28,7 +30,7 @@ class Household
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
* @Serializer\Groups({"read"})
* @Serializer\Groups({"write"})
*/
private ?int $id = null;
@@ -40,6 +42,7 @@ class Household
* cascade={"persist", "remove", "merge", "detach"})
* @ORM\JoinTable(name="chill_person_household_to_addresses")
* @ORM\OrderBy({"validFrom" = "DESC"})
* @Serializer\Groups({"write"})
*/
private Collection $addresses;
@@ -48,7 +51,7 @@ class Household
* targetEntity=HouseholdMember::class,
* mappedBy="household"
* )
* @Serializer\Groups({"read"})
* @Serializer\Groups({"write"})
*/
private Collection $members;
@@ -87,6 +90,11 @@ class Household
{
$this->addresses[] = $address;
foreach ($this->getAddresses() as $a) {
if ($a->getValidFrom() < $address->getValidFrom() && $a->getValidTo() === NULL) {
$a->setValidTo($address->getValidFrom());
}
}
return $this;
}
@@ -102,6 +110,7 @@ class Household
* By default, the addresses are ordered by date, descending (the most
* recent first)
*
* @Assert\Callback(methods={"validate"})
* @return \Chill\MainBundle\Entity\Address[]
*/
public function getAddresses()
@@ -294,4 +303,18 @@ class Household
return $this;
}
public function validate(ExecutionContextInterface $context, $payload)
{
$addresses = $this->getAddresses();
$cond = True;
for ($i=0; $i < count($addresses) - 1; $i++) {
if ($addresses[$i]->getValidFrom() != $addresses[$i + 1]->getValidTo()) {
$cond = False;
$context->buildViolation('The address are not sequentials. The validFrom date of one address should be equal to the validTo date of the previous address.')
->atPath('addresses')
->addViolation();
}
}
dump($cond);
}
}