Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie
1 Odpowiedz
1400 Widoki

Hey

I'm using odoo enterprise, and I'm trying to inherit account.analytic.line and the method adjust_grid to avoid people to timesheet on a project after a date limit represented by the field date_done.

So in my module, I inherit account.analytic.line but my function is never called when I timesheet on a project, it seems that my override is not working. Any idea why ?
Here my code, I just copy paste the function from the class and add some log. I work with odoo.sh. Also my manifest depend of timesheet_grid, and my model is correctly loaded in my __init__ files

"depends": [
"base",
"project",
"timesheet_grid",
...
],

import logging
from odoo import tools, models, fields, api, _
from odoo.osv import expression
from odoo.exceptions import UserError, AccessError

_logger = logging.getLogger(__name__)

class AnalyticLine(models.Model):
_inherit = ['account.analytic.line']
def controlIfProjectIsDone(self):
_logger.debug("-------------------------")
_logger.debug("TEST")
_logger.debug("-------------------------")

def adjust_grid(self, row_domain, column_field, column_value, cell_field, change):
self.controlIfProjectIsDone()
if column_field != 'date' or cell_field != 'unit_amount':
raise ValueError(
"{} can only adjust unit_amount (got {}) by date (got {})".format(
self._name,
cell_field,
column_field,
))

additionnal_domain = self._get_adjust_grid_domain(column_value)
# Remove date from the domain
new_row_domain = []
for leaf in row_domain:
if leaf[0] == 'date':
new_row_domain += ['|', expression.TRUE_LEAF, leaf]
else:
new_row_domain.append(leaf)
domain = expression.AND([new_row_domain, additionnal_domain])
line = self.search(domain)

if line.project_id and not line.project_id.allow_timesheets:
raise UserError(_("You cannot adjust the time of the timesheet for a project with timesheets disabled."))

day = column_value.split('/')[0]


if len(line) > 1 or len(line) == 1 and line.validated: # copy the last line as adjustment
line[0].copy(self._prepare_duplicate_timesheet_line_values(
column_field, day, cell_field, change)
)
elif len(line) == 1: # update existing line
line.write({
cell_field: line[cell_field] + change
})
else: # create new one
line_in_domain = self.search(row_domain, limit=1)
if line_in_domain:
line_in_domain.copy(self._prepare_duplicate_timesheet_line_values(
column_field, day, cell_field, change)
)
else:
project, task = self._get_project_task_from_domain(domain)

if task and not project:
project = self.env['project.task'].browse([task]).project_id.id

if project:
_logger.debug("-------------------------------------------------")
_logger.debug("day %s", day)
_logger.debug("date done %s", project.date_done)
self.create([{
'project_id': project,
'task_id': task,
column_field: day,
cell_field: change,
}])

return False


Awatar
Odrzuć
Najlepsza odpowiedź

It seems like you are trying to override the adjust_grid method in the account.analytic.line model to add some additional logic. Your code looks correct for the most part, but there are a few things to check:


    Correct Inheritance:

    Ensure that your module depends on the timesheet_grid module and that it is correctly loaded before your module. The adjust_grid method is part of the timesheet_grid module, so if your module is not loaded after it, your override might not take effect.


    Check Log Output:

    The logging statements you've added (_logger.debug) should appear in the logs if the controlIfProjectIsDone method is being called. Check the logs to see if you find the log statements when timesheeting on a project.


    Method Signature:

    Verify that the method signature matches exactly with the method you are trying to override. If there have been changes in the Odoo version you are using, the method signature might differ, leading to your override not being recognized.


    Ensure Correct Module Dependencies:

    Ensure that your module's dependencies are correctly specified in the manifest file, and there are no conflicts or issues with other modules.


    Restart Odoo Service:

    After making changes to your code, restart the Odoo service to ensure that the changes are picked up.


    Check for Syntax Errors:

    Ensure that there are no syntax errors in your Python code that could prevent the correct loading of your module.

---------------------------------

    Use Decorators:

    In Odoo, decorators are often used for method overrides. Try using the @api.multi decorator on your overridden method to ensure it's recognized as a multi-record method.

@api.multi

def adjust_grid(self, row_domain, column_field, column_value, cell_field, change):

    # Your code here


---------------------------------


If, after checking these points, the issue persists, you may want to look into the Odoo server logs for any error messages or warnings that could provide more insight into why your method override is not working as expected

Awatar
Odrzuć
Powiązane posty Odpowiedzi Widoki Czynność
1
lip 23
3033
0
lis 17
6294
1
maj 25
1334
1
sie 24
1704
3
kwi 24
7910