event_order.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. # -*- coding: utf-8 -*-
  2. # @authors: Alexander Ezquevo <alexander@acysos.com>
  3. # Copyright (C) 2015 Acysos S.L.
  4. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  5. from openerp import models, fields, api, _
  6. from openerp.exceptions import Warning
  7. EVENT_STATES = [
  8. ('draft', 'Draft'),
  9. ('validated', 'Validated'),
  10. ]
  11. class EventOrder(models.Model):
  12. _name = 'farm.event.order'
  13. _order = 'timestamp DESC'
  14. name = fields.Char(string='Reference', select=True)
  15. state = fields.Selection(string='State', selection=EVENT_STATES,
  16. default='draft')
  17. animal_type = fields.Selection([
  18. ('male', 'Male'),
  19. ('female', 'Female'),
  20. ('individual', 'Individual'),
  21. ('group', 'Group'),
  22. ], string='Animal Type',
  23. select=True)
  24. specie = fields.Many2one(comodel_name='farm.specie', string='Specie',
  25. select=True, required=True)
  26. event_type = fields.Selection([
  27. ('medication', 'Medications'),
  28. ('insemination', 'Inseminations'),
  29. ('pregnancy_diagnosis', 'Pregnancy Diagnosis'),
  30. ('abort', 'Aborts'),
  31. ('farrowing', 'Farrowings'),
  32. ('foster', 'Fosters'),
  33. ('feed', 'Feed'),
  34. ('weaning', 'Weanings'),
  35. ('trasformation_event', 'Trasformation Event'),
  36. ], string="Event Type", required=True, select=True)
  37. farm = fields.Many2one(comodel_name='stock.location', string='Farm',
  38. required=True,
  39. domain=[('usage', '=', 'view'), ])
  40. timestamp = fields.Datetime(string='Date & Time', requiered=True,
  41. default=fields.Datetime.now())
  42. employee = fields.Many2one(comodel_name='res.users', string='Employee',
  43. help='Employee that did the job.')
  44. medication_events = fields.One2many(comodel_name='farm.medication.event',
  45. inverse_name='job_order',
  46. string='Medication')
  47. insemination_events = fields.One2many(
  48. comodel_name='farm.insemination.event', inverse_name='job_order',
  49. string='Insemination')
  50. pregnancy_diagnosis_events = fields.One2many(
  51. comodel_name='farm.pregnancy_diagnosis.event',
  52. inverse_name='job_order',
  53. string='Pregnanci Diagnosis')
  54. abort_events = fields.One2many(comodel_name='farm.abort.event',
  55. inverse_name='job_order',
  56. string='Abort Events')
  57. farrowing_events = fields.One2many(comodel_name='farm.farrowing.event',
  58. inverse_name='job_order',
  59. string='Farrowings')
  60. foster_events = fields.One2many(comodel_name='farm.foster.event',
  61. inverse_name='job_order',
  62. string='Fosters')
  63. weaning_events = fields.One2many(comodel_name='farm.weaning.event',
  64. inverse_name='job_order',
  65. string='weaning')
  66. feed_events = fields.One2many(comodel_name='farm.feed.event',
  67. inverse_name='job_order',
  68. column1='feed_inventory',
  69. string='feed')
  70. trasformation_events = fields.One2many(
  71. comodel_name='farm.transformation.event',
  72. inverse_name='job_order', string='Tasformation Events')
  73. notes = fields.Text(string='Notes')
  74. @api.model
  75. @api.returns('self', lambda value: value.id)
  76. def create(self, vals):
  77. farm_line = self.env['farm.specie.farm_line'].search([
  78. ('farm', '=', vals['farm'])])
  79. if len(farm_line)==0:
  80. vals['name'] = '*'
  81. else:
  82. '''
  83. vals['name'] = farm_line[0].event_order_sequence.get(
  84. farm_line[0].event_order_sequence[0].code)
  85. '''
  86. vals['name'] = farm_line.event_order_sequence.get_id(
  87. farm_line.event_order_sequence.id)
  88. res = super(EventOrder, self).create(vals)
  89. return res
  90. def get_active_events(self):
  91. if self.event_type == 'medication':
  92. active_events = self.medication_events
  93. elif self.event_type == 'insemination':
  94. active_events = self.insemination_events
  95. elif self.event_type == 'pregnancy_diagnosis':
  96. active_events = self.pregnancy_diagnosis_events
  97. elif self.event_type == 'abort':
  98. active_events = self.abort_events
  99. elif self.event_type == 'farrowing':
  100. active_events = self.farrowing_events
  101. elif self.event_type == 'foster':
  102. active_events = self.foster_events
  103. elif self.event_type == 'feed':
  104. active_events = self.feed_events
  105. elif self.event_type == 'weaning':
  106. active_events = self.weaning_events
  107. elif self.event_type == 'trasformation_event':
  108. active_events = self.trasformation_events
  109. return active_events
  110. @api.one
  111. def confirm(self):
  112. if(self.confirm_event(self.get_active_events())):
  113. self.state = 'validated'
  114. else:
  115. raise Warning(
  116. _('There are no event associated with this work order'))
  117. def confirm_event(self, event_type):
  118. events = event_type.search([('state', '=', 'draft'),
  119. ('job_order', '=', self.id)])
  120. control = False
  121. for event in events:
  122. if event.farm.id != self.farm.id:
  123. raise Warning(
  124. _('The values of farm are diferent on order and events'))
  125. elif self.animal_type:
  126. if event.animal_type != self.animal_type:
  127. if self.event_type != 'medication':
  128. raise Warning(
  129. _('The values of animal type are diferent on order'
  130. ' and events'))
  131. if event.specie != self.specie:
  132. raise Warning(
  133. _('The values of specie are diferent on order and events'))
  134. else:
  135. control = event.confirm()
  136. if control is None:
  137. control = False
  138. return control