/**
  * Display a listing of the resource.
  *
  * @return Response
  */
 public function transaksi($id = null)
 {
     if ($id == null) {
         return Transaksi::orderBy('id', 'desc')->get();
     } else {
         return $this->show($id);
     }
 }
 /**
  * Store a newly created resource in storage.
  *
  * @return Response
  */
 public function store()
 {
     $v = Validator::make(Input::all(), ['nama' => 'required|min:1', 'jumlah' => 'required|min:1|numeric', 'tahun' => 'required|numeric']);
     if ($v->fails()) {
         return redirect()->back()->withErrors($v->errors());
     }
     $cek = \App\Transaksi::where('id_koperasi', Auth::user()->assigned_koperasi)->where('id_jenis', Input::get('id_jenis'))->where('bulan', Input::get('bulan'))->where('tahun', Input::get('tahun'))->where('id_anggota', Input::get('id_anggota'))->where('jenis_transaksi', 'Simpanan')->get();
     if (sizeof($cek) == 0) {
         $info_ke = \App\Transaksi::where('id_koperasi', Auth::user()->assigned_koperasi)->where('id_jenis', Input::get('id_jenis'))->where('id_anggota', Input::get('id_anggota'))->where('jenis_transaksi', 'Simpanan')->get();
         $info_ke = sizeof($info_ke);
         $data = \App\Transaksi::orderBy('id', 'desc')->first()['id'];
         $new = new \App\Transaksi();
         $new->no_transaksi = 'KSP-' . date("ymd") . ($data + 1) . "-S";
         $new->id_anggota = Input::get('id_anggota');
         $new->keterangan = Input::get('keterangan');
         $new->jenis_transaksi = 'Simpanan';
         $new->status = 'Lunas';
         $new->id_jenis = Input::get('id_jenis');
         $new->jumlah_asli = Input::get('jumlah');
         $new->jumlah_bunga = 0;
         $new->bunga = 0;
         $new->info_ke = $info_ke + 1;
         $new->bulan = Input::get('bulan');
         $new->tahun = Input::get('tahun');
         $new->jumlah_total = Input::get('jumlah');
         $new->id_koperasi = Auth::user()->assigned_koperasi;
         $new->created_by = Auth::user()->id;
         $new->save();
         $data = \App\Keuangan::orderBy('id', 'desc')->first()['id'];
         $finan = new \App\Keuangan();
         $finan->no_nota = 'KSP-' . date("ymd") . ($data + 1) . "-B";
         $finan->id_koperasi = Auth::user()->assigned_koperasi;
         $finan->id_anggota = Input::get('id_anggota');
         $finan->tabel = 'transaksis';
         $finan->jenis = 'tabungan';
         $finan->info = "Pembayaran " . \App\Simpanan::where('id_koperasi', Auth::user()->assigned_koperasi)->find(Input::get('id_jenis'))['nama'] . " " . \App\Anggota::where('id_koperasi', Auth::user()->assigned_koperasi)->find(Input::get('id_anggota'))['nama'] . " Rp. " . number_format(Input::get('jumlah'), 2, ",", ".") . " (" . date("d/m/Y H:i:s") . ") " . Auth::user()->name;
         $finan->id_transaksi = $new->id;
         $finan->masuk = Input::get('jumlah');
         $finan->keluar = 0;
         $finan->save();
         return redirect(url('transaksi/simpanan'));
     } else {
         return redirect(url('transaksi/simpanan/baru'))->withPesan('sudah_ada');
     }
 }
 /**
  * Store a newly created resource in storage.
  *
  * @return Response
  */
 public function store()
 {
     $v = Validator::make(Input::all(), ['nama' => 'required|min:1', 'jumlah' => 'required|numeric|min:1', 'jangka_waktu' => 'required|numeric|min:1']);
     if ($v->fails()) {
         return redirect()->back()->withErrors($v->errors());
     }
     $data = \App\Transaksi::orderBy('id', 'desc')->first()['id'];
     $new = new \App\Transaksi();
     $new->no_transaksi = 'KSP-' . date("ymd") . ($data + 1) . "-P";
     $new->id_koperasi = Auth::user()->assigned_koperasi;
     $new->created_by = Auth::user()->id;
     $new->id_anggota = Input::get('id_anggota');
     $new->jenis_transaksi = 'Pinjaman';
     $new->id_jenis = Input::get('id_jenis');
     $new->bunga = Input::get('persen_bunga_keseluruhan');
     $new->jumlah_bunga = Input::get('bunga_keseluruhan');
     $new->admin = Input::get('persen_biaya_admin');
     $new->biaya_admin = Input::get('biaya_admin');
     $new->biaya_materai = Input::get('biaya_materai');
     $new->asuransi = Input::get('persen_biaya_asuransi');
     $new->biaya_asuransi = Input::get('biaya_asuransi');
     $new->tabungan = Input::get('persen_tabungan');
     $new->total_tabungan = Input::get('tabungan');
     $new->tabungan_per_bulan = Input::get('tabungan');
     $new->jumlah_asli = Input::get('jumlah');
     $new->jumlah_total = Input::get('jumlah_total');
     $new->status = 'Belum Lunas';
     $new->keterangan = Input::get('keterangan');
     $new->total_peminjaman = Input::get('total_peminjaman');
     $new->info_ke = Input::get('jangka_waktu');
     $new->angsuran = Input::get('angsuran_total');
     $new->save();
     $data = \App\Keuangan::orderBy('id', 'desc')->first()['id'];
     $finan = new \App\Keuangan();
     $finan->no_nota = 'KSP-' . date("ymd") . ($data + 1) . "-KP";
     $finan->id_koperasi = Auth::user()->assigned_koperasi;
     $finan->id_anggota = Input::get('id_anggota');
     $finan->tabel = 'transaksis';
     $finan->jenis = 'pinjaman';
     $finan->info = "Pinjaman " . \App\Anggota::where('id_koperasi', Auth::user()->assigned_koperasi)->find(Input::get('id_anggota'))['nama'] . " Rp. " . number_format(Input::get('jumlah'), 2, ",", ".") . " (" . date("d/m/Y H:i:s") . ") " . Auth::user()->name;
     $finan->id_transaksi = $new->id;
     $finan->masuk = 0;
     $finan->keluar = Input::get('jumlah');
     $finan->save();
     if (Input::get('biaya_admin') > 0) {
         $data = \App\Keuangan::orderBy('id', 'desc')->first()['id'];
         $finan = new \App\Keuangan();
         $finan->no_nota = 'KSP-' . date("ymd") . ($data + 1) . "-KA";
         $finan->id_koperasi = Auth::user()->assigned_koperasi;
         $finan->id_anggota = Input::get('id_anggota');
         $finan->tabel = 'transaksis';
         $finan->jenis = 'biaya_admin';
         $finan->info = "Biaya Admin Pinjaman " . \App\Anggota::where('id_koperasi', Auth::user()->assigned_koperasi)->find(Input::get('id_anggota'))['nama'] . " Rp. " . number_format(Input::get('biaya_admin'), 2, ",", ".") . " (" . date("d/m/Y H:i:s") . ") " . Auth::user()->name;
         $finan->id_transaksi = $new->id;
         $finan->masuk = Input::get('biaya_admin');
         $finan->keluar = 0;
         $finan->save();
     }
     if (Input::get('biaya_asuransi') > 0) {
         $data = \App\Keuangan::orderBy('id', 'desc')->first()['id'];
         $finan = new \App\Keuangan();
         $finan->no_nota = 'KSP-' . date("ymd") . ($data + 1) . "-KL";
         $finan->id_koperasi = Auth::user()->assigned_koperasi;
         $finan->id_anggota = Input::get('id_anggota');
         $finan->tabel = 'transaksis';
         $finan->jenis = 'biaya_asuransi';
         $finan->info = "Biaya Asuransi Pinjaman " . \App\Anggota::where('id_koperasi', Auth::user()->assigned_koperasi)->find(Input::get('id_anggota'))['nama'] . " Rp. " . number_format(Input::get('biaya_asuransi'), 2, ",", ".") . " (" . date("d/m/Y H:i:s") . ") " . Auth::user()->name;
         $finan->id_transaksi = $new->id;
         $finan->masuk = Input::get('biaya_asuransi');
         $finan->keluar = 0;
         $finan->save();
     }
     if (Input::get('biaya_materai') > 0) {
         $data = \App\Keuangan::orderBy('id', 'desc')->first()['id'];
         $finan = new \App\Keuangan();
         $finan->no_nota = 'KSP-' . date("ymd") . ($data + 1) . "-KM";
         $finan->id_koperasi = Auth::user()->assigned_koperasi;
         $finan->id_anggota = Input::get('id_anggota');
         $finan->tabel = 'transaksis';
         $finan->jenis = 'biaya_materai';
         $finan->info = "Biaya Materai Pinjaman " . \App\Anggota::where('id_koperasi', Auth::user()->assigned_koperasi)->find(Input::get('id_anggota'))['nama'] . " Rp. " . number_format(Input::get('biaya_materai'), 2, ",", ".") . " (" . date("d/m/Y H:i:s") . ") " . Auth::user()->name;
         $finan->id_transaksi = $new->id;
         $finan->masuk = Input::get('biaya_materai');
         $finan->keluar = 0;
         $finan->save();
     }
     if (Input::get('tabungan') > 0) {
         $data = \App\Keuangan::orderBy('id', 'desc')->first()['id'];
         $finan = new \App\Keuangan();
         $finan->no_nota = 'KSP-' . date("ymd") . ($data + 1) . "-KT";
         $finan->id_koperasi = Auth::user()->assigned_koperasi;
         $finan->id_anggota = Input::get('id_anggota');
         $finan->tabel = 'transaksis';
         $finan->jenis = 'tabungan';
         $finan->info = "Tabungan Pinjaman " . \App\Anggota::where('id_koperasi', Auth::user()->assigned_koperasi)->find(Input::get('id_anggota'))['nama'] . " Rp. " . number_format(Input::get('tabungan'), 2, ",", ".") . " (" . date("d/m/Y H:i:s") . ") " . Auth::user()->name;
         $finan->id_transaksi = $new->id;
         $finan->masuk = Input::get('tabungan');
         $finan->keluar = 0;
         $finan->save();
     }
     for ($i = 1; $i <= Input::get('jangka_waktu'); $i++) {
         $baru = new \App\Transaksi();
         $baru->id_koperasi = Auth::user()->assigned_koperasi;
         $baru->id_induk = $new->id;
         $baru->created_by = Auth::user()->id;
         $baru->id_anggota = Input::get('id_anggota');
         $baru->jenis_transaksi = 'Pengembalian Pinjaman';
         $baru->id_jenis = 0;
         $baru->bunga = Input::get('persen_bunga');
         $baru->tabungan = Input::get('persen_tabungan');
         $baru->jumlah_asli = Input::get('angsuran');
         $baru->jumlah_bunga = Input::get('bunga');
         $baru->total_tabungan = Input::get('tabungan');
         $baru->jumlah_total = Input::get('angsuran_total');
         $baru->status = 'Belum Lunas';
         $baru->keterangan = '';
         $baru->biaya_admin = 0;
         $baru->biaya_asuransi = 0;
         $baru->total_peminjaman = 0;
         $baru->info_ke = $i;
         $baru->angsuran = 0;
         $baru->created_at = date("Y-m-" . date_format(date_create(Input::get('tanggal_jatuh_tempo')), 'd') . " H:i:s", strtotime("+" . $i . " month"));
         $baru->save();
     }
     return redirect(url('transaksi/pinjaman'));
 }