197 lines
6.2 KiB
PHP
197 lines
6.2 KiB
PHP
<?php
|
|
|
|
namespace App\Nova\Resources\Order\Card\CardTransaction\Actions;
|
|
|
|
use Carbon\Carbon;
|
|
use Illuminate\Bus\Queueable;
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Support\Facades\Blade;
|
|
use Illuminate\Support\Facades\File;
|
|
use Illuminate\Support\Str;
|
|
use Laravel\Nova\Actions\Action;
|
|
use Laravel\Nova\Actions\ActionResponse;
|
|
use Laravel\Nova\Fields\ActionFields;
|
|
use Laravel\Nova\Fields\Date;
|
|
use Laravel\Nova\Http\Requests\NovaRequest;
|
|
use Mpdf\Mpdf;
|
|
|
|
class DownloadCardTransaction extends Action
|
|
{
|
|
use InteractsWithQueue, Queueable;
|
|
|
|
/**
|
|
* Name.
|
|
*/
|
|
public function name(): string
|
|
{
|
|
return __('Download');
|
|
}
|
|
|
|
/**
|
|
* Perform the action on the given models.
|
|
*
|
|
* @param \Laravel\Nova\Fields\ActionFields $fields
|
|
* @param \Illuminate\Support\Collection $models
|
|
* @return mixed
|
|
*/
|
|
public function handle(ActionFields $fields, Collection $models)
|
|
{
|
|
/** @var \App\Models\Order\Card\CardTransaction\CardTransaction */
|
|
$model = $models->first();
|
|
|
|
$start_date = Carbon::create($fields['start_date']);
|
|
$end_date = Carbon::create($fields['end_date']);
|
|
|
|
$response = $this->fetchApi(
|
|
passport_serie: $model->passport_serie,
|
|
passport_id: $model->passport_id,
|
|
card_number_masked: Str::mask($model->card_number, '*', 6, 6),
|
|
card_expire_date: $model->card_month.'/'.substr($model->card_year, 2),
|
|
start_date: $start_date->format('d.m.Y'),
|
|
end_date: $end_date->format('d.m.Y'),
|
|
);
|
|
|
|
/** @var ResponseTypes\AzatApiClientInfoAllResponse */
|
|
$data = Str::isJson($response)
|
|
? json_decode($response)
|
|
: emptyClass(errCode: 1, message: 'Connection issue to VP');
|
|
|
|
if ($data->errCode != 0) {
|
|
return ActionResponse::danger($data->message);
|
|
}
|
|
|
|
$unique_folder_name = Str::snake(str_replace(':', '-', $model->created_at->toDateTimeString()));
|
|
$dir = public_path("files/{$unique_folder_name}");
|
|
|
|
File::makeDirectory($dir, 0777, true, true);
|
|
|
|
$fileDest = $dir."/{$model->id}.pdf";
|
|
|
|
$this->generateFile($data, $fileDest);
|
|
|
|
return ActionResponse::openInNewTab(url("files/{$unique_folder_name}/{$model->id}.pdf"));
|
|
}
|
|
|
|
/**
|
|
* Get the fields available on the action.
|
|
*
|
|
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
|
|
* @return array
|
|
*/
|
|
public function fields(NovaRequest $request): array
|
|
{
|
|
return [
|
|
Date::make(__('Start date'), 'start_date')
|
|
->default(date('Y-m-d', strtotime('-6 months')))
|
|
->rules('required'),
|
|
|
|
Date::make(__('End date'), 'end_date')
|
|
->default(date('Y-m-d'))
|
|
->rules('required'),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Fetch from internal API
|
|
*
|
|
* @param string $passport_serie
|
|
* @param string $passport_id
|
|
* @param string $card_number_masked
|
|
* @param string $card_expire_date
|
|
* @param string $start_date
|
|
* @param string $end_date
|
|
*/
|
|
public function fetchApi(
|
|
string $passport_serie,
|
|
string $passport_id,
|
|
string $card_number_masked,
|
|
string $card_expire_date,
|
|
string $start_date,
|
|
string $end_date
|
|
) {
|
|
$curl = curl_init();
|
|
curl_setopt_array($curl, [
|
|
CURLOPT_URL => 'http://10.3.158.102:9999/api/clientinfo/all',
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
CURLOPT_ENCODING => '',
|
|
CURLOPT_MAXREDIRS => 10,
|
|
CURLOPT_TIMEOUT => 0,
|
|
CURLOPT_FOLLOWLOCATION => true,
|
|
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
|
|
CURLOPT_CUSTOMREQUEST => 'POST',
|
|
CURLOPT_POSTFIELDS => sprintf('{ "idSeria": "%s", "idNo": "%s", "clientType": "recipient", "cardMaskNumber": "%s", "expDate": "%s", "fromDate" : "%s", "toDate" : "%s" }', $passport_serie, $passport_id, $card_number_masked, $card_expire_date, $start_date, $end_date),
|
|
CURLOPT_HTTPHEADER => [
|
|
'Authorization: Basic dGJ1c2VyOlFBWndzeDEyMw==',
|
|
'Content-Type: application/json',
|
|
],
|
|
]);
|
|
|
|
$response = curl_exec($curl);
|
|
|
|
curl_close($curl);
|
|
|
|
return $response;
|
|
}
|
|
|
|
/**
|
|
* Generate file
|
|
*
|
|
* @param ResponseTypes\AzatApiClientInfoAllResponse $data
|
|
* @param string $fileDest
|
|
*/
|
|
public function generateFile($data, $fileDest)
|
|
{
|
|
$mpdf = new Mpdf;
|
|
|
|
// Write HTML content...
|
|
$html = Blade::render('orders.cards.card-transaction.download-card-transaction', [
|
|
'data' => $data,
|
|
'extra' => $this->getExtraVariables($data),
|
|
]);
|
|
|
|
$mpdf->WriteHTML($html);
|
|
|
|
// Save the PDF to a file...
|
|
$mpdf->Output($fileDest, \Mpdf\Output\Destination::FILE);
|
|
}
|
|
|
|
/**
|
|
* @param ResponseTypes\AzatApiClientInfoAllResponse $data
|
|
*/
|
|
public function getExtraVariables($data)
|
|
{
|
|
if (count($data->transactions) < 1) {
|
|
return emptyClass(basdakyGalyndy: 0, ahyrkyGalyndy: 0, girdeji: 0, cykdajy: 0);
|
|
}
|
|
|
|
$basdakyGalyndy = $data->transactions[0]->balsum;
|
|
$basdakyTransaksiya = $data->transactions[0]->opersum;
|
|
$sonkyGalyndy = $data->transactions[count($data->transactions) - 1]->balsum;
|
|
|
|
$positive = 0;
|
|
$negative = 0;
|
|
for ($i = 0; $i < count($data->transactions); $i++) {
|
|
info([
|
|
'balsum' => $data->transactions[$i]->balsum,
|
|
'gettype' => $data->transactions[$i]->balsum,
|
|
'check_1' => $data->transactions[$i]->balsum > 0,
|
|
'check_2' => abs($data->transactions[$i]->balsum) === $data->transactions[$i]->balsum
|
|
]);
|
|
|
|
if ($data->transactions[$i]->balsum > 0) {
|
|
$positive += $data->transactions[$i]->balsum;
|
|
} else {
|
|
$negative += $data->transactions[$i]->balsum;
|
|
}
|
|
}
|
|
|
|
return emptyClass(
|
|
basdakyGalyndy: $basdakyGalyndy - ($basdakyTransaksiya),
|
|
ahyrkyGalyndy: $sonkyGalyndy,
|
|
girdeji: $positive,
|
|
cykdajy: $negative
|
|
);
|
|
}
|
|
}
|