1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-19 16:01:34 +02:00
invoiceninja/app/Models/Account.php

1975 lines
49 KiB
PHP
Raw Normal View History

2017-01-30 20:40:43 +01:00
<?php
namespace App\Models;
2015-03-16 22:45:25 +01:00
2015-08-11 16:38:36 +02:00
use App;
2015-08-14 14:04:33 +02:00
use App\Events\UserSettingsChanged;
2017-05-01 10:13:15 +02:00
use App\Models\LookupAccount;
2017-01-03 20:48:40 +01:00
use App\Models\Traits\GeneratesNumbers;
2017-01-30 20:40:43 +01:00
use App\Models\Traits\PresentsInvoice;
use App\Models\Traits\SendsEmails;
use App\Models\Traits\HasLogo;
2018-04-09 13:45:38 +02:00
use App\Models\Traits\HasCustomMessages;
2017-01-30 20:40:43 +01:00
use Cache;
use Carbon;
use DateTime;
use Eloquent;
use Event;
use Illuminate\Database\Eloquent\SoftDeletes;
use Laracasts\Presenter\PresentableTrait;
use Session;
use Utils;
2015-03-31 11:38:24 +02:00
/**
2017-01-30 20:40:43 +01:00
* Class Account.
*/
2015-03-16 22:45:25 +01:00
class Account extends Eloquent
{
2015-11-06 11:59:53 +01:00
use PresentableTrait;
2015-03-31 11:38:24 +02:00
use SoftDeletes;
2016-09-02 16:53:16 +02:00
use PresentsInvoice;
2017-01-03 20:48:40 +01:00
use GeneratesNumbers;
use SendsEmails;
use HasLogo;
2018-04-09 13:45:38 +02:00
use HasCustomMessages;
2016-05-29 11:26:02 +02:00
/**
* @var string
*/
2015-11-06 11:59:53 +01:00
protected $presenter = 'App\Ninja\Presenters\AccountPresenter';
/**
* @var array
*/
2015-03-31 11:38:24 +02:00
protected $dates = ['deleted_at'];
/**
* @var array
*/
2015-10-02 10:32:13 +02:00
protected $hidden = ['ip'];
2015-03-16 22:45:25 +01:00
/**
* @var array
*/
2016-02-03 13:41:40 +01:00
protected $fillable = [
2017-03-22 10:50:55 +01:00
'timezone_id',
'date_format_id',
'datetime_format_id',
'currency_id',
2016-02-03 13:41:40 +01:00
'name',
'address1',
'address2',
'city',
'state',
'postal_code',
'country_id',
2017-03-22 10:50:55 +01:00
'invoice_terms',
2016-02-03 13:41:40 +01:00
'email_footer',
2017-03-22 10:50:55 +01:00
'industry_id',
'size_id',
2016-05-29 11:26:02 +02:00
'invoice_taxes',
'invoice_item_taxes',
2017-03-22 10:50:55 +01:00
'invoice_design_id',
2017-06-01 18:13:13 +02:00
'quote_design_id',
2017-03-22 10:50:55 +01:00
'work_phone',
'work_email',
'language_id',
'fill_products',
'update_products',
'primary_color',
'secondary_color',
'hide_quantity',
'hide_paid_to_date',
'vat_number',
'invoice_number_prefix',
'invoice_number_counter',
'quote_number_prefix',
'quote_number_counter',
'share_counter',
'id_number',
'token_billing_type_id',
'invoice_footer',
'pdf_email_attachment',
'font_size',
'invoice_labels',
2017-08-28 09:28:00 +02:00
'custom_design1',
'custom_design2',
'custom_design3',
2016-05-29 11:26:02 +02:00
'show_item_taxes',
2017-03-22 10:50:55 +01:00
'military_time',
'enable_reminder1',
'enable_reminder2',
'enable_reminder3',
2017-12-25 20:46:59 +01:00
'enable_reminder4',
2017-03-22 10:50:55 +01:00
'num_days_reminder1',
'num_days_reminder2',
'num_days_reminder3',
'tax_name1',
'tax_rate1',
'tax_name2',
'tax_rate2',
2017-03-22 10:50:55 +01:00
'recurring_hour',
'invoice_number_pattern',
'quote_number_pattern',
'quote_terms',
'email_design_id',
'enable_email_markup',
'website',
'direction_reminder1',
'direction_reminder2',
'direction_reminder3',
'field_reminder1',
'field_reminder2',
'field_reminder3',
'header_font_id',
'body_font_id',
'auto_convert_quote',
2018-03-13 22:04:24 +01:00
'auto_archive_quote',
2018-03-13 21:05:57 +01:00
'auto_archive_invoice',
'auto_email_invoice',
2017-03-22 10:50:55 +01:00
'all_pages_footer',
'all_pages_header',
'show_currency_code',
2016-11-04 14:34:15 +01:00
'enable_portal_password',
'send_portal_password',
2017-03-22 10:50:55 +01:00
'recurring_invoice_number_prefix',
'enable_client_portal',
'invoice_fields',
'invoice_embed_documents',
'document_email_attachment',
2018-01-12 14:15:58 +01:00
'ubl_email_attachment',
2017-03-22 10:50:55 +01:00
'enable_client_portal_dashboard',
'page_size',
'live_preview',
'invoice_number_padding',
'enable_second_tax_rate',
'auto_bill_on_due_date',
'start_of_week',
2016-11-04 14:34:15 +01:00
'enable_buy_now_buttons',
2017-03-22 10:50:55 +01:00
'include_item_taxes_inline',
'financial_year_start',
'enabled_modules',
'enabled_dashboard_sections',
2016-11-04 14:34:15 +01:00
'show_accept_invoice_terms',
'show_accept_quote_terms',
'require_invoice_signature',
'require_quote_signature',
2017-03-22 10:50:55 +01:00
'client_number_prefix',
'client_number_counter',
'client_number_pattern',
2017-01-16 12:59:46 +01:00
'payment_terms',
2017-03-22 10:50:55 +01:00
'reset_counter_frequency_id',
2017-02-23 19:20:15 +01:00
'payment_type_id',
'gateway_fee_enabled',
'send_item_details',
2017-03-23 10:39:44 +01:00
'reset_counter_date',
2017-04-25 03:36:29 +02:00
'domain_id',
2017-06-05 15:31:28 +02:00
'analytics_key',
'credit_number_counter',
'credit_number_prefix',
'credit_number_pattern',
'task_rate',
2017-12-03 12:56:10 +01:00
'inclusive_taxes',
2017-12-25 20:46:59 +01:00
'convert_products',
'signature_on_pdf',
2018-04-04 15:35:28 +02:00
'custom_fields',
'custom_value1',
'custom_value2',
2018-04-08 15:55:25 +02:00
'custom_messages',
2016-02-03 13:41:40 +01:00
];
/**
* @var array
*/
2015-10-14 16:15:39 +02:00
public static $basicSettings = [
ACCOUNT_COMPANY_DETAILS,
ACCOUNT_USER_DETAILS,
ACCOUNT_LOCALIZATION,
ACCOUNT_PAYMENTS,
2015-10-21 13:11:08 +02:00
ACCOUNT_TAX_RATES,
2015-10-14 16:15:39 +02:00
ACCOUNT_PRODUCTS,
ACCOUNT_NOTIFICATIONS,
ACCOUNT_IMPORT_EXPORT,
ACCOUNT_MANAGEMENT,
2015-10-14 16:15:39 +02:00
];
/**
* @var array
*/
2015-10-14 16:15:39 +02:00
public static $advancedSettings = [
ACCOUNT_INVOICE_SETTINGS,
2015-10-16 07:32:02 +02:00
ACCOUNT_INVOICE_DESIGN,
ACCOUNT_CLIENT_PORTAL,
2015-12-15 21:25:12 +01:00
ACCOUNT_EMAIL_SETTINGS,
2015-10-14 16:15:39 +02:00
ACCOUNT_TEMPLATES_AND_REMINDERS,
2016-07-11 19:08:43 +02:00
ACCOUNT_BANKS,
2017-01-22 11:09:29 +01:00
//ACCOUNT_REPORTS,
2015-10-14 16:15:39 +02:00
ACCOUNT_DATA_VISUALIZATIONS,
ACCOUNT_API_TOKENS,
2016-07-18 22:28:30 +02:00
ACCOUNT_USER_MANAGEMENT,
2015-10-14 16:15:39 +02:00
];
2016-10-23 13:41:26 +02:00
public static $modules = [
2016-10-27 10:57:51 +02:00
ENTITY_RECURRING_INVOICE => 1,
ENTITY_CREDIT => 2,
ENTITY_QUOTE => 4,
ENTITY_TASK => 8,
ENTITY_EXPENSE => 16,
2016-10-23 13:41:26 +02:00
];
public static $dashboardSections = [
2016-10-27 10:57:51 +02:00
'total_revenue' => 1,
'average_invoice' => 2,
'outstanding' => 4,
2016-10-23 13:41:26 +02:00
];
2018-04-04 15:24:59 +02:00
public static $customFields = [
'client1',
'client2',
'contact1',
'contact2',
'product1',
'product2',
'invoice1',
'invoice2',
'invoice_surcharge1',
'invoice_surcharge2',
2018-04-04 21:08:37 +02:00
'task1',
'task2',
'project1',
'project2',
'expense1',
'expense2',
'vendor1',
'vendor2',
2018-04-04 15:24:59 +02:00
];
2017-06-21 10:07:59 +02:00
public static $customLabels = [
'address1',
'address2',
'amount',
'amount_paid',
'balance',
'balance_due',
'blank',
'city_state_postal',
'client_name',
'company_name',
'contact_name',
'country',
2017-08-16 14:33:41 +02:00
'credit_card',
'credit_date',
'credit_issued_to',
'credit_note',
'credit_number',
'credit_to',
'custom_value1',
'custom_value2',
'date',
2018-03-25 09:21:28 +02:00
'delivery_note',
2017-06-21 10:07:59 +02:00
'description',
'details',
'discount',
'due_date',
'email',
'from',
'gateway_fee_description',
'gateway_fee_discount_description',
'gateway_fee_item',
'hours',
2017-07-09 19:06:34 +02:00
'id_number',
2017-12-06 14:42:41 +01:00
'invoice',
2018-02-20 10:02:20 +01:00
'invoice_date',
'invoice_due_date',
'invoice_issued_to',
'invoice_no',
2018-02-20 10:02:20 +01:00
'invoice_number',
'invoice_to',
'invoice_total',
'item',
2017-06-21 10:07:59 +02:00
'line_total',
'method',
2017-08-30 14:34:07 +02:00
'outstanding',
'paid_to_date',
2017-06-21 10:07:59 +02:00
'partial_due',
'payment_date',
'phone',
'po_number',
'postal_city_state',
'product_key',
'quantity',
2017-12-06 14:42:41 +01:00
'quote',
2018-02-20 10:02:20 +01:00
'quote_date',
'quote_due_date',
'quote_issued_to',
'quote_no',
2018-02-20 10:02:20 +01:00
'quote_number',
'quote_to',
'rate',
'reference',
'service',
'statement',
'statement_date',
'statement_issued_to',
'statement_to',
2017-06-21 10:07:59 +02:00
'subtotal',
'surcharge',
2017-06-21 10:07:59 +02:00
'tax',
'tax_invoice',
'tax_quote',
'taxes',
'terms',
'to',
'total',
'unit_cost',
2018-03-25 09:21:28 +02:00
'valid_until',
2017-07-09 19:06:34 +02:00
'vat_number',
'website',
'work_phone',
'your_credit',
'your_invoice',
'your_quote',
'your_statement',
2017-06-21 10:07:59 +02:00
];
2018-04-08 15:55:25 +02:00
public static $customMessageTypes = [
CUSTOM_MESSAGE_DASHBOARD,
CUSTOM_MESSAGE_UNPAID_INVOICE,
CUSTOM_MESSAGE_PAID_INVOICE,
CUSTOM_MESSAGE_UNAPPROVED_QUOTE,
//CUSTOM_MESSAGE_APPROVED_QUOTE,
2018-04-09 13:45:38 +02:00
//CUSTOM_MESSAGE_UNAPPROVED_PROPOSAL,
2018-04-08 15:55:25 +02:00
//CUSTOM_MESSAGE_APPROVED_PROPOSAL,
];
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
2015-11-03 20:03:24 +01:00
public function account_tokens()
2015-11-03 03:02:15 +01:00
{
return $this->hasMany('App\Models\AccountToken');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
2015-03-16 22:45:25 +01:00
public function users()
{
2015-03-23 07:52:01 +01:00
return $this->hasMany('App\Models\User');
2015-03-16 22:45:25 +01:00
}
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
2015-03-16 22:45:25 +01:00
public function clients()
{
2015-03-23 07:52:01 +01:00
return $this->hasMany('App\Models\Client');
2015-03-16 22:45:25 +01:00
}
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
2015-11-15 12:04:06 +01:00
public function contacts()
{
return $this->hasMany('App\Models\Contact');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
2015-03-16 22:45:25 +01:00
public function invoices()
{
2015-03-23 07:52:01 +01:00
return $this->hasMany('App\Models\Invoice');
2015-03-16 22:45:25 +01:00
}
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
2015-03-16 22:45:25 +01:00
public function account_gateways()
{
2015-03-23 07:52:01 +01:00
return $this->hasMany('App\Models\AccountGateway');
2015-03-16 22:45:25 +01:00
}
/**
2017-03-14 16:45:36 +01:00
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function account_gateway_settings()
{
return $this->hasMany('App\Models\AccountGatewaySettings');
}
2017-03-31 17:02:56 +02:00
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function account_email_settings()
{
return $this->hasOne('App\Models\AccountEmailSettings');
}
2017-03-14 16:45:36 +01:00
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
2016-01-20 00:07:31 +01:00
public function bank_accounts()
{
return $this->hasMany('App\Models\BankAccount');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
2015-03-16 22:45:25 +01:00
public function tax_rates()
{
2015-03-23 07:52:01 +01:00
return $this->hasMany('App\Models\TaxRate');
2015-03-16 22:45:25 +01:00
}
2019-01-30 11:45:46 +01:00
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function task_statuses()
{
return $this->hasMany('App\Models\TaskStatus')->orderBy('sort_order');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
2015-11-19 12:50:35 +01:00
public function products()
{
return $this->hasMany('App\Models\Product');
}
2017-07-20 21:45:10 +02:00
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function defaultDocuments()
{
return $this->hasMany('App\Models\Document')->whereIsDefault(true);
}
2018-04-11 22:18:01 +02:00
public function background_image()
{
return $this->hasOne('App\Models\Document', 'id', 'background_image_id');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
2015-03-16 22:45:25 +01:00
public function country()
{
2015-03-23 07:52:01 +01:00
return $this->belongsTo('App\Models\Country');
2015-03-16 22:45:25 +01:00
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
2015-03-16 22:45:25 +01:00
public function timezone()
{
2015-03-23 07:52:01 +01:00
return $this->belongsTo('App\Models\Timezone');
2015-03-16 22:45:25 +01:00
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
2015-03-16 22:45:25 +01:00
public function language()
{
2015-03-23 07:52:01 +01:00
return $this->belongsTo('App\Models\Language');
2015-03-16 22:45:25 +01:00
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
2015-03-16 22:45:25 +01:00
public function date_format()
{
2015-03-23 07:52:01 +01:00
return $this->belongsTo('App\Models\DateFormat');
2015-03-16 22:45:25 +01:00
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
2015-03-16 22:45:25 +01:00
public function datetime_format()
{
2015-03-23 07:52:01 +01:00
return $this->belongsTo('App\Models\DatetimeFormat');
2015-03-16 22:45:25 +01:00
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
2015-03-16 22:45:25 +01:00
public function size()
{
2015-03-23 07:52:01 +01:00
return $this->belongsTo('App\Models\Size');
2015-03-16 22:45:25 +01:00
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
2015-04-02 15:06:16 +02:00
public function currency()
{
return $this->belongsTo('App\Models\Currency');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
2015-03-16 22:45:25 +01:00
public function industry()
{
2015-03-23 07:52:01 +01:00
return $this->belongsTo('App\Models\Industry');
2015-03-16 22:45:25 +01:00
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
2017-04-19 16:18:24 +02:00
*/
public function payment_type()
{
return $this->belongsTo('App\Models\PaymentType');
}
/**
* @return mixed
*/
2016-02-17 11:16:13 +01:00
public function expenses()
{
2017-01-30 17:05:31 +01:00
return $this->hasMany('App\Models\Expense', 'account_id', 'id')->withTrashed();
2016-02-17 11:16:13 +01:00
}
2016-02-03 13:41:40 +01:00
/**
* @return mixed
*/
2016-02-17 11:50:52 +01:00
public function payments()
{
2017-01-30 17:05:31 +01:00
return $this->hasMany('App\Models\Payment', 'account_id', 'id')->withTrashed();
2016-02-17 11:50:52 +01:00
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
2016-04-17 00:34:39 +02:00
public function company()
{
return $this->belongsTo('App\Models\Company');
}
/**
* @return mixed
*/
2016-07-04 19:19:22 +02:00
public function expense_categories()
{
2017-01-30 17:05:31 +01:00
return $this->hasMany('App\Models\ExpenseCategory', 'account_id', 'id')->withTrashed();
}
2016-11-29 18:47:26 +01:00
/**
* @return mixed
*/
public function projects()
{
2017-01-30 17:05:31 +01:00
return $this->hasMany('App\Models\Project', 'account_id', 'id')->withTrashed();
2016-11-29 18:47:26 +01:00
}
2018-08-08 16:49:15 +02:00
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function custom_payment_terms()
{
return $this->hasMany('App\Models\PaymentTerm', 'account_id', 'id')->withTrashed();
}
/**
* @param $value
*/
2016-02-03 13:41:40 +01:00
public function setIndustryIdAttribute($value)
{
$this->attributes['industry_id'] = $value ?: null;
}
/**
* @param $value
*/
2016-02-03 13:41:40 +01:00
public function setCountryIdAttribute($value)
{
$this->attributes['country_id'] = $value ?: null;
}
/**
* @param $value
*/
2016-02-03 13:41:40 +01:00
public function setSizeIdAttribute($value)
{
$this->attributes['size_id'] = $value ?: null;
}
2018-04-04 15:24:59 +02:00
/**
* @param $value
*/
public function setCustomFieldsAttribute($data)
{
$fields = [];
2018-04-04 15:35:28 +02:00
if (! is_array($data)) {
$data = json_decode($data);
}
2018-04-04 15:24:59 +02:00
foreach ($data as $key => $value) {
if ($value) {
$fields[$key] = $value;
}
}
$this->attributes['custom_fields'] = count($fields) ? json_encode($fields) : null;
}
public function getCustomFieldsAttribute($value)
{
return json_decode($value ?: '{}');
}
public function customLabel($field)
{
$labels = $this->custom_fields;
return ! empty($labels->$field) ? $labels->$field : '';
}
/**
* @param int $gatewayId
2017-01-30 20:40:43 +01:00
*
* @return bool
*/
2015-03-16 22:45:25 +01:00
public function isGatewayConfigured($gatewayId = 0)
{
2017-01-30 17:05:31 +01:00
if (! $this->relationLoaded('account_gateways')) {
$this->load('account_gateways');
}
2015-03-16 22:45:25 +01:00
if ($gatewayId) {
return $this->getGatewayConfig($gatewayId) != false;
} else {
return $this->account_gateways->count() > 0;
2015-03-16 22:45:25 +01:00
}
}
/**
* @return bool
*/
2015-08-11 16:38:36 +02:00
public function isEnglish()
{
2017-01-30 20:40:43 +01:00
return ! $this->language_id || $this->language_id == DEFAULT_LANGUAGE;
2015-08-11 16:38:36 +02:00
}
/**
* @return bool
*/
public function hasInvoicePrefix()
{
2017-01-30 17:05:31 +01:00
if (! $this->invoice_number_prefix && ! $this->quote_number_prefix) {
return false;
}
return $this->invoice_number_prefix != $this->quote_number_prefix;
}
/**
* @return mixed
*/
2015-03-16 22:45:25 +01:00
public function getDisplayName()
{
if ($this->name) {
return $this->name;
}
//$this->load('users');
2015-03-16 22:45:25 +01:00
$user = $this->users()->first();
return $user->getDisplayName();
}
2017-03-15 16:09:23 +01:00
public function getGatewaySettings($gatewayTypeId)
{
if (! $this->relationLoaded('account_gateway_settings')) {
$this->load('account_gateway_settings');
}
foreach ($this->account_gateway_settings as $settings) {
if ($settings->gateway_type_id == $gatewayTypeId) {
return $settings;
}
}
return false;
}
/**
* @return string
*/
2015-10-13 17:44:01 +02:00
public function getCityState()
{
$swap = $this->country && $this->country->swap_postal_code;
2017-01-30 20:40:43 +01:00
2015-10-13 17:44:01 +02:00
return Utils::cityStateZip($this->city, $this->state, $this->postal_code, $swap);
}
/**
* @return mixed
*/
public function getMomentDateTimeFormat()
{
$format = $this->datetime_format ? $this->datetime_format->format_moment : DEFAULT_DATETIME_MOMENT_FORMAT;
if ($this->military_time) {
$format = str_replace('h:mm:ss a', 'H:mm:ss', $format);
}
return $format;
}
/**
* @return string
*/
2015-12-10 20:36:34 +01:00
public function getMomentDateFormat()
{
$format = $this->getMomentDateTimeFormat();
$format = str_replace('h:mm:ss a', '', $format);
$format = str_replace('H:mm:ss', '', $format);
return trim($format);
}
/**
* @return string
*/
2015-03-16 22:45:25 +01:00
public function getTimezone()
{
if ($this->timezone) {
return $this->timezone->name;
} else {
return 'US/Eastern';
}
}
2016-09-11 16:30:23 +02:00
public function getDate($date = 'now')
2015-10-15 16:14:13 +02:00
{
2017-01-30 17:05:31 +01:00
if (! $date) {
2015-12-30 19:45:52 +01:00
return null;
2017-01-30 17:05:31 +01:00
} elseif (! $date instanceof \DateTime) {
2015-12-30 19:45:52 +01:00
$date = new \DateTime($date);
}
2016-09-11 16:30:23 +02:00
return $date;
}
/**
* @param string $date
2017-01-30 20:40:43 +01:00
*
2016-09-11 16:30:23 +02:00
* @return DateTime|null|string
*/
2018-02-25 09:47:15 +01:00
public function getDateTime($date = 'now', $formatted = false)
2016-09-11 16:30:23 +02:00
{
$date = $this->getDate($date);
2015-12-30 19:45:52 +01:00
$date->setTimeZone(new \DateTimeZone($this->getTimezone()));
2018-02-25 09:47:15 +01:00
return $formatted ? $date->format($this->getCustomDateTimeFormat()) : $date;
2015-10-15 16:14:13 +02:00
}
/**
* @return mixed
*/
2015-10-15 16:14:13 +02:00
public function getCustomDateFormat()
{
2015-12-31 11:23:39 +01:00
return $this->date_format ? $this->date_format->format : DEFAULT_DATE_FORMAT;
2015-10-15 16:14:13 +02:00
}
2017-01-12 14:12:02 +01:00
public function getSampleLink()
{
$invitation = new Invitation();
$invitation->account = $this;
$invitation->invitation_key = '...';
return $invitation->getLink();
}
/**
* @param $amount
2017-01-30 20:49:42 +01:00
* @param null $client
* @param bool $hideSymbol
* @param mixed $decorator
2017-01-30 20:40:43 +01:00
*
* @return string
*/
2016-10-20 17:14:54 +02:00
public function formatMoney($amount, $client = null, $decorator = false)
2015-12-02 14:26:06 +01:00
{
if ($client && $client->currency_id) {
$currencyId = $client->currency_id;
} elseif ($this->currency_id) {
$currencyId = $this->currency_id;
} else {
$currencyId = DEFAULT_CURRENCY;
}
2015-12-02 14:26:06 +01:00
if ($client && $client->country_id) {
$countryId = $client->country_id;
} elseif ($this->country_id) {
$countryId = $this->country_id;
} else {
$countryId = false;
2015-12-02 14:26:06 +01:00
}
2017-01-30 17:05:31 +01:00
if (! $decorator) {
2016-10-20 17:14:54 +02:00
$decorator = $this->show_currency_code ? CURRENCY_DECORATOR_CODE : CURRENCY_DECORATOR_SYMBOL;
}
2016-10-23 13:41:26 +02:00
return Utils::formatMoney($amount, $currencyId, $countryId, $decorator);
2015-12-02 14:26:06 +01:00
}
2018-02-25 21:25:21 +01:00
public function formatNumber($amount, $precision = 0)
{
if ($this->currency_id) {
$currencyId = $this->currency_id;
} else {
$currencyId = DEFAULT_CURRENCY;
}
return Utils::formatNumber($amount, $currencyId, $precision);
}
/**
* @return mixed
*/
2016-01-21 20:15:30 +01:00
public function getCurrencyId()
{
return $this->currency_id ?: DEFAULT_CURRENCY;
}
2018-04-09 20:05:05 +02:00
/**
* @return mixed
*/
public function getCountryId()
{
return $this->country_id ?: DEFAULT_COUNTRY;
}
/**
* @param $date
2017-01-30 20:40:43 +01:00
*
* @return null|string
*/
2015-10-15 16:14:13 +02:00
public function formatDate($date)
{
2016-09-11 16:30:23 +02:00
$date = $this->getDate($date);
2015-12-30 19:45:52 +01:00
2017-01-30 17:05:31 +01:00
if (! $date) {
2015-10-15 16:14:13 +02:00
return null;
}
return $date->format($this->getCustomDateFormat());
}
/**
* @param $date
2017-01-30 20:40:43 +01:00
*
* @return null|string
*/
2015-11-12 21:36:28 +01:00
public function formatDateTime($date)
{
2015-12-30 19:45:52 +01:00
$date = $this->getDateTime($date);
2017-01-30 17:05:31 +01:00
if (! $date) {
2015-11-12 21:36:28 +01:00
return null;
}
return $date->format($this->getCustomDateTimeFormat());
}
/**
* @param $date
2017-01-30 20:40:43 +01:00
*
* @return null|string
*/
2015-12-13 21:12:54 +01:00
public function formatTime($date)
{
2015-12-30 19:45:52 +01:00
$date = $this->getDateTime($date);
2017-01-30 17:05:31 +01:00
if (! $date) {
2015-12-13 21:12:54 +01:00
return null;
}
return $date->format($this->getCustomTimeFormat());
}
/**
* @return string
*/
2015-12-13 21:12:54 +01:00
public function getCustomTimeFormat()
{
return $this->military_time ? 'H:i' : 'g:i a';
}
/**
* @return mixed
*/
2015-11-12 21:36:28 +01:00
public function getCustomDateTimeFormat()
{
2015-12-31 09:04:12 +01:00
$format = $this->datetime_format ? $this->datetime_format->format : DEFAULT_DATETIME_FORMAT;
if ($this->military_time) {
$format = str_replace('g:i a', 'H:i', $format);
}
return $format;
2015-11-12 21:36:28 +01:00
}
2016-06-20 16:14:43 +02:00
/*
public function defaultGatewayType()
2015-03-16 22:45:25 +01:00
{
2016-06-20 16:14:43 +02:00
$accountGateway = $this->account_gateways[0];
$paymentDriver = $accountGateway->paymentDriver();
2016-04-29 23:50:21 +02:00
2016-06-20 16:14:43 +02:00
return $paymentDriver->gatewayTypes()[0];
}
*/
/**
* @param bool $type
2017-01-30 20:40:43 +01:00
*
* @return AccountGateway|bool
*/
2016-06-20 16:14:43 +02:00
public function getGatewayByType($type = false)
{
2017-01-30 17:05:31 +01:00
if (! $this->relationLoaded('account_gateways')) {
2016-06-20 16:14:43 +02:00
$this->load('account_gateways');
2016-05-07 04:33:03 +02:00
}
/** @var AccountGateway $accountGateway */
2016-06-20 16:14:43 +02:00
foreach ($this->account_gateways as $accountGateway) {
2017-01-30 17:05:31 +01:00
if (! $type) {
2016-06-20 16:14:43 +02:00
return $accountGateway;
2016-05-07 04:33:03 +02:00
}
2016-06-20 16:14:43 +02:00
$paymentDriver = $accountGateway->paymentDriver();
if ($paymentDriver->handles($type)) {
return $accountGateway;
2015-03-16 22:45:25 +01:00
}
}
2016-05-29 11:26:02 +02:00
2015-03-16 22:45:25 +01:00
return false;
}
/**
* @return array
*/
2016-06-22 20:42:09 +02:00
public function availableGatewaysIds()
{
2017-01-30 17:05:31 +01:00
if (! $this->relationLoaded('account_gateways')) {
2016-06-22 20:42:09 +02:00
$this->load('account_gateways');
}
$gatewayTypes = [];
$gatewayIds = [];
2018-04-13 12:50:38 +02:00
$usedGatewayIds = [];
2016-06-22 20:42:09 +02:00
foreach ($this->account_gateways as $accountGateway) {
2018-04-13 12:50:38 +02:00
$usedGatewayIds[] = $accountGateway->gateway_id;
2016-06-22 20:42:09 +02:00
$paymentDriver = $accountGateway->paymentDriver();
$gatewayTypes = array_unique(array_merge($gatewayTypes, $paymentDriver->gatewayTypes()));
}
foreach (Cache::get('gateways') as $gateway) {
$paymentDriverClass = AccountGateway::paymentDriverClass($gateway->provider);
$paymentDriver = new $paymentDriverClass();
$available = true;
foreach ($gatewayTypes as $type) {
2018-05-08 16:47:47 +02:00
if ($paymentDriver->handles($type)) {
2016-06-22 20:42:09 +02:00
$available = false;
break;
}
}
if ($available) {
$gatewayIds[] = $gateway->id;
}
}
return $gatewayIds;
}
/**
2017-01-30 20:40:43 +01:00
* @param bool $invitation
2016-09-15 12:41:09 +02:00
* @param mixed $gatewayTypeId
2017-01-30 20:40:43 +01:00
*
* @return bool
*/
2016-09-15 12:41:09 +02:00
public function paymentDriver($invitation = false, $gatewayTypeId = false)
2016-06-20 16:14:43 +02:00
{
/** @var AccountGateway $accountGateway */
2016-09-15 12:41:09 +02:00
if ($accountGateway = $this->getGatewayByType($gatewayTypeId)) {
return $accountGateway->paymentDriver($invitation, $gatewayTypeId);
2016-06-21 18:10:22 +02:00
}
2016-06-20 16:14:43 +02:00
2016-06-21 18:10:22 +02:00
return false;
2016-06-20 16:14:43 +02:00
}
/**
* @return mixed
*/
2016-06-09 09:56:22 +02:00
public function gatewayIds()
{
return $this->account_gateways()->pluck('gateway_id')->toArray();
}
/**
* @param $gatewayId
2017-01-30 20:40:43 +01:00
*
* @return bool
*/
2016-06-09 09:56:22 +02:00
public function hasGatewayId($gatewayId)
{
return in_array($gatewayId, $this->gatewayIds());
}
/**
* @param $gatewayId
2017-01-30 20:40:43 +01:00
*
* @return bool
*/
2015-03-16 22:45:25 +01:00
public function getGatewayConfig($gatewayId)
{
foreach ($this->account_gateways as $gateway) {
if ($gateway->gateway_id == $gatewayId) {
return $gateway;
}
}
return false;
}
/**
* @return mixed
*/
2016-05-08 14:37:32 +02:00
public function getPrimaryUser()
{
return $this->users()
->orderBy('id')
->first();
}
/**
* @param $userId
* @param $name
2017-01-30 20:40:43 +01:00
*
* @return null
*/
2016-05-08 09:05:16 +02:00
public function getToken($userId, $name)
2015-11-04 16:23:22 +01:00
{
foreach ($this->account_tokens as $token) {
2016-05-08 09:05:16 +02:00
if ($token->user_id == $userId && $token->name === $name) {
2015-11-04 16:23:22 +01:00
return $token->token;
}
}
return null;
}
/**
* @param $entityType
* @param null $clientId
2017-01-30 20:40:43 +01:00
*
* @return mixed
*/
2016-02-25 19:34:23 +01:00
public function createInvoice($entityType = ENTITY_INVOICE, $clientId = null)
2015-10-25 08:13:06 +01:00
{
$invoice = Invoice::createNew();
2015-11-28 18:19:37 +01:00
$invoice->is_recurring = false;
2016-05-26 16:56:54 +02:00
$invoice->invoice_type_id = INVOICE_TYPE_STANDARD;
2015-10-25 08:13:06 +01:00
$invoice->invoice_date = Utils::today();
$invoice->start_date = Utils::today();
$invoice->invoice_design_id = $this->invoice_design_id;
$invoice->client_id = $clientId;
2017-03-16 19:20:08 +01:00
$invoice->custom_taxes1 = $this->custom_invoice_taxes1;
$invoice->custom_taxes2 = $this->custom_invoice_taxes2;
2016-05-29 11:26:02 +02:00
2015-10-25 08:13:06 +01:00
if ($entityType === ENTITY_RECURRING_INVOICE) {
$invoice->invoice_number = microtime(true);
$invoice->is_recurring = true;
} else {
if ($entityType == ENTITY_QUOTE) {
2016-05-26 16:56:54 +02:00
$invoice->invoice_type_id = INVOICE_TYPE_QUOTE;
2017-06-01 18:13:13 +02:00
$invoice->invoice_design_id = $this->quote_design_id;
2015-10-25 08:13:06 +01:00
}
2017-01-30 20:40:43 +01:00
if ($this->hasClientNumberPattern($invoice) && ! $clientId) {
2015-10-25 08:13:06 +01:00
// do nothing, we don't yet know the value
2017-01-30 17:05:31 +01:00
} elseif (! $invoice->invoice_number) {
2017-01-04 09:11:32 +01:00
$invoice->invoice_number = $this->getNextNumber($invoice);
2015-10-25 08:13:06 +01:00
}
}
2016-05-29 11:26:02 +02:00
2017-01-30 20:40:43 +01:00
if (! $clientId) {
2015-10-25 08:13:06 +01:00
$invoice->client = Client::createNew();
$invoice->client->public_id = 0;
}
return $invoice;
}
/**
* @param bool $client
*/
public function loadLocalizationSettings($client = false)
2015-03-16 22:45:25 +01:00
{
$this->load('timezone', 'date_format', 'datetime_format', 'language');
2015-10-15 16:14:13 +02:00
$timezone = $this->timezone ? $this->timezone->name : DEFAULT_TIMEZONE;
Session::put(SESSION_TIMEZONE, $timezone);
2015-03-16 22:45:25 +01:00
Session::put(SESSION_DATE_FORMAT, $this->date_format ? $this->date_format->format : DEFAULT_DATE_FORMAT);
Session::put(SESSION_DATE_PICKER_FORMAT, $this->date_format ? $this->date_format->picker_format : DEFAULT_DATE_PICKER_FORMAT);
2015-08-11 16:38:36 +02:00
2016-05-29 11:26:02 +02:00
$currencyId = ($client && $client->currency_id) ? $client->currency_id : $this->currency_id ?: DEFAULT_CURRENCY;
$locale = ($client && $client->language_id) ? $client->language->locale : ($this->language_id ? $this->Language->locale : DEFAULT_LOCALE);
Session::put(SESSION_CURRENCY, $currencyId);
Session::put(SESSION_CURRENCY_DECORATOR, $this->show_currency_code ? CURRENCY_DECORATOR_CODE : CURRENCY_DECORATOR_SYMBOL);
Session::put(SESSION_LOCALE, $locale);
App::setLocale($locale);
$format = $this->datetime_format ? $this->datetime_format->format : DEFAULT_DATETIME_FORMAT;
if ($this->military_time) {
$format = str_replace('g:i a', 'H:i', $format);
}
Session::put(SESSION_DATETIME_FORMAT, $format);
Session::put('start_of_week', $this->start_of_week);
2015-03-16 22:45:25 +01:00
}
/**
* @return bool
*/
public function isNinjaAccount()
{
2017-05-25 19:29:47 +02:00
return strpos($this->account_key, 'zg4ylmzDkdkPOT8yoKQw9LTWaoZJx7') === 0;
}
/**
* @return bool
*/
public function isNinjaOrLicenseAccount()
{
return $this->isNinjaAccount() || $this->account_key == NINJA_LICENSE_ACCOUNT_KEY;
}
/**
* @param $plan
*/
2016-04-17 00:34:39 +02:00
public function startTrial($plan)
{
2017-01-30 17:05:31 +01:00
if (! Utils::isNinja()) {
return;
}
2016-05-29 11:26:02 +02:00
2017-01-04 12:20:56 +01:00
if ($this->company->trial_started && $this->company->trial_started != '0000-00-00') {
return;
}
2016-04-17 00:34:39 +02:00
$this->company->trial_plan = $plan;
$this->company->trial_started = date_create()->format('Y-m-d');
$this->company->save();
}
public function hasReminders()
{
if (! $this->hasFeature(FEATURE_EMAIL_TEMPLATES_REMINDERS)) {
return false;
}
2017-12-25 20:46:59 +01:00
return $this->enable_reminder1 || $this->enable_reminder2 || $this->enable_reminder3 || $this->enable_reminder4;
}
/**
* @param $feature
2017-01-30 20:40:43 +01:00
*
* @return bool
*/
public function hasFeature($feature)
{
2016-05-01 10:43:30 +02:00
if (Utils::isNinjaDev()) {
return true;
}
2016-05-29 11:26:02 +02:00
$planDetails = $this->getPlanDetails();
2017-01-30 20:40:43 +01:00
$selfHost = ! Utils::isNinjaProd();
2016-05-29 11:26:02 +02:00
2017-01-30 20:40:43 +01:00
if (! $selfHost && function_exists('ninja_account_features')) {
$result = ninja_account_features($this, $feature);
2016-05-29 11:26:02 +02:00
if ($result != null) {
return $result;
}
}
2016-05-29 11:26:02 +02:00
switch ($feature) {
// Pro
2016-08-01 10:07:44 +02:00
case FEATURE_TASKS:
case FEATURE_EXPENSES:
2017-12-20 12:04:36 +01:00
case FEATURE_QUOTES:
return true;
2016-08-01 10:07:44 +02:00
case FEATURE_CUSTOMIZE_INVOICE_DESIGN:
case FEATURE_DIFFERENT_DESIGNS:
case FEATURE_EMAIL_TEMPLATES_REMINDERS:
case FEATURE_INVOICE_SETTINGS:
case FEATURE_CUSTOM_EMAILS:
case FEATURE_PDF_ATTACHMENT:
case FEATURE_MORE_INVOICE_DESIGNS:
case FEATURE_REPORTS:
2016-07-13 11:03:39 +02:00
case FEATURE_BUY_NOW_BUTTONS:
case FEATURE_API:
case FEATURE_CLIENT_PORTAL_PASSWORD:
case FEATURE_CUSTOM_URL:
2017-01-30 20:40:43 +01:00
return $selfHost || ! empty($planDetails);
2016-05-29 11:26:02 +02:00
// Pro; No trial allowed, unless they're trialing enterprise with an active pro plan
case FEATURE_MORE_CLIENTS:
2017-01-30 20:40:43 +01:00
return $selfHost || ! empty($planDetails) && (! $planDetails['trial'] || ! empty($this->getPlanDetails(false, false)));
// White Label
case FEATURE_WHITE_LABEL:
2017-01-30 20:40:43 +01:00
if ($this->isNinjaAccount() || (! $selfHost && $planDetails && ! $planDetails['expires'])) {
return false;
}
// Fallthrough
2016-06-29 21:48:56 +02:00
case FEATURE_REMOVE_CREATED_BY:
2017-01-30 20:40:43 +01:00
return ! empty($planDetails); // A plan is required even for self-hosted users
2016-05-29 11:26:02 +02:00
// Enterprise; No Trial allowed; grandfathered for old pro users
case FEATURE_USERS:// Grandfathered for old Pro users
2017-01-30 17:05:31 +01:00
if ($planDetails && $planDetails['trial']) {
// Do they have a non-trial plan?
$planDetails = $this->getPlanDetails(false, false);
}
2016-05-29 11:26:02 +02:00
2017-01-30 20:40:43 +01:00
return $selfHost || ! empty($planDetails) && ($planDetails['plan'] == PLAN_ENTERPRISE || $planDetails['started'] <= date_create(PRO_USERS_GRANDFATHER_DEADLINE));
2016-05-29 11:26:02 +02:00
// Enterprise; No Trial allowed
case FEATURE_DOCUMENTS:
case FEATURE_USER_PERMISSIONS:
2017-01-30 20:40:43 +01:00
return $selfHost || ! empty($planDetails) && $planDetails['plan'] == PLAN_ENTERPRISE && ! $planDetails['trial'];
2016-05-29 11:26:02 +02:00
default:
return false;
}
}
2016-05-29 11:26:02 +02:00
2017-10-22 11:37:24 +02:00
public function isPaid()
{
2018-02-04 09:53:32 +01:00
return Utils::isNinja() ? ($this->isPro() && ! $this->isTrial()) : Utils::isWhiteLabel();
2017-10-22 11:37:24 +02:00
}
/**
* @param null $plan_details
2017-01-30 20:40:43 +01:00
*
* @return bool
*/
2016-04-17 00:34:39 +02:00
public function isPro(&$plan_details = null)
2015-03-16 22:45:25 +01:00
{
2017-01-30 20:40:43 +01:00
if (! Utils::isNinjaProd()) {
2015-03-16 22:45:25 +01:00
return true;
}
if ($this->isNinjaAccount()) {
2015-03-16 22:45:25 +01:00
return true;
}
2016-04-17 00:34:39 +02:00
$plan_details = $this->getPlanDetails();
2016-05-29 11:26:02 +02:00
2017-01-30 20:40:43 +01:00
return ! empty($plan_details);
2016-04-17 00:34:39 +02:00
}
2017-12-01 10:48:13 +01:00
/**
* @return mixed
*/
public function hasActivePromo()
{
return $this->company->hasActivePromo();
}
/**
* @param null $plan_details
2017-01-30 20:40:43 +01:00
*
* @return bool
*/
2016-04-17 00:34:39 +02:00
public function isEnterprise(&$plan_details = null)
{
2017-01-30 20:40:43 +01:00
if (! Utils::isNinjaProd()) {
2016-04-17 00:34:39 +02:00
return true;
}
2015-03-16 22:45:25 +01:00
2016-04-17 00:34:39 +02:00
if ($this->isNinjaAccount()) {
2015-03-16 22:45:25 +01:00
return true;
}
2016-04-17 00:34:39 +02:00
$plan_details = $this->getPlanDetails();
2016-05-29 11:26:02 +02:00
2016-04-17 18:55:03 +02:00
return $plan_details && $plan_details['plan'] == PLAN_ENTERPRISE;
2016-04-17 00:34:39 +02:00
}
2016-05-29 11:26:02 +02:00
/**
* @param bool $include_inactive
* @param bool $include_trial
2017-01-30 20:40:43 +01:00
*
* @return array|null
*/
2016-04-17 18:55:03 +02:00
public function getPlanDetails($include_inactive = false, $include_trial = true)
2016-04-17 00:34:39 +02:00
{
2017-01-30 20:40:43 +01:00
if (! $this->company) {
2016-04-17 00:34:39 +02:00
return null;
}
2016-05-29 11:26:02 +02:00
2016-04-17 00:34:39 +02:00
$plan = $this->company->plan;
2016-07-11 19:08:43 +02:00
$price = $this->company->plan_price;
2016-04-17 00:34:39 +02:00
$trial_plan = $this->company->trial_plan;
2016-05-29 11:26:02 +02:00
2017-01-30 20:40:43 +01:00
if ((! $plan || $plan == PLAN_FREE) && (! $trial_plan || ! $include_trial)) {
2016-04-17 00:34:39 +02:00
return null;
2016-05-29 11:26:02 +02:00
}
2016-04-17 00:34:39 +02:00
$trial_active = false;
if ($trial_plan && $include_trial) {
$trial_started = DateTime::createFromFormat('Y-m-d', $this->company->trial_started);
$trial_expires = clone $trial_started;
$trial_expires->modify('+2 weeks');
2016-05-29 11:26:02 +02:00
2016-04-17 04:09:01 +02:00
if ($trial_expires >= date_create()) {
2017-01-30 17:05:31 +01:00
$trial_active = true;
2016-04-17 00:34:39 +02:00
}
}
2016-05-29 11:26:02 +02:00
2016-04-17 00:34:39 +02:00
$plan_active = false;
2016-05-29 11:26:02 +02:00
if ($plan) {
if ($this->company->plan_expires == null) {
2016-04-17 00:34:39 +02:00
$plan_active = true;
$plan_expires = false;
} else {
$plan_expires = DateTime::createFromFormat('Y-m-d', $this->company->plan_expires);
2016-04-17 04:09:01 +02:00
if ($plan_expires >= date_create()) {
2016-04-17 00:34:39 +02:00
$plan_active = true;
}
}
}
2016-05-29 11:26:02 +02:00
2017-01-30 20:40:43 +01:00
if (! $include_inactive && ! $plan_active && ! $trial_active) {
2016-04-17 18:55:03 +02:00
return null;
}
2016-05-29 11:26:02 +02:00
2016-04-17 00:34:39 +02:00
// Should we show plan details or trial details?
2017-01-30 20:40:43 +01:00
if (($plan && ! $trial_plan) || ! $include_trial) {
2016-04-17 00:34:39 +02:00
$use_plan = true;
2017-01-30 20:40:43 +01:00
} elseif (! $plan && $trial_plan) {
2016-04-17 00:34:39 +02:00
$use_plan = false;
} else {
// There is both a plan and a trial
2017-01-30 20:40:43 +01:00
if (! empty($plan_active) && empty($trial_active)) {
2016-04-17 00:34:39 +02:00
$use_plan = true;
2017-01-30 20:40:43 +01:00
} elseif (empty($plan_active) && ! empty($trial_active)) {
2016-04-17 00:34:39 +02:00
$use_plan = false;
2017-01-30 20:40:43 +01:00
} elseif (! empty($plan_active) && ! empty($trial_active)) {
2016-04-17 00:34:39 +02:00
// Both are active; use whichever is a better plan
if ($plan == PLAN_ENTERPRISE) {
$use_plan = true;
} elseif ($trial_plan == PLAN_ENTERPRISE) {
$use_plan = false;
} else {
// They're both the same; show the plan
$use_plan = true;
}
2016-04-17 18:55:03 +02:00
} else {
// Neither are active; use whichever expired most recently
$use_plan = $plan_expires >= $trial_expires;
2016-04-17 00:34:39 +02:00
}
}
2016-05-29 11:26:02 +02:00
2016-04-17 00:34:39 +02:00
if ($use_plan) {
return [
2016-07-11 19:08:43 +02:00
'company_id' => $this->company->id,
'num_users' => $this->company->num_users,
'plan_price' => $price,
2016-04-17 00:34:39 +02:00
'trial' => false,
'plan' => $plan,
'started' => DateTime::createFromFormat('Y-m-d', $this->company->plan_started),
'expires' => $plan_expires,
'paid' => DateTime::createFromFormat('Y-m-d', $this->company->plan_paid),
'term' => $this->company->plan_term,
'active' => $plan_active,
];
2016-04-17 00:34:39 +02:00
} else {
return [
2016-07-11 19:08:43 +02:00
'company_id' => $this->company->id,
'num_users' => 1,
'plan_price' => 0,
2016-04-17 00:34:39 +02:00
'trial' => true,
'plan' => $trial_plan,
'started' => $trial_started,
'expires' => $trial_expires,
'active' => $trial_active,
];
2016-04-17 00:34:39 +02:00
}
}
/**
* @return bool
*/
public function isTrial()
{
2017-01-30 20:40:43 +01:00
if (! Utils::isNinjaProd()) {
2016-02-15 18:58:18 +01:00
return false;
}
2016-05-29 11:26:02 +02:00
2016-04-17 00:34:39 +02:00
$plan_details = $this->getPlanDetails();
2016-02-15 18:58:18 +01:00
2016-04-17 18:55:03 +02:00
return $plan_details && $plan_details['trial'];
}
/**
* @return int
*/
public function getCountTrialDaysLeft()
{
2016-04-17 18:55:03 +02:00
$planDetails = $this->getPlanDetails(true);
2016-05-29 11:26:02 +02:00
2017-01-30 20:40:43 +01:00
if (! $planDetails || ! $planDetails['trial']) {
2016-04-17 00:34:39 +02:00
return 0;
}
2016-05-29 11:26:02 +02:00
2016-04-17 00:34:39 +02:00
$today = new DateTime('now');
$interval = $today->diff($planDetails['expires']);
2016-05-29 11:26:02 +02:00
return $interval ? $interval->d : 0;
}
/**
* @return mixed
*/
public function getRenewalDate()
{
2016-04-17 00:34:39 +02:00
$planDetails = $this->getPlanDetails();
2016-05-29 11:26:02 +02:00
2016-04-17 18:55:03 +02:00
if ($planDetails) {
2016-04-17 00:34:39 +02:00
$date = $planDetails['expires'];
$date = max($date, date_create());
} else {
$date = date_create();
}
2016-12-15 11:52:10 +01:00
return Carbon::instance($date);
2015-03-16 22:45:25 +01:00
}
/**
* @param $eventId
2017-01-30 20:40:43 +01:00
*
* @return \Illuminate\Database\Eloquent\Model|null|static
*/
2017-11-29 12:12:20 +01:00
public function getSubscriptions($eventId)
2015-03-16 22:45:25 +01:00
{
2017-11-29 12:12:20 +01:00
return Subscription::where('account_id', '=', $this->id)->where('event_id', '=', $eventId)->get();
2015-03-16 22:45:25 +01:00
}
/**
* @return $this
*/
2015-03-16 22:45:25 +01:00
public function hideFieldsForViz()
{
foreach ($this->clients as $client) {
$client->setVisible([
'public_id',
'name',
'balance',
'paid_to_date',
'invoices',
'contacts',
'currency_id',
'currency',
2015-03-16 22:45:25 +01:00
]);
foreach ($client->invoices as $invoice) {
$invoice->setVisible([
'public_id',
'invoice_number',
'amount',
'balance',
'invoice_status_id',
'invoice_items',
'created_at',
2015-08-11 16:38:36 +02:00
'is_recurring',
2016-05-26 16:56:54 +02:00
'invoice_type_id',
'is_public',
'due_date',
2015-03-16 22:45:25 +01:00
]);
foreach ($invoice->invoice_items as $invoiceItem) {
$invoiceItem->setVisible([
'product_key',
'cost',
'qty',
'discount',
2015-03-16 22:45:25 +01:00
]);
}
}
foreach ($client->contacts as $contact) {
$contact->setVisible([
'public_id',
'first_name',
'last_name',
'email', ]);
}
}
return $this;
}
/**
* @param null $storage_gateway
2017-01-30 20:40:43 +01:00
*
* @return bool
*/
2016-04-27 01:59:52 +02:00
public function showTokenCheckbox(&$storage_gateway = null)
2015-03-16 22:45:25 +01:00
{
2017-01-30 20:40:43 +01:00
if (! ($storage_gateway = $this->getTokenGatewayId())) {
return false;
}
2015-03-16 22:45:25 +01:00
return $this->token_billing_type_id == TOKEN_BILLING_OPT_IN
|| $this->token_billing_type_id == TOKEN_BILLING_OPT_OUT;
}
/**
* @return bool
*/
2017-01-30 17:05:31 +01:00
public function getTokenGatewayId()
{
2016-04-27 01:59:52 +02:00
if ($this->isGatewayConfigured(GATEWAY_STRIPE)) {
return GATEWAY_STRIPE;
} elseif ($this->isGatewayConfigured(GATEWAY_BRAINTREE)) {
return GATEWAY_BRAINTREE;
} elseif ($this->isGatewayConfigured(GATEWAY_WEPAY)) {
return GATEWAY_WEPAY;
2016-04-27 01:59:52 +02:00
} else {
return false;
}
}
/**
* @return bool|void
*/
2017-01-30 17:05:31 +01:00
public function getTokenGateway()
{
2016-04-27 01:59:52 +02:00
$gatewayId = $this->getTokenGatewayId();
2017-01-30 20:40:43 +01:00
if (! $gatewayId) {
2016-04-27 01:59:52 +02:00
return;
}
return $this->getGatewayConfig($gatewayId);
}
2018-07-07 21:13:02 +02:00
public function getLocale()
{
return $this->language_id && $this->language ? $this->language->locale : DEFAULT_LOCALE;
}
/**
* @return bool
*/
2015-03-16 22:45:25 +01:00
public function selectTokenCheckbox()
{
return $this->token_billing_type_id == TOKEN_BILLING_OPT_OUT;
}
2015-09-17 21:01:06 +02:00
/**
* @return string
*/
2015-09-17 21:01:06 +02:00
public function getSiteUrl()
{
2018-01-08 11:14:09 +01:00
$url = trim(SITE_URL, '/');
2015-09-17 21:01:06 +02:00
$iframe_url = $this->iframe_url;
2016-05-29 11:26:02 +02:00
2015-09-17 21:01:06 +02:00
if ($iframe_url) {
return "{$iframe_url}/?";
2017-01-30 17:05:31 +01:00
} elseif ($this->subdomain) {
2015-09-17 21:01:06 +02:00
$url = Utils::replaceSubdomain($url, $this->subdomain);
}
return $url;
}
2015-10-13 09:11:44 +02:00
/**
* @param $host
2017-01-30 20:40:43 +01:00
*
* @return bool
*/
2015-10-13 09:11:44 +02:00
public function checkSubdomain($host)
{
2017-01-30 20:40:43 +01:00
if (! $this->subdomain) {
2015-10-13 09:11:44 +02:00
return true;
}
$server = explode('.', $host);
$subdomain = $server[0];
2017-01-30 20:40:43 +01:00
if (! in_array($subdomain, ['app', 'www']) && $subdomain != $this->subdomain) {
2015-10-13 09:11:44 +02:00
return false;
}
return true;
}
/**
* @return bool
*/
public function attachPDF()
2015-10-13 09:11:44 +02:00
{
2016-04-19 22:31:50 +02:00
return $this->hasFeature(FEATURE_PDF_ATTACHMENT) && $this->pdf_email_attachment;
2015-10-13 09:11:44 +02:00
}
2016-05-29 11:26:02 +02:00
2018-01-15 07:05:07 +01:00
/**
* @return bool
*/
public function attachUBL()
{
return $this->hasFeature(FEATURE_PDF_ATTACHMENT) && $this->ubl_email_attachment;
}
/**
* @return mixed
*/
public function getEmailDesignId()
{
2018-09-19 22:41:30 +02:00
return $this->hasFeature(FEATURE_CUSTOM_EMAILS) ? $this->email_design_id : EMAIL_DESIGN_PLAIN;
}
/**
* @return string
*/
2017-01-30 17:05:31 +01:00
public function clientViewCSS()
{
$css = '';
2016-05-29 11:26:02 +02:00
if ($this->hasFeature(FEATURE_CUSTOMIZE_INVOICE_DESIGN)) {
2016-01-07 08:08:30 +01:00
$bodyFont = $this->getBodyFontCss();
$headerFont = $this->getHeaderFontCss();
2016-05-29 11:26:02 +02:00
2016-01-07 08:08:30 +01:00
$css = 'body{'.$bodyFont.'}';
if ($headerFont != $bodyFont) {
$css .= 'h1,h2,h3,h4,h5,h6,.h1,.h2,.h3,.h4,.h5,.h6{'.$headerFont.'}';
}
2017-01-12 14:12:02 +01:00
$css .= $this->client_view_css;
}
2016-05-29 11:26:02 +02:00
2016-01-07 08:08:30 +01:00
return $css;
}
2016-05-29 11:26:02 +02:00
/**
* @param string $protocol
2017-01-30 20:40:43 +01:00
*
* @return string
*/
2017-01-30 17:05:31 +01:00
public function getFontsUrl($protocol = '')
{
2016-01-11 13:50:17 +01:00
$bodyFont = $this->getHeaderFontId();
$headerFont = $this->getBodyFontId();
2016-01-07 08:08:30 +01:00
$bodyFontSettings = Utils::getFromCache($bodyFont, 'fonts');
$google_fonts = [$bodyFontSettings['google_font']];
2016-05-29 11:26:02 +02:00
2017-01-30 17:05:31 +01:00
if ($headerFont != $bodyFont) {
2016-01-07 08:08:30 +01:00
$headerFontSettings = Utils::getFromCache($headerFont, 'fonts');
$google_fonts[] = $headerFontSettings['google_font'];
}
2017-01-30 20:40:43 +01:00
return ($protocol ? $protocol.':' : '').'//fonts.googleapis.com/css?family='.implode('|', $google_fonts);
2016-01-07 08:08:30 +01:00
}
2016-05-29 11:26:02 +02:00
/**
* @return mixed
*/
2017-01-30 17:05:31 +01:00
public function getHeaderFontId()
{
return ($this->hasFeature(FEATURE_CUSTOMIZE_INVOICE_DESIGN) && $this->header_font_id) ? $this->header_font_id : DEFAULT_HEADER_FONT;
2016-01-11 13:50:17 +01:00
}
/**
* @return mixed
*/
2017-01-30 17:05:31 +01:00
public function getBodyFontId()
{
return ($this->hasFeature(FEATURE_CUSTOMIZE_INVOICE_DESIGN) && $this->body_font_id) ? $this->body_font_id : DEFAULT_BODY_FONT;
2016-01-11 13:50:17 +01:00
}
/**
* @return null
*/
2017-01-30 17:05:31 +01:00
public function getHeaderFontName()
{
2016-01-11 13:50:17 +01:00
return Utils::getFromCache($this->getHeaderFontId(), 'fonts')['name'];
2016-01-07 08:08:30 +01:00
}
2016-05-29 11:26:02 +02:00
/**
* @return null
*/
2017-01-30 17:05:31 +01:00
public function getBodyFontName()
{
2016-01-11 13:50:17 +01:00
return Utils::getFromCache($this->getBodyFontId(), 'fonts')['name'];
2016-01-07 08:08:30 +01:00
}
2016-05-29 11:26:02 +02:00
/**
* @param bool $include_weight
2017-01-30 20:40:43 +01:00
*
* @return string
*/
2017-01-30 17:05:31 +01:00
public function getHeaderFontCss($include_weight = true)
{
2016-01-11 13:50:17 +01:00
$font_data = Utils::getFromCache($this->getHeaderFontId(), 'fonts');
2016-01-07 08:08:30 +01:00
$css = 'font-family:'.$font_data['css_stack'].';';
2016-05-29 11:26:02 +02:00
2017-01-30 17:05:31 +01:00
if ($include_weight) {
2016-01-07 08:08:30 +01:00
$css .= 'font-weight:'.$font_data['css_weight'].';';
}
2016-05-29 11:26:02 +02:00
2016-01-07 08:08:30 +01:00
return $css;
}
2016-05-29 11:26:02 +02:00
/**
* @param bool $include_weight
2017-01-30 20:40:43 +01:00
*
* @return string
*/
2017-01-30 17:05:31 +01:00
public function getBodyFontCss($include_weight = true)
{
2016-01-11 13:50:17 +01:00
$font_data = Utils::getFromCache($this->getBodyFontId(), 'fonts');
2016-01-07 08:08:30 +01:00
$css = 'font-family:'.$font_data['css_stack'].';';
2016-05-29 11:26:02 +02:00
2017-01-30 17:05:31 +01:00
if ($include_weight) {
2016-01-07 08:08:30 +01:00
$css .= 'font-weight:'.$font_data['css_weight'].';';
}
2016-05-29 11:26:02 +02:00
2016-01-07 08:08:30 +01:00
return $css;
}
2016-05-29 11:26:02 +02:00
/**
* @return array
*/
2017-01-30 17:05:31 +01:00
public function getFonts()
{
return array_unique([$this->getHeaderFontId(), $this->getBodyFontId()]);
2016-01-07 08:08:30 +01:00
}
2016-05-29 11:26:02 +02:00
/**
* @return array
*/
2017-01-30 17:05:31 +01:00
public function getFontsData()
{
$data = [];
2016-05-29 11:26:02 +02:00
2017-01-30 17:05:31 +01:00
foreach ($this->getFonts() as $font) {
2016-01-07 08:08:30 +01:00
$data[] = Utils::getFromCache($font, 'fonts');
}
2016-05-29 11:26:02 +02:00
2016-01-07 08:08:30 +01:00
return $data;
}
2016-05-29 11:26:02 +02:00
/**
* @return array
*/
2017-01-30 17:05:31 +01:00
public function getFontFolders()
{
return array_map(function ($item) {
return $item['folder'];
}, $this->getFontsData());
}
2016-10-27 10:57:51 +02:00
public function isModuleEnabled($entityType)
{
2017-01-30 17:05:31 +01:00
if (! in_array($entityType, [
2016-11-13 08:14:25 +01:00
ENTITY_RECURRING_INVOICE,
ENTITY_CREDIT,
ENTITY_QUOTE,
ENTITY_TASK,
ENTITY_EXPENSE,
ENTITY_VENDOR,
2017-11-30 12:57:39 +01:00
ENTITY_PROJECT,
2018-02-08 11:39:58 +01:00
ENTITY_PROPOSAL,
2016-10-27 10:57:51 +02:00
])) {
return true;
}
2017-11-30 12:57:39 +01:00
if ($entityType == ENTITY_VENDOR) {
$entityType = ENTITY_EXPENSE;
} elseif ($entityType == ENTITY_PROJECT) {
$entityType = ENTITY_TASK;
2018-02-08 11:39:58 +01:00
} elseif ($entityType == ENTITY_PROPOSAL) {
$entityType = ENTITY_QUOTE;
2017-11-30 12:57:39 +01:00
}
// note: single & checks bitmask match
2016-10-27 10:57:51 +02:00
return $this->enabled_modules & static::$modules[$entityType];
}
2016-11-04 14:34:15 +01:00
public function requiresAuthorization($invoice)
2016-11-04 14:34:15 +01:00
{
return $this->showAcceptTerms($invoice) || $this->showSignature($invoice);
}
public function showAcceptTerms($invoice)
{
if (! $this->isPro()) {
2016-11-04 14:34:15 +01:00
return false;
}
2017-01-01 12:22:05 +01:00
return $invoice->isQuote() ? $this->show_accept_quote_terms : $this->show_accept_invoice_terms;
2016-11-04 14:34:15 +01:00
}
public function showSignature($invoice)
{
2017-01-30 17:05:31 +01:00
if (! $this->isPro()) {
2016-11-04 14:34:15 +01:00
return false;
}
2017-01-01 12:22:05 +01:00
return $invoice->isQuote() ? $this->require_quote_signature : $this->require_invoice_signature;
2016-11-04 14:34:15 +01:00
}
2017-01-12 14:12:02 +01:00
public function emailMarkupEnabled()
{
2017-01-30 17:05:31 +01:00
if (! Utils::isNinja()) {
2017-01-12 14:12:02 +01:00
return false;
}
return $this->enable_email_markup;
}
2017-01-16 12:59:46 +01:00
2017-01-30 13:35:04 +01:00
public function defaultDaysDue($client = false)
2017-01-16 12:59:46 +01:00
{
2017-01-30 13:35:04 +01:00
if ($client && $client->payment_terms != 0) {
return $client->defaultDaysDue();
}
return $this->payment_terms == -1 ? 0 : $this->payment_terms;
}
public function defaultDueDate($client = false)
{
if ($client && $client->payment_terms != 0) {
$numDays = $client->defaultDaysDue();
} elseif ($this->payment_terms != 0) {
$numDays = $this->defaultDaysDue();
} else {
return null;
}
2017-01-16 12:59:46 +01:00
2017-01-30 13:35:04 +01:00
return Carbon::now()->addDays($numDays)->format('Y-m-d');
2017-01-16 12:59:46 +01:00
}
public function hasMultipleAccounts()
{
return $this->company->accounts->count() > 1;
}
2018-05-14 19:12:25 +02:00
public function hasMultipleUsers()
{
return $this->users->count() > 1;
}
public function getPrimaryAccount()
{
return $this->company->accounts()->orderBy('id')->first();
}
2018-09-28 08:51:48 +02:00
public function financialYearStartMonth()
{
if (! $this->financial_year_start) {
return 1;
}
$yearStart = Carbon::parse($this->financial_year_start);
return $yearStart ? $yearStart->month : 1;
}
public function financialYearStart()
{
if (! $this->financial_year_start) {
return false;
}
$yearStart = Carbon::parse($this->financial_year_start);
$yearStart->year = date('Y');
if ($yearStart->isFuture()) {
$yearStart->subYear();
}
return $yearStart->format('Y-m-d');
}
public function isClientPortalPasswordEnabled()
{
return $this->hasFeature(FEATURE_CLIENT_PORTAL_PASSWORD) && $this->enable_portal_password;
}
2017-12-03 14:04:17 +01:00
public function getBaseUrl()
{
if ($this->hasFeature(FEATURE_CUSTOM_URL)) {
if ($this->iframe_url) {
return $this->iframe_url;
}
if (Utils::isNinjaProd() && ! Utils::isReseller()) {
$url = $this->present()->clientPortalLink();
} else {
$url = url('/');
}
if ($this->subdomain) {
$url = Utils::replaceSubdomain($url, $this->subdomain);
}
return $url;
} else {
return url('/');
}
}
2018-03-13 15:04:02 +01:00
public function requiresAddressState() {
return true;
//return ! $this->country_id || $this->country_id == DEFAULT_COUNTRY;
}
2015-08-14 14:04:33 +02:00
}
2017-05-01 10:13:15 +02:00
Account::creating(function ($account)
{
LookupAccount::createAccount($account->account_key, $account->company_id);
});
2017-11-15 13:59:20 +01:00
Account::updating(function ($account) {
$dirty = $account->getDirty();
if (array_key_exists('subdomain', $dirty)) {
LookupAccount::updateAccount($account->account_key, $account);
}
});
2017-01-30 17:05:31 +01:00
Account::updated(function ($account) {
2016-06-02 21:03:59 +02:00
// prevent firing event if the invoice/quote counter was changed
// TODO: remove once counters are moved to separate table
$dirty = $account->getDirty();
if (isset($dirty['invoice_number_counter']) || isset($dirty['quote_number_counter'])) {
return;
}
2015-08-14 14:04:33 +02:00
Event::fire(new UserSettingsChanged());
});
2017-05-01 11:29:45 +02:00
Account::deleted(function ($account)
{
LookupAccount::deleteWhere([
'account_key' => $account->account_key
]);
});