diff --git a/rma/models/res_config_settings.py b/rma/models/res_config_settings.py index 4d889880..53091e60 100644 --- a/rma/models/res_config_settings.py +++ b/rma/models/res_config_settings.py @@ -18,3 +18,5 @@ class ResConfigSettings(models.TransientModel): readonly=False, implied_group="rma.group_rma_groups", ) + + module_rma_account = fields.Boolean(string="RMA invoicing") diff --git a/rma/models/rma_order.py b/rma/models/rma_order.py index c84ddc80..38c53a93 100644 --- a/rma/models/rma_order.py +++ b/rma/models/rma_order.py @@ -110,7 +110,7 @@ class RmaOrder(models.Model): ) rma_line_ids = fields.One2many("rma.order.line", "rma_id", string="RMA lines") in_shipment_count = fields.Integer( - compute="_compute_in_shipment_count", string="# of Invoices" + compute="_compute_in_shipment_count", string="# of Shipments" ) out_shipment_count = fields.Integer( compute="_compute_out_shipment_count", string="# of Outgoing Shipments" diff --git a/rma/views/res_config_settings_views.xml b/rma/views/res_config_settings_views.xml index a749333a..5f20fa52 100644 --- a/rma/views/res_config_settings_views.xml +++ b/rma/views/res_config_settings_views.xml @@ -34,6 +34,18 @@ +
+
+ +
+
+
+
+ diff --git a/rma/views/rma_menu.xml b/rma/views/rma_menu.xml index b95f3e97..3d5bdf73 100644 --- a/rma/views/rma_menu.xml +++ b/rma/views/rma_menu.xml @@ -75,4 +75,25 @@ sequence="40" parent="rma.menu_rma_config" action="action_rma_operation_supplier"/> + + + + + + + + + + + diff --git a/rma_account/README.rst b/rma_account/README.rst new file mode 100644 index 00000000..c464cd70 --- /dev/null +++ b/rma_account/README.rst @@ -0,0 +1,52 @@ +.. image:: https://img.shields.io/badge/licence-LGPL--3-blue.svg + :alt: License LGPL-3 + +=========== +RMA Account +=========== + +This module integrates Return Merchandise Authorizations (RMA) with invoices, +allowing to: + +#. Create complete RMA's using existing invoices as a reference. +#. Create refunds from a RMA. + +Usage +===== + +RMA are accessible though Inventory menu. There's four menus, divided by type. +Users can access to the list of RMA or RMA lines. + +Create an RMA: + +#. Select a partner. Fill the rma lines by selecting an invoice. +#. Request approval and approve. +#. Click on RMA Lines button. +#. Click on more and select an option: "Receive products", "Create Delivery + Order, Create Refund". +#. Go back to the RMA. Set the RMA to done if not further action is required. + +Bug Tracker +=========== + +Bugs are tracked on `GitHub 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 +* Aaron Henriquez +* Lois Rilo +* Bhavesh Odedra +* Akim Juillerat + +Maintainer +---------- + +This module is maintained by ForgeFlow. diff --git a/rma_account/__init__.py b/rma_account/__init__.py new file mode 100644 index 00000000..d4a42780 --- /dev/null +++ b/rma_account/__init__.py @@ -0,0 +1,5 @@ +# Copyright 2017 ForgeFlow S.L. +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html) + +from . import models +from . import wizards diff --git a/rma_account/__manifest__.py b/rma_account/__manifest__.py new file mode 100644 index 00000000..62797653 --- /dev/null +++ b/rma_account/__manifest__.py @@ -0,0 +1,25 @@ +# Copyright 2017 ForgeFlow S.L. +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html) + +{ + "name": "RMA Account", + "version": "13.0.1.0.0", + "license": "LGPL-3", + "category": "RMA", + "summary": "Integrates RMA with Invoice Processing", + "author": "ForgeFlow", + "website": "https://github.com/ForgeFlow/stock-rma", + "depends": ["stock_account", "rma"], + "data": [ + "security/ir.model.access.csv", + "data/rma_operation.xml", + "views/rma_order_view.xml", + "views/rma_operation_view.xml", + "views/rma_order_line_view.xml", + "views/account_move_view.xml", + "views/rma_account_menu.xml", + "wizards/rma_add_account_move.xml", + "wizards/rma_refund.xml", + ], + "installable": True, +} diff --git a/rma_account/data/rma_operation.xml b/rma_account/data/rma_operation.xml new file mode 100644 index 00000000..139981df --- /dev/null +++ b/rma_account/data/rma_operation.xml @@ -0,0 +1,42 @@ + + + + + no + + + + no + + + + Refund after receive + RF-C + received + ordered + no + customer + + + + + + Refund after deliver + RF-S + ordered + no + ordered + supplier + + + + + + no + + + + no + + + diff --git a/rma_account/models/__init__.py b/rma_account/models/__init__.py new file mode 100644 index 00000000..09afbe77 --- /dev/null +++ b/rma_account/models/__init__.py @@ -0,0 +1,7 @@ +# Copyright 2017 ForgeFlow 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 +from . import account_move diff --git a/rma_account/models/account_move.py b/rma_account/models/account_move.py new file mode 100644 index 00000000..602b0542 --- /dev/null +++ b/rma_account/models/account_move.py @@ -0,0 +1,174 @@ +# Copyright 2017 ForgeFlow S.L. +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html) + +from odoo import api, fields, models +from odoo.tools.float_utils import float_compare + + +class AccountMove(models.Model): + _inherit = "account.move" + + @api.depends("line_ids.rma_line_ids") + def _compute_rma_count(self): + for inv in self: + rmas = self.mapped("line_ids.rma_line_ids") + inv.rma_count = len(rmas) + + def _prepare_invoice_line_from_rma_line(self, line): + qty = line.qty_to_refund + if float_compare(qty, 0.0, precision_rounding=line.uom_id.rounding) <= 0: + qty = 0.0 + # Todo fill taxes from somewhere + invoice_line = self.env["account.move.line"] + data = { + "purchase_line_id": line.id, + "name": line.name + ": " + line.name, + "product_uom_id": line.uom_id.id, + "product_id": line.product_id.id, + "account_id": invoice_line.with_context( + {"journal_id": self.journal_id.id, "type": "in_invoice"} + )._default_account(), + "price_unit": line.company_id.currency_id.with_context( + date=self.date + ).compute(line.price_unit, self.currency_id, round=False), + "quantity": qty, + "discount": 0.0, + "rma_line_ids": [(4, line.id)], + } + return data + + @api.onchange("add_rma_line_id") + def on_change_add_rma_line_id(self): + if not self.add_rma_line_id: + return {} + if not self.partner_id: + self.partner_id = self.add_rma_line_id.partner_id.id + + new_line = self.env["account.move.line"] + if self.add_rma_line_id not in (self.line_ids.mapped("rma_line_id")): + data = self._prepare_invoice_line_from_rma_line(self.add_rma_line_id) + new_line = new_line.new(data) + new_line._set_additional_fields(self) + self.line_ids += new_line + self.add_rma_line_id = False + return {} + + rma_count = fields.Integer(compute="_compute_rma_count", string="# of RMA") + + add_rma_line_id = fields.Many2one( + comodel_name="rma.order.line", + string="Add from RMA line", + ondelete="set null", + help="Create a refund in based on an existing rma_line", + ) + + def action_view_rma_supplier(self): + action = self.env.ref("rma.action_rma_supplier_lines") + result = action.read()[0] + rma_ids = self.mapped("line_ids.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_supplier_form", False) + result["views"] = [(res and res.id or False, "form")] + result["res_id"] = rma_ids[0] + return result + + def action_view_rma_customer(self): + action = self.env.ref("rma.action_rma_customer_lines") + result = action.read()[0] + rma_ids = self.mapped("line_ids.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 + + +class AccountMoveLine(models.Model): + _inherit = "account.move.line" + + @api.model + def name_search(self, name, args=None, operator="ilike", limit=100): + """Allows to search by Invoice number. This has to be done this way, + as Odoo adds extra args to name_search on _name_search method that + will make impossible to get the desired result.""" + if not args: + args = [] + lines = self.search([("move_id.name", operator, name)] + args, limit=limit) + res = lines.name_get() + 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: + args += [("id", "not in", lines.ids)] + res += super(AccountMoveLine, self).name_search( + name, args=args, operator=operator, limit=limit_rest + ) + return res + + def name_get(self): + res = [] + if self.env.context.get("rma"): + for inv in self: + if inv.move_id.ref: + res.append( + ( + inv.id, + "INV:%s | REF:%s | ORIG:%s | PART:%s | QTY:%s" + % ( + inv.move_id.name or "", + inv.move_id.invoice_origin or "", + inv.move_id.ref or "", + inv.product_id.name, + inv.quantity, + ), + ) + ) + elif inv.move_id.name: + res.append( + ( + inv.id, + "INV:%s | ORIG:%s | PART:%s | QTY:%s" + % ( + inv.move_id.name or "", + inv.move_id.invoice_origin or "", + inv.product_id.name, + inv.quantity, + ), + ) + ) + else: + res.append(super(AccountMoveLine, inv).name_get()[0]) + return res + else: + return super(AccountMoveLine, self).name_get() + + def _compute_rma_count(self): + for invl in self: + rma_lines = invl.mapped("rma_line_ids") + invl.rma_line_count = len(rma_lines) + + rma_line_count = fields.Integer(compute="_compute_rma_count", string="# of RMA") + rma_line_ids = fields.One2many( + comodel_name="rma.order.line", + inverse_name="account_move_line_id", + string="RMA", + readonly=True, + help="This will contain the RMA lines for the invoice line", + ) + + rma_line_id = fields.Many2one( + comodel_name="rma.order.line", + string="RMA line refund", + ondelete="set null", + help="This will contain the rma line that originated the refund line", + ) diff --git a/rma_account/models/rma_operation.py b/rma_account/models/rma_operation.py new file mode 100644 index 00000000..1e984810 --- /dev/null +++ b/rma_account/models/rma_operation.py @@ -0,0 +1,19 @@ +# Copyright 2017 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" + + refund_policy = fields.Selection( + [ + ("no", "No refund"), + ("ordered", "Based on Ordered Quantities"), + ("delivered", "Based on Delivered Quantities"), + ("received", "Based on Received Quantities"), + ], + string="Refund Policy", + default="no", + ) diff --git a/rma_account/models/rma_order.py b/rma_account/models/rma_order.py new file mode 100644 index 00000000..02e47d4d --- /dev/null +++ b/rma_account/models/rma_order.py @@ -0,0 +1,117 @@ +# Copyright 2017 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" + + def _compute_invoice_refund_count(self): + for rec in self: + invoices = rec.mapped("rma_line_ids.refund_line_ids.move_id") + rec.invoice_refund_count = len(invoices) + + def _compute_invoice_count(self): + for rec in self: + invoices = rec.mapped("rma_line_ids.move_id") + rec.invoice_count = len(invoices) + + add_move_id = fields.Many2one( + comodel_name="account.move", + string="Add Invoice", + ondelete="set null", + readonly=True, + ) + invoice_refund_count = fields.Integer( + compute="_compute_invoice_refund_count", string="# of Refunds" + ) + invoice_count = fields.Integer( + compute="_compute_invoice_count", string="# of Invoices" + ) + + def _prepare_rma_line_from_inv_line(self, line): + if self.type == "customer": + operation = ( + self.rma_line_ids.product_id.rma_customer_operation_id + or self.rma_line_ids.product_id.categ_id.rma_customer_operation_id + ) + else: + operation = ( + self.rma_line_ids.product_id.rma_supplier_operation_id + or self.rma_line_ids.product_id.categ_id.rma_supplier_operation_id + ) + data = { + "account_move_line_id": line.id, + "product_id": line.product_id.id, + "name": line.name, + "origin": line.move_id.name, + "uom_id": line.product_uom_id.id, + "operation_id": operation, + "product_qty": line.quantity, + "price_unit": line.move_id.currency_id.compute( + line.price_unit, line.currency_id, round=False + ), + "rma_id": self.id, + } + return data + + @api.onchange("add_move_id") + def on_change_invoice(self): + if not self.add_move_id: + return {} + if not self.partner_id: + self.partner_id = self.add_move_id.partner_id.id + new_lines = self.env["rma.order.line"] + for line in self.add_move_id.line_ids: + # Load a PO line only once + if line in self.rma_line_ids.mapped("account_move_line_id"): + continue + data = self._prepare_rma_line_from_inv_line(line) + new_line = new_lines.new(data) + new_lines += new_line + + self.rma_line_ids += new_lines + self.date_rma = fields.Datetime.now() + self.delivery_address_id = self.add_move_id.partner_id.id + self.invoice_address_id = self.add_move_id.partner_id.id + self.add_move_id = False + return {} + + @api.model + def prepare_rma_line(self, origin_rma, rma_id, line): + line_values = super(RmaOrder, self).prepare_rma_line(origin_rma, rma_id, line) + line_values["invoice_address_id"] = line.invoice_address_id.id + return line_values + + @api.model + def _prepare_rma_data(self, partner, origin_rma): + res = super(RmaOrder, self)._prepare_rma_data(partner, origin_rma) + res["invoice_address_id"] = partner.id + return res + + def action_view_invoice_refund(self): + 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): + 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": "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 new file mode 100644 index 00000000..9d049572 --- /dev/null +++ b/rma_account/models/rma_order_line.py @@ -0,0 +1,289 @@ +# Copyright 2017 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 UserError, ValidationError + + +class RmaOrderLine(models.Model): + _inherit = "rma.order.line" + + @api.model + def _default_invoice_address(self): + partner_id = self.env.context.get("partner_id") + if partner_id: + return self.env["res.partner"].browse(partner_id) + return self.env["res.partner"] + + @api.depends( + "refund_line_ids", "refund_line_ids.move_id.state", "refund_policy", "type" + ) + def _compute_qty_refunded(self): + for rec in self: + rec.qty_refunded = sum( + rec.refund_line_ids.filtered( + lambda i: i.move_id.state in ("posted") + ).mapped("quantity") + ) + + @api.depends( + "refund_line_ids", + "refund_line_ids.move_id.state", + "refund_policy", + "move_ids", + "move_ids.state", + "type", + ) + def _compute_qty_to_refund(self): + qty = 0.0 + for res in self: + if res.refund_policy == "ordered": + qty = res.product_qty - res.qty_refunded + elif res.refund_policy == "received": + qty = res.qty_received - res.qty_refunded + elif res.refund_policy == "delivered": + qty = res.qty_delivered - res.qty_refunded + res.qty_to_refund = qty + + def _compute_refund_count(self): + for rec in self: + rec.refund_count = len(rec.refund_line_ids.mapped("move_id")) + + invoice_address_id = fields.Many2one( + "res.partner", + string="Partner invoice address", + default=lambda self: self._default_invoice_address(), + readonly=True, + states={"draft": [("readonly", False)]}, + help="Invoice address for current rma order.", + ) + refund_count = fields.Integer( + compute="_compute_refund_count", string="# of Refunds", default=0 + ) + account_move_line_id = fields.Many2one( + comodel_name="account.move.line", + string="Originating Invoice Line", + ondelete="restrict", + index=True, + readonly=True, + states={"draft": [("readonly", False)]}, + ) + refund_line_ids = fields.One2many( + comodel_name="account.move.line", + inverse_name="rma_line_id", + string="Refund Lines", + copy=False, + index=True, + readonly=True, + ) + move_id = fields.Many2one( + "account.move", + string="Source", + related="account_move_line_id.move_id", + index=True, + readonly=True, + ) + refund_policy = fields.Selection( + [ + ("no", "No refund"), + ("ordered", "Based on Ordered Quantities"), + ("delivered", "Based on Delivered Quantities"), + ("received", "Based on Received Quantities"), + ], + string="Refund Policy", + required=True, + default="no", + readonly=True, + states={"draft": [("readonly", False)]}, + ) + qty_to_refund = fields.Float( + string="Qty To Refund", + copy=False, + digits="Product Unit of Measure", + readonly=True, + compute="_compute_qty_to_refund", + store=True, + ) + qty_refunded = fields.Float( + string="Qty Refunded", + copy=False, + digits="Product Unit of Measure", + readonly=True, + compute="_compute_qty_refunded", + store=True, + ) + + @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(RmaOrderLine, self)._onchange_product_id() + 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.insert(2, ("product_id", "=", self.product_id.id)) + res["domain"]["account_move_line_id"] = domain + return res + + def _prepare_rma_line_from_inv_line(self, line): + self.ensure_one() + if not self.type: + self.type = self._get_default_type() + if self.type == "customer": + operation = ( + line.product_id.rma_customer_operation_id + or line.product_id.categ_id.rma_customer_operation_id + ) + else: + operation = ( + line.product_id.rma_supplier_operation_id + or line.product_id.categ_id.rma_supplier_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.move_id.name, + "uom_id": line.product_uom_id.id, + "operation_id": operation.id, + "product_qty": line.quantity, + "price_unit": line.move_id.currency_id.compute( + line.price_unit, line.currency_id, round=False + ), + "delivery_address_id": line.move_id.partner_id.id, + "invoice_address_id": line.move_id.partner_id.id, + "receipt_policy": operation.receipt_policy, + "refund_policy": operation.refund_policy, + "delivery_policy": operation.delivery_policy, + "currency_id": line.currency_id.id, + "in_warehouse_id": operation.in_warehouse_id.id or warehouse.id, + "out_warehouse_id": operation.out_warehouse_id.id or warehouse.id, + "in_route_id": operation.in_route_id.id or route.id, + "out_route_id": operation.out_route_id.id or route.id, + "location_id": ( + operation.location_id.id + or operation.in_warehouse_id.lot_rma_id.id + or warehouse.lot_rma_id.id + ), + } + return data + + @api.onchange("account_move_line_id") + def _onchange_account_move_line_id(self): + if not self.account_move_line_id: + return + data = self._prepare_rma_line_from_inv_line(self.account_move_line_id) + self.update(data) + self._remove_other_data_origin("account_move_line_id") + + @api.constrains("account_move_line_id", "partner_id") + def _check_invoice_partner(self): + for rec in self: + if ( + rec.account_move_line_id + and rec.account_move_line_id.move_id.partner_id != rec.partner_id + ): + raise ValidationError( + _( + "RMA customer and originating invoice line customer " + "doesn't match." + ) + ) + + def _remove_other_data_origin(self, exception): + res = super(RmaOrderLine, self)._remove_other_data_origin(exception) + if not exception == "account_move_line_id": + self.account_move_line_id = False + return res + + @api.onchange("operation_id") + def _onchange_operation_id(self): + result = super(RmaOrderLine, self)._onchange_operation_id() + if self.operation_id: + self.refund_policy = self.operation_id.refund_policy or "no" + return result + + @api.constrains("account_move_line_id") + def _check_duplicated_lines(self): + for line in self: + matching_inv_lines = self.env["account.move.line"].search( + [("id", "=", line.account_move_line_id.id)] + ) + if len(matching_inv_lines) > 1: + raise UserError( + _( + "There's an rma for the invoice line %s " + "and invoice %s" + % (line.account_move_line_id, line.account_move_line_id.move_id) + ) + ) + return {} + + def action_view_invoice(self): + 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): + move_ids = self.mapped("refund_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 name_get(self): + res = [] + if self.env.context.get("rma"): + for rma in self: + res.append( + ( + rma.id, + "%s %s qty:%s" + % (rma.name, rma.product_id.name, rma.product_qty), + ) + ) + return res + else: + return super(RmaOrderLine, self).name_get() diff --git a/rma_account/security/ir.model.access.csv b/rma_account/security/ir.model.access.csv new file mode 100644 index 00000000..74b1c356 --- /dev/null +++ b/rma_account/security/ir.model.access.csv @@ -0,0 +1,5 @@ +id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink +access_account_move_customer_user,access_account_move,account.model_account_move,rma.group_rma_customer_user,1,0,0,0 +access_account_move_supplier_user,access_account_move,account.model_account_move,rma.group_rma_supplier_user,1,0,0,0 +access_account_move_line_customer_user,access_account_move_line,account.model_account_move_line,rma.group_rma_customer_user,1,0,0,0 +access_account_move_line_supplier_user,access_account_move_line,account.model_account_move_line,rma.group_rma_supplier_user,1,0,0,0 diff --git a/rma_account/tests/__init__.py b/rma_account/tests/__init__.py new file mode 100644 index 00000000..4edd8b55 --- /dev/null +++ b/rma_account/tests/__init__.py @@ -0,0 +1,4 @@ +# Copyright 2018 ForgeFlow S.L. +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html) + +from . import test_rma_account diff --git a/rma_account/tests/test_rma_account.py b/rma_account/tests/test_rma_account.py new file mode 100644 index 00000000..ab237287 --- /dev/null +++ b/rma_account/tests/test_rma_account.py @@ -0,0 +1,226 @@ +# 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 + + +class TestRmaAccount(common.SingleTransactionCase): + @classmethod + def setUpClass(cls): + super(TestRmaAccount, cls).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_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"] + 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") + 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 = 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( + {"partner_id": customer1.id, "type": "customer"} + ) + cls.rma_group_supplier = cls.rma_obj.create( + {"partner_id": supplier1.id, "type": "supplier"} + ) + 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, + } + ) + + # Create products + cls.product_1 = cls.product_obj.create( + { + "name": "Test Product 1", + "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( + { + "name": "Test Product 2", + "type": "product", + "list_price": 150.0, + "rma_customer_operation_id": cls.operation_1.id, + "rma_supplier_operation_id": cls.sup_refund_op.id, + } + ) + cls.currency_id = cls.company_id.currency_id + + # Create Invoices: + cls.customer_account = cls.acc_obj.search( + [("user_type_id", "=", receivable_type.id)], limit=1 + ).id + + 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.""" + 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() + + 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") + rma_1 = self.rma_group_customer.rma_line_ids.filtered( + lambda r: r.product_id == self.product_1 + ) + self.assertEqual(rma_1.operation_id, self.cust_refund_op) + rma_2 = self.rma_group_customer.rma_line_ids.filtered( + lambda r: r.product_id == self.product_2 + ) + self.assertEqual(rma_2.operation_id, self.operation_1) + + def test_02_add_from_invoice_supplier(self): + """Test wizard to create RMA from a vendor bill.""" + add_inv = self.rma_add_invoice_wiz.with_context( + { + "supplier": True, + "active_ids": self.rma_group_supplier.id, + "active_model": "rma.order", + } + ).create({"line_ids": [(6, 0, self.inv_supplier.line_ids.ids)]}) + add_inv.add_lines() + self.assertEqual(len(self.rma_group_supplier.rma_line_ids), 2) + for t in self.rma_group_supplier.rma_line_ids.mapped("type"): + self.assertEqual(t, "supplier") + + def test_03_rma_refund_operation(self): + """Test RMA quantities using refund operations.""" + # Received refund_policy: + rma_1 = self.rma_group_customer.rma_line_ids.filtered( + lambda r: r.product_id == self.product_1 + ) + self.assertEqual(rma_1.refund_policy, "received") + self.assertEqual(rma_1.qty_to_refund, 0.0) + # TODO: receive and check qty_to_refund is 12.0 + # Ordered refund_policy: + rma_2 = self.rma_group_customer.rma_line_ids.filtered( + lambda r: r.product_id == self.product_2 + ) + rma_2._onchange_operation_id() + self.assertEqual(rma_2.refund_policy, "ordered") + self.assertEqual(rma_2.qty_to_refund, 2.0) + + def test_04_rma_create_refund(self): + """Generate a Refund from a customer RMA.""" + rma = self.rma_group_customer.rma_line_ids.filtered( + lambda r: r.product_id == self.product_2 + ) + rma.action_rma_to_approve() + rma.action_rma_approve() + self.assertEqual(rma.refund_count, 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.post() + rma._compute_refund_count() + self.assertEqual(rma.refund_count, 1) + self.assertEqual(rma.qty_to_refund, 0.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_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, 3.0) diff --git a/rma_account/views/account_move_view.xml b/rma_account/views/account_move_view.xml new file mode 100644 index 00000000..8c927540 --- /dev/null +++ b/rma_account/views/account_move_view.xml @@ -0,0 +1,69 @@ + + + + account.move.form + account.move + + + +
+ +
+
+
+
+ + + + rma.invoice.line.form + account.move.line + + + + + + + + + + + + + + + + + + + + account.move.customer.rma + account.move + + + + + + + + + + Invoice Line + account.move.line + form + + +
diff --git a/rma_account/views/rma_account_menu.xml b/rma_account/views/rma_account_menu.xml new file mode 100644 index 00000000..6f552638 --- /dev/null +++ b/rma_account/views/rma_account_menu.xml @@ -0,0 +1,59 @@ + + + + + + Customer RMA + rma.order.line + [('type','=', 'customer')] + {"search_default_to_refund":1} + tree,form + + + + Supplier RMA + rma.order.line + [('type','=', 'supplier')] + {"search_default_to_refund":1, "supplier":1} + tree,form + + + + + + + + + + + + + + + + diff --git a/rma_account/views/rma_operation_view.xml b/rma_account/views/rma_operation_view.xml new file mode 100644 index 00000000..6f9c4062 --- /dev/null +++ b/rma_account/views/rma_operation_view.xml @@ -0,0 +1,24 @@ + + + + rma.operation.tree + rma.operation + + + + + + + + + + rma.operation.form + rma.operation + + + + + + + + diff --git a/rma_account/views/rma_order_line_view.xml b/rma_account/views/rma_order_line_view.xml new file mode 100644 index 00000000..7accc439 --- /dev/null +++ b/rma_account/views/rma_order_line_view.xml @@ -0,0 +1,110 @@ + + + + rma.order.line.supplier.form + rma.order.line + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + rma.order.line.form + rma.order.line + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + rma.order.line.select + rma.order.line + + + + + + + + diff --git a/rma_account/views/rma_order_view.xml b/rma_account/views/rma_order_view.xml new file mode 100644 index 00000000..5c6b0e82 --- /dev/null +++ b/rma_account/views/rma_order_view.xml @@ -0,0 +1,64 @@ + + + + rma.order.form - rma_account + rma.order + + + + + + + + + + + rma.order.supplier.form + rma.order + + + + + + + + + diff --git a/rma_account/wizards/__init__.py b/rma_account/wizards/__init__.py new file mode 100644 index 00000000..52d1defb --- /dev/null +++ b/rma_account/wizards/__init__.py @@ -0,0 +1,6 @@ +# Copyright 2017 ForgeFlow S.L. +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html) + +from . import rma_refund +from . import rma_add_account_move +from . import rma_order_line_make_supplier_rma diff --git a/rma_account/wizards/rma_add_account_move.py b/rma_account/wizards/rma_add_account_move.py new file mode 100644 index 00000000..894b5e43 --- /dev/null +++ b/rma_account/wizards/rma_add_account_move.py @@ -0,0 +1,131 @@ +# Copyright 2017 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 RmaAddAccountMove(models.TransientModel): + _name = "rma_add_account_move" + _description = "Wizard to add rma lines" + + @api.model + def default_get(self, 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"] + 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["line_ids"] = False + return res + + rma_id = fields.Many2one( + "rma.order", string="RMA Order", readonly=True, ondelete="cascade" + ) + partner_id = fields.Many2one( + comodel_name="res.partner", string="Partner", readonly=True + ) + line_ids = fields.Many2many( + "account.move.line", + "rma_add_account_move_add_line_rel", + "account_move_line_id", + "rma_add_move_id", + string="Invoice Lines", + ) + + def _prepare_rma_line_from_inv_line(self, line): + if self.env.context.get("customer"): + operation = ( + line.product_id.rma_customer_operation_id + or line.product_id.categ_id.rma_customer_operation_id + ) + else: + operation = ( + line.product_id.rma_supplier_operation_id + or line.product_id.categ_id.rma_supplier_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")) + + 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") + ) + data = { + "partner_id": self.partner_id.id, + "account_move_line_id": line.id, + "product_id": line.product_id.id, + "origin": line.move_id.name, + "uom_id": line.product_uom_id.id, + "operation_id": operation.id, + "product_qty": line.quantity, + "price_unit": line.move_id.currency_id.compute( + line.price_unit, line.currency_id, round=False + ), + "delivery_address_id": line.move_id.partner_id.id, + "invoice_address_id": line.move_id.partner_id.id, + "rma_id": self.rma_id.id, + "receipt_policy": operation.receipt_policy, + "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, + "in_route_id": operation.in_route_id.id or route.id, + "out_route_id": operation.out_route_id.id or route.id, + "location_id": ( + operation.location_id.id + or operation.in_warehouse_id.lot_rma_id.id + or warehouse.lot_rma_id.id + ), + } + return data + + @api.model + def _get_rma_data(self): + data = {"date_rma": fields.Datetime.now()} + return data + + @api.model + def _get_existing_invoice_lines(self): + existing_invoice_lines = [] + for rma_line in self.rma_id.rma_line_ids: + existing_invoice_lines.append(rma_line.account_move_line_id) + return existing_invoice_lines + + 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.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) + rma_line_obj.create(data) + rma = self.rma_id + data_rma = self._get_rma_data() + rma.write(data_rma) + return {"type": "ir.actions.act_window_close"} diff --git a/rma_account/wizards/rma_add_account_move.xml b/rma_account/wizards/rma_add_account_move.xml new file mode 100644 index 00000000..4e4efea7 --- /dev/null +++ b/rma_account/wizards/rma_add_account_move.xml @@ -0,0 +1,132 @@ + + + + + + rma.add.invoice + rma_add_account_move + +
+ + + + + + + + + + + + + + + + + + +
+
+ +
+
+ + + rma.add.invoice.supplier + rma_add_account_move + +
+ + + + + + + + + + + + + + + + + + +
+
+ +
+
+ + + Add Invoice + ir.actions.act_window + rma_add_account_move + form + new + + + + + + Add Invoice + ir.actions.act_window + rma_add_account_move + + form + new + + + + + + rma.order.form - invoice wizard + rma.order + + + +