Skip to content

Commit bbdea1f

Browse files
Pass the total amount calculated in the ViewModel to the API
The `taxes` array returned by the API includes raw taxes, not rounded, while the `total_tax` property is using rounded values, and this leads to some inconsistencies for calculating the amount when using them, so for now, we calculate the total amount using `total_tax` in the ViewModel.
1 parent 2ce1e75 commit bbdea1f

File tree

4 files changed

+27
-18
lines changed

4 files changed

+27
-18
lines changed

WooCommerce/src/main/kotlin/com/woocommerce/android/ui/payments/refunds/IssueRefundViewModel.kt

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,8 @@ class IssueRefundViewModel @Inject constructor(
303303
refundSummaryState = refundSummaryState.copy(
304304
isFormEnabled = true,
305305
previouslyRefunded = formatCurrency(order.refundTotal),
306-
refundAmount = formatCurrency(commonState.refundTotal)
306+
refundAmount = commonState.refundTotal,
307+
refundAmountFormatted = formatCurrency(commonState.refundTotal)
307308
)
308309

309310
triggerEvent(ShowRefundSummary)
@@ -399,11 +400,12 @@ class IssueRefundViewModel @Inject constructor(
399400
selectedFees?.forEach { allItems.add(it.toDataModel()) }
400401

401402
return refundStore.createItemsRefund(
402-
selectedSite.get(),
403-
order.id,
404-
refundSummaryState.refundReason ?: "",
405-
true,
406-
gateway.supportsRefunds,
403+
site = selectedSite.get(),
404+
orderId = order.id,
405+
amount = refundSummaryState.refundAmount,
406+
reason = refundSummaryState.refundReason ?: "",
407+
restockItems = true,
408+
autoRefund = gateway.supportsRefunds,
407409
items = allItems
408410
)
409411
}
@@ -808,7 +810,8 @@ class IssueRefundViewModel @Inject constructor(
808810
data class RefundSummaryViewState(
809811
val isFormEnabled: Boolean? = null,
810812
val previouslyRefunded: String? = null,
811-
val refundAmount: String? = null,
813+
val refundAmount: BigDecimal? = null,
814+
val refundAmountFormatted: String? = null,
812815
val refundMethod: String? = null,
813816
val refundReason: String? = null,
814817
val isMethodDescriptionVisible: Boolean? = null,

WooCommerce/src/main/kotlin/com/woocommerce/android/ui/payments/refunds/RefundSummaryFragment.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ class RefundSummaryFragment : BaseFragment(R.layout.fragment_refund_summary), Ba
100100
new.isSubmitButtonEnabled.takeIfNotEqualTo(old?.isSubmitButtonEnabled) {
101101
binding.refundSummaryBtnRefund.isEnabled = new.isSubmitButtonEnabled
102102
}
103-
new.refundAmount?.takeIfNotEqualTo(old?.refundAmount) {
103+
new.refundAmountFormatted?.takeIfNotEqualTo(old?.refundAmountFormatted) {
104104
binding.refundSummaryRefundAmount.text = it
105105
binding.toolbar.title = resources.getString(
106106
R.string.order_refunds_title_with_amount, it

libs/fluxc-plugin/src/main/kotlin/org/wordpress/android/fluxc/network/rest/wpcom/wc/refunds/RefundRestClient.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ import org.wordpress.android.fluxc.model.refunds.WCRefundModel.WCRefundFeeLine
99
import org.wordpress.android.fluxc.model.refunds.WCRefundModel.WCRefundShippingLine
1010
import org.wordpress.android.fluxc.network.rest.wpcom.wc.WooNetwork
1111
import org.wordpress.android.fluxc.network.rest.wpcom.wc.WooPayload
12-
import org.wordpress.android.fluxc.utils.sumBy
12+
import org.wordpress.android.fluxc.utils.extensions.filterNotNull
1313
import org.wordpress.android.fluxc.utils.toWooPayload
14+
import java.math.BigDecimal
1415
import javax.inject.Inject
1516

1617
class RefundRestClient @Inject constructor(private val wooNetwork: WooNetwork) {
@@ -33,18 +34,20 @@ class RefundRestClient @Inject constructor(private val wooNetwork: WooNetwork) {
3334
suspend fun createRefundByItems(
3435
site: SiteModel,
3536
orderId: Long,
37+
amount: BigDecimal?,
3638
reason: String,
3739
automaticRefund: Boolean,
3840
items: List<RefundRequestItem>,
3941
restockItems: Boolean
4042
): WooPayload<RefundResponse> {
4143
val body = mapOf(
4244
"reason" to reason,
43-
"amount" to items.sumBy { it.total }.toString(),
45+
"amount" to amount?.toString(),
4446
"api_refund" to automaticRefund.toString(),
4547
"line_items" to items,
4648
"restock_items" to restockItems
47-
)
49+
).filterNotNull()
50+
4851
return createRefund(site, orderId, body)
4952
}
5053

libs/fluxc-plugin/src/main/kotlin/org/wordpress/android/fluxc/store/WCRefundStore.kt

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ import javax.inject.Singleton
2020
class WCRefundStore @Inject constructor(
2121
private val restClient: RefundRestClient,
2222
private val coroutineEngine: CoroutineEngine,
23-
private val refundsMapper: RefundMapper
23+
private val refundsMapper: RefundMapper,
24+
private val wooCommerceStore: WooCommerceStore
2425
) {
2526
companion object {
2627
// Just get everything
@@ -54,19 +55,21 @@ class WCRefundStore @Inject constructor(
5455
suspend fun createItemsRefund(
5556
site: SiteModel,
5657
orderId: Long,
58+
amount: BigDecimal?,
5759
reason: String = "",
5860
restockItems: Boolean = true,
5961
autoRefund: Boolean = false,
6062
items: List<RefundRequestItem>
6163
): WooResult<WCRefundModel> {
6264
return coroutineEngine.withDefaultContext(AppLog.T.API, this, "createItemsRefund") {
6365
val response = restClient.createRefundByItems(
64-
site,
65-
orderId,
66-
reason,
67-
autoRefund,
68-
items,
69-
restockItems
66+
site = site,
67+
orderId = orderId,
68+
amount = amount,
69+
reason = reason,
70+
automaticRefund = autoRefund,
71+
items = items,
72+
restockItems = restockItems
7073
)
7174
return@withDefaultContext when {
7275
response.isError -> WooResult(response.error)

0 commit comments

Comments
 (0)