tien_nemo

fix save and load data

...@@ -59,7 +59,7 @@ class OcrController extends Controller ...@@ -59,7 +59,7 @@ class OcrController extends Controller
59 { 59 {
60 try { 60 try {
61 // Lấy template name từ request hoặc mặc định 61 // Lấy template name từ request hoặc mặc định
62 - $templateName = $request->get('template_name', 'NEMO9'); 62 + $templateName = $request->get('template_name', '');
63 63
64 // Giả sử file OCR JSON & ảnh nằm trong storage/app/public/image/ 64 // Giả sử file OCR JSON & ảnh nằm trong storage/app/public/image/
65 $jsonPath = public_path("image/data_picking_detail_1754967679.json"); 65 $jsonPath = public_path("image/data_picking_detail_1754967679.json");
...@@ -74,15 +74,6 @@ class OcrController extends Controller ...@@ -74,15 +74,6 @@ class OcrController extends Controller
74 return response()->json(['error' => 'File OCR JSON không hợp lệ'], 400); 74 return response()->json(['error' => 'File OCR JSON không hợp lệ'], 400);
75 } 75 }
76 76
77 - $formData = [
78 - 'export_date' => "",
79 - 'order_code' => "",
80 - 'customer' => "",
81 - 'address' => "",
82 - 'staff' => "",
83 - 'customer_name' => ""
84 - ];
85 -
86 $dataMapping = []; 77 $dataMapping = [];
87 $is_template = false; 78 $is_template = false;
88 79
...@@ -97,16 +88,15 @@ class OcrController extends Controller ...@@ -97,16 +88,15 @@ class OcrController extends Controller
97 $coords = array_map('intval', explode(',', $detail->field_xy)); 88 $coords = array_map('intval', explode(',', $detail->field_xy));
98 // coords = [x1, y1, x2, y2] 89 // coords = [x1, y1, x2, y2]
99 90
100 - // Tìm text OCR nằm trong bbox này (cải thiện để gộp text)
101 - // $text = $this->findTextInBBox($ocrData, $coords);
102 -
103 - // field_name => text
104 -
105 $dataMapping[$detail->field_name] = [ 91 $dataMapping[$detail->field_name] = [
106 'text' => '', 92 'text' => '',
107 'coords' => $coords 93 'coords' => $coords
108 ]; 94 ];
109 } 95 }
96 + $dataMapping['template_name'] = [
97 + 'text' => $mst->tpl_name,
98 + 'coords' => ''
99 + ];
110 100
111 $is_template = true; 101 $is_template = true;
112 102
...@@ -117,7 +107,6 @@ class OcrController extends Controller ...@@ -117,7 +107,6 @@ class OcrController extends Controller
117 'success' => true, 107 'success' => true,
118 'ocrData' => $ocrData, 108 'ocrData' => $ocrData,
119 'pdfImageUrl' => $imgPath, 109 'pdfImageUrl' => $imgPath,
120 - 'formData' => $formData,
121 'dataMapping' => $dataMapping, 110 'dataMapping' => $dataMapping,
122 'is_template' => $is_template, 111 'is_template' => $is_template,
123 'fieldOptions' => [ 112 'fieldOptions' => [
...@@ -139,47 +128,6 @@ class OcrController extends Controller ...@@ -139,47 +128,6 @@ class OcrController extends Controller
139 } 128 }
140 } 129 }
141 130
142 - private function findTextInBBox($ocrData, $coords)
143 - {
144 - [$x1, $y1, $x2, $y2] = $coords;
145 - $foundItems = [];
146 -
147 - foreach ($ocrData as $item) {
148 - [$ix1, $iy1, $ix2, $iy2] = $item['bbox'];
149 -
150 - // Kiểm tra xem box OCR có nằm trong vùng bbox template không
151 - if ($ix1 >= $x1 && $iy1 >= $y1 && $ix2 <= $x2 && $iy2 <= $y2) {
152 - $foundItems[] = [
153 - 'text' => $item['text'],
154 - 'bbox' => $item['bbox'],
155 - 'x' => $ix1,
156 - 'y' => $iy1
157 - ];
158 - }
159 - }
160 -
161 - if (empty($foundItems)) {
162 - return '';
163 - }
164 -
165 - // Sắp xếp các item theo vị trí (từ trái sang phải, từ trên xuống dưới)
166 - usort($foundItems, function($a, $b) {
167 - // Ưu tiên theo Y trước (hàng), sau đó theo X (cột)
168 - if (abs($a['y'] - $b['y']) < 20) { // Cùng hàng (toler$foundItems = {array[2]} ance 20px)
169 - return $a['x'] - $b['x']; // Sắp xếp theo X
170 - }
171 - return $a['y'] - $b['y']; // Sắp xếp theo Y
172 - });
173 -
174 - // Gộp text theo thứ tự đã sắp xếp
175 - $combinedText = [];
176 - foreach ($foundItems as $item) {
177 - $combinedText[] = trim($item['text']);
178 - }
179 -
180 - return implode(' ', $combinedText);
181 - }
182 -
183 /** 131 /**
184 * Lấy danh sách template 132 * Lấy danh sách template
185 */ 133 */
......
...@@ -128,8 +128,9 @@ ...@@ -128,8 +128,9 @@
128 @focus="highlightField(field.value)" 128 @focus="highlightField(field.value)"
129 @click="onInputClick(field.value)" 129 @click="onInputClick(field.value)"
130 @blur="onInputBlur(field.value)" 130 @blur="onInputBlur(field.value)"
131 - :readonly="field.value === 'customer_name' && !hasCustomerNameXY" 131 +
132 > 132 >
133 +{{-- :readonly="field.value === 'customer_name' && !hasCustomerNameXY"--}}
133 </div> 134 </div>
134 <button @click="saveTemplate">💾Save</button> 135 <button @click="saveTemplate">💾Save</button>
135 </div> 136 </div>
...@@ -161,7 +162,6 @@ ...@@ -161,7 +162,6 @@
161 created() { 162 created() {
162 // Chỉ tạo formData cho các field cần mapping 163 // Chỉ tạo formData cho các field cần mapping
163 this.fieldOptions 164 this.fieldOptions
164 - .filter(f => f.value !== "template_name")
165 .forEach(f => { 165 .forEach(f => {
166 this.$set(this.formData, f.value, ""); 166 this.$set(this.formData, f.value, "");
167 }); 167 });
...@@ -415,16 +415,16 @@ ...@@ -415,16 +415,16 @@
415 this.ocrData = data.ocrData; 415 this.ocrData = data.ocrData;
416 this.pdfImageUrl = data.pdfImageUrl; 416 this.pdfImageUrl = data.pdfImageUrl;
417 this.fieldOptions = data.fieldOptions; 417 this.fieldOptions = data.fieldOptions;
418 - this.formData = data.formData; 418 + // this.formData = data.formData;
419 this.dataMapping = data.dataMapping; 419 this.dataMapping = data.dataMapping;
420 this.is_template = data.is_template; 420 this.is_template = data.is_template;
421 // Đợi image load xong trước khi xử lý 421 // Đợi image load xong trước khi xử lý
422 - if (this.$refs.pdfImage && this.$refs.pdfImage.complete) { 422 + // if (this.$refs.pdfImage && this.$refs.pdfImage.complete) {
423 - this.processLoadedData(); 423 + // // this.processLoadedData();
424 - } else { 424 + // } else {
425 - console.log('Image not loaded yet, waiting for onImageLoad'); 425 + // console.log('Image not loaded yet, waiting for onImageLoad');
426 - // Image sẽ được xử lý trong onImageLoad 426 + // // Image sẽ được xử lý trong onImageLoad
427 - } 427 + // }
428 428
429 429
430 } catch (error) { 430 } catch (error) {
...@@ -451,10 +451,10 @@ ...@@ -451,10 +451,10 @@
451 this.formData = {}; 451 this.formData = {};
452 } 452 }
453 453
454 - const tolerance = 20; // ngưỡng cho cùng 1 hàng 454 + const tolerance = 20; // ngưỡng chenh lech toa do y cho cùng 1 hàng
455 455
456 Object.keys(this.dataMapping).forEach(fieldName => { 456 Object.keys(this.dataMapping).forEach(fieldName => {
457 - let { coords } = this.dataMapping[fieldName]; 457 + let { coords, text } = this.dataMapping[fieldName];
458 let foundItems = this.ocrData 458 let foundItems = this.ocrData
459 .filter(item => this.isBoxInside(item.bbox, coords) && item.text.trim()) 459 .filter(item => this.isBoxInside(item.bbox, coords) && item.text.trim())
460 .map(item => ({ 460 .map(item => ({
...@@ -478,17 +478,15 @@ ...@@ -478,17 +478,15 @@
478 }; 478 };
479 // Nếu là template thì gán vào formData 479 // Nếu là template thì gán vào formData
480 if (this.is_template) { 480 if (this.is_template) {
481 - this.formData[fieldName] = finalText || ''; 481 + this.formData[fieldName] = finalText && finalText.trim() !== "" ? finalText : text;
482 } 482 }
483 +
483 this.manualBoxData[fieldName] = { 484 this.manualBoxData[fieldName] = {
484 text: finalText, 485 text: finalText,
485 coords: coords 486 coords: coords
486 }; 487 };
487 }); 488 });
488 489
489 - // console.log('dataMapping:', this.dataMapping);
490 - // console.log('formData:', this.formData);
491 -
492 }, 490 },
493 491
494 onImageLoad() { 492 onImageLoad() {
...@@ -545,9 +543,6 @@ ...@@ -545,9 +543,6 @@
545 } else { 543 } else {
546 // Kiểm tra xem ocrData đã có box nào với field này chưa 544 // Kiểm tra xem ocrData đã có box nào với field này chưa
547 const existingBox = this.ocrData.find(b => b.field === field && !b.isDeleted); 545 const existingBox = this.ocrData.find(b => b.field === field && !b.isDeleted);
548 - // console.log(`Checking existing box for field "${field}":`, existingBox);
549 - // console.log(`Checking existing manualBoxData for field "${field}":`, this.manualBoxData[field]);
550 -
551 if (this.manualBoxData[field]) { 546 if (this.manualBoxData[field]) {
552 // Nếu không có trong ocrData, dùng manualBoxData (tọa độ mới) 547 // Nếu không có trong ocrData, dùng manualBoxData (tọa độ mới)
553 coords = this.manualBoxData[field].coords; 548 coords = this.manualBoxData[field].coords;
...@@ -601,7 +596,6 @@ ...@@ -601,7 +596,6 @@
601 596
602 // Tính vị trí hiển thị của box 597 // Tính vị trí hiển thị của box
603 const [x1, y1, x2, y2] = item.bbox; 598 const [x1, y1, x2, y2] = item.bbox;
604 - //const [x1, y1, x2, y2] = item.field_xy.split(',').map(Number);
605 if (!this.imageWidth || !this.imageHeight || !this.$refs.pdfImage) return; 599 if (!this.imageWidth || !this.imageHeight || !this.$refs.pdfImage) return;
606 600
607 const displayedWidth = this.$refs.pdfImage.clientWidth; 601 const displayedWidth = this.$refs.pdfImage.clientWidth;
...@@ -828,8 +822,6 @@ ...@@ -828,8 +822,6 @@
828 822
829 // Set active index 823 // Set active index
830 this.activeIndex = this.selectingIndex; 824 this.activeIndex = this.selectingIndex;
831 -
832 - // console.log(`Updated field "${item.field}" for box OCR at index ${this.selectingIndex} with new coordinates`);
833 } 825 }
834 826
835 this.selectingIndex = null; 827 this.selectingIndex = null;
...@@ -878,10 +870,6 @@ ...@@ -878,10 +870,6 @@
878 }); 870 });
879 871
880 const finalText = combinedText.join(" "); 872 const finalText = combinedText.join(" ");
881 - // console.log('Combined text:', finalText);
882 -
883 - // Gán field và text cho box manual
884 - // console.log(`Assigning manual field "${this.manualField}" to box at index ${manualIndex} with text: "${finalText}"`);
885 873
886 // Gán field trực tiếp cho box manual 874 // Gán field trực tiếp cho box manual
887 this.ocrData[manualIndex].field = this.manualField; 875 this.ocrData[manualIndex].field = this.manualField;
...@@ -1007,7 +995,6 @@ ...@@ -1007,7 +995,6 @@
1007 }; 995 };
1008 996
1009 this.ocrData.push(manualBox); 997 this.ocrData.push(manualBox);
1010 - // console.log('Manual box created successfully:', manualBox);
1011 998
1012 // Force re-render 999 // Force re-render
1013 this.$forceUpdate(); 1000 this.$forceUpdate();
......