Added a basic form to perform builds.

Logic does not work yet.
This commit is contained in:
Jan Böhmer 2023-01-22 00:01:16 +01:00
parent 3dc9376f40
commit 83d734747a
6 changed files with 292 additions and 5 deletions

View file

@ -25,7 +25,9 @@ use App\Entity\Parts\Part;
use App\Entity\ProjectSystem\Project; use App\Entity\ProjectSystem\Project;
use App\Entity\ProjectSystem\ProjectBOMEntry; use App\Entity\ProjectSystem\ProjectBOMEntry;
use App\Form\ProjectSystem\ProjectBOMEntryCollectionType; use App\Form\ProjectSystem\ProjectBOMEntryCollectionType;
use App\Form\ProjectSystem\ProjectBuildType;
use App\Form\Type\StructuralEntityType; use App\Form\Type\StructuralEntityType;
use App\Helpers\Projects\ProjectBuildRequest;
use App\Services\ProjectSystem\ProjectBuildHelper; use App\Services\ProjectSystem\ProjectBuildHelper;
use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
@ -84,11 +86,20 @@ class ProjectController extends AbstractController
$number_of_builds = 1; $number_of_builds = 1;
} }
$projectBuildRequest = new ProjectBuildRequest($project, $number_of_builds);
$form = $this->createForm(ProjectBuildType::class, $projectBuildRequest);
return $this->render('Projects/build/build.html.twig', [ $form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
//TODO
}
return $this->renderForm('Projects/build/build.html.twig', [
'buildHelper' => $buildHelper, 'buildHelper' => $buildHelper,
'project' => $project, 'project' => $project,
'build_request' => $projectBuildRequest,
'number_of_builds' => $number_of_builds, 'number_of_builds' => $number_of_builds,
'form' => $form,
]); ]);
} }

View file

@ -0,0 +1,80 @@
<?php
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2023 Jan Böhmer (https://github.com/jbtronics)
*
* 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/>.
*/
namespace App\Form\ProjectSystem;
use App\Form\Type\SIUnitType;
use App\Helpers\Projects\ProjectBuildRequest;
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\FormBuilderInterface;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolver;
class ProjectBuildType extends AbstractType implements DataMapperInterface
{
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'compound' => true,
'data_class' => ProjectBuildRequest::class
]);
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->setDataMapper($this);
$builder->add('submit', SubmitType::class, []);
//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();
/** @var ProjectBuildRequest $build_request */
$build_request = $event->getData();
foreach ($build_request->getPartBomEntries() as $bomEntry) {
//Every part lot has a field to specify the number of parts to take from this lot
foreach ($build_request->getPartLotsForBOMEntry($bomEntry) as $lot) {
$form->add('lot_' . $lot->getID(), SIUnitType::class, [
'label' => false,
'measurement_unit' => $bomEntry->getPart()->getPartUnit(),
]);
}
}
});
}
public function mapDataToForms($viewData, \Traversable $forms)
{
$forms = iterator_to_array($forms);
dump($viewData);
dump ($forms);
}
public function mapFormsToData(\Traversable $forms, &$viewData)
{
// TODO: Implement mapFormsToData() method.
}
}

View file

