public function postDestroymode()
 {
     $mode = RefFailureMode::find(Input::get('id'));
     $input = array();
     $input['active'] = 0;
     $input['updated_by'] = Auth::user()->id;
     $mode->update($input);
     echo "success";
 }
 public function postDupproject()
 {
     $validator = Validator::make(Input::all(), Project::$rules);
     if ($validator->passes()) {
         $project = Project::where('created_by', '=', Auth::user()->id)->where('name', '=', Input::get('name'))->where('active', '=', 1)->get();
         if (!$project->isEmpty()) {
             return Redirect::to('project')->with('message_error', 'Project duplicate !');
         } else {
             $project = new Project();
             $project->name = Input::get('name');
             $project->member_id = Auth::user()->id;
             $project->created_by = Auth::user()->id;
             $project->save();
             $ref_cate = RefCategory::where('project_id', Input::get('project_id'))->where('active', 1)->where('type_use', 2)->get();
             if ($ref_cate) {
                 foreach ($ref_cate as $non) {
                     $ref_cate = RefCategory::find($non->id);
                     $clone = $ref_cate->replicate();
                     unset($clone['created_by'], $clone['updated_by'], $clone['project_id']);
                     $clone['project_id'] = $project->id;
                     $clone['created_by'] = Auth::user()->id;
                     $data = json_decode($clone, true);
                     $insert_id = RefCategory::create($data);
                     $temp = new RefTemp();
                     $temp->old_id = $non->id;
                     $temp->new_id = $insert_id->id;
                     $temp->project_id = $project->id;
                     $temp->table_id = 1;
                     $temp->save();
                 }
             }
             $ref_type = RefType::where('project_id', Input::get('project_id'))->where('active', 1)->where('type_use', 2)->get();
             if ($ref_type) {
                 foreach ($ref_type as $non) {
                     $ref_type = RefType::find($non->id);
                     $clone = $ref_type->replicate();
                     unset($clone['created_by'], $clone['updated_by'], $clone['project_id']);
                     $clone['project_id'] = $project->id;
                     $clone['created_by'] = Auth::user()->id;
                     $data = json_decode($clone, true);
                     $insert_id = RefType::create($data);
                     $temp = new RefTemp();
                     $temp->old_id = $non->id;
                     $temp->new_id = $insert_id->id;
                     $temp->project_id = $project->id;
                     $temp->table_id = 2;
                     $temp->save();
                 }
             }
             $ref_part = RefPart::where('project_id', Input::get('project_id'))->where('active', 1)->where('type_use', 2)->get();
             if ($ref_part) {
                 foreach ($ref_part as $non) {
                     $ref_part = RefPart::find($non->id);
                     $clone = $ref_part->replicate();
                     unset($clone['created_by'], $clone['updated_by'], $clone['project_id']);
                     $clone['project_id'] = $project->id;
                     $clone['created_by'] = Auth::user()->id;
                     $data = json_decode($clone, true);
                     $insert_id = RefPart::create($data);
                     $temp = new RefTemp();
                     $temp->old_id = $non->id;
                     $temp->new_id = $insert_id->id;
                     $temp->project_id = $project->id;
                     $temp->table_id = 3;
                     $temp->save();
                 }
             }
             $basic_equipment = BasicEquipment::where('project_id', Input::get('project_id'))->where('active', 1)->where('type_use', 2)->get();
             if ($basic_equipment) {
                 foreach ($basic_equipment as $non) {
                     $basic_equipment = BasicEquipment::find($non->id);
                     $clone = $basic_equipment->replicate();
                     unset($clone['created_by'], $clone['updated_by'], $clone['project_id']);
                     $clone['project_id'] = $project->id;
                     $clone['created_by'] = Auth::user()->id;
                     $data = json_decode($clone, true);
                     $insert = BasicEquipment::create($data);
                     $tempcat = DB::table('ref_template')->where('old_id', $insert->category_id)->where('table_id', 1)->where('project_id', $project->id)->first();
                     BasicEquipment::where('project_id', $project->id)->where('category_id', $insert->category_id)->update(array('category_id' => $tempcat->new_id));
                     $temptype = DB::table('ref_template')->where('old_id', $insert->type_id)->where('table_id', 2)->where('project_id', $project->id)->first();
                     BasicEquipment::where('project_id', $project->id)->where('type_id', $insert->type_id)->update(array('type_id' => $temptype->new_id));
                     $temppart = DB::table('ref_template')->where('old_id', $insert->part_id)->where('table_id', 3)->where('project_id', $project->id)->first();
                     BasicEquipment::where('project_id', $project->id)->where('part_id', $insert->part_id)->update(array('part_id' => $temppart->new_id));
                 }
                 // DB::table('ref_template')->delete();
             }
             $ref_fail_cause = RefFailureCause::where('project_id', Input::get('project_id'))->where('active', 1)->where('type_use', 2)->get();
             if ($ref_fail_cause) {
                 foreach ($ref_fail_cause as $non) {
                     $ref_fail_cause = RefFailureCause::find($non->id);
                     $clone = $ref_fail_cause->replicate();
                     unset($clone['created_by'], $clone['updated_by'], $clone['project_id']);
                     $clone['project_id'] = $project->id;
                     $clone['created_by'] = Auth::user()->id;
                     $data = json_decode($clone, true);
                     $insert_id = RefFailureCause::create($data);
                     $temp = new RefTemp();
                     $temp->old_id = $non->id;
                     $temp->new_id = $insert_id->id;
                     $temp->project_id = $project->id;
                     $temp->table_id = 4;
                     $temp->save();
                 }
             }
             $ref_fail_mode = RefFailureMode::where('project_id', Input::get('project_id'))->where('active', 1)->where('type_use', 2)->get();
             if ($ref_fail_mode) {
                 foreach ($ref_fail_mode as $non) {
                     $ref_fail_mode = RefFailureMode::find($non->id);
                     $clone = $ref_fail_mode->replicate();
                     unset($clone['created_by'], $clone['updated_by'], $clone['project_id']);
                     $clone['project_id'] = $project->id;
                     $clone['created_by'] = Auth::user()->id;
                     $data = json_decode($clone, true);
                     $insert_id = RefFailureMode::create($data);
                     $temp = new RefTemp();
                     $temp->old_id = $non->id;
                     $temp->new_id = $insert_id->id;
                     $temp->project_id = $project->id;
                     $temp->table_id = 5;
                     $temp->save();
                 }
             }
             $basic_failure = BasicFailure::where('project_id', Input::get('project_id'))->where('active', 1)->where('type_use', 2)->get();
             if ($basic_failure) {
                 foreach ($basic_failure as $non) {
                     $basic_failure = BasicFailure::find($non->id);
                     $clone = $basic_failure->replicate();
                     unset($clone['created_by'], $clone['updated_by'], $clone['project_id']);
                     $clone['project_id'] = $project->id;
                     $clone['created_by'] = Auth::user()->id;
                     $data = json_decode($clone, true);
                     $insert = BasicFailure::create($data);
                     $tempcause = DB::table('ref_template')->where('old_id', $insert->cause_id)->where('table_id', 4)->where('project_id', $project->id)->first();
                     BasicFailure::where('project_id', $project->id)->where('cause_id', $insert->cause_id)->update(array('cause_id' => $tempcause->new_id));
                     $tempmode = DB::table('ref_template')->where('old_id', $insert->mode_id)->where('table_id', 5)->where('project_id', $project->id)->first();
                     BasicFailure::where('project_id', $project->id)->where('mode_id', $insert->mode_id)->update(array('mode_id' => $tempmode->new_id));
                 }
             }
             $ref_task_type = RefTaskType::where('project_id', Input::get('project_id'))->where('active', 1)->where('type_use', 2)->get();
             if ($ref_task_type) {
                 foreach ($ref_task_type as $non) {
                     $ref_task_type = RefTaskType::find($non->id);
                     $clone = $ref_task_type->replicate();
                     unset($clone['created_by'], $clone['updated_by'], $clone['project_id']);
                     $clone['project_id'] = $project->id;
                     $clone['created_by'] = Auth::user()->id;
                     $data = json_decode($clone, true);
                     $insert_id = RefTaskType::create($data);
                     $temp = new RefTemp();
                     $temp->old_id = $non->id;
                     $temp->new_id = $insert_id->id;
                     $temp->project_id = $project->id;
                     $temp->table_id = 6;
                     $temp->save();
                 }
             }
             $ref_task_list = RefTaskList::where('project_id', Input::get('project_id'))->where('active', 1)->where('type_use', 2)->get();
             if ($ref_task_list) {
                 foreach ($ref_task_list as $non) {
                     $ref_task_list = RefTaskList::find($non->id);
                     $clone = $ref_task_list->replicate();
                     unset($clone['created_by'], $clone['updated_by'], $clone['project_id']);
                     $clone['project_id'] = $project->id;
                     $clone['created_by'] = Auth::user()->id;
                     $data = json_decode($clone, true);
                     RefTaskList::create($data);
                     $temp = new RefTemp();
                     $temp->old_id = $non->id;
                     $temp->new_id = $insert_id->id;
                     $temp->project_id = $project->id;
                     $temp->table_id = 7;
                     $temp->save();
                 }
             }
             $basic_task = BasicTask::where('project_id', Input::get('project_id'))->where('active', 1)->where('type_use', 2)->get();
             if ($basic_task) {
                 foreach ($basic_task as $non) {
                     $basic_task = BasicTask::find($non->id);
                     $clone = $basic_task->replicate();
                     unset($clone['created_by'], $clone['updated_by'], $clone['project_id']);
                     $clone['project_id'] = $project->id;
                     $clone['created_by'] = Auth::user()->id;
                     $data = json_decode($clone, true);
                     $insert = BasicTask::create($data);
                     $tempcause = DB::table('ref_template')->where('old_id', $insert->cause_id)->where('table_id', 4)->where('project_id', $project->id)->first();
                     BasicTask::where('project_id', $project->id)->where('cause_id', $insert->cause_id)->update(array('cause_id' => $tempcause->new_id));
                     $temptasktype = DB::table('ref_template')->where('old_id', $insert->type_id)->where('table_id', 6)->where('project_id', $project->id)->first();
                     BasicTask::where('project_id', $project->id)->where('type_id', $insert->type_id)->update(array('type_id' => $temptasktype->new_id));
                     $templist = DB::table('ref_template')->where('old_id', $insert->list_id)->where('table_id', 7)->where('project_id', $project->id)->first();
                     BasicTask::where('project_id', $project->id)->where('list_id', $insert->list_id)->update(array('list_id' => $templist->new_id));
                 }
             }
             $ref_task_interval = RefTaskInterval::where('project_id', Input::get('project_id'))->where('active', 1)->where('type_use', 2)->get();
             if ($ref_task_interval) {
                 foreach ($ref_task_interval as $non) {
                     $ref_task_interval = RefTaskInterval::find($non->id);
                     $clone = $ref_task_interval->replicate();
                     unset($clone['created_by'], $clone['updated_by'], $clone['project_id']);
                     $clone['project_id'] = $project->id;
                     $clone['created_by'] = Auth::user()->id;
                     $data = json_decode($clone, true);
                     RefTaskInterval::create($data);
                 }
             }
             $ref_non_critical_question = RefNonCriticalQuestion::where('project_id', Input::get('project_id'))->where('active', 1)->where('type_use', 2)->get();
             if ($ref_non_critical_question) {
                 foreach ($ref_non_critical_question as $non) {
                     $ref_non_critical_question = RefNonCriticalQuestion::find($non->id);
                     $clone = $ref_non_critical_question->replicate();
                     unset($clone['created_by'], $clone['updated_by'], $clone['project_id']);
                     $clone['project_id'] = $project->id;
                     $clone['created_by'] = Auth::user()->id;
                     $data = json_decode($clone, true);
                     RefNonCriticalQuestion::create($data);
                 }
             }
             $ref_order_type = RefOrderType::where('project_id', Input::get('project_id'))->where('active', 1)->where('type_use', 2)->get();
             if ($ref_order_type) {
                 foreach ($ref_order_type as $non) {
                     $ref_order_type = RefOrderType::find($non->id);
                     $clone = $ref_order_type->replicate();
                     unset($clone['created_by'], $clone['updated_by'], $clone['project_id']);
                     $clone['project_id'] = $project->id;
                     $clone['created_by'] = Auth::user()->id;
                     $data = json_decode($clone, true);
                     RefOrderType::create($data);
                 }
             }
             DB::table('ref_template')->delete();
             Session::put('project_id', $project->id);
             Session::put('project', $project->name);
             return Redirect::to('project')->withSuccess('Duplicate Project Success.');
         }
     }
     return Redirect::to('project')->withErrors($validator)->withInput();
 }
 public function postDupproject()
 {
     $validator = Validator::make(Input::all(), Project::$rules);
     if ($validator->passes()) {
         $project = Project::where('created_by', '=', Auth::user()->id)->where('name', '=', Input::get('name'))->where('active', '=', 1)->get();
         if (!$project->isEmpty()) {
             return Redirect::to('dash-board')->with('message_error', 'Project duplicate !');
         } else {
             $project = new Project();
             $project->name = Input::get('name');
             $project->member_id = Auth::user()->id;
             $project->created_by = Auth::user()->id;
             $project->save();
             $project_id = Input::get('project_id');
             $basic_equipment = BasicEquipment::where('project_id', '=', $project_id)->where('active', '=', 1)->get();
             $basic_failure = BasicFailure::where('project_id', '=', $project_id)->where('active', '=', 1)->get();
             $basic_task = BasicTask::where('project_id', '=', $project_id)->where('active', '=', 1)->get();
             $ref_task_interval = RefTaskInterval::where('project_id', '=', $project_id)->where('active', '=', 1)->get();
             // Duplicate Basic Equipment
             if (!$basic_equipment->isEmpty()) {
                 foreach ($basic_equipment as $bq) {
                     $ref_cate = RefCategory::find($bq->category_id);
                     $category = new RefCategory();
                     $category->description = $ref_cate->description;
                     $category->created_by = Auth::user()->id;
                     $category->project_id = $project->id;
                     $category->save();
                     $ref_type = RefType::find($bq->type_id);
                     $type = new RefType();
                     $type->description = $ref_type->description;
                     $type->created_by = Auth::user()->id;
                     $type->project_id = $project->id;
                     $type->save();
                     $ref_part = RefPart::find($bq->part_id);
                     $part = new RefPart();
                     $part->description = $ref_part->description;
                     $part->created_by = Auth::user()->id;
                     $part->project_id = $project->id;
                     $part->save();
                     $basic_eq = new BasicEquipment();
                     $basic_eq->category_id = $category->id;
                     $basic_eq->type_id = $type->id;
                     $basic_eq->part_id = $part->id;
                     $basic_eq->created_by = Auth::user()->id;
                     $basic_eq->project_id = $project->id;
                     $basic_eq->save();
                 }
             }
             // Duplicate Basic Failure
             if (!$basic_failure->isEmpty()) {
                 foreach ($basic_failure as $bf) {
                     $mode = RefFailureMode::find($bf->mode_id);
                     $f_mode = new RefFailureMode();
                     $f_mode->description = $mode->description;
                     $f_mode->created_by = Auth::user()->id;
                     $f_mode->project_id = $project->id;
                     $f_mode->save();
                     $cause = RefFailureCause::find($bf->cause_id);
                     $f_cause = new RefFailureCause();
                     $f_cause->description = $cause->description;
                     $f_cause->created_by = Auth::user()->id;
                     $f_cause->project_id = $project->id;
                     $f_cause->save();
                     $basic_f = new BasicFailure();
                     $basic_f->mode_id = $f_mode->id;
                     $basic_f->cause_id = $f_cause->id;
                     $basic_f->created_by = Auth::user()->id;
                     $basic_f->project_id = $project->id;
                     $basic_f->save();
                 }
             }
             // Duplication Basic Task
             if (!$basic_task->isEmpty()) {
                 foreach ($basic_task as $bt) {
                     $basic_tk = new BasicTask();
                     $cause_t = RefFailureCause::where('id', '=', $bt->cause_id)->where('project_id', '=', $project->id)->where('active', '=', 1)->get();
                     // print_r($cause_t);
                     if ($cause_t->isEmpty()) {
                         $cause_ft = RefFailureCause::find($bt->cause_id);
                         $f_cause_t = new RefFailureCause();
                         $f_cause_t->description = $cause_ft->description;
                         $f_cause_t->created_by = Auth::user()->id;
                         $f_cause_t->project_id = $project->id;
                         $f_cause_t->save();
                         $basic_tk->cause_id = $f_cause_t->id;
                     } else {
                         $basic_tk->cause_id = $cause_t->id;
                     }
                     $task_tp = RefTaskType::find($bt->type_id);
                     $tk_type = new RefTaskType();
                     $tk_type->description = $task_tp->description;
                     $tk_type->created_by = Auth::user()->id;
                     $tk_type->project_id = $project->id;
                     $tk_type->save();
                     $basic_tk->type_id = $tk_type->id;
                     $task_lt = RefTaskList::find($bt->list_id);
                     $tk_list = new RefTaskList();
                     $tk_list->description = $task_lt->description;
                     $tk_list->created_by = Auth::user()->id;
                     $tk_list->project_id = $project->id;
                     $tk_list->save();
                     $basic_tk->list_id = $tk_list->id;
                     $basic_tk->created_by = Auth::user()->id;
                     $basic_tk->project_id = $project->id;
                     $basic_tk->save();
                 }
             }
             if (!$ref_task_interval->isEmpty()) {
                 foreach ($ref_task_interval as $t_inter) {
                     $tk_inter = new RefTaskInterval();
                     $tk_inter->interval = $t_inter->interval;
                     $tk_inter->description = $t_inter->description;
                     $tk_inter->created_by = Auth::user()->id;
                     $tk_inter->project_id = $project->id;
                     $tk_inter->save();
                 }
             }
             Session::put('project_id', $project->id);
             Session::put('project', $project->name);
             return Redirect::to('dash-board')->withSuccess('Duplicate Project Success.');
         }
     }
     return Redirect::to('dash-board')->withErrors($validator)->withInput();
 }
 public function postFailuremode()
 {
     // $input = array('mode_id' => Input::get('mode_id'));
     // $rules = array(
     //       	'mode_id' => 'required',
     //   	);
     //   	$validator = Validator::make($input, $rules);
     // if ($validator->passes()) {
     $where = '';
     if (!empty(Input::get('mode_id'))) {
         $where .= "WHERE T1.mode_id = " . Input::get('mode_id');
     }
     $data = array();
     $mode = array();
     $mode = RefFailureMode::find(Input::get('mode_id'));
     $data = DB::Select(DB::raw("\n\t\t\t\tSELECT\n\t*\nFROM\n\t(\n\t\tSELECT\n\t\t\tb.mode,\n\t\t\tb.mode_id,\n\t\t\ts.asset_name,s.description,\n\t\t\tf.*\n\t\tFROM\n\t\t\tasset_basic_failure f\n\t\tINNER JOIN (\n\t\t\tSELECT\n\t\t\t\tbf.id,\n\t\t\t\tm.description AS 'mode',\n\t\t\t\tm.id AS 'mode_id'\n\t\t\tFROM\n\t\t\t\tbasic_failures bf\n\t\t\tLEFT JOIN ref_failure_mode m ON bf.mode_id = m.id\n\t\t\tWHERE\n\t\t\t\tm.active = 1\n\t\t) b ON f.basic_failure_id = b.id\n\t\tINNER JOIN asset_registers s ON f.node = s.id\n\t\tWHERE\n\t\t\tf.active = 1\n\t\tAND f.project_id = " . Session::get('project_id') . "\n\t) T1\n{$where} GROUP BY T1.asset_name"));
     $input = array('asset_name' => $data);
     $rules = array('asset_name' => 'required');
     $messages = array('asset_name.required' => 'data not found!');
     $validator = Validator::make($input, $rules, $messages);
     if ($validator->passes()) {
         $count = DB::Select(DB::raw("SELECT\n" . "\tCOUNT(DISTINCT T1.asset_name) as 'total'\n" . "FROM\n" . "\t(\n" . "\t\tSELECT\n" . "\t\t\tb.mode,\n" . "\t\t\tb.mode_id,\n" . "\t\t\ts.asset_name,s.description,\n" . "\t\t\tf.*\n" . "\t\tFROM\n" . "\t\t\tasset_basic_failure f\n" . "\t\tINNER JOIN (\n" . "\t\t\tSELECT\n" . "\t\t\t\tbf.id,\n" . "\t\t\t\tm.description AS 'mode',\n" . "\t\t\t\tm.id AS 'mode_id'\n" . "\t\t\tFROM\n" . "\t\t\t\tbasic_failures bf\n" . "\t\t\tLEFT JOIN ref_failure_mode m ON bf.mode_id = m.id\n" . "\t\t\tWHERE\n" . "\t\t\t\tm.active = 1\n" . "\t\t) b ON f.basic_failure_id = b.id\n" . "\t\tINNER JOIN asset_registers s ON f.node = s.id\n" . "\t\tWHERE\n" . "\t\t\tf.active = 1\n" . "\t\tAND f.project_id = " . Session::get('project_id') . "\n" . "\t) T1\n" . " {$where}"))[0];
         $total = $count->total;
         $view = \View::make('report.print.report_failuremode')->with(compact('mode'))->with('total', $total)->with('data', $data)->render();
         $pdf = \App::make('dompdf.wrapper');
         $pdf->loadHTML($view);
         return $pdf->stream();
     }
     return Redirect::to('report/failuremode')->withErrors($validator)->withInput();
 }