openerp学习笔记 计算字段、关联字段(7.0中非计算字段、关联字段只读时无法修改保存的问题暂未解决)

时间:2022-01-15 00:10:39

计算字段、关联字段,对象修改时自动变更保存(当 store=True 时),当 store=False 时,默认不支持过滤和分组
7.0中非计算字段、关联字段只读时无法修改保存的问题暂未解决

示例代码: 

class dispatch_work_order(osv.osv):
   
_name="dispatch.work_order"
   
_description="工单"
    _order = "id
asc"
        
   
def _get_bill_month(self, cr, uid, ids, field_name, arg,
context=None):
        res =
{}
        if context is
None:
           
context = {}
        DATETIME_FORMAT =
"%Y-%m-%d"
        for rec in
self.browse(cr, uid, ids,
context=context):
           
billdate = datetime.datetime.strptime(rec.bill_date,
DATETIME_FORMAT)
           
res[rec.id] = billdate.strftime('%Y') + '-' +
billdate.strftime('%m')
       
return res
   
   
def _get_done_month(self, cr, uid, ids, field_name, arg,
context=None):
        res =
{}
        if context is
None:
           
context = {}
        DATETIME_FORMAT =
"%Y-%m-%d"
        for rec in
self.browse(cr, uid, ids,
context=context):
           
if
rec.done_date:
               
donedate = datetime.datetime.strptime(rec.done_date,
DATETIME_FORMAT)
               
res[rec.id] = donedate.strftime('%Y') + '-' +
donedate.strftime('%m')
       
return res

_columns={
       
"name":fields.char("服务单号",size=64, select=True, required=True,
readonly=True),
       
"bill_date":fields.date("开单日期", select=True, required=True,
readonly=True),
       
'bill_month':fields.function(_get_bill_month, method=True, type='char',
size=10, string = u'开单月份', store=True,
invisible=True),
       
"customer":fields.many2one("dispatch.customer","客户", select=True, required=True,
readonly=True, states={'draft':[('readonly',False)],
'dealing':[('readonly',False)]}),
       
"mobile":fields.char("手机",size=64,required=True, readonly=True,
states={'draft':[('readonly',False)],
'dealing':[('readonly',False)]}),
       
"phone":fields.char("电话",size=64, readonly=True,
states={'draft':[('readonly',False)],
'dealing':[('readonly',False)]}),
       
"area":fields.many2one("dispatch.area","区域",required=True, readonly=True,
states={'draft':[('readonly',False)],
'dealing':[('readonly',False)]}),
       
'workload':fields.related('area', 'workload', string=u'分值',
type='float', readonly=True,
store=True),
       
'travelling_expenses':fields.related('area', 'travelling_expenses',
string=u'交通费', type='float', readonly=True,
store=True),
       
"address":fields.char("客户地址", size=200, readonly=True,
states={'draft':[('readonly',False)],
'dealing':[('readonly',False)]}),
       
"buy_date":fields.date("购机日期", readonly=True,
states={'draft':[('readonly',False)],
'dealing':[('readonly',False)]}),
       
"brand":fields.many2one("dispatch.brand","品牌型号", readonly=True,
states={'draft':[('readonly',False)],
'dealing':[('readonly',False)]}),
    
"description_of_fault":fields.char("故障描述", size=500, readonly=True,
states={'draft':[('readonly',False)],
'dealing':[('readonly',False)]}),
       
"note":fields.char("备注", size=500, readonly=True,
states={'draft':[('readonly',False)],
'dealing':[('readonly',False)]}),
       
"client_source":fields.many2one("dispatch.client_source","客户来源", readonly=True,
states={'draft':[('readonly',False)],
'dealing':[('readonly',False)]}),
       
"warranty_type":fields.many2one("dispatch.warranty_type","保修类型", readonly=True,
states={'draft':[('readonly',False)],
'dealing':[('readonly',False)]}),
       
"appointment_date":fields.date("预约日期", readonly=True,
states={'draft':[('readonly',False)],
'dealing':[('readonly',False)]}),
       
"appointment_time":fields.selection([("morning","上午"),("afternoon","下午"),("evening","晚上"),],"预约时段",
translate=True,  readonly=True, states={'draft':[('readonly',False)],
'dealing':[('readonly',False)]}),
       
"repair_method":fields.many2one("dispatch.repair_method","维修方式", readonly=True,
states={'draft':[('readonly',False)],
'dealing':[('readonly',False)]}),
       
"series_number":fields.char("序列号",size=64, readonly=True,
states={'draft':[('readonly',False)],
'dealing':[('readonly',False)]}),
       
"buy_address":fields.many2one("dispatch.buy_address","购机地点", readonly=True,
states={'draft':[('readonly',False)],
'dealing':[('readonly',False)]}),
       
"service_card":fields.many2one("dispatch.service_card","服务卡", readonly=True,
states={'draft':[('readonly',False)],
'dealing':[('readonly',False)]}),
       
'service_card_remaining_times':fields.related('service_card', 'remaining_times',
string=u'卡余次数', type='integer', readonly=True,
store=True),
       
"engineer":fields.many2one("hr.employee","工程师",readonly=True,
states={'draft':[('readonly',False)],
'dealing':[('readonly',False)]}),
       
'department':fields.related('engineer', 'department_id', string=u'部门',
type='many2one', relation='hr.department', readonly=True,
store=True),
       
"state":fields.selection([
           
("draft","已接单"),
           
("dealing","已派工"),
           
("done","已完工"),
           
("reply","已交单"),
           
("visit","已回访"),],"状态",
readonly=True),
       
"check_result":fields.char("检测结果", size=500, readonly=True,
states={'draft':[('readonly',False)],
'dealing':[('readonly',False)]}),
       
"measure":fields.char("处理措施", size=500, readonly=True,
states={'draft':[('readonly',False)],
'dealing':[('readonly',False)]}),
       
"service_time_1":fields.datetime("服务开始时间", readonly=True,
states={'draft':[('readonly',False)],
'dealing':[('readonly',False)]}),
       
"service_time_2":fields.datetime("服务结束时间", readonly=True,
states={'draft':[('readonly',False)],
'dealing':[('readonly',False)]}),
       
"amount":fields.float("实收费用",digits=(7,2), readonly=True,
states={'draft':[('readonly',False)],
'dealing':[('readonly',False)]}),
       
"state_use_card":fields.selection([
           
("0","不扣卡"),
           
("1","需扣卡"),],"扣卡", readonly=True, states={'draft':[('readonly',False)],
'dealing':[('readonly',False)]}),
       
"visit_time":fields.datetime("回访时间",
readonly=True),
       
"evaluate":fields.selection([
           
("very_satisfied","非常满意"),
           
("satisfied","满意"),
           
("discontent","不满"),
           
("very_discontent","非常不满"),],"客户评价"),
       
"customer_feedback":fields.char("客户反馈",
size=500),
       
"reduce":fields.boolean("已扣卡",
readonly=True),
       
"done_date":fields.date("完工日期",
readonly=True),
       
'done_month':fields.function(_get_done_month, method=True, type='char',
size=10, string = u'完工月份', store=True,
invisible=True),
       
'create_uid': fields.many2one('res.users', u"创建用户", invisible=False,
readonly=True), #需要在记录中读取该字段或者在视图中显示该字段时,对象中必须包含
   
}
   
