Withdraw selected part lots, when building

This commit is contained in:
Jan Böhmer 2023-01-22 16:59:58 +01:00
parent 31a20d0692
commit 616533ea4a
6 changed files with 79 additions and 3 deletions

View file

@ -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', [

View file

@ -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();

View file

@ -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

View file

@ -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);
}
}
}
}
}

View file

@ -57,6 +57,8 @@
</tbody>
</table>
{{ form_row(form.comment) }}
{{ form_row(form.submit) }}
{{ form_end(form) }}

View file

@ -10345,5 +10345,11 @@ Element 3</target>
<target>You do not have enough parts stocked to build this project %number_of_builds% times. The following parts have missing instock:</target>
</segment>
</unit>
<unit id="dB.JmYm" name="project.build.flash.invalid_input">
<segment>
<source>project.build.flash.invalid_input</source>
<target>Can not build project. Check input!</target>
</segment>
</unit>
</file>
</xliff>