Files
sidata/app/Http/Controllers/JadwalSalurdanaController.php
T
2022-09-15 16:14:22 +07:00

219 lines
8.0 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\Sps_pendapatan;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
class JadwalSalurdanaController extends Controller
{
public function __construct(){
parent::__construct();
$this->setTitle("Jadwal Salur Dana");
$this->setIconTitle("fa fa-calendar-alt");
}
public function index()
{
return view('jadwal_salurdana.index');
}
public function datatable(Request $request)
{
$post = $request->all();
$offset = $post["start"];
$limit = $post["length"];
$src = isset($post["src"]) ? $post["src"]:"";
$ta = $post["ta"];
$dataFilter = 0;
$typeDana = isset($post["type_dana"])?$post["type_dana"]:"";
$idSPS = isset($post["id_sps"])?$post["id_sps"]:"";
$sqlstr = Sps_pendapatan::queryJadwalSalurDana($ta);
if (!empty($idSPS)){
$sqlstr.=" AND x.ID=$idSPS";
}
$dataTotal = count(DB::select(DB::raw($sqlstr)));
if (!empty($typeDana)){
if ($typeDana == "cht"){
$sqlstr.=" AND x.kode_rekening='4.2.01.01.01.0004'";
}else{
$sqlstr.=" AND SUBSTR(x.kode_rekening,1,12) IN ('4.2.01.01.03','4.2.01.01.04')";
}
}
$data = DB::select(DB::raw($sqlstr.'ORDER BY x.kode_rekening OFFSET '.$offset.' ROWS FETCH NEXT '.$limit.' ROWS ONLY'));
$dataFilter = count(DB::select(DB::raw($sqlstr)));
return response()->json([
"recordsTotal" => $dataTotal,
"recordsFiltered" => $dataFilter,
"data"=> $data
]);
}
public function index_cht_dak(Request $request, $typeDana)
{
$judul = strtoupper($typeDana);
$this->setTitle("Jadwal Salur Dana $judul");
$filter = $typeDana == "cht" ? " AND x.jenis_dana = 'DBH' AND x.sub_jenis_dana = 2 ": " AND x.jenis_dana = 'DAK'";
if (getRole()=="skpd"){
$filter.=" AND x.kode_skpd='".Auth::user()->nrk."'";
}
$idSPS = 0;
if ($request->has("id_sps"))
{
if ($request->id_sps > 0)
{
$filter.=" AND x.id_sps=".$request->id_sps;
$idSPS = $request->id_sps;
}
}
$dataAkbRealisasi = DB::select(DB::raw("SELECT x.kode_skpd, x.nama_skpd, x.kode_rsk, x.nama_rsk, x.jumlah_anggaran, y.*
FROM SAMASA.BELANJA_REALISASI x
LEFT JOIN SAMASA.VW_AKB_REALISASI y
ON x.KODE_RSK = y.IDRSK AND x.TAHUN_ANGGARAN = y.TAHUN_ANGGARAN
WHERE x.TAHUN_ANGGARAN = ".getTa()." $filter ORDER BY x.kode_skpd,x.nama_skpd,x.kode_rsk "));
$dataAkbRealisasi = array_map(function ($value) {
return (array)$value;
}, $dataAkbRealisasi);
return view("jadwal_salurdana.index_".$typeDana, compact("dataAkbRealisasi","idSPS"));
}
public function datatable_cht_dak(Request $request, $typeDana)
{
$post = $request->all();
$offset = $post["start"];
$limit = $post["length"];
// $src = $post["src"];
$ta = $post["ta"];
$typeDanaFilter = $typeDana == "cht" ? " AND x.jenis_dana = 'DBH' AND x.sub_jenis_dana = 'CHT' ": " AND x.jenis_dana = 'DAK'";
$dataFilter=0;
$sqlstr = 'SELECT x.kode_skpd, x.nama_skpd, x.kode_rsk, x.nama_rsk, x.jumlah_anggaran, y.*
FROM SAMASA.BELANJA_REALISASI x
LEFT JOIN SAMASA.VW_AKB_REALISASI y
ON x.KODE_RSK = y.IDRSK AND x.TAHUN_ANGGARAN = y.TAHUN_ANGGARAN
WHERE x.TAHUN_ANGGARAN = \''.$ta.'\''.$typeDanaFilter;
if (getRole()=="skpd"){
$sqlstr.=" AND KODE_SKPD='".Auth::user()->nrk."'";
}
$data = DB::select(DB::raw($sqlstr.' ORDER BY x.kode_skpd,x.kode_rsk OFFSET '.$offset.' ROWS FETCH NEXT '.$limit.' ROWS ONLY'));
$dataTotal = count(DB::select(DB::raw($sqlstr)));
// if (!empty($src)){
//$data->whereRaw("(LOWER(NAMA_REKENING) LIKE '%".strtolower($src)."%' OR LOWER(KODE_REKENING) LIKE '%".strtolower($src)."%')");
// }
$dataFilter = count(DB::select(DB::raw($sqlstr)));
return response()->json([
"recordsTotal" => $dataTotal,
"recordsFiltered" => $dataFilter,
"data"=> $data
]);
}
public function download(Request $request)
{
$post = $request->all();
$typeDana = isset($post["type_dana"])?$post["type_dana"]:"";
$idSPS = isset($post["id_sps"])?$post["id_sps"]:"";
$ta = $post["ta"];
$sqlstr = Sps_pendapatan::queryJadwalSalurDana($ta);
if (!empty($idSPS)){
$sqlstr.=" AND x.ID=$idSPS";
}
if (!empty($typeDana)){
if ($typeDana == "cht"){
$sqlstr.=" AND kode_rekening='4.2.01.01.01.0004'";
}else{
$sqlstr.=" AND SUBSTR(kode_rekening,1,12) IN ('4.2.01.01.03','4.2.01.01.04')";
}
}
$sqlstr .= " ORDER BY kode_rekening";
$data = DB::select(DB::raw($sqlstr));
$data = json_decode(json_encode($data),true);
$fileName = 'Jadwal-salur-dana.xlsx';
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'Kode Rekening');
$sheet->setCellValue('B1', 'Nama Rekening');
$alpha = range("A","Z");
$colIndex = 2;
// Nama Kolom bulan
$bulans = GetBulan();
foreach($bulans as $bulan){
$sheet->setCellValue($alpha[$colIndex].'1', $bulan);
$colIndex++;
$sheet->mergeCells($alpha[$colIndex].'1'.':'.$alpha[$colIndex].'1');
$colIndex++;
}
$sheet->setCellValue(getAlphabetCell($colIndex).'1', "Total");
$sheet->mergeCells(getAlphabetCell($colIndex).'1'.':'.getAlphabetCell($colIndex+1).'1');
$sheet->mergeCells('A1:A2');
$sheet->mergeCells('B1:B2');
$colIndex++;
// Nama Kolom Jadwal dan Realisasi
$colName = "Realisasi";
for($i=2;$i<=$colIndex;$i++){
$colName = $colName == "Jadwal" ? "Realisasi":"Jadwal";
$sheet->setCellValue(getAlphabetCell($i).'2', $colName);
}
// Data Kolom Bulan dan Realisasi
$rows = 3;
foreach ($data as $val){
$sheet->setCellValue('A' . $rows, $val["kode_rekening"]);
$sheet->setCellValue('B' . $rows, $val["nama_rekening"]);
$totalJadwalSalur = 0;
$totalRealisasi = 0;
$currIndex = 2;
for($i=1;$i<=12;$i++){
$sheet->setCellValue(getAlphabetCell($currIndex).$rows, $val["spsbulan".$i]);
$totalJadwalSalur += $val["spsbulan".$i];
$currIndex++;
$sheet->setCellValue(getAlphabetCell($currIndex).$rows, $val["real".$i]);
$totalRealisasi += $val["real".$i];
$currIndex++;
}
$sheet->setCellValue(getAlphabetCell($currIndex).$rows, $totalJadwalSalur);
$sheet->setCellValue(getAlphabetCell($currIndex+1).$rows, $totalRealisasi);
$rows++;
}
// TOTAL BAWAH
$sheet->setCellValue("A".$rows , "TOTAL");
$sheet->mergeCells('A'.$rows.':'.'B'.$rows);
for ($x=2;$x<=$colIndex;$x++){
$strAlpha = getAlphabetCell($x);
$SUMRANGE = $strAlpha."3:".$strAlpha.($rows-1);
$sheet->setCellValue($strAlpha.$rows , "=SUM($SUMRANGE)");
}
$writer = new Xlsx($spreadsheet);
$writer->save("downloadFiles/".$fileName);
$headers = [
'Content-Type' => 'application/vnd.ms-excel',
'Content-Disposition' => 'attachment;filename='.$fileName
];
return response()->download("downloadFiles/".$fileName, $fileName, $headers);
}
}