From 2b680f0f8eff4785e1b7bfafb1abb04c32fd327a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Denis=20Duli=C3=A7i?= Date: Fri, 10 Sep 2021 00:31:39 +0300 Subject: [PATCH] added prefix to source --- app/Abstracts/Import.php | 6 +- app/Listeners/Update/V21/Version2125.php | 30 ++++ app/Providers/Event.php | 1 + app/Traits/Sources.php | 38 ++++- app/Utilities/helpers.php | 6 +- database/factories/Account.php | 2 +- database/factories/Category.php | 2 +- database/factories/Contact.php | 2 +- database/factories/Currency.php | 2 +- database/factories/Dashboard.php | 2 +- database/factories/Document.php | 2 +- database/factories/Item.php | 2 +- database/factories/Reconciliation.php | 2 +- database/factories/Role.php | 2 +- database/factories/Tax.php | 2 +- database/factories/Transaction.php | 2 +- database/factories/Transfer.php | 2 +- database/factories/User.php | 2 +- database/factories/Widget.php | 2 +- .../2021_09_10_000000_core_v2125.php | 134 ++++++++++++++++++ database/seeds/Accounts.php | 2 +- database/seeds/Categories.php | 2 +- database/seeds/Currencies.php | 2 +- database/seeds/Dashboards.php | 2 +- database/seeds/EmailTemplates.php | 2 +- database/seeds/Reports.php | 2 +- database/seeds/TestCompany.php | 1 + 27 files changed, 227 insertions(+), 29 deletions(-) create mode 100644 app/Listeners/Update/V21/Version2125.php create mode 100644 database/migrations/2021_09_10_000000_core_v2125.php diff --git a/app/Abstracts/Import.php b/app/Abstracts/Import.php index ca1da0463..26cfc0949 100644 --- a/app/Abstracts/Import.php +++ b/app/Abstracts/Import.php @@ -3,6 +3,7 @@ namespace App\Abstracts; use App\Traits\Import as ImportHelper; +use App\Traits\Sources; use App\Utilities\Date; use Carbon\Exceptions\InvalidFormatException; use Illuminate\Contracts\Queue\ShouldQueue; @@ -10,7 +11,6 @@ use Illuminate\Contracts\Translation\HasLocalePreference; use Illuminate\Support\Arr; use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Validator; -use Illuminate\Support\Str; use Maatwebsite\Excel\Concerns\Importable; use Maatwebsite\Excel\Concerns\SkipsEmptyRows; use Maatwebsite\Excel\Concerns\ToModel; @@ -23,7 +23,7 @@ use PhpOffice\PhpSpreadsheet\Shared\Date as ExcelDate; abstract class Import implements HasLocalePreference, ShouldQueue, SkipsEmptyRows, WithChunkReading, WithHeadingRow, WithLimit, WithMapping, WithValidation, ToModel { - use Importable, ImportHelper; + use Importable, ImportHelper, Sources; public $user; @@ -36,7 +36,7 @@ abstract class Import implements HasLocalePreference, ShouldQueue, SkipsEmptyRow { $row['company_id'] = company_id(); $row['created_by'] = $this->user->id; - $row['created_from'] = 'import'; + $row['created_from'] = $this->getSourcePrefix() . 'import'; // Make enabled field integer if (isset($row['enabled'])) { diff --git a/app/Listeners/Update/V21/Version2125.php b/app/Listeners/Update/V21/Version2125.php new file mode 100644 index 000000000..ca64200b2 --- /dev/null +++ b/app/Listeners/Update/V21/Version2125.php @@ -0,0 +1,30 @@ +skipThisUpdate($event)) { + return; + } + + Artisan::call('migrate', ['--force' => true]); + } +} diff --git a/app/Providers/Event.php b/app/Providers/Event.php index b172a9b6c..d5cb2d7f8 100644 --- a/app/Providers/Event.php +++ b/app/Providers/Event.php @@ -36,6 +36,7 @@ class Event extends Provider 'App\Listeners\Update\V21\Version2117', 'App\Listeners\Update\V21\Version2118', 'App\Listeners\Update\V21\Version2124', + 'App\Listeners\Update\V21\Version2125', ], 'Illuminate\Auth\Events\Login' => [ 'App\Listeners\Auth\Login', diff --git a/app/Traits/Sources.php b/app/Traits/Sources.php index 770a2eec7..5f4706ebc 100644 --- a/app/Traits/Sources.php +++ b/app/Traits/Sources.php @@ -2,6 +2,8 @@ namespace App\Traits; +use Illuminate\Support\Str; + trait Sources { public function isSourcable(): bool @@ -16,22 +18,50 @@ trait Sources return ! $this->isSourcable(); } - public function getSourceName($request = null): string + public function getSourceName($request = null, $alias = null): string { + $prefix = $this->getSourcePrefix($alias); + if (app()->runningInConsole()) { - $source = 'console'; + $source = $prefix . 'console'; } if (empty($source)) { $request = $request ?: request(); - $source = $request->isApi() ? 'api' : null; + $source = $request->isApi() ? $prefix . 'api' : null; } if (empty($source)) { - $source = 'ui'; + $source = $prefix . 'ui'; } return $source; } + + public function getSourcePrefix($alias = null) + { + $alias = is_null($alias) ? $this->getSourceAlias() : $alias; + + return $alias . '::'; + } + + public function getSourceAlias() + { + $prefix = ''; + + $namespaces = explode('\\', get_class($this)); + + if (empty($namespaces[0]) || (empty($namespaces[1]))) { + return $prefix; + } + + if ($namespaces[0] != 'Modules') { + return 'core'; + } + + $prefix = Str::kebab($namespaces[1]); + + return $prefix; + } } diff --git a/app/Utilities/helpers.php b/app/Utilities/helpers.php index 84fbc7424..deade28b1 100644 --- a/app/Utilities/helpers.php +++ b/app/Utilities/helpers.php @@ -132,15 +132,17 @@ if (!function_exists('source_name')) { /** * Get the current source. * + * @param string|null $alias + * * @return string */ - function source_name() + function source_name($alias = null) { $tmp = new class() { use Sources; }; - return $tmp->getSourceName(); + return $tmp->getSourceName(null, $alias); } } diff --git a/database/factories/Account.php b/database/factories/Account.php index 9469448f6..dd745d1a2 100644 --- a/database/factories/Account.php +++ b/database/factories/Account.php @@ -31,7 +31,7 @@ class Account extends Factory 'bank_phone' => $this->faker->phoneNumber, 'bank_address' => $this->faker->address, 'enabled' => $this->faker->boolean ? 1 : 0, - 'created_from' => 'factory', + 'created_from' => 'core::factory', ]; } diff --git a/database/factories/Category.php b/database/factories/Category.php index bea7a5a9c..410d0aa28 100644 --- a/database/factories/Category.php +++ b/database/factories/Category.php @@ -29,7 +29,7 @@ class Category extends Factory 'type' => $this->faker->randomElement($types), 'color' => $this->faker->hexColor, 'enabled' => $this->faker->boolean ? 1 : 0, - 'created_from' => 'factory', + 'created_from' => 'core::factory', ]; } diff --git a/database/factories/Contact.php b/database/factories/Contact.php index 5284e7a90..fb02128df 100644 --- a/database/factories/Contact.php +++ b/database/factories/Contact.php @@ -39,7 +39,7 @@ class Contact extends Factory 'currency_code' => setting('default.currency'), 'reference' => $this->faker->text(5), 'enabled' => $this->faker->boolean ? 1 : 0, - 'created_from' => 'factory', + 'created_from' => 'core::factory', ]; } diff --git a/database/factories/Currency.php b/database/factories/Currency.php index 22deec160..79569a65e 100644 --- a/database/factories/Currency.php +++ b/database/factories/Currency.php @@ -47,7 +47,7 @@ class Currency extends Factory 'decimal_mark' => $currency['decimal_mark'], 'thousands_separator' => $currency['thousands_separator'], 'enabled' => $this->faker->boolean ? 1 : 0, - 'created_from' => 'factory', + 'created_from' => 'core::factory', ]; } diff --git a/database/factories/Dashboard.php b/database/factories/Dashboard.php index d3b591cc2..1e01cab77 100644 --- a/database/factories/Dashboard.php +++ b/database/factories/Dashboard.php @@ -25,7 +25,7 @@ class Dashboard extends Factory 'company_id' => $this->company->id, 'name' => $this->faker->text(15), 'enabled' => $this->faker->boolean ? 1 : 0, - 'created_from' => 'factory', + 'created_from' => 'core::factory', ]; } diff --git a/database/factories/Document.php b/database/factories/Document.php index 67288e2e6..31c0b4abc 100644 --- a/database/factories/Document.php +++ b/database/factories/Document.php @@ -45,7 +45,7 @@ class Document extends AbstractFactory 'currency_rate' => '1', 'notes' => $this->faker->text(5), 'amount' => '0', - 'created_from' => 'factory', + 'created_from' => 'core::factory', ]; } diff --git a/database/factories/Item.php b/database/factories/Item.php index 54f4185f7..3ce7f6f4b 100644 --- a/database/factories/Item.php +++ b/database/factories/Item.php @@ -29,7 +29,7 @@ class Item extends Factory 'sale_price' => $this->faker->randomFloat(2, 10, 20), 'category_id' => $this->company->categories()->item()->get()->random(1)->pluck('id')->first(), 'enabled' => $this->faker->boolean ? 1 : 0, - 'created_from' => 'factory', + 'created_from' => 'core::factory', ]; } diff --git a/database/factories/Reconciliation.php b/database/factories/Reconciliation.php index b915138ad..e021b1d5c 100644 --- a/database/factories/Reconciliation.php +++ b/database/factories/Reconciliation.php @@ -34,7 +34,7 @@ class Reconciliation extends Factory 'started_at' => $started_at, 'ended_at' => $ended_at, 'reconcile' => $this->faker->boolean ? 1 : 0, - 'created_from' => 'factory', + 'created_from' => 'core::factory', ]; } diff --git a/database/factories/Role.php b/database/factories/Role.php index b7962b82a..2cce819b5 100644 --- a/database/factories/Role.php +++ b/database/factories/Role.php @@ -28,7 +28,7 @@ class Role extends Factory 'name' => strtolower($name), 'display_name' => $name, 'description' => $name, - 'created_from' => 'factory', + 'created_from' => 'core::factory', ]; } diff --git a/database/factories/Tax.php b/database/factories/Tax.php index 4d4456e38..2016f0e12 100644 --- a/database/factories/Tax.php +++ b/database/factories/Tax.php @@ -29,7 +29,7 @@ class Tax extends Factory 'rate' => $this->faker->randomFloat(2, 10, 20), 'type' => $this->faker->randomElement($types), 'enabled' => $this->faker->boolean ? 1 : 0, - 'created_from' => 'factory', + 'created_from' => 'core::factory', ]; } diff --git a/database/factories/Transaction.php b/database/factories/Transaction.php index 10e505ab0..45723a14c 100644 --- a/database/factories/Transaction.php +++ b/database/factories/Transaction.php @@ -41,7 +41,7 @@ class Transaction extends Factory 'category_id' => $this->company->categories()->$category_type()->get()->random(1)->pluck('id')->first(), 'reference' => $this->faker->text(5), 'payment_method' => setting('default.payment_method'), - 'created_from' => 'factory', + 'created_from' => 'core::factory', ]; } diff --git a/database/factories/Transfer.php b/database/factories/Transfer.php index 4f4f0908a..643ae77c6 100644 --- a/database/factories/Transfer.php +++ b/database/factories/Transfer.php @@ -43,7 +43,7 @@ class Transfer extends Factory 'category_id' => Category::transfer(), 'description' => $this->faker->text(20), 'reference' => $this->faker->text(20), - 'created_from' => 'factory', + 'created_from' => 'core::factory', ]; $expense_transaction = Transaction::factory()->create(array_merge($request, [ diff --git a/database/factories/User.php b/database/factories/User.php index 3e2f60b17..2ba3475b1 100644 --- a/database/factories/User.php +++ b/database/factories/User.php @@ -34,7 +34,7 @@ class User extends Factory 'companies' => ['1'], 'roles' => ['1'], 'enabled' => $this->faker->boolean ? 1 : 0, - 'created_from' => 'factory', + 'created_from' => 'core::factory', ]; } diff --git a/database/factories/Widget.php b/database/factories/Widget.php index a84fdf3e8..25ec87d2b 100644 --- a/database/factories/Widget.php +++ b/database/factories/Widget.php @@ -42,7 +42,7 @@ class Widget extends Factory 'dashboard_id' => $dashboard->id, 'name' => $this->faker->text(15), 'class' => $this->faker->randomElement($this->classes), - 'created_from' => 'factory', + 'created_from' => 'core::factory', ]; } } diff --git a/database/migrations/2021_09_10_000000_core_v2125.php b/database/migrations/2021_09_10_000000_core_v2125.php new file mode 100644 index 000000000..d6d6c691b --- /dev/null +++ b/database/migrations/2021_09_10_000000_core_v2125.php @@ -0,0 +1,134 @@ +string('created_from', 100)->change(); + }); + + Schema::table('categories', function (Blueprint $table) { + $table->string('created_from', 100)->change(); + }); + + Schema::table('companies', function (Blueprint $table) { + $table->string('created_from', 100)->change(); + }); + + Schema::table('contacts', function (Blueprint $table) { + $table->string('created_from', 100)->change(); + }); + + Schema::table('currencies', function (Blueprint $table) { + $table->string('created_from', 100)->change(); + }); + + Schema::table('dashboards', function (Blueprint $table) { + $table->string('created_from', 100)->change(); + }); + + Schema::table('documents', function (Blueprint $table) { + $table->string('created_from', 100)->change(); + }); + + Schema::table('document_histories', function (Blueprint $table) { + $table->string('created_from', 100)->change(); + }); + + Schema::table('document_items', function (Blueprint $table) { + $table->string('created_from', 100)->change(); + }); + + Schema::table('document_item_taxes', function (Blueprint $table) { + $table->string('created_from', 100)->change(); + }); + + Schema::table('document_totals', function (Blueprint $table) { + $table->string('created_from', 100)->change(); + }); + + Schema::table('email_templates', function (Blueprint $table) { + $table->string('created_from', 100)->change(); + }); + + Schema::table('items', function (Blueprint $table) { + $table->string('created_from', 100)->change(); + }); + + Schema::table('item_taxes', function (Blueprint $table) { + $table->string('created_from', 100)->change(); + }); + + Schema::table('media', function (Blueprint $table) { + $table->string('created_from', 100)->change(); + }); + + Schema::table('mediables', function (Blueprint $table) { + $table->string('created_from', 100)->change(); + }); + + Schema::table('modules', function (Blueprint $table) { + $table->string('created_from', 100)->change(); + }); + + Schema::table('module_histories', function (Blueprint $table) { + $table->string('created_from', 100)->change(); + }); + + Schema::table('reconciliations', function (Blueprint $table) { + $table->string('created_from', 100)->change(); + }); + + Schema::table('recurring', function (Blueprint $table) { + $table->string('created_from', 100)->change(); + }); + + Schema::table('reports', function (Blueprint $table) { + $table->string('created_from', 100)->change(); + }); + + Schema::table('roles', function (Blueprint $table) { + $table->string('created_from', 100)->change(); + }); + + Schema::table('taxes', function (Blueprint $table) { + $table->string('created_from', 100)->change(); + }); + + Schema::table('transactions', function (Blueprint $table) { + $table->string('created_from', 100)->change(); + }); + + Schema::table('transfers', function (Blueprint $table) { + $table->string('created_from', 100)->change(); + }); + + Schema::table('users', function (Blueprint $table) { + $table->string('created_from', 100)->change(); + }); + + Schema::table('widgets', function (Blueprint $table) { + $table->string('created_from', 100)->change(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + // + } +} diff --git a/database/seeds/Accounts.php b/database/seeds/Accounts.php index c618f4070..4cfd88283 100644 --- a/database/seeds/Accounts.php +++ b/database/seeds/Accounts.php @@ -36,7 +36,7 @@ class Accounts extends Seeder 'currency_code' => 'USD', 'bank_name' => trans('demo.accounts.cash'), 'enabled' => '1', - 'created_from' => 'seed', + 'created_from' => 'core::seed', ])); setting()->set('default.account', $account->id); diff --git a/database/seeds/Categories.php b/database/seeds/Categories.php index d60e695f3..61ea87af8 100644 --- a/database/seeds/Categories.php +++ b/database/seeds/Categories.php @@ -70,7 +70,7 @@ class Categories extends Seeder $income_category_id = $expense_category_id = 0; foreach ($rows as $row) { - $row['created_from'] = 'seed'; + $row['created_from'] = 'core::seed'; $category = $this->dispatch(new CreateCategory($row)); diff --git a/database/seeds/Currencies.php b/database/seeds/Currencies.php index b37d41030..783b1d71a 100644 --- a/database/seeds/Currencies.php +++ b/database/seeds/Currencies.php @@ -78,7 +78,7 @@ class Currencies extends Seeder ]; foreach ($rows as $row) { - $row['created_from'] = 'seed'; + $row['created_from'] = 'core::seed'; $this->dispatch(new CreateCurrency($row)); } diff --git a/database/seeds/Dashboards.php b/database/seeds/Dashboards.php index 4f34b539a..927804747 100644 --- a/database/seeds/Dashboards.php +++ b/database/seeds/Dashboards.php @@ -45,7 +45,7 @@ class Dashboards extends Seeder 'App\Widgets\LatestExpenses', ], 'users' => $user_id, - 'created_from' => 'seed', + 'created_from' => 'core::seed', ])); } } diff --git a/database/seeds/EmailTemplates.php b/database/seeds/EmailTemplates.php index e57dfa66f..cd4915722 100644 --- a/database/seeds/EmailTemplates.php +++ b/database/seeds/EmailTemplates.php @@ -90,7 +90,7 @@ class EmailTemplates extends Seeder 'name' => $template['name'], 'subject' => trans('email_templates.' . $template['alias'] . '.subject'), 'body' => trans('email_templates.' . $template['alias'] . '.body'), - 'created_from' => 'seed', + 'created_from' => 'core::seed', ])); } } diff --git a/database/seeds/Reports.php b/database/seeds/Reports.php index 34638914f..1a85f9096 100644 --- a/database/seeds/Reports.php +++ b/database/seeds/Reports.php @@ -68,7 +68,7 @@ class Reports extends Seeder ]; foreach ($rows as $row) { - $row['created_from'] = 'seed'; + $row['created_from'] = 'core::seed'; $this->dispatch(new CreateReport($row)); } diff --git a/database/seeds/TestCompany.php b/database/seeds/TestCompany.php index fad4a8520..4a5a1bece 100644 --- a/database/seeds/TestCompany.php +++ b/database/seeds/TestCompany.php @@ -52,6 +52,7 @@ class TestCompany extends Seeder 'wizard.completed' => '1', 'email.protocol' => 'array', ], + 'created_from' => 'core::seed', ])); $company->makeCurrent(true);