Fixed Coupon to use in sales

This commit is contained in:
Alex
2026-04-12 00:25:33 -06:00
parent 0311887185
commit 96e6cd6dc7
4 changed files with 120 additions and 9 deletions

View File

@@ -201,7 +201,7 @@ public class SaleDetailFragment extends Fragment {
if (sale.getItems() != null) {
binding.llSaleItems.removeAllViews();
for (SaleDTO.SaleItemDTO item : sale.getItems()) {
addItemRow(item.getProductName(), Math.abs(item.getQuantity()), item.getUnitPrice());
addItemRow(item.getProductName(), Math.abs(item.getQuantity()), item.getUnitPrice(), null);
}
}
}
@@ -308,15 +308,16 @@ public class SaleDetailFragment extends Fragment {
break;
}
}
addItemRow(name, item.getQuantity(), price);
addItemRow(name, item.getQuantity(), price, item.getProdId());
}
}
private void addItemRow(String name, int qty, BigDecimal price) {
private void addItemRow(String name, int qty, BigDecimal price, Long prodId) {
if (getContext() == null) return;
LinearLayout row = new LinearLayout(getContext());
row.setOrientation(LinearLayout.HORIZONTAL);
row.setPadding(0, 8, 0, 8);
row.setGravity(android.view.Gravity.CENTER_VERTICAL);
TextView tvName = new TextView(getContext());
tvName.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 2f));
@@ -333,6 +334,20 @@ public class SaleDetailFragment extends Fragment {
row.addView(tvName);
row.addView(tvQty);
row.addView(tvPrice);
if (prodId != null) {
Button btnRemove = new Button(getContext());
btnRemove.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
btnRemove.setText("");
btnRemove.setTextSize(12f);
btnRemove.setBackgroundTintList(android.content.res.ColorStateList.valueOf(0xFFE53935));
btnRemove.setTextColor(0xFFFFFFFF);
btnRemove.setPadding(16, 4, 16, 4);
btnRemove.setOnClickListener(v -> viewModel.removeFromCart(prodId));
row.addView(btnRemove);
}
binding.llSaleItems.addView(row);
}

View File

@@ -95,6 +95,12 @@ public class SaleDetailViewModel extends ViewModel {
cartItems.setValue(currentCart);
}
public void removeFromCart(Long prodId) {
List<SaleDTO.SaleItemDTO> currentCart = new ArrayList<>(cartItems.getValue());
currentCart.removeIf(item -> item.getProdId().equals(prodId));
cartItems.setValue(currentCart);
}
public LiveData<List<SaleDTO.SaleItemDTO>> getCartItems() { return cartItems; }
public LiveData<Resource<CouponDTO>> lookupCoupon(String code) {