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

1379 lines
39 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;
2016-04-17 01:14:43 +02:00
use App\Models\Document;
2015-08-14 14:04:33 +02:00
use App\Events\UserSettingsChanged;
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-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',
'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,
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-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-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-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)
{
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;
}
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');
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);
}
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()
{
if($this->logo == ''){
$this->calculateLogoDetails();
}
return !empty($this->logo);
}
public function getLogoDisk(){
2016-03-23 20:41:05 +01:00
return Storage::disk(env('LOGO_FILESYSTEM', 'logos'));
}
protected function calculateLogoDetails(){
$disk = $this->getLogoDisk();
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';
}
if(!empty($this->logo)){
$image = imagecreatefromstring($disk->get($this->logo));
$this->logo_width = imagesx($image);
$this->logo_height = imagesy($image);
$this->logo_size = $disk->size($this->logo);
} else {
$this->logo = null;
}
$this->save();
2015-03-16 22:45:25 +01:00
}
public function getLogoRaw(){
if(!$this->hasLogo()){
return null;
}
$disk = $this->getLogoDisk();
return $disk->get($this->logo);
2016-01-18 15:09:06 +01:00
}
2016-04-17 01:14:43 +02:00
public function getLogoURL($cachebuster = false)
2015-11-10 23:07:05 +01:00
{
if(!$this->hasLogo()){
return null;
}
$disk = $this->getLogoDisk();
$adapter = $disk->getAdapter();
if($adapter instanceof \League\Flysystem\Adapter\Local) {
// Stored locally
$logo_url = str_replace(public_path(), url('/'), $adapter->applyPathPrefix($this->logo), $count);
2016-04-17 01:14:43 +02:00
if ($cachebuster) {
$logo_url .= '?no_cache='.time();
}
if($count == 1){
return str_replace(DIRECTORY_SEPARATOR, '/', $logo_url);
}
}
2016-04-17 01:14:43 +02:00
return Document::getDirectFileUrl($this->logo, $this->getLogoDisk());
2015-11-10 23:07:05 +01:00
}
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()
{
if(!$this->hasLogo()){
return null;
2015-03-16 22:45:25 +01:00
}
return $this->logo_width;
2015-03-16 22:45:25 +01:00
}
public function getLogoHeight()
{
if(!$this->hasLogo()){
return null;
2015-03-16 22:45:25 +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;
$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->hasFeature(FEATURE_INVOICE_SETTINGS)) {
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->hasFeature(FEATURE_INVOICE_SETTINGS)) {
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}';
2016-04-19 13:26:42 +02:00
$replace[] = str_pad($this->getCounter($invoice->is_quote), $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);
}
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 {
2016-04-19 13:26:42 +02:00
$number = $prefix . str_pad($counter, $this->invoice_number_padding, '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->hasFeature(FEATURE_INVOICE_SETTINGS) && $default != $actual) {
2015-11-29 11:41:32 +01:00
$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();
}
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'],
];
foreach ($map as $key => $values) {
$this->load([$key => function($query) use ($values, $updatedAt) {
$query->withTrashed()->with($values);
if ($updatedAt) {
$query->where('updated_at', '>=', $updatedAt);
}
}]);
}
}
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',
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',
'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;
}
2016-04-17 00:34:39 +02:00
public function startTrial($plan)
{
if ( ! Utils::isNinja()) {
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 hasFeature($feature)
{
2016-05-01 10:43:30 +02:00
if (Utils::isNinjaDev()) {
return true;
}
$planDetails = $this->getPlanDetails();
$selfHost = !Utils::isNinjaProd();
if (!$selfHost && function_exists('ninja_account_features')) {
$result = ninja_account_features($this, $feature);
if ($result != null) {
return $result;
}
}
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);
// 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'])) {
return false;
}
// Fallthrough
case FEATURE_CLIENT_PORTAL_CSS:
return !empty($planDetails);// A plan is required even for self-hosted users
// 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);
}
return $selfHost || !empty($planDetails) && ($planDetails['plan'] == PLAN_ENTERPRISE || $planDetails['started'] <= date_create(PRO_USERS_GRANDFATHER_DEADLINE));
// Enterprise; No Trial allowed
case FEATURE_DOCUMENTS:
case FEATURE_USER_PERMISSIONS:
return $selfHost || !empty($planDetails) && $planDetails['plan'] == PLAN_ENTERPRISE && !$planDetails['trial'];
default:
return false;
}
}
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;
}
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-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-04-17 18:55:03 +02:00
return $plan_details && $plan_details['plan'] == PLAN_ENTERPRISE;
2016-04-17 00:34:39 +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;
}
$plan = $this->company->plan;
$trial_plan = $this->company->trial_plan;
if(!$plan && (!$trial_plan || !$include_trial)) {
return null;
}
$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-04-17 04:09:01 +02:00
if ($trial_expires >= date_create()) {
2016-04-17 00:34:39 +02:00
$trial_active = true;
}
}
$plan_active = false;
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;
}
}
}
if (!$include_inactive && !$plan_active && !$trial_active) {
2016-04-17 18:55:03 +02:00
return null;
}
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
}
}
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,
);
}
}
public function isTrial()
{
2016-02-15 18:58:18 +01:00
if (!Utils::isNinjaProd()) {
return false;
}
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-04-17 00:34:39 +02:00
public function isEligibleForTrial($plan = null)
{
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);
}
}
if ($this->company->trial_plan == PLAN_PRO) {
if ($plan) {
return $plan != PLAN_PRO;
} else {
return array(PLAN_ENTERPRISE);
}
}
return false;
}
public function getCountTrialDaysLeft()
{
2016-04-17 18:55:03 +02:00
$planDetails = $this->getPlanDetails(true);
2016-04-17 00:34:39 +02:00
if(!$planDetails || !$planDetails['trial']) {
return 0;
}
2016-04-17 00:34:39 +02:00
$today = new DateTime('now');
$interval = $today->diff($planDetails['expires']);
return $interval ? $interval->d : 0;
}
public function getRenewalDate()
{
2016-04-17 00:34:39 +02:00
$planDetails = $this->getPlanDetails();
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();
}
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()
{
if(!$this->hasLogo()){
return null;
2015-11-29 21:13:50 +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',
'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
{
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;
}
$template = "<div>\$client,</div><br>";
if ($this->hasFeature(FEATURE_CUSTOM_EMAILS) && $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;
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
}
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
}
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
}
}
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 = false)
2015-10-13 09:11:44 +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;
}
// 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()
{
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
}
public function getEmailDesignId()
{
return $this->hasFeature(FEATURE_CUSTOM_EMAILS) ? $this->email_design_id : EMAIL_DESIGN_PLAIN;
}
public function clientViewCSS(){
$css = '';
2016-01-07 08:08:30 +01:00
if ($this->hasFeature(FEATURE_CUSTOMIZE_INVOICE_DESIGN)) {
2016-01-07 08:08:30 +01:00
$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 ($this->hasFeature(FEATURE_CLIENT_PORTAL_CSS)) {
// 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 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->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() {
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
}
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());
});