project.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. # -*- coding: utf-8 -*-
  2. from openerp import models, api, fields, _
  3. from datetime import datetime
  4. from openerp.exceptions import ValidationError
  5. import json
  6. class project_task_activity(models.Model):
  7. _name = 'project.task.activity'
  8. _order = "sequence"
  9. task_id = fields.Many2one(
  10. 'project.task',
  11. string='Task',
  12. required=True, ondelete='cascade')
  13. project_id = fields.Many2one(
  14. 'project.project',
  15. string='Project',
  16. related='task_id.project_id', store=True)
  17. company_id = fields.Many2one(
  18. 'res.company',
  19. string='Compañía',
  20. related='task_id.company_id', readonly=True, store=True)
  21. name = fields.Char('Name', required=True)
  22. user_id = fields.Many2one('res.users', 'Responsible')
  23. planned_date = fields.Datetime('Fecha Inicial',default=datetime.today())
  24. done_date = fields.Datetime('Fecha Final')
  25. duration= fields.Char(compute='_get_duration', string='Duracion')
  26. description = fields.Text('Description')
  27. sequence = fields.Integer(
  28. 'Sequence',
  29. help="Gives the sequence order when selecting an activitiy.",
  30. default=10)
  31. is_done = fields.Boolean('Done?')
  32. state= fields.Selection(
  33. [('pending', 'Pendiente'), ('done', 'Realizado'), ('cancel', 'Cancelado')],
  34. 'State', required=True)
  35. hex_value=fields.Char(string='Color', compute='_get_state_value')
  36. _defaults = {
  37. 'user_id': lambda obj, cr, uid, ctx=None: uid,
  38. 'state':'pending',
  39. 'hex_value': '#FA5858',
  40. }
  41. @api.multi
  42. @api.onchange('state')
  43. def _get_state_value(self):
  44. for record in self:
  45. if record.state=='pending':
  46. record.hex_value='#FA5858'
  47. if record.state=='done':
  48. record.hex_value='#58ACFA'
  49. if record.state=='cancel':
  50. record.hex_value='#A4A4A4'
  51. @api.multi
  52. def unlink(self):
  53. res = super(project_task_activity,self).unlink()
  54. # res= self.env.cr.execute("DELETE FROM project_task_activity WHERE id= %s" % (self.id))
  55. return res
  56. @api.onchange('done_date')
  57. def _get_duration(self):
  58. res={}
  59. if self.done_date and self.planned_date:
  60. d1 = datetime.strptime(self.planned_date, "%Y-%m-%d %H:%M:%S")
  61. d2 = datetime.strptime(self.done_date, "%Y-%m-%d %H:%M:%S")
  62. if d2<d1:
  63. raise ValidationError('La fecha final no puede ser menor que la fecha de inicio')
  64. else:
  65. x = str(d2-d1)
  66. self.duration=x
  67. res={'duration':self.duration}
  68. return {'value':res}
  69. @api.one
  70. def action_done(self):
  71. if self.state == 'pending':
  72. self.state = 'done'
  73. self.done_date = datetime.today()
  74. self.hex_value='#58ACFA'
  75. else:
  76. self.state = 'pending'
  77. self.done_date = False
  78. self.hex_value='#FA5858'
  79. @api.one
  80. def action_cancel(self):
  81. self.state = 'cancel'
  82. self.hex_value='#A4A4A4'
  83. class project_task(models.Model):
  84. _inherit = 'project.task'
  85. activity_ids = fields.One2many(
  86. 'project.task.activity', 'task_id', 'Activity', copy=True)
  87. activities_progress = fields.Char(
  88. string=_("Progress"),
  89. compute='_get_activities_progress')
  90. @api.one
  91. @api.depends('activity_ids.state')
  92. def _get_activities_progress(self):
  93. res = []
  94. for activity in self.activity_ids:
  95. if activity.state == 'done':
  96. res.insert(0, {'tooltip': activity.name, 'value': 1})
  97. elif activity.state != 'cancel':
  98. res.insert(0, {'tooltip': activity.name, 'value': 0})
  99. self.activities_progress = json.dumps(res)
  100. class project(models.Model):
  101. _inherit = 'project.project'
  102. activity_ids = fields.One2many(
  103. 'project.task.activity',
  104. string='Activity', compute='_get_task_activity')
  105. activities_todo = fields.Float(
  106. string='Activities to do', compute='_get_activities_todo')
  107. activities_done = fields.Float(
  108. string='Activities done', compute='_get_activities_done')
  109. progress_activities = fields.Float(
  110. string=_("Progress"),
  111. compute='_get_progress_activities')
  112. @api.one
  113. def _get_task_activity(self):
  114. self.activity_ids = self.env['project.task.activity'].search(
  115. [('project_id', '=', self.id)])
  116. @api.one
  117. def _get_activities_todo(self):
  118. self.activities_todo = len(
  119. self.activity_ids.filtered(lambda x: x.state != 'cancel'))
  120. @api.one
  121. def _get_activities_done(self):
  122. self.activities_done = len(
  123. self.activity_ids.filtered(lambda x: x.state == 'done'))
  124. @api.one
  125. def _get_progress_activities(self):
  126. self.progress_activities = 0
  127. if self.activity_ids:
  128. if self.activities_todo != 0:
  129. self.progress_activities = round(
  130. 100 * (self.activities_done / self.activities_todo), 1)
  131. @api.model
  132. def create(self, vals):
  133. vals['type'] = 'template'
  134. print vals
  135. project_id = super(project, self).create(vals)
  136. return project_id
  137. class project_category(models.Model):
  138. _inherit = 'project.category'
  139. hex_value=fields.Char(string='Color')