production.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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, timedelta
  7. #from mx.DateTime.DateTime import today
  8. DFORMAT = "%Y-%m-%d %H:%M:%S"
  9. class BOM(models.Model):
  10. _inherit = 'mrp.bom'
  11. semen_dose = fields.Boolean(string='Semen Dose')
  12. specie = fields.Many2one(comodel_name='farm.specie',
  13. string='Dose Specie')
  14. class MrpAnaliticRemain(models.Model):
  15. _name= 'mrp.analitic.remain'
  16. date = fields.Date(string='Date')
  17. qty_per_unit = fields.Float(string='Cost per unit')
  18. class MrpProduction(models.Model):
  19. _inherit = 'mrp.production'
  20. '''
  21. @api.multi
  22. def calculateCost(self, wiz):
  23. super(MrpProduction, self).calculateCost(wiz)
  24. analitic_remain_ob = self.env['purchase.analitic.remain']
  25. remain_history = self.env['mrp.analitic.remain']
  26. factory = self.env['stock.location'].search([
  27. ('factory', '=', True)])
  28. analitic_remain = analitic_remain_ob.search([
  29. ('farm', '=', factory.id)])
  30. if len(analitic_remain) > 0 and analitic_remain.quantity > 0:
  31. today = datetime.today()
  32. if analitic_remain.last_calc:
  33. last_calc = datetime.strptime(
  34. analitic_remain.last_calc, DFORMAT)
  35. dif = today - last_calc
  36. else:
  37. dif = 3
  38. if dif > 1:
  39. self.get_last_productions(analitic_remain, today)
  40. wiz.lot_id.unit_cost = wiz.lot_id.unit_cost \
  41. + analitic_remain.quantity_per_unit
  42. analitic_remain.quantity = analitic_remain.quantity\
  43. - (wiz.product_qty * analitic_remain.quantity_per_unit)
  44. history = remain_history.search([('date', '=', today)])
  45. if not history:
  46. remain_history.create({
  47. 'date': today,
  48. 'qty_per_unit': analitic_remain.quantity_per_unit})
  49. '''
  50. @api.multi
  51. def get_last_productions(self, analitic_remain, today):
  52. productions_obj = self.env['mrp.production']
  53. ref_day = (datetime.today() - timedelta(days=30)).strftime(DFORMAT)
  54. last_prod = productions_obj.search([
  55. ('date_finished', '>', ref_day)])
  56. analitic_remain.last_calc = today
  57. if len(last_prod) < 15:
  58. ref_day = today - timedelta(days=15)
  59. last_prod = productions_obj.search([
  60. ('date_finished', '>', ref_day)])
  61. if len(last_prod) < 8:
  62. analitic_remain.quantity_per_unit = 0
  63. else:
  64. self.get_unit_remain(analitic_remain, last_prod, 15)
  65. else:
  66. self.get_unit_remain(analitic_remain, last_prod, 30)
  67. @api.multi
  68. def get_unit_remain(self, analitic_remain, last_prod, days):
  69. qty = 0
  70. for prod in last_prod:
  71. qty = qty + prod.product_qty
  72. if days == 15:
  73. qty = qty * 2
  74. analitic_remain.quantity_per_unit = analitic_remain.quantity/qty