Skip to content

Commit

Permalink
autoaffectationrule remplace 'all' and 'partner_list' options by null #…
Browse files Browse the repository at this point in the history
  • Loading branch information
hmeneuvrier committed Jan 23, 2025
1 parent 6df57f0 commit d4447ff
Show file tree
Hide file tree
Showing 16 changed files with 108 additions and 360 deletions.
2 changes: 0 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
"knplabs/knp-snappy-bundle": "^1.9",
"league/flysystem-aws-s3-v3": "^3.0",
"league/flysystem-bundle": "^3.0",
"longitude-one/wkt-parser": "^3.0",
"mjaschen/phpgeo": "^5.0",
"nelmio/cors-bundle": "^2.2",
"phpdocumentor/reflection-docblock": "^5.3",
"phpoffice/phpspreadsheet": "^2.2",
Expand Down
18 changes: 9 additions & 9 deletions src/DataFixtures/Files/AutoAffectationRule.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ auto_affectation_rules:
status: "ACTIVE"
profile_declarant: "all"
partner_type: "COMMUNE_SCHS"
insee_to_include: "partner_list"
insee_to_include: null
insee_to_exclude: null
parc: "all"
allocataire: "all"
Expand All @@ -13,7 +13,7 @@ auto_affectation_rules:
status: "ACTIVE"
profile_declarant: "all"
partner_type: "COMMUNE_SCHS"
insee_to_include: "partner_list"
insee_to_include: null
insee_to_exclude: null
parc: "prive"
allocataire: "all"
Expand All @@ -22,7 +22,7 @@ auto_affectation_rules:
status: "ACTIVE"
profile_declarant: "all"
partner_type: "CAF_MSA"
insee_to_include: "all"
insee_to_include: null
insee_to_exclude: null
parc: "prive"
allocataire: "caf"
Expand All @@ -40,7 +40,7 @@ auto_affectation_rules:
status: "ACTIVE"
profile_declarant: "LOCATAIRE"
partner_type: "EPCI"
insee_to_include: "partner_list"
insee_to_include: null
insee_to_exclude:
- "34048"
parc: "prive"
Expand All @@ -50,7 +50,7 @@ auto_affectation_rules:
status: "ARCHIVED"
profile_declarant: "all"
partner_type: "EPCI"
insee_to_include: "partner_list"
insee_to_include: null
insee_to_exclude: null
parc: "all"
allocataire: "all"
Expand All @@ -59,7 +59,7 @@ auto_affectation_rules:
status: "ACTIVE"
profile_declarant: "all"
partner_type: "POLICE_GENDARMERIE"
insee_to_include: "all"
insee_to_include: null
insee_to_exclude: null
parc: "all"
allocataire: "all"
Expand All @@ -70,7 +70,7 @@ auto_affectation_rules:
status: "ACTIVE"
profile_declarant: "all"
partner_type: "COMMUNE_SCHS"
insee_to_include: "partner_list"
insee_to_include: null
insee_to_exclude: null
parc: "all"
allocataire: "all"
Expand All @@ -79,7 +79,7 @@ auto_affectation_rules:
status: "ARCHIVED"
profile_declarant: "all"
partner_type: "EPCI"
insee_to_include: "partner_list"
insee_to_include: null
insee_to_exclude: null
parc: "all"
allocataire: "all"
Expand All @@ -88,7 +88,7 @@ auto_affectation_rules:
status: "ACTIVE"
profile_declarant: "all"
partner_type: "BAILLEUR_SOCIAL"
insee_to_include: "all"
insee_to_include: null
insee_to_exclude: null
parc: "public"
allocataire: "all"
Expand Down
16 changes: 6 additions & 10 deletions src/Entity/AutoAffectationRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,10 @@ enumType: PartnerType::class,
#[AppAssert\ValidProfileDeclarant()]
private string $profileDeclarant;

#[ORM\Column(length: 255, options: ['comment' => 'Value possible all, partner_list or an array of code insee'])]
#[Assert\NotBlank(message: 'Merci de renseigner les code insee des communes concernées.')]
#[ORM\Column(nullable: true, length: 255, options: ['comment' => 'Value possible null or an array of code insee'])]
#[Assert\Length(max: 255)]
#[AppAssert\InseeToInclude()]
private string $inseeToInclude;
private ?string $inseeToInclude;

#[ORM\Column(nullable: true, options: ['comment' => 'Value possible null or an array of code insee'])]
#[AppAssert\InseeToExclude()]
Expand Down Expand Up @@ -144,12 +143,12 @@ public function setProfileDeclarant(string $profileDeclarant): self
return $this;
}

public function getInseeToInclude(): string
public function getInseeToInclude(): ?string
{
return $this->inseeToInclude;
}

