1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 07:02:34 +01:00
invoiceninja/app/Models/Company.php

448 lines
11 KiB
PHP
Raw Normal View History

2019-04-18 07:01:40 +02:00
<?php
2019-05-11 05:32:07 +02:00
/**
* Invoice Ninja (https://invoiceninja.com)
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
2019-05-11 05:32:07 +02:00
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Models;
use App\DataMapper\CompanySettings;
2020-07-08 14:02:16 +02:00
use App\Events\Company\CompanyDocumentsDeleted;
use App\Models\Account;
use App\Models\Client;
use App\Models\CompanyGateway;
2019-06-24 13:05:47 +02:00
use App\Models\CompanyUser;
use App\Models\Country;
use App\Models\Credit;
use App\Models\Currency;
use App\Models\Design;
use App\Models\Expense;
2019-09-10 04:30:43 +02:00
use App\Models\GroupSetting;
use App\Models\Industry;
use App\Models\Invoice;
use App\Models\Language;
use App\Models\Payment;
use App\Models\PaymentType;
use App\Models\Product;
use App\Models\TaxRate;
use App\Models\Timezone;
use App\Models\Traits\AccountTrait;
use App\Models\User;
use App\Services\Notification\NotificationService;
use App\Utils\Ninja;
use App\Utils\Traits\CompanySettingsSaver;
use App\Utils\Traits\MakesHash;
use App\Utils\Traits\ThrottlesEmail;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notification;
2019-04-18 13:57:22 +02:00
use Illuminate\Support\Facades\Log;
use Laracasts\Presenter\PresentableTrait;
class Company extends BaseModel
{
use PresentableTrait;
use MakesHash;
use CompanySettingsSaver;
use ThrottlesEmail;
use \Staudenmeir\EloquentHasManyDeep\HasRelationships;
use \Staudenmeir\EloquentHasManyDeep\HasTableAlias;
const ENTITY_RECURRING_INVOICE = 'recurring_invoice';
const ENTITY_CREDIT = 'credit';
const ENTITY_QUOTE = 'quote';
const ENTITY_TASK = 'task';
const ENTITY_EXPENSE = 'expense';
const ENTITY_PROJECT = 'project';
const ENTITY_VENDOR = 'vendor';
const ENTITY_TICKET = 'ticket';
const ENTITY_PROPOSAL = 'proposal';
const ENTITY_RECURRING_EXPENSE = 'recurring_expense';
const ENTITY_RECURRING_TASK = 'task';
const ENTITY_RECURRING_QUOTE = 'recurring_quote';
// const int kModuleRecurringInvoices = 1;
// const int kModuleCredits = 2;
// const int kModuleQuotes = 4;
// const int kModuleTasks = 8;
// const int kModuleExpenses = 16;
// const int kModuleProjects = 32;
// const int kModuleVendors = 64;
// const int kModuleTickets = 128;
// const int kModuleProposals = 256;
// const int kModuleRecurringExpenses = 512;
// const int kModuleRecurringTasks = 1024;
// const int kModuleRecurringQuotes = 2048;
// kModuleInvoices = 4096;
// kModulePayments = 8192;
// 16383
protected $presenter = 'App\Models\Presenters\CompanyPresenter';
2019-06-17 02:15:42 +02:00
protected $fillable = [
2020-06-24 01:18:38 +02:00
'enabled_item_tax_rates',
'fill_products',
'industry_id',
'subdomain',
'size_id',
'custom_fields',
'enable_product_cost',
'enable_product_quantity',
'enabled_modules',
'default_quantity',
'enabled_tax_rates',
'portal_mode',
'portal_domain',
'convert_products',
'update_products',
'custom_surcharge_taxes1',
'custom_surcharge_taxes2',
'custom_surcharge_taxes3',
'custom_surcharge_taxes4',
'show_product_details',
'first_day_of_week',
'first_month_of_year',
'slack_webhook_url',
'google_analytics_key',
2020-06-28 12:38:41 +02:00
'client_can_register',
];
2019-07-30 00:28:38 +02:00
protected $hidden = [
'id',
'db',
'ip',
];
protected $casts = [
'country_id' => 'string',
'custom_fields' => 'object',
'settings' => 'object',
'custom_fields' => 'object',
'updated_at' => 'timestamp',
'created_at' => 'timestamp',
'deleted_at' => 'timestamp',
];
2019-04-25 09:16:41 +02:00
protected $with = [
// 'tokens'
];
public static $modules = [
self::ENTITY_RECURRING_INVOICE => 1,
self::ENTITY_CREDIT => 2,
self::ENTITY_QUOTE => 4,
self::ENTITY_TASK => 8,
self::ENTITY_EXPENSE => 16,
self::ENTITY_PROJECT => 32,
self::ENTITY_VENDOR => 64,
self::ENTITY_TICKET => 128,
self::ENTITY_PROPOSAL => 256,
self::ENTITY_RECURRING_EXPENSE => 512,
self::ENTITY_RECURRING_TASK => 1024,
self::ENTITY_RECURRING_QUOTE => 2048,
];
2020-06-24 10:59:56 +02:00
public function documents()
{
return $this->morphMany(Document::class, 'documentable');
}
public function getEntityType()
{
return Company::class;
}
public function ledger()
{
return $this->hasMany(CompanyLedger::class);
}
public function getCompanyIdAttribute()
{
return $this->encodePrimaryKey($this->id);
}
public function account()
{
return $this->belongsTo(Account::class);
}
public function users()
{
2020-01-07 10:35:55 +01:00
return $this->hasManyThrough(User::class, CompanyUser::class, 'company_id', 'id', 'id', 'user_id');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function clients()
{
return $this->hasMany(Client::class)->withTrashed();
}
2020-01-21 01:32:34 +01:00
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function tasks()
{
return $this->hasMany(Task::class)->withTrashed();
}
2020-07-07 00:35:28 +02:00
public function webhooks()
{
return $this->hasMany(Webhook::class);
}
2020-01-21 01:32:34 +01:00
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function projects()
{
return $this->hasMany(Project::class)->withTrashed();
}
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function vendors()
{
return $this->hasMany(Vendor::class)->withTrashed();
}
public function activities()
{
2020-06-24 01:18:38 +02:00
return $this->hasMany(Activity::class)->orderBy('id', 'DESC')->take(300);
}
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function contacts()
{
2019-07-08 02:08:57 +02:00
return $this->hasMany(ClientContact::class);
}
public function groups()
{
return $this->hasMany(GroupSetting::class);
}
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function invoices()
{
return $this->hasMany(Invoice::class)->withTrashed();
}
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function quotes()
{
return $this->hasMany(Quote::class)->withTrashed();
}
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function credits()
{
return $this->hasMany(Credit::class)->withTrashed();
}
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function company_gateways()
{
return $this->hasMany(CompanyGateway::class);
}
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function tax_rates()
{
return $this->hasMany(TaxRate::class);
}
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function products()
{
return $this->hasMany(Product::class);
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function country()
{
//return $this->belongsTo(Country::class);
return Country::find($this->settings->country_id);
}
2019-09-10 04:30:43 +02:00
public function group_settings()
{
return $this->hasMany(GroupSetting::class);
}
/**
*
*/
public function timezone()
{
2019-04-30 08:02:39 +02:00
return Timezone::find($this->settings->timezone_id);
}
public function designs()
{
return $this->hasMany(Design::class)->whereCompanyId($this->id)->orWhere('company_id', null);
}
public function payment_terms()
{
return $this->hasMany(PaymentTerm::class)->whereCompanyId($this->id)->orWhere('company_id', null);
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function language()
{
2019-04-30 08:02:39 +02:00
return Language::find($this->settings->language_id);
}
2019-09-11 01:31:55 +02:00
public function getLocale()
{
return isset($this->settings->language_id) && $this->language() ? $this->language()->locale : config('ninja.i18n.locale');
}
2019-10-03 07:17:57 +02:00
public function getLogo()
{
return $this->settings->company_logo ?: null;
2019-10-03 07:17:57 +02:00
}
public function locale()
{
return $this->getLocale();
}
public function getSetting($setting)
{
if (property_exists($this->settings, $setting) != false) {
return $this->settings->{$setting};
}
return null;
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function currency()
{
return $this->belongsTo(Currency::class);
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function industry()
{
return $this->belongsTo(Industry::class);
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function payment_type()
{
return $this->belongsTo(PaymentType::class);
}
/**
* @return mixed
*/
public function expenses()
{
return $this->hasMany(Expense::class)->withTrashed();
}
/**
* @return mixed
*/
public function payments()
{
return $this->hasMany(Payment::class)->withTrashed();
}
2019-03-26 05:46:08 +01:00
public function tokens()
{
return $this->hasMany(CompanyToken::class);
}
public function tokens_hashed()
{
return $this->hasMany(CompanyToken::class);
}
2019-03-27 07:22:27 +01:00
public function company_users()
{
//return $this->hasMany(CompanyUser::class)->withTimestamps();
return $this->hasMany(CompanyUser::class);
2019-03-27 07:22:27 +01:00
}
2019-04-18 13:57:22 +02:00
public function owner()
{
$c = $this->company_users->where('is_owner', true)->first();
2019-04-18 13:57:22 +02:00
return User::find($c->user_id);
}
public function resolveRouteBinding($value)
{
return $this
->where('id', $this->decodePrimaryKey($value))->firstOrFail();
}
public function domain()
{
if (Ninja::isNinja()) {
return $this->subdomain . config('ninja.app_domain');
}
return config('ninja.app_url');
}
public function notification(Notification $notification)
{
return new NotificationService($this, $notification);
}
public function routeNotificationForSlack($notification)
{
return $this->slack_webhook_url;
}
public function setMigration($status)
{
$company_users = CompanyUser::where('company_id', $this->id)->get();
foreach($company_users as $cu)
{
$cu->is_migrating=$status;
$cu->save();
}
}
}