2018-08-09 14:10:51 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Igaster\LaravelTheme\Facades\Theme;
|
2018-08-11 05:09:41 +00:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
use Illuminate\Support\Facades\Validator;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
|
2018-08-20 12:15:28 +00:00
|
|
|
use App\Models\{Account,Product,Service};
|
2018-08-11 05:09:41 +00:00
|
|
|
use App\User;
|
2018-08-09 14:10:51 +00:00
|
|
|
|
|
|
|
class OrderController extends Controller
|
|
|
|
{
|
|
|
|
public function index()
|
|
|
|
{
|
|
|
|
return view('order');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function product_order(Product $o)
|
|
|
|
{
|
|
|
|
Theme::set('metronic-fe');
|
|
|
|
|
|
|
|
return view('widgets.product_order',['o'=>$o]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function product_info(Product $o)
|
|
|
|
{
|
|
|
|
Theme::set('metronic-fe');
|
|
|
|
|
|
|
|
return view('widgets.product_description',['o'=>$o]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function submit(Request $request)
|
|
|
|
{
|
2018-08-11 05:09:41 +00:00
|
|
|
Validator::make($request->all(),[
|
|
|
|
'product_id'=>'required|exists:ab_product,id',
|
|
|
|
])
|
2018-08-20 12:15:28 +00:00
|
|
|
// Reseller
|
|
|
|
->sometimes('account_id','required|email',function($input) use ($request) {
|
|
|
|
return is_null($input->account_id) AND is_null($input->order_email_manual);
|
2018-08-11 05:09:41 +00:00
|
|
|
})
|
2018-08-20 12:15:28 +00:00
|
|
|
// 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();
|
|
|
|
|
2018-08-11 05:09:41 +00:00
|
|
|
|
|
|
|
// Check the plugin details.
|
2018-08-20 12:15:28 +00:00
|
|
|
$po = Product::findOrFail($request->input('product_id'));
|
2018-08-11 05:09:41 +00:00
|
|
|
|
|
|
|
// Check we have the custom attributes for the product
|
|
|
|
$options = $po->orderValidation($request);
|
|
|
|
|
2018-08-20 12:15:28 +00:00
|
|
|
$uo = User::firstOrNew(['email'=>$request->input('order_email') ?: $request->input('order_email_manual')]);
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
}
|
2018-08-11 05:09:41 +00:00
|
|
|
|
2018-08-20 12:15:28 +00:00
|
|
|
// 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();
|
|
|
|
}
|
2018-08-11 05:09:41 +00:00
|
|
|
|
|
|
|
$so = new Service;
|
|
|
|
|
|
|
|
// @todo Make this automatic
|
|
|
|
$so->site_id = config('SITE_SETUP')->id;
|
2018-08-20 12:15:28 +00:00
|
|
|
$so->product_id = $request->input('product_id');
|
2018-08-11 05:09:41 +00:00
|
|
|
$so->order_status = 'ORDER-SUBMIT';
|
2018-08-20 12:15:28 +00:00
|
|
|
$so->orderby_id = Auth::id();
|
2018-08-11 05:09:41 +00:00
|
|
|
|
|
|
|
if ($options->order_info)
|
|
|
|
{
|
|
|
|
$so->order_info = $options->order_info;
|
|
|
|
unset($options->order_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
$so = $ao->services()->save($so);
|
|
|
|
|
|
|
|
if ($options instanceOf Model) {
|
|
|
|
$options->service_id = $so->id;
|
|
|
|
$options->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
return view('order_received',['o'=>$so]);
|
2018-08-09 14:10:51 +00:00
|
|
|
}
|
|
|
|
}
|