Skip to content

Commit

Permalink
Ajout logique statut commande
Browse files Browse the repository at this point in the history
  • Loading branch information
Nour-Dev-Paris committed Mar 4, 2021
1 parent 2be9021 commit c98807d
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 24 deletions.
31 changes: 31 additions & 0 deletions migrations/Version20210304171028.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

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

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20210304171028 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 `order` DROP is_paid');
}

public function down(Schema $schema) : void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE `order` ADD is_paid TINYINT(1) NOT NULL');
}
}
4 changes: 4 additions & 0 deletions public/assets/css/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,8 @@ body {
background-color: #f4f8ff;
padding: 20px;
margin-top: 22px;
}

a.action-updateDelivery {
margin-right: 20px;
}
98 changes: 96 additions & 2 deletions src/Controller/Admin/OrderCrudController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,118 @@

namespace App\Controller\Admin;

use App\Classe\Mail;
use App\Entity\Order;
use Doctrine\ORM\EntityManagerInterface;
use App\Controller\Admin\OrderCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
use EasyCorp\Bundle\EasyAdminBundle\Field\IdField;
use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
use EasyCorp\Bundle\EasyAdminBundle\Field\ArrayField;
use EasyCorp\Bundle\EasyAdminBundle\Field\MoneyField;
use EasyCorp\Bundle\EasyAdminBundle\Field\BooleanField;
use EasyCorp\Bundle\EasyAdminBundle\Field\ChoiceField;
use EasyCorp\Bundle\EasyAdminBundle\Field\DateTimeField;
use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextEditorField;
use EasyCorp\Bundle\EasyAdminBundle\Router\CrudUrlGenerator;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;

class OrderCrudController extends AbstractCrudController
{
private $entityManager;
private $crudUrlGenerator;

public function __construct(EntityManagerInterface $entityManager, CrudUrlGenerator $crudUrlGenerator)
{
$this->entityManager = $entityManager;
$this->crudUrlGenerator = $crudUrlGenerator;
}

public static function getEntityFqcn(): string
{
return Order::class;
}

public function configureActions(Actions $actions): Actions
{
$updatePreparation = Action::new('updatePreparation', 'Préparation en cours', "fas fa-box-open")->linkToCrudAction('updatePreparation');
$updateDelivery = Action::new('updateDelivery', 'Livraison en cours', "fas fa-truck")->linkToCrudAction('updateDelivery');

return $actions
->add('detail', $updatePreparation)
->add('detail', $updateDelivery)
->add('index', 'detail');
}

public function updatePreparation(AdminContext $context)
{
$order = $context->getEntity()->getInstance();

if($order->getState() == 2) {
$this->addFlash('notice', "<span style='color:red;'><strong>La commande " . $order->getReference() . " est déjà <u>en cours de préparation</u></strong></span> ");

} else {

$order->setState(2);

$this->entityManager->flush();

$this->addFlash('notice', "<span style='color:green;'><strong>La commande " . $order->getReference() . " est bien <u>en cours de préparation</u></strong></span> ");

$mail = new Mail();
$content = "Bonjour ". $order->getUser()->getFirstname(). "<br>" . "Votre commande est en cours de préparation !";
$mail->send(
$order->getUser()->getEmail(),
$order->getUser()->getFirstname(),
"Votre commande numéro : " . $order->getReference() . " eCommerce - Symfony",
$content
);

}

$url = $this->crudUrlGenerator->build()
->setController(OrderCrudController::class)
->setAction('index')
->generateUrl();

return $this->redirect($url);
}

public function updateDelivery(AdminContext $context)
{
$order = $context->getEntity()->getInstance();

if($order->getState() == 3) {
$this->addFlash('notice', "<span style='color:red;'><strong>La commande " . $order->getReference() . " est déjà <u>en cours de livraison</u></strong></span> ");

} else {

$order->setState(3);

$this->entityManager->flush();

$this->addFlash('notice', "<span style='color:green;'><strong>La commande " . $order->getReference() . " est bien <u>en cours de livraison</u></strong></span> ");

$mail = new Mail();
$content = "Bonjour ". $order->getUser()->getFirstname(). "<br>" . "Votre commande est en cours de livraison !";
$mail->send(
$order->getUser()->getEmail(),
$order->getUser()->getFirstname(),
"Votre commande numéro : " . $order->getReference() . " eCommerce - Symfony",
$content
);
}

$url = $this->crudUrlGenerator->build()
->setController(OrderCrudController::class)
->setAction('index')
->generateUrl();

return $this->redirect($url);
}

public function configureCrud(Crud $crud) : Crud
{
return $crud->setDefaultSort(['id' => 'DESC']);
Expand All @@ -37,10 +125,16 @@ public function configureFields(string $pageName): iterable
IdField::new('id'),
DateTimeField::new('createdAt', 'Passée le'),
TextField::new('user.getFullName', 'Client'),
TextEditorField::new('delivery', 'Adresse de livraison'),
MoneyField::new('total', 'Total produit')->setCurrency('EUR'),
TextField::new('carrierName', 'Transporteur'),
MoneyField::new('carrierPrice', 'Frais de port')->setCurrency('EUR'),
BooleanField::new('isPaid', 'Payée'),
ChoiceField::new('state', 'Etat de la commande')->setChoices([
'Non payée' => 0,
'Payée' => 1,
'Préparation en cours' => 2,
'Livraison en cours' => 3
]),
ArrayField::new('orderDetails', 'Produits achetés')->hideOnIndex()
];
}
Expand Down
2 changes: 1 addition & 1 deletion src/Controller/OrderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function add(Cart $cart, Request $request): Response
$order->setCarrierName($carriers->getName());
$order->setCarrierPrice($carriers->getPrice());
$order->setDelivery($delivery_content);
$order->setIsPaid(0);
$order->setState(0);

