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

172 lines
5.0 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-14 12:47:45 +00:00
if($app = $this->account->last_application() && $app->state != 'approved')
2022-09-12 09:34:02 +00:00
{
2022-09-14 12:47:45 +00:00
return ApplicationResource::make($app);
}else{
2022-09-12 09:58:09 +00:00
$app->state = 'archive';
2022-09-12 09:34:02 +00:00
$app->save();
}
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-14 11:45:51 +00:00
$appication = $this->account
->applications()
->latest()
2022-09-02 10:19:03 +00:00
->with('attachments')
->first();
return ApplicationResource::make($appication);
}
public function apply(){
2022-09-08 06:45:42 +00:00
if($this->account->aplication)
{
//validate all documents are uploaded
2022-09-02 10:19:03 +00:00
2022-09-08 06:45:42 +00:00
//todo apply
}
2022-09-02 10:19:03 +00:00
2022-09-08 06:45:42 +00:00
return response([
'success' => false,
'message' => 'Required documents list was not found'
],400);
2022-09-02 10:19:03 +00:00
}
2022-09-08 06:45:42 +00:00
public function upload(DocumentUploadRequest $request)
{
//todo size validation
2022-09-14 12:47:45 +00:00
$attachment = Attachment::with(['application','document'])->find($request->get('attachment_id'));
if(!$attachment || $attachment->application || $attachment->application->account_id != $this->account->id ){
return response()->json(['success' => false, 'message' =>'Bad request'],400);
}
2022-09-08 06:45:42 +00:00
$uploadedFile = $request->file('file');
2022-09-14 12:47:45 +00:00
if($attachment->document->max_size != 0
&& $uploadedFile->getSize() > $attachment->document->max_size * 1024){
return response()->json(['success' => false, 'message' =>'Max size exceeded'],400);
}
$filename = Str::snake($attachment->name).$uploadedFile->getClientOriginalExtension();
$path = Storage::disk('local')->putFileAs(
'documents/'.Carbon::today()->year.'/'.$this->account->id.'/'.$filename,
2022-09-08 06:45:42 +00:00
$uploadedFile,
$filename
);
2022-09-14 12:47:45 +00:00
if(!$path){
return response()->json(['success' => false, 'message' =>'Failed upload'],400);
}
$attachment->file = $path;
$attachment->size = $uploadedFile->getSize();
$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-08-08 06:32:22 +00:00
}