19.0 vanilla

This commit is contained in:
Ernad Husremovic 2026-03-09 09:32:12 +01:00
parent 79f83631d5
commit 73afc09215
6267 changed files with 1534193 additions and 1130106 deletions

View file

@ -1,30 +1,31 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from collections import defaultdict
from odoo import api, fields, models, _
from odoo.osv import expression
from odoo.fields import Domain
class AccountMove(models.Model):
_inherit = "account.move"
timesheet_ids = fields.One2many('account.analytic.line', 'timesheet_invoice_id', string='Timesheets', readonly=True, copy=False)
timesheet_count = fields.Integer("Number of timesheets", compute='_compute_timesheet_count')
timesheet_encode_uom_id = fields.Many2one('uom.uom', related='company_id.timesheet_encode_uom_id')
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")
timesheet_ids = fields.One2many('account.analytic.line', 'timesheet_invoice_id', string='Timesheets', readonly=True, copy=False, export_string_translation=False)
timesheet_count = fields.Integer("Number of timesheets", compute='_compute_timesheet_count', compute_sudo=True, export_string_translation=False)
timesheet_encode_uom_id = fields.Many2one('uom.uom', related='company_id.timesheet_encode_uom_id', export_string_translation=False)
timesheet_total_duration = fields.Integer("Timesheet Total Duration",
compute='_compute_timesheet_total_duration', compute_sudo=True,
help="Total recorded duration, expressed in the encoding UoM, and rounded to the unit")
@api.depends('timesheet_ids', 'company_id.timesheet_encode_uom_id')
def _compute_timesheet_total_duration(self):
if not self.user_has_groups('hr_timesheet.group_hr_timesheet_user'):
if not self.env.user.has_group('hr_timesheet.group_hr_timesheet_user'):
self.timesheet_total_duration = 0
return
group_data = self.env['account.analytic.line']._read_group([
('timesheet_invoice_id', 'in', self.ids)
], ['timesheet_invoice_id', 'unit_amount'], ['timesheet_invoice_id'])
], ['timesheet_invoice_id'], ['unit_amount:sum'])
timesheet_unit_amount_dict = defaultdict(float)
timesheet_unit_amount_dict.update({data['timesheet_invoice_id'][0]: data['unit_amount'] for data in group_data})
timesheet_unit_amount_dict.update({timesheet_invoice.id: amount for timesheet_invoice, amount in group_data})
for invoice in self:
total_time = invoice.company_id.project_time_mode_id._compute_quantity(
timesheet_unit_amount_dict[invoice.id],
@ -35,8 +36,8 @@ class AccountMove(models.Model):
@api.depends('timesheet_ids')
def _compute_timesheet_count(self):
timesheet_data = self.env['account.analytic.line']._read_group([('timesheet_invoice_id', 'in', self.ids)], ['timesheet_invoice_id'], ['timesheet_invoice_id'])
mapped_data = dict([(t['timesheet_invoice_id'][0], t['timesheet_invoice_id_count']) for t in timesheet_data])
timesheet_data = self.env['account.analytic.line']._read_group([('timesheet_invoice_id', 'in', self.ids)], ['timesheet_invoice_id'], ['__count'])
mapped_data = {timesheet_invoice.id: count for timesheet_invoice, count in timesheet_data}
for invoice in self:
invoice.timesheet_count = mapped_data.get(invoice.id, 0)
@ -48,7 +49,7 @@ class AccountMove(models.Model):
'domain': [('project_id', '!=', False)],
'res_model': 'account.analytic.line',
'view_id': False,
'view_mode': 'tree,form',
'view_mode': 'list,form',
'help': _("""
<p class="o_view_nocontent_smiling_face">
Record timesheets
@ -76,62 +77,29 @@ class AccountMove(models.Model):
"""
for line in self.filtered(lambda i: i.move_type == 'out_invoice' and i.state == 'draft').invoice_line_ids:
sale_line_delivery = line.sale_line_ids.filtered(lambda sol: sol.product_id.invoice_policy == 'delivery' and sol.product_id.service_type == 'timesheet')
if not start_date and not end_date:
start_date, end_date = self._get_range_dates(sale_line_delivery.order_id)
if sale_line_delivery:
domain = line._timesheet_domain_get_invoiced_lines(sale_line_delivery)
domain = Domain(line._timesheet_domain_get_invoiced_lines(sale_line_delivery))
if start_date:
domain = expression.AND([domain, [('date', '>=', start_date)]])
domain &= Domain('date', '>=', start_date)
if end_date:
domain = expression.AND([domain, [('date', '<=', end_date)]])
domain &= Domain('date', '<=', end_date)
timesheets = self.env['account.analytic.line'].sudo().search(domain)
timesheets.write({'timesheet_invoice_id': line.move_id.id})
def _get_range_dates(self, order):
# A method that can be overridden
# to set the start and end dates according to order values
return None, None
class AccountMoveLine(models.Model):
_inherit = 'account.move.line'
@api.model
def _timesheet_domain_get_invoiced_lines(self, sale_line_delivery):
""" Get the domain for the timesheet to link to the created invoice
:param sale_line_delivery: recordset of sale.order.line to invoice
:return a normalized domain
"""
return [
('so_line', 'in', sale_line_delivery.ids),
def action_post(self):
result = super().action_post()
credit_notes = self.filtered(lambda move: move.move_type == 'out_refund' and move.reversed_entry_id)
timesheets_sudo = self.env['account.analytic.line'].sudo().search([
('timesheet_invoice_id', 'in', credit_notes.reversed_entry_id.ids),
('so_line', 'in', credit_notes.invoice_line_ids.sale_line_ids.ids),
('project_id', '!=', False),
'|', '|',
('timesheet_invoice_id', '=', False),
'&',
('timesheet_invoice_id.state', '=', 'cancel'),
('timesheet_invoice_id.payment_state', '!=', 'invoicing_legacy'),
('timesheet_invoice_id.payment_state', '=', 'reversed')
]
def unlink(self):
move_line_read_group = self.env['account.move.line'].search_read([
('move_id.move_type', '=', 'out_invoice'),
('move_id.state', '=', 'draft'),
('sale_line_ids.product_id.invoice_policy', '=', 'delivery'),
('sale_line_ids.product_id.service_type', '=', 'timesheet'),
('id', 'in', self.ids)],
['move_id', 'sale_line_ids'])
sale_line_ids_per_move = defaultdict(lambda: self.env['sale.order.line'])
for move_line in move_line_read_group:
sale_line_ids_per_move[move_line['move_id'][0]] += self.env['sale.order.line'].browse(move_line['sale_line_ids'])
timesheet_read_group = self.sudo().env['account.analytic.line']._read_group([
('timesheet_invoice_id.move_type', '=', 'out_invoice'),
('timesheet_invoice_id.state', '=', 'draft'),
('timesheet_invoice_id', 'in', self.move_id.ids)],
['timesheet_invoice_id', 'so_line', 'ids:array_agg(id)'],
['timesheet_invoice_id', 'so_line'],
lazy=False)
timesheet_ids = []
for timesheet in timesheet_read_group:
move_id = timesheet['timesheet_invoice_id'][0]
if timesheet['so_line'] and timesheet['so_line'][0] in sale_line_ids_per_move[move_id].ids:
timesheet_ids += timesheet['ids']
self.sudo().env['account.analytic.line'].browse(timesheet_ids).write({'timesheet_invoice_id': False})
return super().unlink()
])
timesheets_sudo.write({'timesheet_invoice_id': False})
return result