Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calculate the distance for a task into a tasklist even when no vehicle is set #4805

Merged
merged 17 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions app/DoctrineMigrations/Version20250114155816.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Application\Migrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20250114155816 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}

public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE task RENAME COLUMN co2_emissions TO emitted_co2');
$this->addSql('ALTER TABLE task RENAME COLUMN distance_from_previous TO traveled_distance_meter');
}

public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE task RENAME COLUMN emitted_co2 TO co2_emissions');
$this->addSql('ALTER TABLE task RENAME COLUMN traveled_distance_meter TO distance_from_previous');

Atala marked this conversation as resolved.
Show resolved Hide resolved
}
}
3 changes: 3 additions & 0 deletions app/config/message_bus/task.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ services:
arguments:
- '@event_recorder'
- '@translator'
- '@AppBundle\Entity\TaskListRepository'
- '@routing_service'
- '@logger'
tags:
- name: command_handler
handles: AppBundle\Domain\Task\Command\MarkAsDone
Expand Down
19 changes: 19 additions & 0 deletions features/deliveries.feature
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ Feature: Deliveries
| sylius_channels.yml |
| sylius_products.yml |
| sylius_taxation.yml |
| payment_methods.yml |
| stores.yml |
And the store with name "Acme" has an OAuth client named "Acme"
And the OAuth client with name "Acme" has an access token
Expand Down Expand Up @@ -194,6 +195,7 @@ Feature: Deliveries
| sylius_channels.yml |
| sylius_products.yml |
| sylius_taxation.yml |
| payment_methods.yml |
| stores.yml |
And the store with name "Acme" has an OAuth client named "Acme"
And the OAuth client with name "Acme" has an access token
Expand Down Expand Up @@ -308,6 +310,7 @@ Feature: Deliveries
| sylius_channels.yml |
| sylius_products.yml |
| sylius_taxation.yml |
| payment_methods.yml |
| stores.yml |
And the store with name "Acme" has an OAuth client named "Acme"
And the OAuth client with name "Acme" has an access token
Expand Down Expand Up @@ -443,6 +446,7 @@ Feature: Deliveries
| sylius_channels.yml |
| sylius_products.yml |
| sylius_taxation.yml |
| payment_methods.yml |
| stores.yml |
And the store with name "Acme" has an OAuth client named "Acme"
And the OAuth client with name "Acme" has an access token
Expand Down Expand Up @@ -540,6 +544,7 @@ Feature: Deliveries
| sylius_channels.yml |
| sylius_products.yml |
| sylius_taxation.yml |
| payment_methods.yml |
| stores.yml |
And the store with name "Acme" has an OAuth client named "Acme"
And the OAuth client with name "Acme" has an access token
Expand Down Expand Up @@ -1252,6 +1257,7 @@ Feature: Deliveries
| sylius_channels.yml |
| sylius_products.yml |
| sylius_taxation.yml |
| payment_methods.yml |
| stores.yml |
And the store with name "Acme" has an OAuth client named "Acme"
And the OAuth client with name "Acme" has an access token
Expand Down Expand Up @@ -1345,6 +1351,7 @@ Feature: Deliveries
| sylius_channels.yml |
| sylius_products.yml |
| sylius_taxation.yml |
| payment_methods.yml |
| stores.yml |
And the store with name "Acme" has an OAuth client named "Acme"
And the OAuth client with name "Acme" has an access token
Expand Down Expand Up @@ -1442,6 +1449,7 @@ Feature: Deliveries
| sylius_channels.yml |
| sylius_products.yml |
| sylius_taxation.yml |
| payment_methods.yml |
| stores.yml |
And the store with name "Acme" has an OAuth client named "Acme"
And the OAuth client with name "Acme" has an access token
Expand Down Expand Up @@ -1541,6 +1549,7 @@ Feature: Deliveries
| sylius_channels.yml |
| sylius_products.yml |
| sylius_taxation.yml |
| payment_methods.yml |
| stores.yml |
And the store with name "Acme" has an OAuth client named "Acme"
And the OAuth client with name "Acme" has an access token
Expand Down Expand Up @@ -1638,6 +1647,7 @@ Feature: Deliveries
| sylius_channels.yml |
| sylius_products.yml |
| sylius_taxation.yml |
| payment_methods.yml |
| stores.yml |
Given the current time is "2020-04-02 11:00:00"
And the store with name "Acme" has an OAuth client named "Acme"
Expand Down Expand Up @@ -1736,6 +1746,7 @@ Feature: Deliveries
| sylius_channels.yml |
| sylius_products.yml |
| sylius_taxation.yml |
| payment_methods.yml |
| stores.yml |
And the store with name "Acme" has an OAuth client named "Acme"
And the OAuth client with name "Acme" has an access token
Expand Down Expand Up @@ -1816,6 +1827,7 @@ Feature: Deliveries
| sylius_channels.yml |
| sylius_products.yml |
| sylius_taxation.yml |
| payment_methods.yml |
| stores.yml |
And the store with name "Acme" has an OAuth client named "Acme"
And the OAuth client with name "Acme" has an access token
Expand Down Expand Up @@ -1857,6 +1869,7 @@ Feature: Deliveries
"""
Then the response status code should be 400
And the response should be in JSON
Then print last JSON response
And the JSON should match:
"""
{
Expand All @@ -1865,6 +1878,11 @@ Feature: Deliveries
"hydra:title":"An error occurred",
"hydra:description":@string@,
"violations":[
{
"propertyPath":"items",
"message":@string@,
"code":null
},
{
"propertyPath":"items",
"message":@string@,
Expand Down Expand Up @@ -2047,6 +2065,7 @@ Feature: Deliveries
| sylius_channels.yml |
| sylius_products.yml |
| sylius_taxation.yml |
| payment_methods.yml |
| stores.yml |
Given the current time is "2022-05-05 12:00:00"
And the store with name "Acme" has an OAuth client named "Acme"
Expand Down
4 changes: 4 additions & 0 deletions features/deliveries_multi.feature
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Feature: Multi-step deliveries
| sylius_channels.yml |
| sylius_products.yml |
| sylius_taxation.yml |
| payment_methods.yml |
| stores.yml |
Given the setting "latlng" has value "48.856613,2.352222"
And the store with name "Acme" has an OAuth client named "Acme"
Expand Down Expand Up @@ -108,6 +109,7 @@ Feature: Multi-step deliveries
| sylius_channels.yml |
| sylius_products.yml |
| sylius_taxation.yml |
| payment_methods.yml |
| stores.yml |
Given the setting "latlng" has value "48.856613,2.352222"
And the store with name "Acme" has an OAuth client named "Acme"
Expand Down Expand Up @@ -241,6 +243,7 @@ Feature: Multi-step deliveries
| sylius_channels.yml |
| sylius_products.yml |
| sylius_taxation.yml |
| payment_methods.yml |
| stores.yml |
Given the setting "latlng" has value "48.856613,2.352222"
And the store with name "Acme" has an OAuth client named "Acme"
Expand Down Expand Up @@ -370,6 +373,7 @@ Feature: Multi-step deliveries
| sylius_channels.yml |
| sylius_products.yml |
| sylius_taxation.yml |
| payment_methods.yml |
| stores.yml |
Given the setting "latlng" has value "48.856613,2.352222"
And the store with name "Acme" has an OAuth client named "Acme"
Expand Down
2 changes: 2 additions & 0 deletions features/dispatch.feature
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ Feature: Dispatch
"orgName": @string@,
"images": @array@,
"hasIncidents": @boolean@,
"emittedCo2": "@integer@",
"traveledDistanceMeter": "@integer@",
"barcode": @array@,
"packages": @array@
}
Expand Down
1 change: 0 additions & 1 deletion features/fixtures/ORM/task_list.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ AppBundle\Entity\Task:
doneAfter: <identity(new \DateTime('2018-03-03 13:00:00'))>
doneBefore: <identity(new \DateTime('2018-03-03 13:30:00'))>
comments: "addToProblem"
previous: "@task_4"
task_6:
address: "@address_3"
doneAfter: <identity(new \DateTime('2018-03-02 12:00:00'))>
Expand Down
42 changes: 30 additions & 12 deletions features/tasks.feature
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ Feature: Tasks
"images":[],
"next":null,
"packages": [],
"barcode": @array@,
"createdAt":"@string@.isDateTime()"
"barcode": "@array@",
"createdAt":"@string@.isDateTime()",
"emittedCo2": "@integer@",
"traveledDistanceMeter": "@integer@"
},
{
"@id":"@string@.startsWith('/api/tasks')",
Expand Down Expand Up @@ -84,8 +86,10 @@ Feature: Tasks
"images":[],
"next":null,
"packages":[],
"barcode": @array@,
"createdAt":"@string@.isDateTime()"
"barcode": "@array@",
"createdAt":"@string@.isDateTime()",
"emittedCo2": "@integer@",
"traveledDistanceMeter": "@integer@"
},
{
"@id":"@string@.startsWith('/api/tasks')",
Expand Down Expand Up @@ -117,7 +121,9 @@ Feature: Tasks
"orgName":"",
"images":[],
"next":null,
"createdAt":"@string@.isDateTime()"
"createdAt":"@string@.isDateTime()",
"emittedCo2": "@integer@",
"traveledDistanceMeter": "@integer@"
},
{
"@id":"@string@.startsWith('/api/tasks')",
Expand Down Expand Up @@ -149,7 +155,9 @@ Feature: Tasks
"orgName":"",
"images":[],
"next":null,
"createdAt":"@string@.isDateTime()"
"createdAt":"@string@.isDateTime()",
"emittedCo2": "@integer@",
"traveledDistanceMeter": "@integer@"
}
],
"hydra:view": {
Expand Down Expand Up @@ -646,8 +654,10 @@ Feature: Tasks
"hasIncidents": false,
"incidents": [],
"packages": [],
"barcode": @array@,
"createdAt":"@string@.isDateTime()"
"barcode": "@array@",
"createdAt":"@string@.isDateTime()",
"emittedCo2": "@integer@",
"traveledDistanceMeter": "@integer@"
}
"""

Expand Down Expand Up @@ -1119,8 +1129,10 @@ Feature: Tasks
"hasIncidents": false,
"incidents": [],
"packages": [],
"barcode": @array@,
"createdAt":"@string@.isDateTime()"
"barcode": "@array@",
"createdAt":"@string@.isDateTime()",
"emittedCo2": "@integer@",
"traveledDistanceMeter": "@integer@"
}
"""

