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

216 lines
6.4 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\Document;
2022-09-08 06:45:42 +00:00
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder;
2022-09-12 10:59:49 +00:00
use Illuminate\Support\Facades\Auth;
2022-12-07 08:44:13 +00:00
use Illuminate\Support\Facades\Config;
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,
2022-10-04 11:30:07 +00:00
'message' => trans('app.application.fill_profile_message'),
2022-11-03 08:08:06 +00:00
],422);
2022-09-08 06:45:42 +00:00
}
//validate legalization number exists
2022-12-20 12:10:10 +00:00
elseif(!$this->account->can_apply_legal_number && !$this->account->can_extend_legal_number){
2022-12-07 08:44:13 +00:00
$month = Config::get('settings.legalization_extend') ?? 1;
2022-09-08 06:45:42 +00:00
return response([
'success' => false,
2022-12-07 08:44:13 +00:00
'message' => trans('app.application.expire_message',['one'=>$month])
2022-11-03 08:08:06 +00:00
],422);
2022-09-08 06:45:42 +00:00
}
2022-09-27 06:27:25 +00:00
//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();
}
}
2022-09-02 10:19:03 +00:00
2022-09-27 06:27:25 +00:00
//upload etmeli dokumentlaryn spisogy
$documents = Document::where($this->account->type,true)
2022-09-29 11:40:47 +00:00
->where(fn($query) => $query->where('all_country',true)
->orWhereHas('countries',function(Builder $query)
{
$query->where('countries.id',$this->account->country_id);
}))
->get();
2022-09-14 11:30:05 +00:00
if($documents->count() == 0)
2022-09-08 06:45:42 +00:00
{
2022-09-02 10:19:03 +00:00
return response([
'success' => false,
2022-10-04 11:17:35 +00:00
'message' => trans('application.list_not_found')
2022-11-03 08:08:06 +00:00
],404);
2022-09-02 10:19:03 +00:00
}
2022-09-12 09:34:02 +00:00
2022-09-02 10:19:03 +00:00
$application = Application::create([
2022-09-08 06:45:42 +00:00
'account_id' => $this->account->id,
2022-11-19 05:31:05 +00:00
'state' => 'draft' //default mysql value is new
2022-09-02 10:19:03 +00:00
]);
2022-09-26 08:10:02 +00:00
foreach ($documents as $document){
2022-09-02 10:19:03 +00:00
$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-29 08:41:36 +00:00
$appication = $this->account
->applications()
->latest()
->with('attachments')
->first();
if($appication){
2022-09-16 09:43:43 +00:00
return ApplicationResource::make($appication);
}
return response()->json(['success' => false,'message' =>'Not Found'],404);
2022-09-02 10:19:03 +00:00
}
2022-09-19 07:36:14 +00:00
public function apply()
{
2022-09-20 06:21:50 +00:00
$app = $this->account
->applications()
2022-11-19 05:31:05 +00:00
->whereIn('state',['draft','refine'])
2022-09-20 06:21:50 +00:00
->with('attachments')
->latest()
->first();
2022-09-20 06:24:57 +00:00
if( $app )
2022-09-08 06:45:42 +00:00
{
// $unAttachedDocumentsCount = $app->attachments->whereNull('file')->count();
//
// if($unAttachedDocumentsCount >0)
// {
// return response([
// 'success' => false,
// 'message' => trans('app.application.required_docs_message')
// ],422);
// }
2022-09-14 13:01:26 +00:00
2022-11-19 05:31:05 +00:00
$app->state = 'new';
2022-09-14 13:01:26 +00:00
$app->save();
2022-09-26 11:05:01 +00:00
//todo send email to operators
2022-09-14 13:01:26 +00:00
return response([
'success' => true,
2022-10-04 11:30:07 +00:00
'message' => trans('app.application.app_success_message')
2022-09-17 10:15:17 +00:00
]);
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-10-04 11:30:07 +00:00
'message' => trans('app.application.app_error_message')
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-10-04 11:30:07 +00:00
return response()->json(['success' => false, 'message' =>trans('app.application.upload_max_size')],422);
2022-09-14 12:47:45 +00:00
}
2022-11-19 06:06:49 +00:00
// if($attachment->file){
// //todo delete or replace old file
//// Stor
// }
2022-09-14 13:13:16 +00:00
2022-09-15 09:21:24 +00:00
$filename = Str::snake($attachment->name).'.'.$uploadedFile->getClientOriginalExtension();
2022-09-17 10:15:17 +00:00
2022-09-15 09:21:24 +00:00
$directory = 'documents/'.Carbon::today()->year.'/'.$this->account->id;
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-10-04 11:30:07 +00:00
return response()->json(['success' => false, 'message' =>trans('app.application.upload_failure')],400);
2022-09-14 12:47:45 +00:00
}
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();
2022-10-04 11:30:07 +00:00
return response()->json(['success' => true, 'message' =>trans('app.app.application.upload_success')]);
2022-09-14 12:47:45 +00:00
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 11:15:06 +00:00
$headers = [
"Content-type"=>"text/html",
"Content-Disposition"=>"attachment;Filename=myGeneratefile.doc"
];
2022-09-17 10:15:17 +00:00
$content = view('oprosniki.'.$this->account->type,$this->account->profile)->render();
2022-09-16 11:15:06 +00:00
return \Response::make($content,200, $headers);
2022-09-12 09:58:09 +00:00
}
2022-08-08 06:32:22 +00:00
}