Skip to content
Draft
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
8 changes: 7 additions & 1 deletion rma/models/rma.py
Original file line number Diff line number Diff line change
Expand Up @@ -911,9 +911,15 @@ def action_refund(self):
{
"refund_line_id": line.id,
"refund_id": refund.id,
"state": "refunded",
}
)
line.rma_id._action_refund_after_hook()

def _action_refund_after_hook(self):
"""This method sets the status to 'refunded' and allows you to add
more actions, such as for rma_sale.
"""
self.state = "refunded"

def action_replace(self):
"""Invoked when 'Replace' button in rma form view is clicked."""
Expand Down
10 changes: 10 additions & 0 deletions rma_sale/i18n/es.po
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ msgstr "Producto Permitido"
msgid "Allowed Quantity"
msgstr "Cantidad permitida"

#. module: rma_sale
#: model:ir.model.fields,field_description:rma_sale.field_rma__can_be_refunded_with_invoice
msgid "Can Be Refunded With Invoice"
msgstr ""

#. module: rma_sale
#: model_terms:ir.ui.view,arch_db:rma_sale.sale_order_rma_wizard_form_view
msgid "Cancel"
Expand Down Expand Up @@ -272,6 +277,11 @@ msgstr "Última actualización el"
msgid "Lines"
msgstr "Líneas"

#. module: rma_sale
#: model_terms:ir.ui.view,arch_db:rma_sale.rma_view_form
msgid "Mark To Refund"
msgstr "Marcar a reembolsar"

#. module: rma_sale
#: model:ir.model.fields,field_description:rma_sale.field_sale_order_line_rma_wizard__move_id
msgid "Move"
Expand Down
10 changes: 10 additions & 0 deletions rma_sale/i18n/rma_sale.pot
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ msgstr ""
msgid "Allowed Quantity"
msgstr ""

#. module: rma_sale
#: model:ir.model.fields,field_description:rma_sale.field_rma__can_be_refunded_with_invoice
msgid "Can Be Refunded With Invoice"
msgstr ""

#. module: rma_sale
#: model_terms:ir.ui.view,arch_db:rma_sale.sale_order_rma_wizard_form_view
msgid "Cancel"
Expand Down Expand Up @@ -256,6 +261,11 @@ msgstr ""
msgid "Lines"
msgstr ""

#. module: rma_sale
#: model_terms:ir.ui.view,arch_db:rma_sale.rma_view_form
msgid "Mark To Refund"
msgstr ""

#. module: rma_sale
#: model:ir.model.fields,field_description:rma_sale.field_sale_order_line_rma_wizard__move_id
msgid "Move"
Expand Down
45 changes: 29 additions & 16 deletions rma_sale/models/rma.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,28 @@ class Rma(models.Model):
)
# Add index to this field, as we perform a search on it
refund_id = fields.Many2one(index=True)
can_be_refunded_with_invoice = fields.Boolean(
compute="_compute_can_be_refunded_with_invoice"
)

@api.depends(
"can_be_refunded",
"sale_line_id",
"sale_line_id.invoice_lines",
"move_id",
"move_id.rma_id",
"move_id.rma_id.sale_line_id",
"move_id.rma_id.sale_line_id.invoice_lines",
)
def _compute_can_be_refunded_with_invoice(self):
for rec in self:
rec.can_be_refunded_with_invoice = bool(
rec.can_be_refunded
and (
rec.sale_line_id.invoice_lines
or rec.move_id.rma_id.sale_line_id.invoice_lines
)
)

@api.depends("partner_id", "order_id")
def _compute_allowed_picking_ids(self):
Expand Down Expand Up @@ -120,33 +142,24 @@ def _unlink_refund_with_reception_move(self):
self.reception_move_id.sale_line_id = False
self.reception_move_id.to_refund = False

def action_refund(self):
def action_refund_without_invoice(self):
for record in self.filtered("can_be_refunded"):
record._action_refund_after_hook()

def _action_refund_after_hook(self):
"""As we have made a refund, the return move + the refund should be linked to
the source sales order line, to decrease both the delivered and invoiced
quantity.

NOTE: The refund line is linked to the SO line in `_prepare_refund_line`.
"""
_self = self
# Do not generate a refund invoice if the sales line has not yet been invoiced
# or if it is an RMA created from another RMA with the same condition
not_refundable = self.filtered(
lambda x: (x.sale_line_id and not x.sale_line_id.invoice_lines)
or (
x.move_id.rma_id.sale_line_id
and not x.move_id.rma_id.sale_line_id.invoice_lines
)
)
self -= not_refundable
res = super().action_refund()
for rma in _self:
res = super()._action_refund_after_hook()
for rma in self.filtered(lambda x: x.state == "refunded"):
if rma.sale_line_id:
rma._link_refund_with_reception_move()
elif not rma.sale_line_id and rma.move_id.rma_id.sudo().sale_line_id:
# If there is no sales line, we must apply it to the original RMA
rma.move_id.rma_id._link_refund_with_reception_move()
# These RMAs should be set to this state anyway
not_refundable.state = "refunded"
return res

def _prepare_refund_vals(self, origin=False):
Expand Down
4 changes: 2 additions & 2 deletions rma_sale/tests/test_rma_sale.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ def test_rma_rma_refund_from_so_without_invoice(self):
rma_extra.reception_move_id.quantity = rma_extra.product_uom_qty
rma_extra.reception_move_id.picking_id.button_validate()
self.assertEqual(rma_extra.state, "received")
rma_extra.action_refund()
rma_extra.action_refund_without_invoice()
self.assertEqual(rma_extra.state, "refunded")
self.assertFalse(rma_extra.refund_line_id)
self.assertFalse(rma_extra.refund_id)
Expand Down Expand Up @@ -313,7 +313,7 @@ def test_create_rma_from_so_without_invoice(self):
rma.reception_move_id.quantity = rma.product_uom_qty
rma.reception_move_id.picking_id.button_validate()
# Refund the RMA
rma.action_refund()
rma.action_refund_without_invoice()
self.assertEqual(self.order_line.qty_delivered, 0)
self.assertEqual(self.order_line.qty_invoiced, 0)
self.assertFalse(rma.refund_id)
Expand Down
12 changes: 12 additions & 0 deletions rma_sale/views/rma_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@
<field name="model">rma</field>
<field name="inherit_id" ref="rma.rma_view_form" />
<field name="arch" type="xml">
<button name="action_refund" position="attributes">
<attribute name="invisible">not can_be_refunded_with_invoice</attribute>
</button>
<button name="action_refund" position="after">
<button
type="object"
string="Mark To Refund"
name="action_refund_without_invoice"
invisible="not can_be_refunded"
class="btn-primary"
/>
</button>
<field name="partner_invoice_id" position="after">
<field
name="order_id"
Expand Down
Loading