我正在尝试创建一个 cron,当产品数量几乎为 0 时发送库存警报
<record id="ir_cron_mort_crm" model="ir.cron">
<field name="name">Product Quantity Alert</field>
<field name="model_id" ref="model_stock_quant"/>
<field name="state">code</field>
<field name="code">model.send_alert_email()</field>
<field name="user_id" ref="base.user_admin"/>
<field name="interval_number">1</field>
<field name="interval_type">days</field>
<field name="numbercall">-1</field>
<field name="priority">0</field>
<field name="active" eval="True"/>
</record>
我正在使用此代码
from odoo import models, fields, api
import base64
class StockQuant(models.Model):
_inherit = 'stock.quant'
alert_qty = fields.Float(string='Alert Quantity', store=True)
def send_alert_email(self):
# Retrieve the PDF report template
print("innnnnn")
report_template_id = self.env.ref('y_slnee_test.report_stock_alert')._render_qweb_pdf(self.id)
print('report---',report_template_id)
# Encode the PDF data in base64 format
data_record = base64.b64encode(report_template_id[0])
print('data-----',data_record)
# Attachment data
ir_values = {
'name': "Report",
'type': 'binary',
'datas': data_record,
'store_fname': 'data_record',
'mimetype': 'application/x-pdf',
}
data_id = self.env['ir.attachment'].sudo().create(ir_values)
print('created data ?----', data_id)
template = self.env.ref('y_slnee_test.report_stock_reorder_report_template_id')
print('email template------', template)
# Associate the created attachment with the email template
template.attachment_ids = [(6, 0, [data_id.id])]
print('attached file---',template.attachment_ids)
email_values = {'email_to': '[email protected]',
'email_from': '[email protected]'}
template.sudo().send_mail(1, email_values=email_values, force_send=True)
# Remove the attachment from the email template
template.attachment_ids = [(3, data_id.id)]
return True
class StaticProducts(models.AbstractModel):
_name = "report.y_slnee_test.report_stock_reorder_report_template"
@api.model
def _get_report_values(self, docids, data=None):
report_data = []
product_ids = self.env['stock.quant'].search([])
for rec in product_ids:
if rec.alert_qty:
alert_qty = rec.available_quantity - rec.alert_qty
if alert_qty < 0:
values = {'product': rec.product_id.name or '',
'location': rec.location_id.name or '',
'available': rec.available_quantity or '',
'alert': alert_qty or ''}
print(values)
report_data.append(values)
print(report_data)
return {
'docs': report_data,
}
当我手动运行 cron 且报告中没有任何内容(只有表标题)时,它正在运行 但当我有价值观时
{'product': 'BROTHER Toner-Kit (TN-2410)', 'location': 'Vendors', 'available': -58.0, 'alert': -68.0}
{'product': 'Tintenpatrone schwarz (K-EPS-C13T34714010XL)', 'location': 'Vendors', 'available': -35.0, 'alert': -45.0}
我收到此错误:
ValueError: <class 'odoo.addons.base.models.qweb.QWebException'>: "Error to render compiling AST" while evaluating
'model.send_alert_email()'
我确定问题出在这一行:
report_template_id = self.env.ref('y_slnee_test.report_stock_alert')._render_qweb_pdf(self.id)
你能帮我吗?
XML:
<template id="report_stock_reorder_report_template">
<t t-call="web.html_container">
<t t-call="web.basic_layout">
<div class="page">
<div>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse; }
</style>
<table class="table table-sm o_main_table">
<thead>
<tr>
<t t-set="colspan" t-value="4"/>
<th class="text-right">Article</th>
<th class="text-right">Emplacement</th>
<th class="text-right">Qty Alert</th>
<th class="text-right">Qty Disponible</th>
<th class="text-right">(Qty Disponible - Qty d’Alert)</th>
</tr>
</thead>
<tbody>
<t t-if="docs" t-foreach="docs" t-as="l">
<tr>
<td>
<span t-if ="l.product" t-esc="l.product"/>
</td>
<td>
<span t-if ="l.location" t-esc="l.location"/>
</td>
<td>
<span t-if ="l.available" t-esc="l.available"/>
</td>
<td>
<span t-if ="l.alert" t-esc="l.alert"/>
</td>
</tr>
</t>
</tbody>
</table>
</div>
</div>
</t>
</t>
</template>
<!-- Definition of the report -->
<record id="report_stock_alert" model="ir.actions.report">
<field name="name">Stock Alert Quantity</field>
<field name="model">stock.quant</field>
<field name="report_type">qweb-pdf</field>
<field name="report_name">y_slnee_test.report_stock_reorder_report_template</field>
<field name="report_file">y_slnee_test.report_stock_reorder_report_template</field>
</record>
我尝试打印每一步来调试代码,我确定问题出在这一行:
report_template_id = self.env.ref('y_slnee_test.report_stock_alert')._render_qweb_pdf(self.id)
我不知道如何解决