tien_nemo

fix save and load data

...@@ -313,6 +313,7 @@ ...@@ -313,6 +313,7 @@
313 customer_name = this.manualBoxData.customer_name.text; 313 customer_name = this.manualBoxData.customer_name.text;
314 customer_coords = this.manualBoxData.customer_name.coords.join(','); 314 customer_coords = this.manualBoxData.customer_name.coords.join(',');
315 fields = this.manualBoxData; 315 fields = this.manualBoxData;
316 + console.log('Using manualBoxData for customer_name:', customer_name, customer_coords);
316 } else { 317 } else {
317 // Không có → tìm trong ocrData 318 // Không có → tìm trong ocrData
318 const found = this.ocrData.find(item => item.field === 'customer_name'); 319 const found = this.ocrData.find(item => item.field === 'customer_name');
...@@ -333,11 +334,12 @@ ...@@ -333,11 +334,12 @@
333 }); 334 });
334 // // convert to array 335 // // convert to array
335 fields = (fieldsByName); 336 fields = (fieldsByName);
337 + console.log('Using ocrData for customer_name:', customer_name, customer_coords);
336 } 338 }
337 339
338 - console.log('fields:', fields); 340 + // console.log('fields:', fields);
339 341
340 - if (!customer_coords) { 342 + if (!customer_coords || !customer_name) {
341 alert("Bạn phải map customer_name (quét/select) trước khi lưu."); 343 alert("Bạn phải map customer_name (quét/select) trước khi lưu.");
342 return; 344 return;
343 } 345 }
...@@ -434,7 +436,6 @@ ...@@ -434,7 +436,6 @@
434 processLoadedData() { 436 processLoadedData() {
435 // Tự động map field cho các box OCR dựa trên formData đã load 437 // Tự động map field cho các box OCR dựa trên formData đã load
436 this.autoMapFieldsFromFormData(); 438 this.autoMapFieldsFromFormData();
437 - // Kiểm tra và sửa lại tọa độ của các box manual
438 439
439 // Force re-render để đảm bảo các box được vẽ 440 // Force re-render để đảm bảo các box được vẽ
440 this.$nextTick(() => { 441 this.$nextTick(() => {
...@@ -449,28 +450,44 @@ ...@@ -449,28 +450,44 @@
449 if(this.is_template) { 450 if(this.is_template) {
450 this.formData = {}; 451 this.formData = {};
451 } 452 }
453 +
454 + const tolerance = 20; // ngưỡng cho cùng 1 hàng
455 +
452 Object.keys(this.dataMapping).forEach(fieldName => { 456 Object.keys(this.dataMapping).forEach(fieldName => {
453 - let { text, coords } = this.dataMapping[fieldName]; 457 + let { coords } = this.dataMapping[fieldName];
454 - console.log(`Auto-mapping field "${fieldName}" with coords:`, coords, 'and text:', text); 458 + let foundItems = this.ocrData
455 - this.ocrData.forEach(item => { 459 + .filter(item => this.isBoxInside(item.bbox, coords) && item.text.trim())
456 - if (this.isBoxInside(item.bbox, coords) && item.text.trim()) { 460 + .map(item => ({
457 - const partial = this.getPartialText(item.text, item.bbox, coords); 461 + text: this.getPartialText(item.text, item.bbox, coords),
458 - text = partial; // cập nhật biến text để giá trị mới được truyền qua manualBoxData 462 + bbox: item.bbox
463 + }))
464 + .sort((a, b) =>
465 + Math.abs(a.bbox[1] - b.bbox[1]) < tolerance
466 + ? a.bbox[0] - b.bbox[0] // cùng hàng → trái sang phải
467 + : a.bbox[1] - b.bbox[1] // khác hàng → trên xuống dưới
468 + );
469 +
470 +
471 + const combinedText = foundItems.map(f => f.text.trim());
472 + const finalText = combinedText.join(" ");
473 +
474 + // Lưu vào dataMapping
459 this.dataMapping[fieldName] = { 475 this.dataMapping[fieldName] = {
460 - text: partial, 476 + text: finalText,
461 coords: coords 477 coords: coords
462 }; 478 };
463 - } 479 + // Nếu là template thì gán vào formData
464 - });
465 -
466 if (this.is_template) { 480 if (this.is_template) {
467 - this.formData[fieldName] = text || ''; 481 + this.formData[fieldName] = finalText || '';
468 } 482 }
469 - this.manualBoxData[fieldName] = { text, coords }; 483 + this.manualBoxData[fieldName] = {
484 + text: finalText,
485 + coords: coords
486 + };
470 }); 487 });
471 488
472 - console.log('dataMapping:', this.dataMapping); 489 + // console.log('dataMapping:', this.dataMapping);
473 - console.log('formData:', this.formData); 490 + // console.log('formData:', this.formData);
474 491
475 }, 492 },
476 493
...@@ -812,7 +829,7 @@ ...@@ -812,7 +829,7 @@
812 // Set active index 829 // Set active index
813 this.activeIndex = this.selectingIndex; 830 this.activeIndex = this.selectingIndex;
814 831
815 - console.log(`Updated field "${item.field}" for box OCR at index ${this.selectingIndex} with new coordinates`); 832 + // console.log(`Updated field "${item.field}" for box OCR at index ${this.selectingIndex} with new coordinates`);
816 } 833 }
817 834
818 this.selectingIndex = null; 835 this.selectingIndex = null;
...@@ -823,8 +840,7 @@ ...@@ -823,8 +840,7 @@
823 840
824 const newBbox = this.ocrData[manualIndex].bbox; 841 const newBbox = this.ocrData[manualIndex].bbox;
825 842
826 - 843 + console.log(`manual for field "${this.manualField}" at index ${manualIndex} with bbox:`, newBbox);
827 - console.log(` manual for field "${this.manualField}" at index ${manualIndex} with bbox:`, newBbox);
828 this.ocrData.forEach((box, i) => { 844 this.ocrData.forEach((box, i) => {
829 if (i !== manualIndex && box.field === this.ocrData[manualIndex].field) { 845 if (i !== manualIndex && box.field === this.ocrData[manualIndex].field) {
830 box.field = ''; 846 box.field = '';
...@@ -838,7 +854,7 @@ ...@@ -838,7 +854,7 @@
838 this.ocrData.forEach(item => { 854 this.ocrData.forEach(item => {
839 if (!item.isManual && this.isBoxInside(item.bbox, newBbox) && item.text.trim()) { 855 if (!item.isManual && this.isBoxInside(item.bbox, newBbox) && item.text.trim()) {
840 const partial = this.getPartialText(item.text, item.bbox, newBbox); 856 const partial = this.getPartialText(item.text, item.bbox, newBbox);
841 - // console.log(`Found OCR item for manual mapping: ${partial} at bbox ${item.bbox}`); 857 +
842 foundItems.push({ 858 foundItems.push({
843 text: partial, 859 text: partial,
844 bbox: item.bbox, 860 bbox: item.bbox,
...@@ -862,10 +878,10 @@ ...@@ -862,10 +878,10 @@
862 }); 878 });
863 879
864 const finalText = combinedText.join(" "); 880 const finalText = combinedText.join(" ");
865 - console.log('Combined text:', finalText); 881 + // console.log('Combined text:', finalText);
866 882
867 // Gán field và text cho box manual 883 // Gán field và text cho box manual
868 - console.log(`Assigning manual field "${this.manualField}" to box at index ${manualIndex} with text: "${finalText}"`); 884 + // console.log(`Assigning manual field "${this.manualField}" to box at index ${manualIndex} with text: "${finalText}"`);
869 885
870 // Gán field trực tiếp cho box manual 886 // Gán field trực tiếp cho box manual
871 this.ocrData[manualIndex].field = this.manualField; 887 this.ocrData[manualIndex].field = this.manualField;
...@@ -922,7 +938,6 @@ ...@@ -922,7 +938,6 @@
922 getPartialText(text, bbox, selectBbox) { 938 getPartialText(text, bbox, selectBbox) {
923 const [x1, y1, x2, y2] = bbox; 939 const [x1, y1, x2, y2] = bbox;
924 const [sx1, sy1, sx2, sy2] = selectBbox; 940 const [sx1, sy1, sx2, sy2] = selectBbox;
925 -
926 // Chiều rộng box OCR 941 // Chiều rộng box OCR
927 const boxWidth = x2 - x1; 942 const boxWidth = x2 - x1;
928 const boxHeight = y2 - y1; 943 const boxHeight = y2 - y1;
......