171 lines
5.6 KiB
PHP
171 lines
5.6 KiB
PHP
<?php
|
|
|
|
namespace App\Modules\LoanOrder\Controllers;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Order\Loan\LoanOrder;
|
|
use App\Modules\DateHelper\Repositories\DateHelperRepository;
|
|
use App\Modules\LoanOrder\Controllers\Requests\LoanOrderStoreRequest;
|
|
use App\Modules\LoanOrder\Controllers\Requests\LoanOrderUpdateRequest;
|
|
use App\Modules\LoanOrder\Controllers\Resources\LoanOrderIndexResource;
|
|
use App\Modules\LoanOrder\Controllers\Resources\LoanOrderShowResource;
|
|
use App\Repos\Order\OrderRepo;
|
|
use Dedoc\Scramble\Attributes\Group;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Str;
|
|
|
|
#[Group('Sargytlar - Karz - Karz sargytlary Mobile')]
|
|
class LoanOrderController extends Controller
|
|
{
|
|
/**
|
|
* LIST* Loan orders.
|
|
*
|
|
* `Karz sargytlary list`-leri list gornushde gelyar. https://online.tbbank.gov.tm/work-place/resources/loan-order-mobiles dan `Label` gorup bilyan. `BaseAppEnum` dan gerek yerlerini match edishene gora alyan.
|
|
*/
|
|
public function index(Request $request): JsonResponse
|
|
{
|
|
return response()->json(LoanOrderIndexResource::collection(
|
|
LoanOrder::query()
|
|
->where('user_id', auth()->id())
|
|
->where('source', OrderRepo::MOBILE_DEVICE)
|
|
->paginate()
|
|
));
|
|
}
|
|
|
|
/**
|
|
* SAVE* Loan order.
|
|
*
|
|
* `Karz sargytlary save`. Example bar, online panelkadan gorup hem bilersin. Update store daky yaly, yone oz ugratyan zadyn update bolyar, eger ugratmasaň, üýtgemez.
|
|
*/
|
|
public function store(LoanOrderStoreRequest $request): JsonResponse
|
|
{
|
|
Log::channel('form_logs')->info('loan-order-store-request', $request->all());
|
|
|
|
$data = $request->validated();
|
|
|
|
$months = DateHelperRepository::monthsAsNumber();
|
|
$years = DateHelperRepository::yearsUntil();
|
|
|
|
$data['card_month'] = indexByValue($request->card_month, $months);
|
|
$data['card_year'] = indexByValue($request->card_year, $years);
|
|
|
|
$data['guarantor_card_month'] = indexByValue($request->guarantor_card_month, $months);
|
|
$data['guarantor_card_year'] = indexByValue($request->guarantor_card_year, $years);
|
|
|
|
$data['guarantor_2_card_month'] = indexByValue($request->guarantor_2_card_month, $months);
|
|
$data['guarantor_2_card_year'] = indexByValue($request->guarantor_2_card_year, $years);
|
|
|
|
LoanOrder::forceCreate([
|
|
...$data,
|
|
...[
|
|
'user_id' => auth()->id(),
|
|
'status' => OrderRepo::PENDING,
|
|
'source' => OrderRepo::MOBILE_DEVICE,
|
|
],
|
|
...$this->uploadedFiles($request),
|
|
]);
|
|
|
|
return response()->json([
|
|
'message' => __('Successfully created'),
|
|
], 201);
|
|
}
|
|
|
|
/**
|
|
* Upload files
|
|
*
|
|
* @return array<string, string>
|
|
*/
|
|
public function uploadedFiles(Request $request): array
|
|
{
|
|
$files = [];
|
|
|
|
foreach (['passport_one', 'passport_two', 'passport_three', 'passport_four'] as $field) {
|
|
if ($request->hasFile($field)) {
|
|
$files[$field] = Str::after($request->file($field)->store('public'), 'public/');
|
|
}
|
|
}
|
|
|
|
return $files;
|
|
}
|
|
|
|
/**
|
|
* SHOW* Loan order
|
|
*
|
|
* `Karz sargytlary show by id`. ID ugradyp alyan route -da. Base App Enum-lardan peydalan. Panelkadan gor.
|
|
*/
|
|
public function show(LoanOrder $loanOrder): JsonResponse
|
|
{
|
|
if ($loanOrder->user_id != auth()->id()) {
|
|
return response()->json(status: 403);
|
|
}
|
|
|
|
return response()->json(new LoanOrderShowResource($loanOrder));
|
|
}
|
|
|
|
/**
|
|
* UPDATE* Loan order
|
|
*
|
|
* `Karz sargytlary update`. ID ugradyp `route`-da update edip bilyan. Base App Enum-lardan peydalan. Panelkadan gor.
|
|
*/
|
|
public function update(LoanOrderUpdateRequest $request, LoanOrder $loanOrder): JsonResponse
|
|
{
|
|
Log::channel('form_logs')->info('loan-order-update-request', $request->all());
|
|
|
|
$data = $request->all();
|
|
|
|
$months = DateHelperRepository::monthsAsNumber();
|
|
$years = DateHelperRepository::yearsUntil();
|
|
|
|
if ($request->filled('card_month')) {
|
|
$data['card_month'] = indexByValue($request->card_month, $months);
|
|
}
|
|
|
|
if ($request->filled('card_year')) {
|
|
$data['card_year'] = indexByValue($request->card_year, $years);
|
|
}
|
|
|
|
if ($request->filled('guarantor_card_month')) {
|
|
$data['guarantor_card_month'] = indexByValue($request->guarantor_card_month, $months);
|
|
}
|
|
|
|
if ($request->filled('guarantor_card_year')) {
|
|
$data['guarantor_card_year'] = indexByValue($request->guarantor_card_year, $years);
|
|
}
|
|
|
|
if ($request->filled('guarantor_2_card_month')) {
|
|
$data['guarantor_2_card_month'] = indexByValue($request->guarantor_2_card_month, $months);
|
|
}
|
|
|
|
if ($request->filled('guarantor_2_card_year')) {
|
|
$data['guarantor_2_card_year'] = indexByValue($request->guarantor_2_card_year, $years);
|
|
}
|
|
|
|
$data += $this->uploadedFiles($request);
|
|
|
|
Model::unguarded(function () use ($loanOrder, $data) {
|
|
$loanOrder->update($data);
|
|
});
|
|
|
|
return response()->json([
|
|
'message' => __('Successfully updates'),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* DELETE* loan order resource
|
|
*/
|
|
public function destroy(LoanOrder $loanOrder): JsonResponse
|
|
{
|
|
if ($loanOrder->user_id != auth()->id()) {
|
|
return response()->json(status: 403);
|
|
}
|
|
|
|
$loanOrder->delete();
|
|
|
|
return response()->json();
|
|
}
|
|
}
|