2017-04-30 19:08:49 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
use Eloquent;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class ExpenseCategory.
|
|
|
|
*/
|
2017-04-30 21:18:17 +02:00
|
|
|
class LookupAccount extends LookupModel
|
2017-04-30 19:08:49 +02:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $fillable = [
|
|
|
|
'lookup_company_id',
|
|
|
|
'account_key',
|
|
|
|
];
|
|
|
|
|
2017-04-30 21:52:05 +02:00
|
|
|
public function lookupCompany()
|
|
|
|
{
|
|
|
|
return $this->belongsTo('App\Models\LookupCompany');
|
|
|
|
}
|
|
|
|
|
2017-05-01 10:13:15 +02:00
|
|
|
public static function createAccount($accountKey, $companyId)
|
|
|
|
{
|
|
|
|
if (! env('MULTI_DB_ENABLED')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$current = config('database.default');
|
|
|
|
config(['database.default' => DB_NINJA_LOOKUP]);
|
2017-05-11 09:52:13 +02:00
|
|
|
|
2017-05-01 10:13:15 +02:00
|
|
|
$server = DbServer::whereName($current)->firstOrFail();
|
|
|
|
$lookupCompany = LookupCompany::whereDbServerId($server->id)
|
|
|
|
->whereCompanyId($companyId)->first();
|
|
|
|
|
|
|
|
if (! $lookupCompany) {
|
|
|
|
$lookupCompany = LookupCompany::create([
|
|
|
|
'db_server_id' => $server->id,
|
|
|
|
'company_id' => $companyId,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
LookupAccount::create([
|
|
|
|
'lookup_company_id' => $lookupCompany->id,
|
|
|
|
'account_key' => $accountKey,
|
|
|
|
]);
|
|
|
|
|
|
|
|
static::setDbServer($current);
|
|
|
|
}
|
2017-05-11 09:52:13 +02:00
|
|
|
|
|
|
|
public function getDbServer()
|
|
|
|
{
|
|
|
|
return $this->lookupCompany->dbServer->name;
|
|
|
|
}
|
|
|
|
|
2017-11-15 13:59:20 +01:00
|
|
|
public static function updateAccount($accountKey, $account)
|
|
|
|
{
|
|
|
|
if (! env('MULTI_DB_ENABLED')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$current = config('database.default');
|
|
|
|
config(['database.default' => DB_NINJA_LOOKUP]);
|
|
|
|
|
|
|
|
$lookupAccount = LookupAccount::whereAccountKey($accountKey)
|
|
|
|
->firstOrFail();
|
|
|
|
|
2017-11-15 19:26:51 +01:00
|
|
|
$lookupAccount->subdomain = $account->subdomain ?: null;
|
2017-11-15 13:59:20 +01:00
|
|
|
$lookupAccount->save();
|
|
|
|
|
|
|
|
config(['database.default' => $current]);
|
|
|
|
}
|
|
|
|
|
2017-11-15 13:51:02 +01:00
|
|
|
public static function validateField($field, $value, $account = false)
|
|
|
|
{
|
|
|
|
if (! env('MULTI_DB_ENABLED')) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$current = config('database.default');
|
2017-11-15 13:59:20 +01:00
|
|
|
|
2017-11-15 13:51:02 +01:00
|
|
|
config(['database.default' => DB_NINJA_LOOKUP]);
|
|
|
|
|
|
|
|
$lookupAccount = LookupAccount::where($field, '=', $value)->first();
|
|
|
|
|
|
|
|
if ($account) {
|
|
|
|
$isValid = ! $lookupAccount || ($lookupAccount->account_key == $account->account_key);
|
|
|
|
} else {
|
|
|
|
$isValid = ! $lookupAccount;
|
|
|
|
}
|
|
|
|
|
|
|
|
config(['database.default' => $current]);
|
|
|
|
|
|
|
|
return $isValid;
|
|
|
|
}
|
2017-04-30 19:08:49 +02:00
|
|
|
}
|