dispatch_work_order()

class lx_purchase_order(osv.osv):

    def _get_order(self, cr, uid, ids,
context=None):
        result =
{}
        for line in
self.pool.get('lx.purchase.order.line').browse(cr, uid, ids,
context=context):
           
result[line.order_id.id] = True
       
return result.keys()

    def _amount_all(self, cr, uid, ids, field_name,
arg, context=None):
        res =
{}
        for order in self.browse(cr,
uid, ids,
context=context):
           
res[order.id] =
{
               
"amount_total":
0.0,
               
}
            val =
0.0
            for
line in
order.order_line:
               
val+=
line.subtotal
           
res[order.id]['amount_total']=val
       
return res

_name='lx.purchase.order'
   
_description=u"lx采购单"
   
_columns={
       
'name':fields.char(u'单号',size=64,required=True,
readonly=True),
        'create_uid':
fields.many2one('res.users', u"创建用户", invisible=False, readonly=True),
#需要在记录中读取该字段或者在视图中显示该字段时,对象中必须包含
       
'employee_id':fields.many2one('hr.employee',u'员工',required=True, readonly=True,
states={'draft':[('readonly',False)]}),
       
'department_id':fields.many2one('hr.department',u'部门',required=True,select=True,
readonly=True,
states={'draft':[('readonly',False)]}),
       
'date':fields.date(u'日期',select=True, readonly=True,
states={'draft':[('readonly',False)]}),
       
'supplier':fields.char(u'供应商',size=64,required=True, readonly=True,
states={'draft':[('readonly',False)]}),
       
'state': fields.selection([('draft', u'草稿'),('confirmed', u'已确认')], u'状态',
readonly=True),
       
'note':fields.text(u'备注', readonly=True,
states={'draft':[('readonly',False)]}),
       
'order_line':fields.one2many('lx.purchase.order.line','order_id',u'采购单明细',
readonly=True,
states={'draft':[('readonly',False)]}),
       
'amount_total': fields.function(_amount_all, digits=(18, 2),
string=u'合计',
                                       
store={
                                           
'lx.purchase.order.line': (_get_order, None,
10),
                                           
}, multi='sums'),
   
}

lx_purchase_order()

class lx_purchase_order_line(osv.osv):

    def _amount_line(self, cr, uid, ids, prop, arg,
context=None):
       
res={}
        for line in
self.browse(cr,uid,ids,context=context):
           
a=self.read(cr,uid,line.id,["price","product_qty"])
           
res[line.id]=a["price"]*a["product_qty"]
       
return res

_name='lx.purchase.order.line'
   
_description=u"lx采购单明细"
   
_columns={
       
'order_id':fields.many2one('lx.purchase.order',u'采购单ID',select=True,
ondelete='cascade'),
       
'product':fields.char(u'产品',size=128,required=True),
       
'price':fields.float(u'价格', digits_compute =
dp.get_precision('lx_purchase_price'),
required=True),
       
'product_qty':fields.integer(u'数量',required=True),
       
'unit':
fields.char(u'计量单位',size=6),
       
'subtotal':fields.function(_amount_line, string=u'小计', digits=(18,
2)),
    }
lx_purchase_order_line()