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

[MIG] rma_analytic to v16 #475

Open
wants to merge 18 commits into
base: 16.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
36 changes: 36 additions & 0 deletions rma_analytic/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
.. image:: https://img.shields.io/badge/license-LGPL--3-blue.png
:target: https://www.gnu.org/licenses/lgpl
:alt: License: LGPL-3

==========================
RMA with Analytic Accounts
==========================

This module introduces the following features:

* Adds the analytic account to the RMA order lines.

* Propagates the analytic account to the procurements created

* Introduce rules to ensure consistency


Usage
=====

* Add the analytic information in the rma line or let the system fill it
from origin


Contributors
------------

* Aaron Henriquez <[email protected]>
* Juany Davila <[email protected]>
* Serpent Consulting Services Pvt. Ltd. <[email protected]>


Maintainer
----------

This module is maintained by ForgeFlow.
2 changes: 2 additions & 0 deletions rma_analytic/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import models
from . import wizards
18 changes: 18 additions & 0 deletions rma_analytic/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright 2023 ForgeFlow S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).

{
"name": "Analytic Account in RMA",
"version": "16.0.1.0.0",
"author": "ForgeFlow," "Odoo Community Association (OCA)",
"license": "AGPL-3",
"website": "https://github.com/ForgeFlow/stock-rma",
"category": "Analytic",
"depends": [
"rma_account",
"stock_analytic",
"procurement_mto_analytic",
],
"data": ["views/rma_order_line_view.xml"],
"installable": True,
}
2 changes: 2 additions & 0 deletions rma_analytic/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import rma_order_line
from . import stock_rule
20 changes: 20 additions & 0 deletions rma_analytic/models/rma_order_line.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright 2023 ForgeFlow S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).

from odoo import fields, models


class RmaOrderLine(models.Model):

_inherit = "rma.order.line"

analytic_account_id = fields.Many2one(
comodel_name="account.analytic.account",
string="Analytic Account",
)

def _prepare_rma_line_from_inv_line(self, line):
res = super(RmaOrderLine, self)._prepare_rma_line_from_inv_line(line)
if line.analytic_account_id:
res.update(analytic_account_id=line.analytic_account_id.id)
return res
33 changes: 33 additions & 0 deletions rma_analytic/models/stock_rule.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Copyright (C) 2017-22 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 "analytic_account_id" in values:
res["analytic_account_id"] = values.get("analytic_account_id")
return res
1 change: 1 addition & 0 deletions rma_analytic/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import test_rma_analytic
165 changes: 165 additions & 0 deletions rma_analytic/tests/test_rma_analytic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
# Copyright 2017-23 ForgeFlow S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)

from odoo import fields
from odoo.tests import Form

from odoo.addons.rma.tests import test_rma


class TestRmaAnalytic(test_rma.TestRma):
@classmethod
def setUpClass(cls):
super(TestRmaAnalytic, cls).setUpClass()
cls.rma_add_invoice_wiz = cls.env["rma_add_account_move"]
cls.rma_refund_wiz = cls.env["rma.refund"]
cls.rma_obj = cls.env["rma.order"]
cls.rma_op_obj = cls.env["rma.operation"]
cls.rma_route_cust = cls.env.ref("rma.route_rma_customer")
cls.cust_refund_op = cls.env.ref("rma_account.rma_operation_customer_refund")
cls.rma_group_customer = cls.rma_obj.create(
{"partner_id": cls.partner_id.id, "type": "customer"}
)
cls.operation_1 = cls.rma_op_obj.create(
{
"code": "TEST",
"name": "Refund and receive",
"type": "customer",
"receipt_policy": "ordered",
"refund_policy": "ordered",
"in_route_id": cls.rma_route_cust.id,
"out_route_id": cls.rma_route_cust.id,
}
)
cls.product_1.update(
{
"rma_customer_operation_id": cls.operation_1.id,
"rma_supplier_operation_id": cls.cust_refund_op.id,
}
)
products2move = [
(cls.product_1, 3),
(cls.product_2, 5),
(cls.product_3, 2),
]
cls.rma_ana_id = cls._create_rma_from_move(
products2move,
"supplier",
cls.env.ref("base.res_partner_2"),
dropship=False,
)
cls.company_id = cls.env.user.company_id
cls.anal = cls.env["account.analytic.account"].create({"name": "Name"})
cls.inv_customer = cls.env["account.move"].create(
{
"partner_id": cls.partner_id.id,
"move_type": "out_invoice",
"invoice_date": fields.Date.from_string("2023-01-01"),
"currency_id": cls.company_id.currency_id,
"invoice_line_ids": [
(
0,
None,
{
"name": cls.partner_id.name,
"product_id": cls.product_1.id,
"product_uom_id": cls.product_1.uom_id.id,
"quantity": 12.0,
"price_unit": 100.0,
"analytic_account_id": cls.anal.id,
},
),
],
}
)

@classmethod
def _prepare_move(cls, product, qty, src, dest, picking_in):
res = super(TestRmaAnalytic, cls)._prepare_move(
product, qty, src, dest, picking_in
)
analytic_1 = cls.env["account.analytic.account"].create(
{"name": "Test account #1"}
)
res.update({"analytic_account_id": analytic_1.id})
return res

def test_analytic(self):
for line in self.rma_ana_id.rma_line_ids:
for move in line.move_ids:
self.assertEqual(
line.analytic_account_id,
move.analytic_account_id,
"the analytic account is not propagated",
)

