/**
  * Add New Manager Action
  * @param Request $request
  * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
  * @author: Vini Dubey<*****@*****.**>
  */
 public function addNewManager(Request $request)
 {
     $ObjPermissions = Permissions::getInstance();
     $ObjPermissionUserRelation = PermissionUserRelation::getInstance();
     if ($request->isMethod('GET')) {
         $where = ['rawQuery' => 'permission_id  NOT IN (1)'];
         //echo"<pre>";print_r($where);die("cfh");
         $permissionDetails = $ObjPermissions->getAllPermissions($where);
         return view('Admin/Views/manager/addNewManager', ['permissionlist' => $permissionDetails]);
     } elseif ($request->isMethod('POST')) {
         $postData = $request->all();
         $rules = array('firstname' => 'required|max:255', 'lastname' => 'required|max:255', 'email' => 'required|email|max:255|unique:users', 'username' => 'required|max:255', 'permitcheck' => 'required');
         $validator = Validator::make($request->all(), $rules);
         if ($validator->fails()) {
             return Redirect::back()->withErrors($validator)->withInput();
         } else {
             $password = "";
             $characters = array_merge(range('A', 'Z'), range('a', 'z'), range('0', '9'));
             $max = count($characters) - 1;
             for ($i = 0; $i < 8; $i++) {
                 $rand = mt_rand(0, $max);
                 $password .= $characters[$rand];
             }
             $manager = User::create(['name' => $postData['firstname'], 'last_name' => $postData['lastname'], 'email' => $postData['email'], 'password' => Hash::make($password), 'role' => '4', 'username' => $postData['username']]);
             $resultdata = DB::getPdo()->lastInsertId($manager);
             $data['user_id'] = $resultdata;
             $permit = $postData['permitcheck'];
             $mainpermission = implode(",", $permit);
             //$mainpermission = 1;
             $data['permission_ids'] = $mainpermission;
             $userPermission = $ObjPermissionUserRelation->insertmanagerpermission($data);
             if ($manager && $userPermission) {
                 $objMailTemplate = MailTemplate::getInstance();
                 $temp_name = "manager_signup_success_mail";
                 //  $where = ['rawQuery' => 'temp_name = ?', 'bindParam' => $temp_name];
                 $mailTempContent = $objMailTemplate->getTemplateByName($temp_name);
                 $key = env('MANDRILL_KEY');
                 $mandrill = new Mandrill($key);
                 $async = false;
                 $ip_pool = 'Main Pool';
                 $message = array('html' => $mailTempContent->temp_content, 'subject' => "Registration Successful As Manager", 'from_email' => "*****@*****.**", 'to' => array(array('email' => $postData['email'], 'type' => 'to')), 'merge_vars' => array(array("rcpt" => $postData['email'], 'vars' => array(array("name" => "firstname", "content" => $postData['firstname']), array("name" => "password", "content" => $password), array("name" => "username", "content" => $postData['username']), array("name" => "email", "content" => $postData['email']), array("name" => "url", "content" => env('WEB_URL') . '/admin/login')))));
                 $mailrespons = $mandrill->messages->send($message, $async, $ip_pool);
                 if ($mailrespons[0]['status'] == "sent") {
                     return Redirect::back()->with(['status' => 'success', 'msg' => 'Mail sent successfully to  ' . $postData['firstname']]);
                 } else {
                     $objuser = new User();
                     $whereForUpdate = ['rawQuery' => 'id =?', 'bindParams' => [$manager->id]];
                     $deleteUser = $objuser->deleteUserDetails($whereForUpdate);
                     return Redirect::back()->with(['status' => 'success', 'msg' => 'Failed To Send Mail.']);
                 }
             }
         }
     }
 }
 public function addNewSupplier(Request $request)
 {
     $response = new stdClass();
     if ($request->isMethod('post')) {
         // echo"<pre>";print_r($request->all());die("fch");
         $postData = $request->all();
         $rules = array('firstname' => 'required|max:255', 'lastname' => 'required|max:255', 'email' => 'required|email|max:255|unique:users', 'username' => 'required|max:255');
         $validator = Validator::make($request->all(), $rules);
         if ($validator->fails()) {
             return Redirect::back()->withErrors($validator)->withInput();
         } else {
             $password = "";
             $characters = array_merge(range('A', 'Z'), range('a', 'z'), range('0', '9'));
             $max = count($characters) - 1;
             for ($i = 0; $i < 8; $i++) {
                 $rand = mt_rand(0, $max);
                 $password .= $characters[$rand];
             }
             $supplier = User::create(['name' => $postData['firstname'], 'last_name' => $postData['lastname'], 'email' => $postData['email'], 'password' => Hash::make($password), 'role' => '3', 'username' => $postData['username']]);
             if ($supplier) {
                 //                    return redirect()->intended('admin/supplierDetails');
                 //                } else {
                 //                    return view("Admin/Views/supplier/addNewSupplier")->withErrors([
                 //                        'registerErrMsg' => 'Something went wrong, please try again.',
                 //                    ]);
                 //                }
                 $objMailTemplate = MailTemplate::getInstance();
                 $temp_name = "supplier_signup_success_mail";
                 //  $where = ['rawQuery' => 'temp_name = ?', 'bindParam' => $temp_name];
                 $mailTempContent = $objMailTemplate->getTemplateByName($temp_name);
                 $key = env('MANDRILL_KEY');
                 $mandrill = new Mandrill($key);
                 $async = false;
                 $ip_pool = 'Main Pool';
                 $message = array('html' => $mailTempContent->temp_content, 'subject' => "Registration Successful As Supplier", 'from_email' => "*****@*****.**", 'to' => array(array('email' => $postData['email'], 'type' => 'to')), 'merge_vars' => array(array("rcpt" => $postData['email'], 'vars' => array(array("name" => "firstname", "content" => $postData['firstname']), array("name" => "password", "content" => $password), array("name" => "username", "content" => $postData['username']), array("name" => "email", "content" => $postData['email']), array("name" => "url", "content" => 'http://localhost.flashsale.com/supplier/login')))));
                 $mailrespons = $mandrill->messages->send($message, $async, $ip_pool);
                 if ($mailrespons[0]['status'] == "sent") {
                     return Redirect::back()->with(['status' => 'success', 'msg' => 'Mail sent successfully to  ' . $postData['firstname']]);
                     // return redirect()->intended('admin/supplier-detail');
                 } else {
                     $objuser = new User();
                     $whereForUpdate = ['rawQuery' => 'id =?', 'bindParams' => [$supplier->id]];
                     $deleteUser = $objuser->deleteUserDetails($whereForUpdate);
                     return Redirect::back()->with(['status' => 'success', 'msg' => 'Failed To Send Mail.']);
                 }
             }
         }
     }
     return view('Admin/Views/supplier/addNewSupplier');
 }
 public function addNewCustomer(Request $request)
 {
     $response = new stdClass();
     if ($request->isMethod("POST")) {
         $postData = $request->all();
         $rules = array('firstname' => 'required|regex:/^[A-Za-z\\s]+$/|max:255', 'lastname' => 'required|regex:/^[A-Za-z\\s]+$/|max:255', 'username' => 'required|regex:/^[A-Za-z0-9._\\s]+$/|max:255|unique:users', 'email' => 'required|email|max:255|unique:users');
         $messages = ['firstname.regex' => 'The :attribute cannot contain special characters.', 'lastname.regex' => 'The :attribute cannot contain special characters.', 'username.regex' => 'The :attribute cannot contain special characters.'];
         $validator = Validator::make($request->all(), $rules, $messages);
         if ($validator->fails()) {
             return redirect('/admin/add-new-customer')->withErrors($validator)->withInput();
         } else {
             $password = "";
             $characters = array_merge(range('A', 'Z'), range('a', 'z'), range('0', '9'));
             $max = count($characters) - 1;
             for ($i = 0; $i < 8; $i++) {
                 $rand = mt_rand(0, $max);
                 $password .= $characters[$rand];
             }
             $supplier = User::create(['name' => $postData['firstname'], 'last_name' => $postData['lastname'], 'email' => $postData['email'], 'password' => Hash::make($password), 'role' => '1', 'status' => '1', 'username' => $postData['username']]);
             if ($supplier) {
                 $objMailTemplate = MailTemplate::getInstance();
                 $temp_name = "signup_success_mail";
                 //  $where = ['rawQuery' => 'temp_name = ?', 'bindParam' => $temp_name];
                 $mailTempContent = $objMailTemplate->getTemplateByName($temp_name);
                 $key = env('MANDRILL_KEY');
                 $mandrill = new Mandrill($key);
                 $async = false;
                 $ip_pool = 'Main Pool';
                 $message = array('html' => $mailTempContent->temp_content, 'subject' => "Registration Successful", 'from_email' => "*****@*****.**", 'to' => array(array('email' => $postData['email'], 'type' => 'to')), 'merge_vars' => array(array("rcpt" => $postData['email'], 'vars' => array(array("name" => "firstname", "content" => $postData['firstname']), array("name" => "password", "content" => $password)))));
                 $mailrespons = $mandrill->messages->send($message, $async, $ip_pool);
                 if ($mailrespons[0]['status'] == "sent") {
                     return Redirect::back()->with(['status' => 'success', 'msg' => 'Mail sent successfully to  ' . $postData['firstname']]);
                 } else {
                     $objuser = new User();
                     $whereForUpdate = ['rawQuery' => 'id =?', 'bindParams' => [$supplier->id]];
                     $deleteUser = $objuser->deleteUserDetails($whereForUpdate);
                     return Redirect::back()->with(['status' => 'success', 'msg' => 'Failed To Send Mail.']);
                 }
             }
         }
     }
     return view('Admin/Views/customer/addNewCustomer');
 }