Backend refactor #141
@@ -22,6 +22,12 @@ public class SaleRequest {
|
||||
|
||||
private Long customerId;
|
||||
|
||||
private String channel;
|
||||
|
||||
private Long couponId;
|
||||
|
||||
private Long cartId;
|
||||
|
||||
public Long getStoreId() {
|
||||
return storeId;
|
||||
}
|
||||
@@ -70,6 +76,30 @@ public class SaleRequest {
|
||||
this.customerId = customerId;
|
||||
}
|
||||
|
||||
public String getChannel() {
|
||||
return channel;
|
||||
}
|
||||
|
||||
public void setChannel(String channel) {
|
||||
this.channel = channel;
|
||||
}
|
||||
|
||||
public Long getCouponId() {
|
||||
return couponId;
|
||||
}
|
||||
|
||||
public void setCouponId(Long couponId) {
|
||||
this.couponId = couponId;
|
||||
}
|
||||
|
||||
public Long getCartId() {
|
||||
return cartId;
|
||||
}
|
||||
|
||||
public void setCartId(Long cartId) {
|
||||
this.cartId = cartId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
@@ -80,12 +110,15 @@ public class SaleRequest {
|
||||
Objects.equals(items, that.items) &&
|
||||
Objects.equals(isRefund, that.isRefund) &&
|
||||
Objects.equals(originalSaleId, that.originalSaleId) &&
|
||||
Objects.equals(customerId, that.customerId);
|
||||
Objects.equals(customerId, that.customerId) &&
|
||||
Objects.equals(channel, that.channel) &&
|
||||
Objects.equals(couponId, that.couponId) &&
|
||||
Objects.equals(cartId, that.cartId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(storeId, paymentMethod, items, isRefund, originalSaleId, customerId);
|
||||
return Objects.hash(storeId, paymentMethod, items, isRefund, originalSaleId, customerId, channel, couponId, cartId);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -97,6 +130,9 @@ public class SaleRequest {
|
||||
", isRefund=" + isRefund +
|
||||
", originalSaleId=" + originalSaleId +
|
||||
", customerId=" + customerId +
|
||||
", channel='" + channel + '\'' +
|
||||
", couponId=" + couponId +
|
||||
", cartId=" + cartId +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,13 @@ public class SaleResponse {
|
||||
private Long storeId;
|
||||
private String storeName;
|
||||
private BigDecimal totalAmount;
|
||||
private BigDecimal subtotalAmount;
|
||||
private BigDecimal couponDiscountAmount;
|
||||
private BigDecimal employeeDiscountAmount;
|
||||
private Integer pointsEarned;
|
||||
private String channel;
|
||||
private Long couponId;
|
||||
private Long cartId;
|
||||
private String paymentMethod;
|
||||
private Boolean isRefund;
|
||||
private Long originalSaleId;
|
||||
@@ -22,21 +29,6 @@ public class SaleResponse {
|
||||
public SaleResponse() {
|
||||
}
|
||||
|
||||
public SaleResponse(Long saleId, LocalDateTime saleDate, Long employeeId, String employeeName, Long storeId, String storeName, BigDecimal totalAmount, String paymentMethod, Boolean isRefund, Long originalSaleId, List<SaleItemResponse> items, LocalDateTime createdAt) {
|
||||
this.saleId = saleId;
|
||||
this.saleDate = saleDate;
|
||||
this.employeeId = employeeId;
|
||||
this.employeeName = employeeName;
|
||||
this.storeId = storeId;
|
||||
this.storeName = storeName;
|
||||
this.totalAmount = totalAmount;
|
||||
this.paymentMethod = paymentMethod;
|
||||
this.isRefund = isRefund;
|
||||
this.originalSaleId = originalSaleId;
|
||||
this.items = items;
|
||||
this.createdAt = createdAt;
|
||||
}
|
||||
|
||||
public Long getSaleId() {
|
||||
return saleId;
|
||||
}
|
||||
@@ -93,6 +85,62 @@ public class SaleResponse {
|
||||
this.totalAmount = totalAmount;
|
||||
}
|
||||
|
||||
public BigDecimal getSubtotalAmount() {
|
||||
return subtotalAmount;
|
||||
}
|
||||
|
||||
public void setSubtotalAmount(BigDecimal subtotalAmount) {
|
||||
this.subtotalAmount = subtotalAmount;
|
||||
}
|
||||
|
||||
public BigDecimal getCouponDiscountAmount() {
|
||||
return couponDiscountAmount;
|
||||
}
|
||||
|
||||
public void setCouponDiscountAmount(BigDecimal couponDiscountAmount) {
|
||||
this.couponDiscountAmount = couponDiscountAmount;
|
||||
}
|
||||
|
||||
public BigDecimal getEmployeeDiscountAmount() {
|
||||
return employeeDiscountAmount;
|
||||
}
|
||||
|
||||
public void setEmployeeDiscountAmount(BigDecimal employeeDiscountAmount) {
|
||||
this.employeeDiscountAmount = employeeDiscountAmount;
|
||||
}
|
||||
|
||||
public Integer getPointsEarned() {
|
||||
return pointsEarned;
|
||||
}
|
||||
|
||||
public void setPointsEarned(Integer pointsEarned) {
|
||||
this.pointsEarned = pointsEarned;
|
||||
}
|
||||
|
||||
public String getChannel() {
|
||||
return channel;
|
||||
}
|
||||
|
||||
public void setChannel(String channel) {
|
||||
this.channel = channel;
|
||||
}
|
||||
|
||||
public Long getCouponId() {
|
||||
return couponId;
|
||||
}
|
||||
|
||||
public void setCouponId(Long couponId) {
|
||||
this.couponId = couponId;
|
||||
}
|
||||
|
||||
public Long getCartId() {
|
||||
return cartId;
|
||||
}
|
||||
|
||||
public void setCartId(Long cartId) {
|
||||
this.cartId = cartId;
|
||||
}
|
||||
|
||||
public String getPaymentMethod() {
|
||||
return paymentMethod;
|
||||
}
|
||||
|
||||
@@ -46,6 +46,29 @@ public class Sale {
|
||||
@JoinColumn(name = "originalSaleId")
|
||||
private Sale originalSale;
|
||||
|
||||
@Column(nullable = false, length = 20)
|
||||
private String channel = "IN_STORE";
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "cartId")
|
||||
private Cart cart;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "couponId")
|
||||
private Coupon coupon;
|
||||
|
||||
@Column(precision = 10, scale = 2)
|
||||
private BigDecimal subtotalAmount;
|
||||
|
||||
@Column(nullable = false, precision = 10, scale = 2)
|
||||
private BigDecimal couponDiscountAmount = BigDecimal.ZERO;
|
||||
|
||||
@Column(nullable = false, precision = 10, scale = 2)
|
||||
private BigDecimal employeeDiscountAmount = BigDecimal.ZERO;
|
||||
|
||||
@Column(nullable = false)
|
||||
private Integer pointsEarned = 0;
|
||||
|
||||
@OneToMany(mappedBy = "sale", cascade = CascadeType.ALL)
|
||||
private List<SaleItem> items = new ArrayList<>();
|
||||
|
||||
@@ -132,6 +155,62 @@ public class Sale {
|
||||
this.originalSale = originalSale;
|
||||
}
|
||||
|
||||
public String getChannel() {
|
||||
return channel;
|
||||
}
|
||||
|
||||
public void setChannel(String channel) {
|
||||
this.channel = channel;
|
||||
}
|
||||
|
||||
public Cart getCart() {
|
||||
return cart;
|
||||
}
|
||||
|
||||
public void setCart(Cart cart) {
|
||||
this.cart = cart;
|
||||
}
|
||||
|
||||
public Coupon getCoupon() {
|
||||
return coupon;
|
||||
}
|
||||
|
||||
public void setCoupon(Coupon coupon) {
|
||||
this.coupon = coupon;
|
||||
}
|
||||
|
||||
public BigDecimal getSubtotalAmount() {
|
||||
return subtotalAmount;
|
||||
}
|
||||
|
||||
public void setSubtotalAmount(BigDecimal subtotalAmount) {
|
||||
this.subtotalAmount = subtotalAmount;
|
||||
}
|
||||
|
||||
public BigDecimal getCouponDiscountAmount() {
|
||||
return couponDiscountAmount;
|
||||
}
|
||||
|
||||
public void setCouponDiscountAmount(BigDecimal couponDiscountAmount) {
|
||||
this.couponDiscountAmount = couponDiscountAmount;
|
||||
}
|
||||
|
||||
public BigDecimal getEmployeeDiscountAmount() {
|
||||
return employeeDiscountAmount;
|
||||
}
|
||||
|
||||
public void setEmployeeDiscountAmount(BigDecimal employeeDiscountAmount) {
|
||||
this.employeeDiscountAmount = employeeDiscountAmount;
|
||||
}
|
||||
|
||||
public Integer getPointsEarned() {
|
||||
return pointsEarned;
|
||||
}
|
||||
|
||||
public void setPointsEarned(Integer pointsEarned) {
|
||||
this.pointsEarned = pointsEarned;
|
||||
}
|
||||
|
||||
public List<SaleItem> getItems() {
|
||||
return items;
|
||||
}
|
||||
|
||||
@@ -25,13 +25,17 @@ public class SaleService {
|
||||
private final StoreRepository storeRepository;
|
||||
private final InventoryRepository inventoryRepository;
|
||||
private final UserRepository userRepository;
|
||||
private final CouponRepository couponRepository;
|
||||
private final CartRepository cartRepository;
|
||||
|
||||
public SaleService(SaleRepository saleRepository, ProductRepository productRepository, StoreRepository storeRepository, InventoryRepository inventoryRepository, UserRepository userRepository) {
|
||||
public SaleService(SaleRepository saleRepository, ProductRepository productRepository, StoreRepository storeRepository, InventoryRepository inventoryRepository, UserRepository userRepository, CouponRepository couponRepository, CartRepository cartRepository) {
|
||||
this.saleRepository = saleRepository;
|
||||
this.productRepository = productRepository;
|
||||
this.storeRepository = storeRepository;
|
||||
this.inventoryRepository = inventoryRepository;
|
||||
this.userRepository = userRepository;
|
||||
this.couponRepository = couponRepository;
|
||||
this.cartRepository = cartRepository;
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
@@ -72,6 +76,19 @@ public class SaleService {
|
||||
sale.setStore(store);
|
||||
sale.setPaymentMethod(normalizePaymentMethod(request.getPaymentMethod()));
|
||||
sale.setIsRefund(request.getIsRefund() != null ? request.getIsRefund() : false);
|
||||
sale.setChannel(request.getChannel() != null ? request.getChannel() : "IN_STORE");
|
||||
|
||||
if (request.getCouponId() != null) {
|
||||
Coupon coupon = couponRepository.findById(request.getCouponId())
|
||||
.orElseThrow(() -> new ResourceNotFoundException("Coupon not found with id: " + request.getCouponId()));
|
||||
sale.setCoupon(coupon);
|
||||
}
|
||||
|
||||
if (request.getCartId() != null) {
|
||||
Cart cart = cartRepository.findById(request.getCartId())
|
||||
.orElseThrow(() -> new ResourceNotFoundException("Cart not found with id: " + request.getCartId()));
|
||||
sale.setCart(cart);
|
||||
}
|
||||
|
||||
if (request.getCustomerId() != null) {
|
||||
User customer = userRepository.findById(request.getCustomerId())
|
||||
@@ -186,6 +203,17 @@ public class SaleService {
|
||||
}
|
||||
|
||||
response.setTotalAmount(sale.getTotalAmount());
|
||||
response.setSubtotalAmount(sale.getSubtotalAmount());
|
||||
response.setCouponDiscountAmount(sale.getCouponDiscountAmount());
|
||||
response.setEmployeeDiscountAmount(sale.getEmployeeDiscountAmount());
|
||||
response.setPointsEarned(sale.getPointsEarned());
|
||||
response.setChannel(sale.getChannel());
|
||||
if (sale.getCoupon() != null) {
|
||||
response.setCouponId(sale.getCoupon().getCouponId());
|
||||
}
|
||||
if (sale.getCart() != null) {
|
||||
response.setCartId(sale.getCart().getCartId());
|
||||
}
|
||||
response.setPaymentMethod(sale.getPaymentMethod());
|
||||
response.setIsRefund(sale.getIsRefund());
|
||||
if (sale.getOriginalSale() != null) {
|
||||
|
||||
Reference in New Issue
Block a user