account_move.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from collections import defaultdict
  4. from odoo import api, fields, models, _
  5. from odoo.osv import expression
  6. class AccountMove(models.Model):
  7. _inherit = "account.move"
  8. timesheet_ids = fields.One2many('account.analytic.line', 'timesheet_invoice_id', string='Timesheets', readonly=True, copy=False)
  9. timesheet_count = fields.Integer("Number of timesheets", compute='_compute_timesheet_count')
  10. timesheet_encode_uom_id = fields.Many2one('uom.uom', related='company_id.timesheet_encode_uom_id')
  11. timesheet_total_duration = fields.Integer("Timesheet Total Duration", compute='_compute_timesheet_total_duration', help="Total recorded duration, expressed in the encoding UoM, and rounded to the unit")
  12. @api.depends('timesheet_ids', 'company_id.timesheet_encode_uom_id')
  13. def _compute_timesheet_total_duration(self):
  14. if not self.user_has_groups('hr_timesheet.group_hr_timesheet_user'):
  15. self.timesheet_total_duration = 0
  16. return
  17. group_data = self.env['account.analytic.line']._read_group([
  18. ('timesheet_invoice_id', 'in', self.ids)
  19. ], ['timesheet_invoice_id', 'unit_amount'], ['timesheet_invoice_id'])
  20. timesheet_unit_amount_dict = defaultdict(float)
  21. timesheet_unit_amount_dict.update({data['timesheet_invoice_id'][0]: data['unit_amount'] for data in group_data})
  22. for invoice in self:
  23. total_time = invoice.company_id.project_time_mode_id._compute_quantity(timesheet_unit_amount_dict[invoice.id], invoice.timesheet_encode_uom_id)
  24. invoice.timesheet_total_duration = round(total_time)
  25. @api.depends('timesheet_ids')
  26. def _compute_timesheet_count(self):
  27. timesheet_data = self.env['account.analytic.line']._read_group([('timesheet_invoice_id', 'in', self.ids)], ['timesheet_invoice_id'], ['timesheet_invoice_id'])
  28. mapped_data = dict([(t['timesheet_invoice_id'][0], t['timesheet_invoice_id_count']) for t in timesheet_data])
  29. for invoice in self:
  30. invoice.timesheet_count = mapped_data.get(invoice.id, 0)
  31. def action_view_timesheet(self):
  32. self.ensure_one()
  33. return {
  34. 'type': 'ir.actions.act_window',
  35. 'name': _('Timesheets'),
  36. 'domain': [('project_id', '!=', False)],
  37. 'res_model': 'account.analytic.line',
  38. 'view_id': False,
  39. 'view_mode': 'tree,form',
  40. 'help': _("""
  41. <p class="o_view_nocontent_smiling_face">
  42. Record timesheets
  43. </p><p>
  44. You can register and track your workings hours by project every
  45. day. Every time spent on a project will become a cost and can be re-invoiced to
  46. customers if required.
  47. </p>
  48. """),
  49. 'limit': 80,
  50. 'context': {
  51. 'default_project_id': self.id,
  52. 'search_default_project_id': [self.id]
  53. }
  54. }
  55. def _link_timesheets_to_invoice(self, start_date=None, end_date=None):
  56. """ Search timesheets from given period and link this timesheets to the invoice
  57. When we create an invoice from a sale order, we need to
  58. link the timesheets in this sale order to the invoice.
  59. Then, we can know which timesheets are invoiced in the sale order.
  60. :param start_date: the start date of the period
  61. :param end_date: the end date of the period
  62. """
  63. for line in self.filtered(lambda i: i.move_type == 'out_invoice' and i.state == 'draft').invoice_line_ids:
  64. sale_line_delivery = line.sale_line_ids.filtered(lambda sol: sol.product_id.invoice_policy == 'delivery' and sol.product_id.service_type == 'timesheet')
  65. if sale_line_delivery:
  66. domain = line._timesheet_domain_get_invoiced_lines(sale_line_delivery)
  67. if start_date:
  68. domain = expression.AND([domain, [('date', '>=', start_date)]])
  69. if end_date:
  70. domain = expression.AND([domain, [('date', '<=', end_date)]])
  71. timesheets = self.env['account.analytic.line'].sudo().search(domain)
  72. timesheets.write({'timesheet_invoice_id': line.move_id.id})
  73. class AccountMoveLine(models.Model):
  74. _inherit = 'account.move.line'
  75. @api.model
  76. def _timesheet_domain_get_invoiced_lines(self, sale_line_delivery):
  77. """ Get the domain for the timesheet to link to the created invoice
  78. :param sale_line_delivery: recordset of sale.order.line to invoice
  79. :return a normalized domain
  80. """
  81. return [
  82. ('so_line', 'in', sale_line_delivery.ids),
  83. ('project_id', '!=', False),
  84. '|', '|',
  85. ('timesheet_invoice_id', '=', False),
  86. ('timesheet_invoice_id.state', '=', 'cancel'),
  87. ('timesheet_invoice_id.payment_state', '=', 'reversed')
  88. ]
  89. def unlink(self):
  90. move_line_read_group = self.env['account.move.line'].search_read([
  91. ('move_id.move_type', '=', 'out_invoice'),
  92. ('move_id.state', '=', 'draft'),
  93. ('sale_line_ids.product_id.invoice_policy', '=', 'delivery'),
  94. ('sale_line_ids.product_id.service_type', '=', 'timesheet'),
  95. ('id', 'in', self.ids)],
  96. ['move_id', 'sale_line_ids'])
  97. sale_line_ids_per_move = defaultdict(lambda: self.env['sale.order.line'])
  98. for move_line in move_line_read_group:
  99. sale_line_ids_per_move[move_line['move_id'][0]] += self.env['sale.order.line'].browse(move_line['sale_line_ids'])
  100. timesheet_read_group = self.sudo().env['account.analytic.line']._read_group([
  101. ('timesheet_invoice_id.move_type', '=', 'out_invoice'),
  102. ('timesheet_invoice_id.state', '=', 'draft'),
  103. ('timesheet_invoice_id', 'in', self.move_id.ids)],
  104. ['timesheet_invoice_id', 'so_line', 'ids:array_agg(id)'],
  105. ['timesheet_invoice_id', 'so_line'],
  106. lazy=False)
  107. timesheet_ids = []
  108. for timesheet in timesheet_read_group:
  109. move_id = timesheet['timesheet_invoice_id'][0]
  110. if timesheet['so_line'] and timesheet['so_line'][0] in sale_line_ids_per_move[move_id].ids:
  111. timesheet_ids += timesheet['ids']
  112. self.sudo().env['account.analytic.line'].browse(timesheet_ids).write({'timesheet_invoice_id': False})
  113. return super().unlink()