Openerp onchange 金额自动转为大写
-
1.对象里定义
<br />contract_amount': fields.float('合同金额', digits_compute=dp.get_precision('contract_amount'),required=True),'contract_amount_big': fields.char('合同金额大写', required=True),<br /><br /><br />
2. xml里 onchange
<field name="contract_amount" on_change="onchange_contractamount(contract_amount)"/>
3. 方法实现<br />def onchange_contractamount(self, cr, uid, ids, contract_amount): if contract_amount > 0 : big = self.numtoCny(contract_amount) return {'value':{'contract_amount_big': big}} else : return {}<br />
4.需要注意的是 因为onchange里再次调用了其他方法,导致总是报参数个数不统一的异常,莫名其妙
经过 [font=verdana]@重庆-mrshelly @南京-海飞 @上海-gavin @南京-ccdos [/font]
[font=verdana]尤其是 总监大人解释后才明白[/font]
重庆-mrshelly(49812643) 15:30:23
因为你使用了 self.numtoCny 所以, 有一个默认的 self 参数传过去了.
所以 只需要将 中文金额大写 的函数传参时注意一下就好了<br /> #人民币金额转大写程序Python版本<br /> #Copyright: zinges at foxmail.com<br /> #blog: http://zingers.iteye.com br /> #感谢zinges提供了Python的版本<br /><br /><br /> def numtoCny(self,contract_amount):<br /><br /><br /> capUnit = ['万','亿','万','圆','']<br /> capDigit = { 2:['角','分',''], 4:['仟','佰','拾','']}<br /> capNum=['零','壹','贰','叁','肆','伍','陆','柒','捌','玖']<br /> snum = str('%019.02f') % contract_amount<br /> if snum.index('.')>16:<br /> return ''<br /> ret,nodeNum,subret,subChr='','','',''<br /> CurChr=['','']<br /> for i in range(5):<br /> j=int(i*4+math.floor(i/4))<br /> subret=''<br /> nodeNum=snum[j:j+4]<br /> lens=len(nodeNum)<br /> for k in range(lens):<br /> if int(nodeNum[k:])==0:<br /> continue<br /> CurChr[k%2] = capNum[int(nodeNum[k:k+1])]<br /> if nodeNum[k:k+1] != '0':<br /> CurChr[k%2] += capDigit[lens][k]<br /> if not ((CurChr[0]==CurChr[1]) and (CurChr[0]==capNum[0])):<br /> if not((CurChr[k%2] == capNum[0]) and (subret=='') and (ret=='')):<br /> subret += CurChr[k%2]<br /> subChr = [subret,subret+capUnit][subret!='']<br /> if not ((subChr == capNum[0]) and (ret=='')):<br /> ret += subChr<br /><br /><br /> return [ret,capNum[0]+capUnit[3]][ret=='']<br />
-
我简单测试了下,一直测到9位数,表现良好
-
测试程序代码正常,在数据维护时出现错误:
subChr = [subret,subret+capUnit][subret!='']
TypeError: cannot concatenate 'str' and 'list' objects
有待解决! -
[quote author=openerp_feng link=topic=14575.msg27073#msg27073 date=1387183648]
测试程序代码正常,在数据维护时出现错误:
subChr = [subret,subret+capUnit][subret!='']
TypeError: cannot concatenate 'str' and 'list' objects
有待解决!
[/quote]
测试数据呢,请提供一下 -
123
123.785
随便录几个数字都是同样的错误 -
很有价值,谢谢分享,有待测试。