/** * 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"); } }
/** * Update Courses from CRM */ public function onUpdateFromCrm() { // Flash::info("Processing Courses…"); $crmcourses = CRMCourse::isNotUpdated()->with(['events', 'types', 'categories'])->get(); if (count($crmcourses) > 0) { $crmcourses->each(function ($course) { $cmscourse = Course::firstOrNew(['crm_course_id' => $course->id]); $cmscourse->fill($course->toArray()); $cmscourse->is_updated = true; $course->update(['is_updated' => true]); foreach ($course->types->toArray() as $type) { $cmstype = CourseType::where('crm_type_id', $type['id'])->first(); if (!is_null($cmstype)) { $course->types()->updateExistingPivot($type['id'], ['is_updated' => true]); $cmscourse->type()->associate($cmstype); } } foreach ($course->categories->toArray() as $category) { $cmscategory = CourseCategory::where('crm_category_id', $category['id'])->first(); if (!is_null($cmscategory)) { $course->categories()->updateExistingPivot($category['id'], ['is_updated' => true]); $cmscourse->category()->associate($cmscategory); } } $cmscourse->save(); // foreach ($course->types->toArray() as $type) { // $cmstype = CourseType::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 = CourseCategory::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"; Flash::success("Courses is update"); } else { Flash::info("No Courses for Create/Update"); } }
public function getResult() { $types = TypeModel::select('id', 'title', 'sort_order', 'description')->get()->toArray(); $categories = CategoryModel::select('id', 'title', 'sort_order')->get()->toArray(); $courses = CourseModel::select('id', 'title')->with(['types', 'categories'])->with(['events' => function ($q) { $q->where('date_start', '>', \Carbon::now())->where('is_closed', '<>', 1); }])->get(); foreach ($types as $type_id => &$type) { // $_courses = array_filter($courses, function ($course) { // return $course->types->contains('id', $type->id); // }); $_courses = $courses->filter(function ($course) use($type) { return $course->types->contains('id', $type['id']); }); $type['courses'] = $_courses; $_cat = []; foreach ($categories as $cat_id => &$category) { // $__courses = array_filter($_courses, function ($course) { // return $course->categories->contains('id', $category->id); // }); $__courses = $_courses->filter(function ($course) use($category) { return $course->categories->contains('id', $category['id']); }); $category['courses'] = $__courses; $_cat[] = $category; } $type['categories'] = $_cat; } // $types->each(function ($type) use ($categories, $courses) { // $_courses = $courses->filter(function ($course) use ($type) { // return $course->types->contains('id', $type->id); // }); // $type['_courses'] = $_courses; // $categories->each(function ($category) use ($_courses, &$type) { // $__courses = $_courses->filter(function ($course) use ($category) { // return $course->categories->contains('id', $category->id); // }); // // $type->categories[$category->id]['__courses'] = $__courses; // $category['__courses'] = $__courses; // // CW::info(['__', $__courses]); // }); // // $type['_categories'] = $categories; // $type['_categories'] = $_cats; // }); CW::info(['Result' => $types]); return; }
/** * 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"); // } }
public function onAddCourse() { extract(post()); $user = $this->user(); if (!$user) { Flash::info('Войдите или Зарегистрируйтесь, чтобы записаться на курс!'); return ['#layout-flash-messages' => $this->renderPartial('@message')]; } $course = CourseModel::find($course_id); // $userevents = $user->userevents()->isInOrder(); try { $el = '#js' . $course_id . $type_id . $cat_id; // CW::info(['check' => $order->courses->contains('id', $course->id)]); if ($user->userevents->contains('course_id', $course->id)) { Flash::info('Учебная программа уже в личном кабинете'); return [$el => $this->renderPartial('@button'), '#layout-flash-messages' => $this->renderPartial('@message')]; } $userevent = new UserEventModel(); $userevent->user()->associate($user); $userevent->course()->associate($course); $userevent->state()->associate(UserEventStateModel::find(1)); // Статус: В заявке $userevent->fill(['course_type_id' => $type_id, 'course_category_id' => $cat_id]); $userevent->save(); Flash::success('Учебная программа добавлена в личный кабинет'); return [$el => $this->renderPartial('@button'), '#layout-flash-messages' => $this->renderPartial('@message')]; } catch (Exception $e) { Flash::error('Ошибка!'); return ['#layout-flash-messages' => $this->renderPartial('@message')]; } }