Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ All notable changes to this project will be documented in this file.
### Removed
### Fixed

## [0.80.14]
### Fixed
* ui: fix jumping of the shopping cart it an item is used with a deposit (APPS-2177)

## [0.80.13]
### Added
* ui: Add new headline option for the payment options (APPS-1915)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import io.snabble.sdk.shoppingcart.data.cart.BackendCartCustomer
import io.snabble.sdk.shoppingcart.data.cart.BackendCartRequiredInformation
import io.snabble.sdk.shoppingcart.data.item.BackendCartItem
import io.snabble.sdk.shoppingcart.data.item.BackendCartItemType
import io.snabble.sdk.shoppingcart.data.item.Deposit
import io.snabble.sdk.shoppingcart.data.item.DepositReturnVoucher
import io.snabble.sdk.shoppingcart.data.item.ItemType
import io.snabble.sdk.shoppingcart.data.listener.ShoppingCartListener
Expand Down Expand Up @@ -996,6 +997,11 @@ class ShoppingCart(
*/
var depositReturnVoucher: DepositReturnVoucher? = null

/**
* Returns the deposit associated with the shopping cart item.
*/
var deposit : Deposit? = null

// The local generated UUID of a coupon which which will be used by the backend
var backendCouponId: String? = null

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import io.snabble.sdk.checkout.Price
import io.snabble.sdk.checkout.SignedCheckoutInfo
import io.snabble.sdk.checkout.Violation
import io.snabble.sdk.codes.ScannedCode.Companion.parseDefault
import io.snabble.sdk.shoppingcart.data.item.Deposit
import io.snabble.sdk.shoppingcart.data.item.ItemType
import io.snabble.sdk.utils.GsonHolder
import io.snabble.sdk.utils.Logger
Expand Down Expand Up @@ -160,7 +161,7 @@ internal class ShoppingCartUpdater(
addLineItemsAsCartItems(filter { it.type == LineItemType.DISCOUNT && it.discountType != "cart" })

addLineItemsAsCartItems(filter { it.type == LineItemType.COUPON })
addLineItemsAsCartItems(filter { it.type == LineItemType.DEPOSIT })
addDepositToItem(filter { it.type == LineItemType.DEPOSIT })
addDepositReturnsToVoucher(filter { it.type == LineItemType.DEPOSIT_RETURN })
}

Expand All @@ -179,12 +180,20 @@ internal class ShoppingCartUpdater(
}
}

private fun addDepositToItem(deposits: List<LineItem>) {
deposits
.forEach { deposit ->
val item = cart.getByItemId(deposit.refersTo)
item?.deposit = Deposit(deposit)
}
}

private fun addDepositReturnsToVoucher(depositReturnItems: List<LineItem>) {
depositReturnItems
.groupBy { it.refersTo }
.forEach { (refersTo, items) ->
val drv = cart.getByItemId(refersTo)
drv?.depositReturnVoucher = drv?.depositReturnVoucher?.copy(lineItems = items)
drv?.depositReturnVoucher = drv.depositReturnVoucher?.copy(lineItems = items)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.snabble.sdk.shoppingcart.data.item

import io.snabble.sdk.checkout.LineItem

data class Deposit(
val lineItem: LineItem
)
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import androidx.lifecycle.viewModelScope
import io.snabble.sdk.PriceFormatter
import io.snabble.sdk.Project
import io.snabble.sdk.Snabble
import io.snabble.sdk.checkout.LineItemType
import io.snabble.sdk.shoppingcart.ShoppingCart
import io.snabble.sdk.shoppingcart.data.item.ItemType
import io.snabble.sdk.shoppingcart.data.listener.SimpleShoppingCartListener
Expand Down Expand Up @@ -106,8 +105,6 @@ class ShoppingCartViewModel : ViewModel() {
filter { it.type == ItemType.PRODUCT }.let { cartItems.addProducts(it) }
filter { it.type == ItemType.DEPOSIT_RETURN_VOUCHER }.let { cartItems.addDepositReturnItems(it) }

filter { it.lineItem?.type == LineItemType.DEPOSIT }.let { cartItems.addDepositsToProducts(it) }

filter { it.isDiscount && it.lineItem?.discountType != "cart" }.let { cartItems.addDiscountsToProducts(it) }
filter { it.isDiscount && it.lineItem?.discountType == "cart" }.let { cartItems.addCartDiscount(it) }
}
Expand Down Expand Up @@ -182,10 +179,16 @@ class ShoppingCartViewModel : ViewModel() {
isAgeRestricted = item.isAgeRestricted,
minimumAge = item.minimumAge,
item = item,
totalPrice = item.lineItem?.totalPrice ?: 0
totalPrice = item.totalPrice,
deposit = item.deposit?.let {
DepositItem(
depositPrice = item.quantity * it.lineItem.price,
depositText = it.lineItem.name,
depositPriceText = priceFormatter?.format(item.quantity * it.lineItem.price)
)
}
)
}
)
})
}

private fun MutableList<CartItem>.addPriceModifiersAsDiscountsProducts() = replaceAll { item ->
Expand Down Expand Up @@ -243,23 +246,6 @@ class ShoppingCartViewModel : ViewModel() {
}
}

private fun MutableList<CartItem>.addDepositsToProducts(deposit: List<ShoppingCart.Item>) =
deposit.forEach { item ->
firstOrNull { it.item.id == item.lineItem?.refersTo }?.let {
remove(it)
val product = it as? ProductItem ?: return@forEach
add(
product.copy(
deposit = DepositItem(
depositPriceText = item.totalPriceText,
depositText = item.displayName,
depositPrice = item.totalPrice
),
)
)
}
}

private fun MutableList<CartItem>.addCartDiscount(cartDiscount: List<ShoppingCart.Item>) =
cartDiscount.forEach { item ->
val discount = item.priceText ?: return@forEach
Expand Down