/**
  * List Ward by District id ajax.
  */
 public function getStreetsByDistrict($district_id)
 {
     $streets = Street::where('district_id', $district_id)->orderBy('priority')->get();
     return $streets->toArray();
 }
 /**
  * Add maintenance data from file which is on external url
  * @param Array
  */
 public static function addMaintenanceDataFromFile($url = null)
 {
     $updatedStreets = [];
     if (is_null($url)) {
         return $updatedStreets;
     }
     //read file
     $file = static::readExternalApiData($url);
     if ($file) {
         while (!feof($file)) {
             //read lines
             $row = fgetcsv($file, 0, ",");
             // example line: 2016-01-22,"Mikael Lybeckin katu","20-2 ja 20 vp-2 vp",00250,60.1858333,24.9124984
             // a bit of cleaning up
             foreach ($row as $key => $value) {
                 $row[$key] = utf8_encode($value);
             }
             //update street
             // needs to be like, otherwise some characters might end up mess it
             // maybe another solution as well "Tarkk'ampujakatu" was the problematic
             $street = Street::where('name', 'like', $row[1])->first();
             if (!is_null($street)) {
                 $street->update(['next_maintenance' => $row[0], 'info' => $row[2], 'lat' => $row[4], 'lng' => $row[5]]);
                 $updatedStreets[] = $street;
             }
         }
         fclose($file);
     }
     return $updatedStreets;
 }
Beispiel #3
0
 /**
  * Returns the name of a street
  * @param $id
  * @return mixed
  */
 public function getStreetName($id)
 {
     $street = $this->street->where('id', '=', $id)->first();
     return $street->streetKeyName;
 }
 public function saveAddress(Request $request, $territoryId = null, $addressId = null)
 {
     if (!$this->hasAccess($request)) {
         return Response()->json(['error' => 'Access denied.'], 500);
     }
     if (empty($territoryId)) {
         return ['error' => 'Territory not found', 'message' => 'Territory not found'];
     }
     if (Gate::denies('update-addresses')) {
         return Response()->json(['error' => 'Method not allowed'], 403);
     }
     if (!empty($addressId)) {
         try {
             $newAddress = $this->unTransform($request->all(), 'address');
             $address = Address::findOrFail($addressId);
             $street = Street::findOrFail($address->street_id)->first();
             // if(!$street->is_apt_building)
             // $newAddress['lat'] = 0;
             $data = $address->update($newAddress);
         } catch (Exception $e) {
             $data = ['error' => 'Address not updated', 'message' => $e->getMessage()];
         }
     } else {
         // dd($request->all());
         // dd($this->unTransform($request->all(), 'address'));
         try {
             // return ['data' => ['street_street' => empty($request->input('street_street')), 'all' => $request->all()]];
             $transformedData = $this->unTransform($request->all(), 'address');
             if (!empty($request->input('street_street'))) {
                 $transformedData['street'] = [['street' => $request->input('street_street'), 'isAptBuilding' => $request->input('street_isAptBuilding')]];
             }
             // return ['data' => $transformedData];
             $territory = Territory::findOrFail($territoryId);
             if (!empty($transformedData['street'])) {
                 $street = Street::where('street', $transformedData['street'][0]['street'])->first();
                 if (empty($street)) {
                     $street = Street::create($transformedData['street'][0]);
                 }
                 // $addressWithStreet = ($address && $street) ? $address->street()->associate($street) : $address;
                 $transformedData['street_id'] = $street ? $street->id : null;
             }
             $address = Address::where(['address' => $transformedData['address'], 'street_id' => $transformedData['street_id'], 'apt' => !empty($transformedData['apt']) ? $transformedData['apt'] : ''])->first();
             // Address alredy exist?
             if (!empty($address)) {
                 // If inactive, make it active
                 // dd($address);
                 if ($address['inactive']) {
                     $address['inactive'] = 0;
                     $data = $address->update(['inactive', $address['inactive']]);
                 }
                 // If in another territory?
                 if ($territoryId != $address->territory_id) {
                     $territoryBelongs = Territory::findOrFail($address->territory_id);
                     // Error: "This address belongs to territory number []"
                     return Response()->json(['error' => 'This address belongs to territory ' . $territoryBelongs->number . '. Please contact Admin about moving this address.', 'data' => ''], 202);
                 } else {
                     return Response()->json(['error' => 'This address already exists in this territory.', 'data' => ''], 202);
                 }
             } else {
                 $address = !empty($territory) ? $territory->addresses()->create($transformedData) : null;
             }
             $data = $address && !empty($transformedData['notes']) ? $address->notes()->create($transformedData['notes'][0]) : $address;
         } catch (Exception $e) {
             $data = ['error' => 'Address not added', 'message' => $e->getMessage()];
             // {"error":"SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '200-23' for key 'addresses_address_street_id_unique' (SQL: insert into `addresses` (`name`, `address`, `street_id`, `territory_id`, `updated_at`, `created_at`) values (Jean Marc, 200, 23, 34, 2016-01-14 18:36:49, 2016-01-14 18:36:49))"}
         }
     }
     return ['data' => $data];
 }
 protected static function restoreTerritory($data)
 {
     $territory = Territory::where('number', $data->terrNumber)->first();
     // var_dump($territory['id']); exit;
     if (empty($territory)) {
         $territory = Territory::create(['number' => $data->terrNumber, 'location' => $data->street]);
     }
     // var_dump($territory['id']); exit;
     if (!empty($territory['id'])) {
         $bad = ['STREET', 'COURT', 'PLACE', '(PARTIE SUD)', '(PARTIE NORD)', '(BILDING)', '(BUILDING)'];
         $good = ['ST', 'CT', 'PL', '', '', '', ''];
         foreach ($data->addresses as $i => $add) {
             $streetEntry = str_replace($bad, $good, strtoupper(Address::getStreet($add->address)));
             $street = Street::where('street', $streetEntry)->first();
             // var_dump($street); exit;
             if (empty($street)) {
                 /*
                 				$street = Street::create([
                 					'street' => $streetEntry,
                 					'is_apt_building' => 0
                 				]);
                 */
                 // else
                 var_dump($streetEntry);
             }
             // var_dump($street); exit;
             $addressEntry = strtoupper(trim(str_replace($streetEntry, '', $add->address)));
             $address = Address::where(['address' => $addressEntry, 'street_id' => $street['id']])->first();
             if (empty($address)) {
                 /*
                 				$address = Address::create([
                 					'territory_id' => $territory['id'],
                 					'street_id' => $street['id'],
                 					'address' => $addressEntry,
                 					'name' => $add->name,
                 					'phone' => $add->phone
                 				]);
                 */
                 // else
                 var_dump([$addressEntry, $address]);
             }
             // var_dump($address); exit;
         }
     }
 }