Skip to content

Commit

Permalink
feat(task_collection): add computeState() and display state on deli…
Browse files Browse the repository at this point in the history
…very list

fixes: #3806
  • Loading branch information
r0xsh authored and alexsegura committed Nov 21, 2023
1 parent 3552a5a commit 9785254
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 0 deletions.
69 changes: 69 additions & 0 deletions src/Entity/TaskCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace AppBundle\Entity;

use AppBundle\Entity\Task\CollectionTrait as TaskCollectionTrait;
use AppBundle\Enum\TaskCollectionState;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
Expand Down Expand Up @@ -128,6 +129,9 @@ public function removeTask(Task $task)
}
}

/**
* @return Task[]
*/
public function getTasks()
{
return $this->getItems()
Expand Down Expand Up @@ -208,4 +212,69 @@ public function getTasksByType(string $type)
return $item->getTask();
})->toArray();
}

/**
* Returns true if all tasks are cancelled
* @return bool
*/
public function computeCancelled(): bool
{
foreach ($this->getTasks() as $task) {
if (!$task->isCancelled()) {
return false;
}
}
return true;
}

public function computeFailed(): bool
{
$tasks = $this->getActiveTasks();
return end($tasks)->isFailed();
}

public function computeDone(): bool
{
foreach ($this->getActiveTasks() as $task) {
if (!$task->isDone()) {
return false;
}
}
return true;
}

public function computeDoing(): bool
{
foreach ($this->getActiveTasks() as $task) {
if ($task->getStatus() == Task::STATUS_DOING || $task->isDone()) {
return true;
}
}
return false;
}

public function computeState(): TaskCollectionState
{
// If all tasks are cancelled, return cancelled
if ($this->computeCancelled()) {
return TaskCollectionState::CANCELLED;
}

// If all tasks are done, return done
if ($this->computeDone()) {
return TaskCollectionState::DELIVERED;
}

// If one task is failed, return failed
if ($this->computeFailed()) {
return TaskCollectionState::FAILED;
}

// If one task is in delivery, return in delivery
if ($this->computeDoing()) {
return TaskCollectionState::IN_DELIVERY;
}

return TaskCollectionState::PENDING;
}
}
41 changes: 41 additions & 0 deletions src/Enum/TaskCollectionState.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace AppBundle\Enum;

enum TaskCollectionState {
case PENDING;
case IN_DELIVERY;
case DELIVERED;
case FAILED;
case CANCELLED;

function toFontAwesome() {
return match ($this) {
TaskCollectionState::PENDING => 'clock-o',
TaskCollectionState::IN_DELIVERY => 'bicycle',
TaskCollectionState::DELIVERED => 'check',
TaskCollectionState::FAILED => 'exclamation-triangle',
TaskCollectionState::CANCELLED => 'times'
};
}

function toLabel() {
return match ($this) {
TaskCollectionState::PENDING => 'delivery.state.pending',
TaskCollectionState::IN_DELIVERY => 'delivery.state.in_delivery',
TaskCollectionState::DELIVERED => 'delivery.state.delivered',
TaskCollectionState::FAILED => 'delivery.state.failed',
TaskCollectionState::CANCELLED => 'delivery.state.cancelled'
};
}

function toColor() {
return match ($this) {
TaskCollectionState::PENDING => 'grey',
TaskCollectionState::IN_DELIVERY => 'skyblue',
TaskCollectionState::DELIVERED => 'green',
TaskCollectionState::FAILED => 'orange',
TaskCollectionState::CANCELLED => 'red'
};
}
}
7 changes: 7 additions & 0 deletions templates/_partials/delivery/list.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<th>{{ 'delivery.table.heading.owner'|trans }}</th>
{% endif %}
<th>{{ 'delivery.table.heading.summary'|trans }}</th>
<th>{{ 'order.list.state'|trans }}</th>
<th>{{ 'task.type.DROPOFF'|trans }}</th>
<th>{{ 'delivery.table.heading.courier'|trans }}</th>
{% if with_order is defined and with_order %}
Expand Down Expand Up @@ -63,6 +64,12 @@
{% endif %}
<small>{{ details|join(' - ') }}</small>
</td>
{% set deliveryState = delivery.computeState() %}
<td>
<span title="{{ deliveryState.toLabel()|trans }}" style="color: {{ deliveryState.toColor() }}">
<i class="fa fa-lg fa-{{ deliveryState.toFontAwesome() }}"></i>
</span>
</td>
<td width="10%">
<small>
{% if delivery.dropoff.before|date('Ymd') == 'now'|date('Ymd') %}
Expand Down
6 changes: 6 additions & 0 deletions translations/messages.de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -711,3 +711,9 @@ basics.settings: Einstellungen
form.store_type.failure_reason_set.label: "Fehlergründe"
profile.delete_my_account: "Mein Konto löschen"
profile.type_username_to_confirm: "Gib deinen Benutzernamen ein, um die Kontolöschung zu bestätigen"
delivery.state:
pending: 'Ausstehend, Abholbereit'
in_delivery: 'Abgeholt, in Lieferung'
delivered: 'Zugestellt'
failed: 'Fehlgeschlagen'
cancelled: 'Abgebrochen'
6 changes: 6 additions & 0 deletions translations/messages.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1370,3 +1370,9 @@ tags.featured: Featured
tags.exclusive: Exclusives
tags.shops.new: New shops
tags.zero_waste: Zero waste
delivery.state:
pending: 'Pending, To be picked up'
in_delivery: 'Picked up, in delivery'
delivered: 'Delivered'
failed: 'Failed'
cancelled: 'Cancelled'
6 changes: 6 additions & 0 deletions translations/messages.es.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1354,3 +1354,9 @@ tags.featured: Destacados
tags.exclusive: Exclusivos
tags.shops.new: Tiendas nuevas
tags.zero_waste: Residuo cero
delivery.state:
pending: 'Pendiente, Por recoger'
in_delivery: 'Recogido, en entrega'
delivered: 'Entregado'
failed: 'Fallido'
cancelled: 'Cancelado'
6 changes: 6 additions & 0 deletions translations/messages.fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1359,3 +1359,9 @@ tags.featured: À la une
tags.exclusive: Exclusivités
tags.shops.new: Nouveautés
tags.zero_waste: Zéro déchet
delivery.state:
pending: 'En attente, À récupérer'
in_delivery: 'Récupéré, en livraison'
delivered: 'Livré'
failed: 'Échoué'
cancelled: 'Annulé'
6 changes: 6 additions & 0 deletions translations/messages.it.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1273,3 +1273,9 @@ basics.settings: Impostazioni
form.store_type.failure_reason_set.label: "Motivi di Fallimento"
profile.delete_my_account: "Elimina il mio account"
profile.type_username_to_confirm: "Inserisci il tuo nome utente per confermare l'eliminazione dell'account"
delivery.state:
pending: 'In attesa, Da ritirare'
in_delivery: 'Ritirato, in consegna'
delivered: 'Consegnato'
failed: 'Fallito'
cancelled: 'Annullato'

0 comments on commit 9785254

Please sign in to comment.