From 1f624e28bcf27e9728dd45eb2471a913ff86c8c6 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Tue, 10 Sep 2019 12:30:43 +1000 Subject: [PATCH] Refactor Group level settings --- app/DataMapper/ClientSettings.php | 2 - app/DataMapper/CompanySettings.php | 29 ------------- app/DataMapper/Group.php | 43 ------------------- app/Models/Client.php | 6 +++ app/Models/Company.php | 6 +++ app/Models/GroupSetting.php | 39 +++++++++++++++++ .../2014_10_13_000000_create_users_table.php | 13 ++++++ database/seeds/RandomDataSeeder.php | 11 +++++ tests/Unit/GroupTest.php | 14 ++---- 9 files changed, 79 insertions(+), 84 deletions(-) delete mode 100644 app/DataMapper/Group.php create mode 100644 app/Models/GroupSetting.php diff --git a/app/DataMapper/ClientSettings.php b/app/DataMapper/ClientSettings.php index 29846aac72a0..e9a9049a5279 100644 --- a/app/DataMapper/ClientSettings.php +++ b/app/DataMapper/ClientSettings.php @@ -60,8 +60,6 @@ class ClientSettings extends BaseSettings public $auto_bill; public $auto_archive_invoice; - public $groups; - /** * Counter Variables * diff --git a/app/DataMapper/CompanySettings.php b/app/DataMapper/CompanySettings.php index 81c5e510d7c3..7e7cef1a1fb5 100644 --- a/app/DataMapper/CompanySettings.php +++ b/app/DataMapper/CompanySettings.php @@ -84,8 +84,6 @@ class CompanySettings extends BaseSettings public $inclusive_taxes; public $translations; - public $group_selectors; - public $groups; /** * Counter Variables @@ -172,34 +170,7 @@ class CompanySettings extends BaseSettings 'design' => 'views/pdf/design1.blade.php', 'translations' => (object) [], - 'group_selectors' => self::groupSelectors(), - 'groups' => self::groupObjects(), ]; } - /** - * Implements App\DataMapper\Group objects - * in order to customise grouped option behaviour - * @return object Settings objects - */ - private static function groupObjects() - { - return (object)[ - 'company_gateways' => NULL, - 'invoice_designs' => NULL - ]; - } - - - /** - * Storage point for ALL Group options - * @return object Settings objects - */ - private static function groupSelectors() - { - return (object)[ - 'company_gateways' => NULL, - 'invoice_designs' => NULL - ]; - } } diff --git a/app/DataMapper/Group.php b/app/DataMapper/Group.php deleted file mode 100644 index 8e347c772dd6..000000000000 --- a/app/DataMapper/Group.php +++ /dev/null @@ -1,43 +0,0 @@ -morphMany(Document::class, 'documentable'); } + public function group_settings() + { + return $this->belongsTo(GroupSetting::class); + } + /** * Generates an array of payment urls per client * for a given amount. diff --git a/app/Models/Company.php b/app/Models/Company.php index 3d2527f630e5..c6a2b5d7f080 100644 --- a/app/Models/Company.php +++ b/app/Models/Company.php @@ -19,6 +19,7 @@ use App\Models\CompanyUser; use App\Models\Country; use App\Models\Currency; use App\Models\Expense; +use App\Models\GroupSetting; use App\Models\Industry; use App\Models\Invoice; use App\Models\Language; @@ -151,6 +152,11 @@ class Company extends BaseModel return $this->belongsTo(Country::class); } + public function group_settings() + { + return $this->hasMany(GroupSetting::class); + } + /** * */ diff --git a/app/Models/GroupSetting.php b/app/Models/GroupSetting.php new file mode 100644 index 000000000000..0f3e7bdc97f2 --- /dev/null +++ b/app/Models/GroupSetting.php @@ -0,0 +1,39 @@ + 'object' + ]; + + public function company() + { + return $this->belongsTo(Company::class); + } + + public function user() + { + return $this->belongsTo(User::class); + } + + +} \ No newline at end of file diff --git a/database/migrations/2014_10_13_000000_create_users_table.php b/database/migrations/2014_10_13_000000_create_users_table.php index 7d8ba25c3e9f..9baf4b020473 100644 --- a/database/migrations/2014_10_13_000000_create_users_table.php +++ b/database/migrations/2014_10_13_000000_create_users_table.php @@ -293,6 +293,7 @@ class CreateUsersTable extends Migration $table->boolean('is_deleted')->default(false); $table->integer('payment_terms')->nullable(); + $table->unsignedInteger('group_settings_id')->nullable(); $table->string('vat_number')->nullable(); $table->string('id_number')->nullable(); @@ -915,6 +916,18 @@ class CreateUsersTable extends Migration $table->foreign('client_id')->references('id')->on('clients')->onDelete('cascade'); }); + Schema::create('group_settings', function ($table){ + $table->increments('id'); + $table->unsignedInteger('company_id'); + $table->unsignedInteger('user_id')->nullable(); + $table->string('name'); + $table->text('settings'); + $table->boolean('is_default')->default(false); + $table->timestamps(6); + + $table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade'); + }); + } /** diff --git a/database/seeds/RandomDataSeeder.php b/database/seeds/RandomDataSeeder.php index 523681561976..602c7722bd8e 100644 --- a/database/seeds/RandomDataSeeder.php +++ b/database/seeds/RandomDataSeeder.php @@ -1,11 +1,13 @@ save(); } + + GroupSetting::create([ + 'company_id' => $company->id, + 'user_id' => $user->id, + 'is_default' =>true, + 'settings' => ClientSettings::buildClientSettings(new CompanySettings(CompanySettings::defaults()), new ClientSettings(ClientSettings::defaults())), + 'name' => 'Default Client Settings', + ]); + } } diff --git a/tests/Unit/GroupTest.php b/tests/Unit/GroupTest.php index 37e5b65bc55e..c99b657ad8dd 100644 --- a/tests/Unit/GroupTest.php +++ b/tests/Unit/GroupTest.php @@ -26,27 +26,21 @@ class GroupTest extends TestCase public function testGroupsPropertiesExistsResponses() { - $this->assertTrue(property_exists($this->settings->groups, 'company_gateways')); - - $this->assertTrue(property_exists($this->settings, 'groups')); + $this->assertTrue(property_exists($this->settings, 'design')); } public function testPropertyValueAccessors() { - $this->settings->groups->company_gateways = 'slug'; + $this->settings->translations = (object) ['hello' => 'world']; - $this->assertEquals('slug', $this->settings->groups->company_gateways); + $this->assertEquals('world', $this->settings->translations->hello); } public function testPropertyIsSet() { - $this->assertFalse(isset($this->settings->groups->company_gateways)); - - $this->settings->groups->company_gateways = 'slug'; - - $this->assertTrue(isset($this->settings->groups->company_gateways)); + $this->assertFalse(isset($this->settings->translations->nope)); } } \ No newline at end of file