public static function saveDistrictCityFromLatLng($lat, $lng, $accId, $isCashier)
 {
     $lat = trim($lat);
     $lng = trim($lng);
     if (self::IsNullOrEmptyString($lat) || self::IsNullOrEmptyString($lng)) {
         return;
     }
     $url = "https://maps.googleapis.com/maps/api/geocode/json?latlng={$lat},{$lng}&result_type=administrative_area_level_3&key=" . self::$geoCodingServerKey;
     $json = json_decode(file_get_contents($url), true);
     $addressComponents = $json['results'][0]['address_components'];
     if (count($addressComponents) < 2) {
         return;
     }
     $district = strtoupper($addressComponents[0]['long_name']);
     $city = strtoupper($addressComponents[1]['long_name']);
     $loc = new LocationModel();
     $arrLoc = $loc->getWhere("district='{$district}' AND city='{$city}'");
     if (count($arrLoc) == 0) {
         $l = new LocationModel();
         $l->district = $district;
         $l->city = $city;
         if ($isCashier) {
             $l->cashier_visit_count = 1;
             $l->user_visit_count = 0;
         } else {
             $l->cashier_visit_count = 0;
             $l->user_visit_count = 1;
         }
         $l->visit_count = 1;
         $l->save();
     } else {
         $l = new LocationModel();
         $l->getByID($arrLoc[0]->id_location);
         if ($isCashier) {
             $l->cashier_visit_count = $l->cashier_visit_count + 1;
         } else {
             $l->user_visit_count = $l->user_visit_count + 1;
         }
         $l->save();
     }
     if ($accId != 0 && !$isCashier) {
         $user = new UserModel();
         $user->getByID($accId);
         $user->last_lat = $lat;
         $user->last_long = $lng;
         $user->last_city = $city;
         $user->last_district = $district;
         if (Generic::IsNullOrEmptyString($user->latitude)) {
             $user->latitude = $lat;
         }
         if (Generic::IsNullOrEmptyString($user->longitude)) {
             $user->longitude = $lng;
         }
         if (Generic::IsNullOrEmptyString($user->district)) {
             $user->district = $district;
         }
         if (Generic::IsNullOrEmptyString($user->city)) {
             $user->city = $city;
         }
         $user->save();
     }
 }
 public function constraints()
 {
     $err = array();
     if (!isset($this->name)) {
         $err['name'] = Lang::t('Name cannot be empty');
     }
     if (!isset($this->address)) {
         $err['address'] = Lang::t('address cannot be empty');
     }
     if (!isset($this->district)) {
         $err['district'] = Lang::t('District cannot be empty');
     }
     if (!isset($this->city)) {
         $err['city'] = Lang::t('City cannot be empty');
     }
     if (!isset($this->email)) {
         $err['email'] = Lang::t('Email cannot be empty');
     }
     if (!isset($this->phone_no)) {
         $err['phone_no'] = Lang::t('phone no cannot be empty');
     }
     if (!isset($this->time_open)) {
         $err['time_open'] = Lang::t('Time open cannot be empty');
     }
     if (!isset($this->time_close)) {
         $err['time_close'] = Lang::t('Time Close cannot be empty');
     }
     if (!isset($this->halal)) {
         $err['halal'] = Lang::t('Halal cannot be empty');
     }
     //Set Default Values
     if (count($err) <= 0) {
         //SAVING NEW LOCATION TO Location DB
         $ll = new LocationModel();
         $arrLoc = $ll->getWhere("district='{$this->district}' AND city='{$this->city}'");
         if (count($arrLoc) <= 0) {
             $loc = new LocationModel();
             $loc->district = $this->district;
             $loc->city = $this->city;
             $loc->user_visit_count = 0;
             $loc->cashier_visit_count = 0;
             if (!Generic::IsNullOrEmptyString($this->district) && !Generic::IsNullOrEmptyString($this->city)) {
                 $loc->save();
             }
         }
         //MAKE id_categories always have 0
         if (Generic::IsNullOrEmptyString($this->id_categories)) {
             $this->id_categories;
         } else {
             $arrCats = explode(',', $this->id_categories);
             if (!in_array("0", $arrCats)) {
                 $arrCats[] = "0";
                 $this->id_categories = implode(',', $arrCats);
             }
         }
     }
     return $err;
 }