<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Poli;
use App\Ruangan;
use App\Organisms;
use App\XFiles;
use App\SIMBHPJenis;
use App\SIMBHPReport; 
use App\User;
use App\Periksa;
use App\PendaftaranOnListiner;

use Validator;
use Session;

class PoliController extends Controller
{
    public function index() {
        if (Session::get('previlage') == ''){
			return redirect('/login');
		} else {
            $data 					= 	[];
            $data['polis'] 			= 	Poli::all();
            $data['ruangans']       =   Ruangan::all();
            return view('admin.poli', $data);
        }
    }
    public function viewGudangIndex() {
        if (Session::get('previlage') == ''){
			return redirect('/login');
		} else {
            $tasks		            	= [];
            $homebase		            = url("/");
            $sekarang 					= date("Y-m-d");
            $pegawais   			    = User::select('id', 'nama', 'previlage')->orderBy('nama', 'ASC')->get();
            $cdatane   			        = SIMBHPJenis::all();
            $cjenis                     = count($cdatane);
            if ($cjenis == 0){
                $tasks['jjenis'][0]['jenis']	=   'Belum Ada Jenis Barang';
                $tasks['jjenis'][0]['satuan']	=   '';  
            } else {
                $i = 0;
                foreach($cdatane as $rdata){
                    $tasks['jjenis'][$i]['jenis']	=   $rdata->jenis;
                    $tasks['jjenis'][$i]['satuan']	=   $rdata->satuan;
                    $i++;
                }
            }
            $getdebet 		= SIMBHPReport::select(DB::raw('SUM(pemasukan) as pemasukan'))->groupBy('marking')->first();
            if (isset($getdebet->pemasukan)){
                $totpemasukan		= $getdebet->pemasukan;
            } else { $totpemasukan = 0 ;}
            $getkredit 		= SIMBHPReport::select(DB::raw('SUM(pengeluaran) as pengeluaran'))->groupBy('marking')->first();
            if (isset($getkredit->pengeluaran)){
                $totpepengeluaran		= $getkredit->pengeluaran;
            } else { $totpepengeluaran = 0 ;}
                
            $tasks['masuk']        	    = $totpemasukan;
            $tasks['keluar']        	= $totpepengeluaran;
            $tasks['pegawai']        	= $pegawais;
            $tasks['tahunne']			= date("Y");
            $tasks['tanggal']			= $sekarang;
            $tasks['sidebar']			= 'simbhp';
            $previlage                  = Session('previlage');
            return view('admin.gudang', $tasks);
        }
    }
    public function storePoli(Request $request) {
    	$validator  =   Validator::make($request->all(), [
	    	'poli'     =>     'required',
			'poli'     =>     'required',
	        'modaliti' =>     'required'
        ]);
    	if($validator->fails()) {
    		return response()->json(['status' => 'error', 'message' => 'Please fill input field or fill with right input']);
    	} else {
			$poli   	= $request->input('poli');
			$subpoli   	= $request->input('subpoli');
			$subsubpoli = $request->input('subsubpoli');
			$modaliti 	= $request->input('modaliti');
       		Poli::create([
            	'poli'      =>  $request->input('poli'),
            	'subpoli' 	=> 	$request->input('subpoli'),
                'subsubpoli'=>  $request->input('subsubpoli'),
                'modaliti'	=>  $request->input('modaliti'),
                'modaliti2'	=>  $request->input('modaliti2')
        	]);
            Periksa::where('reques', $subpoli)->update([
                'kd_spesimen'	=>  $request->input('subsubpoli'),
				'nm_spesimen'	=>  $request->input('modaliti'),
            ]);
			$tulis = 'Data '.$poli.' '.$subpoli.' '.$subsubpoli.' Saved..!!';
			return response()->json(['status' => 'success', 'message' => $tulis]);
      	}
    }
    public function getListPoli() {
        $reloading = Periksa::where('reques', 'Mikrobiologi')->get();
        foreach($reloading as $rl){
            $cekada = PendaftaranOnListiner::where('rnoreg', $rl->nofoto)->first();
            if (isset($cekada->tes)){
                Periksa::where('id', $rl->id)->update([
                    'reques'    => $cekada->tes
                ]);
            }
        }
	    $results    =   Poli::orderBy('subpoli', 'ASC')->get();
	    echo json_encode($results);
    }
    public function updatePoli(Request $request) {
    	$validator = Validator::make($request->all(), [
      		'id'            =>  'required',
            'poli'          =>  'required',
        	'subpoli'    	=>  'required',
		    'modaliti'	    =>  'required'
        ]);
    	if($validator->fails()) {
            return response()->json(['status' => 'error', 'message' => 'Please fill input field or fill with right input']);
        } else {
        	$id   		= $request->input('id');
			$poli   	= $request->input('poli');
			$subpoli   	= $request->input('subpoli');
			$subsubpoli = $request->input('subsubpoli');
        	Poli::where('id', $id)->update([
                'poli'      =>  $poli,
	      		'subpoli' 	=>  $subpoli,
	      		'subsubpoli'=>  $subsubpoli,
                'modaliti'	=>  $request->input('modaliti'),
                'modaliti2'	=>  $request->input('modaliti2')
	      	]);
            Periksa::where('reques', $subpoli)->update([
                'kd_spesimen'	=>  $subsubpoli,
				'nm_spesimen'	=>  $request->input('modaliti'),
            ]);
			$tulis = 'Data '.$poli.' '.$subpoli.' '.$subsubpoli.' Updated..!!';
			return response()->json(['status' => 'success', 'message' => $tulis]);
        }
    }
    public function deletePoli(Request $request) {
    	$id 	=	$request->id;
	   	$poli 	=	Poli::find($id);
	   	$poli->delete();
	   	return back();
    }
    public function storeRuangan(Request $request) {
        $validator  =   Validator::make($request->all(), [
            'poli'      =>  'required',
            'ruangan'   =>  'required'
        ]);
        if($validator->fails()) {
            return response()->json(['status' => 'error', 'message' => 'Please fill input field or fill with right input']);
        } else {
            Ruangan::create([
                'poli'      =>  $request->input('poli'),
                'ruangan'   =>  $request->input('ruangan')
            ]);
            Session::flash('message', 'Data berhasil disimpan'); 
            Session::flash('alert-class', 'alert-success');
            return back();
        }
    }
    public function getListRuangan(Request $request) {
        $results   =   Ruangan::all();
        echo json_encode($results);
    }
    public function updateRuangan(Request $request) {
        $validator = Validator::make($request->all(), [
            'id'        =>  'required',
            'poli'      =>  'required',
            'ruangan'   =>  'required'
        ]);
        if($validator->fails()) {
            return response()->json(['status' => 'error', 'message' => 'Please fill input field or fill with right input']);
        } else {
            $id   =   $request->input('id');
            Ruangan::where('id', $id)->update([
                'poli'      =>  $request->input('poli'),
                'ruangan'    =>  $request->input('ruangan')   
            ]);
        }
    }
    public function deleteRuangan(Request $request) {
        $id         =   $request->id;
        $ruangan    =   Ruangan::find($id);
        $ruangan->delete();
        return back();
    }
    public function jsonRekapbhp() {
		$tahun 		= date("Y");
		$thnlalu 	= $tahun - 1;
		$totale 	= 0;
		$arraysurat	= [];
        $getdata 	= SIMBHPJenis::all();
        if (!empty($getdata)){
            foreach($getdata as $hasil){
                $jenis		= $hasil->kodejenis;
                $satuan		= $hasil->satuan;
                $tlsjenis	= $hasil->jenis;;
                $getdebet 		= SIMBHPReport::select(DB::raw('SUM(pemasukan) as pemasukan'))->where('jenis', $tlsjenis)->groupBy('jenis')->first();
                if (isset($getdebet->pemasukan)){
                    $totpemasukan		= $getdebet->pemasukan;
                } else { $totpemasukan = 0 ;}
                $getkredit 		= SIMBHPReport::select(DB::raw('SUM(pengeluaran) as pengeluaran'))->where('jenis', $tlsjenis)->groupBy('jenis')->first();
                if (isset($getkredit->pengeluaran)){
                    $totpepengeluaran		= $getkredit->pengeluaran;
                } else { $totpepengeluaran = 0 ;}
                    
                $saldoakhir = $totpemasukan - $totpepengeluaran;
                $arraysurat[] = array(
                    'id' 			=> $hasil->id,	
                    'satuan' 		=> $satuan,		
                    'jenis' 		=> $jenis,
                    'tlsjenis' 		=> $tlsjenis,	
                    'saldo' 		=> number_format( $saldoakhir , 0 , '.' , ',' ),				
                );
            }
        }
        echo json_encode($arraysurat);
    }
    public function jsonReportbhp(Request $request) {
        $bulan		= $request->input('val01');
		$tahun		= $request->input('val02');
        $hasil	    = [];
        if ($tahun == 'ALL'){
            $bulan  = date("m");
            $tahun  = date("Y");
            $getdata    = SIMBHPReport::where('bulan', $bulan)->where('tahun', $tahun)->orderBy('id', 'DESC')->get();
        } else {
            if ($bulan == 'ALL'){
                $getdata    = SIMBHPReport::where('tahun', $tahun)->orderBy('id', 'DESC')->get();
            } else {
                $getdata    = SIMBHPReport::where('bulan', $bulan)->where('tahun', $tahun)->orderBy('id', 'DESC')->get();
            }
            
        }
		foreach($getdata as $rdata){
			$dd			= $rdata->tanggal;
			$mm			= $rdata->bulan;
			$yy			= $rdata->tahun;
			$pengeluaran= $rdata->pengeluaran;
			$pemasukan	= $rdata->pemasukan;
			if ($mm < 10){
				$tgllengkap = $dd.'-0'.$mm.'-'.$yy;
			} else {
				$tgllengkap = $dd.'-'.$mm.'-'.$yy;
			}
			if ($pengeluaran == '' OR $pengeluaran == 0) {$total = $pemasukan;}
			else { $total = $pengeluaran; }
			
			$hasil[] = array(
				'id' 			=> $rdata->id,	
				'tanggal' 		=> $rdata->tanggal,		
				'bulan' 		=> $rdata->bulan,
				'tahun' 		=> $rdata->tahun,
				'deskripsi' 	=> $rdata->deskripsi,
				'pemasukan' 	=> number_format( $pemasukan , 0 , '.' , ',' ),	
				'pengeluaran' 	=> number_format( $pengeluaran , 0 , '.' , ',' ),	
				'jenis' 		=> $rdata->jenis,	
				'keterangan' 	=> $rdata->keterangan,
				'tgllengkap' 	=> $tgllengkap,
				'total' 		=> $total,
			);
		}
        echo json_encode($hasil);
    }
    public function jsonReportbhpPaginated(Request $request) {
        $tanggal	= $request->input('tanggal');
		$deskripsi	= $request->input('deskripsi');
        $kategori	= $request->input('kategori');
        $lm         =  10;
        $limit      = ($request->input('limit') == null ? $lm : $request->input('limit'));
        $order      = ($request->input('order') == null ? 'id desc' : $request->input('order'));
        $data 	    = new SIMBHPReport;
        if ($kategori != null AND $kategori != '') $data = $data->where('jenis', $kategori);
        if ($tanggal != null AND $tanggal != '') $data = $data->where('created_at', 'LIKE', '%'.$tanggal.'%');
        if ($deskripsi != null AND $deskripsi != '') $data = $data->where('deskripsi', 'LIKE', '%'.$deskripsi.'%');
        $data       = $data->orderByRaw($order)->paginate($limit);
        $hasil      = [];
        $totaldata  = $data->total();
        $debet      = 0;
        $kredit     = 0;
        if (!empty($data)){
            foreach($data as $rdata){
                $dd			    = $rdata->tanggal;
                $mm			    = $rdata->bulan;
                $yy			    = $rdata->tahun;
                $pengeluaran    = $rdata->pengeluaran;
                $pemasukan	    = $rdata->pemasukan;
                $deskripsi	    = $rdata->deskripsi;
                $jenis	        = $rdata->jenis;
                $debet          = $debet + $pemasukan;
                $kredit         = $kredit + $pengeluaran;
                $cekjenis       = SIMBHPJenis::where('kodejenis', $jenis)->first();
                if (isset($cekjenis->id)){
                    $kodejenis  = $cekjenis->kodejenis;
                    $jenis      = $cekjenis->jenis;
                    $satuan      = $cekjenis->satuan;
                } else {
                    $kodejenis  = $jenis;
                    $jenis      = '';
                    $satuan     = '';
                }

                if ($jenis != ''){
                    $deskripsi  = '<strong>'.$jenis.'</strong><br />'.$deskripsi;
                }
                    
                if ($mm < 10){
                    $tgllengkap = $yy.'-0'.$mm.'-'.$dd;
                } else {
                    $tgllengkap = $yy.'-'.$mm.'-'.$dd;
                }
                if ($pengeluaran == '' OR $pengeluaran == 0) {
                    $total      = $pemasukan;
                    $jentrans   = 'PEMASUKAN';
                }
                else { 
                    $total      = $pengeluaran;
                    $jentrans   = 'PENGELUARAN'; 
                }
                
                $hasil[] = array(
                    'id' 			=> $rdata->id,	
                    'tanggal' 		=> $rdata->tanggal,		
                    'bulan' 		=> $rdata->bulan,
                    'tahun' 		=> $rdata->tahun,
                    'tlsdeskripsi' 	=> $deskripsi,
                    'deskripsi' 	=> $rdata->deskripsi,
                    'pemasukan' 	=> number_format( $pemasukan , 0 , '.' , ',' ),	
                    'pengeluaran' 	=> number_format( $pengeluaran , 0 , '.' , ',' ),	
                    'jenis' 		=> $kodejenis,	
                    'keterangan' 	=> $rdata->keterangan,
                    'tgllengkap' 	=> $tgllengkap,
                    'created_at' 	=> $rdata->created_at,
                    'nominal' 		=> $total.' '.$satuan,
                    'jentrans' 		=> $jentrans,
                );
            }
        }
        
        $response = [
            'message'   => 'List Data',
            'data'      => $hasil,
            'totaldata' => $totaldata
        ];
        return response()->json($response, 200);
    }
    public function exAddbarang(Request $request) {
		$deskripsi	= $request->input('set01');
		$pos		= $request->input('set02');
		$tanggal	= $request->input('set03');
		$jumlah		= $request->input('set04');
		$jenis		= $request->input('set05');
		$postujuan	= $request->input('set06');
		$alasan		= $request->input('set07');
		$nama		= Session('nama');
		if ($tanggal == '' OR is_null($tanggal)){
			$tanggal = date("d-m-Y");
		}
		$total 		= (int)str_replace(',','',$jumlah);
		if ($jenis == 'jenis'){ $jumlah = '-';}
		if ($deskripsi != ''  and $pos != '' and $tanggal != '' and $jumlah != '' and $jenis != ''){
            if ($jenis == 'jenis'){	
                $jenis          = $request->input('set02');
                $satuan         = $request->input('set03');
                $idne           = $request->input('set04');
                $kodejenis 		= preg_replace('/\s+/', '', $jenis);
                if ($idne == 'new' OR $idne == ''){
                    $ceksudah = SIMBHPJenis::where('kodejenis', $kodejenis)->where('satuan', $satuan)->count();
                    if ($ceksudah != 0){
                        return response()->json(['icon' => 'error', 'warna' => '#bf441d', 'status' => 'Gagal', 'message' => $jenis.' Sudah Ada, Silahkan Masukkan Jenis Barang Lain']);
                        return back();
                    } else {
                        $input = SIMBHPJenis::create([
                            'kodejenis' => $kodejenis,
                            'jenis'     => $jenis,
                            'satuan'    => $satuan,
                        ]);
                        if ($input){
                            return response()->json(['status' => 'Success', 'message' => 'Data '.$jenis.' Sukses Ditambahkan']);
					        return back();
                        } else {
                            return response()->json(['icon' => 'error', 'warna' => '#bf441d', 'status' => 'Gagal', 'message' => $jenis.' Gagal di masukkan, silahkan ulangi beberapa saat lagi']);
                            return back();
                        }
                    }
                } else {
                    $ceksudah = SIMBHPJenis::where('id', '!=', $idne)->where('kodejenis', $kodejenis)->where('satuan', $satuan)->count();
                    if ($ceksudah != 0){
                        return response()->json(['icon' => 'error', 'warna' => '#bf441d', 'status' => 'Gagal', 'message' => $jenis.' Sudah Ada, Silahkan Masukkan Jenis Barang Lain']);
                        return back();
                    } else {
                        $input = SIMBHPJenis::where('id', $idne)->update([
                            'kodejenis' => $kodejenis,
                            'jenis'     => $jenis,
                            'satuan'    => $satuan,
                        ]);
                        if ($input){
                            return response()->json(['status' => 'Success', 'message' => 'Data '.$jenis.' Sukses Diupdate']);
					        return back();
                        } else {
                            return response()->json(['icon' => 'error', 'warna' => '#bf441d', 'status' => 'Gagal', 'message' => $jenis.' Gagal di masukkan, silahkan ulangi beberapa saat lagi']);
                            return back();
                        }
                    }
                }
            } else {
                $ahrf 	= explode("-", $tanggal);	
                $tahun 	= $ahrf[0];
                if(isset($ahrf[1])){
                    $wulan 	= (int)$ahrf[1];
                } else { $wulan = date("m"); $wulan = (int)$wulan; }
                if(isset($ahrf[2])){
                    $dino 	= (int)$ahrf[2];
                } else { $dino = date("d");  }
                if ($jenis == 'pemasukan'){
                    $bayar = SIMBHPReport::create([
                        'tanggal'		=> $dino, 
                        'bulan'			=> $wulan, 
                        'tahun'			=> $tahun, 
                        'deskripsi'		=> $deskripsi, 
                        'pemasukan'		=> $total, 
                        'pengeluaran'	=> null,
                        'jenis'			=> $pos, 
                        'keterangan'	=> '', 
                        'marking'		=> '',
                    ]);
                } else if ($jenis == 'pengeluaran'){
                    $getnama        = User::where('id', $deskripsi)->first();
                    $nama           = $getnama->nama ?? 'Unkown';
                    $deskripsi      = 'Diterima oleh '.$nama;
                    $getdebet 		= SIMBHPReport::select(DB::raw('SUM(pemasukan) as pemasukan'))->where('jenis', $pos)->groupBy('jenis')->first();
                    if (isset($getdebet->pemasukan)){
                        $totpemasukan		= $getdebet->pemasukan;
                    } else { $totpemasukan = 0 ;}
                    $getkredit 		= SIMBHPReport::select(DB::raw('SUM(pengeluaran) as pengeluaran'))->where('jenis', $pos)->groupBy('jenis')->first();
                    if (isset($getkredit->pengeluaran)){
                        $totpepengeluaran		= $getkredit->pengeluaran;
                    } else { $totpepengeluaran = 0 ;}
                    $totpepengeluaran = $totpepengeluaran + $total;
                    if ($totpepengeluaran > $totpemasukan){
                        return response()->json(['icon' => 'error', 'warna' => '#bf441d', 'status' => 'Gagal', 'message' => 'Nominal Melebihi Stok']);
                        return back();
                    } else {
                        $bayar 	= SIMBHPReport::create([
                            'tanggal'		=> $dino, 
                            'bulan'			=> $wulan, 
                            'tahun'			=> $tahun, 
                            'deskripsi'		=> $deskripsi, 
                            'pemasukan'		=> null,
                            'pengeluaran'	=> $total,
                            'jenis'			=> $pos,
                            'keterangan'	=> '', 
                            'marking'		=> '',
                        ]);
                    }
                    
                } else if ($jenis == 'editor'){ 
                    if ($alasan == ''){
                        return response()->json(['icon' => 'error', 'warna' => '#bf441d', 'status' => 'Gagal', 'message' => 'Alasan Perubahan Data Wajib Di Isi!!!']);
                        return back();
                    } else {
                        $getdebet 		= SIMBHPReport::select(DB::raw('SUM(pemasukan) as pemasukan'))->where('jenis', $pos)->groupBy('jenis')->first();
                        if (isset($getdebet->pemasukan)){
                            $totpemasukan		= $getdebet->pemasukan;
                        } else { $totpemasukan = 0 ;}
                        $getkredit 		= SIMBHPReport::select(DB::raw('SUM(pengeluaran) as pengeluaran'))->where('jenis', $pos)->groupBy('jenis')->first();
                        if (isset($getkredit->pengeluaran)){
                            $totpepengeluaran		= $getkredit->pengeluaran;
                        } else { $totpepengeluaran = 0 ;}
                        $totpepengeluaran = $totpepengeluaran + $total;
                        
                        $rdatalama		= SIMBHPReport::where('id', $postujuan)->first();		
                        $ldeskripsi		= $rdatalama->deskripsi;
                        $lpemasukan		= $rdatalama->pemasukan;
                        $lpengeluaran	= $rdatalama->pengeluaran;
                        $ljenis			= $rdatalama->jenis;
                        $marking		= $rdatalama->marking;
                        if ($lpengeluaran == '' OR $lpengeluaran == 0) {
                            $ltotal = number_format( $lpemasukan , 0 , '.' , ',' );
                            if ($marking != ''){
                                SIMBHPReport::where('marking', $marking)->whereNotIn('id', [$postujuan])->update([
                                    'tanggal'		=> $dino,
                                    'bulan'			=> $wulan,
                                    'tahun'			=> $tahun,
                                    'pengeluaran'	=> $total
                                ]);
                            }
                            $bayar = SIMBHPReport::where('id', $postujuan)->update([
                                'tanggal'		=> $dino,
                                'bulan'			=> $wulan,
                                'tahun'			=> $tahun,
                                'deskripsi'		=> $deskripsi,
                                'jenis'			=> $pos,
                                'pemasukan'		=> $total,
                                'keterangan'    => $alasan,
                                'updated_at'    => date("Y-m-d H:i:s")
                            ]);
                        } else {
                            $totpepengeluaran = $totpepengeluaran + $total;
                            if ($totpepengeluaran > $totpemasukan){
                                return response()->json(['icon' => 'error', 'warna' => '#bf441d', 'status' => 'Gagal', 'message' => 'Nominal Melebihi Stok']);
                                return back();
                            } else {
                                $ltotal = number_format( $lpengeluaran , 0 , '.' , ',' );
                                if ($marking != ''){
                                    SIMBHPReport::where('marking', $marking)->whereNotIn('id', [$postujuan])->update([
                                        'tanggal'		=> $dino,
                                        'bulan'			=> $wulan,
                                        'tahun'			=> $tahun,
                                        'pemasukan'		=> $total
                                    ]);
                                }
                                $bayar = SIMBHPReport::where('id', $postujuan)->update([
                                    'tanggal'		=> $dino,
                                    'bulan'			=> $wulan,
                                    'tahun'			=> $tahun,
                                    'deskripsi'		=> $deskripsi,
                                    'jenis'			=> $pos,
                                    'pengeluaran'	=> $total,
                                    'keterangan'    => $alasan,
                                    'updated_at'    => date("Y-m-d H:i:s")
                                ]);
                            }
                        }
                        $baris1 	= '<table class="table table-bordered table-striped"><tr><td colspan=2><p align=center><b>Data Lama</b></p></td><td colspan=2><p align=center><b>Data Perubahan</b></p></td></tr>';
                        $baris2		= '<tr><td>Deskripsi</td><td>'.$ldeskripsi.'</td><td><font color=red>Diubah Menjadi</font></td><td>'.$deskripsi.'</td></tr>';
                        $baris3		= '<tr><td>Jenis</td><td>'.$ljenis.'</td><td><font color=red>Diubah Menjadi</font></td><td>'.$pos.'</td></tr>';
                        $baris4		= '<tr><td>Total</td><td>'.$ltotal.'</td><td><font color=red>Diubah Menjadi</font></td><td>'.$jumlah.'</td></tr>';
                        $baris5		= '<tr><td><b>Dengan Alasan</b></td><td colspan=3>'.$alasan.'</td</tr>';
                        $baris6		= '<tr><td><b>Eksekutor</b></td><td colspan=3>'.Session('nama').'</td</tr></table>';
                        $perubahan  = $baris1.$baris2.$baris3.$baris4.$baris5.$baris6;
                        Xfiles::create([
                            'xmarking'	=> 'SIMBHP-'.$postujuan.'-'.time(),
                            'xtabel'	=> 'History SIMBHP',
                            'xjenis'	=> '',
                            'xfile'		=> $perubahan
                        ]);
                    }
                } else { 
                    if ($alasan == ''){
                        return response()->json(['icon' => 'error', 'warna' => '#bf441d', 'status' => 'Gagal', 'message' => 'Alasan Perubahan Data Wajib Di Isi!!!']);
                        return back();
                    }
                    else {
                        $rdatalama		= SIMBHPReport::where('id', $postujuan)->first();		
                        $ldeskripsi		= $rdatalama->deskripsi;
                        $lpemasukan		= $rdatalama->pemasukan;
                        $lpengeluaran	= $rdatalama->pengeluaran;
                        $ljenis			= $rdatalama->jenis;
                        $marking		= $rdatalama->marking;
                        if ($lpengeluaran == '' or $lpengeluaran == 0) {
                            $ltotal = number_format( $lpemasukan , 0 , '.' , ',' );				
                        } else { 
                            $ltotal = number_format( $lpengeluaran , 0 , '.' , ',' );				
                        }
                        $baris1 	= '<table class="table table-bordered table-striped"><tr><td colspan=2><p align=center><b>Data Lama</b></p></td><td colspan=2><p align=center><b>Data Perubahan</b></p></td></tr>';
                        $baris2		= '<tr><td>Deskripsi</td><td>'.$ldeskripsi.'</td><td colspan=2><font color=red>DIHAPUS</font></td></tr>';
                        $baris3		= '<tr><td>Jenis</td><td>'.$ljenis.'</td><td colspan=2><font color=red>DIHAPUS</font></td></tr>';
                        $baris4		= '<tr><td>Total</td><td>'.$ltotal.'</td><td colspan=2><font color=red>DIHAPUS</font></td></tr>';
                        $baris5		= '<tr><td><b>Dengan Alasan</b></td><td colspan=3>'.$alasan.'</td</tr>';
                        $baris6		= '<tr><td><b>Eksekutor</b></td><td colspan=3>'.Session('nama').'</td</tr></table>';
                        $perubahan  = $baris1.$baris2.$baris3.$baris4.$baris5.$baris6;
                        
                        Xfiles::create([
                            'xmarking'	=> 'SIMBHP-'.$postujuan.'-'.time(),
                            'xtabel'	=> 'History SIMBHP',
                            'xjenis'	=> '',
                            'xfile'		=> $perubahan
                        ]);
                        if ($marking != ''){
                            $bayar = SIMBHPReport::where('marking', $marking)->delete();
                        } else {
                            $bayar = SIMBHPReport::where('id', $postujuan)->delete();
                        }
                    }
                }
                if ($bayar){
                    return response()->json(['status' => 'Success', 'message' => 'Transaksi '.$jenis.' Sukses Dilaksanakan']);
					return back();
                } else {
                    return response()->json(['icon' => 'error', 'warna' => '#bf441d', 'status' => 'Gagal', 'message' => 'Update Gagal, Pastikan Data Yang anda Isi Sudah Sesuai']);
                    return back();
                }
            }		
		}
		else {
            return response()->json(['icon' => 'error', 'warna' => '#bf441d', 'status' => 'Gagal', 'message' => 'Pastikan Formnya Anda Isi dengan Lengkap']);
            return back();
		}	
	}
}
