Adding a page to list all non executed sentences
This commit is contained in:
112
src/Controller/UnsettledSentenceController.php
Normal file
112
src/Controller/UnsettledSentenceController.php
Normal file
@@ -0,0 +1,112 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Entity\Criminal;
|
||||
use App\Entity\Directory;
|
||||
use App\Entity\SentenceSettlement;
|
||||
use App\Entity\User;
|
||||
use App\Form\SearchBarType;
|
||||
use App\Form\SentenceSettlementType;
|
||||
use App\Repository\CriminalRepository;
|
||||
use App\Repository\DirectoryRepository;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Knp\Component\Pager\PaginatorInterface;
|
||||
use Symfony\Component\Form\FormError;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
|
||||
#[Route('/unsettled_sentence', name: 'unsettled_sentence_')]
|
||||
class UnsettledSentenceController extends AbstractController
|
||||
{
|
||||
#[Route('/list/{type?*}', name: 'list')]
|
||||
public function list(
|
||||
PaginatorInterface $paginator,
|
||||
Request $request,
|
||||
CriminalRepository $CriminalRepository,
|
||||
string $type = 'fine'
|
||||
): Response {
|
||||
|
||||
$searchForm = $this->createForm(SearchBarType::class);
|
||||
$searchForm->handleRequest($request);
|
||||
|
||||
$pagination = $paginator->paginate(
|
||||
$CriminalRepository->listForUser($this->getUser())
|
||||
->search((
|
||||
$searchForm->isSubmitted()
|
||||
&& $searchForm->isValid()
|
||||
&& $searchForm->getData()['subject'] !== null
|
||||
) ? $searchForm->getData()['subject'] : null)
|
||||
->limitUnsettled($type)
|
||||
->order(['createdAt' => 'DESC'])
|
||||
->getResult(),
|
||||
$request->query->getInt('page', 1)
|
||||
);
|
||||
|
||||
foreach ($pagination as $i => $criminal) {
|
||||
$sentence = new SentenceSettlement($criminal, $type);
|
||||
$form = $this->createForm(
|
||||
SentenceSettlementType::class,
|
||||
$sentence,
|
||||
[
|
||||
'action'=> $this->generateUrl('unsettled_sentence_settle'),
|
||||
]);
|
||||
$pagination[$i]->form = $form->createView();
|
||||
}
|
||||
|
||||
return $this->render('sentence/list.html.twig', [
|
||||
'controller_name' => 'AdminController',
|
||||
'type' => $type,
|
||||
'pagination' => $pagination,
|
||||
'count' => $pagination->getTotalItemCount(),
|
||||
'searchForm' => $searchForm->createView(),
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/settle', name: 'settle')]
|
||||
public function settle(
|
||||
Request $request,
|
||||
CriminalRepository $CriminalRepository
|
||||
) {
|
||||
$type = null;
|
||||
$sentenceForm = $this->createForm(SentenceSettlementType::class);
|
||||
$sentenceForm->handleRequest($request);
|
||||
|
||||
if ($sentenceForm->isSubmitted() && $sentenceForm->isValid()) {
|
||||
$formData = $sentenceForm->getData();
|
||||
$type = $formData['type'];
|
||||
$criminal = $CriminalRepository->find($formData['criminal_id']);
|
||||
if (!$criminal) {
|
||||
$sentenceForm->addError(new FormError('error_document_not_found'));
|
||||
} else {
|
||||
try {
|
||||
$criminal->addSettlement($type, $formData['amount']);
|
||||
} catch (\OutOfRangeException $e) {
|
||||
$sentenceForm->addError(new FormError('error_value_out_of_bound'));
|
||||
}
|
||||
}
|
||||
if ($sentenceForm->isValid()) {
|
||||
$entityManager = $this->getDoctrine()->getManager();
|
||||
$entityManager->persist($criminal);
|
||||
try {
|
||||
$entityManager->flush();
|
||||
} catch (\Throwable $th) {
|
||||
if ($_ENV['APP_ENV'] === 'dev') {
|
||||
throw $th; //DEBUG
|
||||
} else {
|
||||
$this->logger->error($th);
|
||||
}
|
||||
|
||||
$this->addFlash('danger', 'alert_error_editing_document');
|
||||
return $this->redirectToRoute($request->getRequestUri());
|
||||
}
|
||||
} else {
|
||||
$this->addFlash('warning', 'alert_error_form_post');
|
||||
}
|
||||
}
|
||||
|
||||
return $this->redirectToRoute('unsettled_sentence_list', ['type' => $type]);
|
||||
}
|
||||
}
|
||||
79
src/Entity/SentenceSettlement.php
Normal file
79
src/Entity/SentenceSettlement.php
Normal file
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
namespace App\Entity;
|
||||
|
||||
class SentenceSettlement
|
||||
{
|
||||
const TYPE_JAIL = 'jail';
|
||||
const TYPE_FINE = 'fine';
|
||||
const TYPE_COMMUNITY_WORK = 'community_work';
|
||||
|
||||
private Criminal $criminal;
|
||||
private string $type;
|
||||
private $amount = null;
|
||||
|
||||
public function __construct(
|
||||
Criminal $criminal,
|
||||
string $type
|
||||
)
|
||||
{
|
||||
$this->criminal = $criminal;
|
||||
$this->type = $type;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Criminal
|
||||
*/
|
||||
public function getCriminal(): Criminal
|
||||
{
|
||||
return $this->criminal;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Criminal $criminal
|
||||
*/
|
||||
public function setCriminal(Criminal $criminal): void
|
||||
{
|
||||
$this->criminal = $criminal;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Criminal
|
||||
*/
|
||||
public function getCriminalId(): int
|
||||
{
|
||||
return $this->criminal->getId();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getType(): string
|
||||
{
|
||||
return $this->type;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $type
|
||||
*/
|
||||
public function setType(string $type): void
|
||||
{
|
||||
$this->type = $type;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return null
|
||||
*/
|
||||
public function getAmount()
|
||||
{
|
||||
return $this->amount;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param null $amount
|
||||
*/
|
||||
public function setAmount($amount): void
|
||||
{
|
||||
$this->amount = $amount;
|
||||
}
|
||||
}
|
||||
60
src/Form/SentenceSettlementType.php
Normal file
60
src/Form/SentenceSettlementType.php
Normal file
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace App\Form;
|
||||
|
||||
use App\Entity\Document;
|
||||
use App\Form\Type\AllowedGroupsType;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use App\Form\Type\UserGroupSubGroupsType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\NumberType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
|
||||
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
|
||||
|
||||
class SentenceSettlementType extends AbstractType
|
||||
{
|
||||
private TokenStorageInterface $TokenStorage;
|
||||
|
||||
public function __construct(TokenStorageInterface $TokenStorage)
|
||||
{
|
||||
$this->TokenStorage = $TokenStorage;
|
||||
}
|
||||
|
||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||
{
|
||||
$sentence = $builder->getData();
|
||||
|
||||
if ($sentence) {
|
||||
if ($sentence->getType() == "fine") {
|
||||
$amountType = NumberType::class;
|
||||
$amountLabel = 'form_label_paid';
|
||||
} else {
|
||||
$amountType = IntegerType::class;
|
||||
$amountLabel = 'form_label_executed';
|
||||
}
|
||||
} else {
|
||||
$amountType = null;
|
||||
$amountLabel = null;
|
||||
}
|
||||
|
||||
$builder->add('criminal_id', HiddenType::class)
|
||||
->add('type', HiddenType::class)
|
||||
->add('amount', $amountType, ['label' => $amountLabel])
|
||||
->add('submit', SubmitType::class, [
|
||||
'label' => 'form_button_submit',
|
||||
'priority' => -900,
|
||||
])
|
||||
;
|
||||
}
|
||||
|
||||
public function getBlockPrefix()
|
||||
{
|
||||
return ''; // return an empty string here
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -19,4 +19,22 @@ class CriminalRepository extends DocumentRepositoriesExtension
|
||||
parent::__construct($registry, Criminal::class);
|
||||
$this->fields = ['amountMoney', 'amountTime', 'content']; //with title, list fields we can search in
|
||||
}
|
||||
|
||||
public function limitUnsettled(string $type) {
|
||||
if ($type == Criminal::TYPE_FINE) {
|
||||
$column = 'd.amountMoney';
|
||||
} elseif ($type == Criminal::TYPE_JAIL) {
|
||||
$column = 'd.amountTime';
|
||||
} elseif ($type == Criminal::TYPE_COMMUNITY_WORK) {
|
||||
$column = 'd.amountCommunityWork';
|
||||
} else {
|
||||
throw new \OutOfRangeException($type);
|
||||
}
|
||||
|
||||
$settledColumn = $column . 'Settled';
|
||||
|
||||
$this->qb->where("$column <> $settledColumn OR ($column IS NOT NULL AND $settledColumn IS NULL)");
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
class DocumentRepositoriesExtension extends ServiceEntityRepository
|
||||
{
|
||||
public array $fields = [];
|
||||
private QueryBuilder $qb;
|
||||
protected QueryBuilder $qb;
|
||||
private ?QueryBuilder $qbsearch = null;
|
||||
private ?Group $group = null;
|
||||
private ?User $user = null;
|
||||
|
||||
Reference in New Issue
Block a user