stock_scheduler_compute.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. #
  4. # Order Point Method:
  5. # - Order if the virtual stock of today is below the min of the defined order point
  6. #
  7. from odoo import models, tools
  8. import logging
  9. import threading
  10. _logger = logging.getLogger(__name__)
  11. class StockSchedulerCompute(models.TransientModel):
  12. _name = 'stock.scheduler.compute'
  13. _description = 'Run Scheduler Manually'
  14. def _procure_calculation_orderpoint(self):
  15. # As this function is in a new thread, I need to open a new cursor, because the old one may be closed
  16. with self.pool.cursor() as new_cr:
  17. self = self.with_env(self.env(cr=new_cr))
  18. scheduler_cron = self.sudo().env.ref('stock.ir_cron_scheduler_action')
  19. # Avoid to run the scheduler multiple times in the same time
  20. try:
  21. with tools.mute_logger('odoo.sql_db'):
  22. self._cr.execute("SELECT id FROM ir_cron WHERE id = %s FOR UPDATE NOWAIT", (scheduler_cron.id,))
  23. except Exception:
  24. _logger.info('Attempt to run procurement scheduler aborted, as already running')
  25. self._cr.rollback()
  26. return {}
  27. for company in self.env.user.company_ids:
  28. cids = (self.env.user.company_id | self.env.user.company_ids).ids
  29. self.env['procurement.group'].with_context(allowed_company_ids=cids).run_scheduler(
  30. use_new_cursor=self._cr.dbname,
  31. company_id=company.id)
  32. self._cr.rollback()
  33. return {}
  34. def procure_calculation(self):
  35. threaded_calculation = threading.Thread(target=self._procure_calculation_orderpoint, args=())
  36. threaded_calculation.start()
  37. return {'type': 'ir.actions.client', 'tag': 'reload'}