Esempio n. 1
0
 public function postDupproject()
 {
     $validator = Validator::make(Input::all(), Project::$rules);
     if ($validator->passes()) {
         $project = Project::where('created_by', '=', Auth::user()->id)->where('name', '=', Input::get('name'))->where('active', '=', 1)->get();
         if (!$project->isEmpty()) {
             return Redirect::to('project')->with('message_error', 'Project duplicate !');
         } else {
             $project = new Project();
             $project->name = Input::get('name');
             $project->member_id = Auth::user()->id;
             $project->created_by = Auth::user()->id;
             $project->save();
             $ref_cate = RefCategory::where('project_id', Input::get('project_id'))->where('active', 1)->where('type_use', 2)->get();
             if ($ref_cate) {
                 foreach ($ref_cate as $non) {
                     $ref_cate = RefCategory::find($non->id);
                     $clone = $ref_cate->replicate();
                     unset($clone['created_by'], $clone['updated_by'], $clone['project_id']);
                     $clone['project_id'] = $project->id;
                     $clone['created_by'] = Auth::user()->id;
                     $data = json_decode($clone, true);
                     $insert_id = RefCategory::create($data);
                     $temp = new RefTemp();
                     $temp->old_id = $non->id;
                     $temp->new_id = $insert_id->id;
                     $temp->project_id = $project->id;
                     $temp->table_id = 1;
                     $temp->save();
                 }
             }
             $ref_type = RefType::where('project_id', Input::get('project_id'))->where('active', 1)->where('type_use', 2)->get();
             if ($ref_type) {
                 foreach ($ref_type as $non) {
                     $ref_type = RefType::find($non->id);
                     $clone = $ref_type->replicate();
                     unset($clone['created_by'], $clone['updated_by'], $clone['project_id']);
                     $clone['project_id'] = $project->id;
                     $clone['created_by'] = Auth::user()->id;
                     $data = json_decode($clone, true);
                     $insert_id = RefType::create($data);
                     $temp = new RefTemp();
                     $temp->old_id = $non->id;
                     $temp->new_id = $insert_id->id;
                     $temp->project_id = $project->id;
                     $temp->table_id = 2;
                     $temp->save();
                 }
             }
             $ref_part = RefPart::where('project_id', Input::get('project_id'))->where('active', 1)->where('type_use', 2)->get();
             if ($ref_part) {
                 foreach ($ref_part as $non) {
                     $ref_part = RefPart::find($non->id);
                     $clone = $ref_part->replicate();
                     unset($clone['created_by'], $clone['updated_by'], $clone['project_id']);
                     $clone['project_id'] = $project->id;
                     $clone['created_by'] = Auth::user()->id;
                     $data = json_decode($clone, true);
                     $insert_id = RefPart::create($data);
                     $temp = new RefTemp();
                     $temp->old_id = $non->id;
                     $temp->new_id = $insert_id->id;
                     $temp->project_id = $project->id;
                     $temp->table_id = 3;
                     $temp->save();
                 }
             }
             $basic_equipment = BasicEquipment::where('project_id', Input::get('project_id'))->where('active', 1)->where('type_use', 2)->get();
             if ($basic_equipment) {
                 foreach ($basic_equipment as $non) {
                     $basic_equipment = BasicEquipment::find($non->id);
                     $clone = $basic_equipment->replicate();
                     unset($clone['created_by'], $clone['updated_by'], $clone['project_id']);
                     $clone['project_id'] = $project->id;
                     $clone['created_by'] = Auth::user()->id;
                     $data = json_decode($clone, true);
                     $insert = BasicEquipment::create($data);
                     $tempcat = DB::table('ref_template')->where('old_id', $insert->category_id)->where('table_id', 1)->where('project_id', $project->id)->first();
                     BasicEquipment::where('project_id', $project->id)->where('category_id', $insert->category_id)->update(array('category_id' => $tempcat->new_id));
                     $temptype = DB::table('ref_template')->where('old_id', $insert->type_id)->where('table_id', 2)->where('project_id', $project->id)->first();
                     BasicEquipment::where('project_id', $project->id)->where('type_id', $insert->type_id)->update(array('type_id' => $temptype->new_id));
                     $temppart = DB::table('ref_template')->where('old_id', $insert->part_id)->where('table_id', 3)->where('project_id', $project->id)->first();
                     BasicEquipment::where('project_id', $project->id)->where('part_id', $insert->part_id)->update(array('part_id' => $temppart->new_id));
                 }
                 // DB::table('ref_template')->delete();
             }
             $ref_fail_cause = RefFailureCause::where('project_id', Input::get('project_id'))->where('active', 1)->where('type_use', 2)->get();
             if ($ref_fail_cause) {
                 foreach ($ref_fail_cause as $non) {
                     $ref_fail_cause = RefFailureCause::find($non->id);
                     $clone = $ref_fail_cause->replicate();
                     unset($clone['created_by'], $clone['updated_by'], $clone['project_id']);
                     $clone['project_id'] = $project->id;
                     $clone['created_by'] = Auth::user()->id;
                     $data = json_decode($clone, true);
                     $insert_id = RefFailureCause::create($data);
                     $temp = new RefTemp();
                     $temp->old_id = $non->id;
                     $temp->new_id = $insert_id->id;
                     $temp->project_id = $project->id;
                     $temp->table_id = 4;
                     $temp->save();
                 }
             }
             $ref_fail_mode = RefFailureMode::where('project_id', Input::get('project_id'))->where('active', 1)->where('type_use', 2)->get();
             if ($ref_fail_mode) {
                 foreach ($ref_fail_mode as $non) {
                     $ref_fail_mode = RefFailureMode::find($non->id);
                     $clone = $ref_fail_mode->replicate();
                     unset($clone['created_by'], $clone['updated_by'], $clone['project_id']);
                     $clone['project_id'] = $project->id;
                     $clone['created_by'] = Auth::user()->id;
                     $data = json_decode($clone, true);
                     $insert_id = RefFailureMode::create($data);
                     $temp = new RefTemp();
                     $temp->old_id = $non->id;
                     $temp->new_id = $insert_id->id;
                     $temp->project_id = $project->id;
                     $temp->table_id = 5;
                     $temp->save();
                 }
             }
             $basic_failure = BasicFailure::where('project_id', Input::get('project_id'))->where('active', 1)->where('type_use', 2)->get();
             if ($basic_failure) {
                 foreach ($basic_failure as $non) {
                     $basic_failure = BasicFailure::find($non->id);
                     $clone = $basic_failure->replicate();
                     unset($clone['created_by'], $clone['updated_by'], $clone['project_id']);
                     $clone['project_id'] = $project->id;
                     $clone['created_by'] = Auth::user()->id;
                     $data = json_decode($clone, true);
                     $insert = BasicFailure::create($data);
                     $tempcause = DB::table('ref_template')->where('old_id', $insert->cause_id)->where('table_id', 4)->where('project_id', $project->id)->first();
                     BasicFailure::where('project_id', $project->id)->where('cause_id', $insert->cause_id)->update(array('cause_id' => $tempcause->new_id));
                     $tempmode = DB::table('ref_template')->where('old_id', $insert->mode_id)->where('table_id', 5)->where('project_id', $project->id)->first();
                     BasicFailure::where('project_id', $project->id)->where('mode_id', $insert->mode_id)->update(array('mode_id' => $tempmode->new_id));
                 }
             }
             $ref_task_type = RefTaskType::where('project_id', Input::get('project_id'))->where('active', 1)->where('type_use', 2)->get();
             if ($ref_task_type) {
                 foreach ($ref_task_type as $non) {
                     $ref_task_type = RefTaskType::find($non->id);
                     $clone = $ref_task_type->replicate();
                     unset($clone['created_by'], $clone['updated_by'], $clone['project_id']);
                     $clone['project_id'] = $project->id;
                     $clone['created_by'] = Auth::user()->id;
                     $data = json_decode($clone, true);
                     $insert_id = RefTaskType::create($data);
                     $temp = new RefTemp();
                     $temp->old_id = $non->id;
                     $temp->new_id = $insert_id->id;
                     $temp->project_id = $project->id;
                     $temp->table_id = 6;
                     $temp->save();
                 }
             }
             $ref_task_list = RefTaskList::where('project_id', Input::get('project_id'))->where('active', 1)->where('type_use', 2)->get();
             if ($ref_task_list) {
                 foreach ($ref_task_list as $non) {
                     $ref_task_list = RefTaskList::find($non->id);
                     $clone = $ref_task_list->replicate();
                     unset($clone['created_by'], $clone['updated_by'], $clone['project_id']);
                     $clone['project_id'] = $project->id;
                     $clone['created_by'] = Auth::user()->id;
                     $data = json_decode($clone, true);
                     RefTaskList::create($data);
                     $temp = new RefTemp();
                     $temp->old_id = $non->id;
                     $temp->new_id = $insert_id->id;
                     $temp->project_id = $project->id;
                     $temp->table_id = 7;
                     $temp->save();
                 }
             }
             $basic_task = BasicTask::where('project_id', Input::get('project_id'))->where('active', 1)->where('type_use', 2)->get();
             if ($basic_task) {
                 foreach ($basic_task as $non) {
                     $basic_task = BasicTask::find($non->id);
                     $clone = $basic_task->replicate();
                     unset($clone['created_by'], $clone['updated_by'], $clone['project_id']);
                     $clone['project_id'] = $project->id;
                     $clone['created_by'] = Auth::user()->id;
                     $data = json_decode($clone, true);
                     $insert = BasicTask::create($data);
                     $tempcause = DB::table('ref_template')->where('old_id', $insert->cause_id)->where('table_id', 4)->where('project_id', $project->id)->first();
                     BasicTask::where('project_id', $project->id)->where('cause_id', $insert->cause_id)->update(array('cause_id' => $tempcause->new_id));
                     $temptasktype = DB::table('ref_template')->where('old_id', $insert->type_id)->where('table_id', 6)->where('project_id', $project->id)->first();
                     BasicTask::where('project_id', $project->id)->where('type_id', $insert->type_id)->update(array('type_id' => $temptasktype->new_id));
                     $templist = DB::table('ref_template')->where('old_id', $insert->list_id)->where('table_id', 7)->where('project_id', $project->id)->first();
                     BasicTask::where('project_id', $project->id)->where('list_id', $insert->list_id)->update(array('list_id' => $templist->new_id));
                 }
             }
             $ref_task_interval = RefTaskInterval::where('project_id', Input::get('project_id'))->where('active', 1)->where('type_use', 2)->get();
             if ($ref_task_interval) {
                 foreach ($ref_task_interval as $non) {
                     $ref_task_interval = RefTaskInterval::find($non->id);
                     $clone = $ref_task_interval->replicate();
                     unset($clone['created_by'], $clone['updated_by'], $clone['project_id']);
                     $clone['project_id'] = $project->id;
                     $clone['created_by'] = Auth::user()->id;
                     $data = json_decode($clone, true);
                     RefTaskInterval::create($data);
                 }
             }
             $ref_non_critical_question = RefNonCriticalQuestion::where('project_id', Input::get('project_id'))->where('active', 1)->where('type_use', 2)->get();
             if ($ref_non_critical_question) {
                 foreach ($ref_non_critical_question as $non) {
                     $ref_non_critical_question = RefNonCriticalQuestion::find($non->id);
                     $clone = $ref_non_critical_question->replicate();
                     unset($clone['created_by'], $clone['updated_by'], $clone['project_id']);
                     $clone['project_id'] = $project->id;
                     $clone['created_by'] = Auth::user()->id;
                     $data = json_decode($clone, true);
                     RefNonCriticalQuestion::create($data);
                 }
             }
             $ref_order_type = RefOrderType::where('project_id', Input::get('project_id'))->where('active', 1)->where('type_use', 2)->get();
             if ($ref_order_type) {
                 foreach ($ref_order_type as $non) {
                     $ref_order_type = RefOrderType::find($non->id);
                     $clone = $ref_order_type->replicate();
                     unset($clone['created_by'], $clone['updated_by'], $clone['project_id']);
                     $clone['project_id'] = $project->id;
                     $clone['created_by'] = Auth::user()->id;
                     $data = json_decode($clone, true);
                     RefOrderType::create($data);
                 }
             }
             DB::table('ref_template')->delete();
             Session::put('project_id', $project->id);
             Session::put('project', $project->name);
             return Redirect::to('project')->withSuccess('Duplicate Project Success.');
         }
     }
     return Redirect::to('project')->withErrors($validator)->withInput();
 }