account_analytic_account.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # -*- coding: utf-8 -*-
  2. from odoo import api, fields, models, _
  3. class AccountAnalyticAccount(models.Model):
  4. _inherit = 'account.analytic.account'
  5. invoice_count = fields.Integer(
  6. "Invoice Count",
  7. compute='_compute_invoice_count',
  8. )
  9. vendor_bill_count = fields.Integer(
  10. "Vendor Bill Count",
  11. compute='_compute_vendor_bill_count',
  12. )
  13. @api.depends('line_ids')
  14. def _compute_invoice_count(self):
  15. sale_types = self.env['account.move'].get_sale_types(include_receipts=True)
  16. query = self.env['account.move.line']._search([
  17. ('parent_state', '=', 'posted'),
  18. ('move_id.move_type', 'in', sale_types),
  19. ])
  20. query.add_where(
  21. 'account_move_line.analytic_distribution ?| %s',
  22. [[str(account_id) for account_id in self.ids]],
  23. )
  24. query.order = None
  25. query_string, query_param = query.select(
  26. 'jsonb_object_keys(account_move_line.analytic_distribution) as account_id',
  27. 'COUNT(DISTINCT(account_move_line.move_id)) as move_count',
  28. )
  29. query_string = f"{query_string} GROUP BY jsonb_object_keys(account_move_line.analytic_distribution)"
  30. self._cr.execute(query_string, query_param)
  31. data = {int(record.get('account_id')): record.get('move_count') for record in self._cr.dictfetchall()}
  32. for account in self:
  33. account.invoice_count = data.get(account.id, 0)
  34. @api.depends('line_ids')
  35. def _compute_vendor_bill_count(self):
  36. purchase_types = self.env['account.move'].get_purchase_types(include_receipts=True)
  37. query = self.env['account.move.line']._search([
  38. ('parent_state', '=', 'posted'),
  39. ('move_id.move_type', 'in', purchase_types),
  40. ])
  41. query.add_where(
  42. 'account_move_line.analytic_distribution ?| %s',
  43. [[str(account_id) for account_id in self.ids]],
  44. )
  45. query.order = None
  46. query_string, query_param = query.select(
  47. 'jsonb_object_keys(account_move_line.analytic_distribution) as account_id',
  48. 'COUNT(DISTINCT(account_move_line.move_id)) as move_count',
  49. )
  50. query_string = f"{query_string} GROUP BY jsonb_object_keys(account_move_line.analytic_distribution)"
  51. self._cr.execute(query_string, query_param)
  52. data = {int(record.get('account_id')): record.get('move_count') for record in self._cr.dictfetchall()}
  53. for account in self:
  54. account.vendor_bill_count = data.get(account.id, 0)
  55. def action_view_invoice(self):
  56. self.ensure_one()
  57. query = self.env['account.move.line']._search([('move_id.move_type', 'in', self.env['account.move'].get_sale_types())])
  58. query.order = None
  59. query.add_where('analytic_distribution ? %s', [str(self.id)])
  60. query_string, query_param = query.select('DISTINCT account_move_line.move_id')
  61. self._cr.execute(query_string, query_param)
  62. move_ids = [line.get('move_id') for line in self._cr.dictfetchall()]
  63. result = {
  64. "type": "ir.actions.act_window",
  65. "res_model": "account.move",
  66. "domain": [('id', 'in', move_ids)],
  67. "context": {"create": False, 'default_move_type': 'out_invoice'},
  68. "name": _("Customer Invoices"),
  69. 'view_mode': 'tree,form',
  70. }
  71. return result
  72. def action_view_vendor_bill(self):
  73. self.ensure_one()
  74. query = self.env['account.move.line']._search([('move_id.move_type', 'in', self.env['account.move'].get_purchase_types())])
  75. query.order = None
  76. query.add_where('analytic_distribution ? %s', [str(self.id)])
  77. query_string, query_param = query.select('DISTINCT account_move_line.move_id')
  78. self._cr.execute(query_string, query_param)
  79. move_ids = [line.get('move_id') for line in self._cr.dictfetchall()]
  80. result = {
  81. "type": "ir.actions.act_window",
  82. "res_model": "account.move",
  83. "domain": [('id', 'in', move_ids)],
  84. "context": {"create": False, 'default_move_type': 'in_invoice'},
  85. "name": _("Vendor Bills"),
  86. 'view_mode': 'tree,form',
  87. }
  88. return result