Odoo中文社区可以通过以下三个域名访问:shine-it.net , odoocn.org,odoo.net.cn

原论坛用户的基本信息和发帖这里都予以保留,请注意:原论坛用户无需重新注册新用户,但是您的密码需要重置

开发人员可以登录gitter讨论组: http://gitter.im/odoo-china/Talk, 需要github账号

如果您登录系统碰到问题,请在微信公众号留言:

Openerp onchange 金额自动转为大写



  • 1.对象里定义 <br /><br />

    <br />contract_amount&#039;: fields.float(&#039;合同金额&#039;, digits_compute=dp.get_precision(&#039;contract_amount&#039;),required=True),&#039;contract_amount_big&#039;: fields.char(&#039;合同金额大写&#039;, required=True),<br /><br /><br />
    

    <br /><br /><br /><br /><br />2. xml里  onchange  <br /><br /><br /><br /><field name="contract_amount" on_change="onchange_contractamount(contract_amount)"/><br /><br /><br /><br /><br />3. 方法实现<br /><br /><br />

    <br />def onchange_contractamount(self, cr, uid, ids, contract_amount):&nbsp; &nbsp; &nbsp; &nbsp; if contract_amount &gt; 0 :&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; big = self.numtoCny(contract_amount)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return {&#039;value&#039;:{&#039;contract_amount_big&#039;: big}}&nbsp; &nbsp; &nbsp; &nbsp; else :&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return {}<br />
    

    <br /><br /><br />4.需要注意的是 因为onchange里再次调用了其他方法,导致总是报参数个数不统一的异常,莫名其妙<br /><br /><br />经过 [font=verdana]@重庆-mrshelly @南京-海飞 @上海-gavin @南京-ccdos [/font]<br /><br />[font=verdana]尤其是 总监大人解释后才明白[/font]<br /><br /><br /> 重庆-mrshelly(49812643)  15:30:23<br />因为你使用了 self.numtoCny 所以,  有一个默认的 self 参数传过去了.<br /><br /><br />所以  只需要将  中文金额大写 的函数传参时注意一下就好了<br /><br />

    <br />&nbsp; &nbsp;  #人民币金额转大写程序Python版本<br />&nbsp; &nbsp; #Copyright: zinges at foxmail.com<br />&nbsp; &nbsp; #blog: http://zingers.iteye.com br />&nbsp; &nbsp; #感谢zinges提供了Python的版本<br /><br /><br />&nbsp; &nbsp; def numtoCny(self,contract_amount):<br /><br /><br />&nbsp; &nbsp; &nbsp; &nbsp; capUnit = &#91;&#039;万&#039;,&#039;亿&#039;,&#039;万&#039;,&#039;圆&#039;,&#039;&#039;]<br />&nbsp; &nbsp; &nbsp; &nbsp; capDigit = { 2:&#91;&#039;角&#039;,&#039;分&#039;,&#039;&#039;], 4:&#91;&#039;仟&#039;,&#039;佰&#039;,&#039;拾&#039;,&#039;&#039;]}<br />&nbsp; &nbsp; &nbsp; &nbsp; capNum=&#91;&#039;零&#039;,&#039;壹&#039;,&#039;贰&#039;,&#039;叁&#039;,&#039;肆&#039;,&#039;伍&#039;,&#039;陆&#039;,&#039;柒&#039;,&#039;捌&#039;,&#039;玖&#039;]<br />&nbsp; &nbsp; &nbsp; &nbsp; snum = str(&#039;%019.02f&#039;) % contract_amount<br />&nbsp; &nbsp; &nbsp; &nbsp; if snum.index(&#039;.&#039;)&gt;16:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return &#039;&#039;<br />&nbsp; &nbsp; &nbsp; &nbsp; ret,nodeNum,subret,subChr=&#039;&#039;,&#039;&#039;,&#039;&#039;,&#039;&#039;<br />&nbsp; &nbsp; &nbsp; &nbsp; CurChr=&#91;&#039;&#039;,&#039;&#039;]<br />&nbsp; &nbsp; &nbsp; &nbsp; for i in range(5):<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; j=int(i*4+math.floor(i/4))<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; subret=&#039;&#039;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nodeNum=snum[j:j+4]<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lens=len(nodeNum)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for k in range(lens):<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if int(nodeNum[k:])==0:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CurChr[k%2] = capNum[int(nodeNum[k:k+1])]<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if nodeNum[k:k+1] != &#039;0&#039;:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CurChr[k%2] += capDigit[lens][k]<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if&nbsp; not ((CurChr[0]==CurChr[1]) and (CurChr[0]==capNum[0])):<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if not((CurChr[k%2] == capNum[0]) and (subret==&#039;&#039;) and (ret==&#039;&#039;)):<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; subret += CurChr[k%2]<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; subChr = [subret,subret+capUnit][subret!=&#039;&#039;]<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if not ((subChr == capNum[0]) and (ret==&#039;&#039;)):<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ret += subChr<br /><br /><br />&nbsp; &nbsp; &nbsp; &nbsp; return [ret,capNum[0]+capUnit[3]][ret==&#039;&#039;]<br /> 
    

    <br /> <br />



  • 不明觉厉.....................<br /><br />分享便是极好的....



  • 这个 金额转大写  完美不 ?



  • 我简单测试了下,一直测到9位数,表现良好



  • 测试程序代码正常,在数据维护时出现错误:<br />    subChr = [subret,subret+capUnit][subret!='']<br />TypeError: cannot concatenate 'str' and 'list' objects<br /><br />有待解决!<br />



  • [quote author=openerp_feng link=topic=14575.msg27073#msg27073 date=1387183648]<br />测试程序代码正常,在数据维护时出现错误:<br />    subChr = [subret,subret+capUnit][subret!='']<br />TypeError: cannot concatenate 'str' and 'list' objects<br /><br />有待解决!<br />[/quote]<br /><br /><br />测试数据呢,请提供一下



  • 123<br />123.785<br />随便录几个数字都是同样的错误



  • 因为出现这样的错误提示:<br />    subChr = [subret,subret+capUnit][subret!='']<br />TypeError: cannot concatenate 'str' and 'list' objects<br /><br />所以,需要这样修改:<br />subChr = [subret,subret+[color=red]"".join([/color]capUnit[color=red])[/color]][subret!='']<br />



  • 有价值的东东,我也极想要



  • 可以参考下如文件,进行转换大写金额。。



  • 很有价值,谢谢分享,有待测试。