/**
  * Store a newly created resource in storage.
  * POST /project
  *
  * @return Response
  */
 public function store()
 {
     // Input::merge(array_map('trim', Input::all()));
     $input = Input::all();
     $validation = Validator::make($input, Project::$rules);
     //    	 if(Input::get('project_classification') == '0' || Input::get('project_category') == '0' || Input::get('project_stage') == '0')
     // {
     // 	return Redirect::route('project.create')
     // 					->withInput()
     // 					->withErrors($validation)
     // 					->with('class', 'warning')
     // 					->with('message', 'Fill-up project classification, project category & project stage.');
     // }
     $developer = Input::get('developer');
     $sub_developer = Input::get('sub_developer');
     $general_contractor = Input::get('general_contractor');
     $sub_general_contractor = Input::get('sub_general_contractor');
     $project_mgr_designer = Input::get('project_mgr_designer');
     $sub_project_mgr_designer = Input::get('sub_project_mgr_designer');
     $architect = Input::get('architect');
     $sub_architect = Input::get('sub_architect');
     $applicator = Input::get('applicator');
     $sub_applicator = Input::get('sub_applicator');
     $dealer_supplier = Input::get('dealer_supplier');
     $sub_dealer_supplier = Input::get('sub_dealer_supplier');
     $gettime = time() - 14400;
     $datetime_now = date("H:i:s", $gettime);
     if ($validation->passes()) {
         $project = new Project();
         $project->date_reported = date("Y-m-d", strtotime(Input::get('date_reported')));
         $project->project_photos = Input::get('project_photos');
         $project->bdo_id = Input::get('bdo');
         $project->area_id = Input::get('area_region');
         $project->project_name = strtoupper(Input::get('project_name'));
         $project->project_owner = strtoupper(Input::get('project_owner'));
         $project->street = strtoupper(Input::get('street'));
         $project->city = Input::get('city');
         $project->province = Input::get('province');
         $project->country = strtoupper(Input::get('country'));
         $project->zip_code = Input::get('zip_code');
         $project->project_classification = Input::get('project_classification');
         $project->project_category = Input::get('project_category');
         $project->project_stage = Input::get('project_stage');
         $project->project_status = Input::get('project_status');
         $project->project_details = strtoupper(Input::get('project_details'));
         $project->painting_dtstart = Input::get('painting_dtstart');
         $project->painting_dtend = Input::get('painting_dtend');
         $project->painting_specification = Input::get('painting_specification1', 'N/A');
         $project->paints = strtoupper(Input::get('paint'));
         $project->area = strtoupper(Input::get('area_sqm'));
         $project->painting_requirement = strtoupper(Input::get('painting_req'));
         $project->painting_cost = strtoupper(Input::get('painting_cost'));
         $project->painting_specification_2 = Input::get('painting_specification2', 'N/A');
         $project->paints2 = strtoupper(Input::get('2nd_paint'));
         $project->area2 = strtoupper(Input::get('2nd_area_sqm'));
         $project->painting_requirement2 = strtoupper(Input::get('2nd_painting_req'));
         $project->painting_cost2 = strtoupper(Input::get('2nd_painting_cost'));
         $project->painting_specification_3 = Input::get('painting_specification3', 'N/A');
         $project->paints3 = strtoupper(Input::get('3rd_paint'));
         $project->area3 = strtoupper(Input::get('3rd_area_sqm'));
         $project->painting_requirement3 = strtoupper(Input::get('3rd_painting_req'));
         $project->painting_cost3 = strtoupper(Input::get('3rd_painting_cost'));
         $project->status = 1;
         $project->created_by = Auth::id();
         $project->approved_by = 0;
         if (Project::checkifexist($project)) {
             return Redirect::route('project.create')->withInput()->withErrors($validation)->with('class', 'warning')->with('message', 'Record already exist.');
         }
         $project->save();
         DB::table('project_users')->insert(array(['created_by' => Auth::id(), 'user_id' => Input::get('bdo'), 'project_id' => $project->id, 'status' => 1]));
         if (count($developer) > 0) {
             foreach ($developer as $dev_row) {
                 DB::table('developer')->insert(array(['project_id' => $project->id, 'user_id' => Auth::id(), 'developer_id' => $dev_row, 'status' => 1, 'date_created' => date('Y-m-d'), 'time_created' => $datetime_now]));
                 $dev_bdo_id = DB::table('contacts')->select('created_by')->where('id', $dev_row)->first();
                 DB::table('project_users')->insert(array(['created_by' => Auth::id(), 'user_id' => $dev_bdo_id->created_by, 'project_id' => $project->id, 'status' => 1]));
             }
         }
         if (count($sub_developer) > 0) {
             foreach ($sub_developer as $sub_dev_row) {
                 DB::table('sub_developer')->insert(array(['project_id' => $project->id, 'user_id' => Auth::id(), 'sub_developer_id' => $sub_dev_row, 'status' => 1, 'date_created' => date('Y-m-d'), 'time_created' => $datetime_now]));
                 $sub_dev_row = DB::table('contacts')->select('created_by')->where('id', $sub_dev_row)->first();
                 DB::table('project_users')->insert(array(['created_by' => Auth::id(), 'user_id' => $sub_dev_row->created_by, 'project_id' => $project->id, 'status' => 1]));
             }
         }
         if (count($general_contractor) > 0) {
             foreach ($general_contractor as $gencon_row) {
                 DB::table('gencon')->insert(array(['project_id' => $project->id, 'user_id' => Auth::id(), 'gencon_id' => $gencon_row, 'status' => 1, 'date_created' => date('Y-m-d'), 'time_created' => $datetime_now]));
                 $gencon_bdo_id = DB::table('contacts')->select('created_by')->where('id', $gencon_row)->first();
                 DB::table('project_users')->insert(array(['created_by' => Auth::id(), 'user_id' => $gencon_bdo_id->created_by, 'project_id' => $project->id, 'status' => 1]));
             }
         }
         if (count($sub_general_contractor) > 0) {
             foreach ($sub_general_contractor as $sub_gencon_row) {
                 DB::table('sub_gencon')->insert(array(['project_id' => $project->id, 'user_id' => Auth::id(), 'sub_gencon_id' => $sub_gencon_row, 'status' => 1, 'date_created' => date('Y-m-d'), 'time_created' => $datetime_now]));
                 $sub_gencon_bdo_id = DB::table('contacts')->select('created_by')->where('id', $sub_gencon_row)->first();
                 DB::table('project_users')->insert(array(['created_by' => Auth::id(), 'user_id' => $sub_gencon_bdo_id->created_by, 'project_id' => $project->id, 'status' => 1]));
             }
         }
         if (count($project_mgr_designer) > 0) {
             foreach ($project_mgr_designer as $projmgrdes_row) {
                 DB::table('project_mgr_designer')->insert(array(['project_id' => $project->id, 'user_id' => Auth::id(), 'project_mgr_designer_id' => $projmgrdes_row, 'status' => 1, 'date_created' => date('Y-m-d'), 'time_created' => $datetime_now]));
                 $projmgrdes_bdo_id = DB::table('contacts')->select('created_by')->where('id', $projmgrdes_row)->first();
                 DB::table('project_users')->insert(array(['created_by' => Auth::id(), 'user_id' => $projmgrdes_bdo_id->created_by, 'project_id' => $project->id, 'status' => 1]));
             }
         }
         if (count($sub_project_mgr_designer) > 0) {
             foreach ($sub_project_mgr_designer as $sub_projmgrdes_row) {
                 DB::table('sub_project_mgr_designer')->insert(array(['project_id' => $project->id, 'user_id' => Auth::id(), 'sub_project_mgr_designer_id' => $sub_projmgrdes_row, 'status' => 1, 'date_created' => date('Y-m-d'), 'time_created' => $datetime_now]));
                 $sub_projmgrdes_bdo_id = DB::table('contacts')->select('created_by')->where('id', $sub_projmgrdes_row)->first();
                 DB::table('project_users')->insert(array(['created_by' => Auth::id(), 'user_id' => $sub_projmgrdes_bdo_id->created_by, 'project_id' => $project->id, 'status' => 1]));
             }
         }
         if (count($architect) > 0) {
             foreach ($architect as $arch_row) {
                 DB::table('architect')->insert(array(['project_id' => $project->id, 'user_id' => Auth::id(), 'architect_id' => $arch_row, 'status' => 1, 'date_created' => date('Y-m-d'), 'time_created' => $datetime_now]));
                 $arch_bdo_id = DB::table('contacts')->select('created_by')->where('id', $arch_row)->first();
                 DB::table('project_users')->insert(array(['created_by' => Auth::id(), 'user_id' => $arch_bdo_id->created_by, 'project_id' => $project->id, 'status' => 1]));
             }
         }
         if (count($sub_architect) > 0) {
             foreach ($sub_architect as $sub_arch_row) {
                 DB::table('sub_architect')->insert(array(['project_id' => $project->id, 'user_id' => Auth::id(), 'sub_architect_id' => $sub_arch_row, 'status' => 1, 'date_created' => date('Y-m-d'), 'time_created' => $datetime_now]));
                 $sub_arch_bdo_id = DB::table('contacts')->select('created_by')->where('id', $sub_arch_row)->first();
                 DB::table('project_users')->insert(array(['created_by' => Auth::id(), 'user_id' => $sub_arch_bdo_id->created_by, 'project_id' => $project->id, 'status' => 1]));
             }
         }
         if (count($applicator) > 0) {
             foreach ($applicator as $app_row) {
                 DB::table('applicator')->insert(array(['project_id' => $project->id, 'user_id' => Auth::id(), 'applicator_id' => $app_row, 'status' => 1, 'date_created' => date('Y-m-d'), 'time_created' => $datetime_now]));
                 $app_bdo_id = DB::table('contacts')->select('created_by')->where('id', $app_row)->first();
                 DB::table('project_users')->insert(array(['created_by' => Auth::id(), 'user_id' => $app_bdo_id->created_by, 'project_id' => $project->id, 'status' => 1]));
             }
         }
         if (count($sub_applicator) > 0) {
             foreach ($sub_applicator as $sub_app_row) {
                 DB::table('sub_applicator')->insert(array(['project_id' => $project->id, 'user_id' => Auth::id(), 'sub_applicator_id' => $sub_app_row, 'status' => 1, 'date_created' => date('Y-m-d'), 'time_created' => $datetime_now]));
                 $sub_app_bdo_id = DB::table('contacts')->select('created_by')->where('id', $sub_app_row)->first();
                 DB::table('project_users')->insert(array(['created_by' => Auth::id(), 'user_id' => $sub_app_bdo_id->created_by, 'project_id' => $project->id, 'status' => 1]));
             }
         }
         if (count($dealer_supplier) > 0) {
             foreach ($dealer_supplier as $dealsupp_row) {
                 DB::table('dealer_supplier')->insert(array(['project_id' => $project->id, 'user_id' => Auth::id(), 'dealer_supplier_id' => $dealsupp_row, 'status' => 1, 'date_created' => date('Y-m-d'), 'time_created' => $datetime_now]));
                 $dealsupp_bdo_id = DB::table('contacts')->select('created_by')->where('id', $dealsupp_row)->first();
                 DB::table('project_users')->insert(array(['created_by' => Auth::id(), 'user_id' => $dealsupp_bdo_id->created_by, 'project_id' => $project->id, 'status' => 1]));
             }
         }
         if (count($sub_dealer_supplier) > 0) {
             foreach ($sub_dealer_supplier as $sub_dealsupp_row) {
                 DB::table('sub_dealer_supplier')->insert(array(['project_id' => $project->id, 'user_id' => Auth::id(), 'sub_dealer_supplier_id' => $sub_dealsupp_row, 'status' => 1, 'date_created' => date('Y-m-d'), 'time_created' => $datetime_now]));
                 $sub_dealsupp_bdo_id = DB::table('contacts')->select('created_by')->where('id', $sub_dealsupp_row)->first();
                 DB::table('project_users')->insert(array(['created_by' => Auth::id(), 'user_id' => $sub_dealsupp_bdo_id->created_by, 'project_id' => $project->id, 'status' => 1]));
             }
         }
         $imagename = Input::file('image');
         $imagestatus = Input::get('photo_files_type');
         $img_count = count($imagename);
         // $project_id = $project->id;
         // $id = Auth::id();
         if (Input::hasFile('image')) {
             $uploadcount = 0;
             foreach ($imagename as $img) {
                 $mimeType = array('image/png', 'image/gif', 'image/jpeg', 'image/bmp', 'application/zip', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/vnd.ms-office', 'application/vnd.ms-excel', 'application/pdf', 'application/msword');
                 $imgname = $img->getClientOriginalName();
                 if (!in_array($img->getMimeType(), $mimeType)) {
                     DB::table('projects')->where('id', '=', $project->id)->delete();
                     DB::table('project_users')->where('project_id', '=', $project->id)->delete();
                     DB::table('project_images')->where('project_id', '=', $project->id)->delete();
                     DB::table('project_files')->where('project_id', '=', $project->id)->delete();
                     DB::table('developer')->where('project_id', '=', $project->id)->delete();
                     DB::table('sub_developer')->where('project_id', '=', $project->id)->delete();
                     DB::table('gencon')->where('project_id', '=', $project->id)->delete();
                     DB::table('sub_gencon')->where('project_id', '=', $project->id)->delete();
                     DB::table('project_mgr_designer')->where('project_id', '=', $project->id)->delete();
                     DB::table('sub_project_mgr_designer')->where('project_id', '=', $project->id)->delete();
                     DB::table('architect')->where('project_id', '=', $project->id)->delete();
                     DB::table('sub_architect')->where('project_id', '=', $project->id)->delete();
                     DB::table('applicator')->where('project_id', '=', $project->id)->delete();
                     DB::table('sub_applicator')->where('project_id', '=', $project->id)->delete();
                     DB::table('dealer_supplier')->where('project_id', '=', $project->id)->delete();
                     DB::table('sub_dealer_supplier')->where('project_id', '=', $project->id)->delete();
                     // $extension = Input::file('image')->getClientOriginalExtension(); // getting image extension
                     $delete_imgname = $img->getClientOriginalName();
                     if ($img->getClientOriginalExtension() == "gif" || $img->getClientOriginalExtension() == "jpg" || $img->getClientOriginalExtension() == "png" || $img->getClientOriginalExtension() == "bmp" || $img->getClientOriginalExtension() == "jpeg") {
                         $destinationPath = base_path() . '/public/asset/img/project';
                         // upload path
                     } else {
                         $destinationPath = base_path() . '/public/asset/files/project';
                         // upload path
                     }
                     File::delete($destinationPath . '/' . $delete_imgname);
                     return Redirect::route('project.create')->withInput()->withErrors($validation)->with('class', 'error')->with('message', 'Make sure that upload file is correct.');
                 } else {
                     // $extension = Input::file('image')->getClientOriginalExtension(); // getting image extension
                     $imgname = $img->getClientOriginalName();
                     if ($img->getClientOriginalExtension() == "gif" || $img->getClientOriginalExtension() == "jpg" || $img->getClientOriginalExtension() == "png" || $img->getClientOriginalExtension() == "bmp" || $img->getClientOriginalExtension() == "jpeg") {
                         $destinationPath = base_path() . '/public/asset/img/project';
                         // upload path
                     } else {
                         $destinationPath = base_path() . '/public/asset/files/project';
                         // upload path
                     }
                     $upload_success = $img->move($destinationPath, $imgname);
                     if ($img->getClientOriginalExtension() == "gif" || $img->getClientOriginalExtension() == "jpg" || $img->getClientOriginalExtension() == "png" || $img->getClientOriginalExtension() == "bmp" || $img->getClientOriginalExtension() == "jpeg") {
                         DB::table('project_images')->insert(array('user_id' => Auth::id(), 'project_id' => $project->id, 'image' => $imgname, 'status' => 1));
                     } else {
                         DB::table('project_files')->insert(array('user_id' => Auth::id(), 'project_id' => $project->id, 'file' => $imgname, 'status' => 1));
                     }
                 }
                 $uploadcount++;
             }
         }
         DB::table('project_status')->insert(array(['project_id' => $project->id, 'user_id' => Auth::id(), 'update' => 'CREATE' . ' ' . strtoupper(Input::get('project_name')) . ' ' . 'IN PROJECT RECORD', 'created_by' => Auth::id(), 'created_at' => date('Y-m-d') . ' ' . $datetime_now]));
         return Redirect::route('project.index')->with('class', 'success')->with('message', 'Record successfully created.');
     } else {
         return Redirect::route('project.create')->withInput()->withErrors($validation)->with('class', 'error')->with('message', 'There were validation errors.');
     }
 }