Finalisation réception marchandise, ajout de composant UI et ajout de fixtures (!7)
| Numéro du ticket | Titre du ticket | |------------------|-----------------| | | | ## Description de la PR ## Modification du .env ## Check list - [x] Pas de régression - [ ] TU/TI/TF rédigée - [x] TU/TI/TF OK - [x] CHANGELOG modifié Reviewed-on: #7 Co-authored-by: tristan <tristan@yuno.malio.fr> Co-committed-by: tristan <tristan@yuno.malio.fr>
This commit was merged in pull request #7.
This commit is contained in:
256
src/Command/SeedCommand.php
Normal file
256
src/Command/SeedCommand.php
Normal file
@@ -0,0 +1,256 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Command;
|
||||
|
||||
use App\Entity\Address;
|
||||
use App\Entity\Building;
|
||||
use App\Entity\Carrier;
|
||||
use App\Entity\Driver;
|
||||
use App\Entity\MerchandiseType;
|
||||
use App\Entity\PelletType;
|
||||
use App\Entity\ReceptionType;
|
||||
use App\Entity\Supplier;
|
||||
use App\Entity\Truck;
|
||||
use App\Entity\Vehicle;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
|
||||
#[AsCommand(
|
||||
name: 'app:seed',
|
||||
description: 'Seed reference data (idempotent).'
|
||||
)]
|
||||
class SeedCommand extends Command
|
||||
{
|
||||
public function __construct(
|
||||
private readonly EntityManagerInterface $entityManager
|
||||
) {
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$io = new SymfonyStyle($input, $output);
|
||||
|
||||
$created = 0;
|
||||
$updated = 0;
|
||||
|
||||
$truckRepo = $this->entityManager->getRepository(Truck::class);
|
||||
$carrierRepo = $this->entityManager->getRepository(Carrier::class);
|
||||
$driverRepo = $this->entityManager->getRepository(Driver::class);
|
||||
$vehicleRepo = $this->entityManager->getRepository(Vehicle::class);
|
||||
$merchandiseTypeRepo = $this->entityManager->getRepository(MerchandiseType::class);
|
||||
$pelletTypeRepo = $this->entityManager->getRepository(PelletType::class);
|
||||
$buildingRepo = $this->entityManager->getRepository(Building::class);
|
||||
$receptionTypeRepo = $this->entityManager->getRepository(ReceptionType::class);
|
||||
$addressRepo = $this->entityManager->getRepository(Address::class);
|
||||
$supplierRepo = $this->entityManager->getRepository(Supplier::class);
|
||||
|
||||
$upsertByCode = function (string $entityClass, string $code, callable $apply) use (&$created, &$updated) {
|
||||
$repo = $this->entityManager->getRepository($entityClass);
|
||||
$entity = $repo->findOneBy(['code' => $code]);
|
||||
if (!$entity) {
|
||||
$entity = new $entityClass();
|
||||
++$created;
|
||||
} else {
|
||||
++$updated;
|
||||
}
|
||||
$apply($entity);
|
||||
$this->entityManager->persist($entity);
|
||||
|
||||
return $entity;
|
||||
};
|
||||
|
||||
$upsertByName = function (string $entityClass, string $name, callable $apply) use (&$created, &$updated) {
|
||||
$repo = $this->entityManager->getRepository($entityClass);
|
||||
$entity = $repo->findOneBy(['name' => $name]);
|
||||
if (!$entity) {
|
||||
$entity = new $entityClass();
|
||||
++$created;
|
||||
} else {
|
||||
++$updated;
|
||||
}
|
||||
$apply($entity);
|
||||
$this->entityManager->persist($entity);
|
||||
|
||||
return $entity;
|
||||
};
|
||||
|
||||
$trucks = ['Citerne', 'Porteur'];
|
||||
$citerne = null;
|
||||
$porteur = null;
|
||||
foreach ($trucks as $name) {
|
||||
$truck = $upsertByName(Truck::class, $name, static fn (Truck $truck) => $truck->setName($name));
|
||||
if ('Citerne' === $name) {
|
||||
$citerne = $truck;
|
||||
}
|
||||
if ('Porteur' === $name) {
|
||||
$porteur = $truck;
|
||||
}
|
||||
}
|
||||
|
||||
$carriers = [
|
||||
['name' => 'LIOT', 'code' => 'LIOT'],
|
||||
['name' => 'LUI-MEME', 'code' => 'LUI-MEME'],
|
||||
];
|
||||
$liot = null;
|
||||
foreach ($carriers as $carrierData) {
|
||||
$carrier = $upsertByCode(Carrier::class, $carrierData['code'], static function (Carrier $carrier) use ($carrierData) {
|
||||
$carrier
|
||||
->setName($carrierData['name'])
|
||||
->setCode($carrierData['code'])
|
||||
;
|
||||
});
|
||||
if ('LIOT' === $carrierData['code']) {
|
||||
$liot = $carrier;
|
||||
}
|
||||
}
|
||||
|
||||
if ($liot && $citerne && $porteur) {
|
||||
$drivers = ['Eddy', 'Jean-Christophe', 'Etienne', 'Hersand'];
|
||||
foreach ($drivers as $name) {
|
||||
$driver = $driverRepo->findOneBy(['name' => $name, 'carrier' => $liot]);
|
||||
if (!$driver) {
|
||||
$driver = new Driver();
|
||||
++$created;
|
||||
} else {
|
||||
++$updated;
|
||||
}
|
||||
$driver
|
||||
->setName($name)
|
||||
->setCarrier($liot)
|
||||
;
|
||||
$this->entityManager->persist($driver);
|
||||
}
|
||||
|
||||
$vehicles = [
|
||||
['plate' => 'GH-684-VZ', 'truck' => $citerne],
|
||||
['plate' => 'FW-363-EC', 'truck' => $porteur],
|
||||
['plate' => 'FW-370-EC', 'truck' => $porteur],
|
||||
['plate' => 'FW-375-EC', 'truck' => $porteur],
|
||||
['plate' => 'FY-952-HS', 'truck' => $porteur],
|
||||
];
|
||||
foreach ($vehicles as $vehicleData) {
|
||||
$vehicle = $vehicleRepo->findOneBy(['plate' => $vehicleData['plate']]);
|
||||
if (!$vehicle) {
|
||||
$vehicle = new Vehicle();
|
||||
++$created;
|
||||
} else {
|
||||
++$updated;
|
||||
}
|
||||
$vehicle
|
||||
->setPlate($vehicleData['plate'])
|
||||
->setCarrier($liot)
|
||||
->setTruck($vehicleData['truck'])
|
||||
;
|
||||
$this->entityManager->persist($vehicle);
|
||||
}
|
||||
} else {
|
||||
$io->warning('Transport data not fully available; drivers/vehicles skipped.');
|
||||
}
|
||||
|
||||
$merchandiseTypes = [
|
||||
['label' => 'Foin', 'code' => 'FOIN'],
|
||||
['label' => 'Paille', 'code' => 'PAILLE'],
|
||||
['label' => 'Granule', 'code' => 'GRANULE'],
|
||||
];
|
||||
foreach ($merchandiseTypes as $type) {
|
||||
$upsertByCode(MerchandiseType::class, $type['code'], static function (MerchandiseType $entity) use ($type) {
|
||||
$entity
|
||||
->setLabel($type['label'])
|
||||
->setCode($type['code'])
|
||||
;
|
||||
});
|
||||
}
|
||||
|
||||
$pelletTypes = [
|
||||
['label' => 'JB croissance', 'code' => 'K750'],
|
||||
['label' => 'Genisse herbe', 'code' => 'K500'],
|
||||
['label' => 'Bovistart melasse ferme', 'code' => 'K130'],
|
||||
['label' => 'Bovin mise en forme', 'code' => 'K400'],
|
||||
];
|
||||
foreach ($pelletTypes as $type) {
|
||||
$upsertByCode(PelletType::class, $type['code'], static function (PelletType $entity) use ($type) {
|
||||
$entity
|
||||
->setLabel($type['label'])
|
||||
->setCode($type['code'])
|
||||
;
|
||||
});
|
||||
}
|
||||
|
||||
$buildings = [
|
||||
['label' => 'Bâtiment 1', 'code' => 'B1'],
|
||||
['label' => 'Bâtiment 2', 'code' => 'B2'],
|
||||
['label' => 'Bâtiment 3', 'code' => 'B3'],
|
||||
];
|
||||
foreach ($buildings as $buildingData) {
|
||||
$upsertByCode(Building::class, $buildingData['code'], static function (Building $entity) use ($buildingData) {
|
||||
$entity
|
||||
->setLabel($buildingData['label'])
|
||||
->setCode($buildingData['code'])
|
||||
;
|
||||
});
|
||||
}
|
||||
|
||||
$receptionTypes = [
|
||||
['label' => 'Marchandises', 'code' => 'MARCHANDISES'],
|
||||
['label' => 'Bovins', 'code' => 'BOVINS'],
|
||||
];
|
||||
foreach ($receptionTypes as $type) {
|
||||
$upsertByCode(ReceptionType::class, $type['code'], static function (ReceptionType $entity) use ($type) {
|
||||
$entity
|
||||
->setLabel($type['label'])
|
||||
->setCode($type['code'])
|
||||
;
|
||||
});
|
||||
}
|
||||
|
||||
$address = $addressRepo->findOneBy([
|
||||
'label' => 'LIOT CHATELLERAULT',
|
||||
'postalCode' => '86100',
|
||||
]);
|
||||
if (!$address) {
|
||||
$address = new Address();
|
||||
++$created;
|
||||
} else {
|
||||
++$updated;
|
||||
}
|
||||
$address
|
||||
->setLabel('LIOT CHATELLERAULT')
|
||||
->setStreet("14 Allée d'Argenson")
|
||||
->setStreet2('ZI Nord')
|
||||
->setPostalCode('86100')
|
||||
->setCity('CHATELLERAULT')
|
||||
->setCountryCode('FR')
|
||||
;
|
||||
$this->entityManager->persist($address);
|
||||
|
||||
$supplier = $supplierRepo->findOneBy(['name' => 'LIOT']);
|
||||
if (!$supplier) {
|
||||
$supplier = new Supplier();
|
||||
++$created;
|
||||
} else {
|
||||
++$updated;
|
||||
}
|
||||
$supplier
|
||||
->setName('LIOT')
|
||||
->setEmail('lpc.contacts@lpc-liot.fr')
|
||||
->setPhone('05.49.20.09.10')
|
||||
;
|
||||
if (!$supplier->getAddresses()->contains($address)) {
|
||||
$supplier->getAddresses()->add($address);
|
||||
}
|
||||
$this->entityManager->persist($supplier);
|
||||
|
||||
$this->entityManager->flush();
|
||||
|
||||
$io->success(sprintf('Seed completed: %d created, %d updated.', $created, $updated));
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user