Odoo14学习笔记(11) 实例-简单的按年份月份查询报表
第一步:创建查询条件模型:
class report_goods(models.Model): _name = 'xxx.report.goods' _description = u'XXX统计报表-过滤条件' # 默认显示近5年年份 current_year = datetime.now().year year_list = [(repr(current_year), repr(current_year)), (repr(current_year - 1), repr(current_year - 1)), (repr(current_year - 2), repr(current_year - 2)), (repr(current_year - 3), repr(current_year - 3)), (repr(current_year - 4), repr(current_year - 4))] year = fields.Selection(selection=year_list, string=u'年份', default=repr(current_year)) # 显示12个月份 current_month = datetime.now().month month = fields.Selection([('1', u'1月'), ('2', u'2月'), ('3', u'3月'), ('4', u'4月'), ('5', u'5月'), ('6', u'6月'), ('7', u'7月'), ('8', u'8月'), ('9', u'9月'), ('10', u'10月'), ('11', u'11月'), ('12', u'12月')], string=u'月份', default=repr(current_month)) goods_list_ids = fields.One2many('xxx.report.goods.list', 'goods_id', u'列表', readonly=True)
//点击查询按钮获取数据 def get_goods_list(self): if self.goods_list_ids: self.goods_list_ids.unlink() list_obj = self.env['ohs.report.goods.list'] if self.year and self.month: receive_ids = self.env['xxx.goods.receive'].search([('year', '=', self.year), ('month', '=', self.month)]) for r in receive_ids: list_obj.create({ 'year': r.year, 'month': r.month, 'departmentId': r.departmentId.name, 'categoryId': r.categoryId.name, 'goodsId': r.goodsId.name, 'quantity': r.quantity, 'goods_id': self.id, })
第二步:创建数据列表模型:
class report_goods_list(models.Model): _name = 'xxx.report.goods.list' _description = u'XXX统计及报表-明细' year = fields.Char(u'年份', readonly=True) month = fields.Char(u'月份', readonly=True) departmentId = fields.Char(u'车间/实验室', readonly=True) categoryId = fields.Char(u'劳保用品种类', readonly=True) goodsId = fields.Char(u'劳保用品', readonly=True) quantity = fields.Char(u'数量', readonly=True) goods_id = fields.Many2one('xxx.report.goods', u'XXX统计及报表', required=True, ondelete="restrict")
第三步:创建form文件:
<?xml version='1.0' encoding='UTF-8' ?> <odoo> <data> <record id="view_xxx_report_goods_form" model="ir.ui.view"> <field name="name">xxx.report.goods.form</field> <field name="model">xxx.report.goods</field> <field name="arch" type="xml"> <form> <sheet> <h1>XXX统计及报表</h1><br></br> <notebook> <page string="筛选条件"> <group style="text-align:left; width:300px"> <field name="year" style="width:200px;text-align:left;" col="1"/> <field name="month" style="width:200px;text-align:left;" col="1" /> <button name="get_goods_list" type="object" string="查询" col="2"/> </group> </page> </notebook> <notebook> <page string="数据列表"> <field name="goods_list_ids" style="text-align:center;" readonlye="1" options="{'no_create_edit': True, 'no_open':True, 'no_create':True}"> <tree> <field name="year"/> <field name="month"/> <field name="departmentId"/> <field name="categoryId"/> <field name="goodsId"/> <field name="quantity"/> </tree> </field> </page> </notebook> </sheet> </form> </field> </record> </data> </odoo>
效果如下:
BTW,记得给新建的模型赋权限。