diff --git a/app/Http/Controllers/OrderController.php b/app/Http/Controllers/OrderController.php index 09e899b..57bc1fc 100644 --- a/app/Http/Controllers/OrderController.php +++ b/app/Http/Controllers/OrderController.php @@ -8,7 +8,7 @@ use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Validator; use Illuminate\Database\Eloquent\Model; -use App\Models\{Product,Service}; +use App\Models\{Account,Product,Service}; use App\User; class OrderController extends Controller @@ -37,33 +37,70 @@ class OrderController extends Controller Validator::make($request->all(),[ 'product_id'=>'required|exists:ab_product,id', ]) - ->sometimes('order_email','required|email',function($input) use ($request) { - return ($input->order_email AND ! $input->order_email_manual) OR (! $input->order_email_manual); + // Reseller + ->sometimes('account_id','required|email',function($input) use ($request) { + return is_null($input->account_id) AND is_null($input->order_email_manual); }) - ->sometimes('order_email_manual','required|email',function($input) use ($request) { - return $input->order_email_manual AND ! $input->order_email; - })->validate(); + // Un-Authed User + ->sometimes('order_email_manual','required|email|unique:users,email,NULL,id',function($input) use ($request) { + return (is_null($input->order_email_manual) AND ! isset($input->account_id)) OR $input->order_email_manual; + }) + // Authed User + ->sometimes('account_id','required|email',function($input) use ($request) { + return is_null($input->account_id) AND ! isset($input->order_email_manual); + }) + ->validate(); + // Check the plugin details. - $po = Product::findOrFail($request->post('product_id')); + $po = Product::findOrFail($request->input('product_id')); // Check we have the custom attributes for the product $options = $po->orderValidation($request); - $uo = User::where('email','=',$request->post('order_email') ?: $request->post('order_email_manual'))->firstOrFail(); + $uo = User::firstOrNew(['email'=>$request->input('order_email') ?: $request->input('order_email_manual')]); - $ao = $request->input('account_id') - ? $uo->accounts->where('account_id',$request->input('account_id')) - : $uo->accounts->first(); + // If this is a new client + if (! $uo->exists) + { + // @todo Make this automatic + $uo->site_id = config('SITE_SETUP')->id; + $uo->active = FALSE; + $uo->firstname = ''; + $uo->lastname = ''; + $uo->country_id = config('SITE_SETUP')->country_id; // @todo This might be wrong + $uo->parent_id = Auth::id() ?: 1; // @todo This should be configured to a default user + $uo->active = 1; + $uo->save(); + } + + // If we have a new account. + if (is_null($request->input('account_id'))) + { + $ao = new Account; + //$ao->id = Account::NextId(); + // @todo Make this automatic + $ao->site_id = config('SITE_SETUP')->id; + $ao->country_id = config('SITE_SETUP')->country_id; // @todo This might be wrong + $ao->language_id = config('SITE_SETUP')->language_id; // @todo This might be wrong + $ao->currency_id = config('SITE_SETUP')->currency_id; // @todo This might be wrong + $ao->active = 1; + $uo->accounts()->save($ao); + + } else { + // @todo This logic may be wrong, if account_id is not set, it'll pick the first account, which may be wrong. + $ao = $request->input('account_id') + ? $uo->accounts->where('account_id',$request->input('account_id')) + : $uo->accounts->first(); + } $so = new Service; - $so->id = Service::NextId(); // @todo Make this automatic $so->site_id = config('SITE_SETUP')->id; - $so->product_id = $request->post('product_id'); + $so->product_id = $request->input('product_id'); $so->order_status = 'ORDER-SUBMIT'; - $so->orderby_id = Auth::user()->id; + $so->orderby_id = Auth::id(); if ($options->order_info) { diff --git a/app/Models/Account.php b/app/Models/Account.php index 7a6c7ea..da559a7 100644 --- a/app/Models/Account.php +++ b/app/Models/Account.php @@ -4,8 +4,13 @@ namespace App\Models; use Illuminate\Database\Eloquent\Model; +use App\Traits\NextKey; + class Account extends Model { + use NextKey; + public $incrementing = FALSE; + protected $table = 'ab_account'; public $timestamps = FALSE; diff --git a/app/Models/Service.php b/app/Models/Service.php index 60622e4..3154c1f 100644 --- a/app/Models/Service.php +++ b/app/Models/Service.php @@ -8,6 +8,7 @@ use App\Traits\NextKey; class Service extends Model { use NextKey; + public $incrementing = FALSE; protected $table = 'ab_service'; protected $with = ['product.descriptions','account.language','service_adsl','service_domain.tld','service_ssl','service_voip']; @@ -15,7 +16,6 @@ class Service extends Model protected $casts = [ 'order_info'=>'array', ]; - public $incrementing = FALSE; const CREATED_AT = 'date_orig'; const UPDATED_AT = 'date_last'; diff --git a/app/User.php b/app/User.php index 37ce9e2..d82e3fa 100644 --- a/app/User.php +++ b/app/User.php @@ -237,12 +237,11 @@ class User extends Authenticatable $o->level = $level; - $result->push($o); - // Include clients of agents $result->push($o->all_clients($level+1)); } + $result->push($this); return $result->flatten(); } diff --git a/resources/theme/backend/adminlte/r/accounts.blade.php b/resources/theme/backend/adminlte/r/accounts.blade.php index a436e79..f683dec 100644 --- a/resources/theme/backend/adminlte/r/accounts.blade.php +++ b/resources/theme/backend/adminlte/r/accounts.blade.php @@ -1,4 +1,4 @@ -