Showing
2 changed files
with
25 additions
and
16 deletions
| ... | @@ -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 | + ); | ||
| 34 | 33 | ||
| 35 | - // Lưu các field khác vào dt_template | 34 | + foreach ($dataDetail as $field => $value) { |
| 36 | - foreach ($request->fields as $field => $value) { | 35 | + if (empty($value['coords'])) { |
| 37 | - DtTemplate::create([ | 36 | + continue; |
| 38 | - 'tpl_id' => $mst->id, | ||
| 39 | - 'field_name' => $field, | ||
| 40 | - 'field_xy' => is_array($value['coords']) ? implode(',', $value['coords']) : $value['coords'], | ||
| 41 | - ]); | ||
| 42 | } | 37 | } |
| 43 | 38 | ||
| 39 | + DtTemplate::updateOrInsert( | ||
| 40 | + [ | ||
| 41 | + 'tpl_id' => $masterTemplate->id, | ||
| 42 | + 'field_name' => $field, | ||
| 43 | + ], | ||
| 44 | + [ | ||
| 45 | + 'field_xy' => is_array($value['coords']) | ||
| 46 | + ? implode(',', $value['coords']) | ||
| 47 | + : $value['coords'], | ||
| 48 | + ] | ||
| 49 | + ); | ||
| 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 | ... | ... |
-
Please register or sign in to post a comment