tien_nemo

update data tpl

...@@ -17,34 +17,42 @@ class OcrController extends Controller ...@@ -17,34 +17,42 @@ class OcrController extends Controller
17 public function store(Request $request) 17 public function store(Request $request)
18 { 18 {
19 19
20 -// dd($request->all());
21 $request->validate([ 20 $request->validate([
22 'customer_name_text' => 'required|string', 21 'customer_name_text' => 'required|string',
23 'customer_name_xy' => 'required|string', 22 'customer_name_xy' => 'required|string',
24 - 'template_name' => 'required|string|unique:mst_template,tpl_name',
25 ]); 23 ]);
26 - 24 + $dataDetail = $request->fields ?? [];
27 try { 25 try {
28 - // Lưu vào bảng mst_template 26 + $masterTemplate = MstTemplate::updateOrCreate(
29 - $mst = MstTemplate::create([ 27 + ['tpl_name' => $request->template_name],
30 - 'tpl_name' => $request->template_name, 28 + [
31 - 'tpl_text' => $request->customer_name_text, 29 + 'tpl_text' => $request->customer_name_text,
32 - 'tpl_xy' => $request->customer_name_xy, 30 + 'tpl_xy' => $request->customer_name_xy,
33 - ]); 31 + ]
32 + );
33 +
34 + foreach ($dataDetail as $field => $value) {
35 + if (empty($value['coords'])) {
36 + continue;
37 + }
34 38
35 - // Lưu các field khác vào dt_template 39 + DtTemplate::updateOrInsert(
36 - foreach ($request->fields as $field => $value) { 40 + [
37 - DtTemplate::create([ 41 + 'tpl_id' => $masterTemplate->id,
38 - 'tpl_id' => $mst->id, 42 + 'field_name' => $field,
39 - 'field_name' => $field, 43 + ],
40 - 'field_xy' => is_array($value['coords']) ? implode(',', $value['coords']) : $value['coords'], 44 + [
41 - ]); 45 + 'field_xy' => is_array($value['coords'])
42 - } 46 + ? implode(',', $value['coords'])
47 + : $value['coords'],
48 + ]
49 + );
43 50
51 + }
44 return response()->json([ 52 return response()->json([
45 'success' => true, 53 'success' => true,
46 'message' => 'Lưu template thành công', 54 'message' => 'Lưu template thành công',
47 - 'template_id' => $mst->id 55 + 'template_id' => $masterTemplate->id
48 ]); 56 ]);
49 } catch (\Exception $e) { 57 } catch (\Exception $e) {
50 return response()->json([ 58 return response()->json([
...@@ -59,7 +67,7 @@ class OcrController extends Controller ...@@ -59,7 +67,7 @@ class OcrController extends Controller
59 { 67 {
60 try { 68 try {
61 // Lấy template name từ request hoặc mặc định 69 // Lấy template name từ request hoặc mặc định
62 - $templateName = $request->get('template_name', ''); 70 + $templateName = $request->get('template_name', 'nemo12');
63 71
64 // Giả sử file OCR JSON & ảnh nằm trong storage/app/public/image/ 72 // Giả sử file OCR JSON & ảnh nằm trong storage/app/public/image/
65 $jsonPath = public_path("image/data_picking_detail_1754967679.json"); 73 $jsonPath = public_path("image/data_picking_detail_1754967679.json");
......
...@@ -10,6 +10,7 @@ class DtTemplate extends Model ...@@ -10,6 +10,7 @@ class DtTemplate extends Model
10 public $timestamps = false; 10 public $timestamps = false;
11 11
12 protected $table = 'dt_template'; 12 protected $table = 'dt_template';
13 + protected $primaryKey = 'tpl_detail_id';
13 protected $guarded = []; 14 protected $guarded = [];
14 15
15 16
......