Remove old database updates

This commit is contained in:
Darko Gjorgjijoski
2024-01-28 00:16:28 +01:00
parent 3721e5d651
commit 17351efacf
9 changed files with 2 additions and 666 deletions

View File

@@ -1,113 +0,0 @@
<?php
namespace InvoiceShelf\Listeners\Updates\v1;
use InvoiceShelf\Events\UpdateFinished;
use InvoiceShelf\Listeners\Updates\Listener;
use InvoiceShelf\Models\Currency;
use InvoiceShelf\Models\Setting;
class Version110 extends Listener
{
public const VERSION = '1.1.0';
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* @param object $event
* @return void
*/
public function handle(UpdateFinished $event)
{
if ($this->isListenerFired($event)) {
return;
}
// Add currencies
$this->addCurrencies();
// Update Crater app version
Setting::setSetting('version', static::VERSION);
}
private function addCurrencies()
{
$currencies = [
'13' => [
'symbol' => 'S$',
],
'16' => [
'symbol' => '₫',
],
'17' => [
'symbol' => 'Fr.',
],
'21' => [
'symbol' => '฿',
],
'22' => [
'symbol' => '₦',
],
'26' => [
'symbol' => 'HK$',
],
'35' => [
'symbol' => 'NAƒ',
],
'38' => [
'symbol' => 'GH₵',
],
'39' => [
'symbol' => 'Лв.',
],
'42' => [
'symbol' => 'RON',
],
'44' => [
'symbol' => 'SِAR',
],
'46' => [
'symbol' => 'Rf',
],
'47' => [
'symbol' => '₡',
],
'54' => [
'symbol' => '‎د.ت',
],
'55' => [
'symbol' => '₽',
],
'57' => [
'symbol' => 'ر.ع.',
],
'58' => [
'symbol' => '₴',
],
];
foreach ($currencies as $key => $currency) {
Currency::updateOrCreate(['id' => $key], $currency);
}
Currency::create([
'name' => 'Kuwaiti Dinar',
'code' => 'KWD',
'symbol' => 'KWD ',
'precision' => '3',
'thousand_separator' => ',',
'decimal_separator' => '.',
]);
}
}

View File

@@ -1,111 +0,0 @@
<?php
namespace InvoiceShelf\Listeners\Updates\v2;
use InvoiceShelf\Events\UpdateFinished;
use InvoiceShelf\Listeners\Updates\Listener;
use InvoiceShelf\Models\Setting;
use Illuminate\Database\Schema\Blueprint;
class Version200 extends Listener
{
public const VERSION = '2.0.0';
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* @param object $event
* @return void
*/
public function handle(UpdateFinished $event)
{
if ($this->isListenerFired($event)) {
return;
}
// Replace state and city id to name
$this->replaceStateAndCityName();
// Drop states and cities foreign key
$this->dropForeignKey();
// Remove states and cities tables
$this->dropSchemas();
// Delete state & city models, migrations & seeders
$this->deleteFiles();
// Update Crater app version
$this->updateVersion();
}
private function replaceStateAndCityName()
{
\Schema::table('addresses', function (Blueprint $table) {
$table->string('state')->nullable();
$table->string('city')->nullable();
});
$addresses = \InvoiceShelf\Models\Address::all();
foreach ($addresses as $add) {
$city = \InvoiceShelf\City::find($add->city_id);
if ($city) {
$add->city = $city->name;
}
$state = \InvoiceShelf\State::find($add->state_id);
if ($state) {
$add->state = $state->name;
}
$add->save();
}
}
private function dropForeignKey()
{
\Schema::table('addresses', function (Blueprint $table) {
$table->dropForeign('addresses_state_id_foreign');
$table->dropForeign('addresses_city_id_foreign');
$table->dropColumn('state_id');
$table->dropColumn('city_id');
});
}
private function dropSchemas()
{
\Schema::disableForeignKeyConstraints();
\Schema::dropIfExists('states');
\Schema::dropIfExists('cities');
\Schema::enableForeignKeyConstraints();
}
private function deleteFiles()
{
\File::delete(
database_path('migrations/2017_05_06_172817_create_cities_table.php'),
database_path('migrations/2017_05_06_173711_create_states_table.php'),
database_path('seeds/StatesTableSeeder.php'),
database_path('seeds/CitiesTableSeeder.php'),
app_path('City.php'),
app_path('State.php')
);
}
private function updateVersion()
{
Setting::setSetting('version', static::VERSION);
}
}

View File

