Showing
3 changed files
with
46 additions
and
24 deletions
| ... | @@ -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', ''); | 62 | + $templateName = $request->get('template_name', 'NEMO9'); |
| 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,8 +74,17 @@ class OcrController extends Controller | ... | @@ -74,8 +74,17 @@ 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 = []; | 77 | + $formData = [ |
| 78 | + 'export_date' => "", | ||
| 79 | + 'order_code' => "", | ||
| 80 | + 'customer' => "", | ||
| 81 | + 'address' => "", | ||
| 82 | + 'staff' => "", | ||
| 83 | + 'customer_name' => "" | ||
| 84 | + ]; | ||
| 85 | + | ||
| 78 | $dataMapping = []; | 86 | $dataMapping = []; |
| 87 | + $is_template = false; | ||
| 79 | 88 | ||
| 80 | if ($templateName) { | 89 | if ($templateName) { |
| 81 | $mst = MstTemplate::where('tpl_name', $templateName)->first(); | 90 | $mst = MstTemplate::where('tpl_name', $templateName)->first(); |
| ... | @@ -89,28 +98,20 @@ class OcrController extends Controller | ... | @@ -89,28 +98,20 @@ class OcrController extends Controller |
| 89 | // coords = [x1, y1, x2, y2] | 98 | // coords = [x1, y1, x2, y2] |
| 90 | 99 | ||
| 91 | // Tìm text OCR nằm trong bbox này (cải thiện để gộp text) | 100 | // Tìm text OCR nằm trong bbox này (cải thiện để gộp text) |
| 92 | - $text = $this->findTextInBBox($ocrData, $coords); | 101 | + // $text = $this->findTextInBBox($ocrData, $coords); |
| 93 | 102 | ||
| 94 | // field_name => text | 103 | // field_name => text |
| 95 | - $formData[$detail->field_name] = $text; | ||
| 96 | 104 | ||
| 97 | $dataMapping[$detail->field_name] = [ | 105 | $dataMapping[$detail->field_name] = [ |
| 98 | - 'text' => $text, | 106 | + 'text' => '', |
| 99 | 'coords' => $coords | 107 | 'coords' => $coords |
| 100 | ]; | 108 | ]; |
| 101 | } | 109 | } |
| 102 | - } else { | 110 | + |
| 103 | - $formData = [ | 111 | + $is_template = true; |
| 104 | - 'export_date' => "", | 112 | + |
| 105 | - 'order_code' => "", | ||
| 106 | - 'customer' => "", | ||
| 107 | - 'address' => "", | ||
| 108 | - 'staff' => "", | ||
| 109 | - 'customer_name' => "" | ||
| 110 | - ]; | ||
| 111 | } | 113 | } |
| 112 | } | 114 | } |
| 113 | -// dd($formData, $dataMapping); | ||
| 114 | 115 | ||
| 115 | return response()->json([ | 116 | return response()->json([ |
| 116 | 'success' => true, | 117 | 'success' => true, |
| ... | @@ -118,6 +119,7 @@ class OcrController extends Controller | ... | @@ -118,6 +119,7 @@ class OcrController extends Controller |
| 118 | 'pdfImageUrl' => $imgPath, | 119 | 'pdfImageUrl' => $imgPath, |
| 119 | 'formData' => $formData, | 120 | 'formData' => $formData, |
| 120 | 'dataMapping' => $dataMapping, | 121 | 'dataMapping' => $dataMapping, |
| 122 | + 'is_template' => $is_template, | ||
| 121 | 'fieldOptions' => [ | 123 | 'fieldOptions' => [ |
| 122 | [ 'value' => 'template_name', 'label' => 'Tên Mẫu PDF' ], | 124 | [ 'value' => 'template_name', 'label' => 'Tên Mẫu PDF' ], |
| 123 | [ 'value' => 'customer_name', 'label' => 'Tên khách hàng' ], | 125 | [ 'value' => 'customer_name', 'label' => 'Tên khách hàng' ], | ... | ... |
| ... | @@ -412,9 +412,10 @@ | ... | @@ -412,9 +412,10 @@ |
| 412 | 412 | ||
| 413 | this.ocrData = data.ocrData; | 413 | this.ocrData = data.ocrData; |
| 414 | this.pdfImageUrl = data.pdfImageUrl; | 414 | this.pdfImageUrl = data.pdfImageUrl; |
| 415 | - this.formData = data.formData; | ||
| 416 | this.fieldOptions = data.fieldOptions; | 415 | this.fieldOptions = data.fieldOptions; |
| 416 | + this.formData = data.formData; | ||
| 417 | this.dataMapping = data.dataMapping; | 417 | this.dataMapping = data.dataMapping; |
| 418 | + this.is_template = data.is_template; | ||
| 418 | // Đợi image load xong trước khi xử lý | 419 | // Đợi image load xong trước khi xử lý |
| 419 | if (this.$refs.pdfImage && this.$refs.pdfImage.complete) { | 420 | if (this.$refs.pdfImage && this.$refs.pdfImage.complete) { |
| 420 | this.processLoadedData(); | 421 | this.processLoadedData(); |
| ... | @@ -444,14 +445,33 @@ | ... | @@ -444,14 +445,33 @@ |
| 444 | // Tự động map field cho các box OCR dựa trên formData đã load từ DB | 445 | // Tự động map field cho các box OCR dựa trên formData đã load từ DB |
| 445 | autoMapFieldsFromFormData() { | 446 | autoMapFieldsFromFormData() { |
| 446 | this.manualBoxData = {}; // reset | 447 | this.manualBoxData = {}; // reset |
| 448 | + | ||
| 449 | + if(this.is_template) { | ||
| 450 | + this.formData = {}; | ||
| 451 | + } | ||
| 447 | Object.keys(this.dataMapping).forEach(fieldName => { | 452 | Object.keys(this.dataMapping).forEach(fieldName => { |
| 448 | - const { text, coords } = this.dataMapping[fieldName]; | 453 | + let { text, coords } = this.dataMapping[fieldName]; |
| 449 | - // Ví dụ: tạo box từ dữ liệu | 454 | + console.log(`Auto-mapping field "${fieldName}" with coords:`, coords, 'and text:', text); |
| 450 | - //this.createManualBoxFromDB(fieldName, coords, text); | 455 | + this.ocrData.forEach(item => { |
| 456 | + if (this.isBoxInside(item.bbox, coords) && item.text.trim()) { | ||
| 457 | + const partial = 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 | ||
| 459 | + this.dataMapping[fieldName] = { | ||
| 460 | + text: partial, | ||
| 461 | + coords: coords | ||
| 462 | + }; | ||
| 463 | + } | ||
| 464 | + }); | ||
| 451 | 465 | ||
| 466 | + if (this.is_template) { | ||
| 467 | + this.formData[fieldName] = text || ''; | ||
| 468 | + } | ||
| 452 | this.manualBoxData[fieldName] = { text, coords }; | 469 | this.manualBoxData[fieldName] = { text, coords }; |
| 453 | }); | 470 | }); |
| 454 | 471 | ||
| 472 | + console.log('dataMapping:', this.dataMapping); | ||
| 473 | + console.log('formData:', this.formData); | ||
| 474 | + | ||
| 455 | }, | 475 | }, |
| 456 | 476 | ||
| 457 | onImageLoad() { | 477 | onImageLoad() { | ... | ... |
| ... | @@ -511,15 +511,15 @@ | ... | @@ -511,15 +511,15 @@ |
| 511 | } else { | 511 | } else { |
| 512 | // Kiểm tra xem ocrData đã có box nào với field này chưa | 512 | // Kiểm tra xem ocrData đã có box nào với field này chưa |
| 513 | const existingBox = this.ocrData.find(b => b.field === field && !b.isDeleted); | 513 | const existingBox = this.ocrData.find(b => b.field === field && !b.isDeleted); |
| 514 | - if (existingBox) { | 514 | + if (this.manualBoxData[field]) { |
| 515 | - coords = existingBox.bbox; | ||
| 516 | - text = existingBox.text; | ||
| 517 | - console.log(`Using existing box for field "${field}":`, coords, text); | ||
| 518 | - } else if (this.manualBoxData[field]) { | ||
| 519 | // Nếu không có trong ocrData, dùng manualBoxData (tọa độ mới) | 515 | // Nếu không có trong ocrData, dùng manualBoxData (tọa độ mới) |
| 520 | coords = this.manualBoxData[field].coords; | 516 | coords = this.manualBoxData[field].coords; |
| 521 | text = this.manualBoxData[field].text; | 517 | text = this.manualBoxData[field].text; |
| 522 | console.log(`Using manualBoxData (new coordinates) for field "${field}":`, coords, text); | 518 | console.log(`Using manualBoxData (new coordinates) for field "${field}":`, coords, text); |
| 519 | + } else if (existingBox) { | ||
| 520 | + coords = existingBox.bbox; | ||
| 521 | + text = existingBox.text; | ||
| 522 | + console.log(`Using existing box for field "${field}":`, coords, text); | ||
| 523 | } | 523 | } |
| 524 | } | 524 | } |
| 525 | 525 | ... | ... |
-
Please register or sign in to post a comment