1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-12 14:12:44 +01:00
invoiceninja/app/Console/Commands/InitLookup.php

291 lines
10 KiB
PHP
Raw Normal View History

2017-04-30 18:09:13 +02:00
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use DB;
2017-05-02 20:55:36 +02:00
use Mail;
use Exception;
2017-04-30 18:09:13 +02:00
use App\Models\DbServer;
2017-04-30 19:08:49 +02:00
use App\Models\LookupCompany;
use App\Models\LookupAccount;
use App\Models\LookupUser;
use App\Models\LookupContact;
2017-05-01 09:03:42 +02:00
use App\Models\LookupAccountToken;
2017-04-30 19:08:49 +02:00
use App\Models\LookupInvitation;
2017-04-30 18:09:13 +02:00
class InitLookup extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
2017-05-02 11:52:22 +02:00
protected $signature = 'ninja:init-lookup {--truncate=} {--validate=} {--company_id=} {--page_size=100} {--database=db-ninja-1}';
2017-04-30 18:09:13 +02:00
/**
* The console command description.
*
* @var string
*/
protected $description = 'Initialize lookup tables';
2017-05-02 11:52:22 +02:00
protected $log = '';
protected $isValid = true;
2017-04-30 18:09:13 +02:00
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
2017-05-02 11:52:22 +02:00
$this->logMessage('Running InitLookup...');
2017-04-30 18:09:13 +02:00
2017-04-30 20:44:52 +02:00
config(['database.default' => DB_NINJA_LOOKUP]);
2017-04-30 18:09:13 +02:00
2017-05-02 11:52:22 +02:00
$dbServer = DbServer::whereName($this->option('database'))->first();
2017-04-30 19:08:49 +02:00
2017-04-30 20:44:52 +02:00
if ($this->option('truncate')) {
$this->truncateTables();
2017-05-02 11:52:22 +02:00
$this->logMessage('Truncated');
} else {
config(['database.default' => $this->option('database')]);
2017-04-30 20:44:52 +02:00
2017-05-02 11:52:22 +02:00
$count = DB::table('companies')
->where('id', '>=', $this->option('company_id') ?: 1)
->count();
2017-04-30 19:08:49 +02:00
2017-05-02 11:52:22 +02:00
for ($i=0; $i<$count; $i += (int) $this->option('page_size')) {
$this->initCompanies($dbServer->id, $i);
}
2017-04-30 19:08:49 +02:00
}
2017-05-02 11:52:22 +02:00
$this->info($this->log);
2017-05-02 20:55:36 +02:00
if ($this->option('validate') && $errorEmail) {
Mail::raw($this->log, function ($message) use ($errorEmail, $database) {
$message->to($errorEmail)
->from(CONTACT_EMAIL)
->subject("Check-Lookups [{$database}]: " . strtoupper($this->isValid ? RESULT_SUCCESS : RESULT_FAILURE));
});
} elseif (! $this->isValid) {
throw new Exception('Check data failed!!');
}
2017-04-30 19:08:49 +02:00
}
2017-04-30 20:44:52 +02:00
private function initCompanies($dbServerId, $offset = 0)
2017-04-30 19:08:49 +02:00
{
2017-04-30 20:44:52 +02:00
$data = [];
2017-04-30 19:08:49 +02:00
2017-05-02 11:52:22 +02:00
config(['database.default' => $this->option('database')]);
2017-04-30 19:08:49 +02:00
2017-04-30 20:44:52 +02:00
$companies = DB::table('companies')
->offset($offset)
->limit((int) $this->option('page_size'))
2017-04-30 20:44:52 +02:00
->orderBy('id')
->where('id', '>=', $this->option('company_id') ?: 1)
->get(['id']);
2017-04-30 19:08:49 +02:00
foreach ($companies as $company) {
2017-04-30 20:44:52 +02:00
$data[$company->id] = $this->parseCompany($company->id);
}
config(['database.default' => DB_NINJA_LOOKUP]);
foreach ($data as $companyId => $company) {
2017-05-02 11:52:22 +02:00
$this->logMessage('Company Id: ' . $companyId);
2017-04-30 20:44:52 +02:00
2017-05-02 11:52:22 +02:00
if ($this->option('validate')) {
$lookupCompany = LookupCompany::whereDbServerId($dbServerId)->whereCompanyId($companyId)->first();
if (! $lookupCompany) {
$this->logError("LookupCompany - dbServerId: {$dbServerId}, companyId: {$companyId} | Not found!");
continue;
}
} else {
$lookupCompany = LookupCompany::create([
'db_server_id' => $dbServerId,
'company_id' => $companyId,
]);
}
2017-04-30 20:44:52 +02:00
foreach ($company as $accountKey => $account) {
2017-05-02 11:52:22 +02:00
if ($this->option('validate')) {
$lookupAccount = LookupAccount::whereLookupCompanyId($lookupCompany->id)->whereAccountKey($accountKey)->first();
if (! $lookupAccount) {
$this->logError("LookupAccount - lookupCompanyId: {$lookupCompany->id}, accountKey {$accountKey} | Not found!");
continue;
}
} else {
$lookupAccount = LookupAccount::create([
'lookup_company_id' => $lookupCompany->id,
'account_key' => $accountKey
2017-04-30 20:44:52 +02:00
]);
}
2017-05-02 11:52:22 +02:00
foreach ($account['users'] as $user) {
if ($this->option('validate')) {
$lookupUser = LookupUser::whereLookupAccountId($lookupAccount->id)->whereUserId($user['user_id'])->first();
if (! $lookupUser) {
$this->logError("LookupUser - lookupAccountId: {$lookupAccount->id}, userId: {$user['user_id']} | Not found!");
continue;
}
} else {
LookupUser::create([
'lookup_account_id' => $lookupAccount->id,
'email' => $user['email'],
'user_id' => $user['user_id'],
]);
}
}
2017-04-30 20:44:52 +02:00
foreach ($account['contacts'] as $contact) {
2017-05-02 11:52:22 +02:00
if ($this->option('validate')) {
$lookupContact = LookupContact::whereLookupAccountId($lookupAccount->id)->whereContactKey($contact['contact_key'])->first();
if (! $lookupContact) {
$this->logError("LookupContact - lookupAccountId: {$lookupAccount->id}, contactKey: {$contact['contact_key']} | Not found!");
continue;
}
} else {
LookupContact::create([
'lookup_account_id' => $lookupAccount->id,
'contact_key' => $contact['contact_key'],
]);
}
2017-04-30 20:44:52 +02:00
}
2017-05-02 11:52:22 +02:00
2017-04-30 20:44:52 +02:00
foreach ($account['invitations'] as $invitation) {
2017-05-02 11:52:22 +02:00
if ($this->option('validate')) {
$lookupInvitation = LookupInvitation::whereLookupAccountId($lookupAccount->id)->whereInvitationKey($invitation['invitation_key'])->first();
if (! $lookupInvitation) {
$this->logError("LookupInvitation - lookupAccountId: {$lookupAccount->id}, invitationKey: {$invitation['invitation_key']} | Not found!");
continue;
}
} else {
LookupInvitation::create([
'lookup_account_id' => $lookupAccount->id,
'invitation_key' => $invitation['invitation_key'],
'message_id' => $invitation['message_id'] ?: null,
]);
}
2017-04-30 20:44:52 +02:00
}
2017-05-02 11:52:22 +02:00
2017-04-30 20:44:52 +02:00
foreach ($account['tokens'] as $token) {
2017-05-02 11:52:22 +02:00
if ($this->option('validate')) {
$lookupToken = LookupAccountToken::whereLookupAccountId($lookupAccount->id)->whereToken($token['token'])->first();
if (! $lookupToken) {
$this->logError("LookupAccountToken - lookupAccountId: {$lookupAccount->id}, token: {$token['token']} | Not found!");
continue;
}
} else {
LookupAccountToken::create([
'lookup_account_id' => $lookupAccount->id,
'token' => $token['token'],
]);
}
2017-04-30 20:44:52 +02:00
}
}
2017-04-30 19:08:49 +02:00
}
}
2017-04-30 20:44:52 +02:00
private function parseCompany($companyId)
2017-04-30 19:08:49 +02:00
{
$data = [];
2017-05-02 11:52:22 +02:00
config(['database.default' => $this->option('database')]);
2017-04-30 19:08:49 +02:00
2017-04-30 20:44:52 +02:00
$accounts = DB::table('accounts')->whereCompanyId($companyId)->orderBy('id')->get(['id', 'account_key']);
2017-04-30 19:08:49 +02:00
foreach ($accounts as $account) {
2017-04-30 20:44:52 +02:00
$data[$account->account_key] = $this->parseAccount($account->id);
2017-04-30 19:08:49 +02:00
}
2017-04-30 20:44:52 +02:00
return $data;
2017-04-30 19:08:49 +02:00
}
private function parseAccount($accountId)
{
$data = [
'users' => [],
'contacts' => [],
'invitations' => [],
'tokens' => [],
];
2017-04-30 21:29:15 +02:00
$users = DB::table('users')->whereAccountId($accountId)->orderBy('id')->get(['email', 'id']);
2017-04-30 19:08:49 +02:00
foreach ($users as $user) {
2017-04-30 21:29:15 +02:00
$data['users'][] = [
'email' => $user->email,
'user_id' => $user->id,
];
2017-04-30 19:08:49 +02:00
}
$contacts = DB::table('contacts')->whereAccountId($accountId)->orderBy('id')->get(['contact_key']);
foreach ($contacts as $contact) {
2017-04-30 21:29:15 +02:00
$data['contacts'][] = [
'contact_key' => $contact->contact_key,
];
2017-04-30 19:08:49 +02:00
}
$invitations = DB::table('invitations')->whereAccountId($accountId)->orderBy('id')->get(['invitation_key', 'message_id']);
foreach ($invitations as $invitation) {
2017-04-30 21:29:15 +02:00
$data['invitations'][] = [
'invitation_key' => $invitation->invitation_key,
'message_id' => $invitation->message_id,
];
2017-04-30 19:08:49 +02:00
}
$tokens = DB::table('account_tokens')->whereAccountId($accountId)->orderBy('id')->get(['token']);
foreach ($tokens as $token) {
2017-04-30 21:29:15 +02:00
$data['tokens'][] = [
'token' => $token->token,
];
2017-04-30 19:08:49 +02:00
}
return $data;
2017-04-30 18:09:13 +02:00
}
2017-04-30 20:44:52 +02:00
2017-05-02 11:52:22 +02:00
private function logMessage($str)
{
$this->log .= date('Y-m-d h:i:s') . ' ' . $str . "\n";
}
private function logError($str)
{
$this->isValid = false;
$this->logMessage($str);
}
2017-04-30 20:44:52 +02:00
private function truncateTables()
{
DB::statement('SET FOREIGN_KEY_CHECKS = 0');
DB::statement('truncate lookup_companies');
DB::statement('truncate lookup_accounts');
DB::statement('truncate lookup_users');
DB::statement('truncate lookup_contacts');
DB::statement('truncate lookup_invitations');
DB::statement('truncate lookup_account_tokens');
2017-04-30 20:44:52 +02:00
DB::statement('SET FOREIGN_KEY_CHECKS = 1');
}
protected function getOptions()
{
return [
['truncate', null, InputOption::VALUE_OPTIONAL, 'Truncate', null],
['company_id', null, InputOption::VALUE_OPTIONAL, 'Company Id', null],
['page_size', null, InputOption::VALUE_OPTIONAL, 'Page Size', null],
2017-05-02 11:52:22 +02:00
['database', null, InputOption::VALUE_OPTIONAL, 'Database', null],
['validate', null, InputOption::VALUE_OPTIONAL, 'Validate', null],
2017-04-30 20:44:52 +02:00
];
}
2017-04-30 18:09:13 +02:00
}