diff --git a/src/Controller/UnsettledSentenceController.php b/src/Controller/UnsettledSentenceController.php new file mode 100644 index 0000000..f7deaa3 --- /dev/null +++ b/src/Controller/UnsettledSentenceController.php @@ -0,0 +1,112 @@ +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]); + } +} diff --git a/src/Entity/SentenceSettlement.php b/src/Entity/SentenceSettlement.php new file mode 100644 index 0000000..d3c207c --- /dev/null +++ b/src/Entity/SentenceSettlement.php @@ -0,0 +1,79 @@ +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; + } +} \ No newline at end of file diff --git a/src/Form/SentenceSettlementType.php b/src/Form/SentenceSettlementType.php new file mode 100644 index 0000000..d686ff3 --- /dev/null +++ b/src/Form/SentenceSettlementType.php @@ -0,0 +1,60 @@ +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 + } + + + +} diff --git a/src/Repository/CriminalRepository.php b/src/Repository/CriminalRepository.php index 4872ffd..37c06e7 100644 --- a/src/Repository/CriminalRepository.php +++ b/src/Repository/CriminalRepository.php @@ -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; + } } diff --git a/src/Repository/Tools/DocumentRepositoriesExtension.php b/src/Repository/Tools/DocumentRepositoriesExtension.php index 0721497..1a13a81 100644 --- a/src/Repository/Tools/DocumentRepositoriesExtension.php +++ b/src/Repository/Tools/DocumentRepositoriesExtension.php @@ -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; diff --git a/templates/base.html.twig b/templates/base.html.twig index f706353..7556998 100644 --- a/templates/base.html.twig +++ b/templates/base.html.twig @@ -152,7 +152,10 @@
{{ 'title_count'|trans }}: {{ count }}
+ {% if pagination[0].directory is not defined %}{% set nodirectorylink = true %}{% endif %} + {% set slicelimit = (limit is defined)? limit : null %} + {% set counter = 0 %} + + {% if pagination|length > 0 %} +| {% trans %}title_name{% endtrans %} | +{% trans %}title_missing{% endtrans %} | +{% trans %}title_date{% endtrans %} | + {% if noaction is not defined %} +{% trans %}tooltip_view{% endtrans %} | + {% endif %} +
|---|
{% trans %}no_data{% endtrans %}
+ {% endif %} +