fixes on address valid from edit for household

This commit is contained in:
2022-04-14 00:02:52 +02:00
parent 405694a0b4
commit 86ec020f80
6 changed files with 67 additions and 58 deletions

View File

@@ -12,14 +12,17 @@ declare(strict_types=1);
namespace Chill\PersonBundle\Controller;
use Chill\MainBundle\Entity\Address;
use Chill\MainBundle\Form\Type\AddressDateType;
use Chill\MainBundle\Form\Type\AddressType;
use Chill\PersonBundle\Entity\Household\Household;
use Chill\PersonBundle\Form\HouseholdType;
use Chill\PersonBundle\Repository\Household\PositionRepository;
use Chill\PersonBundle\Security\Authorization\AccompanyingPeriodVoter;
use Chill\PersonBundle\Security\Authorization\HouseholdVoter;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\Exception\BadRequestException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Security;
@@ -197,41 +200,35 @@ class HouseholdController extends AbstractController
*/
public function addressValidFromEdit(Request $request, Household $household)
{
// TODO ACL
$this->denyAccessUnlessGranted(HouseholdVoter::EDIT, $household);
$address_id = $request->query->get('address_id');
$address = $this->getDoctrine()->getManager()
->getRepository(Address::class)
->find($address_id);
if (!$request->query->has('address_id')) {
throw new BadRequestException('parameter address_id is missing');
}
$form = $this->createForm(AddressType::class, $address, []);
$address_id = $request->query->getInt('address_id');
$form->handleRequest($request);
$addresses = $household->getAddressesOrdered();
foreach ($addresses as $a) {
if ($a->getId() === $address->getId()) {
$currentValueIndex = array_search($a, $addresses, true);
// loop over adresses of the household, to be sure that the household is associated
// to the edited address
$address = null;
foreach ($household->getAddresses() as $householdAddress) {
if ($address_id === $householdAddress->getId()) {
$address = $householdAddress;
}
}
if (count($addresses) > 1 && 0 < $currentValueIndex) {
$previousAddress = $addresses[$currentValueIndex - 1];
$minValidFrom = $previousAddress->getValidTo();
} else {
$minValidFrom = null;
if (null === $address) {
throw new BadRequestException('The edited address does not belongs to the household');
}
if (count($addresses) > 1 && count($addresses) > $currentValueIndex + 1) {
$nextAddress = $addresses[$currentValueIndex + 1];
$maxValidFrom = $nextAddress->getValidFrom();
} else {
$maxValidFrom = null;
}
$form = $this->createForm(AddressDateType::class, $address, []);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->getDoctrine()->getManager()->persist($address);
$household->makeAddressConsistent();
$this->getDoctrine()->getManager()->flush();
return $this->redirectToRoute('chill_person_household_addresses', [
@@ -245,8 +242,6 @@ class HouseholdController extends AbstractController
'household' => $household,
'address' => $address,
'form' => $form->createView(),
'minValidFrom' => $minValidFrom,
'maxValidFrom' => $maxValidFrom,
]
);
}