From b87e9ff33a3e27735dac8adfbf036e3d9c18b9ee Mon Sep 17 00:00:00 2001 From: Deon George Date: Wed, 14 Aug 2024 22:16:09 +1000 Subject: [PATCH] Rework products with components --- app/Http/Controllers/AccountingController.php | 38 ----- app/Http/Controllers/ProductController.php | 88 ++++------ app/Http/Requests/ProductAddEdit.php | 22 ++- app/Models/Product.php | 18 +- app/Providers/AppServiceProvider.php | 1 + .../adminlte/product/details.blade.php | 10 +- .../adminlte/product/widget/detail.blade.php | 158 +++++------------- .../product/widget/selector.blade.php | 22 +-- .../product/widget/services.blade.php | 48 +----- routes/api.php | 14 -- routes/web.php | 15 +- 11 files changed, 132 insertions(+), 302 deletions(-) delete mode 100644 app/Http/Controllers/AccountingController.php diff --git a/app/Http/Controllers/AccountingController.php b/app/Http/Controllers/AccountingController.php deleted file mode 100644 index 2347ce7..0000000 --- a/app/Http/Controllers/AccountingController.php +++ /dev/null @@ -1,38 +0,0 @@ -middleware('auth'); - } - - /** - * Query the accounting system and get a valid list of accounting codes - * - * @param string $provider - * @return Collection - */ - public static function list(string $provider): Collection - { - // @todo This should be a variable - $uo = User::findOrFail(1); - - $so = ProviderOauth::where('name',$provider)->singleOrFail(); - if (! ($to=$so->token($uo))) - abort(500,sprintf('Unknown Tokens for [%s]',$uo->email)); - - $api = $to->API(); - - return $api->getItems() - ->pluck('pid','Id') - ->transform(function($item,$value) { return ['id'=>$value,'value'=>$item]; }) - ->values(); - } -} \ No newline at end of file diff --git a/app/Http/Controllers/ProductController.php b/app/Http/Controllers/ProductController.php index f9b1320..14cb539 100644 --- a/app/Http/Controllers/ProductController.php +++ b/app/Http/Controllers/ProductController.php @@ -3,6 +3,7 @@ namespace App\Http\Controllers; use Illuminate\Http\Request; +use Illuminate\Support\Arr; use Illuminate\Support\Collection; use App\Http\Requests\ProductAddEdit; @@ -17,14 +18,14 @@ class ProductController extends Controller * @return Collection * @throws \Exception */ - public function api_supplier_products(Request $request): Collection + public function api_supplied_products(Request $request): Collection { switch ($request->type) { case Product\Broadband::class: return Product\Broadband::select(['id','supplier_item_id']) ->with(['supplied.supplier_detail.supplier']) ->get() - ->map(function($item) { return ['id'=>$item->id,'name'=>sprintf('%s: %s',$item->supplied->supplier->name,$item->supplied->name)]; }) + ->map(fn($item)=>['id'=>$item->id,'name'=>sprintf('%s: %s',$item->supplied->supplier->name,$item->supplied->name)]) ->sortBy('name') ->values(); @@ -32,7 +33,7 @@ class ProductController extends Controller return Product\Domain::select(['id','supplier_item_id']) ->with(['supplied.supplier_detail.supplier']) ->get() - ->map(function($item) { return ['id'=>$item->id,'name'=>sprintf('%s: %s',$item->supplied->supplier->name,$item->supplied->name)]; }) + ->map(fn($item)=>['id'=>$item->id,'name'=>sprintf('%s: %s',$item->supplied->supplier->name,$item->supplied->name)]) ->sortBy('name') ->values(); @@ -40,7 +41,7 @@ class ProductController extends Controller return Product\Email::select(['id','supplier_item_id']) ->with(['supplied.supplier_detail.supplier']) ->get() - ->map(function($item) { return ['id'=>$item->id,'name'=>sprintf('%s: %s',$item->supplied->supplier->name,$item->supplied->name)]; }) + ->map(fn($item)=>['id'=>$item->id,'name'=>sprintf('%s: %s',$item->supplied->supplier->name,$item->supplied->name)]) ->sortBy('name') ->values(); @@ -48,7 +49,7 @@ class ProductController extends Controller return Product\Generic::select(['id','supplier_item_id']) ->with(['supplied.supplier_detail.supplier']) ->get() - ->map(function($item) { return ['id'=>$item->id,'name'=>sprintf('%s: %s',$item->supplied->supplier->name,$item->supplied->name)]; }) + ->map(fn($item)=>['id'=>$item->id,'name'=>sprintf('%s: %s',$item->supplied->supplier->name,$item->supplied->name)]) ->sortBy('name') ->values(); @@ -56,7 +57,7 @@ class ProductController extends Controller return Product\Host::select(['id','supplier_item_id']) ->with(['supplied.supplier_detail.supplier']) ->get() - ->map(function($item) { return ['id'=>$item->id,'name'=>sprintf('%s: %s',$item->supplied->supplier->name,$item->supplied->name)]; }) + ->map(fn($item)=>['id'=>$item->id,'name'=>sprintf('%s: %s',$item->supplied->supplier->name,$item->supplied->name)]) ->sortBy('name') ->values(); @@ -64,7 +65,7 @@ class ProductController extends Controller return Product\Phone::select(['id','supplier_item_id']) ->with(['supplied.supplier_detail.supplier']) ->get() - ->map(function($item) { return ['id'=>$item->id,'name'=>sprintf('%s: %s',$item->supplied->supplier->name,$item->supplied->name)]; }) + ->map(fn($item)=>['id'=>$item->id,'name'=>sprintf('%s: %s',$item->supplied->supplier->name,$item->supplied->name)]) ->sortBy('name') ->values(); @@ -73,77 +74,54 @@ class ProductController extends Controller } } - /** - * Update a suppliers details - * - * @param Request $request - * @param Product $o - * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Http\RedirectResponse - */ - public function details(Request $request,Product $o) + public function addedit(ProductAddEdit $request,Product $o) { - if (! $o->exists && $request->name) - $o = Product::where('name',$request->name)->firstOrNew(); - - return view('theme.backend.adminlte.product.details') - ->with('breadcrumb',collect(['Products'=>url('a/product')])) - ->with('o',$o); - } - - public function details_addedit(ProductAddEdit $request,Product $o) - { - foreach ($request->except(['_token','submit','translate','accounting']) as $key => $item) + foreach (Arr::except($request->validated(),['translate','accounting','pricing','active']) as $key => $item) $o->{$key} = $item; $o->active = (bool)$request->active; // Trim down the pricing array, remove null values - $o->pricing = $o->pricing->map(function($item) { - foreach ($item as $k=>$v) { - if (is_array($v)) { - $v = array_filter($v); - $item[$k] = $v; + $o->pricing = collect($request->validated('pricing')) + ->map(function($item) { + foreach ($item as $k=>$v) { + if (is_array($v)) { + $v = array_filter($v); + $item[$k] = $v; + } } - } - return $item; - }); + return $item; + }); try { $o->save(); + } catch (\Exception $e) { - return redirect()->back()->withErrors($e->getMessage())->withInput(); + return redirect() + ->back() + ->withErrors($e->getMessage()) + ->withInput(); } $o->load(['translate']); $oo = $o->translate ?: new ProductTranslate; - foreach ($request->get('translate',[]) as $key => $item) + foreach ($request->validated('translate',[]) as $key => $item) $oo->{$key} = $item; $o->translate()->save($oo); - if ($request->accounting) - foreach ($request->accounting as $k=>$v) - $o->providers()->syncWithoutDetaching([ - $k => [ - 'ref' => $v, - 'site_id'=>$o->site_id, - ], - ]); + foreach ($request->validated('accounting',[]) as $k=>$v) { + $o->providers()->syncWithoutDetaching([ + $k => [ + 'ref' => $v, + 'site_id'=>$o->site_id, + ], + ]); + } return redirect() ->back() ->with('success','Product saved'); } - - /** - * Manage products for a site - * - * @note This method is protected by the routes - * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View - */ - public function home() - { - return view('theme.backend.adminlte.product.home'); - } } \ No newline at end of file diff --git a/app/Http/Requests/ProductAddEdit.php b/app/Http/Requests/ProductAddEdit.php index 54093a5..094946a 100644 --- a/app/Http/Requests/ProductAddEdit.php +++ b/app/Http/Requests/ProductAddEdit.php @@ -3,7 +3,9 @@ namespace App\Http\Requests; use Illuminate\Foundation\Http\FormRequest; -use Illuminate\Support\Facades\Auth; +use Illuminate\Support\Facades\Gate; + +use App\Models\ProviderOauth; /** * Editing Suppliers @@ -17,7 +19,7 @@ class ProductAddEdit extends FormRequest */ public function authorize() { - return Auth::user()->isWholesaler(); + return Gate::allows('wholesaler'); } /** @@ -34,7 +36,21 @@ class ProductAddEdit extends FormRequest 'active' => 'sometimes|accepted', 'model' => 'sometimes|string', // @todo Check that it is a valid model type 'model_id' => 'sometimes|int', // @todo Check that it is a valid model type - 'accounting' => 'nullable|array', // @todo Validate that the value is in the accounting system + 'accounting' => [ + 'nullable', + 'array', + function (string $attribute,mixed $value,\Closure $fail) { + if (! is_array($value)) + $fail("Invalid format for {$attribute}"); + + foreach ($value as $k=>$v) { + if (! ProviderOauth::where('id',$k)->exists()) + $fail("Provider doesnt exist [$k]"); + + // @todo Validate that the value is in the accounting system + } + }, + ], 'pricing' => 'required|array', // @todo Validate the elements in the pricing ]; } diff --git a/app/Models/Product.php b/app/Models/Product.php index dd950fb..5928150 100644 --- a/app/Models/Product.php +++ b/app/Models/Product.php @@ -11,10 +11,11 @@ use Illuminate\Support\Collection; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\File; use Illuminate\Support\Str; +use Intuit\Exceptions\NotTokenException; +use Intuit\Traits\ProviderTokenTrait; use Leenooks\Traits\ScopeActive; use App\Casts\CollectionOrNull; -use App\Http\Controllers\AccountingController; use App\Interfaces\{IDs,ProductItem}; use App\Traits\{ProductDetails,ProviderRef}; @@ -66,7 +67,7 @@ use App\Traits\{ProductDetails,ProviderRef}; */ class Product extends Model implements IDs { - use HasFactory,ProductDetails,ScopeActive,ProviderRef; + use HasFactory,ProductDetails,ScopeActive,ProviderRef,ProviderTokenTrait; protected $casts = [ 'pricing' => CollectionOrNull::class, @@ -368,7 +369,18 @@ class Product extends Model implements IDs public function accounting(string $provider): Collection { - return AccountingController::list($provider); + $so = ProviderOauth::where('name',self::provider) + ->sole(); + + if (! ($to=$so->token(Auth::user()))) + throw new NotTokenException(sprintf('Unknown Tokens for [%s]',Auth::user()->email)); + + return $to + ->API() + ->getItems() + ->pluck('pid','Id') + ->transform(fn($item,$value)=>['id'=>$value,'value'=>$item]) + ->values(); } /** diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 17bbec5..8b9e295 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -35,5 +35,6 @@ class AppServiceProvider extends ServiceProvider Route::model('co',\App\Models\Checkout::class); Route::model('po',\App\Models\Payment::class); + Route::model('pdo',\App\Models\Product::class); } } \ No newline at end of file diff --git a/resources/views/theme/backend/adminlte/product/details.blade.php b/resources/views/theme/backend/adminlte/product/details.blade.php index 556ec84..c044d88 100644 --- a/resources/views/theme/backend/adminlte/product/details.blade.php +++ b/resources/views/theme/backend/adminlte/product/details.blade.php @@ -1,14 +1,14 @@ @extends('adminlte::layouts.app') @section('htmlheader_title') - {{ $o->name ?: 'New Product' }} + {{ $pdo->name ?: 'New Product' }} @endsection @section('page_title') - {{ $o->name ?: 'New Product' }} + {{ $pdo->name ?: 'New Product' }} @endsection @section('contentheader_title') - {{ $o->name ?: 'New Product' }} + {{ $pdo->name ?: 'New Product' }} @endsection @section('contentheader_description') @endsection @@ -23,8 +23,8 @@
-
-