From 938085039531fb25a555ae6e028caa4b23a5e170 Mon Sep 17 00:00:00 2001 From: Deon George Date: Fri, 26 Jul 2024 12:33:42 +1000 Subject: [PATCH] Move charge actions to ChargeController, implemented charge delete --- app/Http/Controllers/ChargeController.php | 69 +++++++++++++++++++ app/Http/Controllers/ServiceController.php | 43 ------------ app/Models/Charge.php | 12 +++- app/Models/Service.php | 2 +- composer.lock | 18 ++--- .../adminlte/service/widget/charge.blade.php | 24 +++---- routes/web.php | 9 ++- 7 files changed, 107 insertions(+), 70 deletions(-) create mode 100644 app/Http/Controllers/ChargeController.php diff --git a/app/Http/Controllers/ChargeController.php b/app/Http/Controllers/ChargeController.php new file mode 100644 index 0000000..e0bada6 --- /dev/null +++ b/app/Http/Controllers/ChargeController.php @@ -0,0 +1,69 @@ +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)); + } + + public function delete(Charge $o): array + { + if (Gate::allows('delete',$o)) { + $o->delete(); + + return ['ok']; + + } else { + abort(401,'Not Allowed'); + } + } + + /** + * Add a charge to a service/account + * + * @param Request $request + * @return View + */ + public function 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); + } +} \ No newline at end of file diff --git a/app/Http/Controllers/ServiceController.php b/app/Http/Controllers/ServiceController.php index 0dc5260..0e22f26 100644 --- a/app/Http/Controllers/ServiceController.php +++ b/app/Http/Controllers/ServiceController.php @@ -266,49 +266,6 @@ 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 * diff --git a/app/Models/Charge.php b/app/Models/Charge.php index 1f47581..aca17df 100644 --- a/app/Models/Charge.php +++ b/app/Models/Charge.php @@ -70,7 +70,7 @@ class Charge extends Model /* ATTRIBUTES */ - public function getNameAttribute() + public function getNameAttribute(): string { return sprintf('%s %s', $this->description, @@ -93,4 +93,14 @@ class Charge extends Model { return Arr::get(InvoiceItem::type,$this->type); } + + /** + * Is this charge processable + * + * @return bool + */ + public function getUnprocessedAttribute(): bool + { + return $this->active && (! $this->processed); + } } \ No newline at end of file diff --git a/app/Models/Service.php b/app/Models/Service.php index 3125087..efc3b82 100644 --- a/app/Models/Service.php +++ b/app/Models/Service.php @@ -1275,7 +1275,7 @@ class Service extends Model implements IDs if ((($future == TRUE) OR (($future == FALSE) AND ($this->invoice_to >= $billdate))) AND ! $this->invoice_items->filter(function($item) { return $item->module_id == 30 AND ! $item->exists; })->count()) { - foreach ($this->charges->filter(function($item) { return ! $item->processed; }) as $oo) { + foreach ($this->charges->filter(function($item) { return $item->unprocessed; }) as $oo) { $o = new InvoiceItem; $o->active = TRUE; $o->service_id = $oo->service_id; diff --git a/composer.lock b/composer.lock index 49e6114..c26f3d8 100644 --- a/composer.lock +++ b/composer.lock @@ -3056,11 +3056,11 @@ }, { "name": "leenooks/laravel", - "version": "11.1.5", + "version": "11.1.6", "source": { "type": "git", "url": "https://gitea.dege.au/laravel/leenooks.git", - "reference": "4a4cf3c5bf32f50dcdc8fdc6c3ff680d7f15d90d" + "reference": "628fbac8f9ce60c5124ee83b288733ae24e48b63" }, "type": "library", "extra": { @@ -3093,7 +3093,7 @@ "laravel", "leenooks" ], - "time": "2024-07-25T03:52:29+00:00" + "time": "2024-07-26T02:26:31+00:00" }, { "name": "leenooks/passkey", @@ -9995,16 +9995,16 @@ }, { "name": "spatie/error-solutions", - "version": "1.1.0", + "version": "1.1.1", "source": { "type": "git", "url": "https://github.com/spatie/error-solutions.git", - "reference": "a014da18f2675ea15af0ba97f7e9aee59e13964f" + "reference": "ae7393122eda72eed7cc4f176d1e96ea444f2d67" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/spatie/error-solutions/zipball/a014da18f2675ea15af0ba97f7e9aee59e13964f", - "reference": "a014da18f2675ea15af0ba97f7e9aee59e13964f", + "url": "https://api.github.com/repos/spatie/error-solutions/zipball/ae7393122eda72eed7cc4f176d1e96ea444f2d67", + "reference": "ae7393122eda72eed7cc4f176d1e96ea444f2d67", "shasum": "" }, "require": { @@ -10057,7 +10057,7 @@ ], "support": { "issues": "https://github.com/spatie/error-solutions/issues", - "source": "https://github.com/spatie/error-solutions/tree/1.1.0" + "source": "https://github.com/spatie/error-solutions/tree/1.1.1" }, "funding": [ { @@ -10065,7 +10065,7 @@ "type": "github" } ], - "time": "2024-07-22T08:18:22+00:00" + "time": "2024-07-25T11:06:04+00:00" }, { "name": "spatie/flare-client-php", diff --git a/resources/views/theme/backend/adminlte/service/widget/charge.blade.php b/resources/views/theme/backend/adminlte/service/widget/charge.blade.php index 569f0a1..cfc7dcf 100644 --- a/resources/views/theme/backend/adminlte/service/widget/charge.blade.php +++ b/resources/views/theme/backend/adminlte/service/widget/charge.blade.php @@ -41,8 +41,8 @@ {{ $oo->processed ? 'YES' : 'NO' }} @if(! $oo->processed) - - + + @endif @@ -59,7 +59,7 @@ - + @section('page-scripts') @append \ No newline at end of file diff --git a/routes/web.php b/routes/web.php index 8f9228e..c052f69 100644 --- a/routes/web.php +++ b/routes/web.php @@ -7,6 +7,7 @@ use Leenooks\Controllers\SwitchUserController; use App\Http\Controllers\{AdminController, Auth\LoginController, Auth\SocialLoginController, + ChargeController, CheckoutController, HomeController, InvoiceController, @@ -145,15 +146,17 @@ Route::group(['middleware'=>['auth','role:reseller'],'prefix'=>'r'],function() { ->name('switch.start'); // Reseller Reports - Route::group(['middleware'=>['auth','role:reseller'],'prefix'=>'report'],function() { + Route::group(['prefix'=>'report'],function() { Route::get('domain',[ServiceController::class,'domain_list']); Route::get('email',[ServiceController::class,'email_list']); Route::get('hosting',[ServiceController::class,'hosting_list']); }); // Charges - Route::post('charge/addedit',[ServiceController::class,'charge_addedit']); - Route::post('charge/edit',[ServiceController::class,'charge_edit']); + Route::post('charge/addedit',[ChargeController::class,'addedit']); + Route::post('charge/delete/{o}',[ChargeController::class,'delete']) + ->where('o','[0-9]+'); + Route::post('charge/edit',[ChargeController::class,'edit']); // Reseller API calls Route::post('service_change_charges/{o}',[ServiceController::class,'service_change_charges_display'])