Skip to content

Commit

Permalink
[IMP] rma: Code and method reduction to simplify logic
Browse files Browse the repository at this point in the history
TT48789
  • Loading branch information
victoralmau committed May 21, 2024
1 parent 9bbe997 commit 421d078
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 114 deletions.
186 changes: 79 additions & 107 deletions rma/models/rma.py
Original file line number Diff line number Diff line change
Expand Up @@ -642,9 +642,9 @@ def action_rma_send(self):
}

def _add_message_subscribe_partner(self):
for rma in self:
if rma.partner_id and rma.partner_id not in rma.message_partner_ids:
rma.message_subscribe([rma.partner_id.id])
self.ensure_one()
if self.partner_id and self.partner_id not in self.message_partner_ids:
self.message_subscribe([self.partner_id.id])

def _product_is_storable(self, product=None):
product = product or self.product_id
Expand All @@ -657,17 +657,15 @@ def _prepare_procurement_group_values(self):
"name": self and ", ".join(self.mapped("name")) or False,
}

def _create_procurement_group(self, values=None):
vals = self._prepare_procurement_group_values()
if values:
vals.update(values)
return self.env["procurement.group"].create(vals)

def _prepare_procurement_values(
self, warehouse=None, scheduled_date=None, group=None
):
self.ensure_one()
group = group or self.procurement_group_id or self._create_procurement_group()
group = group or self.procurement_group_id
if not group:
group = self.env["procurement.group"].create(

Check warning on line 666 in rma/models/rma.py

View check run for this annotation

Codecov / codecov/patch

rma/models/rma.py#L666

Added line #L666 was not covered by tests
self._prepare_procurement_group_values()
)
return {
"company_id": self.company_id,
"group_id": group,
Expand All @@ -677,31 +675,6 @@ def _prepare_procurement_values(
"priority": self.priority,
}

def _prepare_procurement(
self,
location,
values,
product=None,
qty=None,
uom=None,
):
self.ensure_one()
group = values.get("group_id")
product = product or self.product_id
if not qty:
qty = self.product_uom_qty
uom = self.product_uom
return self.env["procurement.group"].Procurement(
product,
qty,
uom,
location,
product.display_name,
group.name,
self.company_id,
values,
)

def _prepare_reception_procurement_values(self, group=None):
values = self._prepare_procurement_values(group=group)
values.update(
Expand All @@ -717,22 +690,27 @@ def _prepare_reception_procurement_values(self, group=None):
)
return values

def _prepare_reception_procurement_group_values(self):
self.ensure_one()
return {}

def _create_reception_procurement_group(self):
return self._create_procurement_group(
self._prepare_reception_procurement_group_values()
return self.env["procurement.group"].create(
self._prepare_procurement_group_values()
)

def _prepare_reception_procurement(self):
self.ensure_one()
group = self.procurement_group_id
if not group:
group = self._create_reception_procurement_group()
values = self._prepare_reception_procurement_values(group)
return self._prepare_procurement(self.location_id, values)
product = self.product_id
return self.env["procurement.group"].Procurement(
product,
self.product_uom_qty,
self.product_uom,
self.location_id,
product.display_name,
group.name,
self.company_id,
self._prepare_reception_procurement_values(group),
)

def _prepare_reception_procurements(self):
procurements = []
Expand All @@ -759,7 +737,8 @@ def action_confirm(self):
self._create_receptions()
self.reception_move_id.picking_id.action_assign()
self.write({"state": "confirmed"})
self._add_message_subscribe_partner()
for rma in self:
rma._add_message_subscribe_partner()
self._send_confirmation_email()

def action_refund(self):
Expand Down Expand Up @@ -815,11 +794,8 @@ def action_replace(self):
self._ensure_can_be_replaced()
# Force active_id to avoid issues when coming from smart buttons
# in other models
action = (
self.env.ref("rma.rma_delivery_wizard_action")
.sudo()
.with_context(active_id=self.id)
.read()[0]
action = self.env["ir.actions.act_window"]._for_xml_id(
"rma.rma_delivery_wizard_action"
)
action["name"] = "Replace product(s)"
action["context"] = dict(self.env.context)
Expand All @@ -838,11 +814,8 @@ def action_return(self):
self._ensure_can_be_returned()
# Force active_id to avoid issues when coming from smart buttons
# in other models
action = (
self.env.ref("rma.rma_delivery_wizard_action")
.sudo()
.with_context(active_id=self.id)
.read()[0]
action = self.env["ir.actions.act_window"]._for_xml_id(

Check warning on line 817 in rma/models/rma.py

View check run for this annotation

Codecov / codecov/patch

rma/models/rma.py#L817

Added line #L817 was not covered by tests
"rma.rma_delivery_wizard_action"
)
action["context"] = dict(self.env.context)
action["context"].update(
Expand All @@ -858,11 +831,8 @@ def action_split(self):
self._ensure_can_be_split()
# Force active_id to avoid issues when coming from smart buttons
# in other models
action = (
self.env.ref("rma.rma_split_wizard_action")
.sudo()
.with_context(active_id=self.id)
.read()[0]
action = self.env["ir.actions.act_window"]._for_xml_id(

Check warning on line 834 in rma/models/rma.py

View check run for this annotation

Codecov / codecov/patch

rma/models/rma.py#L834

Added line #L834 was not covered by tests
"rma.rma_split_wizard_action"
)
action["context"] = dict(self.env.context)
action["context"].update(active_id=self.id, active_ids=self.ids)
Expand All @@ -874,11 +844,8 @@ def action_finish(self):
self._ensure_can_be_returned()
# Force active_id to avoid issues when coming from smart buttons
# in other models
action = (
self.env.ref("rma.rma_finalization_wizard_action")
.sudo()
.with_context(active_id=self.id)
.read()[0]
action = self.env["ir.actions.act_window"]._for_xml_id(
"rma.rma_finalization_wizard_action"
)
action["context"] = dict(self.env.context)
action["context"].update(active_id=self.id, active_ids=self.ids)
Expand Down Expand Up @@ -911,15 +878,12 @@ def action_preview(self):
"url": self.get_portal_url(),
}

def action_view_pickings(self, pickings):
def _action_view_pickings(self, pickings):
self.ensure_one()
# Force active_id to avoid issues when coming from smart buttons
# in other models
action = (
self.env.ref("stock.action_picking_tree_all")
.sudo()
.with_context(active_id=self.id)
.read()[0]
action = self.env["ir.actions.act_window"]._for_xml_id(

Check warning on line 885 in rma/models/rma.py

View check run for this annotation

Codecov / codecov/patch

rma/models/rma.py#L885

Added line #L885 was not covered by tests
"stock.action_picking_tree_all"
)
if len(pickings) > 1:
action["domain"] = [("id", "in", pickings.ids)]

Check warning on line 889 in rma/models/rma.py

View check run for this annotation

Codecov / codecov/patch

rma/models/rma.py#L889

Added line #L889 was not covered by tests
Expand All @@ -934,7 +898,7 @@ def action_view_pickings(self, pickings):

def action_view_receipt(self):
"""Invoked when 'Receipt' smart button in rma form view is clicked."""
return self.action_view_pickings(self.reception_move_id.picking_id)
return self._action_view_pickings(self.reception_move_id.picking_id)

Check warning on line 901 in rma/models/rma.py

View check run for this annotation

Codecov / codecov/patch

rma/models/rma.py#L901

Added line #L901 was not covered by tests

def action_view_refund(self):
"""Invoked when 'Refund' smart button in rma form view is clicked."""
Expand All @@ -951,7 +915,7 @@ def action_view_refund(self):

def action_view_delivery(self):
"""Invoked when 'Delivery' smart button in rma form view is clicked."""
return self.action_view_pickings(self.delivery_move_ids.picking_id)
return self._action_view_pickings(self.delivery_move_ids.picking_id)

Check warning on line 918 in rma/models/rma.py

View check run for this annotation

Codecov / codecov/patch

rma/models/rma.py#L918

Added line #L918 was not covered by tests

# Validation business methods
def _ensure_required_fields(self):
Expand Down Expand Up @@ -1195,19 +1159,22 @@ def _prepare_delivery_procurement_values(self, scheduled_date=None):
def _prepare_delivery_procurement(self, scheduled_date=None, qty=None, uom=None):
self.ensure_one()
values = self._prepare_delivery_procurement_values(scheduled_date)
return self._prepare_procurement(
group = values.get("group_id")
product = self.product_id
return self.env["procurement.group"].Procurement(
product,
qty or self.product_uom_qty,
uom or self.product_uom,
self.partner_shipping_id.property_stock_customer,
product.display_name,
group.name,
self.company_id,
values,
qty=qty,
uom=uom,
)

def _prepare_delivery_procurement_group_values(self):
return {}

def _create_delivery_procurement_group(self):
return self._create_procurement_group(
self._prepare_delivery_procurement_group_values()
return self.env["procurement.group"].create(
self._prepare_procurement_group_values()
)

def _prepare_delivery_procurements(self, scheduled_date=None, qty=None, uom=None):
Expand Down Expand Up @@ -1258,12 +1225,9 @@ def create_return(self, scheduled_date, qty=None, uom=None):
def _prepare_replace_procurement_values(self, warehouse=None, scheduled_date=None):
return self._prepare_outgoing_procurement_values(warehouse, scheduled_date)

def _prepare_replace_procurement_group_values(self):
return {}

def _create_replace_procurement_group(self):
return self._create_procurement_group(
self._prepare_replace_procurement_group_values()
return self.env["procurement.group"].create(
self._prepare_procurement_group_values()
)

def _prepare_replace_procurement(
Expand All @@ -1273,12 +1237,16 @@ def _prepare_replace_procurement(
self.procurement_group_id = self._create_replace_procurement_group()

values = self._prepare_replace_procurement_values(warehouse, scheduled_date)
return self._prepare_procurement(
self.partner_shipping_id.property_stock_customer,
values,
group = values.get("group_id")
return self.env["procurement.group"].Procurement(
product,
qty,
uom,
self.partner_shipping_id.property_stock_customer,
product.display_name,
group.name,
self.company_id,
values,
)

def _prepare_replace_procurements(
Expand Down Expand Up @@ -1332,27 +1300,31 @@ def create_replace(self, scheduled_date, warehouse, product, qty, uom):
+ "\n"
)
for rma in self:
rma.message_post(
body=body
or _(
"Replacement:<br/>"
'Product <a href="#" data-oe-model="product.product" '
'data-oe-id="%(id)d">%(name)s</a><br/>'
"Quantity %(qty)s %(uom)s<br/>"
"This replacement did not create a new move, but one of "
"the previously created moves was updated with this data."
)
% (
{
"id": product.id,
"name": product.display_name,
"qty": qty,
"uom": uom.name,
}
)
)
rma._add_replace_message(body, qty, uom)
self.write({"state": "waiting_replacement"})

def _add_replace_message(self, body, qty, uom):
self.ensure_one()
self.message_post(
body=body
or _(
"Replacement:<br/>"
'Product <a href="#" data-oe-model="product.product" '
'data-oe-id="%(id)d">%(name)s</a><br/>'
"Quantity %(qty)s %(uom)s<br/>"
"This replacement did not create a new move, but one of "
"the previously created moves was updated with this data."
)
% (
{
"id": self.id,
"name": self.display_name,
"qty": qty,
"uom": uom.name,
}
)
)

# Mail business methods
def _creation_subtype(self):
if self.state in ("draft"):
Expand Down
2 changes: 1 addition & 1 deletion rma/models/stock_picking.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def copy(self, default=None):

def action_view_rma(self):
self.ensure_one()
action = self.sudo().env.ref("rma.rma_action").read()[0]
action = self.env["ir.actions.act_window"]._for_xml_id("rma.rma_action")

Check warning on line 32 in rma/models/stock_picking.py

View check run for this annotation

Codecov / codecov/patch

rma/models/stock_picking.py#L32

Added line #L32 was not covered by tests
rma = self.move_lines.mapped("rma_ids")
if len(rma) == 1:
action.update(
Expand Down
12 changes: 6 additions & 6 deletions rma/wizard/stock_picking_return.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ def _prepare_rma_partner_values(self):
def _prepare_rma_values(self):
partner, partner_invoice, partner_shipping = self._prepare_rma_partner_values()
origin = self.picking_id.name
group = self.env["rma"]._create_procurement_group(
{
"partner_id": partner_shipping.id,
"name": origin,
}
)
group_vals = self.env["rma"]._prepare_procurement_group_values()
group_vals.update({
"partner_id": partner_shipping.id,
"name": origin,
})
group = self.env["procurement.group"].create(group_vals)
return {
"user_id": self.env.user.id,
"partner_id": partner.id,
Expand Down

0 comments on commit 421d078

Please sign in to comment.