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
{
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
{
protected $table = 'ab_setup';
public $timestamps = FALSE;
protected $casts = [
'address'=>'array',

View File

@ -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';
}
}

47
composer.lock generated
View File

@ -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",

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()
{
$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();

View File

@ -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';

View File

@ -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,
]);
}
}

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();
}
}