コンテンツへスキップ
メニュー
この質問にフラグが付けられました
2 返信
1898 ビュー

Hi Team,

 I have created a scheduler action for timesheet reminder for the next day morning at 8 am if user forget to enter the working hours .  

python code:

from datetime import datetime, timedelta

from odoo import api, fields, models

class TimesheetReminder(models.TransientModel):

    _name = 'timesheet.reminder'


    @api.model

    def send_timesheet_reminder(self):

        # Find employees with missing timesheets for the previous day

        today = fields.Date.today()

        yesterday = today - timedelta(days=1)


        employees_with_missing_timesheets = self.env['hr.employee'].search([

            ('user_id', '!=', False),

            ('timesheet_ids.date', '=', yesterday),

            ('timesheet_ids.state', '!=', 'done'),

        ])


        # Send reminder emails

        for employee in employees_with_missing_timesheets:

            template = self.env.ref('your_module.email_template_id')

            template.send_mail(employee.user_id.id, force_send=True)


while unbale to find the module.email_template_id  ..


Getting below error :  forbidden opcode(s) in "# Available variables:\n# - env: Odoo Environment on which the action is triggered\n# - model: Odoo Model of the record on which the action is triggered; is a void recordset\n# - record: record on which the action is triggered; may be void\n# - records: recordset of all records on which the action is triggered in multi-mode; may be void\n# - time, datetime, dateutil, timezone: useful Python libraries\n# - float_compare: Odoo function to compare floats based on specific precisions\n# - log: log(message, level='info'): logging function to record debug information in ir.logging table\n# - UserError: Warning Exception to use with raise\n# - Command: x2Many commands namespace\n# To return an action, assign: action = {...}\n\nfrom datetime import datetime, timedelta\nfrom odoo import api, fields, models\nclass TimesheetReminder(models.TransientModel):\n _name = 'timesheet.reminder'\n\n @api.model\n def send_timesheet_reminder(self):\n # Find employees with missing timesheets for the previous day\n today = fields.Date.today()\n yesterday = today - timedelta(days=1)\n\n employees_with_missing_timesheets = self.env['hr.employee'].search([\n ('user_id', '!=', False),\n ('timesheet_ids.date', '=', yesterday),\n ('timesheet_ids.state', '!=', 'done'),\n ])\n\n # Send reminder emails\n for employee in employees_with_missing_timesheets:\n template = self.env.ref('your_module.email_template_id')\n template.send_mail(employee.user_id.id, force_send=True)\n\n# Add this function to the Scheduled Action": IMPORT_NAME, IMPORT_FROM, LOAD_BUILD_CLASS




Thanks and Regards
Vijay Yadav


アバター
破棄
最善の回答

Hi,

The execution of python is a bit different in the scheduled action script


You should try this instead


today = datetime.datetime.today()

yesterday = today - datetime.timedelta(1)


employees_with_missing_timesheets = env['hr.employee'].search([

    ('user_id', '!=', False),

    ('timesheet_ids.date', '=', yesterday),

    ('timesheet_ids.state', '!=', 'done'),

])

for employee in employees_with_missing_timesheets:

    template = env.ref('your_module.email_template_id')

    template.send_mail(employee.user_id.id, force_send=True)


Hope it helps

アバター
破棄
著作者 最善の回答

hi @cybrosys Team,

will this code help with web code editor  and how do we know module and template id for this line of code  your_module.email_template_id' please suggest 

 

getting this error 

 File "C:\Users\www.abcom.in\Desktop\odoo engineering\odoo\odoo\tools\safe_eval.py", line 376, in safe_eval
    raise ValueError('%s: "%s" while evaluating\n%r' % (ustr(type(e)), ustr(e), expr))
ValueError: : "Invalid field hr.employee.timesheet_ids in leaf ('timesheet_ids.state', '!=', 'done')" while evaluating
"today = datetime.datetime.today()\r\n\r\nyesterday = today - datetime.timedelta(1)\r\n\r\n\r\n\r\nemployees_with_missing_timesheets = env['hr.employee'].search([\r\n\r\n    ('user_id', '!=', False),\r\n\r\n    ('timesheet_ids.date', '=', yesterday),\r\n\r\n    ('timesheet_ids.state', '!=', 'done'),\r\n\r\n])\r\n\r\nfor employee in employees_with_missing_timesheets:\r\n\r\n    template = env.ref('your_module.email_template_id')\r\n\r\n    template.send_mail(employee.user_id.id, force_send=True)"


アバター
破棄
関連投稿 返信 ビュー 活動
1
5月 24
2176
0
8月 24
453
2
1月 24
957
1
5月 23
1226
3
7月 23
1764