2021-07-01 09:41:12 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Support\Arr;
|
2021-10-01 04:59:04 +00:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
2021-07-01 09:41:12 +00:00
|
|
|
|
2021-12-20 03:25:43 +00:00
|
|
|
use App\Models\{Account,Charge,InvoiceItem,Payment,PaymentItem,Service,SiteDetail,Supplier,SupplierDetail};
|
2021-07-01 09:41:12 +00:00
|
|
|
|
2021-12-20 03:25:43 +00:00
|
|
|
/**
|
|
|
|
* The AdminController governs all routes that are prefixed with 'a/'.
|
|
|
|
*
|
|
|
|
* This is everything about the configuration of the application as a whole, or administration of a site.
|
|
|
|
*/
|
2021-07-01 09:41:12 +00:00
|
|
|
class AdminController extends Controller
|
|
|
|
{
|
2021-12-20 03:25:43 +00:00
|
|
|
// @todo Move to reseller
|
2021-07-01 09:41:12 +00:00
|
|
|
public function service(Service $o)
|
|
|
|
{
|
|
|
|
return View('a.service',['o'=>$o]);
|
|
|
|
}
|
|
|
|
|
2021-12-20 03:25:43 +00:00
|
|
|
// @todo Move to reseller
|
2021-10-01 04:59:04 +00:00
|
|
|
public function charge_addedit(Request $request,Charge $o)
|
|
|
|
{
|
|
|
|
if ($request->post()) {
|
|
|
|
$request->validate([
|
|
|
|
'account_id' => 'required|exists:accounts,id',
|
|
|
|
'charge_date' => 'required|date',
|
|
|
|
'service_id' => 'required|exists:ab_service,id',
|
|
|
|
'quantity' => 'required|numeric|not_in:0',
|
|
|
|
'amount' => 'required|numeric|min:0.01',
|
|
|
|
'sweep_type' => 'required|numeric|in:'.implode(',',array_keys(Charge::sweep)),
|
|
|
|
'type' => 'required|numeric|in:'.implode(',',array_keys(InvoiceItem::type)),
|
|
|
|
'taxable' => 'nullable|boolean',
|
|
|
|
'description' => 'nullable|string|max:128',
|
|
|
|
]);
|
|
|
|
|
|
|
|
if (! $o->exists) {
|
2021-12-17 05:09:03 +00:00
|
|
|
$o->site_id = config('site')->site_id;
|
2021-10-01 04:59:04 +00:00
|
|
|
$o->user_id = Auth::id();
|
|
|
|
$o->active = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
$o->forceFill($request->only(['account_id','charge_date','service_id','quantity','amount','sweep_type','type','taxable','description']));
|
|
|
|
$o->save();
|
|
|
|
|
|
|
|
return redirect()->back()
|
|
|
|
->with('success','Charge recorded: '.$o->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return view('a.charge.addedit')
|
|
|
|
->with('o',$o);
|
|
|
|
}
|
|
|
|
|
2021-12-20 03:25:43 +00:00
|
|
|
// @todo Move to reseller
|
2021-10-01 04:59:04 +00:00
|
|
|
public function charge_pending_account(Request $request,Account $o)
|
|
|
|
{
|
|
|
|
return view('a.charge.widgets.pending')
|
|
|
|
->with('list',$o->charges->where('active',TRUE)->where('processed',NULL)->except($request->exclude));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* List unprocessed charges
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
|
|
|
|
*/
|
2021-12-20 03:25:43 +00:00
|
|
|
// @todo Move to reseller
|
2021-10-01 04:59:04 +00:00
|
|
|
public function charge_unprocessed()
|
|
|
|
{
|
|
|
|
return view('a.charge.unprocessed');
|
|
|
|
}
|
|
|
|
|
2021-07-02 04:35:43 +00:00
|
|
|
/**
|
|
|
|
* Record payments on an account.
|
|
|
|
*
|
|
|
|
* @param Request $request
|
|
|
|
* @param Payment $o
|
|
|
|
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Http\RedirectResponse
|
|
|
|
*/
|
2021-12-20 03:25:43 +00:00
|
|
|
// @todo Move to reseller
|
2021-07-23 07:25:26 +00:00
|
|
|
public function pay_addedit(Request $request,Payment $o)
|
2021-07-02 04:35:43 +00:00
|
|
|
{
|
|
|
|
if ($request->post()) {
|
|
|
|
$validation = $request->validate([
|
2021-07-19 06:23:38 +00:00
|
|
|
'account_id' => 'required|exists:accounts,id',
|
2021-07-23 07:25:26 +00:00
|
|
|
'payment_date' => 'required|date',
|
2021-07-02 04:35:43 +00:00
|
|
|
'checkout_id' => 'required|exists:ab_checkout,id',
|
|
|
|
'total_amt' => 'required|numeric|min:0.01',
|
|
|
|
'fees_amt' => 'nullable|numeric|lt:total_amt',
|
2021-07-19 06:23:38 +00:00
|
|
|
'source_id' => 'nullable|exists:accounts,id',
|
2021-07-02 04:35:43 +00:00
|
|
|
'pending' => 'nullable|boolean',
|
|
|
|
'notes' => 'nullable|string',
|
|
|
|
'ip' => 'nullable|ip',
|
|
|
|
'invoices' => ['nullable','array',function ($attribute,$value,$fail) use ($request) {
|
|
|
|
if (collect($value)->sum() > $request->post('total_amt'))
|
|
|
|
$fail('Allocation is greater than payment total.');
|
|
|
|
}],
|
|
|
|
'invoices.*.id' => 'nullable|exists:ab_invoice,id',
|
|
|
|
]);
|
|
|
|
|
2021-07-23 07:25:26 +00:00
|
|
|
if (! $o->exists) {
|
|
|
|
$o->forceFill($request->only(['account_id','payment_date','checkout_id','checkout_id','total_amt','fees_amt','source_id','pending','notes','ip']));
|
2021-12-17 05:09:03 +00:00
|
|
|
$o->site_id = config('site')->site_id;
|
2021-07-23 07:25:26 +00:00
|
|
|
$o->save();
|
|
|
|
}
|
2021-07-02 04:35:43 +00:00
|
|
|
|
|
|
|
foreach ($validation['invoices'] as $id => $amount) {
|
2021-07-23 07:25:26 +00:00
|
|
|
// See if we already have a payment item that we need to update
|
|
|
|
$items = $o->items->filter(function($item) use ($id) { return $item->invoice_id == $id; });
|
|
|
|
|
|
|
|
if ($items->count() == 1) {
|
|
|
|
$oo = $items->pop();
|
|
|
|
|
|
|
|
if (! $amount) {
|
|
|
|
$oo->delete();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
$oo = new PaymentItem;
|
|
|
|
$oo->invoice_id = $id;
|
|
|
|
}
|
|
|
|
|
2021-10-01 04:59:04 +00:00
|
|
|
$oo->alloc_amt = ($oo->invoice->due >= 0) && ($oo->invoice->due-$amount >= 0) ? $amount : 0;
|
2021-12-17 05:09:03 +00:00
|
|
|
$oo->site_id = config('site')->site_id;
|
2021-07-23 07:25:26 +00:00
|
|
|
$o->items()->save($oo);
|
2021-07-02 04:35:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return redirect()->back()
|
2021-10-01 04:59:04 +00:00
|
|
|
->with('success','Payment recorded: '.$o->id);
|
2021-07-02 04:35:43 +00:00
|
|
|
}
|
|
|
|
|
2021-07-23 07:25:26 +00:00
|
|
|
return view('a.payment.addedit')
|
2021-07-02 04:35:43 +00:00
|
|
|
->with('o',$o);
|
|
|
|
}
|
|
|
|
|
2021-07-23 07:25:26 +00:00
|
|
|
/**
|
|
|
|
* List unapplied payments
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
|
|
|
|
*/
|
2021-12-20 03:25:43 +00:00
|
|
|
// @todo Move to reseller
|
2021-07-23 07:25:26 +00:00
|
|
|
public function pay_unapplied()
|
|
|
|
{
|
|
|
|
return view('a.payment.unapplied');
|
|
|
|
}
|
|
|
|
|
2021-07-02 04:35:43 +00:00
|
|
|
/**
|
|
|
|
* Show a list of invoices to apply payments to
|
|
|
|
*
|
2021-07-23 07:25:26 +00:00
|
|
|
* @param Request $request
|
2021-07-02 04:35:43 +00:00
|
|
|
* @param Account $o
|
|
|
|
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
|
|
|
|
*/
|
2021-12-20 03:25:43 +00:00
|
|
|
// @todo Move to reseller
|
2021-07-23 07:25:26 +00:00
|
|
|
public function pay_invoices(Request $request,Account $o)
|
2021-07-02 04:35:43 +00:00
|
|
|
{
|
|
|
|
return view('a.payment.widgets.invoices')
|
2021-07-23 07:25:26 +00:00
|
|
|
->with('pid',$request->pid)
|
2021-07-02 04:35:43 +00:00
|
|
|
->with('o',$o);
|
|
|
|
}
|
|
|
|
|
2021-12-20 03:25:43 +00:00
|
|
|
/**
|
|
|
|
* Site up site wide suppliers, or a site's supplier details
|
|
|
|
*
|
|
|
|
* @note This method is protected by the routes
|
|
|
|
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
|
|
|
|
*/
|
|
|
|
public function supplier()
|
|
|
|
{
|
|
|
|
return view('a.supplier');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update a suppliers details
|
|
|
|
*
|
|
|
|
* @param Request $request
|
|
|
|
* @param Supplier $o
|
|
|
|
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Http\RedirectResponse
|
|
|
|
*/
|
|
|
|
public function supplier_addedit(Request $request,Supplier $o)
|
|
|
|
{
|
|
|
|
if ($request->post()) {
|
|
|
|
$validation = $request->validate([
|
|
|
|
'name' => 'required|string|min:2|max:255',
|
|
|
|
'active' => 'sometimes|accepted',
|
|
|
|
'address1' => 'required|string|min:2|max:255',
|
|
|
|
'address2' => 'nullable|string|min:2|max:255',
|
|
|
|
'city' => 'required|string|min:2|max:64',
|
|
|
|
'state' => 'required|string|min:2|max:32',
|
|
|
|
'postcode' => 'required|string|min:2|max:8',
|
|
|
|
'supplier_details.notes' => 'nullable|string|min:3',
|
|
|
|
'supplier_details.accounts' => 'nullable|email',
|
|
|
|
'supplier_details.support' => 'nullable|email',
|
|
|
|
'supplier_details.payments' => 'nullable|string|min:3',
|
|
|
|
]);
|
|
|
|
|
|
|
|
foreach (collect($validation)->except('supplier_details') as $key => $item)
|
|
|
|
$o->{$key} = $item;
|
|
|
|
|
|
|
|
$o->active = (bool)$request->active;
|
|
|
|
|
|
|
|
try {
|
|
|
|
$o->save();
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
return redirect()->back()->withErrors($e->getMessage())->withInput();
|
|
|
|
}
|
|
|
|
|
|
|
|
$o->load(['detail']);
|
|
|
|
$oo = $o->detail ?: new SupplierDetail;
|
|
|
|
|
|
|
|
foreach (collect($validation)->get('supplier_details',[]) as $key => $item)
|
|
|
|
$oo->{$key} = $item;
|
|
|
|
|
|
|
|
$o->detail()->save($oo);
|
|
|
|
|
|
|
|
return redirect()->back()
|
|
|
|
->with('success','Supplier saved');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! $o->exists && $request->name)
|
|
|
|
$o = Supplier::where('name',$request->name)->with(['details'])->firstOrNew();
|
|
|
|
|
|
|
|
return view('a.supplierdetails')
|
|
|
|
->with('o',$o);
|
|
|
|
}
|
|
|
|
|
2021-07-01 09:41:12 +00:00
|
|
|
/**
|
|
|
|
* Site setup
|
|
|
|
*
|
|
|
|
* @note This method is protected by the routes
|
|
|
|
* @param Request $request
|
|
|
|
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Http\RedirectResponse
|
|
|
|
*/
|
|
|
|
public function setup(Request $request)
|
|
|
|
{
|
|
|
|
if ($request->post()) {
|
|
|
|
$validated = $request->validate([
|
2021-12-20 03:25:43 +00:00
|
|
|
'site_name' => 'required|string|min:2|max:255',
|
2021-07-01 09:41:12 +00:00
|
|
|
'site_email' => 'required|string|email|max:255',
|
2021-12-20 03:25:43 +00:00
|
|
|
'site_address1' => 'required|string|min:2|max:255',
|
|
|
|
'site_address2' => 'nullable|string|min:2|max:255',
|
|
|
|
'site_city' => 'required|string|min:2|max:64',
|
|
|
|
'site_state' => 'required|string|min:2|max:32',
|
|
|
|
'site_postcode' => 'required|string|min:2|max:8',
|
2021-07-01 09:41:12 +00:00
|
|
|
'site_description' => 'nullable|string|min:5',
|
|
|
|
'site_phone' => 'nullable|regex:/[0-9 ]+/|min:6|max:12',
|
|
|
|
'site_fax' => 'nullable|regex:/[0-9 ]+/|min:6|max:12',
|
|
|
|
'site_tax' => 'required|regex:/[0-9 ]+/|size:14',
|
|
|
|
'social' => 'nullable|array',
|
|
|
|
'top_menu' => 'nullable|array',
|
|
|
|
'site_logo' => 'nullable|image',
|
2021-07-01 23:12:34 +00:00
|
|
|
'email_logo' => 'nullable|image',
|
2021-07-01 09:41:12 +00:00
|
|
|
]);
|
|
|
|
|
2021-12-17 05:09:03 +00:00
|
|
|
$site = config('site');
|
2021-07-01 09:41:12 +00:00
|
|
|
|
|
|
|
// @todo - not currently rendered in the home page
|
|
|
|
$validated['social'] = [];
|
|
|
|
$validated['top_menu'] = [];
|
|
|
|
|
|
|
|
// Handle the images
|
|
|
|
foreach(['site_logo','email_logo'] as $key)
|
|
|
|
if (array_key_exists($key,$validated))
|
2021-07-01 23:12:34 +00:00
|
|
|
$validated[$key] = ($x=$validated[$key])->storeAs('site/'.$site->site_id,$x->getClientOriginalName(),'public');
|
2021-07-01 09:41:12 +00:00
|
|
|
|
2021-07-01 23:12:34 +00:00
|
|
|
foreach ($site->details as $oo)
|
2021-07-01 09:41:12 +00:00
|
|
|
if (array_key_exists($oo->key,$validated)) {
|
|
|
|
$oo->value = Arr::get($validated,$oo->key);
|
|
|
|
$oo->save();
|
|
|
|
|
|
|
|
unset($validated[$oo->key]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Left over values to be created.
|
|
|
|
foreach ($validated as $k=>$v) {
|
2021-07-01 23:12:34 +00:00
|
|
|
$oo = new SiteDetail;
|
2021-07-01 09:41:12 +00:00
|
|
|
$oo->key = $k;
|
|
|
|
$oo->value = $v ?: '';
|
|
|
|
|
2021-07-01 23:12:34 +00:00
|
|
|
$site->details()->save($oo);
|
2021-07-01 09:41:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return redirect()->back()
|
|
|
|
->with('success','Settings saved');
|
|
|
|
}
|
|
|
|
|
|
|
|
return view('a.setup');
|
|
|
|
}
|
|
|
|
}
|