analytic_account.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from odoo import api, fields, models, _
  4. class AccountAnalyticAccount(models.Model):
  5. _inherit = 'account.analytic.account'
  6. _description = 'Analytic Account'
  7. production_ids = fields.One2many('mrp.production', 'analytic_account_id', string='Manufacturing Orders')
  8. production_count = fields.Integer("Manufacturing Orders Count", compute='_compute_production_count')
  9. bom_ids = fields.One2many('mrp.bom', 'analytic_account_id', string='Bills of Materials')
  10. bom_count = fields.Integer("BoM Count", compute='_compute_bom_count')
  11. workcenter_ids = fields.One2many('mrp.workcenter', 'costs_hour_account_id', string='Workcenters')
  12. workorder_count = fields.Integer("Work Order Count", compute='_compute_workorder_count')
  13. @api.depends('production_ids')
  14. def _compute_production_count(self):
  15. for account in self:
  16. account.production_count = len(account.production_ids)
  17. @api.depends('bom_ids')
  18. def _compute_bom_count(self):
  19. for account in self:
  20. account.bom_count = len(account.bom_ids)
  21. @api.depends('workcenter_ids.order_ids', 'production_ids.workorder_ids')
  22. def _compute_workorder_count(self):
  23. for account in self:
  24. account.workorder_count = len(account.workcenter_ids.order_ids | account.production_ids.workorder_ids)
  25. def action_view_mrp_production(self):
  26. self.ensure_one()
  27. result = {
  28. "type": "ir.actions.act_window",
  29. "res_model": "mrp.production",
  30. "domain": [['id', 'in', self.production_ids.ids]],
  31. "name": _("Manufacturing Orders"),
  32. 'view_mode': 'tree,form',
  33. "context": {'default_analytic_account_id': self.id},
  34. }
  35. if len(self.production_ids) == 1:
  36. result['view_mode'] = 'form'
  37. result['res_id'] = self.production_ids.id
  38. return result
  39. def action_view_mrp_bom(self):
  40. self.ensure_one()
  41. result = {
  42. "type": "ir.actions.act_window",
  43. "res_model": "mrp.bom",
  44. "domain": [['id', 'in', self.bom_ids.ids]],
  45. "name": _("Bills of Materials"),
  46. 'view_mode': 'tree,form',
  47. "context": {'default_analytic_account_id': self.id},
  48. }
  49. if self.bom_count == 1:
  50. result['view_mode'] = 'form'
  51. result['res_id'] = self.bom_ids.id
  52. return result
  53. def action_view_workorder(self):
  54. self.ensure_one()
  55. result = {
  56. "type": "ir.actions.act_window",
  57. "res_model": "mrp.workorder",
  58. "domain": [['id', 'in', (self.workcenter_ids.order_ids | self.production_ids.workorder_ids).ids]],
  59. "context": {"create": False},
  60. "name": _("Work Orders"),
  61. 'view_mode': 'tree',
  62. }
  63. return result
  64. class AccountAnalyticLine(models.Model):
  65. _inherit = 'account.analytic.line'
  66. category = fields.Selection(selection_add=[('manufacturing_order', 'Manufacturing Order')])