@ -0,0 +1,120 @@
<?php
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2023 Jan Böhmer (https://github.com/jbtronics)
*
* 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/>.
*/
namespace App\Helpers\Projects;
use App\Entity\Parts\PartLot;
use App\Entity\ProjectSystem\Project;
use App\Entity\ProjectSystem\ProjectBOMEntry;
use Doctrine\Common\Collections\Collection;
final class ProjectBuildRequest
{
private Project $project;
private int $number_of_builds;
/**
* @param Project $project The project that should be build
* @param int $number_of_builds The number of builds that should be created
*/
public function __construct(Project $project, int $number_of_builds)
{
$this->project = $project;
$this->number_of_builds = $number_of_builds;
}
/**
* Ensure that the projectBOMEntry belongs to the project, otherwise throw an exception.
* @param ProjectBOMEntry $entry
* @return void
*/
private function ensureBOMEntryValid(ProjectBOMEntry $entry): void
{
if ($entry->getProject() !== $this->project) {
throw new \InvalidArgumentException('The given BOM entry does not belong to the project!');
}
}
/**
* Returns the number of available lots to take stock from for the given BOM entry.
* @parm ProjectBOMEntry $entry
* @return PartLot[]|null Returns null if the entry is a non-part BOM entry
*/
public function getPartLotsForBOMEntry(ProjectBOMEntry $projectBOMEntry): ?array
{
$this->ensureBOMEntryValid($projectBOMEntry);
if ($projectBOMEntry->getPart() === null) {
return null;
}
return $projectBOMEntry->getPart()->getPartLots()->toArray();
}
/**
* Returns the needed amount of parts for the given BOM entry.
* @param ProjectBOMEntry $entry
* @return float
*/
public function getNeededAmountForBOMEntry(ProjectBOMEntry $entry): float
{
$this->ensureBOMEntryValid($entry);
return $entry->getQuantity() * $this->number_of_builds;
}
/**
* Returns the list of all bom entries that have to be build.
* @return ProjectBOMEntry[]
*/
public function getBomEntries(): array
{
return $this->project->getBomEntries()->toArray();
}
/**
* Returns the all part bom entries that have to be build.
* @return ProjectBOMEntry[]
*/
public function getPartBomEntries(): array
{
return $this->project->getBomEntries()->filter(function (ProjectBOMEntry $entry) {
return $entry->isPartBomEntry();
})->toArray();
}
/**
* Returns which project should be build
* @return Project
*/
public function getProject(): Project
{
return $this->project;
}
/**
* Returns the number of builds that should be created.
* @return int
*/
public function getNumberOfBuilds(): int
{
return $this->number_of_builds;
}
}

View file

@ -74,12 +74,24 @@ class ProjectBuildHelper
* Checks if the given project can be build with the current stock. * Checks if the given project can be build with the current stock.
* This means that the maximum buildable count is greater or equal than the requested $number_of_projects * This means that the maximum buildable count is greater or equal than the requested $number_of_projects
* @param Project $project * @param Project $project
* @parm int $number_of_projects * @parm int $number_of_builds
* @return bool * @return bool
*/ */
public function isProjectBuildable(Project $project, int $number_of_projects = 1): bool public function isProjectBuildable(Project $project, int $number_of_builds = 1): bool
{ {
return $this->getMaximumBuildableCount($project) >= $number_of_projects; return $this->getMaximumBuildableCount($project) >= $number_of_builds;
}
/**
* Check if the given BOM entry can be build with the current stock.
* This means that the maximum buildable count is greater or equal than the requested $number_of_projects
* @param ProjectBOMEntry $bom_entry
* @param int $number_of_builds
* @return bool
*/
public function isBOMEntryBuildable(ProjectBOMEntry $bom_entry, int $number_of_builds = 1): bool
{
return $this->getMaximumBuildableCountForBOMEntry($bom_entry) >= $number_of_builds;
} }
/** /**

View file

@ -0,0 +1,62 @@
{% import "helper.twig" as helper %}
{{ form_start(form) }}
<table class="table table-sm table-responsive table-hover">
<tbody>
{% for bom_entry in build_request.bomEntries %}
{# 1st row basic infos about the BOM entry #}
<tr class="{% if buildHelper.bOMEntryBuildable(bom_entry, number_of_builds) %}table-primary{% else %}table-danger{% endif %}">
<td style="width: 20px;">
<div class="form-check">
<input class="form-check-input" type="checkbox" value="" id="checkbox_{{ loop.index }}" required>
{# <label class="form-check-label" for="checkbox_{{ loop.index }}"> #}
</label>
</div>
</td>
<td >
{% if bom_entry.part %}
<b><a target="_blank" href="{{ entity_url(bom_entry.part) }}">{{ bom_entry.part.name }}</a></b> {% if bom_entry.name %}({{ bom_entry.name }}){% endif %}
{% endif %}
</td>
<td>
{{ bom_entry.mountnames }}
</td>
<td class="text-end">
<b>{{ build_request.neededAmountForBOMEntry(bom_entry) | format_amount(bom_entry.part.partUnit ?? null) }}</b> {% trans %}project.builds.needed{% endtrans %}
(= {{ number_of_builds }} x {{ bom_entry.quantity | format_amount(bom_entry.part.partUnit ?? null) }})
</td>
</tr>
<tr>
<td colspan="4">
{% set lots = build_request.partLotsForBOMEntry(bom_entry) %}
{% if lots is not null %}
{% for lot in lots %}
{# @var lot \App\Entity\Parts\PartLot #}
<div class="mb-2 row">
<label class="col-form-label col-sm-4" for="category_admin_form_parent">
{% if lot.storageLocation %}
<small>{{ helper.structural_entity_link(lot.storageLocation) }}</small>
{% endif %}
{% if lot.name is not empty %}
(<small>{{ lot.name }}</small>)
{% endif %}
</label>
<div class="col-sm-6">
{{ form_widget(form["lot_"~lot.id]) }}
</div>
<div class="col-sm-2 mt-1 text-end">
/ <b>{{ lot.amount | format_amount(lot.part.partUnit) }}</b> {% trans %}project.builds.stocked{% endtrans %}
</div>
</div>
{% endfor %}
{% endif %}
</td>
</tr>
{% endfor %}
</tbody>
</table>
{{ form_row(form.submit) }}
{{ form_end(form) }}

View file

@ -23,7 +23,7 @@
<b>{% trans with {"%number_of_builds%": number_of_builds} %}project.builds.following_bom_entries_miss_instock_n{% endtrans %}</b> <b>{% trans with {"%number_of_builds%": number_of_builds} %}project.builds.following_bom_entries_miss_instock_n{% endtrans %}</b>
<ul> <ul>
{% for bom_entry in buildHelper.nonBuildableProjectBomEntries(project, number_of_builds) %} {% for bom_entry in buildHelper.nonBuildableProjectBomEntries(project, number_of_builds) %}
<li>{{ project_macros.project_bom_entry_with_missing_instock(bom_entry) }}</li> <li>{{ project_macros.project_bom_entry_with_missing_instock(bom_entry, number_of_builds) }}</li>
{% endfor %} {% endfor %}
</ul> </ul>
{% else %} {% else %}
@ -32,5 +32,7 @@
{% endif %} {% endif %}
</div> </div>
{% include 'Projects/build/_form.html.twig' %}
{% endblock %} {% endblock %}