weaning_event.py 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 WeaningEvent(models.Model):
  8. _name = 'farm.weaning.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='Quantity', compute='get_quantity',
  14. store=True)
  15. female_to_location = fields.Many2one(comodel_name='stock.location',
  16. string='Female Destination',
  17. required=True,
  18. domain=[('usage', '=', 'internal'),
  19. ('silo', '=', False)])
  20. weaned_to_location = fields.Many2one(comodel_name='stock.location',
  21. string='Weaned Destination',
  22. required=True,
  23. domain=[('usage', '=', 'transit'),
  24. ('silo', '=', False)])
  25. weared_group = fields.Many2one(comodel_name='farm.animal.group',
  26. string='Weaned group',
  27. help='Group in which weaned animals should'
  28. 'be added to. If left blank they will keep'
  29. 'the same group.')
  30. female_cycle = fields.One2many(
  31. comodel_name='farm.weaning.event_female_cycle',
  32. inverse_name='event', column1='cycle', string='Female Cicle',
  33. readonly=True)
  34. female_move = fields.Many2one(comodel_name='stock.move',
  35. string='Female Stock Move')
  36. lost_move = fields.Many2one(comodel_name='stock.move',
  37. string='Lost Stock Move')
  38. weared_move = fields.Many2one(comodel_name='stock.move',
  39. string='Weared Stock Move')
  40. transformation_event = fields.Many2one(
  41. comodel_name='farm.transformation.event',
  42. string='Transformation Event')
  43. @api.model
  44. @api.returns('self', lambda value: value.id)
  45. def create(self, vals):
  46. res = super(WeaningEvent, self).create(vals)
  47. res.get_quantity()
  48. return res
  49. @api.one
  50. @api.onchange('animal')
  51. def onchange_animal(self):
  52. c_c = self.animal.current_cycle
  53. self.weared_group = \
  54. c_c.farrowing_event.event.produced_group.animal_group
  55. @api.one
  56. def confirm(self):
  57. if not self.is_compatible():
  58. raise Warning(
  59. _("Only females can wean a group"))
  60. if not self.is_ready():
  61. raise Warning(
  62. _("Only lactating females can wean a group"))
  63. company = self.env['res.company'].search([
  64. (True, '=', True)])[0]
  65. journal = self.env['account.analytic.journal'].search([
  66. ('code', '=', 'FAR')])
  67. analytic_line_obj = self.env['account.analytic.line']
  68. far_event = self.animal.current_cycle.farrowing_event
  69. self.farrowing_group = \
  70. far_event.event.produced_group.animal_group
  71. wean_fem_cy_obj = self.env['farm.weaning.event_female_cycle']
  72. wean_fem_cy_obj.create({
  73. 'event': self.id,
  74. 'cycle': self.animal.current_cycle.id, })
  75. self.get_female_move()
  76. if self.farrowing_group.quantity > 0:
  77. if self.farrowing_group == self.weared_group:
  78. self.move_group()
  79. else:
  80. self.trasform_group()
  81. self.animal.current_cycle.update_state(self)
  82. self.farrowing_group.state = 'transition'
  83. tot_cost = 0
  84. for line in self.animal.account.line_ids:
  85. tot_cost = tot_cost + line.amount
  86. analytic_line_obj.create({
  87. 'name': 'weaning Cost',
  88. 'date': self.timestamp,
  89. 'ref': 'farrow',
  90. 'amount': tot_cost,
  91. 'unit_amount': 1,
  92. 'account_id': self.weared_group.account.id,
  93. 'general_account_id': company.feed_account.id,
  94. 'journal_id': journal.id,
  95. })
  96. analytic_line_obj.create({
  97. 'name': 'weaning Cost',
  98. 'date': self.timestamp,
  99. 'ref': 'farrow',
  100. 'amount': -(tot_cost),
  101. 'unit_amount': 1,
  102. 'account_id': self.animal.account.id,
  103. 'general_account_id': company.feed_account.id,
  104. 'journal_id': journal.id,
  105. })
  106. self.animal.current_cycle.update_state(self)
  107. super(WeaningEvent, self).confirm()
  108. @api.one
  109. def move_group(self):
  110. moves_obj = self.env['stock.move']
  111. quants_obj = self.env['stock.quant']
  112. f_g = self.farrowing_group
  113. target_quant = quants_obj.search([
  114. ('lot_id', '=', f_g.lot.lot.id),
  115. ('location_id', '=', f_g.location.id),
  116. ])
  117. f_g_move = moves_obj.create({
  118. 'name': 'wean-' + f_g.number,
  119. 'create_date': fields.Date.today(),
  120. 'date': self.timestamp,
  121. 'product_id': f_g.lot.lot.product_id.id,
  122. 'product_uom_qty': self.quantity,
  123. 'product_uom':
  124. f_g.lot.lot.product_id.product_tmpl_id.uom_id.id,
  125. 'location_id': f_g.location.id,
  126. 'location_dest_id': self.weaned_to_location.id,
  127. 'company_id': self.animal.farm.company_id.id, })
  128. for q in target_quant:
  129. q.reservation_id = f_g_move.id
  130. f_g_move.action_done()
  131. self.weared_move = f_g_move
  132. f_g.location = self.weaned_to_location
  133. tags_obj = self.env['farm.tags']
  134. tag = tags_obj.search([
  135. ('name', '=', self.farm.name + '-lact')])
  136. tag.animal_group = [(3, self.farrowing_group.id)]
  137. new_tag = tags_obj.search([
  138. ('name', '=', self.farm.name + '-transi')])
  139. if len(new_tag) == 0:
  140. new_tag = tags_obj.create({'name': self.farm.name + '-transi',
  141. })
  142. self.farrowing_group.tags = [(6, 0, [new_tag.id, ])]
  143. @api.one
  144. def trasform_group(self):
  145. if self.quantity == self.farrowing_group.quantity:
  146. self.get_female_move()
  147. trans_eve_obj = self.env['farm.transformation.event']
  148. new_trans_ev = trans_eve_obj.create({
  149. 'animal_type': 'group',
  150. 'specie': self.specie.id,
  151. 'farm': self.farm.id,
  152. 'animal_group': self.farrowing_group.id,
  153. 'timestamp': self.timestamp,
  154. 'from_location': self.farrowing_group.location.id,
  155. 'to_animal_type': 'group',
  156. 'to_location': self.weaned_to_location.id,
  157. 'quantity': self.quantity,
  158. 'to_animal_group': self.weared_group.id,
  159. })
  160. new_trans_ev.confirm()
  161. self.transformation_event = new_trans_ev
  162. self.weared_move = new_trans_ev.move
  163. @api.one
  164. def get_female_move(self):
  165. moves_obj = self.env['stock.move']
  166. quants_obj = self.env['stock.quant']
  167. target_quant = quants_obj.search([
  168. ('lot_id', '=', self.animal.lot.lot.id),
  169. ('location_id', '=', self.animal.location.id),
  170. ])
  171. fem_move = moves_obj.create({
  172. 'name': 'wean-mother-' + self.animal.lot.lot.name,
  173. 'create_date': fields.Date.today(),
  174. 'date': self.timestamp,
  175. 'product_id': self.animal.lot.lot.product_id.id,
  176. 'product_uom_qty': 1,
  177. 'product_uom':
  178. self.animal.lot.lot.product_id.product_tmpl_id.uom_id.id,
  179. 'location_id': self.animal.location.id,
  180. 'location_dest_id': self.female_to_location.id,
  181. 'company_id': self.animal.farm.company_id.id, })
  182. for q in target_quant:
  183. q.reservation_id = fem_move.id
  184. fem_move.action_done()
  185. self.female_move = fem_move
  186. self.animal.location = self.female_to_location
  187. tags_obj = self.env['farm.tags']
  188. tag = tags_obj.search([
  189. ('name', '=', self.farm.name+'-lact')])
  190. tag.animal = [(3, self.animal.id)]
  191. new_tag = tags_obj.search([
  192. ('name', '=', self.farm.name + '-unmated')])
  193. if len(new_tag) == 0:
  194. new_tag = tags_obj.create({'name': self.farm.name + '-unmated',
  195. })
  196. self.animal.tags = [(6, 0, [new_tag.id, ])]
  197. @api.one
  198. def get_farrowing_group(self):
  199. self.farrowing_group = \
  200. self.animal.current_cycle.farrowing_event.event.produced_group
  201. def is_compatible(self):
  202. if self.animal_type == 'female':
  203. return True
  204. else:
  205. return False
  206. def is_ready(self):
  207. if self.animal.current_cycle.state == 'lactating':
  208. return True
  209. else:
  210. return False
  211. @api.one
  212. def get_quantity(self):
  213. far_event = self.animal.current_cycle.farrowing_event
  214. farrowing_group = far_event.event.produced_group.animal_group
  215. self.quantity = farrowing_group.quantity
  216. class WearingEventFemaleCycle(models.Model):
  217. _name = 'farm.weaning.event_female_cycle'
  218. event = fields.Many2one(comodel_name='farm.weaning.event',
  219. string='Wearing Event', required=True,
  220. ondelete='RESTRICT')
  221. cycle = fields.Many2one(comodel_name='farm.animal.female_cycle',
  222. string='Female Cycle', required=True,
  223. ondelete='RESTRICT')