2018-10-12 13:29:34 +02:00
|
|
|
<?php
|
2019-05-11 05:32:07 +02:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2019-05-11 05:32:07 +02:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2021-01-03 22:54:54 +01:00
|
|
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
2019-05-11 05:32:07 +02:00
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
2018-10-12 13:29:34 +02:00
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2020-10-28 11:10:49 +01:00
|
|
|
use App\Models\Presenters\AccountPresenter;
|
2020-03-01 11:18:13 +01:00
|
|
|
use App\Utils\Ninja;
|
2018-11-20 05:36:56 +01:00
|
|
|
use App\Utils\Traits\MakesHash;
|
2021-04-10 00:27:02 +02:00
|
|
|
use Carbon\Carbon;
|
2020-10-28 11:10:49 +01:00
|
|
|
use DateTime;
|
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
2018-10-22 14:04:37 +02:00
|
|
|
use Laracasts\Presenter\PresentableTrait;
|
2018-10-12 13:29:34 +02:00
|
|
|
|
2018-11-02 11:54:46 +01:00
|
|
|
class Account extends BaseModel
|
2018-10-12 13:29:34 +02:00
|
|
|
{
|
2018-10-22 14:04:37 +02:00
|
|
|
use PresentableTrait;
|
2018-11-20 05:36:56 +01:00
|
|
|
use MakesHash;
|
2018-10-15 14:40:34 +02:00
|
|
|
|
|
|
|
/**
|
2018-10-22 14:04:37 +02:00
|
|
|
* @var string
|
2018-10-15 14:40:34 +02:00
|
|
|
*/
|
2020-10-28 11:10:49 +01:00
|
|
|
protected $presenter = AccountPresenter::class;
|
2018-10-15 14:40:34 +02:00
|
|
|
|
|
|
|
/**
|
2018-10-22 14:04:37 +02:00
|
|
|
* @var array
|
2018-10-15 14:40:34 +02:00
|
|
|
*/
|
2018-10-22 14:04:37 +02:00
|
|
|
protected $fillable = [
|
|
|
|
'plan',
|
|
|
|
'plan_term',
|
|
|
|
'plan_price',
|
|
|
|
'plan_paid',
|
|
|
|
'plan_started',
|
|
|
|
'plan_expires',
|
2018-10-24 05:50:15 +02:00
|
|
|
'utm_source',
|
|
|
|
'utm_medium',
|
|
|
|
'utm_campaign',
|
|
|
|
'utm_term',
|
|
|
|
'utm_content',
|
2019-10-24 06:46:24 +02:00
|
|
|
'user_agent',
|
2018-10-22 14:04:37 +02:00
|
|
|
];
|
2018-10-15 14:40:34 +02:00
|
|
|
|
|
|
|
/**
|
2018-10-22 14:04:37 +02:00
|
|
|
* @var array
|
2018-10-15 14:40:34 +02:00
|
|
|
*/
|
2018-10-22 14:04:37 +02:00
|
|
|
protected $dates = [
|
|
|
|
'deleted_at',
|
|
|
|
'promo_expires',
|
|
|
|
'discount_expires',
|
2021-04-06 05:05:40 +02:00
|
|
|
'trial_started',
|
2021-04-08 01:21:09 +02:00
|
|
|
'plan_expires'
|
2018-10-22 14:04:37 +02:00
|
|
|
];
|
2018-10-15 14:40:34 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
const PLAN_FREE = 'free';
|
|
|
|
const PLAN_PRO = 'pro';
|
|
|
|
const PLAN_ENTERPRISE = 'enterprise';
|
|
|
|
const PLAN_WHITE_LABEL = 'white_label';
|
2020-03-01 11:18:13 +01:00
|
|
|
const PLAN_TERM_MONTHLY = 'month';
|
2020-09-06 11:38:10 +02:00
|
|
|
const PLAN_TERM_YEARLY = 'year';
|
2020-03-01 11:18:13 +01:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
const FEATURE_TASKS = 'tasks';
|
|
|
|
const FEATURE_EXPENSES = 'expenses';
|
|
|
|
const FEATURE_QUOTES = 'quotes';
|
|
|
|
const FEATURE_CUSTOMIZE_INVOICE_DESIGN = 'custom_designs';
|
|
|
|
const FEATURE_DIFFERENT_DESIGNS = 'different_designs';
|
2020-03-01 11:18:13 +01:00
|
|
|
const FEATURE_EMAIL_TEMPLATES_REMINDERS = 'template_reminders';
|
2020-09-06 11:38:10 +02:00
|
|
|
const FEATURE_INVOICE_SETTINGS = 'invoice_settings';
|
|
|
|
const FEATURE_CUSTOM_EMAILS = 'custom_emails';
|
|
|
|
const FEATURE_PDF_ATTACHMENT = 'pdf_attachments';
|
|
|
|
const FEATURE_MORE_INVOICE_DESIGNS = 'more_invoice_designs';
|
|
|
|
const FEATURE_REPORTS = 'reports';
|
|
|
|
const FEATURE_BUY_NOW_BUTTONS = 'buy_now_buttons';
|
|
|
|
const FEATURE_API = 'api';
|
|
|
|
const FEATURE_CLIENT_PORTAL_PASSWORD = 'client_portal_password';
|
|
|
|
const FEATURE_CUSTOM_URL = 'custom_url';
|
|
|
|
const FEATURE_MORE_CLIENTS = 'more_clients';
|
|
|
|
const FEATURE_WHITE_LABEL = 'white_label';
|
|
|
|
const FEATURE_REMOVE_CREATED_BY = 'remove_created_by';
|
|
|
|
const FEATURE_USERS = 'users'; // Grandfathered for old Pro users
|
|
|
|
const FEATURE_DOCUMENTS = 'documents';
|
|
|
|
const FEATURE_USER_PERMISSIONS = 'permissions';
|
2020-03-01 11:18:13 +01:00
|
|
|
|
|
|
|
const RESULT_FAILURE = 'failure';
|
|
|
|
const RESULT_SUCCESS = 'success';
|
2020-05-06 13:49:42 +02:00
|
|
|
|
|
|
|
public function getEntityType()
|
|
|
|
{
|
2020-09-06 11:38:10 +02:00
|
|
|
return self::class;
|
2020-05-06 13:49:42 +02:00
|
|
|
}
|
2018-10-15 14:40:34 +02:00
|
|
|
|
2020-03-24 10:15:30 +01:00
|
|
|
public function users()
|
|
|
|
{
|
|
|
|
return $this->hasMany(User::class)->withTrashed();
|
|
|
|
}
|
|
|
|
|
2018-10-29 04:16:17 +01:00
|
|
|
public function default_company()
|
|
|
|
{
|
2019-03-27 05:50:13 +01:00
|
|
|
return $this->hasOne(Company::class, 'id', 'default_company_id');
|
2018-10-29 04:16:17 +01:00
|
|
|
}
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2018-10-22 14:04:37 +02:00
|
|
|
public function payment()
|
2018-10-15 14:40:34 +02:00
|
|
|
{
|
2019-11-25 10:38:55 +01:00
|
|
|
return $this->belongsTo(Payment::class)->withTrashed();
|
2018-10-15 14:40:34 +02:00
|
|
|
}
|
2019-06-25 05:55:02 +02:00
|
|
|
|
|
|
|
public function companies()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Company::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function company_users()
|
|
|
|
{
|
|
|
|
return $this->hasMany(CompanyUser::class);
|
|
|
|
}
|
2019-09-18 14:43:37 +02:00
|
|
|
|
|
|
|
public function getPlan()
|
|
|
|
{
|
|
|
|
return $this->plan ?: '';
|
|
|
|
}
|
2020-03-01 11:18:13 +01:00
|
|
|
|
|
|
|
public function hasFeature($feature)
|
|
|
|
{
|
|
|
|
$plan_details = $this->getPlanDetails();
|
|
|
|
$self_host = ! Ninja::isNinja();
|
|
|
|
|
|
|
|
switch ($feature) {
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-03-01 11:18:13 +01:00
|
|
|
case self::FEATURE_TASKS:
|
|
|
|
case self::FEATURE_EXPENSES:
|
|
|
|
case self::FEATURE_QUOTES:
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case self::FEATURE_CUSTOMIZE_INVOICE_DESIGN:
|
|
|
|
case self::FEATURE_DIFFERENT_DESIGNS:
|
|
|
|
case self::FEATURE_EMAIL_TEMPLATES_REMINDERS:
|
|
|
|
case self::FEATURE_INVOICE_SETTINGS:
|
|
|
|
case self::FEATURE_CUSTOM_EMAILS:
|
|
|
|
case self::FEATURE_PDF_ATTACHMENT:
|
|
|
|
case self::FEATURE_MORE_INVOICE_DESIGNS:
|
|
|
|
case self::FEATURE_REPORTS:
|
|
|
|
case self::FEATURE_BUY_NOW_BUTTONS:
|
|
|
|
case self::FEATURE_API:
|
|
|
|
case self::FEATURE_CLIENT_PORTAL_PASSWORD:
|
|
|
|
case self::FEATURE_CUSTOM_URL:
|
|
|
|
return $self_host || ! empty($plan_details);
|
|
|
|
|
|
|
|
// Pro; No trial allowed, unless they're trialing enterprise with an active pro plan
|
2020-03-02 11:22:37 +01:00
|
|
|
case self::FEATURE_MORE_CLIENTS:
|
2020-03-01 11:18:13 +01:00
|
|
|
return $self_host || ! empty($plan_details) && (! $plan_details['trial'] || ! empty($this->getPlanDetails(false, false)));
|
|
|
|
|
|
|
|
// White Label
|
2020-03-02 11:22:37 +01:00
|
|
|
case self::FEATURE_WHITE_LABEL:
|
2020-03-01 11:18:13 +01:00
|
|
|
if (! $self_host && $plan_details && ! $plan_details['expires']) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Fallthrough
|
2020-03-21 06:37:30 +01:00
|
|
|
// no break
|
2020-03-02 11:22:37 +01:00
|
|
|
case self::FEATURE_REMOVE_CREATED_BY:
|
2020-03-01 11:18:13 +01:00
|
|
|
return ! empty($plan_details); // A plan is required even for self-hosted users
|
|
|
|
|
|
|
|
// Enterprise; No Trial allowed; grandfathered for old pro users
|
2020-03-02 11:22:37 +01:00
|
|
|
case self::FEATURE_USERS:// Grandfathered for old Pro users
|
2021-03-07 11:14:53 +01:00
|
|
|
if ($plan_details && $plan_details['trial']) {
|
2020-03-01 11:18:13 +01:00
|
|
|
// Do they have a non-trial plan?
|
2021-03-07 11:14:53 +01:00
|
|
|
$plan_details = $this->getPlanDetails(false, false);
|
2020-03-01 11:18:13 +01:00
|
|
|
}
|
|
|
|
|
2021-03-07 11:14:53 +01:00
|
|
|
return $self_host || ! empty($plan_details) && ($plan_details['plan'] == self::PLAN_ENTERPRISE);
|
2020-03-01 11:18:13 +01:00
|
|
|
|
|
|
|
// Enterprise; No Trial allowed
|
2020-03-02 11:22:37 +01:00
|
|
|
case self::FEATURE_DOCUMENTS:
|
|
|
|
case self::FEATURE_USER_PERMISSIONS:
|
2021-03-07 11:14:53 +01:00
|
|
|
return $self_host || ! empty($plan_details) && $plan_details['plan'] == self::PLAN_ENTERPRISE && ! $plan_details['trial'];
|
2020-03-01 11:18:13 +01:00
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isPaid()
|
|
|
|
{
|
|
|
|
return Ninja::isNinja() ? ($this->isPaidHostedClient() && ! $this->isTrial()) : $this->hasFeature(self::FEATURE_WHITE_LABEL);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isPaidHostedClient()
|
|
|
|
{
|
2020-03-21 06:37:30 +01:00
|
|
|
if (! Ninja::isNinja()) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-03-02 11:22:37 +01:00
|
|
|
|
2020-03-01 11:18:13 +01:00
|
|
|
return $this->plan == 'pro' || $this->plan == 'enterprise';
|
|
|
|
}
|
|
|
|
|
2020-04-21 07:16:45 +02:00
|
|
|
public function isFreeHostedClient()
|
|
|
|
{
|
|
|
|
if (! Ninja::isNinja()) {
|
|
|
|
return false;
|
2020-09-06 11:38:10 +02:00
|
|
|
}
|
2020-04-21 07:16:45 +02:00
|
|
|
|
2021-06-08 14:17:19 +02:00
|
|
|
return $this->plan == 'free' || is_null($this->plan);
|
2020-04-21 07:16:45 +02:00
|
|
|
}
|
|
|
|
|
2020-04-23 00:54:10 +02:00
|
|
|
public function isEnterpriseClient()
|
|
|
|
{
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! Ninja::isNinja()) {
|
2020-04-23 00:54:10 +02:00
|
|
|
return false;
|
2020-09-06 11:38:10 +02:00
|
|
|
}
|
2020-04-23 00:54:10 +02:00
|
|
|
|
|
|
|
return $this->plan == 'enterprise';
|
|
|
|
}
|
|
|
|
|
2020-03-01 11:18:13 +01:00
|
|
|
public function isTrial()
|
|
|
|
{
|
2020-03-21 06:37:30 +01:00
|
|
|
if (! Ninja::isNinja()) {
|
2020-03-01 11:18:13 +01:00
|
|
|
return false;
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2020-03-01 11:18:13 +01:00
|
|
|
|
|
|
|
$plan_details = $this->getPlanDetails();
|
|
|
|
|
|
|
|
return $plan_details && $plan_details['trial'];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getPlanDetails($include_inactive = false, $include_trial = true)
|
|
|
|
{
|
|
|
|
$plan = $this->plan;
|
|
|
|
$price = $this->plan_price;
|
|
|
|
$trial_plan = $this->trial_plan;
|
|
|
|
|
|
|
|
if ((! $plan || $plan == self::PLAN_FREE) && (! $trial_plan || ! $include_trial)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$trial_active = false;
|
2021-04-06 06:02:27 +02:00
|
|
|
|
2020-03-01 11:18:13 +01:00
|
|
|
if ($trial_plan && $include_trial) {
|
2021-04-06 05:05:40 +02:00
|
|
|
$trial_started = $this->trial_started;
|
|
|
|
$trial_expires = $this->trial_started->addSeconds($this->trial_duration);
|
2020-03-01 11:18:13 +01:00
|
|
|
|
2021-04-06 06:02:27 +02:00
|
|
|
if($trial_expires->greaterThan(now())){
|
2020-03-01 11:18:13 +01:00
|
|
|
$trial_active = true;
|
2021-04-06 05:05:40 +02:00
|
|
|
}
|
|
|
|
|
2020-03-01 11:18:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$plan_active = false;
|
|
|
|
if ($plan) {
|
|
|
|
if ($this->plan_expires == null) {
|
|
|
|
$plan_active = true;
|
|
|
|
$plan_expires = false;
|
|
|
|
} else {
|
2021-04-10 00:27:02 +02:00
|
|
|
$plan_expires = Carbon::parse($this->plan_expires);
|
|
|
|
if ($plan_expires->greaterThan(now())) {
|
2020-03-01 11:18:13 +01:00
|
|
|
$plan_active = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! $include_inactive && ! $plan_active && ! $trial_active) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-04-06 06:02:27 +02:00
|
|
|
|
2020-03-01 11:18:13 +01:00
|
|
|
// Should we show plan details or trial details?
|
|
|
|
if (($plan && ! $trial_plan) || ! $include_trial) {
|
|
|
|
$use_plan = true;
|
|
|
|
} elseif (! $plan && $trial_plan) {
|
|
|
|
$use_plan = false;
|
|
|
|
} else {
|
|
|
|
// There is both a plan and a trial
|
|
|
|
if (! empty($plan_active) && empty($trial_active)) {
|
|
|
|
$use_plan = true;
|
|
|
|
} elseif (empty($plan_active) && ! empty($trial_active)) {
|
|
|
|
$use_plan = false;
|
|
|
|
} elseif (! empty($plan_active) && ! empty($trial_active)) {
|
|
|
|
// Both are active; use whichever is a better plan
|
|
|
|
if ($plan == self::PLAN_ENTERPRISE) {
|
|
|
|
$use_plan = true;
|
|
|
|
} elseif ($trial_plan == self::PLAN_ENTERPRISE) {
|
|
|
|
$use_plan = false;
|
|
|
|
} else {
|
|
|
|
// They're both the same; show the plan
|
|
|
|
$use_plan = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Neither are active; use whichever expired most recently
|
|
|
|
$use_plan = $plan_expires >= $trial_expires;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($use_plan) {
|
|
|
|
return [
|
|
|
|
'account_id' => $this->id,
|
|
|
|
'num_users' => $this->num_users,
|
|
|
|
'plan_price' => $price,
|
|
|
|
'trial' => false,
|
|
|
|
'plan' => $plan,
|
2020-10-28 11:10:49 +01:00
|
|
|
'started' => DateTime::createFromFormat('Y-m-d', $this->plan_started),
|
2020-03-01 11:18:13 +01:00
|
|
|
'expires' => $plan_expires,
|
2020-10-28 11:10:49 +01:00
|
|
|
'paid' => DateTime::createFromFormat('Y-m-d', $this->plan_paid),
|
2020-03-01 11:18:13 +01:00
|
|
|
'term' => $this->plan_term,
|
|
|
|
'active' => $plan_active,
|
|
|
|
];
|
|
|
|
} else {
|
|
|
|
return [
|
|
|
|
'account_id' => $this->id,
|
|
|
|
'num_users' => 1,
|
|
|
|
'plan_price' => 0,
|
|
|
|
'trial' => true,
|
|
|
|
'plan' => $trial_plan,
|
|
|
|
'started' => $trial_started,
|
|
|
|
'expires' => $trial_expires,
|
|
|
|
'active' => $trial_active,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
2021-06-11 00:20:46 +02:00
|
|
|
|
2018-10-12 13:29:34 +02:00
|
|
|
}
|