barcodes.js 6.98 KB
Newer Older
Administrator committed
1 2 3 4 5 6 7
IFCBarcodes = {
    'codes': {},
    'patterns': [],
    'errors': [],
    init : async function() {
        // as it is a long time response task, restrict it
        if (is_time_to('load_barcodes', 5000)) {
8
            openWaiting('Récupération des informations code-barres....');
Administrator committed
9
            try {
10 11 12 13
                let response = await fetch('/products/barcodes');
                let bc_data = await response.json();

                closeModal();
Administrator committed
14
                if (typeof bc_data.res.error == "undefined") {
15
                    this.patterns = bc_data.res.patterns;
16
                    this.aliases = bc_data.res.aliases;
17 18 19
                    this.codes = bc_data.res.list.pdts;
                    this.uoms = bc_data.res.list.uoms;
                    this.keys = bc_data.res.keys;
Administrator committed
20
                } else {
21
                    this.errors.push(bc_data.res.error);
Administrator committed
22
                }
23 24 25 26 27 28
            } catch (e) {
                err = {msg: e.name + ' : ' + e.message, ctx: 'retrieve barcodes'};
                console.error(err);
                report_JS_error(err, 'products');
                closeModal();
                this.errors.push(JSON.stringify(err));
Administrator committed
29 30 31 32
            }
        }
    },
    display_last_error: function() {
33
        alert(this.errors[this.errors.length - 1]);
Administrator committed
34
    },
35 36 37 38 39
    get_quantity_eq_to_encoded_price: function (value, list_price, currency) {
        let qty = 0;

        try {
            let price = parseFloat(value);
40

41 42 43 44 45 46 47 48 49 50
            if (currency == 'FF')
                price = price / 6.55957;

            qty = parseFloat(price / list_price).toFixed(3);
        } catch (error) {
            console.log(error);
        }

        return qty;
    },
Administrator committed
51 52 53 54
    get_corresponding_odoo_product: function(bc) {
        //console.log('To analyze :' + bc)
        var index = 0,
            pattern_found = false,
55 56 57 58 59
            is_alias = false,
            encoded_value = '',
            pattern_type = '',
            odoo_product = null,
            product_data = null;
Administrator committed
60
        // Let's find out if it matches a pattern
61

Administrator committed
62
        while (index < this.patterns.length -1 && pattern_found === false) {
63
            var pattern = this.patterns[index].pattern;
64 65
            var significant_prefix = pattern.replace(/[^0-9]/g, ''); //remove all but figures

Administrator committed
66
            if (bc.indexOf(significant_prefix) === 0) {
67
                /*
68
                    Submitted barcode-code matches a pattern rule
69 70 71 72 73
                    For example,
                    bc = 0493213018809
                    pattern = 0493...{NNDDD}
                */
                odoo_bc = '';
74
                pattern_found = true;
75
                pattern_type = this.patterns[index].type;
76
                pattern = pattern.replace(/[^0-9.ND]/, '');
77
                bc = bc.slice(0, -1); // remove original check figure
78

79 80 81 82 83
                /*
                  Read pattern character by character
                  to find out Odoo article barcode
                  and encoded_value (weight, price, units, if exists)
                */
Administrator committed
84 85
                for (var i = 0; i < pattern.length; i++) {
                    if (/[0-9]/.exec(pattern[i])) {
86
                        // it's a figure, nothing to do but to add it to string
87
                        odoo_bc += pattern[i];
Administrator committed
88
                    } else if (pattern[i].indexOf('.') === 0) {
89 90 91 92
                        /*
                         it's a substitution character,
                         so add the submitted barcode figure which is in this position
                         */
93
                        odoo_bc += bc[i];
Administrator committed
94
                    } else if (/[ND]/.exec(pattern[i])) {
95 96 97 98
                        /*
                         A figure which encoding a value is in this position
                         (corresponding to a 0 in Odoo article barcode)
                        */
99
                        odoo_bc += '0';
100
                        /* let's add a decimal sepator if D is read for the first time */
Administrator committed
101
                        if (pattern[i] === 'D' && encoded_value.indexOf('.') < 0)
102 103
                            encoded_value += '.';
                        encoded_value += bc[i];
Administrator committed
104 105
                    }
                }
106

Administrator committed
107
                // Add check digit at the end of odoo_bc to find out "normalized" code
108
                bc = odoo_bc + eanCheckDigit(odoo_bc);
Administrator committed
109
            }
110
            index++;
Administrator committed
111 112
        }

113 114
        // let's seek "normalized" bc in codes array or alias map
        for (alias in this.aliases) {
115 116 117 118 119 120 121 122
            /*
                bc.indexOf(alias) === 0
                could be enough, 
                but is used to keep in mind .* caracters
                can be used in rules (have been cleaned before beeing here)
             */
            if (bc == alias || bc.indexOf(alias) === 0) {

123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
                is_alias = true;
                for (barcode in this.codes) {
                    if (barcode == this.aliases[alias]) {
                        product_data = this.codes[barcode];
                    }
                }
            }
        }
        if (is_alias === false) {
            for (code in this.codes) {
                if (code == bc) {
                    product_data = this.codes[code];
                }
            }
        }

        if (product_data !== null) {
            p_uom = (this.uoms)[product_data[this.keys.uom_id]];
141
            let qty = 1;
142

143
            if (encoded_value.length > 0 && !isNaN(encoded_value)) {
144 145 146 147 148 149
                qty = 0; //if no rule is found it will advise user that there is a problem
                /*
                  Warning :
                  Tests are dependant on La Cagette / Cooperatic uom system and barcode rules
                  TODO : Defines them outside of this part of code
                */
150 151
                if (p_uom == 'Unit(s)' || p_uom == 'unité') {
                    encoded_value = parseInt(encoded_value, 10);
152
                    qty = encoded_value;
153 154
                } else {
                    encoded_value = parseFloat(encoded_value);
155 156 157 158 159 160
                    if (pattern_type == 'weight' || pattern_type == 'FF_price_to_weight' || pattern_type == 'price_to_weight') {
                        if (pattern_type == 'weight') {
                            qty = encoded_value;
                        } else {
                            let list_price = product_data[this.keys.list_price];
                            let currency = null;
161 162

                            if (pattern_type == 'FF_price_to_weight') currency = 'FF';
163 164 165 166 167

                            qty = parseFloat(this.get_quantity_eq_to_encoded_price(encoded_value, list_price, currency));
                        }
                    }

168
                }
Administrator committed
169
            }
170

171
            odoo_product = {barcode: bc, data: product_data, rule: pattern_type, value: encoded_value, qty: qty};
Administrator committed
172
        }
173

174
        return odoo_product;
Administrator committed
175
    }
176
};
Administrator committed
177 178

init_barcodes = async function() {
179 180 181 182 183 184 185 186 187
    var result = null;
    var ifcb = Object.create(IFCBarcodes);

    await ifcb.init();
    if (ifcb.errors.length > 0)
        ifcb.display_last_error();
    else
        result = ifcb;
    // console.log(result.patterns)
188

189 190
    return result;
};