create page tax with data, upload and export group by subdistrict

This commit is contained in:
arifal hidayat
2025-08-05 01:30:37 +07:00
parent 456eec83dc
commit 7135876ebc
18 changed files with 780 additions and 3 deletions

View File

@@ -0,0 +1,59 @@
<?php
namespace App\Exports;
use App\Models\Tax;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithTitle;
use Maatwebsite\Excel\Concerns\WithHeadings;
class TaxSubdistrictSheetExport implements FromCollection, WithTitle, WithHeadings
{
protected $subdistrict;
public function __construct(string $subdistrict)
{
$this->subdistrict = $subdistrict;
}
public function collection()
{
return Tax::where('subdistrict', $this->subdistrict)
->select(
'tax_code',
'tax_no',
'npwpd',
'wp_name',
'business_name',
'address',
'start_validity',
'end_validity',
'tax_value',
'subdistrict',
'village'
)->get();
}
public function headings(): array
{
return [
'Kode',
'No',
'NPWPD',
'Nama WP',
'Nama Usaha',
'Alamat Usaha',
'Tanggal Mulai Berlaku',
'Tanggal Berakhir Berlaku',
'Nilai Pajak',
'Kecamatan',
'Desa'
];
}
public function title(): string
{
return mb_substr($this->subdistrict, 0, 31);
}
}

View File

@@ -0,0 +1,23 @@
<?php
namespace App\Exports;
use App\Models\Tax;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
class TaxationsExport implements WithMultipleSheets
{
public function sheets(): array
{
$sheets = [];
// Ambil semua subdistrict unik
$subdistricts = Tax::select('subdistrict')->distinct()->pluck('subdistrict');
foreach ($subdistricts as $subdistrict) {
$sheets[] = new TaxSubdistrictSheetExport($subdistrict);
}
return $sheets;
}
}

View File

