📅  最后修改于: 2023-12-03 14:52:27.924000             🧑  作者: Mango
在 Odoo 中,one2many 字段是一个与其它模型的关系字段。这意味着您可以将多个记录绑定到一个主记录中。在本篇文章中,我们将讨论如何在 Odoo 的 one2many 字段中创建报告。
首先,您需要在 Odoo 中安装 report_xlsx 模块。此模块允许您创建 Excel 报告。您可以从 GitHub 上获取该模块的源代码。
在您的模型中,创建一个 One2many 字段,并为该字段创建一个视图。以下是一个示例 One2many 字段和视图的代码示例:
class SaleOrder(models.Model):
_name = 'sale.order'
name = fields.Char('Order Reference', required=True, index=True, copy=False, default=lambda self: _('New'))
order_line = fields.One2many('sale.order.line', 'order_id', string='Order Lines', copy=True)
<field name="order_line" mode="tree,kanban">
<tree editable="top">
<field name="name" />
<field name="product_id" />
<field name="price_unit" />
<field name="product_uom_qty" />
<field name="price_subtotal" />
</tree>
</field>
添加一个按钮,并在点击该按钮时生成报告。以下是一个示例按钮和生成报告的代码示例:
import base64
from odoo import fields, models, api
from odoo.tools import float_round
class SaleOrder(models.Model):
_name = 'sale.order'
name = fields.Char('Order Reference', required=True, index=True, copy=False, default=lambda self: _('New'))
order_line = fields.One2many('sale.order.line', 'order_id', string='Order Lines', copy=True)
@api.multi
def generate_report(self):
for rec in self:
data = {
'date': rec.date_order,
'company': rec.company_id.name,
'name': rec.name,
'order_lines': []
}
for line in rec.order_line:
data['order_lines'].append({
'name': line.name,
'product': line.product_id.name,
'quantity': line.product_uom_qty,
'price': line.price_unit
})
report_name = 'Sale Order Report - %s.xlsx' % rec.name
report = self.env['report_xlsx.sale_order_report'].create({'data': data})
result = base64.b64encode(report.report_xlsx)
rec.write({'report': result, 'report_name': report_name})
class SaleOrderReport(models.AbstractModel):
_name = 'report.report_xlsx.sale_order_report'
def generate_xlsx_report(self, workbook, data, lines):
sheet = workbook.add_worksheet(_('Sale Order Report'))
sheet.set_column(0, 0, 10)
sheet.set_column(1, 1, 20)
sheet.set_column(2, 2, 30)
sheet.set_column(3, 3, 10)
sheet.set_column(4, 4, 15)
bold = workbook.add_format({'bold': True, 'border': 1, 'align': 'center'})
border = workbook.add_format({'border': 1, 'align': 'center'})
sheet.write(0, 0, _('Date'), bold)
sheet.write(0, 1, data['date'], border)
sheet.write(1, 0, _('Company'), bold)
sheet.write(1, 1, data['company'], border)
sheet.write(2, 0, _('Order Reference'), bold)
sheet.write(2, 1, data['name'], border)
sheet.write(4, 0, _('Name'), bold)
sheet.write(4, 1, _('Product'), bold)
sheet.write(4, 2, _('Quantity'), bold)
sheet.write(4, 3, _('Price'), bold)
row = 5
for line in data['order_lines']:
sheet.write(row, 0, line['name'], border)
sheet.write(row, 1, line['product'], border)
sheet.write(row, 2, line['quantity'], border)
sheet.write(row, 3, line['price'], border)
sheet.write(row, 4, float_round(line['quantity'] * line['price'], precision_rounding=0.01), border)
row += 1
sheet.write(row, 3, _('Total'), bold)
sheet.write(row, 4, "=SUM(E6:E%d)" % row, border)
最后,添加您的报告到报告菜单中:
<report
id="report_xlsx_sale_order"
model="sale.order"
string="Sale Order Report"
name="report_xlsx.sale_order_report"
file="report_xlsx.sale_order_report"
report_type="xlsx"
/>
以上就是如何在 Odoo 的 one2many 字段中创建报告的全过程。我们介绍了如何创建一个 One2many 字段和视图、如何添加一个按钮来生成报告、以及如何将报告添加到报告菜单中。希望这篇文章对您有所帮助。