$this->entityManager->persist($order);

Expand Down
6 changes: 3 additions & 3 deletions src/Controller/OrderValidateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ public function index(Cart $cart, $stripeSessionId): Response
$this->redirectToRoute('home');
}

if (!$order->getIsPaid()) {
if ($order->getState() == 0) {

// Vider la session Cart
$cart->remove();

// Modifier le statut isPaid de notre commande en mettant à 1
$order->setIsPaid(1);
// Modifier le statut de notre commande en mettant à 1
$order->setState(1);
$this->entityManager->flush();

// Envoyer un email à un client pour confirmer la commande
Expand Down
34 changes: 17 additions & 17 deletions src/Entity/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,6 @@ class Order
*/
private $orderDetails;

/**
* @ORM\Column(type="boolean")
*/
private $isPaid;

/**
* @ORM\Column(type="string", length=255)
*/
Expand All @@ -66,6 +61,11 @@ class Order
*/
private $stripeSessionId;

/**
* @ORM\Column(type="integer")
*/
private $state;

public function __construct()
{
$this->orderDetails = new ArrayCollection();
Expand Down Expand Up @@ -177,18 +177,6 @@ public function removeOrderDetail(OrderDetails $orderDetail): self
return $this;
}

public function getIsPaid(): ?bool
{
return $this->isPaid;
}

public function setIsPaid(bool $isPaid): self
{
$this->isPaid = $isPaid;

return $this;
}

public function getReference(): ?string
{
return $this->reference;
Expand All @@ -212,4 +200,16 @@ public function setStripeSessionId(?string $stripeSessionId): self

return $this;
}

public function getState(): ?int
{
return $this->state;
}

public function setState(int $state): self
{
$this->state = $state;

return $this;
}
}
2 changes: 1 addition & 1 deletion src/Repository/OrderRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function __construct(ManagerRegistry $registry)
public function findSuccessOrders($user)
{
return $this->createQueryBuilder('o')
->andWhere('o.isPaid = 1')
->andWhere('o.state > 0')
->andWhere('o.user = :user')
->setParameter('user', $user)
->orderBy('o.id', 'DESC')
Expand Down
10 changes: 10 additions & 0 deletions templates/account/order.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<thead>
<tr>
<th scope="col">Référence</th>
<th scope="col">Statut</th>
<th scope="col">Passé le</th>
<th scope="col">Produit(s)</th>
<th scope="col">Total</th>
Expand All @@ -27,6 +28,15 @@
{% for order in orders %}
<tr>
<td><span class="badge badge-secondary">{{order.reference}}</span></td>
<td>
{% if order.state == 1 %}
Paiement accepté
{% elseif order.state == 2 %}
Préparation en cours
{% elseif order.state == 3 %}
En cours de livraison
{% endif %}
</td>
<td>{{order.createdAt|date('d/m/Y') }}</td>
<td>{{order.orderDetails|length}}</td>
<td>{{ ((order.carrierPrice + order.getTotal) / 100)|number_format(2, ',', '.') }}€</td>
Expand Down
8 changes: 8 additions & 0 deletions templates/account/order_show.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@
<h1>Ma commande n° {{order.reference}}</h1>
<a href="{{ path('account_order')}}">Retour</a>
<hr>
<strong>Statut de la commande : </strong>
{% if order.state == 1 %}
Paiement accepté
{% elseif order.state == 2 %}
Préparation en cours
{% elseif order.state == 3 %}
En cours de livraison
{% endif %} <br>
<strong>Commande passée le : </strong>{{ order.createdAt|date('d/m/Y') }}<br>
<strong>Référence de ma commande : </strong><small>{{ order.reference }}</small><br>
<strong>Transporteur choisi : </strong>{{ order.carrierName }}
Expand Down

0 comments on commit c98807d

Please sign in to comment.