Skip to Content
Meniu
Trebuie să fiți înregistrat pentru a interacționa cu comunitatea.
Această întrebare a fost marcată
2 Răspunsuri
5172 Vizualizări

I am customizing the attendance model and made a wizard that displays a table with an overview of worktime, begin, end and overtime. 

Fields worktime, begin, end and overtime are all fields.Float()

I am using widget='float_time' to display them in the form view of the model. 






I wanted to create a report of that overview that is displayed as qweb-html and can be printed if necessary. 

I created an xml template and used 

It seemed to work because the end field was printed like 18:21.

I compared the form view and the qweb view and discovered that the values are different. 

Some values are just one minute below or above the form view value (ok the rounding seems to work differently) but others are completely nonsense. 

Especially the field overtime differs a lot. 

Form view: -06:40  qweb: -07:19

the correct value that I calculated by hand is -06:40 meaning a float value of -6.66666


Question 1) Where is the calculation difference ? What function is used by the form view and what function is used by the qweb view ?

I only found /web/static/src/js/fields/field_utils.js where there is a formatFloatTime() and a parseFloatTime() function. 


Question 2) Why are the qweb values sometimes completely wrong? 


Yes, I made sure that the field values are completely identical. I even displayed the float values in the qweb report and could validate that they are correct. It is "only" a formatting issue.

Imagine profil
Abandonează
Autor

test

Autor

I finally found the answer and if somebody comes across this ...here you are ...

There are indeed two places where conversion is made:

#####################################################

Form view: Javascript method in /web/static/src/js/fields/field_utils.js

#####################################################

function formatFloatTime(value) {

var pattern = '%02d:%02d';

if (value < 0) {

value = Math.abs(value);

pattern = '-' + pattern;

}

var hour = Math.floor(value);

var min = Math.round((value % 1) * 60);

if (min === 60){

min = 0;

hour = hour + 1;

}

return _.str.sprintf(pattern, hour, min);

###########################

QWeb view:

In module base/ir/ir_qweb/fields.py

###########################

class FloatTimeConverter(models.AbstractModel):

""" ``float_time`` converter, to display integral or fractional values as

human-readable time spans (e.g. 1.5 as "01:30").

Can be used on any numerical field.

"""

_name = 'ir.qweb.field.float_time'

_inherit = 'ir.qweb.field'

@api.model

def value_to_html(self, value, options):

hours, minutes = divmod(value * 60, 60)

return '%02d:%02d' % (hours, minutes)

It seems that Math.round and Math.floor behaves differently compared to python divmod function. At least I had huge differences.

Autor Cel mai bun răspuns

Thanks for your answer Karthikeyan if I understand it right, you say there is no difference using the widget in form view and in qweb, but I had different results. There must be a difference. 

Unforunately, I am not used to debugging of javascript otherwise I could follow the code step by step as I do in python code. 


Reply to comment:

If there is a difference while printing in excel where does this come from? Please explain if you know.

Imagine profil
Abandonează

Yeah, the difference will arise only if you print that in excel report

Cel mai bun răspuns

Hi,

    I guess you figured its a formatting issue,

In form view you have used the widget float_time,  to convert the float value into time format (HH:MM)

Same goes while generating a qweb ,  

span t-field="o.your_float_field" t-options= '{"widget": "float_time"}'


I couldn't open and close span tag , make sure you do.

Hope it helps,

Thanks


Imagine profil
Abandonează
Related Posts Răspunsuri Vizualizări Activitate
7
nov. 18
12458
3
ian. 23
6296
1
sept. 20
6203
1
dec. 19
5924
2
nov. 24
7780