From c67db6f9ef4cabd16d0389bcdc47aa6eba9c90c6 Mon Sep 17 00:00:00 2001 From: Jared Kipe Date: Mon, 27 Jan 2020 15:40:29 -0800 Subject: [PATCH] IMP `maintenance_repair` Guard around not having `stock_analytic` installed, improve form of code. --- maintenance_repair/models/maintenance.py | 37 +++++++++---------- .../tests/test_maintenance_repair.py | 2 +- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/maintenance_repair/models/maintenance.py b/maintenance_repair/models/maintenance.py index b058cd2d..dc7ebade 100644 --- a/maintenance_repair/models/maintenance.py +++ b/maintenance_repair/models/maintenance.py @@ -102,28 +102,27 @@ class MaintenanceRequestRepairLine(models.Model): else: line.cost = 0.0 + def _repair_complete_stock_move_values(self): + values = { + 'name': self.request_id.name, + 'product_id': self.product_id.id, + 'product_uom_qty': self.product_uom_qty, + 'product_uom': self.product_uom_id.id, + 'location_id': self.request_id.repair_location_id.id, + 'location_dest_id': self.request_id.repair_location_dest_id.id, + 'maintenance_request_id': self.request_id.id, + 'origin': self.request_id.name, + } + # Optional modules for linking maintenance requests to projects, and stock moves to analytic accounts + if hasattr(self.request_id, 'project_id') and hasattr(self.env['stock.move'], 'analytic_account_id'): + values['analytic_account_id'] = self.request_id.project_id.analytic_account_id.id + return values + def action_complete(self): # Create stock movements. - Inspired by mrp_repair - MoveObj = self.env['stock.move'] + stock_move_model = self.env['stock.move'] for line in self.filtered(lambda l: not l.state == 'done'): - request = line.request_id - - # Optional hooks to `maintenance_timesheet` and `stock_analytic` - analytic_account_id = False - if getattr(request, 'project_id', False): - analytic_account_id = request.project_id.analytic_account_id.id - - move = MoveObj.create({ - 'name': request.name, - 'product_id': line.product_id.id, - 'product_uom_qty': line.product_uom_qty, - 'product_uom': line.product_uom_id.id, - 'location_id': request.repair_location_id.id, - 'location_dest_id': request.repair_location_dest_id.id, - 'maintenance_request_id': request.id, - 'origin': request.name, - 'analytic_account_id': analytic_account_id, - }) + move = stock_move_model.create(self._repair_complete_stock_move_values()) move = move.sudo() move._action_confirm() move._action_assign() diff --git a/maintenance_repair/tests/test_maintenance_repair.py b/maintenance_repair/tests/test_maintenance_repair.py index 7d711853..aa358fa7 100644 --- a/maintenance_repair/tests/test_maintenance_repair.py +++ b/maintenance_repair/tests/test_maintenance_repair.py @@ -20,7 +20,7 @@ class TestMaintenanceRepair(common.TransactionCase): }) self.assertEqual(request.repair_status, 'no') - product_to_repair = self.env.ref('product.product_product_24_product_template') + product_to_repair = self.env.ref('product.product_product_24_product_template').product_variant_id line = self.env['maintenance.request.repair.line'].create({ 'request_id': request.id, 'product_id': product_to_repair.id,