You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
2.1 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2019 Coop IT Easy SCRL fs
  3. # Robin Keunen <robin@coopiteasy.be>
  4. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
  5. from openerp import models, fields, api
  6. import logging
  7. _logger = logging.getLogger(__name__)
  8. class ProcurementOrder(models.Model):
  9. _inherit = "procurement.order"
  10. @api.model
  11. def cron_cleanup_procurement_order(self):
  12. running_procurement = self.env["procurement.order"].search(
  13. [("state", "=", "running")]
  14. )
  15. # checks if procurement order is done
  16. # -> if all moves are 'done' or 'cancel'
  17. _logger.info("check procurements in running state.")
  18. running_procurement.check()
  19. # in remaining procurement, cancel those with no procurement order
  20. unlinked_procurement = self.env["procurement.order"].search(
  21. [("state", "=", "running"), ("purchase_line_id", "=", False)]
  22. )
  23. _logger.info(
  24. "cancelling %s procurement unlinked from PO."
  25. % len(unlinked_procurement)
  26. )
  27. unlinked_procurement.cancel()
  28. # in remaining procurement, delete those from 'done' purchase order
  29. procurement_linked_to_done_PO = self.env["procurement.order"].search(
  30. [
  31. ("state", "=", "running"),
  32. ("purchase_line_id", "!=", False),
  33. ("purchase_line_id.order_id.state", "=", "done"),
  34. ]
  35. )
  36. _logger.info(
  37. "set %s procurement to done (linked to done PO)"
  38. % len(procurement_linked_to_done_PO)
  39. )
  40. procurement_linked_to_done_PO.write({"state": "done"})
  41. # cancel procurement order from exceptions
  42. # Refused by SPP - La Fève, overload this function in custom modules
  43. # to automate cleanup of exceptions
  44. # exception_procurement = self.env["procurement.order"].search(
  45. # [("state", "=", "exception"), ("purchase_line_id", "=", False)]
  46. # )
  47. # _logger.info(
  48. # "cancelling %s procurement in exception"
  49. # % len(exception_procurement)
  50. # )
  51. # exception_procurement.cancel()