Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[12.0][IMP] rma*: Refactor all rma modules in order to consider using the correct price unit in moves (backport) #428

Open
wants to merge 2 commits into
base: 12.0
Choose a base branch
from
Open
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
7 changes: 2 additions & 5 deletions rma/models/procurement.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,8 @@ def _get_stock_move_values(self, product_id, product_qty, product_uom,
if line.delivery_address_id:
res['partner_id'] = line.delivery_address_id.id
else:
res['partner_id'] = line.rma_id.partner_id.id
dest_loc = self.env["stock.location"].browse([
res["location_dest_id"]])[0]
if dest_loc.usage == "internal":
res["price_unit"] = line.price_unit
res["partner_id"] = line.rma_id.partner_id.id
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
3 changes: 0 additions & 3 deletions rma_account/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# © 2017 Eficent Business and IT Consulting Services S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)

from . import rma_order
from . import rma_order_line
from . import rma_operation
Expand Down
4 changes: 2 additions & 2 deletions rma_account/models/invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,6 @@ def _compute_rma_count(self):

rma_line_id = fields.Many2one(
comodel_name='rma.order.line',
string="RMA line refund",
string="RMA line",
ondelete="set null",
help="This will contain the rma line that originated the refund line")
help="This will contain the rma line that originated this line")
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
3 changes: 0 additions & 3 deletions rma_purchase/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
# © 2017 Eficent Business and IT Consulting Services S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)

from . import models
from . import wizards
7 changes: 3 additions & 4 deletions rma_purchase/__manifest__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
# © 2017 Eficent Business and IT Consulting Services S.L.
# Copyright 2017-2022 ForgeFlow S.L.
# 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',
'author': 'Eficent, Odoo Community Association (OCA)',
'author': 'ForgeFlow, Odoo Community Association (OCA)',
'website': 'https://github.com/Eficent/stock-rma',
'depends': ['rma_account', 'purchase'],
'data': [
Expand Down
2 changes: 0 additions & 2 deletions rma_purchase/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)

from . import rma_order
from . import rma_order_line
from . import purchase_order
Expand Down
2 changes: 1 addition & 1 deletion rma_purchase/models/purchase_order.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2017-18 Eficent Business and IT Consulting Services S.L.
# Copyright 2017-2022 ForgeFlow S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)

from odoo import api, models
Expand Down
2 changes: 1 addition & 1 deletion rma_purchase/models/purchase_order_line.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2017 Eficent Business and IT Consulting Services S.L.
# Copyright 2017-2022 ForgeFlow S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)

from odoo import api, fields, models
Expand Down
2 changes: 1 addition & 1 deletion rma_purchase/models/rma_operation.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2018 Eficent Business and IT Consulting Services S.L.
# Copyright 2017-2022 ForgeFlow S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)

from odoo import api, fields, models, _
Expand Down
2 changes: 1 addition & 1 deletion rma_purchase/models/rma_order.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# © 2017 Eficent Business and IT Consulting Services S.L.
# Copyright 2017-2022 ForgeFlow S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)

from odoo import api, fields, models
Expand Down
20 changes: 19 additions & 1 deletion rma_purchase/models/rma_order_line.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# © 2017 Eficent Business and IT Consulting Services S.L.
# Copyright 2017-2022 ForgeFlow S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)

from odoo import api, fields, models, _
Expand Down 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
3 changes: 0 additions & 3 deletions rma_purchase/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
# Copyright 2018 Eficent Business and IT Consulting Services S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)

from . import test_rma_purchase
2 changes: 1 addition & 1 deletion rma_purchase/tests/test_rma_purchase.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2017-18 Eficent Business and IT Consulting Services S.L.
# Copyright 2017-2022 ForgeFlow S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)

from odoo.tests import common
Expand Down
3 changes: 0 additions & 3 deletions rma_purchase/wizards/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# © 2017 Eficent Business and IT Consulting Services S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)

from . import rma_make_picking
from . import rma_add_purchase
from . import rma_order_line_make_purchase_order
3 changes: 1 addition & 2 deletions rma_purchase/wizards/rma_add_purchase.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# © 2017 Eficent Business and IT Consulting Services S.L.
# Copyright 2017-2022 ForgeFlow S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)

from odoo import _, api, fields, models
from odoo.exceptions import ValidationError

Expand Down
2 changes: 1 addition & 1 deletion rma_purchase/wizards/rma_make_picking.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# © 2017 Eficent Business and IT Consulting Services S.L.
# Copyright 2017-2022 ForgeFlow S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)

from odoo import api, fields, models
Expand Down
4 changes: 2 additions & 2 deletions rma_purchase/wizards/rma_order_line_make_purchase_order.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Copyright 2018 Eficent Business and IT Consulting Services S.L.
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl-3.0).
# Copyright 2017-2022 ForgeFlow S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)

import odoo.addons.decimal_precision as dp
from odoo import fields, models, api, _, exceptions
Expand Down
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
2 changes: 1 addition & 1 deletion rma_sale/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2017 Eficent Business and IT Consulting Services S.L.
# Copyright 2020-2022 ForgeFlow S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)
from . import sale_order_line
from . import sale_order
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