2023-03-16 09:44:51 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\API;
|
|
|
|
|
use App\Http\Requests\API\DocumentUploadRequest;
|
|
|
|
|
use App\Http\Resources\BrokerApplicationResource;
|
|
|
|
|
use App\Models\BrokerApplication;
|
|
|
|
|
use App\Models\BrokerAttachment;
|
|
|
|
|
use App\Models\BrokerDocument;
|
|
|
|
|
use Carbon\Carbon;
|
|
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
|
use Illuminate\Support\Facades\Config;
|
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
|
|
|
|
|
class BrokerApplicationController extends Controller
|
|
|
|
|
{
|
|
|
|
|
public function __construct()
|
|
|
|
|
{
|
|
|
|
|
$this->middleware(function ($request, $next) {
|
|
|
|
|
$this->account = auth()->guard('api')->user()
|
|
|
|
|
->account()
|
|
|
|
|
->with('profile')
|
|
|
|
|
->with('broker_applications')
|
|
|
|
|
->first();
|
|
|
|
|
return $next($request);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create new Legalization Application
|
|
|
|
|
* @return BrokerApplicationResource|\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.application.fill_profile_message'),
|
|
|
|
|
],422);
|
|
|
|
|
}
|
|
|
|
|
//validate legalization number exists
|
|
|
|
|
elseif(!$this->account->can_apply_broker && !$this->account->can_extend_broker){
|
|
|
|
|
$month = Config::get('settings.legalization_extend') ?? 1;
|
|
|
|
|
return response([
|
|
|
|
|
'success' => false,
|
|
|
|
|
'message' => trans('app.application.expire_message',['one'=>$month])
|
|
|
|
|
],422);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//delete old application??? Should we replace it???
|
|
|
|
|
if($app = $this->account->last_broker_application())
|
|
|
|
|
{
|
|
|
|
|
if($app->state != 'approved')
|
|
|
|
|
return BrokerApplicationResource::make($app);
|
|
|
|
|
else{
|
|
|
|
|
$app->state = 'archive';
|
|
|
|
|
$app->save();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//upload etmeli dokumentlaryn spisogy
|
|
|
|
|
$documents = BrokerDocument::where($this->account->type,true)->get();
|
|
|
|
|
|
|
|
|
|
if($documents->count() == 0)
|
|
|
|
|
{
|
|
|
|
|
return response([
|
|
|
|
|
'success' => false,
|
|
|
|
|
'message' => trans('application.list_not_found')
|
|
|
|
|
],404);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$application = BrokerApplication::create([
|
2023-03-29 23:17:29 +00:00
|
|
|
'account_id' => $this->account->id,
|
|
|
|
|
'state' => 'draft', //default mysql value is new
|
2023-03-30 10:01:10 +00:00
|
|
|
'is_local' => $this->account->country->code == "TM" ? true : false
|
2023-03-16 09:44:51 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
foreach ($documents as $document){
|
|
|
|
|
$attachment = new BrokerAttachment([
|
2023-03-29 23:17:29 +00:00
|
|
|
'name' => $document->name,
|
|
|
|
|
'broker_document_id' => $document->id
|
2023-03-16 09:44:51 +00:00
|
|
|
]);
|
|
|
|
|
$application->broker_attachments()->save($attachment);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return BrokerApplicationResource::make($application);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function get()
|
|
|
|
|
{
|
|
|
|
|
$application = $this->account
|
|
|
|
|
->broker_applications()
|
|
|
|
|
->latest()
|
|
|
|
|
->with('broker_attachments')->first();
|
|
|
|
|
|
|
|
|
|
if($application){
|
|
|
|
|
return BrokerApplicationResource::make($application);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return response()->json(['success' => false, 'message' => 'Not Found'], 404);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function apply()
|
|
|
|
|
{
|
|
|
|
|
$app = $this->account
|
|
|
|
|
->broker_applications()
|
|
|
|
|
->whereIn('state',['draft','refine'])
|
|
|
|
|
->with('broker_attachments')
|
|
|
|
|
->latest()
|
|
|
|
|
->first();
|
|
|
|
|
|
|
|
|
|
if( $app )
|
|
|
|
|
{
|
|
|
|
|
$app->state = 'new';
|
|
|
|
|
$app->save();
|
|
|
|
|
|
|
|
|
|
//todo send email to operators
|
|
|
|
|
return response([
|
|
|
|
|
'success' => true,
|
|
|
|
|
'message' => trans('app.application.app_success_message')
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return response([
|
|
|
|
|
'success' => false,
|
|
|
|
|
'message' => trans('app.application.app_error_message')
|
|
|
|
|
],400);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function upload(DocumentUploadRequest $request,$attachment_id)
|
|
|
|
|
{
|
|
|
|
|
|
2023-03-28 14:31:42 +00:00
|
|
|
$broker_attachment = BrokerAttachment::with(['broker_application','broker_document'])->find($attachment_id);
|
2023-03-16 09:44:51 +00:00
|
|
|
|
2023-03-28 14:31:42 +00:00
|
|
|
if(!$broker_attachment || !$broker_attachment->broker_application || $broker_attachment->broker_application->account_id != $this->account->id ){
|
2023-03-16 09:44:51 +00:00
|
|
|
|
|
|
|
|
return response()->json(['success' => false, 'message' =>'Bad request'],400);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$uploadedFile = $request->file('file');
|
|
|
|
|
|
|
|
|
|
|
2023-03-28 14:31:42 +00:00
|
|
|
if($broker_attachment->broker_document->max_size != 0
|
|
|
|
|
&& $uploadedFile->getSize() > $broker_attachment->broker_document->max_size * 1024){//max size in kilobytes
|
2023-03-16 09:44:51 +00:00
|
|
|
return response()->json(['success' => false, 'message' =>trans('app.application.upload_max_size')],422);
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-28 14:31:42 +00:00
|
|
|
$filename = Str::snake($broker_attachment->name).'.'.$uploadedFile->getClientOriginalExtension();
|
2023-03-16 09:44:51 +00:00
|
|
|
|
2023-03-28 14:07:51 +00:00
|
|
|
$directory = 'broker-documents/'.Carbon::today()->year.'/'.$this->account->id;
|
2023-03-16 09:44:51 +00:00
|
|
|
|
|
|
|
|
$path = $uploadedFile->storePubliclyAs($directory,$filename);
|
|
|
|
|
|
|
|
|
|
if(!$path){
|
|
|
|
|
return response()->json(['success' => false, 'message' =>trans('app.application.upload_failure')],400);
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-28 14:31:42 +00:00
|
|
|
$broker_attachment->file = $directory.'/'.$filename;
|
|
|
|
|
$broker_attachment->size = number_format($uploadedFile->getSize()/1024, 2, '.', '');
|
|
|
|
|
$broker_attachment->type = $uploadedFile->getClientOriginalExtension();
|
|
|
|
|
$broker_attachment->save();
|
2023-03-16 09:44:51 +00:00
|
|
|
|
|
|
|
|
return response()->json(['success' => true, 'message' =>trans('app.app.application.upload_success')]);
|
|
|
|
|
|
|
|
|
|
//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);
|
|
|
|
|
}
|
|
|
|
|
}
|