public function postImportproject()
 {
     $input = array('file' => Input::file('import'), 'name' => Input::get('name'));
     $rules = array('file' => 'required|max:50000', 'name' => 'required');
     $validator = Validator::make($input, $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();
             $project_id = $project->id;
             $datefile = date('dmY');
             $ranfile = rand(11111, 99999);
             $destinationPath = 'public/backup/import';
             $extension = Input::file('import')->getClientOriginalExtension();
             $fileName = rand(11111, 99999) . '.' . $extension;
             Input::file('import')->move($destinationPath, $fileName);
             $folderimport = $ranfile . '_' . $datefile;
             Zipper::make($destinationPath . '/' . $fileName)->extractTo('public/backup/import');
             $ex_ref_basic_equip = glob("public/backup/import/*.csv");
             for ($i = 0; $i < count($ex_ref_basic_equip); $i++) {
                 if (substr($ex_ref_basic_equip[$i], 21, 3) == '004') {
                     Excel::filter('chunk')->load($ex_ref_basic_equip[$i])->chunk(250, function ($results) use($project_id) {
                         foreach ($results as $row) {
                             $ref_category = new RefCategory();
                             $ref_category->description = $row->description;
                             $ref_category->type_use = 2;
                             $ref_category->created_by = Auth::user()->id;
                             $ref_category->project_id = $project_id;
                             $ref_category->save();
                             // print_r($row->id);
                             $temp = new RefTemp();
                             $temp->old_id = $row->id;
                             $temp->new_id = $ref_category->id;
                             $temp->project_id = $project_id;
                             $temp->table_id = 1;
                             $temp->save();
                         }
                     });
                     unlink($ex_ref_basic_equip[$i]);
                 }
                 if (substr($ex_ref_basic_equip[$i], 21, 3) == '013') {
                     Excel::filter('chunk')->load($ex_ref_basic_equip[$i])->chunk(250, function ($results) use($project_id) {
                         foreach ($results as $row) {
                             $ref_type = new RefType();
                             $ref_type->description = $row->description;
                             $ref_type->type_use = 2;
                             $ref_type->created_by = Auth::user()->id;
                             $ref_type->project_id = $project_id;
                             $ref_type->save();
                             $temp = new RefTemp();
                             $temp->old_id = $row->id;
                             $temp->new_id = $ref_type->id;
                             $temp->project_id = $project_id;
                             $temp->table_id = 2;
                             $temp->save();
                         }
                     });
                     unlink($ex_ref_basic_equip[$i]);
                 }
                 if (substr($ex_ref_basic_equip[$i], 21, 3) == '009') {
                     Excel::filter('chunk')->load($ex_ref_basic_equip[$i])->chunk(250, function ($results) use($project_id) {
                         foreach ($results as $row) {
                             $ref_part = new RefPart();
                             $ref_part->description = $row->description;
                             $ref_part->type_use = 2;
                             $ref_part->created_by = Auth::user()->id;
                             $ref_part->project_id = $project_id;
                             $ref_part->save();
                             $temp = new RefTemp();
                             $temp->old_id = $row->id;
                             $temp->new_id = $ref_part->id;
                             $temp->project_id = $project_id;
                             $temp->table_id = 3;
                             $temp->save();
                         }
                     });
                     unlink($ex_ref_basic_equip[$i]);
                 }
             }
             $ex_basic_equip = glob("public/backup/import/*.csv");
             for ($i = 0; $i < count($ex_basic_equip); $i++) {
                 if (substr($ex_basic_equip[$i], 21, 3) == '001') {
                     Excel::filter('chunk')->load($ex_basic_equip[$i])->chunk(250, function ($results) use($project_id) {
                         foreach ($results as $row) {
                             $basic = new BasicEquipment();
                             $basic->category_id = $row->category_id;
                             $basic->part_id = $row->part_id;
                             $basic->type_id = $row->type_id;
                             $basic->type_use = 2;
                             $basic->created_by = Auth::user()->id;
                             $basic->project_id = $project_id;
                             $basic->save();
                             $temp = new RefTemp();
                             $temp->old_id = $row->id;
                             $temp->new_id = $basic->id;
                             $temp->project_id = $project_id;
                             $temp->table_id = 11;
                             $temp->save();
                             $tempcat = DB::table('ref_template')->where('old_id', $row->category_id)->where('table_id', 1)->where('project_id', $project_id)->first();
                             if (count($tempcat)) {
                                 BasicEquipment::where('project_id', $project_id)->where('category_id', $row->category_id)->update(array('category_id' => $tempcat->new_id));
                             }
                             $temptype = DB::table('ref_template')->where('old_id', $basic->type_id)->where('table_id', 2)->where('project_id', $project_id)->first();
                             if (count($temptype)) {
                                 BasicEquipment::where('project_id', $project_id)->where('type_id', $row->type_id)->update(array('type_id' => $temptype->new_id));
                             }
                             $temppart = DB::table('ref_template')->where('old_id', $row->part_id)->where('table_id', 3)->where('project_id', $project_id)->first();
                             if (count($temppart)) {
                                 BasicEquipment::where('project_id', $project_id)->where('part_id', $row->part_id)->update(array('part_id' => $temppart->new_id));
                             }
                         }
                     });
                     unlink($ex_basic_equip[$i]);
                     unset($ex_basic_equip[$i]);
                 }
             }
             $ext_ref_basicfailure = glob("public/backup/import/*.csv");
             for ($i = 0; $i < count($ext_ref_basicfailure); $i++) {
                 if (substr($ext_ref_basicfailure[$i], 21, 3) == '005') {
                     Excel::filter('chunk')->load($ext_ref_basicfailure[$i])->chunk(250, function ($results) use($project_id) {
                         foreach ($results as $row) {
                             $ref_failure_cause = new RefFailureCause();
                             $ref_failure_cause->description = $row->description;
                             $ref_failure_cause->type_use = 2;
                             $ref_failure_cause->created_by = Auth::user()->id;
                             $ref_failure_cause->project_id = $project_id;
                             $ref_failure_cause->save();
                             $temp = new RefTemp();
                             $temp->old_id = $row->id;
                             $temp->new_id = $ref_failure_cause->id;
                             $temp->project_id = $project_id;
                             $temp->table_id = 4;
                             $temp->save();
                         }
                     });
                     unlink($ext_ref_basicfailure[$i]);
                     unset($ext_ref_basicfailure[$i]);
                 } elseif (substr($ext_ref_basicfailure[$i], 21, 3) == '006') {
                     Excel::filter('chunk')->load($ext_ref_basicfailure[$i])->chunk(250, function ($results) use($project_id) {
                         foreach ($results as $row) {
                             $ref_failure_mode = new RefFailureMode();
                             $ref_failure_mode->description = $row->description;
                             $ref_failure_mode->type_use = 2;
                             $ref_failure_mode->created_by = Auth::user()->id;
                             $ref_failure_mode->project_id = $project_id;
                             $ref_failure_mode->save();
                             $temp = new RefTemp();
                             $temp->old_id = $row->id;
                             $temp->new_id = $ref_failure_mode->id;
                             $temp->project_id = $project_id;
                             $temp->table_id = 5;
                             $temp->save();
                         }
                     });
                     unlink($ext_ref_basicfailure[$i]);
                     unset($ext_ref_basicfailure[$i]);
                 }
             }
             $ex_basic_failure = glob("public/backup/import/*.csv");
             for ($i = 0; $i < count($ex_basic_failure); $i++) {
                 if (substr($ex_basic_failure[$i], 21, 3) == '002') {
                     Excel::filter('chunk')->load($ex_basic_failure[$i])->chunk(250, function ($results) use($project_id) {
                         foreach ($results as $row) {
                             $basic_failure = new BasicFailure();
                             $basic_failure->mode_id = $row->mode_id;
                             $basic_failure->cause_id = $row->cause_id;
                             $basic_failure->type_use = 2;
                             $basic_failure->created_by = Auth::user()->id;
                             $basic_failure->project_id = $project_id;
                             $basic_failure->save();
                             $temp = new RefTemp();
                             $temp->old_id = $row->id;
                             $temp->new_id = $basic_failure->id;
                             $temp->project_id = $project_id;
                             $temp->table_id = 12;
                             $temp->save();
                             $tempcause = DB::table('ref_template')->where('old_id', $basic_failure->cause_id)->where('table_id', 4)->where('project_id', $project_id)->first();
                             if (count($tempcause)) {
                                 BasicFailure::where('project_id', $project_id)->where('cause_id', $basic_failure->cause_id)->update(array('cause_id' => $tempcause->new_id));
                             }
                             $tempmode = DB::table('ref_template')->where('old_id', $basic_failure->mode_id)->where('table_id', 5)->where('project_id', $project_id)->first();
                             if (count($tempmode)) {
                                 BasicFailure::where('project_id', $project_id)->where('mode_id', $basic_failure->mode_id)->update(array('mode_id' => $tempmode->new_id));
                             }
                         }
                     });
                     unlink($ex_basic_failure[$i]);
                 }
             }
             $ext_ref_basictask = glob("public/backup/import/*.csv");
             for ($i = 0; $i < count($ext_ref_basictask); $i++) {
                 if (substr($ext_ref_basictask[$i], 21, 3) == '011') {
                     Excel::filter('chunk')->load($ext_ref_basictask[$i])->chunk(250, function ($results) use($project_id) {
                         foreach ($results as $row) {
                             $ref_task_list = new RefTaskList();
                             $ref_task_list->description = $row->description;
                             $ref_task_list->type_use = 2;
                             $ref_task_list->created_by = Auth::user()->id;
                             $ref_task_list->project_id = $project_id;
                             $ref_task_list->save();
                             $temp = new RefTemp();
                             $temp->old_id = $row->id;
                             $temp->new_id = $ref_task_list->id;
                             $temp->project_id = $project_id;
                             $temp->table_id = 7;
                             $temp->save();
                         }
                     });
                     unlink($ext_ref_basictask[$i]);
                 }
                 if (substr($ext_ref_basictask[$i], 21, 3) == '012') {
                     Excel::filter('chunk')->load($ext_ref_basictask[$i])->chunk(250, function ($results) use($project_id) {
                         foreach ($results as $row) {
                             $ref_task_type = new RefTaskType();
                             $ref_task_type->description = $row->description;
                             $ref_task_type->type_use = 2;
                             $ref_task_type->created_by = Auth::user()->id;
                             $ref_task_type->project_id = $project_id;
                             $ref_task_type->save();
                             $temp = new RefTemp();
                             $temp->old_id = $row->id;
                             $temp->new_id = $ref_task_type->id;
                             $temp->project_id = $project_id;
                             $temp->table_id = 6;
                             $temp->save();
                         }
                     });
                     unlink($ext_ref_basictask[$i]);
                 }
             }
             $ex_basic_task = glob("public/backup/import/*.csv");
             for ($i = 0; $i < count($ex_basic_task); $i++) {
                 if (substr($ex_basic_task[$i], 21, 3) == '003') {
                     Excel::filter('chunk')->load($ex_basic_task[$i])->chunk(250, function ($results) use($project_id) {
                         foreach ($results as $row) {
                             $basic_task = new BasicTask();
                             $basic_task->cause_id = $row->cause_id;
                             $basic_task->type_id = $row->type_id;
                             $basic_task->list_id = $row->list_id;
                             $basic_task->type_use = 2;
                             $basic_task->created_by = Auth::user()->id;
                             $basic_task->project_id = $project_id;
                             $basic_task->save();
                             $temp = new RefTemp();
                             $temp->old_id = $row->id;
                             $temp->new_id = $basic_task->id;
                             $temp->project_id = $project_id;
                             $temp->table_id = 13;
                             $temp->save();
                             $tempcause = DB::table('ref_template')->where('old_id', $basic_task->cause_id)->where('table_id', 4)->where('project_id', $project_id)->first();
                             BasicTask::where('project_id', $project_id)->where('cause_id', $basic_task->cause_id)->update(array('cause_id' => $tempcause->new_id));
                             $temptasktype = DB::table('ref_template')->where('old_id', $basic_task->type_id)->where('table_id', 6)->where('project_id', $project_id)->first();
                             BasicTask::where('project_id', $project_id)->where('type_id', $basic_task->type_id)->update(array('type_id' => $temptasktype->new_id));
                             $templist = DB::table('ref_template')->where('old_id', $basic_task->list_id)->where('table_id', 7)->where('project_id', $project_id)->first();
                             BasicTask::where('project_id', $project_id)->where('list_id', $basic_task->list_id)->update(array('list_id' => $templist->new_id));
                         }
                     });
                     unlink($ex_basic_task[$i]);
                     unset($ex_basic_task[$i]);
                 }
             }
             $re_other = glob("public/backup/import/*.csv");
             for ($i = 0; $i < count($re_other); $i++) {
                 if (substr($re_other[$i], 21, 3) == '010') {
                     Excel::filter('chunk')->load($re_other[$i])->chunk(250, function ($results) use($project_id) {
                         foreach ($results as $row) {
                             $ref_task_interval = new RefTaskInterval();
                             $ref_task_interval->interval = $row->interval;
                             $ref_task_interval->description = $row->description;
                             $ref_task_interval->type_use = 2;
                             $ref_task_interval->created_by = Auth::user()->id;
                             $ref_task_interval->project_id = $project_id;
                             $ref_task_interval->save();
                             $temp = new RefTemp();
                             $temp->old_id = $row->id;
                             $temp->new_id = $ref_task_interval->id;
                             $temp->project_id = $project_id;
                             $temp->table_id = 8;
                             $temp->save();
                         }
                     });
                     unlink($re_other[$i]);
                 } elseif (substr($re_other[$i], 21, 3) == '008') {
                     Excel::filter('chunk')->load($re_other[$i])->chunk(250, function ($results) use($project_id) {
                         foreach ($results as $row) {
                             $ref_order_type = new RefOrderType();
                             $ref_order_type->name = $row->name;
                             $ref_order_type->description = $row->description;
                             $ref_order_type->type_use = 2;
                             $ref_order_type->created_by = Auth::user()->id;
                             $ref_order_type->project_id = $project_id;
                             $ref_order_type->save();
                             $temp = new RefTemp();
                             $temp->old_id = $row->id;
                             $temp->new_id = $ref_order_type->id;
                             $temp->project_id = $project_id;
                             $temp->table_id = 9;
                             $temp->save();
                         }
                     });
                     unlink($re_other[$i]);
                 } elseif (substr($re_other[$i], 21, 3) == '007') {
                     Excel::filter('chunk')->load($re_other[$i])->chunk(250, function ($results) use($project_id) {
                         foreach ($results as $row) {
                             $ref_non = new RefNonCriticalQuestion();
                             $ref_non->questions = $row->questions;
                             $ref_non->type_use = 2;
                             $ref_non->created_by = Auth::user()->id;
                             $ref_non->project_id = $project_id;
                             $ref_non->save();
                             $temp = new RefTemp();
                             $temp->old_id = $row->id;
                             $temp->new_id = $ref_non->id;
                             $temp->project_id = $project_id;
                             $temp->table_id = 10;
                             $temp->save();
                         }
                     });
                     unlink($re_other[$i]);
                 }
             }
         }
         $ex_asset_register = glob("public/backup/import/*.csv");
         for ($i = 0; $i < count($ex_asset_register); $i++) {
             if (substr($ex_asset_register[$i], 21, 3) == '014') {
                 Excel::filter('chunk')->load($ex_asset_register[$i])->chunk(250, function ($results) use($project_id) {
                     foreach ($results as $row) {
                         $asset_registers = new AssetRegister();
                         if (!empty($row->parent)) {
                             $asset_registers->parent = $row->parent;
                         } else {
                             $asset_registers->parent = 0;
                         }
                         $asset_registers->asset_name = $row->asset_name;
                         $asset_registers->description = $row->description;
                         $asset_registers->cat_id = $row->cat_id;
                         $asset_registers->type_id = $row->type_id;
                         $asset_registers->level = $row->level;
                         $asset_registers->rpn = $row->rpn;
                         $asset_registers->drawing = $row->drawing;
                         $asset_registers->picture_path = $row->picture_path;
                         $asset_registers->severity = $row->severity;
                         $asset_registers->occur = $row->occur;
                         $asset_registers->detect = $row->detect;
                         $asset_registers->color = $row->color;
                         $asset_registers->complex_node = $row->complex_node;
                         $asset_registers->business_unit_type_colums = $row->business_unit_type_colums;
                         $asset_registers->created_by = Auth::user()->id;
                         $asset_registers->project_id = $project_id;
                         $asset_registers->save();
                         $temp = new RefTemp();
                         $temp->old_id = $row->id;
                         $temp->new_id = $asset_registers->id;
                         $temp->project_id = $project_id;
                         $temp->table_id = 14;
                         $temp->save();
                         $tempcate = DB::table('ref_template')->where('old_id', $asset_registers->cat_id)->where('table_id', 1)->where('project_id', $project_id)->first();
                         if (count($tempcate)) {
                             AssetRegister::where('project_id', $project_id)->where('cat_id', $asset_registers->cat_id)->where('id', $asset_registers->id)->update(array('cat_id' => $tempcate->new_id));
                         }
                         $temptype = DB::table('ref_template')->where('old_id', $asset_registers->type_id)->where('table_id', 2)->where('project_id', $project_id)->first();
                         if (count($temptype)) {
                             AssetRegister::where('project_id', $project_id)->where('type_id', $asset_registers->type_id)->where('id', $asset_registers->id)->update(array('type_id' => $temptype->new_id));
                         }
                         $tempcate = DB::table('ref_template')->where('old_id', $row->parent)->where('table_id', 14)->where('project_id', $project_id)->first();
                         if (count($tempcate)) {
                             AssetRegister::where('project_id', $project_id)->where('parent', $asset_registers->parent)->where('id', $asset_registers->id)->update(array('parent' => $tempcate->new_id));
                         }
                     }
                 });
                 unlink($ex_asset_register[$i]);
             }
         }
         $ex_asset_complex_detail = glob("public/backup/import/*.csv");
         for ($i = 0; $i < count($ex_asset_complex_detail); $i++) {
             if (substr($ex_asset_complex_detail[$i], 21, 3) == '015') {
                 Excel::filter('chunk')->load($ex_asset_complex_detail[$i])->chunk(250, function ($results) use($project_id) {
                     foreach ($results as $row) {
                         $asset_complex_detail = new AssetComplexDetail();
                         $asset_complex_detail->node = $row->node;
                         $asset_complex_detail->complex_id = $row->complex_id;
                         $asset_complex_detail->rows = $row->rows;
                         $asset_complex_detail->columns = $row->columns;
                         $asset_complex_detail->description = $row->description;
                         $asset_complex_detail->type = $row->type;
                         $asset_complex_detail->ref1 = $row->ref1;
                         $asset_complex_detail->ref_id = $row->ref_id;
                         $asset_complex_detail->question = $row->question;
                         //Yes /No
                         $asset_complex_detail->created_by = Auth::user()->id;
                         $asset_complex_detail->project_id = $project_id;
                         $asset_complex_detail->save();
                         $temp = new RefTemp();
                         $temp->old_id = $row->id;
                         $temp->new_id = $asset_complex_detail->id;
                         $temp->project_id = $project_id;
                         $temp->table_id = 15;
                         $temp->save();
                         $node = array();
                         $node = DB::table('ref_template')->where('old_id', $asset_complex_detail->node)->where('table_id', 14)->where('project_id', $project_id)->first();
                         if (count($node)) {
                             AssetComplexDetail::where('project_id', $project_id)->where('node', $asset_complex_detail->node)->where('id', $asset_complex_detail->id)->update(array('node' => $node->new_id));
                         }
                     }
                 });
                 unlink($ex_asset_complex_detail[$i]);
             }
         }
         $ex_asset_basic_failure = glob("public/backup/import/*.csv");
         for ($i = 0; $i < count($ex_asset_basic_failure); $i++) {
             if (substr($ex_asset_basic_failure[$i], 21, 3) == '016') {
                 Excel::filter('chunk')->load($ex_asset_basic_failure[$i])->chunk(250, function ($results) use($project_id) {
                     foreach ($results as $row) {
                         $asset_basic_failure = new AssetBasicFailure();
                         $asset_basic_failure->part_id = $row->part_id;
                         $asset_basic_failure->basic_failure_id = $row->basic_failure_id;
                         $asset_basic_failure->node = $row->node;
                         $asset_basic_failure->rpn = $row->rpn;
                         $asset_basic_failure->worst_case = $row->worst_case;
                         $asset_basic_failure->failure_effect_remark = $row->failure_effect_remark;
                         $asset_basic_failure->failure_effect = $row->failure_effect;
                         $asset_basic_failure->severity = $row->severity;
                         $asset_basic_failure->occur = $row->occur;
                         $asset_basic_failure->detect = $row->detect;
                         $asset_basic_failure->ref1 = $row->ref1;
                         $asset_basic_failure->ref2 = $row->ref2;
                         $asset_basic_failure->ref3 = $row->ref3;
                         $asset_basic_failure->ref4 = $row->ref4;
                         $asset_basic_failure->ref5 = $row->ref5;
                         $asset_basic_failure->ref6 = $row->ref6;
                         $asset_basic_failure->ref7 = $row->ref7;
                         $asset_basic_failure->ref8 = $row->ref8;
                         $asset_basic_failure->color = $row->color;
                         $asset_basic_failure->created_by = Auth::user()->id;
                         $asset_basic_failure->project_id = $project_id;
                         $asset_basic_failure->save();
                         $temp = new RefTemp();
                         $temp->old_id = $row->id;
                         $temp->new_id = $asset_basic_failure->id;
                         $temp->project_id = $project_id;
                         $temp->table_id = 16;
                         $temp->save();
                         $node = array();
                         $node = DB::table('ref_template')->where('old_id', $asset_basic_failure->node)->where('table_id', 14)->where('project_id', $project_id)->first();
                         if (count($node)) {
                             AssetBasicFailure::where('project_id', $project_id)->where('node', $asset_basic_failure->node)->where('id', $asset_basic_failure->id)->update(array('node' => $node->new_id));
                         }
                         $ref_part = array();
                         $ref_part = DB::table('ref_template')->where('old_id', $asset_basic_failure->part_id)->where('table_id', 3)->where('project_id', $project_id)->first();
                         if (count($ref_part)) {
                             AssetBasicFailure::where('project_id', $project_id)->where('part_id', $asset_basic_failure->part_id)->where('id', $asset_basic_failure->id)->update(array('part_id' => $ref_part->new_id));
                         }
                         $basic_failure = array();
                         $basic_failure = DB::table('ref_template')->where('old_id', $asset_basic_failure->basic_failure_id)->where('table_id', 12)->where('project_id', $project_id)->first();
                         if (count($basic_failure)) {
                             AssetBasicFailure::where('project_id', $project_id)->where('basic_failure_id', $asset_basic_failure->basic_failure_id)->where('id', $asset_basic_failure->id)->update(array('basic_failure_id' => $basic_failure->new_id));
                         }
                     }
                 });
                 unlink($ex_asset_basic_failure[$i]);
             }
         }
         $ex_asset_questions = glob("public/backup/import/*.csv");
         for ($i = 0; $i < count($ex_asset_questions); $i++) {
             if (substr($ex_asset_questions[$i], 21, 3) == '017') {
                 Excel::filter('chunk')->load($ex_asset_questions[$i])->chunk(250, function ($results) use($project_id) {
                     foreach ($results as $row) {
                         $asset_questions = new AssetQuestion();
                         $asset_questions->asset_basic_failure_id = $row->asset_basic_failure_id;
                         $asset_questions->questions = $row->questions;
                         $asset_questions->answers = $row->answers;
                         $asset_questions->created_by = Auth::user()->id;
                         $asset_questions->project_id = $project_id;
                         $asset_questions->save();
                         $temp = new RefTemp();
                         $temp->old_id = $row->id;
                         $temp->new_id = $asset_questions->id;
                         $temp->project_id = $project_id;
                         $temp->table_id = 17;
                         $temp->save();
                         $asset_basic_failure = array();
                         $asset_basic_failure = DB::table('ref_template')->where('old_id', $asset_questions->asset_basic_failure_id)->where('table_id', 16)->where('project_id', $project_id)->first();
                         if (count($asset_basic_failure)) {
                             AssetQuestion::where('project_id', $project_id)->where('asset_basic_failure_id', $asset_questions->asset_basic_failure_id)->where('id', $asset_questions->id)->update(array('asset_basic_failure_id' => $asset_basic_failure->new_id));
                         }
                     }
                 });
                 unlink($ex_asset_questions[$i]);
             }
         }
         $ex_task_selection = glob("public/backup/import/*.csv");
         for ($i = 0; $i < count($ex_task_selection); $i++) {
             if (substr($ex_task_selection[$i], 21, 3) == '018') {
                 Excel::filter('chunk')->load($ex_task_selection[$i])->chunk(250, function ($results) use($project_id) {
                     foreach ($results as $row) {
                         $task_selection = new TaskSelection();
                         $task_selection->node = $row->node;
                         $task_selection->asset_basic_failure_id = $row->asset_basic_failure_id;
                         $task_selection->failure_effect_id = $row->failure_effect_id;
                         $task_selection->evident_id = $row->evident_id;
                         $task_selection->order_type_id = $row->order_type_id;
                         $task_selection->interval_num = $row->interval_num;
                         $task_selection->interval = $row->interval;
                         $task_selection->basic_task_id = $row->basic_task_id;
                         $task_selection->activity_status_id = $row->activity_status_id;
                         $task_selection->activity_detail = $row->activity_detail;
                         $task_selection->created_by = Auth::user()->id;
                         $task_selection->project_id = $project_id;
                         $task_selection->save();
                         $temp = new RefTemp();
                         $temp->old_id = $row->task_selection_id;
                         $temp->new_id = $task_selection->id;
                         $temp->project_id = $project_id;
                         $temp->table_id = 18;
                         $temp->save();
                         $asset_registers = array();
                         $asset_registers = DB::table('ref_template')->where('old_id', $row->node)->where('table_id', 14)->where('project_id', $project_id)->first();
                         if (count($asset_registers)) {
                             TaskSelection::where('project_id', $project_id)->where('node', $row->node)->where('task_selection_id', $task_selection->id)->update(array('node' => $asset_registers->new_id));
                         }
                         $asset_basic_failure = array();
                         $asset_basic_failure = DB::table('ref_template')->where('old_id', $row->asset_basic_failure_id)->where('table_id', 16)->where('project_id', $project_id)->first();
                         if (count($asset_basic_failure)) {
                             TaskSelection::where('project_id', $project_id)->where('asset_basic_failure_id', $row->asset_basic_failure_id)->where('task_selection_id', $task_selection->id)->update(array('asset_basic_failure_id' => $asset_basic_failure->new_id));
                         }
                         $ref_order_type = array();
                         $ref_order_type = DB::table('ref_template')->where('old_id', $row->order_type_id)->where('table_id', 9)->where('project_id', $project_id)->first();
                         if (count($ref_order_type)) {
                             TaskSelection::where('project_id', $project_id)->where('order_type_id', $row->order_type_id)->where('task_selection_id', $task_selection->id)->update(array('order_type_id' => $ref_order_type->new_id));
                         }
                         $basic_task = array();
                         $basic_task = DB::table('ref_template')->where('old_id', $task_selection->basic_task_id)->where('table_id', 13)->where('project_id', $project_id)->first();
                         if (count($basic_task)) {
                             TaskSelection::where('project_id', $project_id)->where('basic_task_id', $task_selection->basic_task_id)->where('task_selection_id', $task_selection->id)->update(array('basic_task_id' => $basic_task->new_id));
                         }
                     }
                 });
                 unlink($ex_task_selection[$i]);
             }
         }
         $ex_package_assum = glob("public/backup/import/*.csv");
         for ($i = 0; $i < count($ex_package_assum); $i++) {
             if (substr($ex_package_assum[$i], 21, 3) == '019') {
                 Excel::filter('chunk')->load($ex_package_assum[$i])->chunk(250, function ($results) use($project_id) {
                     foreach ($results as $row) {
                         $package_assumptions = new PackageAssumption();
                         $package_assumptions->name = $row->name;
                         $package_assumptions->description = $row->description;
                         $package_assumptions->created_by = Auth::user()->id;
                         $package_assumptions->project_id = $project_id;
                         $package_assumptions->save();
                         $temp = new RefTemp();
                         $temp->old_id = $row->id;
                         $temp->new_id = $package_assumptions->id;
                         $temp->project_id = $project_id;
                         $temp->table_id = 19;
                         $temp->save();
                     }
                 });
                 unlink($ex_package_assum[$i]);
             }
         }
         DB::table('ref_template')->delete();
         return Redirect::to('project')->with('message', 'imported project successfully.');
     }
     return Redirect::to('project')->withErrors($validator)->withInput();
 }
 public function validateuniquetask($data = array())
 {
     $rs = array();
     foreach ($data as $row) {
         $rs = BasicTask::where('project_id', Session::get('project_id'))->where('active', 1)->where('type_id', $row->task_type)->where('list_id', $row->task_list)->where('cause_id', $row->failure_cause)->get();
         if (!$rs->isEmpty()) {
             return false;
         } else {
             return true;
         }
     }
 }
 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();
 }