Skip to content

Commit

Permalink
fix: purchaseItems listing on account
Browse files Browse the repository at this point in the history
  • Loading branch information
edouardproust committed Apr 10, 2022
1 parent 318d6d3 commit 33615bf
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 70 deletions.
25 changes: 12 additions & 13 deletions src/Controller/Checkout/CheckoutConfirmationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,18 @@ class CheckoutConfirmationController extends AbstractController

public function __construct(
PurchaseRepository $purchaseRepository,
CartService $cartService,
CheckoutService $checkoutService,
CartService $cartService,
CheckoutService $checkoutService,
DeliveryCountryRepository $deliveryCountriesRepo,
EntityManagerInterface $em,
UserRepository $userRepository
){
) {
$this->purchaseRepository = $purchaseRepository;
$this->cartService = $cartService;
$this->checkoutService = $checkoutService;
$this->deliveryCountriesRepo = $deliveryCountriesRepo;
$this->em = $em;
$this->userRepository = $userRepository;

}

/**
Expand All @@ -47,8 +46,8 @@ public function confirmation($id, Request $request): Response
{
// redirect
$purchase = $this->purchaseRepository->find($id);
if($purchase) {
if(!$this->checkoutService->accessGranted($purchase)) {
if ($purchase) {
if (!$this->checkoutService->accessGranted($purchase)) {
return $this->redirectToRoute('cart');
}
} else {
Expand All @@ -59,37 +58,37 @@ public function confirmation($id, Request $request): Response
$form = $this->createForm(PurchaseConfirmationType::class, $purchase);
// form request handle
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()) {
if ($form->isSubmitted() && $form->isValid()) {
$purchase = $form->getData();
// save user account (if not logged in and 'password' field is not empty)
$user = $this->getUser();
if(!$user && $purchase->getPassword()) {
if (!$user && $purchase->getPassword()) {
$user = $this->checkoutService->createUser($purchase);
$this->em->persist($user);
$this->em->flush();
// add user to purchase (user id is now generated)
$this->addFlash('success', 'An account has been created with your email '.$user->getEmail().' and the password you provided.');
$this->addFlash('success', 'An account has been created with your email ' . $user->getEmail() . ' and the password you provided.');
}
// save purchase
$this->checkoutService->updatePurchase($purchase, $user);
$this->em->persist($purchase);
foreach($purchase->getPurchaseItems() as $item) {
foreach ($purchase->getPurchaseItems() as $item) {
$this->em->persist($item);
}
$this->em->persist($purchase);
$this->em->flush();
// redirect to payment
return $this->redirectToRoute('checkout_payment', [
'id' => $purchase->getId(),
'method' => $purchase->getPaymentMethod()->getSlug()
]);
}

return $this->render("checkout/confirmation.html.twig", [
'formCheckout' => $form->createView(),
'loginLink' => $this->checkoutService->createLinkWithReferer('security_login', $request->get("_route")),
'cart' => $purchase->getPurchaseItems(),
'total' => $purchase->getTotal(),
]);
}

}
}
85 changes: 42 additions & 43 deletions src/DataFixtures/DevFixtures.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php

namespace App\DataFixtures;

Expand All @@ -24,22 +24,22 @@
* Load fixtures for DEV environnement.
* Use console command: "php bin/console doctrine: fixtures:load --env dev"
*/
class DevFixtures extends AbstractFixture
class DevFixtures extends AbstractFixture
{

const CATEGORIES = 10;
const PRODUCTS = 77;
const USERS = 34;
/** Must match a $value in App\App\Helper\DeliveryHelper::worldCountries() */
const USERS_COUNTRY= 'United States';
const USERS_COUNTRY = 'United States';
const PURCHASES = 41;
const PURCHASE_MAX_ROWS = 8;
const PURCHASE_REGISTERED_USERS_PERCENT = 80;
const PURCHASE_PAID_PERCENT = 90;
const MAX_QTY_PER_ROW = 5;
const DELIVERY_METHODS = [
'USPS' => ['Normal - 72h', 490, ['US' => "United States"]],
'FedEx' => ['Quick - 48h', 1390],
'USPS' => ['Normal - 72h', 490, ['US' => "United States"]],
'FedEx' => ['Quick - 48h', 1390],
'UPS' => ['Express - 24h', 2690]
];
const PAYMENT_METHODS = ["Stripe"];
Expand Down Expand Up @@ -101,47 +101,47 @@ protected function envLoad(ObjectManager $manager): void
$this->createPaymentMethods();
$this->createPurchases();
$this->createPurchaseItems();

// persist
foreach($this->productMainImages as $mainImage) {
foreach ($this->productMainImages as $mainImage) {
$manager->persist($mainImage);
}
foreach($this->users as $user) {
$manager->persist($user); // contains admin too
}
foreach($this->products as $product) {
foreach ($this->users as $user) {
$manager->persist($user); // contains admin too
}
foreach ($this->products as $product) {
$manager->persist($product);
}
foreach($this->categories as $category) {
foreach ($this->categories as $category) {
$manager->persist($category);
}
foreach($this->deliveryCountries as $deliveryCountry) {
foreach ($this->deliveryCountries as $deliveryCountry) {
$manager->persist($deliveryCountry);
}
foreach($this->deliveryMethods as $deliveryMethod) {
foreach ($this->deliveryMethods as $deliveryMethod) {
$manager->persist($deliveryMethod);
}
foreach($this->paymentMethods as $paymentMethod) {
foreach ($this->paymentMethods as $paymentMethod) {
$manager->persist($paymentMethod);
}
foreach($this->purchases as $purchase) {
foreach ($this->purchases as $purchase) {
$manager->persist($purchase);
}
foreach($this->purchaseItems as $purchaseItem) {
foreach ($this->purchaseItems as $purchaseItem) {
$manager->persist($purchaseItem);
}

// flush
$manager->flush();
}

/**
* Create all users
* @return void
*/
private function createUsers(): void
{
for($u = 1; $u <= self::USERS; $u++) {
for ($u = 1; $u <= self::USERS; $u++) {
$user = $this->createOneUser(true);
$this->users[] = $user;
}
Expand All @@ -158,19 +158,19 @@ private function createOneUser(bool $setPassword = true): User
$user
->setFirstname($this->faker->firstName())
->setLastname($this->faker->lastName())
->setEmail(strtolower($user->getFirstname()."-".$user->getLastname())."@".$this->faker->freeEmailDomain())
->setEmail(strtolower($user->getFirstname() . "-" . $user->getLastname()) . "@" . $this->faker->freeEmailDomain())
->setCreatedAt(new \DateTime('today'))
->setStreet($this->faker->streetAddress())
->setPostcode($this->faker->postcode())
->setCity($this->faker->city())
->setCountry(self::USERS_COUNTRY)
->setPhone($this->faker->phoneNumber());
if($setPassword) {
if ($setPassword) {
$user->setPassword($this->hasher->hashPassword($user, strtolower($user->getFirstname())));
}
}
return $user;
}

private function createCategories(): void
{
// Category "Undefined"
Expand All @@ -181,11 +181,11 @@ private function createCategories(): void

// Other categories
$categoryNames = [];
for($c = 1; $c <= self::CATEGORIES; $c++) {
for ($c = 1; $c <= self::CATEGORIES; $c++) {
$category = new Category;
// check that each category name is unique
$fakeCatName = $this->faker->category();
while(in_array($fakeCatName, $categoryNames)) {
while (in_array($fakeCatName, $categoryNames)) {
$fakeCatName = $this->faker->category();
}
$categoryNames[] = $fakeCatName;
Expand All @@ -199,34 +199,34 @@ private function createCategories(): void

private function createProductMainImages(): void
{
for($p = 1; $p <= self::PRODUCTS; $p++) {
for ($p = 1; $p <= self::PRODUCTS; $p++) {
$mainImage = new Upload;
$mainImage
->setName("fixture-product-$p-mainImage")
->setUrl($this->faker->imageUrl(600, 450, true));
$this->productMainImages[] = $mainImage;
$this->productMainImages[] = $mainImage;
}
}

private function createProducts(): void
{
for($p = 1; $p <= self::PRODUCTS; $p++) {
for ($p = 1; $p <= self::PRODUCTS; $p++) {
$product = new Product;
$product
->setName($this->faker->productName())
->setPrice($this->faker->price())
->setShortDescription($this->faker->paragraph())
->setMainImage($this->productMainImages[$p-1])
->setMainImage($this->productMainImages[$p - 1])
->setCategory($this->faker->randomElement($this->categories))
->setViews(mt_rand(0, 300))
->setCreatedAt($this->faker->dateTimeBetween('-1 year', 'today'));
$this->products[] = $product;
}
}
}

private function createDeliveryCountries(): void
{
foreach(DeliveryHelper::deliveryCountries() as $code => $name) {
foreach (DeliveryHelper::deliveryCountries() as $code => $name) {
$country = (new DeliveryCountry)
->setCode($code)
->setName($name);
Expand All @@ -236,32 +236,32 @@ private function createDeliveryCountries(): void

private function createDeliveryMethods(): void
{
foreach(self::DELIVERY_METHODS as $carrier => $infos) {
foreach (self::DELIVERY_METHODS as $carrier => $infos) {
$method = (new DeliveryMethod)
->setName($infos[0])
->setCarrier($carrier)
->setPrice($infos[1]);
foreach($this->deliveryCountries as $country) {
$method->addCountry($country);
}
foreach ($this->deliveryCountries as $country) {
$method->addCountry($country);
}
$this->deliveryMethods[] = $method;
}
}

private function createPaymentMethods(): void
{
foreach(self::PAYMENT_METHODS as $methodName) {
foreach (self::PAYMENT_METHODS as $methodName) {
$method = (new PaymentMethod)->setName($methodName);
$this->paymentMethods[] = $method;
}
}

private function createPurchases(): void
{
for($p = 1; $p <= self::PURCHASES; $p++) {
for ($p = 1; $p <= self::PURCHASES; $p++) {
$purchase = new Purchase;
$isRegisteredUser = $this->faker->boolean(self::PURCHASE_REGISTERED_USERS_PERCENT);
if($isRegisteredUser) {
if ($isRegisteredUser) {
$user = $this->faker->randomElement($this->users);
$purchase->setUser($user);
} else {
Expand All @@ -281,7 +281,7 @@ private function createPurchases(): void
->setDeliveryMethod($this->faker->randomElement($this->deliveryMethods))
->setPaymentMethod($this->faker->randomElement($this->paymentMethods));
$isPaid = $this->faker->boolean(self::PURCHASE_PAID_PERCENT);
if($isPaid) {
if ($isPaid) {
$purchase->setStatus(Purchase::STATUS_PAID);
}
$this->purchases[] = $purchase;
Expand All @@ -290,9 +290,9 @@ private function createPurchases(): void

private function createPurchaseItems(): void
{
foreach($this->purchases as $purchase) {
foreach ($this->purchases as $purchase) {
$purchaseItems = mt_rand(1, self::PURCHASE_MAX_ROWS);
for($pi = 1; $pi <= $purchaseItems; $pi++) {
for ($pi = 1; $pi <= $purchaseItems; $pi++) {
$purchaseItem = new PurchaseItem;
$product = $this->faker->randomElement($this->products);
$purchaseItem
Expand All @@ -306,5 +306,4 @@ private function createPurchaseItems(): void
}
}
}

}
}
12 changes: 8 additions & 4 deletions src/Entity/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ public function __construct()
$this->purchaseItems = new ArrayCollection();
}

public function getVars()
{
return get_object_vars($this);
}

public function getId(): ?int
{
return $this->id;
Expand All @@ -104,18 +109,18 @@ public function getPrice(): ?int
return $this->price;
}

public function getFloatPrice(): ? float
public function getFloatPrice(): ?float
{
return $this->price / 100;
}

public function getPriceWithCurrency($currencySign = "$", $before = true): ?string
{
$floatPrice = $this->getFloatPrice();
if($currencySign == "") {
if ($currencySign == "") {
$floatPrice = str_replace(".", ",", $this->getFloatPrice());
}
if($before) {
if ($before) {
return $currencySign . $floatPrice;
}
return $floatPrice . $currencySign;
Expand Down Expand Up @@ -259,5 +264,4 @@ public function addPurchase($quantity): void
{
$this->purchases += $quantity;
}

}
Loading

0 comments on commit 33615bf

Please sign in to comment.