diff --git a/src/Controller/ProjectController.php b/src/Controller/ProjectController.php
index 88db3ddf..b1c31edb 100644
--- a/src/Controller/ProjectController.php
+++ b/src/Controller/ProjectController.php
@@ -76,7 +76,7 @@ class ProjectController extends AbstractController
/**
* @Route("/{id}/build", name="project_build", requirements={"id"="\d+"})
*/
- public function build(Project $project, Request $request, ProjectBuildHelper $buildHelper): Response
+ public function build(Project $project, Request $request, ProjectBuildHelper $buildHelper, EntityManagerInterface $entityManager): Response
{
$this->denyAccessUnlessGranted('read', $project);
@@ -90,8 +90,19 @@ class ProjectController extends AbstractController
$form = $this->createForm(ProjectBuildType::class, $projectBuildRequest);
$form->handleRequest($request);
- if ($form->isSubmitted() && $form->isValid()) {
- dump($projectBuildRequest);
+ if ($form->isSubmitted()) {
+ if ($form->isValid()) {
+ $buildHelper->doWithdrawForProjectBuildRequest($projectBuildRequest);
+ $entityManager->flush();
+ $this->addFlash('success', 'project.build.flash.success');
+
+ return $this->redirect(
+ $request->get('_redirect',
+ $this->generateUrl('project_info', ['id' => $project->getID()]
+ )));
+ } else {
+ $this->addFlash('error', 'project.build.flash.invalid_input');
+ }
}
return $this->renderForm('Projects/build/build.html.twig', [
diff --git a/src/Form/ProjectSystem/ProjectBuildType.php b/src/Form/ProjectSystem/ProjectBuildType.php
index c56c413d..dbd717f3 100644
--- a/src/Form/ProjectSystem/ProjectBuildType.php
+++ b/src/Form/ProjectSystem/ProjectBuildType.php
@@ -26,6 +26,7 @@ use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\DataMapperInterface;
use Symfony\Component\Form\Event\PreSetDataEvent;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
+use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormInterface;
@@ -47,6 +48,12 @@ class ProjectBuildType extends AbstractType implements DataMapperInterface
$builder->add('submit', SubmitType::class, []);
+ $builder->add('comment', TextType::class, [
+ 'label' => 'part.info.withdraw_modal.comment',
+ 'help' => 'part.info.withdraw_modal.comment.hint',
+ 'required' => false,
+ ]);
+
//The form is initially empty, we have to define the fields after we know the data
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (PreSetDataEvent $event) {
$form = $event->getForm();
diff --git a/src/Helpers/Projects/ProjectBuildRequest.php b/src/Helpers/Projects/ProjectBuildRequest.php
index 4097dbad..8cf94063 100644
--- a/src/Helpers/Projects/ProjectBuildRequest.php
+++ b/src/Helpers/Projects/ProjectBuildRequest.php
@@ -38,6 +38,8 @@ final class ProjectBuildRequest
*/
private array $withdraw_amounts = [];
+ private string $comment = '';
+
/**
* @param Project $project The project that should be build
* @param int $number_of_builds The number of builds that should be created
@@ -78,6 +80,24 @@ final class ProjectBuildRequest
}
}
+ /**
+ * Returns the comment where the user can write additional information about the build.
+ * @return string
+ */
+ public function getComment(): string
+ {
+ return $this->comment;
+ }
+
+ /**
+ * Sets the comment where the user can write additional information about the build.
+ * @param string $comment
+ */
+ public function setComment(string $comment): void
+ {
+ $this->comment = $comment;
+ }
+
/**
* Returns the amount of parts that should be withdrawn from the given lot for the corresponding BOM entry.
* @param PartLot|int $lot The part lot (or the ID of the part lot) for which the withdraw amount should be get
diff --git a/src/Services/ProjectSystem/ProjectBuildHelper.php b/src/Services/ProjectSystem/ProjectBuildHelper.php
index 33a8bc4d..73825f98 100644
--- a/src/Services/ProjectSystem/ProjectBuildHelper.php
+++ b/src/Services/ProjectSystem/ProjectBuildHelper.php
@@ -22,9 +22,17 @@ namespace App\Services\ProjectSystem;
use App\Entity\ProjectSystem\Project;
use App\Entity\ProjectSystem\ProjectBOMEntry;
+use App\Helpers\Projects\ProjectBuildRequest;
+use App\Services\Parts\PartLotWithdrawAddHelper;
class ProjectBuildHelper
{
+ private PartLotWithdrawAddHelper $withdraw_add_helper;
+
+ public function __construct(PartLotWithdrawAddHelper $withdraw_add_helper)
+ {
+ $this->withdraw_add_helper = $withdraw_add_helper;
+ }
/**
* Returns the maximum buildable amount of the given BOM entry based on the stock of the used parts.
@@ -125,4 +133,26 @@ class ProjectBuildHelper
return $non_buildable_entries;
}
+
+ /**
+ * Withdraw the parts from the stock using the given ProjectBuildRequest.
+ * The ProjectBuildRequest has to be validated before!!
+ * You have to flush changes to DB afterwards
+ * @param ProjectBuildRequest $buildRequest
+ * @return void
+ */
+ public function doWithdrawForProjectBuildRequest(ProjectBuildRequest $buildRequest): void
+ {
+ $message = $buildRequest->getComment();
+ $message .= ' (Project build: '.$buildRequest->getProject()->getName().')';
+
+ foreach ($buildRequest->getBomEntries() as $bom_entry) {
+ foreach ($buildRequest->getPartLotsForBOMEntry($bom_entry) as $part_lot) {
+ $amount = $buildRequest->getLotWithdrawAmount($part_lot);
+ if ($amount > 0) {
+ $this->withdraw_add_helper->withdraw($part_lot, $amount, $message);
+ }
+ }
+ }
+ }
}
\ No newline at end of file
diff --git a/templates/Projects/build/_form.html.twig b/templates/Projects/build/_form.html.twig
index a0139643..827c7474 100644
--- a/templates/Projects/build/_form.html.twig
+++ b/templates/Projects/build/_form.html.twig
@@ -57,6 +57,8 @@
+{{ form_row(form.comment) }}
+
{{ form_row(form.submit) }}
{{ form_end(form) }}
\ No newline at end of file
diff --git a/translations/messages.en.xlf b/translations/messages.en.xlf
index f75b5b92..7de39995 100644
--- a/translations/messages.en.xlf
+++ b/translations/messages.en.xlf
@@ -10345,5 +10345,11 @@ Element 3
You do not have enough parts stocked to build this project %number_of_builds% times. The following parts have missing instock:
+
+
+ project.build.flash.invalid_input
+ Can not build project. Check input!
+
+