Skip to content

Commit ff3f8f0

Browse files
[IMP] shoppingfeed_integration: auto-pay channel out_refund
TT63051
1 parent 7d595f8 commit ff3f8f0

3 files changed

Lines changed: 105 additions & 8 deletions

File tree

shoppingfeed_integration/models/account_move.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,10 @@ def _shoppingfeed_upload_invoice(self):
8181

8282
def _shoppingfeed_auto_pay(self):
8383
for move in self:
84-
if move.move_type != "out_invoice" or move.payment_state == "paid":
84+
if (
85+
move.move_type not in ("out_invoice", "out_refund")
86+
or move.payment_state == "paid"
87+
):
8588
continue
8689
sale_orders = move.line_ids.sale_line_ids.order_id.filtered(
8790
lambda so, move=move: so.company_id == move.company_id
@@ -90,13 +93,23 @@ def _shoppingfeed_auto_pay(self):
9093
channel = sale_order.shoppingfeed_channel_id
9194
if not sale_order or not channel or not channel.auto_pay:
9295
continue
93-
if (
94-
not move.preferred_payment_method_line_id
95-
and channel.payment_method_line_id
96-
):
97-
move.preferred_payment_method_line_id = channel.payment_method_line_id
98-
if not move.preferred_payment_method_line_id:
99-
continue
96+
if move.move_type == "out_refund":
97+
# Force the outbound mirror line: the computed preferred for a
98+
# sale document is the partner inbound line, invalid here.
99+
method_line = channel._shoppingfeed_get_refund_payment_method_line()
100+
if not method_line:
101+
continue
102+
move.preferred_payment_method_line_id = method_line
103+
else:
104+
if (
105+
not move.preferred_payment_method_line_id
106+
and channel.payment_method_line_id
107+
):
108+
move.preferred_payment_method_line_id = (
109+
channel.payment_method_line_id
110+
)
111+
if not move.preferred_payment_method_line_id:
112+
continue
100113
self.env["account.payment.register"].with_context(
101114
active_model="account.move",
102115
active_ids=move.ids,

shoppingfeed_integration/models/shoppingfeed_channel.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,17 @@ class ShoppingfeedChannel(models.Model):
109109
" channel."
110110
),
111111
)
112+
113+
def _shoppingfeed_get_refund_payment_method_line(self):
114+
self.ensure_one()
115+
inbound = self.payment_method_line_id
116+
if not inbound or not inbound.payment_account_id:
117+
return self.env["account.payment.method.line"]
118+
return self.env["account.payment.method.line"].search(
119+
[
120+
("journal_id", "=", inbound.journal_id.id),
121+
("payment_type", "=", "outbound"),
122+
("payment_account_id", "=", inbound.payment_account_id.id),
123+
],
124+
limit=1,
125+
)

shoppingfeed_integration/tests/test_shoppingfeed_integration.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ class TestShoppingfeedIntegration(AccountTestInvoicingCommon):
1313
def setUpClass(cls):
1414
super().setUpClass()
1515
cls.bank_journal = cls.company_data["default_journal_bank"]
16+
cls.market_account = cls.company_data["default_account_receivable"].copy(
17+
{"name": "SF Market Clearing"}
18+
)
1619
cls.inbound_payment_method_line = cls.env["account.payment.method.line"].create(
1720
{
1821
"name": "SF Inbound Payment Method",
@@ -21,6 +24,21 @@ def setUpClass(cls):
2124
].id,
2225
"payment_type": "inbound",
2326
"journal_id": cls.bank_journal.id,
27+
"payment_account_id": cls.market_account.id,
28+
}
29+
)
30+
cls.outbound_manual_method = cls.env["account.payment.method"].search(
31+
[("code", "=", "manual"), ("payment_type", "=", "outbound")], limit=1
32+
)
33+
cls.outbound_payment_method_line = cls.env[
34+
"account.payment.method.line"
35+
].create(
36+
{
37+
"name": "SF Outbound Payment Method",
38+
"payment_method_id": cls.outbound_manual_method.id,
39+
"payment_type": "outbound",
40+
"journal_id": cls.bank_journal.id,
41+
"payment_account_id": cls.market_account.id,
2442
}
2543
)
2644
cls.sf_store = cls.env["shoppingfeed.store"].create(
@@ -52,6 +70,26 @@ def _sf_order_line(cls, product=None):
5270
}
5371
)
5472

73+
def _sf_refund(self, sale_order, product=None):
74+
product = product or self.product_a
75+
return self.env["account.move"].create(
76+
{
77+
"move_type": "out_refund",
78+
"partner_id": sale_order.partner_id.id,
79+
"invoice_origin": sale_order.name,
80+
"invoice_line_ids": [
81+
Command.create(
82+
{
83+
"product_id": product.id,
84+
"quantity": 1.0,
85+
"price_unit": product.list_price,
86+
"sale_line_ids": [Command.set(sale_order.order_line.ids)],
87+
}
88+
)
89+
],
90+
}
91+
)
92+
5593
def test_partner_gets_payment_method_on_create(self):
5694
"""New partner created during SF import receives
5795
property_inbound_payment_method_line_id from the channel."""
@@ -158,6 +196,38 @@ def test_invoice_not_auto_paid_when_disabled(self):
158196
self.assertNotEqual(invoice.payment_state, "paid")
159197
self.sf_channel.auto_pay = True
160198

199+
def test_refund_auto_paid_on_channel_account(self):
200+
self.partner_a.property_inbound_payment_method_line_id = (
201+
self.inbound_payment_method_line
202+
)
203+
sale_order = self.env["sale.order"].create(
204+
{
205+
"partner_id": self.partner_a.id,
206+
"shoppingfeed_order_ref": "SF-TEST-REFUND-001",
207+
"shoppingfeed_store_id": self.sf_store.id,
208+
"shoppingfeed_channel_id": self.sf_channel.id,
209+
"order_line": [self._sf_order_line()],
210+
}
211+
)
212+
sale_order.action_confirm()
213+
self.sf_channel.auto_pay = False
214+
unpaid = self._sf_refund(sale_order)
215+
unpaid.action_post()
216+
self.assertNotEqual(unpaid.payment_state, "paid")
217+
self.sf_channel.auto_pay = True
218+
refund = self._sf_refund(sale_order)
219+
refund.action_post()
220+
self.assertEqual(refund.payment_state, "paid")
221+
payment = refund.reconciled_payment_ids
222+
self.assertEqual(len(payment), 1)
223+
self.assertEqual(payment.payment_type, "outbound")
224+
self.assertEqual(
225+
payment.payment_method_line_id, self.outbound_payment_method_line
226+
)
227+
self.assertEqual(
228+
payment.payment_method_line_id.payment_account_id, self.market_account
229+
)
230+
161231
def test_product_reference_cleaning(self):
162232
"""_shoppingfeed_clean_product_reference strips 2-char country suffixes only."""
163233
so = self.env["sale.order"]

0 commit comments

Comments
 (0)