refactor for pushing company meta data into settings

This commit is contained in:
David Bomba 2019-10-04 22:37:40 +10:00
parent 2c25f20a25
commit 24075072df
8 changed files with 110 additions and 58 deletions

View File

@ -151,7 +151,37 @@ class CompanySettings extends BaseSettings
public $require_invoice_signature = false; public $require_invoice_signature = false;
public $require_quote_signature = false; public $require_quote_signature = false;
/* Company Meta data that we can use to build sub companies*/
public $name;
public $logo_url;
public $website;
public $address1;
public $address2;
public $city;
public $state;
public $postal_code;
public $phone;
public $email;
public $country_id;
public $vat_number;
public $id_number;
public static $casts = [ public static $casts = [
'name' => 'string',
'logo_url' => 'string',
'website' => 'string',
'address1' => 'string',
'address2' => 'string',
'city' => 'string',
'state' => 'string',
'postal_code' => 'string',
'phone' => 'string',
'email' => 'string',
'country_id' => 'string',
'vat_number' => 'string',
'id_number' => 'string',
'has_custom_design1' => 'bool', 'has_custom_design1' => 'bool',
'has_custom_design2' => 'bool', 'has_custom_design2' => 'bool',
'has_custom_design3' => 'bool', 'has_custom_design3' => 'bool',
@ -263,6 +293,7 @@ class CompanySettings extends BaseSettings
$data->date_format_id = (string)config('ninja.i18n.date_format_id'); $data->date_format_id = (string)config('ninja.i18n.date_format_id');
$data->start_of_week = (int) config('ninja.i18n.start_of_week'); $data->start_of_week = (int) config('ninja.i18n.start_of_week');
$data->financial_year_start = (int)config('ninja.i18n.financial_year_start'); $data->financial_year_start = (int)config('ninja.i18n.financial_year_start');
$data->country_id = (int)config('ninja.i18n.country_id');
$data->translations = (object) []; $data->translations = (object) [];
return self::setCasts($data, self::$casts); return self::setCasts($data, self::$casts);
@ -288,7 +319,9 @@ class CompanySettings extends BaseSettings
$settings->{$key} = self::castAttribute($key, $company_settings->{$key}); $settings->{$key} = self::castAttribute($key, $company_settings->{$key});
} }
return $settings; return $settings;
} }
} }

View File

@ -23,7 +23,7 @@ class CompanyFactory
{ {
$company = new Company; $company = new Company;
$company->name = ''; // $company->name = '';
$company->account_id = $account_id; $company->account_id = $account_id;
$company->company_key = $this->createHash(); $company->company_key = $this->createHash();
$company->settings = CompanySettings::defaults(); $company->settings = CompanySettings::defaults();

View File

@ -43,20 +43,20 @@ class Company extends BaseModel
protected $presenter = 'App\Models\Presenters\CompanyPresenter'; protected $presenter = 'App\Models\Presenters\CompanyPresenter';
protected $fillable = [ protected $fillable = [
'name', // 'name',
'logo', // 'logo',
'industry_id', 'industry_id',
'address1', // 'address1',
'address2', // 'address2',
'city', // 'city',
'state', // 'state',
'postal_code', // 'postal_code',
'phone', // 'phone',
'email', // 'email',
'country_id', // 'country_id',
'domain', 'domain',
'vat_number', // 'vat_number',
'id_number', // 'id_number',
'size_id', 'size_id',
'settings', 'settings',
]; ];
@ -157,7 +157,8 @@ class Company extends BaseModel
*/ */
public function country() public function country()
{ {
return $this->belongsTo(Country::class); //return $this->belongsTo(Country::class);
return Country::find($this->settings->country_id);
} }
public function group_settings() public function group_settings()

View File

@ -36,26 +36,43 @@ class CompanyPresenter extends EntityPresenter
$str = ''; $str = '';
$company = $this->entity; $company = $this->entity;
if ($address1 = $company->address1) { if ($address1 = $company->settings->address1) {
$str .= e($address1) . '<br/>'; $str .= e($address1) . '<br/>';
} }
if ($address2 = $company->address2) { if ($address2 = $company->settings->address2) {
$str .= e($address2) . '<br/>'; $str .= e($address2) . '<br/>';
} }
if ($cityState = $this->getCityState()) { if ($cityState = $this->getCompanyCityState()) {
$str .= e($cityState) . '<br/>'; $str .= e($cityState) . '<br/>';
} }
if ($country = $company->country) { if ($country = $company->country()) {
$str .= e($country->name) . '<br/>'; $str .= e($country->name) . '<br/>';
} }
if ($company->phone) { if ($company->settings->phone) {
$str .= ctrans('texts.work_phone') . ": ". e($company->phone) .'<br/>'; $str .= ctrans('texts.work_phone') . ": ". e($company->settings->phone) .'<br/>';
} }
if ($company->email) { if ($company->settings->email) {
$str .= ctrans('texts.work_email') . ": ". e($company->email) .'<br/>'; $str .= ctrans('texts.work_email') . ": ". e($company->settings->email) .'<br/>';
} }
return $str; return $str;
} }
public function getCompanyCityState()
{
$company = $this->entity;
$swap = $company->country() && $company->country()->swap_postal_code;
$city = e($company->settings->city);
$state = e($company->settings->state);
$postalCode = e($company->settings->postal_code);
if ($city || $state || $postalCode) {
return $this->cityStateZip($city, $state, $postalCode, $swap);
} else {
return false;
}
}
} }

