From a3572de1eccf5f3bae4197c32119b1239ed9570f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Denis=20Duli=C3=A7i?= Date: Thu, 17 Jun 2021 10:59:07 +0300 Subject: [PATCH] added ownership --- app/Abstracts/Import.php | 1 + app/Abstracts/Model.php | 25 +++++- app/Jobs/Banking/CreateAccount.php | 1 + .../CreateBankingDocumentTransaction.php | 4 +- app/Jobs/Banking/CreateReconciliation.php | 1 + app/Jobs/Banking/CreateTransaction.php | 1 + app/Jobs/Banking/CreateTransfer.php | 2 +- app/Jobs/Common/CreateCompany.php | 1 + app/Jobs/Common/CreateContact.php | 3 +- app/Jobs/Common/CreateDashboard.php | 1 + app/Jobs/Common/CreateItem.php | 1 + app/Jobs/Common/CreateReport.php | 1 + app/Jobs/Document/CreateDocument.php | 1 + app/Jobs/Setting/CreateCategory.php | 1 + app/Jobs/Setting/CreateCurrency.php | 1 + app/Jobs/Setting/CreateTax.php | 1 + app/Listeners/Update/V21/Version2117.php | 3 +- app/Models/Auth/Permission.php | 2 - app/Models/Auth/Role.php | 2 - app/Models/Auth/User.php | 4 +- app/Models/Banking/Account.php | 2 +- app/Models/Banking/Reconciliation.php | 2 +- app/Models/Banking/Transaction.php | 18 +++- app/Models/Banking/Transfer.php | 2 +- app/Models/Common/Company.php | 29 +++++-- app/Models/Common/Contact.php | 16 +++- app/Models/Common/Dashboard.php | 2 +- app/Models/Common/Item.php | 2 +- app/Models/Common/Recurring.php | 1 - app/Models/Common/Report.php | 2 +- app/Models/Common/Widget.php | 2 +- app/Models/Document/Document.php | 5 +- app/Models/Setting/Category.php | 2 +- app/Models/Setting/Currency.php | 14 +++- app/Models/Setting/Setting.php | 2 - app/Models/Setting/Tax.php | 2 +- app/Traits/Owners.php | 18 ++++ app/Traits/Tenants.php | 4 +- app/Transformers/Banking/Account.php | 1 + app/Transformers/Banking/Reconciliation.php | 1 + app/Transformers/Banking/Transaction.php | 1 + app/Transformers/Banking/Transfer.php | 1 + app/Transformers/Common/Company.php | 1 + app/Transformers/Common/Contact.php | 1 + app/Transformers/Common/Dashboard.php | 1 + app/Transformers/Common/Item.php | 1 + app/Transformers/Common/Report.php | 1 + app/Transformers/Common/Widget.php | 1 + app/Transformers/Document/Document.php | 1 + app/Transformers/Setting/Category.php | 1 + app/Transformers/Setting/Currency.php | 1 + app/Transformers/Setting/Tax.php | 1 + app/Utilities/helpers.php | 12 +++ .../2021_06_17_000000_core_v2117.php | 82 +++++++++++++++++++ 54 files changed, 253 insertions(+), 37 deletions(-) create mode 100644 app/Traits/Owners.php create mode 100644 database/migrations/2021_06_17_000000_core_v2117.php diff --git a/app/Abstracts/Import.php b/app/Abstracts/Import.php index 1bf5e3c74..ebd689d2f 100644 --- a/app/Abstracts/Import.php +++ b/app/Abstracts/Import.php @@ -33,6 +33,7 @@ abstract class Import implements HasLocalePreference, ShouldQueue, SkipsEmptyRow public function map($row): array { $row['company_id'] = company_id(); + $row['created_by'] = $this->user->id; // Make enabled field integer if (isset($row['enabled'])) { diff --git a/app/Abstracts/Model.php b/app/Abstracts/Model.php index 230826049..ae5842816 100644 --- a/app/Abstracts/Model.php +++ b/app/Abstracts/Model.php @@ -3,16 +3,18 @@ namespace App\Abstracts; use App\Traits\DateTime; +use App\Traits\Owners; use App\Traits\Tenants; use GeneaLabs\LaravelModelCaching\Traits\Cachable; use Illuminate\Database\Eloquent\Model as Eloquent; use Illuminate\Database\Eloquent\SoftDeletes; use Kyslik\ColumnSortable\Sortable; +use Laratrust\Contracts\Ownable; use Lorisleiva\LaravelSearchString\Concerns\SearchString; -abstract class Model extends Eloquent +abstract class Model extends Eloquent implements Ownable { - use Cachable, DateTime, SearchString, SoftDeletes, Sortable, Tenants; + use Cachable, DateTime, Owners, SearchString, SoftDeletes, Sortable, Tenants; protected $tenantable = true; @@ -167,4 +169,23 @@ abstract class Model extends Eloquent return $query->whereIn('contact_id', (array) $contacts); } + + public function scopeIsOwner($query) + { + return $query->where('created_by', user_id()); + } + + public function scopeIsNotOwner($query) + { + return $query->where('created_by', '<>', user_id()); + } + + public function ownerKey($owner) + { + if ($this->isNotOwnable()) { + return 0; + } + + return $this->created_by; + } } diff --git a/app/Jobs/Banking/CreateAccount.php b/app/Jobs/Banking/CreateAccount.php index 15414c879..0676983e7 100644 --- a/app/Jobs/Banking/CreateAccount.php +++ b/app/Jobs/Banking/CreateAccount.php @@ -19,6 +19,7 @@ class CreateAccount extends Job public function __construct($request) { $this->request = $this->getRequestInstance($request); + $this->request->merge(['created_by' => user_id()]); } /** diff --git a/app/Jobs/Banking/CreateBankingDocumentTransaction.php b/app/Jobs/Banking/CreateBankingDocumentTransaction.php index 551ed2771..8429cedc9 100644 --- a/app/Jobs/Banking/CreateBankingDocumentTransaction.php +++ b/app/Jobs/Banking/CreateBankingDocumentTransaction.php @@ -7,9 +7,8 @@ use App\Jobs\Banking\CreateTransaction; use App\Jobs\Document\CreateDocumentHistory; use App\Events\Document\PaidAmountCalculated; use App\Models\Banking\Transaction; -use App\Models\Document\Document; use App\Traits\Currencies; -use Date; +use App\Utilities\Date; class CreateBankingDocumentTransaction extends Job { @@ -31,6 +30,7 @@ class CreateBankingDocumentTransaction extends Job { $this->model = $model; $this->request = $this->getRequestInstance($request); + $this->request->merge(['created_by' => user_id()]); } /** diff --git a/app/Jobs/Banking/CreateReconciliation.php b/app/Jobs/Banking/CreateReconciliation.php index ac91aa19a..41972c6cf 100644 --- a/app/Jobs/Banking/CreateReconciliation.php +++ b/app/Jobs/Banking/CreateReconciliation.php @@ -20,6 +20,7 @@ class CreateReconciliation extends Job public function __construct($request) { $this->request = $this->getRequestInstance($request); + $this->request->merge(['created_by' => user_id()]); } /** diff --git a/app/Jobs/Banking/CreateTransaction.php b/app/Jobs/Banking/CreateTransaction.php index 7a690e4fd..db5b4f0b7 100644 --- a/app/Jobs/Banking/CreateTransaction.php +++ b/app/Jobs/Banking/CreateTransaction.php @@ -21,6 +21,7 @@ class CreateTransaction extends Job public function __construct($request) { $this->request = $this->getRequestInstance($request); + $this->request->merge(['created_by' => user_id()]); } /** diff --git a/app/Jobs/Banking/CreateTransfer.php b/app/Jobs/Banking/CreateTransfer.php index 481daad83..ce87338f8 100644 --- a/app/Jobs/Banking/CreateTransfer.php +++ b/app/Jobs/Banking/CreateTransfer.php @@ -7,7 +7,6 @@ use App\Models\Banking\Account; use App\Models\Banking\Transaction; use App\Models\Banking\Transfer; use App\Models\Setting\Category; -use App\Models\Setting\Currency; use App\Traits\Currencies; class CreateTransfer extends Job @@ -26,6 +25,7 @@ class CreateTransfer extends Job public function __construct($request) { $this->request = $this->getRequestInstance($request); + $this->request->merge(['created_by' => user_id()]); } /** diff --git a/app/Jobs/Common/CreateCompany.php b/app/Jobs/Common/CreateCompany.php index a16548fb1..fedfe5360 100644 --- a/app/Jobs/Common/CreateCompany.php +++ b/app/Jobs/Common/CreateCompany.php @@ -22,6 +22,7 @@ class CreateCompany extends Job public function __construct($request) { $this->request = $this->getRequestInstance($request); + $this->request->merge(['created_by' => user_id()]); } /** diff --git a/app/Jobs/Common/CreateContact.php b/app/Jobs/Common/CreateContact.php index 4f9f1e2cc..14fd1c1c0 100644 --- a/app/Jobs/Common/CreateContact.php +++ b/app/Jobs/Common/CreateContact.php @@ -22,6 +22,7 @@ class CreateContact extends Job public function __construct($request) { $this->request = $this->getRequestInstance($request); + $this->request->merge(['created_by' => user_id()]); } /** @@ -41,7 +42,7 @@ class CreateContact extends Job // Upload logo if ($this->request->file('logo')) { $media = $this->getMedia($this->request->file('logo'), Str::plural($this->contact->type)); - + $this->contact->attachMedia($media, 'logo'); } }); diff --git a/app/Jobs/Common/CreateDashboard.php b/app/Jobs/Common/CreateDashboard.php index 8e87a5291..edc05e45c 100644 --- a/app/Jobs/Common/CreateDashboard.php +++ b/app/Jobs/Common/CreateDashboard.php @@ -24,6 +24,7 @@ class CreateDashboard extends Job public function __construct($request) { $this->request = $this->getRequestInstance($request); + $this->request->merge(['created_by' => user_id()]); } /** diff --git a/app/Jobs/Common/CreateItem.php b/app/Jobs/Common/CreateItem.php index 66d66d667..be4b27981 100644 --- a/app/Jobs/Common/CreateItem.php +++ b/app/Jobs/Common/CreateItem.php @@ -20,6 +20,7 @@ class CreateItem extends Job public function __construct($request) { $this->request = $this->getRequestInstance($request); + $this->request->merge(['created_by' => user_id()]); } /** diff --git a/app/Jobs/Common/CreateReport.php b/app/Jobs/Common/CreateReport.php index d02038297..ce6f6f892 100644 --- a/app/Jobs/Common/CreateReport.php +++ b/app/Jobs/Common/CreateReport.php @@ -19,6 +19,7 @@ class CreateReport extends Job public function __construct($request) { $this->request = $this->getRequestInstance($request); + $this->request->merge(['created_by' => user_id()]); } /** diff --git a/app/Jobs/Document/CreateDocument.php b/app/Jobs/Document/CreateDocument.php index 1f15c3cb9..647a8b507 100644 --- a/app/Jobs/Document/CreateDocument.php +++ b/app/Jobs/Document/CreateDocument.php @@ -23,6 +23,7 @@ class CreateDocument extends Job public function __construct($request) { $this->request = $this->getRequestInstance($request); + $this->request->merge(['created_by' => user_id()]); } /** diff --git a/app/Jobs/Setting/CreateCategory.php b/app/Jobs/Setting/CreateCategory.php index 58d13d0cc..2d415004f 100644 --- a/app/Jobs/Setting/CreateCategory.php +++ b/app/Jobs/Setting/CreateCategory.php @@ -19,6 +19,7 @@ class CreateCategory extends Job public function __construct($request) { $this->request = $this->getRequestInstance($request); + $this->request->merge(['created_by' => user_id()]); } /** diff --git a/app/Jobs/Setting/CreateCurrency.php b/app/Jobs/Setting/CreateCurrency.php index 15db00ee6..716eb0deb 100644 --- a/app/Jobs/Setting/CreateCurrency.php +++ b/app/Jobs/Setting/CreateCurrency.php @@ -19,6 +19,7 @@ class CreateCurrency extends Job public function __construct($request) { $this->request = $this->getRequestInstance($request); + $this->request->merge(['created_by' => user_id()]); } /** diff --git a/app/Jobs/Setting/CreateTax.php b/app/Jobs/Setting/CreateTax.php index 3084813e0..810cc6caa 100644 --- a/app/Jobs/Setting/CreateTax.php +++ b/app/Jobs/Setting/CreateTax.php @@ -19,6 +19,7 @@ class CreateTax extends Job public function __construct($request) { $this->request = $this->getRequestInstance($request); + $this->request->merge(['created_by' => user_id()]); } /** diff --git a/app/Listeners/Update/V21/Version2117.php b/app/Listeners/Update/V21/Version2117.php index cde85ef25..82fe98614 100644 --- a/app/Listeners/Update/V21/Version2117.php +++ b/app/Listeners/Update/V21/Version2117.php @@ -8,6 +8,7 @@ use App\Models\Common\Company; use App\Models\Common\Report; use App\Utilities\Reports as Utility; use Illuminate\Support\Facades\Artisan; +use Illuminate\Support\Facades\Cache; class Version2117 extends Listener { @@ -47,7 +48,7 @@ class Version2117 extends Listener company($company_id)->makeCurrent(); } - + protected function cacheReports() { Report::all()->each(function ($report) { diff --git a/app/Models/Auth/Permission.php b/app/Models/Auth/Permission.php index 9109840d6..6e89fd53e 100644 --- a/app/Models/Auth/Permission.php +++ b/app/Models/Auth/Permission.php @@ -15,8 +15,6 @@ class Permission extends LaratrustPermission protected $table = 'permissions'; - protected $tenantable = false; - /** * The accessors to append to the model's array form. * diff --git a/app/Models/Auth/Role.php b/app/Models/Auth/Role.php index 852dd2e91..676ce046f 100644 --- a/app/Models/Auth/Role.php +++ b/app/Models/Auth/Role.php @@ -15,8 +15,6 @@ class Role extends LaratrustRole protected $table = 'roles'; - protected $tenantable = false; - /** * The attributes that are mass assignable. * diff --git a/app/Models/Auth/User.php b/app/Models/Auth/User.php index a06692a74..3d2c2157e 100644 --- a/app/Models/Auth/User.php +++ b/app/Models/Auth/User.php @@ -6,7 +6,7 @@ use App\Traits\Tenants; use App\Notifications\Auth\Reset; use App\Traits\Media; use App\Traits\Users; -use Date; +use App\Utilities\Date; use Illuminate\Contracts\Translation\HasLocalePreference; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\SoftDeletes; @@ -22,8 +22,6 @@ class User extends Authenticatable implements HasLocalePreference protected $table = 'users'; - protected $tenantable = false; - /** * The attributes that are mass assignable. * diff --git a/app/Models/Banking/Account.php b/app/Models/Banking/Account.php index aeba96ce4..f18019a19 100644 --- a/app/Models/Banking/Account.php +++ b/app/Models/Banking/Account.php @@ -24,7 +24,7 @@ class Account extends Model * * @var array */ - protected $fillable = ['company_id', 'name', 'number', 'currency_code', 'opening_balance', 'bank_name', 'bank_phone', 'bank_address', 'enabled']; + protected $fillable = ['company_id', 'name', 'number', 'currency_code', 'opening_balance', 'bank_name', 'bank_phone', 'bank_address', 'enabled', 'created_by']; /** * The attributes that should be cast. diff --git a/app/Models/Banking/Reconciliation.php b/app/Models/Banking/Reconciliation.php index 039ae40ed..d8265141c 100644 --- a/app/Models/Banking/Reconciliation.php +++ b/app/Models/Banking/Reconciliation.php @@ -15,7 +15,7 @@ class Reconciliation extends Model * * @var array */ - protected $fillable = ['company_id', 'account_id', 'started_at', 'ended_at', 'closing_balance', 'reconciled']; + protected $fillable = ['company_id', 'account_id', 'started_at', 'ended_at', 'closing_balance', 'reconciled', 'created_by']; /** * The attributes that should be cast. diff --git a/app/Models/Banking/Transaction.php b/app/Models/Banking/Transaction.php index 194d75198..9d0fe7f35 100644 --- a/app/Models/Banking/Transaction.php +++ b/app/Models/Banking/Transaction.php @@ -28,7 +28,23 @@ class Transaction extends Model * * @var array */ - protected $fillable = ['company_id', 'type', 'account_id', 'paid_at', 'amount', 'currency_code', 'currency_rate', 'document_id', 'contact_id', 'description', 'category_id', 'payment_method', 'reference', 'parent_id']; + protected $fillable = [ + 'company_id', + 'type', + 'account_id', + 'paid_at', + 'amount', + 'currency_code', + 'currency_rate', + 'document_id', + 'contact_id', + 'description', + 'category_id', + 'payment_method', + 'reference', + 'parent_id', + 'created_by', + ]; /** * The attributes that should be cast. diff --git a/app/Models/Banking/Transfer.php b/app/Models/Banking/Transfer.php index 0799abc1c..aa2664e0e 100644 --- a/app/Models/Banking/Transfer.php +++ b/app/Models/Banking/Transfer.php @@ -18,7 +18,7 @@ class Transfer extends Model * * @var array */ - protected $fillable = ['company_id', 'expense_transaction_id', 'income_transaction_id']; + protected $fillable = ['company_id', 'expense_transaction_id', 'income_transaction_id', 'created_by']; /** * Sortable columns. diff --git a/app/Models/Common/Company.php b/app/Models/Common/Company.php index b647f7255..033d35a67 100644 --- a/app/Models/Common/Company.php +++ b/app/Models/Common/Company.php @@ -9,25 +9,25 @@ use App\Events\Common\CompanyMakingCurrent; use App\Models\Document\Document; use App\Traits\Contacts; use App\Traits\Media; +use App\Traits\Owners; use App\Traits\Tenants; use App\Traits\Transactions; use App\Utilities\Overrider; use Illuminate\Database\Eloquent\Model as Eloquent; use Illuminate\Database\Eloquent\SoftDeletes; use Kyslik\ColumnSortable\Sortable; +use Laratrust\Contracts\Ownable; use Lorisleiva\LaravelSearchString\Concerns\SearchString; -class Company extends Eloquent +class Company extends Eloquent implements Ownable { - use Contacts, Media, SearchString, SoftDeletes, Sortable, Tenants, Transactions; + use Contacts, Media, Owners, SearchString, SoftDeletes, Sortable, Tenants, Transactions; protected $table = 'companies'; - protected $tenantable = false; - protected $dates = ['deleted_at']; - protected $fillable = ['domain', 'enabled']; + protected $fillable = ['domain', 'enabled', 'created_by']; protected $casts = [ 'enabled' => 'boolean', @@ -518,4 +518,23 @@ class Company extends Eloquent { return static::getCurrent() !== null; } + + public function scopeIsOwner($query) + { + return $query->where('created_by', user_id()); + } + + public function scopeIsNotOwner($query) + { + return $query->where('created_by', '<>', user_id()); + } + + public function ownerKey($owner) + { + if ($this->isNotOwnable()) { + return 0; + } + + return $this->created_by; + } } diff --git a/app/Models/Common/Contact.php b/app/Models/Common/Contact.php index 086dc4c46..a42600fcf 100644 --- a/app/Models/Common/Contact.php +++ b/app/Models/Common/Contact.php @@ -24,7 +24,21 @@ class Contact extends Model * * @var array */ - protected $fillable = ['company_id', 'type', 'name', 'email', 'user_id', 'tax_number', 'phone', 'address', 'website', 'currency_code', 'reference', 'enabled']; + protected $fillable = [ + 'company_id', + 'type', + 'name', + 'email', + 'user_id', + 'tax_number', + 'phone', + 'address', + 'website', + 'currency_code', + 'reference', + 'enabled', + 'created_by', + ]; /** * The attributes that should be cast. diff --git a/app/Models/Common/Dashboard.php b/app/Models/Common/Dashboard.php index 3294730f6..7a5b33019 100644 --- a/app/Models/Common/Dashboard.php +++ b/app/Models/Common/Dashboard.php @@ -18,7 +18,7 @@ class Dashboard extends Model * * @var array */ - protected $fillable = ['company_id', 'name', 'enabled']; + protected $fillable = ['company_id', 'name', 'enabled', 'created_by']; /** * The attributes that should be cast. diff --git a/app/Models/Common/Item.php b/app/Models/Common/Item.php index 6e0531836..d76360eff 100644 --- a/app/Models/Common/Item.php +++ b/app/Models/Common/Item.php @@ -27,7 +27,7 @@ class Item extends Model * * @var array */ - protected $fillable = ['company_id', 'name', 'description', 'sale_price', 'purchase_price', 'category_id', 'enabled']; + protected $fillable = ['company_id', 'name', 'description', 'sale_price', 'purchase_price', 'category_id', 'enabled', 'created_by']; /** * The attributes that should be cast. diff --git a/app/Models/Common/Recurring.php b/app/Models/Common/Recurring.php index 8e878050b..a3038f722 100644 --- a/app/Models/Common/Recurring.php +++ b/app/Models/Common/Recurring.php @@ -18,7 +18,6 @@ class Recurring extends Model */ protected $fillable = ['company_id', 'recurable_id', 'recurable_type', 'frequency', 'interval', 'started_at', 'count']; - /** * Get all of the owning recurable models. */ diff --git a/app/Models/Common/Report.php b/app/Models/Common/Report.php index cd624444f..85b6c6e7e 100644 --- a/app/Models/Common/Report.php +++ b/app/Models/Common/Report.php @@ -17,7 +17,7 @@ class Report extends Model * * @var array */ - protected $fillable = ['company_id', 'class', 'name', 'description', 'settings']; + protected $fillable = ['company_id', 'class', 'name', 'description', 'settings', 'created_by']; /** * The attributes that should be cast. diff --git a/app/Models/Common/Widget.php b/app/Models/Common/Widget.php index 8bf650a38..23ef451c8 100644 --- a/app/Models/Common/Widget.php +++ b/app/Models/Common/Widget.php @@ -16,7 +16,7 @@ class Widget extends Model * * @var array */ - protected $fillable = ['company_id', 'dashboard_id', 'class', 'name', 'sort', 'settings']; + protected $fillable = ['company_id', 'dashboard_id', 'class', 'name', 'sort', 'settings', 'created_by']; /** * The attributes that should be cast. diff --git a/app/Models/Document/Document.php b/app/Models/Document/Document.php index fb35b861e..d4a1df5c6 100644 --- a/app/Models/Document/Document.php +++ b/app/Models/Document/Document.php @@ -41,6 +41,7 @@ class Document extends Model 'amount', 'currency_code', 'currency_rate', + 'category_id', 'contact_id', 'contact_name', 'contact_email', @@ -48,9 +49,9 @@ class Document extends Model 'contact_phone', 'contact_address', 'notes', - 'category_id', - 'parent_id', 'footer', + 'parent_id', + 'created_by', ]; /** diff --git a/app/Models/Setting/Category.php b/app/Models/Setting/Category.php index aa65ce5c4..3e39bb193 100644 --- a/app/Models/Setting/Category.php +++ b/app/Models/Setting/Category.php @@ -18,7 +18,7 @@ class Category extends Model * * @var array */ - protected $fillable = ['company_id', 'name', 'type', 'color', 'enabled']; + protected $fillable = ['company_id', 'name', 'type', 'color', 'enabled', 'created_by']; /** * The attributes that should be cast. diff --git a/app/Models/Setting/Currency.php b/app/Models/Setting/Currency.php index dd65cdc1e..f70c908ca 100644 --- a/app/Models/Setting/Currency.php +++ b/app/Models/Setting/Currency.php @@ -19,7 +19,19 @@ class Currency extends Model * * @var array */ - protected $fillable = ['company_id', 'name', 'code', 'rate', 'enabled', 'precision', 'symbol', 'symbol_first', 'decimal_mark', 'thousands_separator']; + protected $fillable = [ + 'company_id', + 'name', + 'code', + 'rate', + 'enabled', + 'precision', + 'symbol', + 'symbol_first', + 'decimal_mark', + 'thousands_separator', + 'created_by', + ]; /** * The attributes that should be cast. diff --git a/app/Models/Setting/Setting.php b/app/Models/Setting/Setting.php index c2c6c0b9e..63be4e078 100644 --- a/app/Models/Setting/Setting.php +++ b/app/Models/Setting/Setting.php @@ -11,8 +11,6 @@ class Setting extends Eloquent protected $table = 'settings'; - protected $tenantable = true; - /** * Attributes that should be mass-assignable. * diff --git a/app/Models/Setting/Tax.php b/app/Models/Setting/Tax.php index a8c591166..ddc69940a 100644 --- a/app/Models/Setting/Tax.php +++ b/app/Models/Setting/Tax.php @@ -24,7 +24,7 @@ class Tax extends Model * * @var array */ - protected $fillable = ['company_id', 'name', 'rate', 'type', 'enabled']; + protected $fillable = ['company_id', 'name', 'rate', 'type', 'enabled', 'created_by']; /** * The attributes that should be cast. diff --git a/app/Traits/Owners.php b/app/Traits/Owners.php new file mode 100644 index 000000000..24df9558b --- /dev/null +++ b/app/Traits/Owners.php @@ -0,0 +1,18 @@ +ownable ?: true; + + return ($ownable === true) && in_array('created_by', $this->getFillable()); + } + + public function isNotOwnable() + { + return !$this->isOwnable(); + } +} diff --git a/app/Traits/Tenants.php b/app/Traits/Tenants.php index 8c11ea328..38c5a8f34 100644 --- a/app/Traits/Tenants.php +++ b/app/Traits/Tenants.php @@ -18,7 +18,9 @@ trait Tenants public function isTenantable() { - return (isset($this->tenantable) && ($this->tenantable === true)); + $tenantable = $this->tenantable ?: true; + + return ($tenantable === true) && in_array('company_id', $this->getFillable()); } public function isNotTenantable() diff --git a/app/Transformers/Banking/Account.php b/app/Transformers/Banking/Account.php index 7d0c042ca..ebd1034b9 100644 --- a/app/Transformers/Banking/Account.php +++ b/app/Transformers/Banking/Account.php @@ -25,6 +25,7 @@ class Account extends TransformerAbstract 'bank_phone' => $model->bank_phone, 'bank_address' => $model->bank_address, 'enabled' => $model->enabled, + 'created_by' => $model->created_by, 'created_at' => $model->created_at ? $model->created_at->toIso8601String() : '', 'updated_at' => $model->updated_at ? $model->updated_at->toIso8601String() : '', ]; diff --git a/app/Transformers/Banking/Reconciliation.php b/app/Transformers/Banking/Reconciliation.php index 409c11799..c91cbf74b 100644 --- a/app/Transformers/Banking/Reconciliation.php +++ b/app/Transformers/Banking/Reconciliation.php @@ -26,6 +26,7 @@ class Reconciliation extends TransformerAbstract 'ended_at' => $model->ended_at->toIso8601String(), 'closing_balance' => $model->closing_balance, 'reconciled' => $model->reconciled, + 'created_by' => $model->created_by, 'created_at' => $model->created_at->toIso8601String(), 'updated_at' => $model->updated_at->toIso8601String(), ]; diff --git a/app/Transformers/Banking/Transaction.php b/app/Transformers/Banking/Transaction.php index 535a7952e..5cfb1399d 100644 --- a/app/Transformers/Banking/Transaction.php +++ b/app/Transformers/Banking/Transaction.php @@ -37,6 +37,7 @@ class Transaction extends TransformerAbstract 'payment_method' => $model->payment_method, 'reference' => $model->reference, 'attachment' => $model->attachment, + 'created_by' => $model->created_by, 'created_at' => $model->created_at->toIso8601String(), 'updated_at' => $model->updated_at->toIso8601String(), ]; diff --git a/app/Transformers/Banking/Transfer.php b/app/Transformers/Banking/Transfer.php index a7fa92d89..8c5429bdb 100644 --- a/app/Transformers/Banking/Transfer.php +++ b/app/Transformers/Banking/Transfer.php @@ -31,6 +31,7 @@ class Transfer extends TransformerAbstract 'amount' => $expense_transaction->amount, 'currency_code' => $expense_transaction->currency_code, 'paid_at' => $expense_transaction->paid_at ? $expense_transaction->paid_at->toIso8601String() : '', + 'created_by' => $model->created_by, 'created_at' => $model->created_at ? $model->created_at->toIso8601String() : '', 'updated_at' => $model->updated_at ? $model->updated_at->toIso8601String() : '', ]; diff --git a/app/Transformers/Common/Company.php b/app/Transformers/Common/Company.php index 90a39797f..9b9eaf0b7 100644 --- a/app/Transformers/Common/Company.php +++ b/app/Transformers/Common/Company.php @@ -22,6 +22,7 @@ class Company extends TransformerAbstract 'address' => $model->address, 'logo' => $model->logo, 'enabled' => $model->enabled, + 'created_by' => $model->created_by, 'created_at' => $model->created_at ? $model->created_at->toIso8601String() : '', 'updated_at' => $model->updated_at ? $model->updated_at->toIso8601String() : '', ]; diff --git a/app/Transformers/Common/Contact.php b/app/Transformers/Common/Contact.php index 4cec09f31..442c25deb 100644 --- a/app/Transformers/Common/Contact.php +++ b/app/Transformers/Common/Contact.php @@ -27,6 +27,7 @@ class Contact extends TransformerAbstract 'currency_code' => $model->currency_code, 'enabled' => $model->enabled, 'reference' => $model->reference, + 'created_by' => $model->created_by, 'created_at' => $model->created_at ? $model->created_at->toIso8601String() : '', 'updated_at' => $model->updated_at ? $model->updated_at->toIso8601String() : '', ]; diff --git a/app/Transformers/Common/Dashboard.php b/app/Transformers/Common/Dashboard.php index fb9a3e6a9..f015ff041 100644 --- a/app/Transformers/Common/Dashboard.php +++ b/app/Transformers/Common/Dashboard.php @@ -24,6 +24,7 @@ class Dashboard extends TransformerAbstract 'company_id' => $model->company_id, 'name' => $model->name, 'enabled' => $model->enabled, + 'created_by' => $model->created_by, 'created_at' => $model->created_at ? $model->created_at->toIso8601String() : '', 'updated_at' => $model->updated_at ? $model->updated_at->toIso8601String() : '', ]; diff --git a/app/Transformers/Common/Item.php b/app/Transformers/Common/Item.php index a554e6ded..7aa671b5a 100644 --- a/app/Transformers/Common/Item.php +++ b/app/Transformers/Common/Item.php @@ -30,6 +30,7 @@ class Item extends TransformerAbstract 'tax_ids' => $model->tax_ids, 'picture' => $model->picture, 'enabled' => $model->enabled, + 'created_by' => $model->created_by, 'created_at' => $model->created_at ? $model->created_at->toIso8601String() : '', 'updated_at' => $model->updated_at ? $model->updated_at->toIso8601String() : '', ]; diff --git a/app/Transformers/Common/Report.php b/app/Transformers/Common/Report.php index 7e3a0df2e..4ac4b20dd 100644 --- a/app/Transformers/Common/Report.php +++ b/app/Transformers/Common/Report.php @@ -22,6 +22,7 @@ class Report extends TransformerAbstract 'description' => $model->description, 'settings' => $model->settings, 'data' => $this->getReportData($model), + 'created_by' => $model->created_by, 'created_at' => $model->created_at ? $model->created_at->toIso8601String() : '', 'updated_at' => $model->updated_at ? $model->updated_at->toIso8601String() : '', ]; diff --git a/app/Transformers/Common/Widget.php b/app/Transformers/Common/Widget.php index 5a366abe9..0b0895786 100644 --- a/app/Transformers/Common/Widget.php +++ b/app/Transformers/Common/Widget.php @@ -27,6 +27,7 @@ class Widget extends TransformerAbstract 'sort' => $model->sort, 'settings' => $model->settings, 'data' => show_widget($model), + 'created_by' => $model->created_by, 'created_at' => $model->created_at ? $model->created_at->toIso8601String() : '', 'updated_at' => $model->updated_at ? $model->updated_at->toIso8601String() : '', ]; diff --git a/app/Transformers/Document/Document.php b/app/Transformers/Document/Document.php index 6a51ef7bf..6663dd35d 100644 --- a/app/Transformers/Document/Document.php +++ b/app/Transformers/Document/Document.php @@ -41,6 +41,7 @@ class Document extends TransformerAbstract 'contact_address' => $model->contact_address, 'notes' => $model->notes, 'attachment' => $model->attachment, + 'created_by' => $model->created_by, 'created_at' => $model->created_at ? $model->created_at->toIso8601String() : '', 'updated_at' => $model->updated_at ? $model->updated_at->toIso8601String() : '', ]; diff --git a/app/Transformers/Setting/Category.php b/app/Transformers/Setting/Category.php index 9fc1f0927..e53b09a72 100644 --- a/app/Transformers/Setting/Category.php +++ b/app/Transformers/Setting/Category.php @@ -20,6 +20,7 @@ class Category extends TransformerAbstract 'type' => $model->type, 'color' => $model->color, 'enabled' => $model->enabled, + 'created_by' => $model->created_by, 'created_at' => $model->created_at ? $model->created_at->toIso8601String() : '', 'updated_at' => $model->updated_at ? $model->updated_at->toIso8601String() : '', ]; diff --git a/app/Transformers/Setting/Currency.php b/app/Transformers/Setting/Currency.php index e74d9e134..2e417efc3 100644 --- a/app/Transformers/Setting/Currency.php +++ b/app/Transformers/Setting/Currency.php @@ -25,6 +25,7 @@ class Currency extends TransformerAbstract 'symbol_first' => $model->symbol_first, 'decimal_mark' => $model->decimal_mark, 'thousands_separator' => $model->thousands_separator, + 'created_by' => $model->created_by, 'created_at' => $model->created_at ? $model->created_at->toIso8601String() : '', 'updated_at' => $model->updated_at ? $model->updated_at->toIso8601String() : '', ]; diff --git a/app/Transformers/Setting/Tax.php b/app/Transformers/Setting/Tax.php index 2581fedcb..eee44b408 100644 --- a/app/Transformers/Setting/Tax.php +++ b/app/Transformers/Setting/Tax.php @@ -19,6 +19,7 @@ class Tax extends TransformerAbstract 'name' => $model->name, 'rate' => $model->rate, 'enabled' => $model->enabled, + 'created_by' => $model->created_by, 'created_at' => $model->created_at ? $model->created_at->toIso8601String() : '', 'updated_at' => $model->updated_at ? $model->updated_at->toIso8601String() : '', ]; diff --git a/app/Utilities/helpers.php b/app/Utilities/helpers.php index 6a14ac412..12b82a177 100644 --- a/app/Utilities/helpers.php +++ b/app/Utilities/helpers.php @@ -24,6 +24,18 @@ if (!function_exists('user')) { } } +if (!function_exists('user_id')) { + /** + * Get id of current user. + * + * @return int + */ + function user_id() + { + return optional(user())->id; + } +} + if (!function_exists('company_date_format')) { /** * Format the given date based on company settings. diff --git a/database/migrations/2021_06_17_000000_core_v2117.php b/database/migrations/2021_06_17_000000_core_v2117.php new file mode 100644 index 000000000..76483b596 --- /dev/null +++ b/database/migrations/2021_06_17_000000_core_v2117.php @@ -0,0 +1,82 @@ +unsignedInteger('created_by')->nullable()->after('enabled'); + }); + + Schema::table('categories', function (Blueprint $table) { + $table->unsignedInteger('created_by')->nullable()->after('enabled'); + }); + + Schema::table('companies', function (Blueprint $table) { + $table->unsignedInteger('created_by')->nullable()->after('enabled'); + }); + + Schema::table('contacts', function (Blueprint $table) { + $table->unsignedInteger('created_by')->nullable()->after('reference'); + }); + + Schema::table('currencies', function (Blueprint $table) { + $table->unsignedInteger('created_by')->nullable()->after('enabled'); + }); + + Schema::table('dashboards', function (Blueprint $table) { + $table->unsignedInteger('created_by')->nullable()->after('enabled'); + }); + + Schema::table('documents', function (Blueprint $table) { + $table->unsignedInteger('created_by')->nullable()->after('parent_id'); + }); + + Schema::table('items', function (Blueprint $table) { + $table->unsignedInteger('created_by')->nullable()->after('enabled'); + }); + + Schema::table('reconciliations', function (Blueprint $table) { + $table->unsignedInteger('created_by')->nullable()->after('reconciled'); + }); + + Schema::table('reports', function (Blueprint $table) { + $table->unsignedInteger('created_by')->nullable()->after('settings'); + }); + + Schema::table('taxes', function (Blueprint $table) { + $table->unsignedInteger('created_by')->nullable()->after('enabled'); + }); + + Schema::table('transactions', function (Blueprint $table) { + $table->unsignedInteger('created_by')->nullable()->after('parent_id'); + }); + + Schema::table('transfers', function (Blueprint $table) { + $table->unsignedInteger('created_by')->nullable()->after('income_transaction_id'); + }); + + Schema::table('widgets', function (Blueprint $table) { + $table->unsignedInteger('created_by')->nullable()->after('settings'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + // + } +}