osb/database/migrations/2021_12_20_225017_optimize_product.php
2022-04-20 12:43:10 +10:00

633 lines
22 KiB
PHP

<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class OptimizeProduct extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
$site = \App\Models\Site::where('site_id',1)->firstOrNew();
Schema::table('ab_product', function (Blueprint $table) {
$table->dropForeign(['site_id']);
$table->dropIndex(['id','site_id']);
$table->dropIndex('ab_product_site_id_foreign');
});
DB::statement('ALTER TABLE ab_product RENAME TO products');
Schema::table('products', function (Blueprint $table) {
$table->dropColumn(['cart_multiple']);
$table->index(['id','site_id']);
$table->foreign(['site_id'])->references(['id'])->on('sites');
$table->dateTime('created_at')->nullable()->after('id');
$table->dateTime('updated_at')->nullable()->after('created_at');
});
DB::statement('ALTER TABLE suppliers MODIFY address1 varchar(255),MODIFY city varchar(255),MODIFY state varchar(255),MODIFY postcode varchar(255)');
Schema::table('supplier_details', function (Blueprint $table) {
$table->jsonb('connections')->nullable();
});
foreach (\Illuminate\Support\Facades\DB::select('SELECT * FROM ab_adsl_supplier') as $o) {
switch($o->name) {
case 'PeopleAgent':
$type = 'broadband';
$name = 'People Telecom';
break;
case 'iiNetADSL':
$type = 'broadband';
$name = 'iiNet';
break;
case 'ExetelVisp':
$type = 'broadband';
$name = 'Exetel';
break;
case 'ExetelHSPA':
$type = 'hspa';
$name = 'Exetel';
break;
case 'ExetelPE':
$type = 'ethernet';
$name = 'Exetel';
break;
default:
throw new Exception('Unknown Supplier: '.$o->name);
}
$so = \App\Models\Supplier::where('name',$name)->singleOrNew();
$so->name = $name;
$so->active = $so->active ?: $o->active;
$so->save();
$sdo = \App\Models\SupplierDetail::withoutGlobalScope(\App\Models\Scopes\SiteScope::class)
->where('supplier_id',$so->id)
->where('site_id',$o->site_id)
->firstOrNew();
if (! $sdo->exists) {
$sdo->site_id = $o->site_id;
}
$connections = $sdo->connections ?: collect();
$connections->put($type,[
'user'=>$o->stats_username,
'pass'=>$o->stats_password,
'last'=>$o->stats_lastupdate,
'url'=>$o->stats_url,
]);
$sdo->connections = $connections;
$so->detail()->save($sdo);
};
Schema::table('ab_adsl_supplier_plan', function (Blueprint $table) {
$table->dropForeign(['site_id']);
$table->dropIndex(['id','site_id']);
$table->dropIndex('ab_adsl_supplier_plan_site_id_foreign');
});
DB::statement('ALTER TABLE ab_adsl_supplier_plan RENAME TO supplier_broadband');
DB::statement('ALTER TABLE supplier_broadband MODIFY product_id varchar(16) NOT NULL');
DB::statement('ALTER TABLE supplier_broadband MODIFY base_cost double NOT NULL');
DB::statement('ALTER TABLE supplier_broadband MODIFY active tinyint(1)');
DB::statement('ALTER TABLE supplier_broadband RENAME COLUMN supplier_id TO old_supplier_id');
DB::statement('ALTER TABLE supplier_broadband RENAME COLUMN offpeak_start TO old_offpeak_start');
DB::statement('ALTER TABLE supplier_broadband RENAME COLUMN offpeak_end TO old_offpeak_end');
Schema::table('supplier_broadband', function (Blueprint $table) {
$table->index(['id','site_id']);
$table->foreign(['site_id'])->references(['id'])->on('sites');
$table->dateTime('created_at')->nullable()->after('id');
$table->dateTime('updated_at')->nullable()->after('created_at');
$table->time('offpeak_start')->nullable()->after('old_offpeak_end');
$table->time('offpeak_end')->nullable()->after('offpeak_start');
});
Schema::table('supplier_broadband', function (Blueprint $table) {
$table->integer('supplier_detail_id')->unsigned()->nullable()->after('old_supplier_id');
$table->foreign(['supplier_detail_id','site_id'])->references(['id','site_id'])->on('supplier_details');
});
\Illuminate\Support\Facades\DB::select("UPDATE ab_service SET model='App\\\\Models\\\\Service\\\\Broadband' where model='App\\\\Models\\\\Service\\\\Adsl'");
\Illuminate\Support\Facades\DB::select("UPDATE products SET model='App\\\\Models\\\\Product\\\\Broadband' where model='App\\\\Models\\\\Product\\\\Adsl'");
// Convert to use the new supplier
foreach (\Illuminate\Support\Facades\DB::select('SELECT * FROM ab_adsl_supplier') as $o) {
switch ($o->name) {
case 'PeopleAgent':
$so = \App\Models\Supplier::where('name','People Telecom')->singleOrFail();
break;
case 'iiNetADSL':
$so = \App\Models\Supplier::where('name','iiNet')->singleOrFail();
break;
case 'ExetelVisp':
case 'ExetelHSPA':
case 'ExetelPE':
$so = \App\Models\Supplier::where('name','Exetel')->singleOrFail();
break;
default:
throw new Exception('Unknown Supplier: ' . $o->name);
}
$sdo = \App\Models\SupplierDetail::withoutGlobalScope(\App\Models\Scopes\SiteScope::class)
->where('supplier_id',$so->id)
->where('site_id',$o->site_id)
->singleOrFail();
\Illuminate\Support\Facades\DB::select(sprintf("UPDATE supplier_broadband SET supplier_detail_id=%d where old_supplier_id=%d and site_id=%d",$sdo->id,$o->id,$sdo->site_id));
}
// Convert out dates
foreach (\App\Models\Supplier\Broadband::withoutGlobalScope(\App\Models\Scopes\SiteScope::class)->cursor() as $o) {
if ($o->date_orig)
$o->created_at = \Carbon\Carbon::createFromTimestamp($o->date_orig);
if ($o->date_last)
$o->updated_at = \Carbon\Carbon::createFromTimestamp($o->date_last);
if ($o->old_offpeak_start)
$o->offpeak_start = \Carbon\Carbon::createFromTimestamp($o->old_offpeak_start);
if ($o->old_offpeak_end)
$o->offpeak_end = \Carbon\Carbon::createFromTimestamp($o->old_offpeak_end);
$o->save();
}
Schema::table('supplier_broadband', function (Blueprint $table) {
$table->dropPrimary();
$table->primary(['id','site_id']);
$table->dropColumn(['date_orig','date_last','old_supplier_id','old_offpeak_start','old_offpeak_end']);
});
Schema::dropIfExists('ab_adsl_supplier');
DB::statement('ALTER TABLE supplier_broadband MODIFY extra_charged tinyint(1)');
DB::statement('ALTER TABLE supplier_broadband MODIFY extra_shaped tinyint(1)');
DB::statement('ALTER TABLE supplier_broadband MODIFY contract_term int unsigned');
Schema::table('ab_adsl_plan', function (Blueprint $table) {
$table->dropForeign(['site_id']);
$table->dropIndex(['id','site_id']);
$table->dropIndex('ab_adsl_plan_site_id_foreign');
});
DB::statement('ALTER TABLE ab_adsl_plan RENAME TO product_broadband');
DB::statement('ALTER TABLE product_broadband DROP PRIMARY KEY,ADD PRIMARY KEY (id,site_id)');
DB::statement('ALTER TABLE product_broadband MODIFY extra_charged tinyint(1)');
DB::statement('ALTER TABLE product_broadband MODIFY extra_shaped tinyint(1)');
DB::statement('ALTER TABLE product_broadband MODIFY contract_term int unsigned');
DB::statement('ALTER TABLE product_broadband RENAME COLUMN adsl_supplier_plan_id TO supplier_broadband_id');
DB::statement('ALTER TABLE product_broadband MODIFY supplier_broadband_id int unsigned');
Schema::table('product_broadband', function (Blueprint $table) {
$table->index(['id','site_id']);
$table->foreign(['site_id'])->references(['id'])->on('sites');
$table->foreign(['supplier_broadband_id','site_id'])->references(['id','site_id'])->on('supplier_broadband');
$table->dateTime('created_at')->nullable()->after('id');
$table->dateTime('updated_at')->nullable()->after('created_at');
});
// Convert product pricegroups
foreach (\App\Models\Product::withoutGlobalScope(\App\Models\Scopes\SiteScope::class)->cursor() as $po) {
if ($po->date_orig)
$po->created_at = \Carbon\Carbon::createFromTimestamp($po->date_orig);
if ($po->date_last)
$po->updated_at = \Carbon\Carbon::createFromTimestamp($po->date_last);
if (! ($po instanceof \Illuminate\Support\Collection) || ! $po->price_group->count()) {
$original = $po->getRawOriginal('price_group');
// serialized
if (preg_match('/^a:/',$original)) {
try {
$price_group = collect(unserialize(str_replace("\n","",$original)));
} catch (Exception $e) {
dd(['error'=>$e->getMessage(),'raw'=>$po->getRawOriginal('price_group')]);
}
} elseif (is_null($po->getRawOriginal('price_group'))) {
$price_group = collect();
} else {
try {
$price_group = unserialize(gzuncompress($po->getRawOriginal('price_group')));
} catch (Exception $e) {
dd(['error'=>$e->getMessage(),'raw'=>$po->getRawOriginal('price_group')]);
}
}
$new_price_group = collect();
// Remove any blank entries, or when base/setup = 0
foreach ($price_group as $group => $values) {
$build = collect();
foreach ($values as $key => $pricing) {
switch ($key) {
case 'show':
$build->put('show',(bool) $pricing);
break;
default:
// key is a time period
if ((! Arr::get($pricing,'price_base')) && (! Arr::get($pricing,'price_setup')))
break;
$build->put($key,[
'base'=>Arr::get($pricing,'price_base'),
'setup'=>Arr::get($pricing,'price_setup'),
]);
}
}
$new_price_group->put($group,$build);
}
$po->price_group = $new_price_group;
}
$po->save();
}
DB::statement('ALTER TABLE products MODIFY taxable tinyint(1),MODIFY active tinyint(1),MODIFY price_recurr_strict tinyint(1),MODIFY prod_plugin_data int unsigned');
DB::statement('ALTER TABLE products RENAME COLUMN price_group TO pricing');
DB::statement('ALTER TABLE products RENAME COLUMN price_recurr_default TO price_recur_default');
DB::statement('ALTER TABLE products RENAME COLUMN price_recurr_strict TO price_recur_strict');
DB::statement('ALTER TABLE products RENAME COLUMN prod_plugin_data TO model_id');
Schema::table('products', function (Blueprint $table) {
$table->dropColumn(['date_orig','date_last','group_avail','avail_category','price_recurr_day','price_recurr_weekday','prod_plugin_file']);
});
Schema::dropIfExists('ab_voip_plan');
Schema::create('tlds',function (Blueprint $table) {
$table->integer('id',TRUE)->unsigned();
$table->timestamps();
$table->string('name',64)->unique();
});
Schema::create('supplier_domain',function (Blueprint $table) {
$table->integer('id',TRUE)->unsigned();
$table->timestamps();
$table->integer('site_id')->unsigned();
$table->boolean('active');
$table->integer('supplier_detail_id')->unsigned();
$table->string('product_id',16);
$table->string('product_desc',128)->nullable();
$table->float('base_cost');
$table->float('setup_cost')->nullable();
$table->integer('contract_term')->unsigned()->nullable();
$table->integer('tld_id')->unsigned();
$table->text('whois_url')->nullable();
$table->text('config')->nullable();
$table->index(['id','site_id']);
$table->foreign(['site_id'])->references(['id'])->on('sites');
$table->foreign(['tld_id'])->references(['id'])->on('tlds');
$table->foreign(['supplier_detail_id','site_id'])->references(['id','site_id'])->on('supplier_details');
});
Schema::create('supplier_host',function (Blueprint $table) {
$table->integer('id',TRUE)->unsigned();
$table->timestamps();
$table->integer('site_id')->unsigned();
$table->boolean('active');
$table->integer('supplier_detail_id')->unsigned();
$table->string('product_id',16);
$table->string('product_desc',128)->nullable();
$table->float('base_cost');
$table->float('setup_cost')->nullable();
$table->integer('contract_term')->unsigned()->nullable();
$table->index(['id','site_id']);
$table->foreign(['site_id'])->references(['id'])->on('sites');
$table->foreign(['supplier_detail_id','site_id'])->references(['id','site_id'])->on('supplier_details');
});
Schema::create('supplier_ssl',function (Blueprint $table) {
$table->integer('id',TRUE)->unsigned();
$table->timestamps();
$table->integer('site_id')->unsigned();
$table->boolean('active');
$table->integer('supplier_detail_id')->unsigned();
$table->string('product_id',16);
$table->string('product_desc',128)->nullable();
$table->float('base_cost');
$table->float('setup_cost')->nullable();
$table->integer('contract_term')->unsigned()->nullable();
$table->index(['id','site_id']);
$table->foreign(['site_id'])->references(['id'])->on('sites');
$table->foreign(['supplier_detail_id','site_id'])->references(['id','site_id'])->on('supplier_details');
});
Schema::create('supplier_generic',function (Blueprint $table) {
$table->integer('id',TRUE)->unsigned();
$table->timestamps();
$table->integer('site_id')->unsigned();
$table->boolean('active');
$table->integer('supplier_detail_id')->unsigned();
$table->string('product_id',16);
$table->string('product_desc',128)->nullable();
$table->float('base_cost');
$table->float('setup_cost')->nullable();
$table->integer('contract_term')->unsigned()->nullable();
$table->index(['id','site_id']);
$table->foreign(['site_id'])->references(['id'])->on('sites');
$table->foreign(['supplier_detail_id','site_id'])->references(['id','site_id'])->on('supplier_details');
});
Schema::create('supplier_voip',function (Blueprint $table) {
$table->integer('id',TRUE)->unsigned();
$table->timestamps();
$table->integer('site_id')->unsigned();
$table->boolean('active');
$table->integer('supplier_detail_id')->unsigned();
$table->string('product_id',16);
$table->string('product_desc',128)->nullable();
$table->float('base_cost');
$table->float('setup_cost')->nullable();
$table->integer('contract_term')->unsigned()->nullable();
$table->index(['id','site_id']);
$table->foreign(['site_id'])->references(['id'])->on('sites');
$table->foreign(['supplier_detail_id','site_id'])->references(['id','site_id'])->on('supplier_details');
});
Schema::create('product_domain',function (Blueprint $table) {
$table->integer('id',TRUE)->unsigned();
$table->timestamps();
$table->integer('site_id')->unsigned();
$table->integer('supplier_domain_id')->unsigned();
$table->string('name',64);
$table->integer('contract_term')->unsigned()->nullable();
$table->index(['id','site_id']);
$table->foreign(['site_id'])->references(['id'])->on('sites');
$table->foreign(['supplier_domain_id','site_id'])->references(['id','site_id'])->on('supplier_domain');
});
Schema::create('product_host',function (Blueprint $table) {
$table->integer('id',TRUE)->unsigned();
$table->timestamps();
$table->integer('site_id')->unsigned();
$table->integer('supplier_host_id')->unsigned();
$table->string('name',64);
$table->integer('contract_term')->unsigned()->nullable();
$table->index(['id','site_id']);
$table->foreign(['site_id'])->references(['id'])->on('sites');
$table->foreign(['supplier_host_id','site_id'])->references(['id','site_id'])->on('supplier_host');
});
Schema::create('product_ssl',function (Blueprint $table) {
$table->integer('id',TRUE)->unsigned();
$table->timestamps();
$table->integer('site_id')->unsigned();
$table->integer('supplier_ssl_id')->unsigned();
$table->string('name',64);
$table->integer('contract_term')->unsigned()->nullable();
$table->index(['id','site_id']);
$table->foreign(['site_id'])->references(['id'])->on('sites');
$table->foreign(['supplier_ssl_id','site_id'])->references(['id','site_id'])->on('supplier_ssl');
});
Schema::create('product_generic',function (Blueprint $table) {
$table->integer('id',TRUE)->unsigned();
$table->timestamps();
$table->integer('site_id')->unsigned();
$table->integer('supplier_generic_id')->unsigned();
$table->string('name',64);
$table->integer('contract_term')->unsigned()->nullable();
$table->index(['id','site_id']);
$table->foreign(['site_id'])->references(['id'])->on('sites');
$table->foreign(['supplier_generic_id','site_id'])->references(['id','site_id'])->on('supplier_generic');
});
Schema::create('product_voip',function (Blueprint $table) {
$table->integer('id',TRUE)->unsigned();
$table->timestamps();
$table->integer('site_id')->unsigned();
$table->integer('supplier_voip_id')->unsigned();
$table->string('name',64);
$table->integer('contract_term')->unsigned()->nullable();
$table->index(['id','site_id']);
$table->foreign(['site_id'])->references(['id'])->on('sites');
$table->foreign(['supplier_voip_id','site_id'])->references(['id','site_id'])->on('supplier_voip');
});
// Setup Domains
foreach (\Illuminate\Support\Facades\DB::select('SELECT * FROM ab_domain_tld') as $o) {
$oo = new \App\Models\TLD;
if ($o->date_orig)
$oo->created_at = \Carbon\Carbon::createFromTimestamp($o->date_orig);
if ($o->date_last)
$oo->created_at = \Carbon\Carbon::createFromTimestamp($o->date_last);
$oo->name = strtolower($o->name);
$oo->save();
};
foreach (\Illuminate\Support\Facades\DB::select('SELECT * FROM ab_domain_registrar') as $o) {
$so = \App\Models\Supplier::where('name',$o->name)->singleOrNew();
$so->name = $o->name;
$so->active = $so->active ?: $o->active;
$so->save();
$sdo = \App\Models\SupplierDetail::withoutGlobalScope(\App\Models\Scopes\SiteScope::class)
->where('supplier_id',$so->id)
->where('site_id',1)
->firstOrNew();
$sdo->site_id = 1;
$connects = collect();
if ($o->whitelabel_url)
$connects->put('whitelabel_url',$o->whitelabel_url);
if ($o->manage_url)
$connects->put('manage_url',$o->manage_url);
$sdo->connections = $connects;
$so->detail()->save($sdo);
foreach (\App\Models\TLD::cursor() as $tldo) {
$sd = \App\Models\Supplier\Domain::withoutGlobalScope(\App\Models\Scopes\SiteScope::class)
->where('tld_id',$tldo->id)
->where('site_id',$sdo->site_id)
->where('supplier_detail_id',$sdo->id)
->firstOrNew();
$sd->supplier_detail_id = $sdo->id;
$sd->tld_id = $tldo->id;
$sd->site_id = $sdo->site_id;
$sd->active = TRUE;
$sd->product_id = 'Domain Name';
$sd->product_desc = 'Domain Name License';
$sd->base_cost = '.01';
$sd->contract_term = 12;
$sd->save();
$pd = \App\Models\Product\Domain::withoutGlobalScope(\App\Models\Scopes\SiteScope::class)
->where('site_id',$sdo->site_id)
->where('supplier_domain_id',$sd->id)
->firstOrNew();
$pd->supplier_domain_id = $sd->id;
$pd->site_id = $sd->site_id;
$pd->name = $sd->name;
$pd->save();
}
}
// No need to update tables, if we dont have a site setup yet.
if (! $site->exists)
return;
// SSL
$so = \App\Models\Supplier::where('name','Graytech Hosting Pty Ltd')->firstOrNew();
$so->name = 'Graytech Hosting Pty Ltd';
$so->active = TRUE;
$so->save();
$sdo = \App\Models\SupplierDetail::withoutGlobalScope(\App\Models\Scopes\SiteScope::class)
->where('site_id','1')
->where('supplier_id',$so->id)
->firstOrNew();
$sdo->site_id = $site->site_id;
$so->detail()->save($sdo);
$o = new \App\Models\Supplier\SSL;
$o->site_id = $sdo->site_id;
$o->active = TRUE;
$o->product_id = 'Graytech SSL';
$o->base_cost = 0;
$o->setup_cost = 0;
$o->supplier_detail_id = $sdo->id;
$o->save();
$oo = new \App\Models\Product\SSL;
$oo->site_id = $sdo->site_id;
$oo->supplier_ssl_id = $o->id;
$oo->name = 'Graytech SSL';
$oo->save();
$o = new \App\Models\Supplier\Host;
$o->site_id = $sdo->site_id;
$o->active = TRUE;
$o->product_id = 'Graytech Hosting';
$o->base_cost = 0;
$o->setup_cost = 0;
$o->supplier_detail_id = $sdo->id;
$o->save();
$oo = new \App\Models\Product\Host;
$oo->site_id = $sdo->site_id;
$oo->supplier_host_id = $o->id;
$oo->name = 'Graytech Hosting';
$oo->save();
$so = \App\Models\Supplier::where('name','crazydomain')->firstOrFail();
$sdo = \App\Models\SupplierDetail::withoutGlobalScope(\App\Models\Scopes\SiteScope::class)
->where('site_id','1')
->where('supplier_id',$so->id)
->firstOrNew();
$sdo->site_id = $site->site_id;
$so->detail()->save($sdo);
$o = new \App\Models\Supplier\Host;
$o->site_id = $sdo->site_id;
$o->active = TRUE;
$o->product_id = 'CD Host';
$o->base_cost = 0;
$o->supplier_detail_id = $sdo->id;
$o->save();
$oo = new \App\Models\Product\Host;
$oo->site_id = $sdo->site_id;
$oo->supplier_host_id = $o->id;
$oo->name = 'Crazy Domains Hosting';
$oo->save();
// VOIP
$so = \App\Models\Supplier::where('name','Exetel')->firstOrFail();
$sdo = \App\Models\SupplierDetail::withoutGlobalScope(\App\Models\Scopes\SiteScope::class)
->where('site_id','1')
->where('supplier_id',$so->id)
->firstOrNew();
$sdo->site_id = $site->site_id;
$so->detail()->save($sdo);
$o = new \App\Models\Supplier\Phone;
$o->site_id = $sdo->site_id;
$o->active = TRUE;
$o->product_id = 'VOIP $10';
$o->base_cost = 9.09;
$o->setup_cost = 0;
$o->supplier_detail_id = $sdo->id;
$o->save();
$oo = new \App\Models\Product\Phone;
$oo->site_id = $sdo->site_id;
$oo->supplier_voip_id = $o->id;
$oo->name = 'VOIP $10';
$oo->save();
$o = new \App\Models\Supplier\Phone;
$o->site_id = $sdo->site_id;
$o->active = TRUE;
$o->product_id = 'VOIP';
$o->base_cost = 0;
$o->setup_cost = 0;
$o->supplier_detail_id = $sdo->id;
$o->save();
$oo = new \App\Models\Product\Phone;
$oo->site_id = $sdo->site_id;
$oo->supplier_voip_id = $o->id;
$oo->name = 'VOIP';
$oo->save();
$o = new \App\Models\Supplier\Phone;
$o->site_id = $sdo->site_id;
$o->active = TRUE;
$o->product_id = 'VOIP B-100';
$o->product_desc = 'VOIP Business 100 Lines';
$o->base_cost = 22.727;
$o->setup_cost = 0;
$o->supplier_detail_id = $sdo->id;
$o->save();
$oo = new \App\Models\Product\Phone;
$oo->site_id = $sdo->site_id;
$oo->supplier_voip_id = $o->id;
$oo->name = 'VOIP B-10';
$oo->save();
// Manually run this, since we havent worked out site_id when running migrate.
// update products set model_id=49,model='App\\Models\\Product\\Domain' where id=78;
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
abort(500,'Cant go back');
}
}