Added an checkbox in the withdrawal/move dialog, that if checked automatically deletes a part lot if it becomes empty during the operation

Fixes issue #327
This commit is contained in:
Jan Böhmer 2023-11-25 19:38:08 +01:00
parent 9e69a09a19
commit ffb5d3e790
4 changed files with 35 additions and 7 deletions

View file

@ -400,6 +400,7 @@ class PartController extends AbstractController
$amount = (float) $request->request->get('amount'); $amount = (float) $request->request->get('amount');
$comment = $request->request->get('comment'); $comment = $request->request->get('comment');
$action = $request->request->get('action'); $action = $request->request->get('action');
$delete_lot_if_empty = $request->request->getBoolean('delete_lot_if_empty', false);
$timestamp = null; $timestamp = null;
$timestamp_str = $request->request->getString('timestamp', ''); $timestamp_str = $request->request->getString('timestamp', '');
@ -418,7 +419,7 @@ class PartController extends AbstractController
case "withdraw": case "withdraw":
case "remove": case "remove":
$this->denyAccessUnlessGranted('withdraw', $partLot); $this->denyAccessUnlessGranted('withdraw', $partLot);
$withdrawAddHelper->withdraw($partLot, $amount, $comment, $timestamp); $withdrawAddHelper->withdraw($partLot, $amount, $comment, $timestamp, $delete_lot_if_empty);
break; break;
case "add": case "add":
$this->denyAccessUnlessGranted('add', $partLot); $this->denyAccessUnlessGranted('add', $partLot);
@ -427,7 +428,7 @@ class PartController extends AbstractController
case "move": case "move":
$this->denyAccessUnlessGranted('move', $partLot); $this->denyAccessUnlessGranted('move', $partLot);
$this->denyAccessUnlessGranted('move', $targetLot); $this->denyAccessUnlessGranted('move', $targetLot);
$withdrawAddHelper->move($partLot, $targetLot, $amount, $comment, $timestamp); $withdrawAddHelper->move($partLot, $targetLot, $amount, $comment, $timestamp, $delete_lot_if_empty);
break; break;
default: default:
throw new \RuntimeException("Unknown action!"); throw new \RuntimeException("Unknown action!");

View file

