212 lines
6.4 KiB
PHP
212 lines
6.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\API;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\API\DocumentUploadRequest;
|
|
use App\Http\Resources\ApplicationResource;
|
|
use App\Models\Application;
|
|
use App\Models\Attachment;
|
|
use App\Models\Documentgroup;
|
|
use App\Models\Document;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Str;
|
|
|
|
class ApplicationController extends Controller
|
|
{
|
|
public function __construct()
|
|
{
|
|
$this->middleware(function ($request, $next) {
|
|
|
|
$this->account = Auth::user()
|
|
->account()
|
|
->with('profile')
|
|
->first();
|
|
return $next($request);
|
|
});
|
|
|
|
}
|
|
/**
|
|
* 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' => trans('app.app.application.fill_profile_message'),
|
|
],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
|
|
|
|
return response([
|
|
'success' => false,
|
|
'message' => trans('app.application.expire_message')
|
|
],400);
|
|
}
|
|
|
|
//upload etmeli dokumentlaryn spisogy
|
|
//$docGroup = Documentgroup::withDocs($this->account->type,$this->account->country_id)->first();
|
|
|
|
$documents = Document::where($this->account->type,true)
|
|
->where('all_country',true)
|
|
->orWhereHas('countries',function(Builder $query) {
|
|
$query->where('countries.id',$this->account->country_id);
|
|
})->get();
|
|
|
|
if($documents->count() == 0)
|
|
{
|
|
return response([
|
|
'success' => false,
|
|
'message' => trans('app.application.list_not_found')
|
|
],400);
|
|
}
|
|
|
|
//delete old application??? Should we replace it???
|
|
if($app = $this->account->last_application())
|
|
{
|
|
if($app->state != 'approved')
|
|
return ApplicationResource::make($app);
|
|
else{
|
|
$app->state = 'archive';
|
|
$app->save();
|
|
}
|
|
}
|
|
|
|
//todo create attachments here
|
|
$application = Application::create([
|
|
'account_id' => $this->account->id,
|
|
// 'state' => 'new' //default mysql value is new
|
|
]);
|
|
|
|
foreach ($documents as $document){
|
|
$attachment = new Attachment([
|
|
'name' => $document->name,
|
|
'document_id' => $document->id
|
|
]);
|
|
$application->attachments()->save($attachment);
|
|
}
|
|
|
|
return ApplicationResource::make($application);
|
|
|
|
}
|
|
|
|
/**
|
|
* Get accounts legalization application
|
|
* @return ApplicationResource
|
|
*/
|
|
public function get()
|
|
{
|
|
if($appication = $this->account->applications()->latest()->with('attachments')->first()){
|
|
|
|
return ApplicationResource::make($appication);
|
|
}
|
|
|
|
return response()->json(['success' => false,'message' =>'Not Found'],404);
|
|
|
|
}
|
|
|
|
public function apply()
|
|
{
|
|
$app = $this->account
|
|
->applications()
|
|
->where('state','new')
|
|
->with('attachments')
|
|
->latest()
|
|
->first();
|
|
|
|
if( $app )
|
|
{
|
|
$unAttachedDocumentsCount = $app->attachments->whereNull('file')->count();
|
|
|
|
if($unAttachedDocumentsCount >0)
|
|
{
|
|
return response([
|
|
'success' => false,
|
|
'message' => trans('app.app.application.required_docs_message')
|
|
],422);
|
|
}
|
|
|
|
$app->state = 'applied';
|
|
$app->save();
|
|
|
|
//todo send email to operators
|
|
return response([
|
|
'success' => true,
|
|
'message' => trans('app.app.application.app_success_message')
|
|
]);
|
|
|
|
}
|
|
|
|
return response([
|
|
'success' => false,
|
|
'message' => trans('app.application.app_error_message')
|
|
],400);
|
|
|
|
}
|
|
|
|
public function upload(DocumentUploadRequest $request,$attachment_id)
|
|
{
|
|
|
|
$attachment = Attachment::with(['application','document'])->find($attachment_id);
|
|
|
|
if(!$attachment || !$attachment->application || $attachment->application->account_id != $this->account->id ){
|
|
|
|
return response()->json(['success' => false, 'message' =>'Bad request'],400);
|
|
}
|
|
|
|
$uploadedFile = $request->file('file');
|
|
|
|
// Log::info($uploadedFile->getSize());
|
|
|
|
if($attachment->document->max_size != 0
|
|
&& $uploadedFile->getSize() > $attachment->document->max_size * 1024){//max size in kilobytes
|
|
return response()->json(['success' => false, 'message' =>'Max size exceeded'],400);
|
|
}
|
|
|
|
if($attachment->file){
|
|
//todo delete or replace old file
|
|
// Stor
|
|
}
|
|
|
|
$filename = Str::snake($attachment->name).'.'.$uploadedFile->getClientOriginalExtension();
|
|
|
|
$directory = 'documents/'.Carbon::today()->year.'/'.$this->account->id;
|
|
|
|
$path = $uploadedFile->storePubliclyAs($directory,$filename);
|
|
|
|
if(!$path){
|
|
return response()->json(['success' => false, 'message' =>'Failed upload'],400);
|
|
}
|
|
|
|
$attachment->file = $directory.'/'.$filename;
|
|
$attachment->size = number_format($uploadedFile->getSize()/1024, 2, '.', '');
|
|
$attachment->type = $uploadedFile->getClientOriginalExtension();
|
|
$attachment->save();
|
|
|
|
return response()->json(['success' => true, 'message' =>'Uploaded successfully']);
|
|
|
|
//todo make attachment relation
|
|
}
|
|
|
|
public function downloadQuestionaire(){
|
|
$headers = [
|
|
"Content-type"=>"text/html",
|
|
"Content-Disposition"=>"attachment;Filename=myGeneratefile.doc"
|
|
];
|
|
|
|
$content = view('oprosniki.'.$this->account->type,$this->account->profile)->render();
|
|
|
|
return \Response::make($content,200, $headers);
|
|
}
|
|
|
|
}
|