1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-09 20:52:56 +01:00
invoiceninja/app/Models/Account.php

1118 lines
29 KiB
PHP
Raw Normal View History

2015-03-31 11:38:24 +02:00
<?php namespace App\Models;
2015-03-16 22:45:25 +01:00
2015-03-23 07:52:01 +01:00
use Eloquent;
2015-03-26 04:52:42 +01:00
use Utils;
2015-03-31 19:42:37 +02:00
use Session;
2015-04-28 22:13:52 +02:00
use DateTime;
2015-08-14 14:04:33 +02:00
use Event;
2015-12-02 14:26:06 +01:00
use Cache;
2015-08-11 16:38:36 +02:00
use App;
2015-11-29 21:13:50 +01:00
use File;
2015-08-14 14:04:33 +02:00
use App\Events\UserSettingsChanged;
2015-03-31 11:38:24 +02:00
use Illuminate\Database\Eloquent\SoftDeletes;
2015-11-06 11:59:53 +01:00
use Laracasts\Presenter\PresentableTrait;
2015-03-31 11:38:24 +02:00
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;
2015-11-06 11:59:53 +01:00
protected $presenter = 'App\Ninja\Presenters\AccountPresenter';
2015-03-31 11:38:24 +02:00
protected $dates = ['deleted_at'];
2015-10-02 10:32:13 +02:00
protected $hidden = ['ip'];
2015-03-16 22:45:25 +01:00
2016-02-03 13:41:40 +01:00
protected $fillable = [
'name',
'id_number',
'vat_number',
'work_email',
'website',
'work_phone',
'address1',
'address2',
'city',
'state',
'postal_code',
'country_id',
'size_id',
'industry_id',
'email_footer',
'timezone_id',
'date_format_id',
'datetime_format_id',
'currency_id',
'language_id',
'military_time',
2016-02-03 13:41:40 +01:00
];
2015-10-14 16:15:39 +02:00
public static $basicSettings = [
ACCOUNT_COMPANY_DETAILS,
ACCOUNT_USER_DETAILS,
ACCOUNT_LOCALIZATION,
ACCOUNT_PAYMENTS,
2016-01-26 21:22:33 +01:00
ACCOUNT_BANKS,
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,
];
public static $advancedSettings = [
ACCOUNT_INVOICE_SETTINGS,
2015-10-16 07:32:02 +02:00
ACCOUNT_INVOICE_DESIGN,
2015-12-15 21:25:12 +01:00
ACCOUNT_EMAIL_SETTINGS,
2015-10-14 16:15:39 +02:00
ACCOUNT_TEMPLATES_AND_REMINDERS,
2016-01-12 10:18:35 +01:00
ACCOUNT_CLIENT_PORTAL,
2015-10-14 16:15:39 +02:00
ACCOUNT_CHARTS_AND_REPORTS,
ACCOUNT_DATA_VISUALIZATIONS,
ACCOUNT_USER_MANAGEMENT,
ACCOUNT_API_TOKENS,
];
/*
2015-07-21 20:51:56 +02:00
protected $casts = [
2015-09-07 11:07:55 +02:00
'invoice_settings' => 'object',
2015-07-21 20:51:56 +02:00
];
*/
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');
}
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
}
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
}
2015-11-15 12:04:06 +01:00
public function contacts()
{
return $this->hasMany('App\Models\Contact');
}
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
}
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
}
2016-01-20 00:07:31 +01:00
public function bank_accounts()
{
return $this->hasMany('App\Models\BankAccount');
}
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
}
2015-11-19 12:50:35 +01:00
public function products()
{
return $this->hasMany('App\Models\Product');
}
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
}
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
}
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
}
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
}
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
}
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
}
2015-04-02 15:06:16 +02:00
public function currency()
{
return $this->belongsTo('App\Models\Currency');
}
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
}
2015-10-21 13:11:08 +02:00
public function default_tax_rate()
{
return $this->belongsTo('App\Models\TaxRate');
}
2016-02-03 13:41:40 +01:00
public function setIndustryIdAttribute($value)
{
$this->attributes['industry_id'] = $value ?: null;
}
public function setCountryIdAttribute($value)
{
$this->attributes['country_id'] = $value ?: null;
}
public function setSizeIdAttribute($value)
{
$this->attributes['size_id'] = $value ?: null;
}
2015-03-16 22:45:25 +01:00
public function isGatewayConfigured($gatewayId = 0)
{
$this->load('account_gateways');
if ($gatewayId) {
return $this->getGatewayConfig($gatewayId) != false;
} else {
return count($this->account_gateways) > 0;
}
}
2015-08-11 16:38:36 +02:00
public function isEnglish()
{
return !$this->language_id || $this->language_id == DEFAULT_LANGUAGE;
}
public function hasInvoicePrefix()
{
if ( ! $this->invoice_number_prefix && ! $this->quote_number_prefix) {
return false;
}
return $this->invoice_number_prefix != $this->quote_number_prefix;
}
2015-03-16 22:45:25 +01:00
public function getDisplayName()
{
if ($this->name) {
return $this->name;
}
$this->load('users');
$user = $this->users()->first();
return $user->getDisplayName();
}
2015-10-13 17:44:01 +02:00
public function getCityState()
{
$swap = $this->country && $this->country->swap_postal_code;
return Utils::cityStateZip($this->city, $this->state, $this->postal_code, $swap);
}
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;
}
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);
}
2015-03-16 22:45:25 +01:00
public function getTimezone()
{
if ($this->timezone) {
return $this->timezone->name;
} else {
return 'US/Eastern';
}
}
2015-10-15 21:37:01 +02:00
public function getDateTime($date = 'now')
2015-10-15 16:14:13 +02:00
{
2015-12-30 19:45:52 +01:00
if ( ! $date) {
return null;
} elseif ( ! $date instanceof \DateTime) {
$date = new \DateTime($date);
}
$date->setTimeZone(new \DateTimeZone($this->getTimezone()));
return $date;
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
}
public function formatMoney($amount, $client = null, $hideSymbol = 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
}
$hideSymbol = $this->show_currency_code || $hideSymbol;
return Utils::formatMoney($amount, $currencyId, $countryId, $hideSymbol);
2015-12-02 14:26:06 +01:00
}
2016-01-21 20:15:30 +01:00
public function getCurrencyId()
{
return $this->currency_id ?: DEFAULT_CURRENCY;
}
2015-10-15 16:14:13 +02:00
public function formatDate($date)
{
2015-12-30 19:45:52 +01:00
$date = $this->getDateTime($date);
2015-12-02 14:26:06 +01:00
if ( ! $date) {
2015-10-15 16:14:13 +02:00
return null;
}
return $date->format($this->getCustomDateFormat());
}
2015-11-12 21:36:28 +01:00
public function formatDateTime($date)
{
2015-12-30 19:45:52 +01:00
$date = $this->getDateTime($date);
2015-12-13 21:12:54 +01:00
if ( ! $date) {
2015-11-12 21:36:28 +01:00
return null;
}
return $date->format($this->getCustomDateTimeFormat());
}
2015-12-13 21:12:54 +01:00
public function formatTime($date)
{
2015-12-30 19:45:52 +01:00
$date = $this->getDateTime($date);
2015-12-13 21:12:54 +01:00
if ( ! $date) {
return null;
}
return $date->format($this->getCustomTimeFormat());
}
public function getCustomTimeFormat()
{
return $this->military_time ? 'H:i' : 'g:i a';
}
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
}
2015-03-16 22:45:25 +01:00
public function getGatewayByType($type = PAYMENT_TYPE_ANY)
{
foreach ($this->account_gateways as $gateway) {
2015-03-17 15:43:35 +01:00
if (!$type || $type == PAYMENT_TYPE_ANY) {
2015-03-16 22:45:25 +01:00
return $gateway;
2015-04-15 18:35:41 +02:00
} elseif ($gateway->isPaymentType($type)) {
2015-03-16 22:45:25 +01:00
return $gateway;
}
}
return false;
}
public function getGatewayConfig($gatewayId)
{
foreach ($this->account_gateways as $gateway) {
if ($gateway->gateway_id == $gatewayId) {
return $gateway;
}
}
return false;
}
2015-07-07 22:08:16 +02:00
public function hasLogo()
{
2016-01-18 15:09:06 +01:00
return file_exists($this->getLogoFullPath());
2015-07-07 22:08:16 +02:00
}
2015-03-16 22:45:25 +01:00
public function getLogoPath()
{
2015-07-24 16:13:17 +02:00
$fileName = 'logo/' . $this->account_key;
2015-08-07 08:14:29 +02:00
return file_exists($fileName.'.png') ? $fileName.'.png' : $fileName.'.jpg';
2015-03-16 22:45:25 +01:00
}
2016-01-18 15:09:06 +01:00
public function getLogoFullPath()
{
$fileName = public_path() . '/logo/' . $this->account_key;
return file_exists($fileName.'.png') ? $fileName.'.png' : $fileName.'.jpg';
}
2015-11-10 23:07:05 +01:00
public function getLogoURL()
{
return SITE_URL . '/' . $this->getLogoPath();
}
2015-11-04 16:23:22 +01:00
public function getToken($name)
{
foreach ($this->account_tokens as $token) {
if ($token->name === $name) {
return $token->token;
}
}
return null;
}
2015-03-16 22:45:25 +01:00
public function getLogoWidth()
{
2016-01-18 15:09:06 +01:00
$path = $this->getLogoFullPath();
2015-03-16 22:45:25 +01:00
if (!file_exists($path)) {
return 0;
}
list($width, $height) = getimagesize($path);
return $width;
}
public function getLogoHeight()
{
2016-01-18 15:09:06 +01:00
$path = $this->getLogoFullPath();
2015-03-16 22:45:25 +01:00
if (!file_exists($path)) {
return 0;
}
list($width, $height) = getimagesize($path);
return $height;
}
2015-10-25 08:13:06 +01:00
public function createInvoice($entityType, $clientId = null)
{
$invoice = Invoice::createNew();
2015-11-28 18:19:37 +01:00
$invoice->is_recurring = false;
$invoice->is_quote = false;
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;
2015-11-28 18:19:37 +01: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) {
$invoice->is_quote = true;
}
2015-10-25 08:35:23 +01:00
if ($this->hasClientNumberPattern($invoice) && !$clientId) {
2015-10-25 08:13:06 +01:00
// do nothing, we don't yet know the value
} else {
$invoice->invoice_number = $this->getNextInvoiceNumber($invoice);
}
}
if (!$clientId) {
$invoice->client = Client::createNew();
$invoice->client->public_id = 0;
}
return $invoice;
}
public function getNumberPrefix($isQuote)
{
if ( ! $this->isPro()) {
return '';
}
return ($isQuote ? $this->quote_number_prefix : $this->invoice_number_prefix) ?: '';
}
2015-10-22 20:48:12 +02:00
public function hasNumberPattern($isQuote)
2015-03-16 22:45:25 +01:00
{
if ( ! $this->isPro()) {
return false;
}
2015-10-22 20:48:12 +02:00
return $isQuote ? ($this->quote_number_pattern ? true : false) : ($this->invoice_number_pattern ? true : false);
}
2015-10-23 13:55:18 +02:00
public function hasClientNumberPattern($invoice)
2015-10-23 10:06:46 +02:00
{
2015-10-23 13:55:18 +02:00
$pattern = $invoice->is_quote ? $this->quote_number_pattern : $this->invoice_number_pattern;
2015-10-23 14:54:07 +02:00
2015-10-23 10:06:46 +02:00
return strstr($pattern, '$custom');
}
2015-10-23 13:55:18 +02:00
public function getNumberPattern($invoice)
2015-10-22 20:48:12 +02:00
{
2015-10-23 13:55:18 +02:00
$pattern = $invoice->is_quote ? $this->quote_number_pattern : $this->invoice_number_pattern;
2015-10-22 20:48:12 +02:00
if (!$pattern) {
return false;
}
$search = ['{$year}'];
$replace = [date('Y')];
$search[] = '{$counter}';
2015-10-23 13:55:18 +02:00
$replace[] = str_pad($this->getCounter($invoice->is_quote), 4, '0', STR_PAD_LEFT);
2015-10-22 20:48:12 +02:00
2015-10-23 13:55:18 +02:00
if (strstr($pattern, '{$userId}')) {
2015-10-23 10:06:46 +02:00
$search[] = '{$userId}';
2015-11-11 17:24:48 +01:00
$replace[] = str_pad(($invoice->user->public_id + 1), 2, '0', STR_PAD_LEFT);
2015-10-23 10:06:46 +02:00
}
2015-10-22 20:48:12 +02:00
$matches = false;
preg_match('/{\$date:(.*?)}/', $pattern, $matches);
if (count($matches) > 1) {
$format = $matches[1];
$search[] = $matches[0];
$replace[] = str_replace($format, date($format), $matches[1]);
}
$pattern = str_replace($search, $replace, $pattern);
2015-10-28 20:22:07 +01:00
if ($invoice->client_id) {
2015-10-23 13:55:18 +02:00
$pattern = $this->getClientInvoiceNumber($pattern, $invoice);
2015-10-22 20:48:12 +02:00
}
return $pattern;
}
2015-10-23 13:55:18 +02:00
private function getClientInvoiceNumber($pattern, $invoice)
2015-10-22 20:48:12 +02:00
{
2015-10-23 13:55:18 +02:00
if (!$invoice->client) {
2015-10-22 20:48:12 +02:00
return $pattern;
}
$search = [
'{$custom1}',
'{$custom2}',
];
$replace = [
2015-10-23 13:55:18 +02:00
$invoice->client->custom_value1,
$invoice->client->custom_value2,
2015-10-22 20:48:12 +02:00
];
return str_replace($search, $replace, $pattern);
}
public function getCounter($isQuote)
{
return $isQuote && !$this->share_counter ? $this->quote_number_counter : $this->invoice_number_counter;
}
2015-12-23 12:49:49 +01:00
public function previewNextInvoiceNumber($entityType = ENTITY_INVOICE)
{
$invoice = $this->createInvoice($entityType);
return $this->getNextInvoiceNumber($invoice);
}
2015-10-25 08:49:10 +01:00
public function getNextInvoiceNumber($invoice)
2015-10-22 20:48:12 +02:00
{
2015-10-23 13:55:18 +02:00
if ($this->hasNumberPattern($invoice->is_quote)) {
return $this->getNumberPattern($invoice);
2015-10-22 20:48:12 +02:00
}
2015-10-23 13:55:18 +02:00
$counter = $this->getCounter($invoice->is_quote);
$prefix = $this->getNumberPrefix($invoice->is_quote);
$counterOffset = 0;
2015-06-04 22:53:58 +02:00
// confirm the invoice number isn't already taken
do {
2015-12-10 14:35:40 +01:00
$number = $prefix . str_pad($counter, 4, '0', STR_PAD_LEFT);
2015-06-10 10:34:20 +02:00
$check = Invoice::scope(false, $this->id)->whereInvoiceNumber($number)->withTrashed()->first();
2015-06-04 22:53:58 +02:00
$counter++;
$counterOffset++;
2015-06-04 22:53:58 +02:00
} while ($check);
// update the invoice counter to be caught up
if ($counterOffset > 1) {
2015-10-23 13:55:18 +02:00
if ($invoice->is_quote && !$this->share_counter) {
$this->quote_number_counter += $counterOffset - 1;
} else {
$this->invoice_number_counter += $counterOffset - 1;
}
$this->save();
}
2015-06-04 22:53:58 +02:00
return $number;
2015-03-16 22:45:25 +01:00
}
2015-10-22 20:48:12 +02:00
public function incrementCounter($invoice)
2015-03-16 22:45:25 +01:00
{
2015-10-22 20:48:12 +02:00
if ($invoice->is_quote && !$this->share_counter) {
$this->quote_number_counter += 1;
2015-03-16 22:45:25 +01:00
} else {
2015-11-29 11:41:32 +01:00
$default = $this->invoice_number_counter;
$actual = Utils::parseInt($invoice->invoice_number);
if ( ! $this->isPro() && $default != $actual) {
$this->invoice_number_counter = $actual + 1;
} else {
$this->invoice_number_counter += 1;
}
2015-03-16 22:45:25 +01:00
}
2015-10-22 20:48:12 +02:00
2015-03-16 22:45:25 +01:00
$this->save();
}
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
$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_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);
2015-03-16 22:45:25 +01:00
}
public function getInvoiceLabels()
{
$data = [];
$custom = (array) json_decode($this->invoice_labels);
2015-03-16 22:45:25 +01:00
$fields = [
'invoice',
'invoice_date',
'due_date',
'invoice_number',
'po_number',
'discount',
'taxes',
'tax',
'item',
'description',
'unit_cost',
'quantity',
'line_total',
'subtotal',
'paid_to_date',
'balance_due',
2015-04-16 21:57:12 +02:00
'amount_due',
2015-03-16 22:45:25 +01:00
'terms',
'your_invoice',
'quote',
'your_quote',
'quote_date',
'quote_number',
'total',
'invoice_issued_to',
'quote_issued_to',
2015-12-13 21:12:54 +01:00
//'date',
2015-06-10 10:34:20 +02:00
'rate',
'hours',
2015-07-07 22:08:16 +02:00
'balance',
2015-07-21 20:51:56 +02:00
'from',
'to',
2015-07-24 16:13:17 +02:00
'invoice_to',
'details',
'invoice_no',
2015-09-07 11:07:55 +02:00
'valid_until',
2015-03-16 22:45:25 +01:00
];
foreach ($fields as $field) {
if (isset($custom[$field]) && $custom[$field]) {
$data[$field] = $custom[$field];
} else {
2015-08-11 16:38:36 +02:00
$data[$field] = $this->isEnglish() ? uctrans("texts.$field") : trans("texts.$field");
}
2015-03-16 22:45:25 +01:00
}
2015-06-10 10:34:20 +02:00
foreach (['item', 'quantity', 'unit_cost'] as $field) {
$data["{$field}_orig"] = $data[$field];
}
2015-03-16 22:45:25 +01:00
return $data;
}
public function isNinjaAccount()
{
return $this->account_key === NINJA_ACCOUNT_KEY;
}
public function startTrial()
{
if ( ! Utils::isNinja()) {
return;
}
$this->pro_plan_trial = date_create()->format('Y-m-d');
$this->save();
}
2015-03-16 22:45:25 +01:00
public function isPro()
{
if (!Utils::isNinjaProd()) {
return true;
}
if ($this->isNinjaAccount()) {
2015-03-16 22:45:25 +01:00
return true;
}
$datePaid = $this->pro_plan_paid;
$trialStart = $this->pro_plan_trial;
2015-03-16 22:45:25 +01:00
2015-11-01 19:21:11 +01:00
if ($datePaid == NINJA_DATE) {
2015-03-16 22:45:25 +01:00
return true;
}
return Utils::withinPastTwoWeeks($trialStart) || Utils::withinPastYear($datePaid);
}
public function isTrial()
{
if ($this->pro_plan_paid && $this->pro_plan_paid != '0000-00-00') {
return false;
}
return Utils::withinPastTwoWeeks($this->pro_plan_trial);
}
public function isEligibleForTrial()
{
return ! $this->pro_plan_trial || $this->pro_plan_trial == '0000-00-00';
}
public function getCountTrialDaysLeft()
{
$interval = Utils::getInterval($this->pro_plan_trial);
return $interval ? 14 - $interval->d : 0;
}
public function getRenewalDate()
{
if ($this->pro_plan_paid && $this->pro_plan_paid != '0000-00-00') {
$date = DateTime::createFromFormat('Y-m-d', $this->pro_plan_paid);
$date->modify('+1 year');
$date = max($date, date_create());
} elseif ($this->isTrial()) {
$date = date_create();
$date->modify('+'.$this->getCountTrialDaysLeft().' day');
} else {
$date = date_create();
}
return $date->format('Y-m-d');
2015-03-16 22:45:25 +01:00
}
public function isWhiteLabel()
{
if ($this->isNinjaAccount()) {
return false;
}
2015-08-14 14:04:33 +02:00
if (Utils::isNinjaProd()) {
2015-08-03 09:15:58 +02:00
return self::isPro() && $this->pro_plan_paid != NINJA_DATE;
} else {
if ($this->pro_plan_paid == NINJA_DATE) {
return true;
}
return Utils::withinPastYear($this->pro_plan_paid);
2015-03-16 22:45:25 +01:00
}
}
2015-11-29 21:13:50 +01:00
public function getLogoSize()
{
if (!$this->hasLogo()) {
return 0;
}
2016-01-18 15:09:06 +01:00
$filename = $this->getLogoFullPath();
2015-11-29 21:13:50 +01:00
return round(File::size($filename) / 1000);
}
public function isLogoTooLarge()
{
return $this->getLogoSize() > MAX_LOGO_FILE_SIZE;
}
2015-03-16 22:45:25 +01:00
public function getSubscription($eventId)
{
return Subscription::where('account_id', '=', $this->id)->where('event_id', '=', $eventId)->first();
}
public function hideFieldsForViz()
{
foreach ($this->clients as $client) {
$client->setVisible([
'public_id',
'name',
'balance',
'paid_to_date',
'invoices',
'contacts',
]);
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',
'is_quote',
2015-03-16 22:45:25 +01:00
]);
foreach ($invoice->invoice_items as $invoiceItem) {
$invoiceItem->setVisible([
'product_key',
'cost',
'qty',
]);
}
}
foreach ($client->contacts as $contact) {
$contact->setVisible([
'public_id',
'first_name',
'last_name',
'email', ]);
}
}
return $this;
}
2015-09-20 23:05:02 +02:00
public function getDefaultEmailSubject($entityType)
2015-09-17 21:01:06 +02:00
{
if (strpos($entityType, 'reminder') !== false) {
$entityType = 'reminder';
}
return trans("texts.{$entityType}_subject", ['invoice' => '$invoice', 'account' => '$account']);
}
2015-09-20 23:05:02 +02:00
public function getEmailSubject($entityType)
2015-03-16 22:45:25 +01:00
{
2015-10-13 09:11:44 +02:00
if ($this->isPro()) {
$field = "email_subject_{$entityType}";
$value = $this->$field;
2015-03-16 22:45:25 +01:00
2015-10-13 09:11:44 +02:00
if ($value) {
return $value;
}
2015-03-16 22:45:25 +01:00
}
2015-09-20 23:05:02 +02:00
return $this->getDefaultEmailSubject($entityType);
}
public function getDefaultEmailTemplate($entityType, $message = false)
{
2015-11-04 22:52:12 +01:00
if (strpos($entityType, 'reminder') !== false) {
2015-09-17 21:01:06 +02:00
$entityType = ENTITY_INVOICE;
}
$template = "<div>\$client,</div><br>";
if ($this->isPro() && $this->email_design_id != EMAIL_DESIGN_PLAIN) {
$template .= "<div>" . trans("texts.{$entityType}_message_button", ['amount' => '$amount']) . "</div><br>" .
"<div style=\"text-align: center;\">\$viewButton</div><br>";
} else {
$template .= "<div>" . trans("texts.{$entityType}_message", ['amount' => '$amount']) . "</div><br>" .
"<div>\$viewLink</div><br>";
}
2015-03-16 22:45:25 +01:00
if ($message) {
$template .= "$message<p/>\r\n\r\n";
}
return $template . "\$footer";
}
2015-09-20 23:05:02 +02:00
public function getEmailTemplate($entityType, $message = false)
{
2015-12-15 22:06:42 +01:00
$template = false;
2015-10-13 09:11:44 +02:00
if ($this->isPro()) {
$field = "email_template_{$entityType}";
$template = $this->$field;
2015-09-20 23:05:02 +02:00
}
2015-10-13 09:11:44 +02:00
2015-12-02 14:26:06 +01:00
if (!$template) {
$template = $this->getDefaultEmailTemplate($entityType, $message);
}
// <br/> is causing page breaks with the email designs
return str_replace('/>', ' />', $template);
2015-09-20 23:05:02 +02:00
}
2015-03-16 22:45:25 +01:00
public function getEmailFooter()
{
if ($this->email_footer) {
2015-04-22 21:21:04 +02:00
// Add line breaks if HTML isn't already being used
2015-09-07 11:07:55 +02:00
return strip_tags($this->email_footer) == $this->email_footer ? nl2br($this->email_footer) : $this->email_footer;
2015-03-16 22:45:25 +01:00
} else {
2015-12-02 14:26:06 +01:00
return "<p>" . trans('texts.email_signature') . "\n<br>\$account</ p>";
2015-03-16 22:45:25 +01:00
}
}
public function getReminderDate($reminder)
{
if ( ! $this->{"enable_reminder{$reminder}"}) {
return false;
}
$numDays = $this->{"num_days_reminder{$reminder}"};
$plusMinus = $this->{"direction_reminder{$reminder}"} == REMINDER_DIRECTION_AFTER ? '-' : '+';
return date('Y-m-d', strtotime("$plusMinus $numDays days"));
}
public function getInvoiceReminder($invoice)
{
for ($i=1; $i<=3; $i++) {
if ($date = $this->getReminderDate($i)) {
$field = $this->{"field_reminder{$i}"} == REMINDER_FIELD_DUE_DATE ? 'due_date' : 'invoice_date';
2016-01-13 22:38:15 +01:00
if ($invoice->$field == $date) {
return "reminder{$i}";
}
}
}
return false;
}
2015-03-16 22:45:25 +01:00
public function showTokenCheckbox()
{
if (!$this->isGatewayConfigured(GATEWAY_STRIPE)) {
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;
}
public function selectTokenCheckbox()
{
return $this->token_billing_type_id == TOKEN_BILLING_OPT_OUT;
}
2015-09-17 21:01:06 +02:00
public function getSiteUrl()
{
$url = SITE_URL;
$iframe_url = $this->iframe_url;
if ($iframe_url) {
return "{$iframe_url}/?";
} else if ($this->subdomain) {
$url = Utils::replaceSubdomain($url, $this->subdomain);
}
return $url;
}
2015-10-13 09:11:44 +02:00
public function checkSubdomain($host)
{
if (!$this->subdomain) {
return true;
}
$server = explode('.', $host);
$subdomain = $server[0];
if (!in_array($subdomain, ['app', 'www']) && $subdomain != $this->subdomain) {
return false;
}
return true;
}
public function showCustomField($field, $entity)
{
if ($this->isPro()) {
return $this->$field ? true : false;
}
if (!$entity) {
return false;
}
// convert (for example) 'custom_invoice_label1' to 'invoice.custom_value1'
$field = str_replace(['invoice_', 'label'], ['', 'value'], $field);
return Utils::isEmpty($entity->$field) ? false : true;
}
public function attatchPDF()
{
return $this->isPro() && $this->pdf_email_attachment;
}
public function getEmailDesignId()
{
return $this->isPro() ? $this->email_design_id : EMAIL_DESIGN_PLAIN;
}
public function clientViewCSS(){
2016-01-07 08:08:30 +01:00
$css = null;
if ($this->isPro()) {
$bodyFont = $this->getBodyFontCss();
$headerFont = $this->getHeaderFontCss();
$css = 'body{'.$bodyFont.'}';
if ($headerFont != $bodyFont) {
$css .= 'h1,h2,h3,h4,h5,h6,.h1,.h2,.h3,.h4,.h5,.h6{'.$headerFont.'}';
}
if ((Utils::isNinja() && $this->isPro()) || $this->isWhiteLabel()) {
// For self-hosted users, a white-label license is required for custom CSS
$css .= $this->client_view_css;
}
}
2016-01-07 08:08:30 +01:00
return $css;
}
public function hasLargeFont()
{
return stripos($this->getBodyFontName(), 'chinese') || stripos($this->getHeaderFontName(), 'chinese');
}
2016-01-07 08:08:30 +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 = array($bodyFontSettings['google_font']);
if($headerFont != $bodyFont){
$headerFontSettings = Utils::getFromCache($headerFont, 'fonts');
$google_fonts[] = $headerFontSettings['google_font'];
}
return ($protocol?$protocol.':':'').'//fonts.googleapis.com/css?family='.implode('|',$google_fonts);
}
2016-01-11 13:50:17 +01:00
public function getHeaderFontId() {
return ($this->isPro() && $this->header_font_id) ? $this->header_font_id : DEFAULT_HEADER_FONT;
2016-01-11 13:50:17 +01:00
}
public function getBodyFontId() {
return ($this->isPro() && $this->body_font_id) ? $this->body_font_id : DEFAULT_BODY_FONT;
2016-01-11 13:50:17 +01:00
}
2016-01-07 08:08:30 +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
}
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
}
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'].';';
if($include_weight){
$css .= 'font-weight:'.$font_data['css_weight'].';';
}
return $css;
}
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'].';';
if($include_weight){
$css .= 'font-weight:'.$font_data['css_weight'].';';
}
return $css;
}
public function getFonts(){
2016-01-11 13:50:17 +01:00
return array_unique(array($this->getHeaderFontId(), $this->getBodyFontId()));
2016-01-07 08:08:30 +01:00
}
public function getFontsData(){
$data = array();
foreach($this->getFonts() as $font){
$data[] = Utils::getFromCache($font, 'fonts');
}
return $data;
}
public function getFontFolders(){
return array_map(function($item){return $item['folder'];}, $this->getFontsData());
}
2015-08-14 14:04:33 +02:00
}
Account::updated(function ($account) {
Event::fire(new UserSettingsChanged());
});