account_analytic_line.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. # -*- coding: utf-8 -*-
  2. from odoo import api, fields, models, _
  3. from odoo.exceptions import ValidationError
  4. class AccountAnalyticLine(models.Model):
  5. _inherit = 'account.analytic.line'
  6. _description = 'Analytic Line'
  7. product_id = fields.Many2one(
  8. 'product.product',
  9. string='Product',
  10. check_company=True,
  11. )
  12. general_account_id = fields.Many2one(
  13. 'account.account',
  14. string='Financial Account',
  15. ondelete='restrict',
  16. domain="[('deprecated', '=', False), ('company_id', '=', company_id)]",
  17. compute='_compute_general_account_id', store=True, readonly=False
  18. )
  19. journal_id = fields.Many2one(
  20. 'account.journal',
  21. string='Financial Journal',
  22. check_company=True,
  23. readonly=True,
  24. related='move_line_id.journal_id',
  25. store=True,
  26. )
  27. partner_id = fields.Many2one(
  28. readonly=False,
  29. compute="_compute_partner_id",
  30. store=True,
  31. )
  32. move_line_id = fields.Many2one(
  33. 'account.move.line',
  34. string='Journal Item',
  35. ondelete='cascade',
  36. index=True,
  37. check_company=True,
  38. )
  39. code = fields.Char(size=8)
  40. ref = fields.Char(string='Ref.')
  41. category = fields.Selection(selection_add=[('invoice', 'Customer Invoice'), ('vendor_bill', 'Vendor Bill')])
  42. @api.depends('move_line_id')
  43. def _compute_general_account_id(self):
  44. for line in self:
  45. line.general_account_id = line.move_line_id.account_id
  46. @api.constrains('move_line_id', 'general_account_id')
  47. def _check_general_account_id(self):
  48. for line in self:
  49. if line.move_line_id and line.general_account_id != line.move_line_id.account_id:
  50. raise ValidationError(_('The journal item is not linked to the correct financial account'))
  51. @api.depends('move_line_id')
  52. def _compute_partner_id(self):
  53. for line in self:
  54. line.partner_id = line.move_line_id.partner_id or line.partner_id
  55. @api.onchange('product_id', 'product_uom_id', 'unit_amount', 'currency_id')
  56. def on_change_unit_amount(self):
  57. if not self.product_id:
  58. return {}
  59. prod_accounts = self.product_id.product_tmpl_id.with_company(self.company_id)._get_product_accounts()
  60. unit = self.product_uom_id
  61. account = prod_accounts['expense']
  62. if not unit or self.product_id.uom_po_id.category_id.id != unit.category_id.id:
  63. unit = self.product_id.uom_po_id
  64. # Compute based on pricetype
  65. amount_unit = self.product_id.price_compute('standard_price', uom=unit)[self.product_id.id]
  66. amount = amount_unit * self.unit_amount or 0.0
  67. result = (self.currency_id.round(amount) if self.currency_id else round(amount, 2)) * -1
  68. self.amount = result
  69. self.general_account_id = account
  70. self.product_uom_id = unit
  71. @api.model
  72. def view_header_get(self, view_id, view_type):
  73. if self.env.context.get('account_id'):
  74. return _(
  75. "Entries: %(account)s",
  76. account=self.env['account.analytic.account'].browse(self.env.context['account_id']).name
  77. )
  78. return super().view_header_get(view_id, view_type)