birzha-legalizasia/app/Http/Controllers/API/ApplicationController.php

190 lines
5.6 KiB
PHP
Raw Normal View History

2022-08-08 06:32:22 +00:00
<?php
namespace App\Http\Controllers\API;
2022-09-02 10:19:03 +00:00
use App\Http\Controllers\Controller;
2022-09-08 06:45:42 +00:00
use App\Http\Requests\API\DocumentUploadRequest;
2022-09-02 10:19:03 +00:00
use App\Http\Resources\ApplicationResource;
use App\Models\Application;
use App\Models\Attachment;
use App\Models\Documentgroup;
2022-09-08 06:45:42 +00:00
use Carbon\Carbon;
2022-09-12 10:59:49 +00:00
use Illuminate\Support\Facades\Auth;
2022-09-14 11:30:05 +00:00
use Illuminate\Support\Facades\Log;
2022-09-08 06:45:42 +00:00
use Illuminate\Support\Facades\Storage;
2022-09-14 12:47:45 +00:00
use Illuminate\Support\Str;
2022-09-02 10:19:03 +00:00
2022-08-08 06:32:22 +00:00
class ApplicationController extends Controller
{
2022-09-12 10:59:49 +00:00
public function __construct()
{
$this->middleware(function ($request, $next) {
$this->account = Auth::user()
->account()
->with('profile')
->first();
return $next($request);
});
}
2022-09-08 06:45:42 +00:00
/**
* Create new Legalization Application
* @return ApplicationResource|\Illuminate\Http\Response
*/
public function create()
{
//validate if profile is filled
if(is_null($this->account) || is_null($this->account->profile))
{
return response([
'success' => false,
'message' => 'Please fill your profile information first!'
],400);
}
//validate legalization number exists
elseif( !empty($this->account->legalization_number) || //legalizasia nomeri bar bolsa
(!empty( $this->account->expires_at) && //legalizasia srogy
$this->account->expires_at->lte(Carbon::now()->subMonth()))){//eger srogyn gutarmagyna 1 ay galmadyk bolsa
2022-09-02 10:19:03 +00:00
2022-09-08 06:45:42 +00:00
return response([
'success' => false,
'message' => 'Dine Legalizasia nomerinizin mohletinin gutarmagyna 1 ay wagt galanda arza doredip bilyaniz'
],400);
}
2022-09-02 10:19:03 +00:00
2022-09-14 12:47:45 +00:00
2022-09-08 06:45:42 +00:00
//upload etmeli dokumentlaryn spisogy
2022-09-14 11:53:55 +00:00
$docGroup = Documentgroup::withDocs($this->account->type,$this->account->country_id)->first();
2022-09-02 10:19:03 +00:00
2022-09-14 11:30:05 +00:00
2022-09-08 06:45:42 +00:00
if(!$docGroup || $docGroup->documents->count() == 0)
{
2022-09-02 10:19:03 +00:00
return response([
'success' => false,
'message' => 'Required documents list was not found'
],400);
}
2022-09-12 09:34:02 +00:00
//delete old application??? Should we replace it???
2022-09-16 09:57:49 +00:00
if($app = $this->account->last_application())
2022-09-12 09:34:02 +00:00
{
2022-09-16 09:57:49 +00:00
if($app->state != 'approved')
return ApplicationResource::make($app);
else{
$app->state = 'archive';
$app->save();
}
2022-09-12 09:34:02 +00:00
}
2022-09-02 10:19:03 +00:00
//todo create attachments here
$application = Application::create([
2022-09-08 06:45:42 +00:00
'account_id' => $this->account->id,
2022-09-12 09:58:09 +00:00
// 'state' => 'new' //default mysql value is new
2022-09-02 10:19:03 +00:00
]);
foreach ($docGroup->documents as $document){
$attachment = new Attachment([
'name' => $document->name,
'document_id' => $document->id
]);
2022-09-14 11:59:19 +00:00
$application->attachments()->save($attachment);
2022-09-02 10:19:03 +00:00
}
return ApplicationResource::make($application);
2022-08-08 06:32:22 +00:00
2022-08-09 09:26:02 +00:00
}
2022-09-02 10:19:03 +00:00
2022-09-08 06:45:42 +00:00
/**
* Get accounts legalization application
* @return ApplicationResource
*/
2022-09-02 10:19:03 +00:00
public function get()
{
2022-09-16 09:43:43 +00:00
if($appication = $this->account->applications()->latest()->with('attachments')->first()){
return ApplicationResource::make($appication);
}
return response()->json(['success' => false,'message' =>'Not Found'],404);
2022-09-02 10:19:03 +00:00
}
public function apply(){
2022-09-14 13:01:26 +00:00
if($app = $this->account->aplication && $app->state == 'new')
2022-09-08 06:45:42 +00:00
{
2022-09-14 13:01:26 +00:00
//todo validate all documents are uploaded
$app->state = 'applied';
$app->save();
return response([
'success' => true,
'message' => 'Application successful'
],400);
2022-09-02 10:19:03 +00:00
2022-09-08 06:45:42 +00:00
}
2022-09-02 10:19:03 +00:00
2022-09-08 06:45:42 +00:00
return response([
'success' => false,
2022-09-14 13:01:26 +00:00
'message' => 'Application unsuccessful'
2022-09-08 06:45:42 +00:00
],400);
2022-09-02 10:19:03 +00:00
}
2022-09-15 08:10:01 +00:00
public function upload(DocumentUploadRequest $request,$attachment_id)
2022-09-08 06:45:42 +00:00
{
2022-09-15 08:10:01 +00:00
2022-09-15 07:54:32 +00:00
$attachment = Attachment::with(['application','document'])->find($attachment_id);
2022-09-14 12:47:45 +00:00
2022-09-15 08:30:40 +00:00
if(!$attachment || !$attachment->application || $attachment->application->account_id != $this->account->id ){
2022-09-14 12:47:45 +00:00
return response()->json(['success' => false, 'message' =>'Bad request'],400);
}
2022-09-08 06:45:42 +00:00
$uploadedFile = $request->file('file');
2022-09-15 09:21:24 +00:00
// Log::info($uploadedFile->getSize());
2022-09-15 08:36:31 +00:00
2022-09-14 12:47:45 +00:00
if($attachment->document->max_size != 0
2022-09-15 08:36:31 +00:00
&& $uploadedFile->getSize() > $attachment->document->max_size * 1024){//max size in kilobytes
2022-09-14 12:47:45 +00:00
return response()->json(['success' => false, 'message' =>'Max size exceeded'],400);
}
2022-09-14 13:13:16 +00:00
if($attachment->file){
//todo delete or replace old file
// Stor
}
2022-09-15 09:21:24 +00:00
$filename = Str::snake($attachment->name).'.'.$uploadedFile->getClientOriginalExtension();
$directory = 'documents/'.Carbon::today()->year.'/'.$this->account->id;
// $path = Storage::disk('local')->putFileAs(
// $directory,
// $uploadedFile,
// $filename
// );
2022-09-14 12:47:45 +00:00
2022-09-15 09:34:20 +00:00
$path = $uploadedFile->storePubliclyAs($directory,$filename);
2022-09-08 06:45:42 +00:00
2022-09-15 09:34:20 +00:00
if(!$path){
2022-09-14 12:47:45 +00:00
return response()->json(['success' => false, 'message' =>'Failed upload'],400);
}
2022-09-15 09:34:20 +00:00
$attachment->file = $directory.'/'.$filename;
$attachment->size = number_format($uploadedFile->getSize()/1024, 2, '.', '');
2022-09-14 12:47:45 +00:00
$attachment->type = $uploadedFile->getClientOriginalExtension();
$attachment->save();
return response()->json(['success' => true, 'message' =>'Uploaded successfully']);
2022-09-08 06:45:42 +00:00
//todo make attachment relation
}
2022-09-12 09:58:09 +00:00
public function downloadQuestionaire(){
2022-09-16 09:43:43 +00:00
//todo generate pdf
2022-09-12 09:58:09 +00:00
}
2022-08-08 06:32:22 +00:00
}