/**
  * Store a newly created resource in storage.
  * Lưu công văn đến từ bộ
  * @return Response
  */
 public function storebo(CongvandenCreateFormRequest $request)
 {
     if (Auth::check() && $this->rolechucnangs && $this->rolechucnangs->Them == 1) {
         // validate name
         $nowtime = date("m/d/Y", time());
         $nowyear = date('Y');
         //$namky = date('Y',strtotime($request->input('ngayky')));
         $namden = date('Y-m-d', strtotime(str_replace('/', '-', $request->input('ngayden'))));
         $namdens = substr($namden, 0, 4);
         //echo $namdens;
         if ($namdens != $nowyear) {
             return \Redirect::route('congvanden.createbo')->with('error', 'Năm của ngày đến không đúng (chỉ nhập được công văn của năm hiện tại)')->withInput();
         } else {
             $phong_id = User::where('id', Auth::user()->id)->value('phong_id');
             $sohosoluu = $request->input('sohosoluu');
             // kiem tra so ho luu da ton tai chua ?
             if (Congvanden::checksohosoluubo($sohosoluu, $phong_id) == TRUE) {
                 $action = 'congvanden.storebo';
                 return \Redirect::route('congvanden.createbo')->with(['error' => 'Số công văn lưu đã tồn tại'])->withInput();
                 //return response()->json('socongvanluu');
             } else {
                 $sohosoluu = $sohosoluu;
             }
             $socongvandengocso = $request->input('socongvandengocso');
             if ($socongvandengocso == '') {
                 $socongvandengocso = NULL;
             }
             $socongvandengocchu = $request->input('socongvandengocchu');
             if ($socongvandengocchu == '') {
                 $socongvandengocchu = NULL;
             }
             //dd($socongvandengocso);
             // luu cong van
             $congvanden = new Congvanden(array('sohosoluubo' => $sohosoluu, 'socongvandenso' => $request->input('socongvandenso'), 'socongvandenchu' => $request->input('socongvandenchu'), 'capphathanh_id' => $request->input('capphathanh'), 'noiphathanh_id' => $request->input('noiphathanh'), 'ngayky' => date('Y-m-d', strtotime(str_replace('/', '-', $request->input('ngayky')))), 'danhmucdomat_id' => $request->input('domatcongvan'), 'danhmucmucdich_id' => $request->input('mucdich'), 'danhmucloaicongvan_id' => $request->input('loaicongvan'), 'linhvucchuyende_id' => $request->input('linhvucchuyende'), 'danhmucchuyende_id' => $request->input('chuyende'), 'nguoiky' => $request->input('nguoiky'), 'danhmucdokhan_id' => $request->input('dokhancongvan'), 'ngayden' => date('Y-m-d', strtotime(str_replace('/', '-', $request->input('ngayden')))), 'noiluucv' => $request->input('noiluucv'), 'trichyeucongvanden' => $request->input('trichyeucongvanden'), 'quatrinhxuly' => $request->input('quatrinhxuly'), 'trichykienlanhdao' => $request->input('trichykienlanhdao'), 'socongvangocso' => $socongvandengocso, 'socongvangocchu' => $socongvandengocchu, 'ngayphathanhcongvangoc' => date('Y-m-d', strtotime(str_replace('/', '-', $request->input('ngaycongvangoc')))), 'capphathanhcongvangoc_id' => $request->input('capphathanhcongvangoc'), 'noiphathanhcongvangoc_id' => $request->input('noiphathanhcongvangoc'), 'ketquaxuly' => $request->input('ketquaxuly'), 'dukienthoihanketthucxuly' => date('Y-m-d', strtotime(str_replace('/', '-', $request->input('dukienthoihanketthucxuly')))), 'traloicongvan' => $request->input('traloicongvan'), 'phong_id' => $phong_id, 'nguoitao_id' => \Auth::user()->id));
             $congvanden->save();
             $insertedId = $congvanden->id;
             $congvandenfind = Congvanden::findOrFail($insertedId);
             $phongs = $request->input('phong');
             // phòng
             if ($phongs != NULL) {
                 $congvandenfind->danhmucphongs()->sync($phongs);
             }
             $dois = $request->input('doi');
             // đội
             if ($dois != NULL) {
                 $congvandenfind->danhmucdois()->sync($dois);
             }
             $canbos = $request->input('canbo');
             // cá nhân
             if ($canbos != NULL) {
                 $congvandenfind->danhmuccanbos()->sync($canbos);
             }
             // xu ly file
             if ($request->input('upload-file') && $request->hasFile('files')) {
                 $files = $request->file('files');
                 $newnamefiles = File::savefiles($files, Auth::user()->id);
                 $congvandenfind->files()->sync($newnamefiles);
             }
             //Gửi email
             //dd($listfiles);
             if ($request->input('guiemail') == 'on' && $request->input('upload-file')) {
                 $dataemail = array('emails' => $request->input('emails'), 'tieude' => $request->input('tieude'), 'noidungemail' => $request->input('noidungemail'), 'attachfiles' => $newnamefiles);
                 guimails($dataemail);
             } else {
                 if ($request->input('guiemail') == 'on') {
                     $dataemail = ['emails' => $request->input('emails'), 'tieude' => $request->input('tieude'), 'noidungemail' => $request->input('noidungemail')];
                     guimails($dataemail);
                 }
             }
             return \Redirect::route('congvanden.index')->with('message', 'Lưu thành công!');
             //return response()->json('success');
         }
     } else {
         return \Redirect::route('congvanden.index')->with('message-error', 'Không có quyền truy cập');
     }
 }