Merge pull request #516 from ForgeFlow/17.0-mig-rma_sale-good

[17.0][MIG] rma_sale
This commit is contained in:
Lois Rilo
2025-01-09 13:54:54 +01:00
committed by GitHub
27 changed files with 1790 additions and 0 deletions

57
rma_sale/README.rst Normal file
View File

@@ -0,0 +1,57 @@
.. image:: https://img.shields.io/badge/licence-LGPL--3-blue.svg
:alt: License LGPL-3
========
RMA Sale
========
This module allows you to:
#. Import sales order lines into RMA lines
#. Create a sales order and/or sales order line from one or more RMA lines
Usage
=====
**Import existing sales order lines into an RMA:**
This feature is useful when you create an RMA associated to a product that
was shipped and you have as a reference the customer PO number.
#. Access to a customer RMA.
#. Fill the customer.
#. Press the button *Add from Sales Order*.
#. In the wizard add a sales order and click on *add item* to select the
lines you want to add to the RMA.
**Create a sales order and/or sales order line from RMA lines:**
#. Go to a approved RMA line.
#. Click on *Create a Sales Quotation*.
#. In the wizard, select an *Existing Quotation to update* or leave it empty
if you want to create a new one.
#. Fill the quantity to sell in the lines.
#. Hit *Create a Sales Quotation*.
Bug Tracker
===========
Bugs are tracked on `GitHub Issues
<https://github.com/Eficent/stock-rma/issues>`_. In case of trouble, please
check there if your issue has already been reported. If you spotted it first,
help us smashing it by providing a detailed and welcomed feedback.
Credits
=======
Contributors
------------
* Jordi Ballester Alomar <jordi.ballester@eficent.com>
* Aaron Henriquez <ahenriquez@eficent.com>
* Lois Rilo <lois.rilo@eficent.com>
Maintainer
----------
This module is maintained by Eficent

4
rma_sale/__init__.py Normal file
View File

@@ -0,0 +1,4 @@
# Copyright 2020 ForgeFlow S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)
from . import models
from . import wizards

24
rma_sale/__manifest__.py Normal file
View File

@@ -0,0 +1,24 @@
# Copyright 2020 ForgeFlow S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)
{
"name": "RMA Sale",
"version": "17.0.1.0.0",
"license": "LGPL-3",
"category": "RMA",
"summary": "Links RMA with Sales Orders",
"author": "ForgeFlow",
"website": "https://github.com/ForgeFlow",
"depends": ["rma_account", "sale_stock"],
"data": [
"security/ir.model.access.csv",
"data/rma_operation.xml",
"views/rma_order_view.xml",
"views/rma_operation_view.xml",
"views/sale_order_view.xml",
"wizards/rma_order_line_make_sale_order_view.xml",
"wizards/rma_add_sale.xml",
"views/rma_order_line_view.xml",
],
"installable": True,
}

View File

@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo noupdate="1">
<record id="rma_operation_customer_sale" model="rma.operation">
<field name="name">Sale after receive</field>
<field name="code">SL-C</field>
<field name="sale_policy">received</field>
<field name="receipt_policy">ordered</field>
<field name="delivery_policy">no</field>
<field name="refund_policy">no</field>
<field name="type">customer</field>
<field name="in_route_id" ref="rma.route_rma_customer" />
<field name="out_route_id" ref="rma.route_rma_customer" />
</record>
<record id="rma_operation_customer_sale_advanced" model="rma.operation">
<field name="name">Advanced Refund</field>
<field name="code">AR-C</field>
<field name="sale_policy">received</field>
<field name="receipt_policy">ordered</field>
<field name="delivery_policy">no</field>
<field name="refund_policy">received</field>
<field name="type">customer</field>
<field name="in_route_id" ref="rma.route_rma_customer" />
<field name="out_route_id" ref="rma.route_rma_customer" />
</record>
</odoo>

View File

@@ -0,0 +1,8 @@
# 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
from . import rma_order_line
from . import rma_order
from . import rma_operation
from . import procurement

View File

@@ -0,0 +1,37 @@
# Copyright 2022 ForgeFlow S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)
from odoo import models
class StockRule(models.Model):
_inherit = "stock.rule"
def _get_stock_move_values(
self,
product_id,
product_qty,
product_uom,
location_id,
name,
origin,
company_id,
values,
):
res = super()._get_stock_move_values(
product_id,
product_qty,
product_uom,
location_id,
name,
origin,
company_id,
values,
)
if "rma_line_id" in values:
line = values.get("rma_line_id")
line = self.env["rma.order.line"].browse([line])
if line.reference_move_id:
return res
res["price_unit"] = line._get_price_unit()
return res

View File

@@ -0,0 +1,26 @@
# Copyright 2020 ForgeFlow S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)
from odoo import fields, models
class RmaOperation(models.Model):
_inherit = "rma.operation"
sale_policy = fields.Selection(
[
("no", "Not required"),
("ordered", "Based on Ordered Quantities"),
("received", "Based on Received Quantities"),
],
default="no",
)
auto_confirm_rma_sale = fields.Boolean(
string="Auto confirm Sales Order upon creation from RMA",
help="When a sales is created from an RMA, automatically confirm it",
readonly=False,
)
free_of_charge_rma_sale = fields.Boolean(
string="Free of charge RMA Sales Order",
help="Sales orders created from RMA are free of charge by default",
readonly=False,
)

View File

@@ -0,0 +1,38 @@
# Copyright 2020 ForgeFlow S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)
from odoo import api, fields, models
class RmaOrder(models.Model):
_inherit = "rma.order"
@api.depends(
"rma_line_ids",
"rma_line_ids.sale_line_id",
"rma_line_ids.sale_line_id.order_id",
)
def _compute_sales_count(self):
for rma in self:
sales = rma.mapped("rma_line_ids.sale_line_id.order_id")
rma.sale_count = len(sales)
sale_count = fields.Integer(compute="_compute_sales_count", string="# of Sales")
@api.model
def _get_line_domain(self, rma_id, line):
if line.sale_line_id and line.sale_line_id.id:
domain = [
("rma_id", "=", rma_id.id),
("type", "=", "supplier"),
("sale_line_id", "=", line.sale_line_id.id),
]
else:
domain = super()._get_line_domain(rma_id, line)
return domain
def action_view_sale_order(self):
action = self.env.ref("sale.action_quotations")
result = action.sudo().read()[0]
so_ids = self.mapped("rma_line_ids.sale_line_id.order_id").ids
result["domain"] = [("id", "in", so_ids)]
return result

View File