@@ -1,86 +0,0 @@
<?php
namespace InvoiceShelf\Listeners\Updates\v2;
use InvoiceShelf\Events\UpdateFinished;
use InvoiceShelf\Listeners\Updates\Listener;
use InvoiceShelf\Models\Setting;
use Illuminate\Database\Schema\Blueprint;
class Version201 extends Listener
{
public const VERSION = '2.0.1';
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* @param object $event
* @return void
*/
public function handle(UpdateFinished $event)
{
if ($this->isListenerFired($event)) {
return;
}
// Remove the language files
$this->removeLanguageFiles();
// Change estimate & invoice migrations
$this->changeMigrations();
// Update Crater app version
Setting::setSetting('version', static::VERSION);
}
private function removeLanguageFiles()
{
$en = resource_path('assets/js/plugins/en.js');
$es = resource_path('assets/js/plugins/es.js');
$fr = resource_path('assets/js/plugins/fr.js');
if (file_exists($en)) {
unlink($en);
}
if (file_exists($es)) {
unlink($es);
}
if (file_exists($fr)) {
unlink($fr);
}
}
private function changeMigrations()
{
\Schema::table('invoices', function (Blueprint $table) {
$table->decimal('discount', 15, 2)->nullable()->change();
});
\Schema::table('estimates', function (Blueprint $table) {
$table->decimal('discount', 15, 2)->nullable()->change();
});
\Schema::table('invoice_items', function (Blueprint $table) {
$table->decimal('quantity', 15, 2)->change();
$table->decimal('discount', 15, 2)->nullable()->change();
});
\Schema::table('estimate_items', function (Blueprint $table) {
$table->decimal('quantity', 15, 2)->change();
$table->decimal('discount', 15, 2)->nullable()->change();
$table->unsignedBigInteger('discount_val')->nullable()->change();
});
}
}

View File

@@ -1,38 +0,0 @@
<?php
namespace InvoiceShelf\Listeners\Updates\v2;
use InvoiceShelf\Events\UpdateFinished;
use InvoiceShelf\Listeners\Updates\Listener;
use InvoiceShelf\Models\Setting;
class Version202 extends Listener
{
public const VERSION = '2.0.2';
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* @param object $event
* @return void
*/
public function handle(UpdateFinished $event)
{
if ($this->isListenerFired($event)) {
return;
}
// Update Crater app version
Setting::setSetting('version', static::VERSION);
}
}

View File

@@ -1,62 +0,0 @@
<?php
namespace InvoiceShelf\Listeners\Updates\v2;
use InvoiceShelf\Events\UpdateFinished;
use InvoiceShelf\Listeners\Updates\Listener;
use InvoiceShelf\Models\CompanySetting;
use InvoiceShelf\Models\Setting;
class Version210 extends Listener
{
public const VERSION = '2.1.0';
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* @param object $event
* @return void
*/
public function handle(UpdateFinished $event)
{
if ($this->isListenerFired($event)) {
return;
}
// Add initial auto generate value
$this->addAutoGenerateSettings();
// Update Crater app version
Setting::setSetting('version', static::VERSION);
}
private function addAutoGenerateSettings()
{
$settings = [
'invoice_auto_generate' => 'YES',
'invoice_prefix' => 'INV',
'estimate_prefix' => 'EST',
'estimate_auto_generate' => 'YES',
'payment_prefix' => 'PAY',
'payment_auto_generate' => 'YES',
];
foreach ($settings as $key => $value) {
CompanySetting::setSetting(
$key,
$value,
auth()->user()->company->id
);
}
}
}

View File

