Skip to content

Commit

Permalink
Update model for location #42
Browse files Browse the repository at this point in the history
  • Loading branch information
drewroberts authored Apr 10, 2021
1 parent d989cac commit 0eafb2c
Showing 1 changed file with 21 additions and 52 deletions.
73 changes: 21 additions & 52 deletions src/Models/Location.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected static function boot()
parent::boot();

static::creating(function (Location $location) {
$location->slug = $location->slug ?: Str::slug($location->city);
$location->slug = $location->slug ?: Str::slug($location->name);
if ($location->page_id) {
$location->page->update($location->pageFields());
} else {
Expand All @@ -59,7 +59,7 @@ protected static function boot()
Assert::lazy()
->that($location->market_id)->notEmpty('A location must be in a market.')
->verifyNow();
$location->timezone_id = $location->timezone_id ?: Timezone::fromAbbreviation('EST');
$location->timezone_id = $location->timezone_id ?: $location->market->timezone->id;

if (empty($location->abbreviation)) {
do {
Expand Down Expand Up @@ -87,11 +87,6 @@ private function pageFields(): array
];
}

public function address()
{
return $this->hasOne(app('domestic_address'));
}

public function market()
{
return $this->belongsTo(app('market'));
Expand All @@ -102,6 +97,21 @@ public function page()
return $this->belongsTo(app('page'));
}

public function timezone()
{
return $this->belongsTo(app('timezone'));
}

public function address()
{
return $this->belongsTo(app('domestic_address'));
}

public function phone()
{
return $this->belongsTo(app('phone'));
}

public function gmbAccount()
{
return $this->belongsTo(app('gmb_account'));
Expand All @@ -114,24 +124,14 @@ public function manager()

public function email()
{
return $this->hasOne(app('email_address'));
}

public function contactEmail()
{
return $this->hasOne(app('email_address'), 'contact_email_id');
return $this->belongsTo(app('email_address'), 'contact_email_id');
}

public function contacts()
{
return $this->hasMany(app('contact'));
}

public function phone()
{
return $this->hasOne(app('phone'));
}

public function users()
{
return $this->belongsToMany(app('user'))->withTimestamps();
Expand Down Expand Up @@ -169,7 +169,7 @@ public function signatures()

public function competitor()
{
return $this->hasOne(app('competitor'));
return $this->belongsTo(app('competitor'));
}

public function snapshots()
Expand Down Expand Up @@ -368,58 +368,27 @@ public function findOrGenerateSlot($slotNumber)
return $slot;
}

/**
* Get current date time at location.
*
* @return Carbon
*/
public function getCurrentDateTime()
{
return Carbon::now($this->getPhpTzAttribute());
}

/**
* Get a string for the php timezone of the location.
*
* @return string
*/
public function getPhpTzAttribute()
{
if ($this->timezone == 'CST') {
return 'America/Chicago';
}

return 'America/New_York';
return $this->timezone->php;
}

/**
* Get data/time at location.
*
* @return Carbon
*/
public function currentDateTime()
{
return Carbon::now()->setTimeZone($this->php_tz);
}

/**
* Apply location timezone to data/time.
*
* @param Carbon|string $dateTime
* @return Carbon
*/
public function toLocalDateTime($dateTime)
{
return Carbon::parse($dateTime)->setTimeZone($this->php_tz);
}

/**
* Apply UTC timezone to local data/time.
*
* @param Carbon|string $dateTime
* @return Carbon
*/
public function toUtclDateTime($dateTime)
public function toUtcDateTime($dateTime)
{
return Carbon::parse($dateTime, $this->php_tz)->setTimeZone('UTC');
}
Expand Down

0 comments on commit 0eafb2c

Please sign in to comment.