当前位置:首页>python>中篇Python(ODOO)篇 第四章 ODOO项目优化部分(四)预约模块增强 在其他数据模型定义函数

中篇Python(ODOO)篇 第四章 ODOO项目优化部分(四)预约模块增强 在其他数据模型定义函数

  • 2026-10-11 07:17:24
中篇Python(ODOO)篇 第四章 ODOO项目优化部分(四)预约模块增强 在其他数据模型定义函数
预约数据模型文件(appointment.py)新增几个字段方便一些功能的实现。
complete_hours = fields.Integer('实际小时数')appointment_hours = fields.Integer('预约小时数')give_money = fields.Integer('赠送金额', help='充值送费')
在其他数据模型定义函数
为了方便管理,我们将读取费用函数代码放到models.system_info.py文件中,这样如果可能修改system_info.py中费用字段时,不用到处找哪里引用了。
新增获取费用函数
def get_charge_hour(self, order_id, auto_type):    result = 0    model_list = self.env['nebula.charge.line'].search_read(        [('order_id', '=', order_id), ('name', '=', auto_type)],        ['charge_hour'])    if model_list:        result = model_list[0]['charge_hour']    return result
新增获取没有分钟的日期,陪驾一般是整点开始。
def appointment_begin_date(add_hours):    now_str = datetime.strftime(datetime.now(), "%Y-%m-%d%H:00:00")    now_date = datetime.strptime(now_str, "%Y-%m-%d%H:%M:%S")    if add_hours != 0:        now_date = now_date + timedelta(hours=add_hours)    return now_date
代码如下:
# -*- coding: utf-8 -*-from odoo import fields, models, apiclass SystemInfo(models.Model):    _name = "nebula.system.info"    _description = "公司费用"    _order = 'name'    active = fields.Boolean('是否启用', default=True)    name = fields.Char('费用名称', size=30, index=True)    start_time = fields.Float('上班时间', default=07.00, digits=(4, 2))    end_time = fields.Float('下班时间', default=23.00, digits=(4, 2))    hours_hour = fields.Integer('每节课小时数', default=3)    wager_hour = fields.Integer('每小时工资', default=50)    note = fields.Text('备注')    order_line = fields.One2many('nebula.charge.line', 'order_id',                                 string='每小时费用', auto_join=True)    _sql_constraints = [('check_uniq_name', 'unique(name)', '不允许费用名称同名')]class ChargeLine(models.Model):    _name = 'nebula.charge.line'    _description = '费用明细'    @api.model    def _select_auto_type(self):        records = self.env['nebula.auto.type'].search([])        return [(r.name, r.name_type) for r in records]    name = fields.Selection(selection='_select_auto_type',                            required=True, string='排档类')    charge_hour = fields.Integer('每小时收费', default=70)    order_id = fields.Many2one('nebula.system.info', string='费用明细行',                               required=True,                               ondelete='cascade', index=True, copy=False)# 获取每小时收费数据def get_charge_hour(self, order_id, auto_type):    result = 0    model_list = self.env['nebula.charge.line'].search_read(        [('order_id', '=', order_id), ('name', '=', auto_type)],        ['charge_hour'])    if model_list:        result = model_list[0]['charge_hour']    return result

最新文章

随机文章