-
+
- {{ $address->location }} +
- {{ $address->address }} +
diff --git a/app/Http/Controllers/Auth/AddressController.php b/app/Http/Controllers/Auth/AddressController.php new file mode 100644 index 0000000..d4cbae8 --- /dev/null +++ b/app/Http/Controllers/Auth/AddressController.php @@ -0,0 +1,56 @@ +user()->addresses; + return view('account.addresses', compact('addresses')); + } + + public function provinces() + { + $provinces = Province::orderBy('name')->get(); + + + return [ + 'data' => $provinces, + ]; + } + + public function cities($provinceId) + { + $cities = City::where('province_id', $provinceId)->orderBy('name')->get(); + + return [ + 'data' => $cities, + ]; + } + + public function districts($city_id) + { + $districts = District::where('city_id', $city_id)->orderBy('name')->get(); + + return [ + 'data' => $districts, + ]; + } + + public function villages($districtId) + { + $villages = Subdistrict::where('district_id', $districtId)->orderBy('name')->get(); + + return [ + 'data' => $villages, + ]; + } +} diff --git a/app/Http/Middleware/SetLocale.php b/app/Http/Middleware/SetLocale.php index e442027..15c58bc 100644 --- a/app/Http/Middleware/SetLocale.php +++ b/app/Http/Middleware/SetLocale.php @@ -31,7 +31,7 @@ class SetLocale // Check if locale is in session elseif (Session::has('locale')) { - $locale = Session::get('locale'); + $locale = Session::get('locale') ?? 'id'; if (in_array($locale, ['en', 'id'])) { App::setLocale($locale); diff --git a/app/Models/Address.php b/app/Models/Address.php new file mode 100644 index 0000000..1844c2a --- /dev/null +++ b/app/Models/Address.php @@ -0,0 +1,80 @@ +belongsTo(User::class, 'user_id', 'id'); + } + + public function province() + { + return $this->belongsTo(Province::class, 'province_id', 'id'); + } + + public function city() + { + return $this->belongsTo(City::class, 'city_id', 'id'); + } + + public function district() + { + return $this->belongsTo(District::class, 'district_id', 'id'); + } + + public function subdistrict() + { + return $this->belongsTo(Subdistrict::class, 'subdistrict_id', 'id'); + } + + + public function getLocationAttribute() + { + $province = $this->province?->name; + $city = $this->city?->name; + $district = $this->district?->name; + $subdistrict = $this->subdistrict?->name; + $postalCode = $this->postal_code; + + return "{$province}, {$city}, {$district}, {$subdistrict}, {$postalCode}"; + } +} diff --git a/app/Models/City.php b/app/Models/City.php new file mode 100644 index 0000000..ab5d9f7 --- /dev/null +++ b/app/Models/City.php @@ -0,0 +1,18 @@ +belongsTo(Province::class, 'province_id', 'id'); + } +} diff --git a/app/Models/District.php b/app/Models/District.php new file mode 100644 index 0000000..926ea5a --- /dev/null +++ b/app/Models/District.php @@ -0,0 +1,18 @@ +belongsTo(City::class, 'city_id', 'id'); + } +} diff --git a/app/Models/PosCode.php b/app/Models/PosCode.php new file mode 100644 index 0000000..e13009e --- /dev/null +++ b/app/Models/PosCode.php @@ -0,0 +1,40 @@ +belongsTo(Province::class,"provice_id","id"); + } + + public function cityRef(){ + return $this->belongsTo(City::class,"city_id","id"); + } + + public function districtRef(){ + return $this->belongsTo(City::class,"district_id","id"); + } + + public function subdistrictRef(){ + return $this->belongsTo(City::class,"subdistrict_id","id"); + } + +} diff --git a/app/Models/Province.php b/app/Models/Province.php new file mode 100644 index 0000000..aa37bb4 --- /dev/null +++ b/app/Models/Province.php @@ -0,0 +1,13 @@ +belongsTo(District::class, 'district_id', 'id'); + } +} diff --git a/app/Models/User.php b/app/Models/User.php index 952651f..25328e4 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -56,4 +56,9 @@ class User extends Authenticatable { return $this->hasOne(Customer::class); } + + public function addresses() + { + return $this->hasMany(Address::class); + } } diff --git a/app/Repositories/Member/Address/MemberAddressRepository.php b/app/Repositories/Member/Address/MemberAddressRepository.php new file mode 100644 index 0000000..675815d --- /dev/null +++ b/app/Repositories/Member/Address/MemberAddressRepository.php @@ -0,0 +1,295 @@ +user()->id)->orderBy('created_at','desc')->paginate($request->limit); + + return $model; + } + + public function geocode($params){ + + $lat = @$params["latitude"]; + $lon = @$params["longitude"]; + $res = Cache::remember("geocode_$lat\_$lon", 60 * 60 * 24, function () use ($lat, $lon){ + return Http::get("https://nominatim.openstreetmap.org/reverse",[ + "email" => "husnusetiawan@gmail.com", + "lat" => $lat, + "lon" => $lon, + "format" => "json" + ])->json(); + }); + + + + try { + $address = @$res["address"]; + + $posCode = PosCode::where("code", $address['postcode'])->firstOrFail(); + + $province_id = $posCode->province_id ?? null; + $city_id = $posCode->city_id ?? null; + $district_id = $posCode->district_id ?? null; + $subdistrict_id = $posCode->subdistrict_id ?? null; + if ($address != null) { + $addr_province = $address["state"] ?? $address["city"] ?? $posCode->province; + $addr_city = $address["city"] ?? $address["county"] ?? $address["city_district"] ?? $posCode->city; + $addr_district = $address["suburb"] ?? $address["district"] ?? $posCode->district; + $addr_subdistrict = $address["neighbourhood"] ?? $address["village"] ?? $posCode->subdistrict; + + if ($addr_province != null && $province_id == null) { + // search by nominatim + $province = Province::whereRaw('name ILIKE ?', ['%' . trim($addr_province) . '%'])->first(); + + // search by poscode + if ($province == null) { + $province = Province::whereRaw('name ILIKE ?', ['%' . trim($posCode->province) . '%'])->first(); + } + + $province_id = $posCode->province_id ?? $province->id ?? null; + + } + + if ($addr_city != null && $city_id == null) { + // search by nominatim + $city = City::whereRaw('name ILIKE ?', ['%' . trim($addr_city) . '%']) + ->where("province_id", $province_id) + ->first(); + + // search by poscode + if ($city == null) { + $city = City::whereRaw('name ILIKE ?', ['%' . trim($posCode->city) . '%']) + ->where("province_id", $province_id) + ->first(); + } + + $city_id = $posCode->city_id ?? $city->id ?? null; + } + + + if ($addr_district != null && $district_id == null) { + // search by nominatim + $district = District::whereRaw('name ILIKE ?', ['%' . trim($addr_district) . '%']) + ->where("city_id", $city_id) + ->first(); + + + + // search by poscode + if ($district == null) { + $district = District::whereRaw('name ILIKE ?', ['%' . trim($posCode->district) . '%']) + ->where("city_id", $city_id) + ->first(); + } + + + + $district_id = $posCode->district_id ?? $district->id ?? null; + } + + if ($addr_subdistrict != null && $subdistrict_id == null) { + // search by nominatim + $subdistrict = Subdistrict::whereRaw('name ILIKE ?', ['%' . trim($addr_subdistrict) . '%']) + ->where("district_id", $district_id) + ->first(); + + // search by poscode + if ($subdistrict == null) { + $subdistrict = Subdistrict::whereRaw('name ILIKE ?', ['%' . trim($posCode->subdistrict) . '%']) + ->where("district_id", $district_id) + ->first(); + } + + $subdistrict_id = $posCode->subdistrict_id ?? $subdistrict->id ?? null; + } + + + } + + $address = new Address([ + 'label' => "", + 'name' => "", + 'address' => $address["road"] ?? $res["display_name"], + 'province_id' => $province_id, + 'city_id' => $city_id, + 'district_id' => $district_id, + 'subdistrict_id' => $subdistrict_id, + + 'postal_code' => $address['postcode'], + 'phone' => "", + 'latitude' => $res['lat'], + 'longitude' => $res['lon'], + 'user_id' => auth()->user()->id, + 'is_primary' => false + ]); + } catch (ModelNotFoundException $e) { + + abort(404, 'lokasi tidak didukung'); + } + + return $address; + } + + public function create($data) + { + $model = DB::transaction(function () use ($data) { + + $model = Address::create([ + 'label' => $data['label'], + 'name' => $data['name'], + 'address' => $data['address'], + 'province_id' => $data['province_id'], + 'city_id' => $data['city_id'], + 'district_id' => $data['district_id'], + 'subdistrict_id' => $data['subdistrict_id'], + 'postal_code' => $data['postal_code'], + 'phone' => $data['phone'], + 'latitude' => $data['latitude'], + 'longitude' => $data['longitude'], + 'user_id' => auth()->user()->id, + 'is_primary' => $data['is_primary'] + ]); + + return $model; + }); + + return $model; + } + + public function update($id ,$data) + { + $model = DB::transaction(function () use ($id, $data) { + + $model = Address::findOrFail($id); + $model->label = $data['label'] ?? $model->label; + $model->name = $data['name'] ?? $model->name; + $model->address = $data['address'] ?? $model->address; + $model->province_id = $data['province_id'] ?? $model->province_id; + $model->city_id = $data['city_id'] ?? $model->city_id; + $model->district_id = $data['district_id'] ?? $model->district_id; + $model->subdistrict_id = $data['subdistrict_id'] ?? $model->subdistrict_id; + $model->postal_code = $data['postal_code'] ?? $model->postal_code; + $model->phone = $data['phone'] ?? $model->phone; + $model->latitude = $data['latitude'] ?? $model->latitude; + $model->longitude = $data['longitude'] ?? $model->longitude; + $model->is_primary = $data['is_primary'] ?? $model->is_primary; + $model->save(); + return $model; + }); + + return $model; + } + + public function setPrimary($id) + { + $model = DB::transaction(function () use ($id) { + + $model = Address::findOrFail($id); + + Address::where("user_id", $model->user_id)->update([ + "is_primary" => 0 + ]); + + + $model->is_primary = 1; + $model->save(); + + return $model; + }); + + return $model; + } + + public function delete($model) + { + $model = DB::transaction(function () use ($model) { + $model->delete(); + return $model; + }); + + return $model; + } + + public function getProvince($request) + { + $model = Province::orderBy('name','desc')->where("is_new",true)->paginate($request->limit); + + return $model; + } + + public function getCity($request) + { + $all = $request->all(); + + $model = City::orderBy('name','desc')->where("is_new",true) + ->where(function($query) use ($all){ + if (@$all["province_id"]) + $query->where("province_id", $all["province_id"]); + })->paginate($request->limit); + + return $model; + } + + public function getDistrict($request) + { + $all = $request->all(); + + $model = District::orderBy('name','desc')->where("is_new",true) + ->where(function($query) use ($all){ + if (@$all["city_id"]) + $query->where("city_id", $all["city_id"]); + })->paginate($request->limit); + + return $model; + } + + public function getSubdistrict($request) + { + $all = $request->all(); + $model = Subdistrict::orderBy('name','desc')->where("is_new",true) + ->where(function($query) use ($all){ + if (@$all["district_id"]) + $query->where("district_id", $all["district_id"]); + }) + ->paginate($request->limit); + + return $model; + } + + public function getPoscode($request) + { + + $all = $request->all(); + $model = PosCode::orderBy('province','desc') + ->where(function($query) use ($all){ + if (@$all["village"]) + $query->where("village", $all["village"]); + + if (@$all["district"]) + $query->where("district", $all["district"]); + + if (@$all["regency"]) + $query->where("regency", $all["regency"]); + }) + ->paginate($request->limit); + + return $model; + } + +} diff --git a/lang/en/addresses.php b/lang/en/addresses.php new file mode 100644 index 0000000..cb466a2 --- /dev/null +++ b/lang/en/addresses.php @@ -0,0 +1,40 @@ + 'Addresses', + 'shipping_address' => 'Shipping address', + 'primary' => 'Primary', + 'edit' => 'Edit', + 'country' => 'Country', + 'select_country' => 'Select country...', + 'province' => 'Province', + 'select_province' => 'Select province...', + 'city' => 'City', + 'select_city' => 'Select city...', + 'district' => 'District', + 'select_district' => 'Select district...', + 'village' => 'Village', + 'select_village' => 'Select village...', + 'zip_code' => 'ZIP code', + 'address' => 'Address', + 'set_as_primary_address' => 'Set as primary address', + 'save_changes' => 'Save changes', + 'close' => 'Close', + 'alternative_shipping_address' => 'Alternative shipping address', + 'add_address' => 'Add address', + 'please_select_country' => 'Please select your country!', + 'please_select_province' => 'Please select your province!', + 'please_select_city' => 'Please select your city!', + 'please_select_district' => 'Please select your district!', + 'please_select_village' => 'Please select your village!', + 'please_enter_zip_code' => 'Please enter your ZIP code!', + 'please_enter_address' => 'Please enter your address!', + 'regions' => [ + 'africa' => 'Africa', + 'asia' => 'Asia', + 'europe' => 'Europe', + 'north_america' => 'North America', + 'south_america' => 'South America', + 'oceania' => 'Oceania' + ] +]; \ No newline at end of file diff --git a/lang/id/addresses.php b/lang/id/addresses.php new file mode 100644 index 0000000..e7275bc --- /dev/null +++ b/lang/id/addresses.php @@ -0,0 +1,40 @@ + 'Alamat', + 'shipping_address' => 'Alamat Pengiriman', + 'primary' => 'Utama', + 'edit' => 'Edit', + 'country' => 'Negara', + 'select_country' => 'Pilih negara...', + 'province' => 'Provinsi', + 'select_province' => 'Pilih provinsi...', + 'city' => 'Kota', + 'select_city' => 'Pilih kota...', + 'district' => 'Kecamatan', + 'select_district' => 'Pilih kecamatan...', + 'village' => 'Kelurahan', + 'select_village' => 'Pilih kelurahan...', + 'zip_code' => 'Kode Pos', + 'address' => 'Alamat', + 'set_as_primary_address' => 'Jadikan alamat utama', + 'save_changes' => 'Simpan perubahan', + 'close' => 'Tutup', + 'alternative_shipping_address' => 'Alamat Pengiriman Alternatif', + 'add_address' => 'Tambah alamat', + 'please_select_country' => 'Silakan pilih negara Anda!', + 'please_select_province' => 'Silakan pilih provinsi Anda!', + 'please_select_city' => 'Silakan pilih kota Anda!', + 'please_select_district' => 'Silakan pilih kecamatan Anda!', + 'please_select_village' => 'Silakan pilih kelurahan Anda!', + 'please_enter_zip_code' => 'Silakan masukkan kode pos Anda!', + 'please_enter_address' => 'Silakan masukkan alamat Anda!', + 'regions' => [ + 'africa' => 'Afrika', + 'asia' => 'Asia', + 'europe' => 'Eropa', + 'north_america' => 'Amerika Utara', + 'south_america' => 'Amerika Selatan', + 'oceania' => 'Oseania' + ] +]; \ No newline at end of file diff --git a/resources/views/account/addresses.blade.php b/resources/views/account/addresses.blade.php index 47cd01e..50ecf9c 100644 --- a/resources/views/account/addresses.blade.php +++ b/resources/views/account/addresses.blade.php @@ -1,4 +1,4 @@ -@extends('layouts.account', ['title' => 'Account - Addresses']) +@extends('layouts.account', ['title' => __('addresses.page_title')]) @section('content') @@ -6,198 +6,177 @@