/**
  * Update the specified resource in storage.
  *
  * @param  \Illuminate\Http\Request  $request
  * @param  int  $id
  * @return \Illuminate\Http\Response
  */
 public function update(Request $request, $id, $franchiseId)
 {
     //
     $registered_resturant = Resturant::Where('id', '=', $id)->firstOrFail();
     $subscription = SubscriptionPlan::Where('id', '=', $registered_resturant->subscription_plan_id)->firstOrFail();
     $franchise_users = User::Where('franchise_id', '=', $franchiseId)->count();
     $all_users = User::Where('resturant_id', '=', $id)->where('franchise_id', '!=', -1)->count();
     $request_data = $request->input('request');
     $request_array = json_decode($request_data, true);
     $Location_users = $request_array['users'];
     $effective_users = $all_users - $franchise_users + sizeof($Location_users);
     if ($effective_users > $subscription->user) {
         return response()->json(["Response" => "error", "message" => "You Subscription plan doesn't allow more users"]);
     } else {
         $franchise = Franchise::Where('id', '=', $franchiseId)->firstOrFail();
         $parsed_object = $request_array['Franchise'];
         $franchise->location_id = $parsed_object['location_id'];
         $franchise->address = $parsed_object['address'];
         $franchise->phone = $parsed_object['phone'];
         $franchise->name = $parsed_object['name'];
         $franchise->save();
         $rules = array('email' => 'unique:users,username');
         foreach ($Location_users as $users) {
             $name = $users['username'];
             $validator = \Validator::make(array('email' => $name), $rules);
             if ($validator->fails()) {
                 return response()->json(["Response" => "error", "message" => 'Email Address : ' . $name . ' is Already Registered in our system.']);
             }
         }
         foreach ($Location_users as $users) {
             $isnew = array_get($users, 'isnew', 'default');
             if ($isnew != 'default') {
                 $user = new User();
                 $name = $users['username'];
                 $password = $user['password'];
                 $user->username = $name;
                 $hashed_password = Hash::make($password);
                 $user->password = $hashed_password;
                 $user->resturant_id = $id;
                 $user->franchise_id = $franchiseId;
                 $user->role_id = 2;
                 $user->active = 0;
                 $user->activation_token = str_random(32);
                 \Event::fire(new registrationEmail($user));
                 $user->save();
             } else {
                 $name = $users['username'];
                 $password = $users['password'];
                 $userId = $users['id'];
                 $user = User::Where('id', '=', $userId)->firstOrFail();
                 $user->username = $name;
                 if ($user->password != $password) {
                     $hashed_password = Hash::make($password);
                     $user->password = $password;
                 }
                 $user->save();
             }
         }
         $franchise_Users = User::Where('franchise_id', '=', $franchiseId)->get();
         foreach ($franchise_Users as $franchiseUsers) {
             $franchiseUserId = $franchiseUsers->id;
             $tobeDeleted = true;
             foreach ($Location_users as $locationUsers) {
                 $isnew = array_get($locationUsers, 'isnew', 'default');
                 if ($isnew == 'default') {
                     if ($locationUsers['id'] == $franchiseUserId) {
                         $tobeDeleted = false;
                         break;
                     }
                 } else {
                     $tobeDeleted = false;
                 }
             }
             if ($tobeDeleted) {
                 User::where('id', '=', $franchiseUserId)->delete();
             }
         }
         return response()->json(["Response" => "success", "message" => "Franchise info Updated"]);
     }
     return response()->json(["Response" => "error", "message" => "There was an error processing your request"]);
 }