diff --git a/rma_repair/__manifest__.py b/rma_repair/__manifest__.py index ff01690c..912bd2e9 100644 --- a/rma_repair/__manifest__.py +++ b/rma_repair/__manifest__.py @@ -9,17 +9,15 @@ "summary": "Links RMA with Repairs.", "author": "Eficent, Odoo Community Association (OCA)", "website": "https://github.com/Eficent/stock-rma", - "depends": [ - "rma_account", - "repair_refurbish" + "depends": ["rma_account", "repair_refurbish"], + "data": [ + "views/rma_order_view.xml", + "views/rma_operation_view.xml", + "views/repair_view.xml", + "wizards/rma_order_line_make_repair_view.xml", + "views/rma_order_line_view.xml", + "data/repair_sequence.xml", ], - "data": ["views/rma_order_view.xml", - "views/rma_operation_view.xml", - "views/repair_view.xml", - "wizards/rma_order_line_make_repair_view.xml", - "views/rma_order_line_view.xml", - "data/repair_sequence.xml", - ], "installable": True, "auto_install": True, } diff --git a/rma_repair/models/repair.py b/rma_repair/models/repair.py index 5e74b6c4..9aafdb0b 100644 --- a/rma_repair/models/repair.py +++ b/rma_repair/models/repair.py @@ -8,9 +8,9 @@ class RepairOrder(models.Model): _inherit = "repair.order" rma_line_id = fields.Many2one( - comodel_name='rma.order.line', string='RMA', ondelete='restrict', + comodel_name="rma.order.line", string="RMA", ondelete="restrict" ) under_warranty = fields.Boolean( - related='rma_line_id.under_warranty', readonly=False, + related="rma_line_id.under_warranty", readonly=False ) - invoice_status = fields.Selection(related='invoice_id.state') + invoice_status = fields.Selection(related="invoice_id.state") diff --git a/rma_repair/models/rma_operation.py b/rma_repair/models/rma_operation.py index c48d5693..464f3893 100644 --- a/rma_repair/models/rma_operation.py +++ b/rma_repair/models/rma_operation.py @@ -5,11 +5,17 @@ from odoo import fields, models class RmaOperation(models.Model): - _inherit = 'rma.operation' + _inherit = "rma.operation" - repair_type = fields.Selection([ - ('no', 'Not required'), ('ordered', 'Based on Ordered Quantities'), - ('received', 'Based on Received Quantities')], - string="Repair Policy", default='no') - delivery_policy = fields.Selection(selection_add=[ - ('repair', 'Based on Repair Quantities')]) + repair_type = fields.Selection( + [ + ("no", "Not required"), + ("ordered", "Based on Ordered Quantities"), + ("received", "Based on Received Quantities"), + ], + string="Repair Policy", + default="no", + ) + delivery_policy = fields.Selection( + selection_add=[("repair", "Based on Repair Quantities")] + ) diff --git a/rma_repair/models/rma_order.py b/rma_repair/models/rma_order.py index 962937ca..8a5de370 100644 --- a/rma_repair/models/rma_order.py +++ b/rma_repair/models/rma_order.py @@ -10,16 +10,17 @@ class RmaOrder(models.Model): @api.multi def _compute_repair_count(self): for rma in self: - repairs = rma.mapped('rma_line_ids.repair_ids') + repairs = rma.mapped("rma_line_ids.repair_ids") rma.repair_count = len(repairs) repair_count = fields.Integer( - compute='_compute_repair_count', string='# of Repairs') + compute="_compute_repair_count", string="# of Repairs" + ) @api.multi def action_view_repair_order(self): - action = self.env.ref('repair.action_repair_order_tree') + action = self.env.ref("repair.action_repair_order_tree") result = action.read()[0] - repair_ids = self.mapped('rma_line_ids.repair_ids').ids - result['domain'] = [('id', 'in', repair_ids)] + repair_ids = self.mapped("rma_line_ids.repair_ids").ids + result["domain"] = [("id", "in", repair_ids)] return result diff --git a/rma_repair/models/rma_order_line.py b/rma_repair/models/rma_order_line.py index 4f577ddd..4394921f 100644 --- a/rma_repair/models/rma_order_line.py +++ b/rma_repair/models/rma_order_line.py @@ -2,114 +2,135 @@ # License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html) from odoo import api, fields, models + from odoo.addons import decimal_precision as dp class RmaOrderLine(models.Model): _inherit = "rma.order.line" - @api.depends('repair_ids', 'repair_type', 'repair_ids.state', - 'qty_to_receive') + @api.depends("repair_ids", "repair_type", "repair_ids.state", "qty_to_receive") def _compute_qty_to_repair(self): for line in self: - if line.repair_type == 'no': + if line.repair_type == "no": line.qty_to_repair = 0.0 - elif line.repair_type == 'ordered': - qty = line._get_rma_repaired_qty() + \ - line._get_rma_under_repair_qty() + elif line.repair_type == "ordered": + qty = line._get_rma_repaired_qty() + line._get_rma_under_repair_qty() line.qty_to_repair = line.product_qty - qty - elif line.repair_type == 'received': - qty = line._get_rma_repaired_qty() + \ - line._get_rma_under_repair_qty() + elif line.repair_type == "received": + qty = line._get_rma_repaired_qty() + line._get_rma_under_repair_qty() line.qty_to_repair = line.qty_received - qty else: line.qty_to_repair = 0.0 - @api.depends('repair_ids', 'repair_type', 'repair_ids.state', - 'qty_to_receive') + @api.depends("repair_ids", "repair_type", "repair_ids.state", "qty_to_receive") def _compute_qty_repaired(self): for line in self: line.qty_repaired = line._get_rma_repaired_qty() - @api.depends('repair_ids', 'repair_type', 'repair_ids.state', - 'qty_to_receive') + @api.depends("repair_ids", "repair_type", "repair_ids.state", "qty_to_receive") def _compute_qty_under_repair(self): for line in self: line.qty_under_repair = line._get_rma_under_repair_qty() - @api.depends('repair_ids') + @api.depends("repair_ids") def _compute_repair_count(self): for line in self: line.repair_count = len(line.repair_ids) repair_ids = fields.One2many( - comodel_name='repair.order', inverse_name='rma_line_id', - string='Repair Orders', readonly=True, - states={'draft': [('readonly', False)]}, copy=False) + comodel_name="repair.order", + inverse_name="rma_line_id", + string="Repair Orders", + readonly=True, + states={"draft": [("readonly", False)]}, + copy=False, + ) qty_to_repair = fields.Float( - string='Qty To Repair', copy=False, - digits=dp.get_precision('Product Unit of Measure'), - readonly=True, compute='_compute_qty_to_repair', - store=True) + string="Qty To Repair", + copy=False, + digits=dp.get_precision("Product Unit of Measure"), + readonly=True, + compute="_compute_qty_to_repair", + store=True, + ) qty_under_repair = fields.Float( - string='Qty Under Repair', copy=False, - digits=dp.get_precision('Product Unit of Measure'), - readonly=True, compute='_compute_qty_under_repair', - store=True) + string="Qty Under Repair", + copy=False, + digits=dp.get_precision("Product Unit of Measure"), + readonly=True, + compute="_compute_qty_under_repair", + store=True, + ) qty_repaired = fields.Float( - string='Qty Repaired', copy=False, - digits=dp.get_precision('Product Unit of Measure'), - readonly=True, compute='_compute_qty_repaired', - store=True, help="Quantity repaired or being repaired.") - repair_type = fields.Selection(selection=[ - ('no', 'Not required'), ('ordered', 'Based on Ordered Quantities'), - ('received', 'Based on Received Quantities')], - string="Repair Policy", default='no', required=True) + string="Qty Repaired", + copy=False, + digits=dp.get_precision("Product Unit of Measure"), + readonly=True, + compute="_compute_qty_repaired", + store=True, + help="Quantity repaired or being repaired.", + ) + repair_type = fields.Selection( + selection=[ + ("no", "Not required"), + ("ordered", "Based on Ordered Quantities"), + ("received", "Based on Received Quantities"), + ], + string="Repair Policy", + default="no", + required=True, + ) repair_count = fields.Integer( - compute='_compute_repair_count', string='# of Repairs') + compute="_compute_repair_count", string="# of Repairs" + ) - delivery_policy = fields.Selection(selection_add=[ - ('repair', 'Based on Repair Quantities')]) - qty_to_pay = fields.Float( - compute='_compute_qty_to_pay') - qty_to_deliver = fields.Float( - compute='_compute_qty_to_deliver') + delivery_policy = fields.Selection( + selection_add=[("repair", "Based on Repair Quantities")] + ) + qty_to_pay = fields.Float(compute="_compute_qty_to_pay") + qty_to_deliver = fields.Float(compute="_compute_qty_to_deliver") @api.multi - @api.depends('delivery_policy', 'product_qty', 'type', 'repair_ids', - 'repair_ids.state', 'repair_ids.invoice_method', - 'repair_type', 'repair_ids.invoice_status') + @api.depends( + "delivery_policy", + "product_qty", + "type", + "repair_ids", + "repair_ids.state", + "repair_ids.invoice_method", + "repair_type", + "repair_ids.invoice_status", + ) def _compute_qty_to_pay(self): - for rec in self.filtered(lambda l: l.delivery_policy == 'repair'): + for rec in self.filtered(lambda l: l.delivery_policy == "repair"): qty_to_pay = 0.0 for repair in rec.repair_ids.filtered( - lambda r: r.invoice_method != 'none' - and r.invoice_status != 'paid'): + lambda r: r.invoice_method != "none" and r.invoice_status != "paid" + ): qty_to_pay += repair.product_qty rec.qty_to_pay = qty_to_pay @api.multi def action_view_repair_order(self): - action = self.env.ref('repair.action_repair_order_tree') + action = self.env.ref("repair.action_repair_order_tree") result = action.read()[0] repair_ids = self.repair_ids.ids if len(repair_ids) != 1: - result['domain'] = [('id', 'in', repair_ids)] + result["domain"] = [("id", "in", repair_ids)] elif len(repair_ids) == 1: - res = self.env.ref('repair.view_repair_order_form', False) - result['views'] = [(res and res.id or False, 'form')] - result['res_id'] = repair_ids[0] + res = self.env.ref("repair.view_repair_order_form", False) + result["views"] = [(res and res.id or False, "form")] + result["res_id"] = repair_ids[0] return result @api.multi def _get_rma_repaired_qty(self): self.ensure_one() qty = 0.0 - for repair in self.repair_ids.filtered( - lambda p: p.state == 'done'): + for repair in self.repair_ids.filtered(lambda p: p.state == "done"): repair_qty = self.uom_id._compute_quantity( - repair.product_qty, - repair.product_uom, + repair.product_qty, repair.product_uom ) qty += repair_qty return qty @@ -119,29 +140,38 @@ class RmaOrderLine(models.Model): self.ensure_one() qty = 0.0 for repair in self.repair_ids.filtered( - lambda p: p.state not in ('draft', 'cancel', 'done')): + lambda p: p.state not in ("draft", "cancel", "done") + ): repair_qty = self.uom_id._compute_quantity( - repair.product_qty, - repair.product_uom, + repair.product_qty, repair.product_uom ) qty += repair_qty return qty - @api.onchange('operation_id') + @api.onchange("operation_id") def _onchange_operation_id(self): result = super(RmaOrderLine, self)._onchange_operation_id() if self.operation_id: - self.repair_type = self.operation_id.repair_type or 'no' + self.repair_type = self.operation_id.repair_type or "no" return result @api.multi - @api.depends('move_ids', 'move_ids.state', - 'delivery_policy', 'product_qty', 'type', 'qty_delivered', - 'qty_received', 'repair_ids', 'repair_type', - 'repair_ids.state', 'qty_to_pay', 'repair_ids.invoice_status') + @api.depends( + "move_ids", + "move_ids.state", + "delivery_policy", + "product_qty", + "type", + "qty_delivered", + "qty_received", + "repair_ids", + "repair_type", + "repair_ids.state", + "qty_to_pay", + "repair_ids.invoice_status", + ) def _compute_qty_to_deliver(self): res = super(RmaOrderLine, self)._compute_qty_to_deliver() - for rec in self.filtered(lambda l: l.delivery_policy == 'repair'): - rec.qty_to_deliver = rec.qty_repaired - rec.qty_delivered - \ - rec.qty_to_pay + for rec in self.filtered(lambda l: l.delivery_policy == "repair"): + rec.qty_to_deliver = rec.qty_repaired - rec.qty_delivered - rec.qty_to_pay return res diff --git a/rma_repair/tests/test_rma_repair.py b/rma_repair/tests/test_rma_repair.py index a1b2a537..9539540b 100644 --- a/rma_repair/tests/test_rma_repair.py +++ b/rma_repair/tests/test_rma_repair.py @@ -5,190 +5,211 @@ from odoo.tests import common class TestRmaRepair(common.SingleTransactionCase): - @classmethod def setUpClass(cls): super(TestRmaRepair, cls).setUpClass() - cls.rma_obj = cls.env['rma.order'] - cls.rma_line_obj = cls.env['rma.order.line'] - cls.rma_op = cls.env['rma.operation'] - cls.rma_add_invoice_wiz = cls.env['rma_add_invoice'] - cls.rma_make_repair_wiz = cls.env['rma.order.line.make.repair'] - cls.repair_line_obj = cls.env['repair.line'] - cls.acc_obj = cls.env['account.account'] - cls.inv_obj = cls.env['account.invoice'] - cls.invl_obj = cls.env['account.invoice.line'] - cls.product_obj = cls.env['product.product'] - cls.partner_obj = cls.env['res.partner'] + cls.rma_obj = cls.env["rma.order"] + cls.rma_line_obj = cls.env["rma.order.line"] + cls.rma_op = cls.env["rma.operation"] + cls.rma_add_invoice_wiz = cls.env["rma_add_invoice"] + cls.rma_make_repair_wiz = cls.env["rma.order.line.make.repair"] + cls.repair_line_obj = cls.env["repair.line"] + cls.acc_obj = cls.env["account.account"] + cls.inv_obj = cls.env["account.invoice"] + cls.invl_obj = cls.env["account.invoice.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') - receivable_type = cls.env.ref('account.data_account_type_receivable') + cls.rma_route_cust = cls.env.ref("rma.route_rma_customer") + receivable_type = cls.env.ref("account.data_account_type_receivable") # Create partners - customer1 = cls.partner_obj.create({'name': 'Customer 1'}) + customer1 = cls.partner_obj.create({"name": "Customer 1"}) # Create RMA group and operation: - cls.rma_group_customer = cls.rma_obj.create({ - 'partner_id': customer1.id, - 'type': 'customer', - }) - cls.operation_1 = cls.rma_op.create({ - 'code': 'TEST', - 'name': 'Repair afer receive', - 'type': 'customer', - 'receipt_policy': 'ordered', - 'repair_type': 'received', - 'in_route_id': cls.rma_route_cust.id, - 'out_route_id': cls.rma_route_cust.id, - }) - cls.operation_2 = cls.rma_op.create({ - 'code': 'TEST', - 'name': 'Repair on order', - 'type': 'customer', - 'receipt_policy': 'ordered', - 'repair_type': 'ordered', - 'in_route_id': cls.rma_route_cust.id, - 'out_route_id': cls.rma_route_cust.id, - }) - cls.operation_3 = cls.rma_op.create({ - 'code': 'TEST', - 'name': 'Deliver after repair', - 'type': 'customer', - 'receipt_policy': 'ordered', - 'repair_type': 'ordered', - 'delivery_policy': 'repair', - 'in_route_id': cls.rma_route_cust.id, - 'out_route_id': cls.rma_route_cust.id, - }) + cls.rma_group_customer = cls.rma_obj.create( + {"partner_id": customer1.id, "type": "customer"} + ) + cls.operation_1 = cls.rma_op.create( + { + "code": "TEST", + "name": "Repair afer receive", + "type": "customer", + "receipt_policy": "ordered", + "repair_type": "received", + "in_route_id": cls.rma_route_cust.id, + "out_route_id": cls.rma_route_cust.id, + } + ) + cls.operation_2 = cls.rma_op.create( + { + "code": "TEST", + "name": "Repair on order", + "type": "customer", + "receipt_policy": "ordered", + "repair_type": "ordered", + "in_route_id": cls.rma_route_cust.id, + "out_route_id": cls.rma_route_cust.id, + } + ) + cls.operation_3 = cls.rma_op.create( + { + "code": "TEST", + "name": "Deliver after repair", + "type": "customer", + "receipt_policy": "ordered", + "repair_type": "ordered", + "delivery_policy": "repair", + "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.operation_1.id, - }) - cls.product_2 = cls.product_obj.create({ - 'name': 'Test Product 2', - 'type': 'product', - 'list_price': 150.0, - 'rma_customer_operation_id': cls.operation_2.id, - }) - cls.product_3 = cls.product_obj.create({ - 'name': 'Test Product 3', - 'type': 'product', - 'list_price': 1.0, - 'rma_customer_operation_id': cls.operation_3.id, - }) + cls.product_1 = cls.product_obj.create( + { + "name": "Test Product 1", + "type": "product", + "list_price": 100.0, + "rma_customer_operation_id": cls.operation_1.id, + } + ) + cls.product_2 = cls.product_obj.create( + { + "name": "Test Product 2", + "type": "product", + "list_price": 150.0, + "rma_customer_operation_id": cls.operation_2.id, + } + ) + cls.product_3 = cls.product_obj.create( + { + "name": "Test Product 3", + "type": "product", + "list_price": 1.0, + "rma_customer_operation_id": cls.operation_3.id, + } + ) # Create Invoices: 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, - 'invoice_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, - 'invoice_id': cls.inv_customer.id, - 'uom_id': cls.product_2.uom_id.id, - 'account_id': customer_account, - }) - cls.inv_customer2 = cls.inv_obj.create({ - 'partner_id': customer1.id, - 'account_id': customer_account, - 'type': 'out_invoice', - }) - cls.inv_line_3 = cls.invl_obj.create({ - 'name': cls.product_3.name, - 'product_id': cls.product_3.id, - 'quantity': 1.0, - 'price_unit': 1000.0, - 'invoice_id': cls.inv_customer2.id, - 'uom_id': cls.product_3.uom_id.id, - 'account_id': customer_account, - }) - cls.rma_group_customer_2 = cls.rma_obj.create({ - 'partner_id': customer1.id, - 'type': 'customer', - }) - cls.bank_journal = cls.env['account.journal'].search( - [('type', '=', 'bank')], limit=1) - cls.material = cls.product_obj.create({ - 'name': 'Materials', - 'type': 'product', - }) + [("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, + "invoice_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, + "invoice_id": cls.inv_customer.id, + "uom_id": cls.product_2.uom_id.id, + "account_id": customer_account, + } + ) + cls.inv_customer2 = cls.inv_obj.create( + { + "partner_id": customer1.id, + "account_id": customer_account, + "type": "out_invoice", + } + ) + cls.inv_line_3 = cls.invl_obj.create( + { + "name": cls.product_3.name, + "product_id": cls.product_3.id, + "quantity": 1.0, + "price_unit": 1000.0, + "invoice_id": cls.inv_customer2.id, + "uom_id": cls.product_3.uom_id.id, + "account_id": customer_account, + } + ) + cls.rma_group_customer_2 = cls.rma_obj.create( + {"partner_id": customer1.id, "type": "customer"} + ) + cls.bank_journal = cls.env["account.journal"].search( + [("type", "=", "bank")], limit=1 + ) + cls.material = cls.product_obj.create({"name": "Materials", "type": "product"}) cls.material.product_tmpl_id.standard_price = 10 - cls.stock_location = cls.env.ref('stock.stock_location_stock') + cls.stock_location = cls.env.ref("stock.stock_location_stock") 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({ - 'invoice_line_ids': - [(6, 0, self.inv_customer.invoice_line_ids.ids)], - }) + add_inv = self.rma_add_invoice_wiz.with_context( + { + "customer": True, + "active_ids": self.rma_group_customer.id, + "active_model": "rma.order", + } + ).create({"invoice_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) rma_1 = self.rma_group_customer.rma_line_ids.filtered( - lambda r: r.product_id == self.product_1) + lambda r: r.product_id == self.product_1 + ) rma_1.repair_type = self.operation_1.repair_type - self.assertEquals(rma_1.operation_id, self.operation_1, - "Operation should be operation_1") + self.assertEquals( + rma_1.operation_id, self.operation_1, "Operation should be operation_1" + ) rma_2 = self.rma_group_customer.rma_line_ids.filtered( - lambda r: r.product_id == self.product_2) + lambda r: r.product_id == self.product_2 + ) rma_2.repair_type = self.operation_2.repair_type - self.assertEquals(rma_2.operation_id, self.operation_2, - "Operation should be operation_2") + self.assertEquals( + rma_2.operation_id, self.operation_2, "Operation should be operation_2" + ) def test_02_rma_repair_operation(self): """Test RMA quantities using repair operations.""" # Received repair_type: rma_1 = self.rma_group_customer.rma_line_ids.filtered( - lambda r: r.product_id == self.product_1) - self.assertEquals(rma_1.operation_id.repair_type, 'received', - "Incorrect Repair operation") - self.assertEquals(rma_1.qty_to_repair, 0.0, - "Quantity to repair should be 0.0") + lambda r: r.product_id == self.product_1 + ) + self.assertEquals( + rma_1.operation_id.repair_type, "received", "Incorrect Repair operation" + ) + self.assertEquals(rma_1.qty_to_repair, 0.0, "Quantity to repair should be 0.0") # Ordered repair_type: rma_2 = self.rma_group_customer.rma_line_ids.filtered( - lambda r: r.product_id == self.product_2) - self.assertEquals(rma_2.operation_id.repair_type, 'ordered', - "Incorrect Repair operation") + lambda r: r.product_id == self.product_2 + ) + self.assertEquals( + rma_2.operation_id.repair_type, "ordered", "Incorrect Repair operation" + ) self.assertEqual(rma_2.qty_to_repair, 15.0) def test_03_create_repair_order(self): """Generate a Repair Order from a customer RMA.""" rma = self.rma_group_customer.rma_line_ids.filtered( - lambda r: r.product_id == self.product_2) + lambda r: r.product_id == self.product_2 + ) rma.action_rma_to_approve() rma.action_rma_approve() self.assertEqual(rma.repair_count, 0) self.assertEqual(rma.qty_to_repair, 15.0) self.assertEqual(rma.qty_repaired, 0.0) - make_repair = self.rma_make_repair_wiz.with_context({ - 'customer': True, - 'active_ids': rma.ids, - 'active_model': 'rma.order.line', - }).create({ - 'description': 'Test refund', - }) + make_repair = self.rma_make_repair_wiz.with_context( + {"customer": True, "active_ids": rma.ids, "active_model": "rma.order.line"} + ).create({"description": "Test refund"}) make_repair.make_repair_order() rma.repair_ids.action_repair_confirm() self.assertEqual(rma.repair_count, 1) @@ -198,44 +219,44 @@ class TestRmaRepair(common.SingleTransactionCase): def test_04_deliver_after_repair(self): """Only deliver after repair""" - add_inv = self.rma_add_invoice_wiz.with_context({ - 'customer': True, - 'active_ids': self.rma_group_customer_2.id, - 'active_model': 'rma.order', - }).create({ - 'invoice_line_ids': - [(6, 0, self.inv_customer2.invoice_line_ids.ids)], - }) + add_inv = self.rma_add_invoice_wiz.with_context( + { + "customer": True, + "active_ids": self.rma_group_customer_2.id, + "active_model": "rma.order", + } + ).create( + {"invoice_line_ids": [(6, 0, self.inv_customer2.invoice_line_ids.ids)]} + ) add_inv.add_lines() rma = self.rma_group_customer_2.rma_line_ids.filtered( - lambda r: r.product_id == self.product_3) + lambda r: r.product_id == self.product_3 + ) rma.operation_id = self.operation_3.id rma._onchange_operation_id() rma.action_rma_to_approve() rma.action_rma_approve() self.assertEqual(rma.qty_to_deliver, 0.0) - make_repair = self.rma_make_repair_wiz.with_context({ - 'customer': True, - 'active_ids': rma.ids, - 'active_model': 'rma.order.line', - }).create({ - 'description': 'Test deliver', - }) + make_repair = self.rma_make_repair_wiz.with_context( + {"customer": True, "active_ids": rma.ids, "active_model": "rma.order.line"} + ).create({"description": "Test deliver"}) make_repair.make_repair_order() repair = rma.repair_ids - line = self.repair_line_obj.create({ - 'name': 'consume stuff to repair', - 'repair_id': repair.id, - 'type': 'add', - 'product_id': self.material.id, - 'product_uom': self.material.uom_id.id, - 'product_uom_qty': 1.0, - 'location_id': self.stock_location.id, - 'location_dest_id': self.stock_location.id, - 'price_unit': 10.0 - }) + line = self.repair_line_obj.create( + { + "name": "consume stuff to repair", + "repair_id": repair.id, + "type": "add", + "product_id": self.material.id, + "product_uom": self.material.uom_id.id, + "product_uom_qty": 1.0, + "location_id": self.stock_location.id, + "location_dest_id": self.stock_location.id, + "price_unit": 10.0, + } + ) line.onchange_product_id() - repair.invoice_method = 'after_repair' + repair.invoice_method = "after_repair" repair.action_repair_confirm() repair.action_repair_start() repair.action_repair_end() @@ -244,6 +265,6 @@ class TestRmaRepair(common.SingleTransactionCase): repair.invoice_id.action_invoice_open() self.assertEqual(rma.qty_to_deliver, 0.0) repair.invoice_id.pay_and_reconcile(self.bank_journal, 200.0) - self.assertEqual(repair.invoice_status, 'paid') + self.assertEqual(repair.invoice_status, "paid") self.assertEqual(rma.qty_to_pay, 0.0) self.assertEqual(rma.qty_to_deliver, 1.0) diff --git a/rma_repair/wizards/rma_order_line_make_repair.py b/rma_repair/wizards/rma_order_line_make_repair.py index c354ca86..569941e1 100644 --- a/rma_repair/wizards/rma_order_line_make_repair.py +++ b/rma_repair/wizards/rma_order_line_make_repair.py @@ -1,18 +1,21 @@ # Copyright 2017 Eficent Business and IT Consulting Services S.L. # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl-3.0). -import odoo.addons.decimal_precision as dp from odoo import _, api, fields, models from odoo.exceptions import ValidationError +import odoo.addons.decimal_precision as dp + class RmaLineMakeRepair(models.TransientModel): _name = "rma.order.line.make.repair" _description = "Make Repair Order from RMA Line" item_ids = fields.One2many( - comodel_name='rma.order.line.make.repair.item', - inverse_name='wiz_id', string='Items') + comodel_name="rma.order.line.make.repair.item", + inverse_name="wiz_id", + string="Items", + ) @api.model def _prepare_item(self, line): @@ -22,56 +25,55 @@ class RmaLineMakeRepair(models.TransientModel): else: to_refurbish = refurbish_product_id = False return { - 'line_id': line.id, - 'product_id': line.product_id.id, - 'product_qty': line.qty_to_repair, - 'rma_id': line.rma_id.id, - 'out_route_id': line.out_route_id.id, - 'product_uom_id': line.uom_id.id, - 'partner_id': line.partner_id.id, - 'to_refurbish': to_refurbish, - 'refurbish_product_id': refurbish_product_id, - 'location_id': line.location_id.id, - 'location_dest_id': line.location_id.id, - 'invoice_method': 'after_repair', + "line_id": line.id, + "product_id": line.product_id.id, + "product_qty": line.qty_to_repair, + "rma_id": line.rma_id.id, + "out_route_id": line.out_route_id.id, + "product_uom_id": line.uom_id.id, + "partner_id": line.partner_id.id, + "to_refurbish": to_refurbish, + "refurbish_product_id": refurbish_product_id, + "location_id": line.location_id.id, + "location_dest_id": line.location_id.id, + "invoice_method": "after_repair", } @api.model def default_get(self, fields_list): - res = super(RmaLineMakeRepair, self).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'] + res = super(RmaLineMakeRepair, self).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' + 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)]) - res['item_ids'] = items + res["item_ids"] = items return res @api.multi def make_repair_order(self): res = [] - repair_obj = self.env['repair.order'] + repair_obj = self.env["repair.order"] for item in self.item_ids: rma_line = item.line_id data = item._prepare_repair_order(rma_line) repair = repair_obj.create(data) res.append(repair.id) return { - 'domain': [('id', 'in', res)], - 'name': _('Repairs'), - 'view_type': 'form', - 'view_mode': 'tree,form', - 'res_model': 'repair.order', - 'view_id': False, - 'context': False, - 'type': 'ir.actions.act_window' + "domain": [("id", "in", res)], + "name": _("Repairs"), + "view_type": "form", + "view_mode": "tree,form", + "res_model": "repair.order", + "view_id": False, + "context": False, + "type": "ir.actions.act_window", } @@ -79,13 +81,13 @@ class RmaLineMakeRepairItem(models.TransientModel): _name = "rma.order.line.make.repair.item" _description = "RMA Line Make Repair Item" - @api.constrains('product_qty') + @api.constrains("product_qty") def _check_prodcut_qty(self): for rec in self: if rec.product_qty <= 0.0: - raise ValidationError(_('Quantity must be positive.')) + raise ValidationError(_("Quantity must be positive.")) - @api.onchange('to_refurbish') + @api.onchange("to_refurbish") def _onchange_to_refurbish(self): if self.to_refurbish: self.refurbish_product_id = self.product_id.refurbish_product_id @@ -93,80 +95,85 @@ class RmaLineMakeRepairItem(models.TransientModel): self.refurbish_product_id = False wiz_id = fields.Many2one( - comodel_name='rma.order.line.make.repair', string='Wizard', - ondelete='cascade', + comodel_name="rma.order.line.make.repair", string="Wizard", ondelete="cascade" ) line_id = fields.Many2one( - comodel_name='rma.order.line', string='RMA', - required=True, + comodel_name="rma.order.line", string="RMA", required=True ) rma_id = fields.Many2one( - comodel_name='rma.order', related='line_id.rma_id', - string='RMA Order', + comodel_name="rma.order", related="line_id.rma_id", string="RMA Order" ) product_id = fields.Many2one( - comodel_name='product.product', string='Product', readonly=True, + comodel_name="product.product", string="Product", readonly=True ) product_qty = fields.Float( - string='Quantity to repair', digits=dp.get_precision('Product UoS'), + string="Quantity to repair", digits=dp.get_precision("Product UoS") ) product_uom_id = fields.Many2one( - comodel_name='uom.uom', string='UoM', readonly=True, + comodel_name="uom.uom", string="UoM", readonly=True ) out_route_id = fields.Many2one( - comodel_name='stock.location.route', string='Outbound Route', - domain=[('rma_selectable', '=', True)], + comodel_name="stock.location.route", + string="Outbound Route", + domain=[("rma_selectable", "=", True)], ) partner_id = fields.Many2one( - comodel_name='res.partner', string='Customer', required=False, - domain=[('customer', '=', True)], readonly=True, + comodel_name="res.partner", + string="Customer", + required=False, + domain=[("customer", "=", True)], + readonly=True, ) location_id = fields.Many2one( - comodel_name="stock.location", string="Location", required=True, + comodel_name="stock.location", string="Location", required=True ) location_dest_id = fields.Many2one( - comodel_name="stock.location", string="Destination location", - required=True, + comodel_name="stock.location", string="Destination location", required=True ) to_refurbish = fields.Boolean(string="To Refurbish?") refurbish_product_id = fields.Many2one( - comodel_name="product.product", string="Refurbished Product", + comodel_name="product.product", string="Refurbished Product" ) invoice_method = fields.Selection( - string="Invoice Method", selection=[ + string="Invoice Method", + selection=[ ("none", "No Invoice"), ("b4repair", "Before Repair"), - ("after_repair", "After Repair")], + ("after_repair", "After Repair"), + ], required=True, help="Selecting 'Before Repair' or 'After Repair' will allow you " - "to generate invoice before or after the repair is done " - "respectively. 'No invoice' means you don't want to generate " - "invoice for this repair order.", + "to generate invoice before or after the repair is done " + "respectively. 'No invoice' means you don't want to generate " + "invoice for this repair order.", ) @api.model def _prepare_repair_order(self, rma_line): - location_dest = (self.location_dest_id if not self.to_refurbish else - rma_line.product_id.property_stock_refurbish) - refurbish_location_dest_id = (self.location_dest_id.id if - self.to_refurbish else False) - addr = rma_line.partner_id.address_get(['delivery', 'invoice']) + location_dest = ( + self.location_dest_id + if not self.to_refurbish + else rma_line.product_id.property_stock_refurbish + ) + refurbish_location_dest_id = ( + self.location_dest_id.id if self.to_refurbish else False + ) + addr = rma_line.partner_id.address_get(["delivery", "invoice"]) return { - 'product_id': rma_line.product_id.id, - 'partner_id': rma_line.partner_id.id, - 'pricelist_id': rma_line.partner_id.property_product_pricelist.id - or False, - 'product_qty': self.product_qty, - 'rma_line_id': rma_line.id, - 'product_uom': rma_line.product_id.uom_po_id.id, - 'company_id': rma_line.company_id.id, - 'location_id': self.location_id.id, - 'location_dest_id': location_dest.id, - 'refurbish_location_dest_id': refurbish_location_dest_id, - 'refurbish_product_id': self.refurbish_product_id.id, - 'to_refurbish': self.to_refurbish, - 'invoice_method': self.invoice_method, - 'address_id': addr['delivery'], - 'partner_invoice_id': addr['invoice'], - 'lot_id': rma_line.lot_id.id, + "product_id": rma_line.product_id.id, + "partner_id": rma_line.partner_id.id, + "pricelist_id": rma_line.partner_id.property_product_pricelist.id or False, + "product_qty": self.product_qty, + "rma_line_id": rma_line.id, + "product_uom": rma_line.product_id.uom_po_id.id, + "company_id": rma_line.company_id.id, + "location_id": self.location_id.id, + "location_dest_id": location_dest.id, + "refurbish_location_dest_id": refurbish_location_dest_id, + "refurbish_product_id": self.refurbish_product_id.id, + "to_refurbish": self.to_refurbish, + "invoice_method": self.invoice_method, + "address_id": addr["delivery"], + "partner_invoice_id": addr["invoice"], + "lot_id": rma_line.lot_id.id, }