Move charge to under service

This commit is contained in:
2024-07-25 14:08:26 +10:00
parent ddd44b643f
commit 743374cb17
17 changed files with 470 additions and 499 deletions

View File

@@ -30,58 +30,6 @@ class AdminController extends Controller
->with('o',$o);
}
// @todo Move to reseller
public function charge_addedit(Request $request,Charge $o)
{
if ($request->post()) {
$request->validate([
'account_id' => 'required|exists:accounts,id',
'charge_at' => 'required|date',
'service_id' => 'required|exists:services,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) {
$o->site_id = config('site')->site_id;
$o->user_id = Auth::id();
$o->active = TRUE;
}
$o->forceFill($request->only(['account_id','charge_at','service_id','quantity','amount','sweep_type','type','taxable','description']));
$o->save();
return redirect()
->back()
->with('success','Charge recorded: '.$o->id);
}
return view('theme.backend.adminlte.a.charge.addedit')
->with('o',$o);
}
// @todo Move to reseller
public function charge_pending_account(Request $request,Account $o)
{
return view('theme.backend.adminlte.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
*/
// @todo Move to reseller
public function charge_unprocessed()
{
return view('theme.backend.adminlte.a.charge.unprocessed');
}
/**
* Record payments on an account.
*

View File

@@ -8,6 +8,7 @@ use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Session;
@@ -17,7 +18,7 @@ use Illuminate\Validation\ValidationException;
use Illuminate\View\View;
use Symfony\Component\HttpKernel\Exception\HttpException;
use App\Http\Requests\ServiceChangeRequest;
use App\Http\Requests\{ChargeAdd,ServiceChangeRequest};
use App\Mail\{CancelRequest,ChangeRequest};
use App\Models\{Charge,Invoice,Product,Service};
@@ -265,6 +266,49 @@ class ServiceController extends Controller
}
}
/**
* Add a charge to a service/account
*
* @param ChargeAdd $request
* @return RedirectResponse
*/
public function charge_addedit(ChargeAdd $request): RedirectResponse
{
$o = Charge::findOrNew(Arr::get($request->validated(),'id'));
// Dont update processed charges
if ($o->processed)
abort(403);
$o->forceFill(array_merge(Arr::except($request->validated(),['id']),['active'=>TRUE]));
$o->save();
return redirect()
->back()
->with('success',sprintf('Charge %s #%d',$o->wasRecentlyCreated ? 'Created' : 'Updated',$o->id));
}
/**
* Add a charge to a service/account
*
* @param Request $request
* @return View
*/
public function charge_edit(Request $request): View
{
$o = Charge::where('processed',FALSE)
->where('id',$request->id)
->firstOrFail();
if (Gate::allows('update',$o)) {
return view('theme.backend.adminlte.charge.widget.addedit')
->with('o',$o)
->with('so',$o->service);
}
abort(403);
}
/**
* List all the domains managed by the user
*

View File

@@ -0,0 +1,46 @@
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Session;
use App\Models\{Charge,InvoiceItem};
class ChargeAdd extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return Auth::user()
->accounts_all
->contains(request()->post('account_id'));
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
Session::put('charge_add',true);
return [
'id' => 'sometimes|exists:charges,id',
'account_id' => 'required|exists:accounts,id',
'charge_at' => 'required|date',
'service_id' => 'required|exists:services,id',
'site_id' => 'required|exists:sites,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|min:5|max:128',
];
}
}

View File

@@ -4,29 +4,24 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Log;
use Leenooks\Traits\ScopeActive;
use App\Casts\CollectionOrNull;
use App\Traits\SiteID;
/**
* CLEANUP NOTES:
* + Charge Date should not be null
* + Attributes should be a collection array
* + type should not be null
* + It would be useful, given an array of Charges to call a function that renders them into invoice format. This may provide consistence and be the single view of how charges do look on an invoice.
*/
class Charge extends Model
{
use SiteID;
use ScopeActive;
protected $casts = [
'attributes' => CollectionOrNull::class,
];
protected $dates = [
'start_at',
'stop_at',
'charge_at', // The date the charge applies - since it can be different to created_at
'start_at' => 'datetime:Y-m-d',
'stop_at' => 'datetime:Y-m-d',
'charge_at' => 'datetime:Y-m-d', // The date the charge applies - since it can be different to created_at
];
public const sweep = [
@@ -61,6 +56,7 @@ class Charge extends Model
/** @deprecated use pending */
public function scopeUnprocessed($query)
{
Log::alert('UMO:! Deprecated function scopeUnprocessed()');
return $this->scopePending();
}
@@ -76,7 +72,21 @@ class Charge extends Model
public function getNameAttribute()
{
return sprintf('%s %s',$this->description,$this->getAttribute('attributes') ? join('|',unserialize($this->getAttribute('attributes'))) : '');
return sprintf('%s %s',
$this->description,
$this->getAttribute('attributes')
? join('|',unserialize($this->getAttribute('attributes')))
: '');
}
public function getSubTotalAttribute(): float
{
return $this->quantity*$this->amount;
}
public function getTotalAttribute(): float
{
return $this->account->taxed($this->getSubTotalAttribute());
}
public function getTypeNameAttribute(): string

View File

@@ -0,0 +1,41 @@
<?php
namespace App\Models\Policies;
use App\Models\{Account,Charge,User};
class ChargePolicy
{
/**
* Determine whether the user can update the model.
*/
public function create(User $user,Account $account): bool
{
return $user->isReseller()
&& $user
->accounts_all
->contains($account->id);
}
/**
* Determine whether the user can delete the model.
*/
public function delete(User $user,Charge $charge): bool
{
return $user->isReseller()
&& $user
->accounts_all
->contains($charge->account_id);
}
/**
* Determine whether the user can update the model.
*/
public function update(User $user,Charge $charge): bool
{
return $user->isReseller()
&& $user
->accounts_all
->contains($charge->account_id);
}
}