forked from Somnia/SomniaRooms
130 lines
3.9 KiB
PHP
130 lines
3.9 KiB
PHP
<?php
|
|
|
|
namespace App\Service;
|
|
|
|
use App\DTO\AppSetupConfig;
|
|
use App\DTO\ExampleDataConfig;
|
|
use App\Entity\ConfigVariable;
|
|
use App\Entity\FareTable;
|
|
use App\Entity\RoomCategory;
|
|
use App\Entity\RoomFare;
|
|
use App\Entity\User;
|
|
use App\Enum\ConfigSectionType;
|
|
use App\Enum\UserRoleEnum;
|
|
use App\Repository\ConfigVariableRepository;
|
|
use App\Repository\FareTableRepository;
|
|
use App\Repository\RoomAvailabilityRepository;
|
|
use App\Repository\RoomCategoryRepository;
|
|
use App\Repository\RoomFareRepository;
|
|
use DateTime;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
/**
|
|
* Service for setup proccess.
|
|
*/
|
|
class ExampleDataService
|
|
{
|
|
|
|
public function __construct(
|
|
private LoggerInterface $logger,
|
|
private EntityManagerInterface $entityManager,
|
|
private RoomCategoryRepository $roomCategoryRepository,
|
|
private RoomAvailabilityRepository $roomAvailabilityRepository,
|
|
private FareTableRepository $fareTableRepository,
|
|
private RoomFareRepository $roomFareRepository,
|
|
) {}
|
|
|
|
public function AddExampleData(ExampleDataConfig $config): string
|
|
{
|
|
$genMode = $config->getGenMode();
|
|
if ($genMode == ExampleDataConfig::GENMODE_EXAMPLE) {
|
|
return "'Example' generation mode is not supported yet!";
|
|
}
|
|
|
|
$this->entityManager->beginTransaction();
|
|
|
|
try {
|
|
if ($config->getGenRoomCategories()) {
|
|
$this->GenerateRoomCategories();
|
|
}
|
|
if ($config->getGenAvailability()) {
|
|
$this->GenerateAvailability($config);
|
|
}
|
|
if ($config->getGenFaretables()) {
|
|
$this->GenerateFares($config);
|
|
}
|
|
|
|
$this->entityManager->flush();
|
|
$this->entityManager->commit();
|
|
} catch (\Throwable $th) {
|
|
$this->entityManager->rollback();
|
|
throw $th;
|
|
}
|
|
|
|
return "OK";
|
|
}
|
|
|
|
private function GenerateRoomCategories(): bool
|
|
{
|
|
//TODOME.
|
|
return true;
|
|
}
|
|
|
|
private function GenerateAvailability(ExampleDataConfig $config): bool
|
|
{
|
|
$roomCategoryList = $this->roomCategoryRepository->findAll();
|
|
foreach ($roomCategoryList as $room) {
|
|
//TODOME: Make amounts configurable.
|
|
$amount = random_int(0, 7);
|
|
$this->roomAvailabilityRepository->bulkEditRoomAvailabilityForCategoryOnPeriod(
|
|
$room,
|
|
$config->getStartDate(),
|
|
$config->getEndDate(),
|
|
$amount,
|
|
useTransactions: false
|
|
);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
private function GenerateFares(ExampleDataConfig $config): bool
|
|
{
|
|
$generationTime = new DateTime();
|
|
$strGenTime = $generationTime->format('d-m-Y H:i:s');
|
|
|
|
$roomCategoryList = $this->roomCategoryRepository->findAll();
|
|
foreach ($roomCategoryList as $room) {
|
|
$list = $this->fareTableRepository->findFareTable($room->getId(), $config->getStartDate(), $config->getEndDate());
|
|
if (count($list) > 0) {
|
|
//TODOME: Log info.
|
|
continue;
|
|
}
|
|
|
|
$fareTable = new FareTable();
|
|
$fareTable->setStartDate($config->getStartDate());
|
|
$fareTable->setEndDate($config->getEndDate());
|
|
$fareTable->setRoomCategory($room);
|
|
$fareTable->setComment("Autogenerated " . $strGenTime);
|
|
|
|
$this->entityManager->persist($fareTable);
|
|
|
|
for ($i = 1; $i < $room->getMaxGuestNum(); $i++) {
|
|
$roomFare = new RoomFare();
|
|
$roomFare->setGuestNumber($i);
|
|
$roomFare->setFareTable($fareTable);
|
|
|
|
//TODOME: Make amounts configurable.
|
|
// Random price per person.
|
|
$amount = random_int(10, 25) * $i * 100;
|
|
$roomFare->setFareAmount($amount);
|
|
|
|
$this->entityManager->persist($roomFare);
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|