public function setInseeToInclude(string $inseeToInclude): self
public function setInseeToInclude(?string $inseeToInclude): self
{
$this->inseeToInclude = $inseeToInclude;

Expand Down Expand Up @@ -292,14 +291,11 @@ public function getDescription(bool $isShort = true): string

$description .= ' Elle s\'applique ';
switch ($this->getInseeToInclude()) {
case 'all':
$description .= 'à tous les logements du territoire';
break;
case 'partner_list':
case null:
$description .= 'aux logements situés dans le périmètre géographique du partenaire (codes insee et/ou zones)';
break;
default:
$description .= 'aux logements situés dans les communes aux codes insee suivants : '.$this->getInseeToInclude();
$description .= 'aux logements situés dans le périmètre géographique du partenaire (codes insee et/ou zones), limités aux codes insee suivants : '.$this->getInseeToInclude();
break;
}
if ($this->getInseeToExclude()) {
Expand Down
6 changes: 3 additions & 3 deletions src/Form/AutoAffectationRuleType.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,12 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
],
])
->add('inseeToInclude', TextType::class, [
'label' => 'Périmètre géographique à inclure',
'label' => 'Périmètre géographique à inclure (facultatif)',
'attr' => [
'class' => 'fr-input',
],
'required' => true,
'help' => 'Valeurs possibles : "all", "partner_list" (codes insee, et zones) ou une liste de codes insee séparés par des virgules.',
'required' => false,
'help' => 'Une liste de codes insee séparés par des virgules.',
'help_attr' => [
'class' => 'fr-hint-text',
],
Expand Down
51 changes: 43 additions & 8 deletions src/Repository/PartnerRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,24 +178,58 @@ public function findFilteredArchivedPaginated(SearchArchivedPartner $searchArchi
* @throws Exception
*/
public function findByLocalization(Signalement $signalement, bool $affected = true): array
{
$queryData = $this->buildLocalizationQuery($signalement, $affected);

$resultSet = $this->getEntityManager()->getConnection()->executeQuery(
$queryData['sql'],
$queryData['params']
);

return $resultSet->fetchAllAssociative();
}

/**
* @throws Exception
*/
public function findPartnersByLocalization(Signalement $signalement): array
{
$queryData = $this->buildLocalizationQuery($signalement, false); // Always use $affected = false

$resultSet = $this->getEntityManager()->getConnection()->executeQuery(
$queryData['sql'],
$queryData['params']
);
$partnerIds = array_column($resultSet->fetchAllAssociative(), 'id');

return $this->getEntityManager()->getRepository(Partner::class)->findBy(['id' => $partnerIds]);
}

/**
* Builds the SQL query and parameters for localization search.
*
* @throws Exception
*/
private function buildLocalizationQuery(Signalement $signalement, bool $affected): array
{
$operator = $affected ? 'IN' : 'NOT IN';

$subquery = $this->getEntityManager()->getRepository(Affectation::class)->createQueryBuilder('a')
->select('IDENTITY(a.partner)')
->where('a.signalement = :signalement')
->setParameter('signalement', $signalement);
->select('IDENTITY(a.partner)')
->where('a.signalement = :signalement')
->setParameter('signalement', $signalement);

$affectedPartners = $subquery->getQuery()->getSingleColumnResult();
$conn = $this->getEntityManager()->getConnection();

$params = [
'territory' => $signalement->getTerritory()->getId(),
'insee' => '%'.$signalement->getInseeOccupant().'%',
'lng' => $signalement->getGeoloc()['lng'] ?? 'notInZone',
'lat' => $signalement->getGeoloc()['lat'] ?? 'notInZone',
];

$clauseSubquery = '';
if (\count($affectedPartners) || 'IN' == $operator) {
if (\count($affectedPartners) || 'IN' === $operator) {
if (0 === \count($affectedPartners)) {
$clauseSubquery = 'AND p.id '.$operator.' (null)';
} else {
Expand Down Expand Up @@ -237,9 +271,10 @@ public function findByLocalization(Signalement $signalement, bool $affected = tr
'.$clauseSubquery.'
ORDER BY p.nom ASC';

$resultSet = $conn->executeQuery($sql, $params);

return $resultSet->fetchAllAssociative();
return [
'sql' => $sql,
'params' => $params,
];
}

public function findPartnersWithQualification(Qualification $qualification, ?Territory $territory)
Expand Down
4 changes: 3 additions & 1 deletion src/Service/Signalement/AutoAssigner.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use App\Manager\SuiviManager;
use App\Manager\UserManager;
use App\Messenger\InterconnectionBus;
use App\Repository\PartnerRepository;
use App\Specification\Affectation\AllocataireSpecification;
use App\Specification\Affectation\CodeInseeSpecification;
use App\Specification\Affectation\ParcSpecification;
Expand All @@ -37,6 +38,7 @@ public function __construct(
private UserManager $userManager,
private ParameterBagInterface $parameterBag,
private InterconnectionBus $interconnectionBus,
private PartnerRepository $partnerRepository,
private LoggerInterface $logger,
) {
}
Expand All @@ -62,7 +64,7 @@ public function assign(Signalement $signalement): void
}
$adminEmail = $this->parameterBag->get('user_system_email');
$adminUser = $this->userManager->findOneBy(['email' => $adminEmail]);
$partners = $signalement->getTerritory()->getPartners();
$partners = $this->partnerRepository->findPartnersByLocalization($signalement);
$assignablePartners = [];

/** @var AutoAffectationRule $rule */
Expand Down
Loading

0 comments on commit d4447ff

Please sign in to comment.