From d681f240e1f5adc128d43769bc3fce74db66567e Mon Sep 17 00:00:00 2001 From: CodeSteak Date: Tue, 5 May 2020 18:59:15 +0200 Subject: [PATCH] implement checkout for non logged in users. --- .../shop/ShopArticleController.java | 9 +- .../shop/ShopCheckoutController.java | 52 ++++++- .../hso/ecommerce/entities/shop/Article.java | 8 + .../ecommerce/entities/shop/ShoppingCart.java | 29 +++- prototype/src/main/resources/db/customers.sql | 7 - .../resources/templates/shop/checkout.html | 141 ++++++------------ 6 files changed, 136 insertions(+), 110 deletions(-) delete mode 100644 prototype/src/main/resources/db/customers.sql diff --git a/prototype/src/main/java/org/hso/ecommerce/controller/shop/ShopArticleController.java b/prototype/src/main/java/org/hso/ecommerce/controller/shop/ShopArticleController.java index bd544b3..5670f49 100644 --- a/prototype/src/main/java/org/hso/ecommerce/controller/shop/ShopArticleController.java +++ b/prototype/src/main/java/org/hso/ecommerce/controller/shop/ShopArticleController.java @@ -63,6 +63,7 @@ public class ShopArticleController { @RequestAttribute(value = "shoppingCart") ShoppingCart shoppingCart, @PathVariable("id") Long id, @RequestParam("quantity") Integer quantity, + @RequestParam(value = "set_amount", required = false) Boolean setAmount, @RequestParam("fastcheckout") Boolean fastcheckout ) { @@ -71,12 +72,16 @@ public class ShopArticleController { throw new RuntimeException("Article not found!"); } - shoppingCart.addArticle(article.get(), quantity); + if (setAmount != null && setAmount) { + shoppingCart.setArticleCount(article.get(), quantity); + } else { + shoppingCart.addArticle(article.get(), quantity); + } if (!fastcheckout) { return "shop/articles/post_add"; } else { - return "shop/checkout"; + return "redirect:/shop/checkout"; } } } diff --git a/prototype/src/main/java/org/hso/ecommerce/controller/shop/ShopCheckoutController.java b/prototype/src/main/java/org/hso/ecommerce/controller/shop/ShopCheckoutController.java index 1573b1b..960a104 100644 --- a/prototype/src/main/java/org/hso/ecommerce/controller/shop/ShopCheckoutController.java +++ b/prototype/src/main/java/org/hso/ecommerce/controller/shop/ShopCheckoutController.java @@ -1,23 +1,73 @@ package org.hso.ecommerce.controller.shop; +import org.hso.ecommerce.entities.shop.Article; +import org.hso.ecommerce.entities.shop.ShoppingCart; +import org.hso.ecommerce.repos.shop.ArticleRepository; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestAttribute; import org.springframework.web.bind.annotation.RequestMapping; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; +import java.util.ArrayList; +import java.util.TreeMap; @Controller @RequestMapping("/shop/") public class ShopCheckoutController { + @Autowired + private final ArticleRepository articleRepository = null; + @GetMapping("/checkout") - public String shopCheckout(HttpSession session, HttpServletRequest request) { + public String shopCheckout(HttpSession session, + HttpServletRequest request, + @RequestAttribute(value = "shoppingCart") ShoppingCart shoppingCart) { session.setAttribute("afterLogin", request.getRequestURI()); + + CheckoutListTotals totals = new CheckoutListTotals(); + ArrayList items = new ArrayList<>(); + for (ShoppingCart.ShoppingCartItem item : shoppingCart.getItems()) { + Article article = articleRepository.findById(item.getArticleId()).get(); + + totals.addItem(article, item.getAmount()); + items.add(new CheckoutListItem(item.getAmount(), article)); + } + + request.setAttribute("checkoutItems", items); + request.setAttribute("checkoutTotals", totals); + return "shop/checkout"; } + public static class CheckoutListTotals { + public int net; + public TreeMap vatAmounts = new TreeMap<>(); + public int total; + + void addItem(Article article, int amount) { + net += article.shopPricePerUnitNetCent * amount; + Integer vatPos = vatAmounts.getOrDefault(article.related.vatPercent, 0) + article.getVat() * amount; + vatAmounts.put(article.related.vatPercent, vatPos); + total += article.getPriceGross() * amount; + } + } + + public static class CheckoutListItem { + public int amount; + public Article article; + public int total; + + public CheckoutListItem(int amount, Article article) { + this.amount = amount; + this.article = article; + this.total = amount * article.getPriceGross(); + } + } + @PostMapping("/checkoutFinish") public String shopCheckoutFinish() { return "shop/checkoutFinish"; diff --git a/prototype/src/main/java/org/hso/ecommerce/entities/shop/Article.java b/prototype/src/main/java/org/hso/ecommerce/entities/shop/Article.java index 6cd198e..2e066cd 100644 --- a/prototype/src/main/java/org/hso/ecommerce/entities/shop/Article.java +++ b/prototype/src/main/java/org/hso/ecommerce/entities/shop/Article.java @@ -37,4 +37,12 @@ public class Article { @ManyToMany @JoinTable(name = "article_categories_bindings") public Set categories = new HashSet<>(); + + public int getVat() { + return (shopPricePerUnitNetCent * related.vatPercent) / 100; + } + + public int getPriceGross() { + return shopPricePerUnitNetCent + getVat(); + } } diff --git a/prototype/src/main/java/org/hso/ecommerce/entities/shop/ShoppingCart.java b/prototype/src/main/java/org/hso/ecommerce/entities/shop/ShoppingCart.java index f7955ec..aee94c2 100644 --- a/prototype/src/main/java/org/hso/ecommerce/entities/shop/ShoppingCart.java +++ b/prototype/src/main/java/org/hso/ecommerce/entities/shop/ShoppingCart.java @@ -1,6 +1,7 @@ package org.hso.ecommerce.entities.shop; import java.util.ArrayList; +import java.util.List; // Not a db entity. Just for session storage public class ShoppingCart { @@ -13,6 +14,10 @@ public class ShoppingCart { items = new ArrayList<>(); } + public List getItems() { + return items; + } + public int getItemCount() { int count = 0; @@ -40,7 +45,25 @@ public class ShoppingCart { items.add(new ShoppingCartItem(quantity, article)); } - private static class ShoppingCartItem { + public void setArticleCount(Article article, Integer quantity) { + this.revision++; + + boolean found = false; + for (ShoppingCartItem i : items) { + if (i.getArticleId() == article.id) { + i.setAmount(quantity); + found = true; + break; + } + } + if (!found) { + items.add(new ShoppingCartItem(quantity, article)); + } + + items.removeIf(i -> i.getAmount() == 0); + } + + public static class ShoppingCartItem { private int amount; private final long articleId; @@ -60,5 +83,9 @@ public class ShoppingCart { public long getArticleId() { return articleId; } + + public void setAmount(Integer amount) { + this.amount = amount; + } } } diff --git a/prototype/src/main/resources/db/customers.sql b/prototype/src/main/resources/db/customers.sql deleted file mode 100644 index b1a108f..0000000 --- a/prototype/src/main/resources/db/customers.sql +++ /dev/null @@ -1,7 +0,0 @@ -CREATE TABLE "customers" ( - "id" INTEGER PRIMARY KEY AUTOINCREMENT, - "lastname" TEXT, - "firstname" TEXT, - "username" TEXT, - "password" TEXT -); diff --git a/prototype/src/main/resources/templates/shop/checkout.html b/prototype/src/main/resources/templates/shop/checkout.html index e6bb15d..e017f9e 100644 --- a/prototype/src/main/resources/templates/shop/checkout.html +++ b/prototype/src/main/resources/templates/shop/checkout.html @@ -31,91 +31,31 @@ Menge - - - Kamera - 100,50 EUR - - - - - - - - - - Earbuds - 63,95 EUR - - - - - - - - - - USB-Magic Light - 11,90 EUR - - - - - - - - - - 3D Magic Stativ - 15,99 EUR - - - - - - - - - - Ersatzfernbedinung - 7,95 EUR - - - - - - - + + + + + EUR + +
+ + + + +
+ + +
@@ -151,23 +91,26 @@ Musterstraße 42 - - - - - - - - - - - - - + + + + + + + - +
Artikel (Netto)200,29 EUR
Bonuspunkte-5,00 EUR
Umsatzsteuer (19%)35,00 EUR
Umsatzsteuer (7%)2,50 EUR + EUR +
Umsatzsteuer (%) + EUR +
Gesamt:240,79 EUR + EUR +