farrowing_event.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 FarrowingProblem(models.Model):
  8. _name = 'farm.farrowing.problem'
  9. name = fields.Char(string='Name', required=True, traslate=True)
  10. class FarrowingEvent(models.Model):
  11. _name = 'farm.farrowing.event'
  12. _inherit = {'farm.event.import.mixin': 'ImportedEventMixin_id'}
  13. _rec_name = 'animal'
  14. _auto = True
  15. live = fields.Integer(string='Live')
  16. stillborn = fields.Integer(string='Stillborn')
  17. mummified = fields.Integer(string='Mummified')
  18. dead = fields.Integer(string='Dead', compute='get_dead')
  19. problem = fields.Many2one(comodel_name='farm.farrowing.problem',
  20. string='Problem')
  21. female_cycle = fields.One2many(
  22. comodel_name='farm.farrowing.event_female_cycle',
  23. inverse_name='event', column1='cycle',
  24. string='Female Cycle')
  25. produced_group = fields.One2many(
  26. comodel_name='farm.farrowing.event_group',
  27. inverse_name='event', column1='animal_group', string='Produced Group')
  28. move = fields.Many2one(comodel_name='stock.move', string='Stock Move')
  29. @api.one
  30. def get_dead(self):
  31. self.dead = (self.stillborn or 0) + (self.mummified or 0)
  32. @api.one
  33. def confirm(self):
  34. if not self.is_compatible():
  35. raise Warning(
  36. _("Only females can be farrow"))
  37. if not self.is_ready():
  38. raise Warning(
  39. _("Only diagnosticated pregnant females can be farrow"))
  40. if self.dead == 0 and self.live == 0:
  41. raise Warning(
  42. _('no deads and no lives'))
  43. company = self.env['res.company'].search([
  44. (True, '=', True)])[0]
  45. journal = self.env['account.analytic.journal'].search([
  46. ('code', '=', 'FAR')])
  47. analytic_line_obj = self.env['account.analytic.line']
  48. farrowing_cycle_obj = self.env['farm.farrowing.event_female_cycle']
  49. farrrowing_animalGroup_obj = self.env['farm.farrowing.event_group']
  50. location_obj = self.env['farm.foster.locations']
  51. foster_locations = []
  52. for loc in self.animal.specie.foster_location:
  53. foster_locations.append(loc.id)
  54. print loc.location
  55. print loc.location.location_id
  56. print foster_locations
  57. print self.farm.id
  58. foster_location = location_obj.search(
  59. [('location.location_id.location_id', '=', self.farm.id),
  60. ('id', 'in', foster_locations),
  61. ],
  62. ).location
  63. print foster_location
  64. farrowing_cycle_obj.create({
  65. 'event': self.id,
  66. 'cycle': self.animal.cycles[-1].id})
  67. if self.live != 0:
  68. self.get_female_move(foster_location)
  69. new_group = self.get_produced_group()
  70. farrrowing_animalGroup_obj.create({
  71. 'event': self.id,
  72. 'animal_group': new_group[0].id
  73. })
  74. self.animal.current_cycle.update_state(self)
  75. tot_cost = 0
  76. for line in self.animal.account.line_ids:
  77. tot_cost = tot_cost + line.amount
  78. analytic_line_obj.create({
  79. 'name': 'Farrow Cost',
  80. 'date': self.timestamp,
  81. 'ref': 'farrow',
  82. 'amount': tot_cost,
  83. 'unit_amount': 1,
  84. 'account_id': new_group[0].account.id,
  85. 'general_account_id': company.feed_account.id,
  86. 'journal_id': journal.id,
  87. })
  88. analytic_line_obj.create({
  89. 'name': 'Farrow Cost',
  90. 'date': self.timestamp,
  91. 'ref': 'farrow',
  92. 'amount': -(tot_cost),
  93. 'unit_amount': 1,
  94. 'account_id': self.animal.account.id,
  95. 'general_account_id': company.feed_account.id,
  96. 'journal_id': journal.id,
  97. })
  98. super(FarrowingEvent, self).confirm()
  99. @api.one
  100. def get_female_move(self, foster_loc):
  101. moves_obj = self.env['stock.move']
  102. quants_obj = self.env['stock.quant']
  103. target_quant = quants_obj.search([
  104. ('lot_id', '=', self.animal.lot.lot.id),
  105. ('location_id', '=', self.animal.location.id),
  106. ])
  107. fem_move = moves_obj.create({
  108. 'name': 'foster-mother-' + self.animal.lot.lot.name,
  109. 'create_date': fields.Date.today(),
  110. 'date': self.timestamp,
  111. 'product_id': self.animal.lot.lot.product_id.id,
  112. 'product_uom_qty': 1,
  113. 'product_uom':
  114. self.animal.lot.lot.product_id.product_tmpl_id.uom_id.id,
  115. 'location_id': self.animal.location.id,
  116. 'location_dest_id': foster_loc.id,
  117. 'company_id': self.animal.farm.company_id.id, })
  118. for q in target_quant:
  119. q.reservation_id = fem_move.id
  120. fem_move.action_done()
  121. self.female_move = fem_move
  122. self.animal.location = foster_loc
  123. tags_obj = self.env['farm.tags']
  124. tag = tags_obj.search([
  125. ('name', '=', self.farm.name+'-mated')])
  126. tag.animal = [(3, self.animal.id)]
  127. new_tag = tags_obj.search([
  128. ('name', '=', self.farm.name + '-lact')])
  129. if len(new_tag) == 0:
  130. new_tag = tags_obj.create({'name': self.farm.name + '-lact',
  131. })
  132. self.animal.tags = [(6, 0, [new_tag.id, ])]
  133. @api.one
  134. def get_produced_group(self):
  135. animalGroup_obj = self.env['farm.animal.group']
  136. tags_obj = self.env['farm.tags']
  137. tags = []
  138. new_tag = tags_obj.search([
  139. ('name', '=', self.farm.name + '-lact')])
  140. if len(new_tag) == 0:
  141. new_tag = tags_obj.create({'name': self.farm.name + '-lact',
  142. })
  143. tags.append(new_tag.id)
  144. new_group = animalGroup_obj.create({
  145. 'specie': self.specie.id,
  146. 'breed': self.animal.breed.id,
  147. 'initial_location': self.animal.location.id,
  148. 'initial_quantity': self.live,
  149. 'farm': self.animal.farm.id,
  150. 'origin': 'raised',
  151. 'arrival_date': self.timestamp,
  152. 'tags': [(6, 0, tags)],
  153. })
  154. new_group.state = 'lactating'
  155. return new_group
  156. def is_compatible(self):
  157. if self.animal_type == 'female':
  158. return True
  159. else:
  160. return False
  161. def is_ready(self):
  162. if self.animal.current_cycle.state in ['pregnat', 'mated']:
  163. return True
  164. else:
  165. return False
  166. class FarrowingEventFemaleCycle(models.Model):
  167. _name = 'farm.farrowing.event_female_cycle'
  168. event = fields.Many2one(comodel_name='farm.farrowing.event',
  169. string='Farrowing Event', required=True,
  170. ondelete='RESTRICT')
  171. cycle = fields.Many2one('farm.animal.female_cycle', string='Female Cycle',
  172. required=True, ondelete='RESTRICT')
  173. class FarrowingEventAnimalGroup(models.Model):
  174. _name = 'farm.farrowing.event_group'
  175. _rec_name = 'animal_group'
  176. event = fields.Many2one(comodel_name='farm.farrowing.event',
  177. string='Farrowing Event', required=True,
  178. ondelete='RESTRICT')
  179. animal_group = fields.Many2one(comodel_name='farm.animal.group',
  180. string='Group', required=True,
  181. ondelete='RESTRICT')
  182. @api.multi
  183. def name_get(self):
  184. displayName = []
  185. for group in self:
  186. displayName.append((group.id, group.animal_group.number))
  187. return displayName