1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-08 20:22:42 +01:00
invoiceninja/app/Models/Company.php

233 lines
5.8 KiB
PHP
Raw Normal View History

2017-01-30 20:40:43 +01:00
<?php
namespace App\Models;
2016-04-17 00:34:39 +02:00
2016-12-14 15:19:16 +01:00
use Carbon;
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;
2017-01-30 20:40:43 +01:00
use Utils;
2016-04-17 00:34:39 +02:00
/**
2017-01-30 20:40:43 +01: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
2017-05-08 10:18:35 +02:00
/**
* @var array
*/
protected $fillable = [
'plan',
'plan_term',
'plan_price',
'plan_paid',
'plan_started',
'plan_expires',
];
/**
* @var array
*/
2016-12-14 15:19:16 +01:00
protected $dates = [
'deleted_at',
'promo_expires',
'discount_expires',
];
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
2016-04-17 00:34:39 +02:00
public function accounts()
{
return $this->hasMany('App\Models\Account');
}
/**
* @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-30 17:05:31 +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();
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)
{
2017-01-30 17:05:31 +01:00
if (! $this->hasActivePromo() && ! $this->hasActiveDiscount()) {
2016-12-14 15:19:16 +01:00
return $price;
}
return $price - ($price * $this->discount);
}
2016-12-27 12:44:15 +01:00
public function daysUntilPlanExpires()
{
2017-01-30 17:05:31 +01:00
if (! $this->hasActivePlan()) {
2016-12-27 12:44:15 +01:00
return 0;
}
return Carbon::parse($this->plan_expires)->diffInDays(Carbon::today());
}
public function hasActivePlan()
{
2019-12-03 10:27:30 +01:00
return $this->plan_expires && Carbon::parse($this->plan_expires) >= Carbon::today();
2016-12-27 12:44:15 +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()
{
2017-01-30 17:05:31 +01:00
if (! Utils::isNinjaProd() || Utils::isPro()) {
2016-12-14 15:19:16 +01:00
return false;
2017-02-27 20:19:31 +01:00
}
// if they've already been pro return false
if ($this->plan_expires && $this->plan_expires != '0000-00-00') {
2016-12-14 15:19:16 +01:00
return false;
}
2017-01-29 08:33:53 +01:00
// if they've already been pro return false
if ($this->plan_expires && $this->plan_expires != '0000-00-00') {
return false;
}
2016-12-14 15:19:16 +01:00
// if they've already had a discount or a promotion is active return false
if ($this->discount_expires || $this->hasActivePromo()) {
return false;
}
$discounts = [
2017-07-11 16:38:28 +02:00
52 => [.6, 3],
16 => [.4, 3],
2016-12-14 15:19:16 +01:00
10 => [.25, 5],
];
foreach ($discounts as $weeks => $promo) {
list($discount, $validFor) = $promo;
$difference = $this->created_at->diffInWeeks();
2017-12-06 09:13:21 +01:00
if ($difference >= $weeks && $discount > $this->discount) {
2016-12-14 15:19:16 +01:00
$this->discount = $discount;
$this->promo_expires = date_create()->modify($validFor . ' days')->format('Y-m-d');
$this->save();
2017-01-30 20:40:43 +01:00
2016-12-14 15:19:16 +01:00
return true;
}
}
return false;
}
2017-05-14 11:11:38 +02:00
public function getPlanDetails($includeInactive = false, $includeTrial = true)
{
$account = $this->accounts()->first();
2017-12-11 15:08:31 +01:00
if (! $account) {
return false;
}
2017-05-14 11:11:38 +02:00
return $account->getPlanDetails($includeInactive, $includeTrial);
}
public function processRefund($user)
{
if (! $this->payment) {
return false;
}
$account = $this->accounts()->first();
$planDetails = $account->getPlanDetails(false, false);
if (! empty($planDetails['started'])) {
$deadline = clone $planDetails['started'];
$deadline->modify('+30 days');
if ($deadline >= date_create()) {
$accountRepo = app('App\Ninja\Repositories\AccountRepository');
$ninjaAccount = $accountRepo->getNinjaAccount();
$paymentDriver = $ninjaAccount->paymentDriver();
$paymentDriver->refundPayment($this->payment);
\Log::info("Refunded Plan Payment: {$account->name} - {$user->email} - Deadline: {$deadline->format('Y-m-d')}");
return true;
}
}
return false;
}
2017-08-25 12:46:32 +02:00
public function applyDiscount($amount)
{
$this->discount = $amount;
2017-11-28 21:37:19 +01:00
$this->promo_expires = date_create()->modify('3 days')->format('Y-m-d');
2017-08-25 12:46:32 +02:00
}
2019-01-30 11:45:46 +01:00
public function applyFreeYear($numYears = 1)
2017-08-25 12:46:32 +02:00
{
2017-12-01 10:50:16 +01:00
if ($this->plan_started && $this->plan_started != '0000-00-00') {
return;
}
2017-08-25 12:46:32 +02:00
$this->plan = PLAN_PRO;
$this->plan_term = PLAN_TERM_YEARLY;
$this->plan_price = PLAN_PRICE_PRO_MONTHLY;
$this->plan_started = date_create()->format('Y-m-d');
$this->plan_paid = date_create()->format('Y-m-d');
2019-01-30 11:45:46 +01:00
$this->plan_expires = date_create()->modify($numYears . ' year')->format('Y-m-d');
2017-08-25 12:46:32 +02:00
}
2016-04-17 00:34:39 +02:00
}
2017-05-01 11:29:45 +02:00
Company::deleted(function ($company)
{
if (! env('MULTI_DB_ENABLED')) {
return;
}
$server = \App\Models\DbServer::whereName(config('database.default'))->firstOrFail();
LookupCompany::deleteWhere([
'company_id' => $company->id,
'db_server_id' => $server->id,
]);
});