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
|
|
|
|
2022-06-13 05:46:38 +00:00
|
|
|
use App\Models\{Account,
|
|
|
|
Charge,
|
|
|
|
Invoice,
|
|
|
|
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',
|
2022-06-13 04:34:45 +00:00
|
|
|
'charge_at' => 'required|date',
|
2022-04-19 07:07:39 +00:00
|
|
|
'service_id' => 'required|exists:services,id',
|
2021-10-01 04:59:04 +00:00
|
|
|
'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;
|
|
|
|
}
|
|
|
|
|
2022-06-13 04:34:45 +00:00
|
|
|
$o->forceFill($request->only(['account_id','charge_at','service_id','quantity','amount','sweep_type','type','taxable','description']));
|
2021-10-01 04:59:04 +00:00
|
|
|
$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()) {
|
2022-06-13 05:46:38 +00:00
|
|
|
|
2021-07-02 04:35:43 +00:00
|
|
|
$validation = $request->validate([
|
2021-07-19 06:23:38 +00:00
|
|
|
'account_id' => 'required|exists:accounts,id',
|
2022-06-13 05:46:38 +00:00
|
|
|
'paid_at' => 'required|date',
|
2022-07-29 06:06:19 +00:00
|
|
|
'checkout_id' => 'required|exists:checkouts,id',
|
2021-07-02 04:35:43 +00:00
|
|
|
'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',
|
2022-06-13 05:46:38 +00:00
|
|
|
'invoices' => ['required','array',function ($attribute,$value,$fail) use ($request) {
|
|
|
|
if (collect($value)->sum('id') > $request->post('total_amt'))
|
2021-07-02 04:35:43 +00:00
|
|
|
$fail('Allocation is greater than payment total.');
|
|
|
|
}],
|
2022-06-13 05:46:38 +00:00
|
|
|
'invoices.*.id' => ['required',function ($attribute,$value,$fail) {
|
|
|
|
if (! Invoice::exists(str_replace(str_replace($attribute,'invoice\.','',),'.id','')))
|
|
|
|
$fail('Invoice doesnt exist in DB');
|
|
|
|
}],
|
2021-07-02 04:35:43 +00:00
|
|
|
]);
|
|
|
|
|
2021-07-23 07:25:26 +00:00
|
|
|
if (! $o->exists) {
|
2021-12-17 05:09:03 +00:00
|
|
|
$o->site_id = config('site')->site_id;
|
2022-06-13 07:24:43 +00:00
|
|
|
$o->active = TRUE;
|
2021-07-23 07:25:26 +00:00
|
|
|
}
|
2021-07-02 04:35:43 +00:00
|
|
|
|
2023-05-13 12:19:22 +00:00
|
|
|
$o->forceFill($request->only(['account_id','paid_at','checkout_id','total_amt','fees_amt','source_id','pending','notes','ip']));
|
|
|
|
$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();
|
2022-10-17 23:23:11 +00:00
|
|
|
if ($amount['id'] == 0) {
|
2021-07-23 07:25:26 +00:00
|
|
|
$oo->delete();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
$oo = new PaymentItem;
|
|
|
|
$oo->invoice_id = $id;
|
|
|
|
}
|
|
|
|
|
2022-06-13 05:46:38 +00:00
|
|
|
$oo->amount = ($oo->invoice->due >= 0) && ($oo->invoice->due-$amount['id'] >= 0) ? $amount['id'] : 0;
|
2022-10-17 23:23:11 +00:00
|
|
|
|
|
|
|
// If the amount is empty, ignore it.
|
|
|
|
if (! $oo->amount)
|
|
|
|
continue;
|
|
|
|
|
2021-12-17 05:09:03 +00:00
|
|
|
$oo->site_id = config('site')->site_id;
|
2022-10-17 23:23:11 +00:00
|
|
|
$oo->active = TRUE;
|
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-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');
|
|
|
|
}
|
|
|
|
}
|