mirror of
https://github.com/shaulascr/ecommerce_serang.git
synced 2025-12-15 07:31:02 +00:00
fix wholesale price and edit profile
This commit is contained in:
@ -151,7 +151,7 @@ class CartActivity : AppCompatActivity() {
|
||||
|
||||
// Debug log
|
||||
Log.d(
|
||||
TAG,
|
||||
TAG,
|
||||
"cartItemId: ${updatedCartItem.cartItemId}, " +
|
||||
"isWholesale: ${info.isWholesale}, " +
|
||||
"wholesalePrice: $wholesalePrice, " +
|
||||
@ -164,7 +164,17 @@ class CartActivity : AppCompatActivity() {
|
||||
val cartItemIds = updatedItems.map { it.cartItem.cartItemId }
|
||||
val wholesaleArray = updatedItems.map { it.isWholesale }.toBooleanArray()
|
||||
|
||||
CheckoutActivity.startForCart(this, cartItemIds, wholesaleArray)
|
||||
// FIX: Pass wholesale prices as IntArray
|
||||
val wholesalePricesArray = updatedItems.map { info ->
|
||||
if (info.isWholesale) {
|
||||
val wholesalePrice = wholesalePriceMap[info.cartItem.cartItemId]
|
||||
wholesalePrice?.toInt() ?: info.cartItem.price
|
||||
} else {
|
||||
info.cartItem.price
|
||||
}
|
||||
}.toIntArray()
|
||||
|
||||
CheckoutActivity.startForCart(this, cartItemIds, wholesaleArray, wholesalePricesArray)
|
||||
}
|
||||
|
||||
private fun observeViewModel() {
|
||||
|
||||
@ -79,9 +79,6 @@ 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
|
||||
@ -99,8 +96,7 @@ class CheckoutActivity : AppCompatActivity() {
|
||||
// Process Cart checkout flow
|
||||
val cartItemIds = intent.getIntArrayExtra(EXTRA_CART_ITEM_IDS)?.toList() ?: emptyList()
|
||||
val isWholesaleArray = intent.getBooleanArrayExtra(EXTRA_CART_ITEM_WHOLESALE)
|
||||
|
||||
|
||||
val wholesalePricesArray = intent.getIntArrayExtra(EXTRA_CART_ITEM_WHOLESALE_PRICES)
|
||||
|
||||
if (cartItemIds.isNotEmpty()) {
|
||||
// Build map of cartItemId -> isWholesale
|
||||
@ -108,17 +104,18 @@ class CheckoutActivity : AppCompatActivity() {
|
||||
cartItemIds.mapIndexed { index, id ->
|
||||
id to isWholesaleArray[index]
|
||||
}.toMap()
|
||||
|
||||
|
||||
} else {
|
||||
emptyMap()
|
||||
}
|
||||
|
||||
|
||||
// Build wholesalePriceMap
|
||||
val wholesalePriceMap = cartItemIds.mapIndexed { index, id ->
|
||||
id to (wholesalePricesArray?.get(index) ?: 0)
|
||||
}.toMap()
|
||||
// Build wholesalePriceMap - FIX: Map cartItemId to wholesale price
|
||||
val wholesalePriceMap = if (wholesalePricesArray != null && wholesalePricesArray.size == cartItemIds.size) {
|
||||
cartItemIds.mapIndexed { index, id ->
|
||||
id to wholesalePricesArray[index]
|
||||
}.toMap()
|
||||
} else {
|
||||
emptyMap()
|
||||
}
|
||||
|
||||
viewModel.initializeFromCart(
|
||||
cartItemIds,
|
||||
|
||||
@ -96,7 +96,7 @@ class CheckoutViewModel(private val repository: OrderRepository) : ViewModel() {
|
||||
fun initializeFromCart(
|
||||
cartItemIds: List<Int>,
|
||||
isWholesaleMap: Map<Int, Boolean> = emptyMap(),
|
||||
wholesalePriceMap: Map<Int, Int> = emptyMap() // new
|
||||
wholesalePriceMap: Map<Int, Int> = emptyMap()
|
||||
) {
|
||||
viewModelScope.launch {
|
||||
_isLoading.value = true
|
||||
@ -111,13 +111,21 @@ class CheckoutViewModel(private val repository: OrderRepository) : ViewModel() {
|
||||
for (store in cartResult.data) {
|
||||
val storeItems = store.cartItems.filter { it.cartItemId in cartItemIds }
|
||||
if (storeItems.isNotEmpty()) {
|
||||
// ✅ Override price with wholesale price if exists
|
||||
// ✅ Apply wholesale prices - Replace item prices with wholesale prices
|
||||
val updatedItems = storeItems.map { item ->
|
||||
val wholesalePrice = wholesalePriceMap[item.cartItemId]
|
||||
if (wholesalePrice != null) {
|
||||
item.copy(price = wholesalePrice.toInt())
|
||||
} else item
|
||||
val isWholesale = isWholesaleMap[item.cartItemId] ?: false
|
||||
|
||||
// Use wholesale price if item is wholesale and price exists
|
||||
if (isWholesale && wholesalePrice != null) {
|
||||
Log.d(TAG, "Applying wholesale price for item ${item.cartItemId}: ${item.price} -> $wholesalePrice")
|
||||
item.copy(price = wholesalePrice)
|
||||
} else {
|
||||
Log.d(TAG, "Using regular price for item ${item.cartItemId}: ${item.price}")
|
||||
item
|
||||
}
|
||||
}
|
||||
|
||||
matchingItems.addAll(updatedItems)
|
||||
storeData = store
|
||||
break
|
||||
@ -143,16 +151,17 @@ class CheckoutViewModel(private val repository: OrderRepository) : ViewModel() {
|
||||
sellerName = storeData.storeName,
|
||||
sellerId = storeData.storeId,
|
||||
isBuyNow = false,
|
||||
cartItems = matchingItems,
|
||||
cartItems = matchingItems, // These now have updated wholesale prices
|
||||
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")
|
||||
Log.d(TAG, "CheckoutData initialized with ${matchingItems.size} items")
|
||||
matchingItems.forEachIndexed { index, item ->
|
||||
val isWholesale = isWholesaleMap[item.cartItemId] ?: false
|
||||
Log.d(TAG, "Item $index: ${item.productName}, Price: ${item.price}, IsWholesale: $isWholesale")
|
||||
}
|
||||
|
||||
// Calculate totals with updated prices
|
||||
calculateSubtotal()
|
||||
calculateTotal()
|
||||
} else {
|
||||
@ -163,11 +172,11 @@ class CheckoutViewModel(private val repository: OrderRepository) : ViewModel() {
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
_errorMessage.value = "Error: ${e.message}"
|
||||
Log.e(TAG, "Error in initializeFromCart", e)
|
||||
} finally {
|
||||
_isLoading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fun getPaymentMethods() {
|
||||
@ -418,8 +427,6 @@ class CheckoutViewModel(private val repository: OrderRepository) : ViewModel() {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
companion object {
|
||||
private const val TAG = "CheckoutViewModel"
|
||||
}
|
||||
|
||||
@ -34,6 +34,8 @@ class SingleCartItemAdapter(private val cartItem: CartItemsItem) :
|
||||
// Load placeholder image
|
||||
Glide.with(ivProduct.context)
|
||||
.load(R.drawable.placeholder_image)
|
||||
.placeholder(R.drawable.placeholder_image)
|
||||
.error(R.drawable.placeholder_image)
|
||||
.into(ivProduct)
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,10 +1,12 @@
|
||||
package com.alya.ecommerce_serang.ui.profile.editprofile
|
||||
|
||||
import android.Manifest
|
||||
import android.app.Activity
|
||||
import android.app.DatePickerDialog
|
||||
import android.content.Intent
|
||||
import android.content.pm.PackageManager
|
||||
import android.net.Uri
|
||||
import android.os.Build
|
||||
import android.os.Bundle
|
||||
import android.provider.MediaStore
|
||||
import android.util.Log
|
||||
@ -159,17 +161,14 @@ class EditProfileCustActivity : AppCompatActivity() {
|
||||
}
|
||||
|
||||
private fun openImagePicker() {
|
||||
// Check for permission first
|
||||
if (ContextCompat.checkSelfPermission(
|
||||
this,
|
||||
android.Manifest.permission.READ_EXTERNAL_STORAGE
|
||||
) != PackageManager.PERMISSION_GRANTED
|
||||
) {
|
||||
ActivityCompat.requestPermissions(
|
||||
this,
|
||||
arrayOf(android.Manifest.permission.READ_EXTERNAL_STORAGE),
|
||||
REQUEST_STORAGE_PERMISSION
|
||||
)
|
||||
val permission = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
||||
Manifest.permission.READ_MEDIA_IMAGES
|
||||
} else {
|
||||
Manifest.permission.READ_EXTERNAL_STORAGE
|
||||
}
|
||||
|
||||
if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
|
||||
ActivityCompat.requestPermissions(this, arrayOf(permission), REQUEST_STORAGE_PERMISSION)
|
||||
} else {
|
||||
launchImagePicker()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user