@ -9,13 +9,15 @@ use App\Entity\LogSystem\PartStockChangedLogEntry;
use App\Entity\Parts\PartLot; use App\Entity\Parts\PartLot;
use App\Services\LogSystem\EventCommentHelper; use App\Services\LogSystem\EventCommentHelper;
use App\Services\LogSystem\EventLogger; use App\Services\LogSystem\EventLogger;
use Doctrine\ORM\EntityManagerInterface;
/** /**
* @see \App\Tests\Services\Parts\PartLotWithdrawAddHelperTest * @see \App\Tests\Services\Parts\PartLotWithdrawAddHelperTest
*/ */
final class PartLotWithdrawAddHelper final class PartLotWithdrawAddHelper
{ {
public function __construct(private readonly EventLogger $eventLogger, private readonly EventCommentHelper $eventCommentHelper) public function __construct(private readonly EventLogger $eventLogger,
private readonly EventCommentHelper $eventCommentHelper, private EntityManagerInterface $entityManager)
{ {
} }
@ -54,9 +56,9 @@ final class PartLotWithdrawAddHelper
* @param float $amount The amount of parts that should be taken from the part lot * @param float $amount The amount of parts that should be taken from the part lot
* @param string|null $comment The optional comment describing the reason for the withdrawal * @param string|null $comment The optional comment describing the reason for the withdrawal
* @param \DateTimeInterface|null $action_timestamp The optional timestamp, where the action happened. Useful if the action happened in the past, and the log entry is created afterwards. * @param \DateTimeInterface|null $action_timestamp The optional timestamp, where the action happened. Useful if the action happened in the past, and the log entry is created afterwards.
* @return PartLot The modified part lot * @param bool $delete_lot_if_empty If true, the part lot will be deleted if the amount is 0 after the withdrawal.
*/ */
public function withdraw(PartLot $partLot, float $amount, ?string $comment = null, ?\DateTimeInterface $action_timestamp = null): PartLot public function withdraw(PartLot $partLot, float $amount, ?string $comment = null, ?\DateTimeInterface $action_timestamp = null, bool $delete_lot_if_empty = false): void
{ {
//Ensure that amount is positive //Ensure that amount is positive
if ($amount <= 0) { if ($amount <= 0) {
@ -92,7 +94,9 @@ final class PartLotWithdrawAddHelper
$this->eventCommentHelper->setMessage($comment); $this->eventCommentHelper->setMessage($comment);
} }
return $partLot; if ($delete_lot_if_empty && $partLot->getAmount() === 0.0) {
$this->entityManager->remove($partLot);
}
} }
/** /**
@ -144,8 +148,9 @@ final class PartLotWithdrawAddHelper
* @param float $amount The amount of parts that should be moved * @param float $amount The amount of parts that should be moved
* @param string|null $comment A comment describing the reason for the move * @param string|null $comment A comment describing the reason for the move
* @param \DateTimeInterface|null $action_timestamp The optional timestamp, where the action happened. Useful if the action happened in the past, and the log entry is created afterwards. * @param \DateTimeInterface|null $action_timestamp The optional timestamp, where the action happened. Useful if the action happened in the past, and the log entry is created afterwards.
* @param bool $delete_lot_if_empty If true, the part lot will be deleted if the amount is 0 after the withdrawal.
*/ */
public function move(PartLot $origin, PartLot $target, float $amount, ?string $comment = null, ?\DateTimeInterface $action_timestamp = null): void public function move(PartLot $origin, PartLot $target, float $amount, ?string $comment = null, ?\DateTimeInterface $action_timestamp = null, bool $delete_lot_if_empty = false): void
{ {
if ($amount <= 0) { if ($amount <= 0) {
throw new \InvalidArgumentException('Amount must be positive'); throw new \InvalidArgumentException('Amount must be positive');
@ -187,5 +192,9 @@ final class PartLotWithdrawAddHelper
if (!$this->eventCommentHelper->isMessageSet() && ($comment !== null && $comment !== '')) { if (!$this->eventCommentHelper->isMessageSet() && ($comment !== null && $comment !== '')) {
$this->eventCommentHelper->setMessage($comment); $this->eventCommentHelper->setMessage($comment);
} }
if ($delete_lot_if_empty && $origin->getAmount() === 0.0) {
$this->entityManager->remove($origin);
}
} }
} }

View file

@ -62,6 +62,18 @@
<div id="emailHelp" class="form-text">{% trans %}part.info.withdraw_modal.timestamp.hint{% endtrans %}</div> <div id="emailHelp" class="form-text">{% trans %}part.info.withdraw_modal.timestamp.hint{% endtrans %}</div>
</div> </div>
</div> </div>
<div class="row mb-2">
<div class="col-sm-9 offset-sm-3">
{# The timestamp must be between a year ago and 1 hour in the future #}
<div class="form-check">
<input class="form-check-input" type="checkbox" name="delete_lot_if_empty" value="true" id="withdraw_modal_delete_if_empty">
<label class="form-check-label" for="withdraw_modal_delete_if_empty">
{% trans %}part.info.withdraw_modal.delete_lot_if_empty{% endtrans %}
</label>
</div>
</div>
</div>
</div> </div>
<div class="modal-footer"> <div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">{% trans %}modal.close{% endtrans %}</button> <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">{% trans %}modal.close{% endtrans %}</button>

View file

@ -12071,5 +12071,11 @@ Please note, that you can not impersonate a disabled user. If you try you will g
<target>This field allows you to specify the real date, when the stock operation actually was performed, and not just when it was logged. This value is saved in the extra field of the log entry.</target> <target>This field allows you to specify the real date, when the stock operation actually was performed, and not just when it was logged. This value is saved in the extra field of the log entry.</target>
</segment> </segment>
</unit> </unit>
<unit id="GIR36qJ" name="part.info.withdraw_modal.delete_lot_if_empty">
<segment>
<source>part.info.withdraw_modal.delete_lot_if_empty</source>
<target>Delete this lot, if it becomes empty</target>
</segment>
</unit>
</file> </file>
</xliff> </xliff>