This commit is contained in:
shaulascr
2025-08-15 05:29:54 +07:00
parent a6d5d10e78
commit 6efbcde784
4 changed files with 74 additions and 17 deletions

View File

@ -310,10 +310,23 @@ class OrderRepository(private val apiService: ApiService) {
suspend fun getAddressDetail(addressId: Int): AddressDetailResponse? {
return try {
Log.d("Order Repository", "Fetching address detail for ID: $addressId")
val response = apiService.getDetailAddress(addressId)
if (response.isSuccessful) response.body() else null
Log.d("Order Repository", "Response code: ${response.code()}")
Log.d("Order Repository", "Response message: ${response.message()}")
if (response.isSuccessful) {
val body = response.body()
Log.d("Order Repository", "Address detail response body: $body")
body
} else {
Log.w("Order Repository", "Failed to get address detail. Error body: ${response.errorBody()?.string()}")
null
}
} catch (e: Exception) {
Log.e("OrderRepository", "Error getting address detail $e ")
Log.e("Order Repository", "Error getting address detail", e)
null
}
}

View File

@ -79,6 +79,9 @@ class CheckoutActivity : AppCompatActivity() {
// Determine if this is Buy Now or Cart checkout
val isBuyNow = intent.hasExtra(EXTRA_PRODUCT_ID) && !intent.hasExtra(EXTRA_CART_ITEM_IDS)
val isWholesaleNow = intent.getBooleanExtra(EXTRA_ISWHOLESALE, false)
val wholesalePricesArray = intent.getIntArrayExtra(EXTRA_CART_ITEM_WHOLESALE_PRICES)
if (isBuyNow) {
// Process Buy Now flow
@ -97,17 +100,39 @@ class CheckoutActivity : AppCompatActivity() {
val cartItemIds = intent.getIntArrayExtra(EXTRA_CART_ITEM_IDS)?.toList() ?: emptyList()
val isWholesaleArray = intent.getBooleanArrayExtra(EXTRA_CART_ITEM_WHOLESALE)
if (cartItemIds.isNotEmpty()) {
// Create a map of cart item IDs to wholesale status if available
val wholesaleMap = if (isWholesaleArray != null && isWholesaleArray.size == cartItemIds.size) {
cartItemIds.mapIndexed { index, id -> id to isWholesaleArray[index] }.toMap()
// Build map of cartItemId -> isWholesale
val isWholesaleMap = if (isWholesaleArray != null && isWholesaleArray.size == cartItemIds.size) {
cartItemIds.mapIndexed { index, id ->
id to isWholesaleArray[index]
}.toMap()
} else {
emptyMap()
}
viewModel.initializeFromCart(cartItemIds, wholesaleMap)
// Build wholesalePriceMap
val wholesalePriceMap = cartItemIds.mapIndexed { index, id ->
id to (wholesalePricesArray?.get(index) ?: 0)
}.toMap()
viewModel.initializeFromCart(
cartItemIds,
isWholesaleMap,
wholesalePriceMap
)
Log.d("CheckoutActivity", "Cart IDs: $cartItemIds")
Log.d("CheckoutActivity", "IsWholesaleArray: ${isWholesaleArray?.joinToString()}")
Log.d("CheckoutActivity", "WholesalePricesArray: ${wholesalePricesArray?.joinToString()}")
Log.d("CheckoutActivity", "IsWholesaleMap: $isWholesaleMap")
Log.d("CheckoutActivity", "WholesalePriceMap: $wholesalePriceMap")
} else {
Toast.makeText(this, "Error: No cart items specified", Toast.LENGTH_SHORT).show()
Toast.makeText(this, "Tidak ada item keranjang", Toast.LENGTH_SHORT).show()
finish()
}
}
@ -416,6 +441,7 @@ class CheckoutActivity : AppCompatActivity() {
const val EXTRA_PRICE = "PRICE"
const val EXTRA_ISWHOLESALE = "ISWHOLESALE"
const val EXTRA_CART_ITEM_WHOLESALE = "EXTRA_CART_ITEM_WHOLESALE"
const val EXTRA_CART_ITEM_WHOLESALE_PRICES = "EXTRA_CART_ITEM_WHOLESALE_PRICES"
// Helper methods for starting activity
@ -449,13 +475,17 @@ class CheckoutActivity : AppCompatActivity() {
fun startForCart(
context: Context,
cartItemIds: List<Int>,
isWholesaleArray: BooleanArray? = null
isWholesaleArray: BooleanArray? = null,
wholesalePrices: IntArray? = null
) {
val intent = Intent(context, CheckoutActivity::class.java).apply {
putExtra(EXTRA_CART_ITEM_IDS, cartItemIds.toIntArray())
if (isWholesaleArray != null) {
putExtra(EXTRA_CART_ITEM_WHOLESALE, isWholesaleArray)
}
if (wholesalePrices != null) {
putExtra(EXTRA_CART_ITEM_WHOLESALE_PRICES, wholesalePrices)
}
}
context.startActivity(intent)
}

View File

@ -93,30 +93,38 @@ class CheckoutViewModel(private val repository: OrderRepository) : ViewModel() {
}
// Initialize checkout from cart
fun initializeFromCart(cartItemIds: List<Int>, isWholesaleMap: Map<Int, Boolean> = emptyMap()) {
fun initializeFromCart(
cartItemIds: List<Int>,
isWholesaleMap: Map<Int, Boolean> = emptyMap(),
wholesalePriceMap: Map<Int, Int> = emptyMap() // new
) {
viewModelScope.launch {
_isLoading.value = true
try {
// Get cart data
val cartResult = repository.getCart()
if (cartResult is Result.Success) {
// Find matching cart items
val matchingItems = mutableListOf<CartItemsItem>()
var storeData: DataItemCart? = null
for (store in cartResult.data) {
val storeItems = store.cartItems.filter { it.cartItemId in cartItemIds }
if (storeItems.isNotEmpty()) {
matchingItems.addAll(storeItems)
// ✅ Override price with wholesale price if exists
val updatedItems = storeItems.map { item ->
val wholesalePrice = wholesalePriceMap[item.cartItemId]
if (wholesalePrice != null) {
item.copy(price = wholesalePrice.toInt())
} else item
}
matchingItems.addAll(updatedItems)
storeData = store
break
}
}
if (matchingItems.isNotEmpty() && storeData != null) {
// Create initial OrderRequest object
val orderRequest = OrderRequest(
addressId = 0,
paymentMethodId = 0,
@ -126,11 +134,9 @@ class CheckoutViewModel(private val repository: OrderRepository) : ViewModel() {
isNego = false,
cartItemId = cartItemIds,
shipEtd = "",
// Add a list tracking which items are wholesale
isReseller = isWholesaleMap.any { it.value } // Set true if any item is wholesale
isReseller = isWholesaleMap.any { it.value }
)
// Create checkout data
_checkoutData.value = CheckoutData(
orderRequest = orderRequest,
productName = matchingItems.first().productName,
@ -138,8 +144,14 @@ class CheckoutViewModel(private val repository: OrderRepository) : ViewModel() {
sellerId = storeData.storeId,
isBuyNow = false,
cartItems = matchingItems,
cartItemWholesaleMap = isWholesaleMap // Store the wholesale map
cartItemWholesaleMap = isWholesaleMap
)
Log.d(TAG, "CheckoutData initialized: ${_checkoutData.value}")
Log.d(TAG, "Matching cart items: ${matchingItems.size}")
Log.d(TAG, "Cart items: ${matchingItems.map { it.productName to it.price }}")
Log.d(TAG, "IsWholesaleMap passed: $isWholesaleMap")
Log.d(TAG, "WholesalePriceMap passed: $wholesalePriceMap")
calculateSubtotal()
calculateTotal()
@ -155,6 +167,7 @@ class CheckoutViewModel(private val repository: OrderRepository) : ViewModel() {
_isLoading.value = false
}
}
}
fun getPaymentMethods() {

View File

@ -38,6 +38,7 @@
android:src="@drawable/baseline_edit_24"
android:focusable="true"
android:clickable="true"
android:visibility="gone"
android:layout_marginHorizontal="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"/>