View File

@ -65,20 +65,20 @@ class CompanyTransformer extends EntityTransformer
{ {
return [ return [
'id' => (string)$this->encodePrimaryKey($company->id), 'id' => (string)$this->encodePrimaryKey($company->id),
'name' => (string)$company->name ?: '', // 'name' => (string)$company->name ?: '',
'website' => (string)$company->website ?: '', // 'website' => (string)$company->website ?: '',
'logo_url' => (string)$company->getLogo(), // 'logo_url' => (string)$company->getLogo(),
'company_key' => (string)$company->company_key ?: '', 'company_key' => (string)$company->company_key ?: '',
'address1' => (string)$company->address1 ?: '', // 'address1' => (string)$company->address1 ?: '',
'address2' => (string)$company->address2 ?: '', // 'address2' => (string)$company->address2 ?: '',
'city' => (string)$company->city ?: '', // 'city' => (string)$company->city ?: '',
'state' => (string)$company->state ?: '', // 'state' => (string)$company->state ?: '',
'postal_code' => (string)$company->postal_code ?: '', // 'postal_code' => (string)$company->postal_code ?: '',
'phone' => (string)$company->phone ?: '', // 'phone' => (string)$company->phone ?: '',
'email' => (string)$company->email ?: '', // 'email' => (string)$company->email ?: '',
'country_id' => (string) $company->country_id ?: '', // 'country_id' => (string) $company->country_id ?: '',
'vat_number' => (string)$company->vat_number ?: '', // 'vat_number' => (string)$company->vat_number ?: '',
'id_number' => (string)$company->id_number ?: '', // 'id_number' => (string)$company->id_number ?: '',
'size_id' => (string) $company->size_id ?: '', 'size_id' => (string) $company->size_id ?: '',
'industry_id' => (string) $company->industry_id ?: '', 'industry_id' => (string) $company->industry_id ?: '',
'settings' => $company->settings ?: '', 'settings' => $company->settings ?: '',

View File

@ -211,7 +211,7 @@ trait MakesInvoiceValues
$data['$company_city'] = $this->company->city; $data['$company_city'] = $this->company->city;
$data['$company_state'] = $this->company->state; $data['$company_state'] = $this->company->state;
$data['$company_postal_code'] = $this->company->postal_code; $data['$company_postal_code'] = $this->company->postal_code;
$data['$company_country'] = $this->company->country ? $this->company->country->name : ''; $data['$company_country'] = $this->company->country() ? $this->company->country()->name : '';
$data['$company_phone'] = $this->company->phone; $data['$company_phone'] = $this->company->phone;
$data['$company_email'] = $this->company->email; $data['$company_email'] = $this->company->email;
$data['$company_vat_number'] = $this->company->vat_number; $data['$company_vat_number'] = $this->company->vat_number;

View File

@ -5,19 +5,19 @@ use Faker\Generator as Faker;
$factory->define(App\Models\Company::class, function (Faker $faker) { $factory->define(App\Models\Company::class, function (Faker $faker) {
return [ return [
'name' => $faker->name, //'name' => $faker->name,
'company_key' => strtolower(\Illuminate\Support\Str::random(config('ninja.key_length'))), 'company_key' => strtolower(\Illuminate\Support\Str::random(config('ninja.key_length'))),
'ip' => $faker->ipv4, 'ip' => $faker->ipv4,
'db' => config('database.default'), 'db' => config('database.default'),
'settings' => CompanySettings::defaults(), 'settings' => CompanySettings::defaults(),
'address1' => $faker->secondaryAddress, // 'address1' => $faker->secondaryAddress,
'address2' => $faker->address, // 'address2' => $faker->address,
'city' => $faker->city, // 'city' => $faker->city,
'state' => $faker->state, // 'state' => $faker->state,
'postal_code' => $faker->postcode, // 'postal_code' => $faker->postcode,
'country_id' => 4, // 'country_id' => 4,
'phone' => $faker->phoneNumber, // 'phone' => $faker->phoneNumber,
'email' => $faker->safeEmail, // 'email' => $faker->safeEmail,
'logo' => 'https://www.invoiceninja.com/wp-content/themes/invoice-ninja/images/logo.png', // 'logo' => 'https://www.invoiceninja.com/wp-content/themes/invoice-ninja/images/logo.png',
]; ];
}); });

View File

@ -127,24 +127,25 @@ class CreateUsersTable extends Migration
Schema::create('companies', function (Blueprint $table) { Schema::create('companies', function (Blueprint $table) {
$table->increments('id'); $table->increments('id');
$table->string('name')->nullable(); //$table->string('name')->nullable();
$table->unsignedInteger('account_id')->index(); $table->unsignedInteger('account_id')->index();
$table->unsignedInteger('industry_id')->nullable(); $table->unsignedInteger('industry_id')->nullable();
$table->string('ip')->nullable(); $table->string('ip')->nullable();
$table->string('company_key',100)->unique(); $table->string('company_key',100)->unique();
$table->string('logo')->nullable(); $table->string('logo')->nullable();
$table->string('address1')->nullable(); // $table->string('website')->nullable();
$table->string('address2')->nullable(); // $table->string('address1')->nullable();
$table->string('city')->nullable(); // $table->string('address2')->nullable();
$table->string('state')->nullable(); // $table->string('city')->nullable();
$table->string('postal_code')->nullable(); // $table->string('state')->nullable();
$table->string('phone')->nullable(); // $table->string('postal_code')->nullable();
$table->string('email')->nullable(); // $table->string('phone')->nullable();
$table->unsignedInteger('country_id')->nullable(); // $table->string('email')->nullable();
// $table->unsignedInteger('country_id')->nullable();
$table->string('domain')->nullable(); $table->string('domain')->nullable();
$table->string('db')->nullable(); $table->string('db')->nullable();
$table->string('vat_number')->nullable(); // $table->string('vat_number')->nullable();
$table->string('id_number')->nullable(); // $table->string('id_number')->nullable();
$table->unsignedInteger('size_id')->nullable(); $table->unsignedInteger('size_id')->nullable();
$table->string('start_of_week')->nullable(); $table->string('start_of_week')->nullable();
$table->string('financial_year_start')->nullable(); $table->string('financial_year_start')->nullable();
@ -154,7 +155,7 @@ class CreateUsersTable extends Migration
$table->timestamps(6); $table->timestamps(6);
$table->softDeletes(); $table->softDeletes();
$table->foreign('country_id')->references('id')->on('countries'); //$table->foreign('country_id')->references('id')->on('countries');
$table->foreign('industry_id')->references('id')->on('industries'); $table->foreign('industry_id')->references('id')->on('industries');
$table->foreign('size_id')->references('id')->on('sizes'); $table->foreign('size_id')->references('id')->on('sizes');
$table->foreign('account_id')->references('id')->on('accounts')->onDelete('cascade'); $table->foreign('account_id')->references('id')->on('accounts')->onDelete('cascade');