Esempio n. 1
0
 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 postImportasset()
 {
     $input = array('file' => Input::file('import'));
     $rules = array('file' => 'required|max:50000');
     $validator = Validator::make($input, $rules);
     if ($validator->passes()) {
         $destinationPath = 'public/import/asset';
         $extension = Input::file('import')->getClientOriginalExtension();
         $fileName = rand(11111, 99999) . '.' . $extension;
         $import = Input::file('import')->move($destinationPath, $fileName);
         Excel::load($import->getPathName(), function ($reader) {
             $reader->each(function ($sheet) {
                 $sheet->each(function ($row) {
                     if (!empty($row->level1)) {
                         $level1 = new AssetRegister();
                         $level1->asset_name = $row->level1;
                         $level1->parent = 0;
                         $level1->level = 1;
                         $level1->active = 2;
                         $level1->project_id = Session::get('project_id');
                         $level1->save();
                     }
                     if (!empty($row->level2)) {
                         $arr_level2 = explode(',', $row->level2);
                         if (!empty($arr_level2[1])) {
                             $bss_unit = DB::table('complex_detail_default')->where('type', '=', 6)->whereIn('columns', [1, 2, 3])->where('description', 'like', $arr_level2[1])->first();
                             if (count($bss_unit)) {
                                 $parent = DB::table('asset_registers')->where('active', '=', 2)->where('level', '=', 1)->where('project_id', '=', Session::get('project_id'))->where('asset_name', '=', $arr_level2[2])->first();
                                 if (count($parent)) {
                                     $level2 = new AssetRegister();
                                     $level2->asset_name = $arr_level2[0];
                                     $level2->level = 2;
                                     $level2->active = 2;
                                     $level2->project_id = Session::get('project_id');
                                     $level2->business_unit_type_colums = $bss_unit->columns;
                                     $level2->parent = $parent->id;
                                     $level2->save();
                                 } else {
                                     DB::table('asset_registers')->where('active', '=', 2)->delete();
                                     Session::flash('message_import', 'asset hierarchy level not found !');
                                     return Redirect::to('asset-register/l8detail');
                                 }
                             } else {
                                 DB::table('asset_registers')->where('active', '=', 2)->delete();
                                 Session::flash('message_import', 'business unit type not found : ' . $arr_level2[1] . '!');
                                 return Redirect::to('asset-register/l8detail');
                             }
                         }
                     }
                     if (!empty($row->level3)) {
                         $arr_level3 = explode(',', $row->level3);
                         if (!empty($arr_level3[1])) {
                             $parent = DB::table('asset_registers')->where('active', '=', 2)->where('level', '=', 2)->where('project_id', '=', Session::get('project_id'))->where('asset_name', '=', $arr_level3[1])->first();
                             if (count($parent) > 0) {
                                 $level3 = new AssetRegister();
                                 $level3->asset_name = $arr_level3[0];
                                 $level3->level = 3;
                                 $level3->active = 2;
                                 $level3->project_id = Session::get('project_id');
                                 $level3->parent = $parent->id;
                                 $level3->save();
                             } else {
                                 DB::table('asset_registers')->where('active', '=', 2)->delete();
                                 Session::flash('message_import', 'asset hierarchy level not found !');
                                 return Redirect::to('asset-register/l8detail');
                             }
                         }
                     }
                     if (!empty($row->level4)) {
                         $arr_level4 = explode(',', $row->level4);
                         if (!empty($arr_level4[1])) {
                             $parent = DB::table('asset_registers')->where('active', '=', 2)->where('level', '=', 3)->where('project_id', '=', Session::get('project_id'))->where('asset_name', '=', $arr_level4[1])->first();
                             if (count($parent) > 0) {
                                 $level4 = new AssetRegister();
                                 $level4->asset_name = $arr_level4[0];
                                 $level4->level = 4;
                                 $level4->active = 2;
                                 $level4->project_id = Session::get('project_id');
                                 $level4->parent = $parent->id;
                                 $level4->save();
                             } else {
                                 DB::table('asset_registers')->where('active', '=', 2)->delete();
                                 Session::flash('message_import', 'asset hierarchy level not found !');
                                 return Redirect::to('asset-register/l8detail');
                             }
                         }
                     }
                     if (!empty($row->level5)) {
                         $arr_level5 = explode(',', $row->level5);
                         if (!empty($arr_level5[1])) {
                             $parent = DB::table('asset_registers')->where('active', '=', 2)->where('level', '=', 4)->where('project_id', '=', Session::get('project_id'))->where('asset_name', '=', $arr_level5[1])->first();
                             print_r($parent);
                             if (count($parent) > 0) {
                                 $level5 = new AssetRegister();
                                 $level5->asset_name = $arr_level5[0];
                                 $level5->level = 5;
                                 $level5->active = 2;
                                 $level5->project_id = Session::get('project_id');
                                 $level5->parent = $parent->id;
                                 $level5->save();
                             } else {
                                 DB::table('asset_registers')->where('active', '=', 2)->delete();
                                 Session::flash('message_import', 'asset hierarchy level not found !');
                                 return Redirect::to('asset-register/l8detail');
                             }
                         }
                     }
                     if (!empty($row->level6)) {
                         $arr_level6 = explode(',', $row->level6);
                         if (!empty($arr_level6[1])) {
                             $parent = DB::table('asset_registers')->where('active', '=', 2)->where('level', '=', 5)->where('project_id', '=', Session::get('project_id'))->where('asset_name', '=', $arr_level6[1])->first();
                             if (count($parent)) {
                                 $level6 = new AssetRegister();
                                 $level6->asset_name = $arr_level6[0];
                                 $level6->level = 6;
                                 $level6->active = 2;
                                 $level6->project_id = Session::get('project_id');
                                 $level6->parent = $parent->id;
                                 $level6->save();
                             } else {
                                 DB::table('asset_registers')->where('active', '=', 2)->delete();
                                 Session::flash('message_import', 'asset hierarchy level not found !');
                                 return Redirect::to('asset-register/l8detail');
                             }
                         }
                     }
                     if (!empty($row->level7)) {
                         $arr_level7 = explode(',', $row->level7);
                         if (!empty($arr_level7[1])) {
                             $parent = DB::table('asset_registers')->where('active', '=', 2)->where('level', '=', 6)->where('project_id', '=', Session::get('project_id'))->where('asset_name', '=', $arr_level7[1])->first();
                             if (count($parent) > 0) {
                                 $level7 = new AssetRegister();
                                 $level7->asset_name = $arr_level7[0];
                                 $level7->level = 7;
                                 $level7->active = 2;
                                 $level7->project_id = Session::get('project_id');
                                 $level7->parent = $parent->id;
                                 $level7->save();
                             } else {
                                 DB::table('asset_registers')->where('active', '=', 2)->delete();
                                 Session::flash('message_import', 'asset hierarchy level not found !');
                                 return Redirect::to('asset-register/l8detail');
                             }
                         }
                     }
                     if (!empty($row->level8)) {
                         $arr_level8 = explode(',', $row->level8);
                         if (!empty($arr_level8[1])) {
                             $parent = DB::table('asset_registers')->where('active', '=', 2)->where('level', '=', 7)->where('project_id', '=', Session::get('project_id'))->where('asset_name', '=', $arr_level8[3])->first();
                             if (count($parent)) {
                                 $level8 = new AssetRegister();
                                 $level8->asset_name = $arr_level8[0];
                                 $level8->level = 8;
                                 $level8->active = 2;
                                 $level8->project_id = Session::get('project_id');
                                 $level8->parent = $parent->id;
                                 $category = DB::table('ref_categories')->where('active', '=', 1)->where('description', '=', $arr_level8[1])->where('project_id', '=', Session::get('project_id'))->first();
                                 if (count($category)) {
                                     $level8->cat_id = $category->id;
                                 } else {
                                     DB::table('asset_registers')->where('active', '=', 2)->delete();
                                     Session::flash('message_import', 'categories not found !');
                                     return Redirect::to('asset-register/l8detail');
                                 }
                                 $type = DB::table('ref_types')->where('active', '=', 1)->where('description', '=', $arr_level8[2])->where('project_id', '=', Session::get('project_id'))->first();
                                 if (count($type)) {
                                     $level8->type_id = $type->id;
                                 } else {
                                     DB::table('asset_registers')->where('active', '=', 2)->delete();
                                     Session::flash('message_import', 'type not found !');
                                     return Redirect::to('asset-register/l8detail');
                                 }
                                 $level8->save();
                             } else {
                                 DB::table('asset_registers')->where('active', '=', 2)->delete();
                                 Session::flash('message_import', 'asset hierarchy level not found !');
                                 return Redirect::to('asset-register/l8detail');
                             }
                         }
                     }
                 });
             });
         });
         DB::table('asset_registers')->where('active', 2)->update(array('active' => 1));
         // Session::flash('message', 'import asset hierarchy level successfully !');
         unlink($import->getPathName());
         return Redirect::to('asset-register/l8detail');
     }
     return Redirect::to('asset-register/l8detail')->withErrors($validator)->withInput();
 }