/**
  * Store a newly created resource in storage.
  *
  * @return Response
  */
 public function store(CongvandiCreateFormRequest $request)
 {
     if (Auth::check() && $this->rolechucnangs && $this->rolechucnangs->Them == 1) {
         // xu ly file
         $now_date = str_replace('/', '_', date('d/m/Y'));
         if ($request->input('upload-file') && $request->hasFile('files')) {
             $files = $request->file('files');
             $newnamefiles = File::savefiles($files, Auth::user()->id);
         }
         // $nowtime = date();
         $nowtime = date("m/d/Y", time());
         $nowyear = date('Y');
         //$namden = date('Y-m-d', strtotime(str_replace('/','-', $request->input('ngayden'))) );
         $namky = date('Y-m-d', strtotime(str_replace('/', '-', $request->input('ngayky'))));
         $namkys = substr($namky, 0, 4);
         $namphathanh = date('Y-m-d', strtotime(str_replace('/', '-', $request->input('ngayphathanh'))));
         $namphathanhs = substr($namphathanh, 0, 4);
         $namluu = date('Y-m-d', strtotime(str_replace('/', '-', $request->input('ngayluu'))));
         $namluus = substr($namluu, 0, 4);
         $namhantraloi = date('Y-m-d', strtotime(str_replace('/', '-', $request->input('hantraloi'))));
         $namhantralois = substr($namhantraloi, 0, 4);
         if ($namkys != $nowyear || $namphathanhs != $nowyear || $namluus != $nowyear) {
             return \Redirect::route('congvandi.create')->with('error', 'Năm văn bản không đúng')->withInput();
         } else {
             // luu cong van
             $phong_id = User::where('id', Auth::user()->id)->value('phong_id');
             $congvandi = new Congvandi(array('socongvandiso' => $request->input('socongvandiso'), 'socongvandichu' => $request->input('socongvandichu'), 'ngayky' => date('Y-m-d', strtotime(str_replace('/', '-', $request->input('ngayky')))), 'noisoanthao_id' => $request->input('noisoanthao'), 'nguoiky_id' => $request->input('nguoiky'), 'danhmucloaicongvan_id' => $request->input('loaicongvan'), 'sobanphathanh' => $request->input('sobanphathanh'), 'trichyeucongvandi' => $request->input('trichyeucongvandi'), 'noinhancongvan' => $request->input('noinhancongvan'), 'danhmucdomat_id' => $request->input('domatcongvan'), 'ngayphathanh' => date('Y-m-d', strtotime(str_replace('/', '-', $request->input('ngayphathanh')))), 'danhmucdokhan_id' => $request->input('dokhancongvan'), 'ngaynhap' => date('Y-m-d', strtotime(str_replace('/', '-', $request->input('ngayluu')))), 'danhmucmucdich_id' => $request->input('mucdichcongvan'), 'danhmuchinhthucchuyen_id' => $request->input('hinhthucchuyen'), 'linhvucchuyende_id' => $request->input('linhvucchuyende'), 'danhmucchuyende_id' => $request->input('chuyende'), 'hantraloi' => date('Y-m-d', strtotime(str_replace('/', '-', $request->input('hantraloi')))), 'noiluu' => $request->input('noiluutru'), 'nguoitao_id' => \Auth::user()->id, 'phong_id' => $phong_id));
             $congvandi->save();
             $insertedId = $congvandi->id;
             $congvandifind = Congvandi::find($insertedId);
             $phongs = $request->input('phong');
             if ($phongs != NULL) {
                 $congvandifind->danhmucphongs()->sync($phongs);
             }
             $dois = $request->input('doi');
             if ($dois != NULL) {
                 $congvandifind->danhmucdois()->sync($phongs);
             }
             $canbos = $request->input('canbo');
             if ($canbos != NULL) {
                 $congvandifind->danhmuccanbos()->sync($canbos);
             }
             if ($request->input('upload-file') && $request->hasFile('files') && $newnamefiles) {
                 $congvandifind->files()->sync($newnamefiles);
             }
             //$findphongs = $congvandifind->danhmucphongs();
             // insert cong van den
             /*
             if($congvandifind && $findphongs)
             {
             	
             	$congvandi = new congvandi();
             	$congvandi->insertcongvandi($congvandifind, $phongs, Auth::user()->id);
             	
             }
             */
             return \Redirect::route('congvandi.index')->with('message', 'Lưu thành công!');
         }
     } else {
         return \Redirect::route('congvandi.index')->with('message-error', 'Không có quyền truy cập');
     }
 }