Work in progress determining user type

This commit is contained in:
Deon George 2018-07-06 16:57:49 +10:00
parent 1ac764f05e
commit 14b568b735
15 changed files with 385 additions and 80 deletions

10
app/Models/Account.php Normal file
View File

@ -0,0 +1,10 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Account extends Model
{
protected $table = 'ab_account';
}

View File

@ -7,4 +7,5 @@ use Illuminate\Database\Eloquent\Model;
class Language extends Model class Language extends Model
{ {
protected $table = 'ab_language'; protected $table = 'ab_language';
public $timestamps = FALSE;
} }

13
app/Models/Rtm.php Normal file
View File

@ -0,0 +1,13 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Rtm extends Model
{
protected $table = 'ab_rtm';
public $timestamps = FALSE;
}

View File

@ -7,6 +7,7 @@ use Illuminate\Database\Eloquent\Model;
class Site extends Model class Site extends Model
{ {
protected $table = 'ab_setup'; protected $table = 'ab_setup';
public $timestamps = FALSE;
protected $casts = [ protected $casts = [
'address'=>'array', 'address'=>'array',

View File

@ -2,17 +2,17 @@
namespace App; namespace App;
use Laravel\Passport\HasApiTokens;
use Illuminate\Notifications\Notifiable; use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Support\Facades\Cache;
use Leenooks\Carbon;
use App\Models\Account;
class User extends Authenticatable class User extends Authenticatable
{ {
protected $table = 'ab_account'; use Notifiable;
// @todo We cannot use timestamps - we should create the normal timestamps columns under laravel.
public $timestamps = FALSE;
use HasApiTokens, Notifiable; protected $dates = ['created_at','updated_at','last_access'];
/** /**
* The attributes that are mass assignable. * The attributes that are mass assignable.
@ -32,81 +32,100 @@ class User extends Authenticatable
'password', 'remember_token', 'password', 'remember_token',
]; ];
/**
* Return the country the user belongs to public function accounts()
*/
public function country()
{ {
return $this->belongsTo(Models\Country::class); return $this->hasMany(Models\Account::class);
} }
/** /**
* This users invoices * Logged in users full name
*/ *
public function invoices() * @return string
{
return $this->hasMany(Models\Invoice::class,'account_id');
}
public function language()
{
return $this->belongsTo(Models\Language::class);
}
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() public function getFullNameAttribute()
{ {
return $this->first_name.' '.$this->last_name; return sprintf('%s %s',$this->firstname,$this->lastname);
} }
public function getInvoicesDueAttribute() /**
* 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)
{ {
return $this->invoices if (! is_null($value))
->where('active',TRUE) return new Carbon($value);
->sortBy('id')
->transform(function ($item) { if ((float) $item->due) return $item; })
->reverse()
->filter();
}
public function getPaymentHistoryAttribute()
{
return $this->payments
->sortBy('date_payment')
->reverse();
} }
/**
* @deprecated Use static::getFullNameAttribute()
* @return mixed
*/
public function getNameAttribute() public function getNameAttribute()
{ {
return $this->full_name; return $this->full_name;
} }
public function getServicesActiveAttribute() 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->services return $this->belongsTo(static::class,'parent_id','id');
->where('active',TRUE); }
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';
} }
} }

47
composer.lock generated
View File

@ -3442,6 +3442,53 @@
], ],
"time": "2018-05-26T01:33:24+00:00" "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", "name": "ramsey/uuid",
"version": "3.7.3", "version": "3.7.3",

View File

@ -0,0 +1,35 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AccountAddUser extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('ab_account', function (Blueprint $table) {
// @todo Change this to not nullable
$table->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');
});
}
}

View File

@ -0,0 +1,34 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class UserAddHeirachy extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->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');
});
}
}

View File

@ -14,10 +14,10 @@ class CountryTableSeeder extends Seeder
public function run() public function run()
{ {
$o = new Country; $o = new Country;
$o->id = 61;
$o->name = 'Australia'; $o->name = 'Australia';
$o->two_code = 'AU'; $o->two_code = 'AU';
$o->three_code = 'AUS'; $o->three_code = 'AUS';
$o->currency_id = '61';
$o->active = TRUE; $o->active = TRUE;
$oo = Currency::where('iso_code','AUD')->firstOrFail(); $oo = Currency::where('iso_code','AUD')->firstOrFail();

View File

@ -14,6 +14,7 @@ class CurrencyTableSeeder extends Seeder
public function run() public function run()
{ {
$o = new Currency; $o = new Currency;
$o->id = 610;
$o->name = 'Australian Dollars'; $o->name = 'Australian Dollars';
$o->symbol = '$'; $o->symbol = '$';
$o->iso_code = 'AUD'; $o->iso_code = 'AUD';

View File

@ -13,7 +13,11 @@ class DatabaseSeeder extends Seeder
{ {
$this->call([ $this->call([
CurrencyTableSeeder::class, CurrencyTableSeeder::class,
CountryTableSeeder::class CountryTableSeeder::class,
LanguageTableSeeder::class,
SiteTableSeeder::class,
UserTableSeeder::class,
RtmTableSeeder::class,
]); ]);
} }
} }

View File

@ -0,0 +1,22 @@
<?php
use Illuminate\Database\Seeder;
use App\Models\Language;
class LanguageTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$o = new Language;
$o->id = 1;
$o->name = 'English';
$o->iso = 'en_EN';
$o->save();
}
}

View File

@ -0,0 +1,23 @@
<?php
use Illuminate\Database\Seeder;
use App\Models\Rtm;
class RtmTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$o = new Rtm;
$o->id = 1;
$o->site_id = 1;
$o->account_id = 1;
$o->name = 'Wholesaler';
$o->save();
}
}

View File

@ -0,0 +1,24 @@
<?php
use Illuminate\Database\Seeder;
use App\Models\Site;
class SiteTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$o = new Site;
$o->id = 1;
$o->country_id = 61;
$o->language_id = 1;
$o->currency_id = 610;
$o->url = 'test';
$o->save();
}
}

View File

@ -0,0 +1,71 @@
<?php
use Illuminate\Database\Seeder;
use App\User;
class UserTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$o = new User;
$o->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();
}
}