diff --git a/database/migrations/2016_04_16_103943_enterprise_plan.php b/database/migrations/2016_04_16_103943_enterprise_plan.php index ab6bf88922..0ff4417a22 100644 --- a/database/migrations/2016_04_16_103943_enterprise_plan.php +++ b/database/migrations/2016_04_16_103943_enterprise_plan.php @@ -12,9 +12,9 @@ class EnterprisePlan extends Migration * * @return void */ - public function up() - { - Schema::create('companies', function($table) + public function up() { + + Schema::create('companies', function($table) { $table->increments('id'); @@ -47,38 +47,84 @@ class EnterprisePlan extends Migration $table->foreign('company_id')->references('id')->on('companies'); }); - foreach (Account::all() as $account) { - $company = Company::create(); - if ($account->pro_plan_paid && $account->pro_plan_paid != '0000-00-00') { - $company->plan = 'pro'; - $company->plan_term = 'year'; - $company->plan_started = $account->pro_plan_paid; - $company->plan_paid = $account->pro_plan_paid; - - if ($company->plan_paid != NINJA_DATE) { - $expires = DateTime::createFromFormat('Y-m-d', $account->pro_plan_paid); - $expires->modify('+1 year'); - $company->plan_expires = $expires->format('Y-m-d'); - } - } - - if ($account->pro_plan_trial && $account->pro_plan_trial != '0000-00-00') { - $company->trial_started = $account->pro_plan_trial; - $company->trial_plan = 'pro'; - } - - $company->save(); - - $account->company_id = $company->id; - $account->save(); + + $single_account_ids = \DB::table('users') + ->leftJoin('user_accounts', function ($join) { + $join->on('user_Accounts.user_id1', '=', 'users.id'); + $join->orOn('user_Accounts.user_id2', '=', 'users.id'); + $join->orOn('user_Accounts.user_id3', '=', 'users.id'); + $join->orOn('user_Accounts.user_id4', '=', 'users.id'); + $join->orOn('user_Accounts.user_id5', '=', 'users.id'); + }) + ->whereNull('user_accounts.id') + ->where(function ($query) { + $query->whereNull('users.public_id'); + $query->orWhere('users.public_id', '=', 0); + }) + ->lists('users.account_id'); + + $group_accounts = \DB::select( + 'SELECT u1.account_id as account1, u2.account_id as account2, u3.account_id as account3, u4.account_id as account4, u5.account_id as account5 FROM `user_accounts` +LEFT JOIN users u1 ON (u1.public_id IS NULL OR u1.public_id = 0) AND user_accounts.user_id1 = u1.id +LEFT JOIN users u2 ON (u2.public_id IS NULL OR u2.public_id = 0) AND user_accounts.user_id2 = u2.id +LEFT JOIN users u3 ON (u3.public_id IS NULL OR u3.public_id = 0) AND user_accounts.user_id3 = u3.id +LEFT JOIN users u4 ON (u4.public_id IS NULL OR u4.public_id = 0) AND user_accounts.user_id4 = u4.id +LEFT JOIN users u5 ON (u5.public_id IS NULL OR u5.public_id = 0) AND user_accounts.user_id5 = u5.id'); + + foreach (Account::find($single_account_ids) as $account) { + $this->upAccounts($account); } - /*Schema::table('accounts', function($table) + foreach ($group_accounts as $group_account) { + $this->upAccounts(null, Account::find(get_object_vars($group_account))); + } + + Schema::table('accounts', function($table) { $table->dropColumn('pro_plan_paid'); $table->dropColumn('pro_plan_trial'); - });*/ + }); } + + private function upAccounts($primaryAccount, $otherAccounts = array()) { + if(!$primaryAccount) { + $primaryAccount = $otherAccounts->first(); + } + + $company = Company::create(); + if ($primaryAccount->pro_plan_paid && $primaryAccount->pro_plan_paid != '0000-00-00') { + $company->plan = 'pro'; + $company->plan_term = 'year'; + $company->plan_started = $primaryAccount->pro_plan_paid; + $company->plan_paid = $primaryAccount->pro_plan_paid; + + if ($company->plan_paid != NINJA_DATE) { + $expires = DateTime::createFromFormat('Y-m-d', $primaryAccount->pro_plan_paid); + $expires->modify('+1 year'); + $company->plan_expires = $expires->format('Y-m-d'); + } + } + + if ($primaryAccount->pro_plan_trial && $primaryAccount->pro_plan_trial != '0000-00-00') { + $company->trial_started = $primaryAccount->pro_plan_trial; + $company->trial_plan = 'pro'; + } + + $company->save(); + + $primaryAccount->company_id = $company->id; + $primaryAccount->save(); + + if (!empty($otherAccounts)) { + foreach ($otherAccounts as $account) { + if ($account && $account->id != $primaryAccount->id) { + $account->company_id = $company->id; + $account->save(); + } + } + } + } + /** * Reverse the migrations. * @@ -86,11 +132,11 @@ class EnterprisePlan extends Migration */ public function down() { - /*Schema::table('accounts', function($table) + Schema::table('accounts', function($table) { $table->date('pro_plan_paid')->nullable(); $table->date('pro_plan_trial')->nullable(); - });*/ + }); foreach (Company::all() as $company) { foreach ($company->accounts as $account) { @@ -106,6 +152,6 @@ class EnterprisePlan extends Migration $table->dropColumn('company_id'); }); - Schema::drop('companies'); + Schema::dropIfExists('companies'); } } \ No newline at end of file