@@ -9,6 +9,7 @@ use App\Http\Resources\CustomersResource;
use App\Imports\CustomersImport;
use App\Models\Customer;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Maatwebsite\Excel\Facades\Excel;
class CustomersController extends Controller
@@ -120,7 +121,7 @@ class CustomersController extends Controller
'message' => 'File uploaded successfully',
]);
}catch(\Exception $e){
\Log::info($e->getMessage());
Log::info($e->getMessage());
return response()->json([
'error' => 'Failed to upload file',
'message' => $e->getMessage()

View File

@@ -0,0 +1,63 @@
<?php
namespace App\Http\Controllers\Api;
use App\Exports\TaxationsExport;
use App\Http\Controllers\Controller;
use App\Http\Requests\ExcelUploadRequest;
use App\Http\Resources\TaxationsResource;
use App\Imports\TaxationsImport;
use Illuminate\Http\Request;
use App\Models\Tax;
use Illuminate\Support\Facades\Log;
use Maatwebsite\Excel\Facades\Excel;
class TaxationsController extends Controller
{
public function index(Request $request)
{
try{
$query = Tax::query()->orderBy('id', 'desc');
if($request->has('search') && !empty($request->get('search'))){
$query->where('tax_no', 'like', '%'. $request->get('search') . '%')
->orWhere('wp_name', 'like', '%'. $request->get('search') . '%')
->orWhere('business_name', 'like', '%'. $request->get('search') . '%');
}
return TaxationsResource::collection($query->paginate(config('app.paginate_per_page', 50)));
}catch(\Exception $e){
Log::info($e->getMessage());
return response()->json([
'error' => 'Failed to get data',
'message' => $e->getMessage()
], 500);
}
}
public function upload(ExcelUploadRequest $request)
{
try{
if(!$request->hasFile('file')){
return response()->json([
'error' => 'No file provided'
], 400);
}
$file = $request->file('file');
Excel::import(new TaxationsImport, $file);
return response()->json(['message' => 'File uploaded successfully'], 200);
}catch(\Exception $e){
Log::info($e->getMessage());
return response()->json([
'error' => 'Failed to upload file',
'message' => $e->getMessage()
], 500);
}
}
public function export(Request $request)
{
return Excel::download(new TaxationsExport, 'pajak_per_kecamatan.xlsx');
}
}

View File

@@ -0,0 +1,75 @@
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class TaxationController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index(Request $request)
{
$menuId = $request->query('menu_id') ?? $request->input('menu_id');
$permissions = $this->permissions[$menuId]?? []; // Avoid undefined index error
$creator = $permissions['allow_create'] ?? 0;
$updater = $permissions['allow_update'] ?? 0;
$destroyer = $permissions['allow_destroy'] ?? 0;
return view('taxation.index', compact('creator', 'updater', 'destroyer', 'menuId'));
}
public function upload(Request $request)
{
$menuId = $request->query('menu_id') ?? $request->input('menu_id');
return view('taxation.upload', compact('menuId'));
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*/
public function show(string $id)
{
//
}
/**
* Show the form for editing the specified resource.
*/
public function edit(string $id)
{
//
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, string $id)
{
//
}
/**
* Remove the specified resource from storage.
*/
public function destroy(string $id)
{
//
}
}

View File

@@ -0,0 +1,19 @@
<?php
namespace App\Http\Resources;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
class TaxationsResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
public function toArray(Request $request): array
{
return parent::toArray($request);
}
}

View File

@@ -0,0 +1,78 @@
<?php
namespace App\Imports;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\WithBatchInserts;
use Maatwebsite\Excel\Concerns\WithChunkReading;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\Models\Tax;
class TaxationsImport implements ToCollection, WithMultipleSheets, WithChunkReading, WithBatchInserts, ShouldQueue, WithHeadingRow
{
/**
* @param Collection $collection
*/
public function collection(Collection $collection)
{
$batchData = [];
$batchSize = 1000;
foreach ($collection as $row) {
$masaPajak = trim($row['masa_pajak']) ?? '';
$masaParts = explode('-', $masaPajak);
$startValidity = null;
$endValidity = null;
if (count($masaParts) === 2) {
$startValidity = \Carbon\Carbon::createFromFormat('d/m/Y', trim($masaParts[0]))->format('Y-m-d');
$endValidity = \Carbon\Carbon::createFromFormat('d/m/Y', trim($masaParts[1]))->format('Y-m-d');
}
$batchData[] = [
'tax_code' => trim($row['kode']) ?? '',
'tax_no' => trim($row['no']) ?? '',
'npwpd' => trim($row['npwpd']) ?? '',
'wp_name' => trim($row['nama_wp']) ?? '',
'business_name' => trim($row['nama_usaha']) ?? '',
'address' => trim($row['alamat_usaha']) ?? '',
'start_validity' => $startValidity,
'end_validity' => $endValidity,
'tax_value' => (float) str_replace(',', '', trim($row['nilai_pajak']) ?? '0'),
'subdistrict' => trim($row['kecamatan']) ?? '',
'village' => trim($row['desa']) ?? '',
];
if (count($batchData) >= $batchSize) {
Tax::upsert($batchData, ['tax_no'], ['tax_code', 'tax_no', 'npwpd', 'wp_name', 'business_name', 'address', 'start_validity', 'end_validity', 'tax_value', 'subdistrict', 'village']);
$batchData = [];
}
}
if (!empty($batchData)) {
Tax::upsert($batchData, ['tax_no'], ['tax_code', 'tax_no', 'npwpd', 'wp_name', 'business_name', 'address', 'start_validity', 'end_validity', 'tax_value', 'subdistrict', 'village']);
}
}
public function sheets(): array {
return [
0 => $this
];
}
public function chunkSize(): int
{
return 1000;
}
public function batchSize(): int
{
return 1000;
}
}

23
app/Models/Tax.php Normal file
View File

@@ -0,0 +1,23 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Tax extends Model
{
protected $table = 'taxs';
protected $fillable = [
'tax_code',
'tax_no',
'npwpd',
'wp_name',
'business_name',
'address',
'start_validity',
'end_validity',
'tax_value',
'subdistrict',
'village',
];
}