diff --git a/rma_account/models/rma_order.py b/rma_account/models/rma_order.py index 15722a8f..02e47d4d 100644 --- a/rma_account/models/rma_order.py +++ b/rma_account/models/rma_order.py @@ -91,33 +91,27 @@ class RmaOrder(models.Model): return res def action_view_invoice_refund(self): - action = self.env.ref("account.action_invoice_tree2") - result = action.read()[0] - move_ids = self.mapped("rma_line_ids.refund_line_ids.move_id").ids - if move_ids: - # choose the view_mode accordingly - if len(move_ids) > 1: - result["domain"] = [("id", "in", move_ids)] - else: - res = self.env.ref("account.move_supplier_form", False) - result["views"] = [(res and res.id or False, "form")] - result["res_id"] = move_ids[0] - return result + move_ids = self.mapped("rma_line_ids.move_id").ids + form_view_ref = self.env.ref("account.view_move_form", False) + tree_view_ref = self.env.ref("account.view_move_tree", False) + + return { + "domain": [("id", "in", move_ids)], + "name": "Refunds", + "res_model": "account.move", + "type": "ir.actions.act_window", + "views": [(tree_view_ref.id, "tree"), (form_view_ref.id, "form")], + } def action_view_invoice(self): - if self.type == "supplier": - action = self.env.ref("account.action_invoice_tree2") - res = self.env.ref("account.move_supplier_form", False) - else: - action = self.env.ref("account.action_invoice_tree") - res = self.env.ref("account.view_move_form", False) - result = action.read()[0] move_ids = self.mapped("rma_line_ids.move_id").ids - if move_ids: - # choose the view_mode accordingly - if len(move_ids) > 1: - result["domain"] = [("id", "in", move_ids)] - else: - result["views"] = [(res and res.id or False, "form")] - result["res_id"] = move_ids[0] - return result + form_view_ref = self.env.ref("account.view_move_form", False) + tree_view_ref = self.env.ref("account.view_move_tree", False) + + return { + "domain": [("id", "in", move_ids)], + "name": "Originating Invoice", + "res_model": "account.move", + "type": "ir.actions.act_window", + "views": [(tree_view_ref.id, "tree"), (form_view_ref.id, "form")], + } diff --git a/rma_account/models/rma_order_line.py b/rma_account/models/rma_order_line.py index 8a1efff5..9d049572 100644 --- a/rma_account/models/rma_order_line.py +++ b/rma_account/models/rma_order_line.py @@ -4,8 +4,6 @@ from odoo import _, api, fields, models from odoo.exceptions import UserError, ValidationError -from odoo.addons import decimal_precision as dp - class RmaOrderLine(models.Model): _inherit = "rma.order.line" @@ -24,7 +22,7 @@ class RmaOrderLine(models.Model): for rec in self: rec.qty_refunded = sum( rec.refund_line_ids.filtered( - lambda i: i.move_id.state in ("open", "paid") + lambda i: i.move_id.state in ("posted") ).mapped("quantity") ) @@ -122,12 +120,16 @@ class RmaOrderLine(models.Model): if not res.get("domain"): res["domain"] = {} domain = [ + "&", + "&", + ("rma_line_id", "=", False), + ("exclude_from_invoice_tab", "=", False), "|", ("move_id.partner_id", "=", self.partner_id.id), ("move_id.partner_id", "child_of", self.partner_id.id), ] - if self.product_id: - domain.append(("product_id", "=", self.product_id.id)) + # if self.product_id: + # domain.insert(2, ("product_id", "=", self.product_id.id)) res["domain"]["account_move_line_id"] = domain return res @@ -247,27 +249,29 @@ class RmaOrderLine(models.Model): return {} def action_view_invoice(self): - action = self.env.ref("account.action_invoice_tree") - result = action.read()[0] - res = self.env.ref("account.view_move_form", False) - result["views"] = [(res and res.id or False, "form")] - result["view_id"] = res and res.id or False - result["res_id"] = self.account_move_line_id.move_id.id - return result + form_view_ref = self.env.ref("account.view_move_form", False) + tree_view_ref = self.env.ref("account.view_move_tree", False) + + return { + "domain": [("id", "in", [self.account_move_line_id.move_id.id])], + "name": "Originating Invoice", + "res_model": "account.move", + "type": "ir.actions.act_window", + "views": [(tree_view_ref.id, "tree"), (form_view_ref.id, "form")], + } def action_view_refunds(self): - action = self.env.ref("account.action_invoice_tree2") - result = action.read()[0] move_ids = self.mapped("refund_line_ids.move_id").ids - if move_ids: - # choose the view_mode accordingly - if len(move_ids) > 1: - result["domain"] = [("id", "in", move_ids)] - else: - res = self.env.ref("account.move_supplier_form", False) - result["views"] = [(res and res.id or False, "form")] - result["res_id"] = move_ids[0] - return result + form_view_ref = self.env.ref("account.view_move_form", False) + tree_view_ref = self.env.ref("account.view_move_tree", False) + + return { + "domain": [("id", "in", move_ids)], + "name": "Refunds", + "res_model": "account.move", + "type": "ir.actions.act_window", + "views": [(tree_view_ref.id, "tree"), (form_view_ref.id, "form")], + } def name_get(self): res = [] diff --git a/rma_account/security/ir.model.access.csv b/rma_account/security/ir.model.access.csv old mode 100755 new mode 100644 diff --git a/rma_account/tests/test_rma_account.py b/rma_account/tests/test_rma_account.py index 32243011..ab237287 100644 --- a/rma_account/tests/test_rma_account.py +++ b/rma_account/tests/test_rma_account.py @@ -1,6 +1,7 @@ # Copyright 2017-18 ForgeFlow S.L. # License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html) +from odoo import fields from odoo.tests import common @@ -12,22 +13,22 @@ class TestRmaAccount(common.SingleTransactionCase): 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_invoice_wiz = cls.env["rma_add_invoice"] + cls.rma_add_invoice_wiz = cls.env["rma_add_account_move"] cls.rma_refund_wiz = cls.env["rma.refund"] cls.acc_obj = cls.env["account.account"] cls.inv_obj = cls.env["account.move"] cls.invl_obj = cls.env["account.move.line"] cls.product_obj = cls.env["product.product"] - cls.partner_obj = cls.env["res.partner"] + customer1_obj = cls.env["res.partner"] cls.rma_route_cust = cls.env.ref("rma.route_rma_customer") receivable_type = cls.env.ref("account.data_account_type_receivable") - payable_type = cls.env.ref("account.data_account_type_payable") cls.cust_refund_op = cls.env.ref("rma_account.rma_operation_customer_refund") - + cls.sup_refund_op = cls.env.ref("rma_account.rma_operation_supplier_refund") + cls.company_id = cls.env.user.company_id # Create partners - customer1 = cls.partner_obj.create({"name": "Customer 1"}) - supplier1 = cls.partner_obj.create({"name": "Supplier 1"}) + customer1 = customer1_obj.create({"name": "Customer 1"}) + supplier1 = customer1_obj.create({"name": "Supplier 1"}) # Create RMA group and operation: cls.rma_group_customer = cls.rma_obj.create( @@ -55,6 +56,7 @@ class TestRmaAccount(common.SingleTransactionCase): "type": "product", "list_price": 100.0, "rma_customer_operation_id": cls.cust_refund_op.id, + "rma_supplier_operation_id": cls.sup_refund_op.id, } ) cls.product_2 = cls.product_obj.create( @@ -63,81 +65,78 @@ class TestRmaAccount(common.SingleTransactionCase): "type": "product", "list_price": 150.0, "rma_customer_operation_id": cls.operation_1.id, + "rma_supplier_operation_id": cls.sup_refund_op.id, } ) - cls.product_3 = cls.product_obj.create( - {"name": "Test Product 3", "type": "product"} - ) - cls.product_4 = cls.product_obj.create( - {"name": "Test Product 4", "type": "product"} - ) + cls.currency_id = cls.company_id.currency_id # Create Invoices: - customer_account = cls.acc_obj.search( + cls.customer_account = cls.acc_obj.search( [("user_type_id", "=", receivable_type.id)], limit=1 ).id - cls.inv_customer = cls.inv_obj.create( - { - "partner_id": customer1.id, - "account_id": customer_account, - "type": "out_invoice", - } - ) - cls.inv_line_1 = cls.invl_obj.create( - { - "name": cls.product_1.name, - "product_id": cls.product_1.id, - "quantity": 12.0, - "price_unit": 100.0, - "move_id": cls.inv_customer.id, - "uom_id": cls.product_1.uom_id.id, - "account_id": customer_account, - } - ) - cls.inv_line_2 = cls.invl_obj.create( - { - "name": cls.product_2.name, - "product_id": cls.product_2.id, - "quantity": 15.0, - "price_unit": 150.0, - "move_id": cls.inv_customer.id, - "uom_id": cls.product_2.uom_id.id, - "account_id": customer_account, - } - ) - supplier_account = cls.acc_obj.search( - [("user_type_id", "=", payable_type.id)], limit=1 - ).id - cls.inv_supplier = cls.inv_obj.create( - { - "partner_id": supplier1.id, - "account_id": supplier_account, - "type": "in_invoice", - } - ) - cls.inv_line_3 = cls.invl_obj.create( - { - "name": cls.product_3.name, - "product_id": cls.product_3.id, - "quantity": 17.0, - "price_unit": 250.0, - "move_id": cls.inv_supplier.id, - "uom_id": cls.product_3.uom_id.id, - "account_id": supplier_account, - } - ) - cls.inv_line_4 = cls.invl_obj.create( - { - "name": cls.product_4.name, - "product_id": cls.product_4.id, - "quantity": 9.0, - "price_unit": 300.0, - "move_id": cls.inv_supplier.id, - "uom_id": cls.product_4.uom_id.id, - "account_id": supplier_account, - } + cls.invoices = cls.env["account.move"].create( + [ + { + "type": "out_invoice", + "partner_id": customer1.id, + "invoice_date": fields.Date.from_string("2016-01-01"), + "currency_id": cls.currency_id.id, + "invoice_line_ids": [ + ( + 0, + None, + { + "product_id": cls.product_1.id, + "product_uom_id": cls.product_1.uom_id.id, + "quantity": 3, + "price_unit": 1000, + }, + ), + ( + 0, + None, + { + "product_id": cls.product_2.id, + "product_uom_id": cls.product_2.uom_id.id, + "quantity": 2, + "price_unit": 3000, + }, + ), + ], + }, + { + "type": "in_invoice", + "partner_id": supplier1.id, + "invoice_date": fields.Date.from_string("2016-01-01"), + "currency_id": cls.currency_id.id, + "invoice_line_ids": [ + ( + 0, + None, + { + "product_id": cls.product_1.id, + "product_uom_id": cls.product_1.uom_id.id, + "quantity": 3, + "price_unit": 1000, + }, + ), + ( + 0, + None, + { + "product_id": cls.product_2.id, + "product_uom_id": cls.product_2.uom_id.id, + "quantity": 2, + "price_unit": 3000, + }, + ), + ], + }, + ] ) + cls.inv_customer = cls.invoices[0] + cls.inv_supplier = cls.invoices[1] def test_01_add_from_invoice_customer(self): """Test wizard to create RMA from a customer invoice.""" @@ -147,8 +146,9 @@ class TestRmaAccount(common.SingleTransactionCase): "active_ids": self.rma_group_customer.id, "active_model": "rma.order", } - ).create({"line_ids": [(6, 0, self.inv_customer.line_ids.ids)]}) + ).create({"line_ids": [(6, 0, self.inv_customer.invoice_line_ids.ids)]}) add_inv.add_lines() + self.assertEqual(len(self.rma_group_customer.rma_line_ids), 2) for t in self.rma_group_supplier.rma_line_ids.mapped("type"): self.assertEqual(t, "customer") @@ -190,7 +190,7 @@ class TestRmaAccount(common.SingleTransactionCase): ) rma_2._onchange_operation_id() self.assertEqual(rma_2.refund_policy, "ordered") - self.assertEqual(rma_2.qty_to_refund, 15.0) + self.assertEqual(rma_2.qty_to_refund, 2.0) def test_04_rma_create_refund(self): """Generate a Refund from a customer RMA.""" @@ -200,27 +200,27 @@ class TestRmaAccount(common.SingleTransactionCase): rma.action_rma_to_approve() rma.action_rma_approve() self.assertEqual(rma.refund_count, 0) - self.assertEqual(rma.qty_to_refund, 15.0) + self.assertEqual(rma.qty_to_refund, 2.0) self.assertEqual(rma.qty_refunded, 0.0) make_refund = self.rma_refund_wiz.with_context( {"customer": True, "active_ids": rma.ids, "active_model": "rma.order.line"} ).create({"description": "Test refund"}) make_refund.invoice_refund() - rma.refund_line_ids.move_id.action_invoice_open() + rma.refund_line_ids.move_id.post() rma._compute_refund_count() self.assertEqual(rma.refund_count, 1) self.assertEqual(rma.qty_to_refund, 0.0) - self.assertEqual(rma.qty_refunded, 15.0) + self.assertEqual(rma.qty_refunded, 2.0) def test_05_fill_rma_from_inv_line(self): """Test filling a RMA (line) from a invoice line.""" rma = self.rma_line_obj.new( { "partner_id": self.inv_customer.partner_id.id, - "account_move_line_id": self.inv_line_1.id, + "account_move_line_id": self.inv_supplier.line_ids.ids[0], } ) self.assertFalse(rma.product_id) rma._onchange_account_move_line_id() self.assertEqual(rma.product_id, self.product_1) - self.assertEqual(rma.product_qty, 12.0) + self.assertEqual(rma.product_qty, 3.0) diff --git a/rma_account/wizards/rma_add_account_move.py b/rma_account/wizards/rma_add_account_move.py index 1bd518f5..894b5e43 100644 --- a/rma_account/wizards/rma_add_account_move.py +++ b/rma_account/wizards/rma_add_account_move.py @@ -11,7 +11,7 @@ class RmaAddAccountMove(models.TransientModel): @api.model def default_get(self, fields_list): - res = super(RmaAddInvoice, self).default_get(fields_list) + res = super(RmaAddAccountMove, self).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"] @@ -118,7 +118,9 @@ class RmaAddAccountMove(models.TransientModel): def add_lines(self): rma_line_obj = self.env["rma.order.line"] existing_invoice_lines = self._get_existing_invoice_lines() - for line in self.line_ids: + for line in self.line_ids.filtered( + lambda aml: aml.exclude_from_invoice_tab is False + ): # Load a PO line only once if line not in existing_invoice_lines: data = self._prepare_rma_line_from_inv_line(line) diff --git a/rma_account/wizards/rma_refund.py b/rma_account/wizards/rma_refund.py index b67b3595..b61ccc73 100644 --- a/rma_account/wizards/rma_refund.py +++ b/rma_account/wizards/rma_refund.py @@ -4,8 +4,6 @@ from odoo import _, api, fields, models from odoo.exceptions import ValidationError -import odoo.addons.decimal_precision as dp - class RmaRefund(models.TransientModel): _name = "rma.refund" @@ -116,8 +114,6 @@ class RmaRefund(models.TransientModel): values = { "name": item.line_id.name or item.rma_id.name, "account_id": account.id, - "debit": item.line_id.price_unit, # todo fix - "credit": item.line_id.price_unit, "price_unit": item.line_id.price_unit, "product_uom_id": item.line_id.uom_id.id, "product_id": item.product_id.id, @@ -144,7 +140,6 @@ class RmaRefund(models.TransientModel): "ref": False, "type": "in_refund" if rma_line.type == "supplier" else "out_refund", "journal_id": journal.id, - "currency_id": rma_line.partner_id.company_id.currency_id.id, "fiscal_position_id": rma_line.partner_id.property_account_position_id.id, "state": "draft", "currency_id": rma_line.currency_id.id, @@ -211,7 +206,7 @@ class RmaRefundItem(models.TransientModel): comodel_name="res.partner", string="Invoice Address" ) qty_to_refund = fields.Float( - string="Quantity To Refund", digits=dp.get_precision("Product Unit of Measure") + string="Quantity To Refund", digits="Product Unit of Measure" ) uom_id = fields.Many2one("uom.uom", string="Unit of Measure", readonly=True) refund_policy = fields.Selection(