This question has been flagged
18 Replies
18059 Views

When I run this query on Postgresql :

SELECT code, total FROM hr_payslip_line WHERE slip_id=1

I get :

____________________________
|  code    |    total      |
__________________________
|  BASE    |    57000.00   |
____________________________

And when I run this code :

    sql = '''
        SELECT code, total FROM hr_payslip_line WHERE slip_id=%s
    ''' % (slip.id)
    cr.execute(sql)
    for code, total in cr.fetchall() :                
        raise osv.except_osv(_('Info'),_('code : %s\nTotal :  %s\nSlip ID : %s\nSQL : %s ' % (code, total,slip.id,sql)))

I've used the raise to catch the error I get this dialog :

    Info

code : BASE
Total : None
Slip ID : 1
SQL : SELECT code, total FROM hr_payslip_line WHERE slip_id=1

The problem is that the total return None and not 57000.00 More informations : 1. I've one slip 2. I've one line 3. the field total is a stored function

Avatar
Discard

Can you add slip.id to your debug message and post the result?

Author

The debug is updated, I have used also res = cr.fetchall() and setting res[0] get 'BASE' and res[1] get False

Best Answer

that's because your code is wrong :-) you should have used

for row in cr.fetchall() :                
        raise osv.except_osv(_('Info'),_('code : %s\nTotal :  %s ' % (row[0], row[1])))

you can also use

    for row in cr.dictfetchall() :                
            raise osv.except_osv(_('Info'),_('code : %s\nTotal :  %s ' % (row['code'], row['total'])))

or you can (could?) also use the browse record instead of using an SQL query that ignore the orm (and the access rights checking for example): try with the following:

for line in slip.line_ids:
    raise osv.except_osv(_('Info'),_('code : %s\nTotal :  %s ' % (line.code, line.total)))

if, even with that you cannot get a value for line.total, it means that the total line is not yet computed. To force the computation, just manually call _calculate_total().

Avatar
Discard
Author

I replaced the code by the last one I get the same thing code='BASE' and Total=None,

na... you should have done something wrong. Print the row before the raise please

Author

for row in cr.fetchall() : raise osv.except_osv(_('Info'),_('Row : %s' % (row,))) I get : Info

Row : (u'BASE', None) I guess that the problem is the type of total in the orm because it's a field calcultaed but it's stored !!

i don't get the reason, this kind of code is widely used in OpenERP and without any problem... but you can still use the browse record to access the total -_- I updated my answer with a code avoiding the SQL query...

Author

It's my first thing that I have done, using browse get the same thing, when i print line.total I get None so I switch to SQL as solution but not work, it's a very complicate case, but if i find the problem I will publish it, I'm already advanced In OpenObject, and it's first time that I face som

Author

calling the function _calculate_total() has resolved the problem.

Best Answer

Your code is correct, there are two possibilities:

  • You changed the value of total before or after the code you shown. As you used raise, the transactions are rollbacked and, when you perform your manual query in SQL, you do not see the changed value. Do a cr.commit() just before raise, you may get NULL in your manual SQL query after calling the python code.
  • Both tests are not on the same database.
Avatar
Discard
Author

Of course the moment when the query is executed the value of total is False, there is an interference, but how can I get the same thing as the sql request, I use just one database, and I puted cr.commit() before the sql execution, and always same thing, I can share with you all the file

Best Answer

For me works this way:

  1. sql = "..."

  2. result = self.env.cr.execute(sql)

  3. value = ''

  4. for res in self.env.cr.dictfetchall():

  5.        if (value != ''):

  6.             value += ','

  7.         value += res['number']


  8. record.invoices = value

Hope it helps

Avatar
Discard
Best Answer

cr.execute('''SELECT code, total FROM hr_payslip_line WHERE slip_id=%s''' % (slip.id)) x = cr.fetchone() #Only if it returns one (1) row

raise osv.except_osv('Info', 'code : %s\nTotal : %s' % (x[0], x[1]))

#or to return just one row SELECT code, SUM(total) FROM hr_payslip_line WHERE slip_id=1 GROUP BY code

Avatar
Discard
Author Best Answer

I guess that the problem is an interference between the function compute_sheet in the hr_payroll module and my function

I have a stored fucntion field that get total from what shown above, but I put store=False it works well, for me it's not logic and I declared this as a bug,

There is somthing that not good in this module, as a advanced developper I get always values of stored function in the database using SQL and anawhere, this time it's different for me, If I Try to use cr.commit(), there isn't any changes, if I use cr.rollback() I get an other message that say that a field quantity from the object hr_payslip_line doesn't exist, for my module there is not any treatement of field quantity

So if you have a logic to resolve this problem, I'm gratfull for you

To recap : I want to compute a stored function field, I execute the code above and it don't work

Avatar
Discard

just manually call _calculate_total()

Author

Ohh Quentin you're found a path , Thank you now I see 57000.00, I have to set this resolved

ok let me add this in my answer, then you can check my answer as correct ^^