/**
  * Store a newly created resource in storage.
  *
  * @return Response
  */
 public function store()
 {
     // 1. setting validasi
     $messages = array('required' => 'Inputan <b>Tidak Boleh Kosong</b>!', 'numeric' => 'Inputan <b>Harus Angka</b>!', 'same' => 'Password <b>Tidak Sama</b>!');
     $validator = Validator::make(Input::all(), array("jmlang" => "required|numeric", "nilhut" => "required|numeric"), $messages);
     // 2a. jika semua validasi terpenuhi simpan ke database
     if ($validator->passes()) {
         $th01 = new th01();
         $idhut = $th01->getAutoIncrement();
         $nilhut = Input::get("nilhut");
         $jmlang = Input::get("jmlang");
         $idkar = Input::get("idkar");
         $sumhut = 0;
         $flaginsert = true;
         if ($th01->checkAktifHutang($idkar) > 0) {
             Session::flash('th01_danger', 'Transaksi Gagal! Hutang / Kasbon Karyawan Belum Lunas!');
         } else {
             if (Input::get("idhut") == "Kas Bon") {
                 if (Input::get("jmlang") != 1) {
                     Session::flash('th01_danger', 'Transaksi Gagal! <b> Angsuran</b> Kasbon Harus Bernilai 1 !');
                     $flaginsert = FALSE;
                 }
             }
             $cek = new th01();
             $latestHutang = $cek->getLatestHutang($idkar);
             if (count($latestHutang) > 0) {
                 if (date("m", strtotime($latestHutang[0]->tglhut)) == date("m")) {
                     Session::flash('th01_danger', 'Transaksi Gagal! Hutang / Kasbon Karyawan Pada Bulan Ini Telah Dibuat !');
                     $flaginsert = FALSE;
                 }
             }
             if ($flaginsert) {
                 $th01->norhut = Input::get("idhut") == "Hutang" ? "HT" . $idhut . date("m") . date("y") : "KB" . $idhut . date("m") . date("y");
                 $th01->tglhut = date("Y-m-d");
                 $th01->jenhut = Input::get("idhut");
                 $th01->jmlang = Input::get("jmlang");
                 $th01->nilhut = Input::get("nilhut");
                 $th01->flglns = "N";
                 $th01->idkar = Input::get("idkar");
                 $th01->save();
                 for ($i = 1; $i <= $jmlang; $i++) {
                     $th02 = new th02();
                     if ((int) date("d") <= 10) {
                         $strhut = "+" . ($i - 1) . " month";
                     } else {
                         $strhut = "+" . $i . " month";
                     }
                     $th02->tglph = date('Y-m-d', strtotime($strhut, strtotime(date("Y-m-d"))));
                     if ($i == $jmlang) {
                         $th02->nilph = $nilhut - $sumhut;
                     } else {
                         $tamp = $nilhut / 1000 / $jmlang;
                         $tamp = floor($tamp) * 1000;
                         $th02->nilph = $tamp;
                         $sumhut += $tamp;
                     }
                     $th02->status = "N";
                     $th02->idhut = $idhut;
                     $th02->save();
                 }
                 Session::flash('th01_success', 'Data Telah Ditambahkan!');
             }
         }
         return Redirect::to('inputdata/hutang');
     } else {
         return Redirect::to('inputdata/hutang')->withErrors($validator)->withInput();
     }
 }