osb/database/migrations/2021_12_20_225017_optimize_product.php
2022-02-02 10:43:59 +11:00

296 lines
10 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()
{
/*
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');
});
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();
if (! $so->exists) {
$so->name = $name;
$so->address1 = '...';
$so->city = '...';
$so->state = '...';
$so->postcode = '...';
}
$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']);
});
*/
abort(500,'here');
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//abort(500,'Cant go back');
}
}