From 14b568b735d748915bde72a08641b3ff93202384 Mon Sep 17 00:00:00 2001 From: Deon George Date: Fri, 6 Jul 2018 16:57:49 +1000 Subject: [PATCH] Work in progress determining user type --- app/Models/Account.php | 10 ++ app/Models/Language.php | 1 + app/Models/Rtm.php | 13 ++ app/Models/Site.php | 1 + app/User.php | 169 ++++++++++-------- composer.lock | 47 +++++ .../2018_06_22_062015_account_add_user.php | 35 ++++ .../2018_06_22_062022_user_add_heirachy.php | 34 ++++ database/seeds/CountryTableSeeder.php | 2 +- database/seeds/CurrencyTableSeeder.php | 1 + database/seeds/DatabaseSeeder.php | 12 +- database/seeds/LanguageTableSeeder.php | 22 +++ database/seeds/RtmTableSeeder.php | 23 +++ database/seeds/SiteTableSeeder.php | 24 +++ database/seeds/UserTableSeeder.php | 71 ++++++++ 15 files changed, 385 insertions(+), 80 deletions(-) create mode 100644 app/Models/Account.php create mode 100644 app/Models/Rtm.php create mode 100644 database/migrations/2018_06_22_062015_account_add_user.php create mode 100644 database/migrations/2018_06_22_062022_user_add_heirachy.php create mode 100644 database/seeds/LanguageTableSeeder.php create mode 100644 database/seeds/RtmTableSeeder.php create mode 100644 database/seeds/SiteTableSeeder.php create mode 100644 database/seeds/UserTableSeeder.php diff --git a/app/Models/Account.php b/app/Models/Account.php new file mode 100644 index 0000000..c932ce9 --- /dev/null +++ b/app/Models/Account.php @@ -0,0 +1,10 @@ +'array', diff --git a/app/User.php b/app/User.php index 3dbadd7..ad42e3c 100644 --- a/app/User.php +++ b/app/User.php @@ -2,17 +2,17 @@ namespace App; -use Laravel\Passport\HasApiTokens; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; +use Illuminate\Support\Facades\Cache; +use Leenooks\Carbon; +use App\Models\Account; class User extends Authenticatable { - protected $table = 'ab_account'; - // @todo We cannot use timestamps - we should create the normal timestamps columns under laravel. - public $timestamps = FALSE; + use Notifiable; - use HasApiTokens, Notifiable; + protected $dates = ['created_at','updated_at','last_access']; /** * The attributes that are mass assignable. @@ -32,81 +32,100 @@ class User extends Authenticatable 'password', 'remember_token', ]; - /** - * Return the country the user belongs to - */ - public function country() + + public function accounts() { - return $this->belongsTo(Models\Country::class); + return $this->hasMany(Models\Account::class); } - /** - * This users invoices - */ - public function invoices() - { - return $this->hasMany(Models\Invoice::class,'account_id'); - } + /** + * Logged in users full name + * + * @return string + */ + public function getFullNameAttribute() + { + return sprintf('%s %s',$this->firstname,$this->lastname); + } - public function language() - { - return $this->belongsTo(Models\Language::class); - } + /** + * Return a Carbon Date if it has a value. + * + * @param $value + * @return Leenooks\Carbon + * @todo This attribute is not in the schema + */ + public function getLastAccessAttribute($value) + { + if (! is_null($value)) + return new Carbon($value); + } - public function payments() - { - return $this->hasMany(Models\Payment::class,'account_id'); - } - - /** - * This users invoices - */ - public function services() - { - return $this->hasMany(Models\Service::class,'account_id'); - } - - /** - * Only query active categories - */ - public function scopeActive() - { - return $this->where('active',TRUE); - } - - /** - * Return the user's full name - */ - public function getFullNameAttribute() - { - return $this->first_name.' '.$this->last_name; - } - - public function getInvoicesDueAttribute() - { - return $this->invoices - ->where('active',TRUE) - ->sortBy('id') - ->transform(function ($item) { if ((float) $item->due) return $item; }) - ->reverse() - ->filter(); - } - - public function getPaymentHistoryAttribute() - { - return $this->payments - ->sortBy('date_payment') - ->reverse(); - } - - public function getNameAttribute() - { + /** + * @deprecated Use static::getFullNameAttribute() + * @return mixed + */ + public function getNameAttribute() + { return $this->full_name; - } + } - public function getServicesActiveAttribute() - { - return $this->services - ->where('active',TRUE); - } + protected function agents() { + return $this->hasMany(static::class,'parent_id','id'); + } + + protected function clients() { + return $this->hasMany(\App\User::class); + } + + protected function supplier() + { + return $this->belongsTo(static::class,'parent_id','id'); + } + + protected function suppliers() { + return $this->hasMany(static::class,'parent_id','id'); + } + + // List all the agents, including agents of agents + public function all_agents() + { + $result = collect(); + + foreach ($this->agents()->orderBy('id')->get() as $o) + { + if (! $o->active) + continue; + + $result->push($o->all_agents()); + $result->push($this); + } + + return $result->flatten(); + } + + public function all_clients() + { + // List all the clients of my agents + } + + public function all_suppliers() + { + // For each supplier, so if that supplier has a parent + } + + public function role() + { + // If I have agents and no parent, I am the wholesaler + if (is_null($this->parent_id) AND $this->all_agents()->count()) + return 'Wholesaler'; + + // If I have agents and a parent, I am a reseller + elseif ($this->parent_id AND $this->all_agents()->count()) + return 'Reseller'; + + // If I have no agents and a parent, I am a customer + elseif (! $this->all_agents()->count()) + return 'Customer'; + } } \ No newline at end of file diff --git a/composer.lock b/composer.lock index a9ef939..5a25bcd 100644 --- a/composer.lock +++ b/composer.lock @@ -3442,6 +3442,53 @@ ], "time": "2018-05-26T01:33:24+00:00" }, + { + "name": "quickbooks/v3-php-sdk", + "version": "v5.0.1", + "source": { + "type": "git", + "url": "https://github.com/intuit/QuickBooks-V3-PHP-SDK.git", + "reference": "f1b1db3171dc2005e072a36ed7d240ccc0412c15" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/intuit/QuickBooks-V3-PHP-SDK/zipball/f1b1db3171dc2005e072a36ed7d240ccc0412c15", + "reference": "f1b1db3171dc2005e072a36ed7d240ccc0412c15", + "shasum": "" + }, + "require": { + "php": ">=5.6.0" + }, + "require-dev": { + "phpunit/phpunit": "^5.7" + }, + "type": "library", + "autoload": { + "psr-4": { + "QuickBooksOnline\\API\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "Apache-2.0" + ], + "authors": [ + { + "name": "hlu2", + "email": "Hao_Lu@intuit.com" + } + ], + "description": "The Official PHP SDK for QuickBooks Online Accounting API", + "homepage": "http://developer.intuit.com", + "keywords": [ + "api", + "http", + "quickbooks", + "rest", + "smallbusiness" + ], + "time": "2018-05-26T01:33:24+00:00" + }, { "name": "ramsey/uuid", "version": "3.7.3", diff --git a/database/migrations/2018_06_22_062015_account_add_user.php b/database/migrations/2018_06_22_062015_account_add_user.php new file mode 100644 index 0000000..2c9823f --- /dev/null +++ b/database/migrations/2018_06_22_062015_account_add_user.php @@ -0,0 +1,35 @@ +integer('user_id')->nullable()->unsigned(); + $table->foreign('user_id')->references('id')->on('users'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('ab_account', function (Blueprint $table) { + $table->dropForeign(['user_id']); + $table->dropColumn('user_id'); + }); + } +} \ No newline at end of file diff --git a/database/migrations/2018_06_22_062022_user_add_heirachy.php b/database/migrations/2018_06_22_062022_user_add_heirachy.php new file mode 100644 index 0000000..1d349f5 --- /dev/null +++ b/database/migrations/2018_06_22_062022_user_add_heirachy.php @@ -0,0 +1,34 @@ +integer('parent_id')->nullable()->unsigned(); + $table->foreign('parent_id')->references('id')->on('users'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('users', function (Blueprint $table) { + $table->dropForeign(['parent_id']); + $table->dropColumn('parent_id'); + }); + } +} \ No newline at end of file diff --git a/database/seeds/CountryTableSeeder.php b/database/seeds/CountryTableSeeder.php index 01d847e..ea64a1b 100644 --- a/database/seeds/CountryTableSeeder.php +++ b/database/seeds/CountryTableSeeder.php @@ -14,10 +14,10 @@ class CountryTableSeeder extends Seeder public function run() { $o = new Country; + $o->id = 61; $o->name = 'Australia'; $o->two_code = 'AU'; $o->three_code = 'AUS'; - $o->currency_id = '61'; $o->active = TRUE; $oo = Currency::where('iso_code','AUD')->firstOrFail(); diff --git a/database/seeds/CurrencyTableSeeder.php b/database/seeds/CurrencyTableSeeder.php index 83a0402..58adadb 100644 --- a/database/seeds/CurrencyTableSeeder.php +++ b/database/seeds/CurrencyTableSeeder.php @@ -14,6 +14,7 @@ class CurrencyTableSeeder extends Seeder public function run() { $o = new Currency; + $o->id = 610; $o->name = 'Australian Dollars'; $o->symbol = '$'; $o->iso_code = 'AUD'; diff --git a/database/seeds/DatabaseSeeder.php b/database/seeds/DatabaseSeeder.php index 7f7921f..290cc66 100644 --- a/database/seeds/DatabaseSeeder.php +++ b/database/seeds/DatabaseSeeder.php @@ -11,9 +11,13 @@ class DatabaseSeeder extends Seeder */ public function run() { - $this->call([ - CurrencyTableSeeder::class, - CountryTableSeeder::class - ]); + $this->call([ + CurrencyTableSeeder::class, + CountryTableSeeder::class, + LanguageTableSeeder::class, + SiteTableSeeder::class, + UserTableSeeder::class, + RtmTableSeeder::class, + ]); } } diff --git a/database/seeds/LanguageTableSeeder.php b/database/seeds/LanguageTableSeeder.php new file mode 100644 index 0000000..95eb26f --- /dev/null +++ b/database/seeds/LanguageTableSeeder.php @@ -0,0 +1,22 @@ +id = 1; + $o->name = 'English'; + $o->iso = 'en_EN'; + $o->save(); + } +} \ No newline at end of file diff --git a/database/seeds/RtmTableSeeder.php b/database/seeds/RtmTableSeeder.php new file mode 100644 index 0000000..5275e55 --- /dev/null +++ b/database/seeds/RtmTableSeeder.php @@ -0,0 +1,23 @@ +id = 1; + $o->site_id = 1; + $o->account_id = 1; + $o->name = 'Wholesaler'; + $o->save(); + } +} \ No newline at end of file diff --git a/database/seeds/SiteTableSeeder.php b/database/seeds/SiteTableSeeder.php new file mode 100644 index 0000000..ead3591 --- /dev/null +++ b/database/seeds/SiteTableSeeder.php @@ -0,0 +1,24 @@ +id = 1; + $o->country_id = 61; + $o->language_id = 1; + $o->currency_id = 610; + $o->url = 'test'; + $o->save(); + } +} \ No newline at end of file diff --git a/database/seeds/UserTableSeeder.php b/database/seeds/UserTableSeeder.php new file mode 100644 index 0000000..028073d --- /dev/null +++ b/database/seeds/UserTableSeeder.php @@ -0,0 +1,71 @@ +id = 1; + $o->site_id = 1; + $o->language_id = 1; + $o->country_id = 61; + $o->currency_id = 610; + $o->email = 'wholesaler@example.com'; + $o->save(); + + $o = new User; + $o->id = 10; + $o->site_id = 1; + $o->language_id = 1; + $o->country_id = 61; + $o->currency_id = 610; + $o->email = 'reseller1-0@example.com'; + $o->save(); + + $o = new User; + $o->id = 11; + $o->site_id = 1; + $o->language_id = 1; + $o->country_id = 61; + $o->currency_id = 610; + $o->email = 'reseller2-0@example.com'; + $o->save(); + + $o = new User; + $o->id = 110; + $o->site_id = 1; + $o->language_id = 1; + $o->country_id = 61; + $o->currency_id = 610; + $o->email = 'reseller2-1@example.com'; + $o->save(); + + $o = new User; + $o->id = 1010; + $o->site_id = 1; + $o->language_id = 1; + $o->country_id = 61; + $o->currency_id = 610; + $o->email = 'user1-0-1@example.com'; + $o->save(); + + $o = new User; + $o->id = 1110; + $o->site_id = 1; + $o->language_id = 1; + $o->country_id = 61; + $o->currency_id = 610; + $o->email = 'user2-1-1@example.com'; + $o->save(); + + } +} \ No newline at end of file