0
votes

I'm trying to extended an existing report from a vendor module, inserting the amount converted to text, so I started from scratch creating the module for my report:

This is my report_file.py

from openerp import api, models
from num2words import num2words

class ReportReceiptReprint(models.AbstractModel):
    _name = 'report.aces_pos_reorder.receipt_reprint'

@api.multi
def _numwords(val):
    pretext = val
    text = ''
    entire_part = int((str(pretext).split('.'))[0])
    decimal_part = int((str(pretext).split('.'))[1])  
    text+=num2words(entire_part, lang='es').upper()
    text+=' CON '
    text+=num2words(decimal_part, lang='es').upper()
    if decimal_num > 1:
        text+= ' CENTAVOS '
    else:
        text+= ' CENTAVO '
    return text

@api.multi
def render_html(self, docids, data=None):
    report = self.env['report']._get_report_from_name('aces_pos_reorder.receipt_reprint')
    docargs = {
        'doc_ids': docids,
        'doc_model': report.model,
        'docs': self,
        'num_words': _numwords,
        }
    return self.env['report'].render('aces_pos_reorder.receipt_reprint', docargs)

And my report_file.xml

<?xml version="1.0" encoding="utf-8"?>
<odoo>
<data>
   <template id="report_receipt_reprint_inherit" inherit_id="aces_pos_reorder.receipt_reprint">
     <xpath expr="//div[@class='numbers_text']" position="replace">
        <div style="font-size: 14px; width: 100%">
          <b>Son: <span t-esc="num_words(receipt.amount_total)"/></b>
        </div>
     </xpath>
   </template>
</data>
</odoo>

but I just get this:

Error to render compiling AST
TypeError: 'NoneType' object is not callable
Template: 1130
Path: /templates/t/t/div[2]/div[9]/div[1]/b/span
Node: <span t-esc="num_words(receipt.amount_total)"/>

This happen even if I put the function _numwords() into the models.py file of the main module. The extend for the module works if I avoid to call the function, the text is inserted as expected. num2words is installed.

I'll appreciate any comment or suggestion! Thanks in advance.

==EDIT==

report_file.py

from odoo import api, models
from num2words import num2words


class ReportReceiptReprint(models.AbstractModel):
      _name = 'report.aces_pos_reorder.receipt_reprint'

@api.multi
def _numwords(self, val):
    pretext = float(val)
    text = ''
    entire_part = int((str(pretext).split('.'))[0])
    decimal_part = (pretext-float(entire_part))*100
    decimal_part = int((str(decimal_part).split('.'))[0])
    text += num2words(entire_part, lang='es').upper()
    text += ' LEMPIRAS CON '
    text += num2words(decimal_part, lang='es').upper()
    if decimal_part > 1:
        text += ' CENTAVOS '
    else:
        text += ' CENTAVO '
    return text

@api.multi
def render_html(self, docids, data=None):
    # report = self.env['report']._get_report_from_name('aces_pos_reorder.receipt_reprint')
    docargs = {
        'doc_ids': docids,
        'doc_model': self.env.context.get('active_model'),
        'docs': self.env['pos.order'].browse(int(docids)),
        'num_words': self._numbwords,
    }
    return self.env['report'].render('aces_pos_reorder.receipt_reprint', docargs)

I don't know why the commented line "# report =" was causing a lot of problems.

2
Maybe self.numwords also try with braces and paramMuhammad Yusuf
I appreciate your comment @MuhammadYusuf , but I changed the code like this: ` 'num_words': self._numwords(val), ` but still getting the same message.RauLeo88

2 Answers

0
votes

On your python file just change the function calling to,

'num_words': self._numwords(data['form']['amount_total']), & def _numwords(self, val)

Where make sure to check that your passed value of amount_total gets from your data and that value you have to pass on the _numwords function. And on the XML file t-esc="num_words" try to access the value like this.

Thanks

0
votes
@api.milti
def _numbwords(self, val)....

@api.multi
def render_html(self, docids, data=None):
# report = self.env['report']._get_report_from_name('aces_pos_reorder.receipt_reprint')
# This line caused problems so commented out
docargs = {
    'doc_ids': docids,
    'doc_model': self.env.context.get('active_model'),
    'docs': self.env['pos.order'].browse(int(docids)), # have to include the model and methods to index the documents
    'num_words': self._numbwords,
}
return self.env['report'].render('aces_pos_reorder.receipt_reprint', docargs)

That's all