/**
  * Store a newly created resource in storage.
  *
  * @param  \Illuminate\Http\Request  $request
  * @return \Illuminate\Http\Response
  */
 public function store($resturant_id, Request $request)
 {
     //
     $registered_resturant = Resturant::Where('id', '=', $resturant_id)->firstOrFail();
     $subscription = SubscriptionPlan::Where('id', '=', $registered_resturant->subscription_plan_id)->firstOrFail();
     $franchise_users = User::Where('resturant_id', '=', $resturant_id)->count();
     if ($franchise_users == $subscription->user) {
         return response()->json(["Response" => "error", "message" => "You Subscription plan doesn't allow more users"]);
     } else {
         $franchise = new Franchise();
         $request_data = $request->input('request');
         $request_array = json_decode($request_data, true);
         $users = $request_array['users_list'];
         if (sizeof($users) > $subscription->user) {
             return response()->json(["Response" => "error", "message" => "You Subscription plan doesn't allow specified number of users"]);
         }
         $rules = array('email' => 'unique:users,username');
         foreach ($users as $user) {
             $name = $user['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.']);
             }
         }
         $parsed_object = $request_array['franchisedetails'];
         $franchise->location_id = $parsed_object['location'];
         $franchise->address = $parsed_object['address'];
         $franchise->phone = $parsed_object['phone'];
         $franchise->name = $parsed_object['name'];
         $franchise->resturantId = $resturant_id;
         $franchise->save();
         foreach ($users as $user) {
             $name = $user['username'];
             $password = $user['password'];
             $user = new User();
             $user->username = $name;
             $hashed_password = Hash::make($password);
             $user->password = $hashed_password;
             $user->resturant_id = $resturant_id;
             $user->franchise_id = $franchise->id;
             $user->role_id = 2;
             $user->active = 0;
             $user->activation_token = str_random(32);
             $user->save();
             //\Event::fire(new registrationEmail($user));
         }
         return response()->json(["Response" => "success", "message" => 'Franchise saved successfully']);
     }
 }