240 lines
6.8 KiB
PHP
240 lines
6.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\CardTransaction;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Order\Card\CardTransaction\CardTransaction;
|
|
use App\Modules\DateHelper\Repositories\DateHelperRepository;
|
|
use App\Nova\Resources\Order\Card\CardTransaction\Actions\DownloadCardTransaction;
|
|
use App\Repos\System\Settings\Legal\PassportRepo;
|
|
use Dedoc\Scramble\Attributes\Group;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Carbon;
|
|
use Illuminate\Support\Str;
|
|
use Illuminate\Validation\Rule;
|
|
|
|
#[Group('Sargytlar - Kart - Kart hereketleri')]
|
|
class CardTransactionsController extends Controller
|
|
{
|
|
/**
|
|
* This month transactions on card
|
|
*/
|
|
public function lastMonth(): JsonResponse
|
|
{
|
|
$user = auth()->user();
|
|
|
|
$start_date = today()->startOfMonth()->format('d.m.Y');
|
|
$end_date = today()->endOfMonth()->format('d.m.Y');
|
|
$passport_serie = $user->getOption('passport_serie');
|
|
$passport_id = $user->getOption('passport_id');
|
|
$card_number = $user->getOption('card_number');
|
|
$card_month = $user->getOption('card_month');
|
|
$card_year = $user->getOption('card_year');
|
|
|
|
if (
|
|
is_null($passport_serie) || $passport_serie === '' ||
|
|
is_null($passport_id) || $passport_id === '' ||
|
|
is_null($card_number) || $card_number === '' ||
|
|
is_null($card_month) || $card_month === '' ||
|
|
is_null($card_year) || $card_year === ''
|
|
) {
|
|
return response()->rest([
|
|
'status' => false,
|
|
'message' => 'Profile data needed',
|
|
'data' => '',
|
|
]);
|
|
}
|
|
|
|
$response = fetchCardTransactionFromAzat(
|
|
passport_serie: $passport_serie,
|
|
passport_id: $passport_id,
|
|
card_number_masked: Str::mask($card_number, '*', 6, 6),
|
|
card_expire_date: $card_month.'/'.substr($card_year, 2),
|
|
start_date: $start_date,
|
|
end_date: $end_date,
|
|
);
|
|
|
|
info(['re' => $response]);
|
|
|
|
if ($response->errCode != 0) {
|
|
return response()->json([
|
|
'status' => false,
|
|
'message' => $response->message,
|
|
'data' => [],
|
|
]);
|
|
}
|
|
|
|
return response()->rest([
|
|
'status' => true,
|
|
'message' => 'a',
|
|
'data' => [],
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* LIST*
|
|
*/
|
|
public function index(): JsonResponse
|
|
{
|
|
return response()->json(
|
|
CardTransaction::query()
|
|
->where('user_id', auth()->id())
|
|
->latest()
|
|
->get()
|
|
);
|
|
}
|
|
|
|
/**
|
|
* SAVE*
|
|
*/
|
|
public function store(Request $request): JsonResponse
|
|
{
|
|
$data = $request->validate([
|
|
/**
|
|
* @example I-AS
|
|
*/
|
|
'passport_serie' => ['required', Rule::in(array_keys(PassportRepo::values()))],
|
|
|
|
/**
|
|
* @example 379514
|
|
*/
|
|
'passport_id' => ['required', 'numeric', 'digits:6'],
|
|
|
|
/**
|
|
* @example 9934612100000243
|
|
*/
|
|
'card_number' => ['required', 'digits:16'],
|
|
|
|
/**
|
|
* @example 12
|
|
*/
|
|
'card_month' => ['required', Rule::in(array_keys(DateHelperRepository::staticNumberMonths()))],
|
|
|
|
/**
|
|
* @example 2049
|
|
*/
|
|
'card_year' => ['required', Rule::in(array_keys(DateHelperRepository::staticNumberYears()))],
|
|
]);
|
|
|
|
CardTransaction::forceCreate([
|
|
...$data,
|
|
...[
|
|
'user_id' => auth()->id(),
|
|
],
|
|
]);
|
|
|
|
return response()->json([
|
|
'message' => __('Successfully created'),
|
|
], 201);
|
|
}
|
|
|
|
/**
|
|
* SHOW*
|
|
*
|
|
* ID ugradyp alyan route -da. Soň page-da download button bolmaly, basaňdan soň, modal çykmaly, start date bilen end date ugradyp download basanda `/api/card-transactions-download/{order}` route gelyan pdf download etmeli.
|
|
*/
|
|
public function show(CardTransaction $order): JsonResponse
|
|
{
|
|
if ($order->user_id != auth()->id()) {
|
|
return response()->json(status: 403);
|
|
}
|
|
|
|
return response()->json($order);
|
|
}
|
|
|
|
/**
|
|
* Download*
|
|
*
|
|
* Download card transaction file
|
|
*/
|
|
public function download(Request $request, CardTransaction $order)
|
|
{
|
|
$data = $request->validate([
|
|
'start_date' => ['required', 'string', 'date'],
|
|
'end_date' => ['required', 'string', 'date', 'after:start_date'],
|
|
]);
|
|
|
|
$start_date = Carbon::create($data['start_date'])->format('d.m.Y');
|
|
$end_date = Carbon::create($data['end_date'])->format('d.m.Y');
|
|
|
|
$response = vp_fetch_ClientInfoAll($order, $start_date, $end_date);
|
|
|
|
if ($response->errCode != 0) {
|
|
return response()->json([
|
|
'status' => false,
|
|
'message' => $response->message,
|
|
'url' => '',
|
|
]);
|
|
}
|
|
|
|
$url = DownloadCardTransaction::make()->doFiles($order, $response);
|
|
|
|
return response()->json([
|
|
'status' => true,
|
|
'message' => $response->message,
|
|
'url' => $url,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* UPDATE*
|
|
*
|
|
* ID ugradyp `route`-da update edip bilyan. Base App Enum-lardan peydalan. Panelkadan gor.
|
|
*/
|
|
public function update(Request $request, CardTransaction $order): JsonResponse
|
|
{
|
|
$data = $request->validate([
|
|
/**
|
|
* @example I-AS
|
|
*/
|
|
'passport_serie' => ['sometimes', Rule::in(array_keys(PassportRepo::values()))],
|
|
|
|
/**
|
|
* @example 379514
|
|
*/
|
|
'passport_id' => ['sometimes', 'numeric', 'digits:6'],
|
|
|
|
/**
|
|
* @example 9934612100000243
|
|
*/
|
|
'card_number' => ['sometimes', 'digits:16'],
|
|
|
|
/**
|
|
* @example 12
|
|
*/
|
|
'card_month' => ['sometimes', Rule::in(array_keys(DateHelperRepository::staticNumberMonths()))],
|
|
|
|
/**
|
|
* @example 2049
|
|
*/
|
|
'card_year' => ['sometimes', Rule::in(array_keys(DateHelperRepository::staticNumberYears()))],
|
|
]);
|
|
|
|
Model::unguarded(function () use ($order, $data) {
|
|
$order->update($data);
|
|
});
|
|
|
|
return response()->json([
|
|
'message' => __('Successfully updated'),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* DELETE*
|
|
*/
|
|
public function destroy(CardTransaction $order): JsonResponse
|
|
{
|
|
if ($order->user_id != auth()->id()) {
|
|
return response()->json(status: 403);
|
|
}
|
|
|
|
$order->delete();
|
|
|
|
return response()->json([
|
|
'message' => __('Successfully deleted'),
|
|
]);
|
|
}
|
|
}
|