跳至内容
菜单
此问题已终结
4 回复
8549 查看

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

形象
丢弃
最佳答案

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

形象
丢弃
编写者

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;

编写者

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

最佳答案
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);
},
});
})​
形象
丢弃
编写者

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

相关帖文 回复 查看 活动
1
5月 21
2970
2
5月 20
9581
1
1月 20
4813
0
3月 19
3197
0
10月 18
5986