App\Console\Commands\CheckData::checkAccountData PHP Method

checkAccountData() private method

private checkAccountData ( )
    private function checkAccountData()
    {
        $tables = ['activities' => [ENTITY_INVOICE, ENTITY_CLIENT, ENTITY_CONTACT, ENTITY_PAYMENT, ENTITY_INVITATION, ENTITY_USER], 'invoices' => [ENTITY_CLIENT, ENTITY_USER], 'payments' => [ENTITY_INVOICE, ENTITY_CLIENT, ENTITY_USER, ENTITY_INVITATION, ENTITY_CONTACT], 'tasks' => [ENTITY_INVOICE, ENTITY_CLIENT, ENTITY_USER], 'credits' => [ENTITY_CLIENT, ENTITY_USER], 'expenses' => [ENTITY_CLIENT, ENTITY_VENDOR, ENTITY_INVOICE, ENTITY_USER]];
        foreach ($tables as $table => $entityTypes) {
            foreach ($entityTypes as $entityType) {
                $records = DB::table($table)->join("{$entityType}s", "{$entityType}s.id", '=', "{$table}.{$entityType}_id");
                if ($entityType != ENTITY_CLIENT) {
                    $records = $records->join('clients', 'clients.id', '=', "{$table}.client_id");
                }
                $records = $records->where("{$table}.account_id", '!=', DB::raw("{$entityType}s.account_id"))->get(["{$table}.id", 'clients.account_id', 'clients.user_id']);
                if (count($records)) {
                    $this->isValid = false;
                    $this->logMessage(count($records) . " {$table} records with incorrect {$entityType} account id");
                    if ($this->option('fix') == 'true') {
                        foreach ($records as $record) {
                            DB::table($table)->where('id', $record->id)->update(['account_id' => $record->account_id, 'user_id' => $record->user_id]);
                        }
                    }
                }
            }
        }
    }