feed_abstract_event.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. class FeedEventMixin(models.Model):
  7. _name = 'farm.event.feed_mixin'
  8. _inherit = {'farm.event': 'AbstractEvent_id'}
  9. _auto = False
  10. location = fields.Many2one(comodel_name='stock.location',
  11. string='Location',
  12. domain=[('usage', '=', 'internal')],
  13. required=True)
  14. quantity = fields.Integer(string='Num. of animals', compute='get_quantity',
  15. store=True)
  16. feed_location = fields.Many2one(comodel_name='stock.location',
  17. string='Feed Source', required=True,
  18. domain=[('usage', '=', 'internal'), ])
  19. feed_product = fields.Many2one(comodel_name='product.product',
  20. string='Feed')
  21. feed_lot = fields.Many2one(comodel_name='stock.production.lot',
  22. string='Feed Lot', required=True)
  23. uom = fields.Many2one(comodel_name='product.uom', string='UOM',
  24. required=True)
  25. feed_quantity = fields.Float(string='Comsumed Cuantity', required=True,
  26. digits=(4, 2), default=1)
  27. start_date = fields.Date(string='Start Date',
  28. default=fields.Date.today(),
  29. help='Start date of the period in'
  30. 'which the given quantity of product was'
  31. 'consumed.')
  32. end_date = fields.Date(string='End Date', default=fields.Date.today(),
  33. help='End date of the period in which the given'
  34. 'quantity of product was consumed. It is the date'
  35. 'of event\'s timestamp.')
  36. move = fields.Many2one(comodel_name='stock.move', string='Stock Move')
  37. @api.onchange('feed_product')
  38. def onchange_feed(self):
  39. self.uom = self.feed_product.product_tmpl_id.uom_id
  40. @api.onchange('animal')
  41. def onchange_animal(self):
  42. self.location = self.animal.location
  43. @api.onchange('animal_group')
  44. def onchange_group(self):
  45. self.location = self.animal_group.location
  46. @api.one
  47. def get_quantity(self):
  48. if self.animal_type == 'group':
  49. self.quantity = self.animal_group.quantity
  50. else:
  51. self.quantity = 1
  52. @api.multi
  53. def consume_feed(self, name, end_date, product, lot, specie, origin,
  54. qty, uom):
  55. quants_obj = self.env['stock.quant']
  56. moves_obj = self.env['stock.move']
  57. target_quant = quants_obj.search([
  58. ('lot_id', '=', lot.id),
  59. ('location_id', '=', origin.id)])
  60. new_move = moves_obj.create({
  61. 'name': name,
  62. 'create_date': fields.Date.today(),
  63. 'date': end_date,
  64. 'product_id': product.id,
  65. 'product_uom_qty': qty,
  66. 'product_uom': uom.id,
  67. 'location_id': origin.id,
  68. 'location_dest_id': specie.consumed_feed_location.id,
  69. 'company_id': origin.company_id.id,
  70. })
  71. for q in target_quant:
  72. q.reservation_id = new_move.id
  73. new_move.action_done()