107 lines
3.5 KiB
PHP
107 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Bo\Credit;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Bo\CreditAccounts\StoreCreditAccountRequest;
|
|
use App\Models\Brand;
|
|
use App\Models\Credit\Account;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Str;
|
|
|
|
class AccountController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the resource.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function index(Request $request): \Illuminate\Contracts\View\View
|
|
{
|
|
/** @var \App\Models\User $user */
|
|
$user = auth()->user();
|
|
|
|
if (!$user->isAdmin()) {
|
|
$brandModels = $user->brands()->pluck('id');
|
|
} else {
|
|
$brandModels = $request->brand ?? Brand::all()->pluck('id');
|
|
}
|
|
|
|
/** @var \Illuminate\Database\Eloquent\Builder $credit_accountModels */
|
|
$credit_accountModels = Account::query()->whereIn('brand_id', $brandModels);
|
|
|
|
/** @var string $search Search field */
|
|
$search = $request->search;
|
|
if ($search) {
|
|
$this->searchQuery(
|
|
$credit_accountModels,
|
|
$search,
|
|
null,
|
|
'label',
|
|
'balance',
|
|
);
|
|
}
|
|
$searchFields = trans('Label, solde');
|
|
|
|
/** Sort columns with a query */
|
|
$this->sortQuery($credit_accountModels);
|
|
|
|
/** Custom pagination */
|
|
$credit_accountModels = $this->customPaginate($credit_accountModels, $request->pagination);
|
|
|
|
return view('bo.pages.credit_accounts.index', compact(
|
|
'credit_accountModels',
|
|
'brandModels',
|
|
'search',
|
|
'searchFields'
|
|
));
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*
|
|
* @param \App\Models\Brand $brand
|
|
* @param \App\Models\Account $account
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function create(Brand $brand, Account $account): \Illuminate\Contracts\View\View
|
|
{
|
|
return view('bo.pages.credit_accounts.create', [
|
|
'brandModel' => $brand,
|
|
'credit_accountModel' => $account
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @param \App\Models\Brand $brand
|
|
* @param \App\Http\Requests\Bo\CreditAccounts\StoreCreditAccountRequest $request
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function store(Brand $brand, StoreCreditAccountRequest $request): \Illuminate\Http\RedirectResponse
|
|
{
|
|
return DB::transaction(function () use ($brand, $request) {
|
|
$credit_accountModel = new Account();
|
|
$credit_accountModel->brand_id = $brand->id;
|
|
$credit_accountModel->fill($request->validated());
|
|
|
|
if ($credit_accountModel->saveOrFail()) {
|
|
return redirect()
|
|
->route('bo.brands.credit_accounts.index', $brand)
|
|
->with('success', trans(
|
|
'crud.messages.has_been_created',
|
|
['model' => Str::singular(trans('models.classes.accounts'))]
|
|
));
|
|
}
|
|
return redirect()
|
|
->back()
|
|
->with('error', trans(
|
|
'crud.messages.cannot_be_created',
|
|
['model' => Str::singular(trans('models.classes.accounts'))]
|
|
));
|
|
});
|
|
}
|
|
}
|