added correct refund logic and points for sales
This commit is contained in:
@@ -18,6 +18,7 @@ public class SaleDTO {
|
||||
private BigDecimal couponDiscountAmount;
|
||||
private BigDecimal employeeDiscountAmount;
|
||||
private BigDecimal loyaltyDiscountAmount;
|
||||
private BigDecimal pointsDiscountAmount;
|
||||
private Integer pointsUsed;
|
||||
private String paymentMethod;
|
||||
private String channel;
|
||||
@@ -144,6 +145,14 @@ public class SaleDTO {
|
||||
return customerName;
|
||||
}
|
||||
|
||||
public BigDecimal getPointsDiscountAmount() {
|
||||
return pointsDiscountAmount;
|
||||
}
|
||||
|
||||
public void setPointsDiscountAmount(BigDecimal pointsDiscountAmount) {
|
||||
this.pointsDiscountAmount = pointsDiscountAmount;
|
||||
}
|
||||
|
||||
// Nested SaleItemDTO
|
||||
public static class SaleItemDTO {
|
||||
private Long saleItemId;
|
||||
|
||||
@@ -302,11 +302,7 @@ public class RefundFragment extends Fragment {
|
||||
}
|
||||
|
||||
private void updateRefundTotal() {
|
||||
BigDecimal total = BigDecimal.ZERO;
|
||||
List<RefundViewModel.RefundItem> cart = viewModel.getRefundCart().getValue();
|
||||
if (cart != null) {
|
||||
for (RefundViewModel.RefundItem item : cart) total = total.add(item.getTotal());
|
||||
}
|
||||
BigDecimal total = viewModel.calculateRefundTotal();
|
||||
binding.tvRefundTotal.setText("Refund Total: $" + total.setScale(2, RoundingMode.HALF_UP));
|
||||
}
|
||||
|
||||
@@ -321,9 +317,7 @@ public class RefundFragment extends Fragment {
|
||||
}
|
||||
|
||||
String payment = PAYMENT_METHODS[binding.spinnerRefundPayment.getSelectedItemPosition()];
|
||||
BigDecimal total = BigDecimal.ZERO;
|
||||
for (RefundViewModel.RefundItem item : viewModel.getRefundCart().getValue()) total = total.add(item.getTotal());
|
||||
final BigDecimal finalTotal = total;
|
||||
final BigDecimal finalTotal = viewModel.calculateRefundTotal();
|
||||
|
||||
DialogUtils.showConfirmDialog(requireContext(), "Confirm Refund",
|
||||
"Process refund for Sale #" + viewModel.getCurrentSale().getSaleId()
|
||||
|
||||
@@ -205,7 +205,13 @@ public class SaleDetailFragment extends Fragment {
|
||||
binding.llEmployeeDiscount.setVisibility(View.GONE);
|
||||
}
|
||||
|
||||
if (sale.getLoyaltyDiscountAmount() != null && sale.getLoyaltyDiscountAmount().compareTo(BigDecimal.ZERO) > 0) {
|
||||
if (sale.getPointsDiscountAmount() != null && sale.getPointsDiscountAmount().compareTo(BigDecimal.ZERO) > 0) {
|
||||
binding.llLoyaltyDiscount.setVisibility(View.VISIBLE);
|
||||
binding.tvSaleLoyaltyDiscount.setText("-$" + String.format(Locale.getDefault(), "%.2f", sale.getPointsDiscountAmount()));
|
||||
if (sale.getPointsUsed() != null) {
|
||||
binding.tvLoyaltyDiscountLabel.setText("Loyalty Discount (" + sale.getPointsUsed() + " pts):");
|
||||
}
|
||||
} else if (sale.getLoyaltyDiscountAmount() != null && sale.getLoyaltyDiscountAmount().compareTo(BigDecimal.ZERO) > 0) {
|
||||
binding.llLoyaltyDiscount.setVisibility(View.VISIBLE);
|
||||
binding.tvSaleLoyaltyDiscount.setText("-$" + String.format(Locale.getDefault(), "%.2f", sale.getLoyaltyDiscountAmount()));
|
||||
} else {
|
||||
@@ -437,7 +443,6 @@ public class SaleDetailFragment extends Fragment {
|
||||
|
||||
if (Boolean.TRUE.equals(viewModel.getUseLoyaltyPoints().getValue())) {
|
||||
dto.setPointsUsed(viewModel.calculatePointsToUse());
|
||||
dto.setLoyaltyDiscountAmount(viewModel.calculateLoyaltyDiscount());
|
||||
}
|
||||
|
||||
viewModel.createSale(dto).observe(getViewLifecycleOwner(), resource -> {
|
||||
|
||||
@@ -10,6 +10,7 @@ import com.example.petstoremobile.repositories.SaleRepository;
|
||||
import com.example.petstoremobile.utils.Resource;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
@@ -125,6 +126,33 @@ public class RefundViewModel extends ViewModel {
|
||||
refundCart.setValue(cart);
|
||||
}
|
||||
|
||||
public BigDecimal calculateRefundTotal() {
|
||||
SaleDTO sale = currentSale.getValue();
|
||||
List<RefundItem> cart = refundCart.getValue();
|
||||
if (sale == null || cart == null || cart.isEmpty()) return BigDecimal.ZERO;
|
||||
|
||||
BigDecimal cartSubtotal = BigDecimal.ZERO;
|
||||
for (RefundItem item : cart) cartSubtotal = cartSubtotal.add(item.getTotal());
|
||||
|
||||
BigDecimal originalSubtotal = sale.getSubtotalAmount();
|
||||
if (originalSubtotal == null || originalSubtotal.compareTo(BigDecimal.ZERO) == 0) {
|
||||
if (sale.getItems() != null) {
|
||||
originalSubtotal = BigDecimal.ZERO;
|
||||
for (SaleDTO.SaleItemDTO item : sale.getItems()) {
|
||||
if (item.getUnitPrice() != null && item.getQuantity() != null)
|
||||
originalSubtotal = originalSubtotal.add(item.getUnitPrice().multiply(BigDecimal.valueOf(Math.abs(item.getQuantity()))));
|
||||
}
|
||||
}
|
||||
}
|
||||
if (originalSubtotal == null || originalSubtotal.compareTo(BigDecimal.ZERO) == 0) return cartSubtotal;
|
||||
|
||||
BigDecimal originalTotal = sale.getTotalAmount();
|
||||
if (originalTotal == null) return cartSubtotal;
|
||||
|
||||
BigDecimal ratio = cartSubtotal.divide(originalSubtotal, 10, RoundingMode.HALF_UP);
|
||||
return originalTotal.abs().multiply(ratio).setScale(2, RoundingMode.HALF_UP);
|
||||
}
|
||||
|
||||
public LiveData<Resource<SaleDTO>> submitRefund(String paymentMethod) {
|
||||
SaleDTO sale = currentSale.getValue();
|
||||
List<RefundItem> cart = refundCart.getValue();
|
||||
|
||||
Reference in New Issue
Block a user