Updates to charges table

This commit is contained in:
Deon George 2022-06-11 17:39:25 +10:00
parent b9ec64fd4f
commit b8f85960aa
3 changed files with 451 additions and 4 deletions

View File

@ -5,6 +5,8 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
use App\Traits\SiteID;
/**
* CLEANUP NOTES:
* + Charge Date should not be null
@ -13,11 +15,15 @@ use Illuminate\Support\Arr;
*/
class Charge extends Model
{
const CREATED_AT = 'date_orig';
const UPDATED_AT = 'date_last';
use SiteID;
protected $dates = ['charge_date'];
public $dateFormat = 'U';
protected $casts = [
'attributes' => 'json',
];
protected $dates = [
'charged_at',
];
public const sweep = [
// 0 => 'Daily',
@ -36,6 +42,11 @@ class Charge extends Model
return $this->belongsTo(Account::class);
}
public function product()
{
return $this->belongsTo(Product::class);
}
public function service()
{
return $this->belongsTo(Service::class);

View File

@ -38,6 +38,7 @@ class InvoiceItem extends Model
6 => 'Service Cancellation', // * Must have corresponding SERVICE_ID
7 => 'Extra Product/Service Charge', // * Service Billing in advance, Must have corresponding SERVICE_ID
8 => 'Product Addition', // * Additional Product Customisation, Must have corresponding SERVICE_ID
9 => 'Usage Charge',
120 => 'Credit/Debit Transfer', // * SERVICE_ID is NULL, MODULE_ID is NULL, MODULE_REF is NULL : INVOICE_ID is NOT NULL
123 => 'Shipping',
124 => 'Late Payment Fee', // * SERVICE_ID is NULL, MODULE_ID = CHECKOUT MODULE,

View File

@ -0,0 +1,435 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
/*
*/
DB::statement('UPDATE charges SET type=126 where type is null');
DB::statement('ALTER TABLE charges MODIFY account_id int unsigned NOT NULL');
DB::statement('ALTER TABLE charges MODIFY product_id int unsigned DEFAULT NULL');
DB::statement('ALTER TABLE charges MODIFY service_id int unsigned DEFAULT NULL');
DB::statement('ALTER TABLE charges MODIFY sweep_type int unsigned NOT NULL');
DB::statement('ALTER TABLE charges MODIFY type int unsigned NOT NULL');
DB::statement('ALTER TABLE charges MODIFY active tinyint(1) NOT NULL,MODIFY processed tinyint(1) DEFAULT NULL,MODIFY taxable tinyint(1) NOT NULL');
Schema::table('charges', function (Blueprint $table) {
$table->datetime('created_at')->nullable()->after('id');
$table->datetime('updated_at')->nullable()->after('created_at');
$table->date('start_at')->nullable();
$table->date('stop_at')->nullable();
$table->date('charge_at')->nullable();
});
// Convert out dates
foreach (\App\Models\Charge::withoutGlobalScope(\App\Models\Scopes\SiteScope::class)->cursor() as $o) {
// If we are running again
if ($o->created_at)
continue;
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->charge_date)
$o->charge_at = \Carbon\Carbon::createFromTimestamp($o->charge_date);
if ($o->getRawOriginal('attributes')) {
Config::set('site',$o->site);
// Uncompress if it is
try {
$attributes = gzuncompress($o->getRawOriginal('attributes'));
// Already uncompressed
} catch (Exception $e) {
$attributes = $o->getRawOriginal('attributes');
}
if (str_starts_with($attributes,'a:')) {
$attributes = join("\n", unserialize($attributes));
} elseif (str_starts_with($attributes,'s:')) {
$attributes = unserialize($attributes);
}
$attributes = array_filter(explode("\n",str_replace("\r",'',$attributes)));
/*
$newattrs =[];
foreach ($attributes as $attr) {
if (str_starts_with($attr,'Period Start==')) {
$date = str_replace('Period Start==', '', $attr);
$o->start_at = \Carbon\Carbon::createFromFormat('d-M-Y', $date);
} elseif (str_starts_with($attr,'Period From:')) {
$date = str_replace('Period From:','',$attr);
$o->start_at = \Carbon\Carbon::createFromFormat('Y-m-d',$date);
} elseif (str_starts_with($attr,'Period End==')) {
$date = str_replace('Period End==','',$attr);
$o->stop_at = \Carbon\Carbon::createFromFormat('d-M-Y',$date);
} elseif (str_starts_with($attr,'Period To:')) {
$date = str_replace('Period To:','',$attr);
$o->stop_at = \Carbon\Carbon::createFromFormat('Y-m-d',$date);
} elseif (str_starts_with($attr,'Connect Date: ')) {
$date = str_replace('Connect Date: ','',$attr);
$o->start_at = \Carbon\Carbon::createFromFormat('Y-m-d',$date);
} elseif (str_starts_with($attr,'Connect:')) {
$date = str_replace('Connect:','',$attr);
$o->start_at = \Carbon\Carbon::createFromFormat('Y-m-d',$date);
} elseif (str_starts_with($attr,'Effective==')) {
$date = str_replace('Effective==','',$attr);
$o->start_at = \Carbon\Carbon::createFromFormat('d-M-Y',$date);
} elseif (str_starts_with($attr,'Effective: ')) {
$date = str_replace('Effective: ','',$attr);
$o->start_at = \Carbon\Carbon::createFromFormat('Y-m-d',$date);
} elseif (str_starts_with($attr,'Effective:')) {
$date = str_replace('Effective:','',$attr);
$o->start_at = \Carbon\Carbon::createFromFormat('Y-m-d',$date);
} elseif (str_starts_with($attr,'Start:')) {
$date = str_replace('Start:','',$attr);
$o->start_at = \Carbon\Carbon::createFromFormat('Y-m-d',$date);
} elseif (str_starts_with($attr,'End:')) {
$date = str_replace('End:','',$attr);
$o->stop_at = \Carbon\Carbon::createFromFormat('Y-m-d',$date);
} elseif (str_starts_with($attr, 'Service Upgraded==')) {
$string = str_replace('Service Upgraded==','',$attr);
array_push($newattrs,['upgrade_to'=>$string]);
dump([$o->id=>sprintf('Removing description: [%s] with (Service Upgrade)',$o->description)]);
$o->description = 'Service Upgrade';
if ($o->type == 126)
$o->type = 7;
} elseif (str_starts_with($attr, 'Upgrade From==')) {
$string = str_replace('Upgrade From==','',$attr);
array_push($newattrs,['upgrade_from'=>$string]);
if ($o->type == 126)
$o->type = 7;
} elseif (str_starts_with($attr, 'Upgrade To==')) {
$string = str_replace('Upgrade To==','',$attr);
array_push($newattrs,['upgrade_to'=>$string]);
dump([$o->id=>sprintf('Removing description: [%s] with (Service Upgrade)',$o->description)]);
$o->description = 'Service Upgrade';
if ($o->type == 126)
$o->type = 7;
} elseif (str_starts_with($attr, 'Service Terminated==')) {
$date = str_replace('Service Terminated==','',$attr);
$o->start_at = \Carbon\Carbon::createFromFormat('d-M-Y',$date);
} elseif (str_starts_with($attr, 'Contact Date=')) {
$date = str_replace('Contact Date=','',$attr);
$o->stop_at = \Carbon\Carbon::createFromFormat('d-M-Y',$date);
if ($o->description == 'ADSL Service Cancellation In Con')
$o->description = 'Service Terminated In Contract';
if ($o->type == 126)
$o->type = 6;
} elseif (str_starts_with($attr, 'Actual Cancel:')) {
$date = str_replace('Actual Cancel:','',$attr);
$o->start_at = \Carbon\Carbon::createFromFormat('Y-m-d',$date);
} elseif (str_starts_with($attr, 'Contact to:')) {
$date = str_replace('Contact to:','',$attr);
$o->stop_at = \Carbon\Carbon::createFromFormat('Y-m-d',$date);
if ($o->description == 'ADSL Contract Cancellation')
$o->description = 'Service Terminated In Contract';
if ($o->type == 126)
$o->type = 6;
} elseif (str_starts_with($attr, 'Service Number=')) {
$string = str_replace('Service Number=','',$attr);
if (! str_replace(' ','',$string) == $o->service->name_short)
dd(['number'=>str_replace(' ','',$string),'service'=>$o->service->name_short]);
} elseif (str_starts_with($attr, 'Service==')) {
$string = str_replace('Service==','',$attr);
if (! str_replace(' ','',$string) == $o->service->name_short)
dd(['number'=>str_replace(' ','',$string),'service'=>$o->service->name_short]);
} elseif (str_starts_with($attr, 'ADSL Service==')) {
$string = str_replace('ADSL Service==','',$attr);
if (! str_replace(' ','',$string) == $o->service->name_short)
dd(['number'=>str_replace(' ','',$string),'service'=>$o->service->name_short]);
} elseif (str_starts_with($attr, 'Service:')) {
$string = str_replace('Service','',$attr);
if (! str_replace(' ','',$string) == $o->service->name_short)
dd(['number'=>str_replace(' ','',$string),'service'=>$o->service->name_short]);
} elseif (str_starts_with($attr, 'Traffic==')) {
$string = str_replace('Traffic==','',$attr);
array_push($newattrs,['traffic'=>$string]);
if ($o->type == 126)
$o->type = 9;
} elseif (str_starts_with($attr, 'Service Activation==')) {
$date = str_replace('Service Activation==','',$attr);
$o->start_at = \Carbon\Carbon::createFromFormat('Y-m-d',$date);
} elseif (str_starts_with($attr, 'Relocation:')) {
$date = str_replace('Relocation:','',$attr);
$o->start_at = \Carbon\Carbon::createFromFormat('Y-m-d',$date);
} elseif (str_starts_with($attr, 'Date:')) {
$date = str_replace('Date:','',$attr);
$o->start_at = \Carbon\Carbon::createFromFormat('Y-m-d',$date);
} elseif (str_starts_with($attr, 'Old Service==')) {
$string = str_replace('Old Service==','',$attr);
array_push($newattrs,['old_service'=>$string]);
} elseif (str_starts_with($attr, 'Traffic From==')) {
$string = str_replace('Traffic From==','',$attr);
array_push($newattrs,['traffic_from'=>$string]);
if ($o->type == 126)
$o->type = 9;
} elseif (str_starts_with($attr, 'Traffic To==')) {
$string = str_replace('Traffic To==','',$attr);
array_push($newattrs,['traffic_to'=>$string]);
if ($o->type == 126)
$o->type = 9;
} elseif (str_starts_with($attr, 'Amount==')) {
$string = str_replace('Amount==','',$attr);
array_push($newattrs,['actual'=>$string]);
} elseif (str_starts_with($attr, 'Actual==')) {
$string = str_replace('Actual==','',$attr);
array_push($newattrs,['actual'=>$string]);
} elseif (str_starts_with($attr, 'Allowance==')) {
$string = str_replace('Allowance==','',$attr);
array_push($newattrs,['allowance'=>$string]);
} elseif (str_starts_with($attr, 'Allowance:')) {
$string = str_replace('Allowance:','',$attr);
array_push($newattrs,['allowance'=>$string]);
} elseif (str_starts_with($attr, 'Old Service==')) {
$string = str_replace('Old Service==','',$attr);
array_push($newattrs,['old'=>$string]);
} elseif (str_starts_with($attr, 'New Service==')) {
$string = str_replace('New Service==','',$attr);
array_push($newattrs,['new'=>$string]);
} elseif (str_starts_with($attr, 'Change From==')) {
$date = str_replace('Change From==','',$attr);
$o->start_at = \Carbon\Carbon::createFromFormat('Y-m-d',$date);
} elseif (str_starts_with($attr, 'Plan Change From==')) {
$date = str_replace('Plan Change From==','',$attr);
$o->start_at = \Carbon\Carbon::createFromFormat('Y-m-d',$date);
} elseif (str_starts_with($attr, 'Changed From==')) {
$date = str_replace('Changed From==','',$attr);
$o->start_at = \Carbon\Carbon::createFromFormat('Y-m-d',$date);
} elseif (str_starts_with($attr, 'From:')) {
$date = str_replace('From:','',$attr);
try {
$o->start_at = \Carbon\Carbon::createFromFormat('Y-m-d',$date);
} catch (Exception $e) {}
} elseif (str_starts_with($attr,'To:')) {
$date = str_replace('To:','',$attr);
try {
$o->stop_at = \Carbon\Carbon::createFromFormat('Y-m-d',$date);
} catch (Exception $e) {}
} elseif (str_starts_with($attr, 'Connection Date==')) {
$date = str_replace('Connection Date==','',$attr);
$o->start_at = \Carbon\Carbon::createFromFormat('Y-m-d',$date);
} elseif (str_starts_with($attr, 'Connection: ')) {
$date = str_replace('Connection: ','',$attr);
$o->start_at = \Carbon\Carbon::createFromFormat('Y-m-d',$date);
} elseif (str_starts_with($attr, 'Connection:')) {
$date = str_replace('Connection:','',$attr);
$o->start_at = \Carbon\Carbon::createFromFormat('Y-m-d',$date);
} elseif (str_starts_with($attr, 'Effective Date==')) {
$date = str_replace('Effective Date==','',$attr);
$o->start_at = \Carbon\Carbon::createFromFormat('Y-m-d',$date);
} elseif (str_starts_with($attr, 'Plan Change:')) {
$date = str_replace('Plan Change:','',$attr);
$o->start_at = \Carbon\Carbon::createFromFormat('Y-m-d',$date);
} elseif (str_starts_with($attr, 'Old Plan==')) {
$string = str_replace('Old Plan==','',$attr);
array_push($newattrs,['plan_old'=>$string]);
} elseif (str_starts_with($attr, 'Old Plan:')) {
$string = str_replace('Old Plan:','',$attr);
array_push($newattrs,['plan_old'=>$string]);
} elseif (str_starts_with($attr, 'New Plan==')) {
$string = str_replace('New Plan==','',$attr);
array_push($newattrs,['plan_new'=>$string]);
} elseif (str_starts_with($attr, 'New Plan:')) {
$string = str_replace('New Plan:','',$attr);
array_push($newattrs,['plan_new'=>$string]);
} elseif (str_starts_with($attr, 'Total Download Peak==')) {
$string = str_replace('Total Download Peak==','',$attr);
array_push($newattrs,['download_peak_total'=>$string]);
} elseif (str_starts_with($attr, 'Allowance Download Peak==')) {
$string = str_replace('Allowance Download Peak==','',$attr);
array_push($newattrs,['download_peak_allow'=>$string]);
if ($o->type == 126)
$o->type = 9;
} elseif (str_starts_with($attr, 'Old Speed==')) {
$string = str_replace('Old Speed==','',$attr);
array_push($newattrs,['speed_old'=>$string]);
} elseif (str_starts_with($attr, 'OldSpeed:')) {
$string = str_replace('OldSpeed:','',$attr);
array_push($newattrs,['speed_old'=>$string]);
} elseif (str_starts_with($attr, 'New Speed==')) {
$string = str_replace('New Speed==','',$attr);
array_push($newattrs,['speed_new'=>$string]);
} elseif (str_starts_with($attr, 'NewSpeed:')) {
$string = str_replace('NewSpeed:','',$attr);
array_push($newattrs,['speed_new'=>$string]);
} elseif (str_starts_with($attr, 'Used==')) {
$string = str_replace('Used==','',$attr);
array_push($newattrs,['used'=>$string]);
} elseif (str_starts_with($attr, 'Used=')) {
$string = str_replace('Used=','',$attr);
array_push($newattrs,['used'=>$string]);
} elseif (str_starts_with($attr, 'Month Ending==')) {
$string = str_replace('Month Ending==','',$attr);
array_push($newattrs,['month'=>$string]);
} elseif (str_starts_with($attr, 'Month==')) {
$string = str_replace('Month==','',$attr);
array_push($newattrs,['month'=>$string]);
} elseif (str_starts_with($attr, 'Old Peak Allowance==')) {
$string = str_replace('Old Peak Allowance==','',$attr);
array_push($newattrs,['allowance_peak_old'=>$string]);
} elseif (str_starts_with($attr, 'Old Allowance==')) {
$string = str_replace('Old Allowance==','',$attr);
array_push($newattrs,['allowance_old'=>$string]);
} elseif (str_starts_with($attr, 'New Peak Allowance==')) {
$string = str_replace('New Peak Allowance==','',$attr);
array_push($newattrs,['allowance_peak_new'=>$string]);
} elseif (str_starts_with($attr, 'New Allowance==')) {
$string = str_replace('New Allowance==','',$attr);
array_push($newattrs,['allowance_new'=>$string]);
} elseif (str_starts_with($attr, 'Invoice==')) {
$string = str_replace('Invoice==','',$attr);
array_push($newattrs,['invoice'=>$string]);
} elseif (str_starts_with($attr, 'Connection Fee==')) {
$string = str_replace('Connection Fee==','',$attr);
array_push($newattrs,['fee'=>$string]);
} elseif (str_starts_with($attr, 'Graytech Contribution==')) {
$string = str_replace('Graytech Contribution==','',$attr);
array_push($newattrs,['discount'=>$string]);
} elseif (str_starts_with($attr, 'Metric==')) {
$string = str_replace('Metric==','',$attr);
array_push($newattrs,['metric'=>$string]);
} elseif (str_starts_with($attr, 'Metric:')) {
$string = str_replace('Metric:','',$attr);
array_push($newattrs,['metric'=>$string]);
} elseif (str_starts_with($attr, 'Month:')) {
$string = str_replace('Month:','',$attr);
array_push($newattrs,['month'=>$string]);
} elseif (str_starts_with($attr, 'Used:')) {
$string = str_replace('Used:','',$attr);
array_push($newattrs,['actual'=>$string]);
} elseif (str_starts_with($attr, 'Ref ')) {
$string = str_replace('Ref ','',$attr);
array_push($newattrs,['ref'=>$string]);
} elseif (
(str_starts_with($attr, 'New Monthly Plan=='))
|| (str_starts_with($attr, 'Monthly Payment=='))
|| (str_starts_with($attr, 'Old Quarterly=='))
|| (str_starts_with($attr, 'New Quarterly=='))
|| (str_starts_with($attr, 'Old Fee=='))
|| (str_starts_with($attr, 'New Fee=='))
|| (str_starts_with($attr, 'Old Rate:'))
|| (str_starts_with($attr, 'New Rate:'))
|| (str_starts_with($attr, 'Site=='))
|| (str_starts_with($attr, 'Invoiced Already:'))
|| (str_starts_with($attr, 'Balance:'))
|| (str_starts_with($attr, 'Contract=='))) {
// Nothing
} else {
dd(['attr'=>$attr,'o'=>$o->getAttributes(),'service'=>$o->service->product->name,'product'=>$o->product_id ? $o->product->name : 'NONE']);
}
*/
}
$o->attributes = $attributes;
$o->save();
}
Schema::table('charges', function (Blueprint $table) {
$table->dropColumn(['date_orig','date_last','charge_date']);
$table->dropUnique('charges_id_account_id_site_id_unique');
$table->foreign(['account_id','site_id'])->references(['id','site_id'])->on('accounts');
$table->foreign(['service_id','site_id'])->references(['id','site_id'])->on('services');
$table->foreign(['product_id','site_id'])->references(['id','site_id'])->on('products');
$table->foreign(['user_id','site_id'])->references(['id','site_id'])->on('users');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
abort(500,'cant go back');
}
};