Přejít na obsah
Menu
You need to be registered to interact with the community.
This question has been flagged
4 Odpovědi
8194 Zobrazení

Hi.

I want to inherit function "get_due" of "Models.Order".

Here is the code in original function in a file point_of_sale/static/src/js/models.js:

get_due: function(paymentline) {
if (!paymentline) {
var due = this.get_total_with_tax() - this.get_total_paid();
} else {
var due = this.get_total_with_tax();
var lines = this.paymentlines.models;
for (var i = 0; i < lines.length; i++) {
if (lines[i] === paymentline) {
break;
} else {
due -= lines[i].get_amount();
}
}
}
return round_pr(due, this.pos.currency.rounding);
},

i want to customize that function and add some code like this: 

odoo.define('pos_mi_module.mi_module', function (require) {
"use strict";

var Models = require('point_of_sale.models');

Models.Order = Models.Order.extend({
get_due: function (paymentline) {
if (!paymentline) {
var due = this.get_total_with_tax() - this.get_total_paid();
} else {
var due = this.get_total_with_tax();
var lines = this.paymentlines.models;
for (var i = 0; i < lines.length; i++) {
if (lines[i] === paymentline) {
break;
} else {
due -= lines[i].get_amount();
}
}
}
return round_pr(due, this.pos.currency.rounding);
},
});
})

and after updating the module when i enter the session of the POS it remains black, without showing anything, and no error appears in the browser console. Both codes of "get_due" are the same because use copy and paste. What happen with this?.

Thanks in advance

Avatar
Zrušit
Nejlepší odpověď

POS model is actually the backbone model. it's not the same as odoo class(widget) which is used in the rest of the backend code.

For the backend class javascript prototype inheritance handle implicitly by odoo class(widget) but for the backbone model, you have to handle it manually.

here is an example of how to override the backbone model method

Avatar
Zrušit
Autor

Ravi Gadhia, i read your answer and the difference lies in "var _super_order = models.Order.prototype;"; but in my case add the variable and the result is the same. I think that is rather used when you need to call the parent class methods just as you do with "this._super();". Please correct me if I am wrong. Thank you.

There must be console error if pos screen black after loading model. please check again your console window again

quick copy-paste your code in my editor for syntax/linting check it seems 'round_pr' is not defined.

have you imported relative asset/function?

try

var utils = require('web.utils');

var round_pr = utils.round_precision;

Autor

Ravi Gadhia, thank you very much, it seems that that was my problem, "round_pr"

Nejlepší odpověď
Hi Raudel, Try like below :

odoo.define('pos_mi_module.mi_module', function (require) {
"use strict";

var Models = require('point_of_sale.models');


var _super_order = models.Order.prototype;
Models.Order = Models.Order.extend({


initialize: function() {
_super_order.initialize.apply(this,arguments);

},

get_due: function (paymentline) {
if (!paymentline) {
var due = this.get_total_with_tax() - this.get_total_paid();
} else {
var due = this.get_total_with_tax();
var lines = this.paymentlines.models;
for (var i = 0; i < lines.length; i++) {
if (lines[i] === paymentline) {
break;
} else {
due -= lines[i].get_amount();
}
}
}
return round_pr(due, this.pos.currency.rounding);
},
});
})​
Avatar
Zrušit
Autor

Jithin, thank you very much, but I also tried that and it didn't solve the problem. The solution was in the comment of Ravi Gadhia

Related Posts Odpovědi Zobrazení Aktivita
1
kvě 21
2679
2
kvě 20
9096
1
led 20
4446
0
bře 19
2856
0
říj 18
5583