@@ -1,161 +0,0 @@
<?php
namespace InvoiceShelf\Listeners\Updates\v3;
use InvoiceShelf\Listeners\Updates\Listener;
use InvoiceShelf\Models\Currency;
use InvoiceShelf\Models\Item;
use InvoiceShelf\Models\Payment;
use InvoiceShelf\Models\PaymentMethod;
use InvoiceShelf\Models\Setting;
use InvoiceShelf\Models\Unit;
use InvoiceShelf\Models\User;
use Illuminate\Database\Schema\Blueprint;
use Vinkla\Hashids\Facades\Hashids;
class Version300 extends Listener
{
public const VERSION = '3.0.0';
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* @param object $event
* @return void
*/
public function handle($event)
{
if ($this->isListenerFired($event)) {
return;
}
$this->changeMigrations();
$this->addSeederData();
$this->databaseChanges();
$this->changeMigrations(true);
Setting::setSetting('version', static::VERSION);
}
public function changeMigrations($removeColumn = false)
{
if ($removeColumn) {
\Schema::table('items', function (Blueprint $table) {
$table->dropColumn('unit');
});
\Schema::table('payments', function (Blueprint $table) {
$table->dropColumn('payment_mode');
});
return true;
}
\Schema::create('units', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->integer('company_id')->unsigned()->nullable();
$table->foreign('company_id')->references('id')->on('companies');
$table->timestamps();
});
\Schema::table('items', function (Blueprint $table) {
$table->integer('unit_id')->unsigned()->nullable();
$table->foreign('unit_id')->references('id')->on('units')->onDelete('cascade');
});
\Schema::create('payment_methods', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->integer('company_id')->unsigned()->nullable();
$table->foreign('company_id')->references('id')->on('companies');
$table->timestamps();
});
\Schema::table('payments', function (Blueprint $table) {
$table->string('unique_hash')->nullable();
$table->integer('payment_method_id')->unsigned()->nullable();
$table->foreign('payment_method_id')->references('id')->on('payment_methods')->onDelete('cascade');
});
return true;
}
public function addSeederData()
{
$company_id = User::where('role', 'admin')->first()->company_id;
Unit::create(['name' => 'box', 'company_id' => $company_id]);
Unit::create(['name' => 'cm', 'company_id' => $company_id]);
Unit::create(['name' => 'dz', 'company_id' => $company_id]);
Unit::create(['name' => 'ft', 'company_id' => $company_id]);
Unit::create(['name' => 'g', 'company_id' => $company_id]);
Unit::create(['name' => 'in', 'company_id' => $company_id]);
Unit::create(['name' => 'kg', 'company_id' => $company_id]);
Unit::create(['name' => 'km', 'company_id' => $company_id]);
Unit::create(['name' => 'lb', 'company_id' => $company_id]);
Unit::create(['name' => 'mg', 'company_id' => $company_id]);
Unit::create(['name' => 'pc', 'company_id' => $company_id]);
PaymentMethod::create(['name' => 'Cash', 'company_id' => $company_id]);
PaymentMethod::create(['name' => 'Check', 'company_id' => $company_id]);
PaymentMethod::create(['name' => 'Credit Card', 'company_id' => $company_id]);
PaymentMethod::create(['name' => 'Bank Transfer', 'company_id' => $company_id]);
Currency::create([
'name' => 'Serbian Dinar',
'code' => 'RSD',
'symbol' => 'RSD',
'precision' => '2',
'thousand_separator' => '.',
'decimal_separator' => ',',
]);
}
public function databaseChanges()
{
$payments = Payment::all();
if ($payments) {
foreach ($payments as $payment) {
$payment->unique_hash = Hashids::connection(Payment::class)->encode($payment->id);
$payment->save();
$paymentMethod = PaymentMethod::where('name', $payment->payment_mode)
->first();
if ($paymentMethod) {
$payment->payment_method_id = $paymentMethod->id;
$payment->save();
}
}
}
$items = Item::all();
if ($items) {
foreach ($items as $item) {
$unit = Unit::where('name', $item->unit)
->first();
if ($unit) {
$item->unit_id = $unit->id;
$item->save();
}
}
}
}
}

View File

@@ -1,47 +0,0 @@
<?php
namespace InvoiceShelf\Listeners\Updates\v3;
use Artisan;
use InvoiceShelf\Events\UpdateFinished;
use InvoiceShelf\Listeners\Updates\Listener;
use InvoiceShelf\Models\Currency;
use InvoiceShelf\Models\Setting;
class Version310 extends Listener
{
public const VERSION = '3.1.0';
/**
* Handle the event.
*
* @param UpdateFinished $event
* @return void
*/
public function handle(UpdateFinished $event)
{
if ($this->isListenerFired($event)) {
return;
}
Currency::firstOrCreate(
[
'name' => 'Kyrgyzstani som',
'code' => 'KGS',
],
[
'name' => 'Kyrgyzstani som',
'code' => 'KGS',
'symbol' => 'С̲ ',
'precision' => '2',
'thousand_separator' => '.',
'decimal_separator' => ',',
]
);
Artisan::call('migrate', ['--force' => true]);
// Update Crater app version
Setting::setSetting('version', static::VERSION);
}
}

View File

@@ -1,31 +0,0 @@
<?php
namespace InvoiceShelf\Listeners\Updates\v3;
use Artisan;
use InvoiceShelf\Events\UpdateFinished;
use InvoiceShelf\Listeners\Updates\Listener;
use InvoiceShelf\Models\Setting;
class Version311 extends Listener
{
public const VERSION = '3.1.1';
/**
* Handle the event.
*
* @param UpdateFinished $event
* @return void
*/
public function handle(UpdateFinished $event)
{
if ($this->isListenerFired($event)) {
return;
}
Artisan::call('migrate', ['--force' => true]);
// Update Crater app version
Setting::setSetting('version', static::VERSION);
}
}

View File

@@ -2,18 +2,10 @@
namespace InvoiceShelf\Providers;
use InvoiceShelf\Events\UpdateFinished;
use InvoiceShelf\Listeners\Updates\v1\Version110;
use InvoiceShelf\Listeners\Updates\v2\Version200;
use InvoiceShelf\Listeners\Updates\v2\Version201;
use InvoiceShelf\Listeners\Updates\v2\Version202;
use InvoiceShelf\Listeners\Updates\v2\Version210;
use InvoiceShelf\Listeners\Updates\v3\Version300;
use InvoiceShelf\Listeners\Updates\v3\Version310;
use InvoiceShelf\Listeners\Updates\v3\Version311;
use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use InvoiceShelf\Events\UpdateFinished;
class EventServiceProvider extends ServiceProvider
{
@@ -24,14 +16,7 @@ class EventServiceProvider extends ServiceProvider
*/
protected $listen = [
UpdateFinished::class => [
Version110::class,
Version200::class,
Version201::class,
Version202::class,
Version210::class,
Version300::class,
Version310::class,
Version311::class,
],
Registered::class => [
SendEmailVerificationNotification::class,