From fa65af44b513129616461ac0f54707262cbf61e9 Mon Sep 17 00:00:00 2001 From: Aji Kamaludin Date: Thu, 6 Jun 2019 14:48:56 +0700 Subject: [PATCH] add module tabungan --- app/Exports/SiswaExport.php | 1 - app/Exports/TabunganExport.php | 25 ++ app/Http/Controllers/SiswaController.php | 21 ++ app/Http/Controllers/TabunganController.php | 89 +++++++ app/Models/Tabungan.php | 2 +- resources/views/shared/navbar.blade.php | 4 +- resources/views/tabungan/export.blade.php | 22 ++ resources/views/tabungan/index.blade.php | 263 ++++++++++++++++++++ routes/api.php | 3 + routes/web.php | 6 + 10 files changed, 432 insertions(+), 4 deletions(-) create mode 100644 app/Exports/TabunganExport.php create mode 100644 app/Http/Controllers/TabunganController.php create mode 100644 resources/views/tabungan/export.blade.php create mode 100644 resources/views/tabungan/index.blade.php diff --git a/app/Exports/SiswaExport.php b/app/Exports/SiswaExport.php index 7963e1f..ccef167 100644 --- a/app/Exports/SiswaExport.php +++ b/app/Exports/SiswaExport.php @@ -3,7 +3,6 @@ namespace App\Exports; use App\Models\Siswa; -use Maatwebsite\Excel\Concerns\FromCollection; use Illuminate\Contracts\View\View; use Maatwebsite\Excel\Concerns\FromView; diff --git a/app/Exports/TabunganExport.php b/app/Exports/TabunganExport.php new file mode 100644 index 0000000..70afc84 --- /dev/null +++ b/app/Exports/TabunganExport.php @@ -0,0 +1,25 @@ + $this->collection() + ]); + } +} diff --git a/app/Http/Controllers/SiswaController.php b/app/Http/Controllers/SiswaController.php index 80df48f..9e798b1 100644 --- a/app/Http/Controllers/SiswaController.php +++ b/app/Http/Controllers/SiswaController.php @@ -9,6 +9,7 @@ use Illuminate\Support\Facades\Input; use Maatwebsite\Excel\Facades\Excel; use App\Exports\SiswaExport; use App\Imports\SiswaImport; +use App\Models\Tabungan; class SiswaController extends Controller { @@ -204,4 +205,24 @@ class SiswaController extends Controller { return Excel::download(new SiswaExport, 'siswa-'.now().'.xlsx'); } + + //api saldo + public function getSaldo(Siswa $siswa) + { + if($siswa == null){ + return response()->json(['msg' => 'siswa tidak ditemukan'], 404); + } + if($siswa->tabungan->count() == 0){ + return response()->json(['saldo' => '0', 'sal' => '0']); + } + + $input = Tabungan::where('tipe','in')->where('siswa_id',$siswa->id)->sum('jumlah'); + $output = Tabungan::where('tipe','out')->where('siswa_id',$siswa->id)->sum('jumlah'); + $verify = Tabungan::where('siswa_id', $siswa->id)->orderBy('created_at','desc')->first()->saldo; + if(($input - $output) == $verify){ + return response()->json(['saldo' => $input - $output, 'sal' => format_idr($input - $output)]); + }else{ + return response()->json(['saldo' => '0', 'sal' => 'invalid '.format_idr($input - $output)]); + } + } } diff --git a/app/Http/Controllers/TabunganController.php b/app/Http/Controllers/TabunganController.php new file mode 100644 index 0000000..e80ae57 --- /dev/null +++ b/app/Http/Controllers/TabunganController.php @@ -0,0 +1,89 @@ +get(); + $tabungan = Tabungan::orderBy('created_at','desc')->paginate(10); + return view('tabungan.index', [ + 'siswa' => $siswa, + 'tabungan' => $tabungan, + ]); + } + + //api manabung + public function menabung(Request $request, Siswa $siswa) + { + DB::beginTransaction(); + $tabungan = Tabungan::where('siswa_id', $siswa->id)->orderBy('created_at','desc')->first(); + if($tabungan != null){ + $menabung = Tabungan::make($request->input()); + if($request->tipe == 'in'){ + $menabung->saldo = $request->jumlah + $tabungan->saldo; + }else if($request->tipe == 'out'){ + $menabung->saldo = $tabungan->saldo - $request->jumlah; + } + if($menabung->saldo >=0 ){ + $menabung->save(); + $pesan = 'Berhasil melakukan transaksi'; + }else{ + $pesan = 'Transaksi gagal'; + } + }else{ + $menabung = Tabungan::make($request->input()); + $menabung->saldo = $request->jumlah; + $menabung->save(); + $pesan = 'Berhasil melakukan transaksi'; + } + + //tambahkan tabungan ke transaksi + $keuangan = Keuangan::orderBy('created_at','desc')->first(); + if($keuangan != null){ + if($menabung->tipe == 'in'){ + $jumlah = $keuangan->total_kas + $menabung->jumlah; + }else if($request->tipe == 'out'){ + $jumlah = $keuangan->total_kas - $menabung->jumlah; + } + }else{ + $jumlah = $menabung->jumlah; + } + $keuangan = Keuangan::create([ + 'tabungan_id' => $menabung->id, + 'tipe' => $menabung->tipe, + 'jumlah' => $menabung->jumlah, + 'total_kas' => $jumlah, + 'keterangan' => 'Transaksi tabungan oleh '. $menabung->siswa->nama."(".$menabung->siswa->kelas->nama.")". + ( ($request->tipe == 'in') ? ' menabung' : ' melakukan penarikan tabungan').' sebesar '. $menabung->jumlah + .' pada '.$menabung->created_at.' dengan total tabungan '.$menabung->saldo. + ( (isset($menabung->keperluan)) ? ' dengan catatan: '.$menabung->keperluan : ''), + ]); + + if($keuangan){ + DB::commit(); + return response()->json(['msg' => $pesan]); + }else{ + DB::rollBack(); + return redirect()->route('tabungan.index')->with([ + 'type' => 'danger', + 'msg' => 'terjadi kesalahan' + ]); + } + + } + + public function export() + { + return Excel::download(new TabunganExport, 'mutasi_tabungan-'.now().'.xlsx'); + } +} diff --git a/app/Models/Tabungan.php b/app/Models/Tabungan.php index 6e731c5..5d20585 100644 --- a/app/Models/Tabungan.php +++ b/app/Models/Tabungan.php @@ -20,7 +20,7 @@ class Tabungan extends Model ]; public function siswa(){ - return $this->belongsTo('App\Models\Siswa','id','siswa_id'); + return $this->belongsTo('App\Models\Siswa','siswa_id','id'); } public function keuangan(){ diff --git a/resources/views/shared/navbar.blade.php b/resources/views/shared/navbar.blade.php index 2489c30..eecafa3 100644 --- a/resources/views/shared/navbar.blade.php +++ b/resources/views/shared/navbar.blade.php @@ -14,8 +14,8 @@