[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.
This commit is contained in:
Simon Lejeune
2017-10-02 12:55:22 +02:00
parent af297345c6
commit abfac77a06
5 changed files with 58 additions and 39 deletions
@@ -56,7 +56,6 @@
<field name="state">code</field>
<field name="code">
model.run_scheduler(True)
env['stock.quant']._merge_quants()
</field>
<field eval="True" name="active"/>
<field name="user_id" ref="base.user_root"/>
+28 -22
View File
@@ -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:
@@ -33,16 +33,7 @@
<field name="model_id" ref="product.model_product_product"></field>
<field name="binding_model_id" ref="product.model_product_product"></field>
<field name="code">
# 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', '&lt;', 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]
</field>
</record>
+23
View File
@@ -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()
@@ -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