Skip to content

Commit

Permalink
[IMP] centralize the logic to get the correct cost of the RMA.
Browse files Browse the repository at this point in the history
  • Loading branch information
JordiBForgeFlow authored and antonioburic committed Apr 10, 2023
1 parent 5fc7f98 commit 2b46c44
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 11 deletions.
5 changes: 1 addition & 4 deletions rma/models/procurement.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ def _get_stock_move_values(self, product_id, product_qty, product_uom,
res['partner_id'] = line.delivery_address_id.id
else:
res["partner_id"] = line.rma_id.partner_id.id
company_id = res["company_id"]
company = self.env["res.company"].browse(company_id)
cost = product_id.with_company(company).standard_price
res["price_unit"] = cost
res["price_unit"] = line._get_price_unit()
return res


Expand Down
19 changes: 15 additions & 4 deletions rma/models/rma_order_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,10 @@ def _compute_rma_line_count(self):
readonly=True, states={'draft': [('readonly', False)]},
)
price_unit = fields.Monetary(
string='Price Unit',
readonly=True, states={'draft': [('readonly', False)]},
string="Unit cost",
readonly=True,
states={"draft": [("readonly", False)]},
help="Unit cost of the items under RMA",
)
in_shipment_count = fields.Integer(compute='_compute_in_shipment_count',
string='# of Shipments')
Expand Down Expand Up @@ -504,15 +506,24 @@ def create(self, vals):
'rma.order.line.customer')
return super(RmaOrderLine, self).create(vals)

@api.onchange('product_id')
def _get_price_unit(self):
"""The price unit corresponds to the cost of that product"""
self.ensure_one()
if self.reference_move_id:
price_unit = self.reference_move_id.price_unit
else:
price_unit = self.product_id.with_context(force_company=self.company_id.id).standard_price
return price_unit

@api.onchange("product_id")
def _onchange_product_id(self):
result = {}
if not self.product_id:
return result
self.uom_id = self.product_id.uom_id.id
self.price_unit = self.product_id.standard_price
if not self.type:
self.type = self._get_default_type()
self.price_unit = self._get_price_unit()
if self.type == 'customer':
self.operation_id = self.product_id.rma_customer_operation_id or \
self.product_id.categ_id.rma_customer_operation_id
Expand Down
2 changes: 1 addition & 1 deletion rma_account/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

{
'name': 'RMA Account',
'version': '12.0.1.0.0',
'version': '12.0.1.0.1',
'license': 'LGPL-3',
'category': 'RMA',
'summary': 'Integrates RMA with Invoice Processing',
Expand Down
10 changes: 10 additions & 0 deletions rma_account/models/rma_order_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,13 @@ def name_get(self):
return res
else:
return super(RmaOrderLine, self).name_get()

def _get_price_unit(self):
self.ensure_one()
price_unit = super(RmaOrderLine, self)._get_price_unit()
if self.reference_move_id and self.reference_move_id.value:
price_unit = self.reference_move_id.value / self.reference_move_id.product_qty
elif self.invoice_line_id and self.type == "supplier":
# We get the cost from the original invoice line
price_unit = self.invoice_line_id.price_unit
return price_unit
2 changes: 1 addition & 1 deletion rma_purchase/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)
{
'name': 'RMA Purchase',
'version': '12.0.1.0.0',
'version': '12.0.1.0.1',
'category': 'RMA',
'summary': 'RMA from PO',
'license': 'LGPL-3',
Expand Down
18 changes: 18 additions & 0 deletions rma_purchase/models/rma_order_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,21 @@ def _get_rma_purchased_qty(self):
qty += self.uom_id._compute_quantity(
line.product_qty, line.product_uom)
return qty

def _get_price_unit(self):
self.ensure_one()
price_unit = super(RmaOrderLine, self)._get_price_unit()
if self.purchase_order_line_id:
moves = self.purchase_order_line_id.move_ids
if moves:
price_unit = sum(moves.mapped("value")) / sum(
moves.mapped("product_qty")
)
elif self.invoice_line_id.purchase_line_id:
purchase_lines = self.invoice_line_id.purchase_line_id
moves = purchase_lines.mapped("move_ids")
if moves:
price_unit = sum(moves.mapped("value")) / sum(
moves.mapped("product_qty")
)
return price_unit
2 changes: 1 addition & 1 deletion rma_sale/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

{
'name': 'RMA Sale',
'version': '12.0.1.1.0',
'version': '12.0.1.1.1',
'license': 'LGPL-3',
'category': 'RMA',
'summary': 'Links RMA with Sales Orders',
Expand Down
26 changes: 26 additions & 0 deletions rma_sale/models/rma_order_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,29 @@ def _get_rma_sold_qty(self):
lambda p: p.state not in ('draft', 'sent', 'cancel')):
qty += sale_line.product_uom_qty
return qty

def _get_price_unit(self):
self.ensure_one()
price_unit = super(RmaOrderLine, self)._get_price_unit()
if self.sale_line_id:
moves = self.sale_line_id.move_ids.filtered(
lambda x: x.state == "done"
and x.location_id.usage in ("internal", "supplier")
and x.location_dest_id.usage == "customer"
)
if moves:
price_unit = sum(moves.mapped("value")) / sum(
moves.mapped("product_qty")
)
elif self.invoice_line_id:
sale_lines = self.invoice_line_id.sale_line_ids
moves = sale_lines.mapped("move_ids").filtered(
lambda x: x.state == "done"
and x.location_id.usage in ("internal", "supplier")
and x.location_dest_id.usage == "customer"
)
if moves:
price_unit = sum(moves.mapped("value")) / sum(
moves.mapped("product_qty")
)
return price_unit

0 comments on commit 2b46c44

Please sign in to comment.