merge with feat reklame
This commit is contained in:
199
app/Http/Controllers/Api/AdvertisementController.php
Normal file
199
app/Http/Controllers/Api/AdvertisementController.php
Normal file
@@ -0,0 +1,199 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Models\Advertisement;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Http\Requests\AdvertisementRequest;
|
||||
use Illuminate\Http\Response;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Resources\AdvertisementResource;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Maatwebsite\Excel\Facades\Excel;
|
||||
use App\Imports\AdvertisementImport;
|
||||
|
||||
class AdvertisementController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index(Request $request)
|
||||
{
|
||||
$perPage = $request->input('per_page', 15); // Default 15 jika tidak dikirim oleh client
|
||||
$search = $request->input('search', ''); // Ambil parameter 'search' jika ada
|
||||
|
||||
// Query dasar untuk mengambil iklan
|
||||
$query = Advertisement::query();
|
||||
// Jika ada pencarian, filter berdasarkan kolom yang diinginkan
|
||||
if ($search) {
|
||||
$query->where(function ($q) use ($search) {
|
||||
$q->where('business_name', 'like', "%$search%")
|
||||
->orWhere('npwpd', 'like', "%$search%")
|
||||
->orWhere('advertisement_content', 'like', "%$search%")
|
||||
->orWhere('business_address', 'like', "%$search%")
|
||||
->orWhere('advertisement_location', 'like', "%$search%")
|
||||
->orWhereIn('village_code', function ($subQuery) use ($search) {
|
||||
$subQuery->select('village_code')
|
||||
->from('villages')
|
||||
->where('village_name', 'like', "%$search%");
|
||||
})
|
||||
->orWhereIn('district_code', function ($subQuery) use ($search) {
|
||||
$subQuery->select('district_code')
|
||||
->from('districts')
|
||||
->where('district_name', 'like', "%$search%");
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
$advertisements = $query->paginate($perPage);
|
||||
|
||||
$advertisements->getCollection()->transform(function ($advertisement) {
|
||||
$village = DB::table('villages')->where('village_code', $advertisement->village_code)->first();
|
||||
$advertisement->village_name = $village ? $village->village_name : null;
|
||||
|
||||
$district = DB::table('districts')->where('district_code', $advertisement->district_code)->first();
|
||||
$advertisement->district_name = $district ? $district->district_name : null;
|
||||
return $advertisement;
|
||||
});
|
||||
|
||||
return response()->json([
|
||||
'data' => AdvertisementResource::collection($advertisements),
|
||||
'meta' => [
|
||||
'total' => $advertisements->total(),
|
||||
'per_page' => $advertisements->perPage(),
|
||||
'current_page' => $advertisements->currentPage(),
|
||||
'last_page' => $advertisements->lastPage(),
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*/
|
||||
public function store(AdvertisementRequest $request): Advertisement
|
||||
{
|
||||
$data = $request->validated();
|
||||
|
||||
// Cari village_code berdasarkan village_name
|
||||
$village_code = DB::table('villages')->where('village_name', $data['village_name'])->value('village_code');
|
||||
// Cari district_code berdasarkan district_name
|
||||
$district_code = DB::table('districts')->where('district_name', $data['district_name'])->value('district_code');
|
||||
|
||||
// Tambahkan village_code dan district_code ke data
|
||||
$data['village_code'] = $village_code;
|
||||
$data['district_code'] = $district_code;
|
||||
|
||||
// Log data setelah transformasi
|
||||
info($data);
|
||||
return Advertisement::create($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Import advertisements from Excel or CSV.
|
||||
*/
|
||||
public function importFromFile(Request $request)
|
||||
{
|
||||
// Validasi file
|
||||
info($request);
|
||||
$validator = Validator::make($request->all(), [
|
||||
'file' => 'required|mimes:xlsx,xls|max:10240', // Max 10MB
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return response()->json([
|
||||
'message' => 'File validation failed.',
|
||||
'errors' => $validator->errors()
|
||||
], 400);
|
||||
}
|
||||
|
||||
try {
|
||||
// Ambil file dari request
|
||||
$file = $request->file('file');
|
||||
|
||||
// Menggunakan Laravel Excel untuk mengimpor file
|
||||
Excel::import(new AdvertisementImport, $file);
|
||||
|
||||
// Jika sukses, kembalikan respons sukses
|
||||
return response()->json([
|
||||
'message' => 'File uploaded and imported successfully!'
|
||||
], 200);
|
||||
} catch (\Exception $e) {
|
||||
// Jika ada error, kembalikan error response
|
||||
return response()->json([
|
||||
'message' => 'Error during file import.',
|
||||
'error' => $e->getMessage()
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*/
|
||||
public function show(Advertisement $advertisement): Advertisement
|
||||
{
|
||||
return $advertisement;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
public function update(AdvertisementRequest $request, Advertisement $advertisement): Advertisement
|
||||
{
|
||||
$data = $request->validated();
|
||||
|
||||
// Cari village_code berdasarkan village_name
|
||||
$village_code = DB::table('villages')->where('village_name', $data['village_name'])->value('village_code');
|
||||
// Cari district_code berdasarkan district_name
|
||||
$district_code = DB::table('districts')->where('district_name', $data['district_name'])->value('district_code');
|
||||
|
||||
// Tambahkan village_code dan district_code ke data
|
||||
$data['village_code'] = $village_code;
|
||||
$data['district_code'] = $district_code;
|
||||
|
||||
// Log data setelah transformasi
|
||||
info($data);
|
||||
|
||||
$advertisement->update($data);
|
||||
|
||||
return $advertisement;
|
||||
}
|
||||
|
||||
public function destroy(Advertisement $advertisement): Response
|
||||
{
|
||||
$advertisement->delete();
|
||||
|
||||
return response()->noContent();
|
||||
}
|
||||
|
||||
public function searchOptionsInAdvertisements(Request $request)
|
||||
{
|
||||
$query = $request->input('query');
|
||||
$field = $request->input('field');
|
||||
$district = $request->input('district'); // Ambil kecamatan jika ada
|
||||
|
||||
info("Query: $query, Field: $field, District: $district");
|
||||
|
||||
if ($field === 'district_name') {
|
||||
$results = DB::table('districts')
|
||||
->where('district_name', 'like', '%' . $query . '%')
|
||||
->limit(10)
|
||||
->get(['district_name AS name', 'district_code AS code']);
|
||||
} elseif ($field === 'village_name' && $district) {
|
||||
$results = DB::table('villages')
|
||||
->where('village_name', 'like', '%' . $query . '%')
|
||||
->whereExists(function ($query) use ($district) {
|
||||
$query->select(DB::raw(1))
|
||||
->from('districts')
|
||||
->whereColumn('villages.district_code', 'districts.district_code')
|
||||
->where('districts.district_name', $district);
|
||||
})
|
||||
->limit(10)
|
||||
->get(['village_name AS name', 'village_code AS code']);
|
||||
} else {
|
||||
$results = collect();
|
||||
}
|
||||
|
||||
return response()->json($results);
|
||||
}
|
||||
}
|
||||
135
app/Http/Controllers/Data/AdvertisementController.php
Normal file
135
app/Http/Controllers/Data/AdvertisementController.php
Normal file
@@ -0,0 +1,135 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Data;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Advertisement;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class AdvertisementController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
return view('data.advertisements.index');
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for uploading a file.
|
||||
*/
|
||||
public function bulkCreate()
|
||||
{
|
||||
// Mengembalikan view form-upload
|
||||
return view('data.advertisements.form-upload');
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
$title = 'Advertisement';
|
||||
$subtitle = 'Create Data';
|
||||
|
||||
// Mengambil data untuk dropdown
|
||||
$dropdownOptions = [
|
||||
'village_name' => DB::table('villages')->orderBy('village_name')->pluck('village_name', 'village_code'),
|
||||
'district_name' => DB::table('districts')->orderBy('district_name')->pluck('district_name', 'district_code'),
|
||||
];
|
||||
|
||||
$fields = $this->getFields();
|
||||
$fieldTypes = $this->getFieldTypes();
|
||||
|
||||
$apiUrl = url('/api/advertisements');
|
||||
|
||||
// $route = 'advertisements.create';
|
||||
// info("AdvertisementController@edit diakses dengan ID: $title");
|
||||
return view('data.advertisements.form', compact('title', 'subtitle', 'fields', 'fieldTypes', 'apiUrl', 'dropdownOptions'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*/
|
||||
public function edit($id)
|
||||
{
|
||||
info("AdvertisementController@edit diakses dengan ID: $id");
|
||||
$title = 'Advertisement';
|
||||
$subtitle = 'Update Data';
|
||||
$modelInstance = Advertisement::find($id);
|
||||
// Pastikan model ditemukan
|
||||
if (!$modelInstance) {
|
||||
info("AdvertisementController@edit: Model tidak ditemukan.");
|
||||
return redirect()->route('advertisements.index')->with('error', 'Advertisement not found');
|
||||
}
|
||||
|
||||
// Mengambil dan memetakan village_name dan district_name
|
||||
$village = DB::table('villages')->where('village_code', $modelInstance->village_code)->first();
|
||||
$modelInstance->village_name = $village ? $village->village_name : null;
|
||||
|
||||
$district = DB::table('districts')->where('district_code', $modelInstance->district_code)->first();
|
||||
$modelInstance->district_name = $district ? $district->district_name : null;
|
||||
|
||||
// Mengambil data untuk dropdown
|
||||
$dropdownOptions = [
|
||||
'village_name' => DB::table('villages')->orderBy('village_name')->pluck('village_name', 'village_code'),
|
||||
'district_name' => DB::table('districts')->orderBy('district_name')->pluck('district_name', 'district_code'),
|
||||
];
|
||||
|
||||
info("AdvertisementController@edit diakses dengan Model Instance: $modelInstance");
|
||||
$fields = $this->getFields();
|
||||
$fieldTypes = $this->getFieldTypes();
|
||||
|
||||
$apiUrl = url('/api/advertisements');
|
||||
|
||||
// $route = 'advertisements.update'; // Menggunakan route update untuk form edit
|
||||
// info("AdvertisementController@edit diakses dengan route: $route");
|
||||
return view('data.advertisements.form', compact('title', 'subtitle', 'modelInstance', 'fields', 'fieldTypes', 'apiUrl', 'dropdownOptions'));
|
||||
}
|
||||
|
||||
private function getFields()
|
||||
{
|
||||
return [
|
||||
"no" => "No",
|
||||
"business_name" => "Nama Wajib Pajak",
|
||||
"npwpd" => "NPWPD",
|
||||
"advertisement_type" => "Jenis Reklame",
|
||||
"advertisement_content" => "Isi Reklame",
|
||||
"business_address" => "Alamat Wajib Pajak",
|
||||
"advertisement_location" => "Lokasi Reklame",
|
||||
"district_name" => "Kecamatan",
|
||||
"village_name" => "Desa",
|
||||
"length" => "Panjang",
|
||||
"width" => "Lebar",
|
||||
"viewing_angle" => "Sudut Pandang",
|
||||
"face" => "Muka",
|
||||
"area" => "Luas",
|
||||
"angle" => "Sudut",
|
||||
"contact" => "Kontak",
|
||||
];
|
||||
}
|
||||
|
||||
private function getFieldTypes()
|
||||
{
|
||||
return [
|
||||
"no" => "text",
|
||||
"business_name" => "text",
|
||||
"npwpd" => "text",
|
||||
"advertisement_type" => "text",
|
||||
"advertisement_content" => "textarea",
|
||||
"business_address" => "text",
|
||||
"advertisement_location" => "text",
|
||||
"village_name" => "combobox",
|
||||
"district_name" => "combobox",
|
||||
"length" => "text",
|
||||
"width" => "text",
|
||||
"viewing_angle" => "text",
|
||||
"face" => "text",
|
||||
"area" => "text",
|
||||
"angle" => "text",
|
||||
"contact" => "text",
|
||||
];
|
||||
}
|
||||
}
|
||||
43
app/Http/Requests/AdvertisementRequest.php
Normal file
43
app/Http/Requests/AdvertisementRequest.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class AdvertisementRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'no' => 'required',
|
||||
'business_name' => 'required|string',
|
||||
'npwpd' => 'required|string',
|
||||
'advertisement_type' => 'required|string',
|
||||
'advertisement_content' => 'required|string',
|
||||
'business_address' => 'required|string',
|
||||
'advertisement_location' => 'required|string',
|
||||
'village_name' => 'required',
|
||||
'district_name' => 'required',
|
||||
'length' => 'required',
|
||||
'width' => 'required',
|
||||
'viewing_angle' => 'required|string',
|
||||
'face' => 'required|string',
|
||||
'area' => 'required|string',
|
||||
'angle' => 'required|string',
|
||||
'contact' => 'required|string',
|
||||
];
|
||||
}
|
||||
}
|
||||
19
app/Http/Resources/AdvertisementResource.php
Normal file
19
app/Http/Resources/AdvertisementResource.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class AdvertisementResource extends JsonResource
|
||||
{
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
return parent::toArray($request);
|
||||
}
|
||||
}
|
||||
92
app/Imports/AdvertisementImport.php
Normal file
92
app/Imports/AdvertisementImport.php
Normal file
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
|
||||
namespace App\Imports;
|
||||
|
||||
use App\Models\Advertisement;
|
||||
use Illuminate\Support\Collection;
|
||||
use Maatwebsite\Excel\Concerns\ToCollection;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class AdvertisementImport implements ToCollection
|
||||
{
|
||||
/**
|
||||
* Process each row in the file.
|
||||
*/
|
||||
public function collection(Collection $rows)
|
||||
{
|
||||
if ($rows->isEmpty())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Ambil data districts dengan normalisasi nama
|
||||
$districts = DB::table('districts')
|
||||
->get()
|
||||
->mapWithKeys(function ($item) {
|
||||
return [strtolower(trim($item->district_name)) => $item->district_code];
|
||||
})
|
||||
->toArray();
|
||||
|
||||
// Cari header secara otomatis
|
||||
$header = $rows->first();
|
||||
$headerIndex = collect($header)->search(fn($value) => !empty($value));
|
||||
|
||||
// Pastikan header ditemukan
|
||||
if ($headerIndex === false) {
|
||||
return;
|
||||
}
|
||||
|
||||
$dataToInsert = [];
|
||||
|
||||
foreach ($rows->skip(1) as $row) {
|
||||
// Normalisasi nama kecamatan dan desa
|
||||
$districtName = strtolower(trim(str_replace('Kecamatan ', '', $row[8])));
|
||||
$villageName = strtolower(trim($row[7]));
|
||||
|
||||
// Cari district_code dari tabel districts
|
||||
$districtCode = $districts[$districtName] ?? null;
|
||||
|
||||
$listTrueVillage = DB::table('villages')
|
||||
->where('district_code', $districtCode) // Perbaikan pada where()
|
||||
->get()
|
||||
->mapWithKeys(function ($item) {
|
||||
return [strtolower(trim($item->village_name)) => [
|
||||
'village_code' => $item->village_code,
|
||||
'district_code' => $item->district_code
|
||||
]];
|
||||
})
|
||||
->toArray();
|
||||
|
||||
// ambil village code yang village_name sama dengan $villageName
|
||||
$villageCode = $listTrueVillage[$villageName]['village_code'] ?? '0000';
|
||||
|
||||
$dataToInsert[] = [
|
||||
'no' => $row[0],
|
||||
'business_name' => $row[1],
|
||||
'npwpd' => $row[2],
|
||||
'advertisement_type' => $row[3],
|
||||
'advertisement_content' => $row[4],
|
||||
'business_address' => $row[5],
|
||||
'advertisement_location' => $row[6],
|
||||
'village_code' => $villageCode,
|
||||
'district_code' => $districtCode,
|
||||
'length' => $row[9],
|
||||
'width' => $row[10],
|
||||
'viewing_angle' => $row[11],
|
||||
'face' => $row[12],
|
||||
'area' => $row[13],
|
||||
'angle' => $row[14],
|
||||
'contact' => $row[15] ?? "-",
|
||||
'created_at' => now(),
|
||||
'updated_at' => now()
|
||||
];
|
||||
}
|
||||
// Bulk insert untuk efisiensi
|
||||
if (!empty($dataToInsert)) {
|
||||
Advertisement::insert($dataToInsert);
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
46
app/Models/Advertisement.php
Normal file
46
app/Models/Advertisement.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
/**
|
||||
* Class Advertisement
|
||||
*
|
||||
* @property $id
|
||||
* @property $created_at
|
||||
* @property $updated_at
|
||||
* @property $no
|
||||
* @property $business_name
|
||||
* @property $npwpd
|
||||
* @property $advertisement_type
|
||||
* @property $advertisement_content
|
||||
* @property $business_address
|
||||
* @property $advertisement_location
|
||||
* @property $village_code
|
||||
* @property $district_code
|
||||
* @property $length
|
||||
* @property $width
|
||||
* @property $viewing_angle
|
||||
* @property $face
|
||||
* @property $area
|
||||
* @property $angle
|
||||
* @property $contact
|
||||
*
|
||||
* @package App
|
||||
* @mixin \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
class Advertisement extends Model
|
||||
{
|
||||
|
||||
protected $perPage = 20;
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array<int, string>
|
||||
*/
|
||||
protected $fillable = ['no', 'business_name', 'npwpd', 'advertisement_type', 'advertisement_content', 'business_address', 'advertisement_location', 'village_code', 'district_code', 'length', 'width', 'viewing_angle', 'face', 'area', 'angle', 'contact'];
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user