Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
21 kB
2
Indexable
Never
 public function storeData(Request $request)
    {
        try {
            $jsonData = json_decode($request['json_data'], true);

            $arrival_date = stripslashes($jsonData[0]['preferred_arrival_date']);
            $departure_date = stripslashes($jsonData[0]['preferred_departure_date']);

            $email = $jsonData[0]['email'];
            $user  = User::where('Email', $email)->first();
            if (isset($user) && !empty($user)) {
                $status   = false;
                $message  = Lang::get('messages.profile.emailFound');
                return response()->json([
                    'status'      => $status,
                    'message'     => $message
                ]);
            } else {
                if (isset($jsonData[0]['user_id']) && !empty($jsonData[0]['user_id'])) {
                    $user           = User::find($jsonData[0]['user_id']);
                    $user_address   = UserAddress::where('UserId', $jsonData[0]['user_id'])->first();
                    if (empty($user_address)) {
                        $user_address = new UserAddress();
                    }
                    $message = Lang::get('messages.profile.update');
                } else {
                    $user           = new User();
                    $user_address   = new UserAddress();
                    $message        = Lang::get('messages.profile.add');
                }
                $user->StatusId         = 1;
                $user->RoleId           = 3;
                $user->Prefix           = isset($jsonData[0]['parent_prefix']) && !empty($jsonData[0]['parent_prefix']) ? $jsonData[0]['parent_prefix'] : NULL;
                $user->CreatedBy        = NULL;
                $user->UpdateBy         = NULL;
                $user->FamilyName       = isset($jsonData[0]['last_name']) && !empty($jsonData[0]['last_name']) ? $jsonData[0]['last_name'] : NULL;
                $user->Name             = isset($jsonData[0]['first_name']) && !empty($jsonData[0]['first_name']) ? $jsonData[0]['first_name'] : NULL;
                $user->Email            = isset($email) ? $email : $user->Email;
                $user->Password         = Hash::make('i30F{j4Y30$?');
                $user->Gender           = isset($jsonData[0]['gender']) && !empty($jsonData[0]['gender']) ? $jsonData[0]['gender'] : NULL;
                $user->DOB              = isset($jsonData[0]['date_of_birth']) && !empty($jsonData[0]['date_of_birth']) ? $jsonData[0]['date_of_birth'] : NULL;
                $user->Greetings        = isset($jsonData[0]['greetings']) && !empty($jsonData[0]['greetings']) ? $jsonData[0]['greetings'] : NULL;
                $user->Nationality      = isset($jsonData[0]['nationality']) && !empty($jsonData[0]['nationality']) ? $jsonData[0]['nationality'] : NULL;
                $user->PhoneNumber      = isset($jsonData[0]['phone']) && !empty($jsonData[0]['phone']) ? $jsonData[0]['phone'] : NULL;
                $user->CountryCode      = isset($jsonData[0]['country_code']) && !empty($jsonData[0]['country_code']) ? $jsonData[0]['country_code'] : 'ch';
                $user->ProfilePic       = NULL;
                $user->CustomerType     = '2';
                $user->save();

                $user_id = isset($jsonData[0]['user_id']) && !empty($jsonData[0]['user_id']) ? $jsonData[0]['user_id'] : $user->Id;

                $user = User::find($user_id);
                $user_address->CountryId      = $jsonData[0]['country'];
                $user_address->UserId         = isset($jsonData[0]['user_id']) && !empty($jsonData[0]['user_id']) ? (int)$jsonData[0]['user_id'] : $user->Id;
                $user_address->CreatedBy      = isset($jsonData[0]['user_id']) && !empty($jsonData[0]['user_id']) ? (int)$jsonData[0]['user_id'] : $user->Id;
                $user_address->UpdateBy       = isset($jsonData[0]['user_id']) && !empty($jsonData[0]['user_id']) ? (int)$jsonData[0]['user_id'] : $user->Id;
                $user_address->City           = isset($jsonData[0]['city']) && !empty($jsonData[0]['city']) ? $jsonData[0]['city'] : NULL;
                $user_address->Address        = isset($jsonData[0]['street_address']) && !empty($jsonData[0]['street_address']) ? $jsonData[0]['street_address'] : NULL;
                $user_address->ZipCode        = isset($jsonData[0]['postal_code']) && !empty($jsonData[0]['postal_code']) ? $jsonData[0]['postal_code'] : NULL;
                $user_address->save();

                // foreach ($jsonData[0]['child_user'] as $key => $val) {
                if (isset($jsonData[0]['childs_school']) && !empty($jsonData[0]['childs_school'])) {
                    $school           = new School();
                    $school->Name       = $jsonData[0]['childs_school'];
                    $school->StatusId   = 1;
                    $school->UserId     = isset($jsonData[0]['user_id']) && !empty($jsonData[0]['user_id']) ? (int)$jsonData[0]['user_id'] : $user->Id;
                    $school->CreatedBy  = isset($jsonData[0]['user_id']) && !empty($jsonData[0]['user_id']) ? (int)$jsonData[0]['user_id'] : $user->Id;
                    $school->UpdateBy   = isset($jsonData[0]['user_id']) && !empty($jsonData[0]['user_id']) ? (int)$jsonData[0]['user_id'] : $user->Id;
                    $school->save();
                }

                if (isset($jsonData[0]['child_first_name']) && !empty($jsonData[0]['child_first_name'])) {
                    if (isset($jsonData[0]['user_id']) && !empty($jsonData[0]['user_id'])) {
                        $child_users    = ChildUser::where('UserId', $jsonData[0]['user_id'])->first();
                    } else {
                        $child_users    = new ChildUser();
                    }
                    $child_users->UserId               = isset($jsonData[0]['user_id']) && !empty($jsonData[0]['user_id']) ? (int)$jsonData[0]['user_id'] : $user->Id;
                    $child_users->StatusId             = 1;
                    $child_users->SchoolId             = isset($jsonData[0]['school_id']) && !empty($jsonData[0]['school_id']) ? $jsonData[0]['school_id'] : NULL;
                    $child_users->CreatedBy            = isset($jsonData[0]['user_id']) && !empty($jsonData[0]['user_id']) ? (int)$jsonData[0]['user_id'] : $user->Id;
                    $child_users->UpdateBy             = isset($jsonData[0]['user_id']) && !empty($jsonData[0]['user_id']) ? (int)$jsonData[0]['user_id'] : $user->Id;
                    $child_users->Prefix               = isset($jsonData[0]['child_prefix']) && !empty($jsonData[0]['child_prefix']) ? $jsonData[0]['child_prefix'] : NULL;
                    $child_users->FamilyName           = isset($jsonData[0]['child_first_name']) && !empty($jsonData[0]['child_first_name']) ? $jsonData[0]['child_first_name'] : NULL;
                    $child_users->Name                 = isset($jsonData[0]['child_last_name']) && !empty($jsonData[0]['child_last_name']) ? $jsonData[0]['child_last_name'] : NULL;
                    $child_users->DOB                  = isset($jsonData[0]['dob']) && !empty($jsonData[0]['dob']) ? Helper::formatDOB($jsonData[0]['dob']) : NULL;
                    $child_users->Gender               = isset($jsonData[0]['child_date_of_birth']) && !empty($jsonData[0]['child_date_of_birth']) ? $jsonData[0]['child_date_of_birth'] : NULL;
                    $child_users->CountryId            = isset($jsonData[0]['child_country']) && !empty($jsonData[0]['child_country']) ? $jsonData[0]['child_country'] : NULL;
                    $child_users->City                 = isset($jsonData[0]['child_city']) && !empty($jsonData[0]['child_city']) ? $jsonData[0]['child_city'] : NULL;
                    $child_users->PassportNo           = isset($jsonData[0]['child_passport_no']) && !empty($jsonData[0]['child_passport_no']) ? $jsonData[0]['child_passport_no'] : NULL;
                    $child_users->NationalityLanguage  = isset($jsonData[0]['child_nationality_and_language']) && !empty($jsonData[0]['child_nationality_and_language']) ? $jsonData[0]['child_nationality_and_language'] : NULL;
                    $child_users->save();
                }
                // }

                if (isset($jsonData[0]['id']) && !empty($jsonData[0]['id'])) {
                    $booking        = Booking::find($jsonData[0]['id']);
                    $paymentStatus  = $booking->PaymentStatus;
                    $message        = Lang::get('messages.booking.update');
                } else {
                    $booking  = new Booking();
                    $paymentStatus = '2';
                    $message  = Lang::get('messages.booking.add');
                }
                // Store booking data
                $booking->BookingType       = 1;
                $booking->BookingUniqueId   = isset($jsonData[0]['id']) && !empty($jsonData[0]['id']) ? $jsonData[0]['booking_unique_id'] : Booking::generateCustomId();
                $booking->CreatedBy         = isset($jsonData[0]['user_id']) && !empty($jsonData[0]['user_id']) ? (int)$jsonData[0]['user_id'] : $user->Id;
                $booking->UpdateBy          = isset($jsonData[0]['user_id']) && !empty($jsonData[0]['user_id']) ? (int)$jsonData[0]['user_id'] : $user->Id;
                $booking->BookingStartDate  = isset($arrival_date) && !empty($arrival_date) ? Helper::formatViewDOB($arrival_date) : NULL;
                $booking->BookingEndDate    = isset($departure_date) && !empty($departure_date) ? Helper::formatViewDOB($departure_date) : NULL;
                $booking->BookingNights     = isset($jsonData[0]['night']) ? $jsonData[0]['night'] : NULL;
                $booking->BookingDays       = isset($jsonData[0]['how_many_days']) && !empty($jsonData[0]['how_many_days']) ? $jsonData[0]['how_many_days'] : NULL;
                $booking->FinalPrice        = isset($jsonData[0]['grand_total']) && !empty($jsonData[0]['grand_total']) ? Helper::formatCHFCurrency($jsonData[0]['grand_total']) : NULL;
                $booking->BookingStatus     = 2;
                $booking->BookingCategory   = 2;
                $booking->BookingTypeStatus = 1;
                $booking->PaymentStatus     = 2;
                $booking->ReservationCategory     = 1;
                $booking->Notes             = isset($jsonData[0]['comments']) && !empty($jsonData[0]['comments']) ? $jsonData[0]['comments'] : NULL;
                $booking->IpAddress         = isset($jsonData[0]['ip_address']) && !empty($jsonData[0]['ip_address']) ? $jsonData[0]['ip_address'] : NULL;
                $booking->save();

                $booking_id = isset($jsonData[0]['id']) && !empty($jsonData[0]['id']) ? $jsonData[0]['id'] : $booking->Id;
                $booking    = Booking::find($booking_id);

                if (isset($jsonData[0]['services']) && is_array($jsonData[0]['services'])) {
                    $existingServices = BookingServiceRelation::where('BookingId', $booking_id)->pluck('ServiceId')->toArray();
                    $servicesToRemove = array_diff($existingServices, $jsonData[0]['services']);
                    if (!empty($servicesToRemove)) {
                        BookingServiceRelation::where('BookingId', $booking_id)
                            ->whereIn('ServiceId', $servicesToRemove)
                            ->delete();
                    }
                    foreach ($jsonData[0]['services'] as $service) {
                        $bookingservicerelation = BookingServiceRelation::where('ServiceId', $service)
                            ->where('BookingId', $booking_id)
                            ->first();
                        if (empty($bookingservicerelation)) {
                            $bookingservicerelation = new BookingServiceRelation();
                            $bookingservicerelation->BookingId = isset($jsonData[0]['id']) && !empty($jsonData[0]['id']) ? (int)$jsonData[0]['id'] : $booking->Id;
                            $bookingservicerelation->ServiceId = $service;
                            $bookingservicerelation->save();
                        } else {
                            $bookingservicerelation->BookingId = isset($jsonData[0]['id']) && !empty($jsonData[0]['id']) ? (int)$jsonData[0]['id'] : $booking->Id;
                            $bookingservicerelation->ServiceId = $service;
                            $bookingservicerelation->save();
                        }
                    }
                }

                if (isset($jsonData[0]['id']) && !empty($jsonData[0]['id'])) {
                    $bookingrelation    = BookingRelation::where('BookingId', $booking_id)->first();
                } else {
                    $bookingrelation    = new BookingRelation();
                }
                // Store booking data
                $bookingrelation->BookingId     = isset($jsonData[0]['id']) && !empty($jsonData[0]['id']) ? (int)$jsonData[0]['id'] : $booking->Id;
                $bookingrelation->CategoryId    = 1;
                $bookingrelation->SessionId     = NULL;
                $bookingrelation->PackageId     = isset($jsonData[0]['package']) && !empty($jsonData[0]['package']) ? $jsonData[0]['package'] : NULL;
                $bookingrelation->LanguageId    = isset($jsonData[0]['language']) && !empty($jsonData[0]['language']) ? $jsonData[0]['language'] : NULL;
                $bookingrelation->MarketingId   = 1;
                $bookingrelation->SourceId      = 2;
                $bookingrelation->AgentId       = NULL;
                $bookingrelation->save();

                if (isset($jsonData[0]['id']) && !empty($jsonData[0]['id'])) {
                    $bookingbooker  = BookingBooker::where('BookingId', $booking_id)->first();
                    if (empty($bookingbooker)) {
                        $bookingbooker = new BookingBooker();
                    }
                } else {
                    $bookingbooker  = new BookingBooker();
                }

                $bookingbooker->BookingId   = isset($jsonData[0]['id']) && !empty($jsonData[0]['id']) ? (int)$jsonData[0]['id'] : $booking->Id;
                $bookingbooker->CountryId   = isset($jsonData[0]['country']) && !empty($jsonData[0]['country']) ? $jsonData[0]['country'] : NULL;;
                $bookingbooker->Prefix      = isset($jsonData[0]['parent_prefix']) && !empty($jsonData[0]['parent_prefix']) ? $jsonData[0]['parent_prefix'] : NULL;;
                $bookingbooker->FamilyName  = isset($jsonData[0]['last_name']) && !empty($jsonData[0]['last_name']) ? $jsonData[0]['last_name'] : NULL;;
                $bookingbooker->Name        = isset($jsonData[0]['first_name']) && !empty($jsonData[0]['first_name']) ? $jsonData[0]['first_name'] : NULL;;
                $bookingbooker->Email       = isset($jsonData[0]['email']) && !empty($jsonData[0]['email']) ? $jsonData[0]['email'] : NULL;;
                $bookingbooker->Gender      = isset($jsonData[0]['gender']) ? $jsonData[0]['gender'] : NULL;;
                $bookingbooker->DOB         = isset($jsonData[0]['dob']) ? Helper::formatViewDOB($jsonData[0]['dob']) : NULL;
                $bookingbooker->Greetings   = isset($jsonData[0]['greetings']) ? $jsonData[0]['greetings'] : NULL;
                isset($greetings) ? $greetings : NULL;
                $bookingbooker->Nationality = isset($jsonData[0]['nationality']) ? $jsonData[0]['nationality'] : NULL;
                isset($nationality) ? $nationality : NULL;
                $bookingbooker->PhoneNumber = isset($jsonData[0]['phone']) ? $jsonData[0]['phone'] : NULL;
                isset($phone_number) ? $phone_number : NULL;
                $bookingbooker->CountryCode = isset($jsonData[0]['country_code']) ? $jsonData[0]['country_code'] : NULL;
                isset($phone_country) ? $phone_country : NULL;
                $bookingbooker->City        = isset($jsonData[0]['city']) ? $jsonData[0]['city'] : NULL;
                $bookingbooker->Address     = isset($jsonData[0]['street_address']) ? $jsonData[0]['street_address'] : NULL;
                isset($street_address) ? $street_address : NULL;
                $bookingbooker->ZipCode     = isset($jsonData[0]['postal_code']) ? $jsonData[0]['postal_code'] : NULL;
                isset($zipcode) ? $zipcode : NULL;
                $bookingbooker->save();

                $bookingbooker_id = isset($jsonData[0]['id']) && !empty($jsonData[0]['id']) ? $jsonData[0]['bookingbooker_id'] : $bookingbooker->Id;

                if (isset($jsonData[0]['child_first_name']) || !empty($jsonData[0]['child_first_name'])) {
                    if (isset($jsonData[0]['id']) && !empty($jsonData[0]['id'])) {
                        $bookingstudent = BookingStudent::where('BookingId', $jsonData[0]['id'])->first();
                        if (empty($bookingstudent)) {
                            $bookingstudent = new BookingStudent();
                        }
                    } else {
                        $bookingstudent = new BookingStudent();
                    }
                    $bookingstudent->BookingId              = isset($jsonData[0]['id']) && !empty($jsonData[0]['id']) ? (int)$jsonData[0]['id'] : $booking->Id;
                    $bookingstudent->CountryId              = isset($jsonData[0]['child_country']) && !empty($jsonData[0]['child_country']) ? $jsonData[0]['child_country'] : NULL;
                    $bookingstudent->SchoolId               = NULL;
                    $bookingstudent->Prefix                 = isset($jsonData[0]['child_prefix']) && !empty($jsonData[0]['child_prefix']) ? $jsonData[0]['child_prefix'] : NULL;
                    $bookingstudent->FamilyName             = isset($jsonData[0]['child_last_name']) && !empty($jsonData[0]['child_last_name']) ? $jsonData[0]['child_last_name'] : NULL;
                    $bookingstudent->Name                   = isset($jsonData[0]['child_first_name']) && !empty($jsonData[0]['child_first_name']) ? $jsonData[0]['child_first_name'] : NULL;
                    $bookingstudent->Gender                 = isset($jsonData[0]['child_gender']) && !empty($jsonData[0]['child_gender']) ? $jsonData[0]['child_gender'] : NULL;
                    $bookingstudent->DOB                    = isset($jsonData[0]['child_date_of_birth']) && !empty($jsonData[0]['child_date_of_birth']) ? Helper::formatViewDOB($jsonData[0]['child_date_of_birth']) : NULL;
                    $bookingstudent->NationalityLanguage    = isset($jsonData[0]['child_nationality_and_language']) && !empty($jsonData[0]['child_nationality_and_language']) ? $jsonData[0]['child_nationality_and_language'] : NULL;
                    $bookingstudent->City                   = isset($jsonData[0]['child_city']) && !empty($jsonData[0]['child_city']) ? $jsonData[0]['child_city'] : NULL;
                    $bookingstudent->PassportNo             = isset($jsonData[0]['child_passport_no']) && !empty($jsonData[0]['child_passport_no']) ? $jsonData[0]['child_passport_no'] : NULL;
                    $bookingstudent->ProfilePic             = NULL;
                    $bookingstudent->save();

                    $bookingstudent_id = isset($jsonData[0]['id']) && !empty($jsonData[0]['id']) ? $jsonData[0]['bookingstudent_id'] : $bookingstudent->Id;

                    if (isset($jsonData[0]['id']) && !empty($jsonData[0]['id'])) {
                        $bookingbookerrelation = BookingBookerRelation::where('BookingId', $jsonData[0]['id'])->first();
                        if (empty($bookingbookerrelation)) {
                            $bookingbookerrelation = new BookingBookerRelation();
                        }
                    } else {
                        $bookingbookerrelation  = new BookingBookerRelation();
                    }
                    // Store booking data
                    $bookingbookerrelation->BookingId           = isset($jsonData[0]['id']) && !empty($jsonData[0]['id']) ? (int)$jsonData[0]['id'] : $booking->Id;
                    $bookingbookerrelation->BookingBookerId     = $bookingbooker_id;
                    $bookingbookerrelation->BookingStudentId    = isset($bookingstudent_id) ? $bookingstudent_id : NULL;
                    $bookingbookerrelation->save();

                }
                $status = true;
                return response()->json([
                    'status'      => $status,
                    'id'          => $booking->Id,
                    'user_id'     => $user_id
                ]);
            }
        } catch (Exception $exception) {
            $status   = false;
            $message  = $exception->getMessage();
            dd($message);
            return response()->json([
                'status'      => $status,
                'message'     => $message
            ]);
        }
    }