Move user suppliers to account suppliers
All checks were successful
Create Docker Image / Build Docker Image (x86_64) (push) Successful in 33s
Create Docker Image / Final Docker Image Manifest (push) Successful in 9s

This commit is contained in:
Deon George 2024-07-23 22:17:09 +10:00
parent b145856ce9
commit cb9be6534f
10 changed files with 230 additions and 112 deletions

View File

@ -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]);

View File

@ -0,0 +1,42 @@
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Session;
use Illuminate\Validation\Rule;
class AccountSupplierAdd extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return Gate::allows('wholesaler');
}
/**
* 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('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',
];
}
}

View File

@ -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
*/

View File

@ -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 */
/**

View File

@ -0,0 +1,47 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('supplier_user', function (Blueprint $table) {
$table->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');
});
}
};

View File

@ -0,0 +1,52 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('account_supplier', function (Blueprint $table) {
$table->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');
}
};

View File

@ -0,0 +1,59 @@
<!-- $o=Account::class -->
@use(App\Models\Supplier)
<!-- Suppliers Configuration for this User -->
<div class="row">
<div class="col-6">
<div class="card">
<div class="card-body">
<x-leenooks::button.success class="float-right"/>
<table class="table">
<thead>
<tr>
<th>Supplier</th>
<th>ID</th>
<th>Added</th>
</tr>
</thead>
<tbody>
@foreach ($o->suppliers as $so)
<tr>
<td>{{ $so->name }}</td>
<td>{{ $so->pivot->supplier_ref }}</td>
<td>{{ $so->pivot->created_at }} <a class="float-right" href="{{ url('a/account/supplier/delete',[$o->id,$so->id]) }}"><i class="fas fa-fw fa-trash"></i></a></td>
</tr>
@endforeach
</tbody>
@if(($x=Supplier::active()->whereNotIn('id',$o->suppliers->pluck('id'))->orderBy('name')->get())->count())
<tfoot>
<tr>
<td colspan="3">
<form method="POST" action="{{ url('a/account/supplier/add',[$o->id]) }}">
@csrf
<div class="row">
<div class="col-6">
<x-leenooks::form.select id="supplier_id" name="supplier_id" icon="fa-handshake" label="Add Supplier" :options="$x->map(function($item) { $item->value = $item->name; return $item; })->toArray()"/>
</div>
<div class="col-4">
<x-leenooks::form.text id="supplier_ref" name="supplier_ref" icon="fa-hashtag" label="ID"/>
</div>
<div class="col-2">
<x-leenooks::button.submit class="float-right">Save</x-leenooks::button.submit>
</div>
</div>
</form>
</td>
</tr>
</tfoot>
@endif
</table>
</div>
</div>
</div>
</div>

View File

@ -82,8 +82,8 @@
</div>
@canany('reseller','wholesaler')
<div class="tab-pane {{ session()->pull('supplier_update') ? 'active' : '' }}" id="tab-supplier" role="tabpanel">
@include('theme.backend.adminlte.user.widget.supplier')
<div @class(['tab-pane','active'=>session()->pull('supplier_update')]) id="tab-supplier" role="tabpanel">
@include('theme.backend.adminlte.account.widget.supplier',['o'=>$ao])
</div>
@endcanany
</div>

View File

@ -1,70 +0,0 @@
<!-- $o=User::class -->
<!-- Suppliers Configuration for this User -->
<div class="row">
<div class="col-6">
<div class="card">
<div class="card-body">
@include('adminlte::widget.success_button')
<table class="table">
<thead>
<tr>
<th>Supplier</th>
<th>ID</th>
<th>Added</th>
</tr>
</thead>
<tbody>
@foreach ($o->suppliers as $so)
<tr>
<td>{{ $so->name }}</td>
<td>{{ $so->pivot->id }}</td>
<td>{{ $so->pivot->created_at }} <a class="float-right" href="{{ url('a/user/supplier/delete',[$o->id,$so->id]) }}"><i class=" fa-fw fas fa-trash"></i></a></td>
</tr>
@endforeach
</tbody>
@if(($x=\App\Models\Supplier::active()->whereNotIn('id',$o->suppliers->pluck('id'))->orderBy('name')->get())->count())
<tfoot>
<tr>
<td colspan="3">
<form class="g-0 needs-validation" method="POST" action="{{ url('a/user/supplier/add',[$o->id]) }}" enctype="multipart/form-data" role="form">
@csrf
<div class="row">
<div class="col-6">
@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'=>'',
])
</div>
<div class="col-4">
@include('adminlte::widget.form_text',[
'label'=>'ID',
'icon'=>'fas fa-hashtag',
'id'=>'id',
'old'=>'id',
'name'=>'id',
'value'=>'',
])
</div>
<div class="col-2">
<div class="form-group">
<button type="submit" class="mt-4 float-right btn btn-sm btn-success">Add</button>
</div>
</div>
</div>
</form>
</td>
</tr>
</tfoot>
@endif
</table>
</div>
</div>
</div>
</div>

View File

@ -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');