foster_event.py 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 FosterEvent(models.Model):
  8. _name = 'farm.foster.event'
  9. _inherit = {'farm.event.import.mixin': 'ImportedEventMixin_id'}
  10. _auto = True
  11. farrowing_group = fields.Many2one(comodel_name='farm.animal.group',
  12. string='Farrowing Group')
  13. quantity = fields.Integer(string='Fosters',
  14. help='If this quantity is negative it is'
  15. 'a Foster Out.')
  16. pair_female = fields.Many2one(comodel_name='farm.animal',
  17. string='Pair Female', required=True,
  18. domain=[('type', '=', 'female'),
  19. ('current_cycle', '!=', None), ])
  20. pair_event = fields.Many2one(comodel_name='farm.animal.female_cycle',
  21. string='Female Cycle')
  22. female_cycle = fields.Many2one(comodel_name='farm.animal.female_cycle',
  23. string='Female Cycle')
  24. move = fields.Many2one(comodel_name='stock.move', string='Female Cycle')
  25. @api.one
  26. def confirm(self):
  27. if not self.is_compatible():
  28. raise Warning(
  29. _("Only females can foster a group"))
  30. if not self.is_ready():
  31. raise Warning(
  32. _("Only lactating females can foster a group"))
  33. far_event = self.animal.current_cycle.farrowing_event
  34. self.farrowing_group = \
  35. far_event.event.produced_group.animal_group
  36. self.female_cycle = self.animal.current_cycle
  37. self.pair_event = self.pair_female.current_cycle
  38. self.trasform_group()
  39. super(FosterEvent, self).confirm()
  40. @api.one
  41. def trasform_group(self):
  42. incoming_group = \
  43. self.pair_event.farrowing_event.event.produced_group.animal_group
  44. if incoming_group.quantity < self.quantity:
  45. raise Warning(
  46. _('quantity is biger than incoming group quantity'))
  47. trans_eve_obj = self.env['farm.transformation.event']
  48. new_trans_ev = trans_eve_obj.create({
  49. 'animal_type': 'group',
  50. 'specie': self.specie.id,
  51. 'farm': self.farm.id,
  52. 'animal_group': incoming_group.id,
  53. 'timestamp': self.timestamp,
  54. 'from_location': incoming_group.location.id,
  55. 'to_animal_type': 'group',
  56. 'to_location': self.animal.location.id,
  57. 'quantity': self.quantity,
  58. 'to_animal_group': self.farrowing_group.id,
  59. })
  60. new_trans_ev.confirm()
  61. self.move = new_trans_ev.move
  62. foster_event_obj = self.env['farm.foster.event']
  63. foster_event_obj.create({
  64. 'aniaml': self.pair_female.id,
  65. 'farm': self.farm.id,
  66. 'state': 'validated',
  67. 'animal_type': 'female',
  68. 'farrowing_group': incoming_group.id,
  69. 'quantity': self.quantity,
  70. 'pair_female': self.animal.id,
  71. 'pair_event': self.female_cycle.id,
  72. 'female_cycle': self.pair_event.id,
  73. 'move': new_trans_ev.move.id})
  74. def is_ready(self):
  75. if self.animal.current_cycle.state == 'lactating' and \
  76. self.pair_female.current_cycle.state == 'lactating':
  77. return True
  78. else:
  79. return False
  80. def is_compatible(self):
  81. if self.animal.type == 'female' and self.pair_female.type == 'female':
  82. return True
  83. else:
  84. return False