mirror of
https://github.com/shaulascr/ecommerce_serang.git
synced 2025-08-16 11:47:25 +00:00
fix
This commit is contained in:
@ -310,10 +310,23 @@ class OrderRepository(private val apiService: ApiService) {
|
|||||||
|
|
||||||
suspend fun getAddressDetail(addressId: Int): AddressDetailResponse? {
|
suspend fun getAddressDetail(addressId: Int): AddressDetailResponse? {
|
||||||
return try {
|
return try {
|
||||||
|
Log.d("Order Repository", "Fetching address detail for ID: $addressId")
|
||||||
|
|
||||||
val response = apiService.getDetailAddress(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) {
|
} catch (e: Exception) {
|
||||||
Log.e("OrderRepository", "Error getting address detail $e ")
|
Log.e("Order Repository", "Error getting address detail", e)
|
||||||
null
|
null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -79,6 +79,9 @@ class CheckoutActivity : AppCompatActivity() {
|
|||||||
// Determine if this is Buy Now or Cart checkout
|
// Determine if this is Buy Now or Cart checkout
|
||||||
val isBuyNow = intent.hasExtra(EXTRA_PRODUCT_ID) && !intent.hasExtra(EXTRA_CART_ITEM_IDS)
|
val isBuyNow = intent.hasExtra(EXTRA_PRODUCT_ID) && !intent.hasExtra(EXTRA_CART_ITEM_IDS)
|
||||||
val isWholesaleNow = intent.getBooleanExtra(EXTRA_ISWHOLESALE, false)
|
val isWholesaleNow = intent.getBooleanExtra(EXTRA_ISWHOLESALE, false)
|
||||||
|
val wholesalePricesArray = intent.getIntArrayExtra(EXTRA_CART_ITEM_WHOLESALE_PRICES)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if (isBuyNow) {
|
if (isBuyNow) {
|
||||||
// Process Buy Now flow
|
// Process Buy Now flow
|
||||||
@ -97,17 +100,39 @@ class CheckoutActivity : AppCompatActivity() {
|
|||||||
val cartItemIds = intent.getIntArrayExtra(EXTRA_CART_ITEM_IDS)?.toList() ?: emptyList()
|
val cartItemIds = intent.getIntArrayExtra(EXTRA_CART_ITEM_IDS)?.toList() ?: emptyList()
|
||||||
val isWholesaleArray = intent.getBooleanArrayExtra(EXTRA_CART_ITEM_WHOLESALE)
|
val isWholesaleArray = intent.getBooleanArrayExtra(EXTRA_CART_ITEM_WHOLESALE)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if (cartItemIds.isNotEmpty()) {
|
if (cartItemIds.isNotEmpty()) {
|
||||||
// Create a map of cart item IDs to wholesale status if available
|
// Build map of cartItemId -> isWholesale
|
||||||
val wholesaleMap = if (isWholesaleArray != null && isWholesaleArray.size == cartItemIds.size) {
|
val isWholesaleMap = if (isWholesaleArray != null && isWholesaleArray.size == cartItemIds.size) {
|
||||||
cartItemIds.mapIndexed { index, id -> id to isWholesaleArray[index] }.toMap()
|
cartItemIds.mapIndexed { index, id ->
|
||||||
|
id to isWholesaleArray[index]
|
||||||
|
}.toMap()
|
||||||
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
emptyMap()
|
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 {
|
} 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()
|
finish()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -416,6 +441,7 @@ class CheckoutActivity : AppCompatActivity() {
|
|||||||
const val EXTRA_PRICE = "PRICE"
|
const val EXTRA_PRICE = "PRICE"
|
||||||
const val EXTRA_ISWHOLESALE = "ISWHOLESALE"
|
const val EXTRA_ISWHOLESALE = "ISWHOLESALE"
|
||||||
const val EXTRA_CART_ITEM_WHOLESALE = "EXTRA_CART_ITEM_WHOLESALE"
|
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
|
// Helper methods for starting activity
|
||||||
|
|
||||||
@ -449,13 +475,17 @@ class CheckoutActivity : AppCompatActivity() {
|
|||||||
fun startForCart(
|
fun startForCart(
|
||||||
context: Context,
|
context: Context,
|
||||||
cartItemIds: List<Int>,
|
cartItemIds: List<Int>,
|
||||||
isWholesaleArray: BooleanArray? = null
|
isWholesaleArray: BooleanArray? = null,
|
||||||
|
wholesalePrices: IntArray? = null
|
||||||
) {
|
) {
|
||||||
val intent = Intent(context, CheckoutActivity::class.java).apply {
|
val intent = Intent(context, CheckoutActivity::class.java).apply {
|
||||||
putExtra(EXTRA_CART_ITEM_IDS, cartItemIds.toIntArray())
|
putExtra(EXTRA_CART_ITEM_IDS, cartItemIds.toIntArray())
|
||||||
if (isWholesaleArray != null) {
|
if (isWholesaleArray != null) {
|
||||||
putExtra(EXTRA_CART_ITEM_WHOLESALE, isWholesaleArray)
|
putExtra(EXTRA_CART_ITEM_WHOLESALE, isWholesaleArray)
|
||||||
}
|
}
|
||||||
|
if (wholesalePrices != null) {
|
||||||
|
putExtra(EXTRA_CART_ITEM_WHOLESALE_PRICES, wholesalePrices)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
context.startActivity(intent)
|
context.startActivity(intent)
|
||||||
}
|
}
|
||||||
|
@ -93,30 +93,38 @@ class CheckoutViewModel(private val repository: OrderRepository) : ViewModel() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Initialize checkout from cart
|
// 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 {
|
viewModelScope.launch {
|
||||||
_isLoading.value = true
|
_isLoading.value = true
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Get cart data
|
|
||||||
val cartResult = repository.getCart()
|
val cartResult = repository.getCart()
|
||||||
|
|
||||||
if (cartResult is Result.Success) {
|
if (cartResult is Result.Success) {
|
||||||
// Find matching cart items
|
|
||||||
val matchingItems = mutableListOf<CartItemsItem>()
|
val matchingItems = mutableListOf<CartItemsItem>()
|
||||||
var storeData: DataItemCart? = null
|
var storeData: DataItemCart? = null
|
||||||
|
|
||||||
for (store in cartResult.data) {
|
for (store in cartResult.data) {
|
||||||
val storeItems = store.cartItems.filter { it.cartItemId in cartItemIds }
|
val storeItems = store.cartItems.filter { it.cartItemId in cartItemIds }
|
||||||
if (storeItems.isNotEmpty()) {
|
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
|
storeData = store
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (matchingItems.isNotEmpty() && storeData != null) {
|
if (matchingItems.isNotEmpty() && storeData != null) {
|
||||||
// Create initial OrderRequest object
|
|
||||||
val orderRequest = OrderRequest(
|
val orderRequest = OrderRequest(
|
||||||
addressId = 0,
|
addressId = 0,
|
||||||
paymentMethodId = 0,
|
paymentMethodId = 0,
|
||||||
@ -126,11 +134,9 @@ class CheckoutViewModel(private val repository: OrderRepository) : ViewModel() {
|
|||||||
isNego = false,
|
isNego = false,
|
||||||
cartItemId = cartItemIds,
|
cartItemId = cartItemIds,
|
||||||
shipEtd = "",
|
shipEtd = "",
|
||||||
// Add a list tracking which items are wholesale
|
isReseller = isWholesaleMap.any { it.value }
|
||||||
isReseller = isWholesaleMap.any { it.value } // Set true if any item is wholesale
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Create checkout data
|
|
||||||
_checkoutData.value = CheckoutData(
|
_checkoutData.value = CheckoutData(
|
||||||
orderRequest = orderRequest,
|
orderRequest = orderRequest,
|
||||||
productName = matchingItems.first().productName,
|
productName = matchingItems.first().productName,
|
||||||
@ -138,8 +144,14 @@ class CheckoutViewModel(private val repository: OrderRepository) : ViewModel() {
|
|||||||
sellerId = storeData.storeId,
|
sellerId = storeData.storeId,
|
||||||
isBuyNow = false,
|
isBuyNow = false,
|
||||||
cartItems = matchingItems,
|
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()
|
calculateSubtotal()
|
||||||
calculateTotal()
|
calculateTotal()
|
||||||
@ -155,6 +167,7 @@ class CheckoutViewModel(private val repository: OrderRepository) : ViewModel() {
|
|||||||
_isLoading.value = false
|
_isLoading.value = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getPaymentMethods() {
|
fun getPaymentMethods() {
|
||||||
|
@ -38,6 +38,7 @@
|
|||||||
android:src="@drawable/baseline_edit_24"
|
android:src="@drawable/baseline_edit_24"
|
||||||
android:focusable="true"
|
android:focusable="true"
|
||||||
android:clickable="true"
|
android:clickable="true"
|
||||||
|
android:visibility="gone"
|
||||||
android:layout_marginHorizontal="16dp"
|
android:layout_marginHorizontal="16dp"
|
||||||
app:layout_constraintEnd_toEndOf="parent"
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
app:layout_constraintTop_toTopOf="parent"/>
|
app:layout_constraintTop_toTopOf="parent"/>
|
||||||
|
Reference in New Issue
Block a user