I have a scheduled action that I would like have send an email once per day that informs which products are below the minimum stock threshold.
When I attempt to run it manually, I get the following output:
Failed to rener template
using values {'format_date': . at
0x7f5dbe285840>, 'format_tz': . at
0x7f5dbe285268>, 'format_amount': . at
0x7f5dbe285598>, 'user': res.users(1,), 'ctx': {'lang': 'pt_AO',
'tz': False, 'uid': 2, 'params': {'id': 13, 'action': 11, 'model':
'ir.cron', 'view_type': 'form', 'menu_id': 4}, 'search_default_all': 1,
'progress_code': 'a6da97cf-df22-4a85-9e81-192fe423134d',
'mail_notify_force_send': False, 'safe': False}, 'object': None}
UndefinedError: 'None' has no attribute 'env'
This is the action's function:
class ProductTemplate(models.Model):
_inherit = 'product.template'
def _send_minimum_stock_report(self):
# Find products with stock below minimum
minimum_stock_products = self.search([('stock_below_minimum', '=', True)])
# Generate report
report = ''
if minimum_stock_products:
report += 'Products that are below minimum stock:\n\n'
for product in minimum_stock_products:
report += '- ' + product.name + '\n'
else:
report += 'There are no products below minimum stock.\n'
# Get email template and send email
template_id = self\.env\.ref\('dcloud_minimum_stock_notifications\.minimum_stock_email_template'\)\.id
\ \ \ \ \ \ \ \ email_to\ =\ self\.env\.user\.partner_id\.email
\ \ \ \ \ \ \ \ mail_values\ =\ \{
\ \ \ \ \ \ \ \ \ \ \ \ 'email_to':\ email_to,
\ \ \ \ \ \ \ \ \ \ \ \ 'subject':\ 'Minimum\ Stock\ Report\ \-\ '\ \+\ str\(date\.today\(\)\),
\ \ \ \ \ \ \ \ \ \ \ \ 'body_html':\ report,
\ \ \ \ \ \ \ \ \ \ \ \ 'email_from':\ self\.env\.user\.email,
\ \ \ \ \ \ \ \ \}
\ \ \ \ \ \ \ \ self\.env\['mail\.template'\]\.browse\(template_id\)\.send_mail\(self.id, email_values=mail_values)
@api.model
def _cron_send_minimum_stock_report(self):
self._send_minimum_stock_report()
return True
This is the email template definition:
Email de Notificação de Estoque Mínimo
pt_AO
Relatório de estoque mínimo - ${datetime.date.today().strftime('%Y-%m-%d')}
${object.env.user.email}
${(env:=object.env)['dcloud_minimum_stock_settings.settings'].search([], order='id desc', limit=1).notification_email}
Prezado usuário,
Por favor, encontre abaixo a lista de produtos que estão atualmente abaixo dos seus níveis mínimos de estoque:
Obrigado,
Seu sistema Odoo
]]>