Improvements to ordering

This commit is contained in:
Deon George
2018-08-20 22:15:28 +10:00
parent e9b5f9ac0c
commit 8d9b0c7027
14 changed files with 217 additions and 124 deletions

View File

@@ -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)
{

View File

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

View File

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

View File

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