2019-02-24 18:05:06 +01:00
|
|
|
<?php
|
2020-02-22 18:14:36 +01:00
|
|
|
/**
|
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
|
|
|
*
|
2022-11-29 22:28:53 +01:00
|
|
|
* Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics)
|
2020-02-22 18:14:36 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published
|
|
|
|
* by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2019-03-05 14:37:41 +01:00
|
|
|
|
2020-01-05 15:46:58 +01:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2019-02-24 18:05:06 +01:00
|
|
|
namespace App\Controller;
|
|
|
|
|
2020-02-22 20:04:43 +01:00
|
|
|
use App\DataTables\LogDataTable;
|
2019-08-12 15:47:57 +02:00
|
|
|
use App\Entity\Parts\Category;
|
2020-04-01 18:02:56 +02:00
|
|
|
use App\Entity\Parts\Footprint;
|
2020-04-10 13:05:08 +02:00
|
|
|
use App\Entity\Parts\Manufacturer;
|
2019-08-12 15:47:57 +02:00
|
|
|
use App\Entity\Parts\Part;
|
2020-04-01 18:02:56 +02:00
|
|
|
use App\Entity\Parts\PartLot;
|
2023-09-04 22:57:40 +02:00
|
|
|
use App\Entity\Parts\StorageLocation;
|
2020-04-01 18:02:56 +02:00
|
|
|
use App\Entity\Parts\Supplier;
|
|
|
|
use App\Entity\PriceInformations\Orderdetail;
|
2022-12-29 16:21:04 +01:00
|
|
|
use App\Entity\ProjectSystem\Project;
|
2019-10-19 18:50:17 +02:00
|
|
|
use App\Exceptions\AttachmentDownloadException;
|
2019-08-19 14:07:00 +02:00
|
|
|
use App\Form\Part\PartBaseType;
|
2019-10-19 17:13:13 +02:00
|
|
|
use App\Services\Attachments\AttachmentSubmitHandler;
|
2019-10-03 18:03:56 +02:00
|
|
|
use App\Services\Attachments\PartPreviewGenerator;
|
2023-11-19 23:47:46 +01:00
|
|
|
use App\Services\EntityMergers\Mergers\PartMerger;
|
2023-07-16 23:46:20 +02:00
|
|
|
use App\Services\InfoProviderSystem\PartInfoRetriever;
|
2020-02-23 00:44:52 +01:00
|
|
|
use App\Services\LogSystem\EventCommentHelper;
|
2020-02-22 20:04:43 +01:00
|
|
|
use App\Services\LogSystem\HistoryHelper;
|
2020-02-16 23:48:57 +01:00
|
|
|
use App\Services\LogSystem\TimeTravel;
|
2020-03-24 16:57:02 +01:00
|
|
|
use App\Services\Parameters\ParameterExtractor;
|
2023-01-01 13:21:50 +01:00
|
|
|
use App\Services\Parts\PartLotWithdrawAddHelper;
|
2022-12-18 17:28:42 +01:00
|
|
|
use App\Services\Parts\PricedetailHelper;
|
2022-12-29 16:21:04 +01:00
|
|
|
use App\Services\ProjectSystem\ProjectBuildPartHelper;
|
2022-08-14 19:32:53 +02:00
|
|
|
use DateTime;
|
2019-03-13 13:23:12 +01:00
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
2022-08-14 19:32:53 +02:00
|
|
|
use Exception;
|
2020-02-22 20:04:43 +01:00
|
|
|
use Omines\DataTablesBundle\DataTableFactory;
|
2022-12-29 16:21:04 +01:00
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
|
2023-06-11 19:42:02 +02:00
|
|
|
use Symfony\Bridge\Doctrine\Attribute\MapEntity;
|
2019-02-24 18:05:06 +01:00
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
2019-08-26 23:30:35 +02:00
|
|
|
use Symfony\Component\Form\FormInterface;
|
2020-01-05 22:49:00 +01:00
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse;
|
2019-03-13 13:23:12 +01:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
2020-01-05 22:49:00 +01:00
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
2019-02-24 18:05:06 +01:00
|
|
|
use Symfony\Component\Routing\Annotation\Route;
|
2023-04-02 23:35:18 +02:00
|
|
|
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
|
2019-03-13 14:53:12 +01:00
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface;
|
2019-02-24 18:05:06 +01:00
|
|
|
|
2023-04-02 23:35:18 +02:00
|
|
|
use function Symfony\Component\Translation\t;
|
|
|
|
|
2023-05-28 01:21:05 +02:00
|
|
|
#[Route(path: '/part')]
|
2019-02-24 18:05:06 +01:00
|
|
|
class PartController extends AbstractController
|
|
|
|
{
|
2023-07-16 23:46:20 +02:00
|
|
|
public function __construct(protected PricedetailHelper $pricedetailHelper,
|
|
|
|
protected PartPreviewGenerator $partPreviewGenerator,
|
|
|
|
private readonly TranslatorInterface $translator,
|
|
|
|
private readonly AttachmentSubmitHandler $attachmentSubmitHandler, private readonly EntityManagerInterface $em,
|
|
|
|
protected EventCommentHelper $commentHelper)
|
2020-02-16 23:48:57 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-02-24 18:05:06 +01:00
|
|
|
/**
|
2019-11-09 00:47:20 +01:00
|
|
|
*
|
2022-08-14 19:32:53 +02:00
|
|
|
* @throws Exception
|
2019-02-24 18:05:06 +01:00
|
|
|
*/
|
2023-05-28 01:21:05 +02:00
|
|
|
#[Route(path: '/{id}/info/{timestamp}', name: 'part_info')]
|
|
|
|
#[Route(path: '/{id}', requirements: ['id' => '\d+'])]
|
2020-02-22 20:04:43 +01:00
|
|
|
public function show(Part $part, Request $request, TimeTravel $timeTravel, HistoryHelper $historyHelper,
|
2023-01-07 20:49:36 +01:00
|
|
|
DataTableFactory $dataTable, ParameterExtractor $parameterExtractor, PartLotWithdrawAddHelper $withdrawAddHelper, ?string $timestamp = null): Response
|
2019-02-24 18:05:06 +01:00
|
|
|
{
|
2019-03-18 19:05:41 +01:00
|
|
|
$this->denyAccessUnlessGranted('read', $part);
|
|
|
|
|
2020-02-16 23:48:57 +01:00
|
|
|
$timeTravel_timestamp = null;
|
2020-03-15 13:56:31 +01:00
|
|
|
if (null !== $timestamp) {
|
2020-03-07 20:49:52 +01:00
|
|
|
$this->denyAccessUnlessGranted('show_history', $part);
|
2020-02-16 23:48:57 +01:00
|
|
|
//If the timestamp only contains numbers interpret it as unix timestamp
|
|
|
|
if (ctype_digit($timestamp)) {
|
2022-08-14 19:32:53 +02:00
|
|
|
$timeTravel_timestamp = new DateTime();
|
2020-02-16 23:48:57 +01:00
|
|
|
$timeTravel_timestamp->setTimestamp((int) $timestamp);
|
|
|
|
} else { //Try to parse it via DateTime
|
2022-08-14 19:32:53 +02:00
|
|
|
$timeTravel_timestamp = new DateTime($timestamp);
|
2020-02-16 23:48:57 +01:00
|
|
|
}
|
|
|
|
$timeTravel->revertEntityToTimestamp($part, $timeTravel_timestamp);
|
|
|
|
}
|
|
|
|
|
2020-03-15 13:56:31 +01:00
|
|
|
if ($this->isGranted('show_history', $part)) {
|
2020-03-07 20:49:52 +01:00
|
|
|
$table = $dataTable->createFromType(LogDataTable::class, [
|
|
|
|
'filter_elements' => $historyHelper->getAssociatedElements($part),
|
2020-03-15 13:56:31 +01:00
|
|
|
'mode' => 'element_history',
|
2020-03-07 20:49:52 +01:00
|
|
|
], ['pageLength' => 10])
|
|
|
|
->handleRequest($request);
|
2020-02-29 22:53:53 +01:00
|
|
|
|
2020-03-07 20:49:52 +01:00
|
|
|
if ($table->isCallback()) {
|
|
|
|
return $table->getResponse();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$table = null;
|
2020-02-29 22:53:53 +01:00
|
|
|
}
|
|
|
|
|
2019-10-03 18:03:56 +02:00
|
|
|
return $this->render(
|
2023-02-04 23:05:39 +01:00
|
|
|
'parts/info/show_part_info.html.twig',
|
2019-02-24 18:05:06 +01:00
|
|
|
[
|
2019-03-20 22:53:06 +01:00
|
|
|
'part' => $part,
|
2020-02-22 20:04:43 +01:00
|
|
|
'datatable' => $table,
|
2020-02-16 23:48:57 +01:00
|
|
|
'pricedetail_helper' => $this->pricedetailHelper,
|
|
|
|
'pictures' => $this->partPreviewGenerator->getPreviewAttachments($part),
|
2020-03-15 13:56:31 +01:00
|
|
|
'timeTravel' => $timeTravel_timestamp,
|
2020-03-24 16:57:02 +01:00
|
|
|
'description_params' => $parameterExtractor->extractParameters($part->getDescription()),
|
2020-03-29 22:16:06 +02:00
|
|
|
'comment_params' => $parameterExtractor->extractParameters($part->getComment()),
|
2023-01-07 20:49:36 +01:00
|
|
|
'withdraw_add_helper' => $withdrawAddHelper,
|
2019-02-24 18:05:06 +01:00
|
|
|
]
|
2019-10-03 18:03:56 +02:00
|
|
|
);
|
2019-02-24 18:05:06 +01:00
|
|
|
}
|
|
|
|
|
2023-05-28 01:21:05 +02:00
|
|
|
#[Route(path: '/{id}/edit', name: 'part_edit')]
|
2023-07-16 23:46:20 +02:00
|
|
|
public function edit(Part $part, Request $request): Response
|
2019-03-13 13:23:12 +01:00
|
|
|
{
|
2019-03-18 19:05:41 +01:00
|
|
|
$this->denyAccessUnlessGranted('edit', $part);
|
2019-03-13 13:23:12 +01:00
|
|
|
|
2023-07-16 23:46:20 +02:00
|
|
|
return $this->renderPartForm('edit', $request, $part);
|
2019-03-13 13:23:12 +01:00
|
|
|
}
|
|
|
|
|
2023-05-28 01:21:05 +02:00
|
|
|
#[Route(path: '/{id}/delete', name: 'part_delete', methods: ['DELETE'])]
|
2023-07-16 23:46:20 +02:00
|
|
|
public function delete(Request $request, Part $part): RedirectResponse
|
2019-09-02 17:42:13 +02:00
|
|
|
{
|
|
|
|
$this->denyAccessUnlessGranted('delete', $part);
|
|
|
|
|
2023-06-18 00:00:58 +02:00
|
|
|
if ($this->isCsrfTokenValid('delete'.$part->getID(), $request->request->get('_token'))) {
|
2019-09-02 17:42:13 +02:00
|
|
|
|
2020-02-23 00:44:52 +01:00
|
|
|
$this->commentHelper->setMessage($request->request->get('log_comment', null));
|
|
|
|
|
2019-09-02 17:42:13 +02:00
|
|
|
//Remove part
|
2023-07-16 23:46:20 +02:00
|
|
|
$this->em->remove($part);
|
2019-09-02 17:42:13 +02:00
|
|
|
|
|
|
|
//Flush changes
|
2023-07-16 23:46:20 +02:00
|
|
|
$this->em->flush();
|
2019-09-02 17:42:13 +02:00
|
|
|
|
|
|
|
$this->addFlash('success', 'part.deleted');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->redirectToRoute('homepage');
|
|
|
|
}
|
|
|
|
|
2023-05-28 01:21:05 +02:00
|
|
|
#[Route(path: '/new', name: 'part_new')]
|
|
|
|
#[Route(path: '/{id}/clone', name: 'part_clone')]
|
|
|
|
#[Route(path: '/new_build_part/{project_id}', name: 'part_new_build_part')]
|
2019-08-26 23:46:38 +02:00
|
|
|
public function new(Request $request, EntityManagerInterface $em, TranslatorInterface $translator,
|
2022-12-29 16:21:04 +01:00
|
|
|
AttachmentSubmitHandler $attachmentSubmitHandler, ProjectBuildPartHelper $projectBuildPartHelper,
|
2023-06-11 19:42:02 +02:00
|
|
|
#[MapEntity(mapping: ['id' => 'id'])] ?Part $part = null,
|
2023-08-20 20:30:38 +02:00
|
|
|
#[MapEntity(mapping: ['project_id' => 'id'])] ?Project $project = null): Response
|
2019-03-13 13:23:12 +01:00
|
|
|
{
|
2022-12-29 16:21:04 +01:00
|
|
|
|
2023-06-11 14:55:06 +02:00
|
|
|
if ($part instanceof Part) {
|
2023-06-11 14:15:46 +02:00
|
|
|
//Clone part
|
2020-03-08 18:56:00 +01:00
|
|
|
$new_part = clone $part;
|
2023-06-11 14:55:06 +02:00
|
|
|
} elseif ($project instanceof Project) {
|
2023-06-11 14:15:46 +02:00
|
|
|
//Initialize a new part for a build part from the given project
|
2022-12-29 16:21:04 +01:00
|
|
|
//Ensure that the project has not already a build part
|
2023-06-11 14:55:06 +02:00
|
|
|
if ($project->getBuildPart() instanceof Part) {
|
2022-12-29 16:21:04 +01:00
|
|
|
$this->addFlash('error', 'part.new_build_part.error.build_part_already_exists');
|
|
|
|
return $this->redirectToRoute('part_edit', ['id' => $project->getBuildPart()->getID()]);
|
|
|
|
}
|
|
|
|
$new_part = $projectBuildPartHelper->getPartInitialization($project);
|
|
|
|
} else { //Create an empty part from scratch
|
|
|
|
$new_part = new Part();
|
2020-03-08 18:56:00 +01:00
|
|
|
}
|
2019-03-14 19:20:02 +01:00
|
|
|
|
2019-03-18 19:05:41 +01:00
|
|
|
$this->denyAccessUnlessGranted('create', $new_part);
|
|
|
|
|
2020-04-01 18:02:56 +02:00
|
|
|
$cid = $request->get('category', null);
|
|
|
|
$category = $cid ? $em->find(Category::class, $cid) : null;
|
2023-06-11 14:55:06 +02:00
|
|
|
if ($category instanceof Category && !$new_part->getCategory() instanceof Category) {
|
2019-11-10 14:00:56 +01:00
|
|
|
$new_part->setCategory($category);
|
2023-01-28 21:07:01 +01:00
|
|
|
$new_part->setDescription($category->getDefaultDescription());
|
|
|
|
$new_part->setComment($category->getDefaultComment());
|
2019-11-10 14:00:56 +01:00
|
|
|
}
|
2019-03-13 13:23:12 +01:00
|
|
|
|
2020-04-01 18:02:56 +02:00
|
|
|
$fid = $request->get('footprint', null);
|
2020-04-09 14:22:58 +02:00
|
|
|
$footprint = $fid ? $em->find(Footprint::class, $fid) : null;
|
2023-06-11 14:55:06 +02:00
|
|
|
if ($footprint instanceof Footprint && !$new_part->getFootprint() instanceof Footprint) {
|
2020-04-01 18:02:56 +02:00
|
|
|
$new_part->setFootprint($footprint);
|
|
|
|
}
|
|
|
|
|
|
|
|
$mid = $request->get('manufacturer', null);
|
|
|
|
$manufacturer = $mid ? $em->find(Manufacturer::class, $mid) : null;
|
2023-06-11 14:55:06 +02:00
|
|
|
if ($manufacturer instanceof Manufacturer && !$new_part->getManufacturer() instanceof Manufacturer) {
|
2020-04-01 18:02:56 +02:00
|
|
|
$new_part->setManufacturer($manufacturer);
|
|
|
|
}
|
|
|
|
|
|
|
|
$store_id = $request->get('storelocation', null);
|
2023-09-04 22:57:40 +02:00
|
|
|
$storelocation = $store_id ? $em->find(StorageLocation::class, $store_id) : null;
|
|
|
|
if ($storelocation instanceof StorageLocation && $new_part->getPartLots()->isEmpty()) {
|
2020-04-01 18:02:56 +02:00
|
|
|
$partLot = new PartLot();
|
|
|
|
$partLot->setStorageLocation($storelocation);
|
|
|
|
$partLot->setInstockUnknown(true);
|
|
|
|
$new_part->addPartLot($partLot);
|
|
|
|
}
|
|
|
|
|
|
|
|
$supplier_id = $request->get('supplier', null);
|
2020-04-10 13:05:08 +02:00
|
|
|
$supplier = $supplier_id ? $em->find(Supplier::class, $supplier_id) : null;
|
2023-06-11 14:55:06 +02:00
|
|
|
if ($supplier instanceof Supplier && $new_part->getOrderdetails()->isEmpty()) {
|
2020-04-01 18:02:56 +02:00
|
|
|
$orderdetail = new Orderdetail();
|
|
|
|
$orderdetail->setSupplier($supplier);
|
|
|
|
$new_part->addOrderdetail($orderdetail);
|
|
|
|
}
|
|
|
|
|
2023-07-16 23:46:20 +02:00
|
|
|
return $this->renderPartForm('new', $request, $new_part);
|
|
|
|
}
|
|
|
|
|
2023-07-17 23:21:30 +02:00
|
|
|
#[Route('/from_info_provider/{providerKey}/{providerId}/create', name: 'info_providers_create_part', requirements: ['providerId' => '.+'])]
|
2023-07-16 23:46:20 +02:00
|
|
|
public function createFromInfoProvider(Request $request, string $providerKey, string $providerId, PartInfoRetriever $infoRetriever): Response
|
|
|
|
{
|
|
|
|
$this->denyAccessUnlessGranted('@info_providers.create_parts');
|
|
|
|
|
|
|
|
$dto = $infoRetriever->getDetails($providerKey, $providerId);
|
|
|
|
$new_part = $infoRetriever->dtoToPart($dto);
|
|
|
|
|
|
|
|
return $this->renderPartForm('new', $request, $new_part, [
|
|
|
|
'info_provider_dto' => $dto,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2023-11-22 20:11:38 +01:00
|
|
|
#[Route('/{target}/merge/{other}', name: 'part_merge')]
|
|
|
|
public function merge(Request $request, Part $target, Part $other, PartMerger $partMerger): Response
|
|
|
|
{
|
|
|
|
$this->denyAccessUnlessGranted('edit', $target);
|
|
|
|
$this->denyAccessUnlessGranted('delete', $other);
|
|
|
|
|
|
|
|
//Save the old name of the target part for the template
|
|
|
|
$target_name = $target->getName();
|
|
|
|
|
2023-11-24 19:45:15 +01:00
|
|
|
$this->addFlash('notice', t('part.merge.flash.please_review'));
|
|
|
|
|
2023-11-22 20:11:38 +01:00
|
|
|
$merged = $partMerger->merge($target, $other);
|
|
|
|
return $this->renderPartForm('merge', $request, $merged, [], [
|
|
|
|
'tname_before' => $target_name,
|
|
|
|
'other_part' => $other,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2023-11-24 19:28:30 +01:00
|
|
|
#[Route(path: '/{id}/from_info_provider/{providerKey}/{providerId}/update', name: 'info_providers_update_part', requirements: ['providerId' => '.+'])]
|
2023-11-19 23:47:46 +01:00
|
|
|
public function updateFromInfoProvider(Part $part, Request $request, string $providerKey, string $providerId,
|
|
|
|
PartInfoRetriever $infoRetriever, PartMerger $partMerger): Response
|
|
|
|
{
|
|
|
|
$this->denyAccessUnlessGranted('edit', $part);
|
|
|
|
$this->denyAccessUnlessGranted('@info_providers.create_parts');
|
|
|
|
|
2023-11-24 19:35:44 +01:00
|
|
|
//Save the old name of the target part for the template
|
|
|
|
$old_name = $part->getName();
|
|
|
|
|
2023-11-19 23:47:46 +01:00
|
|
|
$dto = $infoRetriever->getDetails($providerKey, $providerId);
|
|
|
|
$provider_part = $infoRetriever->dtoToPart($dto);
|
|
|
|
|
|
|
|
$part = $partMerger->merge($part, $provider_part);
|
|
|
|
|
2023-11-24 19:45:15 +01:00
|
|
|
$this->addFlash('notice', t('part.merge.flash.please_review'));
|
|
|
|
|
2023-11-24 19:35:44 +01:00
|
|
|
return $this->renderPartForm('update_from_ip', $request, $part, [
|
2023-11-19 23:47:46 +01:00
|
|
|
'info_provider_dto' => $dto,
|
2023-11-24 19:35:44 +01:00
|
|
|
], [
|
|
|
|
'tname_before' => $old_name
|
2023-11-19 23:47:46 +01:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2023-07-16 23:46:20 +02:00
|
|
|
/**
|
|
|
|
* This function provides a common implementation for methods, which use the part form.
|
|
|
|
* @param Request $request
|
2023-07-17 00:43:35 +02:00
|
|
|
* @param Part $data
|
2023-07-16 23:46:20 +02:00
|
|
|
* @param array $form_options
|
|
|
|
* @return Response
|
|
|
|
*/
|
2023-11-22 20:11:38 +01:00
|
|
|
private function renderPartForm(string $mode, Request $request, Part $data, array $form_options = [], array $merge_infos = []): Response
|
2023-07-16 23:46:20 +02:00
|
|
|
{
|
|
|
|
//Ensure that mode is either 'new' or 'edit
|
2023-11-24 19:35:44 +01:00
|
|
|
if (!in_array($mode, ['new', 'edit', 'merge', 'update_from_ip'], true)) {
|
2023-07-16 23:46:20 +02:00
|
|
|
throw new \InvalidArgumentException('Invalid mode given');
|
|
|
|
}
|
|
|
|
|
|
|
|
$new_part = $data;
|
|
|
|
|
|
|
|
$form = $this->createForm(PartBaseType::class, $new_part, $form_options);
|
2019-03-13 13:23:12 +01:00
|
|
|
|
|
|
|
$form->handleRequest($request);
|
|
|
|
|
|
|
|
if ($form->isSubmitted() && $form->isValid()) {
|
2019-08-26 23:46:38 +02:00
|
|
|
//Upload passed files
|
|
|
|
$attachments = $form['attachments'];
|
|
|
|
foreach ($attachments as $attachment) {
|
2020-01-05 15:46:58 +01:00
|
|
|
/** @var FormInterface $attachment */
|
2019-10-19 18:50:17 +02:00
|
|
|
$options = [
|
|
|
|
'secure_attachment' => $attachment['secureFile']->getData(),
|
2019-11-09 00:47:20 +01:00
|
|
|
'download_url' => $attachment['downloadURL']->getData(),
|
2019-10-19 18:50:17 +02:00
|
|
|
];
|
2020-01-05 15:46:58 +01:00
|
|
|
|
2019-10-19 18:50:17 +02:00
|
|
|
try {
|
2023-07-16 23:46:20 +02:00
|
|
|
$this->attachmentSubmitHandler->handleFormSubmit($attachment->getData(), $attachment['file']->getData(), $options);
|
2020-01-05 22:49:00 +01:00
|
|
|
} catch (AttachmentDownloadException $attachmentDownloadException) {
|
2019-10-19 18:50:17 +02:00
|
|
|
$this->addFlash(
|
|
|
|
'error',
|
2023-07-16 23:46:20 +02:00
|
|
|
$this->translator->trans('attachment.download_failed').' '.$attachmentDownloadException->getMessage()
|
2019-10-19 18:50:17 +02:00
|
|
|
);
|
|
|
|
}
|
2019-08-26 23:46:38 +02:00
|
|
|
}
|
|
|
|
|
2020-02-23 00:44:52 +01:00
|
|
|
$this->commentHelper->setMessage($form['log_comment']->getData());
|
|
|
|
|
2023-07-16 23:46:20 +02:00
|
|
|
$this->em->persist($new_part);
|
2023-11-22 20:11:38 +01:00
|
|
|
|
|
|
|
//When we are in merge mode, we have to remove the other part
|
|
|
|
if ($mode === 'merge') {
|
|
|
|
$this->em->remove($merge_infos['other_part']);
|
|
|
|
}
|
|
|
|
|
2023-07-16 23:46:20 +02:00
|
|
|
$this->em->flush();
|
|
|
|
if ($mode === 'new') {
|
|
|
|
$this->addFlash('success', 'part.created_flash');
|
|
|
|
} else if ($mode === 'edit') {
|
|
|
|
$this->addFlash('success', 'part.edited_flash');
|
|
|
|
}
|
2019-03-13 13:23:12 +01:00
|
|
|
|
2022-12-29 16:21:04 +01:00
|
|
|
//If a redirect URL was given, redirect there
|
|
|
|
if ($request->query->get('_redirect')) {
|
|
|
|
return $this->redirect($request->query->get('_redirect'));
|
|
|
|
}
|
|
|
|
|
2020-05-30 23:50:50 +02:00
|
|
|
//Redirect to clone page if user wished that...
|
2020-05-31 13:10:44 +02:00
|
|
|
//@phpstan-ignore-next-line
|
2020-08-21 21:36:22 +02:00
|
|
|
if ('save_and_clone' === $form->getClickedButton()->getName()) {
|
2020-05-30 23:50:50 +02:00
|
|
|
return $this->redirectToRoute('part_clone', ['id' => $new_part->getID()]);
|
|
|
|
}
|
2023-02-05 21:00:26 +01:00
|
|
|
//@phpstan-ignore-next-line
|
|
|
|
if ('save_and_new' === $form->getClickedButton()->getName()) {
|
|
|
|
return $this->redirectToRoute('part_new');
|
|
|
|
}
|
2020-05-30 23:50:50 +02:00
|
|
|
|
2019-03-20 23:16:07 +01:00
|
|
|
return $this->redirectToRoute('part_edit', ['id' => $new_part->getID()]);
|
2019-09-16 22:04:59 +02:00
|
|
|
}
|
|
|
|
|
2020-08-21 21:36:22 +02:00
|
|
|
if ($form->isSubmitted() && !$form->isValid()) {
|
2019-12-26 14:53:13 +01:00
|
|
|
$this->addFlash('error', 'part.created_flash.invalid');
|
2019-03-20 23:16:07 +01:00
|
|
|
}
|
2019-03-13 13:23:12 +01:00
|
|
|
|
2023-07-17 00:43:35 +02:00
|
|
|
$template = '';
|
2023-07-16 23:46:20 +02:00
|
|
|
if ($mode === 'new') {
|
|
|
|
$template = 'parts/edit/new_part.html.twig';
|
|
|
|
} else if ($mode === 'edit') {
|
|
|
|
$template = 'parts/edit/edit_part_info.html.twig';
|
2023-11-22 20:11:38 +01:00
|
|
|
} else if ($mode === 'merge') {
|
|
|
|
$template = 'parts/edit/merge_parts.html.twig';
|
2023-11-24 19:35:44 +01:00
|
|
|
} else if ($mode === 'update_from_ip') {
|
|
|
|
$template = 'parts/edit/update_from_ip.html.twig';
|
2023-07-16 23:46:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->render($template,
|
2022-03-04 21:20:18 +01:00
|
|
|
[
|
|
|
|
'part' => $new_part,
|
|
|
|
'form' => $form,
|
2023-11-22 20:11:38 +01:00
|
|
|
'merge_old_name' => $merge_infos['tname_before'] ?? null,
|
|
|
|
'merge_other' => $merge_infos['other_part'] ?? null
|
2022-03-04 21:20:18 +01:00
|
|
|
]);
|
2019-03-19 19:53:23 +01:00
|
|
|
}
|
2023-01-01 13:21:50 +01:00
|
|
|
|
2023-07-16 23:46:20 +02:00
|
|
|
|
2023-05-28 01:21:05 +02:00
|
|
|
#[Route(path: '/{id}/add_withdraw', name: 'part_add_withdraw', methods: ['POST'])]
|
2023-01-01 13:21:50 +01:00
|
|
|
public function withdrawAddHandler(Part $part, Request $request, EntityManagerInterface $em, PartLotWithdrawAddHelper $withdrawAddHelper): Response
|
|
|
|
{
|
|
|
|
if ($this->isCsrfTokenValid('part_withraw' . $part->getID(), $request->request->get('_csfr'))) {
|
|
|
|
//Retrieve partlot from the request
|
|
|
|
$partLot = $em->find(PartLot::class, $request->request->get('lot_id'));
|
2023-06-11 14:55:06 +02:00
|
|
|
if(!$partLot instanceof PartLot) {
|
2023-01-07 20:49:36 +01:00
|
|
|
throw new \RuntimeException('Part lot not found!');
|
|
|
|
}
|
2023-01-01 13:21:50 +01:00
|
|
|
//Ensure that the partlot belongs to the part
|
|
|
|
if($partLot->getPart() !== $part) {
|
|
|
|
throw new \RuntimeException("The origin partlot does not belong to the part!");
|
|
|
|
}
|
2023-04-16 01:22:58 +02:00
|
|
|
|
|
|
|
//Try to determine the target lot (used for move actions), if the parameter is existing
|
|
|
|
$targetId = $request->request->get('target_id', null);
|
|
|
|
$targetLot = $targetId ? $em->find(PartLot::class, $targetId) : null;
|
2023-01-01 13:21:50 +01:00
|
|
|
if ($targetLot && $targetLot->getPart() !== $part) {
|
|
|
|
throw new \RuntimeException("The target partlot does not belong to the part!");
|
|
|
|
}
|
|
|
|
|
|
|
|
//Extract the amount and comment from the request
|
|
|
|
$amount = (float) $request->request->get('amount');
|
|
|
|
$comment = $request->request->get('comment');
|
|
|
|
$action = $request->request->get('action');
|
2023-11-25 19:38:08 +01:00
|
|
|
$delete_lot_if_empty = $request->request->getBoolean('delete_lot_if_empty', false);
|
2023-01-01 13:21:50 +01:00
|
|
|
|
2023-11-25 19:10:18 +01:00
|
|
|
$timestamp = null;
|
|
|
|
$timestamp_str = $request->request->getString('timestamp', '');
|
|
|
|
//Try to parse the timestamp
|
|
|
|
if($timestamp_str !== '') {
|
|
|
|
$timestamp = new DateTime($timestamp_str);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Ensure that the timestamp is not in the future
|
|
|
|
if($timestamp !== null && $timestamp > new DateTime("+20min")) {
|
|
|
|
throw new \LogicException("The timestamp must not be in the future!");
|
|
|
|
}
|
2023-01-01 13:21:50 +01:00
|
|
|
|
2023-12-07 00:36:16 +01:00
|
|
|
//Ensure that the amount is not null or negative
|
|
|
|
if ($amount <= 0) {
|
|
|
|
$this->addFlash('warning', 'part.withdraw.zero_amount');
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2023-04-02 23:35:18 +02:00
|
|
|
try {
|
|
|
|
switch ($action) {
|
|
|
|
case "withdraw":
|
|
|
|
case "remove":
|
|
|
|
$this->denyAccessUnlessGranted('withdraw', $partLot);
|
2023-11-25 19:38:08 +01:00
|
|
|
$withdrawAddHelper->withdraw($partLot, $amount, $comment, $timestamp, $delete_lot_if_empty);
|
2023-04-02 23:35:18 +02:00
|
|
|
break;
|
|
|
|
case "add":
|
|
|
|
$this->denyAccessUnlessGranted('add', $partLot);
|
2023-11-25 19:10:18 +01:00
|
|
|
$withdrawAddHelper->add($partLot, $amount, $comment, $timestamp);
|
2023-04-02 23:35:18 +02:00
|
|
|
break;
|
|
|
|
case "move":
|
|
|
|
$this->denyAccessUnlessGranted('move', $partLot);
|
|
|
|
$this->denyAccessUnlessGranted('move', $targetLot);
|
2023-11-25 19:38:08 +01:00
|
|
|
$withdrawAddHelper->move($partLot, $targetLot, $amount, $comment, $timestamp, $delete_lot_if_empty);
|
2023-04-02 23:35:18 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new \RuntimeException("Unknown action!");
|
|
|
|
}
|
2023-06-11 14:15:46 +02:00
|
|
|
} catch (AccessDeniedException) {
|
2023-04-02 23:35:18 +02:00
|
|
|
$this->addFlash('error', t('part.withdraw.access_denied'));
|
|
|
|
goto err;
|
2023-01-01 13:21:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//Save the changes to the DB
|
|
|
|
$em->flush();
|
|
|
|
$this->addFlash('success', 'part.withdraw.success');
|
|
|
|
|
|
|
|
} else {
|
|
|
|
$this->addFlash('error', 'CSRF Token invalid!');
|
|
|
|
}
|
|
|
|
|
2023-04-02 23:35:18 +02:00
|
|
|
err:
|
2023-04-15 23:14:53 +02:00
|
|
|
//If a redirect was passed, then redirect there
|
2023-01-01 13:21:50 +01:00
|
|
|
if($request->request->get('_redirect')) {
|
|
|
|
return $this->redirect($request->request->get('_redirect'));
|
|
|
|
}
|
|
|
|
//Otherwise just redirect to the part page
|
|
|
|
return $this->redirectToRoute('part_info', ['id' => $part->getID()]);
|
|
|
|
}
|
2019-03-20 23:16:07 +01:00
|
|
|
}
|