osb/app/Http/Controllers/OrderController.php
Deon George 3ff34af7f0
All checks were successful
Create Docker Image / Build Docker Image (x86_64) (push) Successful in 37s
Create Docker Image / Final Docker Image Manifest (push) Successful in 9s
Update laravel framework from 9 to 11, removed some old packages
2024-07-04 15:03:11 +10:00

129 lines
3.6 KiB
PHP

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Validator;
use Illuminate\Database\Eloquent\Model;
use App\Mail\OrderRequest;
use App\Models\{Account,Product,Service,User};
class OrderController extends Controller
{
// @todo To check
public function __construct()
{
$this->middleware('auth');
}
// @todo To check
public function index()
{
return view('theme.backend.adminlte.order.home');
}
// @todo To check
public function product_order(Product $o)
{
return view('theme.backend.adminlte.order.widget.order')
->with('o',$o);
}
// @todo To check
public function product_info(Product $o)
{
return view('theme.backend.adminlte.order.widget.info')
->with('o',$o);
}
// @todo To check
public function submit(Request $request)
{
Validator::make($request->all(),['product_id'=>'required|exists:products,id'])
// Reseller
->sometimes('account_id','required|email',function($input) use ($request) {
return is_null($input->account_id) AND is_null($input->order_email_manual);
})
// 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->input('product_id'));
// Check we have the custom attributes for the product
$order = $po->orderValidation($request);
if ($request->input('order_email_manual')) {
$uo = User::firstOrNew(['email'=>$request->input('order_email_manual')]);
// If this is a new client
if (! $uo->exists) {
// @todo Make this automatic
$uo->site_id = config('site')->site_id;
$uo->active = FALSE;
$uo->firstname = '';
$uo->lastname = '';
$uo->country_id = config('site')->country_id; // @todo This might be wrong
$uo->parent_id = Auth::id() ?: 1; // @todo This should be configured to a default user
$uo->language_id = config('site')->language_id; // @todo This might be wrong
$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')->site_id;
$ao->country_id = config('site')->country_id; // @todo This might be wrong
$ao->active = 1;
$uo->accounts()->save($ao);
} else {
$ao = Account::findOrFail($request->input('account_id'));
}
$so = new Service;
// @todo Make this automatic
$so->site_id = config('site')->site_id;
$so->product_id = $po->id;
$so->order_status = 'ORDER-SUBMIT';
$so->ordered_by = Auth::id();
$so->active = FALSE;
$so->model = $order ? get_class($order) : NULL;
$so->recur_schedule = $po->billing_interval;
if ($order && $order->order_info) {
$so->order_info = $order->order_info;
unset($order->order_info);
}
$so = $ao->services()->save($so);
if ($order instanceOf Model) {
$order->service_id = $so->id;
$order->save();
}
// @todo Move this email to a config item
Mail::to('help@graytech.net.au')
->queue((new OrderRequest($so,$request->input('options.notes') ?: ''))->onQueue('email')); //@todo Get email from DB.
return view('theme.backend.adminlte.order_received')
->with('o',$so);
}
}