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.

69 lines
3.1 KiB

  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # Copyright (C) 2013 Daniel Reis
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU Affero General Public License as
  8. # published by the Free Software Foundation, either version 3 of the
  9. # License, or (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU Affero General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU Affero General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. ##############################################################################
  20. from openerp.osv import fields, orm
  21. class AnalyticAccount(orm.Model):
  22. """ Add SLA to Analytic Accounts """
  23. _inherit = 'account.analytic.account'
  24. _columns = {
  25. 'sla_ids': fields.many2many(
  26. 'project.sla', string='Service Level Agreement'),
  27. }
  28. def _reapply_sla(self, cr, uid, ids, recalc_closed=False, context=None):
  29. """
  30. Force SLA recalculation on open documents that already are subject to
  31. this SLA Definition.
  32. To use after changing a Contract SLA or it's Definitions.
  33. The ``recalc_closed`` flag allows to also recompute closed documents.
  34. """
  35. ctrl_obj = self.pool.get('project.sla.control')
  36. proj_obj = self.pool.get('project.project')
  37. exclude_states = ['cancelled'] + (not recalc_closed and ['done'] or [])
  38. for contract in self.browse(cr, uid, ids, context=context):
  39. # for each contract, and for each model under SLA control ...
  40. for m_name in set([sla.control_model for sla in contract.sla_ids]):
  41. model = self.pool.get(m_name)
  42. doc_ids = []
  43. if 'analytic_account_id' in model._columns:
  44. doc_ids += model.search(
  45. cr, uid,
  46. [('analytic_account_id', '=', contract.id),
  47. ('state', 'not in', exclude_states)],
  48. context=context)
  49. if 'project_id' in model._columns:
  50. proj_ids = proj_obj.search(
  51. cr, uid, [('analytic_account_id', '=', contract.id)],
  52. context=context)
  53. doc_ids += model.search(
  54. cr, uid,
  55. [('project_id', 'in', proj_ids),
  56. ('state', 'not in', exclude_states)],
  57. context=context)
  58. if doc_ids:
  59. docs = model.browse(cr, uid, doc_ids, context=context)
  60. ctrl_obj.store_sla_control(cr, uid, docs, context=context)
  61. return True
  62. def reapply_sla(self, cr, uid, ids, context=None):
  63. """ Reapply SLAs button action """
  64. return self._reapply_sla(cr, uid, ids, context=context)