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