@@ -0,0 +1,249 @@
# Copyright 2020 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
class RmaOrderLine(models.Model):
_inherit = "rma.order.line"
@api.depends(
"sale_line_ids",
"sale_policy",
"sales_count",
"sale_line_ids.state",
"qty_received",
"product_qty",
)
def _compute_qty_to_sell(self):
for rec in self:
if rec.sale_policy == "ordered":
qty = rec._get_rma_sold_qty()
rec.qty_to_sell = rec.product_qty - qty
elif rec.sale_policy == "received":
qty = rec._get_rma_sold_qty()
rec.qty_to_sell = rec.qty_received - qty
else:
rec.qty_to_sell = 0.0
@api.depends("sale_line_ids", "sale_policy", "sales_count", "sale_line_ids.state")
def _compute_qty_sold(self):
for rec in self:
rec.qty_sold = rec._get_rma_sold_qty()
@api.depends("sale_line_ids", "sale_line_ids.order_id")
def _compute_sales_count(self):
for line in self:
sales = line.mapped("sale_line_ids.order_id")
line.sales_count = len(sales)
sale_line_id = fields.Many2one(
comodel_name="sale.order.line",
string="Originating Sales Order Line",
ondelete="restrict",
copy=False,
)
sale_id = fields.Many2one(
string="Source Sales Order", related="sale_line_id.order_id"
)
sale_line_ids = fields.One2many(
comodel_name="sale.order.line",
inverse_name="rma_line_id",
string="Sales Order Lines",
copy=False,
)
qty_to_sell = fields.Float(
copy=False,
digits="Product Unit of Measure",
readonly=True,
compute="_compute_qty_to_sell",
store=True,
)
qty_sold = fields.Float(
copy=False,
digits="Product Unit of Measure",
readonly=True,
compute="_compute_qty_sold",
store=True,
)
sale_policy = fields.Selection(
selection=[
("no", "Not required"),
("ordered", "Based on Ordered Quantities"),
("received", "Based on Received Quantities"),
],
default="no",
required=True,
readonly=False,
)
sales_count = fields.Integer(compute="_compute_sales_count", string="# of Sales")
@api.onchange("product_id", "partner_id")
def _onchange_product_id(self):
"""Domain for sale_line_id is computed here to make it dynamic."""
res = super()._onchange_product_id()
if not res.get("domain"):
res["domain"] = {}
domain = [
"|",
("order_id.partner_id", "=", self.partner_id.id),
("order_id.partner_id", "child_of", self.partner_id.id),
]
if self.product_id:
domain.append(("product_id", "=", self.product_id.id))
res["domain"]["sale_line_id"] = domain
return res
@api.onchange("operation_id")
def _onchange_operation_id(self):
res = super()._onchange_operation_id()
if self.operation_id:
self.sale_policy = self.operation_id.sale_policy or "no"
return res
def _prepare_rma_line_from_sale_order_line(self, line):
self.ensure_one()
if not self.type:
self.type = self._get_default_type()
operation = line.product_id.rma_customer_operation_id
if not operation:
operation = line.product_id.categ_id.rma_customer_operation_id
if not operation:
operation = self.env["rma.operation"].search(
[("type", "=", self.type)], limit=1
)
if not operation:
raise ValidationError(_("Please define an operation first"))
if not operation.in_route_id or not operation.out_route_id:
route = self.env["stock.location.route"].search(
[("rma_selectable", "=", True)], limit=1
)
if not route:
raise ValidationError(_("Please define an RMA route"))
if not operation.in_warehouse_id or not operation.out_warehouse_id:
warehouse = self.env["stock.warehouse"].search(
[("company_id", "=", self.company_id.id), ("lot_rma_id", "!=", False)],
limit=1,
)
if not warehouse:
raise ValidationError(
_("Please define a warehouse with a default RMA location.")
)
data = {
"product_id": line.product_id.id,
"origin": line.order_id.name,
"uom_id": line.product_uom.id,
"operation_id": operation.id,
"product_qty": line.product_uom_qty,
"delivery_address_id": line.order_id.partner_id.id,
"invoice_address_id": line.order_id.partner_id.id,
"price_unit": line.currency_id._convert(
line.price_unit,
line.currency_id,
line.company_id,
line.order_id.date_order,
round=False,
),
"in_route_id": operation.in_route_id.id or route.id,
"out_route_id": operation.out_route_id.id or route.id,
"receipt_policy": operation.receipt_policy,
"currency_id": line.currency_id.id,
"location_id": (
operation.location_id.id
or operation.in_warehouse_id.lot_rma_id.id
or warehouse.lot_rma_id.id
),
"refund_policy": operation.refund_policy,
"delivery_policy": operation.delivery_policy,
"in_warehouse_id": operation.in_warehouse_id.id or warehouse.id,
"out_warehouse_id": operation.out_warehouse_id.id or warehouse.id,
}
return data
@api.onchange("sale_line_id")
def _onchange_sale_line_id(self):
if not self.sale_line_id:
return
data = self._prepare_rma_line_from_sale_order_line(self.sale_line_id)
self.update(data)
self._remove_other_data_origin("sale_line_id")
def _remove_other_data_origin(self, exception):
res = super()._remove_other_data_origin(exception)
if not exception == "sale_line_id":
self.sale_line_id = False
return res
@api.constrains("sale_line_id", "partner_id")
def _check_sale_partner(self):
for rec in self:
if (
rec.sale_line_id
and rec.sale_line_id.order_id.partner_id != rec.partner_id
and rec.sale_line_id.order_id.partner_id.parent_id != rec.partner_id
):
raise ValidationError(
_(
"RMA customer and originating sales order line customer "
"doesn't match."
)
)
def action_view_sale_order(self):
action = self.env.ref("sale.action_quotations")
result = action.sudo().read()[0]
order_ids = self.mapped("sale_line_ids.order_id").ids
result["domain"] = [("id", "in", order_ids)]
return result
def action_view_origin_sale_order(self):
action = self.env.ref("sale.action_orders")
result = action.sudo().read()[0]
order_ids = self.sale_id.ids
result["domain"] = [("id", "in", order_ids)]
return result
def action_rma_cancel(self):
res = super().action_rma_cancel()
for line in self.filtered("sale_line_ids"):
line.sale_line_ids.mapped("order_id").action_cancel()
return res
def _get_rma_sold_qty(self):
self.ensure_one()
qty = 0.0
for sale_line in self.sale_line_ids.filtered(
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()._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:
layers = moves.sudo().mapped("stock_valuation_layer_ids")
if layers:
price_unit = sum(layers.mapped("value")) / sum(
layers.mapped("quantity")
)
elif self.account_move_line_id:
sale_lines = self.account_move_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:
layers = moves.sudo().mapped("stock_valuation_layer_ids")
if layers:
price_unit = sum(layers.mapped("value")) / sum(
layers.mapped("quantity")
)
return price_unit

View File

@@ -0,0 +1,36 @@
# Copyright 2017 Eficent Business and IT Consulting Services S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)
from odoo import fields, models
class SaleOrder(models.Model):
_inherit = "sale.order"
rma_line_ids = fields.One2many(
comodel_name="rma.order.line", compute="_compute_rma_line"
)
rma_count = fields.Integer(compute="_compute_rma_count", string="# of RMA")
def _compute_rma_count(self):
for so in self:
rmas = self.mapped("rma_line_ids")
so.rma_count = len(rmas)
def _compute_rma_line(self):
for so in self:
so.rma_line_ids = so.mapped("order_line.rma_line_id")
def action_view_rma(self):
action = self.env.ref("rma.action_rma_customer_lines")
result = action.sudo().read()[0]
rma_ids = self.mapped("rma_line_ids").ids
if rma_ids:
# choose the view_mode accordingly
if len(rma_ids) > 1:
result["domain"] = [("id", "in", rma_ids)]
else:
res = self.env.ref("rma.view_rma_line_form", False)
result["views"] = [(res and res.id or False, "form")]
result["res_id"] = rma_ids[0]
return result

View File

@@ -0,0 +1,57 @@
# Copyright 2020 ForgeFlow S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)
from odoo import api, fields, models
from odoo.osv import expression
class SaleOrderLine(models.Model):
_inherit = "sale.order.line"
_rec_names_search = ["name", "order_id"]
@api.model
def _name_search(self, name, domain=None, operator="ilike", limit=None, order=None):
domain = domain or []
if self.env.context.get("rma"):
domain = expression.AND([domain, [("display_type", "=", False)]])
lines = self.search([("order_id.name", operator, name)] + domain, limit=limit)
if limit:
limit_rest = limit - len(lines)
else:
# limit can be 0 or None representing infinite
limit_rest = limit
if limit_rest or not limit:
domain += [("id", "in", lines.ids)]
return super()._name_search(
name, domain=domain, operator=operator, limit=limit_rest, order=order
)
return self._search(domain, limit=limit, order=order)
def _get_sale_line_rma_name_get_label(self):
self.ensure_one()
return "SO:{} | INV: {}, | PART:{} | QTY:{}".format(
self.order_id.name,
" ".join(str(x) for x in [inv.name for inv in self.order_id.invoice_ids]),
self.product_id.name,
self.product_uom_qty,
)
def _compute_display_name(self):
if not self.env.context.get("rma"):
return super()._compute_display_name()
for sale_line in self:
for sale_line in self:
if sale_line.order_id.name:
sale_line.display_name = (
sale_line._get_sale_line_rma_name_get_label()
)
else:
return super(SaleOrderLine, sale_line)._compute_display_name()
rma_line_id = fields.Many2one(
comodel_name="rma.order.line", string="RMA", ondelete="restrict", copy=False
)
def _prepare_order_line_procurement(self, group_id=False):
vals = super()._prepare_order_line_procurement(group_id=group_id)
vals.update({"rma_line_id": self.rma_line_id.id})
return vals

3
rma_sale/pyproject.toml Normal file
View File

@@ -0,0 +1,3 @@
[build-system]
requires = ["whool"]
build-backend = "whool.buildapi"

View File

@@ -0,0 +1,7 @@
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access_rma_order_line_make_sale_order_customer_user_item,rma.order.line.make.sale.order.customer.user,model_rma_order_line_make_sale_order,rma.group_rma_customer_user,1,1,1,1
access_rma_order_line_make_sale_order_supplier_user_item,rma.order.line.make.sale.order.supplier.user,model_rma_order_line_make_sale_order,rma.group_rma_supplier_user,1,1,1,1
access_rma_order_line_make_sale_order_item_customer_user_item,rma.order.line.make.sale.order.item.customer.user,model_rma_order_line_make_sale_order_item,rma.group_rma_customer_user,1,1,1,1
access_rma_order_line_make_sale_order_item_supplier_user_item,rma.order.line.make.sale.order.item.supplier.user,model_rma_order_line_make_sale_order_item,rma.group_rma_supplier_user,1,1,1,1
access_rma_add_sale_customer_user_item,rma.add.sale.customer.user,model_rma_add_sale,rma.group_rma_customer_user,1,1,1,1
access_rma_add_sale_supplier_user_item,rma.add.sale.supplier.user,model_rma_add_sale,rma.group_rma_supplier_user,1,1,1,1
1 id name model_id:id group_id:id perm_read perm_write perm_create perm_unlink
2 access_rma_order_line_make_sale_order_customer_user_item rma.order.line.make.sale.order.customer.user model_rma_order_line_make_sale_order rma.group_rma_customer_user 1 1 1 1
3 access_rma_order_line_make_sale_order_supplier_user_item rma.order.line.make.sale.order.supplier.user model_rma_order_line_make_sale_order rma.group_rma_supplier_user 1 1 1 1
4 access_rma_order_line_make_sale_order_item_customer_user_item rma.order.line.make.sale.order.item.customer.user model_rma_order_line_make_sale_order_item rma.group_rma_customer_user 1 1 1 1
5 access_rma_order_line_make_sale_order_item_supplier_user_item rma.order.line.make.sale.order.item.supplier.user model_rma_order_line_make_sale_order_item rma.group_rma_supplier_user 1 1 1 1
6 access_rma_add_sale_customer_user_item rma.add.sale.customer.user model_rma_add_sale rma.group_rma_customer_user 1 1 1 1
7 access_rma_add_sale_supplier_user_item rma.add.sale.supplier.user model_rma_add_sale rma.group_rma_supplier_user 1 1 1 1

View File

@@ -0,0 +1,2 @@
from . import test_rma_sale
from . import test_rma_stock_account_sale

View File

@@ -0,0 +1,156 @@
# Copyright 2020 ForgeFlow S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)
from odoo.tests import common
class TestRmaSale(common.SingleTransactionCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.rma_obj = cls.env["rma.order"]
cls.rma_line_obj = cls.env["rma.order.line"]
cls.rma_op_obj = cls.env["rma.operation"]
cls.rma_add_sale_wiz = cls.env["rma_add_sale"]
cls.rma_make_sale_wiz = cls.env["rma.order.line.make.sale.order"]
cls.so_obj = cls.env["sale.order"]
cls.sol_obj = cls.env["sale.order.line"]
cls.product_obj = cls.env["product.product"]
cls.partner_obj = cls.env["res.partner"]
cls.rma_route_cust = cls.env.ref("rma.route_rma_customer")
# Create customer
customer1 = cls.partner_obj.create({"name": "Customer 1"})
# Create products
cls.product_1 = cls.product_obj.create(
{"name": "Test Product 1", "type": "product", "list_price": 100.0}
)
cls.product_2 = cls.product_obj.create(
{"name": "Test Product 2", "type": "product", "list_price": 150.0}
)
# Create SO:
cls.so = cls.so_obj.create(
{
"partner_id": customer1.id,
"partner_invoice_id": customer1.id,
"partner_shipping_id": customer1.id,
"order_line": [
(
0,
0,
{
"name": cls.product_1.name,
"product_id": cls.product_1.id,
"product_uom_qty": 20.0,
"product_uom": cls.product_1.uom_id.id,
"price_unit": cls.product_1.list_price,
},
),
(
0,
0,
{
"name": cls.product_2.name,
"product_id": cls.product_2.id,
"product_uom_qty": 18.0,
"product_uom": cls.product_2.uom_id.id,
"price_unit": cls.product_2.list_price,
},
),
],
}
)
cls.so.action_confirm()
for move in cls.so.picking_ids.move_ids:
move.write({"quantity": move.product_uom_qty, "picked": True})
cls.so.picking_ids._action_done()
# Create RMA group and operation:
cls.rma_group = cls.rma_obj.create({"partner_id": customer1.id})
cls.operation_1 = cls.rma_op_obj.create(
{
"code": "TEST",
"name": "Sale afer receive",
"type": "customer",
"receipt_policy": "ordered",
"sale_policy": "received",
"in_route_id": cls.rma_route_cust.id,
"out_route_id": cls.rma_route_cust.id,
}
)
cls.operation_2 = cls.rma_op_obj.create(
{
"code": "TEST",
"name": "Receive and Sale",
"type": "customer",
"receipt_policy": "ordered",
"sale_policy": "ordered",
"in_route_id": cls.rma_route_cust.id,
"out_route_id": cls.rma_route_cust.id,
}
)
def test_01_add_from_sale_order(self):
"""Test wizard to create RMA from Sales Orders."""
add_sale = self.rma_add_sale_wiz.with_context(
**{
"customer": True,
"active_ids": self.rma_group.id,
"active_model": "rma.order",
}
).create(
{"sale_id": self.so.id, "sale_line_ids": [(6, 0, self.so.order_line.ids)]}
)
add_sale.add_lines()
self.assertEqual(len(self.rma_group.rma_line_ids), 2)
def test_02_rma_sale_operation(self):
"""Test RMA quantities using sale operations."""
# Received sale_policy:
rma_1 = self.rma_group.rma_line_ids.filtered(
lambda r: r.product_id == self.product_1
)
rma_1.write({"operation_id": self.operation_1.id})
rma_1._onchange_operation_id()
self.assertEqual(rma_1.sale_policy, "received")
self.assertEqual(rma_1.qty_to_sell, 0.0)
# TODO: receive and check qty_to_sell is 20.0
# Ordered sale_policy:
rma_2 = self.rma_group.rma_line_ids.filtered(
lambda r: r.product_id == self.product_2
)
rma_2.write({"operation_id": self.operation_2.id})
rma_2._onchange_operation_id()
self.assertEqual(rma_2.sale_policy, "ordered")
self.assertEqual(rma_2.qty_to_sell, 18.0)
def test_03_rma_create_sale(self):
"""Generate a Sales Order from a customer RMA."""
rma = self.rma_group.rma_line_ids.filtered(
lambda r: r.product_id == self.product_2
)
self.assertEqual(rma.sales_count, 0)
self.assertEqual(rma.qty_to_sell, 18.0)
self.assertEqual(rma.qty_sold, 0.0)
make_sale = self.rma_make_sale_wiz.with_context(
**{"customer": True, "active_ids": rma.id, "active_model": "rma.order.line"}
).create({"partner_id": rma.partner_id.id})
make_sale.make_sale_order()
self.assertEqual(rma.sales_count, 1)
rma.sale_line_ids.order_id.action_confirm()
self.assertEqual(rma.qty_to_sell, 0.0)
self.assertEqual(rma.qty_sold, 18.0)
def test_04_fill_rma_from_so_line(self):
"""Test filling a RMA (line) from a Sales Order line."""
so_line = self.so.order_line.filtered(lambda r: r.product_id == self.product_1)
rma = self.rma_line_obj.new(
{"partner_id": self.so.partner_id.id, "sale_line_id": so_line.id}
)
self.assertFalse(rma.product_id)
rma._onchange_sale_line_id()
self.assertEqual(rma.product_id, self.product_1)
self.assertEqual(rma.product_qty, 20.0)

View File

@@ -0,0 +1,161 @@
# Copyright 2022 ForgeFlow S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)
from odoo.tests.common import Form
# pylint: disable=odoo-addons-relative-import
from odoo.addons.rma_account.tests.test_rma_stock_account import TestRmaStockAccount
class TestRmaStockAccountSale(TestRmaStockAccount):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.operation_receive_refund = cls.env.ref(
"rma_account.rma_operation_customer_refund"
)
customer1 = cls.env["res.partner"].create({"name": "Customer 1"})
cls.product_fifo_1.standard_price = 1234
cls._create_inventory(cls.product_fifo_1, 20.0, cls.env.ref("rma.location_rma"))
cls.so1 = cls.env["sale.order"].create(
{
"partner_id": customer1.id,
"partner_invoice_id": customer1.id,
"partner_shipping_id": customer1.id,
"order_line": [
(
0,
0,
{
"name": cls.product_fifo_1.name,
"product_id": cls.product_fifo_1.id,
"product_uom_qty": 20.0,
"product_uom": cls.product_fifo_1.uom_id.id,
"price_unit": 800,
},
),
],
}
)
cls.so1.action_confirm()
for ml in cls.so1.picking_ids.move_line_ids:
ml.quantity = ml.quantity_product_uom
ml.picked = True
cls.so1.picking_ids.button_validate()
def test_01_cost_from_so_move(self):
"""
Test the price unit is taken from the cost of the stock move associated to
the SO
"""
so_line = self.so1.order_line.filtered(
lambda r: r.product_id == self.product_fifo_1
)
self.product_fifo_1.standard_price = 5678 # this should not be taken
customer_view = self.env.ref("rma_sale.view_rma_line_form")
rma_line = Form(
self.rma_line.with_context(customer=1).with_user(self.rma_basic_user),
view=customer_view.id,
)
rma_line.partner_id = self.so1.partner_id
rma_line.sale_line_id = so_line
rma_line.price_unit = 4356
rma_line = rma_line.save()
rma_line.action_rma_to_approve()
picking = self._receive_rma(rma_line)
# The price is not the standard price, is the value of the outgoing layer
# of the SO
rma_move_value = picking.move_ids.stock_valuation_layer_ids.value
so_move_value = self.so1.picking_ids.mapped(
"move_ids.stock_valuation_layer_ids"
)[-1].value
self.assertEqual(rma_move_value, -so_move_value)
# Test the accounts used
account_move = picking.move_ids.stock_valuation_layer_ids.account_move_id
self.check_accounts_used(
account_move, debit_account="inventory", credit_account="gdni"
)
def test_02_return_and_refund_ref_so(self):
"""
Sell a product. Create a customer invoice.
Then create an RMA to return it and refund to the customer
"""
customer_view = self.env.ref("rma_sale.view_rma_line_form")
so_line = self.so1.order_line.filtered(
lambda r: r.product_id == self.product_fifo_1
)
rma_line = Form(
self.rma_line.with_context(customer=1).with_user(self.rma_basic_user),
view=customer_view.id,
)
rma_line.partner_id = self.so1.partner_id
rma_line.sale_line_id = so_line
rma_line.operation_id = self.operation_receive_refund
rma_line.price_unit = 4356 # This should never be used
rma_line = rma_line.save()
rma_line.action_rma_to_approve()
self._receive_rma(rma_line)
make_refund = self.rma_refund_wiz.with_context(
**{
"customer": True,
"active_ids": rma_line.ids,
"active_model": "rma.order.line",
}
).create({"description": "Test refund"})
make_refund.item_ids.qty_to_refund = 20
make_refund.invoice_refund()
refund = rma_line.refund_line_ids.move_id
refund.action_post()
self.assertEqual(refund.invoice_line_ids[0].price_unit, so_line.price_unit)
self.assertEqual(refund.invoice_line_ids[0].currency_id, so_line.currency_id)
gdni_amls = self.env["account.move.line"].search(
[
("account_id", "=", self.account_gdni.id),
("rma_line_id", "=", rma_line.id),
]
)
self.assertEqual(sum(gdni_amls.mapped("balance")), 0.0)
self.assertTrue(all(gdni_amls.mapped("reconciled")))
def test_03_return_and_refund_ref_inv(self):
"""
Sell a product. Then create an RMA to return it and refund to the customer
"""
customer_invoice = self.so1._create_invoices()
customer_view = self.env.ref("rma_sale.view_rma_line_form")
so_line = self.so1.order_line.filtered(
lambda r: r.product_id == self.product_fifo_1
)
rma_line = Form(
self.rma_line.with_context(customer=1).with_user(self.rma_basic_user),
view=customer_view.id,
)
rma_line.partner_id = self.so1.partner_id
rma_line.account_move_line_id = customer_invoice.invoice_line_ids[0]
rma_line.operation_id = self.operation_receive_refund
rma_line.price_unit = 4356 # This should never be used
rma_line = rma_line.save()
rma_line.action_rma_to_approve()
self._receive_rma(rma_line)
make_refund = self.rma_refund_wiz.with_context(
**{
"customer": True,
"active_ids": rma_line.ids,
"active_model": "rma.order.line",
}
).create({"description": "Test refund"})
make_refund.item_ids.qty_to_refund = 20
make_refund.invoice_refund()
refund = rma_line.refund_line_ids.move_id
refund.action_post()
self.assertEqual(refund.invoice_line_ids[0].price_unit, so_line.price_unit)
self.assertEqual(refund.invoice_line_ids[0].currency_id, so_line.currency_id)
gdni_amls = self.env["account.move.line"].search(
[
("account_id", "=", self.account_gdni.id),
("rma_line_id", "=", rma_line.id),
]
)
self.assertEqual(sum(gdni_amls.mapped("balance")), 0.0)
self.assertTrue(all(gdni_amls.mapped("reconciled")))

View File

@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<record id="rma_operation_tree" model="ir.ui.view">
<field name="name">rma.operation.tree</field>
<field name="model">rma.operation</field>
<field name="inherit_id" ref="rma.rma_operation_tree" />
<field name="arch" type="xml">
<field name="delivery_policy" position="after">
<field name="sale_policy" />
</field>
</field>
</record>
<record id="rma_operation_form" model="ir.ui.view">
<field name="name">rma.operation.form</field>
<field name="model">rma.operation</field>
<field name="inherit_id" ref="rma.rma_operation_form" />
<field name="arch" type="xml">
<field name="delivery_policy" position="after">
<field name="sale_policy" />
</field>
<field name="company_id" position="after">
<field name="auto_confirm_rma_sale" />
<field name="free_of_charge_rma_sale" />
</field>
</field>
</record>
</odoo>

View File

@@ -0,0 +1,109 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<record id="view_rma_line_form" model="ir.ui.view">
<field name="name">rma.order.line.form</field>
<field name="model">rma.order.line</field>
<field name="inherit_id" ref="rma.view_rma_line_form" />
<field name="arch" type="xml">
<div name='button_box' position="inside">
<button
type="object"
name="action_view_origin_sale_order"
class="oe_stat_button"
icon="fa-strikethrough"
invisible="sale_id == False"
string="Origin Sale Order"
>
</button>
<button
type="object"
name="action_view_sale_order"
class="oe_stat_button"
icon="fa-strikethrough"
invisible="sales_count == 0"
groups="sales_team.group_sale_salesman_all_leads"
>
<field
name="sales_count"
widget="statinfo"
string="Sales Orders"
/>
</button>
</div>
<group name="main_info" position="inside">
<field name="sale_id" invisible="1" />
<field
name="sale_line_id"
context="{'rma': True}"
options="{'no_create': True}"
invisible="type != 'customer'"
readonly="state != 'draft'"
/>
</group>
<group name="quantities" position="inside">
<group invisible="sale_policy == 'no'">
<field name="qty_to_sell" />
<field name="qty_sold" />
</group>
</group>
<field name="delivery_policy" position="after">
<field name="sale_policy" />
</field>
<notebook position="inside">
<page
name="sale"
string="Sale Lines"
invisible="sale_line_ids == []"
>
<field
name="sale_line_ids"
nolabel="1"
readonly="state != 'draft'"
/>
</page>
</notebook>
</field>
</record>
<record id="view_rma_line_button_sale_form" model="ir.ui.view">
<field name="name">rma.order.line.form</field>
<field name="model">rma.order.line</field>
<field name="inherit_id" ref="rma.view_rma_line_button_form" />
<field name="arch" type="xml">
<header position="inside">
<button
name="%(action_rma_order_line_make_sale_order)d"
string="Create Sales Quotation"
class="oe_highlight"
invisible="qty_to_sell == 0 or qty_to_sell &lt; 0 or state != 'approved' or sale_policy == 'no'"
type="action"
/>
<button
name="%(action_rma_order_line_make_sale_order)d"
string="Create Sales Quotation"
invisible="qty_to_sell &gt; 0 or state != 'approved' or sale_policy == 'no'"
type="action"
/>
</header>
</field>
</record>
<record id="view_rma_rma_line_filter" model="ir.ui.view">
<field name="name">rma.order.line.select - rma_sale</field>
<field name="model">rma.order.line</field>
<field name="inherit_id" ref="rma.view_rma_rma_line_filter" />
<field name="arch" type="xml">
<group name="stock_quantities" position="after">
<group name="sale_quantities" groups="sales_team.group_sale_salesman">
<filter
domain="[('state','!=', 'done'),('qty_to_sell','>',0.0)]"
help="To Sell"
name="to_sell"
/>
</group>
</group>
</field>
</record>
</odoo>

View File

@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<record id="view_rma_form" model="ir.ui.view">
<field name="name">rma.order.form</field>
<field name="model">rma.order</field>
<field name="inherit_id" ref="rma.view_rma_form" />
<field name="arch" type="xml">
<div name="button_box" position="inside">
<button
type="object"
name="action_view_sale_order"
class="oe_stat_button"
icon="fa-pencil-square-o"
groups="sales_team.group_sale_salesman"
invisible="type != 'customer'"
>
<field name="sale_count" widget="statinfo" string="Origin SO" />
</button>
</div>
<xpath expr="//field[@name='rma_line_ids']/tree" position="inside">
<field name="sale_policy" invisible="True" />
</xpath>
</field>
</record>
</odoo>

View File

@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<record id="view_order_form" model="ir.ui.view">
<field name="name">sale.order.form</field>
<field name="model">sale.order</field>
<field name="inherit_id" ref="sale.view_order_form" />
<field name="arch" type="xml">
<xpath
expr="//notebook/page/field[@name='order_line']/form/group/group/field[@name='price_unit']"
position="after"
>
<field
name="rma_line_id"
options="{'no_create': True}"
groups="rma.group_rma_customer_user,rma.group_rma_supplier_user"
/>
</xpath>
</field>
</record>
<record id="view_order_form_inherit_sale_rma" model="ir.ui.view">
<field name="name">sale.order.form.sale.rma</field>
<field name="model">sale.order</field>
<field name="inherit_id" ref="sale.view_order_form" />
<field name="arch" type="xml">
<data>
<xpath
expr="//button[@name='action_view_invoice']"
position="before"
>
<button
type="object"
name="action_view_rma"
class="oe_stat_button"
icon="fa-dropbox"
invisible="rma_count == 0"
groups="rma.group_rma_customer_user"
>
<field name="rma_count" widget="statinfo" string="RMA" />
</button>
</xpath>
</data>
</field>
</record>
</odoo>

View File

@@ -0,0 +1,4 @@
from . import rma_order_line_make_sale_order
from . import rma_make_picking
from . import rma_refund
from . import rma_add_sale

View File

@@ -0,0 +1,251 @@
# Copyright 2020 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
class RmaAddSale(models.TransientModel):
_name = "rma_add_sale"
_description = "Wizard to add rma lines from SO lines"
@api.model
def default_get(self, 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"]
if not rma_id:
return res
assert active_model == "rma.order", "Bad context propagation"
rma = rma_obj.browse(rma_id)
res["rma_id"] = rma.id
res["partner_id"] = rma.partner_id.id
res["sale_id"] = False
res["sale_line_ids"] = False
return res
rma_id = fields.Many2one(
comodel_name="rma.order", string="RMA Order", readonly=True
)
partner_id = fields.Many2one(
comodel_name="res.partner", string="Partner", readonly=True
)
sale_id = fields.Many2one(comodel_name="sale.order", string="Order")
sale_line_ids = fields.Many2many(
"sale.order.line",
"rma_add_sale_add_line_rel",
"sale_line_id",
"rma_add_sale_id",
readonly=False,
string="Sale Lines",
)
show_lot_filter = fields.Boolean(
string="Show lot filter?",
compute="_compute_lot_domain",
)
lot_domain_ids = fields.Many2many(
comodel_name="stock.lot",
string="Lots Domain",
compute="_compute_lot_domain",
)
@api.depends(
"sale_line_ids.move_ids.move_line_ids.lot_id",
)
def _compute_lot_domain(self):
for rec in self:
rec.lot_domain_ids = (
rec.mapped("sale_line_ids.move_ids")
.filtered(lambda x: x.state == "done")
.mapped("move_line_ids.lot_id")
.ids
)
rec.show_lot_filter = bool(rec.lot_domain_ids)
lot_ids = fields.Many2many(comodel_name="stock.lot", string="Lots/Serials selected")
def select_all(self):
self.ensure_one()
self.write(
{
"lot_ids": [(6, 0, self.lot_domain_ids.ids)],
}
)
return {
"type": "ir.actions.act_window",
"name": _("Add Sale Order"),
"view_mode": "form",
"res_model": self._name,
"res_id": self.id,
"target": "new",
}
def _prepare_rma_line_from_sale_order_line(
self, line, product, quantity, uom_id=False, lot=None
):
operation = self.rma_id.operation_default_id
if not operation:
operation = line.product_id.rma_customer_operation_id
if not operation:
operation = line.product_id.categ_id.rma_customer_operation_id
if not operation:
operation = self.env["rma.operation"].search(
[("type", "=", self.rma_id.type)], limit=1
)
if not operation:
raise ValidationError(_("Please define an operation first"))
if not operation.in_route_id or not operation.out_route_id:
route = self.env["stock.location.route"].search(
[("rma_selectable", "=", True)], limit=1
)
if not route:
raise ValidationError(_("Please define an rma route"))
warehouse = self.rma_id.in_warehouse_id
if not warehouse:
if not operation.in_warehouse_id or not operation.out_warehouse_id:
warehouse = self.env["stock.warehouse"].search(
[
("company_id", "=", self.rma_id.company_id.id),
("lot_rma_id", "!=", False),
],
limit=1,
)
if not warehouse:
raise ValidationError(
_("Please define a warehouse with a " "default rma location.")
)
location = self.rma_id.location_id
if not location:
location = (
operation.location_id
or operation.in_warehouse_id.lot_rma_id
or warehouse.lot_rma_id
)
data = {
"partner_id": self.partner_id.id,
"description": self.rma_id.description,
"sale_line_id": line.id,
"product_id": product.id,
"lot_id": lot and lot.id or False,
"origin": line.order_id.name,
"uom_id": uom_id or product.uom_id.id,
"operation_id": operation.id,
"product_qty": quantity,
"delivery_address_id": self.sale_id.partner_shipping_id.id,
"invoice_address_id": self.sale_id.partner_invoice_id.id,
"price_unit": line.product_id == product
and line.currency_id._convert(
line.price_unit,
line.currency_id,
line.company_id,
line.order_id.date_order,
)
or product.lst_price,
"rma_id": self.rma_id.id,
"in_route_id": operation.in_route_id.id or route.id,
"out_route_id": operation.out_route_id.id or route.id,
"receipt_policy": operation.receipt_policy,
"location_id": location.id,
"refund_policy": operation.refund_policy,
"delivery_policy": operation.delivery_policy,
"in_warehouse_id": warehouse.id or operation.in_warehouse_id.id,
"out_warehouse_id": warehouse.id or operation.out_warehouse_id.id,
}
return data
@api.model
def _get_rma_data(self):
data = {"date_rma": fields.Datetime.now()}
return data
@api.model
def _get_existing_sale_lines(self):
existing_sale_lines = []
for rma_line in self.rma_id.rma_line_ids:
existing_sale_lines.append(rma_line.sale_line_id)
return existing_sale_lines
def _should_create_rma_line(self, line, existing_sale_line, lot=False):
if not lot and line in existing_sale_line:
return False
if lot and (
lot.id not in self.lot_ids.ids
or lot.id in self.rma_id.rma_line_ids.mapped("lot_id").ids
):
return False
return True
def _create_from_move_line(self, line):
return True
def _get_lot_quantity_from_move_lines(self, sale_line):
outgoing_lines = self.env["stock.move.line"]
incoming_lines = self.env["stock.move.line"]
sent_moves = sale_line.move_ids.filtered(
lambda m: m.state == "done" and not m.scrapped
)
for move in sent_moves:
if move.location_dest_id.usage == "customer" and (
not move.origin_returned_move_id
or (move.origin_returned_move_id and move.to_refund)
):
outgoing_lines |= move.move_line_ids
elif move.location_dest_id.usage != "customer" and move.to_refund:
incoming_lines |= move.move_line_ids
sent_product_data = {}
for line in outgoing_lines:
key = (line.product_id, line.product_uom_id, line.lot_id)
if key not in sent_product_data:
sent_product_data[key] = 0.0
sent_product_data[key] += line.quantity
for line in incoming_lines:
key = (line.product_id, line.product_uom_id, line.lot_id)
if key not in sent_product_data:
sent_product_data[key] = 0.0
sent_product_data[key] -= line.quantity
return sent_product_data
def add_lines(self):
rma_line_obj = self.env["rma.order.line"]
existing_sale_line = self._get_existing_sale_lines()
for line in self.sale_line_ids:
if self._create_from_move_line(line):
sent_produt_data = self._get_lot_quantity_from_move_lines(line)
for (product, uom, lot), qty in sent_produt_data.items():
if not self._should_create_rma_line(
line, existing_sale_line, lot=lot
):
continue
data = self._prepare_rma_line_from_sale_order_line(
line, product, qty, uom_id=uom.id, lot=lot
)
rec = rma_line_obj.create(data)
# Ensure that configuration on the operation is applied
# TODO MIG: in v16 the usage of such onchange can be removed in
# favor of (pre)computed stored editable fields for all policies
# and configuration in the RMA operation.
rec._onchange_operation_id()
else:
if not self._should_create_rma_line(line, existing_sale_line):
continue
# we can't have lot management based on sale order line
data = self._prepare_rma_line_from_sale_order_line(
line,
line.product_id,
line.product_uom_qty,
uom_id=line.product_uom.id,
lot=False,
)
rec = rma_line_obj.create(data)
# Ensure that configuration on the operation is applied
# TODO MIG: in v16 the usage of such onchange can be removed in
# favor of (pre)computed stored editable fields for all policies
# and configuration in the RMA operation.
rec._onchange_operation_id()
rec.price_unit = rec._get_price_unit()
rma = self.rma_id
data_rma = self._get_rma_data()
rma.write(data_rma)
return {"type": "ir.actions.act_window_close"}

View File

@@ -0,0 +1,120 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<record id="view_rma_add_sale" model="ir.ui.view">
<field name="name">rma.add.sale</field>
<field name="model">rma_add_sale</field>
<field name="arch" type="xml">
<form string="Select Sale Order from customer">
<separator string="Select Sale Order from customer" />
<group>
<field name="partner_id" />
</group>
<separator string="Select Sale Order Lines to Add" />
<group>
<field
name="sale_id"
domain="[
('partner_id','=',partner_id), (('state','not in',['draft','cancel']))]"
context="{'partner_id': partner_id}"
/>
</group>
<field
name="sale_line_ids"
domain="[('order_id', '=', sale_id)]"
string="Sale Order Lines"
>
<tree>
<field name="product_id" invisible="1" />
<field name="order_id" />
<field name="order_partner_id" />
<field name="name" />
<field name="salesman_id" />
<field name="product_uom_qty" string="Qty" />
<field name="qty_delivered" />
<field name="qty_invoiced" />
<field name="qty_to_invoice" />
<field
name="product_uom"
string="Unit of Measure"
groups="uom.group_uom"
/>
<field name="price_subtotal" sum="Total" widget="monetary" />
</tree>
</field>
<field name="show_lot_filter" invisible="1" />
<field name="lot_domain_ids" widget="many2many_tags" invisible="1" />
<div class="oe_grey" invisible="show_lot_filter == False">
The creation of the RMA Lines will be separated according to the lots or serials selected
</div>
<div class="o_row">
<label
for="lot_ids"
invisible="show_lot_filter == False"
string="Selected Lot/Serial Numbers"
/>
<field
name="lot_ids"
widget="many2many_tags"
domain="[('id', 'in', lot_domain_ids)]"
invisible="show_lot_filter == False"
options="{'no_create': True}"
/>
<button
name="select_all"
type="object"
string="Select all"
class="oe_inline"
invisible="show_lot_filter == False"
/>
</div>
<footer>
<button
string="Confirm"
name="add_lines"
type="object"
class="oe_highlight"
/>
or
<button
name="action_cancel"
string="Cancel"
class="oe_link"
special="cancel"
/>
</footer>
</form>
</field>
</record>
<record id="action_rma_add_sale" model="ir.actions.act_window">
<field name="name">Add Sale Order</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">rma_add_sale</field>
<field name="view_mode">form</field>
<field name="target">new</field>
<field name="view_id" ref="view_rma_add_sale" />
<field
name="groups_id"
eval="[(4, ref('rma.group_rma_customer_user')), (4, ref('rma.group_rma_customer_user'))]"
/>
</record>
<record id="view_rma_add_sale_form" model="ir.ui.view">
<field name="name">rma.order.form - sale wizard</field>
<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_add_sale)d"
string="Add From Sale Order"
type="action"
invisible="type != 'customer'"
/>
</xpath>
</field>
</record>
</odoo>

View File

@@ -0,0 +1,19 @@
# Copyright 2020 ForgeFlow S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)
from odoo import api, fields, models
class RmaMakePicking(models.TransientModel):
_inherit = "rma_make_picking.wizard"
@api.returns("rma.order.line")
def _prepare_item(self, line):
res = super()._prepare_item(line)
res["sale_line_id"] = line.sale_line_id.id
return res
class RmaMakePickingItem(models.TransientModel):
_inherit = "rma_make_picking.wizard.item"
sale_line_id = fields.Many2one(comodel_name="sale.order.line", string="Sale Line")

View File

@@ -0,0 +1,179 @@
# Copyright 2020 ForgeFlow S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)
from odoo import _, api, exceptions, fields, models
class RmaLineMakeSaleOrder(models.TransientModel):
_name = "rma.order.line.make.sale.order"
_description = "Make Sales Order from RMA Line"
partner_id = fields.Many2one(
comodel_name="res.partner",
string="Customer",
required=False,
)
item_ids = fields.One2many(
comodel_name="rma.order.line.make.sale.order.item",
inverse_name="wiz_id",
string="Items",
readonly=False,
)
sale_order_id = fields.Many2one(
comodel_name="sale.order",
string="Sales Order",
required=False,
domain=[("state", "=", "draft")],
)
@api.model
def _prepare_item(self, line):
free_of_charge_rma_sale = line.operation_id.free_of_charge_rma_sale
return {
"line_id": line.id,
"product_id": line.product_id.id,
"name": line.product_id.name,
"product_qty": line.qty_to_sell,
"rma_id": line.rma_id.id,
"out_warehouse_id": line.out_warehouse_id.id,
"product_uom_id": line.uom_id.id,
"free_of_charge": free_of_charge_rma_sale,
}
@api.model
def default_get(self, 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:
return res
assert active_model == "rma.order.line", "Bad context propagation"
items = []
lines = rma_line_obj.browse(rma_line_ids)
for line in lines:
items.append([0, 0, self._prepare_item(line)])
customers = lines.mapped("partner_id")
if len(customers) == 1:
res["partner_id"] = customers.id
else:
raise exceptions.Warning(
_(
"Only RMA lines from the same partner can be processed at "
"the same time"
)
)
res["item_ids"] = items
return res
@api.model
def _prepare_sale_order(self, line):
if not self.partner_id:
raise exceptions.Warning(_("Enter a customer."))
customer = self.partner_id
auto = self.env["account.fiscal.position"].search(
[("auto_apply", "=", True), ("country_id", "=", customer.country_id.id)],
limit=1,
)
fiscal_position = False
if customer.property_account_position_id:
fiscal_position = customer.property_account_position_id
elif auto:
fiscal_position = auto
data = {
"origin": line.name,
"partner_id": customer.id,
"warehouse_id": line.out_warehouse_id.id,
"company_id": line.company_id.id,
"fiscal_position_id": fiscal_position.id if fiscal_position else False,
}
return data
@api.model
def _prepare_sale_order_line(self, so, item):
product = item.product_id
vals = {
"name": item.name,
"order_id": so.id,
"product_id": product.id,
"product_uom": product.uom_po_id.id,
"product_uom_qty": item.product_qty,
"rma_line_id": item.line_id.id,
}
if item.free_of_charge:
vals["price_unit"] = 0.0
return vals
def _post_process_sale_order(self, item, sale_line):
line = item.line_id
if line.operation_id.auto_confirm_rma_sale:
sale_line.order_id.action_confirm()
def make_sale_order(self):
res = []
sale_obj = self.env["sale.order"]
so_line_obj = self.env["sale.order.line"]
sale = False
for item in self.item_ids:
line = item.line_id
if item.product_qty <= 0.0:
raise exceptions.Warning(_("Enter a positive quantity."))
if self.sale_order_id:
sale = self.sale_order_id
if not sale:
po_data = self._prepare_sale_order(line)
sale = sale_obj.create(po_data)
sale.name = sale.name + " - " + line.name
so_line_data = self._prepare_sale_order_line(sale, item)
sale_line = so_line_obj.create(so_line_data)
self._post_process_sale_order(item, sale_line)
res.append(sale.id)
action = self.env.ref("sale.action_orders")
result = action.sudo().read()[0]
result["domain"] = "[('id','in', [" + ",".join(map(str, res)) + "])]"
return result
class RmaLineMakeSaleOrderItem(models.TransientModel):
_name = "rma.order.line.make.sale.order.item"
_description = "RMA Line Make Sale Order Item"
wiz_id = fields.Many2one(
comodel_name="rma.order.line.make.sale.order", string="Wizard"
)
line_id = fields.Many2one(
comodel_name="rma.order.line", string="RMA Line", compute="_compute_line_id"
)
rma_id = fields.Many2one(
comodel_name="rma.order", related="line_id.rma_id", readonly=False
)
product_id = fields.Many2one(comodel_name="product.product", string="Product")
name = fields.Char(string="Description")
product_qty = fields.Float(string="Quantity to sell", digits="Product UoS")
product_uom_id = fields.Many2one(comodel_name="uom.uom", string="UoM")
out_warehouse_id = fields.Many2one(
comodel_name="stock.warehouse", string="Outbound Warehouse"
)
free_of_charge = fields.Boolean(string="Free of Charge")
def _compute_line_id(self):
rma_line_obj = self.env["rma.order.line"]
for rec in self:
if not self.env.context["active_ids"]:
return
rma_line_ids = self.env.context["active_ids"] or []
lines = rma_line_obj.browse(rma_line_ids)
rec.line_id = lines[0]
@api.onchange("product_id")
def onchange_product_id(self):
if self.product_id:
self.name = self.product_id.name

View File

@@ -0,0 +1,66 @@
<?xml version="1.0" encoding="utf-8" ?>
<!-- Copyright 2016 Eficent Business and IT Consulting Services S.L.
License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl-3.0) -->
<odoo>
<record id="view_rma_order_line_make_sale_order" model="ir.ui.view">
<field name="name">RMA Line Make Sale Order</field>
<field name="model">rma.order.line.make.sale.order</field>
<field name="type">form</field>
<field name="arch" type="xml">
<form string="Create Quotation">
<separator string="Existing Quotation to update:" />
<newline />
<group>
<field
name="sale_order_id"
domain="[('partner_id','=', partner_id), ('state', '=', 'draft')]"
context="{'partner_id': partner_id}"
/>
</group>
<newline />
<separator string="New Sales Order details:" />
<newline />
<group>
<field name="partner_id" />
</group>
<newline />
<group>
<field name="item_ids" nolabel="1" colspan="2">
<tree name="Details" editable="bottom">
<field name="wiz_id" invisible="True" />
<field name="line_id" invisible="True" />
<field name="product_id" />
<field name="name" />
<field name="product_qty" />
<field name="product_uom_id" groups="uom.group_uom" />
<field name="free_of_charge" />
</tree>
</field>
</group>
<footer colspan="2">
<button
name="make_sale_order"
string="Create Sales Quotation"
type="object"
class="oe_highlight"
/>
<button special="cancel" string="Cancel" class="oe_link" />
</footer>
</form>
</field>
</record>
<record
id="action_rma_order_line_make_sale_order"
model="ir.actions.act_window"
>
<field name="name">Create Sales Quotation</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">rma.order.line.make.sale.order</field>
<field name="view_mode">form</field>
<field name="view_id" ref="view_rma_order_line_make_sale_order" />
<field name="target">new</field>
</record>
</odoo>

View File

@@ -0,0 +1,46 @@
# Copyright 2020 ForgeFlow S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)
from odoo import api, fields, models
class RmaRefund(models.TransientModel):
_inherit = "rma.refund"
@api.returns("rma.order.line")
def _prepare_item(self, line):
res = super()._prepare_item(line)
res["sale_line_id"] = line.sale_line_id.id
return res
def _get_refund_price_unit(self, rma):
price_unit = super()._get_refund_price_unit(rma)
if rma.operation_id.refund_free_of_charge:
return price_unit
if rma.type == "customer":
if rma.account_move_line_id:
price_unit = rma.account_move_line_id.price_unit
elif rma.sale_line_id:
price_unit = rma.sale_line_id.price_unit
else:
# Fall back to the sale price if no reference is found.
price_unit = rma.product_id.with_company(rma.company_id).lst_price
return price_unit
def _get_refund_currency(self, rma):
currency = rma.currency_id
if rma.type == "customer":
if rma.account_move_line_id:
currency = rma.account_move_line_id.currency_id
elif rma.sale_line_id:
currency = rma.sale_line_id.currency_id
else:
currency = rma.company_id.currency_id
return currency
class RmaRefundItem(models.TransientModel):
_inherit = "rma.refund.item"
sale_line_id = fields.Many2one(
comodel_name="sale.order.line", string="Sale Order Line"
)