diff --git a/app/DataMapper/ClientSettings.php b/app/DataMapper/ClientSettings.php index ffaa05b8fb66..205fed4fff99 100644 --- a/app/DataMapper/ClientSettings.php +++ b/app/DataMapper/ClientSettings.php @@ -4,6 +4,7 @@ namespace App\DataMapper; use App\DataMapper\ClientSettings; use App\DataMapper\CompanySettings; +use App\Models\Client; use App\Utils\TranslationHelper; /** @@ -53,15 +54,18 @@ class ClientSettings extends BaseSettings */ public $invoice_number_prefix; public $invoice_number_pattern; + public $invoice_number_counter; public $quote_number_prefix; public $quote_number_pattern; + public $quote_number_counter; public $client_number_prefix; public $client_number_pattern; public $credit_number_prefix; public $credit_number_pattern; + public $credit_number_counter; public $shared_invoice_quote_counter; @@ -95,6 +99,7 @@ class ClientSettings extends BaseSettings { return (object)[ + 'entity' => Client::class, 'industry_id' => NULL, 'size_id' => NULL, 'invoice_email_list' => NULL, diff --git a/app/DataMapper/CompanySettings.php b/app/DataMapper/CompanySettings.php index 174548ca0db9..1ab089192efe 100644 --- a/app/DataMapper/CompanySettings.php +++ b/app/DataMapper/CompanySettings.php @@ -2,6 +2,8 @@ namespace App\DataMapper; +use App\Models\Company; + /** * CompanySettings */ @@ -77,15 +79,18 @@ class CompanySettings extends BaseSettings */ public $invoice_number_prefix; public $invoice_number_pattern; + public $invoice_number_counter; public $quote_number_prefix; public $quote_number_pattern; + public $quote_number_counter; public $client_number_prefix; public $client_number_pattern; public $credit_number_prefix; public $credit_number_pattern; + public $credit_number_counter; public $shared_invoice_quote_counter; @@ -115,6 +120,7 @@ class CompanySettings extends BaseSettings $config = json_decode(config('ninja.settings')); return (object) [ + 'entity' => Company::class, 'timezone_id' => config('ninja.i18n.timezone_id'), 'language_id' => config('ninja.i18n.language_id'), 'currency_id' => config('ninja.i18n.currency_id'), @@ -134,7 +140,10 @@ class CompanySettings extends BaseSettings 'custom_taxes2' => 'FALSE', 'lock_sent_invoices' => 'TRUE', 'shared_invoice_quote_counter' => 'FALSE', - + 'invoice_number_counter' => 1, + 'quote_number_counter' => 1, + 'credit_number_counter' => 1, + 'translations' => (object) [], ]; } diff --git a/app/Models/Client.php b/app/Models/Client.php index 0e8c0adbd13c..a56501065228 100644 --- a/app/Models/Client.php +++ b/app/Models/Client.php @@ -4,6 +4,7 @@ namespace App\Models; use App\DataMapper\ClientSettings; use App\DataMapper\CompanySettings; +use App\Models\Client; use App\Models\Company; use App\Models\Country; use App\Models\Filterable; @@ -90,6 +91,54 @@ class Client extends BaseModel return ClientSettings::buildClientSettings(new CompanySettings($this->company->settings), new ClientSettings($this->settings)); } + + /** + * Gets the settings by key. + * + * When we need to update a setting value, we need to harvest + * the object of the setting. This is not possible when using the merged settings + * as we do not know which object the setting has come from. + * + * The following method will return the entire object of the property searched for + * where a value exists for $key. + * + * This object can then be mutated by the handling class, + * to persist the new settings we will also need to pass back a + * reference to the parent class. + * + * @param mixes $key The key of property + */ + public function getSettingsByKey($key) + { + + /* Does Setting Exist @ client level */ + if(isset($this->getSettings()->{$key})) + { + return $this->getSettings(); + } + else + return new CompanySettings($this->company->settings); + + } + + public function setSettingsByEntity($entity, $settings) + { + switch ($entity) { + case Client::class: + $this->settings = $settings; + $this->save(); + break; + case Company::class: + $this->company->settings = $settings; + $this->company->save(); + break; + + default: + # code... + break; + } + } + public function documents() { return $this->morphMany(Document::class, 'documentable'); @@ -97,4 +146,5 @@ class Client extends BaseModel + } diff --git a/app/Utils/Traits/GeneratesNumberCounter.php b/app/Utils/Traits/GeneratesNumberCounter.php new file mode 100644 index 000000000000..a3f727cd6fd3 --- /dev/null +++ b/app/Utils/Traits/GeneratesNumberCounter.php @@ -0,0 +1,36 @@ +getSettingsByKey($shared_invoice_quote_counter)->shared_invoice_quote_counter; + + } + + public function incrementCounter($entity) + { + + } + + public function entity_name($entity) + { + + return strtolower(class_basename($entity)); + + } + +} \ 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 b78d8b526a31..0a6e214da273 100644 --- a/database/migrations/2014_10_13_000000_create_users_table.php +++ b/database/migrations/2014_10_13_000000_create_users_table.php @@ -147,10 +147,6 @@ class CreateUsersTable extends Migration $table->string('vat_number')->nullable(); $table->string('id_number')->nullable(); $table->unsignedInteger('size_id')->nullable(); - - $table->unsignedInteger('invoice_number_counter')->default(1); - $table->unsignedInteger('quote_number_counter')->default(1); - $table->unsignedInteger('credit_number_counter')->default(1); $table->text('settings'); @@ -288,10 +284,6 @@ class CreateUsersTable extends Migration $table->unsignedInteger('shipping_country_id')->nullable(); $table->text('settings'); - $table->unsignedInteger('invoice_number_counter')->default(1); - $table->unsignedInteger('quote_number_counter')->default(1); - $table->unsignedInteger('credit_number_counter')->default(1); - $table->boolean('is_deleted')->default(false); $table->string('payment_terms')->nullable(); //todo type? depends how we are storing this $table->string('vat_number')->nullable();