feed_inventory.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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
  6. INVENTORY_STATES = [
  7. ('draft', 'Draft'),
  8. ('validated', 'Validated'),
  9. ('cancel', 'Cancelled'),
  10. ]
  11. class FeedInventoryMixin(models.Model):
  12. _name = 'farm.feed.inventory.mixin'
  13. _auto = False
  14. specie = fields.Many2one(comodel_name='farm.specie', string='Specie',
  15. required=True, select=True)
  16. location = fields.Many2one(comodel_name='stock.location', string='Silo',
  17. required=True, domain=[('silo', '=', True)])
  18. dest_locations = fields.Many2many(comodel_name='farm.feed.stock.location',
  19. inverse_name='inventory',
  20. column1='location',
  21. string='Location to feed')
  22. timestamp = fields.Datetime(string='Date & Time', required=True,
  23. default=fields.Datetime.now())
  24. uom = fields.Many2one(comodel_name='product.uom', string='UOM')
  25. quantity = fields.Float(string='Quantity', digits=(16, 2), required=True)
  26. feed_events = fields.One2many(comodel_name='farm.feed.event',
  27. inverse_name='feed_inventory',
  28. string='Feed Events', readonly=True)
  29. state = fields.Selection(selection=INVENTORY_STATES, string='States',
  30. required=True, readonly=True, select=True,
  31. default='draft')
  32. class FeedInventoryLocation(models.Model):
  33. _name = 'farm.feed.stock.location'
  34. inventory = fields.Selection(string='Inventory',
  35. selection='get_inventory', required=True,
  36. select=True)
  37. location = fields.Many2one(comodel_name='stock.location',
  38. string='Location', required=True, select=True)
  39. def get_inventory(self):
  40. irModel_obj = self.env['ir.model']
  41. varModels = irModel_obj.search([
  42. ('model', 'in', ['farm.feed.inventory',
  43. 'farm.feed.provisional_inventory']), ])
  44. return [(m.model, m.name) for m in varModels]
  45. class FeedInventory(models.Model):
  46. _name = 'farm.feed.inventory'
  47. _inherit = {'farm.feed.inventory.mixin': 'FeedInventoryMixin_id'}
  48. _auto = True
  49. prev_inventory = fields.Many2one(comodel_name='farm.feed.inventory',
  50. string='Previous Inventory',
  51. readonly=True)
  52. class FeedProvisionalInventory(models.Model):
  53. _name = 'farm.feed.provisional_inventory'
  54. _inherit = {'farm.feed.inventory.mixin': 'FeedInventoryMixin_id'}
  55. _auto = True
  56. prev_inventory_date = fields.Date(string='Previous Inventory Date',
  57. readonly=True,
  58. computed='get_previous_inventory_date')
  59. inventory = fields.Many2one(comodel_name='farm.feed.inventory',
  60. string='Inventory', readonly=True)
  61. def get_previous_inventory_date(self):
  62. inventory_obj = self.env['farm.feed.inventory']
  63. prev_inventories = inventory_obj.search([
  64. ('location', '=', self.location.id),
  65. ('timestamp', '<', self.timestamp),
  66. ('state', '=', 'validated'), ],
  67. order=[('timestamp', 'DESC'), ],
  68. limit=1)
  69. if not prev_inventories:
  70. return None
  71. prev_prov_inventories = self.search([
  72. ('location', '=', self.location.id),
  73. ('timestamp', '<', self.timestamp),
  74. ('timestamp', '>', prev_inventories[0].timestamp),
  75. ('state', '=', 'validated'),
  76. ],
  77. order=[('timestamp', 'DESC'), ], limit=1)
  78. if prev_prov_inventories:
  79. return prev_prov_inventories[0].timestamp.date()
  80. return prev_inventories[0].timestamp.date()
  81. class FeedAnimalLocationDate(models.Model):
  82. _name = 'farm.feed.animal_location_date'
  83. _order = [('location', 'ASC'), ('date', 'DESC'), ]
  84. animal_type = fields.Selection([
  85. ('male', 'Male'),
  86. ('female', 'Female'),
  87. ('individual', 'Individual'),
  88. ('group', 'Group'),
  89. ], "Animal Type", required=True, readonly=True, select=True)
  90. animal = fields.Many2one(comodel_name='farm.animal', string='Animal')
  91. animal_group = fields.Many2one(comodel_name='farm.animal.group',
  92. string='Group', select=True)
  93. location = fields.Many2one(comodel_name='stock.location',
  94. string='Location fed', select=True)
  95. animale_qty = fields.Integer(string='Num. Animals')
  96. date = fields.Date('Date', select=True)
  97. consumed_qty_animal = fields.Float(string='Consumed Qty, per Animal',
  98. digits=(16, 2), select=True)
  99. consumed_qty = fields.Float(string='Consumed Qty', digits=(16, 2))
  100. inventory_qty = fields.Integer(string='Inventoryes',
  101. help='Number of Inventories which include'
  102. 'this date.')