diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index 0c4e462..162106b 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -4,14 +4,12 @@ namespace App\Http\Controllers; use Carbon\Carbon; use Illuminate\Http\RedirectResponse; -use Illuminate\Http\Request; use Illuminate\Support\Arr; use Illuminate\Support\Facades\Hash; use Illuminate\Support\Facades\Session; -use Illuminate\Validation\Rule; -use App\Http\Requests\UserEdit; -use App\Models\{Supplier,User}; +use App\Http\Requests\{AccountSupplierAdd,UserEdit}; +use App\Models\{Account,Supplier,User}; class UserController extends Controller { @@ -38,23 +36,15 @@ class UserController extends Controller /** * Add a supplier to a user's profile * - * @param Request $request - * @param User $o - * @return \Illuminate\Http\RedirectResponse + * @param AccountSupplierAdd $request + * @param Account $o + * @return RedirectResponse */ - public function supplier_addedit(Request $request,User $o) + public function supplier_addedit(AccountSupplierAdd $request,Account $o): RedirectResponse { - Session::put('supplier_update',true); - - $validated = $request->validate([ - 'id'=> ['required','string',Rule::unique('supplier_user')->where(fn ($query) => $query->where('supplier_id',$request->supplier_id)->where('user_id','<>',$o->id))], - 'supplier_id'=>'required|exists:suppliers,id', - ]); - $o->suppliers()->attach([ - $validated['supplier_id'] => [ - 'id'=>$validated['id'], - 'site_id'=>$o->site_id, + $request->validated('supplier_id') => [ + 'supplier_ref'=>$request->validated('supplier_ref'), 'created_at'=>Carbon::now(), ] ]); @@ -67,13 +57,13 @@ class UserController extends Controller /** * Remove a supplier from a user's profile * - * @param User $o + * @param Account $o * @param Supplier $so - * @return \Illuminate\Http\RedirectResponse + * @return RedirectResponse */ - public function supplier_delete(User $o,Supplier $so) + public function supplier_delete(Account $o,Supplier $so): RedirectResponse { - Session::put('supplier_update',true); + Session::put('supplier_update',TRUE); $o->suppliers()->detach([$so->id]); diff --git a/app/Http/Requests/AccountSupplierAdd.php b/app/Http/Requests/AccountSupplierAdd.php new file mode 100644 index 0000000..2c23cb8 --- /dev/null +++ b/app/Http/Requests/AccountSupplierAdd.php @@ -0,0 +1,42 @@ +|string> + */ + public function rules(): array + { + Session::put('supplier_update',true); + + return [ + 'supplier_ref'=> [ + 'required', + 'string', + 'min:2', + Rule::unique('account_supplier') + ->where(fn($query)=>$query + ->where('account_id',request()->get('account_id'))) + ->where('supplier_id',request()->get('supplier_id')), + ], + 'supplier_id'=>'required|exists:suppliers,id', + ]; + } +} \ No newline at end of file diff --git a/app/Models/Account.php b/app/Models/Account.php index a233d68..6db2df6 100644 --- a/app/Models/Account.php +++ b/app/Models/Account.php @@ -159,6 +159,15 @@ class Account extends Model implements IDs ->active(); } + /** + * Supplier configuration for this account + */ + public function suppliers() + { + return $this->belongsToMany(Supplier::class) + ->withPivot('supplier_ref','created_at'); + } + /** * Taxes applicable for this account */ diff --git a/app/Models/User.php b/app/Models/User.php index 32e4ff4..ebb1174 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -132,17 +132,6 @@ class User extends Authenticatable implements IDs return $this->hasOneThrough(Rtm::class,Account::class); } - /** - * Supplier configuration for this user - * - * @deprecated To move to account->suppliers() - */ - public function suppliers() - { - return $this->belongsToMany(Supplier::class) - ->withPivot('id','created_at'); - } - /* ATTRIBUTES */ /** diff --git a/database/migrations/2024_07_23_210016_update_supplier_user.php b/database/migrations/2024_07_23_210016_update_supplier_user.php new file mode 100644 index 0000000..7a09788 --- /dev/null +++ b/database/migrations/2024_07_23_210016_update_supplier_user.php @@ -0,0 +1,47 @@ +string('supplier_ref')->nullable(); + $table->dropUnique(['id','site_id']); + $table->dropUnique(['id','supplier_id']); + $table->unique(['user_id','supplier_ref']); + }); + + DB::update('UPDATE supplier_user set supplier_ref=id'); + DB::update('ALTER TABLE supplier_user ALTER COLUMN supplier_ref SET NOT NULL'); + + Schema::table('supplier_user', function (Blueprint $table) { + $table->dropColumn('id'); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('supplier_user', function (Blueprint $table) { + $table->string('id')->nullable(); + $table->unique(['id','site_id']); + $table->unique(['id','supplier_id']); + $table->dropUnique(['user_id','supplier_ref']); + }); + + DB::update('UPDATE supplier_user set id=supplier_ref'); + + Schema::table('supplier_user', function (Blueprint $table) { + $table->dropColumn('supplier_ref'); + }); + } +}; diff --git a/database/migrations/2024_07_23_212746_move_supplier_user.php b/database/migrations/2024_07_23_212746_move_supplier_user.php new file mode 100644 index 0000000..62f88ec --- /dev/null +++ b/database/migrations/2024_07_23_212746_move_supplier_user.php @@ -0,0 +1,52 @@ +timestamps(); + $table->integer('supplier_id')->unsigned(); + $table->integer('account_id')->unsigned(); + $table->string('supplier_ref'); + $table->boolean('active')->default(FALSE); + + $table->unique(['account_id','supplier_id']); + $table->unique(['supplier_id','supplier_ref']); + + $table->foreign('account_id')->references('id')->on('accounts'); + $table->foreign('supplier_id')->references('id')->on('suppliers'); + }); + + foreach (DB::table('supplier_user')->cursor() as $o) { + $ao = \App\Models\Account::where('user_id',$o->user_id)->firstOrfail(); + + DB::table('account_supplier') + ->insert([ + 'created_at' => $o->created_at, + 'updated_at' => $o->updated_at, + 'supplier_id' => $o->supplier_id, + 'account_id' => $ao->id, + 'supplier_ref' => $o->supplier_ref, + 'active' => $o->active, + ]); + } + + Schema::drop('supplier_user'); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + abort(500,'Cant go back'); + } +}; diff --git a/resources/views/theme/backend/adminlte/account/widget/supplier.blade.php b/resources/views/theme/backend/adminlte/account/widget/supplier.blade.php new file mode 100644 index 0000000..927c346 --- /dev/null +++ b/resources/views/theme/backend/adminlte/account/widget/supplier.blade.php @@ -0,0 +1,59 @@ + +@use(App\Models\Supplier) + + +
+
+
+
+ + + + + + + + + + + + + @foreach ($o->suppliers as $so) + + + + + + @endforeach + + + @if(($x=Supplier::active()->whereNotIn('id',$o->suppliers->pluck('id'))->orderBy('name')->get())->count()) + + + + + + @endif +
SupplierIDAdded
{{ $so->name }}{{ $so->pivot->supplier_ref }}{{ $so->pivot->created_at }}
+
+ @csrf + +
+
+ +
+ +
+ +
+ +
+ Save +
+
+
+
+
+
+
+
\ No newline at end of file diff --git a/resources/views/theme/backend/adminlte/home.blade.php b/resources/views/theme/backend/adminlte/home.blade.php index 254bba8..43ba3d8 100644 --- a/resources/views/theme/backend/adminlte/home.blade.php +++ b/resources/views/theme/backend/adminlte/home.blade.php @@ -82,8 +82,8 @@ @canany('reseller','wholesaler') -
- @include('theme.backend.adminlte.user.widget.supplier') +
session()->pull('supplier_update')]) id="tab-supplier" role="tabpanel"> + @include('theme.backend.adminlte.account.widget.supplier',['o'=>$ao])
@endcanany
diff --git a/resources/views/theme/backend/adminlte/user/widget/supplier.blade.php b/resources/views/theme/backend/adminlte/user/widget/supplier.blade.php deleted file mode 100644 index 6bddb5b..0000000 --- a/resources/views/theme/backend/adminlte/user/widget/supplier.blade.php +++ /dev/null @@ -1,70 +0,0 @@ - - -
-
-
-
- @include('adminlte::widget.success_button') - - - - - - - - - - - @foreach ($o->suppliers as $so) - - - - - - @endforeach - - - @if(($x=\App\Models\Supplier::active()->whereNotIn('id',$o->suppliers->pluck('id'))->orderBy('name')->get())->count()) - - - - - - @endif -
SupplierIDAdded
{{ $so->name }}{{ $so->pivot->id }}{{ $so->pivot->created_at }}
-
- @csrf - -
-
- @include('adminlte::widget.form_select',[ - 'label'=>'Add Supplier', - 'icon'=>'fas fa-handshake', - 'id'=>'supplier_id', - 'old'=>'supplier_id', - 'options'=>$x->transform(function($item) { return ['id'=>$item->id,'value'=>$item->name]; }), - 'value'=>'', - ]) -
-
- @include('adminlte::widget.form_text',[ - 'label'=>'ID', - 'icon'=>'fas fa-hashtag', - 'id'=>'id', - 'old'=>'id', - 'name'=>'id', - 'value'=>'', - ]) -
-
-
- -
-
-
-
-
-
-
-
-
\ No newline at end of file diff --git a/routes/web.php b/routes/web.php index 402170f..48ab86c 100644 --- a/routes/web.php +++ b/routes/web.php @@ -62,6 +62,13 @@ Route::get('admin/switch/stop',[SwitchUserController::class,'switch_stop']) // Our Admin Routes - for wholesalers Route::group(['middleware'=>['auth','role:wholesaler'],'prefix'=>'a'],function() { + // Linking supplier to account + Route::post('account/supplier/add/{o}',[UserController::class,'supplier_addedit']) + ->where('o','[0-9]+'); + Route::get('account/supplier/delete/{o}/{so}',[UserController::class,'supplier_delete']) + ->where('o','[0-9]+') + ->where('so','[0-9]+'); + // Site Setup Route::view('setup','theme.backend.adminlte.a.setup'); Route::post('setup',[AdminController::class,'setup']); @@ -119,13 +126,6 @@ Route::group(['middleware'=>['auth','role:wholesaler'],'prefix'=>'a'],function() Route::post('service/update/{o}',[ServiceController::class,'update']) ->where('o','[0-9]+'); - // Linking supplier to user - Route::post('user/supplier/add/{o}',[UserController::class,'supplier_addedit']) - ->where('o','[0-9]+'); - Route::get('user/supplier/delete/{o}/{so}',[UserController::class,'supplier_delete']) - ->where('o','[0-9]+') - ->where('so','[0-9]+'); - //@deprecated // Route::get('service/{o}','AdminHomeController@service'); // Route::post('service/{o}','AdminHomeController@service_update');