Browse Source

[ADD] beesdoo_base: cron to cleanup stale procurement orders

pull/123/head
robin.keunen 5 years ago
parent
commit
2d5828d439
  1. 1
      beesdoo_base/__openerp__.py
  2. 21
      beesdoo_base/data/cron.xml
  3. 4
      beesdoo_base/models/__init__.py
  4. 47
      beesdoo_base/models/procurement.py

1
beesdoo_base/__openerp__.py

@ -20,6 +20,7 @@
'data': [
'security/groups.xml',
'security/ir.model.access.csv',
'data/cron.xml',
'views/partner.xml',
'wizard/views/member_card.xml',
'wizard/views/partner.xml',

21
beesdoo_base/data/cron.xml

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2019 Coop IT Easy
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
-->
<odoo>
<data noupdate="0">
<record id="cron_cleanup_procurement_order" model="ir.cron">
<field name="name">Procurement: cleanup procurement orders blocking the procurement rules</field>
<field name="active" eval="True"/>
<field name="user_id" ref="base.user_root"/>
<field name="interval_number">1</field>
<field name="interval_type">days</field>
<field name="numbercall">-1</field>
<field name="doall" eval="True"/>
<field name="model">procurement.order</field>
<field name="function">cron_cleanup_procurement_order</field>
<field name="args">()</field>
</record>
</data>
</odoo>

4
beesdoo_base/models/__init__.py

@ -1 +1,3 @@
import partner, membercard
from . import partner
from . import membercard
from . import procurement

47
beesdoo_base/models/procurement.py

@ -0,0 +1,47 @@
# Copyright 2019 Coop IT Easy SCRL fs
# Robin Keunen <robin@coopiteasy.be>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from openerp import models, fields, api
import logging
_logger = logging.getLogger(__name__)
class ProcurementOrder(models.Model):
_inherit = "procurement.order"
@api.model
def cron_cleanup_procurement_order(self):
running_procurement = self.env["procurement.order"].search(
[("state", "=", "running")]
)
# checks if procurement order is done
# -> if all moves are 'done' or 'cancel'
_logger.info("check procurements in running state.")
running_procurement.check()
# in remaining procurement, cancel those with no procurement order
unlinked_procurement = self.env["procurement.order"].search(
[("state", "=", "running"),
("purchase_line_id", "=", False)]
)
_logger.info("cancelling %s procurement unlinked from PO." % len(unlinked_procurement))
unlinked_procurement.cancel()
# in remaining procurement, delete those from 'done' purchase order
procurement_linked_to_done_PO = self.env["procurement.order"].search(
[("state", "=", "running"),
("purchase_line_id", "!=", False),
("purchase_line_id.order_id.state", "=", "done")]
)
_logger.info("set %s procurement to done (linked to done PO)" % len(procurement_linked_to_done_PO))
procurement_linked_to_done_PO.write({"state": "done"})
# cancel procurement order from exceptions
exception_procurement = self.env["procurement.order"].search(
[("state", "=", "exception"),
("purchase_line_id", "=", False)]
)
_logger.info("cancelling %s procurement in exception" % len(exception_procurement))
exception_procurement.cancel()
Loading…
Cancel
Save