Commit 7bc57959 by Damien Moulard

add error case management in case of duplicate flux in db

parent e88447c6
......@@ -66,7 +66,8 @@ class CreateAllocationsFromExistingFlux extends Command
// get cotisations that don't have an allocation set
$fluxRepository = $this->em->getRepository(Flux::class);
$cotisations = $fluxRepository->getTavCotisationsWithoutAllocation($batchAmount);
$processedComplementaryFluxIds = [];
$count = 0;
foreach ($cotisations as $key => $cotis) {
// $this->io->success(json_encode($cotis));
......@@ -83,17 +84,36 @@ class CreateAllocationsFromExistingFlux extends Command
}
$allocation->setAdherent($this->em->getRepository(Adherent::class)->find($adherent_id));
// set montant & complementary flux if exists
$complementaryFlux = $fluxRepository->getRelatedTavAllocationFlux($cotis["id"]);
$amount = floatval($cotis["montant"]);
if ($complementaryFlux["id"] !== null) {
/**
* An error can have occured with online payment leading to a payment being processed twice.
* In this case, two flux with a complementary flux for each of them have been created at the same time.
* Then, getRelatedTavAllocationFlux() will return multiple results, so we need not to process the same flux twice.
*
* So we look for a complementary flux that exists and that's not been processed before
*/
$complementaryFlux = null;
$possibleComplementaryFlux = $fluxRepository->getRelatedTavAllocationFlux($cotis["id"]);
foreach($possibleComplementaryFlux as $f) {
if ($f["id"] !== null && array_search($f["id"], $processedComplementaryFluxIds) === false) {
$complementaryFlux = $f;
break;
}
}
if ($complementaryFlux !== null) {
$allocation->setComplementaryFlux($fluxRepository->find($complementaryFlux["id"]));
// mark as processed
$processedComplementaryFluxIds[] = $complementaryFlux["id"];
// if there is a complementary flux, add or subsctract amount to get full allocation amount
if ($complementaryFlux["type"] == "reversement_cotisation_adherent") {
$amount += floatval($complementaryFlux["montant"]);
} else if ($complementaryFlux["type"] == "prelevement_cotisation_adherent") {
$amount -= floatval($complementaryFlux["montant"]);
}
}
}
$allocation->setAmount($amount);
......
......@@ -480,6 +480,8 @@ class FluxRepository extends ServiceEntityRepository
/**
* Given a TAV cotisation flux, get the potential complementary flux that completes the full TAV allocation.
* Get a flux that's not been already saved as a complementary flux in allocation.
*
* @see App\Entity\Allocation
*
* @param int $fluxCotisId, cotisation flux id
......@@ -497,13 +499,15 @@ class FluxRepository extends ServiceEntityRepository
OR f.adherent_dest_id = fb.adherent_dest_id
)
AND fb.type IN ('prelevement_cotisation_adherent', 'reversement_cotisation_adherent')
WHERE f.id = :fluxId";
LEFT JOIN allocation a ON a.complementary_flux_id = fb.id
WHERE f.id = :fluxId
AND a.id IS NULL";
$statement = $this->connection->prepare($sqlQuery);
$statement->bindValue(':fluxId', $fluxCotisId);
$statement->execute();
$results = $statement->fetchAll();
return $results[0];
return $results;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment