medication_event.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 MedicationEvent(models.Model):
  7. _name = 'farm.medication.event'
  8. _inherit = {'farm.event.feed_mixin': 'FeedEventMixin_id'}
  9. _auto = True
  10. medication_in_feed = fields.Boolean(string='Medication in feed',
  11. default=False)
  12. medicated_feed = fields.Many2one(string='Medicated Feed',
  13. comodel_name='product.product')
  14. feed_product_uom_category = fields.Many2one(
  15. comodel_name='product.uom.categ', string='Feed Uom Category',
  16. readonly=True)
  17. @api.one
  18. def confirm(self):
  19. if not self.medication_in_feed:
  20. quants_obj = self.env['stock.quant']
  21. moves_obj = self.env['stock.move']
  22. target_quant = quants_obj.search([
  23. ('lot_id', '=', self.feed_lot.id),
  24. ('location_id', '=', self.feed_location.id)])
  25. medication_cost = 0
  26. for tq in target_quant:
  27. medication_cost = tq.cost
  28. if len(self.move) == 0:
  29. new_move = moves_obj.create({
  30. 'name': self.job_order.name+'-'+self.lot.name+'-mov',
  31. 'create_date': fields.Date.today(),
  32. 'date': self.start_date,
  33. 'product_id': self.feed_product.id,
  34. 'product_uom_qty': self.feed_quantity,
  35. 'product_uom': self.uom.id,
  36. 'location_id': self.feed_location.id,
  37. 'location_dest_id': self.location.id,
  38. 'company_id': self.location.company_id.id,
  39. 'origin': self.job_order.name,
  40. })
  41. self.move = new_move
  42. for q in target_quant:
  43. q.reservation_id = new_move.id
  44. new_move.action_done()
  45. self.consume_feed('consume_feed', self.end_date, self.feed_product,
  46. self.feed_lot, self.specie, self.location,
  47. self.feed_quantity, self.uom)
  48. if self.animal_type == 'group':
  49. account = self.animal_group.account
  50. else:
  51. account = self.animal.account
  52. self.set_cost(account, medication_cost, self.feed_quantity)
  53. super(MedicationEvent, self).confirm()
  54. @api.multi
  55. def set_cost(self, account, cost, qty):
  56. company = self.env['res.company'].search([
  57. ('id', '=', self.farm.company_id.id)])
  58. journal = self.env['account.analytic.journal'].search([
  59. ('code', '=', 'FAR')])
  60. analytic_line_obj = self.env['account.analytic.line']
  61. analytic_line_obj.create({
  62. 'name': self.job_order.name,
  63. 'date': self.end_date,
  64. 'amount': -(cost * qty),
  65. 'unit_amount': qty,
  66. 'account_id': account.id,
  67. 'general_account_id': company.feed_account.id,
  68. 'journal_id': journal.id,
  69. })