<commit_before>odoo.define('jim_telesale.new_order_widgets2', function (require) {
"use strict";
var Model = require('web.DataModel');
var NewOrderWidgets = require('telesale.new_order_widgets');
var core = require('web.core');
var _t = core._t;

var OrderWidget = NewOrderWidgets.OrderWidget.include({
    create_line_from_vals: function(product_id, line_vals){
        var added_line = this.create_line_empty(product_id);
        added_line.set('qty', line_vals.qty || 1.0);
        added_line.set('pvp', line_vals.price || 0.0);
        added_line.set('discount', line_vals.discount || 0.0);
        added_line.set('taxes_ids', line_vals.tax_ids || []);
        added_line.update_line_values();
        return added_line;
    },

    // Creates a line with product and unit seted
    create_line_empty: function(product_id){
        var added_line = this._super(product_id);
        var product_obj = this.ts_model.db.get_product_by_id(product_id);
        var description = product_obj.display_name;
        if (product_obj.description_sale){
            description = description + '\n' + product.description_sale
        }
        added_line.set('description', description);
        return added_line;
    },

});

var OrderlineWidget = NewOrderWidgets.OrderlineWidget.include({

    // Link change chained discount to method
    set_input_handlers: function() {
        this._super();
        this.$('.col-chained_discount').change(_.bind(this.set_value, this, 'chained_discount'));
        this.$('.col-chained_discount').focus(_.bind(this.click_handler, this, 'chained_discount'));

        this.$('.col-description').blur(_.bind(this.set_value, this, 'description'));
        this.$('.col-description').focus(_.bind(this.click_handler, this, 'description'));

        this.$('.col-note').blur(_.bind(this.set_value, this, 'note'));
        this.$('.col-note').focus(_.bind(this.click_handler, this, 'note'));
    },


    // Convert a chained discout like (10.5+20) in a float (20.5)
    // or returns false if something wrong.
    chained_discount2float: function(value){
        if (!value){
            return 0.0
        }
        var split_disc = value.split('+')
        var discount = 0.0
        var disc = 0
        for (var i in split_disc){
            disc = split_disc[i]
            if (isNaN(disc) || disc == "") {
               return false // Break
            }
            else{
              discount = discount + parseFloat(disc);
            }
        }
        return discount;

    },

    // Manage chained_discount
    set_value: function(key) {
        this._super(key)
        if (key == 'chained_discount'){
            var value = this.$('.col-chained_discount').val();

            var discount = this.chained_discount2float(value);
            if (!discount){
                alert(value + _t("is not a valid format for chained discount. It must be something like 23+5.2+1"));
                this.model.set('discount', 0.0);
                this.model.set('chained_discount', 0.0);
            }
            else{
                this.model.set('discount', discount);
            }
            this.refresh('chained_discount');
        }
    },

    perform_onchange: function(key){
        if (key == 'pvp'){
            this.refresh('chained_discount');
        }
        if (key == 'qty'){
            return;
        }

        this._super(key);

        if (key == 'template'){
            var value = this.$('.col-'+key).val();
            // Case name not valid
            var template_obj = this.get_template();
          
            // Get product from model
            if (template_obj){
                var description = template_obj.display_name;
                this.model.set('description', description);
            }
            
        }

    },

    // OVERWRITED Get global stock available in product_id change
    call_product_id_change: function(product_id, add_qty){
        var self = this;

        if (!add_qty){
            add_qty = 1.0
        }

        var customer_id = self.ts_model.db.partner_name_id[self.order.get('partner')];
        var pricelist_id = self.ts_model.db.pricelist_name_id[self.order.get('pricelist')];
        var model = new Model("sale.order.line");
        return model.call("ts_product_id_change", [product_id, customer_id, pricelist_id])
        .then(function(result){
            var product_obj = self.ts_model.db.get_product_by_id(product_id);
            var uom_obj = self.ts_model.db.get_unit_by_id(product_obj.uom_id)
            var description = product_obj.display_name;
            if (product_obj.description_sale){
                description = description + '\n' + product.description_sale
            }
            self.model.set('description', description);
            self.model.set('code', product_obj.default_code || "");
            self.model.set('product', product_obj.display_name || "");
            self.model.set('taxes_ids', result.tax_id || []); //TODO poner impuestos de producto o vacio
            self.model.set('unit', self.model.ts_model.db.unit_by_id[result.product_uom].name);
            self.model.set('qty', add_qty);
            self.model.set('discount', result.discount || 0.0);
            self.model.set('chained_discount', result.chained_discount || '0.00');
            self.model.set('standard_price', result.standard_price, 0.0);
            self.model.set('pvp', self.ts_model.my_round( result.price_unit));

            var subtotal = self.model.get('pvp') * self.model.get('qty') * (1 - self.model.get('discount') / 100.0)
            self.model.set('total', self.ts_model.my_round(subtotal || 0,2));

            // ADDED
            self.model.set('global_available_stock', result.global_available_stock || 0.0)

            self.refresh('qty');
            self.$('.col-qty').select();
        });
    },<commit_msg>[MOD] im_telesale. modificado en el servidor se ñade para updates<commit_after>odoo.define('jim_telesale.new_order_widgets2', function (require) {
"use strict";
var Model = require('web.DataModel');
var NewOrderWidgets = require('telesale.new_order_widgets');
var core = require('web.core');
var _t = core._t;

var OrderWidget = NewOrderWidgets.OrderWidget.include({
    create_line_from_vals: function(product_id, line_vals){
        var added_line = this.create_line_empty(product_id);
        added_line.set('qty', line_vals.qty || 1.0);
        added_line.set('pvp', line_vals.price || 0.0);
        added_line.set('discount', line_vals.discount || 0.0);
        added_line.set('taxes_ids', line_vals.tax_ids || []);
        added_line.update_line_values();
        return added_line;
    },

    // Creates a line with product and unit seted
    create_line_empty: function(product_id){
        var added_line = this._super(product_id);
        var product_obj = this.ts_model.db.get_product_by_id(product_id);
        var description = product_obj.display_name;
        if (product_obj.description_sale){
            description = description + '\n' + product.description_sale
        }
        added_line.set('description', description);
        return added_line;
    },

});

var OrderlineWidget = NewOrderWidgets.OrderlineWidget.include({

    // Link change chained discount to method
    set_input_handlers: function() {
        this._super();
        this.$('.col-chained_discount').change(_.bind(this.set_value, this, 'chained_discount'));
        this.$('.col-chained_discount').focus(_.bind(this.click_handler, this, 'chained_discount'));

        this.$('.col-description').blur(_.bind(this.set_value, this, 'description'));
        this.$('.col-description').focus(_.bind(this.click_handler, this, 'description'));

        this.$('.col-note').blur(_.bind(this.set_value, this, 'note'));
        this.$('.col-note').focus(_.bind(this.click_handler, this, 'note'));
    },


    // Convert a chained discout like (10.5+20) in a float (20.5)
    // or returns false if something wrong.
    chained_discount2float: function(value){
        if (!value){
            return 0.0
        }
        var split_disc = value.split('+')
        var discount = 0.0
        var disc = 0
        for (var i in split_disc){
            disc = split_disc[i]
            if (isNaN(disc) || disc == "") {
               return false // Break
            }
            else{
              discount = discount + parseFloat(disc);
            }
        }
        return discount;

    },

    // Manage chained_discount
    set_value: function(key) {
        this._super(key)
        if (key == 'chained_discount'){
            var value = this.$('.col-chained_discount').val();

            var discount = this.chained_discount2float(value);
            if (!discount){
                alert(value + _t("is not a valid format for chained discount. It must be something like 23+5.2+1"));
                this.model.set('discount', 0.0);
                this.model.set('chained_discount', 0.0);
            }
            else{
                this.model.set('discount', discount);
            }
            this.refresh('chained_discount');
        }
    },

    perform_onchange: function(key){
        if (key == 'pvp'){
            this.refresh('chained_discount');
        }
        if (key == 'qty'){
            return;
        }

        this._super(key);

        if (key == 'template'){
            var value = this.$('.col-'+key).val();
            // Case name not valid
            var template_obj = this.get_template();
          
            // Get product from model
            if (template_obj){
                var description = template_obj.display_name;
                this.model.set('description', description);
            }
            
        }

    },

    // OVERWRITED Get global stock available in product_id change
    call_product_id_change: function(product_id, add_qty){
        var self = this;

        if (!add_qty){
            add_qty = 1.0
        }

        var customer_id = self.ts_model.db.partner_name_id[self.order.get('partner')];
        var pricelist_id = self.ts_model.db.pricelist_name_id[self.order.get('pricelist')];
        var model = new Model("sale.order.line");
        return model.call("ts_product_id_change", [product_id, customer_id, pricelist_id])
        .then(function(result){
            var product_obj = self.ts_model.db.get_product_by_id(product_id);
            var uom_obj = self.ts_model.db.get_unit_by_id(product_obj.uom_id)
            var description = product_obj.display_name;
            if (product_obj.description_sale){
                description = description + '\n' + product.description_sale
            }
            self.model.set('description', description);
            self.model.set('code', product_obj.default_code || "");
            self.model.set('product', product_obj.display_name || "");
            self.model.set('taxes_ids', result.tax_id || []); //TODO poner impuestos de producto o vacio
            self.model.set('unit', self.model.ts_model.db.unit_by_id[result.product_uom].name);
            self.model.set('qty', add_qty);
            self.model.set('discount', result.discount || 0.0);
            self.model.set('chained_discount', result.chained_discount || '0.00');
            self.model.set('standard_price', result.standard_price, 0.0);
            self.model.set('pvp', self.ts_model.my_round( result.price_unit));

            var subtotal = self.model.get('pvp') * self.model.get('qty') * (1 - self.model.get('discount') / 100.0)
            self.model.set('total', self.ts_model.my_round(subtotal || 0,2));

            // ADDED
            self.model.set('global_available_stock', result.global_available_stock || 0.0)

            self.refresh('qty');
            self.$('.col-qty').select();
            return result 
        });
    },
{'fraction_non_alphanumeric': 0.13766584974224627, 'fraction_numerical': 0.008281923434462943, 'mean_word_length': 2.251098901098901, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1880117', 'n_tokens_mistral': 3700, 'n_tokens_neox': 3556, 'n_words': 649}