2020-04-29 22:44:16 +02:00
package org.hso.ecommerce.controller.shop ;
2020-04-28 22:41:29 +02:00
2020-05-05 22:56:12 +02:00
import org.hso.ecommerce.action.shop.CreateOrderAction ;
import org.hso.ecommerce.entities.booking.BookingAccountEntry ;
import org.hso.ecommerce.entities.booking.PaymentMethod ;
import org.hso.ecommerce.entities.shop.Address ;
2020-05-05 18:59:15 +02:00
import org.hso.ecommerce.entities.shop.Article ;
import org.hso.ecommerce.entities.shop.ShoppingCart ;
2020-05-05 22:56:12 +02:00
import org.hso.ecommerce.entities.user.User ;
import org.hso.ecommerce.repos.booking.BookingAccountEntryRepository ;
import org.hso.ecommerce.repos.booking.BookingRepository ;
2020-05-05 18:59:15 +02:00
import org.hso.ecommerce.repos.shop.ArticleRepository ;
2020-05-19 11:52:41 +02:00
import org.hso.ecommerce.repos.shop.CustomerOrderRepository ;
2020-05-05 22:56:12 +02:00
import org.hso.ecommerce.repos.user.UserRepository ;
import org.hso.ecommerce.repos.warehouse.WarehouseBookingPositionSlotEntryRepository ;
import org.hso.ecommerce.repos.warehouse.WarehouseBookingRepository ;
2020-05-05 18:59:15 +02:00
import org.springframework.beans.factory.annotation.Autowired ;
2020-04-28 22:41:29 +02:00
import org.springframework.stereotype.Controller ;
2020-05-05 22:56:12 +02:00
import org.springframework.web.bind.annotation.* ;
2020-05-05 17:24:25 +02:00
import javax.servlet.http.HttpServletRequest ;
2020-05-05 23:34:17 +02:00
import javax.servlet.http.HttpServletResponse ;
2020-05-05 17:24:25 +02:00
import javax.servlet.http.HttpSession ;
2020-05-05 18:59:15 +02:00
import java.util.ArrayList ;
import java.util.TreeMap ;
2020-04-28 22:41:29 +02:00
@Controller
2020-05-05 17:24:25 +02:00
@RequestMapping ( " /shop/ " )
2020-05-01 10:48:12 +02:00
public class ShopCheckoutController {
2020-05-05 17:24:25 +02:00
2020-05-05 22:56:12 +02:00
@Autowired
private final UserRepository userRepository = null ;
2020-05-05 18:59:15 +02:00
@Autowired
private final ArticleRepository articleRepository = null ;
2020-05-05 22:56:12 +02:00
@Autowired
private final BookingAccountEntryRepository bookingEntryRepository = null ;
@Autowired
private final BookingRepository bookingRepository = null ;
@Autowired
private final WarehouseBookingRepository warehouseBookingRepository = null ;
@Autowired
2020-05-19 11:52:41 +02:00
private final CustomerOrderRepository customerOderRepository = null ;
2020-05-05 22:56:12 +02:00
@Autowired
private final WarehouseBookingPositionSlotEntryRepository wbeseRepo = null ;
2020-05-05 17:24:25 +02:00
@GetMapping ( " /checkout " )
2020-05-05 18:59:15 +02:00
public String shopCheckout ( HttpSession session ,
HttpServletRequest request ,
@RequestAttribute ( value = " shoppingCart " ) ShoppingCart shoppingCart ) {
2020-05-05 17:24:25 +02:00
session . setAttribute ( " afterLogin " , request . getRequestURI ( ) ) ;
2020-05-05 18:59:15 +02:00
CheckoutListTotals totals = new CheckoutListTotals ( ) ;
ArrayList < CheckoutListItem > 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 ) ;
2020-05-05 17:24:25 +02:00
return " shop/checkout " ;
}
2020-05-05 18:59:15 +02:00
public static class CheckoutListTotals {
public int net ;
public TreeMap < Integer , Integer > 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 ( ) ;
}
}
2020-05-05 17:24:25 +02:00
@PostMapping ( " /checkoutFinish " )
2020-05-05 22:56:12 +02:00
public String shopCheckoutFinish (
2020-05-10 14:29:08 +02:00
HttpSession session ,
2020-05-05 23:34:17 +02:00
HttpServletRequest request ,
HttpServletResponse response ,
2020-05-05 22:56:12 +02:00
@RequestAttribute ( value = " user " ) User user ,
@RequestAttribute ( value = " shoppingCart " ) ShoppingCart shoppingCart ,
@RequestParam ( " address " ) String address ,
@RequestParam ( " cardnumber " ) String cardnumber ,
@RequestParam ( " shopping_cart_revision " ) Integer cartRevision ,
@RequestParam ( " expected_total " ) Integer expectedPrice
) {
2020-05-05 23:34:17 +02:00
if ( shoppingCart . getRevision ( ) ! = cartRevision ) {
2020-06-17 18:55:29 +02:00
request . setAttribute ( " error " , " Der Warenkorb wurde zwischenzeitlich bearbeitet. Daher konnte der Kaufvorgang nicht abgeschlossen werden. Bitte versuchen Sie es erneut. " ) ;
2020-05-05 23:34:17 +02:00
response . setStatus ( HttpServletResponse . SC_CONFLICT ) ;
return " shop/checkout " ;
}
2020-05-05 22:56:12 +02:00
// Must be refetched for persitence.
user = userRepository . findById ( user . id ) . get ( ) ;
CreateOrderAction action = new CreateOrderAction (
user ,
expectedPrice ,
Address . fromString ( address ) ,
2020-05-27 14:56:15 +02:00
PaymentMethod . fromCreditCardNumber ( cardnumber ) ,
2020-05-06 14:10:37 +02:00
bookingEntryRepository . getByUser ( user . id ) . orElse ( BookingAccountEntry . newUser ( user ) ) ,
2020-05-05 22:56:12 +02:00
bookingEntryRepository . getByVat ( ) . orElse ( BookingAccountEntry . newVat ( ) ) ,
bookingEntryRepository . getByMain ( ) . orElse ( BookingAccountEntry . newMain ( ) )
) ;
for ( ShoppingCart . ShoppingCartItem item : shoppingCart . getItems ( ) ) {
Article article = articleRepository . findById ( item . getArticleId ( ) ) . get ( ) ;
2020-05-06 14:10:37 +02:00
action . addArticle ( article , item . getAmount ( ) , wbeseRepo . getByArticle ( article . id ) ) ;
2020-05-05 22:56:12 +02:00
}
2020-05-10 14:29:08 +02:00
CreateOrderAction . Result result = null ;
try {
result = action . finish ( ) ;
2020-05-05 22:56:12 +02:00
2020-05-10 14:29:08 +02:00
customerOderRepository . save ( result . customerOrder ) ;
bookingRepository . saveAll ( result . bookings ) ;
warehouseBookingRepository . save ( result . warehouseBooking ) ;
2020-05-05 22:56:12 +02:00
2020-06-25 11:46:01 +02:00
if ( user . defaultPayment = = null ) {
user . defaultPayment = PaymentMethod . fromCreditCardNumber ( cardnumber ) ;
userRepository . save ( user ) ;
}
2020-05-10 14:29:08 +02:00
shoppingCart . clear ( ) ;
} catch ( CreateOrderAction . ArticleNotInStockException e ) {
request . setAttribute ( " error " , " Der Artikel ' " + e . getArticle ( ) . title + " ' ist leider nicht mehr in ausreichender Menge verfügbar. Bitte passen Sie die Artikelmenge an. " ) ;
return shopCheckout ( session , request , shoppingCart ) ;
}
2020-05-05 22:56:12 +02:00
2020-05-05 17:24:25 +02:00
return " shop/checkoutFinish " ;
}
@GetMapping ( " /checkoutFinish " )
public String shopCheckoutFinishGET ( ) {
return " shop/checkoutFinish " ;
}
2020-04-28 22:41:29 +02:00
}