2022-08-07 02:17:20 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
2024-07-22 14:13:54 +00:00
|
|
|
use Illuminate\Http\RedirectResponse;
|
|
|
|
use Illuminate\Support\Arr;
|
|
|
|
use Illuminate\Support\Facades\Hash;
|
2022-08-07 02:17:20 +00:00
|
|
|
use Illuminate\Support\Facades\Session;
|
|
|
|
|
2024-07-23 12:17:09 +00:00
|
|
|
use App\Http\Requests\{AccountSupplierAdd,UserEdit};
|
|
|
|
use App\Models\{Account,Supplier,User};
|
2022-08-07 02:17:20 +00:00
|
|
|
|
|
|
|
class UserController extends Controller
|
|
|
|
{
|
2024-07-22 14:13:54 +00:00
|
|
|
/**
|
|
|
|
* Update user settings
|
|
|
|
*
|
|
|
|
* @param UserEdit $request
|
|
|
|
* @param User $o
|
|
|
|
* @return RedirectResponse
|
|
|
|
*/
|
|
|
|
public function edit(UserEdit $request,User $o): RedirectResponse
|
|
|
|
{
|
|
|
|
foreach (Arr::except($request->validated(),['password']) as $field => $value)
|
|
|
|
$o->{$field} = $value;
|
|
|
|
|
|
|
|
if ($x=$request->validated('password'))
|
|
|
|
$o->password = Hash::make($x);
|
|
|
|
|
|
|
|
return redirect()
|
|
|
|
->back()
|
|
|
|
->with('success',($o->isDirty() && $o->save()) ? 'User Updated' : 'No Changes');
|
|
|
|
}
|
|
|
|
|
2022-08-07 02:17:20 +00:00
|
|
|
/**
|
|
|
|
* Add a supplier to a user's profile
|
|
|
|
*
|
2024-07-23 12:17:09 +00:00
|
|
|
* @param AccountSupplierAdd $request
|
|
|
|
* @param Account $o
|
|
|
|
* @return RedirectResponse
|
2022-08-07 02:17:20 +00:00
|
|
|
*/
|
2024-07-23 12:17:09 +00:00
|
|
|
public function supplier_addedit(AccountSupplierAdd $request,Account $o): RedirectResponse
|
2022-08-07 02:17:20 +00:00
|
|
|
{
|
|
|
|
$o->suppliers()->attach([
|
2024-07-23 12:17:09 +00:00
|
|
|
$request->validated('supplier_id') => [
|
|
|
|
'supplier_ref'=>$request->validated('supplier_ref'),
|
2022-08-07 02:17:20 +00:00
|
|
|
'created_at'=>Carbon::now(),
|
|
|
|
]
|
|
|
|
]);
|
|
|
|
|
2024-07-04 05:03:11 +00:00
|
|
|
return redirect()
|
|
|
|
->back()
|
|
|
|
->with('success','Supplier Added');
|
2022-08-07 02:17:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a supplier from a user's profile
|
|
|
|
*
|
2024-07-23 12:17:09 +00:00
|
|
|
* @param Account $o
|
2022-08-07 02:17:20 +00:00
|
|
|
* @param Supplier $so
|
2024-07-23 12:17:09 +00:00
|
|
|
* @return RedirectResponse
|
2022-08-07 02:17:20 +00:00
|
|
|
*/
|
2024-07-23 12:17:09 +00:00
|
|
|
public function supplier_delete(Account $o,Supplier $so): RedirectResponse
|
2022-08-07 02:17:20 +00:00
|
|
|
{
|
2024-07-23 12:17:09 +00:00
|
|
|
Session::put('supplier_update',TRUE);
|
2022-08-07 02:17:20 +00:00
|
|
|
|
|
|
|
$o->suppliers()->detach([$so->id]);
|
|
|
|
|
2024-07-04 05:03:11 +00:00
|
|
|
return redirect()
|
|
|
|
->back()
|
|
|
|
->with('success','Supplier Deleted');
|
2022-08-07 02:17:20 +00:00
|
|
|
}
|
|
|
|
}
|