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_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 = [
'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_design2' => '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->start_of_week = (int) config('ninja.i18n.start_of_week');
$data->financial_year_start = (int)config('ninja.i18n.financial_year_start');
$data->country_id = (int)config('ninja.i18n.country_id');
$data->translations = (object) [];
return self::setCasts($data, self::$casts);
@ -288,7 +319,9 @@ class CompanySettings extends BaseSettings
$settings->{$key} = self::castAttribute($key, $company_settings->{$key});
}
return $settings;
}
}

View File

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

View File

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

View File

@ -36,26 +36,43 @@ class CompanyPresenter extends EntityPresenter
$str = '';
$company = $this->entity;
if ($address1 = $company->address1) {
if ($address1 = $company->settings->address1) {
$str .= e($address1) . '<br/>';
}
if ($address2 = $company->address2) {
if ($address2 = $company->settings->address2) {
$str .= e($address2) . '<br/>';
}
if ($cityState = $this->getCityState()) {
if ($cityState = $this->getCompanyCityState()) {
$str .= e($cityState) . '<br/>';
}
if ($country = $company->country) {
if ($country = $company->country()) {
$str .= e($country->name) . '<br/>';
}
if ($company->phone) {
$str .= ctrans('texts.work_phone') . ": ". e($company->phone) .'<br/>';
if ($company->settings->phone) {
$str .= ctrans('texts.work_phone') . ": ". e($company->settings->phone) .'<br/>';
}
if ($company->email) {
$str .= ctrans('texts.work_email') . ": ". e($company->email) .'<br/>';
if ($company->settings->email) {
$str .= ctrans('texts.work_email') . ": ". e($company->settings->email) .'<br/>';
}
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 [
'id' => (string)$this->encodePrimaryKey($company->id),
'name' => (string)$company->name ?: '',
'website' => (string)$company->website ?: '',
'logo_url' => (string)$company->getLogo(),
// 'name' => (string)$company->name ?: '',
// 'website' => (string)$company->website ?: '',
// 'logo_url' => (string)$company->getLogo(),
'company_key' => (string)$company->company_key ?: '',
'address1' => (string)$company->address1 ?: '',
'address2' => (string)$company->address2 ?: '',
'city' => (string)$company->city ?: '',
'state' => (string)$company->state ?: '',
'postal_code' => (string)$company->postal_code ?: '',
'phone' => (string)$company->phone ?: '',
'email' => (string)$company->email ?: '',
'country_id' => (string) $company->country_id ?: '',
'vat_number' => (string)$company->vat_number ?: '',
'id_number' => (string)$company->id_number ?: '',
// 'address1' => (string)$company->address1 ?: '',
// 'address2' => (string)$company->address2 ?: '',
// 'city' => (string)$company->city ?: '',
// 'state' => (string)$company->state ?: '',
// 'postal_code' => (string)$company->postal_code ?: '',
// 'phone' => (string)$company->phone ?: '',
// 'email' => (string)$company->email ?: '',
// 'country_id' => (string) $company->country_id ?: '',
// 'vat_number' => (string)$company->vat_number ?: '',
// 'id_number' => (string)$company->id_number ?: '',
'size_id' => (string) $company->size_id ?: '',
'industry_id' => (string) $company->industry_id ?: '',
'settings' => $company->settings ?: '',

View File

@ -211,7 +211,7 @@ trait MakesInvoiceValues
$data['$company_city'] = $this->company->city;
$data['$company_state'] = $this->company->state;
$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_email'] = $this->company->email;
$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) {
return [
'name' => $faker->name,
//'name' => $faker->name,
'company_key' => strtolower(\Illuminate\Support\Str::random(config('ninja.key_length'))),
'ip' => $faker->ipv4,
'db' => config('database.default'),
'settings' => CompanySettings::defaults(),
'address1' => $faker->secondaryAddress,
'address2' => $faker->address,
'city' => $faker->city,
'state' => $faker->state,
'postal_code' => $faker->postcode,
'country_id' => 4,
'phone' => $faker->phoneNumber,
'email' => $faker->safeEmail,
'logo' => 'https://www.invoiceninja.com/wp-content/themes/invoice-ninja/images/logo.png',
// 'address1' => $faker->secondaryAddress,
// 'address2' => $faker->address,
// 'city' => $faker->city,
// 'state' => $faker->state,
// 'postal_code' => $faker->postcode,
// 'country_id' => 4,
// 'phone' => $faker->phoneNumber,
// 'email' => $faker->safeEmail,
// '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) {
$table->increments('id');
$table->string('name')->nullable();
//$table->string('name')->nullable();
$table->unsignedInteger('account_id')->index();
$table->unsignedInteger('industry_id')->nullable();
$table->string('ip')->nullable();
$table->string('company_key',100)->unique();
$table->string('logo')->nullable();
$table->string('address1')->nullable();
$table->string('address2')->nullable();
$table->string('city')->nullable();
$table->string('state')->nullable();
$table->string('postal_code')->nullable();
$table->string('phone')->nullable();
$table->string('email')->nullable();
$table->unsignedInteger('country_id')->nullable();
// $table->string('website')->nullable();
// $table->string('address1')->nullable();
// $table->string('address2')->nullable();
// $table->string('city')->nullable();
// $table->string('state')->nullable();
// $table->string('postal_code')->nullable();
// $table->string('phone')->nullable();
// $table->string('email')->nullable();
// $table->unsignedInteger('country_id')->nullable();
$table->string('domain')->nullable();
$table->string('db')->nullable();
$table->string('vat_number')->nullable();
$table->string('id_number')->nullable();
// $table->string('vat_number')->nullable();
// $table->string('id_number')->nullable();
$table->unsignedInteger('size_id')->nullable();
$table->string('start_of_week')->nullable();
$table->string('financial_year_start')->nullable();
@ -154,7 +155,7 @@ class CreateUsersTable extends Migration
$table->timestamps(6);
$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('size_id')->references('id')->on('sizes');
$table->foreign('account_id')->references('id')->on('accounts')->onDelete('cascade');