feed_event.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 datetime import datetime
  7. DFORMAT = "%Y-%m-%d"
  8. class FeedEvent(models.Model):
  9. _name = 'farm.feed.event'
  10. _inherit = {'farm.event.feed_mixin': 'FeedEventMixin_id'}
  11. _auto = True
  12. animal_type = fields.Selection([
  13. ('male', 'Male'), ('female', 'Female'),
  14. ('individual', 'Individual'), ('group', 'Group'),
  15. ], string="Animal Type", select=True)
  16. feed_quantity_animal_day = fields.Float(string='Qty. per Animal Day',
  17. digits=(16, 4),
  18. compute='get_feed_per_day')
  19. feed_inventory = fields.Many2one(comodel_name='farm.feed.inventory',
  20. string='Inventory')
  21. feed_inventory = fields.Selection(string='Inventory',
  22. selection='get_inventory',
  23. readonly=True, select=True,
  24. help='The inventory that generated this'
  25. 'event automatically.')
  26. @api.model
  27. @api.returns('self', lambda value: value.id)
  28. def create(self, vals):
  29. res = super(FeedEvent, self).create(vals)
  30. if len(res.move) == 0:
  31. quants_obj = self.env['stock.quant']
  32. moves_obj = self.env['stock.move']
  33. target_quant = quants_obj.search([
  34. ('lot_id', '=', res.feed_lot.id),
  35. ('location_id', '=', res.feed_location.id)])
  36. new_move = moves_obj.create({
  37. 'name': res.job_order.name+'-'+res.lot.name+'-mov',
  38. 'create_date': fields.Date.today(),
  39. 'date': res.start_date,
  40. 'product_id': res.feed_product.id,
  41. 'product_qty': res.feed_quantity,
  42. 'product_uom': res.uom.id,
  43. 'location_id': res.feed_location.id,
  44. 'location_dest_id': res.location.id,
  45. 'company_id': res.location.company_id.id,
  46. 'origin': res.job_order.name,
  47. })
  48. for q in target_quant:
  49. q.reservation_id = new_move.id
  50. res.move = new_move
  51. new_move.action_done()
  52. return res
  53. @api.multi
  54. def get_feed_per_day(self):
  55. for res in self:
  56. if res.animal or res.animal_group:
  57. if res.feed_quantity and res.start_date and res.end_date:
  58. days = (datetime.strptime(res.end_date, DFORMAT) -
  59. datetime.strptime(res.start_date, DFORMAT)).days
  60. if days > 0:
  61. qty = 1
  62. res.feed_quantity_animal_day = \
  63. (res.feed_quantity/res.quantity)/days
  64. @api.onchange('move')
  65. @api.multi
  66. def onchange_move(self):
  67. for res in self:
  68. if len(res.move) > 0:
  69. res.feed_product = res.move.product_id
  70. res.uom = res.move.product_uom
  71. res.feed_quantity = res.move.product_qty
  72. res.farm = res.move.location_dest_id.location_id.location_id
  73. res.feed_location = res.move.location_id
  74. res.location = res.move.location_dest_id
  75. if len(res.move.reserved_quant_ids) > 0:
  76. res.feed_lot = res.move.reserved_quant_ids[0].lot_id
  77. else:
  78. if len(res.move.quant_ids) > 0:
  79. res.feed_lot = res.move.quant_ids[-1].lot_id
  80. @api.one
  81. def confirm(self):
  82. quants_obj = self.env['stock.quant']
  83. if self.animal_type == 'group':
  84. self.animal_group.feed_quantity += self.feed_quantity
  85. self.set_cost(
  86. self.animal_group.account, self.feed_lot, self.feed_quantity)
  87. else:
  88. self.animal.consumed_feed += self.feed_quantity
  89. self.set_cost(
  90. self.animal.account, self.feed_lot, self.feed_quantity)
  91. self.consume_feed('consume_feed', self.end_date, self.feed_product,
  92. self.feed_lot, self.specie, self.location,
  93. self.feed_quantity, self.uom)
  94. super(FeedEvent, self).confirm()
  95. @api.one
  96. def set_cost(self, account, lot, qty):
  97. company = self.env['res.company'].search([
  98. (True, '=', True)])[0]
  99. journal = self.env['account.analytic.journal'].search([
  100. ('code', '=', 'FAR')])
  101. analytic_line_obj = self.env['account.analytic.line']
  102. stock_move_obj = self.env['stock.move']
  103. cost = 0
  104. if lot.unit_cost and lot.unit_cost > 0:
  105. cost = lot.unit_cost * qty
  106. else:
  107. quants_obj = self.env['stock.quant']
  108. quants = quants_obj.search([
  109. ('lot_id', '=', self.feed_lot.id)])
  110. ids = []
  111. for q in quants:
  112. ids.append(q.id)
  113. moves = stock_move_obj.search([
  114. ('quant_ids', 'in', ids),
  115. ('picking_id', '!=', False)])
  116. amount = 0.0
  117. raw_qty = 0
  118. if len(moves) != 0:
  119. for move in moves:
  120. if move.price_unit > 0:
  121. amount += move.price_unit * move.product_qty
  122. raw_qty += move.product_qty
  123. if raw_qty > 0:
  124. unit_price = amount/raw_qty
  125. cost += qty * unit_price
  126. if cost == 0:
  127. prod_tmpl = self.feed_product.product_tmpl_id
  128. cost = prod_tmpl.standard_price * qty
  129. analytic_line_obj.create({
  130. 'name': self.job_order.name,
  131. 'date': self.end_date,
  132. 'ref': 'feed',
  133. 'amount': -(cost),
  134. 'unit_amount': qty,
  135. 'account_id': account.id,
  136. 'general_account_id': company.feed_account.id,
  137. 'journal_id': journal.id,
  138. })
  139. def get_inventory(self):
  140. irModel_obj = self.env['ir.model']
  141. models = irModel_obj.search([
  142. ('model', 'in', ['farm.feed.inventory',
  143. 'farm.feed.provisional_inventory']), ])
  144. return [('', '')] + [(m.model, m.name) for m in models]