/**
  * Add New Shop
  * @param Request $request
  * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
  * @since 13-05-2016
  * @author Vini Dubey <*****@*****.**>
  */
 public function addNewShop(Request $request)
 {
     $objCategoryModel = ProductCategory::getInstance();
     $objLocationModel = Location::getInstance();
     $objShopModel = Shops::getInstance();
     $objShopMetadataModel = ShopMetadata::getInstance();
     $userId = Session::get('fs_admin')['id'];
     $whereforCategory = ['rawQuery' => 'category_status =? and parent_category_id =?', 'bindParams' => [1, 0]];
     $allCategories = $objCategoryModel->getAllCategoriesWhere($whereforCategory);
     $whereforCountry = ['rawQuery' => 'is_visible =? and location_type =?', 'bindParams' => [0, 0]];
     $allCountry = $objLocationModel->getAllLocationsWhere($whereforCountry);
     $whereforShop = ['rawQuery' => 'user_id =?', 'bindParams' => [$userId]];
     $allShop = $objShopModel->getAllshopsWhere($whereforShop);
     //        print_a($allShop);
     //echo "<pre>";print_r($allShop);die;
     /////////////////////////////Flag Set By admin side///////////////Todo- Flag Set By admin side
     $multiple_store_flag = 1;
     // Value 1 if flag is set
     $sub_store_flag = 1;
     // Value 1 if flag is set
     $parent_category_flag = 1;
     // Value 1 if flag is set
     /////////////////////////////////////////////////////////////////
     $flag['multiple_store_flag'] = $multiple_store_flag;
     $flag['sub_store_flag'] = $sub_store_flag;
     $flag['parent_category_flag'] = $parent_category_flag;
     $data['allCategories'] = $allCategories;
     $data['Country'] = $allCountry;
     $data['Shop'] = $allShop;
     if (!empty($allShop) && $multiple_store_flag != 1) {
         //Error msg if multiple shop not allowed and shopeady added
         return view("Supplier/Views/supplier/addNewShop", ['multiple_store_err' => "Shop already added, Can not add Multiple Shop"]);
     } else {
         $parentCategoryId = 0;
         if (isset($request['parent_category']) && !empty($request['parent_category'])) {
             $parentCategoryId = $request['parent_category'];
         }
         $parentShopId = "";
         if (isset($request['parent_shop']) && !empty($request['parent_shop'])) {
             $parentShopId = $request['parent_shop'];
         }
         if ($request->isMethod('post')) {
             if ($parentShopId == '') {
                 //Sub store flag is not set
                 $rules = array('shop_name' => 'required');
             } else {
                 //Sub store flag is set
                 $rules = array();
             }
             $validator = Validator::make($request->all(), $rules);
             if ($validator->fails()) {
                 return Redirect::back()->withErrors($validator)->withInput();
             } else {
                 try {
                     $addressLine1 = "";
                     if (isset($request['address_line_1'])) {
                         $addressLine1 = $request['address_line_1'];
                     }
                     $addressLine2 = "";
                     if (isset($request['address_line_2'])) {
                         $addressLine2 = $request['address_line_2'];
                     }
                     $country = "";
                     if (isset($request['country'])) {
                         $country = $request['country'];
                     }
                     $state = "";
                     if (isset($request['state'])) {
                         $state = $request['state'];
                     }
                     $city = "";
                     if (isset($request['city'])) {
                         $city = $request['city'];
                     }
                     $zipcode = "";
                     if (isset($request['zipcode'])) {
                         $zipcode = $request['zipcode'];
                     }
                     $shop_flag = 1;
                     if (isset($request['shop_flag'])) {
                         $shop_flag = $request['shop_flag'];
                     }
                     $show_shop = 2;
                     if (isset($request['show_shop'])) {
                         $show_shop = $request['show_shop'];
                     }
                     ////////////Upload Shop Banner Start///////////////////////
                     if (isset($_FILES["shop_banner"]["name"]) && !empty($_FILES["shop_banner"]["name"])) {
                         $bannerFilePath = uploadImageToStoragePath(Input::file('shop_banner'), 'shopbanner', 'shopbanner_' . $userId . '_' . time() . ".jpg");
                     } else {
                         $bannerFilePath = uploadImageToStoragePath($_SERVER['DOCUMENT_ROOT'] . "/assets/images/no-image.png", 'shopbanner', 'shopbanner_' . $userId . '_' . time() . ".jpg");
                     }
                     ////////////Upload Shop banner End///////////////////////
                     ////////////Upload Shop Logo Start///////////////////////
                     if (isset($_FILES["shop_logo"]["name"]) && !empty($_FILES["shop_logo"]["name"])) {
                         $logoFilePath = uploadImageToStoragePath(Input::file('shop_logo'), 'shoplogo', 'shoplogo_' . $userId . '_' . time() . ".jpg");
                     } else {
                         $logoFilePath = uploadImageToStoragePath($_SERVER['DOCUMENT_ROOT'] . "/assets/images/no-image.png", 'shoplogo', 'shoplogo_' . $userId . '_' . time() . ".jpg");
                     }
                     ////////////Upload Shop Logo End///////////////////////
                     if ($parentShopId == "") {
                         //Sub store flag is not set
                         $shopdata = array('user_id' => $userId, 'shop_name' => $request['shop_name'], 'shop_banner' => $bannerFilePath, 'shop_logo' => $logoFilePath, 'parent_category_id' => $parentCategoryId, 'shop_flag' => $shop_flag);
                         $addShop = $objShopModel->addShop($shopdata);
                         $shop_id = $addShop;
                         $shopType = "0";
                     } else {
                         //Sub store flag is set
                         $shopType = "1";
                         $shop_id = $parentShopId;
                     }
                     $shopMatadata = array('shop_id' => $shop_id, 'shop_type' => $shopType, 'address_line_1' => $addressLine1, 'address_line_2' => $addressLine2, 'city' => $city, 'state' => $state, 'country' => $country, 'zipcode' => $zipcode, 'added_date' => time(), 'show_shop_address' => $show_shop, 'shop_metadata_status' => 1);
                     $addShop = $objShopMetadataModel->addShopMetadata($shopMatadata);
                     if ($addShop) {
                         if ($parentShopId == "") {
                             return redirect()->back()->with('shop_success_msg', 'Shop Added Successfully, Waiting for Admin Approval.');
                         } else {
                             return redirect()->back()->with('shop_success_msg', 'Shop Added Successfully.');
                         }
                     }
                 } catch (\Exception $ex) {
                     return redirect()->back()->with('exception', 'An exception occurred, please reload the page and try again.');
                 }
             }
         }
         return view("Admin/Views/shop/addNewShop", ['data' => $data], ['flag' => $flag]);
     }
 }
 /**
  * Handle ajax call for settings
  * @param Request $request
  * @throws \Exception
  * @since 07-01-2016
  * @author Dinanath Thakur <*****@*****.**>
  */
 public function settingsAjaxHandler(Request $request)
 {
     $objLocationModel = Location::getInstance();
     $inputData = $request->input();
     $method = $inputData['method'];
     switch ($method) {
         case 'getLocations':
             $params = array_except($inputData, 'method');
             $filedNames = '';
             foreach ($params as $index => $param) {
                 $filedNames .= $index . '=? AND ';
             }
             $whereForLocation = ['rawQuery' => substr_replace($filedNames, '', strrpos($filedNames, 'AND'), strlen('AND')), 'bindParams' => $params];
             $selectedColumns = ['location_id', 'name'];
             $allCountries = $objLocationModel->getAllLocationsWhere($whereForLocation, $selectedColumns);
             echo json_encode($allCountries);
             break;
         default:
             break;
     }
 }
 /**
  * Edit Language Action
  * @param Request $request
  * @param $lid
  * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
  * @author Vini Dubey
  */
 public function editLanguage(Request $request, $lid)
 {
     $response = new stdClass();
     $ObjLanguageModel = Languages::getInstance();
     $ObjLocationModel = Location::getInstance();
     $postData = $request->all();
     if ($request->isMethod('GET')) {
         $where = ['rawQuery' => 'location_type = ?', 'bindParams' => [0]];
         $countrydetails = $ObjLocationModel->getAllCountryDetails($where);
         $where = ['rawQuery' => 'lang_id = ?', 'bindParams' => [$lid]];
         $selectedColumns = ['location.name as location_name', 'languages.*'];
         $languagedetails = $ObjLanguageModel->getAllLanguageDetails($where, $selectedColumns);
         return view('Admin/Views/administration/editLanguage', ['countrydetail' => $countrydetails, 'languagedetails' => $languagedetails[0]]);
     } elseif ($request->isMethod('POST')) {
         $rules = array('lang_code' => 'unique:languages,lang_code,' . $lid . ',lang_id', 'name' => trans('unique:languages,name,' . $lid . ',lang_id'), 'country_code' => 'unique:languages,country_code,' . $lid . ',lang_id');
         $validator = Validator::make($request->all(), $rules);
         if ($validator->fails()) {
             return Redirect::back()->withErrors($validator)->withInput();
         } else {
             $data['lang_code'] = $postData['lang_code'];
             $data['name'] = $postData['name'];
             $data['country_code'] = $postData['country_code'];
             // $data['status'] = $postData['statact'];
             $where = ['rawQuery' => 'lang_id = ?', 'bindParams' => [$lid]];
             $updateUser = $ObjLanguageModel->updateLanguageStatus($data, $where);
             if ($updateUser) {
                 return Redirect::back()->with(['status' => 'success', 'msg' => 'Language details has been updated.']);
             } else {
                 //NOTHING TO UPDATE
                 return Redirect::back()->with(['status' => 'info', 'msg' => 'Nothing to update.']);
             }
         }
     }
 }
    public function supplierAjaxHandler(Request $request)
    {
        $inputData = $request->input();
        $method = $request->input('method');
        $ObjUser = User::getInstance();
        $objModelLocation = Location::getInstance();
        $ObjUsermeta = Usersmeta::getInstance();
        if ($method) {
            switch ($method) {
                case 'availableSupplier':
                    $objuser = User::getInstance();
                    $where = array('rawQuery' => 'role = ?', 'bindParams' => [3]);
                    $available_supplier = $objuser->getAvailableSupplierDetails($where);
                    //                    echo"<pre>";print_r($available_supplier);die("gvj");
                    //                    foreach ($available_supplier as $key => $val) {
                    //                        $vail[$key] = $val->id;
                    //                        $available_supplier[$key] = $val;
                    //                        $available_supplier[$key]->usermeta = array();
                    //                        $available_supplier[$key] = $val;
                    //                        $available_supplier[$key]->filter = array();
                    //                    }
                    //                    $supplier = implode(",",$vail);
                    //                    echo"<pre>";print_r($supplier);die("cvjh");
                    //  $whereuser = ['rawQuery' => 'user_id = ?', 'bindParams' => $vail];
                    //                    $usermetaInfo = $ObjUsermeta->getAvaiableUserMetaDetails();
                    //                    foreach($usermetaInfo as $keymeta => $valmeta){
                    //                        $usermetaInfo[$keymeta] = $valmeta;
                    ////                        $usermetaInfo[$keymeta]->filter = array();
                    //                    }
                    //
                    //                    if(!empty($usermetaInfo)){
                    //
                    //                        $available_supplier[$key]->usermeta =  $usermetaInfo;
                    //
                    //                    }
                    //                    echo"<pre>";print_r($available_supplier);die("cvjh");
                    //  echo"<pre>";print_r($usermetaInfo);die("cvjh");
                    //
                    //                    $whereuser = array('rawQuery' => 'user_id IN('.$supplier.')');
                    //                    $available_supplier[$key] = $usermetadescription;
                    ////                    echo"<pre>";print_r($available_customers);die("cvjh");
                    //
                    //  $usermetaInfo = $ObjUsermeta->getAvaiableUserMetaDetails($whereuser);
                    //  foreach ($usermetaInfo as $filtergroupkey => $filtergroupvalue) {
                    //    $usermetaInfo[$filtergroupkey]->filtergroup = array();
                    //                        if ($filtergroupvalue->permission_ids != '') {
                    //                            $catfilterName = array_values(array_unique(explode(',', $filtergroupvalue->permission_ids)));
                    //                            $per = implode(",", $catfilterName);
                    //                            $where = ['rawQuery' => 'permission_id IN(' . $per . ')'];
                    //                            $getcategory = $objPermissionModel->getPermissionNameByIds($where);
                    //
                    //                            foreach ($getcategory as $catkey => $catval) {
                    //                                $availPermissionRelation[$filtergroupkey]->filter = $catval;
                    //                                $available_customers[$key]->filter = $availPermissionRelation[$filtergroupkey]->filter;
                    //                            }
                    //    $available_supplier[$key]->filter = $usermetaInfo[$filtergroupkey]->filtergroup;
                    //
                    //   }
                    //                    }
                    //                    echo"<pre>";print_r($available_supplier);die("gvj");
                    return Datatables::of($available_supplier)->addColumn('action', function ($available_supplier) {
                        $action = '<span class="tooltips" title="Edit Supplier Details." data-placement="top"> <a href="/admin/edit-supplier/' . $available_supplier->id . '" class="btn btn-sm grey-cascade" style="margin-left: 10%;">';
                        $action .= '<i class="fa fa-pencil-square-o"></i></a>';
                        $action .= '</span> &nbsp;&nbsp;';
                        $action .= '<span class="tooltips" title="Delete Supplier Details." data-placement="top"> <a href="#" data-cid="' . $available_supplier->id . '" class="btn btn-danger delete-supplier" style="margin-left: 10%;">';
                        $action .= '<i class="fa fa-trash-o"></i>';
                        $action .= '</a></span>';
                        return $action;
                    })->addColumn('status', function ($available_supplier) {
                        $button = '<td style="text-align: center">';
                        $button .= '<button class="btn ' . ($available_supplier->status == 1 ? "btn-success" : "btn-danger") . ' supplier-status" data-id="' . $available_supplier->id . '">' . ($available_supplier->status == 1 ? "Active" : "Inactve") . ' </button>';
                        $button .= '<td>';
                        return $button;
                    })->addColumn('supplierdetail', function ($available_supplier) {
                        return '<td style = "text-align: center" ><div class="container" style = "width: 50px " >
                             <span class="tooltips" title = "Review Description." data-placement = "top" ><button data-id = "' . $available_supplier->id . '" type = "button" class="btn btn-sm btn-default modaldescription" data-toggle = "modal" data-target = "#mymodel" ><i class="fa fa-expand" ></i ></button ></span >
                               </div >
                            </td >';
                    })->removeColumn('name')->removeColumn('updated_at')->make();
                    break;
                case 'pendingSupplier':
                    $objuser = User::getInstance();
                    $where = array('rawQuery' => 'role = ? and status = ?', 'bindParams' => [3, 0]);
                    //                    $status = array('rawQuery' => 'status = ?', 'bindParams' => [0]);
                    $pending_supplier = $objuser->getPendingUserDetails($where);
                    return Datatables::of($pending_supplier)->addColumn('status', function ($pending_supplier) {
                        return ' < td style = "text-align: center" >
                                            <button class="btn btn-primary customer-status"
                                                    data - id = ' . $pending_supplier->id . ' > Pending
                                            </button >

                                    </td > ';
                    })->removeColumn('name')->removeColumn('updated_at')->make();
                    break;
                case 'deletedSupplier':
                    $objuser = User::getInstance();
                    $where = array('rawQuery' => 'role = ?', 'bindParams' => [3]);
                    $status = array('rawQuery' => 'status = ?', 'bindParams' => [4]);
                    $deleted_supplier = $objuser->getDeletedUserDetails($where, $status);
                    return Datatables::of($deleted_supplier)->addColumn('status', function ($deleted_supplier) {
                        return ' < td style = "text-align: center" >
                                            <button class="btn btn-primary customer-status"
                                                    data - id = ' . $deleted_supplier->id . ' > Deleted
                                            </button >

                                    </td > ';
                    })->removeColumn('name')->removeColumn('updated_at')->make();
                    break;
                case 'changeSupplierStatus':
                    $userId = $inputData['UserId'];
                    $whereForUpdate = ['rawQuery' => 'id =?', 'bindParams' => [$userId]];
                    $dataToUpdate['status'] = $inputData['status'];
                    $updateResult = $ObjUser->updateUserWhere($dataToUpdate, $whereForUpdate);
                    if ($updateResult == 1) {
                        echo json_encode(['status' => 'success', 'msg' => 'Status has been changed . ']);
                    } else {
                        echo json_encode(['status' => 'error', 'msg' => 'Something went wrong, please reload the page and try again . ']);
                    }
                    break;
                case 'deleteSupplierStatus':
                    $userId = $inputData['UserId'];
                    $where = ['rawQuery' => 'id = ?', 'bindParams' => [$userId]];
                    $deleteStatus = $ObjUser->deleteUserDetails($where);
                    if ($deleteStatus) {
                        echo json_encode(['status' => 'success', 'msg' => 'User Deleted']);
                    } else {
                        echo json_encode(['status' => 'error', 'msg' => 'Something went wrong, please reload the page and try again . ']);
                    }
                    break;
                case "locationinfo":
                    $where = ['rawQuery' => 'location_type = ?', 'bindParams' => [0]];
                    $locationdetail = $objModelLocation->getAllCountryDetails($where);
                    echo json_encode($locationdetail);
                    break;
                case 'stateInfoByLocation':
                    $countryId = $request->input('countryId');
                    $where = array('rawQuery' => 'parent_id = ? and location_type = ?', 'bindParams' => [$countryId, 1]);
                    $stateInfo = $objModelLocation->getStateByCountryId($where);
                    echo json_encode($stateInfo);
                    break;
                case 'cityInfoByLocation':
                    $countryId = $request->input('countryId');
                    $where = array('rawQuery' => 'parent_id = ? and location_type = ?', 'bindParams' => [$countryId, 1]);
                    $cityInfo = $objModelLocation->getCityByCountryId($where);
                    echo json_encode($cityInfo);
                    break;
                case 'getUsermetaInfoByUserId':
                    $UserId = $request->input('UserId');
                    $where = array('rawQuery' => 'user_id = ?', 'bindParams' => [$UserId]);
                    $selectedColumns = ['location.*', 'usersmeta.addressline1', 'usersmeta.addressline2', 'usersmeta.city', 'usersmeta.state', 'usersmeta.zipcode', 'usersmeta.phone', 'usersmeta.user_id'];
                    $userMetaInfo = $ObjUsermeta->getUserMetaInfoByUserId($where, $selectedColumns);
                    //                    echo"<pre>";print_r($userMetaInfo);die("Cfh");
                    echo json_encode($userMetaInfo);
                    break;
                default:
                    break;
            }
        }
    }
 public function editLanguage(Request $request, $lid)
 {
     $response = new stdClass();
     $ObjLanguageModel = Languages::getInstance();
     $ObjLocationModel = Location::getInstance();
     $postData = $request->all();
     //
     if ($request->isMethod('GET')) {
         $where = ['rawQuery' => 'location_type = ?', 'bindParams' => [0]];
         $countrydetails = $ObjLocationModel->getAllCountryDetails($where);
         $where = ['rawQuery' => 'lang_id = ?', 'bindParams' => [$lid]];
         $selectedColumns = ['location.name as location_name', 'languages.*'];
         $languagedetails = $ObjLanguageModel->getAllLanguageDetails($where, $selectedColumns);
         return view('Admin/Views/administration/editLanguage', ['countrydetail' => $countrydetails, 'languagedetails' => $languagedetails[0]]);
     } elseif ($request->isMethod('POST')) {
         //            echo"<pre>";print_r($request->all());die("dgvf");
         $data['lang_code'] = $postData['lang_code'];
         $data['name'] = $postData['name'];
         $data['country_code'] = $postData['country_code'];
         // $data['status'] = $postData['statact'];
         $where = ['rawQuery' => 'lang_id = ?', 'bindParams' => [$lid]];
         //$where['id'] = $mid;
         $updateUser = $ObjLanguageModel->updateLanguageStatus($data, $where);
         //            echo"<pre>";print_r($updateUser);die("cfh");
         if ($updateUser) {
         }
     }
 }