2016-04-17 00:34:39 +02:00
|
|
|
<?php namespace App\Models;
|
|
|
|
|
2016-12-14 15:19:16 +01:00
|
|
|
use Carbon;
|
|
|
|
use Utils;
|
2016-04-17 00:34:39 +02:00
|
|
|
use Eloquent;
|
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
2016-12-14 15:19:16 +01:00
|
|
|
use Laracasts\Presenter\PresentableTrait;
|
2016-04-17 00:34:39 +02:00
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* Class Company
|
|
|
|
*/
|
2016-04-17 00:34:39 +02:00
|
|
|
class Company extends Eloquent
|
|
|
|
{
|
|
|
|
use SoftDeletes;
|
2016-12-14 15:19:16 +01:00
|
|
|
use PresentableTrait;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $presenter = 'App\Ninja\Presenters\CompanyPresenter';
|
2016-04-17 00:34:39 +02:00
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2016-12-14 15:19:16 +01:00
|
|
|
protected $dates = [
|
|
|
|
'deleted_at',
|
|
|
|
'promo_expires',
|
|
|
|
'discount_expires',
|
|
|
|
];
|
2016-07-03 18:11:58 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
|
|
*/
|
2016-04-17 00:34:39 +02:00
|
|
|
public function accounts()
|
|
|
|
{
|
|
|
|
return $this->hasMany('App\Models\Account');
|
|
|
|
}
|
2016-07-03 18:11:58 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
|
|
*/
|
2016-04-17 00:34:39 +02:00
|
|
|
public function payment()
|
|
|
|
{
|
|
|
|
return $this->belongsTo('App\Models\Payment');
|
|
|
|
}
|
2016-12-14 15:19:16 +01:00
|
|
|
|
|
|
|
public function hasActivePromo()
|
|
|
|
{
|
|
|
|
if ($this->discount_expires) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->promo_expires && $this->promo_expires->gte(Carbon::today());
|
|
|
|
}
|
|
|
|
|
|
|
|
// handle promos and discounts
|
2016-12-15 11:52:10 +01:00
|
|
|
public function hasActiveDiscount(Carbon $date = null)
|
2016-12-14 15:19:16 +01:00
|
|
|
{
|
2017-01-01 19:23:51 +01:00
|
|
|
if ( ! $this->discount || ! $this->discount_expires) {
|
2016-12-14 15:19:16 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-12-15 11:52:10 +01:00
|
|
|
$date = $date ?: Carbon::today();
|
|
|
|
|
2017-01-01 19:23:51 +01:00
|
|
|
if ($this->plan_term == PLAN_TERM_MONTHLY) {
|
|
|
|
return $this->discount_expires->gt($date);
|
|
|
|
} else {
|
|
|
|
return $this->discount_expires->subMonths(11)->gt($date);
|
|
|
|
}
|
2016-12-14 15:19:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function discountedPrice($price)
|
|
|
|
{
|
|
|
|
if ( ! $this->hasActivePromo() && ! $this->hasActiveDiscount()) {
|
|
|
|
return $price;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $price - ($price * $this->discount);
|
|
|
|
}
|
|
|
|
|
2016-12-27 12:44:15 +01:00
|
|
|
public function daysUntilPlanExpires()
|
|
|
|
{
|
|
|
|
if ( ! $this->hasActivePlan()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Carbon::parse($this->plan_expires)->diffInDays(Carbon::today());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function hasActivePlan()
|
|
|
|
{
|
|
|
|
return Carbon::parse($this->plan_expires) >= Carbon::today();
|
|
|
|
}
|
|
|
|
|
2016-12-28 15:39:29 +01:00
|
|
|
public function hasExpiredPlan($plan)
|
|
|
|
{
|
|
|
|
if ($this->plan != $plan) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Carbon::parse($this->plan_expires) < Carbon::today();
|
|
|
|
}
|
|
|
|
|
2016-12-14 15:19:16 +01:00
|
|
|
public function hasEarnedPromo()
|
|
|
|
{
|
|
|
|
if ( ! Utils::isNinjaProd() || Utils::isPro()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if they've already had a discount or a promotion is active return false
|
|
|
|
if ($this->discount_expires || $this->hasActivePromo()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// after 52 weeks, offer a 50% discount for 3 days
|
|
|
|
$discounts = [
|
|
|
|
52 => [.5, 3],
|
|
|
|
16 => [.5, 3],
|
|
|
|
10 => [.25, 5],
|
|
|
|
];
|
|
|
|
|
|
|
|
foreach ($discounts as $weeks => $promo) {
|
|
|
|
list($discount, $validFor) = $promo;
|
|
|
|
$difference = $this->created_at->diffInWeeks();
|
|
|
|
if ($difference >= $weeks) {
|
|
|
|
$this->discount = $discount;
|
|
|
|
$this->promo_expires = date_create()->modify($validFor . ' days')->format('Y-m-d');
|
|
|
|
$this->save();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2016-04-17 00:34:39 +02:00
|
|
|
}
|