2018-08-09 14:10:51 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use Illuminate\Http\Request;
|
2018-08-11 05:09:41 +00:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
2019-01-24 03:40:33 +00:00
|
|
|
use Illuminate\Support\Facades\Mail;
|
2018-08-11 05:09:41 +00:00
|
|
|
use Illuminate\Support\Facades\Validator;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
|
2019-01-24 03:40:33 +00:00
|
|
|
use App\Mail\OrderRequest;
|
2021-06-29 03:18:52 +00:00
|
|
|
use App\Models\{Account,Product,Service,User};
|
2018-08-09 14:10:51 +00:00
|
|
|
|
|
|
|
class OrderController extends Controller
|
|
|
|
{
|
2021-12-24 01:14:01 +00:00
|
|
|
// @todo To check
|
2021-06-29 03:18:52 +00:00
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->middleware('auth');
|
|
|
|
}
|
|
|
|
|
2021-12-24 01:14:01 +00:00
|
|
|
// @todo To check
|
2018-08-09 14:10:51 +00:00
|
|
|
public function index()
|
|
|
|
{
|
2024-07-04 05:03:11 +00:00
|
|
|
return view('theme.backend.adminlte.order.home');
|
2018-08-09 14:10:51 +00:00
|
|
|
}
|
|
|
|
|
2021-12-24 01:14:01 +00:00
|
|
|
// @todo To check
|
2018-08-09 14:10:51 +00:00
|
|
|
public function product_order(Product $o)
|
|
|
|
{
|
2024-07-04 05:03:11 +00:00
|
|
|
return view('theme.backend.adminlte.order.widget.order')
|
|
|
|
->with('o',$o);
|
2018-08-09 14:10:51 +00:00
|
|
|
}
|
|
|
|
|
2021-12-24 01:14:01 +00:00
|
|
|
// @todo To check
|
2018-08-09 14:10:51 +00:00
|
|
|
public function product_info(Product $o)
|
|
|
|
{
|
2024-07-04 05:03:11 +00:00
|
|
|
return view('theme.backend.adminlte.order.widget.info')
|
|
|
|
->with('o',$o);
|
2018-08-09 14:10:51 +00:00
|
|
|
}
|
|
|
|
|
2021-12-24 01:14:01 +00:00
|
|
|
// @todo To check
|
2018-08-09 14:10:51 +00:00
|
|
|
public function submit(Request $request)
|
|
|
|
{
|
2021-12-24 01:14:01 +00:00
|
|
|
Validator::make($request->all(),['product_id'=>'required|exists:products,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
|
2021-12-24 01:14:01 +00:00
|
|
|
$order = $po->orderValidation($request);
|
2018-08-11 05:09:41 +00:00
|
|
|
|
2021-07-09 01:39:27 +00:00
|
|
|
if ($request->input('order_email_manual')) {
|
2018-08-20 12:53:16 +00:00
|
|
|
$uo = User::firstOrNew(['email'=>$request->input('order_email_manual')]);
|
|
|
|
|
|
|
|
// If this is a new client
|
2021-07-09 01:39:27 +00:00
|
|
|
if (! $uo->exists) {
|
2018-08-20 12:53:16 +00:00
|
|
|
// @todo Make this automatic
|
2021-12-17 05:09:03 +00:00
|
|
|
$uo->site_id = config('site')->site_id;
|
2018-08-20 12:53:16 +00:00
|
|
|
$uo->active = FALSE;
|
|
|
|
$uo->firstname = '';
|
|
|
|
$uo->lastname = '';
|
2021-12-17 05:09:03 +00:00
|
|
|
$uo->country_id = config('site')->country_id; // @todo This might be wrong
|
2018-08-20 12:53:16 +00:00
|
|
|
$uo->parent_id = Auth::id() ?: 1; // @todo This should be configured to a default user
|
2024-01-31 22:51:58 +00:00
|
|
|
$uo->language_id = config('site')->language_id; // @todo This might be wrong
|
2018-08-20 12:53:16 +00:00
|
|
|
$uo->active = 1;
|
|
|
|
$uo->save();
|
|
|
|
}
|
2018-08-20 12:15:28 +00:00
|
|
|
}
|
2018-08-11 05:09:41 +00:00
|
|
|
|
2018-08-20 12:15:28 +00:00
|
|
|
// If we have a new account.
|
2021-07-09 01:39:27 +00:00
|
|
|
if (is_null($request->input('account_id'))) {
|
2018-08-20 12:15:28 +00:00
|
|
|
$ao = new Account;
|
|
|
|
//$ao->id = Account::NextId();
|
|
|
|
// @todo Make this automatic
|
2021-12-17 05:09:03 +00:00
|
|
|
$ao->site_id = config('site')->site_id;
|
|
|
|
$ao->country_id = config('site')->country_id; // @todo This might be wrong
|
2018-08-20 12:15:28 +00:00
|
|
|
$ao->active = 1;
|
|
|
|
$uo->accounts()->save($ao);
|
|
|
|
|
|
|
|
} else {
|
2018-08-20 12:53:16 +00:00
|
|
|
$ao = Account::findOrFail($request->input('account_id'));
|
2018-08-20 12:15:28 +00:00
|
|
|
}
|
2018-08-11 05:09:41 +00:00
|
|
|
|
|
|
|
$so = new Service;
|
|
|
|
|
|
|
|
// @todo Make this automatic
|
2021-12-17 05:09:03 +00:00
|
|
|
$so->site_id = config('site')->site_id;
|
2022-04-04 10:29:45 +00:00
|
|
|
$so->product_id = $po->id;
|
2018-08-11 05:09:41 +00:00
|
|
|
$so->order_status = 'ORDER-SUBMIT';
|
2022-07-25 13:24:45 +00:00
|
|
|
$so->ordered_by = Auth::id();
|
|
|
|
$so->active = FALSE;
|
2022-04-04 10:29:45 +00:00
|
|
|
$so->model = $order ? get_class($order) : NULL;
|
2023-05-13 12:19:22 +00:00
|
|
|
$so->recur_schedule = $po->billing_interval;
|
2018-08-11 05:09:41 +00:00
|
|
|
|
2022-04-04 10:29:45 +00:00
|
|
|
if ($order && $order->order_info) {
|
2021-12-24 01:14:01 +00:00
|
|
|
$so->order_info = $order->order_info;
|
2021-07-09 01:39:27 +00:00
|
|
|
|
2021-12-24 01:14:01 +00:00
|
|
|
unset($order->order_info);
|
2018-08-11 05:09:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$so = $ao->services()->save($so);
|
|
|
|
|
2021-12-24 01:14:01 +00:00
|
|
|
if ($order instanceOf Model) {
|
|
|
|
$order->service_id = $so->id;
|
|
|
|
$order->save();
|
2018-08-11 05:09:41 +00:00
|
|
|
}
|
|
|
|
|
2024-07-04 05:03:11 +00:00
|
|
|
// @todo Move this email to a config item
|
2020-02-09 05:48:03 +00:00
|
|
|
Mail::to('help@graytech.net.au')
|
2021-12-24 01:14:01 +00:00
|
|
|
->queue((new OrderRequest($so,$request->input('options.notes') ?: ''))->onQueue('email')); //@todo Get email from DB.
|
2020-02-09 05:48:03 +00:00
|
|
|
|
2024-07-04 05:03:11 +00:00
|
|
|
return view('theme.backend.adminlte.order_received')
|
|
|
|
->with('o',$so);
|
2018-08-09 14:10:51 +00:00
|
|
|
}
|
2021-12-24 01:14:01 +00:00
|
|
|
}
|