added checking order details grid
This commit is contained in:
parent
bcb659c570
commit
57421ff905
@ -354,95 +354,6 @@ export function DynamicForm({ client, activityId: initialActivityId, instanceId:
|
|||||||
finalValues[actionField.id] = clickedActionRef.current;
|
finalValues[actionField.id] = clickedActionRef.current;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 1. Pre-submit consolidation for grid fields & total fields
|
|
||||||
normalFields.forEach((f) => {
|
|
||||||
if (f.data_type.startsWith('grid')) {
|
|
||||||
const rawGrid = (finalValues[f.id] as Record<string, unknown>[]) || [];
|
|
||||||
if (rawGrid.length > 0) {
|
|
||||||
const merged: Record<string, unknown>[] = [];
|
|
||||||
const indexMap = new Map<string, number>();
|
|
||||||
|
|
||||||
const getGridVal = (obj: Record<string, unknown>, searchTokens: string[], excludeTokens: string[] = []) => {
|
|
||||||
for (const k of Object.keys(obj)) {
|
|
||||||
const lowerK = k.toLowerCase();
|
|
||||||
if (searchTokens.some(t => lowerK.includes(t)) && !excludeTokens.some(t => lowerK.includes(t))) {
|
|
||||||
return obj[k];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return undefined;
|
|
||||||
};
|
|
||||||
|
|
||||||
for (const r of rawGrid) {
|
|
||||||
const cat = String(getGridVal(r, ['category', 'cat']) || '').trim().toLowerCase();
|
|
||||||
const prod = String(getGridVal(r, ['product', 'name'], ['category', 'cat']) || '').trim().toLowerCase();
|
|
||||||
const currentBags = Number(getGridVal(r, ['bags', 'quantity'])) || 0;
|
|
||||||
const currentSku = Number(getGridVal(r, ['sku'], ['code'])) || 0;
|
|
||||||
|
|
||||||
const key = prod ? (cat ? `${cat}::${prod}` : prod) : cat;
|
|
||||||
if (key) {
|
|
||||||
if (indexMap.has(key)) {
|
|
||||||
if (currentBags > 0) {
|
|
||||||
const targetIdx = indexMap.get(key)!;
|
|
||||||
const existingRow = merged[targetIdx];
|
|
||||||
const existingBags = Number(getGridVal(existingRow, ['bags', 'quantity'])) || 0;
|
|
||||||
const addedBags = currentBags;
|
|
||||||
const newBags = existingBags + addedBags;
|
|
||||||
const skuVal = Number(getGridVal(existingRow, ['sku'], ['code']) || currentSku || 0);
|
|
||||||
|
|
||||||
merged[targetIdx] = {
|
|
||||||
...existingRow,
|
|
||||||
bags: newBags,
|
|
||||||
quantity: newBags,
|
|
||||||
row_kgs: skuVal * newBags,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
continue;
|
|
||||||
} else {
|
|
||||||
indexMap.set(key, merged.length);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
merged.push(r);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Clean virtual keys
|
|
||||||
const cleanGrid = merged.map((r) => {
|
|
||||||
const clean: Record<string, unknown> = {};
|
|
||||||
Object.keys(r).forEach((k) => {
|
|
||||||
if (k !== '_row_total_kgs' && k !== 'row_kgs' && k !== 'row_total') {
|
|
||||||
clean[k] = r[k];
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return clean;
|
|
||||||
});
|
|
||||||
|
|
||||||
finalValues[f.id] = cleanGrid;
|
|
||||||
|
|
||||||
// Recalculate total_bags and total_kgs
|
|
||||||
let totalB = 0;
|
|
||||||
let totalK = 0;
|
|
||||||
cleanGrid.forEach((r) => {
|
|
||||||
const b = Number(getGridVal(r, ['bags', 'quantity'])) || 0;
|
|
||||||
const sku = Number(getGridVal(r, ['sku'], ['code'])) || 0;
|
|
||||||
totalB += b;
|
|
||||||
totalK += b * sku;
|
|
||||||
});
|
|
||||||
|
|
||||||
const getBaseId = (id: string) => id.replace(/_\d+$/, '');
|
|
||||||
normalFields.forEach((tf) => {
|
|
||||||
const baseId = getBaseId(tf.id);
|
|
||||||
const mapped = (tf.mapped_workflow_field || '').toLowerCase();
|
|
||||||
if (baseId === 'total_bags' || mapped === 'total_bags' || tf.id === 'total_bags') {
|
|
||||||
finalValues[tf.id] = Math.round(totalB);
|
|
||||||
}
|
|
||||||
if (baseId === 'total_kgs' || mapped === 'total_kgs' || tf.id === 'total_kgs') {
|
|
||||||
finalValues[tf.id] = Math.round(totalK);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// 2. Validate grid fields (Order Details)
|
|
||||||
const extractGridRowDetails = (r: Record<string, unknown>, columns?: FormScreenField[]) => {
|
const extractGridRowDetails = (r: Record<string, unknown>, columns?: FormScreenField[]) => {
|
||||||
let cat = String(r.category || r.product_category || r.cat || '').trim();
|
let cat = String(r.category || r.product_category || r.cat || '').trim();
|
||||||
let prod = String(r.productName || r.product_name || r.product || r.name || '').trim();
|
let prod = String(r.productName || r.product_name || r.product || r.name || '').trim();
|
||||||
@ -488,6 +399,109 @@ export function DynamicForm({ client, activityId: initialActivityId, instanceId:
|
|||||||
return { cat, prod, bags: isNaN(bagsNum) ? 0 : bagsNum };
|
return { cat, prod, bags: isNaN(bagsNum) ? 0 : bagsNum };
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// 1. Pre-submit consolidation for grid fields & total fields
|
||||||
|
normalFields.forEach((f) => {
|
||||||
|
if (f.data_type.startsWith('grid')) {
|
||||||
|
const rawGrid = (finalValues[f.id] as Record<string, unknown>[]) || [];
|
||||||
|
if (rawGrid.length > 0) {
|
||||||
|
const merged: Record<string, unknown>[] = [];
|
||||||
|
const indexMap = new Map<string, number>();
|
||||||
|
|
||||||
|
const getGridVal = (obj: Record<string, unknown>, searchTokens: string[], excludeTokens: string[] = []) => {
|
||||||
|
for (const k of Object.keys(obj)) {
|
||||||
|
const lowerK = k.toLowerCase();
|
||||||
|
if (searchTokens.some(t => lowerK.includes(t)) && !excludeTokens.some(t => lowerK.includes(t))) {
|
||||||
|
return obj[k];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return undefined;
|
||||||
|
};
|
||||||
|
|
||||||
|
for (const r of rawGrid) {
|
||||||
|
const cat = String(getGridVal(r, ['category', 'cat']) || '').trim().toLowerCase();
|
||||||
|
const prod = String(getGridVal(r, ['product', 'name'], ['category', 'cat']) || '').trim().toLowerCase();
|
||||||
|
const currentBags = Number(getGridVal(r, ['bags', 'quantity'])) || 0;
|
||||||
|
const currentSku = Number(getGridVal(r, ['sku'], ['code'])) || 0;
|
||||||
|
|
||||||
|
const key = prod ? (cat ? `${cat}::${prod}` : prod) : cat;
|
||||||
|
if (key) {
|
||||||
|
if (indexMap.has(key)) {
|
||||||
|
if (currentBags > 0) {
|
||||||
|
const targetIdx = indexMap.get(key)!;
|
||||||
|
const existingRow = merged[targetIdx];
|
||||||
|
const existingBags = Number(getGridVal(existingRow, ['bags', 'quantity'])) || 0;
|
||||||
|
const addedBags = currentBags;
|
||||||
|
const newBags = existingBags + addedBags;
|
||||||
|
const skuVal = Number(getGridVal(existingRow, ['sku'], ['code']) || currentSku || 0);
|
||||||
|
|
||||||
|
const updatedRow = { ...existingRow };
|
||||||
|
Object.keys(existingRow).forEach((k) => {
|
||||||
|
const lowerK = k.toLowerCase();
|
||||||
|
if ((lowerK.includes('bags') || lowerK.includes('quantity') || lowerK.includes('potential')) && !lowerK.includes('total') && !lowerK.includes('kgs')) {
|
||||||
|
updatedRow[k] = newBags;
|
||||||
|
}
|
||||||
|
if (lowerK.includes('row_kgs') || lowerK.includes('rowkgs')) {
|
||||||
|
updatedRow[k] = skuVal * newBags;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
updatedRow.bags = newBags;
|
||||||
|
updatedRow.quantity = newBags;
|
||||||
|
updatedRow.row_kgs = skuVal * newBags;
|
||||||
|
|
||||||
|
merged[targetIdx] = updatedRow;
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
} else {
|
||||||
|
indexMap.set(key, merged.length);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
merged.push(r);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clean virtual keys and filter out empty rows
|
||||||
|
const cleanGrid = merged
|
||||||
|
.filter((r) => {
|
||||||
|
const { cat, prod, bags } = extractGridRowDetails(r, f.columns);
|
||||||
|
return !!(cat || prod || bags > 0);
|
||||||
|
})
|
||||||
|
.map((r) => {
|
||||||
|
const clean: Record<string, unknown> = {};
|
||||||
|
Object.keys(r).forEach((k) => {
|
||||||
|
if (k !== '_row_total_kgs' && k !== 'row_kgs' && k !== 'row_total') {
|
||||||
|
clean[k] = r[k];
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return clean;
|
||||||
|
});
|
||||||
|
|
||||||
|
finalValues[f.id] = cleanGrid;
|
||||||
|
|
||||||
|
// Recalculate total_bags and total_kgs
|
||||||
|
let totalB = 0;
|
||||||
|
let totalK = 0;
|
||||||
|
cleanGrid.forEach((r) => {
|
||||||
|
const b = Number(getGridVal(r, ['bags', 'quantity'])) || 0;
|
||||||
|
const sku = Number(getGridVal(r, ['sku'], ['code'])) || 0;
|
||||||
|
totalB += b;
|
||||||
|
totalK += b * sku;
|
||||||
|
});
|
||||||
|
|
||||||
|
const getBaseId = (id: string) => id.replace(/_\d+$/, '');
|
||||||
|
normalFields.forEach((tf) => {
|
||||||
|
const baseId = getBaseId(tf.id);
|
||||||
|
const mapped = (tf.mapped_workflow_field || '').toLowerCase();
|
||||||
|
if (baseId === 'total_bags' || mapped === 'total_bags' || tf.id === 'total_bags') {
|
||||||
|
finalValues[tf.id] = Math.round(totalB);
|
||||||
|
}
|
||||||
|
if (baseId === 'total_kgs' || mapped === 'total_kgs' || tf.id === 'total_kgs') {
|
||||||
|
finalValues[tf.id] = Math.round(totalK);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// 2. Validate grid fields (Order Details)
|
||||||
for (const f of normalFields) {
|
for (const f of normalFields) {
|
||||||
if (f.data_type.startsWith('grid')) {
|
if (f.data_type.startsWith('grid')) {
|
||||||
if (currentActivityId === 'af8ac8df-d868-4f7d-88b2-123955d69c56') {
|
if (currentActivityId === 'af8ac8df-d868-4f7d-88b2-123955d69c56') {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user