[FIX]rma_analytic. Propagate analytic to other models

This commit is contained in:
ahenriquez
2019-10-28 18:11:57 +01:00
committed by Aaron ForgeFlow
parent 7b119f8872
commit 8e42137a85
6 changed files with 262 additions and 58 deletions

View File

@@ -15,4 +15,5 @@
"views/rma_order_line_view.xml"
],
'installable': True,
}

View File

@@ -2,7 +2,7 @@
# © 2018 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
from odoo import api, fields, models
class RmaOrderLine(models.Model):
@@ -13,3 +13,14 @@ class RmaOrderLine(models.Model):
comodel_name='account.analytic.account',
string='Analytic Account',
)
@api.multi
def _prepare_rma_line_from_inv_line(self, line):
res = super(
RmaOrderLine, self
)._prepare_rma_line_from_inv_line(line)
if line.account_analytic_id:
res.update(
analytic_account_id=line.account_analytic_id.id
)
return res

View File

@@ -2,55 +2,70 @@
# © 2017 Eficent Business and IT Consulting Services S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)
from odoo.tests import common
from odoo.addons.rma.tests import test_rma
class TestRmaAnalytic(common.SavepointCase):
class TestRmaAnalytic(test_rma.TestRma):
@classmethod
def setUpClass(cls):
super(TestRmaAnalytic, cls).setUpClass()
cls.stock_picking_model = cls.env['stock.picking']
cls.rma_line_model = cls.env['rma.order.line']
cls.rma_model = cls.env['rma.order']
cls.rma_add_stock_move = cls.env['rma_add_stock_move']
cls.stock_location = cls.env.ref('stock.stock_location_stock')
cls.product_1 = cls.env.ref('product.product_product_25')
cls.customer_location = cls.env.ref(
'stock.stock_location_customers')
cls.product_uom_id = cls.env.ref('product.product_uom_unit')
products2move = [(cls.product_1, 3), ]
cls.analytic_1 = cls.env['account.analytic.account'].create({
'name': 'Test account #1',
})
cls.partner_id = cls.env.ref('base.res_partner_1')
cls.rma_ana_id = cls._create_rma_analytic(
products2move, cls.partner_id)
cls.rma_add_invoice_wiz = cls.env["rma_add_invoice"]
cls.rma_refund_wiz = cls.env["rma.refund"]
products2move = [
(cls.product_1, 3),
(cls.product_2, 5),
(cls.product_3, 2),
]
cls.rma_add_invoice_wiz = cls.env["rma_add_invoice"]
cls.rma_ana_id = cls._create_rma_from_move(
products2move,
"supplier",
cls.env.ref("base.res_partner_2"),
dropship=False,
)
receivable_type = cls.env.ref(
"account.data_account_type_receivable"
)
# Create Invoices:
customer_account = (
cls.env["account.account"]
.search(
[("user_type_id", "=", receivable_type.id)], limit=1
)
.id
)
cls.inv_customer = cls.env["account.invoice"].create(
{
"partner_id": cls.partner_id.id,
"account_id": customer_account,
"type": "out_invoice",
}
)
cls.anal = cls.env["account.analytic.account"].create(
{"name": "Name"}
)
cls.inv_line_1 = cls.env["account.invoice.line"].create(
{
"name": cls.partner_id.name,
"product_id": cls.product_1.id,
"quantity": 12.0,
"price_unit": 100.0,
"account_analytic_id": cls.anal.id,
"invoice_id": cls.inv_customer.id,
"uom_id": cls.product_1.uom_id.id,
"account_id": customer_account,
}
)
@classmethod
def _create_picking(cls, partner):
return cls.stock_picking_model.create({
'partner_id': partner.id,
'picking_type_id': cls.env.ref('stock.picking_type_in').id,
'location_id': cls.stock_location.id,
'location_dest_id': cls.customer_location.id
})
@classmethod
def _prepare_anal_move(cls, product, qty, src, dest, picking_in, analytic):
res = {
'partner_id': cls.partner_id.id,
'product_id': product.id,
'name': product.partner_ref,
'state': 'confirmed',
'product_uom': cls.product_uom_id.id or product.uom_id.id,
'product_uom_qty': qty,
'origin': 'Test RMA',
'location_id': src.id,
'location_dest_id': dest.id,
'picking_id': picking_in.id,
'analytic_account_id': analytic.id,
}
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
@classmethod
@@ -59,28 +74,38 @@ class TestRmaAnalytic(common.SavepointCase):
moves = []
for item in products2move:
move_values = cls._prepare_anal_move(
item[0], item[1], cls.stock_location,
cls.customer_location, picking_in, cls.analytic_1)
moves.append(cls.env['stock.move'].create(move_values))
item[0],
item[1],
cls.stock_location,
cls.customer_location,
picking_in,
cls.analytic_1,
)
moves.append(cls.env["stock.move"].create(move_values))
rma_id = cls.rma_model.create(
{
'reference': '0001',
'type': 'customer',
'partner_id': partner.id,
'company_id': cls.env.ref('base.main_company').id
})
"reference": "0001",
"type": "customer",
"partner_id": partner.id,
"company_id": cls.env.ref("base.main_company").id,
}
)
for move in moves:
wizard = cls.rma_add_stock_move.with_context(
{'stock_move_id': move.id, 'customer': True,
'active_ids': rma_id.id,
'active_model': 'rma.order',
}
{
"stock_move_id": move.id,
"customer": True,
"active_ids": rma_id.id,
"active_model": "rma.order",
}
).create({})
data = wizard._prepare_rma_line_from_stock_move(move)
wizard.add_lines()
for operation in move.product_id.rma_customer_operation_id:
for (
operation
) in move.product_id.rma_customer_operation_id:
operation.in_route_id = False
move.product_id.categ_id.rma_customer_operation_id = False
move.product_id.rma_customer_operation_id = False
@@ -92,5 +117,133 @@ class TestRmaAnalytic(common.SavepointCase):
for line in cls.rma_ana_id.rma_line_ids:
for move in line.move_ids:
cls.assertEqual(
line.analytic_account_id, move.analytic_account_id,
"the analytic account is not propagated")
line.analytic_account_id,
move.analytic_account_id,
"the analytic account is not propagated",
)
def test_invoice_analytic(cls):
"""Test wizard to create RMA from a customer invoice."""
rma_line = (
cls.env["rma.order.line"]
.with_context(customer=True)
.new(
{
"partner_id": cls.partner_id.id,
"product_id": cls.product_1.id,
"operation_id": cls.env.ref(
"rma.rma_operation_customer_replace"
).id,
"in_route_id": cls.env.ref(
"rma.route_rma_customer"
),
"out_route_id": cls.env.ref(
"rma.route_rma_customer"
),
"in_warehouse_id": cls.env.ref(
"stock.warehouse0"
),
"out_warehouse_id": cls.env.ref(
"stock.warehouse0"
),
"location_id": cls.env.ref(
"stock.stock_location_stock"
),
"type": "customer",
"invoice_line_id": cls.inv_line_1.id,
"uom_id": cls.product_1.uom_id.id,
}
)
)
rma_line._onchange_invoice_line_id()
cls.assertEqual(
rma_line.analytic_account_id,
cls.inv_line_1.account_analytic_id,
)
def test_invoice_analytic02(cls):
cls.product_1.rma_customer_operation_id = cls.env.ref(
"rma.rma_operation_customer_replace"
).id
rma_order = (
cls.env["rma.order"]
.with_context(customer=True)
.create(
{
"name": "RMA",
"partner_id": cls.partner_id.id,
"type": "customer",
"rma_line_ids": [],
}
)
)
add_inv = cls.rma_add_invoice_wiz.with_context(
{
"customer": True,
"active_ids": [rma_order.id],
"active_model": "rma.order",
}
).create(
{
"invoice_line_ids": [
(6, 0, cls.inv_customer.invoice_line_ids.ids)
]
}
)
add_inv.add_lines()
cls.assertEqual(
rma_order.mapped("rma_line_ids.analytic_account_id"),
cls.inv_line_1.account_analytic_id,
)
def test_refund_analytic(cls):
cls.product_1.rma_customer_operation_id = cls.env.ref(
"rma_account.rma_operation_customer_refund"
).id
rma_line = (
cls.env["rma.order.line"]
.with_context(customer=True)
.create(
{
"partner_id": cls.partner_id.id,
"product_id": cls.product_1.id,
"operation_id": cls.env.ref(
"rma_account.rma_operation_customer_refund"
).id,
"in_route_id": cls.env.ref(
"rma.route_rma_customer"
).id,
"out_route_id": cls.env.ref(
"rma.route_rma_customer"
).id,
"in_warehouse_id": cls.env.ref(
"stock.warehouse0"
).id,
"out_warehouse_id": cls.env.ref(
"stock.warehouse0"
).id,
"location_id": cls.env.ref(
"stock.stock_location_stock"
).id,
"type": "customer",
"invoice_line_id": cls.inv_line_1.id,
"uom_id": cls.product_1.uom_id.id,
}
)
)
rma_line._onchange_invoice_line_id()
rma_line.action_rma_to_approve()
rma_line.action_rma_approve()
make_refund = cls.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()
cls.assertEqual(
rma_line.mapped("analytic_account_id"),
rma_line.mapped("refund_line_ids.account_analytic_id"),
)

View File

@@ -4,3 +4,5 @@
from . import rma_add_stock_move
from . import rma_make_picking
from . import rma_add_invoice
from . import rma_refund

View File

@@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
# © 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 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.account_analytic_id:
res.update(
analytic_account_id=line.account_analytic_id.id
)
return res

View File

@@ -0,0 +1,18 @@
# -*- coding: utf-8 -*-
# © 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 api, models
class RmaRefund(models.TransientModel):
_inherit = "rma.refund"
@api.model
def prepare_refund_line(self, item, refund):
res = super(RmaRefund, self).prepare_refund_line(item, refund)
if item.line_id.analytic_account_id:
res.update(
account_analytic_id=item.line_id.analytic_account_id.id
)
return res