Commit 6d4a164d by Damien Moulard

get distant changes in reception index

parent 2d9012a9
...@@ -7,7 +7,7 @@ var orders = [], ...@@ -7,7 +7,7 @@ var orders = [],
callback_report = false, callback_report = false,
selection_type = null, selection_type = null,
order_groups = { order_groups = {
_id: null, _id: 'grouped_orders',
groups: [] groups: []
}, },
dbc = null, dbc = null,
...@@ -34,7 +34,6 @@ function goto(id) { ...@@ -34,7 +34,6 @@ function goto(id) {
* @param {int} group_index index of group in groups array * @param {int} group_index index of group in groups array
*/ */
function group_goto(group_index) { function group_goto(group_index) {
console.log(order_groups.groups);
// Make sure a couchdb document exists for all group's orders // Make sure a couchdb document exists for all group's orders
for (let i in order_groups.groups[group_index]) { for (let i in order_groups.groups[group_index]) {
let order_data = null; let order_data = null;
...@@ -215,6 +214,7 @@ function group_action() { ...@@ -215,6 +214,7 @@ function group_action() {
*/ */
function display_grouped_orders() { function display_grouped_orders() {
if (table_orders !== null) { if (table_orders !== null) {
$('#groups_items').empty();
let groups_display_content = "<ul>"; let groups_display_content = "<ul>";
for (let group_index in order_groups.groups) { for (let group_index in order_groups.groups) {
...@@ -270,6 +270,11 @@ function display_grouped_orders() { ...@@ -270,6 +270,11 @@ function display_grouped_orders() {
* Display the main orders table * Display the main orders table
*/ */
function display_orders_table() { function display_orders_table() {
if (table_orders) {
table_orders.clear().destroy();
$('#orders').empty();
}
table_orders = $('#orders').DataTable({ table_orders = $('#orders').DataTable({
data: orders, data: orders,
columns:[ columns:[
...@@ -445,9 +450,45 @@ $(document).ready(function() { ...@@ -445,9 +450,45 @@ $(document).ready(function() {
auto_compaction: false auto_compaction: false
}); });
// TODO sync on change : get data, update data & dom // On distant changes
sync.on('change', function (info) { sync.on('change', function (info) {
console.log(info); // If important data changed somewhere else, update local data
let need_to_reload = false;
if (info.direction === "pull") {
for (let doc of info.change.docs) {
if (doc._id === "grouped_orders") {
// If groups doc changed, update local groups
need_to_reload = true;
order_groups = doc;
} else if ("_deleted" in doc && doc._deleted === true) {
// If order was deleted, delete it locally
try {
const deleted_order_id = parseInt(doc._id.split('_')[1]);
let index = orders.findIndex(order => order.id == deleted_order_id);
if (index !== -1) {
orders.splice(index, 1);
need_to_reload = true;
}
} catch (error) {
console.log(error);
}
} else {
// Find updated order in local orders & update it if reception status changed
let index = orders.findIndex(order => order.id == doc.id);
if (index !== -1 && orders[index].reception_status !== doc.reception_status) {
orders[index] = doc;
need_to_reload = true;
break;
}
}
}
}
if (need_to_reload) {
display_orders_table();
display_grouped_orders();
}
}).on('error', function (err) { }).on('error', function (err) {
console.log(err); console.log(err);
}); });
......
...@@ -121,7 +121,6 @@ function select_product_from_bc(barcode) { ...@@ -121,7 +121,6 @@ function select_product_from_bc(barcode) {
function update_distant_order(order_id) { function update_distant_order(order_id) {
// TODO insert fingerprint & timestamp // TODO insert fingerprint & timestamp
console.log(order_id);
dbc.put(orders[order_id], (err, result) => { dbc.put(orders[order_id], (err, result) => {
if (!err && result !== undefined) { if (!err && result !== undefined) {
orders[order_id]._rev = result.rev; orders[order_id]._rev = result.rev;
...@@ -1074,6 +1073,7 @@ function send() { ...@@ -1074,6 +1073,7 @@ function send() {
// Loading on // Loading on
openModal(); openModal();
/* Prepare data for orders update */
// Only send to server the updated lines // Only send to server the updated lines
var update_data = { var update_data = {
update_type: updateType, update_type: updateType,
...@@ -1152,6 +1152,30 @@ function send() { ...@@ -1152,6 +1152,30 @@ function send() {
update_data.orders[prod_order_id]['po'].push(updatedProducts[i]); update_data.orders[prod_order_id]['po'].push(updatedProducts[i]);
} }
/* Prepare data for error report */
// Send changes between items to process and processed items
var updates = {
'group_amount_total' : 0,
'update_type' : updateType,
'updated_products' : updatedProducts,
'user_comments': user_comments,
'orders' : []
};
for (i in orders) {
updates.group_amount_total += orders[i].amount_total;
updates.orders.push(orders[i]);
}
// TODO: (le vrai) step 1 : don't save report, just previous data
// step 2 : prepare data by concatening both steps updated data. Envoyer ça au serveur. Coté back, just read ça.
// on peut supprimer les données dans couchdb au retour de update_orders sans problème parce qu'on a déjà anvoyé les données à save_error_report (préparées ici)
// -> Code beaucoup plus simple côté back dans save error report! et probablement ok ici
// + àa permettra de personaliser le back dans update_orders :
// - si paramètre "reception_3_steps" activée, on fait rien tant que pas step 3
// - on envoie toutes les updated_data concaténées après step 3
/* Update orders */
$.ajax({ $.ajax({
type: "PUT", type: "PUT",
url: "../update_orders", url: "../update_orders",
...@@ -1163,8 +1187,9 @@ function send() { ...@@ -1163,8 +1187,9 @@ function send() {
closeModal(); closeModal();
try { try {
// If step 1 (counting), open pop-up with procedure explanation // If step 1 (counting)
if (reception_status == "False") { if (reception_status == "False") {
/* Open pop-up with procedure explanation */
var barcodes_to_print = false; var barcodes_to_print = false;
// Select products with local barcode and without barcode, when qty > 0 // Select products with local barcode and without barcode, when qty > 0
...@@ -1226,6 +1251,24 @@ function send() { ...@@ -1226,6 +1251,24 @@ function send() {
true, true,
false false
); );
/* Not last step: update distant data */
for (let order_id in orders) {
// Save current step updated data
orders[order_id].previous_steps_data = {}
orders[order_id].previous_steps_data[reception_status] = {
updated_products: orders[order_id].updated_products
}
orders[order_id].reception_status = updateType;
// Delete temp data
delete orders[order_id].valid_products;
delete orders[order_id].updated_products;
}
dbc.bulkDocs(Object.values(orders)).catch((err) => {
console.log(err);
})
} else { } else {
// Print etiquettes with new prices // Print etiquettes with new prices
if (updatedProducts.length > 0) { if (updatedProducts.length > 0) {
...@@ -1239,43 +1282,45 @@ function send() { ...@@ -1239,43 +1282,45 @@ function send() {
true, true,
false false
); );
/* Last step: Clear distant data */
// TODO test
// Delete orders doc
for (let order_id in orders) {
orders[order_id]._deleted = true;
} }
// Go back to to_process list if modal closed // Remove orders group
dbc.get("grouped_orders").then((doc) => {
let couchdb_update_data = Object.values(orders);
// We're in a group, remove it & update groups doc
if (Object.keys(orders).length > 1) {
let groups_doc = doc;
const first_order_id = Object.keys(orders)[0];
for (let i in groups_doc.groups) {
if (groups_doc.groups[i].includes(first_order_id)) {
groups_doc.groups.splice(i, 1);
break;
}
}
couchdb_update_data.push(groups_doc);
}
return dbc.bulkDocs(couchdb_update_data);
})
.catch(function (err) {
console.log(err)
});
}
// Back if modal closed
$('#modal_closebtn_top').on('click', back); $('#modal_closebtn_top').on('click', back);
$('#modal_closebtn_bottom').on('click', back); $('#modal_closebtn_bottom').on('click', back);
// TODO data clearing. // TODO ? Dans modal : "merci de patienter avant de quitter la page..." -> "vous pouvez quitter la page !"
// according to process state:
// - step1 : clear only products related datar :valid_products, updated_products & save data from step 1
// - step2 : remove orders in couchdb & groups
// -> server side ?
// Clear local storage before leaving
// for (order_id in orders) {
// localStorage.removeItem("order_" + order_id);
// }
// Delete group(s)
// if (Object.keys(orders).length > 1) {
// var grouped_orders = JSON.parse(localStorage.getItem('grouped_orders'));
// // Remove all groups containing these orders
// for (order_id in orders) {
// search:
// for (var h = 0; i < grouped_orders.length; h++) {
// for (var j = 0; j < grouped_orders[h].length; j++) {
// if (grouped_orders[h][j] == order_id) {
// grouped_orders.splice(h);
// break search;
// }
// }
// }
// }
// localStorage.setItem('grouped_orders', JSON.stringify(grouped_orders));
// }
} catch (ee) { } catch (ee) {
err = {msg: ee.name + ' : ' + ee.message, ctx: 'callback update_orders'}; err = {msg: ee.name + ' : ' + ee.message, ctx: 'callback update_orders'};
console.error(err); console.error(err);
...@@ -1289,21 +1334,7 @@ function send() { ...@@ -1289,21 +1334,7 @@ function send() {
}); });
// TODO : for step 1, instead of saving a temp report, save data in couchdb ? // TODO : for step 1, instead of saving a temp report, save data in couchdb ?
/* Create error report */
// Send changes between items to process and processed items
var updates = {
'group_amount_total' : 0,
'update_type' : updateType,
'updated_products' : updatedProducts,
'user_comments': user_comments,
'orders' : []
};
for (i in orders) {
updates.group_amount_total += orders[i].amount_total;
updates.orders.push(orders[i]);
}
$.ajax({ $.ajax({
type: "POST", type: "POST",
url: "../save_error_report", url: "../save_error_report",
......
...@@ -357,7 +357,7 @@ def save_error_report(request): ...@@ -357,7 +357,7 @@ def save_error_report(request):
# Create report with data from steps 1 & 2 # Create report with data from steps 1 & 2
else: elif data['update_type'] == 'br_valid':
for order in data['orders']: for order in data['orders']:
# Read step 1 data from temp file # Read step 1 data from temp file
data_qties = {} data_qties = {}
......
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