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;
|
2016-04-17 01:14:43 +02:00
|
|
|
use App\Models\Document;
|
2015-08-14 14:04:33 +02:00
|
|
|
use App\Events\UserSettingsChanged;
|
2016-03-22 21:19:55 +01:00
|
|
|
use Illuminate\Support\Facades\Storage;
|
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;
|
2016-05-29 11:26:02 +02:00
|
|
|
|
2016-04-17 00:34:39 +02:00
|
|
|
public static $plan_prices = array(
|
|
|
|
PLAN_PRO => array(
|
|
|
|
PLAN_TERM_MONTHLY => PLAN_PRICE_PRO_MONTHLY,
|
|
|
|
PLAN_TERM_YEARLY => PLAN_PRICE_PRO_YEARLY,
|
|
|
|
),
|
|
|
|
PLAN_ENTERPRISE => array(
|
|
|
|
PLAN_TERM_MONTHLY => PLAN_PRICE_ENTERPRISE_MONTHLY,
|
|
|
|
PLAN_TERM_YEARLY => PLAN_PRICE_ENTERPRISE_YEARLY,
|
|
|
|
),
|
|
|
|
);
|
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',
|
2016-02-03 15:03:56 +01:00
|
|
|
'timezone_id',
|
|
|
|
'date_format_id',
|
|
|
|
'datetime_format_id',
|
|
|
|
'currency_id',
|
|
|
|
'language_id',
|
|
|
|
'military_time',
|
2016-05-29 11:26:02 +02:00
|
|
|
'invoice_taxes',
|
|
|
|
'invoice_item_taxes',
|
|
|
|
'show_item_taxes',
|
|
|
|
'default_tax_rate_id',
|
|
|
|
'enable_second_tax_rate',
|
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,
|
2016-04-16 16:58:53 +02:00
|
|
|
ACCOUNT_MANAGEMENT,
|
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,
|
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-08-12 21:16:02 +02:00
|
|
|
/*
|
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-08-12 21:16:02 +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-17 11:16:13 +01:00
|
|
|
public function expenses()
|
|
|
|
{
|
|
|
|
return $this->hasMany('App\Models\Expense','account_id','id')->withTrashed();
|
|
|
|
}
|
2016-02-03 13:41:40 +01:00
|
|
|
|
2016-02-17 11:50:52 +01:00
|
|
|
public function payments()
|
|
|
|
{
|
|
|
|
return $this->hasMany('App\Models\Payment','account_id','id')->withTrashed();
|
|
|
|
}
|
|
|
|
|
2016-04-17 00:34:39 +02:00
|
|
|
public function company()
|
|
|
|
{
|
|
|
|
return $this->belongsTo('App\Models\Company');
|
|
|
|
}
|
|
|
|
|
2016-06-13 12:18:30 +02:00
|
|
|
public function expenseCategories()
|
|
|
|
{
|
2016-06-13 12:26:47 +02:00
|
|
|
return $this->hasMany('App\Models\ExpenseCategory','account_id','id')->withTrashed();
|
2016-06-13 12:18:30 +02:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2016-05-05 09:15:51 +02: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 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;
|
|
|
|
}
|
|
|
|
|
2015-11-06 09:21:45 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-05-05 09:15:51 +02:00
|
|
|
//$this->load('users');
|
2015-03-16 22:45:25 +01:00
|
|
|
$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);
|
|
|
|
}
|
|
|
|
|
2015-09-10 19:50:09 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-12-07 14:34:55 +01:00
|
|
|
public function formatMoney($amount, $client = null, $hideSymbol = false)
|
2015-12-02 14:26:06 +01:00
|
|
|
{
|
2015-12-07 14:34:55 +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
|
|
|
|
2015-12-07 14:34:55 +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
|
|
|
}
|
|
|
|
|
2016-02-04 15:19:49 +01:00
|
|
|
$hideSymbol = $this->show_currency_code || $hideSymbol;
|
|
|
|
|
2015-12-07 14:34:55 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
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];
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
public function getGatewayByType($type = false)
|
|
|
|
{
|
|
|
|
if ( ! $this->relationLoaded('account_gateways')) {
|
|
|
|
$this->load('account_gateways');
|
2016-05-07 04:33:03 +02:00
|
|
|
}
|
|
|
|
|
2016-06-20 16:14:43 +02:00
|
|
|
foreach ($this->account_gateways as $accountGateway) {
|
|
|
|
if ( ! $type) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-06-20 16:14:43 +02:00
|
|
|
public function paymentDriver($invitation = false, $gatewayType = false)
|
|
|
|
{
|
|
|
|
$accountGateway = $this->getGatewayByType($gatewayType);
|
|
|
|
|
|
|
|
return $accountGateway->paymentDriver($invitation, $gatewayType);
|
|
|
|
}
|
|
|
|
|
2016-06-09 09:56:22 +02:00
|
|
|
public function gatewayIds()
|
|
|
|
{
|
|
|
|
return $this->account_gateways()->pluck('gateway_id')->toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function hasGatewayId($gatewayId)
|
|
|
|
{
|
|
|
|
return in_array($gatewayId, $this->gatewayIds());
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-07-07 22:08:16 +02:00
|
|
|
public function hasLogo()
|
|
|
|
{
|
2016-03-22 21:19:55 +01:00
|
|
|
if($this->logo == ''){
|
|
|
|
$this->calculateLogoDetails();
|
|
|
|
}
|
2016-05-29 11:26:02 +02:00
|
|
|
|
2016-03-22 21:19:55 +01:00
|
|
|
return !empty($this->logo);
|
|
|
|
}
|
2016-05-29 11:26:02 +02:00
|
|
|
|
2016-03-22 21:19:55 +01:00
|
|
|
public function getLogoDisk(){
|
2016-03-23 20:41:05 +01:00
|
|
|
return Storage::disk(env('LOGO_FILESYSTEM', 'logos'));
|
2016-03-22 21:19:55 +01:00
|
|
|
}
|
2016-05-29 11:26:02 +02:00
|
|
|
|
2016-03-22 21:19:55 +01:00
|
|
|
protected function calculateLogoDetails(){
|
|
|
|
$disk = $this->getLogoDisk();
|
2016-05-29 11:26:02 +02:00
|
|
|
|
2016-03-22 21:19:55 +01:00
|
|
|
if($disk->exists($this->account_key.'.png')){
|
|
|
|
$this->logo = $this->account_key.'.png';
|
|
|
|
} else if($disk->exists($this->account_key.'.jpg')) {
|
|
|
|
$this->logo = $this->account_key.'.jpg';
|
|
|
|
}
|
2016-05-29 11:26:02 +02:00
|
|
|
|
2016-03-22 21:19:55 +01:00
|
|
|
if(!empty($this->logo)){
|
|
|
|
$image = imagecreatefromstring($disk->get($this->logo));
|
|
|
|
$this->logo_width = imagesx($image);
|
2016-05-29 11:26:02 +02:00
|
|
|
$this->logo_height = imagesy($image);
|
2016-03-22 21:19:55 +01:00
|
|
|
$this->logo_size = $disk->size($this->logo);
|
|
|
|
} else {
|
|
|
|
$this->logo = null;
|
|
|
|
}
|
|
|
|
$this->save();
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
2016-03-22 21:19:55 +01:00
|
|
|
public function getLogoRaw(){
|
|
|
|
if(!$this->hasLogo()){
|
|
|
|
return null;
|
|
|
|
}
|
2016-05-29 11:26:02 +02:00
|
|
|
|
2016-03-22 21:19:55 +01:00
|
|
|
$disk = $this->getLogoDisk();
|
|
|
|
return $disk->get($this->logo);
|
2016-01-18 15:09:06 +01:00
|
|
|
}
|
2016-05-29 11:26:02 +02:00
|
|
|
|
2016-04-17 01:14:43 +02:00
|
|
|
public function getLogoURL($cachebuster = false)
|
2015-11-10 23:07:05 +01:00
|
|
|
{
|
2016-03-22 21:19:55 +01:00
|
|
|
if(!$this->hasLogo()){
|
|
|
|
return null;
|
|
|
|
}
|
2016-05-29 11:26:02 +02:00
|
|
|
|
2016-03-22 21:19:55 +01:00
|
|
|
$disk = $this->getLogoDisk();
|
|
|
|
$adapter = $disk->getAdapter();
|
2016-05-29 11:26:02 +02:00
|
|
|
|
2016-03-22 21:19:55 +01:00
|
|
|
if($adapter instanceof \League\Flysystem\Adapter\Local) {
|
|
|
|
// Stored locally
|
|
|
|
$logo_url = str_replace(public_path(), url('/'), $adapter->applyPathPrefix($this->logo), $count);
|
2016-05-29 11:26:02 +02:00
|
|
|
|
2016-04-17 01:14:43 +02:00
|
|
|
if ($cachebuster) {
|
|
|
|
$logo_url .= '?no_cache='.time();
|
|
|
|
}
|
2016-05-29 11:26:02 +02:00
|
|
|
|
2016-03-22 21:19:55 +01:00
|
|
|
if($count == 1){
|
|
|
|
return str_replace(DIRECTORY_SEPARATOR, '/', $logo_url);
|
|
|
|
}
|
|
|
|
}
|
2016-05-29 11:26:02 +02:00
|
|
|
|
2016-04-17 01:14:43 +02:00
|
|
|
return Document::getDirectFileUrl($this->logo, $this->getLogoDisk());
|
2015-11-10 23:07:05 +01:00
|
|
|
}
|
|
|
|
|
2016-05-08 14:37:32 +02:00
|
|
|
public function getPrimaryUser()
|
|
|
|
{
|
|
|
|
return $this->users()
|
|
|
|
->orderBy('id')
|
|
|
|
->first();
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-03-16 22:45:25 +01:00
|
|
|
public function getLogoWidth()
|
|
|
|
{
|
2016-03-22 21:19:55 +01:00
|
|
|
if(!$this->hasLogo()){
|
|
|
|
return null;
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
2016-03-22 21:19:55 +01:00
|
|
|
return $this->logo_width;
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getLogoHeight()
|
|
|
|
{
|
2016-03-22 21:19:55 +01:00
|
|
|
if(!$this->hasLogo()){
|
|
|
|
return null;
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
2016-03-22 21:19:55 +01:00
|
|
|
return $this->logo_height;
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
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;
|
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;
|
2015-10-25 08:13:06 +01:00
|
|
|
}
|
|
|
|
|
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
|
2016-06-02 21:03:59 +02:00
|
|
|
} elseif ( ! $invoice->invoice_number) {
|
2015-10-25 08:13:06 +01:00
|
|
|
$invoice->invoice_number = $this->getNextInvoiceNumber($invoice);
|
|
|
|
}
|
|
|
|
}
|
2016-05-29 11:26:02 +02:00
|
|
|
|
2015-10-25 08:13:06 +01:00
|
|
|
if (!$clientId) {
|
|
|
|
$invoice->client = Client::createNew();
|
|
|
|
$invoice->client->public_id = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $invoice;
|
|
|
|
}
|
|
|
|
|
2016-05-26 16:56:54 +02:00
|
|
|
public function getNumberPrefix($invoice_type_id)
|
2016-02-11 16:12:27 +01:00
|
|
|
{
|
2016-04-19 04:35:18 +02:00
|
|
|
if ( ! $this->hasFeature(FEATURE_INVOICE_SETTINGS)) {
|
2016-02-11 16:12:27 +01:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2016-05-26 16:56:54 +02:00
|
|
|
return ($invoice_type_id == INVOICE_TYPE_QUOTE ? $this->quote_number_prefix : $this->invoice_number_prefix) ?: '';
|
2016-02-11 16:12:27 +01:00
|
|
|
}
|
|
|
|
|
2016-05-26 16:56:54 +02:00
|
|
|
public function hasNumberPattern($invoice_type_id)
|
2015-03-16 22:45:25 +01:00
|
|
|
{
|
2016-04-19 04:35:18 +02:00
|
|
|
if ( ! $this->hasFeature(FEATURE_INVOICE_SETTINGS)) {
|
2016-02-11 16:12:27 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-05-26 16:56:54 +02:00
|
|
|
return $invoice_type_id == INVOICE_TYPE_QUOTE ? ($this->quote_number_pattern ? true : false) : ($this->invoice_number_pattern ? true : false);
|
2015-10-22 20:48:12 +02:00
|
|
|
}
|
|
|
|
|
2015-10-23 13:55:18 +02:00
|
|
|
public function hasClientNumberPattern($invoice)
|
2015-10-23 10:06:46 +02:00
|
|
|
{
|
2016-05-26 16:56:54 +02:00
|
|
|
$pattern = $invoice->invoice_type_id == INVOICE_TYPE_QUOTE ? $this->quote_number_pattern : $this->invoice_number_pattern;
|
2016-05-29 11:26:02 +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
|
|
|
{
|
2016-05-26 16:56:54 +02:00
|
|
|
$pattern = $invoice->invoice_type_id == INVOICE_TYPE_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}';
|
2016-05-26 16:56:54 +02:00
|
|
|
$replace[] = str_pad($this->getCounter($invoice->invoice_type_id), $this->invoice_number_padding, '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);
|
|
|
|
}
|
|
|
|
|
2016-05-26 16:56:54 +02:00
|
|
|
public function getCounter($invoice_type_id)
|
2015-10-22 20:48:12 +02:00
|
|
|
{
|
2016-05-26 16:56:54 +02:00
|
|
|
return $invoice_type_id == INVOICE_TYPE_QUOTE && !$this->share_counter ? $this->quote_number_counter : $this->invoice_number_counter;
|
2015-10-22 20:48:12 +02:00
|
|
|
}
|
|
|
|
|
2015-12-23 12:49:49 +01:00
|
|
|
public function previewNextInvoiceNumber($entityType = ENTITY_INVOICE)
|
|
|
|
{
|
|
|
|
$invoice = $this->createInvoice($entityType);
|
|
|
|
return $this->getNextInvoiceNumber($invoice);
|
|
|
|
}
|
|
|
|
|
2016-06-02 21:03:59 +02:00
|
|
|
public function getNextInvoiceNumber($invoice, $validateUnique = true)
|
2015-10-22 20:48:12 +02:00
|
|
|
{
|
2016-05-26 16:56:54 +02:00
|
|
|
if ($this->hasNumberPattern($invoice->invoice_type_id)) {
|
2016-06-01 09:56:44 +02:00
|
|
|
$number = $this->getNumberPattern($invoice);
|
|
|
|
} else {
|
|
|
|
$counter = $this->getCounter($invoice->invoice_type_id);
|
|
|
|
$prefix = $this->getNumberPrefix($invoice->invoice_type_id);
|
|
|
|
$counterOffset = 0;
|
2016-06-02 21:03:59 +02:00
|
|
|
$check = false;
|
2016-06-01 09:56:44 +02:00
|
|
|
|
|
|
|
// confirm the invoice number isn't already taken
|
|
|
|
do {
|
|
|
|
$number = $prefix . str_pad($counter, $this->invoice_number_padding, '0', STR_PAD_LEFT);
|
2016-06-02 21:03:59 +02:00
|
|
|
if ($validateUnique) {
|
|
|
|
$check = Invoice::scope(false, $this->id)->whereInvoiceNumber($number)->withTrashed()->first();
|
|
|
|
$counter++;
|
|
|
|
$counterOffset++;
|
|
|
|
}
|
2016-06-01 09:56:44 +02:00
|
|
|
} while ($check);
|
|
|
|
|
|
|
|
// update the invoice counter to be caught up
|
|
|
|
if ($counterOffset > 1) {
|
|
|
|
if ($invoice->isType(INVOICE_TYPE_QUOTE) && !$this->share_counter) {
|
|
|
|
$this->quote_number_counter += $counterOffset - 1;
|
|
|
|
} else {
|
|
|
|
$this->invoice_number_counter += $counterOffset - 1;
|
|
|
|
}
|
2015-06-04 22:53:58 +02:00
|
|
|
|
2016-06-01 09:56:44 +02:00
|
|
|
$this->save();
|
2015-07-30 16:44:47 +02:00
|
|
|
}
|
2016-06-01 09:56:44 +02:00
|
|
|
}
|
2015-07-30 16:44:47 +02:00
|
|
|
|
2016-06-01 09:56:44 +02:00
|
|
|
if ($invoice->recurring_invoice_id) {
|
|
|
|
$number = $this->recurring_invoice_number_prefix . $number;
|
2015-07-30 16:44:47 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2016-06-01 09:56:44 +02:00
|
|
|
// if they didn't use the counter don't increment it
|
2016-06-02 21:03:59 +02:00
|
|
|
if ($invoice->invoice_number != $this->getNextInvoiceNumber($invoice, false)) {
|
2016-06-01 09:56:44 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-26 16:56:54 +02:00
|
|
|
if ($invoice->isType(INVOICE_TYPE_QUOTE) && !$this->share_counter) {
|
2015-07-30 16:44:47 +02:00
|
|
|
$this->quote_number_counter += 1;
|
2015-03-16 22:45:25 +01:00
|
|
|
} else {
|
2016-06-01 09:56:44 +02:00
|
|
|
$this->invoice_number_counter += 1;
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
2016-05-29 11:26:02 +02:00
|
|
|
|
2015-03-16 22:45:25 +01:00
|
|
|
$this->save();
|
|
|
|
}
|
|
|
|
|
2016-03-13 12:56:56 +01:00
|
|
|
public function loadAllData($updatedAt = null)
|
|
|
|
{
|
|
|
|
$map = [
|
|
|
|
'users' => [],
|
|
|
|
'clients' => ['contacts'],
|
|
|
|
'invoices' => ['invoice_items', 'user', 'client', 'payments'],
|
|
|
|
'products' => [],
|
|
|
|
'tax_rates' => [],
|
|
|
|
'expenses' => ['client', 'invoice', 'vendor'],
|
|
|
|
'payments' => ['invoice'],
|
2016-06-13 12:18:30 +02:00
|
|
|
'expenseCategories' => [],
|
2016-03-13 12:56:56 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
foreach ($map as $key => $values) {
|
|
|
|
$this->load([$key => function($query) use ($values, $updatedAt) {
|
|
|
|
$query->withTrashed()->with($values);
|
|
|
|
if ($updatedAt) {
|
|
|
|
$query->where('updated_at', '>=', $updatedAt);
|
|
|
|
}
|
|
|
|
}]);
|
2016-05-29 11:26:02 +02:00
|
|
|
}
|
2016-03-13 12:56:56 +01:00
|
|
|
}
|
|
|
|
|
2015-09-10 19:50:09 +02:00
|
|
|
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);
|
2015-09-10 19:50:09 +02:00
|
|
|
|
|
|
|
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 = [];
|
2015-05-27 22:20:35 +02:00
|
|
|
$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',
|
2016-03-18 13:34:46 +01:00
|
|
|
'partial_due',
|
2015-03-16 22:45:25 +01:00
|
|
|
'terms',
|
|
|
|
'your_invoice',
|
|
|
|
'quote',
|
|
|
|
'your_quote',
|
|
|
|
'quote_date',
|
|
|
|
'quote_number',
|
|
|
|
'total',
|
|
|
|
'invoice_issued_to',
|
2016-02-03 21:19:15 +01:00
|
|
|
'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) {
|
2015-05-27 22:20:35 +02:00
|
|
|
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-05-27 22:20:35 +02:00
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2015-10-06 19:55:55 +02:00
|
|
|
public function isNinjaAccount()
|
|
|
|
{
|
|
|
|
return $this->account_key === NINJA_ACCOUNT_KEY;
|
|
|
|
}
|
|
|
|
|
2016-04-17 00:34:39 +02:00
|
|
|
public function startTrial($plan)
|
2016-02-11 16:12:27 +01:00
|
|
|
{
|
|
|
|
if ( ! Utils::isNinja()) {
|
|
|
|
return;
|
|
|
|
}
|
2016-05-29 11:26:02 +02:00
|
|
|
|
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();
|
2016-02-11 16:12:27 +01:00
|
|
|
}
|
|
|
|
|
2016-04-19 04:35:18 +02:00
|
|
|
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
|
|
|
|
2016-04-19 04:35:18 +02:00
|
|
|
$planDetails = $this->getPlanDetails();
|
|
|
|
$selfHost = !Utils::isNinjaProd();
|
2016-05-29 11:26:02 +02:00
|
|
|
|
2016-04-19 16:26:29 +02:00
|
|
|
if (!$selfHost && function_exists('ninja_account_features')) {
|
|
|
|
$result = ninja_account_features($this, $feature);
|
2016-05-29 11:26:02 +02:00
|
|
|
|
2016-04-19 16:26:29 +02:00
|
|
|
if ($result != null) {
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
}
|
2016-05-29 11:26:02 +02:00
|
|
|
|
2016-04-19 04:35:18 +02:00
|
|
|
switch ($feature) {
|
|
|
|
// Pro
|
|
|
|
case FEATURE_CUSTOMIZE_INVOICE_DESIGN:
|
|
|
|
case FEATURE_REMOVE_CREATED_BY:
|
|
|
|
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_QUOTES:
|
|
|
|
case FEATURE_REPORTS:
|
|
|
|
case FEATURE_API:
|
|
|
|
case FEATURE_CLIENT_PORTAL_PASSWORD:
|
|
|
|
case FEATURE_CUSTOM_URL:
|
|
|
|
return $selfHost || !empty($planDetails);
|
2016-05-29 11:26:02 +02:00
|
|
|
|
2016-04-19 04:35:18 +02:00
|
|
|
// Pro; No trial allowed, unless they're trialing enterprise with an active pro plan
|
|
|
|
case FEATURE_MORE_CLIENTS:
|
|
|
|
return $selfHost || !empty($planDetails) && (!$planDetails['trial'] || !empty($this->getPlanDetails(false, false)));
|
|
|
|
|
|
|
|
// White Label
|
|
|
|
case FEATURE_WHITE_LABEL:
|
2016-04-19 22:31:50 +02:00
|
|
|
if ($this->isNinjaAccount() || (!$selfHost && $planDetails && !$planDetails['expires'])) {
|
2016-04-19 04:35:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Fallthrough
|
|
|
|
case FEATURE_CLIENT_PORTAL_CSS:
|
|
|
|
return !empty($planDetails);// A plan is required even for self-hosted users
|
2016-05-29 11:26:02 +02:00
|
|
|
|
2016-04-19 04:35:18 +02:00
|
|
|
// Enterprise; No Trial allowed; grandfathered for old pro users
|
|
|
|
case FEATURE_USERS:// Grandfathered for old Pro users
|
|
|
|
if($planDetails && $planDetails['trial']) {
|
|
|
|
// Do they have a non-trial plan?
|
|
|
|
$planDetails = $this->getPlanDetails(false, false);
|
|
|
|
}
|
2016-05-29 11:26:02 +02:00
|
|
|
|
2016-04-19 04:35:18 +02: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
|
|
|
|
2016-04-19 04:35:18 +02:00
|
|
|
// Enterprise; No Trial allowed
|
2016-04-19 16:26:29 +02:00
|
|
|
case FEATURE_DOCUMENTS:
|
2016-04-19 04:35:18 +02:00
|
|
|
case FEATURE_USER_PERMISSIONS:
|
|
|
|
return $selfHost || !empty($planDetails) && $planDetails['plan'] == PLAN_ENTERPRISE && !$planDetails['trial'];
|
2016-05-29 11:26:02 +02:00
|
|
|
|
2016-04-19 04:35:18 +02:00
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2016-05-29 11:26:02 +02:00
|
|
|
|
2016-04-17 00:34:39 +02:00
|
|
|
public function isPro(&$plan_details = null)
|
2015-03-16 22:45:25 +01:00
|
|
|
{
|
|
|
|
if (!Utils::isNinjaProd()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-10-06 19:55:55 +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 !empty($plan_details);
|
2016-04-17 00:34:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function isEnterprise(&$plan_details = null)
|
|
|
|
{
|
|
|
|
if (!Utils::isNinjaProd()) {
|
|
|
|
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
|
|
|
|
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
|
|
|
{
|
|
|
|
if (!$this->company) {
|
|
|
|
return null;
|
|
|
|
}
|
2016-05-29 11:26:02 +02:00
|
|
|
|
2016-04-17 00:34:39 +02:00
|
|
|
$plan = $this->company->plan;
|
|
|
|
$trial_plan = $this->company->trial_plan;
|
2016-05-29 11:26:02 +02:00
|
|
|
|
2016-04-17 00:34:39 +02:00
|
|
|
if(!$plan && (!$trial_plan || !$include_trial)) {
|
|
|
|
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()) {
|
2016-04-17 00:34:39 +02:00
|
|
|
$trial_active = true;
|
|
|
|
}
|
|
|
|
}
|
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) {
|
2016-04-19 04:35:18 +02:00
|
|
|
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
|
|
|
|
2016-04-19 04:35:18 +02: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?
|
|
|
|
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;
|
2016-04-17 18:55:03 +02: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 array(
|
|
|
|
'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,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return array(
|
|
|
|
'trial' => true,
|
|
|
|
'plan' => $trial_plan,
|
|
|
|
'started' => $trial_started,
|
|
|
|
'expires' => $trial_expires,
|
|
|
|
'active' => $trial_active,
|
|
|
|
);
|
|
|
|
}
|
2016-02-11 16:12:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function isTrial()
|
|
|
|
{
|
2016-02-15 18:58:18 +01:00
|
|
|
if (!Utils::isNinjaProd()) {
|
|
|
|
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'];
|
2016-02-11 16:12:27 +01:00
|
|
|
}
|
|
|
|
|
2016-04-17 00:34:39 +02:00
|
|
|
public function isEligibleForTrial($plan = null)
|
2016-02-11 16:12:27 +01:00
|
|
|
{
|
2016-04-17 00:34:39 +02:00
|
|
|
if (!$this->company->trial_plan) {
|
|
|
|
if ($plan) {
|
|
|
|
return $plan == PLAN_PRO || $plan == PLAN_ENTERPRISE;
|
|
|
|
} else {
|
|
|
|
return array(PLAN_PRO, PLAN_ENTERPRISE);
|
|
|
|
}
|
|
|
|
}
|
2016-05-29 11:26:02 +02:00
|
|
|
|
2016-04-17 00:34:39 +02:00
|
|
|
if ($this->company->trial_plan == PLAN_PRO) {
|
|
|
|
if ($plan) {
|
|
|
|
return $plan != PLAN_PRO;
|
|
|
|
} else {
|
|
|
|
return array(PLAN_ENTERPRISE);
|
|
|
|
}
|
|
|
|
}
|
2016-05-29 11:26:02 +02:00
|
|
|
|
2016-04-17 00:34:39 +02:00
|
|
|
return false;
|
2016-02-11 16:12:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getCountTrialDaysLeft()
|
|
|
|
{
|
2016-04-17 18:55:03 +02:00
|
|
|
$planDetails = $this->getPlanDetails(true);
|
2016-05-29 11:26:02 +02:00
|
|
|
|
2016-04-17 00:34:39 +02:00
|
|
|
if(!$planDetails || !$planDetails['trial']) {
|
|
|
|
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
|
|
|
|
2016-04-19 04:35:18 +02:00
|
|
|
return $interval ? $interval->d : 0;
|
2016-02-11 16:12:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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'];
|
2016-02-11 16:12:27 +01:00
|
|
|
$date = max($date, date_create());
|
|
|
|
} else {
|
|
|
|
$date = date_create();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $date->format('Y-m-d');
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
2015-11-29 21:13:50 +01:00
|
|
|
public function getLogoSize()
|
|
|
|
{
|
2016-03-22 21:19:55 +01:00
|
|
|
if(!$this->hasLogo()){
|
|
|
|
return null;
|
2015-11-29 21:13:50 +01:00
|
|
|
}
|
|
|
|
|
2016-03-22 21:19:55 +01:00
|
|
|
return round($this->logo_size / 1000);
|
2015-11-29 21:13:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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',
|
2016-05-26 16:56:54 +02:00
|
|
|
'invoice_type_id',
|
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
|
|
|
{
|
2016-04-19 04:35:18 +02:00
|
|
|
if ($this->hasFeature(FEATURE_CUSTOM_EMAILS)) {
|
2015-10-13 09:11:44 +02:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2016-01-11 13:40:48 +01:00
|
|
|
$template = "<div>\$client,</div><br>";
|
|
|
|
|
2016-04-19 04:35:18 +02:00
|
|
|
if ($this->hasFeature(FEATURE_CUSTOM_EMAILS) && $this->email_design_id != EMAIL_DESIGN_PLAIN) {
|
2016-01-11 13:40:48 +01:00
|
|
|
$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;
|
|
|
|
|
2016-04-19 04:35:18 +02:00
|
|
|
if ($this->hasFeature(FEATURE_CUSTOM_EMAILS)) {
|
2015-10-13 09:11:44 +02:00
|
|
|
$field = "email_template_{$entityType}";
|
|
|
|
$template = $this->$field;
|
2015-09-20 23:05:02 +02:00
|
|
|
}
|
2016-05-29 11:26:02 +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
|
|
|
}
|
|
|
|
|
2016-05-05 16:46:22 +02:00
|
|
|
public function getTemplateView($view = '')
|
|
|
|
{
|
|
|
|
return $this->getEmailDesignId() == EMAIL_DESIGN_PLAIN ? $view : 'design' . $this->getEmailDesignId();
|
|
|
|
}
|
|
|
|
|
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 {
|
2016-05-06 14:54:58 +02:00
|
|
|
return "<p><div>" . trans('texts.email_signature') . "\n<br>\$account</div></p>";
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-28 11:15:56 +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) {
|
2015-12-28 11:15:56 +01:00
|
|
|
return "reminder{$i}";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-04-27 01:59:52 +02:00
|
|
|
public function showTokenCheckbox(&$storage_gateway = null)
|
2015-03-16 22:45:25 +01:00
|
|
|
{
|
2016-04-28 04:13:51 +02:00
|
|
|
if (!($storage_gateway = $this->getTokenGatewayId())) {
|
2015-03-17 14:09:28 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-04-27 01:59:52 +02:00
|
|
|
public function getTokenGatewayId() {
|
|
|
|
if ($this->isGatewayConfigured(GATEWAY_STRIPE)) {
|
|
|
|
return GATEWAY_STRIPE;
|
|
|
|
} elseif ($this->isGatewayConfigured(GATEWAY_BRAINTREE)) {
|
|
|
|
return GATEWAY_BRAINTREE;
|
2016-05-13 15:30:22 +02:00
|
|
|
} elseif ($this->isGatewayConfigured(GATEWAY_WEPAY)) {
|
|
|
|
return GATEWAY_WEPAY;
|
2016-04-27 01:59:52 +02:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getTokenGateway() {
|
|
|
|
$gatewayId = $this->getTokenGatewayId();
|
|
|
|
if (!$gatewayId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->getGatewayConfig($gatewayId);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
public function getSiteUrl()
|
|
|
|
{
|
|
|
|
$url = SITE_URL;
|
|
|
|
$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}/?";
|
|
|
|
} 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;
|
|
|
|
}
|
|
|
|
|
2016-03-15 21:08:12 +01:00
|
|
|
public function showCustomField($field, $entity = false)
|
2015-10-13 09:11:44 +02:00
|
|
|
{
|
2016-04-19 04:35:18 +02:00
|
|
|
if ($this->hasFeature(FEATURE_INVOICE_SETTINGS)) {
|
2015-10-13 09:11:44 +02:00
|
|
|
return $this->$field ? true : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$entity) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-05-29 11:26:02 +02:00
|
|
|
|
2015-10-13 09:11:44 +02:00
|
|
|
// convert (for example) 'custom_invoice_label1' to 'invoice.custom_value1'
|
|
|
|
$field = str_replace(['invoice_', 'label'], ['', 'value'], $field);
|
2016-05-29 11:26:02 +02:00
|
|
|
|
2015-10-13 09:11:44 +02:00
|
|
|
return Utils::isEmpty($entity->$field) ? false : true;
|
|
|
|
}
|
|
|
|
|
2016-05-29 18:32:45 +02:00
|
|
|
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
|
|
|
|
2016-02-11 16:12:27 +01:00
|
|
|
public function getEmailDesignId()
|
|
|
|
{
|
2016-04-19 04:35:18 +02:00
|
|
|
return $this->hasFeature(FEATURE_CUSTOM_EMAILS) ? $this->email_design_id : EMAIL_DESIGN_PLAIN;
|
2016-02-11 16:12:27 +01:00
|
|
|
}
|
|
|
|
|
2015-12-30 19:11:51 +01:00
|
|
|
public function clientViewCSS(){
|
2016-04-19 04:35:18 +02:00
|
|
|
$css = '';
|
2016-05-29 11:26:02 +02:00
|
|
|
|
2016-04-19 04:35:18 +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.'}';
|
|
|
|
}
|
2016-04-19 04:35:18 +02:00
|
|
|
}
|
|
|
|
if ($this->hasFeature(FEATURE_CLIENT_PORTAL_CSS)) {
|
|
|
|
// For self-hosted users, a white-label license is required for custom CSS
|
|
|
|
$css .= $this->client_view_css;
|
2015-12-30 19:11:51 +01:00
|
|
|
}
|
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
|
|
|
|
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']);
|
2016-05-29 11:26:02 +02:00
|
|
|
|
2016-01-07 08:08:30 +01:00
|
|
|
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-05-29 11:26:02 +02:00
|
|
|
|
2016-01-11 13:50:17 +01:00
|
|
|
public function getHeaderFontId() {
|
2016-04-19 04:35:18 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
public function getBodyFontId() {
|
2016-04-19 04:35:18 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2016-05-29 11:26:02 +02:00
|
|
|
|
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
|
|
|
}
|
2016-05-29 11:26:02 +02:00
|
|
|
|
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'].';';
|
2016-05-29 11:26:02 +02:00
|
|
|
|
2016-01-07 08:08:30 +01:00
|
|
|
if($include_weight){
|
|
|
|
$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
|
|
|
|
2016-01-07 08:08:30 +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
|
|
|
|
2016-01-07 08:08:30 +01:00
|
|
|
if($include_weight){
|
|
|
|
$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
|
|
|
|
2016-01-07 08:08:30 +01:00
|
|
|
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
|
|
|
}
|
2016-05-29 11:26:02 +02:00
|
|
|
|
2016-01-07 08:08:30 +01:00
|
|
|
public function getFontsData(){
|
|
|
|
$data = array();
|
2016-05-29 11:26:02 +02:00
|
|
|
|
2016-01-07 08:08:30 +01:00
|
|
|
foreach($this->getFonts() as $font){
|
|
|
|
$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
|
|
|
|
2016-01-07 08:08:30 +01:00
|
|
|
public function getFontFolders(){
|
|
|
|
return array_map(function($item){return $item['folder'];}, $this->getFontsData());
|
2015-12-30 19:11:51 +01:00
|
|
|
}
|
2015-08-14 14:04:33 +02:00
|
|
|
}
|
|
|
|
|
2016-06-02 21:03:59 +02:00
|
|
|
Account::updated(function ($account)
|
|
|
|
{
|
|
|
|
// 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());
|
|
|
|
});
|