Expand Down Expand Up @@ -1202,8 +1214,10 @@ Feature: Tasks
"hasIncidents": false,
"incidents": [],
"packages": [],
"barcode": @array@,
"createdAt":"@string@.isDateTime()"
"barcode": "@array@",
"createdAt":"@string@.isDateTime()",
"emittedCo2": "@integer@",
"traveledDistanceMeter": "@integer@"
}
"""

Expand Down Expand Up @@ -1335,6 +1349,8 @@ Feature: Tasks
"hasIncidents": false,
"incidents": [],
"packages": [],
"emittedCo2": "@integer@",
"traveledDistanceMeter": "@integer@",
"barcode": @array@,
"createdAt":"@string@.isDateTime()"
},
Expand Down Expand Up @@ -1367,6 +1383,8 @@ Feature: Tasks
"hasIncidents": false,
"incidents": [],
"packages": [],
"emittedCo2": "@integer@",
"traveledDistanceMeter": "@integer@",
"barcode": @array@,
"createdAt":"@string@.isDateTime()"
}
Expand Down
1 change: 1 addition & 0 deletions features/urbantz.feature
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ Feature: Urbantz
| sylius_channels.yml |
| sylius_products.yml |
| sylius_taxation.yml |
| payment_methods.yml |
| stores.yml |
And the store with name "Acme" has an API key
And the store with name "Acme" is associated with Urbantz hub "61289572c2b7aab94f380d76"
Expand Down
6 changes: 4 additions & 2 deletions src/Action/TaskList/SetItems.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace AppBundle\Action\TaskList;

use ApiPlatform\Core\Api\IriConverterInterface;
use AppBundle\Doctrine\EventSubscriber\TaskSubscriber\TaskListProvider;
use AppBundle\Entity\TaskList;
use AppBundle\Entity\TaskList\Item;
use AppBundle\Entity\Tour;
Expand All @@ -20,7 +21,8 @@ public function __construct(
private EntityManagerInterface $entityManager,
private UserManager $userManager,
private TaskListManager $taskListManager,
private TaskListNormalizer $taskListNormalizer
private TaskListNormalizer $taskListNormalizer,
private TaskListProvider $taskListProvider
)
{}

Expand All @@ -29,7 +31,7 @@ public function __invoke(Request $request)
$date = new \DateTime($request->get('date'));
$user = $this->userManager->findUserByUsername($request->get('username'));

$taskList = $this->taskListManager->getTaskListForUser($date, $user);
$taskList = $this->taskListProvider->getTaskListForUserAndDate($date, $user);

// Tasks are sent as JSON payload
$data = json_decode($request->getContent(), true);
Expand Down
15 changes: 1 addition & 14 deletions src/Doctrine/EventSubscriber/TaskListSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public function __construct(
private readonly TranslatorInterface $translator,
private readonly RoutingInterface $routing,
private readonly TaskListRepository $taskListRepository,
private readonly EntityManagerInterface $em,
private readonly LoggerInterface $logger
)
{
Expand All @@ -53,7 +52,7 @@ private function calculate(TaskList $taskList)
{
$coordinates = [];
$tasks = [];
$vehicle = $taskList->getVehicle();
$vehicle = $taskList->getVehicle();

if (!is_null($vehicle)) {
$coordinates[] = $taskList->getVehicle()->getWarehouse()->getAddress()->getGeo();
Expand All @@ -77,18 +76,6 @@ private function calculate(TaskList $taskList)
$taskList->setDistance($this->routing->getDistance(...$coordinates));
$taskList->setDuration($this->routing->getDuration(...$coordinates));
$taskList->setPolyline($this->routing->getPolyline(...$coordinates));

if (!is_null($vehicle)) {
$route = $this->routing->route(...$coordinates)['routes'][0];
$legs = array_slice($route["legs"], 0, -1);
foreach ($legs as $index => $leg) {
$task = $taskList->getTasks()[$index];
$emissions = intval($vehicle->getCo2emissions() * $leg['distance'] / 1000);
$task->setDistanceFromPrevious(intval($leg['distance'])); // in meter
$task->setCo2Emissions($emissions);
$this->em->getUnitOfWork()->recomputeSingleEntityChangeSet($this->em->getClassMetadata(Task::class), $task);
}
}
}
}

Expand Down
Loading
Loading