Skip to content

Commit

Permalink
Merge pull request #13261 from woocommerce/issue/13258-fix-refunded-i…
Browse files Browse the repository at this point in the history
…tems

Fix issue with displaying refunded items
  • Loading branch information
hafizrahman authored Jan 9, 2025
2 parents 9949e8f + 0fed58c commit b834846
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 5 deletions.
1 change: 1 addition & 0 deletions RELEASE-NOTES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
- [**] Improved UX of card reader payment flow in Point of Sale, allowing faster payment collection [https://github.com/woocommerce/woocommerce-android/pull/12977]
- [*] Fixed crash when trying to open barcode scanner on device without camera [https://github.com/woocommerce/woocommerce-android/pull/13172]
- [Internal] Woo POS now accepts cash payments and there is a functionality of sending receipts [https://github.com/woocommerce/woocommerce-android/pull/13157]
- [**] Fixed a bug that caused the app to not show the list of refunded products in the order details screen [https://github.com/woocommerce/woocommerce-android/pull/13261]

-----
21.2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ fun WCRefundModel.WCRefundItem.toAppModel(): Refund.Item {
-totalTax, // WCRefundItem.totalTax is NEGATIVE
sku ?: "",
price ?: BigDecimal.ZERO,
metaData?.get(0)?.value?.toString()?.toLongOrNull() ?: -1
metaData?.get(0)?.value?.stringValue?.toLongOrNull() ?: -1
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import com.woocommerce.android.ui.products.ProductHelper
import kotlinx.parcelize.IgnoredOnParcel
import kotlinx.parcelize.Parcelize
import org.wordpress.android.fluxc.model.refunds.WCRefundModel
import org.wordpress.android.fluxc.model.refunds.WCRefundModel.*
import org.wordpress.android.fluxc.model.refunds.WCRefundModel.WCRefundFeeLine
import org.wordpress.android.fluxc.model.refunds.WCRefundModel.WCRefundItem
import org.wordpress.android.fluxc.model.refunds.WCRefundModel.WCRefundShippingLine
import java.math.BigDecimal
import java.math.RoundingMode.HALF_UP
import java.util.Date
Expand Down Expand Up @@ -94,13 +96,13 @@ fun WCRefundItem.toAppModel(): Refund.Item {
-totalTax, // WCRefundItem.totalTax is NEGATIVE
sku ?: "",
price ?: BigDecimal.ZERO,
metaData?.get(0)?.value?.toString()?.toLongOrNull() ?: -1
metaData?.get(0)?.value?.stringValue?.toLongOrNull() ?: -1
)
}

fun WCRefundShippingLine.toAppModel(): Refund.ShippingLine {
return Refund.ShippingLine(
itemId = metaData?.get(0)?.value?.toString()?.toLongOrNull() ?: -1,
itemId = metaData?.get(0)?.value?.stringValue?.toLongOrNull() ?: -1,
methodId = methodId ?: "",
methodTitle = methodTitle ?: "",
totalTax = -totalTax, // WCRefundShippineLine.totalTax is NEGATIVE
Expand All @@ -110,7 +112,7 @@ fun WCRefundShippingLine.toAppModel(): Refund.ShippingLine {

fun WCRefundFeeLine.toAppModel(): Refund.FeeLine {
return Refund.FeeLine(
id = metaData?.get(0)?.value?.toString()?.toLongOrNull() ?: -1,
id = metaData?.get(0)?.value?.stringValue?.toLongOrNull() ?: -1,
name = name,
totalTax = -totalTax, // WCRefundFeeLine.totalTax is NEGATIVE
total = (total), // WCRefundFeeLine.total is NEGATIVE
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package com.woocommerce.android.model

import org.junit.Assert.assertEquals
import org.junit.Test
import org.wordpress.android.fluxc.model.metadata.WCMetaData
import org.wordpress.android.fluxc.model.metadata.WCMetaDataValue
import org.wordpress.android.fluxc.model.refunds.WCRefundModel
import java.math.BigDecimal

class RefundMapperTest {
@Test
fun `when mapping refund item from domain model, then extract its properties correctly`() {
val domainModel = WCRefundModel.WCRefundItem(
productId = 1,
quantity = -1,
itemId = 2,
name = "name",
variationId = null,
subtotal = BigDecimal.TEN,
total = BigDecimal.TEN,
totalTax = BigDecimal.TEN,
sku = "sku",
price = BigDecimal.TEN,
metaData = listOf(WCMetaData(id = 0, key = "_refunded_item_id", value = WCMetaDataValue(10)))
)

val appModel = domainModel.toAppModel()

assertEquals(1, appModel.productId)
assertEquals(1, appModel.quantity)
assertEquals(2, appModel.id)
assertEquals("name", appModel.name)
assertEquals(-1, appModel.variationId)
assertEquals(BigDecimal.TEN.negate(), appModel.subtotal)
assertEquals(BigDecimal.TEN.negate(), appModel.total)
assertEquals(BigDecimal.TEN.negate(), appModel.totalTax)
assertEquals("sku", appModel.sku)
assertEquals(BigDecimal.TEN, appModel.price)
assertEquals(10, appModel.orderItemId)
}

@Test
fun `given item ID is wrapped as String, when mapping refund item from domain model, then extract it correctly`() {
val domainModel = WCRefundModel.WCRefundItem(
productId = 1,
quantity = -1,
itemId = 2,
name = "name",
variationId = null,
subtotal = BigDecimal.TEN,
total = BigDecimal.TEN,
totalTax = BigDecimal.TEN,
sku = "sku",
price = BigDecimal.TEN,
metaData = listOf(WCMetaData(id = 0, key = "_refunded_item_id", value = WCMetaDataValue("10")))
)

val appModel = domainModel.toAppModel()

assertEquals(10, appModel.orderItemId)
}

@Test
fun `when mapping shipping lines from domain model, then extract its properties correctly`() {
val domainModel = WCRefundModel.WCRefundShippingLine(
id = 1,
methodId = "methodId",
methodTitle = "methodTitle",
totalTax = BigDecimal.TEN,
total = BigDecimal.TEN,
metaData = listOf(WCMetaData(id = 0, key = "_refunded_item_id", value = WCMetaDataValue(10)))
)

val appModel = domainModel.toAppModel()

// Intentionally not checking all properties, we have an inconsistency in the function where
// we negate the totalTax but not the total, and there is no explanation for this.
// See https://github.com/woocommerce/woocommerce-android/pull/5517#discussion_r774573314
// We should update the test when we have a clear understanding of the expected behavior.
// Note: these properties are not used in the app currently.
assertEquals(10, appModel.itemId)
}

@Test
fun `when mapping fee lines from domain model, then extract its properties correctly`() {
val domainModel = WCRefundModel.WCRefundFeeLine(
id = 1,
name = "name",
totalTax = BigDecimal.TEN,
total = BigDecimal.TEN,
metaData = listOf(WCMetaData(id = 0, key = "_refunded_item_id", value = WCMetaDataValue(10)))
)

val appModel = domainModel.toAppModel()

// See comment in the previous test.
assertEquals(10, appModel.id)
}
}

0 comments on commit b834846

Please sign in to comment.