/**
  * @param Request $request
  */
 public function productAjaxHandler(Request $request)
 {
     $method = $request->input('method');
     $response = new stdClass();
     if ($method != "") {
         switch ($method) {
             case 'productsizingdetails':
                 $postData = $request->all();
                 if ($postData) {
                     $objProductmetaModel = new Productmeta();
                     $objUserModel = new User();
                     $userId = '';
                     if (isset($postData['id'])) {
                         $userId = $postData['id'];
                     }
                     $whereForloginToken = $userId;
                     $productmetaId = '';
                     if (isset($postData['productmeta_id'])) {
                         $productmetaId = $postData['productmeta_id'];
                     }
                     $mytoken = 0;
                     $authflag = false;
                     if (isset($postData['mytoken'])) {
                         $mytoken = $postData['mytoken'];
                         if ($mytoken == env("API_TOKEN")) {
                             $authflag = true;
                         } else {
                             if ($userId != '') {
                                 DB::setFetchMode(PDO::FETCH_ASSOC);
                                 $Userscredentials = $objUserModel->getUsercredsWhere($whereForloginToken);
                                 if ($mytoken == $Userscredentials['login_token']) {
                                     $authflag = true;
                                 }
                             }
                         }
                     }
                     if ($authflag) {
                         if ($productmetaId != '') {
                             DB::setFetchMode(PDO::FETCH_ASSOC);
                             $productsizeDetails = $objProductmetaModel->getProductsizeDetails($productmetaId);
                             $data = array();
                             foreach ($productsizeDetails as $sizekey => $sizeval) {
                                 $presentTime = time();
                                 $sizeval['discountFlag'] = 0;
                                 if ($sizeval['discount_value'] > 0) {
                                     $disountFlag = TRUE;
                                     if ($sizeval['available_from'] != '' || $sizeval['available_upto'] != '') {
                                         if ($sizeval['available_from'] != '' && $sizeval['available_from'] > $presentTime) {
                                             $disountFlag = FALSE;
                                         }
                                         if ($sizeval['available_upto'] != '' && $sizeval['available_upto'] < $presentTime) {
                                             $disountFlag = FALSE;
                                         }
                                     }
                                     if ($disountFlag) {
                                         $discountedValue = 0;
                                         $productPrice = (int) $sizeval['price'];
                                         if ($sizeval['discount_type'] == 1) {
                                             $discountedValue = $productPrice - (int) $sizeval['discount_value'];
                                         }
                                         if ($sizeval['discount_type'] == 2) {
                                             $discountedValue = $productPrice - (int) ($productPrice * ((int) $sizeval['discount_value'] / 100));
                                         }
                                         $data[$sizekey] = $sizeval;
                                         //['productsizeDetails']
                                         $data[$sizekey]['discountedprice'] = $discountedValue;
                                         $data[$sizekey]['discountFlag'] = 1;
                                     }
                                 }
                             }
                             $response->code = 200;
                             $response->message = "Success";
                             $response->data = $data;
                         } else {
                             $response->code = 100;
                             $response->message = "Something went Wrong. No Product Details found.";
                             $response->data = null;
                         }
                     } else {
                         $response->code = 401;
                         $response->message = "Access Denied";
                         $response->data = null;
                     }
                 } else {
                     $response->code = 100;
                     $response->message = "Something went Wrong. No Details for Post.";
                     $response->data = null;
                 }
                 echo json_encode($response, true);
                 break;
             case 'getCategoryForMenu':
                 $ObjProductCategoryModel = new ProductCategories();
                 $objuser = new User();
                 $API_TOKEN = env('API_TOKEN');
                 if ($request->isMethod("POST")) {
                     $postData = $request->all();
                     if (isset($postData['api_token'])) {
                         $apitoken = $postData['api_token'];
                     }
                     if ($apitoken == $API_TOKEN) {
                         $ObjProductCategoryModel = ProductCategories::getInstance();
                         $where = ['rawQuery' => 'category_status = ? AND is_visible = ?', 'bindParams' => [1, 'Y']];
                         $selectColumn = ['product_categories.*', DB::raw('GROUP_CONCAT(product_categories.category_id)AS category_ids'), DB::raw('GROUP_CONCAT(product_categories.category_name)AS category_names')];
                         //                            $cacheKey = "product_categories::" . implode('-', array_flatten($where));
                         //                            if (cacheGet($cacheKey)) {
                         //                                $categoryInfo = cacheGet($cacheKey);
                         //                            } else {
                         $categoryInfo = $ObjProductCategoryModel->getAllCategories($where, $selectColumn);
                         //echo'                        <pre>';print_r($categoryInfo);die("sdfv");
                         //                            $com = array();
                         //                            foreach($categoryInfo as $key => $val){
                         //                                 if($val->parent_category_id != 0 && $val->category_id) {
                         //                                     $com[$key] = $val->category_name;
                         //                                 }
                         //                            }
                         //    echo'<pre>';print_r($com);die("dxg");
                         //                                cacheForever($cacheKey, $categoryInfo);
                         //                            }
                         if ($categoryInfo) {
                             $response->code = 200;
                             $response->message = "Success";
                             $response->data = $categoryInfo;
                         } else {
                             $response->code = 400;
                             $response->message = "No user Details found.";
                             $response->data = null;
                         }
                     } else {
                         $response->code = 401;
                         $response->message = "Access Denied";
                         $response->data = null;
                     }
                 } else {
                     $response->code = 401;
                     $response->message = "Invalid request";
                     $response->data = null;
                 }
                 echo json_encode($response, true);
                 break;
             default:
                 break;
         }
     }
 }
 /**
  * Flashsale Ajax Handler
  * @param Request $request
  * @author: Vini Dubey<*****@*****.**>
  */
 public function flashsaleAjaxHandler(Request $request)
 {
     $method = $request->input('method');
     $objUserModel = new User();
     $objCategoryModel = ProductCategories::getInstance();
     $objProductModel = Products::getInstance();
     $objOptionVariant = ProductOptionVariants::getInstance();
     $objCampaigns = Campaigns::getInstance();
     if ($method != "") {
         switch ($method) {
             case 'optionVariantDetails':
                 $postData = $request->all();
                 $response = new stdClass();
                 if ($postData) {
                     $userId = '';
                     if (isset($postData['id'])) {
                         $userId = $postData['id'];
                     }
                     $productId = '';
                     if (isset($postData['variant_id'])) {
                         $variantId = $postData['variant_id'];
                     }
                     if (isset($postData['product_id'])) {
                         $productId = $postData['product_id'];
                     }
                     if (isset($postData['selectedCombination'])) {
                         $selectedCombination = $postData['selectedCombination'];
                     }
                     $mytoken = '';
                     $authflag = false;
                     if (isset($postData['api_token'])) {
                         $mytoken = $postData['api_token'];
                         if ($mytoken == env("API_TOKEN")) {
                             $authflag = true;
                         } else {
                             if ($userId != '') {
                                 $whereForloginToken = $userId;
                                 $Userscredentials = $objUserModel->getUsercredsWhere($whereForloginToken);
                                 if ($mytoken == $Userscredentials['login_token']) {
                                     $authflag = true;
                                 }
                             }
                         }
                     }
                     $variantDetails = '';
                     if ($authflag) {
                         $where = ['rawQuery' => 'product_option_variants_combination.product_id = ? AND product_option_variants_combination.variant_ids IN("' . $selectedCombination . '","' . strrev($selectedCombination) . '")', 'bindParams' => [$productId]];
                         $selectedColumn = ['product_option_variants.*', 'product_images.*', 'product_option_variants_combination.*', 'product_option_variant_relation.*', DB::raw('GROUP_CONCAT(
                 CASE
                 WHEN ((SELECT COUNT(pi_id) FROM product_images  WHERE product_images.for_combination_id !="0")!=0)
                 THEN
                     CASE
                         WHEN (product_images.image_type =1 AND (product_images.for_combination_id!=0 OR product_images.for_combination_id!=""))
                         THEN product_images.image_type
                      END
                  ELSE  product_images.image_type
                 END) AS image_types'), DB::raw('GROUP_CONCAT(DISTINCT
                 CASE
                 WHEN ((SELECT COUNT(pi_id) FROM product_images  WHERE product_images.for_combination_id !="0")!=0)
                 THEN
                     CASE
                         WHEN (product_images.image_type =1 AND (product_images.for_combination_id!=0 OR product_images.for_combination_id!=""))
                         THEN product_images.image_url
                      END
                  ELSE  product_images.image_url
                 END) AS image_urls'), DB::raw('GROUP_CONCAT(DISTINCT product_option_variants_combination.variant_ids) AS variant_ids_combination'), DB::raw('GROUP_CONCAT(DISTINCT product_option_variant_relation.variant_ids) AS variant_id')];
                         $optionVariantDetailsForPopUp = $objOptionVariant->getOptionVariantDetailsForPopup($where, $selectedColumn);
                         //  echo'<pre>';print_r($optionVariantDetailsForPopUp);die("fchb");
                         if ($optionVariantDetailsForPopUp[0]) {
                             $response->code = 200;
                             $response->message = "Success";
                             $response->data = $optionVariantDetailsForPopUp[0];
                         } else {
                             $response->code = 100;
                             $response->message = "Something went Wrong. No Product Details found.";
                             $response->data = null;
                         }
                     } else {
                         $response->code = 401;
                         $response->message = "Access Denied";
                         $response->data = null;
                     }
                 } else {
                     $response->code = 401;
                     $response->message = "Invalid request";
                     $response->data = null;
                 }
                 echo json_encode($response, true);
             case 'getCampaignsForMenu':
                 $postData = $request->all();
                 $response = new stdClass();
                 if ($postData) {
                     $userId = '';
                     if (isset($postData['id'])) {
                         $userId = $postData['id'];
                     }
                     $mytoken = '';
                     $authflag = false;
                     if (isset($postData['api_token'])) {
                         $mytoken = $postData['api_token'];
                         if ($mytoken == env("API_TOKEN")) {
                             $authflag = true;
                         } else {
                             if ($userId != '') {
                                 $whereForloginToken = $userId;
                                 $Userscredentials = $objUserModel->getUsercredsWhere($whereForloginToken);
                                 if ($mytoken == $Userscredentials['login_token']) {
                                     $authflag = true;
                                 }
                             }
                         }
                     }
                     $variantDetails = '';
                     if ($authflag) {
                         $where = ['rawQuery' => 'available_from < ? AND available_upto > ? AND campaign_status = ?', 'bindParams' => [time(), time(), 1]];
                         $selectedColumns = ['Campaigns.*'];
                         $campaignDetails = $objCampaigns->getFlashsaleDetail($where, $selectedColumns);
                         $campData = [];
                         foreach ([1 => 'DS', 2 => 'FS'] as $index => $item) {
                             $campData[$item] = implode(",", array_unique(array_flatten(array_filter(array_map(function ($camp) use($index) {
                                 if ($camp->campaign_type == $index) {
                                     return array_unique(array_merge(array_keys(json_decode($camp->for_category_ids, true)), array_flatten(json_decode($camp->for_category_ids, true))));
                                 } else {
                                     return null;
                                 }
                             }, $campaignDetails)))));
                         }
                         foreach ([1 => 'DS', 2 => 'FS'] as $index => $item) {
                             $campDatasForCampaignName[$item] = implode(",", array_unique(array_flatten(array_filter(array_map(function ($campDatasForCampaignName) use($index) {
                                 if ($campDatasForCampaignName->campaign_type == $index) {
                                     //                                        return array_unique(array_merge(array_keys(json_decode($camp->for_category_ids, true)), array_flatten(json_decode($camp->for_category_ids, true))));
                                     return $campDatasForCampaignName->campaign_banner;
                                 } else {
                                     return null;
                                 }
                             }, $campaignDetails)))));
                         }
                         $where = ['rawQuery' => 'category_status = ? AND category_id IN(' . implode(',', array_unique(explode(',', implode(',', $campData)))) . ')', 'bindParams' => [1]];
                         $selectColumn = ['product_categories.*'];
                         $categoryInfo = $objCategoryModel->getCategoryWhere($where, $selectColumn);
                         $final['categoryInfo'] = $categoryInfo;
                         $final['campaignCatId'] = $campData;
                         $final['campName'] = $campDatasForCampaignName;
                         //                            echo'<pre>';print_r($final);die("fgvj");
                         if ($final) {
                             $response->code = 200;
                             $response->message = "Success";
                             $response->data = $final;
                         } else {
                             $response->code = 100;
                             $response->message = "Something went Wrong. No Product Details found.";
                             $response->data = null;
                         }
                     } else {
                         $response->code = 401;
                         $response->message = "Access Denied";
                         $response->data = null;
                     }
                 } else {
                     $response->code = 401;
                     $response->message = "Invalid request";
                     $response->data = null;
                 }
                 echo json_encode($response, true);
         }
     }
 }
 /**
  * Get all the products based on filtering and category selection.
  * Gets product listing
  * Category,Subcategory,Filters & Feature Variant name,Sort By.
  * @param Request $request
  * @author: Vini Dubey<*****@*****.**>
  * @since: 05/05/2016
  */
 public function productList(Request $request)
 {
     $postData = $request->all();
     $response = new stdClass();
     $objUserModel = new User();
     if ($postData) {
         $userId = '';
         if (isset($postData['id'])) {
             $userId = $postData['id'];
         }
         $mytoken = '';
         $authflag = false;
         if (isset($postData['api_token'])) {
             $mytoken = $postData['api_token'];
             if ($mytoken == env("API_TOKEN")) {
                 $authflag = true;
             } else {
                 if ($userId != '') {
                     $whereForloginToken = $userId;
                     $Userscredentials = $objUserModel->getUsercredsWhere($whereForloginToken);
                     if ($mytoken == $Userscredentials['login_token']) {
                         $authflag = true;
                     }
                 }
             }
         }
         if ($authflag) {
             //LOGIN TOKEN
             //                if (isset($postData['option']) && isset($postData['limit']) && isset($postData['page_number'])) {
             if (isset($postData['option']) && isset($postData['limit']) && isset($postData['page_number'])) {
                 $objProductModel = Products::getInstance();
                 $objProductCategoryModel = ProductCategories::getInstance();
                 $wherePriceRange = ['rawQuery' => 1];
                 if (isset($postData['price_range_from']) && !empty($postData['price_range_from']) && isset($postData['price_range_upto']) && !empty($postData['price_range_upto'])) {
                     $priceFrom = $postData['price_range_from'];
                     $priceTo = $postData['price_range_upto'];
                     $wherePriceRange = ['rawQuery' => 'price_total >= ' . $priceFrom . ' AND price_total <= ' . $priceTo . ''];
                 }
                 //                    $sortClause = "products.product_id DESC";
                 //                    $sortClause = ('products.product_id desc');
                 $sortClause = ['products.product_id' => 'desc'];
                 if (isset($postData['sort_by']) && !empty($postData['sort_by'])) {
                     $sortBy = $postData['sort_by'];
                     switch ($sortBy) {
                         case "null-asc":
                             //                                $sortClause = ('products.product_id desc');
                             $sortClause = ['products.product_id' => 'asc'];
                             break;
                         case "timestamp-asc":
                             //                                $sortClause = ('products.product_id desc');
                             $sortClause = ['products.product_id' => 'asc'];
                             break;
                         case "position-asc":
                             //                                $sortClause = ('products.product_id desc');
                             $sortClause = ['products.product_id' => 'asc'];
                             break;
                         case "position-desc":
                             //                                $sortClause = ('products.product_id desc');
                             $sortClause = ['products.product_id' => 'asc'];
                             break;
                         case "price-asc":
                             //                                $sortClause = ('products.product_id desc');
                             $sortClause = ['products.price_total' => 'asc'];
                             break;
                         case "price-desc":
                             //                                $sortClause = ('products.product_id desc');
                             $sortClause = ['products.price_total' => 'desc'];
                             break;
                         case "popularity-asc":
                             //                                $sortClause = ('products.product_id desc');
                             $sortClause = ['products.price_total' => 'asc'];
                             break;
                         case "bestsellers-asc":
                             //                                $sortClause = ('products.product_id desc');
                             $sortClause = ['products.product_id' => 'asc'];
                             break;
                         case "bestsellers-desc":
                             //                                $sortClause = ('products.product_id desc');
                             $sortClause = ['products.product_id' => 'desc'];
                             break;
                         case "on_sale-asc":
                             //                                $sortClause = ('products.product_id desc');
                             $sortClause = ['products.product_id' => 'asc'];
                             break;
                         case "on_sale-desc":
                             //                                $sortClause = ('products.product_id desc');
                             $sortClause = ['products.product_id' => 'desc'];
                             break;
                         case "pricelowtohigh":
                             //                                $sortClause = ('products.price_total asc');
                             $sortClause = ['products.price_total' => 'asc'];
                             break;
                         case "pricehightolow":
                             //                                $sortClause = ('products.price_total desc');
                             $sortClause = ['products.price_total' => 'desc'];
                             break;
                         default:
                             break;
                     }
                 }
                 $limit = $postData['limit'];
                 $pagenumber = $postData['page_number'];
                 if (empty($postData['page_number'])) {
                     $pagenumber = 1;
                 }
                 $categoryName = '';
                 $subcategoryName = '';
                 $whereForCategoryFilter = ['rawQuery' => 1];
                 $objProductModel = Products::getInstance();
                 if (isset($postData['category_name']) && !empty($postData['category_name'])) {
                     $categoryName = $postData['category_name'];
                     if (isset($postData['subcategory_name']) && !empty($postData['subcategory_name'])) {
                         $subcategoryName = $postData['subcategory_name'];
                     }
                     $objCategoryModel = ProductCategories::getInstance();
                     $whereCategoryName = ['rawQuery' => 'category_name = ? AND parent_category_id = ? AND category_status = ?', 'bindParams' => [$categoryName, 0, 1]];
                     $selectedColumn = ['product_categories.*'];
                     $categoryDetails = $objCategoryModel->getCategoryWhere($whereCategoryName, $selectedColumn);
                     if ($categoryDetails) {
                         $categoryTreeIds = $categoryDetails[0]->category_id;
                         $whereForCategoryFilter = ['rawQuery' => 'category_id IN(' . $categoryTreeIds . ')'];
                         $whereForSubcat = ['rawQuery' => 'parent_category_id = ? AND category_status = ?', 'bindParams' => [$categoryDetails[0]->category_id, 1]];
                         $selectedColumn = ['product_categories.*', DB::raw('GROUP_CONCAT(DISTINCT category_id)AS subcatIds')];
                         $allSubcatsInCat = $objCategoryModel->getAllCategoryWhereByGrouping($whereForSubcat, $selectedColumn);
                         if (!empty($allSubcatsInCat)) {
                             $allSubcatsInCatIds = '';
                             $count = 1;
                             foreach ($allSubcatsInCat as $valueAllSubcatsInCat) {
                                 if ($count == 1) {
                                     $allSubcatsInCatIds = $valueAllSubcatsInCat->subcatIds;
                                 } else {
                                     $allSubcatsInCatIds .= "," . $valueAllSubcatsInCat->subcatIds;
                                 }
                                 $count++;
                             }
                             $categoryTreeIds .= "," . $allSubcatsInCatIds;
                             if ($subcategoryName != '') {
                                 $whereForSelectedSubcat = ['rawQuery' => 'category_name = ? AND parent_category_id = ?', 'bindParams' => [$subcategoryName, $categoryDetails[0]->category_id]];
                                 $selectedColumn = ['product_categories.*'];
                                 $selectedSubcatDetails = $objCategoryModel->getCategoryWhere($whereForSelectedSubcat, $selectedColumn);
                                 if ($selectedSubcatDetails) {
                                     $allSubcatsInCatIds = $selectedSubcatDetails[0]->category_id;
                                     $categoryTreeIds = $allSubcatsInCatIds;
                                 }
                             }
                             $whereForCategoryFilter = ['rawQuery' => 'category_id IN(' . $categoryTreeIds . ')'];
                             $selectedColumn = ['product_categories.*', DB::raw('GROUP_CONCAT(DISTINCT category_id)AS subcatIds')];
                             $allSubsubcatsInCat = $objCategoryModel->getAllCategoryWhereByGrouping($whereForCategoryFilter, $selectedColumn);
                             if (!empty($allSubsubcatsInCat)) {
                                 foreach ($allSubsubcatsInCat as $valueAllSubsubcatsInCat) {
                                     $categoryTreeIds .= "," . $valueAllSubsubcatsInCat->subcatIds;
                                 }
                             }
                         }
                     }
                     // For Filter Option and features //
                     $ObjProductFilterOptionModel = ProductFilterOption::getInstance();
                     //                        $where = ['rawQuery' => 'product_filter_option_status = ? AND product_filter_categories REGEXP "^[[:<:]]' . implode("|", array_unique(explode(",", $categoryTreeIds))) . '[[:<:]]"', 'bindParams' => [1]];
                     $where = ['rawQuery' => 'product_filter_option.product_filter_option_status = ? AND product_filter_option.product_filter_category_id REGEXP  "^' . implode("|", array_unique(explode(",", $categoryTreeIds))) . '"', 'bindParams' => [1]];
                     $selectColumn = ['product_filter_option.*', DB::raw('GROUP_CONCAT(DISTINCT pg.product_filter_option_name)AS variant_name'), DB::raw('GROUP_CONCAT(DISTINCT pg.product_filter_option_id)AS variant_ids')];
                     $filterOptionInfo = $ObjProductFilterOptionModel->getAllFilterOption($where, $selectColumn);
                     // End for filter option and feature//
                 }
                 $offset = ((int) $pagenumber - 1) * (int) $limit;
                 $whereOption = ['rawQuery' => 1];
                 if ($postData['option'] != '') {
                     $whereOption = ['rawQuery' => 'product_option_variants.variant_id IN (' . $postData["option"] . ')'];
                     //                                $whereForFilter.= " and pcr.color_id in (" . $postData['selectedcolors'] . ")";
                 }
                 $whereForFilter = $whereOption;
                 $where = ['rawQuery' => 'product_status = ?', 'bindParams' => [1]];
                 $selectedColumn = ['products.*', 'product_images.image_url', 'productmeta.*', DB::raw('GROUP_CONCAT(DISTINCT product_option_variant_relation.option_id)AS option_ids'), DB::raw('GROUP_CONCAT(DISTINCT product_options.option_name)AS option_names'), DB::raw('GROUP_CONCAT(DISTINCT product_option_variant_relation.variant_data  SEPARATOR "____")AS variant_datas'), DB::raw('GROUP_CONCAT(DISTINCT product_option_variants_combination.variant_ids) AS variant_ids_combination')];
                 $productsFiltered = $objProductModel->getProducts($where, $whereForCategoryFilter, $whereForFilter, $limit, $offset, $sortClause, $wherePriceRange, $selectedColumn);
                 $FilterDatas['filterDetails'] = $filterOptionInfo;
                 $FilterDatas['productList'] = $productsFiltered;
                 //                    echo'<pre>';print_r($FilterDatas);die("dv");
                 if ($FilterDatas) {
                     $data = $filterOptionInfo;
                     $response->code = 200;
                     $response->message = "Success";
                     $response->data = $data;
                 } else {
                     $response->code = 100;
                     $response->message = "Something went Wrong. No Product Details found.";
                     $response->data = null;
                 }
             } else {
                 $errorMsg = "No parameters were found.";
                 $response->code = 100;
                 $response->message = $errorMsg;
                 $response->data = null;
             }
         } else {
             $response->code = 401;
             $response->message = "Access Denied";
             $response->data = null;
         }
     } else {
         $response->code = 401;
         $response->message = "Invalid request";
         $response->data = null;
     }
     echo json_encode($response, true);
 }