OcrController.php
5.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
namespace App\Http\Controllers;
use App\Models\DtTemplate;
use App\Models\MstTemplate;
use Illuminate\Http\Request;
class OcrController extends Controller
{
public function index()
{
return view('ocr.index');
}
public function store(Request $request)
{
// dd($request->all());
$request->validate([
'customer_name_text' => 'required|string',
'customer_name_xy' => 'required|string',
'template_name' => 'required|string|unique:mst_template,tpl_name',
]);
try {
// Lưu vào bảng mst_template
$mst = MstTemplate::create([
'tpl_name' => $request->template_name,
'tpl_text' => $request->customer_name_text,
'tpl_xy' => $request->customer_name_xy,
]);
// Lưu các field khác vào dt_template
foreach ($request->fields as $field => $value) {
DtTemplate::create([
'tpl_id' => $mst->id,
'field_name' => $field,
'field_xy' => is_array($value['coords']) ? implode(',', $value['coords']) : $value['coords'],
]);
}
return response()->json([
'success' => true,
'message' => 'Lưu template thành công',
'template_id' => $mst->id
]);
} catch (\Exception $e) {
return response()->json([
'success' => false,
'message' => 'Lỗi khi lưu template: ' . $e->getMessage()
], 500);
}
}
public function getData(Request $request)
{
try {
// Lấy template name từ request hoặc mặc định
$templateName = $request->get('template_name', '');
// Giả sử file OCR JSON & ảnh nằm trong storage/app/public/image/
$jsonPath = public_path("image/data_picking_detail_1754967679.json");
$imgPath = ("image/data_picking_detail_1754967679.jpg");
if (!file_exists($jsonPath)) {
return response()->json(['error' => 'File OCR JSON không tìm thấy'], 404);
}
$ocrData = json_decode(file_get_contents($jsonPath), true);
if (json_last_error() !== JSON_ERROR_NONE) {
return response()->json(['error' => 'File OCR JSON không hợp lệ'], 400);
}
$dataMapping = [];
$is_template = false;
if ($templateName) {
$mst = MstTemplate::where('tpl_name', $templateName)->first();
if ($mst) {
// Lấy detail của template
$details = DtTemplate::where('tpl_id', $mst->id)->get();
foreach ($details as $detail) {
$coords = array_map('intval', explode(',', $detail->field_xy));
// coords = [x1, y1, x2, y2]
$dataMapping[$detail->field_name] = [
'text' => '',
'coords' => $coords
];
}
$dataMapping['template_name'] = [
'text' => $mst->tpl_name,
'coords' => ''
];
$is_template = true;
}
}
return response()->json([
'success' => true,
'ocrData' => $ocrData,
'pdfImageUrl' => $imgPath,
'dataMapping' => $dataMapping,
'is_template' => $is_template,
'fieldOptions' => [
[ 'value' => 'template_name', 'label' => 'Tên Mẫu PDF' ],
[ 'value' => 'customer_name', 'label' => 'Tên khách hàng' ],
[ 'value' => 'export_date', 'label' => 'Ngày xuất' ],
[ 'value' => 'order_code', 'label' => 'Mã đơn hàng' ],
[ 'value' => 'customer', 'label' => 'Khách hàng' ],
[ 'value' => 'address', 'label' => 'Địa chỉ' ],
[ 'value' => 'staff', 'label' => 'Nhân viên' ],
],
'template_name' => $templateName
]);
} catch (\Exception $e) {
return response()->json([
'success' => false,
'error' => 'Lỗi khi load data: ' . $e->getMessage()
], 500);
}
}
/**
* Lấy danh sách template
*/
public function getTemplateList()
{
try {
$templates = MstTemplate::select('id', 'tpl_name', 'tpl_text', 'in_date')
->orderBy('created_at', 'desc')
->get();
return response()->json([
'success' => true,
'templates' => $templates
]);
} catch (\Exception $e) {
return response()->json([
'success' => false,
'error' => 'Lỗi khi lấy danh sách template: ' . $e->getMessage()
], 500);
}
}
/**
* Xóa template
*/
public function deleteTemplate($id)
{
try {
$template = MstTemplate::findOrFail($id);
// Xóa các field detail trước
DtTemplate::where('tpl_id', $id)->delete();
// Xóa template chính
$template->delete();
return response()->json([
'success' => true,
'message' => 'Xóa template thành công'
]);
} catch (\Exception $e) {
return response()->json([
'success' => false,
'error' => 'Lỗi khi xóa template: ' . $e->getMessage()
], 500);
}
}
}