Commit 4922c3e4 by François

Barcode JS improvements : qty corresponding to uom computation done inside,…

Barcode JS improvements : qty corresponding to uom computation done inside, considering price_to_weight (euros), stock/movements cleaning
parent 1ddbc7fd
...@@ -32,6 +32,21 @@ IFCBarcodes = { ...@@ -32,6 +32,21 @@ IFCBarcodes = {
display_last_error: function() { display_last_error: function() {
alert(this.errors[this.errors.length - 1]); alert(this.errors[this.errors.length - 1]);
}, },
get_quantity_eq_to_encoded_price: function (value, list_price, currency) {
let qty = 0;
try {
let price = parseFloat(value);
if (currency == 'FF')
price = price / 6.55957;
qty = parseFloat(price / list_price).toFixed(3);
} catch (error) {
console.log(error);
}
return qty;
},
get_corresponding_odoo_product: function(bc) { get_corresponding_odoo_product: function(bc) {
//console.log('To analyze :' + bc) //console.log('To analyze :' + bc)
var index = 0, var index = 0,
...@@ -49,31 +64,45 @@ IFCBarcodes = { ...@@ -49,31 +64,45 @@ IFCBarcodes = {
if (bc.indexOf(significant_prefix) === 0) { if (bc.indexOf(significant_prefix) === 0) {
/* /*
Submitted barcode-code matches a pattern rule
For example, For example,
bc = 0493213018809 bc = 0493213018809
pattern = 0493...{NNDDD} pattern = 0493...{NNDDD}
*/ */
//console.log(pattern)
// console.log(bc)
odoo_bc = ''; odoo_bc = '';
pattern_found = true; pattern_found = true;
pattern_type = this.patterns[index].type; pattern_type = this.patterns[index].type;
pattern = pattern.replace(/[^0-9.ND]/, ''); pattern = pattern.replace(/[^0-9.ND]/, '');
bc = bc.slice(0, -1); // remove original check figure bc = bc.slice(0, -1); // remove original check figure
// Read pattern character by character /*
Read pattern character by character
to find out Odoo article barcode
and encoded_value (weight, price, units, if exists)
*/
for (var i = 0; i < pattern.length; i++) { for (var i = 0; i < pattern.length; i++) {
if (/[0-9]/.exec(pattern[i])) { if (/[0-9]/.exec(pattern[i])) {
// it's a figure, nothing to do but to add it to string
odoo_bc += pattern[i]; odoo_bc += pattern[i];
} else if (pattern[i].indexOf('.') === 0) { } else if (pattern[i].indexOf('.') === 0) {
/*
it's a substitution character,
so add the submitted barcode figure which is in this position
*/
odoo_bc += bc[i]; odoo_bc += bc[i];
} else if (/[ND]/.exec(pattern[i])) { } else if (/[ND]/.exec(pattern[i])) {
/*
A figure which encoding a value is in this position
(corresponding to a 0 in Odoo article barcode)
*/
odoo_bc += '0'; odoo_bc += '0';
/* let's add a decimal sepator if D is read for the first time */
if (pattern[i] === 'D' && encoded_value.indexOf('.') < 0) if (pattern[i] === 'D' && encoded_value.indexOf('.') < 0)
encoded_value += '.'; encoded_value += '.';
encoded_value += bc[i]; encoded_value += bc[i];
} }
} }
// Add check digit at the end of odoo_bc to find out "normalized" code // Add check digit at the end of odoo_bc to find out "normalized" code
bc = odoo_bc + eanCheckDigit(odoo_bc); bc = odoo_bc + eanCheckDigit(odoo_bc);
} }
...@@ -101,16 +130,35 @@ IFCBarcodes = { ...@@ -101,16 +130,35 @@ IFCBarcodes = {
if (product_data !== null) { if (product_data !== null) {
p_uom = (this.uoms)[product_data[this.keys.uom_id]]; p_uom = (this.uoms)[product_data[this.keys.uom_id]];
let qty = 1;
if (encoded_value.length > 0 && !isNaN(encoded_value)) { if (encoded_value.length > 0 && !isNaN(encoded_value)) {
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
*/
if (p_uom == 'Unit(s)' || p_uom == 'unité') { if (p_uom == 'Unit(s)' || p_uom == 'unité') {
encoded_value = parseInt(encoded_value, 10); encoded_value = parseInt(encoded_value, 10);
qty = encoded_value;
} else { } else {
encoded_value = parseFloat(encoded_value); encoded_value = parseFloat(encoded_value);
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;
if (pattern_type == 'FF_price_to_weight') currency = 'FF'
qty = parseFloat(this.get_quantity_eq_to_encoded_price(encoded_value, list_price, currency));
} }
} }
odoo_product = {barcode: bc, data: product_data, rule: pattern_type, value: encoded_value}; }
}
odoo_product = {barcode: bc, data: product_data, rule: pattern_type, value: encoded_value, qty: qty};
} }
return odoo_product; return odoo_product;
......
...@@ -256,10 +256,10 @@ function init_confirmation_datatable() { ...@@ -256,10 +256,10 @@ function init_confirmation_datatable() {
}); });
} }
function without_consent_update_product(p) { function without_consent_update_product(p, added_qty) {
let undo_option = true; let undo_option = true;
update_existing_product(p, undo_option); update_existing_product(p, added_qty, undo_option);
} }
function get_stored_product_with_bc(barcode) { function get_stored_product_with_bc(barcode) {
...@@ -307,19 +307,17 @@ function fetch_product_from_bc(barcode) { ...@@ -307,19 +307,17 @@ function fetch_product_from_bc(barcode) {
'uom': barcodes['uoms'][p.data[barcodes['keys']['uom_id']]], 'uom': barcodes['uoms'][p.data[barcodes['keys']['uom_id']]],
'standard_price' : p.data[barcodes['keys']['standard_price']], // cost 'standard_price' : p.data[barcodes['keys']['standard_price']], // cost
'list_price': p.data[barcodes['keys']['list_price']] // public price 'list_price': p.data[barcodes['keys']['list_price']], // public price
'qty': p.qty
}; };
product['uom']['id'] = p.data[barcodes['keys']['uom_id']]; product['uom']['id'] = p.data[barcodes['keys']['uom_id']];
product['value'] = parseFloat(p.value) || 1;
product['rule'] = p.rule; product['rule'] = p.rule;
p_existing = get_stored_product_with_bc(p.barcode); p_existing = get_stored_product_with_bc(p.barcode);
if (p_existing !== null) { if (p_existing !== null) {
product.qty = p_existing.qty; without_consent_update_product(p_existing, product.qty);
without_consent_update_product(product);
return 0; return 0;
} else { } else {
add_product(product); add_product(product);
...@@ -335,16 +333,6 @@ function fetch_product_from_bc(barcode) { ...@@ -335,16 +333,6 @@ function fetch_product_from_bc(barcode) {
var add_product = function(product) { var add_product = function(product) {
try { try {
// Add to list // Add to list
product.qty = 1;
if (typeof product.value == "number" || (product.value.length > 0 && !isNaN(product.value))) {
//encoded value will be translated in quantity
if (product.rule == "FF_price_to_weight") {
product.qty = get_quantity_eq_to_franc_price(product);
} else {
product.qty = parseFloat(product.value);
}
}
products.push(product); products.push(product);
...@@ -385,26 +373,11 @@ var update_in_products = function(product) { ...@@ -385,26 +373,11 @@ var update_in_products = function(product) {
else console.log("Le produit n'a pas pu être trouvé dans la variable products !"); else console.log("Le produit n'a pas pu être trouvé dans la variable products !");
}; };
var get_quantity_eq_to_franc_price = function(product) {
let value = 0;
try {
let price = parseFloat(product.value / 6.55957);
value = parseFloat(price / product.list_price).toFixed(3);
} catch (error) {
console.log(error);
}
return value;
};
/* /*
* Update a line in the table: update quantity * Update a line in the table: update quantity
*/ */
var update_existing_product = function(product, undo_option = false) { var update_existing_product = function(product, added_qty, undo_option = false) {
// By default added qty is 1 unit
let added_qty = 1;
let op = "augmentée"; let op = "augmentée";
let notify_options = { let notify_options = {
...@@ -413,35 +386,8 @@ var update_existing_product = function(product, undo_option = false) { ...@@ -413,35 +386,8 @@ var update_existing_product = function(product, undo_option = false) {
clickToHide: false clickToHide: false
}; };
// type product qty value
if (product.rule == 'weight' || product.rule == 'FF_price_to_weight' && product.value) {
// Quantities are kg or price
product.qty = parseFloat(product.qty) || 0;
if (product.rule == 'weight') {
added_qty = parseFloat(product.value);
} else {
if (product.value < 0) added_qty = parseFloat(product.value); // value is already a qty
else added_qty = parseFloat(get_quantity_eq_to_franc_price(product));
}
} else {
//Quantity is by "defaut" considered as to be in "unit"
product.qty = parseInt(product.qty, 10);
if (product.rule == "" && product.value) {
added_qty = product.value;
}
}
product.qty += added_qty; product.qty += added_qty;
/* Surprisingly, this assignment by addition (0 + value)
always correctly "typing" the value
whereas "product.qty = added_qty"
is sometimes typed as "string" !!
*/
// always set to empty to avoid next operation on same product to be misprocessed
product.value = "";
// Find index of row which match product id in the first column // Find index of row which match product id in the first column
var indexes = products_table.rows().eq(0) var indexes = products_table.rows().eq(0)
...@@ -470,10 +416,7 @@ var update_existing_product = function(product, undo_option = false) { ...@@ -470,10 +416,7 @@ var update_existing_product = function(product, undo_option = false) {
notify_options.autoHide = false; notify_options.autoHide = false;
// notify_options.autoHideDelay = 10000; // notify_options.autoHideDelay = 10000;
notify_options.style = 'cancelable'; notify_options.style = 'cancelable';
// msg = $('<span>').text(msg)
// .attr('data-barcode', product.barcode)
// .attr('data-addedqty', added_qty);
// msg = msg.html()
msg = '<span class="msg" data-barcode="' + product.barcode + '" data-added_qty="' + added_qty + '">' msg = '<span class="msg" data-barcode="' + product.barcode + '" data-added_qty="' + added_qty + '">'
+ "<b>" + product.name + "</b><br/>" + msg + "<b>" + product.name + "</b><br/>" + msg
+ '</span>'; + '</span>';
...@@ -919,8 +862,7 @@ $(document).ready(function() { ...@@ -919,8 +862,7 @@ $(document).ready(function() {
let product = get_stored_product_with_bc(bc); let product = get_stored_product_with_bc(bc);
if (product !== null) { if (product !== null) {
product.value = - added_qty; update_existing_product(product, - added_qty);
update_existing_product(product);
} else { } else {
alert("Le produit n'a pas été retrouvé dans la mémoire de travail."); alert("Le produit n'a pas été retrouvé dans la mémoire de travail.");
} }
......
  • Here the material for the public and the coding discussion would be a wise idea. I’m sure after the help on your blog people would be able to exchange their thoughts and my perfect resume reviews coding styling. I like your wise recommendation please continue to share more.

Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment