简体   繁体   中英

Value is undefined returning model in JS Odoo POS

I'm trying to overwrite Orderline price in POS Odoo

My price.js

get_unit_display_price: function(){
    var self = this;                
    var line = self.export_as_JSON();
    var product = this.pos.db.get_product_by_id(line.product_id);        
    fields.product_id  = line.product_id;
    fields.pricelist_id   = this.pos.config.pricelist_id[0];
    fields.uom = product.uom_id;
    fields.line_qty = line.qty;
    fields.price_unit = line.price_unit;
    var model = new Model('pos.order');
     this.total_price = model.call('calculate_price',
            [0, fields]).done(function(result){
                 total_price = result['total_price'];
                 return  result['total_price'];
            });

}

price.xml

    <t t-jquery=".price" t-operation="append">      
        <t t-esc="widget.format_currency(line.get_unit_display_price)"/>
    </t>

I'm getting value total_price from Model (price.py) But returning is undefined in get_unit_display_price in xml file.

How to set value in xml from js after execution of new model function (js value from model) ?.

There are many problem in your code, i can list some:

  1. In your price.js which extends Orderline model, you called function "caculate_price" from backend => it's asynchronous function, so i can't return value for you immediately => your function return undefined before the call success
  2. You don't need export_as_JSON() , you can get value you want (field: product_id, uom, qty, price_unit) directly from Orderline object.
  3. In your "price.xml", you want to call a function from model, you missed parenttheses, it should be like this line.get_unit_display_price() .

How to set value in xml from js after execution of new model function (js value from model) ?.

There are 2 options:

  • Option 1: call method of py file on server via rpc , then wait for response result as you did (i don't recommend this way). So when the call has done, you should get a DOM where value should display in HTML, then update value to it.
  • Option 2: i recommend you implement an method "calculate_price" which will do same logic as server in Orderline model, so your POS can work without the internet (semi-offline mode). Then you can easy call it from xml file. It means you write function calculate_price in your price.js then call it in get_unit_display_price

Hope it help, i hope you will do option 2.

get_orderline: function() {
    var order = this.pos.get_order();
    var orderlines = order.orderlines.models;
    var all_lines = [];
    for (var i = 0; i < orderlines.length; i++) {
        var line = orderlines[i]
        if (line) {
            all_lines.push({
                'product_id': line.product.id,
                'qty': line.quantity,
                'price': line.get_display_price(),
            })
        }
    }
    return all_lines
},

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM