openerp学习笔记 对象调用(创建、修改),用于后台代码创建和更新对象
#服务卡创建,自动更新服务卡为开卡状态
def create(self, cr, uid, values,
context=None):
values['state'] =
'1'
values['times_limit'] =
values['remaining_times']
#保存时按设置的剩余次数给初始次数赋值
cardid =
super(dispatch_service_card, self).create(cr, uid, values,
context=context)
#开卡记录
service_card=self.pool.get("dispatch.service_card")
service_card_rec=service_card.browse(cr,uid,cardid)
# 自动记录服务卡增减记录(创建对象过程)
card_record =
self.pool.get('dispatch.service.card.recording')
card_record.create(cr, uid, {'customer': service_card_rec.customer.id,
'service_card': service_card_rec.id,
'start_times': 0, 'increase_times': service_card_rec.remaining_times,
'end_times': service_card_rec.remaining_times,
'action': "开卡"})
return cardid
#服务单报工,验证并更新服务卡状态
def set_to_done(self, cr, uid, ids,
context=None):
for rec in
self.browse(cr, uid, ids,
context=context):
if not rec.engineer:
raise
osv.except_osv(_(u'警告!'),_(u'服务单报工时必须指定服务工程师.'))
#判断是否已指定需要扣卡
if not
rec.state_use_card:
raise
osv.except_osv(_(u'警告!'),_(u'请首先指定是否需要扣卡.'))
if (rec.state_use_card ==
"1"):
if not rec.service_card:
raise
osv.except_osv(_(u'警告!'),_(u'需要扣卡时必须指定服务卡.'))
service_card=self.pool.get("dispatch.service_card")
service_card_rec=service_card.browse(cr,uid,rec.service_card.id)
if rec.customer.id !=
service_card_rec.customer.id:
raise
osv.except_osv(_(u'警告!'),_(u'当前服务卡所属客户与当前服务单对应客户不一致,请更换服务卡.'))
if service_card_rec.remaining_times <=
0:
raise
osv.except_osv(_(u'警告!'),_(u'当前服务卡已经扣完,请更换新卡.'))
if rec.bill_date >
service_card_rec.due_date:
raise
osv.except_osv(_(u'警告!'),_(u'当前服务卡已超期,请更换新卡.'))
if service_card_rec.state !=
'1':
raise
osv.except_osv(_(u'警告!'),_(u'当前服务卡未处于开卡状态,请更换服务卡.'))
#扣卡
service_card.write(cr, uid, service_card_rec.id,
{'remaining_times':service_card_rec.remaining_times-1})
#更新服务卡为已完成状态
if service_card_rec.remaining_times ==
1:
service_card.write(cr, uid, service_card_rec.id,
{'state':'2'}) #(更新对象过程)
#
自动记录服务卡增减记录
card_record =
self.pool.get('dispatch.service.card.recording')
card_record.create(cr, uid, {'customer': rec.customer.id, 'service_card':
service_card_rec.id,
'start_times': service_card_rec.remaining_times, 'decrease_times': 1,
'end_times': service_card_rec.remaining_times-1,
'work_order': rec.id, 'action':
"扣卡"})
return self.write(cr, uid, ids, {'state':'done', 'done_date':
datetime.datetime.now()})
else:
return self.write(cr, uid, ids, {'service_card':False,
'service_card_remaining_times':False, 'state':'done', 'done_date':
datetime.datetime.now()}) #(更新对象过程)
#新增产品时,记录初始成本
def
create(self, cr, uid, values,
context=None):
productid =
super(dispatch_product, self).create(cr, uid, values,
context=context)
costrecord =
self.pool.get('dispatch.product.cost.recording')
costrecord.create(cr, uid, {'dispatch_product': productid, 'cost':
values['cost']})
return
productid
#修改产品时,判断成本是否修改并记录成本变更
def write(self, cr, uid,
ids, vals, context=None):
#
更新成本是自动记录
if (vals != [])
and
vals.has_key('cost'): #(更新时判断对象中的字段是否被修改)
costrecord =
self.pool.get('dispatch.product.cost.recording')
for productid in
ids:
costrecord.create(cr, uid, {'dispatch_product': productid, 'cost':
vals['cost']})
return
super(dispatch_product, self).write(cr, uid, ids, vals, context=context)