fix(cities): perbaiki pencarian dan pemetaan provinsi untuk kota

- Tambahkan logika untuk menangani kode kota khusus di provinsi Papua.
- Perbarui pemetaan nama provinsi berdasarkan kode kota.
- Sesuaikan tampilan data kota di tabel dengan nama provinsi yang benar.
This commit is contained in:
Daeng Deni Mardaeni
2025-04-27 12:14:22 +07:00
parent 1a7703a6cc
commit bf288013f4
2 changed files with 45 additions and 7 deletions

View File

@@ -80,8 +80,21 @@ class CitiesController extends Controller
if ($request->has('search') && !empty($request->get('search'))) { if ($request->has('search') && !empty($request->get('search'))) {
$search = $request->get('search'); $search = $request->get('search');
$search = explode('|', $search); $search = explode('|', $search);
if(isset($search[0]) &&!empty($search[0])){ if(isset($search[0]) && !empty($search[0])){
$query->where('province_code',$search[0]); // Define special city codes once
$specialCityCodes = ['92.01', '92.04', '92.05', '92.09', '92.10', '92.71'];
// Handle Papua province special cases
if($search[0] == '92'){
$query->where('province_code', '92')
->whereNotIn('code', $specialCityCodes);
} else if($search[0] == '92.1'){
$query->where('province_code', '92')
->whereIn('code', $specialCityCodes);
} else {
// For all other provinces
$query->where('province_code', $search[0]);
}
} }
$query->where(function ($q) use ($search) { $query->where(function ($q) use ($search) {
$q->where('code', 'LIKE', "%$search[1]%"); $q->where('code', 'LIKE', "%$search[1]%");
@@ -121,6 +134,19 @@ class CitiesController extends Controller
// Calculate the current page number // Calculate the current page number
$currentPage = 0 + 1; $currentPage = 0 + 1;
//maping data for datatables,
//jika city code adalah '92.01', '92.04', '92.05', '92.09', '92.10', '92.71' maka province code yang dipilih adalah 92.1
$data = $data->map(function ($item) {
if (in_array($item->code, ['92.01', '92.04', '92.05', '92.09', '92.10', '92.71'])) {
$item->province_name = Province::where('code', '92.1')->first()->name;
} else {
$item->province_name = $item->province->name;
}
return $item;
});
// Apply the search filter again if there is a search filter
// Return the response data as a JSON object // Return the response data as a JSON object
return response()->json([ return response()->json([
'draw' => $request->get('draw'), 'draw' => $request->get('draw'),
@@ -138,7 +164,20 @@ class CitiesController extends Controller
return Excel::download(new CitiesExport, 'cities.xlsx'); return Excel::download(new CitiesExport, 'cities.xlsx');
} }
public function getCitiesByProvinceId($id){ public function getCitiesByProvinceId($id)
return response()->json(City::where('province_code', $id)->get()); {
$query = City::query();
$provinceCode = substr($id, 0, 2);
$query->where('province_code', $provinceCode);
if ($id == '92.1') {
$query->whereIn('code', ['92.01', '92.04', '92.05', '92.09', '92.10', '92.71']);
} elseif ($id == '92') {
$query->whereNotIn('code', ['92.01', '92.04', '92.05', '92.09', '92.10', '92.71']);
}
return response()->json($query->get());
} }
} }

View File

@@ -38,9 +38,8 @@
</label> </label>
<button class="search btn btn-sm btn-outline btn-primary"> <button class="search btn btn-sm btn-outline btn-primary">
<i class="ki-filled ki-setting-4"> </i> <i class="ki-filled ki-setting-4"> </i>
<Filters></Filters>
</button> </button>
<div class="h-[24px] border border-r-gray-200"></div> <div class="border border-r-gray-200"></div>
<a class="btn btn-sm btn-light" href="{{ route('locations.cities.export') }}"> Export to Excel </a> <a class="btn btn-sm btn-light" href="{{ route('locations.cities.export') }}"> Export to Excel </a>
<a class="btn btn-sm btn-primary" href="{{ route('locations.cities.create') }}"> Add City </a> <a class="btn btn-sm btn-primary" href="{{ route('locations.cities.create') }}"> Add City </a>
</div> </div>
@@ -150,7 +149,7 @@
province: { province: {
title: 'Province', title: 'Province',
render: (item, data) => { render: (item, data) => {
return data.province.name; return data.province_name;
} }
}, },
actions: { actions: {