/** * Update Events from CRM */ public function onUpdateFromCrm() { // echo "\nProcessing Events: \n"; $crmevents = CRMEvent::isNotUpdated()->with(['course'])->get(); if (count($crmevents) > 0) { $crmevents->each(function ($event) { $cmsevent = Event::firstOrNew(['crm_event_id' => $event->id]); $cmsevent->fill($event->toArray()); // print_r($cmsevent->toArray()); $cmscourse = Course::where('crm_course_id', $event->course->id)->first(); // $cmstypes = CMSType::whereIn('crm_type_id', $event->types->pluck('id'))->get(); // $cmscategories = CMSCategory::whereIn('crm_category_id', $event->categories->pluck('id'))->get(); // \CW::info(['event' => $event->id]); // \CW::info(['cmscourse' => $cmscourse]); // \CW::info(['cmsevent' => $cmsevent]); // \CW::info(['type' => $cmscourse->type]); if ($cmscourse && $cmscourse->type && $cmscourse->category) { $cmsevent->course()->associate($cmscourse); $cmsevent->type()->associate($cmscourse->type); // $cmsevent->category()->associate($cmscourse->category); $cmsevent->is_updated = true; $event->update(['is_updated' => true]); // $cmsevent = $cmscourse->events()->save($cmsevent); $cmsevent->save(); } }); Flash::success("Events is update"); } else { Flash::info("No Events to Create/Update"); } }
/** * Execute the console command. * @return void */ public function fire() { // $cmstypes = CMSType::all(); echo "\nProcessing Types: \n"; $crmtypes = CRMType::isNotUpdated()->get(); if (count($crmtypes) > 0) { $crmtypes->each(function ($type) { $cmstype = CMSType::firstOrNew(['crm_type_id' => $type->id]); $cmstype->fill(['title' => $type->title]); $cmstype->is_updated = true; $type->is_updated = true; $cmstype->save(); $type->save(); echo " [^]\n"; }); echo "\n"; } else { $this->info(" No Types for Create/Update"); } echo "\nProcessing Categories: \n"; $crmcategories = CRMCategory::isNotUpdated()->get(); if (count($crmcategories) > 0) { $crmcategories->each(function ($category) { $cmscategory = CMSCategory::firstOrNew(['crm_category_id' => $category->id]); $cmscategory->fill(['title' => $category->title]); $cmscategory->is_updated = true; $category->is_updated = true; $cmscategory->save(); $category->save(); echo " [^]\n"; }); echo "\n"; } else { $this->info(" No Categories for Create/Update"); } echo "\nProcessing Courses: \n"; $crmcourses = CRMCourse::isNotUpdated()->with(['events', 'types', 'categories'])->get(); if (count($crmcourses) > 0) { $crmcourses->each(function ($course) { $cmscourse = CMSCourse::firstOrNew(['crm_course_id' => $course->id]); $cmscourse->fill($course->toArray()); $cmscourse->is_updated = true; $course->update(['is_updated' => true]); $cmscourse->save(); foreach ($course->types->toArray() as $type) { $cmstype = CMSType::where('crm_type_id', $type['id'])->first(); if (!is_null($cmstype) && !$cmscourse->types->contains($cmstype->id)) { $course->types()->updateExistingPivot($type['id'], ['is_updated' => true]); $cmscourse->types()->attach($cmstype->id, ['is_updated' => true]); } } foreach ($course->categories->toArray() as $category) { $cmscategory = CMSCategory::where('crm_category_id', $category['id'])->first(); if (!is_null($cmscategory) && !$cmscourse->categories->contains($cmscategory->id)) { $course->categories()->updateExistingPivot($category['id'], ['is_updated' => true]); $cmscourse->categories()->attach($cmscategory->id, ['is_updated' => true]); } } echo " [+]\n"; }); echo "\n"; } else { $this->info(" No Courses for Create/Update"); } echo "\nProcessing Events: \n"; $crmevents = CRMEvent::isNotUpdated()->with(['course', 'types', 'categories'])->get(); if (count($crmevents) > 0) { $crmevents->each(function ($event) { $cmsevent = CMSEvent::firstOrNew(['crm_event_id' => $event->id]); $cmsevent->fill($event->toArray()); print_r($cmsevent->toArray()); $cmscourse = CMSCourse::where('crm_course_id', $event->course->id)->first(); // $cmstypes = CMSType::whereIn('crm_type_id', $event->types->pluck('id'))->get(); // $cmscategories = CMSCategory::whereIn('crm_category_id', $event->categories->pluck('id'))->get(); $cmsevent->course()->associate($cmscourse); $cmsevent->type()->associate($cmscourse->types->first()); $cmsevent->category()->associate($cmscourse->categories->first()); $cmsevent->is_updated = true; $event->update(['is_updated' => true]); // $cmsevent = $cmscourse->events()->save($cmsevent); $cmsevent->save(); echo " [+]\n"; }); echo "\n"; } else { $this->info(" No Events to Create/Update"); } // echo "\nProcessing Courses Types: \n"; // $_test = CRMType::courses()->get(); // print_r($_test->toArray()); // $crmcourses = CRMCourse::find(1)->types()->where('types.is_updated', '=', true)->get(); // if (count($crmcourses) > 0) { // print_r($crmcourses->toArray()); // } else { // $this->info(" No Courses Types to Create/Update"); // } // $crmcourses = CRMCourse::with(['types'])->whereHas('types', function ($q) { // // $q->wherePivot('is_updated', '=', false); // $q // ->where('type_id', '=', 1) // // ->withPivot('is_updated') // ->wherePivot('is_updated', '=', true) // ; // })->get(); // if (count($crmcourses) > 0) { // print_r($crmcourses->toArray()); // } else { // $this->info(" No Courses Types to Create/Update"); // } }