added correct refund logic and points for sales

This commit is contained in:
Alex
2026-04-13 19:46:52 -06:00
parent c244e5742a
commit 572895efa9
4 changed files with 46 additions and 10 deletions

View File

@@ -18,6 +18,7 @@ public class SaleDTO {
private BigDecimal couponDiscountAmount; private BigDecimal couponDiscountAmount;
private BigDecimal employeeDiscountAmount; private BigDecimal employeeDiscountAmount;
private BigDecimal loyaltyDiscountAmount; private BigDecimal loyaltyDiscountAmount;
private BigDecimal pointsDiscountAmount;
private Integer pointsUsed; private Integer pointsUsed;
private String paymentMethod; private String paymentMethod;
private String channel; private String channel;
@@ -144,6 +145,14 @@ public class SaleDTO {
return customerName; return customerName;
} }
public BigDecimal getPointsDiscountAmount() {
return pointsDiscountAmount;
}
public void setPointsDiscountAmount(BigDecimal pointsDiscountAmount) {
this.pointsDiscountAmount = pointsDiscountAmount;
}
// Nested SaleItemDTO // Nested SaleItemDTO
public static class SaleItemDTO { public static class SaleItemDTO {
private Long saleItemId; private Long saleItemId;

View File

@@ -302,11 +302,7 @@ public class RefundFragment extends Fragment {
} }
private void updateRefundTotal() { private void updateRefundTotal() {
BigDecimal total = BigDecimal.ZERO; BigDecimal total = viewModel.calculateRefundTotal();
List<RefundViewModel.RefundItem> cart = viewModel.getRefundCart().getValue();
if (cart != null) {
for (RefundViewModel.RefundItem item : cart) total = total.add(item.getTotal());
}
binding.tvRefundTotal.setText("Refund Total: $" + total.setScale(2, RoundingMode.HALF_UP)); 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()]; String payment = PAYMENT_METHODS[binding.spinnerRefundPayment.getSelectedItemPosition()];
BigDecimal total = BigDecimal.ZERO; final BigDecimal finalTotal = viewModel.calculateRefundTotal();
for (RefundViewModel.RefundItem item : viewModel.getRefundCart().getValue()) total = total.add(item.getTotal());
final BigDecimal finalTotal = total;
DialogUtils.showConfirmDialog(requireContext(), "Confirm Refund", DialogUtils.showConfirmDialog(requireContext(), "Confirm Refund",
"Process refund for Sale #" + viewModel.getCurrentSale().getSaleId() "Process refund for Sale #" + viewModel.getCurrentSale().getSaleId()

View File

@@ -205,7 +205,13 @@ public class SaleDetailFragment extends Fragment {
binding.llEmployeeDiscount.setVisibility(View.GONE); 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.llLoyaltyDiscount.setVisibility(View.VISIBLE);
binding.tvSaleLoyaltyDiscount.setText("-$" + String.format(Locale.getDefault(), "%.2f", sale.getLoyaltyDiscountAmount())); binding.tvSaleLoyaltyDiscount.setText("-$" + String.format(Locale.getDefault(), "%.2f", sale.getLoyaltyDiscountAmount()));
} else { } else {
@@ -437,7 +443,6 @@ public class SaleDetailFragment extends Fragment {
if (Boolean.TRUE.equals(viewModel.getUseLoyaltyPoints().getValue())) { if (Boolean.TRUE.equals(viewModel.getUseLoyaltyPoints().getValue())) {
dto.setPointsUsed(viewModel.calculatePointsToUse()); dto.setPointsUsed(viewModel.calculatePointsToUse());
dto.setLoyaltyDiscountAmount(viewModel.calculateLoyaltyDiscount());
} }
viewModel.createSale(dto).observe(getViewLifecycleOwner(), resource -> { viewModel.createSale(dto).observe(getViewLifecycleOwner(), resource -> {

View File

@@ -10,6 +10,7 @@ import com.example.petstoremobile.repositories.SaleRepository;
import com.example.petstoremobile.utils.Resource; import com.example.petstoremobile.utils.Resource;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@@ -125,6 +126,33 @@ public class RefundViewModel extends ViewModel {
refundCart.setValue(cart); 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) { public LiveData<Resource<SaleDTO>> submitRefund(String paymentMethod) {
SaleDTO sale = currentSale.getValue(); SaleDTO sale = currentSale.getValue();
List<RefundItem> cart = refundCart.getValue(); List<RefundItem> cart = refundCart.getValue();