/**
  * Returns the country analytics
  */
 public function countryAnalytics(Request $request, $id, $rangeFrom, $rangeTo, $unit)
 {
     $url = Url::find($id);
     if (empty($url)) {
         echo 'Not Found';
         return;
     }
     Utility::getUnit($unit);
     $countryData = $url->hits()->whereBetween('created_at', array($rangeFrom, $rangeTo))->select(\DB::raw('count(*) as count, country_iso_code, country'))->groupBy('country_iso_code')->get();
     echo json_encode($countryData);
     return;
 }
 public function syncData()
 {
     if (Auth::check()) {
         $user_id = Auth::user()->id;
         $store_name = Utility::get_store_name($user_id);
         $shopify = new Shopify(Token::$shopify_api_key, Token::$shopify_secret_key, $store_name);
         $rAllProducts_s = $shopify->get_all_products('GET');
         $rVariants = array();
         $sValues = "";
         $rInsert = array();
         foreach ($rAllProducts as $rP) {
             foreach ($rP as $rPP) {
                 $rVariants[] = $rPP['variants'];
             }
         }
         foreach ($rVariants as $rV) {
             foreach ($rV as $rVV) {
                 $rInsert[] = array('sku' => $rVV['sku'], 'product_name' => $rVV['title'], 'quantity' => $rVV['inventory_quantity'], 'price' => $rVV['price']);
             }
         }
         //print_r($rInsert);
         $rProd = Products::where('user_id', '=', $user_id)->get();
         $rProd = json_decode(json_encode($rProd), true);
         //Make SKU the key
         $rData = array();
         foreach ($rProd as $rP) {
             $key = $rP['sku'];
             $rData[$key]['product_name'] = $rP['product_name'];
             $rData[$key]['quantity'] = $rP['quantity'];
             $rData[$key]['price'] = $rP['price'];
         }
         $rDiff = array();
         $rNew = array();
         //print_r($rInsert);
         //die();
         //Compare both arrays now to see any difference and get the difference in new array
         foreach ($rInsert as $rI) {
             if (isset($rData[$rI['sku']])) {
                 //Check for product_name, quantity,price
                 if ($rI['product_name'] != $rData[$rI['sku']]['product_name']) {
                     $rDiff[$rI['sku']]['product_name'] = $rI['product_name'];
                 }
                 if ($rI['quantity'] != $rData[$rI['sku']]['quantity']) {
                     $rDiff[$rI['sku']]['quantity'] = $rI['quantity'];
                 }
                 if ($rI['price'] != $rData[$rI['sku']]['price']) {
                     $rDiff[$rI['sku']]['price'] = $rI['price'];
                 }
             } else {
                 $rNew[$rI['sku']]['product_name'] = $rI['product_name'];
                 $rNew[$rI['sku']]['quantity'] = $rI['quantity'];
                 $rNew[$rI['sku']]['price'] = $rI['price'];
             }
         }
         //Update each changed entry
         foreach ($rDiff as $rK => $rV) {
             $sQ = "update lookup_products set ";
             foreach ($rV as $rKK => $rVV) {
                 $sQ .= "{$rKK} = '{$rVV}',";
             }
             $sQ = substr($sQ, 0, -1);
             $sQ .= " where sku = ?";
             DB::update($sQ, [$rK]);
         }
         //Add new Entry
         foreach ($rNew as $rK => $rV) {
             $sQ = "insert into lookup_products (sku,product_name,quantity,price,user_id) values (?, ?, ?, ?, ?)";
             DB::insert($sQ, [$rK, $rV['product_name'], $rV['quantity'], $rV['price'], $user_id]);
         }
     } else {
         echo 'You must login first';
         return view('auth/login');
     }
     return view('products.index', ['rProd' => Utility::get_slite_products($user_id)]);
 }