This question has been flagged

I am trying to add a new field in pos order line and update the value in button click.


**pos.oerder.line.py**


    

    class pos_order_line(models.Model):

        _inherit = 'pos.order.line'

        is_promo = fields.Boolean(string='Promo',default=False)


***.js**


    models.Orderline = models.Orderline.extend({


    initialize: function() {

      _super_orderline.initialize.apply(this,arguments);

      this.is_promo = false;

    },


    // Help me to understand this function

    export_as_JSON: function(){ 

        var json = _super_orderline.export_as_JSON.apply(this,arguments);

        json.is_promo = this.is_promo;

        return json;

    },


    // Help me to understand this function

    init_from_JSON: function(json){

        _super_orderline.init_from_JSON.apply(this,arguments);

        this.is_promo = json.is_promo;

    },

  

    });


    var OfferButton = screens.ActionButtonWidget.extend({

    'template': 'PromotioanlButton',

    button_click: function(){

        var self = this;

        self.get_promo_config();

    },



    get_promo_config: function () {

      var self = this;

      var order    = this.pos.get_order();

      var orderline = order.get_orderlines();

      var order_product_ids = []

      _.each(orderline,function(line){

        order_product_ids.push({'product_id':line.product.id,'qty':line.quantity});

      });


      rpc.query({

        model:'promo.config',

        method:'check_promo_offer',

        args :[order_product_ids],

      }).then(function(product_ids){


        _.each(product_ids, function(pid){

          var product  = self.pos.db.get_product_by_id(pid.product_id);



     **// I added new item into cart using below code and its working fine. 

     //It add all the fields below mention except `is_promo`, i mean it still false. 

      //how can i make it true.**

          order.add_product(product, { price:0.0 ,quantity:pid.product_uom_qty,discount:25,is_promo:true,merge:false});

      });

      });

    },

    });




Please suggest any solution.


Avatar
Discard

<?xml version="1.0" encoding="UTF-8"?>

<templates id="template" xml:space="preserve">

<t t-extend="Orderline">

<t t-jquery="ul[class*='info-list']" t-operation="append">

<li class="info orderline-note">

<i class="fa fa-caret-right"/>Note

</li>

</t>

</t>

</templates>

Best Answer

In button click make is_promo true:


    button_click: function(){

        this.is_promo = true;

    }


And in product selection or change, update the lines:


var models = require('point_of_sale.models');
var _super_Order = models.Order.prototype;
models.Order = models.Order.extend({
export_as_JSON: function () {
var json = _super_Order.export_as_JSON.apply(this, arguments);
    if (json.lines){
        ......
        your logic
        ......
        json.lines[j][2].is_promo = this.is_promo;
    }
}

* No need of init_from_JSON

*Export as json will regenerate the order lines(JSON) on product selection/removal/updation etc

Avatar
Discard
Author

@Pranav, what is `j` in this line, `json.lines[j][2].is_promo = this.is_promo;`

And i think it will update `is_promo` for all orderlines, right?

'j' i meant for line number, you can replace 'j' with actual line number.

Author

I changed to this `var len = json.lines.length

json.lines[len-1][2].is_promo = true;`

But in the view it didnt change the value.

No it will not change the view, this json is to pass value to odoo backend.

Author

But i want it to change it in the view too.

How and where you have to display in view?

Author

I updated my question with a screen shot.

Please refer <t t-name="Orderline"> in pos.xml.

Extend view and add your method to return this.is_promo value in models.js same as below.

Eg:

pos.xml

<t t-esc="line.get_quantity_str()" />

models.js:

get_quantity_str: function(){

return this.quantityStr;

},