Files
sidata/app/Http/Controllers/RealisasiPendapatanController.php
T

324 lines
13 KiB
PHP

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
use Maatwebsite\Excel\Facades\Excel;
use App\Models\PendapatanRealisasi;
use App\Models\Sps_pendapatan;
use App\Models\VwJadwalSalur;
use App\Models\User;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Facades\File;
class RealisasiPendapatanController extends Controller
{
public function __construct(){
$this->siteTitle = "Realisasi Pendapatan";
parent::__construct();
$this->setIconTitle("fa fa-reply");
}
public function index()
{
return view('realisasi_pendapatan.index')
->with('i', (request()->input('page', 1) - 1) * 10);
}
public function create()
{
// if (!Auth::user()->can("Tambah SKPP")){
// abort(403, 'Youre not allowed to visit this page!');
// }
$this->setSubtitle("Tambah Data");
return view('realisasi_pendapatan.create');
}
protected function dataPendapatan($req= [])
{
$data["tahun"] = $req["txt_ta"];
// $data["id_sps"] = $req["txt_id_sps"];
$txt_rek_sps = explode("|",$req["txt_rek_sps"]);
$data["kode_rekening_sps"] = trim($txt_rek_sps[0]);
$data["nama_rekening_sps"] = trim($txt_rek_sps[1]);
$data["simtrad4_uraian"] = $req["txt_simtrad4_uraian"];
$data["simtrad4_kotor"] = replaceMoney($req["txt_simtrad4_kotor"]);
$data["simtrad4_potongan"] = replaceMoney($req["txt_simtrad4_potongan"]);
$data["simtrad4_bersih"] = replaceMoney($req["txt_simtrad4_bersih"]);
$data["simtrad4_tgl_sp2d_bun"] = ConvertIndoDate($req["txt_simtrad4_tgl_sp2d_bun"]);
$data["simtrad4_sp2d_bun"] = $req["txt_simtrad4_sp2d_bun"];
$data["retur"] = replaceMoney($req["txt_retur"]);
$data["bersih"] = replaceMoney($req["txt_bersih"]);
return $data;
}
public function store(Request $request)
{
try{
$date = date("Ymd");
DB::beginTransaction();
$req = $request->all();
$data = $this->dataPendapatan($req);
$data["created_by"] = Auth::user()->id;
// var_dump($data); exit;
$PendapatanRealisasi = PendapatanRealisasi::create($data);
// SEND NOTIF
$userBendahara = User::where("role_name","bendahara")->get()->toArray();
$userBendahara = array_column($userBendahara,"id");
$url = URL::to("/realisasi-pendapatan/".$PendapatanRealisasi->id);
$message = "Realisasi Pendapatan telah dibuat oleh Bidang Anggaran dengan No. SP2D BUN ".$req["txt_simtrad4_sp2d_bun"];
$this->sendNotif("anggaran", "bendahara", $userBendahara, $message, $url, "Realisasi Pendapatan", $PendapatanRealisasi->id);
DB::commit();
return redirect(URL::to("/realisasi-pendapatan"))->with('success','Realisasi Pendapatan berhasil dibuat.');
}catch(\Exception $e){
DB::rollBack();
return redirect(URL::to("/realisasi-pendapatan"))->with('error',$e->getMessage());
}
}
public function edit(PendapatanRealisasi $realisasi_pendapatan)
{
$this->setSubtitle("Ubah Data");
return view('realisasi_pendapatan.edit',compact('realisasi_pendapatan'));
}
public function update(Request $request, PendapatanRealisasi $realisasi_pendapatan)
{
try{
DB::beginTransaction();
$req = $request->all();
$data = $this->dataPendapatan($req);
$data["updated_by"] = Auth::user()->id;
$realisasi_pendapatan->update($data);
DB::commit();
return redirect(URL::to("/realisasi-pendapatan"))->with('success','Realisasi Pendapatan berhasil diubah.');
}catch(Throwable $e){
DB::rollBack();
return redirect(URL::to("/realisasi-pendapatan"))->with('error','Realisasi Pendapatan gagal diubah!');
}
}
public function rinciDBH(Request $request)
{
$idSPS = $request->idSPS;
$select = DB::raw("spsbulan1,spsbulan2,spsbulan3,spsbulan4,spsbulan5,spsbulan6,spsbulan7,spsbulan8,spsbulan9,spsbulan10,spsbulan11,spsbulan12");
$sps = VwJadwalSalur::select($select)->where("TAHUN",getTA())->where("KODE_REKENING",$idSPS)->firstOrNew();
return response()->json($sps);
}
public function datatable(Request $request)
{
$post = $request->all();
$offset = $post["start"];
$limit = $post["length"];
$sps = $post["sps"];
$ta = $post["ta"];
$src = isset($post["search"]["value"]) ? strtolower($post["search"]["value"]):"";
$dataFilter=0;
$data = PendapatanRealisasi::where("tahun",$ta)->orderBy("SIMTRAD4_TGL_SP2D_BUN","DESC");
if (!empty($sps))
{
$data->where("kode_rekening_sps",$sps);
}
if (!empty($src))
{
$src = strtolower($src);
$data->whereRaw("( LOWER(simtrad4_uraian) like '%".$src."%' OR LOWER(simtrad4_sp2d_bun) like '%".$src."%' OR LOWER(rkud_no_sts) like '%".$src."%')");
}
$dataFilter = $data->count();
$dataTotal = PendapatanRealisasi::where("tahun",$ta)->count();
return response()->json([
"recordsTotal" => $dataTotal,
"recordsFiltered" => $dataFilter,
"data"=> $data->skip($offset)->take($limit)->get()
]);
}
public function destroy(PendapatanRealisasi $realisasi_pendapatan)
{
try{
DB::beginTransaction();
// BUG FIX 20 FEB 2024
$uploadDir = config('constants.upload_path_sts').getTA();
if (File::exists($uploadDir.$realisasi_pendapatan->file_sts)){
File::delete($uploadDir.$realisasi_pendapatan->file_sts);
}
$realisasi_pendapatan->delete();
DB::commit();
return redirect(URL::to("/realisasi-pendapatan"))->with('success','Data Realisasi Pendapatan berhasil dihapus!');
}catch(Throwable $e){
DB::rollBack();
return redirect(URL::to("/realisasi-pendapatan"))->with('error','Data Realisasi Pendapatan gagal dihapus.');
}
}
public function show(PendapatanRealisasi $realisasi_pendapatan)
{
// if (!Auth::user()->can("Lihat SKPP")){
// abort(403, 'Youre not allowed to visit this page!');
// }
$this->readNotif($realisasi_pendapatan->id);
$this->setSubtitle("Detail Data");
return view('realisasi_pendapatan.show',compact("realisasi_pendapatan"));
}
public function sts(Request $request, $realisasiId)
{
// $request->validate([
// 'nama' => 'required',
// 'nip' => 'required',
// 'nrk' => 'required',
// ]);
// if (!Auth::user()->can("Tambah SKPP")){
// abort(403, 'Youre not allowed to visit this page!');
// }
$req = $request->all();
try{
$date = date("Ymd");
DB::beginTransaction();
$data = PendapatanRealisasi::firstOrNew(array('id' => $realisasiId));
if ($data["simtrad4_bersih"]<>replaceMoney($req["rkud_jumlah"])){
return redirect(URL::to("/realisasi-pendapatan/".$realisasiId))->with('error', "Jumlah STS harus sama dengan Rencana !");
}
$data["rkud_tgl"] = $req["rkud_tgl"];
$data["rkud_no_sts"] = $req["rkud_no_sts"];
$data["rkud_kode_akun"] = $req["rkud_kode_akun"];
$data["rkud_nama_akun"] = $req["rkud_nama_akun"];
$data["rkud_jumlah"] = replaceMoney($req["rkud_jumlah"]);
$file = $request->file('file_sts');
if ($file){
$fileName = $req["rkud_no_sts"].".".$file->getClientOriginalExtension();
$tujuan_upload = config('constants.upload_path_sts').getTA();
File::isDirectory($tujuan_upload) or File::makeDirectory($tujuan_upload, 0777, true, true);
$file->move($tujuan_upload, $fileName);
$data["file_sts"] = $fileName;
}
$data->save();
DB::commit();
$userAnggaran = User::where("role_name","anggaran")->get()->toArray();
$userAnggaran = array_column($userAnggaran,"id");
$url = URL::to("/realisasi-pendapatan/".$realisasiId);
$message = "Realisasi Pendapatan dengan No. SP2D BUN ".$data["simtrad4_sp2d_bun"]." telah divalidasi oleh Bidang Perben!";
$this->sendNotif("bendahara", "anggaran", $userAnggaran, $message, $url, "Realisasi Pendapatan", $data->id);
return redirect(URL::to("/realisasi-pendapatan"))->with('success','STS berhasil diinput.');
}catch(\Exception $e){
DB::rollBack();
return redirect(URL::to("/realisasi-pendapatan/".$realisasiId))->with('error',$e->getMessage());
}
}
public function download(Request $request)
{
$post = $request->all();
$sps = isset($post["sps"])?$post["sps"]:"";
$ta = $post["ta"];
if ($post["export-type"]=="excel")
{
$data = PendapatanRealisasi::where("tahun", $ta)->orderBy("created_at","ASC");
if (!empty($sps)){
$data->where("kode_rekening_sps",$sps);
}
$fileName = 'Realisasi-pendapatan.xlsx';
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue(getAlphabetCell(0).'1', 'Uraian');
$sheet->setCellValue(getAlphabetCell(1).'1', 'Kotor');
$sheet->setCellValue(getAlphabetCell(2).'1', 'Potongan');
$sheet->setCellValue(getAlphabetCell(3).'1', 'Bersih');
$sheet->setCellValue(getAlphabetCell(4).'1', 'Retur');
$sheet->setCellValue(getAlphabetCell(5).'1', 'Jumlah Bersih');
$sheet->setCellValue(getAlphabetCell(6).'1', 'SP2D BUN');
$sheet->setCellValue(getAlphabetCell(7).'1', 'TGL SP2D BUN');
$sheet->setCellValue(getAlphabetCell(8).'1', 'TGL STS');
$sheet->setCellValue(getAlphabetCell(9).'1', 'NO STS');
$sheet->setCellValue(getAlphabetCell(10).'1', 'Kode Akun');
$sheet->setCellValue(getAlphabetCell(11).'1', 'Nama Akun');
$sheet->setCellValue(getAlphabetCell(12).'1', 'Jumlah');
$rows = 2;
foreach ($data->get() as $val){
$sheet->setCellValue(getAlphabetCell(0) . $rows, $val["simtrad4_uraian"]);
$sheet->setCellValue(getAlphabetCell(1) . $rows, $val["simtrad4_kotor"]);
$sheet->setCellValue(getAlphabetCell(2) . $rows, $val["simtrad4_potongan"]);
$sheet->setCellValue(getAlphabetCell(3) . $rows, $val["simtrad4_bersih"]);
$sheet->setCellValue(getAlphabetCell(4) . $rows, $val["retur"]);
$sheet->setCellValue(getAlphabetCell(5) . $rows, $val["bersih"]);
$sheet->setCellValue(getAlphabetCell(6) . $rows, $val["simtrad4_sp2d_bun"]);
$sheet->setCellValue(getAlphabetCell(7) . $rows, $val["simtrad4_tgl_sp2d_bun"]);
$sheet->setCellValue(getAlphabetCell(8) . $rows, $val["rkud_tgl"]);
$sheet->setCellValue(getAlphabetCell(9) . $rows, $val["rkud_no_sts"]);
$sheet->setCellValue(getAlphabetCell(10) . $rows, $val["kode_rekening_sps"]);
$sheet->setCellValue(getAlphabetCell(11) . $rows, $val["nama_rekening_sps"]);
$sheet->setCellValue(getAlphabetCell(12) . $rows, $val["rkud_jumlah"]);
$rows++;
}
$writer = new Xlsx($spreadsheet);
$writer->save("downloadFiles/".$fileName);
}else if($post["export-type"]=="all_sts"){
$zip = new \ZipArchive();
$fileName = "STS_ALL_".getTA().".zip";
if ($zip->open(public_path("downloadFiles/".$fileName), \ZipArchive::CREATE)== TRUE)
{
$files = File::files(public_path(config('constants.upload_path_sts').getTA()));
foreach ($files as $key => $value){
$relativeName = basename($value);
$zip->addFile($value, $relativeName);
}
$zip->close();
}
}
$headers = [
'Content-Type' => 'application/vnd.ms-excel',
'Content-Disposition' => 'attachment;filename='.$fileName
];
return response()->download("downloadFiles/".$fileName, $fileName, $headers);
}
public function downloadSts(PendapatanRealisasi $realisasi)
{
$headers = [
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'attachment;filename='.$realisasi->file_sts
];
return response()->download(config('constants.upload_path_sts').getTA()."/".$realisasi->file_sts, $realisasi->file_sts, $headers);
}
}