From abfac77a0644cad5eafe5a261b693ce4549bb0b5 Mon Sep 17 00:00:00 2001 From: Simon Lejeune Date: Sun, 1 Oct 2017 21:19:30 +0200 Subject: [PATCH] [REF] stock, stock_account: scheduler The scheduler cron will now only call `run_scheduler`. We refactore this method to extract `_run_scheduler_tasks` and place a call to `_merge_quant` a the end. We override `_run_scheduler_tasks` in stock_account to run the fifo vacuum when the scheduler cron is executed. --- addons/stock/data/stock_sequence_data.xml | 1 - addons/stock/models/procurement.py | 50 +++++++++++-------- .../stock_account/data/stock_account_data.xml | 11 +--- addons/stock_account/models/stock.py | 23 +++++++++ .../tests/test_stockvaluation.py | 12 ++--- 5 files changed, 58 insertions(+), 39 deletions(-) diff --git a/addons/stock/data/stock_sequence_data.xml b/addons/stock/data/stock_sequence_data.xml index 65409e46227..7f07dd7e1d8 100644 --- a/addons/stock/data/stock_sequence_data.xml +++ b/addons/stock/data/stock_sequence_data.xml @@ -56,7 +56,6 @@ code model.run_scheduler(True) -env['stock.quant']._merge_quants() diff --git a/addons/stock/models/procurement.py b/addons/stock/models/procurement.py index 105416390de..2b51cf9f66a 100644 --- a/addons/stock/models/procurement.py +++ b/addons/stock/models/procurement.py @@ -232,6 +232,33 @@ class ProcurementGroup(models.Model): def _get_exceptions_domain(self): return [('procure_method', '=', 'make_to_order'), ('move_orig_ids', '=', False)] + @api.model + def _run_scheduler_tasks(self, use_new_cursor=False, company_id=False): + # Minimum stock rules + self.sudo()._procure_orderpoint_confirm(use_new_cursor=use_new_cursor, company_id=company_id) + + # Search all confirmed stock_moves and try to assign them + confirmed_moves = self.env['stock.move'].search([('state', '=', 'confirmed')], limit=None, order='priority desc, date_expected asc') + for moves_chunk in split_every(100, confirmed_moves.ids): + self.env['stock.move'].browse(moves_chunk)._action_assign() + if use_new_cursor: + self._cr.commit() + + exception_moves = self.env['stock.move'].search(self._get_exceptions_domain()) + for move in exception_moves: + values = move._prepare_procurement_values() + try: + with self._cr.savepoint(): + origin = (move.group_id and (move.group_id.name + ":") or "") + (move.rule_id and move.rule_id.name or move.origin or move.picking_id.name or "/") + self.run(move.product_id, move.product_uom_qty, move.product_uom, move.location_id, move.rule_id and move.rule_id.name or "/", origin, values) + except UserError as error: + self.env['procurement.rule']._log_next_activity(move.product_id, error.name) + if use_new_cursor: + self._cr.commit() + + # Merge duplicated quants + self.env['stock.quant']._merge_quants() + @api.model def run_scheduler(self, use_new_cursor=False, company_id=False): """ Call the scheduler in order to check the running procurements (super method), to check the minimum stock rules @@ -242,28 +269,7 @@ class ProcurementGroup(models.Model): cr = registry(self._cr.dbname).cursor() self = self.with_env(self.env(cr=cr)) # TDE FIXME - # Minimum stock rules - self.sudo()._procure_orderpoint_confirm(use_new_cursor=use_new_cursor, company_id=company_id) - - # Search all confirmed stock_moves and try to assign them - confirmed_moves = self.env['stock.move'].search([('state', '=', 'confirmed')], limit=None, order='priority desc, date_expected asc') - for moves_chunk in split_every(100, confirmed_moves.ids): - self.env['stock.move'].browse(moves_chunk)._action_assign() - if use_new_cursor: - self._cr.commit() - - exception_moves = self.env['stock.move'].search(self._get_exceptions_domain()) - for move in exception_moves: - values = move._prepare_procurement_values() - try: - with self._cr.savepoint(): - origin = (move.group_id and (move.group_id.name + ":") or "") + (move.rule_id and move.rule_id.name or move.origin or move.picking_id.name or "/") - self.run(move.product_id, move.product_uom_qty, move.product_uom, move.location_id, move.rule_id and move.rule_id.name or "/", origin, values) - except UserError as error: - self.env['procurement.rule']._log_next_activity(move.product_id, error.name) - - if use_new_cursor: - self._cr.commit() + self._run_scheduler_tasks(use_new_cursor=use_new_cursor, company_id=company_id) finally: if use_new_cursor: try: diff --git a/addons/stock_account/data/stock_account_data.xml b/addons/stock_account/data/stock_account_data.xml index f8957f2cd95..d5f55811def 100644 --- a/addons/stock_account/data/stock_account_data.xml +++ b/addons/stock_account/data/stock_account_data.xml @@ -33,16 +33,7 @@ -# Call `_fifo_vacuum` on concerned moves -fifo_valued_products = env['product.product'] -fifo_valued_products |= env['product.template'].search([('property_cost_method', '=', 'fifo')]).mapped('product_variant_ids') -fifo_valued_categories = env['product.category'].search([('property_cost_method', '=', 'fifo')]) -fifo_valued_products |= env['product.product'].search([('categ_id', 'child_of', fifo_valued_categories.ids)]) -moves_to_vacuum = env['stock.move'] -Move = env['stock.move'] -for product in fifo_valued_products: - moves_to_vacuum |= Move.search([('product_id', '=', product.id), ('remaining_qty', '<', 0)] + Move._get_all_base_domain()) -moves_to_vacuum._fifo_vacuum() +env['stock.move']._run_fifo_vacuum() action = env.ref('stock_account.product_valuation_action').read()[0] diff --git a/addons/stock_account/models/stock.py b/addons/stock_account/models/stock.py index a20f32a5c3f..36df77a8048 100644 --- a/addons/stock_account/models/stock.py +++ b/addons/stock_account/models/stock.py @@ -340,6 +340,20 @@ class StockMove(models.Model): else: move.with_context(force_valuation_amount=corrected_value)._account_entry_move() + @api.model + def _run_fifo_vacuum(self): + # Call `_fifo_vacuum` on concerned moves + fifo_valued_products = self.env['product.product'] + fifo_valued_products |= self.env['product.template'].search([('property_cost_method', '=', 'fifo')]).mapped( + 'product_variant_ids') + fifo_valued_categories = self.env['product.category'].search([('property_cost_method', '=', 'fifo')]) + fifo_valued_products |= self.env['product.product'].search([('categ_id', 'child_of', fifo_valued_categories.ids)]) + moves_to_vacuum = self.env['stock.move'] + for product in fifo_valued_products: + moves_to_vacuum |= self.search( + [('product_id', '=', product.id), ('remaining_qty', '<', 0)] + self._get_all_base_domain()) + moves_to_vacuum._fifo_vacuum() + @api.multi def _get_accounting_data_for_valuation(self): """ Return the accounts and journal to use to post Journal Entries for @@ -520,3 +534,12 @@ class StockReturnPickingLine(models.TransientModel): _inherit = "stock.return.picking.line" to_refund = fields.Boolean(string="To Refund (update SO/PO)", help='Trigger a decrease of the delivered/received quantity in the associated Sale Order/Purchase Order') + + +class ProcurementGroup(models.Model): + _inherit = 'procurement.group' + + @api.model + def _run_scheduler_tasks(self, use_new_cursor=False, company_id=False): + super(ProcurementGroup, self)._run_scheduler_tasks(use_new_cursor=use_new_cursor, company_id=company_id) + self.env['stock.move']._run_fifo_vacuum() diff --git a/addons/stock_account/tests/test_stockvaluation.py b/addons/stock_account/tests/test_stockvaluation.py index 3059e927acc..42920acb3eb 100644 --- a/addons/stock_account/tests/test_stockvaluation.py +++ b/addons/stock_account/tests/test_stockvaluation.py @@ -479,7 +479,7 @@ class TestStockValuation(TransactionCase): # --------------------------------------------------------------------- # Vacuum is called, we cleanup the negatives. # --------------------------------------------------------------------- - move5._fifo_vacuum() + self.env['stock.move']._run_fifo_vacuum() # account values after vacuum input_aml = self._get_stock_input_move_lines() @@ -609,7 +609,7 @@ class TestStockValuation(TransactionCase): # ----------------------------------------------------------- # vacuum, compensate in # ----------------------------------------------------------- - move6._fifo_vacuum() + self.env['stock.move']._run_fifo_vacuum() # account values after vacuum input_aml = self._get_stock_input_move_lines() @@ -1103,7 +1103,7 @@ class TestStockValuation(TransactionCase): # --------------------------------------------------------------------- # Run the vacuum # --------------------------------------------------------------------- - move1._fifo_vacuum() + self.env['stock.move']._run_fifo_vacuum() # stock values for move1 and move2 self.assertEqual(move1.value, -400.0) @@ -1169,7 +1169,7 @@ class TestStockValuation(TransactionCase): # --------------------------------------------------------------------- # Run the vacuum # --------------------------------------------------------------------- - move1._fifo_vacuum() + self.env['stock.move']._run_fifo_vacuum() # stock values for move1-3 self.assertEqual(move1.value, -400.0) @@ -1298,7 +1298,7 @@ class TestStockValuation(TransactionCase): # --------------------------------------------------------------------- # Run the vacuum # --------------------------------------------------------------------- - move2._fifo_vacuum() + self.env['stock.move']._run_fifo_vacuum() self.assertEqual(move1.value, 100.0) self.assertEqual(move1.remaining_qty, 0.0) @@ -1449,7 +1449,7 @@ class TestStockValuation(TransactionCase): # --------------------------------------------------------------------- # Run the vacuum # --------------------------------------------------------------------- - (move1 + move2 + move3)._fifo_vacuum() + self.env['stock.move']._run_fifo_vacuum() self.assertEqual(len(move3.account_move_ids), 1) # the vacuum shouldn't do anything in this case