108 lines
3.6 KiB
PHP
108 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Bo\Credit;
|
|
|
|
use App\Enums\Credit\BalanceOperationEnum;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Bo\CreditLogs\StoreCreditLogRequest;
|
|
use App\Models\Brand;
|
|
use App\Models\Credit\Account;
|
|
use App\Models\Credit\CreditLog;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Str;
|
|
|
|
class CreditLogController 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
|
|
{
|
|
$brandModel = $request->brand;
|
|
$credit_accountModel = $request->account;
|
|
|
|
/** @var \Illuminate\Database\Eloquent\Builder $credit_logModels */
|
|
$credit_logModels = CreditLog::query()->where('credit_account_id', $credit_accountModel->id);
|
|
|
|
/** @var string $search Search field */
|
|
$search = $request->search;
|
|
if ($search) {
|
|
$this->searchQuery(
|
|
$credit_logModels,
|
|
$search,
|
|
null,
|
|
'amount',
|
|
);
|
|
}
|
|
$searchFields = trans('Montant');
|
|
|
|
/** Sort columns with a query */
|
|
$this->sortQuery($credit_logModels);
|
|
|
|
/** Custom pagination */
|
|
$credit_logModels = $this->customPaginate($credit_logModels, $request->pagination);
|
|
|
|
return view('bo.pages.credit_logs.index', compact(
|
|
'credit_logModels',
|
|
'credit_accountModel',
|
|
'brandModel',
|
|
'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_logs.create', [
|
|
'brandModel' => $brand,
|
|
'credit_accountModel' => $account
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @param \App\Models\Brand $brand
|
|
* @param \App\Models\Account $account
|
|
* @param \App\Http\Requests\Bo\CreditLogs\StoreCreditLogRequest $request
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function store(
|
|
Brand $brand,
|
|
Account $account,
|
|
StoreCreditLogRequest $request
|
|
): \Illuminate\Http\RedirectResponse {
|
|
return DB::transaction(function () use ($brand, $account, $request) {
|
|
$credit_logModel = new CreditLog();
|
|
$credit_logModel->credit_account_id = $request->account->id;
|
|
$credit_logModel->type = BalanceOperationEnum::credit;
|
|
$credit_logModel->fill($request->validated());
|
|
|
|
if ($credit_logModel->saveOrFail()) {
|
|
return redirect()
|
|
->route('bo.brands.credit_accounts.index', ['brand' => $brand, 'account' => $account])
|
|
->with('success', trans(
|
|
':model a été crédité',
|
|
['model' => Str::singular(trans('models.classes.accounts'))]
|
|
));
|
|
}
|
|
return redirect()
|
|
->back()
|
|
->with('error', trans(
|
|
':model ne peut être crédité',
|
|
['model' => Str::singular(trans('models.classes.accounts'))]
|
|
));
|
|
});
|
|
}
|
|
}
|