removal_event.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 RemovalType(models.Model):
  8. _name = 'farm.removal.type'
  9. name = fields.Char(string='Name', required=True, traslate=True)
  10. class RemovalReason(models.Model):
  11. _name = 'farm.removal.reason'
  12. name = fields.Char(string='Name', required=True, traslate=True)
  13. class RemovalEvent(models.Model):
  14. _name = 'farm.removal.event'
  15. _inherit = {'farm.event': 'AbstractEvent_id'}
  16. _auto = True
  17. from_location = fields.Many2one(comodel_name='stock.location',
  18. string='Origin', required=True,
  19. domain=[('silo', '=', False), ])
  20. quantity = fields.Integer(string='Quantity', required=True,
  21. default=1)
  22. removal_type = fields.Many2one(comodel_name='farm.removal.type',
  23. string='Type')
  24. reason = fields.Many2one(comodel_name='farm.removal.reason',
  25. string='Reason')
  26. move = fields.Many2one(comodel_name='stock.move', string='Stock Move')
  27. specific_lot = fields.Many2one(comodel_name='stock.production.lot',
  28. string='Specific lot')
  29. @api.multi
  30. def copy(self, default={}):
  31. for res in self:
  32. default['animal_type'] = res.animal_type
  33. default['specie'] = res.specie.id
  34. default['removal_type'] = res.removal_type.id
  35. default['reason'] = res.reason.id
  36. default['animal'] = False
  37. default['animal_group'] = False
  38. default['move'] = False
  39. return super(RemovalEvent, self).copy(default)
  40. @api.one
  41. @api.onchange('animal', 'animal_group')
  42. def get_from_location(self):
  43. if self.animal_type == 'group':
  44. self.from_location = self.animal_group.location
  45. self.quantity = self.animal_group.quantity
  46. else:
  47. self.from_location = self.animal.location
  48. @api.one
  49. def confirm(self):
  50. if not self.is_compatible_quant():
  51. return False
  52. elif not self.is_compatible_to_location():
  53. return False
  54. ani = None
  55. party = None
  56. if self.animal_type == 'group':
  57. self.remove_group()
  58. else:
  59. self.remove_animal()
  60. if self.animal:
  61. ani = self.animal.id
  62. else:
  63. party = self.animal_group.id
  64. self.env['farm.move.event'].create({
  65. 'animal_type': self.animal_type,
  66. 'specie': self.specie.id,
  67. 'farm': self.farm.id,
  68. 'animal': ani,
  69. 'animal_group': party,
  70. 'timestamp': self.timestamp,
  71. 'from_location': self.from_location.id,
  72. 'to_location': self.move.location_dest_id.id,
  73. 'quantity': self.quantity,
  74. 'unit_price': 1,
  75. 'move': self.move.id,
  76. })
  77. super(RemovalEvent, self).confirm()
  78. @api.one
  79. def remove_group(self):
  80. moves_obj = self.env['stock.move']
  81. quants_obj = self.env['stock.quant']
  82. scrap_loc = self.animal_group.specie.removed_location
  83. if len(self.specific_lot) == 0:
  84. if len(self.animal_group.lot) < 3:
  85. lot = self.animal_group.lot[0].lot
  86. else:
  87. lot = self.animal_group.lot[2].lot
  88. else:
  89. lot = self.specific_lot
  90. target_quant = quants_obj.search([
  91. ('lot_id', '=', lot.id),
  92. ('location_id', '=', self.from_location.id),
  93. ])
  94. product_uom = \
  95. lot.product_id.product_tmpl_id.uom_id.id
  96. new_move = moves_obj.create({
  97. 'name': 'remove-' + self.animal_group.number,
  98. 'create_date': fields.Date.today(),
  99. 'date': self.timestamp,
  100. 'product_id': lot.product_id.id,
  101. 'product_uom_qty': self.quantity,
  102. 'product_uom': product_uom,
  103. 'location_id': self.from_location.id,
  104. 'location_dest_id': scrap_loc.id,
  105. 'company_id': self.animal_group.initial_location.company_id.id,
  106. 'origin': self.animal_group.number,
  107. })
  108. for q in target_quant:
  109. q.reservation_id = new_move.id
  110. new_move.action_done()
  111. self.move = new_move
  112. self.animal_group.quantity -= self.quantity
  113. if self.animal_group.quantity < 1:
  114. self.animal_group.removal_date = self.timestamp
  115. tags_obj = self.env['farm.tags']
  116. for tag in self.animal_group.tags:
  117. tag.animal_group = [(3, self.animal_group.id)]
  118. new_tag = tags_obj.search([
  119. ('name', '=', 'Removed Animals')])
  120. if len(new_tag) == 0:
  121. new_tag = tags_obj.create(
  122. {'name': 'Removed Animals', })
  123. self.animal_group.tags = [(6, 0, [new_tag.id, ])]
  124. self.animal_group.state = 'sold'
  125. @api.one
  126. def remove_animal(self):
  127. moves_obj = self.env['stock.move']
  128. quants_obj = self.env['stock.quant']
  129. scrap_loc = self.env['stock.location'].search([
  130. ('scrap_location', '=', True)])[0]
  131. target_quant = quants_obj.search([
  132. ('lot_id', '=', self.animal.lot.lot.id),
  133. ('location_id', '=', self.from_location.id),
  134. ])
  135. product_uom = \
  136. self.animal.lot.lot.product_id.product_tmpl_id.uom_id.id
  137. new_move = moves_obj.create({
  138. 'name': 'remove-' + self.animal.number,
  139. 'create_date': fields.Date.today(),
  140. 'date': self.timestamp,
  141. 'product_id': self.animal.lot.lot.product_id.id,
  142. 'product_uom_qty': self.quantity,
  143. 'product_uom': product_uom,
  144. 'location_id': self.from_location.id,
  145. 'location_dest_id': scrap_loc.id,
  146. 'company_id': self.animal.location.company_id.id,
  147. 'origin': self.animal.lot.lot.name,
  148. })
  149. for q in target_quant:
  150. q.reservation_id = new_move.id
  151. new_move.action_done()
  152. self.animal.removal_date = self.timestamp
  153. self.animal.removal_reason = self.reason
  154. self.animal.active = False
  155. self.move = new_move
  156. def is_compatible_quant(self):
  157. if self.animal_type == 'individual' or \
  158. self.animal_type == 'male' or \
  159. self.animal_type == 'female':
  160. if self.quantity != 1 or self.quantity < 1:
  161. raise Warning(
  162. _('Quantity no compatible'))
  163. elif self.quantity > self.animal_group.quantity or self.quantity < 1:
  164. raise Warning(
  165. _('Quantity no compatible'))
  166. return True
  167. def is_compatible_to_location(self):
  168. if self.animal_type == 'group':
  169. if self.animal_group.location.id != self.from_location.id:
  170. raise Warning(
  171. _('the origin is different from the location of'
  172. ' the group'))
  173. else:
  174. if self.animal.location.id != self.from_location.id:
  175. raise Warning(
  176. _('the origin is different from the location of'
  177. ' the animal'))
  178. return True