insemination_event.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. class InseminationEvent(models.Model):
  8. _name = 'farm.insemination.event'
  9. _inherit = {'farm.event.import.mixin': 'ImportedEventMixin_id'}
  10. _rec_name = 'animal'
  11. _auto = True
  12. dose_bom = fields.Many2one(comodel_name='mrp.bom', string='Dose',
  13. domain=[(('semen_dose', '=', True))])
  14. dose_product = fields.Many2one(comodel_name='product.product',
  15. string='Dose product', required=True)
  16. dose_lot = fields.Many2one(comodel_name='stock.production.lot',
  17. string='Dose lot')
  18. female_cycle = fields.Many2one(comodel_name='farm.animal.female_cycle',
  19. string='Female cycle')
  20. move = fields.Many2one(comodel_name='stock.move', string='Stock move',
  21. readonly=True)
  22. @api.one
  23. def confirm(self):
  24. if not self.is_compatible():
  25. raise Warning(
  26. _("Only females can be bred"))
  27. current_cycle = self.animal.current_cycle
  28. if (not current_cycle or current_cycle.diagnosis_events or
  29. current_cycle.farrowing_event):
  30. self.create_new_female_cycle()
  31. elif not self.female_cycle:
  32. self.female_cycle = self.animal.current_cycle
  33. if not self.is_ready():
  34. raise Warning(
  35. _("female's cycle is not compatible to be bred"))
  36. self.get_event_move()
  37. self.animal.update_state()
  38. self.female_cycle.update_state(self)
  39. tags_obj = self.env['farm.tags']
  40. tag = tags_obj.search([
  41. ('name', '=', self.farm.name +'-unmated')])
  42. tag.animal = [(3, self.animal.id)]
  43. new_tag = tags_obj.search([
  44. ('name', '=', self.farm.name + '-mated')])
  45. if len(new_tag) == 0:
  46. new_tag = tags_obj.create({'name': self.farm.name + '-mated',
  47. })
  48. self.animal.tags = [(6, 0, [new_tag.id, ])]
  49. super(InseminationEvent, self).confirm()
  50. @api.onchange('dose_bom')
  51. def onchange_specie(self):
  52. product_obj = self.env['product.product']
  53. dose_pro = product_obj.search(
  54. [('product_tmpl_id', '=', self.dose_bom.product_tmpl_id.id)])
  55. product_ids = []
  56. for product in dose_pro:
  57. product_ids.append(product.id)
  58. return {'domain': {
  59. 'dose_product': [('id', 'in', product_ids)]}}
  60. @api.one
  61. def get_event_move(self):
  62. moves_obj = self.env['stock.move']
  63. quants_obj = self.env['stock.quant']
  64. target_quant = quants_obj.search([
  65. ('product_id', '=', self.dose_product.id),
  66. ('lot_id', '=', self.dose_lot.id),
  67. ('qty', '>', 1),
  68. ])
  69. if not target_quant:
  70. raise Warning(
  71. _('semen dose no avairable'))
  72. new_move = moves_obj.create({
  73. 'name': 'ins' + self.dose_lot.name,
  74. 'create_date': fields.Date.today(),
  75. 'date': self.timestamp,
  76. 'product_id': self.dose_product.id,
  77. 'product_uom_qty': 1,
  78. 'product_uom': self.dose_product.uom_id.id,
  79. 'location_id': target_quant[0].location_id.id,
  80. 'location_dest_id': self.specie.consumed_feed_location.id,
  81. 'company_id': self.animal.initial_location.company_id.id,
  82. 'origin': self.job_order.name,
  83. })
  84. for q in target_quant:
  85. q.reservation_id = new_move.id
  86. new_move.action_done()
  87. self.move = new_move
  88. def is_compatible(self):
  89. if self.animal_type == 'female':
  90. return True
  91. else:
  92. return False
  93. def is_ready(self):
  94. if self.female_cycle.state == 'unmated':
  95. return True
  96. elif self.female_cycle.state == 'mated':
  97. return True
  98. else:
  99. return False
  100. @api.one
  101. def create_new_female_cycle(self):
  102. female_clicle_obj = self.env['farm.animal.female_cycle']
  103. self.female_cycle = female_clicle_obj.create(
  104. {'animal': self.animal.id, })