Skip to content

Commit

Permalink
[IMP] create refund from rma group
Browse files Browse the repository at this point in the history
  • Loading branch information
chafique-delli committed Jul 3, 2024
1 parent 8d59e55 commit 2e59754
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 10 deletions.
9 changes: 9 additions & 0 deletions rma_account/models/rma_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ def _compute_invoice_count(self):
invoices = rec.mapped("rma_line_ids.move_id")
rec.invoice_count = len(invoices)

@api.depends("rma_line_ids", "rma_line_ids.qty_to_refund")
def _compute_qty_to_refund(self):
for rec in self:
rec.qty_to_refund = sum(rec.rma_line_ids.mapped("qty_to_refund"))

add_move_id = fields.Many2one(
comodel_name="account.move",
string="Add Invoice",
Expand All @@ -29,6 +34,10 @@ def _compute_invoice_count(self):
invoice_count = fields.Integer(
compute="_compute_invoice_count", string="# of Invoices"
)
qty_to_refund = fields.Float(
digits="Product Unit of Measure",
compute="_compute_qty_to_refund",
)

def _prepare_rma_line_from_inv_line(self, line):
if self.type == "customer":
Expand Down
3 changes: 3 additions & 0 deletions rma_account/views/rma_order_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
<xpath expr="//field[@name='rma_line_ids']/tree" position="inside">
<field name="refund_policy" invisible="True" />
</xpath>
<xpath expr="//field[@name='qty_to_deliver']" position="after">
<field name="qty_to_refund" invisible="1" />
</xpath>
</field>
</record>

Expand Down
2 changes: 1 addition & 1 deletion rma_account/wizards/rma_add_account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class RmaAddAccountMove(models.TransientModel):

@api.model
def default_get(self, fields_list):
res = super(RmaAddAccountMove, self).default_get(fields_list)
res = super().default_get(fields_list)
rma_obj = self.env["rma.order"]
rma_id = self.env.context["active_ids"] or []
active_model = self.env.context["active_model"]
Expand Down
41 changes: 32 additions & 9 deletions rma_account/wizards/rma_refund.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@ class RmaRefund(models.TransientModel):
@api.model
def _get_reason(self):
active_ids = self.env.context.get("active_ids", False)
return self.env["rma.order.line"].browse(active_ids[0]).rma_id.name or ""
active_model = self.env.context.get("active_model", False)
reason = ""
if active_model and active_ids:
if active_model == "rma.order":
reason = self.env[active_model].browse(active_ids[0]).name or ""
else:
reason = self.env[active_model].browse(active_ids[0]).rma_id.name or ""
return reason

@api.returns("rma.order.line")
def _prepare_item(self, line):
Expand All @@ -37,16 +44,24 @@ def default_get(self, fields_list):
supplier.
"""
context = self._context.copy()
res = super(RmaRefund, self).default_get(fields_list)
res = super().default_get(fields_list)
rma_line_obj = self.env["rma.order.line"]
rma_line_ids = self.env.context["active_ids"] or []
active_model = self.env.context["active_model"]
if not rma_line_ids:
rma_obj = self.env["rma.order"]
active_ids = context.get("active_ids") or []
active_model = context.get("active_model")
if not active_ids:
return res
assert active_model == "rma.order.line", "Bad context propagation"
assert active_model in [
"rma.order.line",
"rma.order",
], "Bad context propagation"

items = []
lines = rma_line_obj.browse(rma_line_ids)
if active_model == "rma.order":
rma = rma_obj.browse(active_ids)
lines = rma.rma_line_ids
else:
lines = rma_line_obj.browse(active_ids)
if len(lines.mapped("partner_id")) > 1:
raise ValidationError(
_(
Expand Down Expand Up @@ -90,8 +105,16 @@ def compute_refund(self):
return new_refund

def invoice_refund(self):
rma_line_ids = self.env["rma.order.line"].browse(self.env.context["active_ids"])
for line in rma_line_ids:
rma_line_obj = self.env["rma.order.line"]
rma_obj = self.env["rma.order"]
active_ids = self.env.context.get("active_ids") or []
active_model = self.env.context.get("active_model")
if active_model == "rma.order":
rma = rma_obj.browse(active_ids)
lines = rma.rma_line_ids
else:
lines = rma_line_obj.browse(active_ids)
for line in lines:
if line.state != "approved":
raise ValidationError(_("RMA %s is not approved") % line.name)
new_invoice = self.compute_refund()
Expand Down
22 changes: 22 additions & 0 deletions rma_account/wizards/rma_refund.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,26 @@
</field>
</record>

<record id="view_rma_button_refund_form" model="ir.ui.view">
<field name="model">rma.order</field>
<field name="inherit_id" ref="rma.view_rma_form" />
<field name="arch" type="xml">
<xpath expr="//header" position="inside">
<button
name="%(action_rma_refund)d"
string="Create Refund"
class="oe_highlight"
attrs="{'invisible':['|', '|', ('qty_to_refund', '=', 0), ('qty_to_refund', '&lt;', 0), ('state', '!=', 'approved')]}"
type="action"
/>
<button
name="%(action_rma_refund)d"
string="Create Refund"
attrs="{'invisible':['|', ('qty_to_refund', '>', 0), ('state', '!=', 'approved')]}"
type="action"
/>
</xpath>
</field>
</record>

</odoo>

0 comments on commit 2e59754

Please sign in to comment.