diff --git a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/payments/refunds/IssueRefundViewModel.kt b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/payments/refunds/IssueRefundViewModel.kt index 0b3bb781f35..c8cc6dc68fb 100644 --- a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/payments/refunds/IssueRefundViewModel.kt +++ b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/payments/refunds/IssueRefundViewModel.kt @@ -18,9 +18,6 @@ import com.woocommerce.android.analytics.AnalyticsEvent.REFUND_CREATE_SUCCESS import com.woocommerce.android.analytics.AnalyticsTracker import com.woocommerce.android.analytics.AnalyticsTrackerWrapper import com.woocommerce.android.extensions.adminUrlOrDefault -import com.woocommerce.android.extensions.calculateTotalSubtotal -import com.woocommerce.android.extensions.calculateTotalTaxes -import com.woocommerce.android.extensions.calculateTotals import com.woocommerce.android.extensions.isCashPayment import com.woocommerce.android.extensions.isEqualTo import com.woocommerce.android.extensions.joinToString @@ -38,9 +35,6 @@ import com.woocommerce.android.ui.payments.refunds.IssueRefundViewModel.IssueRef import com.woocommerce.android.ui.payments.refunds.IssueRefundViewModel.IssueRefundEvent.ShowNumberPicker import com.woocommerce.android.ui.payments.refunds.IssueRefundViewModel.IssueRefundEvent.ShowRefundConfirmation import com.woocommerce.android.ui.payments.refunds.IssueRefundViewModel.IssueRefundEvent.ShowRefundSummary -import com.woocommerce.android.ui.payments.refunds.RefundFeeListAdapter.FeeRefundListItem -import com.woocommerce.android.ui.payments.refunds.RefundProductListAdapter.ProductRefundListItem -import com.woocommerce.android.ui.payments.refunds.RefundShippingListAdapter.ShippingRefundListItem import com.woocommerce.android.util.CurrencyFormatter import com.woocommerce.android.util.max import com.woocommerce.android.util.min @@ -56,8 +50,8 @@ import kotlinx.coroutines.Job import kotlinx.coroutines.launch import kotlinx.coroutines.runBlocking import kotlinx.parcelize.Parcelize +import org.wordpress.android.fluxc.model.refunds.RefundRequestItem import org.wordpress.android.fluxc.model.refunds.WCRefundModel -import org.wordpress.android.fluxc.model.refunds.WCRefundModel.WCRefundItem import org.wordpress.android.fluxc.network.rest.wpcom.wc.WooResult import org.wordpress.android.fluxc.store.WCGatewayStore import org.wordpress.android.fluxc.store.WCOrderStore @@ -387,7 +381,7 @@ class IssueRefundViewModel @Inject constructor( } private suspend fun initiateRefund(): WooResult { - val allItems = mutableListOf() + val allItems = mutableListOf() refundItems.value?.let { it.forEach { item -> allItems.add(item.toDataModel()) } } diff --git a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/payments/refunds/RefundDetailViewModel.kt b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/payments/refunds/RefundDetailViewModel.kt index 6957fa8ef12..886ceb4f541 100644 --- a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/payments/refunds/RefundDetailViewModel.kt +++ b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/payments/refunds/RefundDetailViewModel.kt @@ -7,14 +7,12 @@ import androidx.lifecycle.SavedStateHandle import com.woocommerce.android.R import com.woocommerce.android.analytics.AnalyticsEvent import com.woocommerce.android.analytics.AnalyticsTracker -import com.woocommerce.android.extensions.calculateTotals import com.woocommerce.android.extensions.isCashPayment import com.woocommerce.android.model.Order import com.woocommerce.android.model.OrderMapper import com.woocommerce.android.model.Refund import com.woocommerce.android.model.toAppModel import com.woocommerce.android.tools.SelectedSite -import com.woocommerce.android.ui.payments.refunds.RefundProductListAdapter.ProductRefundListItem import com.woocommerce.android.ui.products.addons.AddonRepository import com.woocommerce.android.util.CoroutineDispatchers import com.woocommerce.android.util.CurrencyFormatter diff --git a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/payments/refunds/RefundFeeListAdapter.kt b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/payments/refunds/RefundFeeListAdapter.kt index 6acb5aeadd2..cf4f23b5c2a 100644 --- a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/payments/refunds/RefundFeeListAdapter.kt +++ b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/payments/refunds/RefundFeeListAdapter.kt @@ -1,6 +1,5 @@ package com.woocommerce.android.ui.payments.refunds -import android.os.Parcelable import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -9,9 +8,6 @@ import androidx.recyclerview.widget.RecyclerView import com.google.android.material.switchmaterial.SwitchMaterial import com.woocommerce.android.R import com.woocommerce.android.extensions.hide -import com.woocommerce.android.model.Order -import kotlinx.parcelize.Parcelize -import org.wordpress.android.fluxc.model.refunds.WCRefundModel.WCRefundItem import java.math.BigDecimal class RefundFeeListAdapter( @@ -71,18 +67,4 @@ class RefundFeeListAdapter( val switch: SwitchMaterial = view.findViewById(R.id.issueRefund_feeLineSwitch) val divider: View = view.findViewById(R.id.issueRefund_feesDivider) } - - @Parcelize - data class FeeRefundListItem( - val feeLine: Order.FeeLine - ) : Parcelable { - fun toDataModel(): WCRefundItem { - return WCRefundItem( - feeLine.id, - quantity = 1, /* Hardcoded because a fee line always has a quantity of 1 */ - subtotal = feeLine.total, - totalTax = feeLine.totalTax - ) - } - } } diff --git a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/payments/refunds/RefundProductListAdapter.kt b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/payments/refunds/RefundProductListAdapter.kt index 0ce0e28f1fb..8ce75acb0c6 100644 --- a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/payments/refunds/RefundProductListAdapter.kt +++ b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/payments/refunds/RefundProductListAdapter.kt @@ -1,7 +1,6 @@ package com.woocommerce.android.ui.payments.refunds import android.annotation.SuppressLint -import android.os.Parcelable import android.view.LayoutInflater import android.view.ViewGroup import android.widget.ImageView @@ -24,12 +23,8 @@ import com.woocommerce.android.extensions.show import com.woocommerce.android.model.Order import com.woocommerce.android.tools.ProductImageMap import com.woocommerce.android.ui.payments.refunds.RefundProductListAdapter.RefundViewHolder -import kotlinx.parcelize.IgnoredOnParcel -import kotlinx.parcelize.Parcelize -import org.wordpress.android.fluxc.model.refunds.WCRefundModel.WCRefundItem import org.wordpress.android.util.PhotonUtils import java.math.BigDecimal -import java.math.RoundingMode.HALF_UP typealias ViewAddonClickListener = (Order.Item) -> Unit @@ -175,28 +170,6 @@ class RefundProductListAdapter( } } - @Parcelize - data class ProductRefundListItem( - val orderItem: Order.Item, - val maxQuantity: Float = 0f, - val quantity: Int = 0, - val subtotal: String? = null, - val taxes: String? = null - ) : Parcelable { - @IgnoredOnParcel - val availableRefundQuantity - get() = maxQuantity.toInt() - fun toDataModel(): WCRefundItem { - return WCRefundItem( - orderItem.itemId, - quantity, - quantity.toBigDecimal().times(orderItem.price), - orderItem.totalTax.divide(orderItem.quantity.toBigDecimal(), 2, HALF_UP) - .times(quantity.toBigDecimal()) - ) - } - } - class OrderItemDiffCallback( private val oldList: List, private val newList: List diff --git a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/payments/refunds/RefundShippingListAdapter.kt b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/payments/refunds/RefundShippingListAdapter.kt index 44395c0babf..fca3769f7c7 100644 --- a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/payments/refunds/RefundShippingListAdapter.kt +++ b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/payments/refunds/RefundShippingListAdapter.kt @@ -1,6 +1,5 @@ package com.woocommerce.android.ui.payments.refunds -import android.os.Parcelable import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -9,9 +8,6 @@ import androidx.recyclerview.widget.RecyclerView import com.google.android.material.switchmaterial.SwitchMaterial import com.woocommerce.android.R import com.woocommerce.android.extensions.hide -import com.woocommerce.android.model.Order -import kotlinx.parcelize.Parcelize -import org.wordpress.android.fluxc.model.refunds.WCRefundModel.WCRefundItem import java.math.BigDecimal class RefundShippingListAdapter( @@ -71,18 +67,4 @@ class RefundShippingListAdapter( val switch: SwitchMaterial = view.findViewById(R.id.issueRefund_shippingLineSwitch) val divider: View = view.findViewById(R.id.issueRefund_shippingDivider) } - - @Parcelize - data class ShippingRefundListItem( - val shippingLine: Order.ShippingLine - ) : Parcelable { - fun toDataModel(): WCRefundItem { - return WCRefundItem( - shippingLine.itemId, - quantity = 1, /* Hardcoded because a shipping line always has a quantity of 1 */ - subtotal = shippingLine.total, - totalTax = shippingLine.totalTax - ) - } - } } diff --git a/WooCommerce/src/main/kotlin/com/woocommerce/android/extensions/RefundsExt.kt b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/payments/refunds/RefundsExt.kt similarity index 84% rename from WooCommerce/src/main/kotlin/com/woocommerce/android/extensions/RefundsExt.kt rename to WooCommerce/src/main/kotlin/com/woocommerce/android/ui/payments/refunds/RefundsExt.kt index f7a32524773..81197dce261 100644 --- a/WooCommerce/src/main/kotlin/com/woocommerce/android/extensions/RefundsExt.kt +++ b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/payments/refunds/RefundsExt.kt @@ -1,6 +1,5 @@ -package com.woocommerce.android.extensions +package com.woocommerce.android.ui.payments.refunds -import com.woocommerce.android.ui.payments.refunds.RefundProductListAdapter.ProductRefundListItem import java.math.BigDecimal import java.math.RoundingMode.HALF_UP diff --git a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/payments/refunds/RefundsUiModels.kt b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/payments/refunds/RefundsUiModels.kt new file mode 100644 index 00000000000..433ec6c9825 --- /dev/null +++ b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/payments/refunds/RefundsUiModels.kt @@ -0,0 +1,73 @@ +package com.woocommerce.android.ui.payments.refunds + +import android.os.Parcelable +import com.woocommerce.android.model.Order +import kotlinx.parcelize.Parcelize +import org.wordpress.android.fluxc.model.refunds.RefundRequestItem +import org.wordpress.android.fluxc.model.refunds.RefundRequestTax +import java.math.RoundingMode.HALF_UP + +@Parcelize +data class ProductRefundListItem( + val orderItem: Order.Item, + val maxQuantity: Float = 0f, + val quantity: Int = 0, + val subtotal: String? = null, + val taxes: String? = null +) : Parcelable { + val availableRefundQuantity + get() = maxQuantity.toInt() + + fun toDataModel(): RefundRequestItem { + return RefundRequestItem( + itemId = orderItem.itemId, + quantity = quantity, + refundTotal = quantity.toBigDecimal().times(orderItem.price), + refundTax = listOf( + RefundRequestTax( + taxRateId = 0L, + refundTotal = orderItem.totalTax.divide(orderItem.quantity.toBigDecimal(), 2, HALF_UP) + .times(quantity.toBigDecimal()) + ) + ) + ) + } +} + +@Parcelize +data class ShippingRefundListItem( + val shippingLine: Order.ShippingLine +) : Parcelable { + fun toDataModel(): RefundRequestItem { + return RefundRequestItem( + shippingLine.itemId, + quantity = 1, // Hardcoded because a shipping line always has a quantity of 1 + refundTotal = shippingLine.total, + refundTax = listOf( + RefundRequestTax( + taxRateId = 0L, + refundTotal = shippingLine.totalTax + ) + ) + ) + } +} + +@Parcelize +data class FeeRefundListItem( + val feeLine: Order.FeeLine +) : Parcelable { + fun toDataModel(): RefundRequestItem { + return RefundRequestItem( + feeLine.id, + quantity = 1, // Hardcoded because a fee line always has a quantity of 1 + refundTotal = feeLine.total, + refundTax = listOf( + RefundRequestTax( + taxRateId = 0L, + refundTotal = feeLine.totalTax, + ) + ) + ) + } +} diff --git a/config/detekt/baseline.xml b/config/detekt/baseline.xml index 19aaa240577..0aeada4c994 100644 --- a/config/detekt/baseline.xml +++ b/config/detekt/baseline.xml @@ -7,8 +7,6 @@ ClassNaming:ProductDetailViewModel_AddFlowTest.kt$ProductDetailViewModel_AddFlowTest : BaseUnitTest CommentWrapping:IssueRefundViewModel.kt$IssueRefundViewModel$/* Fees lines that haven't been refunded */ CommentWrapping:IssueRefundViewModel.kt$IssueRefundViewModel$/* Shipping lines that haven't been refunded */ - CommentWrapping:RefundFeeListAdapter.kt$RefundFeeListAdapter.FeeRefundListItem$/* Hardcoded because a fee line always has a quantity of 1 */ - CommentWrapping:RefundShippingListAdapter.kt$RefundShippingListAdapter.ShippingRefundListItem$/* Hardcoded because a shipping line always has a quantity of 1 */ CommentWrapping:ShippingLabelCreateCustomPackageViewModel.kt$ShippingLabelCreateCustomPackageViewModel$/* Safe to set as empty, as it's not used for package creation */ CommentWrapping:ShippingPackage.kt$ShippingPackage$/* Can be empty, only needed by predefined packages */ ComplexCondition:ProductImageViewerFragment.kt$ProductImageViewerFragment$(show && binding.fakeToolbar.visibility == View.VISIBLE) || (!show && binding.fakeToolbar.visibility != View.VISIBLE) diff --git a/libs/fluxc-plugin/src/main/kotlin/org/wordpress/android/fluxc/model/order/FeeLine.kt b/libs/fluxc-plugin/src/main/kotlin/org/wordpress/android/fluxc/model/order/FeeLine.kt index 4806ee32325..2e699f6fab7 100644 --- a/libs/fluxc-plugin/src/main/kotlin/org/wordpress/android/fluxc/model/order/FeeLine.kt +++ b/libs/fluxc-plugin/src/main/kotlin/org/wordpress/android/fluxc/model/order/FeeLine.kt @@ -23,6 +23,9 @@ class FeeLine { @SerializedName("tax_status") var taxStatus: FeeLineTaxStatus? = null + + @SerializedName("taxes") + var taxes: List? = null } enum class FeeLineTaxStatus(val value: String) { diff --git a/libs/fluxc-plugin/src/main/kotlin/org/wordpress/android/fluxc/model/order/LineItem.kt b/libs/fluxc-plugin/src/main/kotlin/org/wordpress/android/fluxc/model/order/LineItem.kt index 4adbc5ffd88..3185cd2ee9c 100644 --- a/libs/fluxc-plugin/src/main/kotlin/org/wordpress/android/fluxc/model/order/LineItem.kt +++ b/libs/fluxc-plugin/src/main/kotlin/org/wordpress/android/fluxc/model/order/LineItem.kt @@ -30,7 +30,9 @@ data class LineItem( @SerializedName("bundled_by") val bundledBy: String? = null, @SerializedName("composite_parent") - val compositeParent: String? = null + val compositeParent: String? = null, + @SerializedName("taxes") + val taxes: List? = null, ) { class Attribute(val key: String?, val value: String?) diff --git a/libs/fluxc-plugin/src/main/kotlin/org/wordpress/android/fluxc/model/order/ShippingLine.kt b/libs/fluxc-plugin/src/main/kotlin/org/wordpress/android/fluxc/model/order/ShippingLine.kt index 260b79b36eb..7b9e934114a 100644 --- a/libs/fluxc-plugin/src/main/kotlin/org/wordpress/android/fluxc/model/order/ShippingLine.kt +++ b/libs/fluxc-plugin/src/main/kotlin/org/wordpress/android/fluxc/model/order/ShippingLine.kt @@ -13,5 +13,7 @@ data class ShippingLine( @JsonAdapter(NullStringJsonAdapter::class, nullSafe = false) val methodId: String? = null, @SerializedName("method_title") - val methodTitle: String? = null + val methodTitle: String? = null, + @SerializedName("taxes") + val taxes: List? = null, ) diff --git a/libs/fluxc-plugin/src/main/kotlin/org/wordpress/android/fluxc/model/order/WCLineTaxEntry.kt b/libs/fluxc-plugin/src/main/kotlin/org/wordpress/android/fluxc/model/order/WCLineTaxEntry.kt new file mode 100644 index 00000000000..3265bdc1b86 --- /dev/null +++ b/libs/fluxc-plugin/src/main/kotlin/org/wordpress/android/fluxc/model/order/WCLineTaxEntry.kt @@ -0,0 +1,17 @@ +package org.wordpress.android.fluxc.model.order + +import com.google.gson.annotations.SerializedName +import java.math.BigDecimal + +/** + * Tax information for a line item, it can be part of a line item or a shipping line or a fee line. + */ +data class WCLineTaxEntry( + @SerializedName("id") + val rateId: Long? = null, + @SerializedName("total") + private val totalString: String? = null, +) { + val total: BigDecimal? + get() = totalString?.toBigDecimalOrNull() +} diff --git a/libs/fluxc-plugin/src/main/kotlin/org/wordpress/android/fluxc/model/refunds/RefundMapper.kt b/libs/fluxc-plugin/src/main/kotlin/org/wordpress/android/fluxc/model/refunds/RefundMapper.kt index cdde5ce027f..e9e3491e78f 100644 --- a/libs/fluxc-plugin/src/main/kotlin/org/wordpress/android/fluxc/model/refunds/RefundMapper.kt +++ b/libs/fluxc-plugin/src/main/kotlin/org/wordpress/android/fluxc/model/refunds/RefundMapper.kt @@ -30,7 +30,8 @@ class RefundMapper @Inject constructor() { total = it.total?.toBigDecimalOrNull(), sku = it.sku, price = it.price?.toBigDecimalOrNull(), - metaData = it.metaData + metaData = it.metaData, + taxes = it.taxes ) } ?: emptyList(), shippingLineItems = response.shippingLineItems ?: emptyList(), diff --git a/libs/fluxc-plugin/src/main/kotlin/org/wordpress/android/fluxc/model/refunds/RefundRequestItem.kt b/libs/fluxc-plugin/src/main/kotlin/org/wordpress/android/fluxc/model/refunds/RefundRequestItem.kt new file mode 100644 index 00000000000..9ce0816b1e4 --- /dev/null +++ b/libs/fluxc-plugin/src/main/kotlin/org/wordpress/android/fluxc/model/refunds/RefundRequestItem.kt @@ -0,0 +1,25 @@ +package org.wordpress.android.fluxc.model.refunds + +import com.google.gson.annotations.SerializedName +import java.math.BigDecimal + +data class RefundRequestItem( + @SerializedName("id") + val itemId: Long, + @SerializedName("quantity") + val quantity: Int?, + @SerializedName("refund_total") + val refundTotal: BigDecimal, + @SerializedName("refund_tax") + val refundTax: List +) { + val total: BigDecimal + get() = refundTotal + refundTax.sumOf { it.refundTotal } +} + +data class RefundRequestTax( + @SerializedName("id") + val taxRateId: Long, + @SerializedName("refund_total") + val refundTotal: BigDecimal +) diff --git a/libs/fluxc-plugin/src/main/kotlin/org/wordpress/android/fluxc/model/refunds/WCRefundModel.kt b/libs/fluxc-plugin/src/main/kotlin/org/wordpress/android/fluxc/model/refunds/WCRefundModel.kt index 45a3475ebcf..36d4f73c329 100644 --- a/libs/fluxc-plugin/src/main/kotlin/org/wordpress/android/fluxc/model/refunds/WCRefundModel.kt +++ b/libs/fluxc-plugin/src/main/kotlin/org/wordpress/android/fluxc/model/refunds/WCRefundModel.kt @@ -2,6 +2,7 @@ package org.wordpress.android.fluxc.model.refunds import com.google.gson.annotations.SerializedName import org.wordpress.android.fluxc.model.metadata.WCMetaData +import org.wordpress.android.fluxc.model.order.WCLineTaxEntry import java.math.BigDecimal import java.util.Date @@ -26,11 +27,8 @@ data class WCRefundModel( ) { data class WCRefundItem( val itemId: Long, - @SerializedName("qty") val quantity: Int, - @SerializedName("refund_total") val subtotal: BigDecimal, - @SerializedName("refund_tax") val totalTax: BigDecimal, val name: String? = null, val productId: Long? = null, @@ -38,8 +36,8 @@ data class WCRefundModel( val total: BigDecimal? = null, val sku: String? = null, val price: BigDecimal? = null, - @SerializedName("meta_data") - val metaData: List? = null + val metaData: List? = null, + val taxes: List? = null ) data class WCRefundShippingLine( diff --git a/libs/fluxc-plugin/src/main/kotlin/org/wordpress/android/fluxc/network/rest/wpcom/wc/refunds/RefundRestClient.kt b/libs/fluxc-plugin/src/main/kotlin/org/wordpress/android/fluxc/network/rest/wpcom/wc/refunds/RefundRestClient.kt index 95ebec819f8..8ebf13a8377 100644 --- a/libs/fluxc-plugin/src/main/kotlin/org/wordpress/android/fluxc/network/rest/wpcom/wc/refunds/RefundRestClient.kt +++ b/libs/fluxc-plugin/src/main/kotlin/org/wordpress/android/fluxc/network/rest/wpcom/wc/refunds/RefundRestClient.kt @@ -4,7 +4,7 @@ import com.google.gson.annotations.SerializedName import org.wordpress.android.fluxc.generated.endpoint.WOOCOMMERCE import org.wordpress.android.fluxc.model.SiteModel import org.wordpress.android.fluxc.model.order.LineItem -import org.wordpress.android.fluxc.model.refunds.WCRefundModel +import org.wordpress.android.fluxc.model.refunds.RefundRequestItem import org.wordpress.android.fluxc.model.refunds.WCRefundModel.WCRefundFeeLine import org.wordpress.android.fluxc.model.refunds.WCRefundModel.WCRefundShippingLine import org.wordpress.android.fluxc.network.rest.wpcom.wc.WooNetwork @@ -35,14 +35,14 @@ class RefundRestClient @Inject constructor(private val wooNetwork: WooNetwork) { orderId: Long, reason: String, automaticRefund: Boolean, - items: List, + items: List, restockItems: Boolean ): WooPayload { val body = mapOf( "reason" to reason, - "amount" to items.sumBy { it.subtotal + it.totalTax }.toString(), + "amount" to items.sumBy { it.total }.toString(), "api_refund" to automaticRefund.toString(), - "line_items" to items.associateBy { it.itemId }, + "line_items" to items, "restock_items" to restockItems ) return createRefund(site, orderId, body) diff --git a/libs/fluxc-plugin/src/main/kotlin/org/wordpress/android/fluxc/persistence/WCRefundSqlUtils.kt b/libs/fluxc-plugin/src/main/kotlin/org/wordpress/android/fluxc/persistence/WCRefundSqlUtils.kt index bf3bacef134..f007d5e3685 100644 --- a/libs/fluxc-plugin/src/main/kotlin/org/wordpress/android/fluxc/persistence/WCRefundSqlUtils.kt +++ b/libs/fluxc-plugin/src/main/kotlin/org/wordpress/android/fluxc/persistence/WCRefundSqlUtils.kt @@ -20,6 +20,15 @@ object WCRefundSqlUtils { fun insertOrUpdate(site: SiteModel, orderId: Long, data: RefundResponse) = insertOrUpdate(site, orderId, listOf(data)) + fun deleteRefunds(site: SiteModel, orderId: Long) { + WellSql.delete(RefundBuilder::class.java) + .where() + .equals(WCRefundsTable.LOCAL_SITE_ID, site.id) + .equals(WCRefundsTable.ORDER_ID, orderId) + .endWhere() + .execute() + } + fun insertOrUpdate(site: SiteModel, orderId: Long, data: List) { data.forEach { item -> val json = gson.toJson(item) diff --git a/libs/fluxc-plugin/src/main/kotlin/org/wordpress/android/fluxc/store/WCRefundStore.kt b/libs/fluxc-plugin/src/main/kotlin/org/wordpress/android/fluxc/store/WCRefundStore.kt index 580cee47ea8..c89f86d3b82 100644 --- a/libs/fluxc-plugin/src/main/kotlin/org/wordpress/android/fluxc/store/WCRefundStore.kt +++ b/libs/fluxc-plugin/src/main/kotlin/org/wordpress/android/fluxc/store/WCRefundStore.kt @@ -2,6 +2,7 @@ package org.wordpress.android.fluxc.store import org.wordpress.android.fluxc.model.SiteModel import org.wordpress.android.fluxc.model.refunds.RefundMapper +import org.wordpress.android.fluxc.model.refunds.RefundRequestItem import org.wordpress.android.fluxc.model.refunds.WCRefundModel import org.wordpress.android.fluxc.network.BaseRequest.GenericErrorType.UNKNOWN import org.wordpress.android.fluxc.network.rest.wpcom.wc.WooError @@ -56,7 +57,7 @@ class WCRefundStore @Inject constructor( reason: String = "", restockItems: Boolean = true, autoRefund: Boolean = false, - items: List + items: List ): WooResult { return coroutineEngine.withDefaultContext(AppLog.T.API, this, "createItemsRefund") { val response = restClient.createRefundByItems( @@ -115,6 +116,7 @@ class WCRefundStore @Inject constructor( return@withDefaultContext when { response.isError -> WooResult(response.error) response.result != null -> { + WCRefundSqlUtils.deleteRefunds(site, orderId) WCRefundSqlUtils.insertOrUpdate(site, orderId, response.result.toList()) WooResult(response.result.map { refundsMapper.map(it) }) }