def test_invoice_analytic(self):
"""Test wizard to create RMA from a customer invoice."""
rma_line_form = Form(self.env["rma.order.line"].with_context(customer=True))
rma_line_form.partner_id = self.partner_id
rma_line_form.product_id = self.product_1
rma_line_form.operation_id = self.env.ref("rma.rma_operation_customer_replace")
rma_line_form.in_route_id = self.env.ref("rma.route_rma_customer")
rma_line_form.out_route_id = self.env.ref("rma.route_rma_customer")
rma_line_form.in_warehouse_id = self.env.ref("stock.warehouse0")
rma_line_form.out_warehouse_id = self.env.ref("stock.warehouse0")
rma_line_form.location_id = self.env.ref("stock.stock_location_stock")
rma_line_form.account_move_line_id = self.inv_customer.invoice_line_ids[0]
rma_line_form.uom_id = self.product_1.uom_id
rma_line = rma_line_form.save()
self.assertEqual(
rma_line.analytic_account_id,
self.inv_customer.invoice_line_ids[0].analytic_account_id,
)

def test_invoice_analytic02(self):
self.product_1.rma_customer_operation_id = self.env.ref(
"rma.rma_operation_customer_replace"
).id
rma_order = (
self.env["rma.order"]
.with_context(customer=True)
.create(
{
"name": "RMA",
"partner_id": self.partner_id.id,
"type": "customer",
"rma_line_ids": [],
}
)
)
add_inv = self.rma_add_invoice_wiz.with_context(
customer=True,
active_ids=[rma_order.id],
active_model="rma.order",
).create({"line_ids": [(6, 0, self.inv_customer.invoice_line_ids.ids)]})
add_inv.add_lines()

self.assertEqual(
rma_order.mapped("rma_line_ids.analytic_account_id"),
self.inv_customer.invoice_line_ids[0].analytic_account_id,
)

def test_refund_analytic(self):
add_inv = self.rma_add_invoice_wiz.with_context(
customer=True,
active_ids=[self.rma_group_customer.id],
active_model="rma.order",
).create({"line_ids": [(6, 0, self.inv_customer.invoice_line_ids.ids)]})
add_inv.add_lines()
rma_line = self.rma_group_customer.rma_line_ids.filtered(
lambda r: r.product_id == self.product_1
)
rma_line._onchange_account_move_line_id()
rma_line.action_rma_to_approve()
rma_line.action_rma_approve()
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.invoice_refund()
self.assertEqual(
rma_line.mapped("analytic_account_id"),
rma_line.mapped("refund_line_ids.analytic_account_id"),
)
60 changes: 60 additions & 0 deletions rma_analytic/views/rma_order_line_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?xml version="1.0" ?>
<!-- Copyright 2023 ForgeFlow S.L.
License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl-3.0) -->
<odoo>
<record id="view_rma_line_tree" model="ir.ui.view">
<field name="name">rma.order.line.tree</field>
<field name="model">rma.order.line</field>
<field name="inherit_id" ref="rma.view_rma_line_tree" />
<field name="arch" type="xml">
<field name="state" position="after">
<field
name="analytic_account_id"
groups="analytic.group_analytic_accounting"
/>
</field>
</field>
</record>

<record id="view_rma_line_supplier_tree" model="ir.ui.view">
<field name="name">rma.order.line.supplier.tree</field>
<field name="model">rma.order.line</field>
<field name="inherit_id" ref="rma.view_rma_line_supplier_tree" />
<field name="arch" type="xml">
<field name="state" position="after">
<field
name="analytic_account_id"
groups="analytic.group_analytic_accounting"
/>
</field>
</field>
</record>

<record id="view_rma_line_supplier_form" model="ir.ui.view">
<field name="name">rma.order.line.supplier.form</field>
<field name="model">rma.order.line</field>
<field name="inherit_id" ref="rma.view_rma_line_supplier_form" />
<field name="arch" type="xml">
<group name="main_info" position="inside">
<field
name="analytic_account_id"
groups="analytic.group_analytic_accounting"
/>
</group>
</field>
</record>

<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">
<group name="main_info" position="inside">
<field
name="analytic_account_id"
groups="analytic.group_analytic_accounting"
/>
</group>
</field>
</record>
</odoo>
7 changes: 7 additions & 0 deletions rma_analytic/wizards/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Copyright 2023 ForgeFlow S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)

from . import rma_add_stock_move
from . import rma_make_picking
from . import rma_add_account_move
from . import rma_refund
14 changes: 14 additions & 0 deletions rma_analytic/wizards/rma_add_account_move.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Copyright 2023 ForgeFlow S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)

from odoo import models


class RmaAddAccountMove(models.TransientModel):
_inherit = "rma_add_account_move"

def _prepare_rma_line_from_inv_line(self, line):
res = super(RmaAddAccountMove, self)._prepare_rma_line_from_inv_line(line)
if line.analytic_account_id:
res.update(analytic_account_id=line.analytic_account_id.id)
return res
14 changes: 14 additions & 0 deletions rma_analytic/wizards/rma_add_invoice.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Copyright 2023 ForgeFlow S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)

from odoo import models


class RmaAddInvoice(models.TransientModel):
_inherit = "rma_add_invoice"

def _prepare_rma_line_from_inv_line(self, line):
res = super(RmaAddInvoice, self)._prepare_rma_line_from_inv_line(line)
if line.analytic_account_id:
res.update(analytic_account_id=line.analytic_account_id.id)
return res
13 changes: 13 additions & 0 deletions rma_analytic/wizards/rma_add_stock_move.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright 2023 ForgeFlow S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)

from odoo import models


class RmaAddStockMove(models.TransientModel):
_inherit = "rma_add_stock_move"

def _prepare_rma_line_from_stock_move(self, sm, lot=False):
data = super(RmaAddStockMove, self)._prepare_rma_line_from_stock_move(sm, lot)
data.update(analytic_account_id=sm.analytic_account_id.id)
return data
Loading
Loading