2021-07-01 09:41:12 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2024-07-23 10:22:51 +00:00
|
|
|
use Illuminate\Http\RedirectResponse;
|
2021-07-01 09:41:12 +00:00
|
|
|
use Illuminate\Http\Request;
|
2021-10-01 04:59:04 +00:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
2021-07-01 09:41:12 +00:00
|
|
|
|
2024-07-23 10:22:51 +00:00
|
|
|
use App\Http\Requests\SiteEdit;
|
2022-06-13 05:46:38 +00:00
|
|
|
use App\Models\{Account,
|
|
|
|
Charge,
|
|
|
|
Invoice,
|
|
|
|
InvoiceItem,
|
|
|
|
Payment,
|
|
|
|
PaymentItem,
|
|
|
|
Service,
|
2024-07-04 05:03:11 +00:00
|
|
|
SiteDetail};
|
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)
|
|
|
|
{
|
2024-07-04 05:03:11 +00:00
|
|
|
return view('theme.backend.adminlte.a.service')
|
|
|
|
->with('o',$o);
|
2021-07-01 09:41:12 +00:00
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
2024-07-04 05:03:11 +00:00
|
|
|
return redirect()
|
|
|
|
->back()
|
2021-10-01 04:59:04 +00:00
|
|
|
->with('success','Charge recorded: '.$o->id);
|
|
|
|
}
|
|
|
|
|
2024-07-04 05:03:11 +00:00
|
|
|
return view('theme.backend.adminlte.a.charge.addedit')
|
2021-10-01 04:59:04 +00:00
|
|
|
->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)
|
|
|
|
{
|
2024-07-04 05:03:11 +00:00
|
|
|
return view('theme.backend.adminlte.a.charge.widgets.pending')
|
2021-10-01 04:59:04 +00:00
|
|
|
->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()
|
|
|
|
{
|
2024-07-04 05:03:11 +00:00
|
|
|
return view('theme.backend.adminlte.a.charge.unprocessed');
|
2021-10-01 04:59:04 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-07-04 05:03:11 +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
|
|
|
}
|
|
|
|
|
2024-07-04 05:03:11 +00:00
|
|
|
return view('theme.backend.adminlte.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()
|
|
|
|
{
|
2024-07-04 05:03:11 +00:00
|
|
|
return view('theme.backend.adminlte.a.payment.unapplied');
|
2021-07-23 07:25:26 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2024-07-04 05:03:11 +00:00
|
|
|
return view('theme.backend.adminlte.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
|
2024-07-23 10:22:51 +00:00
|
|
|
* @param SiteEdit $request
|
|
|
|
* @return RedirectResponse
|
2021-07-01 09:41:12 +00:00
|
|
|
*/
|
2024-07-23 10:22:51 +00:00
|
|
|
public function setup(SiteEdit $request)
|
2021-07-01 09:41:12 +00:00
|
|
|
{
|
2024-07-23 10:22:51 +00:00
|
|
|
$site = config('site');
|
|
|
|
$images = ['site_logo','email_logo'];
|
|
|
|
$validated = collect($request->validated());
|
|
|
|
|
|
|
|
// Handle the images
|
|
|
|
foreach($images as $key)
|
|
|
|
if ($x=$request->validated($key))
|
|
|
|
$validated->put($key,$x->storeAs('site/'.$site->site_id,$x->getClientOriginalName(),'public'));
|
|
|
|
|
|
|
|
foreach ($site->details as $oo)
|
|
|
|
if ($validated->has($oo->key)) {
|
|
|
|
// Dont set the following keys to null if they are null
|
|
|
|
if (in_array($oo->key,$images) && is_null($validated->get($oo->key)))
|
|
|
|
continue;
|
2021-07-01 09:41:12 +00:00
|
|
|
|
2024-07-23 10:22:51 +00:00
|
|
|
$oo->value = $validated->get($oo->key) ?: '';
|
|
|
|
$oo->save();
|
2021-07-01 09:41:12 +00:00
|
|
|
|
2024-07-23 10:22:51 +00:00
|
|
|
$validated->forget($oo->key);
|
2021-07-01 09:41:12 +00:00
|
|
|
}
|
|
|
|
|
2024-07-23 10:22:51 +00:00
|
|
|
// Left over values to be created.
|
|
|
|
foreach ($validated as $k=>$v) {
|
|
|
|
$oo = new SiteDetail;
|
|
|
|
$oo->key = $k;
|
|
|
|
$oo->value = $v ?: '';
|
|
|
|
|
|
|
|
$site->details()->save($oo);
|
2021-07-01 09:41:12 +00:00
|
|
|
}
|
|
|
|
|
2024-07-23 10:22:51 +00:00
|
|
|
return redirect()
|
|
|
|
->back()
|
|
|
|
->with('success','Settings saved');
|
2021-07-01 09:41:12 +00:00
|
|
|
}
|
|
|
|
}
|