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,記得給新建的模型賦權限。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM