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

576 lines
15 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-08-11 16:38:36 +02:00
use App;
2015-08-14 14:04:33 +02:00
use App\Events\UserSettingsChanged;
2015-03-31 11:38:24 +02:00
use Illuminate\Database\Eloquent\SoftDeletes;
2015-03-16 22:45:25 +01:00
class Account extends Eloquent
{
2015-03-31 11:38:24 +02:00
use SoftDeletes;
protected $dates = ['deleted_at'];
2015-10-02 10:32:13 +02:00
protected $hidden = ['ip'];
2015-03-16 22:45:25 +01:00
2015-10-14 16:15:39 +02:00
public static $basicSettings = [
ACCOUNT_COMPANY_DETAILS,
ACCOUNT_USER_DETAILS,
ACCOUNT_LOCALIZATION,
ACCOUNT_PAYMENTS,
ACCOUNT_PRODUCTS,
ACCOUNT_NOTIFICATIONS,
ACCOUNT_IMPORT_EXPORT,
];
public static $advancedSettings = [
ACCOUNT_INVOICE_DESIGN,
ACCOUNT_INVOICE_SETTINGS,
ACCOUNT_TEMPLATES_AND_REMINDERS,
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-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
}
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
}
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
}
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
}
public function isGatewayConfigured($gatewayId = 0)
{
$this->load('account_gateways');
if ($gatewayId) {
return $this->getGatewayConfig($gatewayId) != false;
} else {
return count($this->account_gateways) > 0;
}
}
2015-08-11 16:38:36 +02:00
public function isEnglish()
{
return !$this->language_id || $this->language_id == DEFAULT_LANGUAGE;
}
2015-03-16 22:45:25 +01:00
public function getDisplayName()
{
if ($this->name) {
return $this->name;
}
$this->load('users');
$user = $this->users()->first();
return $user->getDisplayName();
}
2015-10-13 17:44:01 +02:00
public function getCityState()
{
$swap = $this->country && $this->country->swap_postal_code;
return Utils::cityStateZip($this->city, $this->state, $this->postal_code, $swap);
}
public function getMomentDateTimeFormat()
{
$format = $this->datetime_format ? $this->datetime_format->format_moment : DEFAULT_DATETIME_MOMENT_FORMAT;
if ($this->military_time) {
$format = str_replace('h:mm:ss a', 'H:mm:ss', $format);
}
return $format;
}
2015-03-16 22:45:25 +01:00
public function getTimezone()
{
if ($this->timezone) {
return $this->timezone->name;
} else {
return 'US/Eastern';
}
}
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()
{
2015-10-13 17:44:01 +02:00
return file_exists($this->getLogoPath());
2015-07-07 22:08:16 +02:00
}
2015-03-16 22:45:25 +01:00
public function getLogoPath()
{
2015-07-24 16:13:17 +02:00
$fileName = 'logo/' . $this->account_key;
2015-08-07 08:14:29 +02:00
return file_exists($fileName.'.png') ? $fileName.'.png' : $fileName.'.jpg';
2015-03-16 22:45:25 +01:00
}
public function getLogoWidth()
{
$path = $this->getLogoPath();
if (!file_exists($path)) {
return 0;
}
list($width, $height) = getimagesize($path);
return $width;
}
public function getLogoHeight()
{
$path = $this->getLogoPath();
if (!file_exists($path)) {
return 0;
}
list($width, $height) = getimagesize($path);
return $height;
}
2015-06-04 22:53:58 +02:00
public function getNextInvoiceNumber($isQuote = false, $prefix = '')
2015-03-16 22:45:25 +01:00
{
$counter = $isQuote && !$this->share_counter ? $this->quote_number_counter : $this->invoice_number_counter;
2015-06-04 22:53:58 +02:00
$prefix .= $isQuote ? $this->quote_number_prefix : $this->invoice_number_prefix;
$counterOffset = 0;
2015-06-04 22:53:58 +02:00
// confirm the invoice number isn't already taken
do {
$number = $prefix.str_pad($counter, 4, "0", STR_PAD_LEFT);
2015-06-10 10:34:20 +02:00
$check = Invoice::scope(false, $this->id)->whereInvoiceNumber($number)->withTrashed()->first();
2015-06-04 22:53:58 +02:00
$counter++;
$counterOffset++;
2015-06-04 22:53:58 +02:00
} while ($check);
// update the invoice counter to be caught up
if ($counterOffset > 1) {
if ($isQuote && !$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
}
public function incrementCounter($isQuote = false)
2015-03-16 22:45:25 +01:00
{
if ($isQuote && !$this->share_counter) {
$this->quote_number_counter += 1;
2015-03-16 22:45:25 +01:00
} else {
$this->invoice_number_counter += 1;
2015-03-16 22:45:25 +01:00
}
$this->save();
}
public function getLocale()
{
2015-04-08 15:19:17 +02:00
$language = Language::where('id', '=', $this->account->language_id)->first();
2015-03-16 22:45:25 +01:00
return $language->locale;
}
public function loadLocalizationSettings($client = false)
2015-03-16 22:45:25 +01:00
{
$this->load('timezone', 'date_format', 'datetime_format', 'language');
Session::put(SESSION_TIMEZONE, $this->timezone ? $this->timezone->name : DEFAULT_TIMEZONE);
Session::put(SESSION_DATE_FORMAT, $this->date_format ? $this->date_format->format : DEFAULT_DATE_FORMAT);
Session::put(SESSION_DATE_PICKER_FORMAT, $this->date_format ? $this->date_format->picker_format : DEFAULT_DATE_PICKER_FORMAT);
2015-08-11 16:38:36 +02:00
$currencyId = ($client && $client->currency_id) ? $client->currency_id : $this->currency_id ?: DEFAULT_CURRENCY;
$locale = ($client && $client->language_id) ? $client->language->locale : ($this->language_id ? $this->Language->locale : DEFAULT_LOCALE);
Session::put(SESSION_CURRENCY, $currencyId);
Session::put(SESSION_LOCALE, $locale);
App::setLocale($locale);
$format = $this->datetime_format ? $this->datetime_format->format : DEFAULT_DATETIME_FORMAT;
if ($this->military_time) {
$format = str_replace('g:i a', 'H:i', $format);
}
Session::put(SESSION_DATETIME_FORMAT, $format);
2015-03-16 22:45:25 +01:00
}
public function getInvoiceLabels()
{
$data = [];
$custom = (array) json_decode($this->invoice_labels);
2015-03-16 22:45:25 +01:00
$fields = [
'invoice',
'invoice_date',
'due_date',
'invoice_number',
'po_number',
'discount',
'taxes',
'tax',
'item',
'description',
'unit_cost',
'quantity',
'line_total',
'subtotal',
'paid_to_date',
'balance_due',
2015-04-16 21:57:12 +02:00
'amount_due',
2015-03-16 22:45:25 +01:00
'terms',
'your_invoice',
'quote',
'your_quote',
'quote_date',
'quote_number',
'total',
'invoice_issued_to',
2015-06-10 10:34:20 +02:00
'date',
'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;
}
2015-03-16 22:45:25 +01:00
public function isPro()
{
if (!Utils::isNinjaProd()) {
return true;
}
if ($this->isNinjaAccount()) {
2015-03-16 22:45:25 +01:00
return true;
}
$datePaid = $this->pro_plan_paid;
if (!$datePaid || $datePaid == '0000-00-00') {
return false;
} elseif ($datePaid == NINJA_DATE) {
return true;
}
$today = new DateTime('now');
$datePaid = DateTime::createFromFormat('Y-m-d', $datePaid);
$interval = $today->diff($datePaid);
return $interval->y == 0;
}
public function isWhiteLabel()
{
if ($this->isNinjaAccount()) {
return false;
}
2015-08-14 14:04:33 +02:00
if (Utils::isNinjaProd()) {
2015-08-03 09:15:58 +02:00
return self::isPro() && $this->pro_plan_paid != NINJA_DATE;
} else {
return $this->pro_plan_paid == NINJA_DATE;
2015-03-16 22:45:25 +01:00
}
}
public function getSubscription($eventId)
{
return Subscription::where('account_id', '=', $this->id)->where('event_id', '=', $eventId)->first();
}
public function hideFieldsForViz()
{
foreach ($this->clients as $client) {
$client->setVisible([
'public_id',
'name',
'balance',
'paid_to_date',
'invoices',
'contacts',
]);
foreach ($client->invoices as $invoice) {
$invoice->setVisible([
'public_id',
'invoice_number',
'amount',
'balance',
'invoice_status_id',
'invoice_items',
'created_at',
2015-08-11 16:38:36 +02:00
'is_recurring',
'is_quote',
2015-03-16 22:45:25 +01:00
]);
foreach ($invoice->invoice_items as $invoiceItem) {
$invoiceItem->setVisible([
'product_key',
'cost',
'qty',
]);
}
}
foreach ($client->contacts as $contact) {
$contact->setVisible([
'public_id',
'first_name',
'last_name',
'email', ]);
}
}
return $this;
}
2015-09-20 23:05:02 +02:00
public function getDefaultEmailSubject($entityType)
2015-09-17 21:01:06 +02:00
{
if (strpos($entityType, 'reminder') !== false) {
$entityType = 'reminder';
}
return trans("texts.{$entityType}_subject", ['invoice' => '$invoice', 'account' => '$account']);
}
2015-09-20 23:05:02 +02:00
public function getEmailSubject($entityType)
2015-03-16 22:45:25 +01:00
{
2015-10-13 09:11:44 +02:00
if ($this->isPro()) {
$field = "email_subject_{$entityType}";
$value = $this->$field;
2015-03-16 22:45:25 +01:00
2015-10-13 09:11:44 +02:00
if ($value) {
return $value;
}
2015-03-16 22:45:25 +01:00
}
2015-09-20 23:05:02 +02:00
return $this->getDefaultEmailSubject($entityType);
}
public function getDefaultEmailTemplate($entityType, $message = false)
{
2015-09-17 21:01:06 +02:00
if (strpos($entityType, 'reminder') >= 0) {
$entityType = ENTITY_INVOICE;
}
2015-03-16 22:45:25 +01:00
$template = "\$client,<p/>\r\n\r\n" .
2015-09-07 11:07:55 +02:00
trans("texts.{$entityType}_message", ['amount' => '$amount']) . "<p/>\r\n\r\n" .
"<a href=\"\$link\">\$link</a><p/>\r\n\r\n";
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-10-13 09:11:44 +02:00
if ($this->isPro()) {
$field = "email_template_{$entityType}";
$template = $this->$field;
2015-09-20 23:05:02 +02:00
2015-10-13 09:11:44 +02:00
if ($template) {
return $template;
}
2015-09-20 23:05:02 +02:00
}
2015-10-13 09:11:44 +02:00
2015-09-20 23:05:02 +02:00
return $this->getDefaultEmailTemplate($entityType, $message);
}
2015-03-16 22:45:25 +01:00
public function getEmailFooter()
{
if ($this->email_footer) {
2015-04-22 21:21:04 +02:00
// Add line breaks if HTML isn't already being used
2015-09-07 11:07:55 +02:00
return strip_tags($this->email_footer) == $this->email_footer ? nl2br($this->email_footer) : $this->email_footer;
2015-03-16 22:45:25 +01:00
} else {
2015-09-17 21:01:06 +02:00
return "<p>" . trans('texts.email_signature') . "\n<br>\$account</p>";
2015-03-16 22:45:25 +01:00
}
}
public function showTokenCheckbox()
{
if (!$this->isGatewayConfigured(GATEWAY_STRIPE)) {
return false;
}
2015-03-16 22:45:25 +01:00
return $this->token_billing_type_id == TOKEN_BILLING_OPT_IN
|| $this->token_billing_type_id == TOKEN_BILLING_OPT_OUT;
}
public function selectTokenCheckbox()
{
return $this->token_billing_type_id == TOKEN_BILLING_OPT_OUT;
}
2015-09-17 21:01:06 +02:00
public function getSiteUrl()
{
$url = SITE_URL;
$iframe_url = $this->iframe_url;
if ($iframe_url) {
return "{$iframe_url}/?";
} else if ($this->subdomain) {
$url = Utils::replaceSubdomain($url, $this->subdomain);
}
return $url;
}
2015-10-13 09:11:44 +02:00
public function checkSubdomain($host)
{
if (!$this->subdomain) {
return true;
}
$server = explode('.', $host);
$subdomain = $server[0];
if (!in_array($subdomain, ['app', 'www']) && $subdomain != $this->subdomain) {
return false;
}
return true;
}
public function showCustomField($field, $entity)
{
if ($this->isPro()) {
return $this->$field ? true : false;
}
if (!$entity) {
return false;
}
// convert (for example) 'custom_invoice_label1' to 'invoice.custom_value1'
$field = str_replace(['invoice_', 'label'], ['', 'value'], $field);
return Utils::isEmpty($entity->$field) ? false : true;
}
public function attatchPDF()
{
return $this->isPro() && $this->pdf_email_attachment;
}
2015-08-14 14:04:33 +02:00
}
Account::updated(function ($account) {
Event::fire(new UserSettingsChanged());
});