diff --git a/app/Models/Account.php b/app/Models/Account.php index 42e427a23936..5e830735c943 100644 --- a/app/Models/Account.php +++ b/app/Models/Account.php @@ -4,6 +4,7 @@ namespace App\Models; use App; use App\Events\UserSettingsChanged; +use App\Models\LookupAccount; use App\Models\Traits\GeneratesNumbers; use App\Models\Traits\PresentsInvoice; use App\Models\Traits\SendsEmails; @@ -1655,6 +1656,12 @@ class Account extends Eloquent } } +Account::creating(function ($account) +{ + LookupAccount::createAccount($account->account_key, $account->company_id); +}); + + Account::updated(function ($account) { // prevent firing event if the invoice/quote counter was changed // TODO: remove once counters are moved to separate table diff --git a/app/Models/LookupAccount.php b/app/Models/LookupAccount.php index fcb30596ce0c..7354e3c6746a 100644 --- a/app/Models/LookupAccount.php +++ b/app/Models/LookupAccount.php @@ -22,4 +22,31 @@ class LookupAccount extends LookupModel return $this->belongsTo('App\Models\LookupCompany'); } + public static function createAccount($accountKey, $companyId) + { + if (! env('MULTI_DB_ENABLED')) { + return; + } + + $current = config('database.default'); + config(['database.default' => DB_NINJA_LOOKUP]); + + $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); + } } diff --git a/app/Models/LookupModel.php b/app/Models/LookupModel.php index 0133909cf931..0e8a634bb636 100644 --- a/app/Models/LookupModel.php +++ b/app/Models/LookupModel.php @@ -61,10 +61,11 @@ class LookupModel extends Eloquent config(['database.default' => DB_NINJA_LOOKUP]); if ($lookupUser = static::where($field, '=', $value)->first()) { + $entity = new $className(); $server = $lookupUser->getDbServer(); static::setDbServer($server); - $entity = new $className(); + // check entity is found on the server if (! $entity::where($field, '=', $value)->first()) { abort("Looked up {$className} not found: {$field} => {$value}"); } diff --git a/app/Models/User.php b/app/Models/User.php index d89a88b66a8d..b9edafa6da3b 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -413,7 +413,7 @@ class User extends Authenticatable } } -User::creating(function ($user) +User::created(function ($user) { LookupUser::createNew($user->account->account_key, [ 'email' => $user->email, diff --git a/database/migrations/2017_04_30_174702_add_multiple_database_support.php b/database/migrations/2017_04_30_174702_add_multiple_database_support.php index 01aff58507d5..a0fbbeb6fe61 100644 --- a/database/migrations/2017_04_30_174702_add_multiple_database_support.php +++ b/database/migrations/2017_04_30_174702_add_multiple_database_support.php @@ -25,7 +25,7 @@ class AddMultipleDatabaseSupport extends Migration }); Schema::table('lookup_users', function ($table) { - $table->string('email')->change()->unique(); + $table->string('email')->change()->nullable()->unique(); $table->unsignedInteger('user_id')->index(); });