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) + + +