odoo 12自定义pos锁定屏幕模块this.pos为空

时间:2019-03-04 20:12:10

标签: javascript underscore.js typeerror odoo

我为odoo 12创建了一个模块来锁定pos屏幕,该代码在odoo 10中可以正常工作,但是给出错误this.pos为null或this.pos.currency未定义。在models.js中,我想切换不同收银员的订单,但是this.pos未初始化,我应该怎么做才能使其初始化? 这是我的代码:

odoo.define('pos_user_lock.models',
//['point_of_sale.models','point_of_sale.screens','point_of_sale.chrome'
//,'web.ajax','web.core'],
function (require) {
"use strict";

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

var screens = require('point_of_sale.screens');

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

var chrome = require('point_of_sale.chrome');

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

var QWeb = core.qweb;

var _t = core._t;

models.Order = models.Order.extend({

    initialize: function(attributes,options){

        var order =  models.Order.__super__.initialize.call(this,attributes,options);

        if (!this.cashier)

        {

            this.cashier = this.pos.cashier || this.pos.user;

        }





        this.is_last_selected = (this.is_last_selected === undefined) ? false:this.is_last_selected;

        return order;

    },

    finalize: function() {

        var res =  models.Order.__super__.finalize.call(this);

        this.pos.lock_user_screen();

    },

    init_from_JSON: function(json) {

        var user = this.get_user_by_id(json.user_id);

        if (user)

            this.cashier = user;

        this.is_last_selected = json.is_last_selected;

        models.Order.__super__.init_from_JSON.call(this, json);

    },

    export_as_JSON: function() {

        var res =  models.Order.__super__.export_as_JSON.call(this);

        if (this.cashier)

            res.user_id = this.cashier.id ;

        if(this.pos && this.pos.get_cashier())

            res.user_id = this.pos.get_cashier().id ;

        res.is_last_selected = this.is_last_selected;

        return res;

    },

    get_user_by_id: function(id) {

        var users = this.pos.users;

        for (var i = 0; i < users.length; i++) {

            var user = users[i];

            if (user.id == id)

                return user;

        }

    },



});



models.PosModel = models.PosModel.extend({

    initialize: function(session, attributes) {

        models.PosModel.__super__.initialize.call(this, session, attributes);



        var self = this;

        alert(JSON.stringify(attributes.pos));

        this.ready.then(function(){

            if (self.config.auto_push_order)

                self.config.iface_precompute_cash = true;

        });

    },

    is_locked: function(){

        if (

            this.gui.current_popup 

            && this.gui.current_popup.template == 'PasswordPinPopupWidget'

        ){

            return true;

        }

        return false;

    },

    unlock: function(){

        if (this.is_locked()){

            this.gui.close_popup();

        }

    },

    lock_user_screen: function(){

        var self = this;

        if (!this.config.lock_users) return;

        this.gui.show_popup('passwordPin',{

            'title': _t('Insert password or scan your barcode'),

        });

    },

    get_last_order: function(){

        var orders = this.get_order_list();

        for (var i = 0; i < orders.length; i++) {

            if (orders[i].is_last_selected) {

                return orders[i];

            }

        }

        return null;

    },

    set_last_order: function(order){

        var orders = this.get_order_list();

        for (var i = 0; i < orders.length; i++) {

            if (orders[i].uid == order.uid) {

                orders[i].is_last_selected = true;

            }else{

                orders[i].is_last_selected = false;

            }

        }

        return null;

    },

    set_order: function(order){

        models.PosModel.__super__.set_order.call(this, order);

        this.set_last_order(order);

    },
    get_order_list: function(){
        var orders = models.PosModel.__super__.get_order_list.call(this);
        var c_orders = [];
        var cashier = this.cashier || this.user;
        for (var i = 0; i < orders.length; i++) {
            if (cashier && orders[i].cashier.id === cashier.id) {
                c_orders.push(orders[i]);
            }
        }
        return c_orders;
    },
    switch_order: function(){
        var self = this;
        if(self.pos)
        {
        }
        else
        {
            alert("self.pos is null");
        }
        if(self !== undefined && self != null && self.pos !== undefined && self.pos.currency != null)
        {
            alert("in switch order");
            //console.log(this.pos.currency);
                if (!self.config.lock_users) return;
                var user = self.pos.get_cashier() || self.user;
                var orders = self.get_order_list();
                var last_order = self.get_last_order() || orders[0];
                if (orders.length) {
                    self.set_order(last_order);
                }
                else {
                    self.add_new_order();
                }
        }
    },
    set_cashier: function(user){
        models.PosModel.__super__.set_cashier.call(this,user);
        if(this.pos)
        {
            alert("this.pos is not null in set_cashier");
        }
        else
        {
            alert("this.pos is null in set_cashier");
        }
        this.switch_order(this);
        if (!this.table && this.config.iface_floorplan)
            this.set_table(null);
    },
});
return models;
});

1 个答案:

答案 0 :(得分:0)

网页显示this.pos为null。并且this.currency为null。 我通过添加以下内容找到了解决方案:

this.pos = this;

在PosModel的内部初始化函数中。