Merge branch '2025/ntctien/14567_edit_template' into 'dev'
2025/ntctien/14567 edit template See merge request !2
Showing
3 changed files
with
28 additions
and
67 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 | + ); | ||
| 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"); |
| ... | @@ -127,52 +135,4 @@ class OcrController extends Controller | ... | @@ -127,52 +135,4 @@ class OcrController extends Controller |
| 127 | ], 500); | 135 | ], 500); |
| 128 | } | 136 | } |
| 129 | } | 137 | } |
| 130 | - | ||
| 131 | - /** | ||
| 132 | - * Lấy danh sách template | ||
| 133 | - */ | ||
| 134 | - public function getTemplateList() | ||
| 135 | - { | ||
| 136 | - try { | ||
| 137 | - $templates = MstTemplate::select('id', 'tpl_name', 'tpl_text', 'in_date') | ||
| 138 | - ->orderBy('created_at', 'desc') | ||
| 139 | - ->get(); | ||
| 140 | - | ||
| 141 | - return response()->json([ | ||
| 142 | - 'success' => true, | ||
| 143 | - 'templates' => $templates | ||
| 144 | - ]); | ||
| 145 | - } catch (\Exception $e) { | ||
| 146 | - return response()->json([ | ||
| 147 | - 'success' => false, | ||
| 148 | - 'error' => 'Lỗi khi lấy danh sách template: ' . $e->getMessage() | ||
| 149 | - ], 500); | ||
| 150 | - } | ||
| 151 | - } | ||
| 152 | - | ||
| 153 | - /** | ||
| 154 | - * Xóa template | ||
| 155 | - */ | ||
| 156 | - public function deleteTemplate($id) | ||
| 157 | - { | ||
| 158 | - try { | ||
| 159 | - $template = MstTemplate::findOrFail($id); | ||
| 160 | - | ||
| 161 | - // Xóa các field detail trước | ||
| 162 | - DtTemplate::where('tpl_id', $id)->delete(); | ||
| 163 | - | ||
| 164 | - // Xóa template chính | ||
| 165 | - $template->delete(); | ||
| 166 | - | ||
| 167 | - return response()->json([ | ||
| 168 | - 'success' => true, | ||
| 169 | - 'message' => 'Xóa template thành công' | ||
| 170 | - ]); | ||
| 171 | - } catch (\Exception $e) { | ||
| 172 | - return response()->json([ | ||
| 173 | - 'success' => false, | ||
| 174 | - 'error' => 'Lỗi khi xóa template: ' . $e->getMessage() | ||
| 175 | - ], 500); | ||
| 176 | - } | ||
| 177 | - } | ||
| 178 | } | 138 | } | ... | ... |
| ... | @@ -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 | ... | ... |
This diff is collapsed.
Click to expand it.
-
Please register or sign in to post a comment